source: trunk/src/cutest_sh_tiger0.c@ 448

Last change on this file since 448 was 444, checked in by katerina, 11 years ago

Support for sha2-256 checksum (ticket #348).

File size: 15.1 KB
RevLine 
[17]1
2#include "config_xor.h"
3
4#include <string.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include "CuTest.h"
8
9#include "sh_tiger.h"
[444]10#include "sh_checksum.h"
[17]11
[137]12#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
[170]13extern void sh_g_init(void);
[137]14#endif
15
16
[170]17static void init(void) {
[17]18
19 extern unsigned char TcpFlag[8][PW_LEN+1];
20 extern UINT32 ErrFlag[2];
21 unsigned char * dez = NULL;
22 int i;
23
[137]24#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
25 sh_g_init();
26#endif
[17]27 skey = (sh_key_t *) malloc (sizeof(sh_key_t));
[96]28 if (skey != NULL)
[17]29 {
[96]30 skey->mlock_failed = SL_FALSE;
31 skey->rngI = BAD;
32 /* properly initialized later
33 */
34 skey->rng0[0] = 0x03; skey->rng0[1] = 0x09; skey->rng0[2] = 0x17;
35 skey->rng1[0] = 0x03; skey->rng1[1] = 0x09; skey->rng1[2] = 0x17;
36 skey->rng2[0] = 0x03; skey->rng2[1] = 0x09; skey->rng2[2] = 0x17;
37
38 for (i = 0; i < KEY_BYT; ++i)
39 skey->poolv[i] = '\0';
40
41 skey->poolc = 0;
42
43 skey->ErrFlag[0] = ErrFlag[0];
44 ErrFlag[0] = 0;
45 skey->ErrFlag[1] = ErrFlag[1];
46 ErrFlag[1] = 0;
47
48 dez = &(TcpFlag[POS_TF-1][0]);
49 for (i = 0; i < PW_LEN; ++i)
50 {
51 skey->pw[i] = (char) (*dez);
52 (*dez) = '\0';
53 ++dez;
54 }
55
56 skey->sh_sockpass[0] = '\0';
57 skey->sigkey_old[0] = '\0';
58 skey->sigkey_new[0] = '\0';
59 skey->mailkey_old[0] = '\0';
60 skey->mailkey_new[0] = '\0';
61 skey->crypt[0] = '\0';
62 skey->session[0] = '\0';
63 skey->vernam[0] = '\0';
64 }
65 else
66 {
[17]67 perror(_("sh_init"));
68 _exit (EXIT_FAILURE);
69 }
70
71}
72
73void Test_tiger(CuTest *tc) {
74
75 char * input;
76 char * actual;
77 char * expected;
[133]78 char hashbuf[KEYBUF_SIZE];
[17]79
[137]80#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
81 sh_g_init();
82#endif
83
[17]84 input = "";
[133]85 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]86 expected = "24F0130C63AC933216166E76B1BB925FF373DE2D49584E7A";
87 CuAssertStrEquals(tc, expected, actual);
88
89 input = "abc";
[133]90 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]91 expected = "F258C1E88414AB2A527AB541FFC5B8BF935F7B951C132951";
92 CuAssertStrEquals(tc, expected, actual);
93
94 input = "Tiger";
[133]95 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]96 expected = "9F00F599072300DD276ABB38C8EB6DEC37790C116F9D2BDF";
97 CuAssertStrEquals(tc, expected, actual);
98
99 input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
[133]100 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]101 expected = "87FB2A9083851CF7470D2CF810E6DF9EB586445034A5A386";
102 CuAssertStrEquals(tc, expected, actual);
103
104 input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789";
[133]105 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]106 expected = "467DB80863EBCE488DF1CD1261655DE957896565975F9197";
107 CuAssertStrEquals(tc, expected, actual);
108
109 input = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham";
[133]110 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]111 expected = "0C410A042968868A1671DA5A3FD29A725EC1E457D3CDB303";
112 CuAssertStrEquals(tc, expected, actual);
113
114 input = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge.";
[133]115 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]116 expected = "EBF591D5AFA655CE7F22894FF87F54AC89C811B6B0DA3193";
117 CuAssertStrEquals(tc, expected, actual);
118
119 input = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.";
[133]120 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]121 expected = "3D9AEB03D1BD1A6357B2774DFD6D5B24DD68151D503974FC";
122 CuAssertStrEquals(tc, expected, actual);
123
124 input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
[133]125 actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
[17]126 expected = "00B83EB4E53440C576AC6AAEE0A7485825FD15E70A59FFE4";
127 CuAssertStrEquals(tc, expected, actual);
128}
129
130void Test_tiger_file(CuTest *tc) {
131
132 SL_TICKET rval_open;
133 FILE * fp;
134 int result;
135 char * actual;
136 char * expected;
[133]137 char hashbuf[KEYBUF_SIZE];
[444]138 char hexdigest[SHA256_DIGEST_STRING_LENGTH];
[151]139 UINT64 length;
[17]140
141 init();
142
143 fp = fopen("cutest_foo", "w");
144 CuAssertPtrNotNull(tc, fp);
145
146 result = fprintf(fp, "%s\n",
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789");
148 CuAssertTrue(tc, result >= 0);
149
150 result = fclose(fp);
151 CuAssertTrue(tc, result == 0);
152
153 result = sh_tiger_hashtype("TIGER192");
154 CuAssertTrue(tc, result == 0);
155
156 /* same result as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192)
157 */
[151]158 length = TIGER_NOLIM;
159 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[17]160 expected = "0E9321614C966A33608C2A15F156E0435CACFD1213B9F095";
161 CuAssertStrEquals(tc, expected, actual);
162
[248]163 rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
[17]164 CuAssertTrue(tc, rval_open >= 0);
165
[151]166 length = TIGER_NOLIM;
167 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[17]168 expected = "0E9321614C966A33608C2A15F156E0435CACFD1213B9F095";
169 CuAssertStrEquals(tc, expected, actual);
170
171 result = sl_close(rval_open);
172 CuAssertTrue(tc, result == 0);
173
174 result = sh_tiger_hashtype("MD5");
175 CuAssertTrue(tc, result == 0);
176
[248]177 rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
[17]178 CuAssertTrue(tc, rval_open >= 0);
179
180 /* same result as GNU md5sum
181 */
[151]182 length = TIGER_NOLIM;
183 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[17]184 expected = "AEEC4DDA496BCFBA691F4E8863BA84C00000000000000000";
185 CuAssertStrEquals(tc, expected, actual);
186
187 result = sl_close(rval_open);
188 CuAssertTrue(tc, result == 0);
189
190 result = sh_tiger_hashtype("SHA1");
191 CuAssertTrue(tc, result == 0);
192
[248]193 rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
[17]194 CuAssertTrue(tc, rval_open >= 0);
195
196 /* same result as gpg --print-md SHA1
197 */
[151]198 length = TIGER_NOLIM;
199 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[17]200 expected = "2FE65D1D995B8F8BC8B13F798C07E7E935A787ED00000000";
201 CuAssertStrEquals(tc, expected, actual);
202
203 result = sl_close(rval_open);
204 CuAssertTrue(tc, result == 0);
205
[444]206 result = sh_tiger_hashtype("SHA256");
207 CuAssertTrue(tc, result == 0);
208
209 rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
210 CuAssertTrue(tc, rval_open >= 0);
211
212 /* same result as gpg --print-md SHA256
213 */
214 length = TIGER_NOLIM;
215 {
216 char * tmp = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
217 actual = SHA256_Base2Hex(tmp, hexdigest);
218 }
219 expected = "235790848f95e96b2c627f1bf58a2b8c05c535ada8c0a3326aac34ce1391ad40";
220 CuAssertStrEquals(tc, expected, actual);
221
222 result = sl_close(rval_open);
223 CuAssertTrue(tc, result == 0);
224
[17]225 result = remove("cutest_foo");
226 CuAssertTrue(tc, result == 0);
227
228 /* --------------------------------------------------- */
229
230 fp = fopen("cutest_foo", "w");
231 CuAssertPtrNotNull(tc, fp);
232
233 result = fprintf(fp, "\n");
234 CuAssertTrue(tc, result >= 0);
235
236 result = fclose(fp);
237 CuAssertTrue(tc, result == 0);
238
239 result = sh_tiger_hashtype("TIGER192");
240 CuAssertTrue(tc, result == 0);
241
242 /* same result as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192)
243 */
[151]244 length = TIGER_NOLIM;
245 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[17]246 expected = "F987845A0EA784367BF9E4DB09014995810F27C99C891734";
247 CuAssertStrEquals(tc, expected, actual);
248
249 result = remove("cutest_foo");
250 CuAssertTrue(tc, result == 0);
251
252 /* --------------------------------------------------- */
253
254 fp = fopen("cutest_foo", "w");
255 CuAssertPtrNotNull(tc, fp);
256
257 result = fprintf(fp, "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.\n");
258 CuAssertTrue(tc, result >= 0);
259
260 result = fclose(fp);
261 CuAssertTrue(tc, result == 0);
262
263 result = sh_tiger_hashtype("TIGER192");
264 CuAssertTrue(tc, result == 0);
265
266 /* same result as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192)
267 */
[151]268 length = TIGER_NOLIM;
269 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[17]270 expected = "75B98A7AE257A230189828A40792E30B4038D286479CC7B8";
271 CuAssertStrEquals(tc, expected, actual);
272
273 result = remove("cutest_foo");
274 CuAssertTrue(tc, result == 0);
275
[19]276}
[17]277
[19]278/* test checksum of file upto some given length
279 */
280void Test_tiger_file_with_length(CuTest *tc) {
281
282 SL_TICKET rval_open;
283 FILE * fp;
284 int result;
285 char * actual;
286 char * expected;
[133]287 char hashbuf[KEYBUF_SIZE];
[151]288 UINT64 length;
[19]289
290 char * teststring = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.\n";
[151]291 size_t testlen = strlen(teststring);
[19]292
293 init();
294
295 fp = fopen("cutest_foo", "w");
296 CuAssertPtrNotNull(tc, fp);
297
298 result = fprintf(fp, "%s", teststring);
299 CuAssertTrue(tc, result >= 0);
300 result = fprintf(fp, "%s", teststring);
301 CuAssertTrue(tc, result >= 0);
302
303 result = fclose(fp);
304 CuAssertTrue(tc, result == 0);
305
306 result = sh_tiger_hashtype("TIGER192");
307 CuAssertTrue(tc, result == 0);
308
309 /* same as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192)
310 */
[151]311 length = 0;
312 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[107]313 expected = "24F0130C63AC933216166E76B1BB925FF373DE2D49584E7A";
314 CuAssertStrEquals(tc, expected, actual);
[151]315 CuAssertTrue(tc, 0 == length);
[107]316
[151]317 length = testlen;
318 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]319 expected = "75B98A7AE257A230189828A40792E30B4038D286479CC7B8";
320 CuAssertStrEquals(tc, expected, actual);
[151]321 CuAssertTrue(tc, testlen == length);
[19]322
[151]323 length = 2*testlen;
324 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]325 expected = "B5B4FB97B01ADB58794D87A6A01B2368852FA764BD93AB90";
326 CuAssertStrEquals(tc, expected, actual);
[151]327 CuAssertTrue(tc, 2*testlen == length);
[19]328
[151]329 length = TIGER_NOLIM;
330 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]331 expected = "B5B4FB97B01ADB58794D87A6A01B2368852FA764BD93AB90";
332 CuAssertStrEquals(tc, expected, actual);
[151]333 CuAssertTrue(tc, 2*testlen == length);
[19]334
335 fp = fopen("cutest_foo", "a");
336 CuAssertPtrNotNull(tc, fp);
337 result = fprintf(fp, "%s", teststring);
338 CuAssertTrue(tc, result >= 0);
339 result = fclose(fp);
340 CuAssertTrue(tc, result == 0);
341
[151]342 length = testlen;
343 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]344 expected = "75B98A7AE257A230189828A40792E30B4038D286479CC7B8";
345 CuAssertStrEquals(tc, expected, actual);
[151]346 CuAssertTrue(tc, testlen == length);
[19]347
[151]348 length = 2*testlen;
349 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]350 expected = "B5B4FB97B01ADB58794D87A6A01B2368852FA764BD93AB90";
351 CuAssertStrEquals(tc, expected, actual);
[151]352 CuAssertTrue(tc, 2*testlen == length);
[19]353
[151]354 length = 3*testlen;
355 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]356 expected = "D0EE1A9956CAB22D84B51A5E0C093B724828C6A1F9CBDB7F";
357 CuAssertStrEquals(tc, expected, actual);
[151]358 CuAssertTrue(tc, 3*testlen == length);
[19]359
[151]360 length = TIGER_NOLIM;
361 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]362 expected = "D0EE1A9956CAB22D84B51A5E0C093B724828C6A1F9CBDB7F";
363 CuAssertStrEquals(tc, expected, actual);
[151]364 CuAssertTrue(tc, 3*testlen == length);
[19]365
[151]366 length = 5;
367 actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
[19]368 expected = "9F00F599072300DD276ABB38C8EB6DEC37790C116F9D2BDF";
369 CuAssertStrEquals(tc, expected, actual);
[151]370 CuAssertTrue(tc, 5 == length);
[19]371
372 /* same results as GNU md5sum */
373
374 result = sh_tiger_hashtype("MD5");
375 CuAssertTrue(tc, result == 0);
376
[248]377 rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
[19]378 CuAssertTrue(tc, rval_open >= 0);
379
[151]380 length = testlen;
381 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]382 expected = "11E7E7EA486136273606BEE57C71F34B0000000000000000";
383 CuAssertStrEquals(tc, expected, actual);
[151]384 CuAssertTrue(tc, testlen == length);
[19]385
386 result = sl_rewind(rval_open);
387 CuAssertTrue(tc, rval_open >= 0);
388
[151]389 length = 2*testlen;
390 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]391 expected = "D49DAD474095D467E2E5EFCB2DC23A770000000000000000";
392 CuAssertStrEquals(tc, expected, actual);
[151]393 CuAssertTrue(tc, 2*testlen == length);
[19]394
395 result = sl_rewind(rval_open);
396 CuAssertTrue(tc, rval_open >= 0);
397
[151]398 length = 3*testlen;
399 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]400 expected = "00A1F1C5EDDCCFC430D3862FDA94593E0000000000000000";
401 CuAssertStrEquals(tc, expected, actual);
[151]402 CuAssertTrue(tc, 3*testlen == length);
[19]403
404 result = sl_rewind(rval_open);
405 CuAssertTrue(tc, rval_open >= 0);
406
[151]407 length = TIGER_NOLIM;
408 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]409 expected = "00A1F1C5EDDCCFC430D3862FDA94593E0000000000000000";
410 CuAssertStrEquals(tc, expected, actual);
[151]411 CuAssertTrue(tc, 3*testlen == length);
[19]412
413 /* same result as gpg --print-md SHA1
414 */
415
416 result = sh_tiger_hashtype("SHA1");
417 CuAssertTrue(tc, result == 0);
418
419 result = sl_rewind(rval_open);
420 CuAssertTrue(tc, rval_open >= 0);
421
[151]422 length = testlen;
423 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]424 expected = "F37DB4344CCD140EE315179E9A27512FB4704F0F00000000";
425 CuAssertStrEquals(tc, expected, actual);
[151]426 CuAssertTrue(tc, testlen == length);
[19]427
428 result = sl_rewind(rval_open);
429 CuAssertTrue(tc, rval_open >= 0);
430
[151]431 length = 2*testlen;
432 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]433 expected = "D2AD5FC366452D81400BAC31F96269DEEF314BC200000000";
434 CuAssertStrEquals(tc, expected, actual);
[151]435 CuAssertTrue(tc, 2*testlen == length);
[19]436
437 result = sl_rewind(rval_open);
438 CuAssertTrue(tc, rval_open >= 0);
439
[151]440 length = 3*testlen;
441 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]442 expected = "FAA937EF3389C7E786EB0F1006D049D7AEA7B7B600000000";
443 CuAssertStrEquals(tc, expected, actual);
[151]444 CuAssertTrue(tc, 3*testlen == length);
[19]445
446 result = sl_rewind(rval_open);
447 CuAssertTrue(tc, rval_open >= 0);
448
[151]449 length = TIGER_NOLIM;
450 actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
[19]451 expected = "FAA937EF3389C7E786EB0F1006D049D7AEA7B7B600000000";
452 CuAssertStrEquals(tc, expected, actual);
[151]453 CuAssertTrue(tc, 3*testlen == length);
[19]454
455 result = sl_close(rval_open);
456 CuAssertTrue(tc, result == 0);
457
458 result = remove("cutest_foo");
459 CuAssertTrue(tc, result == 0);
460}
Note: See TracBrowser for help on using the repository browser.