Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/headers/tomcrypt_pk.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,15 @@ int dh_set_pg(const unsigned char *p, unsigned long plen,
int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
int dh_set_pg_groupsize(int groupsize, dh_key *key);

int dh_set_key(const unsigned char *pub, unsigned long publen,
const unsigned char *priv, unsigned long privlen,
dh_key *key);
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
int dh_generate_key(prng_state *prng, int wprng, dh_key *key);

int dh_shared_secret(dh_key *private_key, dh_key *public_key,
unsigned char *out, unsigned long *outlen);

void dh_free(dh_key *key);

int dh_export_key(void *out, unsigned long *outlen,
int type, dh_key *key);
int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);

#ifdef LTC_SOURCE
/* internal helper functions */
Expand Down Expand Up @@ -449,9 +446,7 @@ int dsa_set_pqg(const unsigned char *p, unsigned long plen,
int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);

int dsa_set_key(const unsigned char *pub, unsigned long publen,
const unsigned char *priv, unsigned long privlen,
dsa_key *key);
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);

void dsa_free(dsa_key *key);
Expand Down
35 changes: 13 additions & 22 deletions src/pk/dh/dh_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,40 +78,31 @@ int dh_set_pg_groupsize(int groupsize, dh_key *key)
}

/**
Import DH key parts pub and priv from raw numbers
Import DH public or private key part from raw numbers

@param pub DH's pub (public key) (can be NULL if priv is valid)
@param publen DH's pub's length
@param priv DH's priv (private key) (can be NULL if pub is valid)
@param privlen DH's priv's length
NB: The p & g parts must be set beforehand

@param in The key-part to import, either public or private.
@param inlen The key-part's length
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
@param key [out] the destination for the imported key
@return CRYPT_OK if successful
*/
int dh_set_key(const unsigned char *pub, unsigned long publen,
const unsigned char *priv, unsigned long privlen,
dh_key *key)
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key)
{
int err;

LTC_ARGCHK(key != NULL);
LTC_ARGCHK(ltc_mp.name != NULL);

if(priv == NULL) {
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)pub, publen)) != CRYPT_OK) { goto LBL_ERR; }
key->type = PK_PUBLIC;
mp_clear(key->x);
key->x = NULL;
if (type == PK_PRIVATE) {
key->type = PK_PRIVATE;
if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; }
}
else {
if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)priv, privlen)) != CRYPT_OK) { goto LBL_ERR; }
if (pub != NULL) {
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)pub, publen)) != CRYPT_OK) { goto LBL_ERR; }
}
else {
/* compute y value */
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; }
}
key->type = PK_PRIVATE;
key->type = PK_PUBLIC;
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
}

/* check public key */
Expand Down
23 changes: 12 additions & 11 deletions src/pk/dsa/dsa_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ int dsa_set_pqg(const unsigned char *p, unsigned long plen,
}

/**
Import DSA public or private key from raw numbers
@param pub DSA's y (public key) in binary representation
@param publen The length of pub
@param priv DSA's x (private key) in binary representation (can be NULL when importing public key)
@param privlen The length of priv
Import DSA public or private key-part from raw numbers

NB: The p, q & g parts must be set beforehand

@param in The key-part to import, either public or private.
@param inlen The key-part's length
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
@param key [out] the destination for the imported key
@return CRYPT_OK if successful.
*/
int dsa_set_key(const unsigned char *pub, unsigned long publen,
const unsigned char *priv, unsigned long privlen,
dsa_key *key)
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key)
{
int err;

Expand All @@ -80,13 +80,14 @@ int dsa_set_key(const unsigned char *pub, unsigned long publen,
LTC_ARGCHK(key->q != NULL);
LTC_ARGCHK(ltc_mp.name != NULL);

if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)pub , publen)) != CRYPT_OK) { goto LBL_ERR; }
if (priv != NULL) {
if (type == PK_PRIVATE) {
key->type = PK_PRIVATE;
if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)priv , privlen)) != CRYPT_OK) { goto LBL_ERR; }
if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; }
}
else {
key->type = PK_PUBLIC;
if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
}

return CRYPT_OK;
Expand Down
6 changes: 3 additions & 3 deletions tests/dh_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static int _set_test(void)

for (i = 0; i < 1; i++) {
DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k1));
DO(dh_set_key(NULL, 0, test[i].x, test[i].xlen, &k1));
DO(dh_set_key(test[i].x, test[i].xlen, PK_PRIVATE, &k1));

len = sizeof(buf);
DO(dh_export(buf, &len, PK_PRIVATE, &k1));
Expand Down Expand Up @@ -301,7 +301,7 @@ static int _set_test(void)
dh_free(&k1);

DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k1));
DO(dh_set_key(test[i].y, test[i].ylen, test[i].x, test[i].xlen, &k1));
DO(dh_set_key(test[i].x, test[i].xlen, PK_PRIVATE, &k1));

len = sizeof(buf);
DO(dh_export(buf, &len, PK_PRIVATE, &k1));
Expand All @@ -320,7 +320,7 @@ static int _set_test(void)
dh_free(&k1);

DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k2));
DO(dh_set_key(test[i].y, test[i].ylen, NULL, 0, &k2));
DO(dh_set_key(test[i].y, test[i].ylen, PK_PUBLIC, &k2));

len = sizeof(buf);
DO(dh_export(buf, &len, PK_PUBLIC, &k2));
Expand Down
12 changes: 6 additions & 6 deletions tests/dsa_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ static int _dsa_compat_test(void)
key_parts[1], key_lens[1],
key_parts[2], key_lens[2],
&key));
DO(dsa_set_key(key_parts[3], key_lens[3],
key_parts[4], key_lens[4],
DO(dsa_set_key(key_parts[4], key_lens[4],
PK_PRIVATE,
&key));
len = sizeof(buf);
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
Expand All @@ -196,7 +196,7 @@ static int _dsa_compat_test(void)
key_parts[2], key_lens[2],
&key));
DO(dsa_set_key(key_parts[3], key_lens[3],
NULL, 0,
PK_PUBLIC,
&key));
len = sizeof(buf);
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
Expand All @@ -220,7 +220,7 @@ static int _dsa_compat_test(void)
/* try import dsaparam - our public key */
DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
DO(dsa_set_key(key_parts[3], key_lens[3],
NULL, 0,
PK_PUBLIC,
&key));
len = sizeof(buf);
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
Expand All @@ -232,8 +232,8 @@ static int _dsa_compat_test(void)

/* try import dsaparam - our private key */
DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
DO(dsa_set_key(key_parts[3], key_lens[3],
key_parts[4], key_lens[4],
DO(dsa_set_key(key_parts[4], key_lens[4],
PK_PRIVATE,
&key));
len = sizeof(buf);
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
Expand Down