⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 NIST推荐的素域上的椭圆曲线
💻 C
📖 第 1 页 / 共 3 页
字号:
      printf(" **failed**\n");       return;    }   printf("  passed\n");}#elsevoid base64_test(void) { printf("Base64 not compiled in\n"); }#endifvoid time_hash(void){    clock_t t1;    int x, y;    unsigned long z;    unsigned char input[4096], out[MAXBLOCKSIZE];    printf("Hash Time Trials (4KB blocks):\n");    for (x = 0; hash_descriptor[x].name != NULL; x++) {        t1 = XCLOCK();        z = sizeof(out);        y = 0;        while (XCLOCK() - t1 < (3 * XCLOCKS_PER_SEC)) {            hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);            hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);            hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);            hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z);             hash_memory(x, input, 4096, out, &z); y += 16;        }        t1 = XCLOCK() - t1;        printf("%-20s: Hash at %5.2f Mbit/sec\n", hash_descriptor[x].name,               ((8.0 * 4096.0) * ((double)y / ((double)t1 / (double)XCLOCKS_PER_SEC))) / 1000000.0);    }}void time_ecb(void){    clock_t t1, t2;    long x, y1, y2;    unsigned char pt[32], key[32];    symmetric_key skey;    void (*func)(const unsigned char *, unsigned char *, symmetric_key *);    printf("ECB Time Trials for the Symmetric Ciphers:\n");    for (x = 0; cipher_descriptor[x].name != NULL; x++) {        cipher_descriptor[x].setup(key, cipher_descriptor[x].min_key_length, 0, &skey);#define DO1   func(pt,pt,&skey);#define DO2   DO1 DO1#define DO4   DO2 DO2#define DO8   DO4 DO4#define DO16  DO8 DO8#define DO32  DO16 DO16#define DO64  DO32 DO32#define DO128 DO64 DO64#define DO256 DO128 DO128         func = cipher_descriptor[x].ecb_encrypt;        y1 = 0;        t1 = XCLOCK();        while (XCLOCK() - t1 < 2*XCLOCKS_PER_SEC) {            DO256; y1 += 256;        }        t1 = XCLOCK() - t1;        func = cipher_descriptor[x].ecb_decrypt;        y2 = 0;        t2 = XCLOCK();        while (XCLOCK() - t2 < 2*XCLOCKS_PER_SEC) {            DO256; y2 += 256;        }        t2 = XCLOCK() - t2;        printf("%-20s: Encrypt at %5.2f Mbit/sec and Decrypt at %5.2f Mbit/sec\n",               cipher_descriptor[x].name,               ((8.0 * (double)cipher_descriptor[x].block_length) * ((double)y1 / ((double)t1 / (double)XCLOCKS_PER_SEC))) / 1000000.0,               ((8.0 * (double)cipher_descriptor[x].block_length) * ((double)y2 / ((double)t2 / (double)XCLOCKS_PER_SEC))) / 1000000.0);#undef DO256#undef DO128#undef DO64#undef DO32#undef DO16#undef DO8#undef DO4#undef DO2#undef DO1    }}#ifdef MDHvoid dh_tests(void){   unsigned char buf[3][4096];   unsigned long x, y, z;   int low, high, stat, stat2;   dh_key usera, userb;   clock_t t1;/*   if ((errno = dh_test()) != CRYPT_OK) printf("DH Error: %s\n", error_to_string(errno)); */   dh_sizes(&low, &high);   printf("DH Keys from %d to %d supported.\n", low*8, high*8);   /* make up two keys */   if ((errno = dh_make_key(&prng, find_prng("yarrow"), 96, &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   if ((errno = dh_make_key(&prng, find_prng("yarrow"), 96, &userb)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   /* make the shared secret */   x = 4096;   if ((errno = dh_shared_secret(&usera, &userb, buf[0], &x)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   y = 4096;   if ((errno = dh_shared_secret(&userb, &usera, buf[1], &y)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   if (y != x) { printf("DH Shared keys are not same size.\n"); return; }   if (memcmp(buf[0], buf[1], x)) { printf("DH Shared keys not same contents.\n"); return; }   /* now export userb */   y = 4096;   if ((errno = dh_export(buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   dh_free(&userb);   /* import and make the shared secret again */   if ((errno = dh_import(buf[1], &userb)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   z = 4096;   if ((errno = dh_shared_secret(&usera, &userb, buf[2], &z)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   printf("DH routines: ");   if (z != x) { printf("failed.  Size don't match?\n"); return; }   if (memcmp(buf[0], buf[2], x)) { printf("Failed.  Content didn't match.\n"); return; }   printf("Passed\n");   dh_free(&usera);   dh_free(&userb);/* time stuff */   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 64, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-512 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 96, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-768 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 128, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-1024 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);#ifndef SONY_PS2   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 160, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-1280 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 192, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-1536 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 224, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-1792 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 256, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-2048 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);   t1 = XCLOCK();   dh_make_key(&prng, find_prng("yarrow"), 320, &usera);   t1 = XCLOCK() - t1;   printf("Make dh-2560 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   dh_free(&usera);#endif      #ifdef PK_PACKET/* try dh packet stuff */   for (x = 0; x < 16; x++) buf[0][x] = (unsigned char)x;   dh_make_key(&prng, find_prng("yarrow"), 24, &usera);   x = 4096;   if (dh_encrypt(buf[0], 16, buf[1], &x, &prng, find_prng("yarrow"), find_cipher("rijndael"),                   find_hash("sha1"), &usera) != CRYPT_OK) {      printf("dh_encrypt says %s\n", error_to_string(errno));      return;   }   printf("dh encrypted 16 bytes into %ld bytes!\n", x);   y = 4096;   if ((errno = dh_decrypt(buf[1], x, buf[2], &y, &usera)) != CRYPT_OK) {      printf("dh_decrypt says %s\n", error_to_string(errno));      return;   }   printf("dh packet: ");   if (16 != y) { printf("Failed: Sizes different! 16 vs %ld\n", y); return; }   if (memcmp(buf[0], buf[2], 16)) { printf("Failed; Content mismatch.\n"); return; }   printf("Passed!\n");   dh_free(&usera);/* try dh signatures */   dh_make_key(&prng, find_prng("yarrow"), 96, &usera);   x = 4096;   if ((errno = dh_sign("hello", 5, buf[0], &x, find_hash("sha1"), &prng, find_prng("yarrow"), &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   printf("dh-768 Signature took %ld bytes\n", x);   if ((errno = dh_verify(buf[0], "hello", 5, &stat, &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   if ((errno = dh_verify(buf[0], "hellp", 5, &stat2, &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   printf("dh Signatures: %s (%d,%d)\n", ((stat==1)&&(stat2==0))?"passed":"failed", stat,stat2);   dh_free(&usera);#endif  /* test encrypt_key */ dh_make_key(&prng, find_prng("yarrow"), 96, &usera); for (x = 0; x < 16; x++) buf[0][x] = x; y = sizeof(buf[1]); if ((errno = dh_encrypt_key(buf[0], 16, buf[1], &y, &prng, find_prng("yarrow"), find_hash("md5"), &usera)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } zeromem(buf[0], sizeof(buf[0])); x = sizeof(buf[0]); if ((errno = dh_decrypt_key(buf[1], buf[0], &x, &usera)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("DH en/de crypt key routines: "); if (x != 16) { printf("Failed (length)\n"); return; } for (x = 0; x < 16; x++) if (buf[0][x] != x) { printf("Failed (contents)\n"); return; } printf("Passed (size %lu)\n", y);/* test sign_hash */   for (x = 0; x < 16; x++) buf[0][x] = x;   x = sizeof(buf[1]);   if ((errno = dh_sign_hash(buf[0], 16, buf[1], &x, &prng, find_prng("yarrow"), &usera)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return;   }   if (dh_verify_hash(buf[1], buf[0], 16, &stat, &usera)) {    printf("Error: %s\n", error_to_string(errno));    return;   }   buf[0][0] ^= 1;   if (dh_verify_hash(buf[1], buf[0], 16, &stat2, &usera)) {    printf("Error: %s\n", error_to_string(errno));    return;   }   printf("dh_sign/verify_hash: %s (%d,%d)\n", ((stat==1)&&(stat2==0))?"passed":"failed", stat,stat2); dh_free(&usera);}#elsevoid dh_tests(void) { printf("MDH not compiled in\n"); }#endifint callback_x = 0;void callback(void){    printf("%c\x08", "-\\|/"[++callback_x & 3]); #ifndef SONY_PS2   fflush(stdout);#endif}void rng_tests(void){   unsigned char buf[16];   clock_t t1;   int x, y;   printf("RNG tests\n");   t1 = XCLOCK();   x = rng_get_bytes(buf, sizeof(buf), &callback);   t1 = XCLOCK() - t1;   printf("  %f bytes per second...",         (double)x / ((double)t1 / (double)XCLOCKS_PER_SEC));   printf("read %d bytes.\n  ", x);   for (y = 0; y < x; y++)       printf("%02x ", buf[y]);   printf("\n");#ifdef YARROW   if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, &callback)) != CRYPT_OK) {       printf(" starting yarrow error: %s\n", error_to_string(errno));       exit(-1);   }#endif}#ifdef MECCvoid ecc_tests(void){   unsigned char buf[4][4096];   unsigned long x, y, z;   int stat, stat2, low, high;   ecc_key usera, userb;   clock_t t1;   if ((errno = ecc_test()) != CRYPT_OK) printf("ecc Error: %s\n", error_to_string(errno));   ecc_sizes(&low, &high);   printf("ecc Keys from %d to %d supported.\n", low*8, high*8);   /* make up two keys */   if ((errno = ecc_make_key(&prng, find_prng("yarrow"), 24, &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   if ((errno = ecc_make_key(&prng, find_prng("yarrow"), 24, &userb)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   /* make the shared secret */   x = 4096;   if ((errno = ecc_shared_secret(&usera, &userb, buf[0], &x)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   y = 4096;   if ((errno = ecc_shared_secret(&userb, &usera, buf[1], &y)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   if (y != x) { printf("ecc Shared keys are not same size.\n"); return; }   if (memcmp(buf[0], buf[1], x)) { printf("ecc Shared keys not same contents.\n"); return; }   /* now export userb */   y = 4096;   if ((errno = ecc_export(buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   ecc_free(&userb);   printf("ECC-192 export took %ld bytes\n", y);   /* import and make the shared secret again */   if ((errno = ecc_import(buf[1], &userb)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   z = 4096;   if ((errno = ecc_shared_secret(&usera, &userb, buf[2], &z)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   printf("ecc routines: ");   if (z != x) { printf("failed.  Size don't match?\n"); return; }   if (memcmp(buf[0], buf[2], x)) { printf("Failed.  Content didn't match.\n"); return; }   printf("Passed\n");   ecc_free(&usera);   ecc_free(&userb);/* time stuff */   t1 = XCLOCK();   ecc_make_key(&prng, find_prng("yarrow"), 20, &usera);   t1 = XCLOCK() - t1;   printf("Make ECC-160 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   ecc_free(&usera);   t1 = XCLOCK();   ecc_make_key(&prng, find_prng("yarrow"), 24, &usera);   t1 = XCLOCK() - t1;   printf("Make ECC-192 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   ecc_free(&usera);   t1 = XCLOCK();   ecc_make_key(&prng, find_prng("yarrow"), 28, &usera);   t1 = XCLOCK() - t1;   printf("Make ECC-224 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   ecc_free(&usera);   #ifndef SONY_PS2   t1 = XCLOCK();   ecc_make_key(&prng, find_prng("yarrow"), 32, &usera);   t1 = XCLOCK() - t1;   printf("Make ECC-256 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   ecc_free(&usera);   t1 = XCLOCK();   ecc_make_key(&prng, find_prng("yarrow"), 48, &usera);   t1 = XCLOCK() - t1;   printf("Make ECC-384 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   ecc_free(&usera);   t1 = XCLOCK();   ecc_make_key(&prng, find_prng("yarrow"), 65, &usera);   t1 = XCLOCK() - t1;   printf("Make ECC-521 key took %f msec\n", 1000.0 * ((double)t1 / (double)XCLOCKS_PER_SEC));   ecc_free(&usera);#endif   #ifdef PK_PACKET/* try ECC packet stuff */   for (x = 0; x < 16; x++) buf[0][x] = (unsigned char)x;   ecc_make_key(&prng, find_prng("yarrow"), 20, &usera);   x = 4096;   if (ecc_encrypt(buf[0], 16, buf[1], &x, &prng, find_prng("yarrow"), find_cipher("rijndael"),                   find_hash("tiger"), &usera) != CRYPT_OK) {      printf("ecc_encrypt says %s\n", error_to_string(errno));      return;   }   printf("Ecc encrypted 16 bytes into %ld bytes!\n", x);   y = 4096;   if ((errno = ecc_decrypt(buf[1], x, buf[2], &y, &usera)) != CRYPT_OK) {      printf("ecc_decrypt says %s\n", error_to_string(errno));      return;   }   printf("ECC packet: ");   if (16 != y) { printf("Failed: Sizes different! 16 vs %ld\n", y); return; }   if (memcmp(buf[0], buf[2], 16)) { printf("Failed; Content mismatch.\n"); return; }   printf("Passed!\n");   ecc_free(&usera);/* try ECC signatures */   ecc_make_key(&prng, find_prng("yarrow"), 20, &usera);   x = 4096;   if ((errno = ecc_sign("hello", 5, buf[0], &x, find_hash("sha1"), &prng, find_prng("yarrow"), &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   printf("ECC-160 Signature took %ld bytes\n", x);   if ((errno = ecc_verify(buf[0], "hello", 5, &stat, &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   if ((errno = ecc_verify(buf[0], "hellp", 5, &stat2, &usera)) != CRYPT_OK) {      printf("Error: %s\n", error_to_string(errno));      return;   }   printf("ECC Signatures: %s (%d,%d)\n", ((stat==1)&&(stat2==0))?"passed":"failed", stat,stat2);   ecc_free(&usera);#endif/* test encrypt_key */ ecc_make_key(&prng, find_prng("yarrow"), 32, &usera); for (x = 0; x < 16; x++) buf[0][x] = x; y = sizeof(buf[1]); if ((errno = ecc_encrypt_key(buf[0], 16, buf[1], &y, &prng, find_prng("yarrow"), find_hash("md5"), &usera)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } zeromem(buf[0], sizeof(buf[0])); x = sizeof(buf[0]); if ((errno = ecc_decrypt_key(buf[1],buf[0], &x, &usera)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("ECC en/de crypt key routines: "); if (x != 16) { printf("Failed (length)\n"); return; } for (x = 0; x < 16; x++) if (buf[0][x] != x) { printf("Failed (contents)\n"); return; } printf("Passed (size: %lu)\n", y);/* test sign_hash */   for (x = 0; x < 16; x++) buf[0][x] = x;   x = sizeof(buf[1]);   if ((errno = ecc_sign_hash(buf[0], 16, buf[1], &x, &prng, find_prng("yarrow"), &usera)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return;   }   if (ecc_verify_hash(buf[1], buf[0], 16, &stat, &usera)) {    printf("Error: %s\n", error_to_string(errno));    return;   }   buf[0][0] ^= 1;   if (ecc_verify_hash(buf[1], buf[0], 16, &stat2, &usera)) {    printf("Error: %s\n", error_to_string(errno));    return;   }   printf("ecc_sign/verify_hash: %s (%d,%d)\n", ((stat==1)&&(stat2==0))?"passed":"failed", stat,stat2);

⌨️ 快捷键说明

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