Skip to content

Commit e133231

Browse files
committed
Add CMAC cleanup and RSA TLS bounds check
1 parent 1d0fb43 commit e133231

7 files changed

Lines changed: 101 additions & 12 deletions

File tree

src/wp_aes_block.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,19 @@ static int wp_aes_block_doit(wp_AesBlockCtx *ctx, unsigned char *out,
433433
{
434434
int rc = 0;
435435

436+
/* Reject unsupported modes up-front so an inLen == 0 call with a
437+
* non-CBC/non-ECB mode does not silently succeed. */
438+
if (1
439+
#ifdef WP_HAVE_AESCBC
440+
&& (ctx->mode != EVP_CIPH_CBC_MODE)
441+
#endif
442+
#ifdef WP_HAVE_AESECB
443+
&& (ctx->mode != EVP_CIPH_ECB_MODE)
444+
#endif
445+
) {
446+
return 0;
447+
}
448+
436449
while ((rc == 0) && (inLen > 0)) {
437450
/* Chunk must be block-aligned (AES block size = 16). */
438451
word32 chunk = (inLen > 0xFFFFFFF0U) ? 0xFFFFFFF0U : (word32)inLen;

src/wp_aes_stream.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ static int wp_aes_stream_doit(wp_AesStreamCtx *ctx, unsigned char *out,
534534
if (ctx->mode == EVP_CIPH_CTR_MODE) {
535535
XMEMCPY(&ctx->aes.reg, ctx->iv, ctx->ivLen);
536536
while (ok && (inLen > 0)) {
537-
word32 chunk = (inLen > 0xFFFFFFFFU) ? 0xFFFFFFFFU : (word32)inLen;
537+
/* Cap chunk to largest word32 multiple of AES_BLOCK_SIZE so the
538+
* IV/counter state is consistent across chunk boundaries. */
539+
word32 chunk = (inLen > 0xFFFFFFF0U) ? 0xFFFFFFF0U : (word32)inLen;
538540
int rc = wc_AesCtrEncrypt(&ctx->aes, out, in, chunk);
539541
if (rc != 0) {
540542
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG,
@@ -555,7 +557,9 @@ static int wp_aes_stream_doit(wp_AesStreamCtx *ctx, unsigned char *out,
555557
if (ctx->mode == EVP_CIPH_CFB_MODE) {
556558
XMEMCPY(&ctx->aes.reg, ctx->iv, ctx->ivLen);
557559
while (ok && (inLen > 0)) {
558-
word32 chunk = (inLen > 0xFFFFFFFFU) ? 0xFFFFFFFFU : (word32)inLen;
560+
/* Cap chunk to largest word32 multiple of AES_BLOCK_SIZE so the
561+
* IV/counter state is consistent across chunk boundaries. */
562+
word32 chunk = (inLen > 0xFFFFFFF0U) ? 0xFFFFFFF0U : (word32)inLen;
559563
int rc;
560564
if (ctx->enc) {
561565
rc = wc_AesCfbEncrypt(&ctx->aes, out, in, chunk);

src/wp_cmac.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ static wp_CmacCtx* wp_cmac_new(WOLFPROV_CTX* provCtx)
9090
static void wp_cmac_free(wp_CmacCtx* macCtx)
9191
{
9292
if (macCtx != NULL) {
93+
#ifndef HAVE_FIPS
94+
wc_CmacFree(&macCtx->cmac);
95+
#endif
9396
OPENSSL_cleanse(macCtx->key, macCtx->keyLen);
9497
OPENSSL_clear_free(macCtx, sizeof(*macCtx));
9598
}

src/wp_rsa_asym.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ static int wp_rsaa_decrypt(wp_RsaAsymCtx* ctx, unsigned char* out,
463463
if (ctx->clientVersion <= 0) {
464464
ok = 0;
465465
}
466+
if (ok && (outSize < WOLFSSL_MAX_MASTER_KEY_LENGTH)) {
467+
ok = 0;
468+
}
466469
if (ok) {
467470
byte mask;
468471
byte negMask;

test/test_cipher.c

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,12 +1490,18 @@ int test_aes256_cbc_bad_pad(void *data)
14901490

14911491
/******************************************************************************/
14921492

1493+
#endif /* WP_HAVE_AESCBC */
1494+
1495+
#if defined(WP_HAVE_AESCBC) || defined(WP_HAVE_AESCTR) || \
1496+
defined(WP_HAVE_AESCFB) || defined(WP_HAVE_DES3CBC)
14931497
/**
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).
1498+
* Test cipher encrypt/decrypt roundtrip with a large buffer processed in
1499+
* multiple update calls. Validates the chunked loop path used by
1500+
* wp_aes_block_doit, wp_aes_stream_doit, and wp_des3_block_doit
1501+
* (F-1641, F-1642, F-1643).
14971502
*/
1498-
static int test_aes_cbc_large_update_helper(OSSL_LIB_CTX *libCtx)
1503+
static int test_cipher_large_update_helper(OSSL_LIB_CTX *libCtx,
1504+
const char *cipherName, int keyLen, int ivLen)
14991505
{
15001506
int err;
15011507
EVP_CIPHER_CTX *ctx = NULL;
@@ -1511,11 +1517,13 @@ static int test_aes_cbc_large_update_helper(OSSL_LIB_CTX *libCtx)
15111517
int totalDec = 0;
15121518
size_t i;
15131519

1514-
RAND_bytes(key, sizeof(key));
1515-
RAND_bytes(iv, sizeof(iv));
1520+
RAND_bytes(key, keyLen);
1521+
if (ivLen > 0) {
1522+
RAND_bytes(iv, ivLen);
1523+
}
15161524
RAND_bytes(plain, sizeof(plain));
15171525

1518-
err = (cipher = EVP_CIPHER_fetch(libCtx, "AES-256-CBC", "")) == NULL;
1526+
err = (cipher = EVP_CIPHER_fetch(libCtx, cipherName, "")) == NULL;
15191527

15201528
/* Encrypt in 1024-byte chunks */
15211529
if (err == 0) {
@@ -1575,20 +1583,72 @@ static int test_aes_cbc_large_update_helper(OSSL_LIB_CTX *libCtx)
15751583
EVP_CIPHER_free(cipher);
15761584
return err;
15771585
}
1586+
#endif /* any large-update-testable cipher */
15781587

1588+
#ifdef WP_HAVE_AESCBC
15791589
int test_aes_cbc_large_update(void *data)
15801590
{
15811591
int err;
15821592

15831593
(void)data;
15841594

15851595
PRINT_MSG("AES-CBC large update with OpenSSL");
1586-
err = test_aes_cbc_large_update_helper(osslLibCtx);
1596+
err = test_cipher_large_update_helper(osslLibCtx, "AES-256-CBC", 32, 16);
15871597
if (err == 0) {
15881598
PRINT_MSG("AES-CBC large update with wolfProvider");
1589-
err = test_aes_cbc_large_update_helper(wpLibCtx);
1599+
err = test_cipher_large_update_helper(wpLibCtx, "AES-256-CBC", 32, 16);
15901600
}
15911601
return err;
15921602
}
1593-
15941603
#endif /* WP_HAVE_AESCBC */
1604+
1605+
#ifdef WP_HAVE_AESCTR
1606+
int test_aes_ctr_large_update(void *data)
1607+
{
1608+
int err;
1609+
1610+
(void)data;
1611+
1612+
PRINT_MSG("AES-CTR large update with OpenSSL");
1613+
err = test_cipher_large_update_helper(osslLibCtx, "AES-256-CTR", 32, 16);
1614+
if (err == 0) {
1615+
PRINT_MSG("AES-CTR large update with wolfProvider");
1616+
err = test_cipher_large_update_helper(wpLibCtx, "AES-256-CTR", 32, 16);
1617+
}
1618+
return err;
1619+
}
1620+
#endif /* WP_HAVE_AESCTR */
1621+
1622+
#ifdef WP_HAVE_AESCFB
1623+
int test_aes_cfb_large_update(void *data)
1624+
{
1625+
int err;
1626+
1627+
(void)data;
1628+
1629+
PRINT_MSG("AES-CFB large update with OpenSSL");
1630+
err = test_cipher_large_update_helper(osslLibCtx, "AES-256-CFB", 32, 16);
1631+
if (err == 0) {
1632+
PRINT_MSG("AES-CFB large update with wolfProvider");
1633+
err = test_cipher_large_update_helper(wpLibCtx, "AES-256-CFB", 32, 16);
1634+
}
1635+
return err;
1636+
}
1637+
#endif /* WP_HAVE_AESCFB */
1638+
1639+
#ifdef WP_HAVE_DES3CBC
1640+
int test_des3_cbc_large_update(void *data)
1641+
{
1642+
int err;
1643+
1644+
(void)data;
1645+
1646+
PRINT_MSG("DES3-CBC large update with OpenSSL");
1647+
err = test_cipher_large_update_helper(osslLibCtx, "DES-EDE3-CBC", 24, 8);
1648+
if (err == 0) {
1649+
PRINT_MSG("DES3-CBC large update with wolfProvider");
1650+
err = test_cipher_large_update_helper(wpLibCtx, "DES-EDE3-CBC", 24, 8);
1651+
}
1652+
return err;
1653+
}
1654+
#endif /* WP_HAVE_DES3CBC */

test/unit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ TEST_CASE test_case[] = {
232232
TEST_DECL(test_des3_cbc, NULL),
233233
TEST_DECL(test_des3_cbc_stream, NULL),
234234
TEST_DECL(test_des3_cbc_bad_pad, NULL),
235+
TEST_DECL(test_des3_cbc_large_update, NULL),
235236
#endif
236237
#endif
237238
#ifdef WP_HAVE_AESECB
@@ -257,11 +258,13 @@ TEST_CASE test_case[] = {
257258
TEST_DECL(test_aes128_ctr_stream, NULL),
258259
TEST_DECL(test_aes192_ctr_stream, NULL),
259260
TEST_DECL(test_aes256_ctr_stream, NULL),
261+
TEST_DECL(test_aes_ctr_large_update, NULL),
260262
#endif
261263
#ifdef WP_HAVE_AESCFB
262264
TEST_DECL(test_aes128_cfb_stream, NULL),
263265
TEST_DECL(test_aes192_cfb_stream, NULL),
264266
TEST_DECL(test_aes256_cfb_stream, NULL),
267+
TEST_DECL(test_aes_cfb_large_update, NULL),
265268
#endif
266269
#ifdef WP_HAVE_AESCTS
267270
TEST_DECL(test_aes128_cts, NULL),

test/unit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ int test_krb5kdf(void *data);
153153
int test_des3_cbc(void *data);
154154
int test_des3_cbc_stream(void *data);
155155
int test_des3_cbc_bad_pad(void *data);
156+
int test_des3_cbc_large_update(void *data);
156157
#endif
157158

158159
#ifdef WP_HAVE_AESECB
@@ -185,6 +186,7 @@ int test_aes_cbc_large_update(void *data);
185186
int test_aes128_ctr_stream(void *data);
186187
int test_aes192_ctr_stream(void *data);
187188
int test_aes256_ctr_stream(void *data);
189+
int test_aes_ctr_large_update(void *data);
188190

189191
#endif
190192

@@ -193,6 +195,7 @@ int test_aes256_ctr_stream(void *data);
193195
int test_aes128_cfb_stream(void *data);
194196
int test_aes192_cfb_stream(void *data);
195197
int test_aes256_cfb_stream(void *data);
198+
int test_aes_cfb_large_update(void *data);
196199

197200
#endif
198201

0 commit comments

Comments
 (0)