comparison demos/tv_gen.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents 5d99163f7e32
children 39d5d58461d6
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
1 #include <mycrypt.h> 1 #include <tomcrypt.h>
2 2
3 void reg_algs(void) 3 void reg_algs(void)
4 { 4 {
5 int err; 5 int err;
6 6
44 #ifdef NOEKEON 44 #ifdef NOEKEON
45 register_cipher (&noekeon_desc); 45 register_cipher (&noekeon_desc);
46 #endif 46 #endif
47 #ifdef SKIPJACK 47 #ifdef SKIPJACK
48 register_cipher (&skipjack_desc); 48 register_cipher (&skipjack_desc);
49 #endif
50 #ifdef ANUBIS
51 register_cipher (&anubis_desc);
52 #endif
53 #ifdef KHAZAD
54 register_cipher (&khazad_desc);
49 #endif 55 #endif
50 56
51 #ifdef TIGER 57 #ifdef TIGER
52 register_hash (&tiger_desc); 58 register_hash (&tiger_desc);
53 #endif 59 #endif
471 plaintext[z] = (unsigned char)(z & 255); 477 plaintext[z] = (unsigned char)(z & 255);
472 } 478 }
473 len = sizeof(tag); 479 len = sizeof(tag);
474 if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) { 480 if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
475 printf("Error OCB'ing: %s\n", error_to_string(err)); 481 printf("Error OCB'ing: %s\n", error_to_string(err));
482 exit(EXIT_FAILURE);
483 }
484 fprintf(out, "%3d: ", y1);
485 for (z = 0; z < y1; z++) {
486 fprintf(out, "%02X", plaintext[z]);
487 }
488 fprintf(out, ", ");
489 for (z = 0; z <(int)len; z++) {
490 fprintf(out, "%02X", tag[z]);
491 }
492 fprintf(out, "\n");
493
494 /* forward the key */
495 for (z = 0; z < kl; z++) {
496 key[z] = tag[z % len];
497 }
498 }
499 fprintf(out, "\n");
500 }
501 fclose(out);
502 }
503
504
505 void ccm_gen(void)
506 {
507 int err, kl, x, y1, z;
508 FILE *out;
509 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
510 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
511 unsigned long len;
512
513 out = fopen("ccm_tv.txt", "w");
514 fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
515 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
516 "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
517
518 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
519 kl = cipher_descriptor[x].block_length;
520
521 /* skip ciphers which do not have 128 bit block sizes */
522 if (kl != 16) continue;
523
524 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
525 kl = cipher_descriptor[x].max_key_length;
526 }
527 fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
528
529 /* the key */
530 for (z = 0; z < kl; z++) {
531 key[z] = (z & 255);
532 }
533
534 /* fixed nonce */
535 for (z = 0; z < cipher_descriptor[x].block_length; z++) {
536 nonce[z] = z;
537 }
538
539 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
540 for (z = 0; z < y1; z++) {
541 plaintext[z] = (unsigned char)(z & 255);
542 }
543 len = sizeof(tag);
544 if ((err = ccm_memory(x, key, kl, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
545 printf("Error CCM'ing: %s\n", error_to_string(err));
546 exit(EXIT_FAILURE);
547 }
548 fprintf(out, "%3d: ", y1);
549 for (z = 0; z < y1; z++) {
550 fprintf(out, "%02X", plaintext[z]);
551 }
552 fprintf(out, ", ");
553 for (z = 0; z <(int)len; z++) {
554 fprintf(out, "%02X", tag[z]);
555 }
556 fprintf(out, "\n");
557
558 /* forward the key */
559 for (z = 0; z < kl; z++) {
560 key[z] = tag[z % len];
561 }
562 }
563 fprintf(out, "\n");
564 }
565 fclose(out);
566 }
567
568 void gcm_gen(void)
569 {
570 int err, kl, x, y1, z;
571 FILE *out;
572 unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
573 unsigned long len;
574
575 out = fopen("gcm_tv.txt", "w");
576 fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
577 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
578 "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
579
580 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
581 kl = cipher_descriptor[x].block_length;
582
583 /* skip ciphers which do not have 128 bit block sizes */
584 if (kl != 16) continue;
585
586 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
587 kl = cipher_descriptor[x].max_key_length;
588 }
589 fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
590
591 /* the key */
592 for (z = 0; z < kl; z++) {
593 key[z] = (z & 255);
594 }
595
596 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
597 for (z = 0; z < y1; z++) {
598 plaintext[z] = (unsigned char)(z & 255);
599 }
600 len = sizeof(tag);
601 if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {
602 printf("Error GCM'ing: %s\n", error_to_string(err));
476 exit(EXIT_FAILURE); 603 exit(EXIT_FAILURE);
477 } 604 }
478 fprintf(out, "%3d: ", y1); 605 fprintf(out, "%3d: ", y1);
479 for (z = 0; z < y1; z++) { 606 for (z = 0; z < y1; z++) {
480 fprintf(out, "%02X", plaintext[z]); 607 fprintf(out, "%02X", plaintext[z]);
522 printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n"); 649 printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
523 printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n"); 650 printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
524 printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n"); 651 printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
525 printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n"); 652 printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
526 printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n"); 653 printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
654 printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n");
655 printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
527 printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); 656 printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
528 return 0; 657 return 0;
529 } 658 }
530 659
531 660