Skip to content

Commit 70f8a57

Browse files
authored
Merge pull request #249 from libtom/pr/dh-dsa-api
dh_set_key + dsa_set_key
2 parents fa4713b + 1819a02 commit 70f8a57

5 files changed

Lines changed: 37 additions & 50 deletions

File tree

src/headers/tomcrypt_pk.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,15 @@ int dh_set_pg(const unsigned char *p, unsigned long plen,
224224
int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
225225
int dh_set_pg_groupsize(int groupsize, dh_key *key);
226226

227-
int dh_set_key(const unsigned char *pub, unsigned long publen,
228-
const unsigned char *priv, unsigned long privlen,
229-
dh_key *key);
227+
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
230228
int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
231229

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

235233
void dh_free(dh_key *key);
236234

237-
int dh_export_key(void *out, unsigned long *outlen,
238-
int type, dh_key *key);
235+
int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);
239236

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

452-
int dsa_set_key(const unsigned char *pub, unsigned long publen,
453-
const unsigned char *priv, unsigned long privlen,
454-
dsa_key *key);
449+
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
455450
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
456451

457452
void dsa_free(dsa_key *key);

src/pk/dh/dh_set.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,40 +78,31 @@ int dh_set_pg_groupsize(int groupsize, dh_key *key)
7878
}
7979

8080
/**
81-
Import DH key parts pub and priv from raw numbers
81+
Import DH public or private key part from raw numbers
8282
83-
@param pub DH's pub (public key) (can be NULL if priv is valid)
84-
@param publen DH's pub's length
85-
@param priv DH's priv (private key) (can be NULL if pub is valid)
86-
@param privlen DH's priv's length
83+
NB: The p & g parts must be set beforehand
84+
85+
@param in The key-part to import, either public or private.
86+
@param inlen The key-part's length
87+
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
8788
@param key [out] the destination for the imported key
8889
@return CRYPT_OK if successful
8990
*/
90-
int dh_set_key(const unsigned char *pub, unsigned long publen,
91-
const unsigned char *priv, unsigned long privlen,
92-
dh_key *key)
91+
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key)
9392
{
9493
int err;
9594

9695
LTC_ARGCHK(key != NULL);
9796
LTC_ARGCHK(ltc_mp.name != NULL);
9897

99-
if(priv == NULL) {
100-
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)pub, publen)) != CRYPT_OK) { goto LBL_ERR; }
101-
key->type = PK_PUBLIC;
102-
mp_clear(key->x);
103-
key->x = NULL;
98+
if (type == PK_PRIVATE) {
99+
key->type = PK_PRIVATE;
100+
if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
101+
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; }
104102
}
105103
else {
106-
if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)priv, privlen)) != CRYPT_OK) { goto LBL_ERR; }
107-
if (pub != NULL) {
108-
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)pub, publen)) != CRYPT_OK) { goto LBL_ERR; }
109-
}
110-
else {
111-
/* compute y value */
112-
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; }
113-
}
114-
key->type = PK_PRIVATE;
104+
key->type = PK_PUBLIC;
105+
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
115106
}
116107

117108
/* check public key */

src/pk/dsa/dsa_set.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ int dsa_set_pqg(const unsigned char *p, unsigned long plen,
5858
}
5959

6060
/**
61-
Import DSA public or private key from raw numbers
62-
@param pub DSA's y (public key) in binary representation
63-
@param publen The length of pub
64-
@param priv DSA's x (private key) in binary representation (can be NULL when importing public key)
65-
@param privlen The length of priv
61+
Import DSA public or private key-part from raw numbers
62+
63+
NB: The p, q & g parts must be set beforehand
64+
65+
@param in The key-part to import, either public or private.
66+
@param inlen The key-part's length
67+
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
6668
@param key [out] the destination for the imported key
6769
@return CRYPT_OK if successful.
6870
*/
69-
int dsa_set_key(const unsigned char *pub, unsigned long publen,
70-
const unsigned char *priv, unsigned long privlen,
71-
dsa_key *key)
71+
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key)
7272
{
7373
int err;
7474

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

83-
if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)pub , publen)) != CRYPT_OK) { goto LBL_ERR; }
84-
if (priv != NULL) {
83+
if (type == PK_PRIVATE) {
8584
key->type = PK_PRIVATE;
86-
if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)priv , privlen)) != CRYPT_OK) { goto LBL_ERR; }
85+
if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
86+
if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; }
8787
}
8888
else {
8989
key->type = PK_PUBLIC;
90+
if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
9091
}
9192

9293
return CRYPT_OK;

tests/dh_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ static int _set_test(void)
268268

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

273273
len = sizeof(buf);
274274
DO(dh_export(buf, &len, PK_PRIVATE, &k1));
@@ -301,7 +301,7 @@ static int _set_test(void)
301301
dh_free(&k1);
302302

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

306306
len = sizeof(buf);
307307
DO(dh_export(buf, &len, PK_PRIVATE, &k1));
@@ -320,7 +320,7 @@ static int _set_test(void)
320320
dh_free(&k1);
321321

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

325325
len = sizeof(buf);
326326
DO(dh_export(buf, &len, PK_PUBLIC, &k2));

tests/dsa_test.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ static int _dsa_compat_test(void)
179179
key_parts[1], key_lens[1],
180180
key_parts[2], key_lens[2],
181181
&key));
182-
DO(dsa_set_key(key_parts[3], key_lens[3],
183-
key_parts[4], key_lens[4],
182+
DO(dsa_set_key(key_parts[4], key_lens[4],
183+
PK_PRIVATE,
184184
&key));
185185
len = sizeof(buf);
186186
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
@@ -196,7 +196,7 @@ static int _dsa_compat_test(void)
196196
key_parts[2], key_lens[2],
197197
&key));
198198
DO(dsa_set_key(key_parts[3], key_lens[3],
199-
NULL, 0,
199+
PK_PUBLIC,
200200
&key));
201201
len = sizeof(buf);
202202
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
@@ -220,7 +220,7 @@ static int _dsa_compat_test(void)
220220
/* try import dsaparam - our public key */
221221
DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
222222
DO(dsa_set_key(key_parts[3], key_lens[3],
223-
NULL, 0,
223+
PK_PUBLIC,
224224
&key));
225225
len = sizeof(buf);
226226
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
@@ -232,8 +232,8 @@ static int _dsa_compat_test(void)
232232

233233
/* try import dsaparam - our private key */
234234
DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
235-
DO(dsa_set_key(key_parts[3], key_lens[3],
236-
key_parts[4], key_lens[4],
235+
DO(dsa_set_key(key_parts[4], key_lens[4],
236+
PK_PRIVATE,
237237
&key));
238238
len = sizeof(buf);
239239
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));

0 commit comments

Comments
 (0)