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