Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions tests/api/test_evp_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -2861,3 +2861,77 @@ int test_wolfSSL_EVP_PKEY_x448(void)
return EXPECT_RESULT();
}

/*
* Regression test: EVP_DigestSignUpdate and EVP_DigestVerifyUpdate must both
* accept size_t for the byte count.
*
* Before the fix:
* - DigestSignUpdate declared unsigned int (not size_t), breaking FFI parity.
* - DigestVerifyUpdate declared size_t but immediately cast to unsigned int
* internally, silently truncating any count > (word32)-1.
*
* Test vector: RFC 4231 Section 4.2 HMAC-SHA256 (independent oracle).
* Key : "Jefe"
* Data : "what do ya want for nothing?" (28 bytes)
* HMAC : 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
*/
int test_wolfSSL_EVP_DigestSign_size_t_cnt(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_HMAC) && !defined(NO_SHA256)
static const byte kKey[] = "Jefe";
static const byte kMsg[] = "what do ya want for nothing?";
static const byte kExpected[] = {
0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e,
0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7,
0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83,
0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43
};
WOLFSSL_EVP_PKEY *key = NULL;
WOLFSSL_EVP_MD_CTX mdCtx;
unsigned char sig[WC_MAX_DIGEST_SIZE];
size_t sigSz = sizeof(sig);
/* Deliberately size_t -- not unsigned int -- to verify both APIs accept it
* without a cast. This was the type mismatch caught by ZD-21734. */
size_t msgSz = sizeof(kMsg) - 1;

ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
kKey,
(int)sizeof(kKey) - 1));
wolfSSL_EVP_MD_CTX_init(&mdCtx);

/* Sign: passes size_t count directly -- regression for unsigned int decl */
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(),
NULL, key), 1);
ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, kMsg, msgSz), 1);
ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, sig, &sigSz), 1);
ExpectIntEQ((int)sigSz, (int)sizeof(kExpected));
ExpectIntEQ(XMEMCMP(sig, kExpected, sizeof(kExpected)), 0);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);

/* Verify: passes size_t count directly -- regression for silent truncation */
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(),
NULL, key), 1);
ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, kMsg, msgSz), 1);
ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, kExpected,
sizeof(kExpected)), 1);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);

/* Overflow guard: cnt > (word32)-1 must fail, not silently truncate.
* Only reachable on 64-bit platforms where size_t exceeds word32. */
if (sizeof(size_t) > sizeof(word32)) {
size_t oversized = (size_t)(word32)-1 + 1; /* (word32)-1 + 1 */
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(),
NULL, key), 1);
ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, kMsg, oversized),
WOLFSSL_FAILURE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
}

wolfSSL_EVP_PKEY_free(key);
#endif
return EXPECT_RESULT();
} /* END test_wolfSSL_EVP_DigestSign_size_t_cnt */

4 changes: 3 additions & 1 deletion tests/api/test_evp_pkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int test_wolfSSL_CTX_use_PrivateKey_ed25519(void);
int test_wolfSSL_EVP_PKEY_ed448(void);
int test_wolfSSL_EVP_PKEY_x25519(void);
int test_wolfSSL_EVP_PKEY_x448(void);
int test_wolfSSL_EVP_DigestSign_size_t_cnt(void);

#define TEST_EVP_PKEY_DECLS \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_CTX_new_id), \
Expand Down Expand Up @@ -114,6 +115,7 @@ int test_wolfSSL_EVP_PKEY_x448(void);
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_CTX_use_PrivateKey_ed25519), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_ed448), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_x25519), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_x448)
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_x448), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_DigestSign_size_t_cnt)

#endif /* WOLFCRYPT_TEST_EVP_PKEY_H */
10 changes: 6 additions & 4 deletions wolfcrypt/src/evp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4827,10 +4827,12 @@ static int wolfSSL_evp_digest_pk_init(WOLFSSL_EVP_MD_CTX *ctx,
* Update a digest for RSA and ECC keys, or HMAC for HMAC key.
*/
static int wolfssl_evp_digest_pk_update(WOLFSSL_EVP_MD_CTX *ctx,
const void *d, unsigned int cnt)
const void *d, size_t cnt)
{
if (ctx->isHMAC) {
if (wc_HmacUpdate(&ctx->hash.hmac, (const byte *)d, cnt) != 0)
if (cnt > (word32)-1)
return WOLFSSL_FAILURE;
if (wc_HmacUpdate(&ctx->hash.hmac, (const byte *)d, (word32)cnt) != 0)
return WOLFSSL_FAILURE;

return WOLFSSL_SUCCESS;
Expand Down Expand Up @@ -4981,7 +4983,7 @@ int wolfSSL_EVP_DigestSignInit(WOLFSSL_EVP_MD_CTX *ctx,


int wolfSSL_EVP_DigestSignUpdate(WOLFSSL_EVP_MD_CTX *ctx, const void *d,
unsigned int cnt)
size_t cnt)
{
WOLFSSL_ENTER("EVP_DigestSignUpdate");

Expand Down Expand Up @@ -5137,7 +5139,7 @@ int wolfSSL_EVP_DigestVerifyUpdate(WOLFSSL_EVP_MD_CTX *ctx, const void *d,
if (ctx == NULL || d == NULL)
return WOLFSSL_FAILURE;

return wolfssl_evp_digest_pk_update(ctx, d, (unsigned int)cnt);
return wolfssl_evp_digest_pk_update(ctx, d, cnt);
}


Expand Down
2 changes: 1 addition & 1 deletion wolfssl/openssl/evp.h
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ WOLFSSL_API int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx,
WOLFSSL_API int wolfSSL_EVP_DigestFinalXOF(WOLFSSL_EVP_MD_CTX* ctx,
unsigned char* md, size_t sz);
WOLFSSL_API int wolfSSL_EVP_DigestSignUpdate(WOLFSSL_EVP_MD_CTX *ctx,
const void *d, unsigned int cnt);
const void *d, size_t cnt);
WOLFSSL_API int wolfSSL_EVP_DigestSignFinal(WOLFSSL_EVP_MD_CTX *ctx,
unsigned char *sig, size_t *siglen);
WOLFSSL_API int wolfSSL_EVP_DigestSign(WOLFSSL_EVP_MD_CTX *ctx,
Expand Down
Loading