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
+13
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);
+68
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$ */
+90
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$ */
+42 -10
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);
}
/**