Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/api/test_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
91 changes: 91 additions & 0 deletions tests/api/test_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 3 additions & 1 deletion tests/api/test_rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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), \
Expand Down Expand Up @@ -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 */
173 changes: 173 additions & 0 deletions tests/api/test_wolfmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
6 changes: 5 additions & 1 deletion tests/api/test_wolfmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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), \
Expand All @@ -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 */
6 changes: 3 additions & 3 deletions tests/unit-mcdc/test_rsa_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions wolfcrypt/src/dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading