Skip to content

Commit 519bed6

Browse files
Add unit test for wolfSSH_RsaVerify
1 parent 90c0ee6 commit 519bed6

3 files changed

Lines changed: 210 additions & 1 deletion

File tree

src/internal.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11929,6 +11929,16 @@ int wolfSSH_RsaVerify(const byte *sig, word32 sigSz,
1192911929
WOLFSSH_UNUSED(loc); /* Unused when WLOG is not defined */
1193011930
return ret;
1193111931
}
11932+
11933+
#ifdef WOLFSSH_TEST_INTERNAL
11934+
int wolfSSH_TestRsaVerify(const byte* sig, word32 sigSz,
11935+
const byte* encDigest, word32 encDigestSz,
11936+
RsaKey* key, void* heap)
11937+
{
11938+
return wolfSSH_RsaVerify(sig, sigSz, encDigest, encDigestSz,
11939+
key, heap, "wolfSSH_TestRsaVerify");
11940+
}
11941+
#endif /* WOLFSSH_TEST_INTERNAL */
1193211942
#endif /* WOLFSSH_NO_RSA */
1193311943

1193411944

tests/unit.c

Lines changed: 195 additions & 1 deletion
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>
@@ -845,8 +849,192 @@ static int test_DoChannelRequest(void)
845849
return result;
846850
}
847851

848-
#endif /* WOLFSSH_TEST_INTERNAL */
852+
#if !defined(WOLFSSH_NO_RSA)
853+
854+
/* 2048-bit RSA private key (PKCS#1 DER).
855+
* Same key as tests/auth.c hanselPrivateRsa - copied here so this
856+
* test has no dependency on WOLFSSH_KEYGEN. */
857+
static const byte unitTestRsaPrivKey[] = {
858+
0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
859+
0xbd, 0x3f, 0x76, 0x45, 0xa3, 0x03, 0xac, 0x38, 0xd5, 0xc7, 0x0f, 0x93,
860+
0x30, 0x5a, 0x20, 0x9c, 0x89, 0x7c, 0xad, 0x05, 0x16, 0x46, 0x86, 0x83,
861+
0x0d, 0x8a, 0x2b, 0x16, 0x4a, 0x05, 0x2c, 0xe4, 0x77, 0x47, 0x70, 0x00,
862+
0xae, 0x1d, 0x83, 0xe2, 0xd9, 0x6e, 0x99, 0xd4, 0xf0, 0x45, 0x98, 0x15,
863+
0x93, 0xf6, 0x87, 0x4e, 0xac, 0x64, 0x63, 0xa1, 0x95, 0xc9, 0x7c, 0x30,
864+
0xe8, 0x3e, 0x2f, 0xa3, 0xf1, 0x24, 0x9f, 0x0c, 0x6b, 0x1c, 0xfe, 0x1b,
865+
0x02, 0x99, 0xcd, 0xc6, 0xa7, 0x6c, 0x84, 0x85, 0x46, 0x54, 0x12, 0x40,
866+
0xe1, 0xb4, 0xe5, 0xf2, 0xaa, 0x39, 0xec, 0xd6, 0x27, 0x24, 0x0b, 0xd1,
867+
0xa1, 0xe2, 0xef, 0x34, 0x69, 0x25, 0x6d, 0xc0, 0x74, 0x67, 0x25, 0x98,
868+
0x7d, 0xc4, 0xf8, 0x52, 0xab, 0x9b, 0x4b, 0x3a, 0x12, 0x1d, 0xe1, 0xe3,
869+
0xfa, 0xd6, 0xcf, 0x9a, 0xe6, 0x9c, 0x23, 0x4e, 0x39, 0xc4, 0x84, 0x16,
870+
0x88, 0x3d, 0x42, 0x4e, 0xd8, 0x2f, 0xcc, 0xd2, 0x91, 0x67, 0x9d, 0xb6,
871+
0x71, 0x2a, 0x02, 0x65, 0x5f, 0xbb, 0x75, 0x0e, 0x8c, 0xbb, 0x87, 0x97,
872+
0x97, 0xc6, 0xf8, 0xb2, 0x98, 0xe2, 0x2f, 0x68, 0x26, 0x4a, 0x53, 0xec,
873+
0x79, 0x3a, 0x8a, 0x5f, 0xcc, 0xcf, 0xf0, 0x16, 0x47, 0xb2, 0xd0, 0x43,
874+
0xd6, 0x36, 0x6c, 0xc8, 0xe7, 0x2f, 0xfe, 0xa7, 0x35, 0x39, 0x69, 0xfb,
875+
0x1d, 0x78, 0x45, 0x9d, 0x89, 0x00, 0xc8, 0x41, 0xcf, 0x34, 0x1f, 0xa3,
876+
0xf3, 0xf1, 0xfb, 0x28, 0x14, 0xfb, 0xd8, 0x48, 0x6f, 0xac, 0xe3, 0xfc,
877+
0x33, 0xd1, 0xdb, 0xae, 0xef, 0x27, 0x9e, 0x57, 0x56, 0x29, 0xa2, 0x1a,
878+
0x3a, 0xe5, 0x9a, 0xfe, 0xa4, 0x49, 0xc8, 0x7f, 0xb7, 0x4e, 0xd0, 0x1f,
879+
0x04, 0x6e, 0x58, 0x16, 0xb7, 0xeb, 0x9d, 0xf8, 0x92, 0x3c, 0xc2, 0xb0,
880+
0x21, 0x7c, 0x4e, 0x31, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
881+
0x01, 0x00, 0x8d, 0xa4, 0x61, 0x06, 0x2f, 0xc3, 0x40, 0xf4, 0x6c, 0xf4,
882+
0x87, 0x30, 0xb8, 0x00, 0xcc, 0xe5, 0xbc, 0x75, 0x87, 0x1e, 0x06, 0x95,
883+
0x14, 0x7a, 0x23, 0xf9, 0x24, 0xd4, 0x92, 0xe4, 0x1a, 0xbc, 0x88, 0x95,
884+
0xfc, 0x3b, 0x56, 0x16, 0x1b, 0x2e, 0xff, 0x64, 0x2b, 0x58, 0xd7, 0xd8,
885+
0x8e, 0xc2, 0x9f, 0xb2, 0xe5, 0x84, 0xb9, 0xbc, 0x8d, 0x61, 0x54, 0x35,
886+
0xb0, 0x70, 0xfe, 0x72, 0x04, 0xc0, 0x24, 0x6d, 0x2f, 0x69, 0x61, 0x06,
887+
0x1b, 0x1d, 0xe6, 0x2d, 0x6d, 0x79, 0x60, 0xb7, 0xf4, 0xdb, 0xb7, 0x4e,
888+
0x97, 0x36, 0xde, 0x77, 0xc1, 0x9f, 0x85, 0x4e, 0xc3, 0x77, 0x69, 0x66,
889+
0x2e, 0x3e, 0x61, 0x76, 0xf3, 0x67, 0xfb, 0xc6, 0x9a, 0xc5, 0x6f, 0x99,
890+
0xff, 0xe6, 0x89, 0x43, 0x92, 0x44, 0x75, 0xd2, 0x4e, 0x54, 0x91, 0x58,
891+
0xb2, 0x48, 0x2a, 0xe6, 0xfa, 0x0d, 0x4a, 0xca, 0xd4, 0x14, 0x9e, 0xf6,
892+
0x27, 0x67, 0xb7, 0x25, 0x7a, 0x43, 0xbb, 0x2b, 0x67, 0xd1, 0xfe, 0xd1,
893+
0x68, 0x23, 0x06, 0x30, 0x7c, 0xbf, 0x60, 0x49, 0xde, 0xcc, 0x7e, 0x26,
894+
0x5a, 0x3b, 0xfe, 0xa6, 0xa6, 0xe7, 0xa8, 0xdd, 0xac, 0xb9, 0xaf, 0x82,
895+
0x9a, 0x3a, 0x41, 0x7e, 0x61, 0x21, 0x37, 0xa3, 0x08, 0xe4, 0xc4, 0xbc,
896+
0x11, 0xf5, 0x3b, 0x8e, 0x4d, 0x51, 0xf3, 0xbd, 0xda, 0xba, 0xb2, 0xc5,
897+
0xee, 0xfb, 0xcf, 0xdf, 0x83, 0xa1, 0x82, 0x01, 0xe1, 0x51, 0x9d, 0x07,
898+
0x5a, 0x5d, 0xd8, 0xc7, 0x5b, 0x3f, 0x97, 0x13, 0x6a, 0x4d, 0x1e, 0x8d,
899+
0x39, 0xac, 0x40, 0x95, 0x82, 0x6c, 0xa2, 0xa1, 0xcc, 0x8a, 0x9b, 0x21,
900+
0x32, 0x3a, 0x58, 0xcc, 0xe7, 0x2d, 0x1a, 0x79, 0xa4, 0x31, 0x50, 0xb1,
901+
0x4b, 0x76, 0x23, 0x1b, 0xb3, 0x40, 0x3d, 0x3d, 0x72, 0x72, 0x32, 0xec,
902+
0x5f, 0x38, 0xb5, 0x8d, 0xb2, 0x8d, 0x02, 0x81, 0x81, 0x00, 0xed, 0x5a,
903+
0x7e, 0x8e, 0xa1, 0x62, 0x7d, 0x26, 0x5c, 0x78, 0xc4, 0x87, 0x71, 0xc9,
904+
0x41, 0x57, 0x77, 0x94, 0x93, 0x93, 0x26, 0x78, 0xc8, 0xa3, 0x15, 0xbd,
905+
0x59, 0xcb, 0x1b, 0xb4, 0xb2, 0x6b, 0x0f, 0xe7, 0x80, 0xf2, 0xfa, 0xfc,
906+
0x8e, 0x32, 0xa9, 0x1b, 0x1e, 0x7f, 0xe1, 0x26, 0xef, 0x00, 0x25, 0xd8,
907+
0xdd, 0xc9, 0x1a, 0x23, 0x00, 0x26, 0x3b, 0x46, 0x23, 0xc0, 0x50, 0xe7,
908+
0xce, 0x62, 0xb2, 0x36, 0xb2, 0x98, 0x09, 0x16, 0x34, 0x18, 0x9e, 0x46,
909+
0xbc, 0xaf, 0x2c, 0x28, 0x94, 0x2f, 0xe0, 0x5d, 0xc9, 0xb2, 0xc8, 0xfb,
910+
0x5d, 0x13, 0xd5, 0x36, 0xaa, 0x15, 0x0f, 0x89, 0xa5, 0x16, 0x59, 0x5d,
911+
0x22, 0x74, 0xa4, 0x47, 0x5d, 0xfa, 0xfb, 0x0c, 0x5e, 0x80, 0xbf, 0x0f,
912+
0xc2, 0x9c, 0x95, 0x0f, 0xe7, 0xaa, 0x7f, 0x16, 0x1b, 0xd4, 0xdb, 0x38,
913+
0x7d, 0x58, 0x2e, 0x57, 0x78, 0x2f, 0x02, 0x81, 0x81, 0x00, 0xcc, 0x1d,
914+
0x7f, 0x74, 0x36, 0x6d, 0xb4, 0x92, 0x25, 0x62, 0xc5, 0x50, 0xb0, 0x5c,
915+
0xa1, 0xda, 0xf3, 0xb2, 0xfd, 0x1e, 0x98, 0x0d, 0x8b, 0x05, 0x69, 0x60,
916+
0x8e, 0x5e, 0xd2, 0x89, 0x90, 0x4a, 0x0d, 0x46, 0x7e, 0xe2, 0x54, 0x69,
917+
0xae, 0x16, 0xe6, 0xcb, 0xd5, 0xbd, 0x7b, 0x30, 0x2b, 0x7b, 0x5c, 0xee,
918+
0x93, 0x12, 0xcf, 0x63, 0x89, 0x9c, 0x3d, 0xc8, 0x2d, 0xe4, 0x7a, 0x61,
919+
0x09, 0x5e, 0x80, 0xfb, 0x3c, 0x03, 0xb3, 0x73, 0xd6, 0x98, 0xd0, 0x84,
920+
0x0c, 0x59, 0x9f, 0x4e, 0x80, 0xf3, 0x46, 0xed, 0x03, 0x9d, 0xd5, 0xdc,
921+
0x8b, 0xe7, 0xb1, 0xe8, 0xaa, 0x57, 0xdc, 0xd1, 0x41, 0x55, 0x07, 0xc7,
922+
0xdf, 0x67, 0x3c, 0x72, 0x78, 0xb0, 0x60, 0x8f, 0x85, 0xa1, 0x90, 0x99,
923+
0x0c, 0xa5, 0x67, 0xab, 0xf0, 0xb6, 0x74, 0x90, 0x03, 0x55, 0x7b, 0x5e,
924+
0xcc, 0xc5, 0xbf, 0xde, 0xa7, 0x9f, 0x02, 0x81, 0x80, 0x40, 0x81, 0x6e,
925+
0x91, 0xae, 0xd4, 0x88, 0x74, 0xab, 0x7e, 0xfa, 0xd2, 0x60, 0x9f, 0x34,
926+
0x8d, 0xe3, 0xe6, 0xd2, 0x30, 0x94, 0xad, 0x10, 0xc2, 0x19, 0xbf, 0x6b,
927+
0x2e, 0xe2, 0xe9, 0xb9, 0xef, 0x94, 0xd3, 0xf2, 0xdc, 0x96, 0x4f, 0x9b,
928+
0x09, 0xb3, 0xa1, 0xb6, 0x29, 0x44, 0xf4, 0x82, 0xd1, 0xc4, 0x77, 0x6a,
929+
0xd7, 0x23, 0xae, 0x4d, 0x75, 0x16, 0x78, 0xda, 0x70, 0x82, 0xcc, 0x6c,
930+
0xef, 0xaf, 0xc5, 0x63, 0xc6, 0x23, 0xfa, 0x0f, 0xd0, 0x7c, 0xfb, 0x76,
931+
0x7e, 0x18, 0xff, 0x32, 0x3e, 0xcc, 0xb8, 0x50, 0x7f, 0xb1, 0x55, 0x77,
932+
0x17, 0x53, 0xc3, 0xd6, 0x77, 0x80, 0xd0, 0x84, 0xb8, 0x4d, 0x33, 0x1d,
933+
0x91, 0x1b, 0xb0, 0x75, 0x9f, 0x27, 0x29, 0x56, 0x69, 0xa1, 0x03, 0x54,
934+
0x7d, 0x9f, 0x99, 0x41, 0xf9, 0xb9, 0x2e, 0x36, 0x04, 0x24, 0x4b, 0xf6,
935+
0xec, 0xc7, 0x33, 0x68, 0x6b, 0x02, 0x81, 0x80, 0x60, 0x35, 0xcb, 0x3c,
936+
0xd0, 0xe6, 0xf7, 0x05, 0x28, 0x20, 0x1d, 0x57, 0x82, 0x39, 0xb7, 0x85,
937+
0x07, 0xf7, 0xa7, 0x3d, 0xc3, 0x78, 0x26, 0xbe, 0x3f, 0x44, 0x66, 0xf7,
938+
0x25, 0x0f, 0xf8, 0x76, 0x1f, 0x39, 0xca, 0x57, 0x0e, 0x68, 0xdd, 0xc9,
939+
0x27, 0xb2, 0x8e, 0xa6, 0x08, 0xa9, 0xd4, 0xe5, 0x0a, 0x11, 0xde, 0x3b,
940+
0x30, 0x8b, 0xff, 0x72, 0x28, 0xe0, 0xf1, 0x58, 0xcf, 0xa2, 0x6b, 0x93,
941+
0x23, 0x02, 0xc8, 0xf0, 0x09, 0xa7, 0x21, 0x50, 0xd8, 0x80, 0x55, 0x7d,
942+
0xed, 0x0c, 0x48, 0xd5, 0xe2, 0xe9, 0x97, 0x19, 0xcf, 0x93, 0x6c, 0x52,
943+
0xa2, 0xd6, 0x43, 0x6c, 0xb4, 0xc5, 0xe1, 0xa0, 0x9d, 0xd1, 0x45, 0x69,
944+
0x58, 0xe1, 0xb0, 0x27, 0x9a, 0xec, 0x2b, 0x95, 0xd3, 0x1d, 0x81, 0x0b,
945+
0x7a, 0x09, 0x5e, 0xa5, 0xf1, 0xdd, 0x6b, 0xe4, 0xe0, 0x08, 0xf8, 0x46,
946+
0x81, 0xc1, 0x06, 0x8b, 0x02, 0x81, 0x80, 0x00, 0xf6, 0xf2, 0xeb, 0x25,
947+
0xba, 0x78, 0x04, 0xad, 0x0e, 0x0d, 0x2e, 0xa7, 0x69, 0xd6, 0x57, 0xe6,
948+
0x36, 0x32, 0x50, 0xd2, 0xf2, 0xeb, 0xad, 0x31, 0x46, 0x65, 0xc0, 0x07,
949+
0x97, 0x83, 0x6c, 0x66, 0x27, 0x3e, 0x94, 0x2c, 0x05, 0x01, 0x5f, 0x5c,
950+
0xe0, 0x31, 0x30, 0xec, 0x61, 0xd2, 0x74, 0x35, 0xb7, 0x9f, 0x38, 0xe7,
951+
0x8e, 0x67, 0xb1, 0x50, 0x08, 0x68, 0xce, 0xcf, 0xd8, 0xee, 0x88, 0xfd,
952+
0x5d, 0xc4, 0xcd, 0xe2, 0x86, 0x3d, 0x4a, 0x0e, 0x04, 0x7f, 0xee, 0x8a,
953+
0xe8, 0x9b, 0x16, 0xa1, 0xfc, 0x09, 0x82, 0xe2, 0x62, 0x03, 0x3c, 0xe8,
954+
0x25, 0x7f, 0x3c, 0x9a, 0xaa, 0x83, 0xf8, 0xd8, 0x93, 0xd1, 0x54, 0xf9,
955+
0xce, 0xb4, 0xfa, 0x35, 0x36, 0xcc, 0x18, 0x54, 0xaa, 0xf2, 0x90, 0xb7,
956+
0x7c, 0x97, 0x0b, 0x27, 0x2f, 0xae, 0xfc, 0xc3, 0x93, 0xaf, 0x1a, 0x75,
957+
0xec, 0x18, 0xdb
958+
};
959+
static const word32 unitTestRsaPrivKeySz =
960+
(word32)sizeof(unitTestRsaPrivKey);
849961

962+
/* wolfSSH_RsaVerify unit test
963+
*
964+
* Verifies that wolfSSH_RsaVerify returns WS_RSA_E when given a signature
965+
* whose decoded digest is the correct size but contains wrong content.
966+
* This makes the `compare = ConstantCompare(...)` term in wolfSSH_RsaVerify
967+
* load-bearing: deleting it from the condition would silently pass this test.
968+
*/
969+
static int test_RsaVerify_BadDigest(void)
970+
{
971+
int result = 0;
972+
int ret;
973+
RsaKey key;
974+
WC_RNG rng;
975+
word32 idx = 0;
976+
byte data[32];
977+
byte digest[WC_SHA256_DIGEST_SIZE];
978+
byte encDigest[MAX_ENCODED_SIG_SZ];
979+
int encDigestSz;
980+
byte badEncDigest[MAX_ENCODED_SIG_SZ];
981+
byte sig[256]; /* 2048-bit RSA produces a 256-byte signature */
982+
int sigSz;
983+
984+
WMEMSET(data, 0x42, sizeof(data));
985+
986+
if (wc_InitRng(&rng) != 0) {
987+
printf("RsaVerify_BadDigest: wc_InitRng failed\n");
988+
return -500;
989+
}
990+
if (wc_InitRsaKey(&key, NULL) != 0) {
991+
printf("RsaVerify_BadDigest: wc_InitRsaKey failed\n");
992+
wc_FreeRng(&rng);
993+
return -501;
994+
}
995+
996+
ret = wc_RsaPrivateKeyDecode(unitTestRsaPrivKey, &idx, &key,
997+
unitTestRsaPrivKeySz);
998+
if (ret != 0) { result = -502; goto done; }
999+
1000+
/* Hash the payload */
1001+
ret = wc_Hash(WC_HASH_TYPE_SHA256, data, sizeof(data),
1002+
digest, WC_SHA256_DIGEST_SIZE);
1003+
if (ret != 0) { result = -503; goto done; }
1004+
1005+
/* Encode as PKCS#1 v1.5 DigestInfo */
1006+
encDigestSz = wc_EncodeSignature(encDigest, digest,
1007+
WC_SHA256_DIGEST_SIZE, wc_HashGetOID(WC_HASH_TYPE_SHA256));
1008+
if (encDigestSz <= 0) { result = -504; goto done; }
1009+
1010+
/* Sign */
1011+
sigSz = wc_RsaSSL_Sign(encDigest, (word32)encDigestSz,
1012+
sig, sizeof(sig), &key, &rng);
1013+
if (sigSz <= 0) { result = -505; goto done; }
1014+
1015+
/* Positive case: correct sig + correct encDigest must succeed */
1016+
ret = wolfSSH_TestRsaVerify(sig, (word32)sigSz,
1017+
encDigest, (word32)encDigestSz, &key, NULL);
1018+
if (ret != WS_SUCCESS) { result = -506; goto done; }
1019+
1020+
/* Negative case: correct sig but tampered encDigest (same size,
1021+
* last byte of the SHA-256 hash flipped) must return WS_RSA_E.
1022+
* This is the scenario that deleting `compare` from the condition
1023+
* inside wolfSSH_RsaVerify would silently pass. */
1024+
WMEMCPY(badEncDigest, encDigest, encDigestSz);
1025+
badEncDigest[encDigestSz - 1] ^= 0xFF;
1026+
ret = wolfSSH_TestRsaVerify(sig, (word32)sigSz,
1027+
badEncDigest, (word32)encDigestSz, &key, NULL);
1028+
if (ret != WS_RSA_E) { result = -507; goto done; }
1029+
1030+
done:
1031+
wc_FreeRng(&rng);
1032+
wc_FreeRsaKey(&key);
1033+
return result;
1034+
}
1035+
1036+
#endif /* !WOLFSSH_NO_RSA */
1037+
#endif /* WOLFSSH_TEST_INTERNAL */
8501038

8511039
/* Error Code And Message Test */
8521040

@@ -944,6 +1132,12 @@ int wolfSSH_UnitTest(int argc, char** argv)
9441132
unitResult = test_DoChannelRequest();
9451133
printf("DoChannelRequest: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
9461134
testResult = testResult || unitResult;
1135+
#if !defined(WOLFSSH_NO_RSA)
1136+
unitResult = test_RsaVerify_BadDigest();
1137+
printf("RsaVerify_BadDigest: %s\n",
1138+
(unitResult == 0 ? "SUCCESS" : "FAILED"));
1139+
testResult = testResult || unitResult;
1140+
#endif
9471141
#endif
9481142

9491143
#ifdef WOLFSSH_KEYGEN

wolfssh/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,11 @@ enum WS_MessageIdLimits {
13391339
word32 primeGroupSz, const byte* generator, word32 generatorSz,
13401340
word32 minBits, word32 maxBits, WC_RNG* rng);
13411341
#endif /* !WOLFSSH_NO_DH_GEX_SHA256 */
1342+
#ifndef WOLFSSH_NO_RSA
1343+
WOLFSSH_API int wolfSSH_TestRsaVerify(const byte* sig, word32 sigSz,
1344+
const byte* encDigest, word32 encDigestSz,
1345+
RsaKey* key, void* heap);
1346+
#endif /* !WOLFSSH_NO_RSA */
13421347
#endif /* WOLFSSH_TEST_INTERNAL */
13431348

13441349
/* dynamic memory types */

0 commit comments

Comments
 (0)