Skip to content

Commit d6bc30e

Browse files
committed
test: add test rsa key generate with public exponent upto 256 bits
Add a RSA test generating a RSA key with a 256 bits public exponent. Signed-off-by: Cedric Neveux <cedric.neveux@nxp.com>
1 parent 49556a8 commit d6bc30e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/rsa_test.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,52 @@ static int s_rsa_issue_301(int prng_idx)
369369
return CRYPT_OK;
370370
}
371371

372+
static int s_rsa_public_ubin_e(int prng_idx)
373+
{
374+
rsa_key key;
375+
unsigned char e[32] = {0};
376+
unsigned long elen = sizeof(e);
377+
378+
/* Check public exponent too small */
379+
e[elen - 1] = 1;
380+
SHOULD_FAIL_WITH(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key),
381+
CRYPT_INVALID_ARG);
382+
383+
/*
384+
* Generate about 256 bits to check error when public exponent
385+
* overflow.
386+
*/
387+
DO(rng_make_prng(elen * 8, prng_idx, &yarrow_prng, NULL));
388+
LTC_ARGCHK(yarrow_read(e, elen, &yarrow_prng) == elen);
389+
390+
/* Ensure that public exponent is:
391+
* - odd value
392+
* - MSB is even
393+
*/
394+
e[elen - 1] |= 0x1;
395+
e[0] &= ~0x1;
396+
397+
/* Check public exponent overflow */
398+
/* Set high bit of MSB set to get 256 bits, to get e overflow */
399+
e[0] |= 0x80;
400+
SHOULD_FAIL_WITH(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key),
401+
CRYPT_INVALID_ARG);
402+
403+
404+
/* Check public exponent not odd but e value < 256 bits */
405+
e[elen - 1] &= ~0x1;
406+
e[0] &= ~0x80;
407+
SHOULD_FAIL_WITH(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key),
408+
CRYPT_INVALID_ARG);
409+
410+
/* Ensure that public exponent is odd value and e value < 256 bits */
411+
e[elen - 1] |= 0x1;
412+
DO(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key));
413+
rsa_free(&key);
414+
415+
return CRYPT_OK;
416+
}
417+
372418
#ifdef LTC_TEST_READDIR
373419
static int s_rsa_import_x509(const void *in, unsigned long inlen, void *key)
374420
{
@@ -426,6 +472,7 @@ int rsa_test(void)
426472

427473
DO(s_rsa_cryptx_issue_69());
428474
DO(s_rsa_issue_301(prng_idx));
475+
DO(s_rsa_public_ubin_e(prng_idx));
429476

430477
/* make 10 random key */
431478
for (cnt = 0; cnt < 10; cnt++) {

0 commit comments

Comments
 (0)