@@ -4189,8 +4189,9 @@ FIO_BASIC Basic Kitchen Sink Inclusion
41894189/* *****************************************************************************
41904190FIO_CRYPT Poor-man's Cryptographic Elements
41914191***************************************************************************** */
4192- #if defined(FIO_CRYPT)
4192+ #if defined(FIO_CRYPT) || defined(FIO_CRYPTO)
41934193#undef FIO_CRYPT
4194+ #undef FIO_CRYPTO
41944195#undef FIO_AES
41954196#undef FIO_CRYPTO_CORE
41964197#undef FIO_CHACHA
@@ -4208,7 +4209,7 @@ FIO_CRYPT Poor-man's Cryptographic Elements
42084209#define FIO_SECRET
42094210#define FIO_OTP
42104211
4211- #endif /* FIO_CRYPT */
4212+ #endif /* FIO_CRYPT || defined(FIO_CRYPTO) */
42124213
42134214/* *****************************************************************************
42144215FIO_CORE Core Inclusion
@@ -4428,7 +4429,7 @@ FIO_MAP Ordering & Naming Shortcut
44284429
44294430
44304431***************************************************************************** */
4431- #if defined(FIO_PUBSUB) || defined(FIO_ED25519)
4432+ #if defined(FIO_PUBSUB)
44324433#define FIO_CHACHA
44334434#define FIO_SECRET
44344435#endif
@@ -23052,6 +23053,21 @@ Copyright and License: see header file (000 copyright.h) or top of file
2305223053***************************************************************************** */
2305323054#define H___FIO_CRYPTO_CORE___H
2305423055
23056+ typedef void(fio_crypto_enc_fn)(void *restrict mac,
23057+ void *restrict data,
23058+ size_t len,
23059+ const void *ad, /* additional data */
23060+ size_t adlen,
23061+ const void *key,
23062+ const void *nonce);
23063+ typedef int(fio_crypto_dec_fn)(void *restrict mac,
23064+ void *restrict data,
23065+ size_t len,
23066+ const void *ad, /* additional data */
23067+ size_t adlen,
23068+ const void *key,
23069+ const void *nonce);
23070+
2305523071/* *****************************************************************************
2305623072Module Implementation - possibly externed functions.
2305723073***************************************************************************** */
@@ -27936,12 +27952,10 @@ their private key. No prior key exchange or handshake is required.
2793627952The scheme uses:
2793727953- X25519 for ephemeral key agreement
2793827954- SHA-256 for key derivation (HKDF-like)
27939- - ChaCha20-Poly1305 for authenticated encryption
27955+ - ChaCha20-Poly1305 or AES256-GCM for authenticated encryption
2794027956
2794127957Ciphertext format: [32-byte ephemeral public key][16-byte MAC][encrypted data]
2794227958Total overhead: 48 bytes
27943-
27944- Note: Requires FIO_CHACHA to be defined for ChaCha20-Poly1305 support.
2794527959***************************************************************************** */
2794627960
2794727961/**
@@ -27957,12 +27971,14 @@ Note: Requires FIO_CHACHA to be defined for ChaCha20-Poly1305 support.
2795727971 * @param ciphertext Output buffer (must be at least message_len + 48 bytes)
2795827972 * @param message The plaintext message to encrypt
2795927973 * @param message_len Length of the message
27974+ * @param encryption_function Encryption function (fio_chacha20_poly1305_enc)
2796027975 * @param recipient_pk The recipient's X25519 public key (32 bytes)
2796127976 * @return 0 on success, -1 on failure
2796227977 */
2796327978SFUNC int fio_x25519_encrypt(uint8_t *ciphertext,
2796427979 const void *message,
2796527980 size_t message_len,
27981+ fio_crypto_enc_fn encryption_function,
2796627982 const uint8_t recipient_pk[32]);
2796727983
2796827984/**
@@ -27971,12 +27987,14 @@ SFUNC int fio_x25519_encrypt(uint8_t *ciphertext,
2797127987 * @param plaintext Output buffer (must be at least ciphertext_len - 48 bytes)
2797227988 * @param ciphertext The ciphertext (ephemeral_pk || mac || encrypted_data)
2797327989 * @param ciphertext_len Length of the ciphertext (must be >= 48)
27990+ * @param decryption_function Decryption function (fio_chacha20_poly1305_dec)
2797427991 * @param recipient_sk The recipient's X25519 secret key (32 bytes)
2797527992 * @return 0 on success, -1 on failure (authentication failed or invalid input)
2797627993 */
2797727994SFUNC int fio_x25519_decrypt(uint8_t *plaintext,
2797827995 const uint8_t *ciphertext,
2797927996 size_t ciphertext_len,
27997+ fio_crypto_dec_fn decryption_function,
2798027998 const uint8_t recipient_sk[32]);
2798127999
2798228000/**
@@ -27990,7 +28008,7 @@ SFUNC int fio_x25519_decrypt(uint8_t *plaintext,
2799028008 * Returns 0 if ciphertext_len < 48 (invalid ciphertext).
2799128009 */
2799228010#define FIO_X25519_PLAINTEXT_LEN(ciphertext_len) \
27993- ((ciphertext_len) >= 48 ? ((ciphertext_len)-48) : 0)
28011+ ((ciphertext_len) > 48 ? ((ciphertext_len)-48) : 0)
2799428012
2799528013/* *****************************************************************************
2799628014Implementation - possibly externed functions.
@@ -29054,6 +29072,7 @@ Format: [32-byte ephemeral public key][16-byte MAC][encrypted data]
2905429072SFUNC int fio_x25519_encrypt(uint8_t *ciphertext,
2905529073 const void *message,
2905629074 size_t message_len,
29075+ fio_crypto_enc_fn encryption_function,
2905729076 const uint8_t recipient_pk[32]) {
2905829077 /* generate ephemeral key pair */
2905929078 uint8_t eph_sk[32], eph_pk[32];
@@ -29085,13 +29104,13 @@ SFUNC int fio_x25519_encrypt(uint8_t *ciphertext,
2908529104
2908629105 /* encrypt with chacha20-poly1305 */
2908729106 /* nonce: first 12 bytes of ephemeral public key (unique per encryption) */
29088- fio_chacha20_poly1305_enc (ciphertext + 32, /* mac output */
29089- ciphertext + 48, /* data to encrypt */
29090- message_len, /* data length */
29091- ciphertext, /* additional data: eph_pk */
29092- 32, /* ad length */
29093- key.u8, /* encryption key */
29094- eph_pk); /* nonce (first 12 bytes) */
29107+ encryption_function (ciphertext + 32, /* mac output */
29108+ ciphertext + 48, /* data to encrypt */
29109+ message_len, /* data length */
29110+ ciphertext, /* additional data: eph_pk */
29111+ 32, /* ad length */
29112+ key.u8, /* encryption key */
29113+ eph_pk); /* nonce (first 12 bytes) */
2909529114
2909629115 fio_secure_zero(&key, sizeof(key));
2909729116 return 0;
@@ -29100,6 +29119,7 @@ SFUNC int fio_x25519_encrypt(uint8_t *ciphertext,
2910029119SFUNC int fio_x25519_decrypt(uint8_t *plaintext,
2910129120 const uint8_t *ciphertext,
2910229121 size_t ciphertext_len,
29122+ fio_crypto_dec_fn decryption_function,
2910329123 const uint8_t recipient_sk[32]) {
2910429124 /* validate minimum ciphertext length */
2910529125 if (ciphertext_len < 48)
@@ -29133,13 +29153,13 @@ SFUNC int fio_x25519_decrypt(uint8_t *plaintext,
2913329153 fio_memcpy16(mac_copy, mac);
2913429154
2913529155 /* decrypt and verify with chacha20-poly1305 */
29136- int result = fio_chacha20_poly1305_dec (mac_copy, /* mac to verify */
29137- plaintext, /* data to decrypt */
29138- message_len, /* data length */
29139- eph_pk, /* additional data */
29140- 32, /* ad length */
29141- key.u8, /* decryption key */
29142- eph_pk); /* nonce */
29156+ int result = decryption_function (mac_copy, /* mac to verify */
29157+ plaintext, /* data to decrypt */
29158+ message_len, /* data length */
29159+ eph_pk, /* additional data */
29160+ 32, /* ad length */
29161+ key.u8, /* decryption key */
29162+ eph_pk); /* nonce */
2914329163
2914429164 fio_secure_zero(&key, sizeof(key));
2914529165 return result;
0 commit comments