2003-03-03 00:59:24 +00:00
|
|
|
/* This is the worst code you have ever seen written on purpose.... this code is just a big hack to test
|
|
|
|
out the functionality of the library */
|
|
|
|
|
|
|
|
#ifdef SONY_PS2
|
|
|
|
#include <eetypes.h>
|
|
|
|
#include <eeregs.h>
|
|
|
|
#include "timer.h"
|
|
|
|
#endif
|
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
#include <mycrypt.h>
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
int errnum;
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
int
|
|
|
|
null_setup (const unsigned char *key, int keylen, int num_rounds,
|
2003-06-01 18:55:11 +00:00
|
|
|
symmetric_key * skey)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
return CRYPT_OK;
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
null_ecb_encrypt (const unsigned char *pt, unsigned char *ct,
|
2003-06-01 18:55:11 +00:00
|
|
|
symmetric_key * key)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
memcpy (ct, pt, 8);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
null_ecb_decrypt (const unsigned char *ct, unsigned char *pt,
|
2003-06-01 18:55:11 +00:00
|
|
|
symmetric_key * key)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
memcpy (pt, ct, 8);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
int
|
|
|
|
null_test (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
return CRYPT_OK;
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
int
|
|
|
|
null_keysize (int *desired_keysize)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
const struct _cipher_descriptor null_desc = {
|
|
|
|
"memcpy()",
|
|
|
|
255,
|
|
|
|
8, 8, 8, 1,
|
|
|
|
&null_setup,
|
|
|
|
&null_ecb_encrypt,
|
|
|
|
&null_ecb_decrypt,
|
|
|
|
&null_test,
|
|
|
|
&null_keysize
|
2003-03-03 00:59:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
prng_state prng;
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
store_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
L = 0x12345678UL;
|
|
|
|
STORE32H (L, &buf[0]);
|
|
|
|
L = 0;
|
|
|
|
LOAD32H (L, &buf[0]);
|
|
|
|
if (L != 0x12345678UL) {
|
2003-09-08 01:06:11 +00:00
|
|
|
printf ("LOAD/STORE32 High don't work, %08lx\n", L);
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
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: %4d to %4d, Block Size: %3d, Default # of rounds: %2d\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);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ecb_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
int x;
|
|
|
|
|
|
|
|
printf ("ECB tests\n");
|
|
|
|
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
|
|
|
|
printf (" %12s: ", cipher_descriptor[x].name);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = cipher_descriptor[x].test ()) != CRYPT_OK) {
|
|
|
|
printf (" **failed** Reason: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
} else {
|
|
|
|
printf ("passed\n");
|
|
|
|
}
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CBC
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
cbc_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
cbc_start (find_cipher ("blowfish"), IV, key, 16, 0,
|
2003-06-01 18:55:11 +00:00
|
|
|
&cbc)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("CBC Setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets encode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = cbc_encrypt (blk + 8 * x, ct + 8 * x, &cbc)) != CRYPT_OK) {
|
|
|
|
printf ("CBC encrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
cbc_start (find_cipher ("blowfish"), IV, key, 16, 0,
|
2003-06-01 18:55:11 +00:00
|
|
|
&cbc)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("CBC Setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets decode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = cbc_decrypt (ct + 8 * x, blk + 8 * x, &cbc)) != CRYPT_OK) {
|
|
|
|
printf ("CBC decrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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);
|
2003-06-01 18:55:11 +00:00
|
|
|
IV[0] = 0xFF; /* IV = FF 00 00 00 00 00 00 00 */
|
2003-03-03 01:03:50 +00:00
|
|
|
memset (blk, 0, 32);
|
2003-06-01 18:55:11 +00:00
|
|
|
blk[8] = 0xFF; /* BLK = 00 00 00 00 00 00 00 00 FF 00 00 00 00 00 00 00 */
|
2003-03-03 01:03:50 +00:00
|
|
|
cbc_start (find_cipher ("memcpy()"), IV, key, 8, 0, &cbc);
|
2003-06-01 18:55:11 +00:00
|
|
|
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 */
|
2003-03-03 01:03:50 +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");
|
|
|
|
exit (-1);
|
|
|
|
} else {
|
|
|
|
printf ("CBC passed logical testing.\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
cbc_tests (void)
|
|
|
|
{
|
|
|
|
printf ("CBC not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OFB
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ofb_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ofb_start (find_cipher ("cast5"), IV, key, 16, 0, &ofb)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("OFB Setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets encode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ofb_encrypt (blk + 8 * x, ct + 8 * x, 8, &ofb)) != CRYPT_OK) {
|
|
|
|
printf ("OFB encrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ofb_start (find_cipher ("cast5"), IV, key, 16, 0, &ofb)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("OFB setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets decode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ofb_decrypt (ct + 8 * x, blk + 8 * x, 8, &ofb)) != CRYPT_OK) {
|
|
|
|
printf ("OFB decrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* print output */
|
|
|
|
for (x = y = 0; x < 32; x++)
|
|
|
|
if (blk[x] != x)
|
|
|
|
y = 1;
|
|
|
|
printf (" %s\n", y ? "failed" : "passed");
|
|
|
|
if (y)
|
|
|
|
exit (-1);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ofb_tests (void)
|
|
|
|
{
|
|
|
|
printf ("OFB not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CFB
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
cfb_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
cfb_start (find_cipher ("blowfish"), IV, key, 16, 0,
|
2003-06-01 18:55:11 +00:00
|
|
|
&cfb)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("CFB setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets encode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = cfb_encrypt (blk + 8 * x, ct + 8 * x, 8, &cfb)) != CRYPT_OK) {
|
|
|
|
printf ("CFB encrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
cfb_start (find_cipher ("blowfish"), IV, key, 16, 0,
|
2003-06-01 18:55:11 +00:00
|
|
|
&cfb)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("CFB Setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets decode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = cfb_decrypt (ct + 8 * x, blk + 8 * x, 8, &cfb)) != CRYPT_OK) {
|
|
|
|
printf ("CFB decrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* print output */
|
|
|
|
for (x = y = 0; x < 32; x++)
|
|
|
|
if (blk[x] != x)
|
|
|
|
y = 1;
|
|
|
|
printf (" %s\n", y ? "failed" : "passed");
|
|
|
|
if (y)
|
|
|
|
exit (-1);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
cfb_tests (void)
|
|
|
|
{
|
|
|
|
printf ("CFB not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CTR
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ctr_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ctr_start (find_cipher ("xtea"), count, key, 16, 0,
|
2003-06-01 18:55:11 +00:00
|
|
|
&ctr)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("CTR Setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets encode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ctr_encrypt (blk + 8 * x, ct + 8 * x, 8, &ctr)) != CRYPT_OK) {
|
|
|
|
printf ("CTR encrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ctr_start (find_cipher ("xtea"), count, key, 16, 0,
|
2003-06-01 18:55:11 +00:00
|
|
|
&ctr)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("CTR Setup: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now lets decode 32 bytes */
|
|
|
|
for (x = 0; x < 4; x++) {
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ctr_decrypt (ct + 8 * x, blk + 8 * x, 8, &ctr)) != CRYPT_OK) {
|
|
|
|
printf ("CTR decrypt: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 01:02:10 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* print output */
|
|
|
|
for (x = y = 0; x < 32; x++)
|
|
|
|
if (blk[x] != x)
|
|
|
|
y = 1;
|
|
|
|
printf (" %s\n", y ? "failed" : "passed");
|
|
|
|
if (y)
|
|
|
|
exit (-1);
|
|
|
|
|
|
|
|
/* lets actually check the bytes */
|
|
|
|
memset (count, 0, 8);
|
2003-06-01 18:55:11 +00:00
|
|
|
count[0] = 0xFF; /* IV = FF 00 00 00 00 00 00 00 */
|
2003-03-03 01:03:50 +00:00
|
|
|
memset (blk, 0, 32);
|
2003-06-01 18:55:11 +00:00
|
|
|
blk[9] = 2; /* BLK = 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 */
|
2003-03-03 01:03:50 +00:00
|
|
|
ctr_start (find_cipher ("memcpy()"), count, key, 8, 0, &ctr);
|
2003-06-01 18:55:11 +00:00
|
|
|
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 */
|
2003-03-03 01:03:50 +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");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ctr_tests (void)
|
|
|
|
{
|
|
|
|
printf ("CTR not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
hash_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = hash_descriptor[x].test ()) != CRYPT_OK) {
|
|
|
|
printf ("**failed** Reason: %s\n", error_to_string (errnum));
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
2003-03-03 01:03:50 +00:00
|
|
|
printf ("passed\n");
|
2003-03-13 02:12:16 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MRSA
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
pad_test (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
rsa_pad (in, 16, out, &y, find_prng ("yarrow"), &prng)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* depad the message to get the original content */
|
|
|
|
memset (in, 0, sizeof (in));
|
|
|
|
x = 100;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_depad (out, y, in, &x)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check outcome */
|
|
|
|
printf ("rsa_pad: ");
|
|
|
|
if (x != 16) {
|
|
|
|
printf ("Failed. Wrong size.\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
for (x = 0; x < 16; x++)
|
|
|
|
if (in[x] != x) {
|
|
|
|
printf ("Failed. Expected %02lx and got %02x.\n", x, in[x]);
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("passed.\n");
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
rsa_test (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2004-02-20 20:03:32 +00:00
|
|
|
unsigned char in[520], out[520];
|
2003-03-03 01:03:50 +00:00
|
|
|
unsigned long x, y, z, limit;
|
|
|
|
int stat;
|
|
|
|
rsa_key key;
|
|
|
|
clock_t t;
|
|
|
|
|
|
|
|
/* ---- SINGLE ENCRYPT ---- */
|
|
|
|
/* encrypt a short 8 byte string */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
rsa_make_key (&prng, find_prng ("yarrow"), 1024 / 8, 65537,
|
2003-06-01 18:55:11 +00:00
|
|
|
&key)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
for (x = 0; x < 8; x++)
|
|
|
|
in[x] = (unsigned char) (x + 1);
|
|
|
|
y = sizeof (in);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_exptmod (in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decrypt it */
|
|
|
|
zeromem (in, sizeof (in));
|
|
|
|
x = sizeof (out);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_exptmod (out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
rsa_encrypt_key (in, 16, out, &y, &prng, find_prng ("yarrow"),
|
2003-06-01 18:55:11 +00:00
|
|
|
&key)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
zeromem (in, sizeof (in));
|
|
|
|
x = sizeof (in);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_decrypt_key (out, y, in, &x, &key)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
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);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_sign_hash (in, 16, out, &x, &key)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("RSA signed hash: %lu bytes\n", x);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("Verify hash: %s, ", stat ? "passed" : "failed");
|
|
|
|
in[0] ^= 1;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("%s\n", (!stat) ? "passed" : "failed");
|
|
|
|
if (stat)
|
|
|
|
exit (-1);
|
|
|
|
rsa_free (&key);
|
|
|
|
|
|
|
|
/* make a RSA key */
|
2003-03-03 01:02:10 +00:00
|
|
|
#ifdef SONY_PS2_NOPE
|
2003-03-03 01:03:50 +00:00
|
|
|
limit = 1024;
|
2003-03-03 00:59:24 +00:00
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
limit = 2048;
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
{
|
|
|
|
int tt;
|
|
|
|
|
|
|
|
for (z = 1024; z <= limit; z += 512) {
|
|
|
|
t = XCLOCK ();
|
|
|
|
for (tt = 0; tt < 3; tt++) {
|
2003-06-01 18:55:11 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (tt < 2)
|
|
|
|
rsa_free (&key);
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
t = XCLOCK () - t;
|
|
|
|
printf ("Took %.0f ms to make a %ld-bit RSA key.\n",
|
2003-06-01 18:55:11 +00:00
|
|
|
1000.0 * (((double) t / 3.0) / (double) XCLOCKS_PER_SEC), z);
|
2003-03-03 01:03:50 +00:00
|
|
|
|
|
|
|
/* time encryption */
|
|
|
|
t = XCLOCK ();
|
|
|
|
|
2003-06-01 18:55:11 +00:00
|
|
|
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);
|
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
t = XCLOCK () - t;
|
|
|
|
printf ("Took %.0f ms to encrypt with a %ld-bit RSA key.\n",
|
2003-06-01 18:55:11 +00:00
|
|
|
1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z);
|
2003-03-03 01:03:50 +00:00
|
|
|
|
|
|
|
/* time decryption */
|
|
|
|
t = XCLOCK ();
|
2003-06-01 18:55:11 +00:00
|
|
|
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);
|
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
t = XCLOCK () - t;
|
|
|
|
printf ("Took %.0f ms to decrypt with a %ld-bit RSA key.\n",
|
2003-06-01 18:55:11 +00:00
|
|
|
1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z);
|
2003-03-03 01:03:50 +00:00
|
|
|
rsa_free (&key);
|
|
|
|
}
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
pad_test (void)
|
|
|
|
{
|
|
|
|
printf ("MRSA not compiled in\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rsa_test (void)
|
|
|
|
{
|
|
|
|
printf ("MRSA not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BASE64
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
base64_test (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf (" error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
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) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf (" error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
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");
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
base64_test (void)
|
|
|
|
{
|
|
|
|
printf ("Base64 not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
time_hash (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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;
|
2003-03-13 02:12:16 +00:00
|
|
|
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);
|
2003-03-03 01:03:50 +00:00
|
|
|
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);
|
2003-03-13 02:12:16 +00:00
|
|
|
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;
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
t1 = XCLOCK () - t1;
|
|
|
|
printf ("%-20s: Hash at %5.2f Mbit/sec\n", hash_descriptor[x].name,
|
2003-06-01 18:55:11 +00:00
|
|
|
((8.0 * 4096.0) *
|
|
|
|
((double) y / ((double) t1 / (double) XCLOCKS_PER_SEC))) /
|
|
|
|
1000000.0);
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
time_ecb (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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 *);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
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,
|
2003-06-01 18:55:11 +00:00
|
|
|
&skey);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#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
|
2003-03-03 01:03:50 +00:00
|
|
|
#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) *
|
2003-06-01 18:55:11 +00:00
|
|
|
((double) y1 / ((double) t1 / (double) XCLOCKS_PER_SEC))) / 1000000.0,
|
2003-03-03 01:03:50 +00:00
|
|
|
((8.0 * (double) cipher_descriptor[x].block_length) *
|
2003-06-01 18:55:11 +00:00
|
|
|
((double) y2 / ((double) t2 / (double) XCLOCKS_PER_SEC))) /
|
2003-03-03 01:03:50 +00:00
|
|
|
1000000.0);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#undef DO256
|
|
|
|
#undef DO128
|
|
|
|
#undef DO64
|
|
|
|
#undef DO32
|
|
|
|
#undef DO16
|
|
|
|
#undef DO8
|
|
|
|
#undef DO4
|
|
|
|
#undef DO2
|
|
|
|
#undef DO1
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MDH
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
dh_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
unsigned char buf[3][4096];
|
|
|
|
unsigned long x, y, z;
|
|
|
|
int low, high, stat, stat2;
|
|
|
|
dh_key usera, userb;
|
|
|
|
clock_t t1;
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-09-08 01:06:11 +00:00
|
|
|
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");
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
dh_sizes (&low, &high);
|
|
|
|
printf ("DH Keys from %d to %d supported.\n", low * 8, high * 8);
|
|
|
|
|
|
|
|
/* make up two keys */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
dh_make_key (&prng, find_prng ("yarrow"), 96, &usera)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
dh_make_key (&prng, find_prng ("yarrow"), 96, &userb)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make the shared secret */
|
|
|
|
x = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = dh_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
y = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = dh_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
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;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = dh_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
dh_free (&userb);
|
|
|
|
|
|
|
|
/* import and make the shared secret again */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = dh_import (buf[1], y, &userb)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
z = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = dh_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
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);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
/* time stuff */
|
2003-03-03 01:03:50 +00:00
|
|
|
{
|
|
|
|
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 ();
|
2003-09-08 01:06:11 +00:00
|
|
|
for (tt = 0; tt < 25; tt++) {
|
2003-06-01 18:55:11 +00:00
|
|
|
dh_make_key (&prng, find_prng ("yarrow"), sizes[ii], &usera);
|
|
|
|
dh_free (&usera);
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
t1 = XCLOCK () - t1;
|
|
|
|
printf ("Make dh-%d key took %f msec\n", sizes[ii] * 8,
|
2003-09-08 01:06:11 +00:00
|
|
|
1000.0 * (((double) t1 / 25.0) / (double) XCLOCKS_PER_SEC));
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-03 00:59:24 +00:00
|
|
|
/* test encrypt_key */
|
2003-12-24 18:59:57 +00:00
|
|
|
dh_make_key (&prng, find_prng ("yarrow"), 128, &usera);
|
2003-03-03 01:03:50 +00:00
|
|
|
for (x = 0; x < 16; x++)
|
|
|
|
buf[0][x] = x;
|
|
|
|
y = sizeof (buf[1]);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
dh_encrypt_key (buf[0], 16, buf[1], &y, &prng, find_prng ("yarrow"),
|
2003-06-01 18:55:11 +00:00
|
|
|
find_hash ("md5"), &usera)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
zeromem (buf[0], sizeof (buf[0]));
|
|
|
|
x = sizeof (buf[0]);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = dh_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
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);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
/* test sign_hash */
|
2003-03-03 01:03:50 +00:00
|
|
|
for (x = 0; x < 16; x++)
|
|
|
|
buf[0][x] = x;
|
|
|
|
x = sizeof (buf[1]);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
dh_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"),
|
2003-06-01 18:55:11 +00:00
|
|
|
&usera)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
if (dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
buf[0][0] ^= 1;
|
|
|
|
if (dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("dh_sign/verify_hash: %s (%d,%d), %lu\n",
|
2003-06-01 18:55:11 +00:00
|
|
|
((stat == 1)
|
|
|
|
&& (stat2 == 0)) ? "passed" : "failed", stat, stat2, x);
|
2003-03-03 01:03:50 +00:00
|
|
|
dh_free (&usera);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
dh_tests (void)
|
|
|
|
{
|
|
|
|
printf ("MDH not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
int callback_x = 0;
|
|
|
|
void
|
|
|
|
callback (void)
|
|
|
|
{
|
|
|
|
printf ("%c\x08", "-\\|/"[++callback_x & 3]);
|
2003-03-03 00:59:24 +00:00
|
|
|
#ifndef SONY_PS2
|
2003-03-03 01:03:50 +00:00
|
|
|
fflush (stdout);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
rng_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
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...",
|
2003-06-01 18:55:11 +00:00
|
|
|
(double) x / ((double) t1 / (double) XCLOCKS_PER_SEC));
|
2003-03-03 01:03:50 +00:00
|
|
|
printf ("read %d bytes.\n ", x);
|
|
|
|
for (y = 0; y < x; y++)
|
|
|
|
printf ("%02x ", buf[y]);
|
|
|
|
printf ("\n");
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#ifdef YARROW
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
rng_make_prng (128, find_prng ("yarrow"), &prng,
|
2003-06-01 18:55:11 +00:00
|
|
|
&callback)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf (" starting yarrow error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MECC
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ecc_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
unsigned char buf[4][4096];
|
|
|
|
unsigned long x, y, z;
|
|
|
|
int stat, stat2, low, high;
|
|
|
|
ecc_key usera, userb;
|
|
|
|
clock_t t1;
|
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_test ()) != CRYPT_OK) {
|
|
|
|
printf ("ecc Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ecc_sizes (&low, &high);
|
|
|
|
printf ("ecc Keys from %d to %d supported.\n", low * 8, high * 8);
|
|
|
|
|
|
|
|
/* make up two keys */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ecc_make_key (&prng, find_prng ("yarrow"), 24, &usera)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ecc_make_key (&prng, find_prng ("yarrow"), 24, &userb)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make the shared secret */
|
|
|
|
x = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
y = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y != x) {
|
|
|
|
printf ("ecc Shared keys are not same size.\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp (buf[0], buf[1], x)) {
|
|
|
|
printf ("ecc Shared keys not same contents.\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now export userb */
|
|
|
|
y = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
ecc_free (&userb);
|
|
|
|
printf ("ECC-192 export took %ld bytes\n", y);
|
|
|
|
|
|
|
|
/* import and make the shared secret again */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_import (buf[1], y, &userb)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
z = 4096;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("ecc 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");
|
|
|
|
ecc_free (&usera);
|
|
|
|
ecc_free (&userb);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
/* time stuff */
|
2003-03-03 01:03:50 +00:00
|
|
|
{
|
|
|
|
static int sizes[] = { 20, 24, 28, 32, 48, 65 };
|
|
|
|
int ii, tt;
|
|
|
|
|
|
|
|
for (ii = 0; ii < (int) (sizeof (sizes) / sizeof (sizes[0])); ii++) {
|
|
|
|
t1 = XCLOCK ();
|
2003-06-01 18:55:11 +00:00
|
|
|
for (tt = 0; tt < 10; tt++) {
|
|
|
|
if ((errnum =
|
|
|
|
ecc_make_key (&prng, find_prng ("yarrow"), sizes[ii],
|
|
|
|
&usera)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
ecc_free (&usera);
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
t1 = XCLOCK () - t1;
|
|
|
|
printf ("Make ECC-%d key took %f msec\n", sizes[ii] * 8,
|
2003-06-01 18:55:11 +00:00
|
|
|
1000.0 * (((double) t1 / 10.0) / (double) XCLOCKS_PER_SEC));
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
/* test encrypt_key */
|
2003-03-29 18:19:07 +00:00
|
|
|
ecc_make_key (&prng, find_prng ("yarrow"), 20, &usera);
|
2003-03-13 02:12:16 +00:00
|
|
|
for (x = 0; x < 32; x++)
|
2003-03-03 01:03:50 +00:00
|
|
|
buf[0][x] = x;
|
|
|
|
y = sizeof (buf[1]);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
|
|
|
ecc_encrypt_key (buf[0], 32, buf[1], &y, &prng, find_prng ("yarrow"),
|
2003-06-01 18:55:11 +00:00
|
|
|
find_hash ("sha256"), &usera)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
zeromem (buf[0], sizeof (buf[0]));
|
|
|
|
x = sizeof (buf[0]);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = ecc_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) {
|
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("ECC en/de crypt key routines: ");
|
2003-03-13 02:12:16 +00:00
|
|
|
if (x != 32) {
|
2003-03-03 01:03:50 +00:00
|
|
|
printf ("Failed (length)\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-13 02:12:16 +00:00
|
|
|
for (x = 0; x < 32; x++)
|
2003-03-03 01:03:50 +00:00
|
|
|
if (buf[0][x] != x) {
|
|
|
|
printf ("Failed (contents)\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("Passed (size: %lu)\n", y);
|
2003-03-03 00:59:24 +00:00
|
|
|
/* test sign_hash */
|
2003-03-03 01:03:50 +00:00
|
|
|
for (x = 0; x < 16; x++)
|
|
|
|
buf[0][x] = x;
|
|
|
|
x = sizeof (buf[1]);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
ecc_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"),
|
2003-06-01 18:55:11 +00:00
|
|
|
&usera)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-29 18:19:07 +00:00
|
|
|
printf("Signature size: %lu\n", x);
|
2003-03-03 01:03:50 +00:00
|
|
|
if (ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
buf[0][0] ^= 1;
|
|
|
|
if (ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("ecc_sign/verify_hash: %s (%d,%d)\n",
|
2003-06-01 18:55:11 +00:00
|
|
|
((stat == 1) && (stat2 == 0)) ? "passed" : "failed", stat, stat2);
|
2003-03-03 01:03:50 +00:00
|
|
|
ecc_free (&usera);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
ecc_tests (void)
|
|
|
|
{
|
|
|
|
printf ("MECC not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef GF
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
gf_tests (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
gf_int a, b, c, d;
|
|
|
|
int n;
|
|
|
|
unsigned char buf[1024];
|
|
|
|
|
|
|
|
printf ("GF tests\n");
|
|
|
|
gf_zero (a);
|
|
|
|
gf_zero (b);
|
|
|
|
gf_zero (c);
|
|
|
|
gf_zero (d);
|
|
|
|
|
|
|
|
/* a == 0x18000000b */
|
|
|
|
a[1] = 1;
|
|
|
|
a[0] = 0x8000000bUL;
|
|
|
|
|
|
|
|
/* b == 0x012345678 */
|
|
|
|
b[0] = 0x012345678UL;
|
|
|
|
|
|
|
|
/* find 1/b mod a */
|
|
|
|
gf_invmod (b, a, c);
|
|
|
|
|
|
|
|
/* find 1/1/b mod a */
|
|
|
|
gf_invmod (c, a, d);
|
|
|
|
|
|
|
|
/* display them */
|
|
|
|
printf (" %08lx %08lx\n", c[0], d[0]);
|
|
|
|
|
|
|
|
/* store as binary string */
|
|
|
|
n = gf_size (a);
|
|
|
|
printf (" a takes %d bytes\n", n);
|
|
|
|
gf_toraw (a, buf);
|
|
|
|
gf_readraw (a, buf, n);
|
|
|
|
printf (" a == %08lx%08lx\n", a[1], a[0]);
|
|
|
|
|
|
|
|
/* primality testing */
|
|
|
|
gf_zero (a);
|
|
|
|
a[0] = 0x169;
|
|
|
|
printf (" GF prime: %s, ", gf_is_prime (a) ? "passed" : "failed");
|
|
|
|
a[0] = 0x168;
|
|
|
|
printf (" %s\n", gf_is_prime (a) ? "failed" : "passed");
|
|
|
|
|
|
|
|
/* test sqrt code */
|
|
|
|
gf_zero (a);
|
|
|
|
a[1] = 0x00000001;
|
|
|
|
a[0] = 0x8000000bUL;
|
|
|
|
gf_zero (b);
|
|
|
|
b[0] = 0x12345678UL;
|
|
|
|
|
|
|
|
gf_sqrt (b, a, c);
|
|
|
|
gf_mulmod (c, c, a, b);
|
|
|
|
printf (" (%08lx)^2 = %08lx (mod %08lx%08lx) \n", c[0], b[0], a[1], a[0]);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
gf_tests (void)
|
|
|
|
{
|
|
|
|
printf ("GF not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MPI
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
test_prime (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-07-11 02:09:41 +00:00
|
|
|
char buf[1024];
|
2003-03-03 01:03:50 +00:00
|
|
|
mp_int a;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
/* make a 1024 bit prime */
|
|
|
|
mp_init (&a);
|
|
|
|
rand_prime (&a, 128, &prng, find_prng ("yarrow"));
|
|
|
|
|
|
|
|
/* dump it */
|
|
|
|
mp_todecimal (&a, buf);
|
|
|
|
printf ("1024-bit prime:\n");
|
|
|
|
for (x = 0; x < (int) strlen (buf);) {
|
|
|
|
printf ("%c", buf[x]);
|
|
|
|
if (!(++x % 60))
|
|
|
|
printf ("\\ \n");
|
|
|
|
}
|
|
|
|
printf ("\n\n");
|
|
|
|
|
|
|
|
mp_clear (&a);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
test_prime (void)
|
|
|
|
{
|
|
|
|
printf ("MPI not compiled in\n");
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
register_all_algs (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-06-11 21:10:22 +00:00
|
|
|
#ifdef RIJNDAEL
|
|
|
|
register_cipher (&aes_desc);
|
|
|
|
#endif
|
2003-03-03 00:59:24 +00:00
|
|
|
#ifdef BLOWFISH
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&blowfish_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef XTEA
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&xtea_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RC5
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&rc5_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RC6
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&rc6_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SAFERP
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&saferp_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
2003-03-03 01:01:00 +00:00
|
|
|
#ifdef TWOFISH
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&twofish_desc);
|
2003-03-03 01:01:00 +00:00
|
|
|
#endif
|
2003-03-03 00:59:24 +00:00
|
|
|
#ifdef SAFER
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&safer_k64_desc);
|
|
|
|
register_cipher (&safer_sk64_desc);
|
|
|
|
register_cipher (&safer_k128_desc);
|
|
|
|
register_cipher (&safer_sk128_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RC2
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&rc2_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef DES
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&des_desc);
|
|
|
|
register_cipher (&des3_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
2003-03-03 01:01:00 +00:00
|
|
|
#ifdef CAST5
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&cast5_desc);
|
2003-03-03 01:01:00 +00:00
|
|
|
#endif
|
2003-03-03 01:01:40 +00:00
|
|
|
#ifdef NOEKEON
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&noekeon_desc);
|
2003-03-03 01:01:40 +00:00
|
|
|
#endif
|
2003-12-24 18:59:57 +00:00
|
|
|
#ifdef SKIPJACK
|
|
|
|
register_cipher (&skipjack_desc);
|
|
|
|
#endif
|
2003-03-03 01:03:50 +00:00
|
|
|
register_cipher (&null_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#ifdef TIGER
|
2003-03-03 01:03:50 +00:00
|
|
|
register_hash (&tiger_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
2003-06-15 22:37:45 +00:00
|
|
|
#ifdef MD2
|
|
|
|
register_hash (&md2_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef MD4
|
|
|
|
register_hash (&md4_desc);
|
|
|
|
#endif
|
2003-03-03 00:59:24 +00:00
|
|
|
#ifdef MD5
|
2003-03-03 01:03:50 +00:00
|
|
|
register_hash (&md5_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
2003-06-15 22:37:45 +00:00
|
|
|
#ifdef SHA1
|
|
|
|
register_hash (&sha1_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef SHA256
|
|
|
|
register_hash (&sha256_desc);
|
|
|
|
#endif
|
2003-12-24 18:59:57 +00:00
|
|
|
#ifdef SHA224
|
|
|
|
register_hash (&sha224_desc);
|
|
|
|
#endif
|
2003-03-03 00:59:24 +00:00
|
|
|
#ifdef SHA384
|
2003-03-03 01:03:50 +00:00
|
|
|
register_hash (&sha384_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SHA512
|
2003-03-03 01:03:50 +00:00
|
|
|
register_hash (&sha512_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
2003-09-08 01:06:11 +00:00
|
|
|
#ifdef RIPEMD128
|
|
|
|
register_hash (&rmd128_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef RIPEMD160
|
|
|
|
register_hash (&rmd160_desc);
|
|
|
|
#endif
|
2004-02-20 20:03:32 +00:00
|
|
|
#ifdef WHIRLPOOL
|
|
|
|
register_hash (&whirlpool_desc);
|
|
|
|
#endif
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#ifdef YARROW
|
2003-03-03 01:03:50 +00:00
|
|
|
register_prng (&yarrow_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SPRNG
|
2003-03-03 01:03:50 +00:00
|
|
|
register_prng (&sprng_desc);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-06-11 21:10:22 +00:00
|
|
|
#ifdef KR
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
kr_display (pk_key * kr)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-29 18:19:07 +00:00
|
|
|
static const char *sys[] = { "NON-KEY", "RSA", "DH", "ECC" };
|
2003-03-03 01:03:50 +00:00
|
|
|
static const char *type[] = { "PRIVATE", "PUBLIC", "PRIVATE_OPTIMIZED" };
|
|
|
|
|
|
|
|
while (kr->system != NON_KEY) {
|
|
|
|
printf ("CRC [%08lx], System [%10s], Type [%20s], %s, %s, %s\n", kr->ID,
|
2003-06-01 18:55:11 +00:00
|
|
|
sys[kr->system], type[kr->key_type], kr->name, kr->email,
|
|
|
|
kr->description);
|
2003-03-03 01:03:50 +00:00
|
|
|
kr = kr->next;
|
|
|
|
}
|
|
|
|
printf ("\n");
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
kr_test_makekeys (pk_key ** kr)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_init (kr)) != CRYPT_OK) {
|
|
|
|
printf ("KR init error %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make a DH key */
|
|
|
|
printf ("KR: Making DH key...\n");
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_make_key (*kr, &prng, find_prng ("yarrow"), DH_KEY, 128, "dhkey",
|
2003-06-01 18:55:11 +00:00
|
|
|
"dh@dh.dh", "dhkey one")) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Make key error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make a ECC key */
|
|
|
|
printf ("KR: Making ECC key...\n");
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_make_key (*kr, &prng, find_prng ("yarrow"), ECC_KEY, 20, "ecckey",
|
2003-06-01 18:55:11 +00:00
|
|
|
"ecc@ecc.ecc", "ecckey one")) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Make key error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make a RSA key */
|
|
|
|
printf ("KR: Making RSA key...\n");
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_make_key (*kr, &prng, find_prng ("yarrow"), RSA_KEY, 128, "rsakey",
|
2003-06-01 18:55:11 +00:00
|
|
|
"rsa@rsa.rsa", "rsakey one")) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Make key error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
void
|
|
|
|
kr_test (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
pk_key *kr, *_kr;
|
|
|
|
unsigned char buf[8192], buf2[8192], buf3[8192];
|
|
|
|
unsigned long len;
|
|
|
|
int i, j, stat;
|
|
|
|
#ifndef NO_FILE
|
|
|
|
FILE *f;
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_test_makekeys (&kr);
|
|
|
|
|
|
|
|
printf ("The original list:\n");
|
|
|
|
kr_display (kr);
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
len = sizeof (buf);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_export (kr, kr->ID, kr->key_type, buf, &len)) != CRYPT_OK) {
|
|
|
|
printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("Exported key was: %lu bytes\n", len);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
|
|
|
|
printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_display (kr);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
|
|
|
|
printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_display (kr);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
len = sizeof (buf);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_export (kr, kr->ID, PK_PUBLIC, buf, &len)) != CRYPT_OK) {
|
|
|
|
printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("Exported key was: %lu bytes\n", len);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
|
|
|
|
printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_display (kr);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
|
|
|
|
printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_display (kr);
|
|
|
|
}
|
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
|
|
|
|
printf ("Error clearing ring: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* TEST output to file */
|
|
|
|
#ifndef NO_FILE
|
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_init (&kr)) != CRYPT_OK) {
|
|
|
|
printf ("KR init error %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_test_makekeys (&kr);
|
|
|
|
|
|
|
|
/* save to file */
|
|
|
|
f = fopen ("ring.dat", "wb");
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_save (kr, f, NULL)) != CRYPT_OK) {
|
|
|
|
printf ("kr_save error %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
fclose (f);
|
|
|
|
|
|
|
|
/* delete and load */
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
|
|
|
|
printf ("clear error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen ("ring.dat", "rb");
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_load (&kr, f, NULL)) != CRYPT_OK) {
|
|
|
|
printf ("kr_load error %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
fclose (f);
|
|
|
|
remove ("ring.dat");
|
|
|
|
printf ("After load and save...\n");
|
|
|
|
kr_display (kr);
|
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
|
|
|
|
printf ("clear error: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* test the packet encryption/sign stuff */
|
2003-03-03 01:03:50 +00:00
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
buf[i] = i;
|
|
|
|
kr_test_makekeys (&kr);
|
|
|
|
_kr = kr;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
printf ("Testing a key with system %d, type %d:\t", _kr->system,
|
2003-06-01 18:55:11 +00:00
|
|
|
_kr->key_type);
|
2003-03-03 01:03:50 +00:00
|
|
|
len = sizeof (buf2);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-06-01 18:55:11 +00:00
|
|
|
kr_encrypt_key (kr, _kr->ID, buf, 16, buf2, &len, &prng,
|
|
|
|
find_prng ("yarrow"),
|
|
|
|
find_hash ("md5"))) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
len = sizeof (buf3);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_decrypt_key (kr, buf2, buf3, &len)) != CRYPT_OK) {
|
|
|
|
printf ("decrypt error, %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
if (len != 16 || memcmp (buf3, buf, 16)) {
|
|
|
|
printf ("kr_decrypt_key failed, %i, %lu\n", i, len);
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("kr_encrypt_key passed, ");
|
|
|
|
|
|
|
|
len = sizeof (buf2);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-06-01 18:55:11 +00:00
|
|
|
kr_sign_hash (kr, _kr->ID, buf, 32, buf2, &len, &prng,
|
|
|
|
find_prng ("yarrow"))) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("kr_sign_hash: ");
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_verify_hash (kr, buf2, buf, 32, &stat)) != CRYPT_OK) {
|
|
|
|
printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|
2003-03-03 01:03:50 +00:00
|
|
|
printf ("%s, ", stat ? "passed" : "failed");
|
|
|
|
buf[15] ^= 1;
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_verify_hash (kr, buf2, buf, 32, &stat)) != CRYPT_OK) {
|
|
|
|
printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("%s\n", (!stat) ? "passed" : "failed");
|
|
|
|
buf[15] ^= 1;
|
|
|
|
|
|
|
|
len = sizeof (buf);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-06-01 18:55:11 +00:00
|
|
|
kr_fingerprint (kr, _kr->ID, find_hash ("sha1"), buf,
|
|
|
|
&len)) != CRYPT_OK) {
|
2003-03-03 01:03:50 +00:00
|
|
|
printf ("kr_fingerprint failed, %i, %lu\n", i, len);
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
printf ("Fingerprint: ");
|
|
|
|
for (j = 0; j < 20; j++) {
|
|
|
|
printf ("%02x", buf[j]);
|
|
|
|
if (j < 19)
|
2003-06-01 18:55:11 +00:00
|
|
|
printf (":");
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
printf ("\n\n");
|
|
|
|
|
|
|
|
_kr = _kr->next;
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
/* Test encrypting/decrypting to a public key */
|
|
|
|
/* first dump the other two keys */
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_del (&kr, kr->ID);
|
|
|
|
kr_del (&kr, kr->ID);
|
|
|
|
kr_display (kr);
|
|
|
|
|
|
|
|
/* now export it as public and private */
|
|
|
|
len = sizeof (buf);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_export (kr, kr->ID, PK_PUBLIC, buf, &len)) != CRYPT_OK) {
|
|
|
|
printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check boundaries */
|
|
|
|
memset (buf + len, 0, sizeof (buf) - len);
|
|
|
|
|
|
|
|
len = sizeof (buf2);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_export (kr, kr->ID, PK_PRIVATE, buf2, &len)) != CRYPT_OK) {
|
|
|
|
printf ("Error exporting key %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check boundaries */
|
|
|
|
memset (buf2 + len, 0, sizeof (buf2) - len);
|
|
|
|
|
|
|
|
/* delete the key and import the public */
|
|
|
|
kr_clear (&kr);
|
|
|
|
kr_init (&kr);
|
|
|
|
kr_display (kr);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
|
|
|
|
printf ("Error importing key %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_display (kr);
|
|
|
|
|
|
|
|
/* now encrypt a buffer */
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
buf[i] = i;
|
|
|
|
len = sizeof (buf3);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum =
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_encrypt_key (kr, kr->ID, buf, 16, buf3, &len, &prng,
|
2003-06-01 18:55:11 +00:00
|
|
|
find_prng ("yarrow"),
|
|
|
|
find_hash ("md5"))) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now delete the key and import the private one */
|
|
|
|
kr_clear (&kr);
|
|
|
|
kr_init (&kr);
|
|
|
|
kr_display (kr);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_import (kr, buf2, len)) != CRYPT_OK) {
|
|
|
|
printf ("Error importing key %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
kr_display (kr);
|
|
|
|
|
|
|
|
/* now decrypt */
|
|
|
|
len = sizeof (buf2);
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = kr_decrypt_key (kr, buf3, buf2, &len)) != CRYPT_OK) {
|
|
|
|
printf ("decrypt error, %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("KR encrypt to public, decrypt with private: ");
|
|
|
|
if (len == 16 && !memcmp (buf2, buf, 16)) {
|
|
|
|
printf ("passed\n");
|
|
|
|
} else {
|
|
|
|
printf ("failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
kr_clear (&kr);
|
|
|
|
}
|
2003-06-11 21:10:22 +00:00
|
|
|
#endif
|
2003-03-03 01:03:50 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
test_errs (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
2003-03-03 01:03:50 +00:00
|
|
|
#define ERR(x) printf("%25s => %s\n", #x, error_to_string(x));
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_OK);
|
|
|
|
ERR (CRYPT_ERROR);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_INVALID_KEYSIZE);
|
|
|
|
ERR (CRYPT_INVALID_ROUNDS);
|
|
|
|
ERR (CRYPT_FAIL_TESTVECTOR);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_BUFFER_OVERFLOW);
|
|
|
|
ERR (CRYPT_INVALID_PACKET);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_INVALID_PRNGSIZE);
|
|
|
|
ERR (CRYPT_ERROR_READPRNG);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_INVALID_CIPHER);
|
|
|
|
ERR (CRYPT_INVALID_HASH);
|
|
|
|
ERR (CRYPT_INVALID_PRNG);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_MEM);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_PK_TYPE_MISMATCH);
|
|
|
|
ERR (CRYPT_PK_NOT_PRIVATE);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_INVALID_ARG);
|
2003-09-26 01:16:18 +00:00
|
|
|
ERR (CRYPT_FILE_NOTFOUND);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_PK_INVALID_TYPE);
|
|
|
|
ERR (CRYPT_PK_INVALID_SYSTEM);
|
|
|
|
ERR (CRYPT_PK_DUP);
|
|
|
|
ERR (CRYPT_PK_NOT_FOUND);
|
|
|
|
ERR (CRYPT_PK_INVALID_SIZE);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ERR (CRYPT_INVALID_PRIME_SIZE);
|
|
|
|
}
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
|
2003-12-24 18:59:57 +00:00
|
|
|
void dsa_tests(void)
|
|
|
|
{
|
|
|
|
unsigned char msg[16], out[1024], out2[1024];
|
|
|
|
unsigned long x, y;
|
|
|
|
int err, stat1, stat2;
|
|
|
|
dsa_key key, key2;
|
|
|
|
|
|
|
|
/* make a random key */
|
|
|
|
if ((err = dsa_make_key(&prng, find_prng("yarrow"), 20, 128, &key)) != CRYPT_OK) {
|
|
|
|
printf("Error making DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("DSA Key Made\n");
|
|
|
|
|
|
|
|
/* verify it */
|
|
|
|
if ((err = dsa_verify_key(&key, &stat1)) != CRYPT_OK) {
|
|
|
|
printf("Error verifying DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("DSA key verification: %s\n", stat1 == 1 ? "passed" : "failed");
|
|
|
|
if (stat1 == 0) exit(-1);
|
|
|
|
|
|
|
|
/* sign the message */
|
|
|
|
x = sizeof(out);
|
|
|
|
if ((err = dsa_sign_hash(msg, sizeof(msg), out, &x, &prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
|
|
|
|
printf("Error signing with DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("DSA 160/1024 signature is %lu bytes long\n", x);
|
|
|
|
|
|
|
|
/* verify it once */
|
|
|
|
if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key)) != CRYPT_OK) {
|
|
|
|
printf("Error verifying with DSA key 1: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modify and verify again */
|
|
|
|
msg[0] ^= 1;
|
|
|
|
if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key)) != CRYPT_OK) {
|
|
|
|
printf("Error verifying with DSA key 2: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
msg[0] ^= 1;
|
|
|
|
printf("DSA Verification: %d, %d, %s\n", stat1, stat2, (stat1 == 1 && stat2 == 0) ? "passed" : "failed");
|
|
|
|
if (!(stat1 == 1 && stat2 == 0)) exit(-1);
|
|
|
|
|
|
|
|
/* test exporting it */
|
|
|
|
x = sizeof(out2);
|
|
|
|
if ((err = dsa_export(out2, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
|
|
|
|
printf("Error export PK_PRIVATE DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("Exported PK_PRIVATE DSA key in %lu bytes\n", x);
|
|
|
|
if ((err = dsa_import(out2, x, &key2)) != CRYPT_OK) {
|
|
|
|
printf("Error importing PK_PRIVATE DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
/* verify a signature with it */
|
|
|
|
if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2)) != CRYPT_OK) {
|
|
|
|
printf("Error verifying with DSA key 3: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("PRIVATE Import Test: %s\n", stat1 == 1 ? "passed" : "failed");
|
|
|
|
if (stat1 == 0) exit(-1);
|
|
|
|
dsa_free(&key2);
|
|
|
|
|
|
|
|
/* export as public now */
|
|
|
|
x = sizeof(out2);
|
|
|
|
if ((err = dsa_export(out2, &x, PK_PUBLIC, &key)) != CRYPT_OK) {
|
|
|
|
printf("Error export PK_PUBLIC DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("Exported PK_PUBLIC DSA key in %lu bytes\n", x);
|
|
|
|
if ((err = dsa_import(out2, x, &key2)) != CRYPT_OK) {
|
|
|
|
printf("Error importing PK_PUBLIC DSA key: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
/* verify a signature with it */
|
|
|
|
if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2)) != CRYPT_OK) {
|
|
|
|
printf("Error verifying with DSA key 4: %s\n", error_to_string(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
printf("PUBLIC Import Test: %s\n", stat1 == 1 ? "passed" : "failed");
|
|
|
|
if (stat1 == 0) exit(-1);
|
|
|
|
|
|
|
|
dsa_free(&key2);
|
|
|
|
dsa_free(&key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
int
|
|
|
|
main (void)
|
2003-03-03 00:59:24 +00:00
|
|
|
{
|
|
|
|
#ifdef SONY_PS2
|
2003-03-03 01:03:50 +00:00
|
|
|
TIMER_Init ();
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
register_all_algs ();
|
2003-06-15 22:37:45 +00:00
|
|
|
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = yarrow_start (&prng)) != CRYPT_OK) {
|
|
|
|
printf ("yarrow_start: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
2003-09-08 01:06:11 +00:00
|
|
|
if ((errnum = yarrow_add_entropy ((unsigned char *)"hello", 5, &prng)) != CRYPT_OK) {
|
2003-03-13 02:12:16 +00:00
|
|
|
printf ("yarrow_add_entropy: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
2003-03-13 02:12:16 +00:00
|
|
|
if ((errnum = yarrow_ready (&prng)) != CRYPT_OK) {
|
|
|
|
printf ("yarrow_ready: %s\n", error_to_string (errnum));
|
2003-03-03 01:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf (crypt_build_settings);
|
|
|
|
test_errs ();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-12-24 18:59:57 +00:00
|
|
|
|
2003-03-03 00:59:24 +00:00
|
|
|
#ifdef HMAC
|
2003-03-03 01:03:50 +00:00
|
|
|
printf ("HMAC: %s\n", hmac_test () == CRYPT_OK ? "passed" : "failed");
|
2003-12-24 18:59:57 +00:00
|
|
|
if (hmac_test() != CRYPT_OK) exit(EXIT_FAILURE);
|
|
|
|
#endif
|
|
|
|
|
2004-01-25 17:40:34 +00:00
|
|
|
#ifdef OMAC
|
2003-12-24 18:59:57 +00:00
|
|
|
printf ("OMAC: %s\n", omac_test () == CRYPT_OK ? "passed" : "failed");
|
|
|
|
if (omac_test() != CRYPT_OK) exit(EXIT_FAILURE);
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2004-02-20 20:03:32 +00:00
|
|
|
#ifdef PMAC
|
|
|
|
printf ("PMAC: %s\n", pmac_test () == CRYPT_OK ? "passed" : "failed");
|
|
|
|
if (pmac_test() != CRYPT_OK) exit(EXIT_FAILURE);
|
|
|
|
#endif
|
|
|
|
|
2004-01-25 17:40:34 +00:00
|
|
|
#ifdef EAX_MODE
|
|
|
|
printf ("EAX : %s\n", eax_test () == CRYPT_OK ? "passed" : "failed");
|
|
|
|
if (eax_test() != CRYPT_OK) exit(EXIT_FAILURE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OCB_MODE
|
|
|
|
printf ("OCB : %s\n", ocb_test () == CRYPT_OK ? "passed" : "failed");
|
|
|
|
if (ocb_test() != CRYPT_OK) exit(EXIT_FAILURE);
|
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
store_tests ();
|
|
|
|
cipher_tests ();
|
|
|
|
hash_tests ();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
ecb_tests ();
|
|
|
|
cbc_tests ();
|
|
|
|
ctr_tests ();
|
|
|
|
ofb_tests ();
|
|
|
|
cfb_tests ();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
rng_tests ();
|
2003-06-15 22:37:45 +00:00
|
|
|
test_prime();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-06-11 21:10:22 +00:00
|
|
|
#ifdef KR
|
2003-03-03 01:03:50 +00:00
|
|
|
kr_test ();
|
2003-09-08 01:06:11 +00:00
|
|
|
#endif
|
2003-12-24 18:59:57 +00:00
|
|
|
dsa_tests();
|
2003-03-03 01:03:50 +00:00
|
|
|
rsa_test ();
|
|
|
|
pad_test ();
|
|
|
|
ecc_tests ();
|
|
|
|
dh_tests ();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-12-24 18:59:57 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
gf_tests ();
|
|
|
|
base64_test ();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
time_ecb ();
|
|
|
|
time_hash ();
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#ifdef SONY_PS2
|
2003-03-03 01:03:50 +00:00
|
|
|
TIMER_Shutdown ();
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
return 0;
|
2003-03-03 00:59:24 +00:00
|
|
|
}
|