tv_gen.c

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

C
671
字号
#include <tomcrypt.h>void reg_algs(void){  int err;#ifdef RIJNDAEL  register_cipher (&aes_desc);#endif#ifdef BLOWFISH  register_cipher (&blowfish_desc);#endif#ifdef XTEA  register_cipher (&xtea_desc);#endif#ifdef RC5  register_cipher (&rc5_desc);#endif#ifdef RC6  register_cipher (&rc6_desc);#endif#ifdef SAFERP  register_cipher (&saferp_desc);#endif#ifdef TWOFISH  register_cipher (&twofish_desc);#endif#ifdef SAFER  register_cipher (&safer_k64_desc);  register_cipher (&safer_sk64_desc);  register_cipher (&safer_k128_desc);  register_cipher (&safer_sk128_desc);#endif#ifdef RC2  register_cipher (&rc2_desc);#endif#ifdef DES  register_cipher (&des_desc);  register_cipher (&des3_desc);#endif#ifdef CAST5  register_cipher (&cast5_desc);#endif#ifdef NOEKEON  register_cipher (&noekeon_desc);#endif#ifdef SKIPJACK  register_cipher (&skipjack_desc);#endif#ifdef ANUBIS  register_cipher (&anubis_desc);#endif#ifdef KHAZAD  register_cipher (&khazad_desc);#endif#ifdef TIGER  register_hash (&tiger_desc);#endif#ifdef MD2  register_hash (&md2_desc);#endif#ifdef MD4  register_hash (&md4_desc);#endif#ifdef MD5  register_hash (&md5_desc);#endif#ifdef SHA1  register_hash (&sha1_desc);#endif#ifdef SHA224  register_hash (&sha224_desc);#endif#ifdef SHA256  register_hash (&sha256_desc);#endif#ifdef SHA384  register_hash (&sha384_desc);#endif#ifdef SHA512  register_hash (&sha512_desc);#endif#ifdef RIPEMD128  register_hash (&rmd128_desc);#endif#ifdef RIPEMD160  register_hash (&rmd160_desc);#endif#ifdef WHIRLPOOL  register_hash (&whirlpool_desc);#endif#ifdef CHC_HASH  register_hash(&chc_desc);  if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {     printf("chc_register error: %s\n", error_to_string(err));     exit(EXIT_FAILURE);  }#endif}void hash_gen(void){   unsigned char md[MAXBLOCKSIZE], *buf;   unsigned long outlen, x, y, z;   FILE *out;   int   err;      out = fopen("hash_tv.txt", "w");   if (out == NULL) {      perror("can't open hash_tv");   }      fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");   for (x = 0; hash_descriptor[x].name != NULL; x++) {      buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1);      if (buf == NULL) {         perror("can't alloc mem");         exit(EXIT_FAILURE);      }      fprintf(out, "Hash: %s\n", hash_descriptor[x].name);      for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {         for (z = 0; z < y; z++) {            buf[z] = (unsigned char)(z & 255);         }         outlen = sizeof(md);         if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) {            printf("hash_memory error: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         fprintf(out, "%3lu: ", y);         for (z = 0; z < outlen; z++) {            fprintf(out, "%02X", md[z]);         }         fprintf(out, "\n");      }      fprintf(out, "\n");      XFREE(buf);   }   fclose(out);}void cipher_gen(void){   unsigned char *key, pt[MAXBLOCKSIZE];   unsigned long x, y, z, w;   int err, kl, lastkl;   FILE *out;   symmetric_key skey;      out = fopen("cipher_tv.txt", "w");      fprintf(out, "Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n""The output of step N is used as the key and plaintext for step N+1 (key bytes repeated as required to fill the key)\n\n");                      for (x = 0; cipher_descriptor[x].name != NULL; x++) {      fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name);            /* three modes, smallest, medium, large keys */      lastkl = 10000;      for (y = 0; y < 3; y++) {         switch (y) {            case 0: kl = cipher_descriptor[x].min_key_length; break;            case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;            case 2: kl = cipher_descriptor[x].max_key_length; break;         }         if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) {            printf("keysize error: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }         if (kl == lastkl) break;         lastkl = kl;         fprintf(out, "Key Size: %d bytes\n", kl);         key = XMALLOC(kl);         if (key == NULL) {            perror("can't malloc memory");            exit(EXIT_FAILURE);         }         for (z = 0; (int)z < kl; z++) {             key[z] = (unsigned char)z;         }         if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {            printf("setup error: %s\n", error_to_string(err));            exit(EXIT_FAILURE);         }                  for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {            pt[z] = (unsigned char)z;         }         for (w = 0; w < 50; w++) {             cipher_descriptor[x].ecb_encrypt(pt, pt, &skey);             fprintf(out, "%2lu: ", w);             for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {                fprintf(out, "%02X", pt[z]);             }             fprintf(out, "\n");             /* reschedule a new key */             for (z = 0; z < (unsigned long)kl; z++) {                 key[z] = pt[z % cipher_descriptor[x].block_length];             }             if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {                printf("cipher setup2 error: %s\n", error_to_string(err));                exit(EXIT_FAILURE);             }         }         fprintf(out, "\n");         XFREE(key);     }     fprintf(out, "\n");  }  fclose(out);}  void hmac_gen(void){   unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input;   int x, y, z, err;   FILE *out;   unsigned long len;     out = fopen("hmac_tv.txt", "w");   fprintf(out, "HMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed.  The initial key is\n""of the same format (the same length as the HASH output size).  The HMAC key in step N+1 is the HMAC output of\n""step N.\n\n");   for (x = 0; hash_descriptor[x].name != NULL; x++) {      fprintf(out, "HMAC-%s\n", hash_descriptor[x].name);            /* initial key */      for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {          key[y] = (y&255);      }      input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1);      if (input == NULL) {         perror("Can't malloc memory");         exit(EXIT_FAILURE);      }            for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {         for (z = 0; z < y; z++) {            input[z] = (unsigned char)(z & 255);         }         len = sizeof(output);         if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) {            printf("Error hmacing: %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 */         memcpy(key, output, hash_descriptor[x].hashsize);      }      XFREE(input);      fprintf(out, "\n");   }   fclose(out);}   void omac_gen(void){   unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];   int err, x, y, z, kl;   FILE *out;   unsigned long len;     out = fopen("omac_tv.txt", "w");   fprintf(out, "OMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed.  The initial key is\n""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, "OMAC-%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 = omac_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 pmac_gen(void){   unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];   int err, x, y, z, kl;   FILE *out;   unsigned long len;     out = fopen("pmac_tv.txt", "w");   fprintf(out, "PMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed.  The initial key is\n"

⌨️ 快捷键说明

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