Skip to content

Commit 1d0fb43

Browse files
committed
Add unit tests for Fenrir fixes
1 parent 4aeb35e commit 1d0fb43

9 files changed

Lines changed: 640 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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,4 +1488,107 @@ int test_aes256_cbc_bad_pad(void *data)
14881488
return err;
14891489
}
14901490

1491+
/******************************************************************************/
1492+
1493+
/**
1494+
* Test AES-CBC encrypt/decrypt roundtrip with a large buffer processed in
1495+
* multiple update calls. Validates the chunked loop path in
1496+
* wp_aes_block_doit (F-1641).
1497+
*/
1498+
static int test_aes_cbc_large_update_helper(OSSL_LIB_CTX *libCtx)
1499+
{
1500+
int err;
1501+
EVP_CIPHER_CTX *ctx = NULL;
1502+
EVP_CIPHER *cipher = NULL;
1503+
unsigned char key[32];
1504+
unsigned char iv[16];
1505+
unsigned char plain[8192];
1506+
unsigned char enc[8192 + 16];
1507+
unsigned char dec[8192 + 16];
1508+
int outLen;
1509+
int fLen;
1510+
int totalEnc = 0;
1511+
int totalDec = 0;
1512+
size_t i;
1513+
1514+
RAND_bytes(key, sizeof(key));
1515+
RAND_bytes(iv, sizeof(iv));
1516+
RAND_bytes(plain, sizeof(plain));
1517+
1518+
err = (cipher = EVP_CIPHER_fetch(libCtx, "AES-256-CBC", "")) == NULL;
1519+
1520+
/* Encrypt in 1024-byte chunks */
1521+
if (err == 0) {
1522+
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
1523+
}
1524+
if (err == 0) {
1525+
err = EVP_EncryptInit(ctx, cipher, key, iv) != 1;
1526+
}
1527+
if (err == 0) {
1528+
err = EVP_CIPHER_CTX_set_padding(ctx, 0) != 1;
1529+
}
1530+
for (i = 0; err == 0 && i < sizeof(plain); i += 1024) {
1531+
err = EVP_EncryptUpdate(ctx, enc + totalEnc, &outLen,
1532+
plain + i, 1024) != 1;
1533+
if (err == 0) {
1534+
totalEnc += outLen;
1535+
}
1536+
}
1537+
if (err == 0) {
1538+
err = EVP_EncryptFinal_ex(ctx, enc + totalEnc, &fLen) != 1;
1539+
totalEnc += fLen;
1540+
}
1541+
EVP_CIPHER_CTX_free(ctx);
1542+
ctx = NULL;
1543+
1544+
/* Decrypt in 1024-byte chunks */
1545+
if (err == 0) {
1546+
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
1547+
}
1548+
if (err == 0) {
1549+
err = EVP_DecryptInit(ctx, cipher, key, iv) != 1;
1550+
}
1551+
if (err == 0) {
1552+
err = EVP_CIPHER_CTX_set_padding(ctx, 0) != 1;
1553+
}
1554+
for (i = 0; err == 0 && (int)i < totalEnc; i += 1024) {
1555+
int chunk = (totalEnc - (int)i < 1024) ? totalEnc - (int)i : 1024;
1556+
err = EVP_DecryptUpdate(ctx, dec + totalDec, &outLen,
1557+
enc + i, chunk) != 1;
1558+
if (err == 0) {
1559+
totalDec += outLen;
1560+
}
1561+
}
1562+
if (err == 0) {
1563+
err = EVP_DecryptFinal_ex(ctx, dec + totalDec, &fLen) != 1;
1564+
totalDec += fLen;
1565+
}
1566+
if (err == 0) {
1567+
if (totalDec != (int)sizeof(plain) ||
1568+
memcmp(dec, plain, sizeof(plain)) != 0) {
1569+
PRINT_ERR_MSG("AES-CBC large update decrypt mismatch");
1570+
err = 1;
1571+
}
1572+
}
1573+
1574+
EVP_CIPHER_CTX_free(ctx);
1575+
EVP_CIPHER_free(cipher);
1576+
return err;
1577+
}
1578+
1579+
int test_aes_cbc_large_update(void *data)
1580+
{
1581+
int err;
1582+
1583+
(void)data;
1584+
1585+
PRINT_MSG("AES-CBC large update with OpenSSL");
1586+
err = test_aes_cbc_large_update_helper(osslLibCtx);
1587+
if (err == 0) {
1588+
PRINT_MSG("AES-CBC large update with wolfProvider");
1589+
err = test_aes_cbc_large_update_helper(wpLibCtx);
1590+
}
1591+
return err;
1592+
}
1593+
14911594
#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)