Skip to content

Commit e0beffe

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

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
@@ -22586,6 +22586,13 @@ static wc_test_ret_t rsa_flatten_test(RsaKey* key)
2258622586
word32 eSz = sizeof(e);
2258722587
word32 nSz = sizeof(n);
2258822588

22589+
#ifdef WOLFSSL_MICROCHIP_TA100
22590+
/* TA100 keys are hardware-only; flattening isn't supported. */
22591+
if (key != NULL && (key->rKeyH != 0 || key->uKeyH != 0)) {
22592+
return 0;
22593+
}
22594+
#endif
22595+
2258922596
/* Parameter Validation testing. */
2259022597
ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
2259122598
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
@@ -22643,6 +22650,13 @@ static wc_test_ret_t rsa_export_key_test(RsaKey* key)
2264322650
word32 qSz = sizeof(q);
2264422651
word32 zero = 0;
2264522652

22653+
#ifdef WOLFSSL_MICROCHIP_TA100
22654+
/* TA100 keys are hardware-only; exporting components is not supported. */
22655+
if (key != NULL && (key->rKeyH != 0 || key->uKeyH != 0)) {
22656+
return 0;
22657+
}
22658+
#endif
22659+
2264622660
ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
2264722661
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
2264822662
return WC_TEST_RET_ENC_EC(ret);
@@ -23313,6 +23327,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2331323327
const char inStr[] = TEST_STRING;
2331423328
word32 inLen = (word32)TEST_STRING_SZ;
2331523329
word32 outSz;
23330+
word32 sigSz;
2331623331
word32 plainSz;
2331723332
word32 digestSz;
2331823333
int i, j;
@@ -23323,6 +23338,10 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2332323338
int len;
2332423339
#endif
2332523340
byte* plain;
23341+
#ifdef WOLFSSL_MICROCHIP_TA100
23342+
int mgf[] = { WC_MGF1SHA256 };
23343+
enum wc_HashType hash[] = { WC_HASH_TYPE_SHA256 };
23344+
#else
2332623345
int mgf[] = {
2332723346
#ifndef NO_SHA
2332823347
WC_MGF1SHA1,
@@ -23357,6 +23376,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2335723376
WC_HASH_TYPE_SHA512,
2335823377
#endif
2335923378
};
23379+
#endif /* WOLFSSL_MICROCHIP_TA100 */
2336023380

2336123381
WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
2336223382
WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
@@ -23400,11 +23420,29 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2340023420
if (ret <= 0)
2340123421
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
2340223422
outSz = (word32)ret;
23423+
/* Preserve signature length for TA100 verify. */
23424+
sigSz = outSz;
2340323425

2340423426
XMEMCPY(sig, out, outSz);
2340523427
plain = NULL;
2340623428
TEST_SLEEP();
2340723429

23430+
#if defined(WOLFSSL_MICROCHIP_TA100)
23431+
do {
23432+
#if defined(WOLFSSL_ASYNC_CRYPT)
23433+
ret = wc_AsyncWait(ret, &key->asyncDev,
23434+
WC_ASYNC_FLAG_CALL_AGAIN);
23435+
#endif
23436+
if (ret >= 0) {
23437+
ret = wc_RsaPSS_VerifyCheck(sig, sigSz, out, outSz,
23438+
digest, digestSz, hash[j], mgf[i], key);
23439+
}
23440+
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
23441+
if (ret <= 0)
23442+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
23443+
/* TA100 PSS verify done; skip remaining software-only variants. */
23444+
return 0;
23445+
#else
2340823446
do {
2340923447
#if defined(WOLFSSL_ASYNC_CRYPT)
2341023448
ret = wc_AsyncWait(ret, &key->asyncDev,
@@ -23433,6 +23471,7 @@ static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
2343323471
#endif
2343423472
if (ret != 0)
2343523473
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
23474+
#endif /* WOLFSSL_MICROCHIP_TA100 */
2343623475

2343723476
#ifdef RSA_PSS_TEST_WRONG_PARAMS
2343823477
for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
@@ -25299,13 +25338,21 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2529925338
}
2530025339
#endif
2530125340
#endif
25341+
#ifdef WOLFSSL_MICROCHIP_TA100
25342+
/* TA100 RSA tests are limited to PSS verify/sign with HW keys. */
25343+
goto ta100_rsa_pss_only;
25344+
#endif
2530225345
#endif /* WOLFSSL_KEY_GEN && WOLFSSL_MICROCHIP_TA100 */
2530325346

2530425347
#ifndef NO_SIG_WRAPPER
2530525348
#ifndef NO_SHA256
25349+
#if !defined(WOLFSSL_MICROCHIP_TA100)
2530625350
ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
2530725351
if (ret != 0)
2530825352
goto exit_rsa;
25353+
#else
25354+
(void)modLen;
25355+
#endif /* !WOLFSSL_MICROCHIP_TA100 */
2530925356
#else /* NO_SHA256 */
2531025357
(void)modLen;
2531125358
#endif /* NO_SHA256 */
@@ -25319,6 +25366,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2531925366

2532025367
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
2532125368
!defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
25369+
#ifndef WOLFSSL_MICROCHIP_TA100
2532225370
do {
2532325371
#if defined(WOLFSSL_ASYNC_CRYPT)
2532425372
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
@@ -25385,18 +25433,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2538525433
ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
2538625434
}
2538725435
TEST_SLEEP();
25388-
25389-
do {
25390-
#if defined(WOLFSSL_ASYNC_CRYPT)
25391-
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
25392-
#endif
25393-
if (ret >= 0) {
25394-
ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
25395-
}
25396-
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
25397-
if (ret < 0)
25398-
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
25399-
TEST_SLEEP();
25436+
#endif /* !WOLFSSL_MICROCHIP_TA100 */
2540025437

2540125438
#elif defined(WOLFSSL_PUBLIC_MP)
2540225439
{
@@ -25741,6 +25778,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
2574125778
#endif /* WOLFSSL_CERT_REQ */
2574225779
#endif /* WOLFSSL_CERT_GEN */
2574325780

25781+
#ifdef WOLFSSL_MICROCHIP_TA100
25782+
ta100_rsa_pss_only:
25783+
#endif
25784+
2574425785
#if defined(WC_RSA_PSS) && \
2574525786
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0)) && \
2574625787
!defined(WC_NO_RNG)

0 commit comments

Comments
 (0)