~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/demos/tv_gen.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <mycrypt.h>
 
1
#include <tomcrypt.h>
2
2
 
3
3
void reg_algs(void)
4
4
{
47
47
#ifdef SKIPJACK
48
48
  register_cipher (&skipjack_desc);
49
49
#endif
 
50
#ifdef ANUBIS
 
51
  register_cipher (&anubis_desc);
 
52
#endif
 
53
#ifdef KHAZAD
 
54
  register_cipher (&khazad_desc);
 
55
#endif
50
56
 
51
57
#ifdef TIGER
52
58
  register_hash (&tiger_desc);
495
501
   fclose(out);
496
502
}
497
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));
 
603
            exit(EXIT_FAILURE);
 
604
         }
 
605
         fprintf(out, "%3d: ", y1);
 
606
         for (z = 0; z < y1; z++) {
 
607
            fprintf(out, "%02X", plaintext[z]);
 
608
         }
 
609
         fprintf(out, ", ");
 
610
         for (z = 0; z <(int)len; z++) {
 
611
            fprintf(out, "%02X", tag[z]);
 
612
         }
 
613
         fprintf(out, "\n");
 
614
 
 
615
         /* forward the key */
 
616
         for (z = 0; z < kl; z++) {
 
617
             key[z] = tag[z % len];
 
618
         }
 
619
      }
 
620
      fprintf(out, "\n");
 
621
   }
 
622
   fclose(out);
 
623
}
 
624
 
498
625
void base64_gen(void)
499
626
{
500
627
   FILE *out;
524
651
   printf("Generating PMAC   vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
525
652
   printf("Generating EAX    vectors..."); fflush(stdout); eax_gen(); printf("done\n");
526
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
656
   printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
528
657
   return 0;
529
658
}
535
664
      
536
665
    
537
666
   
 
667
 
 
668
/* $Source: /cvs/libtom/libtomcrypt/demos/tv_gen.c,v $ */
 
669
/* $Revision: 1.4 $ */
 
670
/* $Date: 2005/05/05 14:35:56 $ */