Re-import libtomcrypt 1.05 for cleaner propagating.

From crypt-1.05.tar.bz2, SHA1 of 88250202bb51570dc64f7e8f1c943cda9479258f

--HG--
branch : libtomcrypt-orig
extra : convert_revision : 5c0c50e93111636ccf8deb758a689ad713797baf
This commit is contained in:
Matt Johnston
2006-03-08 12:58:00 +00:00
commit c57e1d8def
298 changed files with 70737 additions and 0 deletions

24
testprof/base64_test.c Normal file
View File

@@ -0,0 +1,24 @@
#include <tomcrypt_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, &yarrow_prng);
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)) {
fprintf(stderr, "base64 failed %lu %lu %lu", x, l1, l2);
return 1;
}
}
return 0;
}
/* $Source: /cvs/libtom/libtomcrypt/testprof/base64_test.c,v $ */
/* $Revision: 1.5 $ */
/* $Date: 2005/05/21 12:51:25 $ */

View File

@@ -0,0 +1,45 @@
/* test the ciphers and hashes using their built-in self-tests */
#include <tomcrypt_test.h>
int cipher_hash_test(void)
{
int x;
unsigned char buf[4096];
unsigned long n;
prng_state nprng;
/* 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());
}
/* test prngs (test, import/export */
for (x = 0; prng_descriptor[x].name != NULL; x++) {
DO(prng_descriptor[x].test());
DO(prng_descriptor[x].start(&nprng));
DO(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng));
DO(prng_descriptor[x].ready(&nprng));
n = sizeof(buf);
DO(prng_descriptor[x].pexport(buf, &n, &nprng));
prng_descriptor[x].done(&nprng);
DO(prng_descriptor[x].pimport(buf, n, &nprng));
DO(prng_descriptor[x].ready(&nprng));
if (prng_descriptor[x].read(buf, 100, &nprng) != 100) {
fprintf(stderr, "Error reading from imported PRNG!\n");
exit(EXIT_FAILURE);
}
prng_descriptor[x].done(&nprng);
}
return 0;
}
/* $Source: /cvs/libtom/libtomcrypt/testprof/cipher_hash_test.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2005/05/05 14:35:59 $ */

370
testprof/der_tests.c Normal file
View File

@@ -0,0 +1,370 @@
#include <tomcrypt_test.h>
#ifndef LTC_DER
int der_tests(void)
{
fprintf(stderr, "NOP");
return 0;
}
#else
static int der_choice_test(void)
{
ltc_asn1_list types[7], host[1];
unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
unsigned long integer, oidbuf[10], outlen, inlen, x, y;
mp_int mpinteger;
ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
/* setup variables */
for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
integer = 1;
for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; }
DO(mpi_to_ltc_error(mp_init(&mpinteger)));
for (x = 0; x < 14; x++) {
/* setup list */
LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
if (x > 7) {
LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
} else {
LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, &mpinteger, 1);
}
LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
/* encode */
outlen = sizeof(outbuf);
DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
/* decode it */
inlen = outlen;
DO(der_decode_sequence(outbuf, inlen, &host, 1));
for (y = 0; y < 7; y++) {
if (types[y].used && y != (x>6?x-7:x)) {
fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x);
return 1;
}
if (!types[y].used && y == (x>6?x-7:x)) {
fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x);
return 1;
}
}
}
mp_clear(&mpinteger);
return 0;
}
int der_tests(void)
{
unsigned long x, y, z, zz, oid[2][32];
unsigned char buf[3][2048];
mp_int a, b, c, d, e, f, g;
static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
static const unsigned char rsa_ia5[] = "test1@rsa.com";
static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
static const unsigned char rsa_printable[] = "Test User 1";
static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
0x73, 0x65, 0x72, 0x20, 0x31 };
static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
ltc_utctime tmp_time;
static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 };
static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
DO(mpi_to_ltc_error(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL)));
for (zz = 0; zz < 16; zz++) {
for (z = 0; z < 1024; z++) {
if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
return 1;
}
DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
if (mp_iszero(&a) == MP_NO) { a.sign = buf[0][0] & 1 ? MP_ZPOS : MP_NEG; }
x = sizeof(buf[0]);
DO(der_encode_integer(&a, buf[0], &x));
DO(der_length_integer(&a, &y));
if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
mp_zero(&b);
DO(der_decode_integer(buf[0], y, &b));
if (y != x || mp_cmp(&a, &b) != MP_EQ) {
fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
#ifdef BN_MP_TORADIX_C
mp_todecimal(&a, buf[0]);
mp_todecimal(&b, buf[1]);
fprintf(stderr, "a == %s\nb == %s\n", buf[0], buf[1]);
#endif
mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
return 1;
}
}
}
/* test short integer */
for (zz = 0; zz < 256; zz++) {
for (z = 1; z < 4; z++) {
if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
return 1;
}
/* encode with normal */
DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
x = sizeof(buf[0]);
DO(der_encode_integer(&a, buf[0], &x));
/* encode with short */
y = sizeof(buf[1]);
DO(der_encode_short_integer(mp_get_int(&a), buf[1], &y));
if (x != y || memcmp(buf[0], buf[1], x)) {
fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
return 1;
}
/* decode it */
x = 0;
DO(der_decode_short_integer(buf[1], y, &x));
if (x != mp_get_int(&a)) {
fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(&a));
mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
return 1;
}
}
}
mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
/* Test bit string */
for (zz = 1; zz < 1536; zz++) {
yarrow_read(buf[0], zz, &yarrow_prng);
for (z = 0; z < zz; z++) {
buf[0][z] &= 0x01;
}
x = sizeof(buf[1]);
DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
DO(der_length_bit_string(zz, &y));
if (y != x) {
fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
return 1;
}
y = sizeof(buf[2]);
DO(der_decode_bit_string(buf[1], x, buf[2], &y));
if (y != zz || memcmp(buf[0], buf[2], zz)) {
fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
return 1;
}
}
/* Test octet string */
for (zz = 1; zz < 1536; zz++) {
yarrow_read(buf[0], zz, &yarrow_prng);
x = sizeof(buf[1]);
DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
DO(der_length_octet_string(zz, &y));
if (y != x) {
fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
return 1;
}
y = sizeof(buf[2]);
DO(der_decode_octet_string(buf[1], x, buf[2], &y));
if (y != zz || memcmp(buf[0], buf[2], zz)) {
fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
return 1;
}
}
/* test OID */
x = sizeof(buf[0]);
DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
fprintf(stderr, "\n");
return 1;
}
y = sizeof(oid[0])/sizeof(oid[0][0]);
DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
fprintf(stderr, "\n");
return 1;
}
/* do random strings */
for (zz = 0; zz < 5000; zz++) {
/* pick a random number of words */
yarrow_read(buf[0], 4, &yarrow_prng);
LOAD32L(z, buf[0]);
z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
/* fill them in */
oid[0][0] = buf[0][0] % 3;
oid[0][1] = buf[0][1] % 40;
for (y = 2; y < z; y++) {
yarrow_read(buf[0], 4, &yarrow_prng);
LOAD32L(oid[0][y], buf[0]);
}
/* encode it */
x = sizeof(buf[0]);
DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
DO(der_length_object_identifier(oid[0], z, &y));
if (x != y) {
fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
return 1;
}
/* decode it */
y = sizeof(oid[0])/sizeof(oid[0][0]);
DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
if (y != z) {
fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
return 1;
}
if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
return 1;
}
}
/* IA5 string */
x = sizeof(buf[0]);
DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x));
if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
return 1;
}
DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y));
if (y != x) {
fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
return 1;
}
y = sizeof(buf[1]);
DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) {
fprintf(stderr, "DER IA5 failed test vector\n");
return 1;
}
/* Printable string */
x = sizeof(buf[0]);
DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x));
if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
return 1;
}
DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y));
if (y != x) {
fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
return 1;
}
y = sizeof(buf[1]);
DO(der_decode_printable_string(buf[0], x, buf[1], &y));
if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) {
fprintf(stderr, "DER printable failed test vector\n");
return 1;
}
/* Test UTC time */
x = sizeof(buf[0]);
DO(der_encode_utctime(&rsa_time1, buf[0], &x));
if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
fprintf(stderr, "\n\n");
for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
return 1;
}
DO(der_length_utctime(&rsa_time1, &y));
if (y != x) {
fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
return 1;
}
DO(der_decode_utctime(buf[0], &y, &tmp_time));
if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
tmp_time.YY,
tmp_time.MM,
tmp_time.DD,
tmp_time.hh,
tmp_time.mm,
tmp_time.ss,
tmp_time.off_dir,
tmp_time.off_mm,
tmp_time.off_hh);
return 1;
}
x = sizeof(buf[0]);
DO(der_encode_utctime(&rsa_time2, buf[0], &x));
if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
fprintf(stderr, "\n\n");
for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
return 1;
}
DO(der_length_utctime(&rsa_time2, &y));
if (y != x) {
fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
return 1;
}
DO(der_decode_utctime(buf[0], &y, &tmp_time));
if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
tmp_time.YY,
tmp_time.MM,
tmp_time.DD,
tmp_time.hh,
tmp_time.mm,
tmp_time.ss,
tmp_time.off_dir,
tmp_time.off_mm,
tmp_time.off_hh);
return 1;
}
return der_choice_test();
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */
/* $Revision: 1.25 $ */
/* $Date: 2005/06/20 20:37:45 $ */

103
testprof/dh_tests.c Normal file
View File

@@ -0,0 +1,103 @@
#include <tomcrypt_test.h>
#ifdef MDH
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 (&yarrow_prng, find_prng ("yarrow"), 512, &usera));
DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), 512, &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) {
fprintf(stderr, "DH Shared keys are not same size.\n");
return 1;
}
if (memcmp (buf[0], buf[1], x)) {
fprintf(stderr, "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) {
fprintf(stderr, "failed. Size don't match?\n");
return 1;
}
if (memcmp (buf[0], buf[2], x)) {
fprintf(stderr, "Failed. Content didn't match.\n");
return 1;
}
dh_free (&usera);
dh_free (&userb);
/* test encrypt_key */
dh_make_key (&yarrow_prng, find_prng ("yarrow"), 512, &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, &yarrow_prng, 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) {
fprintf(stderr, "Failed (length)\n");
return 1;
}
for (x = 0; x < 16; x++)
if (buf[0][x] != x) {
fprintf(stderr, "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, &yarrow_prng , 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)) {
fprintf(stderr, "dh_sign/verify_hash %d %d", stat, stat2);
return 1;
}
dh_free (&usera);
return 0;
}
#else
int dh_tests(void)
{
fprintf(stderr, "NOP");
return 0;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/dh_tests.c,v $ */
/* $Revision: 1.5 $ */
/* $Date: 2005/05/21 12:51:25 $ */

68
testprof/dsa_test.c Normal file
View File

@@ -0,0 +1,68 @@
#include <tomcrypt_test.h>
#ifdef MDSA
int dsa_test(void)
{
unsigned char msg[16], out[1024], out2[1024];
unsigned long x;
int stat1, stat2;
dsa_key key, key2;
/* make a random key */
DO(dsa_make_key(&yarrow_prng, find_prng("yarrow"), 20, 128, &key));
/* verify it */
DO(dsa_verify_key(&key, &stat1));
if (stat1 == 0) { fprintf(stderr, "dsa_verify_key "); return 1; }
/* sign the message */
x = sizeof(out);
DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &yarrow_prng, 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)) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "dsa_verify (import public) %d ", stat1); return 1; }
dsa_free(&key2);
dsa_free(&key);
return 0;
}
#else
int dsa_test(void)
{
fprintf(stderr, "NOP");
return 0;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/dsa_test.c,v $ */
/* $Revision: 1.8 $ */
/* $Date: 2005/06/03 19:24:32 $ */

137
testprof/ecc_test.c Normal file
View File

@@ -0,0 +1,137 @@
#include <tomcrypt_test.h>
#ifdef MECC
static int sizes[] = {
#ifdef ECC192
24,
#endif
#ifdef ECC224
28,
#endif
#ifdef ECC256
32,
#endif
#ifdef ECC384
48,
#endif
#ifdef ECC512
65
#endif
};
int ecc_tests (void)
{
unsigned char buf[4][4096];
unsigned long x, y, z, s;
int stat, stat2;
ecc_key usera, userb, pubKey, privKey;
DO(ecc_test ());
for (s = 0; s < (int)(sizeof(sizes)/sizeof(sizes[0])); s++) {
/* make up two keys */
DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &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) {
fprintf(stderr, "ecc Shared keys are not same size.");
return 1;
}
if (memcmp (buf[0], buf[1], x)) {
fprintf(stderr, "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) {
fprintf(stderr, "failed. Size don't match?");
return 1;
}
if (memcmp (buf[0], buf[2], x)) {
fprintf(stderr, "Failed. Contents didn't match.");
return 1;
}
ecc_free (&usera);
ecc_free (&userb);
/* test encrypt_key */
DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
/* export key */
x = sizeof(buf[0]);
DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
DO(ecc_import(buf[0], x, &pubKey));
x = sizeof(buf[0]);
DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
DO(ecc_import(buf[0], x, &privKey));
for (x = 0; x < 32; x++) {
buf[0][x] = x;
}
y = sizeof (buf[1]);
DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
zeromem (buf[0], sizeof (buf[0]));
x = sizeof (buf[0]);
DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
if (x != 32) {
fprintf(stderr, "Failed (length)");
return 1;
}
for (x = 0; x < 32; x++) {
if (buf[0][x] != x) {
fprintf(stderr, "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, &yarrow_prng, find_prng ("yarrow"), &privKey));
DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
buf[0][0] ^= 1;
DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
return 1;
}
ecc_free (&usera);
ecc_free (&pubKey);
ecc_free (&privKey);
}
return 0;
}
#else
int ecc_tests(void)
{
fprintf(stderr, "NOP");
return 0;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/ecc_test.c,v $ */
/* $Revision: 1.9 $ */
/* $Date: 2005/06/14 19:43:29 $ */

35
testprof/mac_test.c Normal file
View File

@@ -0,0 +1,35 @@
/* test pmac/omac/hmac */
#include <tomcrypt_test.h>
int mac_test(void)
{
#ifdef HMAC
DO(hmac_test());
#endif
#ifdef PMAC
DO(pmac_test());
#endif
#ifdef OMAC
DO(omac_test());
#endif
#ifdef EAX_MODE
DO(eax_test());
#endif
#ifdef OCB_MODE
DO(ocb_test());
#endif
#ifdef CCM_MODE
DO(ccm_test());
#endif
#ifdef GCM_MODE
DO(gcm_test());
#endif
#ifdef PELICAN
DO(pelican_test());
#endif
return 0;
}
/* $Source: /cvs/libtom/libtomcrypt/testprof/mac_test.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2005/05/05 14:35:59 $ */

15
testprof/makefile Normal file
View File

@@ -0,0 +1,15 @@
CFLAGS += -I../src/headers -I./ -Wall -W
OBJECTS = base64_test.o cipher_hash_test.o der_tests.o dh_tests.o \
dsa_test.o ecc_test.o mac_test.o modes_test.o pkcs_1_test.o rsa_test.o \
store_test.o test.o x86_prof.o
default: libtomcrypt_prof.a
libtomcrypt_prof.a: $(OBJECTS)
$(AR) $(ARFLAGS) libtomcrypt_prof.a $(OBJECTS)
ranlib libtomcrypt_prof.a
clean:
rm -f *.o *.a

15
testprof/makefile.icc Normal file
View File

@@ -0,0 +1,15 @@
CFLAGS += -I../src/headers -I./
CC=icc
OBJECTS = base64_test.o cipher_hash_test.o der_tests.o dh_tests.o \
dsa_test.o ecc_test.o mac_test.o modes_test.o pkcs_1_test.o rsa_test.o \
store_test.o test.o x86_prof.o
default: libtomcrypt_prof.a
libtomcrypt_prof.a: $(OBJECTS)
$(AR) $(ARFLAGS) libtomcrypt_prof.a $(OBJECTS)
clean:
rm -f *.o *.a

10
testprof/makefile.msvc Normal file
View File

@@ -0,0 +1,10 @@
CFLAGS = /I../src/headers/ /I./ /Ox /DWIN32 /W3 /Fo$@
OBJECTS=base64_test.obj cipher_hash_test.obj der_tests.obj dh_tests.obj \
dsa_test.obj ecc_test.obj mac_test.obj modes_test.obj pkcs_1_test.obj \
rsa_test.obj store_test.obj test.obj x86_prof.obj
tomcrypt_prof.lib: $(OBJECTS)
lib /out:tomcrypt_prof.lib $(OBJECTS)

15
testprof/makefile.shared Normal file
View File

@@ -0,0 +1,15 @@
CC=libtool --mode=compile gcc
CFLAGS += -I../src/headers -I./ -O3 -fomit-frame-pointer -funroll-loops -Wall -W
OBJECTS = base64_test.o cipher_hash_test.o der_tests.o dh_tests.o \
dsa_test.o ecc_test.o mac_test.o modes_test.o pkcs_1_test.o rsa_test.o \
store_test.o test.o x86_prof.o
default: $(LIBNAME)
$(LIBNAME): $(OBJECTS)
libtool --silent --mode=link gcc $(CFLAGS) `find . -type f | grep "[.]lo" | xargs` -o libtomcrypt_prof.la -rpath $(LIBPATH) -version-info $(VERSION)
libtool --silent --mode=link gcc $(CFLAGS) `find . -type f | grep "[.]o" | xargs` -o libtomcrypt_prof.a
ranlib libtomcrypt_prof.a
libtool --silent --mode=install install -c libtomcrypt_prof.la $(LIBPATH)/libtomcrypt_prof.la

120
testprof/modes_test.c Normal file
View File

@@ -0,0 +1,120 @@
/* test CFB/OFB/CBC modes */
#include <tomcrypt_test.h>
int modes_test(void)
{
unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
int 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, &yarrow_prng);
yarrow_read(key, 16, &yarrow_prng);
yarrow_read(iv, 16, &yarrow_prng);
/* get idx of AES handy */
cipher_idx = find_cipher("aes");
if (cipher_idx == -1) {
fprintf(stderr, "test requires AES");
return 1;
}
#ifdef CBC
/* 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)) {
fprintf(stderr, "cbc_getiv failed");
return 1;
}
DO(cbc_encrypt(pt, ct, 64, &cbc));
/* decode the block */
DO(cbc_setiv(iv2, l, &cbc));
zeromem(tmp, sizeof(tmp));
DO(cbc_decrypt(ct, tmp, 64, &cbc));
if (memcmp(tmp, pt, 64) != 0) {
fprintf(stderr, "CBC failed");
return 1;
}
#endif
#ifdef CFB
/* 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) {
fprintf(stderr, "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) {
fprintf(stderr, "CFB failed");
return 1;
}
#endif
#ifdef OFB
/* 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)) {
fprintf(stderr, "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) {
fprintf(stderr, "OFB failed");
return 1;
}
#endif
#ifdef CTR
/* test CTR mode */
/* encode the block */
DO(ctr_start(cipher_idx, iv, key, 16, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr));
l = sizeof(iv2);
DO(ctr_getiv(iv2, &l, &ctr));
if (l != 16 || memcmp(iv2, iv, 16)) {
fprintf(stderr, "ctr_getiv failed");
return 1;
}
DO(ctr_encrypt(pt, ct, 57, &ctr));
/* decode the block */
DO(ctr_setiv(iv2, l, &ctr));
zeromem(tmp, sizeof(tmp));
DO(ctr_decrypt(ct, tmp, 57, &ctr));
if (memcmp(tmp, pt, 57) != 0) {
fprintf(stderr, "CTR failed");
return 1;
}
#endif
return 0;
}
/* $Source: /cvs/libtom/libtomcrypt/testprof/modes_test.c,v $ */
/* $Revision: 1.6 $ */
/* $Date: 2005/05/21 12:51:25 $ */

96
testprof/pkcs_1_test.c Normal file
View File

@@ -0,0 +1,96 @@
#include <tomcrypt_test.h>
#ifdef PKCS_1
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) {
fprintf(stderr, "pkcs_1 tests require sha1/yarrow");
return 1;
}
/* do many tests */
for (x = 0; x < 100; 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);
/* 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, &yarrow_prng, 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) {
fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
fprintf(stderr, "ORIGINAL:\n");
for (x = 0; x < l3; x++) {
fprintf(stderr, "%02x ", buf[0][x]);
}
fprintf(stderr, "\nRESULT:\n");
for (x = 0; x < l2; x++) {
fprintf(stderr, "%02x ", buf[2][x]);
}
fprintf(stderr, "\n\n");
return 1;
}
/* test PSS */
l1 = sizeof(buf[1]);
DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &yarrow_prng, 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)) {
fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
return 1;
}
}
return 0;
}
#else
int pkcs_1_test(void)
{
fprintf(stderr, "NOP");
return 0;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/pkcs_1_test.c,v $ */
/* $Revision: 1.6 $ */
/* $Date: 2005/05/21 12:51:25 $ */

338
testprof/rsa_test.c Normal file
View File

@@ -0,0 +1,338 @@
#include <tomcrypt_test.h>
#ifdef MRSA
#define RSA_MSGSIZE 78
/* These are test keys [see file test.key] that I use to test my import/export against */
static const unsigned char openssl_private_rsa[] = {
0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a,
0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7,
0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 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 0xa8, 0x33, 0xc1, 0x04, 0x17,
0x4a, 0x9f, };
/*** NOTE: OpenSSL seems to have more to their public key format. I've stripped the extra headers... */
static const unsigned char openssl_public_rsa[] = {
0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
0x00, 0x01, };
static int rsa_compat_test(void)
{
rsa_key key;
unsigned char buf[1024];
unsigned long len;
/* try reading the key */
DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key));
/* now try to export private/public and compare */
len = sizeof(buf);
DO(rsa_export(buf, &len, PK_PRIVATE, &key));
if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, sizeof(openssl_private_rsa));
{
int x;
printf("\n\n");
for (x = 0; x < len; ) { if (buf[x] == openssl_private_rsa[x]) printf("-- "); else printf("%02x ", buf[x]^openssl_private_rsa[x]); if (!(++x & 15)) printf("\n"); }
}
printf("\n\n");
return 1;
}
len = sizeof(buf);
DO(rsa_export(buf, &len, PK_PUBLIC, &key));
if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n");
return 1;
}
rsa_free(&key);
/* try reading the public key */
DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
len = sizeof(buf);
DO(rsa_export(buf, &len, PK_PUBLIC, &key));
if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
fprintf(stderr, "RSA(public) public export failed to match OpenSSL output\n");
return 1;
}
rsa_free(&key);
return 0;
}
int rsa_test(void)
{
unsigned char in[1024], out[1024], tmp[1024];
rsa_key key, privKey, pubKey;
int hash_idx, prng_idx, stat, stat2;
unsigned long rsa_msgsize, len, len2, cnt;
static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
if (rsa_compat_test() != 0) {
return 1;
}
hash_idx = find_hash("sha1");
prng_idx = find_prng("yarrow");
if (hash_idx == -1 || prng_idx == -1) {
fprintf(stderr, "rsa_test requires SHA1 and yarrow");
return 1;
}
/* make 10 random key */
for (cnt = 0; cnt < 10; cnt++) {
DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
if (mp_count_bits(&key.N) != 1024) {
fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(&key.N));
len = mp_unsigned_bin_size(&key.N);
mp_to_unsigned_bin(&key.N, tmp);
fprintf(stderr, "N == \n");
for (cnt = 0; cnt < len; ) {
fprintf(stderr, "%02x ", tmp[cnt]);
if (!(++cnt & 15)) fprintf(stderr, "\n");
}
len = mp_unsigned_bin_size(&key.p);
mp_to_unsigned_bin(&key.p, tmp);
fprintf(stderr, "p == \n");
for (cnt = 0; cnt < len; ) {
fprintf(stderr, "%02x ", tmp[cnt]);
if (!(++cnt & 15)) fprintf(stderr, "\n");
}
len = mp_unsigned_bin_size(&key.q);
mp_to_unsigned_bin(&key.q, tmp);
fprintf(stderr, "\nq == \n");
for (cnt = 0; cnt < len; ) {
fprintf(stderr, "%02x ", tmp[cnt]);
if (!(++cnt & 15)) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
return 1;
}
if (cnt != 9) {
rsa_free(&key);
}
}
/* encrypt the key (without lparam) */
for (cnt = 0; cnt < 4; cnt++) {
for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
/* make a random key/msg */
yarrow_read(in, rsa_msgsize, &yarrow_prng);
len = sizeof(out);
len2 = rsa_msgsize;
DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
/* change a byte */
out[8] ^= 1;
DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key));
/* change a byte back */
out[8] ^= 1;
if (len2 != rsa_msgsize) {
fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
return 1;
}
len2 = rsa_msgsize;
DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "rsa_decrypt_key failed");
return 1;
}
if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
unsigned long x;
fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
fprintf(stderr, "Original contents: \n");
for (x = 0; x < rsa_msgsize; ) {
fprintf(stderr, "%02x ", in[x]);
if (!(++x % 16)) {
fprintf(stderr, "\n");
}
}
fprintf(stderr, "\n");
fprintf(stderr, "Output contents: \n");
for (x = 0; x < rsa_msgsize; ) {
fprintf(stderr, "%02x ", out[x]);
if (!(++x % 16)) {
fprintf(stderr, "\n");
}
}
fprintf(stderr, "\n");
return 1;
}
}
}
/* encrypt the key (with lparam) */
for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
len = sizeof(out);
len2 = rsa_msgsize;
DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
/* change a byte */
out[8] ^= 1;
DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key));
if (len2 != rsa_msgsize) {
fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
return 1;
}
/* change a byte back */
out[8] ^= 1;
len2 = rsa_msgsize;
DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "rsa_decrypt_key failed");
return 1;
}
if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
fprintf(stderr, "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, &yarrow_prng, prng_idx, hash_idx, 0, &key));
/* export key and import as both private and public */
len2 = sizeof(tmp);
DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
DO(rsa_import(tmp, len2, &privKey));
len2 = sizeof(tmp);
DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
DO(rsa_import(tmp, len2, &pubKey));
/* verify with original */
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
rsa_free(&key);
rsa_free(&pubKey);
rsa_free(&privKey);
return 1;
}
/* verify with privKey */
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
rsa_free(&key);
rsa_free(&pubKey);
rsa_free(&privKey);
return 1;
}
/* verify with pubKey */
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
rsa_free(&key);
rsa_free(&pubKey);
rsa_free(&privKey);
return 1;
}
/* sign a message (salted) now (use privKey to make, pubKey to verify) */
len = sizeof(out);
DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
/* change a byte */
in[0] ^= 1;
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
if (!(stat == 1 && stat2 == 0)) {
fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
rsa_free(&key);
rsa_free(&pubKey);
rsa_free(&privKey);
return 1;
}
/* free the key and return */
rsa_free(&key);
rsa_free(&pubKey);
rsa_free(&privKey);
return 0;
}
#else
int rsa_test(void)
{
fprintf(stderr, "NOP");
return 0;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/rsa_test.c,v $ */
/* $Revision: 1.10 $ */
/* $Date: 2005/06/03 19:18:33 $ */

78
testprof/store_test.c Normal file
View File

@@ -0,0 +1,78 @@
#include <tomcrypt_test.h>
/* Test store/load macros with offsets */
int store_test(void)
{
unsigned char buf[256];
int y;
ulong32 L, L1;
ulong64 LL, LL1;
#ifdef LTC_FAST
int x, z;
#endif
for (y = 0; y < 4; y++) {
L = 0x12345678UL;
L1 = 0;
STORE32L(L, buf + y);
LOAD32L(L1, buf + y);
if (L1 != L) {
fprintf(stderr, "\n32L failed at offset %d\n", y);
return 1;
}
STORE32H(L, buf + y);
LOAD32H(L1, buf + y);
if (L1 != L) {
fprintf(stderr, "\n32H failed at offset %d\n", y);
return 1;
}
}
for (y = 0; y < 8; y++) {
LL = CONST64 (0x01020304050607);
LL1 = 0;
STORE64L(LL, buf + y);
LOAD64L(LL1, buf + y);
if (LL1 != LL) {
fprintf(stderr, "\n64L failed at offset %d\n", y);
return 1;
}
STORE64H(LL, buf + y);
LOAD64H(LL1, buf + y);
if (LL1 != LL) {
fprintf(stderr, "\n64H failed at offset %d\n", y);
return 1;
}
}
/* test LTC_FAST */
#ifdef LTC_FAST
y = 16;
for (z = 0; z < y; z++) {
/* fill y bytes with random */
yarrow_read(buf+z, y, &yarrow_prng);
yarrow_read(buf+z+y, y, &yarrow_prng);
/* now XOR it byte for byte */
for (x = 0; x < y; x++) {
buf[2*y+z+x] = buf[z+x] ^ buf[z+y+x];
}
/* now XOR it word for word */
for (x = 0; x < y; x += sizeof(LTC_FAST_TYPE)) {
*((LTC_FAST_TYPE*)(&buf[3*y+z+x])) = *((LTC_FAST_TYPE*)(&buf[z+x])) ^ *((LTC_FAST_TYPE*)(&buf[z+y+x]));
}
if (memcmp(&buf[2*y+z], &buf[3*y+z], y)) {
fprintf(stderr, "\nLTC_FAST failed at offset %d\n", z);
return 1;
}
}
#endif
return 0;
}
/* $Source: /cvs/libtom/libtomcrypt/testprof/store_test.c,v $ */
/* $Revision: 1.6 $ */
/* $Date: 2005/05/05 14:35:59 $ */

13
testprof/test.c Normal file
View File

@@ -0,0 +1,13 @@
#include <tomcrypt_test.h>
void run_cmd(int res, int line, char *file, char *cmd)
{
if (res != CRYPT_OK) {
fprintf(stderr, "%s (%d)\n%s:%d:%s\n", error_to_string(res), res, file, line, cmd);
exit(EXIT_FAILURE);
}
}
/* $Source: /cvs/libtom/libtomcrypt/testprof/test.c,v $ */
/* $Revision: 1.6 $ */
/* $Date: 2005/05/05 14:35:59 $ */

15
testprof/test.key Normal file
View File

@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDPmt5kitrIMyCp14MxGVSymoWnobd1M7aprIQks97bfYUtlmXl
P3KVJJ8oaMpP20QcPmASit0mpev/C17UiDhJKm5bvxI3R70Fa7zb8+7kEY5BaHxh
E9dCyIC+No/cCItPrKTidgzJY2xJWJPtzKrcJTsKYD+LVDrDTTHnlKRE/QIDAQAB
AoGBAMhiuereRFMdVpfZl54azzAeCohFhikwo02fYWVz4NaHj7bzBqOC3Hys/pso
mq79+/4vDtiXBOO7H9HsDbqjSX9HrIpEBH6GtzlCP60etw6lUfRAYx79veqfQZ+o
kB1vClqVExENgK9fZJiKLHhoZbAri6JTh8rxZASr8nvbg8iBAkEA975eI8MyP7+L
jjru/PzL5ffxC7xCgq7Vej7K99VpP2Qloh+3dXUFkkLruPHzCgXjlNFVeDWgNqCb
fJKEbN3cTQJBANaGDoVCCwQIhCFg8A4NiP0eNhBlTx5TtAhygFw/WWYX5pjy6Wx6
Bkysdj3tjKHOrRu9tH0ovOMOOI2Z2AW1o3ECQG3rwy0u8F6kiDEFKQCK0ZUpm4PP
ddsx43on3jp0MAx2TNRQKkAtOdmZY6ldgK5TypQ/BSMe+AUE4bg18hezoIkCQQCr
kIj6YAgpUJpDi6BQzNha/pdkY3F0IqMgAlrP68YWlVTRy6uNGsYA+giSnHHVUlI1
lnFLi5IM0Om/rWMLpemxAkEA3MwnyOTcYkjVm6/1q2D2If1T4rddCckaoQSp/GEs
XQRYOlo58UohVmf9zCCjj3gYWnk9Lo5+hgrmqDPBBBdKnw==
-----END RSA PRIVATE KEY-----

77
testprof/tomcrypt_test.h Normal file
View File

@@ -0,0 +1,77 @@
#ifndef __TEST_H_
#define __TEST_H_
#include <tomcrypt.h>
/* enable stack testing */
// #define STACK_TEST
/* stack testing, define this if stack usage goes downwards [e.g. x86] */
#define STACK_DOWN
typedef struct {
char *name, *prov, *req;
int (*entry)(void);
} test_entry;
extern prng_state yarrow_prng;
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);
int der_tests(void);
/* timing */
#define KTIMES 25
#define TIMES 100000
extern struct list {
int id;
unsigned long spd1, spd2, avg;
} results[];
extern int no_results;
int sorter(const void *a, const void *b);
void tally_results(int type);
ulong64 rdtsc (void);
void t_start(void);
ulong64 t_read(void);
void init_timer(void);
/* register default algs */
void reg_algs(void);
int time_keysched(void);
int time_cipher(void);
int time_cipher2(void);
int time_cipher3(void);
int time_hash(void);
void time_mult(void);
void time_sqr(void);
void time_prng(void);
void time_rsa(void);
void time_ecc(void);
void time_dh(void);
void time_macs_(unsigned long MAC_SIZE);
void time_macs(void);
void time_encmacs(void);
#endif
/* $Source: /cvs/libtom/libtomcrypt/testprof/tomcrypt_test.h,v $ */
/* $Revision: 1.8 $ */
/* $Date: 2005/05/05 14:35:59 $ */

1050
testprof/x86_prof.c Normal file

File diff suppressed because it is too large Load Diff