1 #include <tomcrypt_test.h>
3 prng_state yarrow_prng
;
5 struct list results
[100];
7 int sorter(const void *a
, const void *b
)
9 const struct list
*A
, *B
;
12 if (A
->avg
< B
->avg
) return -1;
13 if (A
->avg
> B
->avg
) return 1;
17 void tally_results(int type
)
21 /* qsort the results */
22 qsort(results
, no_results
, sizeof(struct list
), &sorter
);
24 fprintf(stderr
, "\n");
26 for (x
= 0; x
< no_results
; x
++) {
27 fprintf(stderr
, "%-20s: Schedule at %6lu\n", cipher_descriptor
[results
[x
].id
].name
, (unsigned long)results
[x
].spd1
);
29 } else if (type
== 1) {
30 for (x
= 0; x
< no_results
; x
++) {
32 ("%-20s[%3d]: Encrypt at %5lu, Decrypt at %5lu\n", cipher_descriptor
[results
[x
].id
].name
, cipher_descriptor
[results
[x
].id
].ID
, results
[x
].spd1
, results
[x
].spd2
);
35 for (x
= 0; x
< no_results
; x
++) {
37 ("%-20s: Process at %5lu\n", hash_descriptor
[results
[x
].id
].name
, results
[x
].spd1
/ 1000);
42 /* RDTSC from Scott Duplichan */
45 #if defined __GNUC__ && !defined(LTC_NO_ASM)
48 asm ( " rdtsc ":"=A"(a
));
50 #elif defined(__i386__) || defined(__x86_64__)
52 asm __volatile__ ("rdtsc\nmovl %%eax,(%0)\nmovl %%edx,4(%0)\n"::"r"(&a
):"%eax","%edx");
54 #elif defined(LTC_PPC32) || defined(TFM_PPC32)
56 __asm__
__volatile__ ("mftbu %1 \nmftb %0\n":"=r"(a
), "=r"(b
));
57 return (((ulong64
)b
) << 32ULL) | ((ulong64
)a
);
58 #elif defined(__ia64__) /* gcc-IA64 version */
60 __asm__
__volatile__("mov %0=ar.itc" : "=r"(result
) :: "memory");
61 while (__builtin_expect ((int) result
== -1, 0))
62 __asm__
__volatile__("mov %0=ar.itc" : "=r"(result
) :: "memory");
64 #elif defined(__sparc__)
65 #if defined(__arch64__)
67 asm volatile("rd %%tick,%0" : "=r" (a
));
70 register unsigned long x
, y
;
71 __asm__
__volatile__ ("rd %%tick, %0; clruw %0, %1; srlx %0, 32, %0" : "=r" (x
), "=r" (y
) : "0" (x
), "1" (y
));
72 return ((unsigned long long) x
<< 32) | y
;
78 /* Microsoft and Intel Windows compilers */
79 #elif defined _M_IX86 && !defined(LTC_NO_ASM)
81 #elif defined _M_AMD64 && !defined(LTC_NO_ASM)
83 #elif defined _M_IA64 && !defined(LTC_NO_ASM)
84 #if defined __INTEL_COMPILER
85 #include <ia64intrin.h>
87 return __getReg (3116);
93 static ulong64 timer
, skew
= 0;
102 return rdtsc() - timer
;
105 void init_timer(void)
107 ulong64 c1
, c2
, t1
, t2
, t3
;
110 c1
= c2
= (ulong64
)-1;
111 for (y1
= 0; y1
< TIMES
*100; y1
++) {
115 t2
= (t_read() - t1
)>>1;
117 c1
= (t1
> c1
) ? t1
: c1
;
118 c2
= (t2
> c2
) ? t2
: c2
;
121 fprintf(stderr
, "Clock Skew: %lu\n", (unsigned long)skew
);
128 register_cipher (&aes_desc
);
131 register_cipher (&blowfish_desc
);
134 register_cipher (&xtea_desc
);
137 register_cipher (&rc5_desc
);
140 register_cipher (&rc6_desc
);
143 register_cipher (&saferp_desc
);
146 register_cipher (&twofish_desc
);
149 register_cipher (&safer_k64_desc
);
150 register_cipher (&safer_sk64_desc
);
151 register_cipher (&safer_k128_desc
);
152 register_cipher (&safer_sk128_desc
);
155 register_cipher (&rc2_desc
);
158 register_cipher (&des_desc
);
159 register_cipher (&des3_desc
);
162 register_cipher (&cast5_desc
);
165 register_cipher (&noekeon_desc
);
168 register_cipher (&skipjack_desc
);
171 register_cipher (&khazad_desc
);
174 register_cipher (&anubis_desc
);
177 register_cipher (&kseed_desc
);
180 register_cipher (&kasumi_desc
);
184 register_hash (&tiger_desc
);
187 register_hash (&md2_desc
);
190 register_hash (&md4_desc
);
193 register_hash (&md5_desc
);
196 register_hash (&sha1_desc
);
199 register_hash (&sha224_desc
);
202 register_hash (&sha256_desc
);
205 register_hash (&sha384_desc
);
208 register_hash (&sha512_desc
);
211 register_hash (&rmd128_desc
);
214 register_hash (&rmd160_desc
);
217 register_hash (&rmd256_desc
);
220 register_hash (&rmd320_desc
);
223 register_hash (&whirlpool_desc
);
226 register_hash(&chc_desc
);
227 if ((err
= chc_register(register_cipher(&aes_desc
))) != CRYPT_OK
) {
228 fprintf(stderr
, "chc_register error: %s\n", error_to_string(err
));
235 #error This demo requires Yarrow.
237 register_prng(&yarrow_desc
);
239 register_prng(&fortuna_desc
);
242 register_prng(&rc4_desc
);
245 register_prng(&sober128_desc
);
248 if ((err
= rng_make_prng(128, find_prng("yarrow"), &yarrow_prng
, NULL
)) != CRYPT_OK
) {
249 fprintf(stderr
, "rng_make_prng failed: %s\n", error_to_string(err
));
255 int time_keysched(void)
261 int (*func
) (const unsigned char *, int , int , symmetric_key
*);
262 unsigned char key
[MAXBLOCKSIZE
];
264 fprintf(stderr
, "\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
266 for (x
= 0; cipher_descriptor
[x
].name
!= NULL
; x
++) {
267 #define DO1(k) func(k, kl, 0, &skey);
269 func
= cipher_descriptor
[x
].setup
;
270 kl
= cipher_descriptor
[x
].min_key_length
;
272 for (y1
= 0; y1
< KTIMES
; y1
++) {
273 yarrow_read(key
, kl
, &yarrow_prng
);
277 c1
= (t1
> c1
) ? c1
: t1
;
280 results
[no_results
].spd1
= results
[no_results
].avg
= t1
;
281 results
[no_results
++].id
= x
;
282 fprintf(stderr
, "."); fflush(stdout
);
291 int time_cipher(void)
294 ulong64 t1
, t2
, c1
, c2
, a1
, a2
;
296 unsigned char key
[MAXBLOCKSIZE
], pt
[4096];
299 fprintf(stderr
, "\n\nECB Time Trials for the Symmetric Ciphers:\n");
301 for (x
= 0; cipher_descriptor
[x
].name
!= NULL
; x
++) {
302 ecb_start(x
, key
, cipher_descriptor
[x
].min_key_length
, 0, &ecb
);
304 /* sanity check on cipher */
305 if ((err
= cipher_descriptor
[x
].test()) != CRYPT_OK
) {
306 fprintf(stderr
, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor
[x
].name
, error_to_string(err
));
310 #define DO1 ecb_encrypt(pt, pt, sizeof(pt), &ecb);
313 c1
= c2
= (ulong64
)-1;
314 for (y1
= 0; y1
< 100; y1
++) {
322 c1
= (t1
> c1
? c1
: t1
);
323 c2
= (t2
> c2
? c2
: t2
);
329 #define DO1 ecb_decrypt(pt, pt, sizeof(pt), &ecb);
332 c1
= c2
= (ulong64
)-1;
333 for (y1
= 0; y1
< 100; y1
++) {
341 c1
= (t1
> c1
? c1
: t1
);
342 c2
= (t2
> c2
? c2
: t2
);
347 results
[no_results
].id
= x
;
348 results
[no_results
].spd1
= a1
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
349 results
[no_results
].spd2
= a2
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
350 results
[no_results
].avg
= (results
[no_results
].spd1
+ results
[no_results
].spd2
+1)/2;
352 fprintf(stderr
, "."); fflush(stdout
);
363 int time_cipher2(void)
366 ulong64 t1
, t2
, c1
, c2
, a1
, a2
;
368 unsigned char key
[MAXBLOCKSIZE
], pt
[4096];
371 fprintf(stderr
, "\n\nCBC Time Trials for the Symmetric Ciphers:\n");
373 for (x
= 0; cipher_descriptor
[x
].name
!= NULL
; x
++) {
374 cbc_start(x
, pt
, key
, cipher_descriptor
[x
].min_key_length
, 0, &cbc
);
376 /* sanity check on cipher */
377 if ((err
= cipher_descriptor
[x
].test()) != CRYPT_OK
) {
378 fprintf(stderr
, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor
[x
].name
, error_to_string(err
));
382 #define DO1 cbc_encrypt(pt, pt, sizeof(pt), &cbc);
385 c1
= c2
= (ulong64
)-1;
386 for (y1
= 0; y1
< 100; y1
++) {
394 c1
= (t1
> c1
? c1
: t1
);
395 c2
= (t2
> c2
? c2
: t2
);
401 #define DO1 cbc_decrypt(pt, pt, sizeof(pt), &cbc);
404 c1
= c2
= (ulong64
)-1;
405 for (y1
= 0; y1
< 100; y1
++) {
413 c1
= (t1
> c1
? c1
: t1
);
414 c2
= (t2
> c2
? c2
: t2
);
419 results
[no_results
].id
= x
;
420 results
[no_results
].spd1
= a1
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
421 results
[no_results
].spd2
= a2
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
422 results
[no_results
].avg
= (results
[no_results
].spd1
+ results
[no_results
].spd2
+1)/2;
424 fprintf(stderr
, "."); fflush(stdout
);
434 int time_cipher2(void) { fprintf(stderr
, "NO CBC\n"); return 0; }
438 int time_cipher3(void)
441 ulong64 t1
, t2
, c1
, c2
, a1
, a2
;
443 unsigned char key
[MAXBLOCKSIZE
], pt
[4096];
446 fprintf(stderr
, "\n\nCTR Time Trials for the Symmetric Ciphers:\n");
448 for (x
= 0; cipher_descriptor
[x
].name
!= NULL
; x
++) {
449 ctr_start(x
, pt
, key
, cipher_descriptor
[x
].min_key_length
, 0, CTR_COUNTER_LITTLE_ENDIAN
, &ctr
);
451 /* sanity check on cipher */
452 if ((err
= cipher_descriptor
[x
].test()) != CRYPT_OK
) {
453 fprintf(stderr
, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor
[x
].name
, error_to_string(err
));
457 #define DO1 ctr_encrypt(pt, pt, sizeof(pt), &ctr);
460 c1
= c2
= (ulong64
)-1;
461 for (y1
= 0; y1
< 100; y1
++) {
469 c1
= (t1
> c1
? c1
: t1
);
470 c2
= (t2
> c2
? c2
: t2
);
476 #define DO1 ctr_decrypt(pt, pt, sizeof(pt), &ctr);
479 c1
= c2
= (ulong64
)-1;
480 for (y1
= 0; y1
< 100; y1
++) {
488 c1
= (t1
> c1
? c1
: t1
);
489 c2
= (t2
> c2
? c2
: t2
);
494 results
[no_results
].id
= x
;
495 results
[no_results
].spd1
= a1
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
496 results
[no_results
].spd2
= a2
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
497 results
[no_results
].avg
= (results
[no_results
].spd1
+ results
[no_results
].spd2
+1)/2;
499 fprintf(stderr
, "."); fflush(stdout
);
509 int time_cipher3(void) { fprintf(stderr
, "NO CTR\n"); return 0; }
513 int time_cipher4(void)
516 ulong64 t1
, t2
, c1
, c2
, a1
, a2
;
518 unsigned char key
[MAXBLOCKSIZE
], pt
[4096];
521 fprintf(stderr
, "\n\nLRW Time Trials for the Symmetric Ciphers:\n");
523 for (x
= 0; cipher_descriptor
[x
].name
!= NULL
; x
++) {
524 if (cipher_descriptor
[x
].block_length
!= 16) continue;
525 lrw_start(x
, pt
, key
, cipher_descriptor
[x
].min_key_length
, key
, 0, &lrw
);
527 /* sanity check on cipher */
528 if ((err
= cipher_descriptor
[x
].test()) != CRYPT_OK
) {
529 fprintf(stderr
, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor
[x
].name
, error_to_string(err
));
533 #define DO1 lrw_encrypt(pt, pt, sizeof(pt), &lrw);
536 c1
= c2
= (ulong64
)-1;
537 for (y1
= 0; y1
< 100; y1
++) {
545 c1
= (t1
> c1
? c1
: t1
);
546 c2
= (t2
> c2
? c2
: t2
);
552 #define DO1 lrw_decrypt(pt, pt, sizeof(pt), &lrw);
555 c1
= c2
= (ulong64
)-1;
556 for (y1
= 0; y1
< 100; y1
++) {
564 c1
= (t1
> c1
? c1
: t1
);
565 c2
= (t2
> c2
? c2
: t2
);
571 results
[no_results
].id
= x
;
572 results
[no_results
].spd1
= a1
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
573 results
[no_results
].spd2
= a2
/(sizeof(pt
)/cipher_descriptor
[x
].block_length
);
574 results
[no_results
].avg
= (results
[no_results
].spd1
+ results
[no_results
].spd2
+1)/2;
576 fprintf(stderr
, "."); fflush(stdout
);
586 int time_cipher4(void) { fprintf(stderr
, "NO LRW\n"); return 0; }
592 unsigned long x
, y1
, len
;
593 ulong64 t1
, t2
, c1
, c2
;
595 int (*func
)(hash_state
*, const unsigned char *, unsigned long), err
;
596 unsigned char pt
[MAXBLOCKSIZE
];
599 fprintf(stderr
, "\n\nHASH Time Trials for:\n");
601 for (x
= 0; hash_descriptor
[x
].name
!= NULL
; x
++) {
603 /* sanity check on hash */
604 if ((err
= hash_descriptor
[x
].test()) != CRYPT_OK
) {
605 fprintf(stderr
, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor
[x
].name
, error_to_string(err
));
609 hash_descriptor
[x
].init(&md
);
611 #define DO1 func(&md,pt,len);
614 func
= hash_descriptor
[x
].process
;
615 len
= hash_descriptor
[x
].blocksize
;
617 c1
= c2
= (ulong64
)-1;
618 for (y1
= 0; y1
< TIMES
; y1
++) {
624 c1
= (t1
> c1
) ? c1
: t1
;
625 c2
= (t2
> c2
) ? c2
: t2
;
628 t1
= ((t1
* CONST64(1000))) / ((ulong64
)hash_descriptor
[x
].blocksize
);
629 results
[no_results
].id
= x
;
630 results
[no_results
].spd1
= results
[no_results
].avg
= t1
;
632 fprintf(stderr
, "."); fflush(stdout
);
642 /*#warning you need an mp_rand!!!*/
651 fprintf(stderr
, "Timing Multiplying:\n");
652 mp_init_multi(&a
,&b
,&c
,NULL
);
653 for (x
= 128/DIGIT_BIT
; x
<= 1536/DIGIT_BIT
; x
+= 128/DIGIT_BIT
) {
657 #define DO1 mp_mul(&a, &b, &c);
658 #define DO2 DO1; DO1;
661 for (y
= 0; y
< TIMES
; y
++) {
665 t1
= (t_read() - t1
)>>1;
666 if (t1
< t2
) t2
= t1
;
668 fprintf(stderr
, "%4lu bits: %9llu cycles\n", x
*DIGIT_BIT
, t2
);
670 mp_clear_multi(&a
,&b
,&c
,NULL
);
682 fprintf(stderr
, "Timing Squaring:\n");
683 mp_init_multi(&a
,&b
,NULL
);
684 for (x
= 128/DIGIT_BIT
; x
<= 1536/DIGIT_BIT
; x
+= 128/DIGIT_BIT
) {
687 #define DO1 mp_sqr(&a, &b);
688 #define DO2 DO1; DO1;
691 for (y
= 0; y
< TIMES
; y
++) {
695 t1
= (t_read() - t1
)>>1;
696 if (t1
< t2
) t2
= t1
;
698 fprintf(stderr
, "%4lu bits: %9llu cycles\n", x
*DIGIT_BIT
, t2
);
700 mp_clear_multi(&a
,&b
,NULL
);
706 void time_mult(void) { fprintf(stderr
, "NO MULT\n"); }
707 void time_sqr(void) { fprintf(stderr
, "NO SQR\n"); }
713 unsigned char buf
[4096];
718 fprintf(stderr
, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
719 for (x
= 0; prng_descriptor
[x
].name
!= NULL
; x
++) {
721 /* sanity check on prng */
722 if ((err
= prng_descriptor
[x
].test()) != CRYPT_OK
) {
723 fprintf(stderr
, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor
[x
].name
, error_to_string(err
));
727 prng_descriptor
[x
].start(&tprng
);
729 prng_descriptor
[x
].add_entropy(buf
, 256, &tprng
);
730 prng_descriptor
[x
].ready(&tprng
);
733 #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
735 for (y
= 0; y
< 10000; y
++) {
739 t1
= (t_read() - t1
)>>1;
740 if (t1
< t2
) t2
= t1
;
742 fprintf(stderr
, "%20s: %5llu ", prng_descriptor
[x
].name
, t2
>>12);
746 #define DO1 prng_descriptor[x].start(&tprng); prng_descriptor[x].add_entropy(buf, 32, &tprng); prng_descriptor[x].ready(&tprng); prng_descriptor[x].done(&tprng);
748 for (y
= 0; y
< 10000; y
++) {
752 t1
= (t_read() - t1
)>>1;
753 if (t1
< t2
) t2
= t1
;
755 fprintf(stderr
, "%5llu\n", t2
);
763 /* time various DSA operations */
770 static const struct {
780 for (x
= 0; x
< (sizeof(groups
)/sizeof(groups
[0])); x
++) {
782 for (y
= 0; y
< 4; y
++) {
785 if ((err
= dsa_make_key(&yarrow_prng
, find_prng("yarrow"), groups
[x
].group
, groups
[x
].modulus
, &key
)) != CRYPT_OK
) {
786 fprintf(stderr
, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
801 fprintf(stderr
, "DSA-(%lu, %lu) make_key took %15llu cycles\n", (unsigned long)groups
[x
].group
*8, (unsigned long)groups
[x
].modulus
*8, t2
);
808 /* time various RSA operations */
813 unsigned char buf
[2][2048];
814 unsigned long x
, y
, z
, zzz
;
817 for (x
= 1024; x
<= 2048; x
+= 256) {
819 for (y
= 0; y
< 4; y
++) {
822 if ((err
= rsa_make_key(&yarrow_prng
, find_prng("yarrow"), x
/8, 65537, &key
)) != CRYPT_OK
) {
823 fprintf(stderr
, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
839 fprintf(stderr
, "RSA-%lu make_key took %15llu cycles\n", x
, t2
);
842 for (y
= 0; y
< 16; y
++) {
846 if ((err
= rsa_encrypt_key(buf
[0], 32, buf
[1], &z
, (const unsigned char *)"testprog", 8, &yarrow_prng
,
847 find_prng("yarrow"), find_hash("sha1"),
848 &key
)) != CRYPT_OK
) {
849 fprintf(stderr
, "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
860 fprintf(stderr
, "RSA-%lu encrypt_key took %15llu cycles\n", x
, t2
);
863 for (y
= 0; y
< 2048; y
++) {
866 zzz
= sizeof(buf
[0]);
867 if ((err
= rsa_decrypt_key(buf
[1], z
, buf
[0], &zzz
, (const unsigned char *)"testprog", 8, find_hash("sha1"),
868 &zz
, &key
)) != CRYPT_OK
) {
869 fprintf(stderr
, "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
880 fprintf(stderr
, "RSA-%lu decrypt_key took %15llu cycles\n", x
, t2
);
883 for (y
= 0; y
< 256; y
++) {
887 if ((err
= rsa_sign_hash(buf
[0], 20, buf
[1], &z
, &yarrow_prng
,
888 find_prng("yarrow"), find_hash("sha1"), 8, &key
)) != CRYPT_OK
) {
889 fprintf(stderr
, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
900 fprintf(stderr
, "RSA-%lu sign_hash took %15llu cycles\n", x
, t2
);
903 for (y
= 0; y
< 2048; y
++) {
906 if ((err
= rsa_verify_hash(buf
[1], z
, buf
[0], 20, find_hash("sha1"), 8, &stat
, &key
)) != CRYPT_OK
) {
907 fprintf(stderr
, "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
911 fprintf(stderr
, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x
, y
);
922 fprintf(stderr
, "RSA-%lu verify_hash took %15llu cycles\n", x
, t2
);
923 fprintf(stderr
, "\n\n");
928 void time_rsa(void) { fprintf(stderr
, "NO RSA\n"); }
932 /* time various KAT operations */
933 void time_katja(void)
937 unsigned char buf
[2][4096];
938 unsigned long x
, y
, z
, zzz
;
941 for (x
= 1024; x
<= 2048; x
+= 256) {
943 for (y
= 0; y
< 4; y
++) {
946 if ((err
= katja_make_key(&yarrow_prng
, find_prng("yarrow"), x
/8, &key
)) != CRYPT_OK
) {
947 fprintf(stderr
, "\n\nkatja_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
958 fprintf(stderr
, "Katja-%lu make_key took %15llu cycles\n", x
, t2
);
961 for (y
= 0; y
< 16; y
++) {
965 if ((err
= katja_encrypt_key(buf
[0], 32, buf
[1], &z
, "testprog", 8, &yarrow_prng
,
966 find_prng("yarrow"), find_hash("sha1"),
967 &key
)) != CRYPT_OK
) {
968 fprintf(stderr
, "\n\nkatja_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
975 fprintf(stderr
, "Katja-%lu encrypt_key took %15llu cycles\n", x
, t2
);
978 for (y
= 0; y
< 2048; y
++) {
981 zzz
= sizeof(buf
[0]);
982 if ((err
= katja_decrypt_key(buf
[1], z
, buf
[0], &zzz
, "testprog", 8, find_hash("sha1"),
983 &zz
, &key
)) != CRYPT_OK
) {
984 fprintf(stderr
, "\n\nkatja_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
991 fprintf(stderr
, "Katja-%lu decrypt_key took %15llu cycles\n", x
, t2
);
998 void time_katja(void) { fprintf(stderr
, "NO Katja\n"); }
1002 /* time various ECC operations */
1007 unsigned char buf
[2][256];
1008 unsigned long i
, w
, x
, y
, z
;
1010 static unsigned long sizes
[] = {
1037 for (x
= sizes
[i
=0]; x
< 100000; x
= sizes
[++i
]) {
1039 for (y
= 0; y
< 256; y
++) {
1042 if ((err
= ecc_make_key(&yarrow_prng
, find_prng("yarrow"), x
, &key
)) != CRYPT_OK
) {
1043 fprintf(stderr
, "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
1059 fprintf(stderr
, "ECC-%lu make_key took %15llu cycles\n", x
*8, t2
);
1062 for (y
= 0; y
< 256; y
++) {
1066 if ((err
= ecc_encrypt_key(buf
[0], 20, buf
[1], &z
, &yarrow_prng
, find_prng("yarrow"), find_hash("sha1"),
1067 &key
)) != CRYPT_OK
) {
1068 fprintf(stderr
, "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
1079 fprintf(stderr
, "ECC-%lu encrypt_key took %15llu cycles\n", x
*8, t2
);
1082 for (y
= 0; y
< 256; y
++) {
1086 if ((err
= ecc_decrypt_key(buf
[1], z
, buf
[0], &w
, &key
)) != CRYPT_OK
) {
1087 fprintf(stderr
, "\n\necc_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
1098 fprintf(stderr
, "ECC-%lu decrypt_key took %15llu cycles\n", x
*8, t2
);
1101 for (y
= 0; y
< 256; y
++) {
1105 if ((err
= ecc_sign_hash(buf
[0], 20, buf
[1], &z
, &yarrow_prng
,
1106 find_prng("yarrow"), &key
)) != CRYPT_OK
) {
1107 fprintf(stderr
, "\n\necc_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
1118 fprintf(stderr
, "ECC-%lu sign_hash took %15llu cycles\n", x
*8, t2
);
1121 for (y
= 0; y
< 256; y
++) {
1124 if ((err
= ecc_verify_hash(buf
[1], z
, buf
[0], 20, &stat
, &key
)) != CRYPT_OK
) {
1125 fprintf(stderr
, "\n\necc_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err
), error_to_string(CRYPT_OK
));
1129 fprintf(stderr
, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x
*8, y
);
1140 fprintf(stderr
, "ECC-%lu verify_hash took %15llu cycles\n", x
*8, t2
);
1142 fprintf(stderr
, "\n\n");
1147 void time_ecc(void) { fprintf(stderr
, "NO ECC\n"); }
1150 void time_macs_(unsigned long MAC_SIZE
)
1152 unsigned char *buf
, key
[16], tag
[16];
1155 int err
, cipher_idx
, hash_idx
;
1157 fprintf(stderr
, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE
);
1159 buf
= XMALLOC(MAC_SIZE
*1024);
1161 fprintf(stderr
, "\n\nout of heap yo\n\n");
1165 cipher_idx
= find_cipher("aes");
1166 hash_idx
= find_hash("sha1");
1168 if (cipher_idx
== -1 || hash_idx
== -1) {
1169 fprintf(stderr
, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
1173 yarrow_read(buf
, MAC_SIZE
*1024, &yarrow_prng
);
1174 yarrow_read(key
, 16, &yarrow_prng
);
1178 for (x
= 0; x
< 10000; x
++) {
1182 if ((err
= omac_memory(cipher_idx
, key
, 16, buf
, MAC_SIZE
*1024, tag
, &z
)) != CRYPT_OK
) {
1183 fprintf(stderr
, "\n\nomac error... %s\n", error_to_string(err
));
1187 if (t1
< t2
) t2
= t1
;
1189 fprintf(stderr
, "OMAC-%s\t\t%9llu\n", cipher_descriptor
[cipher_idx
].name
, t2
/(ulong64
)(MAC_SIZE
*1024));
1194 for (x
= 0; x
< 10000; x
++) {
1198 if ((err
= xcbc_memory(cipher_idx
, key
, 16, buf
, MAC_SIZE
*1024, tag
, &z
)) != CRYPT_OK
) {
1199 fprintf(stderr
, "\n\nxcbc error... %s\n", error_to_string(err
));
1203 if (t1
< t2
) t2
= t1
;
1205 fprintf(stderr
, "XCBC-%s\t\t%9llu\n", cipher_descriptor
[cipher_idx
].name
, t2
/(ulong64
)(MAC_SIZE
*1024));
1210 for (x
= 0; x
< 10000; x
++) {
1214 if ((err
= f9_memory(cipher_idx
, key
, 16, buf
, MAC_SIZE
*1024, tag
, &z
)) != CRYPT_OK
) {
1215 fprintf(stderr
, "\n\nF9 error... %s\n", error_to_string(err
));
1219 if (t1
< t2
) t2
= t1
;
1221 fprintf(stderr
, "F9-%s\t\t\t%9llu\n", cipher_descriptor
[cipher_idx
].name
, t2
/(ulong64
)(MAC_SIZE
*1024));
1226 for (x
= 0; x
< 10000; x
++) {
1230 if ((err
= pmac_memory(cipher_idx
, key
, 16, buf
, MAC_SIZE
*1024, tag
, &z
)) != CRYPT_OK
) {
1231 fprintf(stderr
, "\n\npmac error... %s\n", error_to_string(err
));
1235 if (t1
< t2
) t2
= t1
;
1237 fprintf(stderr
, "PMAC-AES\t\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1242 for (x
= 0; x
< 10000; x
++) {
1246 if ((err
= pelican_memory(key
, 16, buf
, MAC_SIZE
*1024, tag
)) != CRYPT_OK
) {
1247 fprintf(stderr
, "\n\npelican error... %s\n", error_to_string(err
));
1251 if (t1
< t2
) t2
= t1
;
1253 fprintf(stderr
, "PELICAN \t\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1258 for (x
= 0; x
< 10000; x
++) {
1262 if ((err
= hmac_memory(hash_idx
, key
, 16, buf
, MAC_SIZE
*1024, tag
, &z
)) != CRYPT_OK
) {
1263 fprintf(stderr
, "\n\nhmac error... %s\n", error_to_string(err
));
1267 if (t1
< t2
) t2
= t1
;
1269 fprintf(stderr
, "HMAC-%s\t\t%9llu\n", hash_descriptor
[hash_idx
].name
, t2
/(ulong64
)(MAC_SIZE
*1024));
1275 void time_macs(void)
1282 void time_encmacs_(unsigned long MAC_SIZE
)
1284 unsigned char *buf
, IV
[16], key
[16], tag
[16];
1287 int err
, cipher_idx
;
1290 fprintf(stderr
, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE
);
1292 buf
= XMALLOC(MAC_SIZE
*1024);
1294 fprintf(stderr
, "\n\nout of heap yo\n\n");
1298 cipher_idx
= find_cipher("aes");
1300 yarrow_read(buf
, MAC_SIZE
*1024, &yarrow_prng
);
1301 yarrow_read(key
, 16, &yarrow_prng
);
1302 yarrow_read(IV
, 16, &yarrow_prng
);
1306 for (x
= 0; x
< 10000; x
++) {
1310 if ((err
= eax_encrypt_authenticate_memory(cipher_idx
, key
, 16, IV
, 16, NULL
, 0, buf
, MAC_SIZE
*1024, buf
, tag
, &z
)) != CRYPT_OK
) {
1311 fprintf(stderr
, "\nEAX error... %s\n", error_to_string(err
));
1315 if (t1
< t2
) t2
= t1
;
1317 fprintf(stderr
, "EAX \t\t\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1322 for (x
= 0; x
< 10000; x
++) {
1326 if ((err
= ocb_encrypt_authenticate_memory(cipher_idx
, key
, 16, IV
, buf
, MAC_SIZE
*1024, buf
, tag
, &z
)) != CRYPT_OK
) {
1327 fprintf(stderr
, "\nOCB error... %s\n", error_to_string(err
));
1331 if (t1
< t2
) t2
= t1
;
1333 fprintf(stderr
, "OCB \t\t\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1338 for (x
= 0; x
< 10000; x
++) {
1342 if ((err
= ccm_memory(cipher_idx
, key
, 16, NULL
, IV
, 16, NULL
, 0, buf
, MAC_SIZE
*1024, buf
, tag
, &z
, CCM_ENCRYPT
)) != CRYPT_OK
) {
1343 fprintf(stderr
, "\nCCM error... %s\n", error_to_string(err
));
1347 if (t1
< t2
) t2
= t1
;
1349 fprintf(stderr
, "CCM (no-precomp) \t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1351 cipher_descriptor
[cipher_idx
].setup(key
, 16, 0, &skey
);
1353 for (x
= 0; x
< 10000; x
++) {
1357 if ((err
= ccm_memory(cipher_idx
, key
, 16, &skey
, IV
, 16, NULL
, 0, buf
, MAC_SIZE
*1024, buf
, tag
, &z
, CCM_ENCRYPT
)) != CRYPT_OK
) {
1358 fprintf(stderr
, "\nCCM error... %s\n", error_to_string(err
));
1362 if (t1
< t2
) t2
= t1
;
1364 fprintf(stderr
, "CCM (precomp) \t\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1365 cipher_descriptor
[cipher_idx
].done(&skey
);
1370 for (x
= 0; x
< 100; x
++) {
1374 if ((err
= gcm_memory(cipher_idx
, key
, 16, IV
, 16, NULL
, 0, buf
, MAC_SIZE
*1024, buf
, tag
, &z
, GCM_ENCRYPT
)) != CRYPT_OK
) {
1375 fprintf(stderr
, "\nGCM error... %s\n", error_to_string(err
));
1379 if (t1
< t2
) t2
= t1
;
1381 fprintf(stderr
, "GCM (no-precomp)\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1385 #ifdef GCM_TABLES_SSE2
1386 __attribute__ ((aligned (16)))
1390 if ((err
= gcm_init(&gcm
, cipher_idx
, key
, 16)) != CRYPT_OK
) { fprintf(stderr
, "gcm_init: %s\n", error_to_string(err
)); exit(EXIT_FAILURE
); }
1392 for (x
= 0; x
< 10000; x
++) {
1396 if ((err
= gcm_reset(&gcm
)) != CRYPT_OK
) {
1397 fprintf(stderr
, "\nGCM error[%d]... %s\n", __LINE__
, error_to_string(err
));
1400 if ((err
= gcm_add_iv(&gcm
, IV
, 16)) != CRYPT_OK
) {
1401 fprintf(stderr
, "\nGCM error[%d]... %s\n", __LINE__
, error_to_string(err
));
1404 if ((err
= gcm_add_aad(&gcm
, NULL
, 0)) != CRYPT_OK
) {
1405 fprintf(stderr
, "\nGCM error[%d]... %s\n", __LINE__
, error_to_string(err
));
1408 if ((err
= gcm_process(&gcm
, buf
, MAC_SIZE
*1024, buf
, GCM_ENCRYPT
)) != CRYPT_OK
) {
1409 fprintf(stderr
, "\nGCM error[%d]... %s\n", __LINE__
, error_to_string(err
));
1413 if ((err
= gcm_done(&gcm
, tag
, &z
)) != CRYPT_OK
) {
1414 fprintf(stderr
, "\nGCM error[%d]... %s\n", __LINE__
, error_to_string(err
));
1418 if (t1
< t2
) t2
= t1
;
1420 fprintf(stderr
, "GCM (precomp)\t\t%9llu\n", t2
/(ulong64
)(MAC_SIZE
*1024));
1427 void time_encmacs(void)
1434 /* $Source: /cvs/libtom/libtomcrypt/testprof/x86_prof.c,v $ */
1435 /* $Revision: 1.51 $ */
1436 /* $Date: 2006/11/21 00:10:18 $ */