From e20e204b8cb60459eb8d09113580abe7792d2132 Mon Sep 17 00:00:00 2001 From: Karel Miko Date: Tue, 11 Jul 2017 00:13:26 +0200 Subject: [PATCH] dh_set_key + dsa_set_key API change described in #248 --- src/headers/tomcrypt_pk.h | 8 ++------ src/pk/dh/dh_set.c | 24 +++++++----------------- src/pk/dsa/dsa_set.c | 11 +++++------ tests/dh_test.c | 6 +++--- tests/dsa_test.c | 12 ++++++------ 5 files changed, 23 insertions(+), 38 deletions(-) diff --git a/src/headers/tomcrypt_pk.h b/src/headers/tomcrypt_pk.h index 89e96111..49b9a9b1 100644 --- a/src/headers/tomcrypt_pk.h +++ b/src/headers/tomcrypt_pk.h @@ -224,9 +224,7 @@ 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, @@ -449,9 +447,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); diff --git a/src/pk/dh/dh_set.c b/src/pk/dh/dh_set.c index edd28bb8..8dd3f0d4 100644 --- a/src/pk/dh/dh_set.c +++ b/src/pk/dh/dh_set.c @@ -87,31 +87,21 @@ LBL_ERR: @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 */ diff --git a/src/pk/dsa/dsa_set.c b/src/pk/dsa/dsa_set.c index d25de01b..fd183411 100644 --- a/src/pk/dsa/dsa_set.c +++ b/src/pk/dsa/dsa_set.c @@ -66,9 +66,7 @@ LBL_ERR: @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; @@ -80,13 +78,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; diff --git a/tests/dh_test.c b/tests/dh_test.c index e3111c67..4ef15a9e 100644 --- a/tests/dh_test.c +++ b/tests/dh_test.c @@ -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)); @@ -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)); @@ -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)); diff --git a/tests/dsa_test.c b/tests/dsa_test.c index d871a293..f1d8ecf2 100644 --- a/tests/dsa_test.c +++ b/tests/dsa_test.c @@ -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)); @@ -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)); @@ -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)); @@ -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));