|
25 | 25 |
|
26 | 26 | #include "../fipsmodule/cipher/internal.h" |
27 | 27 | #include "../internal.h" |
| 28 | +#include "./internal.h" |
28 | 29 | #include "../test/abi_test.h" |
29 | 30 | #include "../test/file_test.h" |
30 | 31 | #include "../test/test_util.h" |
@@ -1247,6 +1248,117 @@ TEST(AEADTest, WycheproofXChaCha20Poly1305) { |
1247 | 1248 | }); |
1248 | 1249 | } |
1249 | 1250 |
|
| 1251 | +static int awslc_encrypt(EVP_AEAD_CTX *ctx, uint8_t *nonce, |
| 1252 | + uint8_t *ct, uint8_t *pt) { |
| 1253 | + size_t ct_len = 0; |
| 1254 | + GTEST_LOG_(INFO) << "awslc_encrypt: Ctx.State Location: " |
| 1255 | + << &ctx->state; |
| 1256 | + if (EVP_AEAD_CTX_seal(ctx, ct, &ct_len, 32, nonce, 12, pt, 16, NULL, 0) != |
| 1257 | + 1) { |
| 1258 | + return 1; |
| 1259 | + } |
| 1260 | + |
| 1261 | + return 0; |
| 1262 | +} |
| 1263 | + |
| 1264 | +static int awslc_decrypt(const EVP_AEAD *cipher, uint8_t ct[32], uint8_t *key, |
| 1265 | + size_t key_len, uint8_t nonce[12], uint8_t pt[16]) { |
| 1266 | + |
| 1267 | + EVP_AEAD_CTX ctx; |
| 1268 | + size_t pt_len = 0; |
| 1269 | + |
| 1270 | + EVP_AEAD_CTX_zero(&ctx); |
| 1271 | + if (EVP_AEAD_CTX_init(&ctx, cipher, key, key_len, 16, NULL) != 1) { |
| 1272 | + return 1; |
| 1273 | + } |
| 1274 | + GTEST_LOG_(INFO) << "awslc_decrypt: Ctx.State Location: " << &ctx.state; |
| 1275 | + |
| 1276 | + if (EVP_AEAD_CTX_open(&ctx, pt, &pt_len, 16, nonce, 12, ct, 32, NULL, 0) != |
| 1277 | + 1) { |
| 1278 | + return 1; |
| 1279 | + } |
| 1280 | + |
| 1281 | + return 0; |
| 1282 | +} |
| 1283 | + |
| 1284 | +TEST(AEADTest, TestGCMSIV128Change16Alignment) { |
| 1285 | + uint8_t key[16] = {0}; |
| 1286 | + uint8_t nonce[12] = {0}; |
| 1287 | + uint8_t pt[16] = {0}; |
| 1288 | + uint8_t ct[32] = {0}; |
| 1289 | + EVP_AEAD_CTX* encrypt_ctx_128 = (EVP_AEAD_CTX*)malloc(sizeof(EVP_AEAD_CTX) + 8); |
| 1290 | + |
| 1291 | + const EVP_AEAD *cipher_128 = EVP_aead_aes_128_gcm_siv(); |
| 1292 | + |
| 1293 | + EVP_AEAD_CTX_zero(encrypt_ctx_128); |
| 1294 | + ASSERT_TRUE(EVP_AEAD_CTX_init(encrypt_ctx_128, cipher_128, key, 16, 16, NULL)) |
| 1295 | + << ERR_error_string(ERR_get_error(), NULL); |
| 1296 | + ASSERT_FALSE(awslc_encrypt(encrypt_ctx_128, nonce, ct, pt)) |
| 1297 | + << ERR_error_string(ERR_get_error(), NULL); |
| 1298 | + ASSERT_FALSE(awslc_decrypt(cipher_128, ct, key, 16, nonce, pt)) |
| 1299 | + << ERR_error_string(ERR_get_error(), NULL); |
| 1300 | + |
| 1301 | + GTEST_LOG_(INFO) << "Orig. Ctx.State Location: " << &encrypt_ctx_128->state; |
| 1302 | + EVP_AEAD_CTX *moved_encrypt_ctx_128 = |
| 1303 | + (EVP_AEAD_CTX *)(((uint8_t *)encrypt_ctx_128) + 8); |
| 1304 | + memmove(moved_encrypt_ctx_128, encrypt_ctx_128, sizeof(EVP_AEAD_CTX)); |
| 1305 | + GTEST_LOG_(INFO) << "Moved Ctx.State Location: " |
| 1306 | + << &moved_encrypt_ctx_128->state; |
| 1307 | + |
| 1308 | + if (awslc_encrypt(moved_encrypt_ctx_128, nonce, ct, pt) != 1) { |
| 1309 | + if (x86_64_assembly_implementation_FOR_TESTING()) { |
| 1310 | + FAIL() << "Expected failure in awslc_encrypt"; |
| 1311 | + } |
| 1312 | + } else { |
| 1313 | + if (!x86_64_assembly_implementation_FOR_TESTING()) { |
| 1314 | + FAIL() << "Failure in awslc_encrypt"; |
| 1315 | + } |
| 1316 | + uint32_t err = ERR_get_error(); |
| 1317 | + EXPECT_EQ(ERR_R_CIPHER_LIB, ERR_GET_LIB(err)); |
| 1318 | + EXPECT_EQ(CIPHER_R_ALIGNMENT_CHANGED, ERR_GET_REASON(err)); |
| 1319 | + } |
| 1320 | + free(encrypt_ctx_128); |
| 1321 | +} |
| 1322 | + |
| 1323 | +TEST(AEADTest, TestGCMSIV256Change16Alignment) { |
| 1324 | + uint8_t nonce[12] = {0}; |
| 1325 | + uint8_t key[32] = {0}; |
| 1326 | + uint8_t pt[16] = {0}; |
| 1327 | + uint8_t ct[32] = {0}; |
| 1328 | + EVP_AEAD_CTX* encrypt_ctx_256 = (EVP_AEAD_CTX*)malloc(sizeof(EVP_AEAD_CTX) + 8); |
| 1329 | + |
| 1330 | + const EVP_AEAD *cipher_256 = EVP_aead_aes_256_gcm_siv(); |
| 1331 | + |
| 1332 | + EVP_AEAD_CTX_zero(encrypt_ctx_256); |
| 1333 | + ASSERT_TRUE(EVP_AEAD_CTX_init(encrypt_ctx_256, cipher_256, key, 32, 16, NULL)) |
| 1334 | + << ERR_error_string(ERR_get_error(), NULL); |
| 1335 | + ASSERT_FALSE(awslc_encrypt(encrypt_ctx_256, nonce, ct, pt)) |
| 1336 | + << ERR_error_string(ERR_get_error(), NULL); |
| 1337 | + ASSERT_FALSE(awslc_decrypt(cipher_256, ct, key, 32, nonce, pt)) |
| 1338 | + << ERR_error_string(ERR_get_error(), NULL); |
| 1339 | + |
| 1340 | + GTEST_LOG_(INFO) << "Orig. Ctx.State Location: " << &encrypt_ctx_256->state; |
| 1341 | + EVP_AEAD_CTX *moved_encrypt_ctx_256 = |
| 1342 | + (EVP_AEAD_CTX *)(((uint8_t *)encrypt_ctx_256) + 8); |
| 1343 | + memmove(moved_encrypt_ctx_256, encrypt_ctx_256, sizeof(EVP_AEAD_CTX)); |
| 1344 | + GTEST_LOG_(INFO) << "Moved Ctx.State Location: " |
| 1345 | + << &moved_encrypt_ctx_256->state; |
| 1346 | + |
| 1347 | + if (awslc_encrypt(moved_encrypt_ctx_256, nonce, ct, pt) != 1) { |
| 1348 | + if (x86_64_assembly_implementation_FOR_TESTING()) { |
| 1349 | + FAIL() << "Expected failure in awslc_encrypt"; |
| 1350 | + } |
| 1351 | + } else { |
| 1352 | + if (!x86_64_assembly_implementation_FOR_TESTING()) { |
| 1353 | + FAIL() << "Failure in awslc_encrypt"; |
| 1354 | + } |
| 1355 | + uint32_t err = ERR_get_error(); |
| 1356 | + EXPECT_EQ(ERR_R_CIPHER_LIB, ERR_GET_LIB(err)); |
| 1357 | + EXPECT_EQ(CIPHER_R_ALIGNMENT_CHANGED, ERR_GET_REASON(err)); |
| 1358 | + } |
| 1359 | + free(encrypt_ctx_256); |
| 1360 | +} |
| 1361 | + |
1250 | 1362 | TEST(AEADTest, FreeNull) { EVP_AEAD_CTX_free(nullptr); } |
1251 | 1363 |
|
1252 | 1364 | // Deterministic IV generation for AES-GCM 256. |
|
0 commit comments