Skip to content

Commit a5ce080

Browse files
committed
Add libsodium PFX example
1 parent 92f542a commit a5ce080

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

www/_implementations/libsodium.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ examples:
3535
/* Decrypt */
3636
crypto_ipcrypt_decrypt(decrypted_ip, encrypted_ip, key);
3737
}
38+
- title: Prefix-Preserving Encryption (PFX)
39+
description: Encrypt an IP address using prefix-preserving mode with dual AES-128
40+
code: |
41+
#include <sodium.h>
42+
43+
int main(void) {
44+
unsigned char key[crypto_ipcrypt_pfx_KEYBYTES]; /* 32 bytes */
45+
unsigned char ip[16], encrypted_ip[16], decrypted_ip[16];
46+
47+
if (sodium_init() < 0) return 1;
48+
49+
crypto_ipcrypt_pfx_keygen(key);
50+
51+
memset(ip, 0, 16);
52+
ip[10] = 0xff; ip[11] = 0xff;
53+
ip[12] = 192; ip[13] = 0; ip[14] = 2; ip[15] = 1;
54+
55+
/* Encrypt (output is same size as input) */
56+
crypto_ipcrypt_pfx_encrypt(encrypted_ip, ip, key);
57+
58+
/* Decrypt */
59+
crypto_ipcrypt_pfx_decrypt(decrypted_ip, encrypted_ip, key);
60+
}
3861
- title: Non-Deterministic Encryption (ND)
3962
description: Encrypt an IP address using non-deterministic mode with KIASU-BC
4063
code: |
@@ -134,6 +157,20 @@ crypto_ipcrypt_encrypt(encrypted_ip, ip, key);
134157
crypto_ipcrypt_decrypt(decrypted_ip, encrypted_ip, key);
135158
```
136159
160+
### Prefix-Preserving Encryption (PFX)
161+
162+
```c
163+
#include <sodium.h>
164+
165+
unsigned char key[crypto_ipcrypt_pfx_KEYBYTES]; /* 32 bytes: two 16-byte keys */
166+
unsigned char ip[16], encrypted_ip[16], decrypted_ip[16];
167+
168+
crypto_ipcrypt_pfx_keygen(key);
169+
170+
crypto_ipcrypt_pfx_encrypt(encrypted_ip, ip, key);
171+
crypto_ipcrypt_pfx_decrypt(decrypted_ip, encrypted_ip, key);
172+
```
173+
137174
### Non-Deterministic Encryption (ND)
138175

139176
```c
@@ -174,6 +211,7 @@ crypto_ipcrypt_ndx_decrypt(decrypted_ip, encrypted, key);
174211
|---|---|---|
175212
| `crypto_ipcrypt_KEYBYTES` | 16 | Key size for deterministic and ND modes |
176213
| `crypto_ipcrypt_INPUTBYTES` | 16 | IP address size (IPv4-mapped IPv6) |
214+
| `crypto_ipcrypt_pfx_KEYBYTES` | 32 | Key size for PFX mode (two 16-byte keys) |
177215
| `crypto_ipcrypt_nd_BYTES` | 24 | ND ciphertext size (8-byte tweak + 16-byte ciphertext) |
178216
| `crypto_ipcrypt_ndx_KEYBYTES` | 32 | Key size for NDX mode |
179217
| `crypto_ipcrypt_ndx_BYTES` | 32 | NDX ciphertext size (16-byte tweak + 16-byte ciphertext) |
@@ -190,6 +228,18 @@ int crypto_ipcrypt_decrypt(unsigned char out[16], const unsigned char in[16],
190228
const unsigned char k[crypto_ipcrypt_KEYBYTES]);
191229
```
192230
231+
### Prefix-Preserving Encryption (PFX)
232+
233+
```c
234+
void crypto_ipcrypt_pfx_keygen(unsigned char k[crypto_ipcrypt_pfx_KEYBYTES]);
235+
236+
int crypto_ipcrypt_pfx_encrypt(unsigned char out[16], const unsigned char in[16],
237+
const unsigned char k[crypto_ipcrypt_pfx_KEYBYTES]);
238+
239+
int crypto_ipcrypt_pfx_decrypt(unsigned char out[16], const unsigned char in[16],
240+
const unsigned char k[crypto_ipcrypt_pfx_KEYBYTES]);
241+
```
242+
193243
### Non-Deterministic Encryption (ND)
194244

195245
```c
@@ -230,6 +280,7 @@ The libsodium implementation includes:
230280
- IPv4 address encryption/decryption
231281
- IPv6 address encryption/decryption
232282
- Deterministic encryption (AES-128)
283+
- Prefix-preserving encryption (dual AES-128)
233284
- Non-deterministic encryption (KIASU-BC)
234285
- Extended non-deterministic encryption (AES-XTS)
235286
- Hardware-accelerated AES (AES-NI, ARMv8)

0 commit comments

Comments
 (0)