2011-01-18 14:06:03 -05:00
|
|
|
#include <tomcrypt_test.h>
|
|
|
|
|
2014-02-25 03:59:21 -05:00
|
|
|
#ifdef LTC_MDH
|
2011-01-18 14:06:03 -05:00
|
|
|
|
|
|
|
#ifdef DH4096
|
|
|
|
#define KEYSIZE 4096
|
|
|
|
#else
|
|
|
|
#define KEYSIZE 2048
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int dh_test (void)
|
|
|
|
{
|
2017-05-09 16:05:30 -04:00
|
|
|
unsigned char buf[3][4096], ch;
|
2011-01-18 14:06:03 -05:00
|
|
|
unsigned long x, y, z;
|
|
|
|
int stat, stat2;
|
|
|
|
dh_key usera, userb;
|
|
|
|
|
|
|
|
if (register_prng(&yarrow_desc) == -1) {
|
|
|
|
printf("Error registering yarrow PRNG\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (register_hash(&md5_desc) == -1) {
|
|
|
|
printf("Error registering md5 hash\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2014-01-03 09:16:59 -05:00
|
|
|
|
2011-01-18 14:06:03 -05:00
|
|
|
DO(dh_compat_test());
|
|
|
|
|
|
|
|
|
|
|
|
/* make up two keys */
|
|
|
|
DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), KEYSIZE/8, &usera));
|
|
|
|
DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), KEYSIZE/8, &userb));
|
|
|
|
|
|
|
|
/* make the shared secret */
|
|
|
|
x = KEYSIZE;
|
|
|
|
DO(dh_shared_secret (&usera, &userb, buf[0], &x));
|
|
|
|
|
|
|
|
y = KEYSIZE;
|
|
|
|
DO(dh_shared_secret (&userb, &usera, buf[1], &y));
|
|
|
|
if (y != x) {
|
|
|
|
fprintf(stderr, "DH Shared keys are not same size.\n");
|
|
|
|
dh_free (&usera);
|
|
|
|
dh_free (&userb);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (memcmp (buf[0], buf[1], x)) {
|
|
|
|
fprintf(stderr, "DH Shared keys not same contents.\n");
|
|
|
|
dh_free (&usera);
|
|
|
|
dh_free (&userb);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now export userb */
|
|
|
|
y = KEYSIZE;
|
|
|
|
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 = KEYSIZE;
|
|
|
|
DO(dh_shared_secret (&usera, &userb, buf[2], &z));
|
|
|
|
|
|
|
|
dh_free (&usera);
|
|
|
|
dh_free (&userb);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test encrypt_key */
|
|
|
|
dh_make_key (&yarrow_prng, find_prng ("yarrow"), KEYSIZE/8, &usera);
|
2017-05-09 16:05:30 -04:00
|
|
|
for (ch = 0; ch < 16; ch++) {
|
|
|
|
buf[0][ch] = ch;
|
2011-01-18 14:06:03 -05:00
|
|
|
}
|
|
|
|
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");
|
|
|
|
dh_free (&usera);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-05-09 16:05:30 -04:00
|
|
|
for (ch = 0; ch < 16; ch++)
|
|
|
|
if (buf[0][ch] != ch) {
|
2011-01-18 14:06:03 -05:00
|
|
|
fprintf(stderr, "Failed (contents)\n");
|
|
|
|
dh_free (&usera);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test sign_hash */
|
2017-05-09 16:05:30 -04:00
|
|
|
for (ch = 0; ch < 16; ch++) {
|
|
|
|
buf[0][ch] = ch;
|
2011-01-18 14:06:03 -05:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
dh_free (&usera);
|
2014-01-03 09:16:59 -05:00
|
|
|
if (!(stat == 1 && stat2 == 0)) {
|
2011-01-18 14:06:03 -05:00
|
|
|
fprintf(stderr, "dh_sign/verify_hash %d %d", stat, stat2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
int dh_test(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NOP");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|