Skip to content

Commit 6e03c3b

Browse files
committed
Add tests for Fenrir fix validation
1 parent de5677f commit 6e03c3b

7 files changed

Lines changed: 504 additions & 1 deletion

File tree

test/test_aestag.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,117 @@ int test_aes128_gcm_set_iv_inv(void *data)
11961196
EVP_GCM_TLS_FIXED_IV_LEN, 12);
11971197
}
11981198

1199+
/******************************************************************************/
1200+
1201+
/**
1202+
* Test GCM decrypt init with key only (NULL IV), then set IV via params.
1203+
* Without the F-175 fix, this would crash with a NULL pointer dereference
1204+
* under WOLFSSL_AESGCM_STREAM.
1205+
*/
1206+
static int test_gcm_key_then_iv_helper(OSSL_LIB_CTX *libCtx)
1207+
{
1208+
int err;
1209+
EVP_CIPHER_CTX *encCtx = NULL;
1210+
EVP_CIPHER_CTX *decCtx = NULL;
1211+
EVP_CIPHER *cipher = NULL;
1212+
unsigned char key[16];
1213+
unsigned char iv[12];
1214+
unsigned char msg[] = "GCM key-then-iv test";
1215+
unsigned char aad[] = "additional data";
1216+
unsigned char enc[sizeof(msg) + 16];
1217+
unsigned char dec[sizeof(msg) + 16];
1218+
unsigned char tag[16];
1219+
int encLen = 0;
1220+
int decLen = 0;
1221+
int fLen = 0;
1222+
1223+
RAND_bytes(key, sizeof(key));
1224+
RAND_bytes(iv, sizeof(iv));
1225+
1226+
err = (cipher = EVP_CIPHER_fetch(libCtx, "AES-128-GCM", "")) == NULL;
1227+
1228+
/* Encrypt normally to produce ciphertext + tag */
1229+
if (err == 0) {
1230+
err = (encCtx = EVP_CIPHER_CTX_new()) == NULL;
1231+
}
1232+
if (err == 0) {
1233+
err = EVP_EncryptInit(encCtx, cipher, key, iv) != 1;
1234+
}
1235+
if (err == 0) {
1236+
err = EVP_EncryptUpdate(encCtx, NULL, &encLen, aad,
1237+
sizeof(aad)) != 1;
1238+
}
1239+
if (err == 0) {
1240+
err = EVP_EncryptUpdate(encCtx, enc, &encLen, msg,
1241+
sizeof(msg)) != 1;
1242+
}
1243+
if (err == 0) {
1244+
err = EVP_EncryptFinal_ex(encCtx, enc + encLen, &fLen) != 1;
1245+
}
1246+
if (err == 0) {
1247+
err = EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_AEAD_GET_TAG, sizeof(tag),
1248+
tag) != 1;
1249+
}
1250+
EVP_CIPHER_CTX_free(encCtx);
1251+
1252+
/* Decrypt with key-only init, then set IV separately */
1253+
if (err == 0) {
1254+
err = (decCtx = EVP_CIPHER_CTX_new()) == NULL;
1255+
}
1256+
if (err == 0) {
1257+
/* Init with key but NULL IV */
1258+
err = EVP_DecryptInit_ex(decCtx, cipher, NULL, key, NULL) != 1;
1259+
}
1260+
if (err == 0) {
1261+
/* Set IV via ctrl */
1262+
err = EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_AEAD_SET_IVLEN,
1263+
sizeof(iv), NULL) != 1;
1264+
}
1265+
if (err == 0) {
1266+
err = EVP_DecryptInit_ex(decCtx, NULL, NULL, NULL, iv) != 1;
1267+
}
1268+
if (err == 0) {
1269+
err = EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag),
1270+
tag) != 1;
1271+
}
1272+
if (err == 0) {
1273+
err = EVP_DecryptUpdate(decCtx, NULL, &decLen, aad,
1274+
sizeof(aad)) != 1;
1275+
}
1276+
if (err == 0) {
1277+
err = EVP_DecryptUpdate(decCtx, dec, &decLen, enc, encLen) != 1;
1278+
}
1279+
if (err == 0) {
1280+
err = EVP_DecryptFinal_ex(decCtx, dec + decLen, &fLen) != 1;
1281+
}
1282+
if (err == 0) {
1283+
if (decLen != (int)sizeof(msg) ||
1284+
memcmp(dec, msg, sizeof(msg)) != 0) {
1285+
PRINT_ERR_MSG("GCM key-then-iv decrypt mismatch");
1286+
err = 1;
1287+
}
1288+
}
1289+
1290+
EVP_CIPHER_CTX_free(decCtx);
1291+
EVP_CIPHER_free(cipher);
1292+
return err;
1293+
}
1294+
1295+
int test_aes128_gcm_key_then_iv(void *data)
1296+
{
1297+
int err;
1298+
1299+
(void)data;
1300+
1301+
PRINT_MSG("GCM key-then-iv with OpenSSL");
1302+
err = test_gcm_key_then_iv_helper(osslLibCtx);
1303+
if (err == 0) {
1304+
PRINT_MSG("GCM key-then-iv with wolfProvider");
1305+
err = test_gcm_key_then_iv_helper(wpLibCtx);
1306+
}
1307+
return err;
1308+
}
1309+
11991310
#endif /* WP_HAVE_AESGCM */
12001311

12011312
/******************************************************************************/

test/test_cipher.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,4 +1326,108 @@ int test_aes256_cbc_multiple(void *data)
13261326

13271327
return err;
13281328
}
1329+
1330+
/******************************************************************************/
1331+
1332+
/**
1333+
* Test AES-CBC encrypt/decrypt roundtrip with a large buffer processed in
1334+
* multiple update calls. Validates the chunked loop path in
1335+
* wp_aes_block_doit (F-1641).
1336+
*/
1337+
static int test_aes_cbc_large_update_helper(OSSL_LIB_CTX *libCtx)
1338+
{
1339+
int err;
1340+
EVP_CIPHER_CTX *ctx = NULL;
1341+
EVP_CIPHER *cipher = NULL;
1342+
unsigned char key[32];
1343+
unsigned char iv[16];
1344+
unsigned char plain[8192];
1345+
unsigned char enc[8192 + 16];
1346+
unsigned char dec[8192 + 16];
1347+
int outLen;
1348+
int fLen;
1349+
int totalEnc = 0;
1350+
int totalDec = 0;
1351+
size_t i;
1352+
1353+
RAND_bytes(key, sizeof(key));
1354+
RAND_bytes(iv, sizeof(iv));
1355+
RAND_bytes(plain, sizeof(plain));
1356+
1357+
err = (cipher = EVP_CIPHER_fetch(libCtx, "AES-256-CBC", "")) == NULL;
1358+
1359+
/* Encrypt in 1024-byte chunks */
1360+
if (err == 0) {
1361+
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
1362+
}
1363+
if (err == 0) {
1364+
err = EVP_EncryptInit(ctx, cipher, key, iv) != 1;
1365+
}
1366+
if (err == 0) {
1367+
err = EVP_CIPHER_CTX_set_padding(ctx, 0) != 1;
1368+
}
1369+
for (i = 0; err == 0 && i < sizeof(plain); i += 1024) {
1370+
err = EVP_EncryptUpdate(ctx, enc + totalEnc, &outLen,
1371+
plain + i, 1024) != 1;
1372+
if (err == 0) {
1373+
totalEnc += outLen;
1374+
}
1375+
}
1376+
if (err == 0) {
1377+
err = EVP_EncryptFinal_ex(ctx, enc + totalEnc, &fLen) != 1;
1378+
totalEnc += fLen;
1379+
}
1380+
EVP_CIPHER_CTX_free(ctx);
1381+
ctx = NULL;
1382+
1383+
/* Decrypt in 1024-byte chunks */
1384+
if (err == 0) {
1385+
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
1386+
}
1387+
if (err == 0) {
1388+
err = EVP_DecryptInit(ctx, cipher, key, iv) != 1;
1389+
}
1390+
if (err == 0) {
1391+
err = EVP_CIPHER_CTX_set_padding(ctx, 0) != 1;
1392+
}
1393+
for (i = 0; err == 0 && (int)i < totalEnc; i += 1024) {
1394+
int chunk = (totalEnc - (int)i < 1024) ? totalEnc - (int)i : 1024;
1395+
err = EVP_DecryptUpdate(ctx, dec + totalDec, &outLen,
1396+
enc + i, chunk) != 1;
1397+
if (err == 0) {
1398+
totalDec += outLen;
1399+
}
1400+
}
1401+
if (err == 0) {
1402+
err = EVP_DecryptFinal_ex(ctx, dec + totalDec, &fLen) != 1;
1403+
totalDec += fLen;
1404+
}
1405+
if (err == 0) {
1406+
if (totalDec != (int)sizeof(plain) ||
1407+
memcmp(dec, plain, sizeof(plain)) != 0) {
1408+
PRINT_ERR_MSG("AES-CBC large update decrypt mismatch");
1409+
err = 1;
1410+
}
1411+
}
1412+
1413+
EVP_CIPHER_CTX_free(ctx);
1414+
EVP_CIPHER_free(cipher);
1415+
return err;
1416+
}
1417+
1418+
int test_aes_cbc_large_update(void *data)
1419+
{
1420+
int err;
1421+
1422+
(void)data;
1423+
1424+
PRINT_MSG("AES-CBC large update with OpenSSL");
1425+
err = test_aes_cbc_large_update_helper(osslLibCtx);
1426+
if (err == 0) {
1427+
PRINT_MSG("AES-CBC large update with wolfProvider");
1428+
err = test_aes_cbc_large_update_helper(wpLibCtx);
1429+
}
1430+
return err;
1431+
}
1432+
13291433
#endif /* WP_HAVE_AESCBC */

test/test_cmac.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,106 @@ int test_cmac_create(void *data)
257257
return ret;
258258
}
259259

260+
/******************************************************************************/
261+
262+
/**
263+
* Test that CMAC produces consistent results when data is fed in many small
264+
* updates vs. a single large update. Exercises the chunked update path
265+
* (F-1640).
266+
*/
267+
static int test_cmac_multi_update_helper(OSSL_LIB_CTX *libCtx)
268+
{
269+
int err;
270+
EVP_MAC *emac = NULL;
271+
EVP_MAC_CTX *ctx = NULL;
272+
OSSL_PARAM params[3];
273+
char cipher[] = "AES-256-CBC";
274+
unsigned char key[32] = {
275+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
276+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
277+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
278+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
279+
};
280+
unsigned char data[2048];
281+
unsigned char macOne[16];
282+
unsigned char macMulti[16];
283+
size_t macOneSz = sizeof(macOne);
284+
size_t macMultiSz = sizeof(macMulti);
285+
size_t i;
286+
287+
RAND_bytes(data, sizeof(data));
288+
289+
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
290+
cipher, 0);
291+
params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
292+
(void *)key, sizeof(key));
293+
params[2] = OSSL_PARAM_construct_end();
294+
295+
err = (emac = EVP_MAC_fetch(libCtx, "CMAC", NULL)) == NULL;
296+
297+
/* Single update */
298+
if (err == 0) {
299+
err = (ctx = EVP_MAC_CTX_new(emac)) == NULL;
300+
}
301+
if (err == 0) {
302+
err = EVP_MAC_CTX_set_params(ctx, params) != 1;
303+
}
304+
if (err == 0) {
305+
err = EVP_MAC_init(ctx, NULL, 0, NULL) != 1;
306+
}
307+
if (err == 0) {
308+
err = EVP_MAC_update(ctx, data, sizeof(data)) != 1;
309+
}
310+
if (err == 0) {
311+
err = EVP_MAC_final(ctx, macOne, &macOneSz, sizeof(macOne)) != 1;
312+
}
313+
EVP_MAC_CTX_free(ctx);
314+
ctx = NULL;
315+
316+
/* Many small updates (16 bytes each — one AES block) */
317+
if (err == 0) {
318+
err = (ctx = EVP_MAC_CTX_new(emac)) == NULL;
319+
}
320+
if (err == 0) {
321+
err = EVP_MAC_CTX_set_params(ctx, params) != 1;
322+
}
323+
if (err == 0) {
324+
err = EVP_MAC_init(ctx, NULL, 0, NULL) != 1;
325+
}
326+
for (i = 0; err == 0 && i < sizeof(data); i += 16) {
327+
err = EVP_MAC_update(ctx, data + i, 16) != 1;
328+
}
329+
if (err == 0) {
330+
err = EVP_MAC_final(ctx, macMulti, &macMultiSz,
331+
sizeof(macMulti)) != 1;
332+
}
333+
if (err == 0) {
334+
if (macOneSz != macMultiSz ||
335+
memcmp(macOne, macMulti, macOneSz) != 0) {
336+
PRINT_ERR_MSG("Multi-update CMAC doesn't match single update");
337+
err = 1;
338+
}
339+
}
340+
341+
EVP_MAC_CTX_free(ctx);
342+
EVP_MAC_free(emac);
343+
return err;
344+
}
345+
346+
int test_cmac_multi_update(void *data)
347+
{
348+
int err;
349+
350+
(void)data;
351+
352+
PRINT_MSG("CMAC multi-update with OpenSSL");
353+
err = test_cmac_multi_update_helper(osslLibCtx);
354+
if (err == 0) {
355+
PRINT_MSG("CMAC multi-update with wolfProvider");
356+
err = test_cmac_multi_update_helper(wpLibCtx);
357+
}
358+
return err;
359+
}
360+
260361
#endif /* WP_HAVE_CMAC */
261362

0 commit comments

Comments
 (0)