Skip to content

Commit 7372b16

Browse files
committed
F-2381 - https://fenrir.wolfssl.com/finding/2381 - Add test for HMAC truncated signature rejection
1 parent 32c0251 commit 7372b16

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

tests/pkcs11test.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13490,6 +13490,78 @@ static CK_RV test_hmac_sha256_fail(void* args)
1349013490

1349113491
return ret;
1349213492
}
13493+
13494+
static CK_RV test_hmac_sha256_truncated_sig(void* args)
13495+
{
13496+
CK_SESSION_HANDLE session = *(CK_SESSION_HANDLE*)args;
13497+
CK_RV ret;
13498+
CK_OBJECT_HANDLE key;
13499+
byte data[32], sig[32];
13500+
CK_ULONG dataSz = sizeof(data), sigSz = sizeof(sig);
13501+
CK_MECHANISM mech;
13502+
static unsigned char keyData[] = {
13503+
0x74, 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47,
13504+
0xD6, 0xA6, 0x36, 0xB2, 0x07, 0x32, 0x8E, 0xD0,
13505+
0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4,
13506+
0x81, 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67,
13507+
};
13508+
13509+
memset(data, 9, sizeof(data));
13510+
mech.mechanism = CKM_SHA256_HMAC;
13511+
mech.ulParameterLen = 0;
13512+
mech.pParameter = NULL;
13513+
13514+
ret = get_generic_key(session, keyData, sizeof(keyData), CK_FALSE, &key);
13515+
13516+
/* Sign to get a valid signature */
13517+
if (ret == CKR_OK) {
13518+
ret = funcList->C_SignInit(session, &mech, key);
13519+
CHECK_CKR(ret, "HMAC Sign Init for truncation test");
13520+
}
13521+
if (ret == CKR_OK) {
13522+
ret = funcList->C_Sign(session, data, dataSz, sig, &sigSz);
13523+
CHECK_CKR(ret, "HMAC Sign for truncation test");
13524+
}
13525+
13526+
/* Verify with truncated signature (1 byte) — must fail */
13527+
if (ret == CKR_OK) {
13528+
ret = funcList->C_VerifyInit(session, &mech, key);
13529+
CHECK_CKR(ret, "HMAC Verify Init truncated");
13530+
}
13531+
if (ret == CKR_OK) {
13532+
ret = funcList->C_Verify(session, data, dataSz, sig, 1);
13533+
CHECK_CKR_FAIL(ret, CKR_SIGNATURE_INVALID,
13534+
"Verify with 1-byte truncated HMAC should fail");
13535+
}
13536+
13537+
/* Verify with truncated signature (sigSz - 1) — must fail */
13538+
if (ret == CKR_OK) {
13539+
ret = funcList->C_VerifyInit(session, &mech, key);
13540+
CHECK_CKR(ret, "HMAC Verify Init truncated-1");
13541+
}
13542+
if (ret == CKR_OK) {
13543+
ret = funcList->C_Verify(session, data, dataSz, sig, sigSz - 1);
13544+
CHECK_CKR_FAIL(ret, CKR_SIGNATURE_INVALID,
13545+
"Verify with sigSz-1 truncated HMAC should fail");
13546+
}
13547+
13548+
/* Verify multi-part with truncated signature */
13549+
if (ret == CKR_OK) {
13550+
ret = funcList->C_VerifyInit(session, &mech, key);
13551+
CHECK_CKR(ret, "HMAC Verify Init for multi-part truncated");
13552+
}
13553+
if (ret == CKR_OK) {
13554+
ret = funcList->C_VerifyUpdate(session, data, dataSz);
13555+
CHECK_CKR(ret, "HMAC Verify Update for truncated");
13556+
}
13557+
if (ret == CKR_OK) {
13558+
ret = funcList->C_VerifyFinal(session, sig, 1);
13559+
CHECK_CKR_FAIL(ret, CKR_SIGNATURE_INVALID,
13560+
"VerifyFinal with 1-byte truncated HMAC should fail");
13561+
}
13562+
13563+
return ret;
13564+
}
1349313565
#endif
1349413566
#ifdef WOLFSSL_SHA384
1349513567
static CK_RV test_hmac_sha384(void* args)
@@ -17230,6 +17302,7 @@ static TEST_FUNC testFunc[] = {
1723017302
#ifndef NO_SHA256
1723117303
PKCS11TEST_FUNC_SESS_DECL(test_hmac_sha256),
1723217304
PKCS11TEST_FUNC_SESS_DECL(test_hmac_sha256_fail),
17305+
PKCS11TEST_FUNC_SESS_DECL(test_hmac_sha256_truncated_sig),
1723317306
#endif
1723417307
#ifdef WOLFSSL_SHA384
1723517308
PKCS11TEST_FUNC_SESS_DECL(test_hmac_sha384),

0 commit comments

Comments
 (0)