From c493a2a0a3cb3a5d05eb96f7d08200a8de193a76 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 27 Jun 2017 22:09:21 +0200 Subject: [PATCH] re-factor dh_export_radix() to dh_export_key() --- src/headers/tomcrypt_pk.h | 5 ++--- src/pk/dh/dh_export_radix.c | 29 ++++------------------------- tests/dh_test.c | 20 +++----------------- 3 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/headers/tomcrypt_pk.h b/src/headers/tomcrypt_pk.h index fcb74da..b73b8c0 100644 --- a/src/headers/tomcrypt_pk.h +++ b/src/headers/tomcrypt_pk.h @@ -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 */ diff --git a/src/pk/dh/dh_export_radix.c b/src/pk/dh/dh_export_radix.c index 301fd7f..d48c011 100644 --- a/src/pk/dh/dh_export_radix.c +++ b/src/pk/dh/dh_export_radix.c @@ -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 */ diff --git a/tests/dh_test.c b/tests/dh_test.c index 077157c..1cf6c22 100644 --- a/tests/dh_test.c +++ b/tests/dh_test.c @@ -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);