libsodium_crypto_ipcrypt (libsodium v3.0.1)
View SourceIP address encryption (ipcrypt) for pseudonymizing addresses in logs and
telemetry. Every mode works on the 16-byte IPv6 representation of an
address, in which IPv4 addresses are IPv4-mapped; use
libsodium_utils:ip2bin/1 and libsodium_utils:bin2ip/1 to convert
between textual addresses and these binaries.
The deterministic mode (encrypt/2) maps an address to another 16-byte
address, so equal inputs give equal outputs. The non-deterministic modes
add a random tweak and produce longer outputs that hide repeated
addresses: nd_encrypt/2 (8-byte tweak, 24-byte output) and
ndx_encrypt/2 (16-byte tweak, 32-byte output, a larger tweak space for
more encryptions per key). The prefix-preserving mode (pfx_encrypt/2)
keeps addresses that share a network prefix sharing an encrypted prefix,
and keeps IPv4 addresses IPv4.
Each mode has its own key size and keygen function. Encryption hides
addresses but does not authenticate them: decryption never fails.
K = libsodium_crypto_ipcrypt:keygen(),
Ip = libsodium_utils:ip2bin(<<"192.0.2.1">>),
Encrypted = libsodium_crypto_ipcrypt:encrypt(Ip, K),
Encrypted = libsodium_crypto_ipcrypt:encrypt(Ip, K),
<<"192.0.2.1">> = libsodium_utils:bin2ip(libsodium_crypto_ipcrypt:decrypt(Encrypted, K)),
NdK = libsodium_crypto_ipcrypt:nd_keygen(),
Nd = libsodium_crypto_ipcrypt:nd_encrypt(Ip, NdK),
24 = byte_size(Nd),
Ip = libsodium_crypto_ipcrypt:nd_decrypt(Nd, NdK).
Summary
Types
A crypto_ipcrypt_BYTES (16-byte) binary.
A crypto_ipcrypt_KEYBYTES (16-byte) binary.
A crypto_ipcrypt_ND_OUTPUTBYTES (24-byte) binary.
A crypto_ipcrypt_ND_KEYBYTES (16-byte) binary.
A crypto_ipcrypt_ND_TWEAKBYTES (8-byte) binary.
A crypto_ipcrypt_NDX_OUTPUTBYTES (32-byte) binary.
A crypto_ipcrypt_NDX_KEYBYTES (32-byte) binary.
A crypto_ipcrypt_NDX_TWEAKBYTES (16-byte) binary.
A crypto_ipcrypt_PFX_KEYBYTES (32-byte) binary.
Functions
Return crypto_ipcrypt_bytes(), in bytes.
Deterministically encrypt the bytes/0 byte address In with the
keybytes/0 byte key K, returning another bytes/0 byte address.
Return crypto_ipcrypt_keybytes(), in bytes.
Generate a random keybytes/0 byte key for encrypt/2.
Decrypt the nd_outputbytes/0 byte output In of nd_encrypt/2 with
the key K, returning the nd_inputbytes/0 byte address.
Encrypt the nd_inputbytes/0 byte address In with the
nd_keybytes/0 byte key K and a fresh random tweak. Returns
nd_outputbytes/0 bytes: the tweak followed by the encrypted
address.
Encrypt the nd_inputbytes/0 byte address In with the
nd_keybytes/0 byte key K and the nd_tweakbytes/0 byte tweak T.
Returns nd_outputbytes/0 bytes: the tweak followed by the encrypted
address. The tweak must be random and should not repeat for a key.
Return crypto_ipcrypt_nd_inputbytes(), in bytes.
Return crypto_ipcrypt_nd_keybytes(), in bytes.
Generate a random nd_keybytes/0 byte key for nd_encrypt/2.
Return crypto_ipcrypt_nd_outputbytes(), in bytes.
Return crypto_ipcrypt_nd_tweakbytes(), in bytes.
Decrypt the ndx_outputbytes/0 byte output In of ndx_encrypt/2
with the key K, returning the ndx_inputbytes/0 byte address.
Encrypt the ndx_inputbytes/0 byte address In with the
ndx_keybytes/0 byte key K and a fresh random tweak. Returns
ndx_outputbytes/0 bytes: the tweak followed by the encrypted
address.
Encrypt the ndx_inputbytes/0 byte address In with the
ndx_keybytes/0 byte key K and the ndx_tweakbytes/0 byte tweak
T. Returns ndx_outputbytes/0 bytes: the tweak followed by the
encrypted address. The tweak must be random and should not repeat for
a key.
Return crypto_ipcrypt_ndx_inputbytes(), in bytes.
Return crypto_ipcrypt_ndx_keybytes(), in bytes.
Generate a random ndx_keybytes/0 byte key for ndx_encrypt/2.
Return crypto_ipcrypt_ndx_outputbytes(), in bytes.
Return crypto_ipcrypt_ndx_tweakbytes(), in bytes.
Return crypto_ipcrypt_pfx_bytes(), in bytes.
Decrypt the pfx_bytes/0 byte address In produced by pfx_encrypt/2 with the key K.
Encrypt the pfx_bytes/0 byte address In with the pfx_keybytes/0
byte key K, preserving prefixes: addresses that share their first
bits share the first bits of their encryptions, and IPv4 addresses
stay IPv4-mapped.
Return crypto_ipcrypt_pfx_keybytes(), in bytes.
Generate a random pfx_keybytes/0 byte key for pfx_encrypt/2.
Types
-type ip() :: <<_:128>>.
A crypto_ipcrypt_BYTES (16-byte) binary.
-type key() :: <<_:128>>.
A crypto_ipcrypt_KEYBYTES (16-byte) binary.
-type nd_ciphertext() :: <<_:192>>.
A crypto_ipcrypt_ND_OUTPUTBYTES (24-byte) binary.
-type nd_key() :: <<_:128>>.
A crypto_ipcrypt_ND_KEYBYTES (16-byte) binary.
-type nd_tweak() :: <<_:64>>.
A crypto_ipcrypt_ND_TWEAKBYTES (8-byte) binary.
-type ndx_ciphertext() :: <<_:256>>.
A crypto_ipcrypt_NDX_OUTPUTBYTES (32-byte) binary.
-type ndx_key() :: <<_:256>>.
A crypto_ipcrypt_NDX_KEYBYTES (32-byte) binary.
-type ndx_tweak() :: <<_:128>>.
A crypto_ipcrypt_NDX_TWEAKBYTES (16-byte) binary.
-type pfx_key() :: <<_:256>>.
A crypto_ipcrypt_PFX_KEYBYTES (32-byte) binary.
Functions
-spec bytes() -> Result when Result :: 16.
Return crypto_ipcrypt_bytes(), in bytes.
Decrypt the bytes/0 byte address In produced by encrypt/2 with the key K.
Deterministically encrypt the bytes/0 byte address In with the
keybytes/0 byte key K, returning another bytes/0 byte address.
-spec keybytes() -> Result when Result :: 16.
Return crypto_ipcrypt_keybytes(), in bytes.
-spec keygen() -> Result when Result :: key().
Generate a random keybytes/0 byte key for encrypt/2.
-spec nd_decrypt(In, K) -> Result when In :: nd_ciphertext(), K :: nd_key(), Result :: ip().
Decrypt the nd_outputbytes/0 byte output In of nd_encrypt/2 with
the key K, returning the nd_inputbytes/0 byte address.
-spec nd_encrypt(In, K) -> Result when In :: ip(), K :: nd_key(), Result :: nd_ciphertext().
Encrypt the nd_inputbytes/0 byte address In with the
nd_keybytes/0 byte key K and a fresh random tweak. Returns
nd_outputbytes/0 bytes: the tweak followed by the encrypted
address.
-spec nd_encrypt(In, T, K) -> Result when In :: ip(), T :: nd_tweak(), K :: nd_key(), Result :: nd_ciphertext().
Encrypt the nd_inputbytes/0 byte address In with the
nd_keybytes/0 byte key K and the nd_tweakbytes/0 byte tweak T.
Returns nd_outputbytes/0 bytes: the tweak followed by the encrypted
address. The tweak must be random and should not repeat for a key.
-spec nd_inputbytes() -> Result when Result :: 16.
Return crypto_ipcrypt_nd_inputbytes(), in bytes.
-spec nd_keybytes() -> Result when Result :: 16.
Return crypto_ipcrypt_nd_keybytes(), in bytes.
-spec nd_keygen() -> Result when Result :: nd_key().
Generate a random nd_keybytes/0 byte key for nd_encrypt/2.
-spec nd_outputbytes() -> Result when Result :: 24.
Return crypto_ipcrypt_nd_outputbytes(), in bytes.
-spec nd_tweakbytes() -> Result when Result :: 8.
Return crypto_ipcrypt_nd_tweakbytes(), in bytes.
-spec ndx_decrypt(In, K) -> Result when In :: ndx_ciphertext(), K :: ndx_key(), Result :: ip().
Decrypt the ndx_outputbytes/0 byte output In of ndx_encrypt/2
with the key K, returning the ndx_inputbytes/0 byte address.
-spec ndx_encrypt(In, K) -> Result when In :: ip(), K :: ndx_key(), Result :: ndx_ciphertext().
Encrypt the ndx_inputbytes/0 byte address In with the
ndx_keybytes/0 byte key K and a fresh random tweak. Returns
ndx_outputbytes/0 bytes: the tweak followed by the encrypted
address.
-spec ndx_encrypt(In, T, K) -> Result when In :: ip(), T :: ndx_tweak(), K :: ndx_key(), Result :: ndx_ciphertext().
Encrypt the ndx_inputbytes/0 byte address In with the
ndx_keybytes/0 byte key K and the ndx_tweakbytes/0 byte tweak
T. Returns ndx_outputbytes/0 bytes: the tweak followed by the
encrypted address. The tweak must be random and should not repeat for
a key.
-spec ndx_inputbytes() -> Result when Result :: 16.
Return crypto_ipcrypt_ndx_inputbytes(), in bytes.
-spec ndx_keybytes() -> Result when Result :: 32.
Return crypto_ipcrypt_ndx_keybytes(), in bytes.
-spec ndx_keygen() -> Result when Result :: ndx_key().
Generate a random ndx_keybytes/0 byte key for ndx_encrypt/2.
-spec ndx_outputbytes() -> Result when Result :: 32.
Return crypto_ipcrypt_ndx_outputbytes(), in bytes.
-spec ndx_tweakbytes() -> Result when Result :: 16.
Return crypto_ipcrypt_ndx_tweakbytes(), in bytes.
-spec pfx_bytes() -> Result when Result :: 16.
Return crypto_ipcrypt_pfx_bytes(), in bytes.
Decrypt the pfx_bytes/0 byte address In produced by pfx_encrypt/2 with the key K.
Encrypt the pfx_bytes/0 byte address In with the pfx_keybytes/0
byte key K, preserving prefixes: addresses that share their first
bits share the first bits of their encryptions, and IPv4 addresses
stay IPv4-mapped.
-spec pfx_keybytes() -> Result when Result :: 32.
Return crypto_ipcrypt_pfx_keybytes(), in bytes.
-spec pfx_keygen() -> Result when Result :: pfx_key().
Generate a random pfx_keybytes/0 byte key for pfx_encrypt/2.