Skip to content

Commit 9118318

Browse files
committed
tests/swdev: add RSA support to wc_swdev
Extend the swdev callback to handle RSA operations: public/private encrypt and decrypt, plus key generation.
1 parent 7f35215 commit 9118318

2 files changed

Lines changed: 48 additions & 10 deletions

File tree

tests/swdev/swdev.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <wolfssl/wolfcrypt/error-crypt.h>
88
#include <wolfssl/wolfcrypt/wc_port.h>
99

10+
#ifndef NO_RSA
11+
#include <wolfssl/wolfcrypt/rsa.h>
12+
#endif
1013
#ifdef HAVE_ECC
1114
#include <wolfssl/wolfcrypt/ecc.h>
1215
#endif
@@ -24,6 +27,31 @@ static int swdev_ensure_init(void)
2427
return 0;
2528
}
2629

30+
#ifndef NO_RSA
31+
static int swdev_rsa(wc_CryptoInfo* info)
32+
{
33+
switch (info->pk.rsa.type) {
34+
case RSA_PUBLIC_ENCRYPT:
35+
case RSA_PUBLIC_DECRYPT:
36+
case RSA_PRIVATE_ENCRYPT:
37+
case RSA_PRIVATE_DECRYPT:
38+
return wc_RsaFunction(info->pk.rsa.in, info->pk.rsa.inLen,
39+
info->pk.rsa.out, info->pk.rsa.outLen, info->pk.rsa.type,
40+
info->pk.rsa.key, info->pk.rsa.rng);
41+
default:
42+
return CRYPTOCB_UNAVAILABLE;
43+
}
44+
}
45+
46+
#ifdef WOLFSSL_KEY_GEN
47+
static int swdev_rsa_keygen(wc_CryptoInfo* info)
48+
{
49+
return wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
50+
info->pk.rsakg.e, info->pk.rsakg.rng);
51+
}
52+
#endif
53+
#endif /* !NO_RSA */
54+
2755
#ifdef HAVE_ECC
2856
static int swdev_ecc_keygen(wc_CryptoInfo* info)
2957
{
@@ -108,9 +136,18 @@ WC_SWDEV_EXPORT int wc_SwDev_Callback(int devId, wc_CryptoInfo* info,
108136
return ret;
109137

110138
switch (info->algo_type) {
111-
#ifdef HAVE_ECC
139+
#if !defined(NO_RSA) || defined(HAVE_ECC)
112140
case WC_ALGO_TYPE_PK:
113141
switch (info->pk.type) {
142+
#ifndef NO_RSA
143+
case WC_PK_TYPE_RSA:
144+
return swdev_rsa(info);
145+
#ifdef WOLFSSL_KEY_GEN
146+
case WC_PK_TYPE_RSA_KEYGEN:
147+
return swdev_rsa_keygen(info);
148+
#endif
149+
#endif /* !NO_RSA */
150+
#ifdef HAVE_ECC
114151
case WC_PK_TYPE_EC_KEYGEN:
115152
return swdev_ecc_keygen(info);
116153
case WC_PK_TYPE_ECDH:
@@ -123,10 +160,11 @@ WC_SWDEV_EXPORT int wc_SwDev_Callback(int devId, wc_CryptoInfo* info,
123160
return swdev_ecc_get_size(info);
124161
case WC_PK_TYPE_EC_GET_SIG_SIZE:
125162
return swdev_ecc_get_sig_size(info);
163+
#endif /* HAVE_ECC */
126164
default:
127165
return CRYPTOCB_UNAVAILABLE;
128166
}
129-
#endif /* HAVE_ECC */
167+
#endif
130168
default:
131169
return CRYPTOCB_UNAVAILABLE;
132170
}

wolfcrypt/test/test.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24118,7 +24118,7 @@ static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG
2411824118
if (ret != WC_NO_ERR_TRACE(SIG_TYPE_E))
2411924119
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_sig);
2412024120
#endif
24121-
#if defined(WOLF_CRYPTO_CB_ONLY_RSA)
24121+
#if defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLFSSL_SWDEV)
2412224122
ret = 0;
2412324123
goto exit_rsa_sig;
2412424124
#endif
@@ -24601,7 +24601,7 @@ static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
2460124601
!defined(WC_NO_RNG)
2460224602
/* Need to create known good signatures to test with this. */
2460324603
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
24604-
!defined(WOLF_CRYPTO_CB_ONLY_RSA)
24604+
(!defined(WOLF_CRYPTO_CB_ONLY_RSA) || defined(WOLFSSL_SWDEV))
2460524605
static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2460624606
{
2460724607
byte digest[WC_MAX_DIGEST_SIZE];
@@ -25983,7 +25983,7 @@ static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
2598325983
int keySz = 2048;
2598425984
#endif
2598525985

25986-
#ifdef WOLF_CRYPTO_CB_ONLY_RSA
25986+
#if defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLFSSL_SWDEV)
2598725987
if (devId == INVALID_DEVID) {
2598825988
/* must call keygen with devId */
2598925989
return 0;
@@ -26086,7 +26086,7 @@ static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
2608626086
#if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
2608726087
(!defined(HAVE_FIPS) || \
2608826088
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
26089-
&& !defined(WOLF_CRYPTO_CB_ONLY_RSA)
26089+
&& (!defined(WOLF_CRYPTO_CB_ONLY_RSA) || defined(WOLFSSL_SWDEV))
2609026090
static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
2609126091
{
2609226092
wc_test_ret_t ret = 0;
@@ -26623,7 +26623,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2662326623
#endif
2662426624

2662526625
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
26626-
!defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
26626+
!defined(WC_NO_RNG) && (!defined(WOLF_CRYPTO_CB_ONLY_RSA) || defined(WOLFSSL_SWDEV))
2662726627
/* Reload the key so the public-encrypt below is the first operation
2662826628
* against it. Exercises backends that distinguish public-only material
2662926629
* from full-keypair bindings: a public-encrypt on a freshly-loaded key
@@ -26751,7 +26751,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2675126751

2675226752
#if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
2675326753
!defined(WOLFSSL_RSA_VERIFY_ONLY) && defined(WOLFSSL_PUBLIC_MP) && \
26754-
!defined(WOLF_CRYPTO_CB_ONLY_RSA)
26754+
(!defined(WOLF_CRYPTO_CB_ONLY_RSA) || defined(WOLFSSL_SWDEV))
2675526755
idx = (word32)ret;
2675626756
XMEMSET(plain, 0, plainSz);
2675726757
do {
@@ -26798,7 +26798,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2679826798
#if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
2679926799
#if (!defined(HAVE_FIPS) || \
2680026800
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
26801-
&& !defined(WOLF_CRYPTO_CB_ONLY_RSA)
26801+
&& (!defined(WOLF_CRYPTO_CB_ONLY_RSA) || defined(WOLFSSL_SWDEV))
2680226802
ret = rsa_oaep_padding_test(key, &rng);
2680326803
if (ret != 0)
2680426804
goto exit_rsa;
@@ -27064,7 +27064,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2706427064
!defined(WC_NO_RNG)
2706527065
/* Need to create known good signatures to test with this. */
2706627066
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
27067-
!defined(WOLF_CRYPTO_CB_ONLY_RSA)
27067+
(!defined(WOLF_CRYPTO_CB_ONLY_RSA) || defined(WOLFSSL_SWDEV))
2706827068
ret = rsa_pss_test(&rng, key);
2706927069
if (ret != 0)
2707027070
goto exit_rsa;

0 commit comments

Comments
 (0)