comparison libtomcrypt/tests/katja_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 #if defined(LTC_MKAT) && defined(LTC_TEST_MPI)
12
13 int katja_test(void)
14 {
15 unsigned char in[1024], out[1024], tmp[1024];
16 katja_key key, privKey, pubKey;
17 int hash_idx, prng_idx, stat, stat2, size;
18 unsigned long kat_msgsize, len, len2, cnt;
19 static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
20
21 hash_idx = find_hash("sha1");
22 prng_idx = find_prng("yarrow");
23 if (hash_idx == -1 || prng_idx == -1) {
24 fprintf(stderr, "katja_test requires LTC_SHA1 and yarrow");
25 return 1;
26 }
27
28 for (size = 1024; size <= 2048; size += 256) {
29
30 /* make 10 random key */
31 for (cnt = 0; cnt < 10; cnt++) {
32 DO(katja_make_key(&yarrow_prng, prng_idx, size/8, &key));
33 if (mp_count_bits(key.N) < size - 7) {
34 fprintf(stderr, "katja_%d key modulus has %d bits\n", size, mp_count_bits(key.N));
35
36 len = mp_unsigned_bin_size(key.N);
37 mp_to_unsigned_bin(key.N, tmp);
38 fprintf(stderr, "N == \n");
39 for (cnt = 0; cnt < len; ) {
40 fprintf(stderr, "%02x ", tmp[cnt]);
41 if (!(++cnt & 15)) fprintf(stderr, "\n");
42 }
43
44 len = mp_unsigned_bin_size(key.p);
45 mp_to_unsigned_bin(key.p, tmp);
46 fprintf(stderr, "p == \n");
47 for (cnt = 0; cnt < len; ) {
48 fprintf(stderr, "%02x ", tmp[cnt]);
49 if (!(++cnt & 15)) fprintf(stderr, "\n");
50 }
51
52 len = mp_unsigned_bin_size(key.q);
53 mp_to_unsigned_bin(key.q, tmp);
54 fprintf(stderr, "\nq == \n");
55 for (cnt = 0; cnt < len; ) {
56 fprintf(stderr, "%02x ", tmp[cnt]);
57 if (!(++cnt & 15)) fprintf(stderr, "\n");
58 }
59 fprintf(stderr, "\n");
60
61
62 return 1;
63 }
64 if (cnt != 9) {
65 katja_free(&key);
66 }
67 }
68 /* encrypt the key (without lparam) */
69 for (cnt = 0; cnt < 4; cnt++) {
70 for (kat_msgsize = 1; kat_msgsize <= 42; kat_msgsize++) {
71 /* make a random key/msg */
72 yarrow_read(in, kat_msgsize, &yarrow_prng);
73
74 len = sizeof(out);
75 len2 = kat_msgsize;
76
77 DO(katja_encrypt_key(in, kat_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
78 /* change a byte */
79 out[8] ^= 1;
80 DO(katja_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key));
81 /* change a byte back */
82 out[8] ^= 1;
83 if (len2 != kat_msgsize) {
84 fprintf(stderr, "\nkatja_decrypt_key mismatch len %lu (first decrypt)", len2);
85 return 1;
86 }
87
88 len2 = kat_msgsize;
89 DO(katja_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
90 if (!(stat == 1 && stat2 == 0)) {
91 fprintf(stderr, "katja_decrypt_key failed");
92 return 1;
93 }
94 if (len2 != kat_msgsize || memcmp(tmp, in, kat_msgsize)) {
95 unsigned long x;
96 fprintf(stderr, "\nkatja_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
97 fprintf(stderr, "Original contents: \n");
98 for (x = 0; x < kat_msgsize; ) {
99 fprintf(stderr, "%02x ", in[x]);
100 if (!(++x % 16)) {
101 fprintf(stderr, "\n");
102 }
103 }
104 fprintf(stderr, "\n");
105 fprintf(stderr, "Output contents: \n");
106 for (x = 0; x < kat_msgsize; ) {
107 fprintf(stderr, "%02x ", out[x]);
108 if (!(++x % 16)) {
109 fprintf(stderr, "\n");
110 }
111 }
112 fprintf(stderr, "\n");
113 return 1;
114 }
115 }
116 }
117
118 /* encrypt the key (with lparam) */
119 for (kat_msgsize = 1; kat_msgsize <= 42; kat_msgsize++) {
120 len = sizeof(out);
121 len2 = kat_msgsize;
122 DO(katja_encrypt_key(in, kat_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
123 /* change a byte */
124 out[8] ^= 1;
125 DO(katja_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key));
126 if (len2 != kat_msgsize) {
127 fprintf(stderr, "\nkatja_decrypt_key mismatch len %lu (first decrypt)", len2);
128 return 1;
129 }
130 /* change a byte back */
131 out[8] ^= 1;
132
133 len2 = kat_msgsize;
134 DO(katja_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
135 if (!(stat == 1 && stat2 == 0)) {
136 fprintf(stderr, "katja_decrypt_key failed");
137 return 1;
138 }
139 if (len2 != kat_msgsize || memcmp(tmp, in, kat_msgsize)) {
140 fprintf(stderr, "katja_decrypt_key mismatch len %lu", len2);
141 return 1;
142 }
143 }
144
145 #if 0
146
147 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
148 len = sizeof(out);
149 DO(katja_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key));
150
151 /* export key and import as both private and public */
152 len2 = sizeof(tmp);
153 DO(katja_export(tmp, &len2, PK_PRIVATE, &key));
154 DO(katja_import(tmp, len2, &privKey));
155 len2 = sizeof(tmp);
156 DO(katja_export(tmp, &len2, PK_PUBLIC, &key));
157 DO(katja_import(tmp, len2, &pubKey));
158
159 /* verify with original */
160 DO(katja_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
161 /* change a byte */
162 in[0] ^= 1;
163 DO(katja_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
164
165 if (!(stat == 1 && stat2 == 0)) {
166 fprintf(stderr, "katja_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
167 katja_free(&key);
168 katja_free(&pubKey);
169 katja_free(&privKey);
170 return 1;
171 }
172
173 /* verify with privKey */
174 /* change a byte */
175 in[0] ^= 1;
176 DO(katja_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
177 /* change a byte */
178 in[0] ^= 1;
179 DO(katja_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
180
181 if (!(stat == 1 && stat2 == 0)) {
182 fprintf(stderr, "katja_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
183 katja_free(&key);
184 katja_free(&pubKey);
185 katja_free(&privKey);
186 return 1;
187 }
188
189 /* verify with pubKey */
190 /* change a byte */
191 in[0] ^= 1;
192 DO(katja_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
193 /* change a byte */
194 in[0] ^= 1;
195 DO(katja_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
196
197 if (!(stat == 1 && stat2 == 0)) {
198 fprintf(stderr, "katja_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
199 katja_free(&key);
200 katja_free(&pubKey);
201 katja_free(&privKey);
202 return 1;
203 }
204
205 /* sign a message (salted) now (use privKey to make, pubKey to verify) */
206 len = sizeof(out);
207 DO(katja_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
208 DO(katja_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
209 /* change a byte */
210 in[0] ^= 1;
211 DO(katja_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
212
213 if (!(stat == 1 && stat2 == 0)) {
214 fprintf(stderr, "katja_verify_hash (salted) failed, %d, %d", stat, stat2);
215 katja_free(&key);
216 katja_free(&pubKey);
217 katja_free(&privKey);
218 return 1;
219 }
220 #endif
221
222 katja_free(&key);
223 katja_free(&pubKey);
224 katja_free(&privKey);
225 }
226
227 /* free the key and return */
228 return 0;
229 }
230
231 #else
232
233 int katja_test(void)
234 {
235 return CRYPT_NOP;
236 }
237
238 #endif
239
240 /* ref: $Format:%D$ */
241 /* git commit: $Format:%H$ */
242 /* commit time: $Format:%ai$ */