re-factor dh_export_radix() to dh_export_key()

This commit is contained in:
Steffen Jaeckel 2017-06-27 22:09:21 +02:00
parent f226efc9a9
commit c493a2a0a3
3 changed files with 9 additions and 45 deletions

View File

@ -236,9 +236,8 @@ int dh_shared_secret(dh_key *private_key, dh_key *public_key,
void dh_free(dh_key *key);
int dh_export_radix(int radix,
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 */

View File

@ -11,35 +11,15 @@
#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
Binary export a DH key to a buffer
@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)
int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key)
{
unsigned long len;
void *k;
@ -47,10 +27,9 @@ int dh_export_radix(int radix, void *out, unsigned long *outlen, int type, dh_ke
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;
len = mp_unsigned_bin_size(k);
if (*outlen < len) {
*outlen = len;
@ -58,7 +37,7 @@ int dh_export_radix(int radix, void *out, unsigned long *outlen, int type, dh_ke
}
*outlen = len;
return (radix == 256) ? mp_to_unsigned_bin(k, out) : mp_toradix(k, out, radix);
return mp_to_unsigned_bin(k, out);
}
#endif /* LTC_MDH */

View File

@ -350,33 +350,19 @@ static int _radix_test(void)
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(256, buf, &len, PK_PRIVATE, &k1));
DO(dh_export_key(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));
DO(dh_export_key(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);
if(test[i].radix != 256) {
@ -396,7 +382,7 @@ static int _radix_test(void)
return CRYPT_ERROR;
}
len = sizeof(buf);
DO(dh_export_radix(256, buf, &len, PK_PUBLIC, &k2));
DO(dh_export_key(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);