tomcrypt/testprof/pkcs_1_test.c

94 lines
2.7 KiB
C
Raw Normal View History

2005-04-17 07:37:13 -04:00
#include <tomcrypt_test.h>
2004-05-30 22:36:47 -04:00
2007-07-20 13:48:02 -04:00
#ifdef LTC_PKCS_1
2004-10-29 23:00:26 -04:00
2004-05-30 22:36:47 -04:00
int pkcs_1_test(void)
{
unsigned char buf[3][128];
2006-12-16 13:10:04 -05:00
int res1, res2, res3, prng_idx, hash_idx, err;
2004-05-30 22:36:47 -04:00
unsigned long x, y, l1, l2, l3, i1, i2, lparamlen, saltlen, modlen;
static const unsigned char lparam[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
/* get hash/prng */
hash_idx = find_hash("sha1");
prng_idx = find_prng("yarrow");
2014-01-03 09:16:59 -05:00
2004-05-30 22:36:47 -04:00
if (hash_idx == -1 || prng_idx == -1) {
2005-06-08 20:08:13 -04:00
fprintf(stderr, "pkcs_1 tests require sha1/yarrow");
2004-05-30 22:36:47 -04:00
return 1;
2014-01-03 09:16:59 -05:00
}
2004-05-30 22:36:47 -04:00
2006-12-16 13:10:04 -05:00
srand(time(NULL));
2004-05-30 22:36:47 -04:00
/* do many tests */
2004-06-19 22:41:49 -04:00
for (x = 0; x < 100; x++) {
2004-05-30 22:36:47 -04:00
zeromem(buf, sizeof(buf));
/* make a dummy message (of random length) */
l3 = (rand() & 31) + 8;
for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
/* pick a random lparam len [0..16] */
lparamlen = abs(rand()) % 17;
/* pick a random saltlen 0..16 */
saltlen = abs(rand()) % 17;
2014-01-03 09:16:59 -05:00
/* PKCS #1 v2.0 supports modlens not multiple of 8 */
2004-05-30 22:36:47 -04:00
modlen = 800 + (abs(rand()) % 224);
/* encode it */
l1 = sizeof(buf[1]);
2005-04-17 07:37:13 -04:00
DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &yarrow_prng, prng_idx, hash_idx, buf[1], &l1));
2004-05-30 22:36:47 -04:00
/* decode it */
l2 = sizeof(buf[2]);
DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1));
if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
2005-06-08 20:08:13 -04:00
fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
fprintf(stderr, "ORIGINAL:\n");
2004-05-30 22:36:47 -04:00
for (x = 0; x < l3; x++) {
2005-06-08 20:08:13 -04:00
fprintf(stderr, "%02x ", buf[0][x]);
2004-05-30 22:36:47 -04:00
}
2005-06-08 20:08:13 -04:00
fprintf(stderr, "\nRESULT:\n");
2004-05-30 22:36:47 -04:00
for (x = 0; x < l2; x++) {
2005-06-08 20:08:13 -04:00
fprintf(stderr, "%02x ", buf[2][x]);
2004-05-30 22:36:47 -04:00
}
2005-06-08 20:08:13 -04:00
fprintf(stderr, "\n\n");
2004-05-30 22:36:47 -04:00
return 1;
}
/* test PSS */
l1 = sizeof(buf[1]);
2005-04-17 07:37:13 -04:00
DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &yarrow_prng, prng_idx, hash_idx, modlen, buf[1], &l1));
2004-05-30 22:36:47 -04:00
DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
2014-01-03 09:16:59 -05:00
2004-05-30 22:36:47 -04:00
buf[0][i1 = abs(rand()) % l3] ^= 1;
DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
buf[0][i1] ^= 1;
2006-12-16 13:10:04 -05:00
buf[1][i2 = abs(rand()) % (l1 - 1)] ^= 1;
pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3);
2004-05-30 22:36:47 -04:00
if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
2005-06-08 20:08:13 -04:00
fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
2004-05-30 22:36:47 -04:00
return 1;
}
}
return 0;
}
2004-10-29 23:00:26 -04:00
#else
int pkcs_1_test(void)
{
2005-06-08 20:08:13 -04:00
fprintf(stderr, "NOP");
2004-10-29 23:00:26 -04:00
return 0;
}
#endif
2005-06-08 20:08:13 -04:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */