Skip to content

Commit d044d0d

Browse files
Add unit test for wolfSSH_RsaVerify
1 parent 91cbb64 commit d044d0d

3 files changed

Lines changed: 206 additions & 0 deletions

File tree

src/internal.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11886,6 +11886,16 @@ int wolfSSH_RsaVerify(const byte *sig, word32 sigSz,
1188611886
WOLFSSH_UNUSED(loc); /* Unused when WLOG is not defined */
1188711887
return ret;
1188811888
}
11889+
11890+
#ifdef WOLFSSH_TEST_INTERNAL
11891+
int wolfSSH_TestRsaVerify(const byte* sig, word32 sigSz,
11892+
const byte* encDigest, word32 encDigestSz,
11893+
RsaKey* key, void* heap)
11894+
{
11895+
return wolfSSH_RsaVerify(sig, sigSz, encDigest, encDigestSz,
11896+
key, heap, "wolfSSH_TestRsaVerify");
11897+
}
11898+
#endif /* WOLFSSH_TEST_INTERNAL */
1188911899
#endif /* WOLFSSH_NO_RSA */
1189011900

1189111901

tests/unit.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include <wolfssl/wolfcrypt/random.h>
3737
#include <wolfssl/wolfcrypt/integer.h>
3838
#include <wolfssl/wolfcrypt/hmac.h>
39+
#ifndef WOLFSSH_NO_RSA
40+
#include <wolfssl/wolfcrypt/rsa.h>
41+
#include <wolfssl/wolfcrypt/asn.h>
42+
#endif
3943

4044
#define WOLFSSH_TEST_HEX2BIN
4145
#include <wolfssh/test.h>
@@ -626,6 +630,186 @@ static int test_DhGexGroupValidate(void)
626630
#endif /* WOLFSSH_TEST_INTERNAL && !WOLFSSH_NO_DH_GEX_SHA256 */
627631

628632

633+
#if defined(WOLFSSH_TEST_INTERNAL) && !defined(WOLFSSH_NO_RSA)
634+
635+
/* 2048-bit RSA private key (PKCS#1 DER).
636+
* Same key as tests/auth.c hanselPrivateRsa - copied here so this
637+
* test has no dependency on WOLFSSH_KEYGEN. */
638+
static const byte unitTestRsaPrivKey[] = {
639+
0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
640+
0xbd, 0x3f, 0x76, 0x45, 0xa3, 0x03, 0xac, 0x38, 0xd5, 0xc7, 0x0f, 0x93,
641+
0x30, 0x5a, 0x20, 0x9c, 0x89, 0x7c, 0xad, 0x05, 0x16, 0x46, 0x86, 0x83,
642+
0x0d, 0x8a, 0x2b, 0x16, 0x4a, 0x05, 0x2c, 0xe4, 0x77, 0x47, 0x70, 0x00,
643+
0xae, 0x1d, 0x83, 0xe2, 0xd9, 0x6e, 0x99, 0xd4, 0xf0, 0x45, 0x98, 0x15,
644+
0x93, 0xf6, 0x87, 0x4e, 0xac, 0x64, 0x63, 0xa1, 0x95, 0xc9, 0x7c, 0x30,
645+
0xe8, 0x3e, 0x2f, 0xa3, 0xf1, 0x24, 0x9f, 0x0c, 0x6b, 0x1c, 0xfe, 0x1b,
646+
0x02, 0x99, 0xcd, 0xc6, 0xa7, 0x6c, 0x84, 0x85, 0x46, 0x54, 0x12, 0x40,
647+
0xe1, 0xb4, 0xe5, 0xf2, 0xaa, 0x39, 0xec, 0xd6, 0x27, 0x24, 0x0b, 0xd1,
648+
0xa1, 0xe2, 0xef, 0x34, 0x69, 0x25, 0x6d, 0xc0, 0x74, 0x67, 0x25, 0x98,
649+
0x7d, 0xc4, 0xf8, 0x52, 0xab, 0x9b, 0x4b, 0x3a, 0x12, 0x1d, 0xe1, 0xe3,
650+
0xfa, 0xd6, 0xcf, 0x9a, 0xe6, 0x9c, 0x23, 0x4e, 0x39, 0xc4, 0x84, 0x16,
651+
0x88, 0x3d, 0x42, 0x4e, 0xd8, 0x2f, 0xcc, 0xd2, 0x91, 0x67, 0x9d, 0xb6,
652+
0x71, 0x2a, 0x02, 0x65, 0x5f, 0xbb, 0x75, 0x0e, 0x8c, 0xbb, 0x87, 0x97,
653+
0x97, 0xc6, 0xf8, 0xb2, 0x98, 0xe2, 0x2f, 0x68, 0x26, 0x4a, 0x53, 0xec,
654+
0x79, 0x3a, 0x8a, 0x5f, 0xcc, 0xcf, 0xf0, 0x16, 0x47, 0xb2, 0xd0, 0x43,
655+
0xd6, 0x36, 0x6c, 0xc8, 0xe7, 0x2f, 0xfe, 0xa7, 0x35, 0x39, 0x69, 0xfb,
656+
0x1d, 0x78, 0x45, 0x9d, 0x89, 0x00, 0xc8, 0x41, 0xcf, 0x34, 0x1f, 0xa3,
657+
0xf3, 0xf1, 0xfb, 0x28, 0x14, 0xfb, 0xd8, 0x48, 0x6f, 0xac, 0xe3, 0xfc,
658+
0x33, 0xd1, 0xdb, 0xae, 0xef, 0x27, 0x9e, 0x57, 0x56, 0x29, 0xa2, 0x1a,
659+
0x3a, 0xe5, 0x9a, 0xfe, 0xa4, 0x49, 0xc8, 0x7f, 0xb7, 0x4e, 0xd0, 0x1f,
660+
0x04, 0x6e, 0x58, 0x16, 0xb7, 0xeb, 0x9d, 0xf8, 0x92, 0x3c, 0xc2, 0xb0,
661+
0x21, 0x7c, 0x4e, 0x31, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
662+
0x01, 0x00, 0x8d, 0xa4, 0x61, 0x06, 0x2f, 0xc3, 0x40, 0xf4, 0x6c, 0xf4,
663+
0x87, 0x30, 0xb8, 0x00, 0xcc, 0xe5, 0xbc, 0x75, 0x87, 0x1e, 0x06, 0x95,
664+
0x14, 0x7a, 0x23, 0xf9, 0x24, 0xd4, 0x92, 0xe4, 0x1a, 0xbc, 0x88, 0x95,
665+
0xfc, 0x3b, 0x56, 0x16, 0x1b, 0x2e, 0xff, 0x64, 0x2b, 0x58, 0xd7, 0xd8,
666+
0x8e, 0xc2, 0x9f, 0xb2, 0xe5, 0x84, 0xb9, 0xbc, 0x8d, 0x61, 0x54, 0x35,
667+
0xb0, 0x70, 0xfe, 0x72, 0x04, 0xc0, 0x24, 0x6d, 0x2f, 0x69, 0x61, 0x06,
668+
0x1b, 0x1d, 0xe6, 0x2d, 0x6d, 0x79, 0x60, 0xb7, 0xf4, 0xdb, 0xb7, 0x4e,
669+
0x97, 0x36, 0xde, 0x77, 0xc1, 0x9f, 0x85, 0x4e, 0xc3, 0x77, 0x69, 0x66,
670+
0x2e, 0x3e, 0x61, 0x76, 0xf3, 0x67, 0xfb, 0xc6, 0x9a, 0xc5, 0x6f, 0x99,
671+
0xff, 0xe6, 0x89, 0x43, 0x92, 0x44, 0x75, 0xd2, 0x4e, 0x54, 0x91, 0x58,
672+
0xb2, 0x48, 0x2a, 0xe6, 0xfa, 0x0d, 0x4a, 0xca, 0xd4, 0x14, 0x9e, 0xf6,
673+
0x27, 0x67, 0xb7, 0x25, 0x7a, 0x43, 0xbb, 0x2b, 0x67, 0xd1, 0xfe, 0xd1,
674+
0x68, 0x23, 0x06, 0x30, 0x7c, 0xbf, 0x60, 0x49, 0xde, 0xcc, 0x7e, 0x26,
675+
0x5a, 0x3b, 0xfe, 0xa6, 0xa6, 0xe7, 0xa8, 0xdd, 0xac, 0xb9, 0xaf, 0x82,
676+
0x9a, 0x3a, 0x41, 0x7e, 0x61, 0x21, 0x37, 0xa3, 0x08, 0xe4, 0xc4, 0xbc,
677+
0x11, 0xf5, 0x3b, 0x8e, 0x4d, 0x51, 0xf3, 0xbd, 0xda, 0xba, 0xb2, 0xc5,
678+
0xee, 0xfb, 0xcf, 0xdf, 0x83, 0xa1, 0x82, 0x01, 0xe1, 0x51, 0x9d, 0x07,
679+
0x5a, 0x5d, 0xd8, 0xc7, 0x5b, 0x3f, 0x97, 0x13, 0x6a, 0x4d, 0x1e, 0x8d,
680+
0x39, 0xac, 0x40, 0x95, 0x82, 0x6c, 0xa2, 0xa1, 0xcc, 0x8a, 0x9b, 0x21,
681+
0x32, 0x3a, 0x58, 0xcc, 0xe7, 0x2d, 0x1a, 0x79, 0xa4, 0x31, 0x50, 0xb1,
682+
0x4b, 0x76, 0x23, 0x1b, 0xb3, 0x40, 0x3d, 0x3d, 0x72, 0x72, 0x32, 0xec,
683+
0x5f, 0x38, 0xb5, 0x8d, 0xb2, 0x8d, 0x02, 0x81, 0x81, 0x00, 0xed, 0x5a,
684+
0x7e, 0x8e, 0xa1, 0x62, 0x7d, 0x26, 0x5c, 0x78, 0xc4, 0x87, 0x71, 0xc9,
685+
0x41, 0x57, 0x77, 0x94, 0x93, 0x93, 0x26, 0x78, 0xc8, 0xa3, 0x15, 0xbd,
686+
0x59, 0xcb, 0x1b, 0xb4, 0xb2, 0x6b, 0x0f, 0xe7, 0x80, 0xf2, 0xfa, 0xfc,
687+
0x8e, 0x32, 0xa9, 0x1b, 0x1e, 0x7f, 0xe1, 0x26, 0xef, 0x00, 0x25, 0xd8,
688+
0xdd, 0xc9, 0x1a, 0x23, 0x00, 0x26, 0x3b, 0x46, 0x23, 0xc0, 0x50, 0xe7,
689+
0xce, 0x62, 0xb2, 0x36, 0xb2, 0x98, 0x09, 0x16, 0x34, 0x18, 0x9e, 0x46,
690+
0xbc, 0xaf, 0x2c, 0x28, 0x94, 0x2f, 0xe0, 0x5d, 0xc9, 0xb2, 0xc8, 0xfb,
691+
0x5d, 0x13, 0xd5, 0x36, 0xaa, 0x15, 0x0f, 0x89, 0xa5, 0x16, 0x59, 0x5d,
692+
0x22, 0x74, 0xa4, 0x47, 0x5d, 0xfa, 0xfb, 0x0c, 0x5e, 0x80, 0xbf, 0x0f,
693+
0xc2, 0x9c, 0x95, 0x0f, 0xe7, 0xaa, 0x7f, 0x16, 0x1b, 0xd4, 0xdb, 0x38,
694+
0x7d, 0x58, 0x2e, 0x57, 0x78, 0x2f, 0x02, 0x81, 0x81, 0x00, 0xcc, 0x1d,
695+
0x7f, 0x74, 0x36, 0x6d, 0xb4, 0x92, 0x25, 0x62, 0xc5, 0x50, 0xb0, 0x5c,
696+
0xa1, 0xda, 0xf3, 0xb2, 0xfd, 0x1e, 0x98, 0x0d, 0x8b, 0x05, 0x69, 0x60,
697+
0x8e, 0x5e, 0xd2, 0x89, 0x90, 0x4a, 0x0d, 0x46, 0x7e, 0xe2, 0x54, 0x69,
698+
0xae, 0x16, 0xe6, 0xcb, 0xd5, 0xbd, 0x7b, 0x30, 0x2b, 0x7b, 0x5c, 0xee,
699+
0x93, 0x12, 0xcf, 0x63, 0x89, 0x9c, 0x3d, 0xc8, 0x2d, 0xe4, 0x7a, 0x61,
700+
0x09, 0x5e, 0x80, 0xfb, 0x3c, 0x03, 0xb3, 0x73, 0xd6, 0x98, 0xd0, 0x84,
701+
0x0c, 0x59, 0x9f, 0x4e, 0x80, 0xf3, 0x46, 0xed, 0x03, 0x9d, 0xd5, 0xdc,
702+
0x8b, 0xe7, 0xb1, 0xe8, 0xaa, 0x57, 0xdc, 0xd1, 0x41, 0x55, 0x07, 0xc7,
703+
0xdf, 0x67, 0x3c, 0x72, 0x78, 0xb0, 0x60, 0x8f, 0x85, 0xa1, 0x90, 0x99,
704+
0x0c, 0xa5, 0x67, 0xab, 0xf0, 0xb6, 0x74, 0x90, 0x03, 0x55, 0x7b, 0x5e,
705+
0xcc, 0xc5, 0xbf, 0xde, 0xa7, 0x9f, 0x02, 0x81, 0x80, 0x40, 0x81, 0x6e,
706+
0x91, 0xae, 0xd4, 0x88, 0x74, 0xab, 0x7e, 0xfa, 0xd2, 0x60, 0x9f, 0x34,
707+
0x8d, 0xe3, 0xe6, 0xd2, 0x30, 0x94, 0xad, 0x10, 0xc2, 0x19, 0xbf, 0x6b,
708+
0x2e, 0xe2, 0xe9, 0xb9, 0xef, 0x94, 0xd3, 0xf2, 0xdc, 0x96, 0x4f, 0x9b,
709+
0x09, 0xb3, 0xa1, 0xb6, 0x29, 0x44, 0xf4, 0x82, 0xd1, 0xc4, 0x77, 0x6a,
710+
0xd7, 0x23, 0xae, 0x4d, 0x75, 0x16, 0x78, 0xda, 0x70, 0x82, 0xcc, 0x6c,
711+
0xef, 0xaf, 0xc5, 0x63, 0xc6, 0x23, 0xfa, 0x0f, 0xd0, 0x7c, 0xfb, 0x76,
712+
0x7e, 0x18, 0xff, 0x32, 0x3e, 0xcc, 0xb8, 0x50, 0x7f, 0xb1, 0x55, 0x77,
713+
0x17, 0x53, 0xc3, 0xd6, 0x77, 0x80, 0xd0, 0x84, 0xb8, 0x4d, 0x33, 0x1d,
714+
0x91, 0x1b, 0xb0, 0x75, 0x9f, 0x27, 0x29, 0x56, 0x69, 0xa1, 0x03, 0x54,
715+
0x7d, 0x9f, 0x99, 0x41, 0xf9, 0xb9, 0x2e, 0x36, 0x04, 0x24, 0x4b, 0xf6,
716+
0xec, 0xc7, 0x33, 0x68, 0x6b, 0x02, 0x81, 0x80, 0x60, 0x35, 0xcb, 0x3c,
717+
0xd0, 0xe6, 0xf7, 0x05, 0x28, 0x20, 0x1d, 0x57, 0x82, 0x39, 0xb7, 0x85,
718+
0x07, 0xf7, 0xa7, 0x3d, 0xc3, 0x78, 0x26, 0xbe, 0x3f, 0x44, 0x66, 0xf7,
719+
0x25, 0x0f, 0xf8, 0x76, 0x1f, 0x39, 0xca, 0x57, 0x0e, 0x68, 0xdd, 0xc9,
720+
0x27, 0xb2, 0x8e, 0xa6, 0x08, 0xa9, 0xd4, 0xe5, 0x0a, 0x11, 0xde, 0x3b,
721+
0x30, 0x8b, 0xff, 0x72, 0x28, 0xe0, 0xf1, 0x58, 0xcf, 0xa2, 0x6b, 0x93,
722+
0x23, 0x02, 0xc8, 0xf0, 0x09, 0xa7, 0x21, 0x50, 0xd8, 0x80, 0x55, 0x7d,
723+
0xed, 0x0c, 0x48, 0xd5, 0xe2, 0xe9, 0x97, 0x19, 0xcf, 0x93, 0x6c, 0x52,
724+
0xa2, 0xd6, 0x43, 0x6c, 0xb4, 0xc5, 0xe1, 0xa0, 0x9d, 0xd1, 0x45, 0x69,
725+
0x58, 0xe1, 0xb0, 0x27, 0x9a, 0xec, 0x2b, 0x95, 0xd3, 0x1d, 0x81, 0x0b,
726+
0x7a, 0x09, 0x5e, 0xa5, 0xf1, 0xdd, 0x6b, 0xe4, 0xe0, 0x08, 0xf8, 0x46,
727+
0x81, 0xc1, 0x06, 0x8b, 0x02, 0x81, 0x80, 0x00, 0xf6, 0xf2, 0xeb, 0x25,
728+
0xba, 0x78, 0x04, 0xad, 0x0e, 0x0d, 0x2e, 0xa7, 0x69, 0xd6, 0x57, 0xe6,
729+
0x36, 0x32, 0x50, 0xd2, 0xf2, 0xeb, 0xad, 0x31, 0x46, 0x65, 0xc0, 0x07,
730+
0x97, 0x83, 0x6c, 0x66, 0x27, 0x3e, 0x94, 0x2c, 0x05, 0x01, 0x5f, 0x5c,
731+
0xe0, 0x31, 0x30, 0xec, 0x61, 0xd2, 0x74, 0x35, 0xb7, 0x9f, 0x38, 0xe7,
732+
0x8e, 0x67, 0xb1, 0x50, 0x08, 0x68, 0xce, 0xcf, 0xd8, 0xee, 0x88, 0xfd,
733+
0x5d, 0xc4, 0xcd, 0xe2, 0x86, 0x3d, 0x4a, 0x0e, 0x04, 0x7f, 0xee, 0x8a,
734+
0xe8, 0x9b, 0x16, 0xa1, 0xfc, 0x09, 0x82, 0xe2, 0x62, 0x03, 0x3c, 0xe8,
735+
0x25, 0x7f, 0x3c, 0x9a, 0xaa, 0x83, 0xf8, 0xd8, 0x93, 0xd1, 0x54, 0xf9,
736+
0xce, 0xb4, 0xfa, 0x35, 0x36, 0xcc, 0x18, 0x54, 0xaa, 0xf2, 0x90, 0xb7,
737+
0x7c, 0x97, 0x0b, 0x27, 0x2f, 0xae, 0xfc, 0xc3, 0x93, 0xaf, 0x1a, 0x75,
738+
0xec, 0x18, 0xdb
739+
};
740+
static const word32 unitTestRsaPrivKeySz =
741+
(word32)sizeof(unitTestRsaPrivKey);
742+
743+
/* wolfSSH_RsaVerify unit test
744+
*
745+
* Verifies that wolfSSH_RsaVerify returns WS_RSA_E when given a signature
746+
* whose decoded digest is the correct size but contains wrong content.
747+
* This makes the `compare = ConstantCompare(...)` term in wolfSSH_RsaVerify
748+
* load-bearing: deleting it from the condition would silently pass this test.
749+
*/
750+
static int test_RsaVerify_BadDigest(void)
751+
{
752+
int result = 0;
753+
int ret;
754+
RsaKey key;
755+
WC_RNG rng;
756+
word32 idx = 0;
757+
byte data[32];
758+
byte digest[WC_SHA256_DIGEST_SIZE];
759+
byte encDigest[MAX_ENCODED_SIG_SZ];
760+
int encDigestSz;
761+
byte badEncDigest[MAX_ENCODED_SIG_SZ];
762+
byte sig[256]; /* 2048-bit RSA produces a 256-byte signature */
763+
int sigSz;
764+
765+
WMEMSET(data, 0x42, sizeof(data));
766+
767+
wc_InitRng(&rng);
768+
wc_InitRsaKey(&key, NULL);
769+
770+
ret = wc_RsaPrivateKeyDecode(unitTestRsaPrivKey, &idx, &key,
771+
unitTestRsaPrivKeySz);
772+
if (ret != 0) { result = -500; goto done; }
773+
774+
/* Hash the payload */
775+
ret = wc_Hash(WC_HASH_TYPE_SHA256, data, sizeof(data),
776+
digest, WC_SHA256_DIGEST_SIZE);
777+
if (ret != 0) { result = -501; goto done; }
778+
779+
/* Encode as PKCS#1 v1.5 DigestInfo */
780+
encDigestSz = wc_EncodeSignature(encDigest, digest,
781+
WC_SHA256_DIGEST_SIZE, wc_HashGetOID(WC_HASH_TYPE_SHA256));
782+
if (encDigestSz <= 0) { result = -502; goto done; }
783+
784+
/* Sign */
785+
sigSz = wc_RsaSSL_Sign(encDigest, (word32)encDigestSz,
786+
sig, sizeof(sig), &key, &rng);
787+
if (sigSz <= 0) { result = -503; goto done; }
788+
789+
/* Positive case: correct sig + correct encDigest must succeed */
790+
ret = wolfSSH_TestRsaVerify(sig, (word32)sigSz,
791+
encDigest, (word32)encDigestSz, &key, NULL);
792+
if (ret != WS_SUCCESS) { result = -504; goto done; }
793+
794+
/* Negative case: correct sig but tampered encDigest (same size,
795+
* last byte of the SHA-256 hash flipped) must return WS_RSA_E.
796+
* This is the scenario that deleting `compare` from the condition
797+
* inside wolfSSH_RsaVerify would silently pass. */
798+
WMEMCPY(badEncDigest, encDigest, encDigestSz);
799+
badEncDigest[encDigestSz - 1] ^= 0xFF;
800+
ret = wolfSSH_TestRsaVerify(sig, (word32)sigSz,
801+
badEncDigest, (word32)encDigestSz, &key, NULL);
802+
if (ret != WS_RSA_E) { result = -505; goto done; }
803+
804+
done:
805+
wc_FreeRng(&rng);
806+
wc_FreeRsaKey(&key);
807+
return result;
808+
}
809+
810+
#endif /* WOLFSSH_TEST_INTERNAL && !WOLFSSH_NO_RSA */
811+
812+
629813
/* Error Code And Message Test */
630814

631815
static int test_Errors(void)
@@ -714,6 +898,13 @@ int wolfSSH_UnitTest(int argc, char** argv)
714898
testResult = testResult || unitResult;
715899
#endif
716900

901+
#if defined(WOLFSSH_TEST_INTERNAL) && !defined(WOLFSSH_NO_RSA)
902+
unitResult = test_RsaVerify_BadDigest();
903+
printf("RsaVerify_BadDigest: %s\n",
904+
(unitResult == 0 ? "SUCCESS" : "FAILED"));
905+
testResult = testResult || unitResult;
906+
#endif
907+
717908
#ifdef WOLFSSH_KEYGEN
718909
#ifndef WOLFSSH_NO_RSA
719910
unitResult = test_RsaKeyGen();

wolfssh/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,11 @@ enum WS_MessageIdLimits {
13321332
word32 primeGroupSz, const byte* generator, word32 generatorSz,
13331333
word32 minBits, word32 maxBits, WC_RNG* rng);
13341334
#endif /* !WOLFSSH_NO_DH_GEX_SHA256 */
1335+
#ifndef WOLFSSH_NO_RSA
1336+
WOLFSSH_API int wolfSSH_TestRsaVerify(const byte* sig, word32 sigSz,
1337+
const byte* encDigest, word32 encDigestSz,
1338+
RsaKey* key, void* heap);
1339+
#endif /* !WOLFSSH_NO_RSA */
13351340
#endif /* WOLFSSH_TEST_INTERNAL */
13361341

13371342
/* dynamic memory types */

0 commit comments

Comments
 (0)