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

📄 test.c

📁 NIST推荐的素域上的椭圆曲线
💻 C
📖 第 1 页 / 共 3 页
字号:
/* This is the worst code you have ever seen written on purpose.... this code is just a big hack to testout the functionality of the library */#ifdef SONY_PS2#include <eetypes.h>#include <eeregs.h>#include "timer.h"#endif#include "../mycrypt.h"int errno;int null_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey){   return CRYPT_OK;}void null_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key){   memcpy(ct, pt, 8);}void null_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key){   memcpy(pt, ct, 8);}int null_test(void){   return CRYPT_OK;}int null_keysize(int *desired_keysize){   return CRYPT_OK;} const struct _cipher_descriptor null_desc ={    "memcpy()",    255,    8, 8, 8, 1,    &null_setup,    &null_ecb_encrypt,    &null_ecb_decrypt,    &null_test,    &null_keysize};prng_state prng;void store_tests(void) { unsigned char buf[8]; unsigned long L; ulong64 LL; printf("LOAD32/STORE32 tests\n"); L = 0x12345678UL; STORE32L(L, &buf[0]); L = 0; LOAD32L(L, &buf[0]); if (L != 0x12345678UL) printf("LOAD/STORE32 Little don't work\n"); LL = CONST64(0x01020304050607); STORE64L(LL, &buf[0]); LL = 0; LOAD64L(LL, &buf[0]) if (LL != CONST64(0x01020304050607)) printf("LOAD/STORE64 Little don't work\n"); L = 0x12345678UL; STORE32H(L, &buf[0]); L = 0; LOAD32H(L, &buf[0]); if (L != 0x12345678UL) printf("LOAD/STORE32 High don't work\n"); LL = CONST64(0x01020304050607); STORE64H(LL, &buf[0]); LL = 0; LOAD64H(LL, &buf[0]) if (LL != CONST64(0x01020304050607)) printf("LOAD/STORE64 High don't work\n");}void cipher_tests(void) {   int x;   printf("Ciphers compiled in\n"); for (x = 0; cipher_descriptor[x].name != NULL; x++) {     printf(" %12s (%2d) Key Size: %4ld to %4ld, Block Size: %3ld, Default # of rounds: %2ld\n", cipher_descriptor[x].name,            cipher_descriptor[x].ID,            cipher_descriptor[x].min_key_length*8,cipher_descriptor[x].max_key_length*8,            cipher_descriptor[x].block_length*8, cipher_descriptor[x].default_rounds); }}void ecb_tests(void){ int x; printf("ECB tests\n"); for (x = 0; cipher_descriptor[x].name != NULL; x++) {     printf(" %12s: ",           cipher_descriptor[x].name);     if ((errno = cipher_descriptor[x].test()) != CRYPT_OK)        printf(" **failed** Reason: %s\n", error_to_string(errno));     else         printf("passed\n"); }}#ifdef CBCvoid cbc_tests(void){ symmetric_CBC cbc; int x, y; unsigned char blk[32], ct[32], key[32], IV[32]; const unsigned char test[] = { 0XFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; printf("CBC tests\n"); /* ---- CBC ENCODING ---- */ /* make up a block and IV */ for (x = 0; x < 32; x++) blk[x] = IV[x] = x; /* now lets start a cbc session */ if ((errno = cbc_start(find_cipher("blowfish"), IV, key, 16, 0, &cbc)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets encode 32 bytes */ for (x = 0; x < 4; x++)    cbc_encrypt(blk+8*x, ct+8*x, &cbc); zeromem(blk, sizeof(blk)); /* ---- CBC DECODING ---- */ /* make up a IV */ for (x = 0; x < 32; x++) IV[x] = x; /* now lets start a cbc session */ if ((errno = cbc_start(find_cipher("blowfish"), IV, key, 16, 0, &cbc)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets decode 32 bytes */ for (x = 0; x < 4; x++)    cbc_decrypt(ct+8*x, blk+8*x, &cbc); /* print output */ for (x = y = 0; x < 32; x++) if (blk[x] != x) y = 1; printf("  %s\n", y?"failed":"passed"); /* lets actually check the bytes */ memset(IV, 0, 8); IV[0] = 0xFF;              /* IV  = FF 00 00 00 00 00 00 00 */ memset(blk, 0, 32); blk[8] = 0xFF;           /* BLK = 00 00 00 00 00 00 00 00 FF 00 00 00 00 00 00 00 */ cbc_start(find_cipher("memcpy()"), IV, key, 8, 0, &cbc); cbc_encrypt(blk, ct, &cbc);                  /* expect: FF 00 00 00 00 00 00 00 */ cbc_encrypt(blk+8, ct+8, &cbc);              /* expect: 00 00 00 00 00 00 00 00 */ if (memcmp(ct, test, 16)) {    printf("CBC failed logical testing.\n");    for (x = 0; x < 16; x++) printf("%02x ", ct[x]);    printf("\n"); } else {    printf("CBC passed logical testing.\n"); }}#elsevoid cbc_tests(void) { printf("CBC not compiled in\n"); }#endif#ifdef OFBvoid ofb_tests(void){ symmetric_OFB ofb; int x, y; unsigned char blk[32], ct[32], key[32], IV[32]; printf("OFB tests\n"); /* ---- ofb ENCODING ---- */ /* make up a block and IV */ for (x = 0; x < 32; x++) blk[x] = IV[x] = x; /* now lets start a ofb session */ if ((errno = ofb_start(find_cipher("blowfish"), IV, key, 16, 0, &ofb)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets encode 32 bytes */ for (x = 0; x < 4; x++)    ofb_encrypt(blk+8*x, ct+8*x, 8, &ofb); zeromem(blk, sizeof(blk)); /* ---- ofb DECODING ---- */ /* make up a IV */ for (x = 0; x < 32; x++) IV[x] = x; /* now lets start a ofb session */ if ((errno = ofb_start(find_cipher("blowfish"), IV, key, 16, 0, &ofb)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets decode 32 bytes */ for (x = 0; x < 4; x++)    ofb_decrypt(ct+8*x, blk+8*x, 8, &ofb); /* print output */ for (x = y = 0; x < 32; x++) if (blk[x] != x) y = 1; printf("  %s\n", y?"failed":"passed");}#elsevoid ofb_tests(void) { printf("OFB not compiled in\n"); }#endif#ifdef CFBvoid cfb_tests(void){ symmetric_CFB cfb; int x, y; unsigned char blk[32], ct[32], key[32], IV[32]; printf("CFB tests\n"); /* ---- cfb ENCODING ---- */ /* make up a block and IV */ for (x = 0; x < 32; x++) blk[x] = IV[x] = x; /* now lets start a cfb session */ if ((errno = cfb_start(find_cipher("blowfish"), IV, key, 16, 0, &cfb)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets encode 32 bytes */ for (x = 0; x < 4; x++)    cfb_encrypt(blk+8*x, ct+8*x, 8, &cfb); zeromem(blk, sizeof(blk)); /* ---- cfb DECODING ---- */ /* make up ahash_descriptor[prng->yarrow.hash].hashsize IV */ for (x = 0; x < 32; x++) IV[x] = x; /* now lets start a cfb session */ if ((errno = cfb_start(find_cipher("blowfish"), IV, key, 16, 0, &cfb)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets decode 32 bytes */ for (x = 0; x < 4; x++)    cfb_decrypt(ct+8*x, blk+8*x, 8, &cfb); /* print output */ for (x = y = 0; x < 32; x++) if (blk[x] != x) y = 1; printf("  %s\n", y?"failed":"passed");}#elsevoid cfb_tests(void) { printf("CFB not compiled in\n"); }#endif#ifdef CTRvoid ctr_tests(void){ symmetric_CTR ctr; int x, y; unsigned char blk[32], ct[32], key[32], count[32]; const unsigned char test[] = { 0xFF, 0, 0, 0, 0, 0, 0, 0,  0, 3, 0, 0, 0, 0, 0, 0 }; printf("CTR tests\n"); /* ---- CTR ENCODING ---- */ /* make up a block and IV */ for (x = 0; x < 32; x++) blk[x] = count[x] = x; /* now lets start a ctr session */ if ((errno = ctr_start(find_cipher("rijndael"), count, key, 16, 0, &ctr)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets encode 32 bytes */ for (x = 0; x < 4; x++)    ctr_encrypt(blk+8*x, ct+8*x, 8, &ctr); zeromem(blk, sizeof(blk)); /* ---- CTR DECODING ---- */ /* make up a IV */ for (x = 0; x < 32; x++) count[x] = x; /* now lets start a cbc session */ if ((errno = ctr_start(find_cipher("rijndael"), count, key, 16, 0, &ctr)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* now lets decode 32 bytes */ for (x = 0; x < 4; x++)    ctr_decrypt(ct+8*x, blk+8*x, 8, &ctr); /* print output */ for (x = y = 0; x < 32; x++) if (blk[x] != x) y = 1; printf("  %s\n", y?"failed":"passed"); /* lets actually check the bytes */ memset(count, 0, 8); count[0] = 0xFF;        /* IV  = FF 00 00 00 00 00 00 00 */ memset(blk, 0, 32); blk[9] = 2;              /* BLK = 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 */ ctr_start(find_cipher("memcpy()"), count, key, 8, 0, &ctr); ctr_encrypt(blk, ct, 8, &ctr);               /* expect: FF 00 00 00 00 00 00 00 */ ctr_encrypt(blk+8, ct+8, 8, &ctr);           /* expect: 00 03 00 00 00 00 00 00 */ if (memcmp(ct, test, 16)) {    printf("CTR failed logical testing.\n");    for (x = 0; x < 16; x++) printf("%02x ", ct[x]);    printf("\n"); } else {    printf("CTR passed logical testing.\n"); }}#elsevoid ctr_tests(void) { printf("CTR not compiled in\n"); }#endifvoid hash_tests(void){ int x; printf("Hash tests\n"); for (x = 0; hash_descriptor[x].name != NULL; x++) {     printf(" %10s (%2d) ", hash_descriptor[x].name, hash_descriptor[x].ID);     if (hash_descriptor[x].test() != CRYPT_OK)        printf("**failed** Reason: %s\n", error_to_string(errno));     else         printf("passed\n"); }}#ifdef MRSAvoid pad_test(void){ unsigned char in[100], out[100]; unsigned long x, y;   /* make a dummy message */ for (x = 0; x < 16; x++) in[x] = (unsigned char)x; /* pad the message so that random filler is placed before and after it */ y = 100; if ((errno = rsa_pad(in, 16, out, &y, find_prng("yarrow"), &prng)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* depad the message to get the original content */ memset(in, 0, sizeof(in)); x = 100; if ((errno = rsa_depad(out, y, in, &x)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* check outcome */ printf("rsa_pad: "); if (x != 16) { printf("Failed.  Wrong size.\n"); return; } for (x = 0; x < 16; x++) if (in[x] != x) { printf("Failed.  Expected %02lx and got %02x.\n", x, in[x]); return; } printf("passed.\n");}void rsa_test(void){ unsigned char in[4096], out[4096]; unsigned long x, y, z, limit; int stat; rsa_key key; clock_t t; /* ---- SINGLE ENCRYPT ---- */ /* encrypt a short 8 byte string */ if ((errno = rsa_make_key(&prng, find_prng("yarrow"), 1024/8, 65537, &key)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } for (x = 0; x < 8; x++) in[x] = (unsigned char)(x+1); y = sizeof(in); if ((errno = rsa_exptmod(in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* decrypt it */ zeromem(in, sizeof(in)); x = sizeof(out); if ((errno = rsa_exptmod(out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; } /* compare */ printf("RSA    : "); for (x = 0; x < 8; x++) if (in[x] != (x+1)) { printf("Failed.  x==%02lx, in[%ld]==%02x\n", x, x, in[x]); } printf("passed.\n");#ifdef PK_PACKET  /* ---- BLOCK ENCRYPT ---- */ /* now lets test rsa_encrypt() */ for (x = 0; x < 8; x++) in[x] = (unsigned char)x; x = sizeof(out); if ((errno = rsa_encrypt(in, 8, out, &x, &prng, find_prng("yarrow"), find_cipher("rijndael"), &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } /* test rsa_decrypt() */ zeromem(in, sizeof(in)); y = sizeof(in); if ((errno = rsa_decrypt(out, x, in, &y, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("rsa_encrypt()/rsa_decrypt(): "); for (y = 0; y < 8; y++) if (in[y] != y) { printf("failed.\n"); return; } printf("Passed.\n"); /* ---- SIGNATURES ---- */ x = sizeof(in); if ((errno = rsa_sign("hello", 5, in, &x, find_hash("md5"), &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } if ((errno = rsa_verify(in, "hello", 5, &stat, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("RSA Signatures: %s, ", (stat==1)?"pass":"fail"); if ((errno = rsa_verify(in, "abcde", 5, &stat, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("%s\n", (stat==0)?"pass":"fail"); /* ---- EXPORT/IMPORT ---- */ x = sizeof(out); if ((errno = rsa_export(out, &x, PK_PRIVATE_OPTIMIZED, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("RSA Export takes %lu bytes\n", x); rsa_free(&key); if ((errno = rsa_import(out, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("RSA Import: "); if ((errno = rsa_verify(in, "hello", 5, &stat, &key)) != CRYPT_OK) {     printf("Error: %s\n", error_to_string(errno));    return; } printf("%s, ", (stat==1)?"pass":"fail"); if ((errno = rsa_verify(in, "abcde", 5, &stat, &key)) != CRYPT_OK) {     printf("Error: %s\n", error_to_string(errno));    return; } printf("%s\n", (stat==0)?"pass":"fail");#endif  /* test the rsa_encrypt_key functions */ for (x = 0; x < 16; x++) in[x] = x; y = sizeof(out); if ((errno = rsa_encrypt_key(in, 16, out, &y, &prng, find_prng("yarrow"), &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } zeromem(in, sizeof(in)); x = sizeof(in); if ((errno = rsa_decrypt_key(out, in, &x, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("RSA en/de crypt key routines: "); if (x != 16) { printf("Failed (length)\n"); return; } for (x = 0; x < 16; x++) if (in[x] != x) { printf("Failed (contents)\n"); return; } printf("Passed\n"); /* test sign_hash functions */ for (x = 0; x < 16; x++) in[x] = x; x = sizeof(in); if ((errno = rsa_sign_hash(in, 16, out, &x, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("RSA signed hash: %lu bytes\n", x); if ((errno = rsa_verify_hash(out, in, &stat, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("Verify hash: %s, ", stat?"passed":"failed"); in[0] ^= 1; if ((errno = rsa_verify_hash(out, in, &stat, &key)) != CRYPT_OK) {    printf("Error: %s\n", error_to_string(errno));    return; } printf("%s\n", (!stat)?"passed":"failed"); rsa_free(&key); /* make a RSA key */#ifdef SONY_PS2   limit = 1024;#else   limit = 2048;#endif for (z = 1024; z <= limit; z += 512) {    t = XCLOCK();    if ((errno = rsa_make_key(&prng, find_prng("yarrow"), z/8, 65537, &key)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; }    t = XCLOCK() - t;    printf("Took %.0f ms to make a %ld-bit RSA key.\n", 1000.0 * ((double)t / (double)XCLOCKS_PER_SEC), z);    /* time encryption */    y = sizeof(in);    t = XCLOCK();    if ((errno = rsa_exptmod(in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; }    t = XCLOCK() - t;    printf("Took %.0f ms to encrypt with a %ld-bit RSA key.\n", 1000.0 * ((double)t / (double)XCLOCKS_PER_SEC), z);    /* time decryption */    x = sizeof(out);    t = XCLOCK();    if ((errno = rsa_exptmod(out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) { printf("Error: %s\n", error_to_string(errno)); return; }    t = XCLOCK() - t;    printf("Took %.0f ms to decrypt with a %ld-bit RSA key.\n", 1000.0 * ((double)t / (double)XCLOCKS_PER_SEC), z);    rsa_free(&key); } }#elsevoid pad_test(void) { printf("MRSA not compiled in\n"); }void rsa_test(void) { printf("MRSA not compiled in\n"); }#endif#ifdef BASE64void base64_test(void){   unsigned char buf[2][100];   unsigned long x, y;   printf("Base64 tests\n");   zeromem(buf, sizeof(buf));   for (x = 0; x < 16; x++) buf[0][x] = (unsigned char)x;   x = 100;   if (base64_encode(buf[0], 16, buf[1], &x) != CRYPT_OK) {      printf("  error: %s\n", error_to_string(errno));      return;   }   printf("  encoded 16 bytes to %ld bytes...[%s]\n", x, buf[1]);   memset(buf[0], 0, 100);   y = 100;   if (base64_decode(buf[1], x, buf[0], &y) != CRYPT_OK) {      printf("  error: %s\n", error_to_string(errno));      return;   }   printf("  decoded %ld bytes to %ld bytes\n", x, y);   for (x = 0; x < 16; x++) if (buf[0][x] != x) { 

⌨️ 快捷键说明

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