@@ -1364,6 +1364,48 @@ TEST(AEADTest, TestGCMSIV256Change16Alignment) {
1364
1364
free (encrypt_ctx_256);
1365
1365
}
1366
1366
1367
+ TEST (AEADTest, TestMonotonicityCheck) {
1368
+
1369
+ static const uint8_t kEvpAeadCtxKey [32 ] = {0 };
1370
+
1371
+ // Only the tls13() ciphers have monotonicity checks
1372
+ struct {
1373
+ const EVP_AEAD *cipher;
1374
+ const size_t key_len;
1375
+ } ctx[] = { { .cipher = EVP_aead_aes_128_gcm_tls13 (), .key_len = 16 },
1376
+ { .cipher = EVP_aead_aes_256_gcm_tls13 (), .key_len = 32 } };
1377
+
1378
+ for (int i = 0 ; i < 2 ; i++) {
1379
+ const EVP_AEAD *cipher = ctx[i].cipher ;
1380
+ EVP_AEAD_CTX *encrypt_ctx =
1381
+ (EVP_AEAD_CTX *)malloc (sizeof (EVP_AEAD_CTX) + 8 );
1382
+ ASSERT_TRUE (encrypt_ctx);
1383
+
1384
+ EVP_AEAD_CTX_zero (encrypt_ctx);
1385
+ ASSERT_TRUE (EVP_AEAD_CTX_init (encrypt_ctx, cipher, kEvpAeadCtxKey , ctx[i].key_len , 16 , NULL ))
1386
+ << ERR_error_string (ERR_get_error (), NULL );
1387
+
1388
+ uint8_t nonce[12 ] = {0 };
1389
+ uint8_t last_byte = sizeof (nonce) - 1 ;
1390
+ uint8_t plaintext[16 ] = {0 };
1391
+ uint8_t ciphertext[32 ] = {0 };
1392
+ size_t out_len = 0 ;
1393
+
1394
+ // Checks that sequence numbers are allowed to increment by more than one
1395
+ // as long as monotonicity is preserved. Here the implicit IV is presumed
1396
+ // to be a zero-filled array. That lets us update the nonce value directly
1397
+ // with an increasing sequence number.
1398
+ for (size_t sequence_num = 0 ; sequence_num <= 255 ; sequence_num+=10 ) {
1399
+ nonce[last_byte] = sequence_num;
1400
+ ASSERT_TRUE (EVP_AEAD_CTX_seal (encrypt_ctx, ciphertext, &out_len,
1401
+ sizeof (ciphertext), nonce, sizeof (nonce), plaintext,
1402
+ sizeof (plaintext), nullptr /* ad */ , 0 ));
1403
+ }
1404
+
1405
+ free (encrypt_ctx);
1406
+ }
1407
+ }
1408
+
1367
1409
struct EvpAeadCtxSerdeTestParams {
1368
1410
const char *name;
1369
1411
const EVP_AEAD *cipher;
0 commit comments