1 #include <tomcrypt_test.h>
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 };
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");
20 for (size
= 1024; size
<= 2048; size
+= 256) {
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
));
28 len
= mp_unsigned_bin_size(key
.N
);
29 mp_to_unsigned_bin(key
.N
, tmp
);
30 fprintf(stderr
, "N == \n");
31 for (cnt
= 0; cnt
< len
; ) {
32 fprintf(stderr
, "%02x ", tmp
[cnt
]);
33 if (!(++cnt
& 15)) fprintf(stderr
, "\n");
36 len
= mp_unsigned_bin_size(key
.p
);
37 mp_to_unsigned_bin(key
.p
, tmp
);
38 fprintf(stderr
, "p == \n");
39 for (cnt
= 0; cnt
< len
; ) {
40 fprintf(stderr
, "%02x ", tmp
[cnt
]);
41 if (!(++cnt
& 15)) fprintf(stderr
, "\n");
44 len
= mp_unsigned_bin_size(key
.q
);
45 mp_to_unsigned_bin(key
.q
, tmp
);
46 fprintf(stderr
, "\nq == \n");
47 for (cnt
= 0; cnt
< len
; ) {
48 fprintf(stderr
, "%02x ", tmp
[cnt
]);
49 if (!(++cnt
& 15)) fprintf(stderr
, "\n");
51 fprintf(stderr
, "\n");
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
);
69 DO(katja_encrypt_key(in
, kat_msgsize
, out
, &len
, NULL
, 0, &yarrow_prng
, prng_idx
, hash_idx
, &key
));
72 DO(katja_decrypt_key(out
, len
, tmp
, &len2
, NULL
, 0, hash_idx
, &stat2
, &key
));
73 /* change a byte back */
75 if (len2
!= kat_msgsize
) {
76 fprintf(stderr
, "\nkatja_decrypt_key mismatch len %lu (first decrypt)", len2
);
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");
86 if (len2
!= kat_msgsize
|| memcmp(tmp
, in
, kat_msgsize
)) {
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
]);
93 fprintf(stderr
, "\n");
96 fprintf(stderr
, "\n");
97 fprintf(stderr
, "Output contents: \n");
98 for (x
= 0; x
< kat_msgsize
; ) {
99 fprintf(stderr
, "%02x ", out
[x
]);
101 fprintf(stderr
, "\n");
104 fprintf(stderr
, "\n");
110 /* encrypt the key (with lparam) */
111 for (kat_msgsize
= 1; kat_msgsize
<= 42; kat_msgsize
++) {
114 DO(katja_encrypt_key(in
, kat_msgsize
, out
, &len
, lparam
, sizeof(lparam
), &yarrow_prng
, prng_idx
, hash_idx
, &key
));
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
);
122 /* change a byte back */
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");
131 if (len2
!= kat_msgsize
|| memcmp(tmp
, in
, kat_msgsize
)) {
132 fprintf(stderr
, "katja_decrypt_key mismatch len %lu", len2
);
139 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
141 DO(katja_sign_hash(in
, 20, out
, &len
, &yarrow_prng
, prng_idx
, hash_idx
, 0, &key
));
143 /* export key and import as both private and public */
145 DO(katja_export(tmp
, &len2
, PK_PRIVATE
, &key
));
146 DO(katja_import(tmp
, len2
, &privKey
));
148 DO(katja_export(tmp
, &len2
, PK_PUBLIC
, &key
));
149 DO(katja_import(tmp
, len2
, &pubKey
));
151 /* verify with original */
152 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 0, &stat
, &key
));
155 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 0, &stat2
, &key
));
157 if (!(stat
== 1 && stat2
== 0)) {
158 fprintf(stderr
, "katja_verify_hash (unsalted, origKey) failed, %d, %d", stat
, stat2
);
161 katja_free(&privKey
);
165 /* verify with privKey */
168 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 0, &stat
, &privKey
));
171 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 0, &stat2
, &privKey
));
173 if (!(stat
== 1 && stat2
== 0)) {
174 fprintf(stderr
, "katja_verify_hash (unsalted, privKey) failed, %d, %d", stat
, stat2
);
177 katja_free(&privKey
);
181 /* verify with pubKey */
184 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 0, &stat
, &pubKey
));
187 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 0, &stat2
, &pubKey
));
189 if (!(stat
== 1 && stat2
== 0)) {
190 fprintf(stderr
, "katja_verify_hash (unsalted, pubkey) failed, %d, %d", stat
, stat2
);
193 katja_free(&privKey
);
197 /* sign a message (salted) now (use privKey to make, pubKey to verify) */
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
));
203 DO(katja_verify_hash(out
, len
, in
, 20, hash_idx
, 8, &stat2
, &pubKey
));
205 if (!(stat
== 1 && stat2
== 0)) {
206 fprintf(stderr
, "katja_verify_hash (salted) failed, %d, %d", stat
, stat2
);
209 katja_free(&privKey
);
216 katja_free(&privKey
);
219 /* free the key and return */
227 fprintf(stderr
, "NOP");