Skip to content

Commit df709fd

Browse files
committed
[TA-100] Fixed RSA keygen/sign/verify, tests
1 parent f53985a commit df709fd

File tree

3 files changed

+100
-29
lines changed

3 files changed

+100
-29
lines changed

wolfcrypt/src/port/atmel/atmel.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ int wc_Microchip_rsa_create_key(struct RsaKey* key, int size, long e)
880880

881881
/* Private key for signing AND decryption */
882882
ret = talib_handle_init_private_key(&rKeyA, TA_KEY_TYPE_RSA2048,
883-
TA_ALG_MODE_RSA_SSA_1_5, TA_PROP_SIGN_INT_EXT_DIGEST,
883+
TA_ALG_MODE_RSA_SSA_PSS, TA_PROP_SIGN_INT_EXT_DIGEST,
884884
TA_PROP_KEY_AGREEMENT_OUT_BUFF);
885885
if (ret != ATCA_SUCCESS)
886886
return WC_HW_E;
@@ -893,7 +893,7 @@ int wc_Microchip_rsa_create_key(struct RsaKey* key, int size, long e)
893893

894894
/* Public key - use 0, 0 for encryption support! */
895895
ret = talib_handle_init_public_key(&uKeyA, TA_KEY_TYPE_RSA2048,
896-
TA_ALG_MODE_RSA_SSA_1_5, 0, 0);
896+
TA_ALG_MODE_RSA_SSA_PSS, 0, 0);
897897
if (ret != ATCA_SUCCESS)
898898
return WC_HW_E;
899899

@@ -958,24 +958,28 @@ int wc_Microchip_rsa_sign(const byte* in, word32 inLen, byte* out, word32 outLen
958958
{
959959
int ret;
960960
uint16_t sign_size = (uint16_t)outLen;
961-
byte hash_data[WC_SHA256_DIGEST_SIZE];
962961

963962
if (in == NULL || out == NULL || key == NULL) {
964963
return BAD_FUNC_ARG;
965964
}
966965

967-
/* Hash the input message */
968-
ret = wc_Sha256Hash(in, inLen, hash_data);
969-
if (ret != 0) {
970-
return ret;
966+
/* TA100 expects a digest for RSA sign. */
967+
if (inLen != WC_SHA256_DIGEST_SIZE) {
968+
return BAD_FUNC_ARG;
971969
}
972970

973971
/* Sign using the signing private key handle */
974-
ret = talib_sign_external(atcab_get_device(), WOLFSSL_TA_KEY_TYPE_RSA,
975-
key->rKeyH, TA_HANDLE_INPUT_BUFFER, hash_data,
976-
WC_SHA256_DIGEST_SIZE, out, &sign_size);
972+
ret = talib_sign_external(atcab_get_device(),
973+
(uint8_t)(TA_SIGN_MODE_EXTERNAL_MSG |
974+
WOLFSSL_TA_KEY_TYPE_RSA),
975+
key->rKeyH, TA_HANDLE_INPUT_BUFFER, in,
976+
(uint16_t)inLen, out, &sign_size);
977977

978-
return atmel_ecc_translate_err(ret);
978+
ret = atmel_ecc_translate_err(ret);
979+
if (ret == 0) {
980+
return (int)sign_size;
981+
}
982+
return ret;
979983
}
980984

981985

@@ -984,22 +988,20 @@ int wc_Microchip_rsa_verify(const byte* in, word32 inLen, byte* sig, word32 sigL
984988
{
985989
int ret;
986990
bool verified = false;
987-
byte hash_data[WC_SHA256_DIGEST_SIZE];
988991

989992
if (in == NULL || sig == NULL || key == NULL) {
990993
return BAD_FUNC_ARG;
991994
}
992995

993-
/* Hash the input message */
994-
ret = wc_Sha256Hash(in, inLen, hash_data);
995-
if (ret != 0) {
996-
return ret;
996+
/* TA100 expects a digest for RSA verify. */
997+
if (inLen != WC_SHA256_DIGEST_SIZE) {
998+
return BAD_FUNC_ARG;
997999
}
9981000

9991001
/* Verify using the verification public key handle */
10001002
ret = talib_verify(atcab_get_device(), WOLFSSL_TA_KEY_TYPE_RSA,
10011003
TA_HANDLE_INPUT_BUFFER, key->uKeyH, sig,
1002-
sigLen, hash_data, WC_SHA256_DIGEST_SIZE, NULL,
1004+
sigLen, in, (uint16_t)inLen, NULL,
10031005
sigLen, &verified);
10041006

10051007
ret = atmel_ecc_translate_err(ret);

wolfcrypt/src/rsa.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,6 +3393,9 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33933393
else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
33943394
pad_value == RSA_BLOCK_TYPE_1) {
33953395
if (key->rKeyH != 0) {
3396+
if (pad_type != WC_RSA_PSS_PAD) {
3397+
return WC_HW_E;
3398+
}
33963399
return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
33973400
}
33983401
return WC_HW_E;
@@ -3570,6 +3573,9 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35703573
else if (rsa_type == RSA_PUBLIC_DECRYPT &&
35713574
pad_value == RSA_BLOCK_TYPE_1) {
35723575
if (key->uKeyH != 0) {
3576+
if (pad_type != WC_RSA_PSS_PAD) {
3577+
return WC_HW_E;
3578+
}
35733579
int tmp;
35743580
return wc_Microchip_rsa_verify(in, inLen, out, outLen, key, &tmp);
35753581
}
@@ -4269,6 +4275,17 @@ int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
42694275
enum wc_HashType hash, int mgf, RsaKey* key)
42704276
{
42714277
int ret = 0, verify, saltLen, hLen, bits = 0;
4278+
#ifdef WOLFSSL_MICROCHIP_TA100
4279+
if (key != NULL && key->uKeyH != 0) {
4280+
int verified = 0;
4281+
ret = wc_Microchip_rsa_verify(digest, digestLen, in, inLen, key,
4282+
&verified);
4283+
if (ret != 0) {
4284+
return ret;
4285+
}
4286+
return verified ? (int)inLen : SIG_VERIFY_E;
4287+
}
4288+
#endif
42724289

42734290
hLen = wc_HashGetDigestSize(hash);
42744291
if (hLen < 0)
@@ -4318,6 +4335,17 @@ int wc_RsaPSS_VerifyCheck(const byte* in, word32 inLen, byte* out, word32 outLen
43184335
RsaKey* key)
43194336
{
43204337
int ret = 0, verify, saltLen, hLen, bits = 0;
4338+
#ifdef WOLFSSL_MICROCHIP_TA100
4339+
if (key != NULL && key->uKeyH != 0) {
4340+
int verified = 0;
4341+
ret = wc_Microchip_rsa_verify(digest, digestLen, (byte*)in, inLen,
4342+
key, &verified);
4343+
if (ret != 0) {
4344+
return ret;
4345+
}
4346+
return verified ? (int)inLen : SIG_VERIFY_E;
4347+
}
4348+
#endif
43214349

43224350
hLen = wc_HashGetDigestSize(hash);
43234351
if (hLen < 0)

wolfcrypt/test/test.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22006,6 +22006,13 @@ static wc_test_ret_t rsa_flatten_test(RsaKey* key)
2200622006
word32 eSz = sizeof(e);
2200722007
word32 nSz = sizeof(n);
2200822008

22009+
#ifdef WOLFSSL_MICROCHIP_TA100
22010+
/* TA100 keys are hardware-only; flattening isn't supported. */
22011+
if (key != NULL && (key->rKeyH != 0 || key->uKeyH != 0)) {
22012+
return 0;
22013+
}
22014+
#endif
22015+
2200922016
/* Parameter Validation testing. */
2201022017
ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
2201122018
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
@@ -22063,6 +22070,13 @@ static wc_test_ret_t rsa_export_key_test(RsaKey* key)
2206322070
word32 qSz = sizeof(q);
2206422071
word32 zero = 0;
2206522072

22073+
#ifdef WOLFSSL_MICROCHIP_TA100
22074+
/* TA100 keys are hardware-only; exporting components is not supported. */
22075+
if (key != NULL && (key->rKeyH != 0 || key->uKeyH != 0)) {
22076+
return 0;
22077+
}
22078+
#endif
22079+
2206622080
ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
2206722081
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
2206822082
return WC_TEST_RET_ENC_EC(ret);
@@ -22717,6 +22731,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2271722731
const char inStr[] = TEST_STRING;
2271822732
word32 inLen = (word32)TEST_STRING_SZ;
2271922733
word32 outSz;
22734+
word32 sigSz;
2272022735
word32 plainSz;
2272122736
word32 digestSz;
2272222737
int i, j;
@@ -22727,6 +22742,10 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2272722742
int len;
2272822743
#endif
2272922744
byte* plain;
22745+
#ifdef WOLFSSL_MICROCHIP_TA100
22746+
int mgf[] = { WC_MGF1SHA256 };
22747+
enum wc_HashType hash[] = { WC_HASH_TYPE_SHA256 };
22748+
#else
2273022749
int mgf[] = {
2273122750
#ifndef NO_SHA
2273222751
WC_MGF1SHA1,
@@ -22761,6 +22780,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2276122780
WC_HASH_TYPE_SHA512,
2276222781
#endif
2276322782
};
22783+
#endif /* WOLFSSL_MICROCHIP_TA100 */
2276422784

2276522785
WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
2276622786
WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
@@ -22804,11 +22824,29 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2280422824
if (ret <= 0)
2280522825
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
2280622826
outSz = (word32)ret;
22827+
/* Preserve signature length for TA100 verify. */
22828+
sigSz = outSz;
2280722829

2280822830
XMEMCPY(sig, out, outSz);
2280922831
plain = NULL;
2281022832
TEST_SLEEP();
2281122833

22834+
#if defined(WOLFSSL_MICROCHIP_TA100)
22835+
do {
22836+
#if defined(WOLFSSL_ASYNC_CRYPT)
22837+
ret = wc_AsyncWait(ret, &key->asyncDev,
22838+
WC_ASYNC_FLAG_CALL_AGAIN);
22839+
#endif
22840+
if (ret >= 0) {
22841+
ret = wc_RsaPSS_VerifyCheck(sig, sigSz, out, outSz,
22842+
digest, digestSz, hash[j], mgf[i], key);
22843+
}
22844+
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
22845+
if (ret <= 0)
22846+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
22847+
/* TA100 PSS verify done; skip remaining software-only variants. */
22848+
return 0;
22849+
#else
2281222850
do {
2281322851
#if defined(WOLFSSL_ASYNC_CRYPT)
2281422852
ret = wc_AsyncWait(ret, &key->asyncDev,
@@ -22837,6 +22875,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2283722875
#endif
2283822876
if (ret != 0)
2283922877
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
22878+
#endif /* WOLFSSL_MICROCHIP_TA100 */
2284022879

2284122880
#ifdef RSA_PSS_TEST_WRONG_PARAMS
2284222881
for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
@@ -24703,13 +24742,21 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2470324742
}
2470424743
#endif
2470524744
#endif
24745+
#ifdef WOLFSSL_MICROCHIP_TA100
24746+
/* TA100 RSA tests are limited to PSS verify/sign with HW keys. */
24747+
goto ta100_rsa_pss_only;
24748+
#endif
2470624749
#endif /* WOLFSSL_KEY_GEN && WOLFSSL_MICROCHIP_TA100 */
2470724750

2470824751
#ifndef NO_SIG_WRAPPER
2470924752
#ifndef NO_SHA256
24753+
#if !defined(WOLFSSL_MICROCHIP_TA100)
2471024754
ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
2471124755
if (ret != 0)
2471224756
goto exit_rsa;
24757+
#else
24758+
(void)modLen;
24759+
#endif /* !WOLFSSL_MICROCHIP_TA100 */
2471324760
#else /* NO_SHA256 */
2471424761
(void)modLen;
2471524762
#endif /* NO_SHA256 */
@@ -24723,6 +24770,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2472324770

2472424771
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
2472524772
!defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
24773+
#ifndef WOLFSSL_MICROCHIP_TA100
2472624774
do {
2472724775
#if defined(WOLFSSL_ASYNC_CRYPT)
2472824776
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
@@ -24789,18 +24837,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2478924837
ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
2479024838
}
2479124839
TEST_SLEEP();
24792-
24793-
do {
24794-
#if defined(WOLFSSL_ASYNC_CRYPT)
24795-
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
24796-
#endif
24797-
if (ret >= 0) {
24798-
ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
24799-
}
24800-
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
24801-
if (ret < 0)
24802-
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
24803-
TEST_SLEEP();
24840+
#endif /* !WOLFSSL_MICROCHIP_TA100 */
2480424841

2480524842
#elif defined(WOLFSSL_PUBLIC_MP)
2480624843
{
@@ -25145,6 +25182,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2514525182
#endif /* WOLFSSL_CERT_REQ */
2514625183
#endif /* WOLFSSL_CERT_GEN */
2514725184

25185+
#ifdef WOLFSSL_MICROCHIP_TA100
25186+
ta100_rsa_pss_only:
25187+
#endif
25188+
2514825189
#if defined(WC_RSA_PSS) && \
2514925190
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0)) && \
2515025191
!defined(WC_NO_RNG)

0 commit comments

Comments
 (0)