0.96 release of LibTomCrypt

--HG--
branch : libtomcrypt-orig
extra : convert_revision : a1c3f430d3257b3ce499c24261ad1366b200f2ce
This commit is contained in:
Matt Johnston
2004-06-15 14:07:21 +00:00
parent c9bcca21e4
commit b8237ec416
91 changed files with 3451 additions and 3229 deletions

View File

@@ -4,7 +4,7 @@
int main(void)
{
register_cipher(&rijndael_desc);
register_cipher(&rijndael_enc_desc);
register_prng(&yarrow_desc);
register_hash(&sha256_desc);
return 0;

View File

@@ -1202,69 +1202,6 @@ ecc_tests (void)
}
#endif
#ifdef GF
void
gf_tests (void)
{
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]);
}
#else
void
gf_tests (void)
{
printf ("GF not compiled in\n");
}
#endif
#ifdef MPI
void
test_prime (void)
@@ -1390,299 +1327,6 @@ register_all_algs (void)
#endif
}
#ifdef KR
void
kr_display (pk_key * kr)
{
static const char *sys[] = { "NON-KEY", "RSA", "DH", "ECC" };
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,
sys[kr->system], type[kr->key_type], kr->name, kr->email,
kr->description);
kr = kr->next;
}
printf ("\n");
}
void
kr_test_makekeys (pk_key ** kr)
{
if ((errnum = kr_init (kr)) != CRYPT_OK) {
printf ("KR init error %s\n", error_to_string (errnum));
exit (-1);
}
/* make a DH key */
printf ("KR: Making DH key...\n");
if ((errnum =
kr_make_key (*kr, &prng, find_prng ("yarrow"), DH_KEY, 128, "dhkey",
"dh@dh.dh", "dhkey one")) != CRYPT_OK) {
printf ("Make key error: %s\n", error_to_string (errnum));
exit (-1);
}
/* make a ECC key */
printf ("KR: Making ECC key...\n");
if ((errnum =
kr_make_key (*kr, &prng, find_prng ("yarrow"), ECC_KEY, 20, "ecckey",
"ecc@ecc.ecc", "ecckey one")) != CRYPT_OK) {
printf ("Make key error: %s\n", error_to_string (errnum));
exit (-1);
}
/* make a RSA key */
printf ("KR: Making RSA key...\n");
if ((errnum =
kr_make_key (*kr, &prng, find_prng ("yarrow"), RSA_KEY, 128, "rsakey",
"rsa@rsa.rsa", "rsakey one")) != CRYPT_OK) {
printf ("Make key error: %s\n", error_to_string (errnum));
exit (-1);
}
}
void
kr_test (void)
{
pk_key *kr, *_kr;
unsigned char buf[8192], buf2[8192], buf3[8192];
unsigned long len;
int i, j, stat;
#ifndef NO_FILE
FILE *f;
#endif
kr_test_makekeys (&kr);
printf ("The original list:\n");
kr_display (kr);
for (i = 0; i < 3; i++) {
len = sizeof (buf);
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));
exit (-1);
}
printf ("Exported key was: %lu bytes\n", len);
if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
exit (-1);
}
kr_display (kr);
if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
exit (-1);
}
kr_display (kr);
}
for (i = 0; i < 3; i++) {
len = sizeof (buf);
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));
exit (-1);
}
printf ("Exported key was: %lu bytes\n", len);
if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
exit (-1);
}
kr_display (kr);
if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
exit (-1);
}
kr_display (kr);
}
if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
printf ("Error clearing ring: %s\n", error_to_string (errnum));
exit (-1);
}
/* TEST output to file */
#ifndef NO_FILE
if ((errnum = kr_init (&kr)) != CRYPT_OK) {
printf ("KR init error %s\n", error_to_string (errnum));
exit (-1);
}
kr_test_makekeys (&kr);
/* save to file */
f = fopen ("ring.dat", "wb");
if ((errnum = kr_save (kr, f, NULL)) != CRYPT_OK) {
printf ("kr_save error %s\n", error_to_string (errnum));
exit (-1);
}
fclose (f);
/* delete and load */
if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
printf ("clear error: %s\n", error_to_string (errnum));
exit (-1);
}
f = fopen ("ring.dat", "rb");
if ((errnum = kr_load (&kr, f, NULL)) != CRYPT_OK) {
printf ("kr_load error %s\n", error_to_string (errnum));
exit (-1);
}
fclose (f);
remove ("ring.dat");
printf ("After load and save...\n");
kr_display (kr);
if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
printf ("clear error: %s\n", error_to_string (errnum));
exit (-1);
}
#endif
/* test the packet encryption/sign stuff */
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,
_kr->key_type);
len = sizeof (buf2);
if ((errnum =
kr_encrypt_key (kr, _kr->ID, buf, 16, buf2, &len, &prng,
find_prng ("yarrow"),
find_hash ("md5"))) != CRYPT_OK) {
printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
exit (-1);
}
len = sizeof (buf3);
if ((errnum = kr_decrypt_key (kr, buf2, buf3, &len)) != CRYPT_OK) {
printf ("decrypt error, %d, %s\n", i, error_to_string (errnum));
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);
if ((errnum =
kr_sign_hash (kr, _kr->ID, buf, 32, buf2, &len, &prng,
find_prng ("yarrow"))) != CRYPT_OK) {
printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
exit (-1);
}
printf ("kr_sign_hash: ");
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));
exit (-1);
}
printf ("%s, ", stat ? "passed" : "failed");
buf[15] ^= 1;
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));
exit (-1);
}
printf ("%s\n", (!stat) ? "passed" : "failed");
buf[15] ^= 1;
len = sizeof (buf);
if ((errnum =
kr_fingerprint (kr, _kr->ID, find_hash ("sha1"), buf,
&len)) != CRYPT_OK) {
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)
printf (":");
}
printf ("\n\n");
_kr = _kr->next;
}
/* Test encrypting/decrypting to a public key */
/* first dump the other two keys */
kr_del (&kr, kr->ID);
kr_del (&kr, kr->ID);
kr_display (kr);
/* now export it as public and private */
len = sizeof (buf);
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));
exit (-1);
}
/* check boundaries */
memset (buf + len, 0, sizeof (buf) - len);
len = sizeof (buf2);
if ((errnum = kr_export (kr, kr->ID, PK_PRIVATE, buf2, &len)) != CRYPT_OK) {
printf ("Error exporting key %s\n", error_to_string (errnum));
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);
if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
printf ("Error importing key %s\n", error_to_string (errnum));
exit (-1);
}
kr_display (kr);
/* now encrypt a buffer */
for (i = 0; i < 16; i++)
buf[i] = i;
len = sizeof (buf3);
if ((errnum =
kr_encrypt_key (kr, kr->ID, buf, 16, buf3, &len, &prng,
find_prng ("yarrow"),
find_hash ("md5"))) != CRYPT_OK) {
printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
exit (-1);
}
/* now delete the key and import the private one */
kr_clear (&kr);
kr_init (&kr);
kr_display (kr);
if ((errnum = kr_import (kr, buf2, len)) != CRYPT_OK) {
printf ("Error importing key %s\n", error_to_string (errnum));
exit (-1);
}
kr_display (kr);
/* now decrypt */
len = sizeof (buf2);
if ((errnum = kr_decrypt_key (kr, buf3, buf2, &len)) != CRYPT_OK) {
printf ("decrypt error, %s\n", error_to_string (errnum));
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);
}
#endif
void
test_errs (void)
{
@@ -1840,13 +1484,13 @@ void pkcs1_test(void)
/* decode it */
l2 = sizeof(buf[2]);
if ((err = pkcs_1_oaep_decode(buf[1], l1, NULL, 0, 1024, hash_idx, buf[2], &l2)) != CRYPT_OK) {
if ((err = pkcs_1_oaep_decode(buf[1], l1, NULL, 0, 1024, hash_idx, buf[2], &l2, &res1)) != CRYPT_OK) {
printf("OAEP decode: %s\n", error_to_string(err));
exit(-1);
}
if (l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
printf("Outsize == %lu, should have been %lu, msg contents follow.\n", l2, l3);
if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
printf("res == %d, Outsize == %lu, should have been %lu, msg contents follow.\n", res1, l2, l3);
printf("ORIGINAL:\n");
for (x = 0; x < l3; x++) {
printf("%02x ", buf[0][x]);
@@ -1959,16 +1603,12 @@ main (void)
rng_tests ();
test_prime();
#ifdef KR
kr_test ();
#endif
dsa_tests();
rsa_test ();
pad_test ();
ecc_tests ();
dh_tests ();
gf_tests ();
base64_test ();
time_ecb ();

20
demos/test/base64_test.c Normal file
View File

@@ -0,0 +1,20 @@
#include "test.h"
int base64_test(void)
{
unsigned char in[64], out[256], tmp[64];
unsigned long x, l1, l2;
for (x = 0; x < 64; x++) {
yarrow_read(in, x, &test_yarrow);
l1 = sizeof(out);
DO(base64_encode(in, x, out, &l1));
l2 = sizeof(tmp);
DO(base64_decode(out, l1, tmp, &l2));
if (l2 != x || memcmp(tmp, in, x)) {
printf("base64 failed %lu %lu %lu", x, l1, l2);
return 1;
}
}
return 0;
}

View File

@@ -0,0 +1,20 @@
/* test the ciphers and hashes using their built-in self-tests */
#include "test.h"
int cipher_hash_test(void)
{
int x;
/* test ciphers */
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
DO(cipher_descriptor[x].test());
}
/* test hashes */
for (x = 0; hash_descriptor[x].name != NULL; x++) {
DO(hash_descriptor[x].test());
}
return 0;
}

87
demos/test/dh_tests.c Normal file
View File

@@ -0,0 +1,87 @@
#include "test.h"
int dh_tests (void)
{
unsigned char buf[3][4096];
unsigned long x, y, z;
int stat, stat2;
dh_key usera, userb;
DO(dh_test());
/* make up two keys */
DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &usera));
DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &userb));
/* make the shared secret */
x = 4096;
DO(dh_shared_secret (&usera, &userb, buf[0], &x));
y = 4096;
DO(dh_shared_secret (&userb, &usera, buf[1], &y));
if (y != x) {
printf ("DH Shared keys are not same size.\n");
return 1;
}
if (memcmp (buf[0], buf[1], x)) {
printf ("DH Shared keys not same contents.\n");
return 1;
}
/* now export userb */
y = 4096;
DO(dh_export (buf[1], &y, PK_PUBLIC, &userb));
dh_free (&userb);
/* import and make the shared secret again */
DO(dh_import (buf[1], y, &userb));
z = 4096;
DO(dh_shared_secret (&usera, &userb, buf[2], &z));
if (z != x) {
printf ("failed. Size don't match?\n");
return 1;
}
if (memcmp (buf[0], buf[2], x)) {
printf ("Failed. Content didn't match.\n");
return 1;
}
dh_free (&usera);
dh_free (&userb);
/* test encrypt_key */
dh_make_key (&test_yarrow, find_prng ("yarrow"), 128, &usera);
for (x = 0; x < 16; x++) {
buf[0][x] = x;
}
y = sizeof (buf[1]);
DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("md5"), &usera));
zeromem (buf[0], sizeof (buf[0]));
x = sizeof (buf[0]);
DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera));
if (x != 16) {
printf ("Failed (length)\n");
return 1;
}
for (x = 0; x < 16; x++)
if (buf[0][x] != x) {
printf ("Failed (contents)\n");
return 1;
}
/* test sign_hash */
for (x = 0; x < 16; x++) {
buf[0][x] = x;
}
x = sizeof (buf[1]);
DO(dh_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow , find_prng ("yarrow"), &usera));
DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
buf[0][0] ^= 1;
DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
if (!(stat == 1 && stat2 == 0)) {
printf("dh_sign/verify_hash %d %d", stat, stat2);
return 1;
}
dh_free (&usera);
return 0;
}

51
demos/test/dsa_test.c Normal file
View File

@@ -0,0 +1,51 @@
#include "test.h"
int dsa_test(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 */
DO(dsa_make_key(&test_yarrow, find_prng("yarrow"), 20, 128, &key));
/* verify it */
DO(dsa_verify_key(&key, &stat1));
if (stat1 == 0) { printf("dsa_verify_key "); return 1; }
/* sign the message */
x = sizeof(out);
DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &test_yarrow, find_prng("yarrow"), &key));
/* verify it once */
DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
/* Modify and verify again */
msg[0] ^= 1;
DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
msg[0] ^= 1;
if (!(stat1 == 1 && stat2 == 0)) { printf("dsa_verify %d %d", stat1, stat2); return 1; }
/* test exporting it */
x = sizeof(out2);
DO(dsa_export(out2, &x, PK_PRIVATE, &key));
DO(dsa_import(out2, x, &key2));
/* verify a signature with it */
DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
if (stat1 == 0) { printf("dsa_verify (import private) %d ", stat1); return 1; }
dsa_free(&key2);
/* export as public now */
x = sizeof(out2);
DO(dsa_export(out2, &x, PK_PUBLIC, &key));
DO(dsa_import(out2, x, &key2));
/* verify a signature with it */
DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
if (stat1 == 0) { printf("dsa_verify (import public) %d ", stat1); return 1; }
dsa_free(&key2);
dsa_free(&key);
return 0;
}

89
demos/test/ecc_test.c Normal file
View File

@@ -0,0 +1,89 @@
#include "test.h"
int ecc_tests (void)
{
unsigned char buf[4][4096];
unsigned long x, y, z;
int stat, stat2;
ecc_key usera, userb;
DO(ecc_test ());
/* make up two keys */
DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 24, &usera));
DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 24, &userb));
/* make the shared secret */
x = 4096;
DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
y = 4096;
DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
if (y != x) {
printf ("ecc Shared keys are not same size.");
return 1;
}
if (memcmp (buf[0], buf[1], x)) {
printf ("ecc Shared keys not same contents.");
return 1;
}
/* now export userb */
y = 4096;
DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
ecc_free (&userb);
/* import and make the shared secret again */
DO(ecc_import (buf[1], y, &userb));
z = 4096;
DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
if (z != x) {
printf ("failed. Size don't match?");
return 1;
}
if (memcmp (buf[0], buf[2], x)) {
printf ("Failed. Content didn't match.");
return 1;
}
ecc_free (&usera);
ecc_free (&userb);
/* test encrypt_key */
ecc_make_key (&test_yarrow, find_prng ("yarrow"), 20, &usera);
for (x = 0; x < 32; x++) {
buf[0][x] = x;
}
y = sizeof (buf[1]);
DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("sha256"), &usera));
zeromem (buf[0], sizeof (buf[0]));
x = sizeof (buf[0]);
DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &usera));
if (x != 32) {
printf ("Failed (length)");
return 1;
}
for (x = 0; x < 32; x++)
if (buf[0][x] != x) {
printf ("Failed (contents)");
return 1;
}
/* test sign_hash */
for (x = 0; x < 16; x++) {
buf[0][x] = x;
}
x = sizeof (buf[1]);
DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow, find_prng ("yarrow"), &usera));
DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
buf[0][0] ^= 1;
DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
if (!(stat == 1 && stat2 == 0)) {
printf("ecc_verify_hash failed");
return 1;
}
ecc_free (&usera);
return 0;
}

12
demos/test/mac_test.c Normal file
View File

@@ -0,0 +1,12 @@
/* test pmac/omac/hmac */
#include "test.h"
int mac_test(void)
{
DO(hmac_test());
DO(pmac_test());
DO(omac_test());
DO(eax_test());
DO(ocb_test());
return 0;
}

13
demos/test/makefile Normal file
View File

@@ -0,0 +1,13 @@
# make test harness, it is good.
CFLAGS += -Wall -W -Os -I../../ -I./
default: test
OBJECTS=test.o cipher_hash_test.o mac_test.o modes_test.o \
pkcs_1_test.o store_test.o rsa_test.o ecc_test.o dsa_test.c dh_tests.o
test: $(OBJECTS)
$(CC) $(OBJECTS) -ltomcrypt -o test
clean:
rm -f test *.o *.obj *.exe *~

14
demos/test/makefile.icc Normal file
View File

@@ -0,0 +1,14 @@
# make test harness, it is good.
CFLAGS += -O3 -xN -ip -I../../ -I./
CC=icc
default: test
OBJECTS=test.o cipher_hash_test.o mac_test.o modes_test.o \
pkcs_1_test.o store_test.o rsa_test.o ecc_test.o dsa_test.c dh_tests.o
test: $(OBJECTS)
$(CC) $(OBJECTS) -ltomcrypt -o test
clean:
rm -f test *.o *~

14
demos/test/makefile.msvc Normal file
View File

@@ -0,0 +1,14 @@
# make test harness, it is good.
CFLAGS = $(CFLAGS) /W3 /Ox -I../../ -I./
default: test.exe
OBJECTS = test.obj cipher_hash_test.obj mac_test.obj modes_test.obj \
pkcs_1_test.obj store_test.obj rsa_test.obj ecc_test.obj dsa_test.c dh_tests.obj
test.exe: $(OBJECTS)
cl $(OBJECTS) tomcrypt.lib advapi32.lib
clean:
rm -f test.exe *.obj *~

112
demos/test/modes_test.c Normal file
View File

@@ -0,0 +1,112 @@
/* test CFB/OFB/CBC modes */
#include "test.h"
int modes_test(void)
{
unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
int x, cipher_idx;
symmetric_CBC cbc;
symmetric_CFB cfb;
symmetric_OFB ofb;
symmetric_CTR ctr;
unsigned long l;
/* make a random pt, key and iv */
yarrow_read(pt, 64, &test_yarrow);
yarrow_read(key, 16, &test_yarrow);
yarrow_read(iv, 16, &test_yarrow);
/* get idx of AES handy */
cipher_idx = find_cipher("aes");
if (cipher_idx == -1) {
printf("test requires AES");
return 1;
}
/* test CBC mode */
/* encode the block */
DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
l = sizeof(iv2);
DO(cbc_getiv(iv2, &l, &cbc));
if (l != 16 || memcmp(iv2, iv, 16)) {
printf("cbc_getiv failed");
return 1;
}
for (x = 0; x < 4; x++) {
DO(cbc_encrypt(pt+x*16, ct+x*16, &cbc));
}
/* decode the block */
DO(cbc_setiv(iv2, l, &cbc));
zeromem(tmp, sizeof(tmp));
for (x = 0; x < 4; x++) {
DO(cbc_decrypt(ct+x*16, tmp+x*16, &cbc));
}
if (memcmp(tmp, pt, 64) != 0) {
printf("CBC failed");
return 1;
}
/* test CFB mode */
/* encode the block */
DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
l = sizeof(iv2);
DO(cfb_getiv(iv2, &l, &cfb));
/* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
if (l != 16) {
printf("cfb_getiv failed");
return 1;
}
DO(cfb_encrypt(pt, ct, 64, &cfb));
/* decode the block */
DO(cfb_setiv(iv, l, &cfb));
zeromem(tmp, sizeof(tmp));
DO(cfb_decrypt(ct, tmp, 64, &cfb));
if (memcmp(tmp, pt, 64) != 0) {
printf("CFB failed");
return 1;
}
/* test OFB mode */
/* encode the block */
DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
l = sizeof(iv2);
DO(ofb_getiv(iv2, &l, &ofb));
if (l != 16 || memcmp(iv2, iv, 16)) {
printf("ofb_getiv failed");
return 1;
}
DO(ofb_encrypt(pt, ct, 64, &ofb));
/* decode the block */
DO(ofb_setiv(iv2, l, &ofb));
zeromem(tmp, sizeof(tmp));
DO(ofb_decrypt(ct, tmp, 64, &ofb));
if (memcmp(tmp, pt, 64) != 0) {
printf("OFB failed");
return 1;
}
/* test CTR mode */
/* encode the block */
DO(ctr_start(cipher_idx, iv, key, 16, 0, &ctr));
l = sizeof(iv2);
DO(ctr_getiv(iv2, &l, &ctr));
if (l != 16 || memcmp(iv2, iv, 16)) {
printf("ctr_getiv failed");
return 1;
}
DO(ctr_encrypt(pt, ct, 64, &ctr));
/* decode the block */
DO(ctr_setiv(iv2, l, &ctr));
zeromem(tmp, sizeof(tmp));
DO(ctr_decrypt(ct, tmp, 64, &ctr));
if (memcmp(tmp, pt, 64) != 0) {
printf("CTR failed");
return 1;
}
return 0;
}

103
demos/test/pkcs_1_test.c Normal file
View File

@@ -0,0 +1,103 @@
#include "test.h"
int pkcs_1_test(void)
{
unsigned char buf[3][128];
int res1, res2, res3, prng_idx, hash_idx;
unsigned long x, y, l1, l2, l3, i1, i2, lparamlen, saltlen, modlen;
static const unsigned char lparam[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
/* get hash/prng */
hash_idx = find_hash("sha1");
prng_idx = find_prng("yarrow");
if (hash_idx == -1 || prng_idx == -1) {
printf("pkcs_1 tests require sha1/yarrow");
return 1;
}
/* do many tests */
for (x = 0; x < 10000; x++) {
zeromem(buf, sizeof(buf));
/* make a dummy message (of random length) */
l3 = (rand() & 31) + 8;
for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
/* random modulus len (v1.5 must be multiple of 8 though arbitrary sizes seem to work) */
modlen = 800 + 8 * (abs(rand()) % 28);
/* PKCS v1.5 testing (encryption) */
l1 = sizeof(buf[1]);
DO(pkcs_1_v15_es_encode(buf[0], l3, modlen, &test_yarrow, prng_idx, buf[1], &l1));
DO(pkcs_1_v15_es_decode(buf[1], l1, modlen, buf[2], l3, &res1));
if (res1 != 1 || memcmp(buf[0], buf[2], l3)) {
printf("pkcs v1.5 encrypt failed %d, %lu, %lu ", res1, l1, l3);
return 1;
}
/* PKCS v1.5 testing (signatures) */
l1 = sizeof(buf[1]);
DO(pkcs_1_v15_sa_encode(buf[0], l3, hash_idx, modlen, buf[1], &l1));
DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res1));
buf[0][i1 = abs(rand()) % l3] ^= 1;
DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res2));
buf[0][i1] ^= 1;
buf[1][i2 = abs(rand()) % l1] ^= 1;
DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res3));
if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
printf("pkcs v1.5 sign failed %d %d %d ", res1, res2, res3);
return 1;
}
/* pick a random lparam len [0..16] */
lparamlen = abs(rand()) % 17;
/* pick a random saltlen 0..16 */
saltlen = abs(rand()) % 17;
/* PKCS #1 v2.0 supports modlens not multiple of 8 */
modlen = 800 + (abs(rand()) % 224);
/* encode it */
l1 = sizeof(buf[1]);
DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &test_yarrow, prng_idx, hash_idx, buf[1], &l1));
/* decode it */
l2 = sizeof(buf[2]);
DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1));
if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
printf("Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
printf("ORIGINAL:\n");
for (x = 0; x < l3; x++) {
printf("%02x ", buf[0][x]);
}
printf("\nRESULT:\n");
for (x = 0; x < l2; x++) {
printf("%02x ", buf[2][x]);
}
printf("\n\n");
return 1;
}
/* test PSS */
l1 = sizeof(buf[1]);
DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &test_yarrow, prng_idx, hash_idx, modlen, buf[1], &l1));
DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
buf[0][i1 = abs(rand()) % l3] ^= 1;
DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
buf[0][i1] ^= 1;
buf[1][i2 = abs(rand()) % l1] ^= 1;
DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3));
if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
printf("PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
return 1;
}
}
return 0;
}

91
demos/test/rsa_test.c Normal file
View File

@@ -0,0 +1,91 @@
#include "test.h"
int rsa_test(void)
{
unsigned char in[1024], out[1024], tmp[1024];
rsa_key key;
int hash_idx, prng_idx, stat, stat2;
unsigned long len, len2;
static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
hash_idx = find_hash("sha1");
prng_idx = find_prng("yarrow");
if (hash_idx == -1 || prng_idx == -1) {
printf("rsa_test requires SHA1 and yarrow");
return 1;
}
/* make a random key/msg */
yarrow_read(in, 20, &test_yarrow);
/* make a random key */
DO(rsa_make_key(&test_yarrow, prng_idx, 1024/8, 65537, &key));
/* encrypt the key (without lparam) */
len = sizeof(out);
len2 = sizeof(tmp);
DO(rsa_encrypt_key(in, 20, out, &len, NULL, 0, &test_yarrow, prng_idx, hash_idx, &key));
/* change a byte */
out[0] ^= 1;
DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, &test_yarrow, prng_idx, hash_idx, &stat2, &key));
/* change a byte back */
out[0] ^= 1;
DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, &test_yarrow, prng_idx, hash_idx, &stat, &key));
if (!(stat == 1 && stat2 == 0)) {
printf("rsa_decrypt_key failed");
return 1;
}
if (len2 != 20 || memcmp(tmp, in, 20)) {
printf("rsa_decrypt_key mismatch len %lu", len2);
return 1;
}
/* encrypt the key (with lparam) */
len = sizeof(out);
len2 = sizeof(tmp);
DO(rsa_encrypt_key(in, 20, out, &len, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &key));
/* change a byte */
out[0] ^= 1;
DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &stat2, &key));
/* change a byte back */
out[0] ^= 1;
DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &stat, &key));
if (!(stat == 1 && stat2 == 0)) {
printf("rsa_decrypt_key failed");
return 1;
}
if (len2 != 20 || memcmp(tmp, in, 20)) {
printf("rsa_decrypt_key mismatch len %lu", len2);
return 1;
}
/* sign a message (unsalted, lower cholestorol and Atkins approved) now */
len = sizeof(out);
DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 0, &key));
DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat, &key));
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat2, &key));
if (!(stat == 1 && stat2 == 0)) {
printf("rsa_verify_hash (unsalted) failed, %d, %d", stat, stat2);
return 1;
}
/* sign a message (salted) now */
len = sizeof(out);
DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 8, &key));
DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat, &key));
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat2, &key));
if (!(stat == 1 && stat2 == 0)) {
printf("rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
return 1;
}
/* free the key and return */
rsa_free(&key);
return 0;
}

43
demos/test/store_test.c Normal file
View File

@@ -0,0 +1,43 @@
#include "test.h"
int store_test(void)
{
unsigned char buf[8];
unsigned long L;
ulong64 LL;
L = 0x12345678UL;
STORE32L (L, &buf[0]);
L = 0;
LOAD32L (L, &buf[0]);
if (L != 0x12345678UL) {
printf ("LOAD/STORE32 Little don't work");
return 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");
return 1;
}
L = 0x12345678UL;
STORE32H (L, &buf[0]);
L = 0;
LOAD32H (L, &buf[0]);
if (L != 0x12345678UL) {
printf ("LOAD/STORE32 High don't work, %08lx", L);
return 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");
return 1;
}
return 0;
}

177
demos/test/test.c Normal file
View File

@@ -0,0 +1,177 @@
#include "test.h"
test_entry tests[26];
test_entry test_list[26] = {
/* test name provides requires entry */
{"store_test", "a", "", store_test },
{"cipher_hash_test", "b", "a", cipher_hash_test },
{"modes_test", "c", "b", modes_test },
{"mac_test", "d", "c", mac_test },
{"pkcs_1_test", "e", "b", pkcs_1_test },
{"rsa_test", "f", "e", rsa_test },
{"ecc_test", "g", "a", ecc_tests },
{"dsa_test", "h", "a", dsa_test },
{"dh_test", "i", "a", dh_tests },
{NULL, NULL, NULL, NULL}
};
prng_state test_yarrow;
static int current_test;
void run_cmd(int res, int line, char *file, char *cmd)
{
if (res != CRYPT_OK) {
fprintf(stderr, "[%s]: %s (%d)\n%s:%d:%s\n", tests[current_test].name, error_to_string(res), res, file, line, cmd);
exit(EXIT_FAILURE);
}
}
void register_algs(void)
{
#ifdef RIJNDAEL
register_cipher (&aes_desc);
#endif
#ifdef BLOWFISH
register_cipher (&blowfish_desc);
#endif
#ifdef XTEA
register_cipher (&xtea_desc);
#endif
#ifdef RC5
register_cipher (&rc5_desc);
#endif
#ifdef RC6
register_cipher (&rc6_desc);
#endif
#ifdef SAFERP
register_cipher (&saferp_desc);
#endif
#ifdef TWOFISH
register_cipher (&twofish_desc);
#endif
#ifdef SAFER
register_cipher (&safer_k64_desc);
register_cipher (&safer_sk64_desc);
register_cipher (&safer_k128_desc);
register_cipher (&safer_sk128_desc);
#endif
#ifdef RC2
register_cipher (&rc2_desc);
#endif
#ifdef DES
register_cipher (&des_desc);
register_cipher (&des3_desc);
#endif
#ifdef CAST5
register_cipher (&cast5_desc);
#endif
#ifdef NOEKEON
register_cipher (&noekeon_desc);
#endif
#ifdef SKIPJACK
register_cipher (&skipjack_desc);
#endif
#ifdef TIGER
register_hash (&tiger_desc);
#endif
#ifdef MD2
register_hash (&md2_desc);
#endif
#ifdef MD4
register_hash (&md4_desc);
#endif
#ifdef MD5
register_hash (&md5_desc);
#endif
#ifdef SHA1
register_hash (&sha1_desc);
#endif
#ifdef SHA256
register_hash (&sha256_desc);
#endif
#ifdef SHA224
register_hash (&sha224_desc);
#endif
#ifdef SHA384
register_hash (&sha384_desc);
#endif
#ifdef SHA512
register_hash (&sha512_desc);
#endif
#ifdef RIPEMD128
register_hash (&rmd128_desc);
#endif
#ifdef RIPEMD160
register_hash (&rmd160_desc);
#endif
#ifdef WHIRLPOOL
register_hash (&whirlpool_desc);
#endif
if (register_prng(&yarrow_desc) == -1) {
printf("Error registering yarrow PRNG\n");
exit(-1);
}
if (register_prng(&sprng_desc) == -1) {
printf("Error registering sprng PRNG\n");
exit(-1);
}
}
/* sort tests based on their requirement/services. Helps make sure dependencies are tested first */
void sort(void)
{
unsigned x, y, z, a, pidx[26];
/* find out where things are provided */
zeromem(pidx, sizeof(pidx));
z = 0;
do {
y = 0;
for (x = 0; test_list[x].name != NULL; x++) {
if (test_list[x].entry == NULL) continue;
if (strlen(test_list[x].prov) == 0) {
y = 1;
tests[z++] = test_list[x]; test_list[x].entry = NULL;
pidx[test_list[x].prov[0]-'a'] = 1;
break;
} else {
for (a = 0; a < strlen(test_list[x].req); a++) {
if (pidx[test_list[x].req[a]-'a'] == 0) break;
}
if (a == strlen(test_list[x].req)) {
y = 1;
tests[z++] = test_list[x]; test_list[x].entry = NULL;
pidx[test_list[x].prov[0]-'a'] = 1;
break;
}
}
}
} while (y == 1);
}
int main(void)
{
printf("Built with\n%s\n", crypt_build_settings);
srand(time(NULL));
sort();
register_algs();
// start dummy yarrow for internal use
DO(yarrow_start(&test_yarrow));
DO(yarrow_add_entropy("test", 4, &test_yarrow));
DO(yarrow_ready(&test_yarrow));
// do tests
for (current_test = 0; tests[current_test].name != NULL; current_test++) {
printf("[%-20s]: ", tests[current_test].name); fflush(stdout);
printf("\t%s\n", tests[current_test].entry()==0?"passed":"failed");
}
return 0;
}

29
demos/test/test.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef __TEST_H_
#define __TEST_H_
#include "mycrypt.h"
typedef struct {
char *name, *prov, *req;
int (*entry)(void);
} test_entry;
extern prng_state test_yarrow;
void run_cmd(int res, int line, char *file, char *cmd);
#define DO(x) run_cmd((x), __LINE__, __FILE__, #x)
/* TESTS */
int cipher_hash_test(void);
int modes_test(void);
int mac_test(void);
int pkcs_1_test(void);
int store_test(void);
int rsa_test(void);
int ecc_tests(void);
int dsa_test(void);
int dh_tests(void);
#endif