Skip to content

Commit f6b7144

Browse files
danielinuxtmael
authored andcommitted
[TA-100] Fixed RSA keygen/sign/verify, tests
1 parent c1978ab commit f6b7144

3 files changed

Lines changed: 100 additions & 29 deletions

File tree

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
@@ -3401,6 +3401,9 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
34013401
else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
34023402
pad_value == RSA_BLOCK_TYPE_1) {
34033403
if (key->rKeyH != 0) {
3404+
if (pad_type != WC_RSA_PSS_PAD) {
3405+
return WC_HW_E;
3406+
}
34043407
return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
34053408
}
34063409
return WC_HW_E;
@@ -3578,6 +3581,9 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35783581
else if (rsa_type == RSA_PUBLIC_DECRYPT &&
35793582
pad_value == RSA_BLOCK_TYPE_1) {
35803583
if (key->uKeyH != 0) {
3584+
if (pad_type != WC_RSA_PSS_PAD) {
3585+
return WC_HW_E;
3586+
}
35813587
int tmp;
35823588
return wc_Microchip_rsa_verify(in, inLen, out, outLen, key, &tmp);
35833589
}
@@ -4277,6 +4283,17 @@ int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
42774283
enum wc_HashType hash, int mgf, RsaKey* key)
42784284
{
42794285
int ret = 0, verify, saltLen, hLen, bits = 0;
4286+
#ifdef WOLFSSL_MICROCHIP_TA100
4287+
if (key != NULL && key->uKeyH != 0) {
4288+
int verified = 0;
4289+
ret = wc_Microchip_rsa_verify(digest, digestLen, in, inLen, key,
4290+
&verified);
4291+
if (ret != 0) {
4292+
return ret;
4293+
}
4294+
return verified ? (int)inLen : SIG_VERIFY_E;
4295+
}
4296+
#endif
42804297

42814298
hLen = wc_HashGetDigestSize(hash);
42824299
if (hLen < 0)
@@ -4326,6 +4343,17 @@ int wc_RsaPSS_VerifyCheck(const byte* in, word32 inLen, byte* out, word32 outLen
43264343
RsaKey* key)
43274344
{
43284345
int ret = 0, verify, saltLen, hLen, bits = 0;
4346+
#ifdef WOLFSSL_MICROCHIP_TA100
4347+
if (key != NULL && key->uKeyH != 0) {
4348+
int verified = 0;
4349+
ret = wc_Microchip_rsa_verify(digest, digestLen, (byte*)in, inLen,
4350+
key, &verified);
4351+
if (ret != 0) {
4352+
return ret;
4353+
}
4354+
return verified ? (int)inLen : SIG_VERIFY_E;
4355+
}
4356+
#endif
43294357

43304358
hLen = wc_HashGetDigestSize(hash);
43314359
if (hLen < 0)

wolfcrypt/test/test.c

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

22564+
#ifdef WOLFSSL_MICROCHIP_TA100
22565+
/* TA100 keys are hardware-only; flattening isn't supported. */
22566+
if (key != NULL && (key->rKeyH != 0 || key->uKeyH != 0)) {
22567+
return 0;
22568+
}
22569+
#endif
22570+
2256422571
/* Parameter Validation testing. */
2256522572
ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
2256622573
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
@@ -22618,6 +22625,13 @@ static wc_test_ret_t rsa_export_key_test(RsaKey* key)
2261822625
word32 qSz = sizeof(q);
2261922626
word32 zero = 0;
2262022627

22628+
#ifdef WOLFSSL_MICROCHIP_TA100
22629+
/* TA100 keys are hardware-only; exporting components is not supported. */
22630+
if (key != NULL && (key->rKeyH != 0 || key->uKeyH != 0)) {
22631+
return 0;
22632+
}
22633+
#endif
22634+
2262122635
ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
2262222636
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
2262322637
return WC_TEST_RET_ENC_EC(ret);
@@ -23288,6 +23302,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2328823302
const char inStr[] = TEST_STRING;
2328923303
word32 inLen = (word32)TEST_STRING_SZ;
2329023304
word32 outSz;
23305+
word32 sigSz;
2329123306
word32 plainSz;
2329223307
word32 digestSz;
2329323308
int i, j;
@@ -23298,6 +23313,10 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2329823313
int len;
2329923314
#endif
2330023315
byte* plain;
23316+
#ifdef WOLFSSL_MICROCHIP_TA100
23317+
int mgf[] = { WC_MGF1SHA256 };
23318+
enum wc_HashType hash[] = { WC_HASH_TYPE_SHA256 };
23319+
#else
2330123320
int mgf[] = {
2330223321
#ifndef NO_SHA
2330323322
WC_MGF1SHA1,
@@ -23332,6 +23351,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2333223351
WC_HASH_TYPE_SHA512,
2333323352
#endif
2333423353
};
23354+
#endif /* WOLFSSL_MICROCHIP_TA100 */
2333523355

2333623356
WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
2333723357
WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
@@ -23375,11 +23395,29 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2337523395
if (ret <= 0)
2337623396
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
2337723397
outSz = (word32)ret;
23398+
/* Preserve signature length for TA100 verify. */
23399+
sigSz = outSz;
2337823400

2337923401
XMEMCPY(sig, out, outSz);
2338023402
plain = NULL;
2338123403
TEST_SLEEP();
2338223404

23405+
#if defined(WOLFSSL_MICROCHIP_TA100)
23406+
do {
23407+
#if defined(WOLFSSL_ASYNC_CRYPT)
23408+
ret = wc_AsyncWait(ret, &key->asyncDev,
23409+
WC_ASYNC_FLAG_CALL_AGAIN);
23410+
#endif
23411+
if (ret >= 0) {
23412+
ret = wc_RsaPSS_VerifyCheck(sig, sigSz, out, outSz,
23413+
digest, digestSz, hash[j], mgf[i], key);
23414+
}
23415+
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
23416+
if (ret <= 0)
23417+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
23418+
/* TA100 PSS verify done; skip remaining software-only variants. */
23419+
return 0;
23420+
#else
2338323421
do {
2338423422
#if defined(WOLFSSL_ASYNC_CRYPT)
2338523423
ret = wc_AsyncWait(ret, &key->asyncDev,
@@ -23408,6 +23446,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2340823446
#endif
2340923447
if (ret != 0)
2341023448
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
23449+
#endif /* WOLFSSL_MICROCHIP_TA100 */
2341123450

2341223451
#ifdef RSA_PSS_TEST_WRONG_PARAMS
2341323452
for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
@@ -25274,13 +25313,21 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2527425313
}
2527525314
#endif
2527625315
#endif
25316+
#ifdef WOLFSSL_MICROCHIP_TA100
25317+
/* TA100 RSA tests are limited to PSS verify/sign with HW keys. */
25318+
goto ta100_rsa_pss_only;
25319+
#endif
2527725320
#endif /* WOLFSSL_KEY_GEN && WOLFSSL_MICROCHIP_TA100 */
2527825321

2527925322
#ifndef NO_SIG_WRAPPER
2528025323
#ifndef NO_SHA256
25324+
#if !defined(WOLFSSL_MICROCHIP_TA100)
2528125325
ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
2528225326
if (ret != 0)
2528325327
goto exit_rsa;
25328+
#else
25329+
(void)modLen;
25330+
#endif /* !WOLFSSL_MICROCHIP_TA100 */
2528425331
#else /* NO_SHA256 */
2528525332
(void)modLen;
2528625333
#endif /* NO_SHA256 */
@@ -25294,6 +25341,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2529425341

2529525342
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
2529625343
!defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
25344+
#ifndef WOLFSSL_MICROCHIP_TA100
2529725345
do {
2529825346
#if defined(WOLFSSL_ASYNC_CRYPT)
2529925347
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
@@ -25360,18 +25408,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2536025408
ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
2536125409
}
2536225410
TEST_SLEEP();
25363-
25364-
do {
25365-
#if defined(WOLFSSL_ASYNC_CRYPT)
25366-
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
25367-
#endif
25368-
if (ret >= 0) {
25369-
ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
25370-
}
25371-
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
25372-
if (ret < 0)
25373-
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
25374-
TEST_SLEEP();
25411+
#endif /* !WOLFSSL_MICROCHIP_TA100 */
2537525412

2537625413
#elif defined(WOLFSSL_PUBLIC_MP)
2537725414
{
@@ -25716,6 +25753,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2571625753
#endif /* WOLFSSL_CERT_REQ */
2571725754
#endif /* WOLFSSL_CERT_GEN */
2571825755

25756+
#ifdef WOLFSSL_MICROCHIP_TA100
25757+
ta100_rsa_pss_only:
25758+
#endif
25759+
2571925760
#if defined(WC_RSA_PSS) && \
2572025761
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0)) && \
2572125762
!defined(WC_NO_RNG)

0 commit comments

Comments
 (0)