diff --git a/tests/api/test_dh.c b/tests/api/test_dh.c index 07c0ea30f8a..ce05d0b4304 100644 --- a/tests/api/test_dh.c +++ b/tests/api/test_dh.c @@ -695,6 +695,7 @@ int test_wc_DhAgree_nonblock(void) byte alicePriv[TEST_DH_BUF_SIZE], alicePub[TEST_DH_BUF_SIZE]; byte bobPriv[TEST_DH_BUF_SIZE], bobPub[TEST_DH_BUF_SIZE]; byte agree[TEST_DH_BUF_SIZE]; + const byte zeroPriv[1] = { 0x00 }; word32 alicePrivSz = sizeof(alicePriv), alicePubSz = sizeof(alicePub); word32 bobPrivSz = sizeof(bobPriv), bobPubSz = sizeof(bobPub); word32 agreeSz; @@ -742,6 +743,20 @@ int test_wc_DhAgree_nonblock(void) } ExpectIntEQ(ret, 0); + /* a zero private value makes the agreement 1, which must be rejected */ + ret = WC_NO_ERR_TRACE(MP_WOULDBLOCK); + for (rounds = 0; ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK) && rounds < 200000; + rounds++) { + agreeSz = sizeof(agree); + ret = wc_DhAgree(&aliceKey, agree, &agreeSz, zeroPriv, + (word32)sizeof(zeroPriv), bobPub, bobPubSz); + } +#ifdef WOLFSSL_VALIDATE_FFC_IMPORT + ExpectIntEQ(ret, WC_NO_ERR_TRACE(DH_CHECK_PRIV_E)); +#else + ExpectIntEQ(ret, WC_NO_ERR_TRACE(MP_VAL)); +#endif + /* disable non-blocking mode (nb == NULL) */ ExpectIntEQ(wc_DhSetNonBlock(&aliceKey, NULL), 0); agreeSz = sizeof(agree); diff --git a/tests/api/test_rsa.c b/tests/api/test_rsa.c index 2ea2fb28c86..abd15cdfe19 100644 --- a/tests/api/test_rsa.c +++ b/tests/api/test_rsa.c @@ -3693,3 +3693,94 @@ int test_wc_RsaUnPad_OaepBlockLen(void) #endif return EXPECT_RESULT(); } + +/* + * Non-blocking RSA private operations are refused without an RNG and match the + * blocking result with one. The fastmath backend leaves no ladder values or + * blinding inverse in the RsaNb context once complete. + */ +int test_wc_RsaNonBlock_Blinding(void) +{ + EXPECT_DECLS; +#if !defined(NO_RSA) && defined(WC_RSA_NONBLOCK) && defined(WC_RSA_BLINDING) && \ + !defined(WC_NO_RNG) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(HAVE_FIPS) && \ + defined(USE_CERT_BUFFERS_2048) && \ + (defined(USE_FAST_MATH) || (defined(WOLFSSL_SP_NONBLOCK) && \ + (defined(RSA_LOW_MEM) || defined(SP_RSA_PRIVATE_EXP_D)))) + RsaKey key; + RsaNb nb; + WC_RNG rng; + const byte in[] = TEST_STRING; + const word32 inLen = (word32)TEST_STRING_SZ; + byte sigRef[256]; + byte sigNb[256]; + byte plain[256]; + word32 idx = 0; + int ret; +#if defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_NONBLOCK) + fp_digit acc = 0; + int blindYield = 0; + int i; + int j; +#endif + + XMEMSET(&key, 0, sizeof(key)); + XMEMSET(&rng, 0, sizeof(rng)); + + ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0); + ExpectIntEQ(wc_InitRng(&rng), 0); + ExpectIntEQ(wc_RsaPrivateKeyDecode(client_key_der_2048, &idx, &key, + (word32)sizeof_client_key_der_2048), 0); + + /* PKCS#1 v1.5 signatures are deterministic: blocking reference. */ + ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, sigRef, sizeof(sigRef), &key, &rng), + (int)sizeof(sigRef)); + + ExpectIntEQ(wc_RsaSetNonBlock(&key, &nb), 0); + ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, sigNb, sizeof(sigNb), &key, NULL), + WC_NO_ERR_TRACE(MISSING_RNG_E)); + + do { + ret = wc_RsaSSL_Sign(in, inLen, sigNb, sizeof(sigNb), &key, &rng); + #if defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_NONBLOCK) + /* r is only held while r^e is computed. */ + if ((ret == WC_NO_ERR_TRACE(FP_WOULDBLOCK)) && (nb.rnd.used != 0) && + (nb.exptmod.state != TFM_EXPTMOD_NB_INIT)) { + blindYield = 1; + } + #endif + } while (ret == WC_NO_ERR_TRACE(FP_WOULDBLOCK)); + ExpectIntEQ(ret, (int)sizeof(sigNb)); + ExpectBufEQ(sigNb, sigRef, sizeof(sigRef)); + +#if defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_NONBLOCK) + ExpectIntEQ(blindYield, 1); + for (i = 0; i < (int)(sizeof(nb.exptmod.R) / sizeof(nb.exptmod.R[0])); + i++) { + acc |= (fp_digit)nb.exptmod.R[i].used; + for (j = 0; j < FP_SIZE; j++) { + acc |= nb.exptmod.R[i].dp[j]; + } + } + for (j = 0; j < FP_SIZE; j++) { + acc |= nb.tmp.dp[j] | nb.rnd.dp[j] | nb.rndi.dp[j]; + } + acc |= nb.exptmod.buf; + ExpectIntEQ(acc == 0, 1); + ExpectIntEQ(nb.exptmod.state, TFM_EXPTMOD_NB_INIT); +#endif + + do { + ret = wc_RsaSSL_Verify(sigNb, sizeof(sigNb), plain, sizeof(plain), + &key); + } while (ret == WC_NO_ERR_TRACE(FP_WOULDBLOCK)); + ExpectIntEQ(ret, (int)inLen); + ExpectBufEQ(plain, in, inLen); + + ExpectIntEQ(wc_RsaSetNonBlock(&key, NULL), 0); + DoExpectIntEQ(wc_FreeRsaKey(&key), 0); + DoExpectIntEQ(wc_FreeRng(&rng), 0); +#endif + return EXPECT_RESULT(); +} diff --git a/tests/api/test_rsa.h b/tests/api/test_rsa.h index 57607e607d8..d6b89d39b66 100644 --- a/tests/api/test_rsa.h +++ b/tests/api/test_rsa.h @@ -59,6 +59,7 @@ int test_wc_CryptoCb_RsaPssVerify(void); int test_wc_CryptoCb_RsaPssVerifyRecover(void); int test_wc_MakeRsaKey_id_dev(void); int test_wc_FreeRsaKey_CryptoCb(void); +int test_wc_RsaNonBlock_Blinding(void); #define TEST_RSA_DECLS \ TEST_DECL_GROUP("rsa", test_wc_InitRsaKey), \ @@ -95,6 +96,7 @@ int test_wc_FreeRsaKey_CryptoCb(void); TEST_DECL_GROUP("rsa", test_wc_CryptoCb_RsaPssVerify), \ TEST_DECL_GROUP("rsa", test_wc_CryptoCb_RsaPssVerifyRecover), \ TEST_DECL_GROUP("rsa", test_wc_MakeRsaKey_id_dev), \ - TEST_DECL_GROUP("rsa", test_wc_FreeRsaKey_CryptoCb) + TEST_DECL_GROUP("rsa", test_wc_FreeRsaKey_CryptoCb), \ + TEST_DECL_GROUP("rsa", test_wc_RsaNonBlock_Blinding) #endif /* WOLFCRYPT_TEST_RSA_H */ diff --git a/tests/api/test_wolfmath.c b/tests/api/test_wolfmath.c index c4bf291b5da..be9e42c7199 100644 --- a/tests/api/test_wolfmath.c +++ b/tests/api/test_wolfmath.c @@ -1751,3 +1751,176 @@ int test_mp_exptmod_neg_base(void) #endif return EXPECT_RESULT(); } /* End test_mp_exptmod_neg_base */ + +#if defined(USE_FAST_MATH) && defined(WOLFSSL_PUBLIC_MP) && \ + defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) && \ + !defined(WOLFSSL_DEBUG_MEMORY) && !defined(WOLFSSL_NO_MALLOC) && \ + !defined(WOLFSSL_SMALL_STACK) +/* Only the exponentiation table spans more than one fp_int on the heap when + * small stack is off. Count those freed while still holding non-zero bytes. */ +#define FP_TABLE_TRACK_MAX 4 +static void* fp_table_ptr[FP_TABLE_TRACK_MAX]; +static size_t fp_table_sz[FP_TABLE_TRACK_MAX]; +static int fp_table_seen = 0; +static int fp_table_dirty = 0; + +static void* fp_table_malloc(size_t sz) +{ + int i; + void* p = malloc(sz); + + if ((p != NULL) && (sz >= 2 * sizeof(fp_int))) { + for (i = 0; i < FP_TABLE_TRACK_MAX; i++) { + if (fp_table_ptr[i] == NULL) { + fp_table_ptr[i] = p; + fp_table_sz[i] = sz; + fp_table_seen++; + break; + } + } + } + return p; +} + +static void fp_table_free(void* p) +{ + int i; + size_t j; + + for (i = 0; i < FP_TABLE_TRACK_MAX; i++) { + if ((p != NULL) && (fp_table_ptr[i] == p)) { + for (j = 0; j < fp_table_sz[i]; j++) { + if (((byte*)p)[j] != 0) { + fp_table_dirty++; + break; + } + } + fp_table_ptr[i] = NULL; + break; + } + } + free(p); +} + +static void* fp_table_realloc(void* p, size_t sz) +{ + return realloc(p, sz); +} +#endif + +/* + * Testing that the fastmath windowed exponentiation wipes its table of powers + * before freeing it. + */ +int test_mp_exptmod_nct_zeroize(void) +{ + EXPECT_DECLS; +#if defined(USE_FAST_MATH) && defined(WOLFSSL_PUBLIC_MP) && \ + defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) && \ + !defined(WOLFSSL_DEBUG_MEMORY) && !defined(WOLFSSL_NO_MALLOC) && \ + !defined(WOLFSSL_SMALL_STACK) + wolfSSL_Malloc_cb prevM = NULL; + wolfSSL_Free_cb prevF = NULL; + wolfSSL_Realloc_cb prevR = NULL; + mp_int g; + mp_int x; + mp_int p; + mp_int y; + byte val[128]; + + XMEMSET(&g, 0, sizeof(mp_int)); + XMEMSET(&x, 0, sizeof(mp_int)); + XMEMSET(&p, 0, sizeof(mp_int)); + XMEMSET(&y, 0, sizeof(mp_int)); + XMEMSET(fp_table_ptr, 0, sizeof(fp_table_ptr)); + fp_table_seen = 0; + fp_table_dirty = 0; + + ExpectIntEQ(mp_init_multi(&g, &x, &p, &y, NULL, NULL), MP_OKAY); + XMEMSET(val, 0xFF, sizeof(val)); + ExpectIntEQ(mp_read_unsigned_bin(&p, val, (word32)sizeof(val)), MP_OKAY); + XMEMSET(val, 0xC3, sizeof(val)); + ExpectIntEQ(mp_read_unsigned_bin(&x, val, (word32)sizeof(val)), MP_OKAY); + XMEMSET(val, 0xA5, sizeof(val)); + ExpectIntEQ(mp_read_unsigned_bin(&g, val, (word32)sizeof(val)), MP_OKAY); + + ExpectIntEQ(wolfSSL_GetAllocators(&prevM, &prevF, &prevR), 0); + ExpectIntEQ(wolfSSL_SetAllocators(fp_table_malloc, fp_table_free, + fp_table_realloc), 0); + ExpectIntEQ(mp_exptmod_nct(&g, &x, &p, &y), MP_OKAY); + (void)wolfSSL_SetAllocators(prevM, prevF, prevR); + + ExpectIntGT(fp_table_seen, 0); + ExpectIntEQ(fp_table_dirty, 0); + + mp_clear(&g); + mp_clear(&x); + mp_clear(&p); + mp_clear(&y); +#endif + return EXPECT_RESULT(); +} + +/* + * Testing that the constant time Montgomery reduction gives the same result + * as the variable time one. + */ +int test_mp_montgomery_reduce_ct(void) +{ + EXPECT_DECLS; +#if defined(USE_FAST_MATH) && defined(WOLFSSL_PUBLIC_MP) && !defined(WC_NO_RNG) + WC_RNG rng; + mp_int m; + mp_int a; + mp_int b; + mp_int r; + mp_digit mp = 0; + byte val[256]; + word32 len; + int i; + + XMEMSET(&rng, 0, sizeof(rng)); + XMEMSET(&m, 0, sizeof(mp_int)); + XMEMSET(&a, 0, sizeof(mp_int)); + XMEMSET(&b, 0, sizeof(mp_int)); + XMEMSET(&r, 0, sizeof(mp_int)); + + ExpectIntEQ(wc_InitRng(&rng), 0); + ExpectIntEQ(mp_init_multi(&m, &a, &b, &r, NULL, NULL), MP_OKAY); + + for (i = 0; (i < 64) && EXPECT_SUCCESS(); i++) { + /* Odd modulus of a varying length, 2048 bits at most. */ + len = (word32)sizeof(val) - (word32)(i % 4) * 32; + ExpectIntEQ(wc_RNG_GenerateBlock(&rng, val, sizeof(val)), 0); + val[0] |= 0x80; + val[len - 1] |= 0x01; + ExpectIntEQ(mp_read_unsigned_bin(&m, val, len), MP_OKAY); + ExpectIntEQ(mp_montgomery_setup(&m, &mp), MP_OKAY); + + /* a = x * y with x and y less than m. */ + ExpectIntEQ(wc_RNG_GenerateBlock(&rng, val, sizeof(val)), 0); + ExpectIntEQ(mp_read_unsigned_bin(&a, val, sizeof(val)), MP_OKAY); + ExpectIntEQ(mp_mod(&a, &m, &a), MP_OKAY); + ExpectIntEQ(wc_RNG_GenerateBlock(&rng, val, sizeof(val)), 0); + ExpectIntEQ(mp_read_unsigned_bin(&b, val, sizeof(val)), MP_OKAY); + ExpectIntEQ(mp_mod(&b, &m, &b), MP_OKAY); + if ((i % 8) == 0) { + /* Small values exercise the carry handling. */ + mp_set(&b, (mp_digit)(i + 1)); + } + ExpectIntEQ(mp_mul(&a, &b, &a), MP_OKAY); + ExpectIntEQ(mp_copy(&a, &r), MP_OKAY); + + ExpectIntEQ(mp_montgomery_reduce_ex(&a, &m, mp, 0), MP_OKAY); + ExpectIntEQ(mp_montgomery_reduce_ct(&r, &m, mp), MP_OKAY); + ExpectIntEQ(mp_cmp(&a, &r), MP_EQ); + } + + mp_clear(&m); + mp_clear(&a); + mp_clear(&b); + mp_clear(&r); + DoExpectIntEQ(wc_FreeRng(&rng), 0); +#endif + return EXPECT_RESULT(); +} diff --git a/tests/api/test_wolfmath.h b/tests/api/test_wolfmath.h index 9b9f8d09536..cb8e454a44b 100644 --- a/tests/api/test_wolfmath.h +++ b/tests/api/test_wolfmath.h @@ -43,6 +43,8 @@ int test_wc_SpIntArithDecisionCoverage(void); int test_wc_SpIntConvDecisionCoverage(void); int test_wc_SpIntExptGcdDecisionCoverage(void); int test_mp_exptmod_neg_base(void); +int test_mp_exptmod_nct_zeroize(void); +int test_mp_montgomery_reduce_ct(void); #define TEST_WOLFMATH_DECLS \ TEST_DECL_GROUP("wolfmath", test_mp_get_digit_count), \ @@ -63,6 +65,8 @@ int test_mp_exptmod_neg_base(void); TEST_DECL_GROUP("wolfmath", test_wc_SpIntArithDecisionCoverage), \ TEST_DECL_GROUP("wolfmath", test_wc_SpIntConvDecisionCoverage), \ TEST_DECL_GROUP("wolfmath", test_wc_SpIntExptGcdDecisionCoverage), \ - TEST_DECL_GROUP("wolfmath", test_mp_exptmod_neg_base) + TEST_DECL_GROUP("wolfmath", test_mp_exptmod_neg_base), \ + TEST_DECL_GROUP("wolfmath", test_mp_exptmod_nct_zeroize), \ + TEST_DECL_GROUP("wolfmath", test_mp_montgomery_reduce_ct) #endif /* WOLFCRYPT_TEST_WOLFMATH_H */ diff --git a/tests/unit-mcdc/test_rsa_whitebox.c b/tests/unit-mcdc/test_rsa_whitebox.c index 75e7e8ced54..40ad9bbfa75 100644 --- a/tests/unit-mcdc/test_rsa_whitebox.c +++ b/tests/unit-mcdc/test_rsa_whitebox.c @@ -574,18 +574,18 @@ static void wb_rsa_function_nonblock(void) /* key==NULL -> idx0 true */ outLen = sizeof(out); (void)wc_RsaFunctionNonBlock(in, sizeof(in), out, &outLen, - RSA_PUBLIC_ENCRYPT, NULL); + RSA_PUBLIC_ENCRYPT, NULL, NULL); /* key!=NULL but key->nb==NULL (never attached) -> idx1 true */ outLen = sizeof(out); (void)wc_RsaFunctionNonBlock(in, sizeof(in), out, &outLen, - RSA_PUBLIC_ENCRYPT, &key); + RSA_PUBLIC_ENCRYPT, &key, NULL); /* all-false: nb attached, guard passes into the state machine */ if (wc_RsaSetNonBlock(&key, &nb) == 0) { outLen = sizeof(out); (void)wc_RsaFunctionNonBlock(in, sizeof(in), out, &outLen, - RSA_PUBLIC_ENCRYPT, &key); + RSA_PUBLIC_ENCRYPT, &key, NULL); } /* rsa.c:3377 - wc_RsaDirect()'s "skip cleanup while still pending" guard. diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 00e60b80128..33d534fe20b 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -2251,6 +2251,12 @@ static int wc_DhAgree_Sync(DhKey* key, byte* agree, word32* agreeSz, if (nb_ret != WC_NO_ERR_TRACE(MP_WOULDBLOCK)) { key->nb->pubKeyValidated = 0; } + /* make sure agree is > 1 (SP800-56A, 5.7.1.1) */ + if ((nb_ret == 0) && + ((*agreeSz == 0) || ((*agreeSz == 1) && (agree[0] == 1)))) { + ForceZero(agree, *agreeSz); + nb_ret = MP_VAL; + } return nb_ret; } /* size not nb-supported - the blocking path below completes in diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index e7a2bd8ae0d..e9a852d365f 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -2496,12 +2496,197 @@ PRAGMA_DIAG_POP #endif #ifdef WC_RSA_NONBLOCK +#if defined(USE_FAST_MATH) && defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) +enum { + RSA_NB_BLIND_START = 0, + RSA_NB_BLIND_EXPT_E, + RSA_NB_BLIND_EXPT_D, + RSA_NB_BLIND_UNBLIND +}; +#ifndef WOLFSSL_RSA_PUBLIC_ONLY + #define RSA_NB_BLINDING +#endif +#endif + +#ifdef RSA_NB_BLINDING +/* Pick a random r into key->nb->rnd and keep r^-1 in key->nb->rndi. */ +static int RsaNbBlind(RsaKey* key, WC_RNG* rng) +{ + int ret = 0; + DECL_MP_INT_SIZE_DYN(mask, mp_bitsused(&key->n), RSA_MAX_SIZE); + + NEW_MP_INT_SIZE(mask, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); +#ifdef MP_INT_SIZE_CHECK_NULL + if (mask == NULL) { + return MEMORY_E; + } +#endif + + if ((INIT_MP_INT_SIZE(mask, mp_bitsused(&key->n)) != MP_OKAY) || + (mp_init(&key->nb->rnd) != MP_OKAY) || + (mp_init(&key->nb->rndi) != MP_OKAY)) { + ret = MP_INIT_E; + } + + if (ret == 0) { + ret = mp_rand(&key->nb->rnd, mp_get_digit_count(&key->n), rng); + } + /* rndi = (rnd*mask)^-1 * mask so the inversion does not see rnd. */ + if (ret == 0) { + ret = mp_rand(mask, mp_get_digit_count(&key->n), rng); + } + if (ret == 0) { + if (mp_mulmod(&key->nb->rnd, mask, &key->n, &key->nb->rndi) != + MP_OKAY) { + ret = MP_MULMOD_E; + } + } + if (ret == 0) { + if (mp_invmod(&key->nb->rndi, &key->n, &key->nb->rndi) != MP_OKAY) { + ret = MP_INVMOD_E; + } + } + if (ret == 0) { + if (mp_mulmod(&key->nb->rndi, mask, &key->n, &key->nb->rndi) != + MP_OKAY) { + ret = MP_MULMOD_E; + } + } + + mp_forcezero(mask); + FREE_MP_INT_SIZE(mask, key->heap, DYNAMIC_TYPE_RSA); + return ret; +} + +/* Remove the blinding: tmp = tmp * r^-1 mod n, in Montgomery form. */ +static int RsaNbUnblind(RsaKey* key) +{ + int ret = 0; + mp_digit mp = 0; + DECL_MP_INT_SIZE_DYN(norm, mp_bitsused(&key->n), RSA_MAX_SIZE); + + NEW_MP_INT_SIZE(norm, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); +#ifdef MP_INT_SIZE_CHECK_NULL + if (norm == NULL) { + return MEMORY_E; + } +#endif + + if (INIT_MP_INT_SIZE(norm, mp_bitsused(&key->n)) != MP_OKAY) { + ret = MP_INIT_E; + } + if ((ret == 0) && (mp_montgomery_setup(&key->n, &mp) != MP_OKAY)) { + ret = MP_MULMOD_E; + } + if ((ret == 0) && (mp_montgomery_calc_normalization(norm, &key->n) != + MP_OKAY)) { + ret = MP_MULMOD_E; + } + if ((ret == 0) && (mp_mul(&key->nb->rndi, norm, &key->nb->rndi) != + MP_OKAY)) { + ret = MP_MULMOD_E; + } + if ((ret == 0) && (mp_mod(&key->nb->rndi, &key->n, &key->nb->rndi) != + MP_OKAY)) { + ret = MP_MULMOD_E; + } + if ((ret == 0) && (mp_mul(&key->nb->tmp, &key->nb->rndi, &key->nb->tmp) != + MP_OKAY)) { + ret = MP_MULMOD_E; + } + if ((ret == 0) && (mp_montgomery_reduce_ct(&key->nb->tmp, &key->n, mp) != + MP_OKAY)) { + ret = MP_MULMOD_E; + } + + mp_forcezero(norm); + FREE_MP_INT_SIZE(norm, key->heap, DYNAMIC_TYPE_RSA); + return ret; +} + +/* One step of a blinded private operation, leaving the result in + * key->nb->tmp. Returns FP_WOULDBLOCK until it completes. + * START: tmp = in, pick r and r^-1 + * EXPT_E: r = r^e mod n a step per call, then tmp = tmp * r mod n + * EXPT_D: tmp = tmp^d mod n a step per call + * UNBLIND: tmp = tmp * r^-1 mod n + */ +static int RsaNbPrivateBlinded(RsaKey* key, const byte* in, word32 inLen, + WC_RNG* rng) +{ + int ret = 0; + + switch (key->nb->blindState) { + case RSA_NB_BLIND_START: + if (rng == NULL) { + return MISSING_RNG_E; + } + if (mp_init(&key->nb->tmp) != MP_OKAY) { + ret = MP_INIT_E; + } + if ((ret == 0) && (mp_read_unsigned_bin(&key->nb->tmp, in, inLen) != + MP_OKAY)) { + ret = MP_READ_E; + } + if (ret == 0) { + ret = RsaNbBlind(key, rng); + } + if (ret == 0) { + key->nb->blindState = RSA_NB_BLIND_EXPT_E; + ret = FP_WOULDBLOCK; + } + break; + case RSA_NB_BLIND_EXPT_E: + ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->rnd, &key->e, + &key->n, &key->nb->rnd); + if (ret == FP_WOULDBLOCK) { + break; + } + if (ret != MP_OKAY) { + ret = MP_EXPTMOD_E; + } + else if (mp_mulmod(&key->nb->tmp, &key->nb->rnd, &key->n, + &key->nb->tmp) != MP_OKAY) { + ret = MP_MULMOD_E; + } + mp_forcezero(&key->nb->rnd); + if (ret == 0) { + key->nb->blindState = RSA_NB_BLIND_EXPT_D; + ret = FP_WOULDBLOCK; + } + break; + case RSA_NB_BLIND_EXPT_D: + ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->tmp, &key->d, + &key->n, &key->nb->tmp); + if (ret == FP_WOULDBLOCK) { + break; + } + if (ret != MP_OKAY) { + ret = MP_EXPTMOD_E; + } + else { + key->nb->blindState = RSA_NB_BLIND_UNBLIND; + ret = FP_WOULDBLOCK; + } + break; + case RSA_NB_BLIND_UNBLIND: + ret = RsaNbUnblind(key); + break; + default: + ret = BAD_STATE_E; + break; + } + + return ret; +} +#endif /* RSA_NB_BLINDING */ + static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, - word32* outLen, int type, RsaKey* key) + word32* outLen, int type, RsaKey* key, WC_RNG* rng) { int ret = 0; #ifdef USE_FAST_MATH - word32 keyLen, len; + word32 keyLen; #endif /* SP non-blocking RSA wrappers depend on sp__mod_exp__nb, * which the SP generator only emits when (!RSA_PUBLIC_ONLY || @@ -2513,6 +2698,8 @@ static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, int bits; #endif + (void)rng; + if (key == NULL || key->nb == NULL) { return BAD_FUNC_ARG; } @@ -2521,6 +2708,12 @@ static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) && \ (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_HAVE_SP_DH)) bits = mp_count_bits(&key->n); +#if defined(WC_RSA_BLINDING) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) + if ((rng == NULL) && ((type == RSA_PRIVATE_DECRYPT) || + (type == RSA_PRIVATE_ENCRYPT))) { + return MISSING_RNG_E; + } +#endif #ifndef WOLFSSL_SP_NO_2048 if (bits == 2048) { if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) { @@ -2563,6 +2756,14 @@ static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, #endif /* SP nonblock RSA */ #ifdef USE_FAST_MATH +#ifdef RSA_NB_BLINDING + if ((type == RSA_PRIVATE_DECRYPT) || (type == RSA_PRIVATE_ENCRYPT)) { + ret = RsaNbPrivateBlinded(key, in, inLen, rng); + if (ret == FP_WOULDBLOCK) + return ret; + } + else +#endif if (key->nb->exptmod.state == TFM_EXPTMOD_NB_INIT) { if (mp_init(&key->nb->tmp) != MP_OKAY) { ret = MP_INIT_E; @@ -2580,12 +2781,14 @@ static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) case RSA_PRIVATE_DECRYPT: case RSA_PRIVATE_ENCRYPT: + #ifndef RSA_NB_BLINDING ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->tmp, &key->d, &key->n, &key->nb->tmp); if (ret == FP_WOULDBLOCK) return ret; if (ret != MP_OKAY) ret = MP_EXPTMOD_E; + #endif break; #endif case RSA_PUBLIC_ENCRYPT: @@ -2609,23 +2812,19 @@ static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, ret = RSA_BUFFER_E; } if (ret == 0) { - len = mp_unsigned_bin_size(&key->nb->tmp); - - /* pad front w/ zeros to match key length */ - while (len < keyLen) { - *out++ = 0x00; - len++; - } - *outLen = keyLen; - - /* convert */ - if (mp_to_unsigned_bin(&key->nb->tmp, out) != MP_OKAY) { - ret = MP_TO_E; + if (mp_to_unsigned_bin_len_ct(&key->nb->tmp, out, (int)keyLen) != + MP_OKAY) { + ret = MP_TO_E; } } - mp_clear(&key->nb->tmp); + mp_forcezero(&key->nb->tmp); +#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) + mp_forcezero(&key->nb->rnd); + mp_forcezero(&key->nb->rndi); + key->nb->blindState = RSA_NB_BLIND_START; +#endif #else /* No non-blocking backend available for this build. The SP non-block * dispatch above only matches enabled key sizes; if we reach this @@ -3478,7 +3677,8 @@ static int wc_RsaFunctionAsync(const byte* in, word32 inLen, byte* out, * MP_WOULDBLOCK) translates per-yield MP_WOULDBLOCK into WC_PENDING_E * so the TLS / async event loop can drive the operation to completion. */ if (key->nb != NULL) { - return wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key); + return wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key, + rng); } #endif @@ -3782,7 +3982,8 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out, /* Direct (non-async) nonblock dispatch - the caller (e.g. wolfcrypt * test) drives the loop on MP_WOULDBLOCK directly. Reached when no * async marker is set on the key. */ - ret = wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key); + ret = wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key, + rng); } else #endif diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index bc8bb9bb18d..5037c15547b 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -2000,6 +2000,22 @@ static int fp_sqr_nc(fp_int *a, fp_int *r) #endif /* !FP_EXPTMOD_NB_CHECKTIME */ #endif /* WC_RSA_NONBLOCK_TIME */ +/* Zero the ladder values and exponent bits left in the context and return it + * to the initial state. */ +static void fp_exptmod_nb_clear(exptModNb_t* nb) +{ + fp_forcezero(&nb->R[0]); + fp_forcezero(&nb->R[1]); +#ifndef WC_NO_CACHE_RESISTANT + fp_forcezero(&nb->R[2]); +#endif + ForceZero(&nb->buf, sizeof(nb->buf)); + ForceZero(&nb->y, sizeof(nb->y)); + nb->bitcnt = 0; + nb->digidx = 0; + nb->state = TFM_EXPTMOD_NB_INIT; +} + /* non-blocking version of timing resistant fp_exptmod function */ /* supports cache resistance */ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) @@ -2017,18 +2033,18 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) switch (nb->state) { case TFM_EXPTMOD_NB_INIT: - /* now setup montgomery */ - if ((err = fp_montgomery_setup(P, &nb->mp)) != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; - return err; - } - /* init ints */ fp_init(&nb->R[0]); fp_init(&nb->R[1]); #ifndef WC_NO_CACHE_RESISTANT fp_init(&nb->R[2]); #endif + + /* now setup montgomery */ + if ((err = fp_montgomery_setup(P, &nb->mp)) != FP_OKAY) { + fp_exptmod_nb_clear(nb); + return err; + } nb->state = TFM_EXPTMOD_NB_MONT; break; @@ -2036,7 +2052,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) /* mod m -> R[0] */ err = fp_montgomery_calc_normalization(&nb->R[0], P); if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } @@ -2049,7 +2065,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) /* G > P so we reduce it first */ err = fp_mod(G, P, &nb->R[1]); if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } } else { @@ -2063,7 +2079,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) /* G (R[1]) * m (R[0]) */ err = fp_mul(&nb->R[1], &nb->R[0], &nb->R[1]); if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } @@ -2074,7 +2090,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) /* mod m */ err = fp_div(&nb->R[1], P, NULL, &nb->R[1]); if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } @@ -2086,7 +2102,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) if (nb->R[1].sign != P->sign) { err = fp_add(&nb->R[1], P, &nb->R[1]); if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } } @@ -2133,7 +2149,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) err = FP_EXPTMOD_MUL(&nb->R[0], &nb->R[1], &nb->R[2]); #endif if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } nb->state = TFM_EXPTMOD_NB_MUL_RED; @@ -2146,7 +2162,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) err = FP_EXPTMOD_RED(&nb->R[2], P, nb->mp); #endif if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } #if !defined(WC_NO_CACHE_RESISTANT) && !defined(WC_FP_MODEXP_PTR_COPY) @@ -2172,7 +2188,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) err = FP_EXPTMOD_SQR(&nb->R[2], &nb->R[2]); #endif /* WC_NO_CACHE_RESISTANT */ if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } @@ -2193,7 +2209,7 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) ((wc_ptr_t)&nb->R[1] & wc_off_on_addr[nb->y]) ) ); #endif /* WC_NO_CACHE_RESISTANT */ if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } @@ -2204,12 +2220,12 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) /* final reduce */ err = FP_EXPTMOD_RED_LAST(&nb->R[0], P, nb->mp); if (err != FP_OKAY) { - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); return err; } fp_copy(&nb->R[0], Y); - nb->state = TFM_EXPTMOD_NB_INIT; + fp_exptmod_nb_clear(nb); ret = FP_OKAY; break; } /* switch */ @@ -2226,6 +2242,18 @@ int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y) #endif /* WC_RSA_NONBLOCK */ +/* Ladder values may be derived from secrets - zero them before release. */ +#ifdef WC_NO_CACHE_RESISTANT + #define FP_EXPTMOD_CT_R_CNT 2 +#else + #define FP_EXPTMOD_CT_R_CNT 3 +#endif +#define FP_EXPTMOD_FREE_R(R, n) \ + do { \ + ForceZero(R, sizeof(fp_int) * (size_t)(n)); \ + WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); \ + } while (0) + #ifndef WC_PROTECT_ENCRYPTED_MEM /* timing resistant montgomery ladder based exptmod @@ -2270,7 +2298,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, /* now we need R mod m */ err = fp_montgomery_calc_normalization (&R[0], P); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } @@ -2279,7 +2307,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, /* G > P so we reduce it first */ err = fp_mod(G, P, &R[1]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } } else { @@ -2287,7 +2315,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, } err = fp_mulmod (&R[1], &R[0], P, &R[1]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } /* Values are not secret - pad to modulus length so that all working values @@ -2324,35 +2352,35 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, /* do ops */ err = FP_EXPTMOD_MUL(&R[0], &R[1], &R[y^1]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } err = FP_EXPTMOD_RED(&R[y^1], P, mp); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } err = FP_EXPTMOD_SQR(&R[y], &R[y]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } err = FP_EXPTMOD_RED(&R[y], P, mp); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } #else /* do ops */ err = FP_EXPTMOD_MUL(&R[0], &R[1], &R[2]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } err = FP_EXPTMOD_RED(&R[2], P, mp); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } /* instead of using R[y^1] for mul, which leaks key bit to cache monitor, @@ -2382,12 +2410,12 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, #endif err = FP_EXPTMOD_SQR(&R[2], &R[2]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } err = FP_EXPTMOD_RED(&R[2], P, mp); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } #ifndef WC_FP_MODEXP_PTR_COPY @@ -2403,7 +2431,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, err = FP_EXPTMOD_RED_LAST(&R[0], P, mp); fp_copy(&R[0], Y); - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, FP_EXPTMOD_CT_R_CNT); return err; } @@ -2468,7 +2496,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, /* now we need R mod m */ err = fp_montgomery_calc_normalization (&R[0], P); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } @@ -2477,7 +2505,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, /* G > P so we reduce it first */ err = fp_mod(G, P, &R[1]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } } else { @@ -2485,7 +2513,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, } err = fp_mulmod (&R[1], &R[0], P, &R[1]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } /* Values are not secret - pad to modulus length so that all working values @@ -2521,12 +2549,12 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, /* do ops */ err = FP_EXPTMOD_MUL(&R[0], &R[1], &R[2]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } err = FP_EXPTMOD_RED(&R[2], P, mp); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } @@ -2544,12 +2572,12 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, #endif err = FP_EXPTMOD_SQR(&R[3], &R[3]); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } err = FP_EXPTMOD_RED(&R[3], P, mp); if (err != FP_OKAY) { - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } fp_copy_2_ct(&R[2], &R[3], &R[0], &R[1], y, P->used); @@ -2557,7 +2585,7 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, err = FP_EXPTMOD_RED_LAST(&R[0], P, mp); fp_copy(&R[0], Y); - WC_FREE_VAR_EX(R, NULL, DYNAMIC_TYPE_BIGINT); + FP_EXPTMOD_FREE_R(R, 4); return err; } @@ -2565,6 +2593,18 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, #endif /* TFM_TIMING_RESISTANT */ +/* Table values may be derived from a secret base - zero them before release. */ +#ifndef WOLFSSL_NO_MALLOC +#define FP_EXPTMOD_FREE_M(M, n) \ + do { \ + ForceZero(M, sizeof(fp_int) * (size_t)(n)); \ + XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); \ + } while (0) +#else +#define FP_EXPTMOD_FREE_M(M, n) \ + ForceZero(M, sizeof(fp_int) * (size_t)(n)) +#endif + /* y = g**x (mod b) * Some restrictions... x must be positive and < b */ @@ -2625,9 +2665,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* now we need R mod m */ err = fp_montgomery_calc_normalization (res, P); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } @@ -2636,9 +2674,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* G > P so we reduce it first */ err = fp_mod(G, P, &M[1]); if (err != FP_OKAY) { - #ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); - #endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } } else { @@ -2646,9 +2682,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) } err = fp_mulmod (&M[1], res, P, &M[1]); if (err != FP_OKAY) { - #ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); - #endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } @@ -2659,16 +2693,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) err = fp_sqr (&M[(word32)(1 << (winsize - 1))], &M[(word32)(1 << (winsize - 1))]); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(&M[(word32)(1 << (winsize - 1))], P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } } @@ -2677,16 +2707,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { err = fp_mul(&M[x - 1], &M[1], &M[x]); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(&M[x], P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } } @@ -2728,16 +2754,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) if (mode == 1 && y == 0) { err = fp_sqr(res, res); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(res, P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } continue; @@ -2753,16 +2775,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) for (x = 0; x < winsize; x++) { err = fp_sqr(res, res); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(res, P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } } @@ -2770,16 +2788,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* then multiply */ err = fp_mul(res, &M[bitbuf], res); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(res, P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } @@ -2796,16 +2810,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) for (x = 0; x < bitcpy; x++) { err = fp_sqr(res, res); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(res, P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } @@ -2815,16 +2825,12 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* then multiply */ err = fp_mul(res, &M[1], res); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } err = fp_montgomery_reduce_ex(res, P, mp, 0); if (err != FP_OKAY) { -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } } @@ -2842,9 +2848,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* swap res with Y */ fp_copy (res, Y); -#ifndef WOLFSSL_NO_MALLOC - XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); -#endif + FP_EXPTMOD_FREE_M(M, (1 << winsize) + 1); return err; } @@ -4648,6 +4652,7 @@ int fp_to_unsigned_bin(const fp_int *a, unsigned char *b) x = fp_to_unsigned_bin_at_pos(0, t, b); mp_reverse (b, x); + fp_forcezero(t); WC_FREE_VAR_EX(t, NULL, DYNAMIC_TYPE_BIGINT); return FP_OKAY; } @@ -4758,6 +4763,7 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c) return FP_OKAY; #else int x; + int err; WC_DECLARE_VAR(t, fp_int, 1, 0); if (c < 0) { @@ -4774,11 +4780,10 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c) } mp_reverse (b, x); + err = (fp_iszero(t) == FP_YES) ? FP_OKAY : FP_VAL; + fp_forcezero(t); WC_FREE_VAR_EX(t, NULL, DYNAMIC_TYPE_BIGINT); - if (!fp_iszero(t)) { - return FP_VAL; - } - return FP_OKAY; + return err; #endif } diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index 5a9346632c6..aa5944cf95b 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -221,6 +221,11 @@ typedef struct RsaNb { defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) sp_rsa_ctx_t sp_ctx; /* SP non-block wrapper state */ #endif +#if defined(USE_FAST_MATH) && defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) + mp_int rnd; /* blinding value, raised to e across calls */ + mp_int rndi; /* blinding inverse held until the private op completes */ + int blindState; /* step of the blinded private op */ +#endif } RsaNb; #endif