Skip to content

Commit d1c6423

Browse files
make the padding check constant time and move evp exponent print size macro to local file
1 parent ecfd117 commit d1c6423

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

wolfcrypt/src/evp.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ int wolfSSL_EVP_PKEY_is_a(const WOLFSSL_EVP_PKEY *pkey, const char *name) {
318318

319319
#define WOLFSSL_EVP_PKEY_PRINT_LINE_WIDTH_MAX 80
320320
#define WOLFSSL_EVP_PKEY_PRINT_DIGITS_PER_LINE 15
321+
#define WOLFSSL_EVP_EXPONENT_PRINT_MAX 24
321322

322323
static unsigned int cipherType(const WOLFSSL_EVP_CIPHER *cipher);
323324

@@ -11877,7 +11878,7 @@ static int PrintHexWithColon(WOLFSSL_BIO* out, const byte* input,
1187711878
static int PrintPubKeyRSA(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
1187811879
int indent, int bitlen, WOLFSSL_ASN1_PCTX* pctx)
1187911880
{
11880-
byte buff[EVP_EXPONENT_PRINT_MAX] = { 0 };
11881+
byte buff[WOLFSSL_EVP_EXPONENT_PRINT_MAX] = { 0 };
1188111882
int res = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
1188211883
word32 inOutIdx = 0;
1188311884
word32 nSz; /* size of modulus */
@@ -12021,7 +12022,7 @@ static int PrintPubKeyEC(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
1202112022
{
1202212023
byte* pub = NULL;
1202312024
word32 pubSz = 0;
12024-
byte buff[EVP_EXPONENT_PRINT_MAX] = { 0 };
12025+
byte buff[WOLFSSL_EVP_EXPONENT_PRINT_MAX] = { 0 };
1202512026
int res = WOLFSSL_SUCCESS;
1202612027
word32 inOutIdx = 0;
1202712028
int curveId = 0;
@@ -12210,7 +12211,7 @@ static int PrintPubKeyDSA(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
1221012211
int indent, int bitlen, WOLFSSL_ASN1_PCTX* pctx)
1221112212
{
1221212213

12213-
byte buff[EVP_EXPONENT_PRINT_MAX] = { 0 };
12214+
byte buff[WOLFSSL_EVP_EXPONENT_PRINT_MAX] = { 0 };
1221412215
int length;
1221512216
int res = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
1221612217
word32 inOutIdx = 0;
@@ -12417,7 +12418,7 @@ static int PrintPubKeyDH(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
1241712418
int indent, int bitlen, WOLFSSL_ASN1_PCTX* pctx)
1241812419
{
1241912420

12420-
byte buff[EVP_EXPONENT_PRINT_MAX] = { 0 };
12421+
byte buff[WOLFSSL_EVP_EXPONENT_PRINT_MAX] = { 0 };
1242112422
int res = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
1242212423
word32 length;
1242312424
word32 inOutIdx;

wolfcrypt/src/pkcs7.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13263,18 +13263,16 @@ int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1326313263

1326413264
padLen = encryptedContent[encryptedContentSz-1];
1326513265

13266-
/* copy plaintext to output */
13267-
if (padLen == 0 || padLen > expBlockSz ||
13268-
padLen > encryptedContentSz) {
13269-
ret = BUFFER_E;
13270-
break;
13271-
}
13272-
13273-
/* Check all padding bytes. Better implementation would be to run
13274-
* through the entire block. */
13275-
for (padIndex = encryptedContentSz - padLen;
13266+
/* Constant-time padding check */
13267+
padCheck |= ctMaskEq(padLen, 0);
13268+
padCheck |= ctMaskGT(padLen, expBlockSz);
13269+
padCheck |= ctMaskGT(padLen, encryptedContentSz);
13270+
padCheck |= ctMaskGT(expBlockSz, encryptedContentSz);
13271+
for (padIndex = encryptedContentSz - expBlockSz;
1327613272
padIndex < encryptedContentSz; padIndex++) {
13277-
padCheck |= encryptedContent[padIndex] ^ padLen;
13273+
byte inPad = ctMaskGTE(padIndex,
13274+
encryptedContentSz - (int)padLen);
13275+
padCheck |= inPad & (encryptedContent[padIndex] ^ padLen);
1327813276
}
1327913277
if (padCheck != 0) {
1328013278
ret = BUFFER_E;
@@ -15331,19 +15329,16 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1533115329
if (ret == 0) {
1533215330
padLen = encryptedContent[encryptedContentSz-1];
1533315331

15334-
if (padLen == 0 || padLen > expBlockSz ||
15335-
padLen > encryptedContentSz) {
15336-
WOLFSSL_MSG("Bad padding size found");
15337-
ret = BUFFER_E;
15338-
XFREE(encryptedContent, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
15339-
break;
15340-
}
15341-
15342-
/* Check all padding bytes. Better implementation would be to
15343-
* run through the entire block. */
15344-
for (padIndex = encryptedContentSz - padLen;
15332+
/* Constant-time padding check */
15333+
padCheck |= ctMaskEq(padLen, 0);
15334+
padCheck |= ctMaskGT(padLen, expBlockSz);
15335+
padCheck |= ctMaskGT(padLen, encryptedContentSz);
15336+
padCheck |= ctMaskGT(expBlockSz, encryptedContentSz);
15337+
for (padIndex = encryptedContentSz - expBlockSz;
1534515338
padIndex < encryptedContentSz; padIndex++) {
15346-
padCheck |= encryptedContent[padIndex] ^ padLen;
15339+
byte inPad = ctMaskGTE(padIndex,
15340+
encryptedContentSz - (int)padLen);
15341+
padCheck |= inPad & (encryptedContent[padIndex] ^ padLen);
1534715342
}
1534815343
if (padCheck != 0) {
1534915344
WOLFSSL_MSG("Bad padding bytes found");

wolfssl/wolfcrypt/types.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,8 +2423,6 @@ enum Max_ASN {
24232423

24242424
#endif /* WOLFSSL_CERT_GEN */
24252425

2426-
#define EVP_EXPONENT_PRINT_MAX 24
2427-
24282426
#ifdef __cplusplus
24292427
} /* extern "C" */
24302428
#endif

0 commit comments

Comments
 (0)