Skip to content

Commit bba8f20

Browse files
committed
Resolve comments of Sanketh
1 parent 8f805b6 commit bba8f20

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

crypto/fipsmodule/cipher/e_aes.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/*
17891788
The following function performs the step #2 of CMAC specified in:
17901789
https://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+
18091817
static 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
}

include/openssl/aead.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_192_gcm(void);
121121
// parameters, only use 12-byte nonces.
122122
OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm(void);
123123

124+
// EVP_aead_xaes_256_gcm is AES-256 in Galois Counter Mode with CMAC-based KDF
125+
OPENSSL_EXPORT const EVP_AEAD *EVP_aead_xaes_256_gcm(void);
126+
124127
// EVP_aead_chacha20_poly1305 is the AEAD built from ChaCha20 and
125128
// Poly1305 as described in RFC 8439.
126129
OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305(void);
@@ -431,9 +434,6 @@ OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm_tls13(void);
431434
// 1.3 nonce construction.
432435
OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm_tls13(void);
433436

434-
// EVP_aead_xaes_256_gcm is AES-256 in Galois Counter Mode with CMAC-based KDF
435-
OPENSSL_EXPORT const EVP_AEAD *EVP_aead_xaes_256_gcm(void);
436-
437437
// Obscure functions.
438438

439439
// evp_aead_direction_t denotes the direction of an AEAD operation.

tool/speed.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2950,7 +2950,9 @@ bool Speed(const std::vector<std::string> &args) {
29502950
!SpeedEvpCipherGeneric(EVP_aes_128_gcm(), "EVP-AES-128-GCM", kTLSADLen, selected) ||
29512951
!SpeedEvpCipherGeneric(EVP_aes_192_gcm(), "EVP-AES-192-GCM", kTLSADLen, selected) ||
29522952
!SpeedEvpCipherGeneric(EVP_aes_256_gcm(), "EVP-AES-256-GCM", kTLSADLen, selected) ||
2953+
#if AWSLC_API_VERSION > 34
29532954
!SpeedEvpCipherGeneric(EVP_xaes_256_gcm(), "EVP-XAES-256-GCM", kTLSADLen, selected) ||
2955+
#endif
29542956
!SpeedEvpCipherGeneric(EVP_aes_128_ctr(), "EVP-AES-128-CTR", kTLSADLen, selected) ||
29552957
!SpeedEvpCipherGeneric(EVP_aes_192_ctr(), "EVP-AES-192-CTR", kTLSADLen, selected) ||
29562958
!SpeedEvpCipherGeneric(EVP_aes_256_ctr(), "EVP-AES-256-CTR", kTLSADLen, selected) ||
@@ -3057,7 +3059,7 @@ bool Speed(const std::vector<std::string> &args) {
30573059
!SpeedAEADOpen(EVP_aead_xaes_256_gcm(), "AEAD-XAES-256-GCM", kTLSADLen, selected) ||
30583060
#endif
30593061
#if AWSLC_API_VERSION > 31
3060-
!SpeedDigestSign(selected) ||
3062+
!SpeedDigestSign(selected) ||
30613063
#endif
30623064
!SpeedAEADSeal(EVP_aead_aes_128_gcm(), "AEAD-AES-128-GCM", kTLSADLen, selected) ||
30633065
!SpeedAEADOpen(EVP_aead_aes_128_gcm(), "AEAD-AES-128-GCM", kTLSADLen, selected) ||

0 commit comments

Comments
 (0)