comparison libtomcrypt/tests/pkcs_1_test.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9 #include <tomcrypt_test.h>
10
11 #ifdef LTC_PKCS_1
12
13 #ifdef LTC_TEST_REAL_RAND
14 #define LTC_TEST_RAND_SEED time(NULL)
15 #else
16 #define LTC_TEST_RAND_SEED 23
17 #endif
18
19 int pkcs_1_test(void)
20 {
21 unsigned char buf[3][128];
22 int res1, res2, res3, prng_idx, hash_idx;
23 unsigned long x, y, l1, l2, l3, i1, i2, lparamlen, saltlen, modlen;
24 static const unsigned char lparam[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
25
26 /* get hash/prng */
27 hash_idx = find_hash("sha1");
28 prng_idx = find_prng("yarrow");
29
30 if (hash_idx == -1 || prng_idx == -1) {
31 fprintf(stderr, "pkcs_1 tests require sha1/yarrow");
32 return 1;
33 }
34
35 srand(LTC_TEST_RAND_SEED);
36 /* do many tests */
37 for (x = 0; x < 100; x++) {
38 zeromem(buf, sizeof(buf));
39
40 /* make a dummy message (of random length) */
41 l3 = (rand() & 31) + 8;
42 for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
43
44 /* pick a random lparam len [0..16] */
45 lparamlen = abs(rand()) % 17;
46
47 /* pick a random saltlen 0..16 */
48 saltlen = abs(rand()) % 17;
49
50 /* PKCS #1 v2.0 supports modlens not multiple of 8 */
51 modlen = 800 + (abs(rand()) % 224);
52
53 /* encode it */
54 l1 = sizeof(buf[1]);
55 DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &yarrow_prng, prng_idx, hash_idx, buf[1], &l1));
56
57 /* decode it */
58 l2 = sizeof(buf[2]);
59 DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1));
60
61 if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
62 fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
63 fprintf(stderr, "ORIGINAL:\n");
64 for (x = 0; x < l3; x++) {
65 fprintf(stderr, "%02x ", buf[0][x]);
66 }
67 fprintf(stderr, "\nRESULT:\n");
68 for (x = 0; x < l2; x++) {
69 fprintf(stderr, "%02x ", buf[2][x]);
70 }
71 fprintf(stderr, "\n\n");
72 return 1;
73 }
74
75 /* test PSS */
76 l1 = sizeof(buf[1]);
77 DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &yarrow_prng, prng_idx, hash_idx, modlen, buf[1], &l1));
78 DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
79
80 buf[0][i1 = abs(rand()) % l3] ^= 1;
81 DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
82
83 buf[0][i1] ^= 1;
84 buf[1][i2 = abs(rand()) % (l1 - 1)] ^= 1;
85 pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3);
86 if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
87 fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
88 return 1;
89 }
90 }
91 return 0;
92 }
93
94 #else
95
96 int pkcs_1_test(void)
97 {
98 return CRYPT_NOP;
99 }
100
101 #endif
102
103
104 /* ref: $Format:%D$ */
105 /* git commit: $Format:%H$ */
106 /* commit time: $Format:%ai$ */