📄 test.c
字号:
/* 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]); exit (-1); } printf ("passed.\n"); /* test the rsa_encrypt_key functions */ for (x = 0; x < 16; x++) in[x] = x; y = sizeof (out); if ((errnum = rsa_encrypt_key (in, 16, out, &y, &prng, find_prng ("yarrow"), &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } zeromem (in, sizeof (in)); x = sizeof (in); if ((errnum = rsa_decrypt_key (out, y, in, &x, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("RSA en/de crypt key routines: "); if (x != 16) { printf ("Failed (length)\n"); exit (-1); } for (x = 0; x < 16; x++) if (in[x] != x) { printf ("Failed (contents)\n"); exit (-1); } printf ("Passed\n"); /* test sign_hash functions */ for (x = 0; x < 16; x++) in[x] = x; x = sizeof (in); if ((errnum = rsa_sign_hash (in, 16, out, &x, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("RSA signed hash: %lu bytes\n", x); if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("Verify hash: %s, ", stat ? "passed" : "failed"); in[0] ^= 1; if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("%s\n", (!stat) ? "passed" : "failed"); if (stat) exit (-1); rsa_free (&key); /* make a RSA key */#ifdef SONY_PS2_NOPE limit = 1024;#else limit = 2048;#endif { int tt; for (z = 1024; z <= limit; z += 512) { t = XCLOCK (); for (tt = 0; tt < 3; tt++) { if ((errnum = rsa_make_key (&prng, find_prng ("yarrow"), z / 8, 65537, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } /* check modulus size */ if (mp_unsigned_bin_size(&key.N) != (int)(z/8)) { printf("\nRSA key supposed to be %lu bits but was %d bits\n", z, mp_count_bits(&key.N)); exit(EXIT_FAILURE); } if (tt < 2) { rsa_free (&key); } } t = XCLOCK () - t; printf ("Took %.0f ms to make a %ld-bit RSA key.\n", 1000.0 * (((double) t / 3.0) / (double) XCLOCKS_PER_SEC), z); /* time encryption */ t = XCLOCK (); for (tt = 0; tt < 20; tt++) { y = sizeof (in); if ((errnum = rsa_exptmod (in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } } t = XCLOCK () - t; printf ("Took %.0f ms to encrypt with a %ld-bit RSA key.\n", 1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z); /* time decryption */ t = XCLOCK (); for (tt = 0; tt < 20; tt++) { x = sizeof (out); if ((errnum = rsa_exptmod (out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } } t = XCLOCK () - t; printf ("Took %.0f ms to decrypt with a %ld-bit RSA key.\n", 1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z); rsa_free (&key); } }}#elsevoidpad_test (void){ printf ("MRSA not compiled in\n");}voidrsa_test (void){ printf ("MRSA not compiled in\n");}#endif#ifdef BASE64voidbase64_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 (errnum)); exit (-1); } 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 (errnum)); exit (-1); } printf (" decoded %ld bytes to %ld bytes\n", x, y); for (x = 0; x < 16; x++) if (buf[0][x] != x) { printf (" **failed**\n"); exit (-1); } printf (" passed\n");}#elsevoidbase64_test (void){ printf ("Base64 not compiled in\n");}#endifvoidtime_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 < (5 * 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); 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 += 32; } 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); }}voidtime_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 < 3 * XCLOCKS_PER_SEC) { DO256; y1 += 256; } t1 = XCLOCK () - t1; func = cipher_descriptor[x].ecb_decrypt; y2 = 0; t2 = XCLOCK (); while (XCLOCK () - t2 < 3 * 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 MDHvoiddh_tests (void){ unsigned char buf[3][4096]; unsigned long x, y, z; int low, high, stat, stat2; dh_key usera, userb; clock_t t1; printf("Testing builting DH parameters...."); fflush(stdout); if ((errnum = dh_test()) != CRYPT_OK) { printf("DH Error: %s\n", error_to_string(errnum)); exit(-1); } printf("Passed.\n"); dh_sizes (&low, &high); printf ("DH Keys from %d to %d supported.\n", low * 8, high * 8); /* make up two keys */ if ((errnum = dh_make_key (&prng, find_prng ("yarrow"), 96, &usera)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } if ((errnum = dh_make_key (&prng, find_prng ("yarrow"), 96, &userb)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } /* make the shared secret */ x = 4096; if ((errnum = dh_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } y = 4096; if ((errnum = dh_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } if (y != x) { printf ("DH Shared keys are not same size.\n"); exit (-1); } if (memcmp (buf[0], buf[1], x)) { printf ("DH Shared keys not same contents.\n"); exit (-1); } /* now export userb */ y = 4096; if ((errnum = dh_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } dh_free (&userb); /* import and make the shared secret again */ if ((errnum = dh_import (buf[1], y, &userb)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } z = 4096; if ((errnum = dh_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("DH routines: "); if (z != x) { printf ("failed. Size don't match?\n"); exit (-1); } if (memcmp (buf[0], buf[2], x)) { printf ("Failed. Content didn't match.\n"); exit (-1); } printf ("Passed\n"); dh_free (&usera); dh_free (&userb);/* time stuff */ { static int sizes[] = { 96, 128, 160, 192, 224, 256, 320, 384, 512 }; int ii, tt; for (ii = 0; ii < (int) (sizeof (sizes) / sizeof (sizes[0])); ii++) { t1 = XCLOCK (); for (tt = 0; tt < 25; tt++) { dh_make_key (&prng, find_prng ("yarrow"), sizes[ii], &usera); dh_free (&usera); } t1 = XCLOCK () - t1; printf ("Make dh-%d key took %f msec\n", sizes[ii] * 8, 1000.0 * (((double) t1 / 25.0) / (double) XCLOCKS_PER_SEC)); } }/* test encrypt_key */ dh_make_key (&prng, find_prng ("yarrow"), 128, &usera); for (x = 0; x < 16; x++) buf[0][x] = x; y = sizeof (buf[1]); if ((errnum = 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 (errnum)); exit (-1); } zeromem (buf[0], sizeof (buf[0])); x = sizeof (buf[0]); if ((errnum = dh_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("DH en/de crypt key routines: "); if (x != 16) { printf ("Failed (length)\n"); exit (-1); } for (x = 0; x < 16; x++) if (buf[0][x] != x) { printf ("Failed (contents)\n"); exit (-1); } printf ("Passed (size %lu)\n", y);/* test sign_hash */ for (x = 0; x < 16; x++) buf[0][x] = x; x = sizeof (buf[1]); if ((errnum = dh_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"), &usera)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } if ((errnum = dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } buf[0][0] ^= 1; if ((errnum = dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } printf ("dh_sign/verify_hash: %s (%d,%d), %lu\n", ((stat == 1) && (stat2 == 0)) ? "passed" : "failed", stat, stat2, x); dh_free (&usera);}#elsevoiddh_tests (void){ printf ("MDH not compiled in\n");}#endifint callback_x = 0;voidcallback (void){ printf ("%c\x08", "-\\|/"[++callback_x & 3]);#ifndef SONY_PS2 fflush (stdout);#endif}voidrng_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 ((errnum = rng_make_prng (128, find_prng ("yarrow"), &prng, &callback)) != CRYPT_OK) { printf (" starting yarrow error: %s\n", error_to_string (errnum)); exit (-1); }#endif}#ifdef MECCvoidecc_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 ((errnum = ecc_test ()) != CRYPT_OK) { printf ("ecc Error: %s\n", error_to_string (errnum)); exit (-1); } ecc_sizes (&low, &high); printf ("ecc Keys from %d to %d supported.\n", low * 8, high * 8); /* make up two keys */ if ((errnum = ecc_make_key (&prng, find_prng ("yarrow"), 24, &usera)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } if ((errnum = ecc_make_key (&prng, find_prng ("yarrow"), 24, &userb)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } /* make the shared secret */ x = 4096; if ((errnum = ecc_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } y = 4096; if ((errnum = ecc_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) { printf ("Error: %s\n", error_to_string (errnum)); exit (-1); } if (y != x) { printf ("ecc Shared keys are not same size.\n"); exit (-1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -