dh_make_key_ex dh_export_radix dh_import_radix

This commit is contained in:
Karel Miko 2017-06-22 10:21:32 +02:00 committed by Steffen Jaeckel
parent a42f467ff1
commit f60e2902ed
5 changed files with 474 additions and 10 deletions

View File

@ -216,12 +216,25 @@ typedef struct {
int dh_get_groupsize(dh_key *key);
int dh_make_key(prng_state *prng, int wprng, int groupsize, dh_key *key);
int dh_make_key_ex(prng_state *prng, int wprng, int radix,
void *prime, unsigned long primelen,
void *base, unsigned long baselen,
dh_key *key);
int dh_make_key_dhparam(prng_state *prng, int wprng, unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
void dh_free(dh_key *key);
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
int dh_export_radix(int radix,
void *out, unsigned long *outlen,
int type, dh_key *key);
int dh_import_radix(int radix,
void *in, unsigned long inlen,
void *prime, unsigned long primelen,
void *base, unsigned long baselen,
int type, dh_key *key);
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
unsigned char *out, unsigned long *outlen);

View File

@ -0,0 +1,68 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*/
#include "tomcrypt.h"
#ifdef LTC_MDH
static unsigned long _count_digits(int radix, void *num)
{
void *r, *t;
unsigned long digits = 0;
if (mp_iszero(num) == LTC_MP_YES) return 1;
if (mp_init_multi(&t, &r, NULL) != CRYPT_OK) return 0;
mp_copy(num, t);
mp_set_int(r, radix);
while (mp_iszero(t) == LTC_MP_NO) {
if (mp_div(t, r, t, NULL) != CRYPT_OK) {
mp_clear_multi(t, r, NULL);
return 0;
}
digits++;
}
mp_clear_multi(t, r, NULL);
return digits;
}
/**
Export a DH key to a binary packet
@param out [out] The destination for the key
@param outlen [in/out] The max size and resulting size of the DH key
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
@param key The key you wish to export
@return CRYPT_OK if successful
*/
int dh_export_radix(int radix, void *out, unsigned long *outlen, int type, dh_key *key)
{
unsigned long len;
void *k;
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
LTC_ARGCHK(key != NULL);
LTC_ARGCHK((radix >= 2 && radix <= 64) || radix == 256);
k = (type == PK_PRIVATE) ? key->x : key->y;
len = (radix == 256) ? mp_unsigned_bin_size(k) : _count_digits(radix, k) + 1;
if (*outlen < len) {
*outlen = len;
return CRYPT_BUFFER_OVERFLOW;
}
*outlen = len;
return (radix == 256) ? mp_to_unsigned_bin(k, out) : mp_toradix(k, out, radix);
}
#endif /* LTC_MDH */
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */

View File

@ -0,0 +1,90 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*/
#include "tomcrypt.h"
#ifdef LTC_MDH
/**
Import a DH key from a binary string
@param in The string to read
@param inlen The length of the input packet
@param type The type of key (PK_PRIVATE or PK_PUBLIC)
@param base The base (generator) in hex string
@param prime The prime in hex string
@param key [out] Where to import the key to
@return CRYPT_OK if successful, on error all allocated memory is freed automatically
*/
int dh_import_radix(int radix,
void *in, unsigned long inlen,
void *prime, unsigned long primelen,
void *base, unsigned long baselen,
int type, dh_key *key)
{
int err;
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(base != NULL);
LTC_ARGCHK(prime != NULL);
LTC_ARGCHK(key != NULL);
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
goto error;
}
if (radix == 256) {
if ((err = mp_read_unsigned_bin(key->base, base, baselen)) != CRYPT_OK) { goto error; }
if ((err = mp_read_unsigned_bin(key->prime, prime, primelen)) != CRYPT_OK) { goto error; }
}
else {
if ((err = mp_read_radix(key->base, base, radix)) != CRYPT_OK) { goto error; }
if ((err = mp_read_radix(key->prime, prime, radix)) != CRYPT_OK) { goto error; }
}
if (type == PK_PRIVATE) {
/* load the x value */
if (radix == 256) {
if ((err = mp_read_unsigned_bin(key->x, in, inlen)) != CRYPT_OK) { goto error; }
}
else {
if ((err = mp_read_radix(key->x, in, radix)) != CRYPT_OK) { goto error; }
}
/* compute y value */
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto error; }
key->type = PK_PRIVATE;
}
else {
/* load the y value */
if (radix == 256) {
if ((err = mp_read_unsigned_bin(key->y, in, inlen)) != CRYPT_OK) { goto error; }
}
else {
if ((err = mp_read_radix(key->y, in, radix)) != CRYPT_OK) { goto error; }
}
key->type = PK_PUBLIC;
mp_clear(key->x);
key->x = NULL;
}
/* check public key */
if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
goto error;
}
return CRYPT_OK;
error:
mp_clear_multi(key->prime, key->base, key->y, key->x, NULL);
return err;
}
#endif /* LTC_MDH */
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */

View File

@ -106,6 +106,43 @@ freemp:
return err;
}
/**
Make a DH key (custom DH group) [private key pair]
@param prng An active PRNG state
@param wprng The index for the PRNG you desire to use
@param prime_hex The prime p (hexadecimal string)
@param base_hex The base g (hexadecimal string)
@param key [out] Where the newly created DH key will be stored
@return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically.
*/
int dh_make_key_ex(prng_state *prng, int wprng, int radix,
void *prime, unsigned long primelen,
void *base, unsigned long baselen,
dh_key *key)
{
void *p, *b;
int err;
LTC_ARGCHK(prime != NULL);
LTC_ARGCHK(base != NULL);
LTC_ARGCHK((radix >= 2 && radix <= 64) || radix == 256);
if ((err = mp_init_multi(&p, &b, NULL)) != CRYPT_OK) { return err; }
if (radix == 256) {
if ((err = mp_read_unsigned_bin(b, base, baselen)) != CRYPT_OK) { goto error; }
if ((err = mp_read_unsigned_bin(p, prime, primelen)) != CRYPT_OK) { goto error; }
}
else {
if ((err = mp_read_radix(b, base, radix)) != CRYPT_OK) { goto error; }
if ((err = mp_read_radix(p, prime, radix)) != CRYPT_OK) { goto error; }
}
err = _dh_make_key(prng, wprng, p, b, key);
error:
mp_clear_multi(p, b, NULL);
return err;
}
/**
Make a DH key (use built-in DH groups) [private key pair]
@param prng An active PRNG state
@ -116,22 +153,17 @@ freemp:
*/
int dh_make_key(prng_state *prng, int wprng, int groupsize, dh_key *key)
{
void *p, *b;
int i, err;
int i;
LTC_ARGCHK(groupsize > 0);
for (i = 0; (groupsize > ltc_dh_sets[i].size) && (ltc_dh_sets[i].size != 0); i++);
if (ltc_dh_sets[i].size == 0) return CRYPT_INVALID_KEYSIZE;
if ((err = mp_init_multi(&p, &b, NULL)) != CRYPT_OK) { return err; }
if ((err = mp_read_radix(b, ltc_dh_sets[i].base, 16)) != CRYPT_OK) { goto error; }
if ((err = mp_read_radix(p, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto error; }
err = _dh_make_key(prng, wprng, p, b, key);
error:
mp_clear_multi(p, b, NULL);
return err;
return dh_make_key_ex(prng, wprng, 16,
ltc_dh_sets[i].prime, strlen(ltc_dh_sets[i].prime) + 1,
ltc_dh_sets[i].base, strlen(ltc_dh_sets[i].base) + 1,
key);
}
/**

View File

@ -147,6 +147,261 @@ static int _dhparam_test(void)
return CRYPT_OK;
}
static int _radix_test(void)
{
dh_key k1, k2, k3;
unsigned char buf[DH_BUF_SIZE];
unsigned long len;
int i;
/* RADIX 16 */
char *ghex = "2";
char *phex = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22"
"514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6"
"F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB"
"9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E8603"
"9B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
"15728E5A8AACAA68FFFFFFFFFFFFFFFF";
char *xhex = "A6681ADC386CE944C3DED9A7301DCC9C518250E3EDB62F959198F8DC0057DD6FB57ABAFD788198B1";
char *yhex = "39046632C834418DFA07B3091538B614D1FB5DBB785C0FBEA3B98B295BC0CD076A88D9452141A269"
"E8BAEB1DD654EBA03A5705318D129754CDF4003A8C399240FBB8F162490F6F0DC70E414B6FEE8808"
"6AFAA48E9F3A248EDC093452663D34E0E809D4F6BADBB36F80B6813EBF7C3281B862209E5604BDEA"
"8B8F5F7BFDC3EEB7ADB73048289BCEA0F5A5CDEE7DF91CD1F0BA632F06DBE9BA7EF014B84B02D497"
"CA7D0C60F734752A649DA496946B4E531B30D9F82EDD855636C0B0F2AE232E4186454E8887BB423E"
"32A5A2495EACBA99620ACD03A38345EBB6735E62330A8EE9AA6C8370410F5CD45AF37EE90A0DA95B"
"E96FC939E88FE0BD2CD09FC8F524208C";
/* RADIX 47 */
char *gr47 = "2";
char *pr47 = "F27Mg1SadOFIRbDOJ5dHgHiVF02Z1LHHQ6G5SLG2U8aTdfH1ETk4GARRE7WW99dBUBLb9e2OHFIaSM1A"
"ag2LNNjgYa9I9CjQGJihL3J7A2SGQe8j5Ch8EHMj5jVbAYDiQKhhPhM6Hc56fKS40GUfJkGO7KJ6EXZQ"
"VgbSa2AkPC65F91g0PaYie8AGNVaFKaV9HOQf3ia1iW4i6eCOB9CcBbH7TbQij8AEgjZ0VRBcLKc6UYO"
"1Zc3I2Jc0h1H2HBEH8ONI3OYBbaPV6XhAd8WCc60D0RDBU3H9U7cWL28a0c90XNO0dh5RXEFBbUCE2ZG"
"gh9XQSVIHkVbFIS5F5IGVOkiWAVc9i8BHB2V0UbGW6UdRTZVV";
char *xr47 = "6bhO7O9NWFRgEMjdU0Y5POj3c1JP15MYEdIg3FO1PEjUY2aGYNSXcaF01R";
char *yr47 = "3GNPNWEYfKML1cIbI7Cc1Z0O7aQLJgB734dO2i56LLYDdI4gHYk2GAbQH2WI97hNeC7dj3fPEH8I9gV9"
"U323AXj1AJXbFPFIHGOTdC29QUUeH2SSc6NWhfQDDXd5Q5iXCKEAUGX3SKcNFIfVOYJgZCLjfHYQdgOQ"
"GCjKNgbEV7Hj34MU3b79iANX2DbMYfb9iGi78BWH2HYAd7IAhk7U0OYGHKJX1bIUUj1KBLhAUg46GaER"
"G9W3ARMfBCj6kSdDF9TdkWAjWTDj722IeVJERC4bKU2VDFG20kDhCMF985efD1SS8DfXcdCHF1kDUkSA"
"884FHYiFEPkaagQOBQaN9BNaEHNbbd002DCIIX5eMP4HgPJPF";
/* RADIX 64 */
char *gr64 = "2";
char *pr64 = "3//////////yaFsg8XQC8qnCPYYu3S7D4f0au8YcVCT08BlgOx4viYKKe8UOuq1DtlbHcppJf36p0h2c"
"toNnGtJ+4rRMrHmaNaXRLsObv+nlHCGkccD+rh2/zSjlG6j+tkE6lxMecVfQwV915yIn/cIIXcKUpaMp"
"t207oueME/1PZQI3OSLTEQQHO/gFqapr+3PLqZtAEjbXnYyrOWXLAxdjKf1t2Mbcrd33LEIhoO1F5qR0"
"ZA625yCf1UHYuspZlZddSi60w60vidWwBi1wAFjSLTy6zCKidUAylsbLWN63cLINpgbMhb5T8c69Zw1H"
"0LSevQYgogQF//////////";
char *xr64 = "2cQ1hSE6pfHCFUsQSm7SoSKO9Gu+ssBvMHcFZS05VTRxLwklruWPYn";
char *yr64 = "v16Ooo3H1ZVe7imaLEBOKqVjTktXS3xwZkOifMy3D1sg8sKKXGQ9fwBhh7TPKww0wLmKnZHANLCtq03g"
"CEP90+xZnOaaFRmt73a5BR+w826hwf8wVEYIEt0aqKcOzDE3e2TJskjkpRu2sWJw/V3A1k68WdbO4lUg"
"BZrzx/SFkjwstC4WecywWzQNDxdtv7D7mkcCl1jlfkdxm5BXB0jINodqCOFS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62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB,
0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D, 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04,
0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2, 0x8F,
0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9, 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18,
0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char xbin[] = {
0xA6, 0x68, 0x1A, 0xDC, 0x38, 0x6C, 0xE9, 0x44, 0xC3, 0xDE, 0xD9, 0xA7, 0x30, 0x1D, 0xCC, 0x9C,
0x51, 0x82, 0x50, 0xE3, 0xED, 0xB6, 0x2F, 0x95, 0x91, 0x98, 0xF8, 0xDC, 0x00, 0x57, 0xDD, 0x6F,
0xB5, 0x7A, 0xBA, 0xFD, 0x78, 0x81, 0x98, 0xB1
};
unsigned char ybin[] = {
0x39, 0x04, 0x66, 0x32, 0xC8, 0x34, 0x41, 0x8D, 0xFA, 0x07, 0xB3, 0x09, 0x15, 0x38, 0xB6, 0x14,
0xD1, 0xFB, 0x5D, 0xBB, 0x78, 0x5C, 0x0F, 0xBE, 0xA3, 0xB9, 0x8B, 0x29, 0x5B, 0xC0, 0xCD, 0x07,
0x6A, 0x88, 0xD9, 0x45, 0x21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E, 0x62, 0x33, 0x0A, 0x8E, 0xE9,
0xAA, 0x6C, 0x83, 0x70, 0x41, 0x0F, 0x5C, 0xD4, 0x5A, 0xF3, 0x7E, 0xE9, 0x0A, 0x0D, 0xA9, 0x5B,
0xE9, 0x6F, 0xC9, 0x39, 0xE8, 0x8F, 0xE0, 0xBD, 0x2C, 0xD0, 0x9F, 0xC8, 0xF5, 0x24, 0x20, 0x8C
};
struct {
int radix;
void* g; int glen;
void* p; int plen;
void* x; int xlen;
void* y; int ylen;
} test[4] = {
{ 16, ghex, strlen(ghex)+1, phex, strlen(phex)+1, xhex, strlen(xhex)+1, yhex, strlen(yhex)+1 },
{ 47, gr47, strlen(gr47)+1, pr47, strlen(pr47)+1, xr47, strlen(xr47)+1, yr47, strlen(yr47)+1 },
{ 64, gr64, strlen(gr64)+1, pr64, strlen(pr64)+1, xr64, strlen(xr64)+1, yr64, strlen(yr64)+1 },
{ 256, gbin, sizeof(gbin), pbin, sizeof(pbin), xbin, sizeof(xbin), ybin, sizeof(ybin) }
};
unsigned char export_private[] = {
0x30, 0x82, 0x01, 0x3A, 0x02, 0x01, 0x00, 0x03, 0x02, 0x07, 0x80, 0x02, 0x82, 0x01, 0x01, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC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xFF, 0xFF,
0x02, 0x01, 0x02, 0x02, 0x29, 0x00, 0xA6, 0x68, 0x1A, 0xDC, 0x38, 0x6C, 0xE9, 0x44, 0xC3, 0xDE,
0xD9, 0xA7, 0x30, 0x1D, 0xCC, 0x9C, 0x51, 0x82, 0x50, 0xE3, 0xED, 0xB6, 0x2F, 0x95, 0x91, 0x98,
0xF8, 0xDC, 0x00, 0x57, 0xDD, 0x6F, 0xB5, 0x7A, 0xBA, 0xFD, 0x78, 0x81, 0x98, 0xB1
};
unsigned char export_public[] = {
0x30, 0x82, 0x02, 0x13, 0x02, 0x01, 0x00, 0x03, 0x02, 0x07, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 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0x8E, 0xE9, 0xAA, 0x6C, 0x83, 0x70, 0x41, 0x0F, 0x5C, 0xD4, 0x5A,
0xF3, 0x7E, 0xE9, 0x0A, 0x0D, 0xA9, 0x5B, 0xE9, 0x6F, 0xC9, 0x39, 0xE8, 0x8F, 0xE0, 0xBD, 0x2C,
0xD0, 0x9F, 0xC8, 0xF5, 0x24, 0x20, 0x8C
};
if (register_prng(&yarrow_desc) == -1) {
printf("Error registering yarrow PRNG\n");
return CRYPT_ERROR;
}
for (i = 0; i < 4; i++) {
DO(dh_import_radix(test[i].radix, test[i].x, test[i].xlen, test[i].p, test[i].plen, test[i].g, test[i].glen, PK_PRIVATE, &k1));
len = sizeof(buf);
DO(dh_export(buf, &len, PK_PRIVATE, &k1));
if (compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test", i*10 + 0)) {
printf("radix_test: dh_export+PK_PRIVATE mismatch\n");
dh_free(&k1);
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export(buf, &len, PK_PUBLIC, &k1));
if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 1)) {
printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
dh_free(&k1);
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(256, buf, &len, PK_PRIVATE, &k1));
if (compare_testvector(buf, len, xbin, sizeof(xbin), "radix_test", i*10 + 2)) {
printf("radix_test: dh_export+PK_PRIVATE mismatch\n");
dh_free(&k1);
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(256, buf, &len, PK_PUBLIC, &k1));
if (compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test", i*10 + 3)) {
printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
dh_free(&k1);
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(47, buf, &len, PK_PRIVATE, &k1));
if (compare_testvector(buf, len, xr47, strlen(xr47)+1, "radix_test", i*10 + 4)) {
printf("radix_test: dh_export+PK_PRIVATE mismatch\n");
dh_free(&k1);
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(47, buf, &len, PK_PUBLIC, &k1));
if (compare_testvector(buf, len, yr47, strlen(yr47)+1, "radix_test", i*10 + 5)) {
printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
dh_free(&k1);
return CRYPT_ERROR;
}
dh_free(&k1);
DO(dh_import_radix(test[i].radix, test[i].y, test[i].ylen, test[i].p, test[i].plen, test[i].g, test[i].glen, PK_PUBLIC, &k2));
len = sizeof(buf);
DO(dh_export(buf, &len, PK_PUBLIC, &k2));
if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 6)) {
printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
dh_free(&k2);
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(256, buf, &len, PK_PUBLIC, &k2));
if (compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test", i*10 + 7)) {
printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
dh_free(&k2);
return CRYPT_ERROR;
}
dh_free(&k2);
DO(dh_make_key_ex(&yarrow_prng, find_prng("yarrow"), test[i].radix,
test[i].p, test[i].plen, test[i].g, test[i].glen, &k3));
len = mp_unsigned_bin_size(k3.prime);
DO(mp_to_unsigned_bin(k3.prime, buf));
if (compare_testvector(buf, len, pbin, sizeof(pbin), "radix_test", i*10 + 8)) {
printf("radix_test: dh_make_key_ex prime mismatch\n");
dh_free(&k3);
return CRYPT_ERROR;
}
len = mp_unsigned_bin_size(k3.base);
DO(mp_to_unsigned_bin(k3.base, buf));
if (compare_testvector(buf, len, gbin, sizeof(gbin), "radix_test", i*10 + 9)) {
printf("radix_test: dh_make_key_ex base mismatch\n");
dh_free(&k3);
return CRYPT_ERROR;
}
dh_free(&k3);
}
return CRYPT_OK;
}
static int _basic_test(void)
{
unsigned char buf[3][4096];
@ -216,6 +471,11 @@ static int _basic_test(void)
fprintf(stderr, "dh_groupsize mismatch %d %d\n", size, ltc_dh_sets[x].size);
return CRYPT_ERROR;
}
DO(dh_make_key_ex(&yarrow_prng, find_prng ("yarrow"), 16,
ltc_dh_sets[x].prime, strlen(ltc_dh_sets[x].prime) + 1,
ltc_dh_sets[x].base, strlen(ltc_dh_sets[x].base) + 1,
&usera));
dh_free(&usera);
}
return CRYPT_OK;
@ -227,6 +487,7 @@ int dh_test(void)
if (_prime_test() != CRYPT_OK) fails++;
if (_basic_test() != CRYPT_OK) fails++;
if (_dhparam_test() != CRYPT_OK) fails++;
if (_radix_test() != CRYPT_OK) fails++;
return fails > 0 ? CRYPT_FAIL_TESTVECTOR : CRYPT_OK;
}