tv_gen.c

来自「Dropbear is an SSH 2 server, designed to」· C语言 代码 · 共 671 行 · 第 1/2 页

C
671
字号
"of the same format (length specified per cipher).  The OMAC key in step N+1 is the OMAC output of\n""step N (repeated as required to fill the array).\n\n");   for (x = 0; cipher_descriptor[x].name != NULL; x++) {      kl = cipher_descriptor[x].block_length;      /* skip ciphers which do not have 64 or 128 bit block sizes */      if (kl != 8 && kl != 16) continue;      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {         kl = cipher_descriptor[x].max_key_length;      }      fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);            /* initial key/block */      for (y = 0; y < kl; y++) {          key[y] = (y & 255);      }            for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {         for (z = 0; z < y; z++) {            input[z] = (unsigned char)(z & 255);         }         len = sizeof(output);         if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {            printf("Error omacing: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         fprintf(out, "%3d: ", y);         for (z = 0; z <(int)len; z++) {            fprintf(out, "%02X", output[z]);         }         fprintf(out, "\n");         /* forward the key */         for (z = 0; z < kl; z++) {             key[z] = output[z % len];         }      }      fprintf(out, "\n");   }   fclose(out);}void eax_gen(void){   int err, kl, x, y1, z;   FILE *out;   unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];   unsigned long len;   out = fopen("eax_tv.txt", "w");   fprintf(out, "EAX Test Vectors.  Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key.  The outputs\n"                "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"                "step repeated sufficiently.\n\n");   for (x = 0; cipher_descriptor[x].name != NULL; x++) {      kl = cipher_descriptor[x].block_length;      /* skip ciphers which do not have 64 or 128 bit block sizes */      if (kl != 8 && kl != 16) continue;      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {         kl = cipher_descriptor[x].max_key_length;      }      fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);      /* the key */      for (z = 0; z < kl; z++) {          key[z] = (z & 255);      }            for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){         for (z = 0; z < y1; z++) {            plaintext[z] = (unsigned char)(z & 255);            nonce[z]     = (unsigned char)(z & 255);            header[z]    = (unsigned char)(z & 255);         }         len = sizeof(tag);         if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {            printf("Error EAX'ing: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         fprintf(out, "%3d: ", y1);         for (z = 0; z < y1; z++) {            fprintf(out, "%02X", plaintext[z]);         }         fprintf(out, ", ");         for (z = 0; z <(int)len; z++) {            fprintf(out, "%02X", tag[z]);         }         fprintf(out, "\n");         /* forward the key */         for (z = 0; z < kl; z++) {             key[z] = tag[z % len];         }      }      fprintf(out, "\n");   }   fclose(out);}void ocb_gen(void){   int err, kl, x, y1, z;   FILE *out;   unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];   unsigned long len;   out = fopen("ocb_tv.txt", "w");   fprintf(out, "OCB Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/plaintext/key.  The outputs\n"                "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"                "step repeated sufficiently.  The nonce is fixed throughout.\n\n");   for (x = 0; cipher_descriptor[x].name != NULL; x++) {      kl = cipher_descriptor[x].block_length;      /* skip ciphers which do not have 64 or 128 bit block sizes */      if (kl != 8 && kl != 16) continue;      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {         kl = cipher_descriptor[x].max_key_length;      }      fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);      /* the key */      for (z = 0; z < kl; z++) {          key[z] = (z & 255);      }      /* fixed nonce */      for (z = 0; z < cipher_descriptor[x].block_length; z++) {          nonce[z] = z;      }            for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){         for (z = 0; z < y1; z++) {            plaintext[z] = (unsigned char)(z & 255);         }         len = sizeof(tag);         if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {            printf("Error OCB'ing: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         fprintf(out, "%3d: ", y1);         for (z = 0; z < y1; z++) {            fprintf(out, "%02X", plaintext[z]);         }         fprintf(out, ", ");         for (z = 0; z <(int)len; z++) {            fprintf(out, "%02X", tag[z]);         }         fprintf(out, "\n");         /* forward the key */         for (z = 0; z < kl; z++) {             key[z] = tag[z % len];         }      }      fprintf(out, "\n");   }   fclose(out);}void ccm_gen(void){   int err, kl, x, y1, z;   FILE *out;   unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];   unsigned long len;   out = fopen("ccm_tv.txt", "w");   fprintf(out, "CCM Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key.  The outputs\n"                "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"                "step repeated sufficiently.  The nonce is fixed throughout at 13 bytes 000102...\n\n");   for (x = 0; cipher_descriptor[x].name != NULL; x++) {      kl = cipher_descriptor[x].block_length;      /* skip ciphers which do not have 128 bit block sizes */      if (kl != 16) continue;      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {         kl = cipher_descriptor[x].max_key_length;      }      fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);      /* the key */      for (z = 0; z < kl; z++) {          key[z] = (z & 255);      }      /* fixed nonce */      for (z = 0; z < cipher_descriptor[x].block_length; z++) {          nonce[z] = z;      }            for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){         for (z = 0; z < y1; z++) {            plaintext[z] = (unsigned char)(z & 255);         }         len = sizeof(tag);         if ((err = ccm_memory(x, key, kl, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {            printf("Error CCM'ing: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         fprintf(out, "%3d: ", y1);         for (z = 0; z < y1; z++) {            fprintf(out, "%02X", plaintext[z]);         }         fprintf(out, ", ");         for (z = 0; z <(int)len; z++) {            fprintf(out, "%02X", tag[z]);         }         fprintf(out, "\n");         /* forward the key */         for (z = 0; z < kl; z++) {             key[z] = tag[z % len];         }      }      fprintf(out, "\n");   }   fclose(out);}void gcm_gen(void){   int err, kl, x, y1, z;   FILE *out;   unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];   unsigned long len;   out = fopen("gcm_tv.txt", "w");   fprintf(out, "GCM Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key.  The outputs\n"                "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"                "step repeated sufficiently.  The nonce is fixed throughout at 13 bytes 000102...\n\n");   for (x = 0; cipher_descriptor[x].name != NULL; x++) {      kl = cipher_descriptor[x].block_length;      /* skip ciphers which do not have 128 bit block sizes */      if (kl != 16) continue;      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {         kl = cipher_descriptor[x].max_key_length;      }      fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);      /* the key */      for (z = 0; z < kl; z++) {          key[z] = (z & 255);      }           for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){         for (z = 0; z < y1; z++) {            plaintext[z] = (unsigned char)(z & 255);         }         len = sizeof(tag);         if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {            printf("Error GCM'ing: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         fprintf(out, "%3d: ", y1);         for (z = 0; z < y1; z++) {            fprintf(out, "%02X", plaintext[z]);         }         fprintf(out, ", ");         for (z = 0; z <(int)len; z++) {            fprintf(out, "%02X", tag[z]);         }         fprintf(out, "\n");         /* forward the key */         for (z = 0; z < kl; z++) {             key[z] = tag[z % len];         }      }      fprintf(out, "\n");   }   fclose(out);}void base64_gen(void){   FILE *out;   unsigned char dst[256], src[32];   unsigned long x, y, len;      out = fopen("base64_tv.txt", "w");   fprintf(out, "Base64 vectors.  These are the base64 encodings of the strings 00,01,02...NN-1\n\n");   for (x = 0; x <= 32; x++) {       for (y = 0; y < x; y++) {           src[y] = y;       }       len = sizeof(dst);       base64_encode(src, x, dst, &len);       fprintf(out, "%2lu: %s\n", x, dst);   }   fclose(out);}int main(void){   reg_algs();   printf("Generating hash   vectors..."); fflush(stdout); hash_gen(); printf("done\n");   printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");   printf("Generating HMAC   vectors..."); fflush(stdout); hmac_gen(); printf("done\n");   printf("Generating OMAC   vectors..."); fflush(stdout); omac_gen(); printf("done\n");   printf("Generating PMAC   vectors..."); fflush(stdout); pmac_gen(); printf("done\n");   printf("Generating EAX    vectors..."); fflush(stdout); eax_gen(); printf("done\n");   printf("Generating OCB    vectors..."); fflush(stdout); ocb_gen(); printf("done\n");   printf("Generating CCM    vectors..."); fflush(stdout); ccm_gen(); printf("done\n");   printf("Generating GCM    vectors..."); fflush(stdout); gcm_gen(); printf("done\n");   printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");   return 0;}                                  /* $Source: /cvs/libtom/libtomcrypt/demos/tv_gen.c,v $ *//* $Revision: 1.4 $ *//* $Date: 2005/05/05 14:35:56 $ */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?