@@ -1784,7 +1784,6 @@ Extension to support nonce size less than 24 bytes:
17841784#define XAES_256_GCM_KEY_COMMIT_SIZE (AES_BLOCK_SIZE * 2)
17851785#define XAES_256_GCM_MAX_NONCE_SIZE (AES_GCM_NONCE_LENGTH * 2)
17861786#define XAES_256_GCM_MIN_NONCE_SIZE (20)
1787-
17881787/*
17891788The following function performs the step #2 of CMAC specified in:
17901789https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38b.pdf#page=14
@@ -1806,6 +1805,15 @@ do { \
18061805 out [i ] = (in [i ] << 1 ) ^ ((0 - carry ) & 0x87 ); \
18071806} while (0 );
18081807
1808+ // Reference for nonce size < 24 bytes:
1809+ // https://eprint.iacr.org/2025/758.pdf#page=24
1810+ /* When nonce size b < 24 bytes, it uses bytes [b-12:b]
1811+ * of input nonce as iv for the underlying AES encryption.
1812+ * nonce_len is b in the referece, where 20 <= b <= 24 */
1813+ static inline const uint8_t * get_iv_for_aes_gcm (const uint8_t * nonce , const size_t nonce_len ) {
1814+ return nonce + nonce_len - AES_GCM_NONCE_LENGTH ;
1815+ }
1816+
18091817static int xaes_256_gcm_CMAC_derive_key (AES_KEY * xaes_key , uint8_t * k1 ,
18101818 const uint8_t * nonce , uint8_t * derived_key ) {
18111819 uint8_t M [AES_BLOCK_SIZE ] = {0x00 , 0x01 , 0x58 , 0x00 };
@@ -1859,10 +1867,8 @@ static int xaes_256_gcm_set_gcm_key(EVP_CIPHER_CTX *ctx, const uint8_t *nonce, i
18591867 // set the nonce (iv) length to AES_GCM_NONCE_LENGTH.
18601868 gctx -> ivlen = AES_GCM_NONCE_LENGTH ;
18611869
1862- // For nonce size < 24 bytes
1863- // Reference: https://eprint.iacr.org/2025/758.pdf#page=24
1864- aes_gcm_init_key (ctx , derived_key , nonce + ivlen - AES_GCM_NONCE_LENGTH , enc );
1865-
1870+ aes_gcm_init_key (ctx , derived_key , get_iv_for_aes_gcm (nonce , ivlen ), enc );
1871+
18661872 // Re-assign the original nonce size of XAES-256-GCM (20 <= |N| <= 24)
18671873 gctx -> ivlen = ivlen ;
18681874
@@ -1988,14 +1994,9 @@ static int aead_xaes_256_gcm_seal_scatter(
19881994 return 0 ;
19891995 }
19901996
1991- // Reference for nonce size < 24 bytes:
1992- // https://eprint.iacr.org/2025/758.pdf#page=24
1993- /* When nonce size b < 24 bytes, it uses bytes [b-12:b]
1994- * of input nonce as iv for the underlying AES encryption.
1995- * nonce_len is b in the referece, where 20 <= b <= 24 */
19961997 return aead_aes_gcm_seal_scatter_impl (
19971998 & gcm_ctx , out , out_tag , out_tag_len , max_out_tag_len ,
1998- nonce + nonce_len - AES_GCM_NONCE_LENGTH , AES_GCM_NONCE_LENGTH ,
1999+ get_iv_for_aes_gcm ( nonce , nonce_len ) , AES_GCM_NONCE_LENGTH ,
19992000 in , in_len , extra_in , extra_in_len , ad , ad_len , ctx -> tag_len );
20002001}
20012002
@@ -2013,13 +2014,8 @@ static int aead_xaes_256_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
20132014 return 0 ;
20142015 }
20152016
2016- // Reference for nonce size < 24 bytes:
2017- // https://eprint.iacr.org/2025/758.pdf#page=24
2018- /* When nonce size b < 24 bytes, it uses bytes [b-12:b]
2019- * of input nonce as iv for the underlying AES decryption.
2020- * nonce_len is b in the referece, where 20 <= b <= 24 */
20212017 return aead_aes_gcm_open_gather_impl (
2022- & gcm_ctx , out , nonce + nonce_len - AES_GCM_NONCE_LENGTH ,
2018+ & gcm_ctx , out , get_iv_for_aes_gcm ( nonce , nonce_len ) ,
20232019 AES_GCM_NONCE_LENGTH , in , in_len , in_tag , in_tag_len ,
20242020 ad , ad_len , ctx -> tag_len );
20252021}
0 commit comments