Skip to content

Commit 316450c

Browse files
authored
Merge pull request #237 from libtom/pr/dh-facelift-part1
DH facelift part 1
2 parents 5e71ac2 + 243a1dc commit 316450c

19 files changed

Lines changed: 992 additions & 1387 deletions

demos/timing.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,7 @@ static void time_dh(void)
891891
{
892892
dh_key key;
893893
ulong64 t1, t2;
894-
unsigned char buf[2][4096];
895-
unsigned long i, x, y, z;
894+
unsigned long i, x, y;
896895
int err;
897896
static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, 3072/8, 4096/8, 6144/8, 8192/8, 100000};
898897

@@ -908,29 +907,10 @@ static void time_dh(void)
908907
t1 = t_read() - t1;
909908
t2 += t1;
910909

911-
if (y < 15) {
912-
dh_free(&key);
913-
}
910+
dh_free(&key);
914911
}
915912
t2 >>= 4;
916913
fprintf(stderr, "DH-%4lu make_key took %15llu cycles\n", x*8, t2);
917-
918-
t2 = 0;
919-
for (y = 0; y < 16; y++) {
920-
t_start();
921-
t1 = t_read();
922-
z = sizeof(buf[1]);
923-
if ((err = dh_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
924-
&key)) != CRYPT_OK) {
925-
fprintf(stderr, "\n\ndh_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
926-
exit(EXIT_FAILURE);
927-
}
928-
t1 = t_read() - t1;
929-
t2 += t1;
930-
}
931-
t2 >>= 4;
932-
fprintf(stderr, "DH-%4lu encrypt_key took %15llu cycles\n", x*8, t2);
933-
dh_free(&key);
934914
}
935915
}
936916
#else

helper.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ sub die_usage {
366366
$failure ||= check_source() if $check_all || $check_source;
367367
$failure ||= check_defines() if $check_all || $check_defines;
368368
$failure ||= check_descriptors() if $check_all || $check_descriptors;
369-
$failure ||= check_comments() if $check_comments; #XXX-FIXME not included in "--check-all"
369+
$failure ||= check_comments() if $check_all || $check_comments;
370370
$failure ||= process_makefiles(0) if $check_all || $check_makefiles;
371371
$failure ||= process_makefiles(1) if $update_makefiles;
372372

libtomcrypt_VS2008.vcproj

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,15 +2051,27 @@
20512051
>
20522052
</File>
20532053
<File
2054-
RelativePath="src\pk\dh\dh_static.c"
2054+
RelativePath="src\pk\dh\dh_check_pubkey.c"
20552055
>
20562056
</File>
20572057
<File
2058-
RelativePath="src\pk\dh\dh_static.h"
2058+
RelativePath="src\pk\dh\dh_export.c"
20592059
>
20602060
</File>
20612061
<File
2062-
RelativePath="src\pk\dh\dh_sys.c"
2062+
RelativePath="src\pk\dh\dh_free.c"
2063+
>
2064+
</File>
2065+
<File
2066+
RelativePath="src\pk\dh\dh_import.c"
2067+
>
2068+
</File>
2069+
<File
2070+
RelativePath="src\pk\dh\dh_make_key.c"
2071+
>
2072+
</File>
2073+
<File
2074+
RelativePath="src\pk\dh\dh_shared_secret.c"
20632075
>
20642076
</File>
20652077
</Filter>

makefile.mingw

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ src/pk/asn1/der/teletex_string/der_length_teletex_string.o \
160160
src/pk/asn1/der/utctime/der_decode_utctime.o src/pk/asn1/der/utctime/der_encode_utctime.o \
161161
src/pk/asn1/der/utctime/der_length_utctime.o src/pk/asn1/der/utf8/der_decode_utf8_string.o \
162162
src/pk/asn1/der/utf8/der_encode_utf8_string.o src/pk/asn1/der/utf8/der_length_utf8_string.o \
163-
src/pk/dh/dh.o src/pk/dh/dh_static.o src/pk/dh/dh_sys.o src/pk/dsa/dsa_decrypt_key.o \
163+
src/pk/dh/dh.o src/pk/dh/dh_check_pubkey.o src/pk/dh/dh_export.o src/pk/dh/dh_free.o \
164+
src/pk/dh/dh_import.o src/pk/dh/dh_make_key.o src/pk/dh/dh_shared_secret.o src/pk/dsa/dsa_decrypt_key.o \
164165
src/pk/dsa/dsa_encrypt_key.o src/pk/dsa/dsa_export.o src/pk/dsa/dsa_free.o src/pk/dsa/dsa_import.o \
165166
src/pk/dsa/dsa_import_radix.o src/pk/dsa/dsa_make_key.o src/pk/dsa/dsa_shared_secret.o \
166167
src/pk/dsa/dsa_sign_hash.o src/pk/dsa/dsa_verify_hash.o src/pk/dsa/dsa_verify_key.o src/pk/ecc/ecc.o \

makefile.msvc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ src/pk/asn1/der/teletex_string/der_length_teletex_string.obj \
153153
src/pk/asn1/der/utctime/der_decode_utctime.obj src/pk/asn1/der/utctime/der_encode_utctime.obj \
154154
src/pk/asn1/der/utctime/der_length_utctime.obj src/pk/asn1/der/utf8/der_decode_utf8_string.obj \
155155
src/pk/asn1/der/utf8/der_encode_utf8_string.obj src/pk/asn1/der/utf8/der_length_utf8_string.obj \
156-
src/pk/dh/dh.obj src/pk/dh/dh_static.obj src/pk/dh/dh_sys.obj src/pk/dsa/dsa_decrypt_key.obj \
156+
src/pk/dh/dh.obj src/pk/dh/dh_check_pubkey.obj src/pk/dh/dh_export.obj src/pk/dh/dh_free.obj \
157+
src/pk/dh/dh_import.obj src/pk/dh/dh_make_key.obj src/pk/dh/dh_shared_secret.obj src/pk/dsa/dsa_decrypt_key.obj \
157158
src/pk/dsa/dsa_encrypt_key.obj src/pk/dsa/dsa_export.obj src/pk/dsa/dsa_free.obj src/pk/dsa/dsa_import.obj \
158159
src/pk/dsa/dsa_import_radix.obj src/pk/dsa/dsa_make_key.obj src/pk/dsa/dsa_shared_secret.obj \
159160
src/pk/dsa/dsa_sign_hash.obj src/pk/dsa/dsa_verify_hash.obj src/pk/dsa/dsa_verify_key.obj src/pk/ecc/ecc.obj \

makefile.unix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ src/pk/asn1/der/teletex_string/der_length_teletex_string.o \
170170
src/pk/asn1/der/utctime/der_decode_utctime.o src/pk/asn1/der/utctime/der_encode_utctime.o \
171171
src/pk/asn1/der/utctime/der_length_utctime.o src/pk/asn1/der/utf8/der_decode_utf8_string.o \
172172
src/pk/asn1/der/utf8/der_encode_utf8_string.o src/pk/asn1/der/utf8/der_length_utf8_string.o \
173-
src/pk/dh/dh.o src/pk/dh/dh_static.o src/pk/dh/dh_sys.o src/pk/dsa/dsa_decrypt_key.o \
173+
src/pk/dh/dh.o src/pk/dh/dh_check_pubkey.o src/pk/dh/dh_export.o src/pk/dh/dh_free.o \
174+
src/pk/dh/dh_import.o src/pk/dh/dh_make_key.o src/pk/dh/dh_shared_secret.o src/pk/dsa/dsa_decrypt_key.o \
174175
src/pk/dsa/dsa_encrypt_key.o src/pk/dsa/dsa_export.o src/pk/dsa/dsa_free.o src/pk/dsa/dsa_import.o \
175176
src/pk/dsa/dsa_import_radix.o src/pk/dsa/dsa_make_key.o src/pk/dsa/dsa_shared_secret.o \
176177
src/pk/dsa/dsa_sign_hash.o src/pk/dsa/dsa_verify_hash.o src/pk/dsa/dsa_verify_key.o src/pk/ecc/ecc.o \

makefile_include.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ src/pk/asn1/der/teletex_string/der_length_teletex_string.o \
276276
src/pk/asn1/der/utctime/der_decode_utctime.o src/pk/asn1/der/utctime/der_encode_utctime.o \
277277
src/pk/asn1/der/utctime/der_length_utctime.o src/pk/asn1/der/utf8/der_decode_utf8_string.o \
278278
src/pk/asn1/der/utf8/der_encode_utf8_string.o src/pk/asn1/der/utf8/der_length_utf8_string.o \
279-
src/pk/dh/dh.o src/pk/dh/dh_static.o src/pk/dh/dh_sys.o src/pk/dsa/dsa_decrypt_key.o \
279+
src/pk/dh/dh.o src/pk/dh/dh_check_pubkey.o src/pk/dh/dh_export.o src/pk/dh/dh_free.o \
280+
src/pk/dh/dh_import.o src/pk/dh/dh_make_key.o src/pk/dh/dh_shared_secret.o src/pk/dsa/dsa_decrypt_key.o \
280281
src/pk/dsa/dsa_encrypt_key.o src/pk/dsa/dsa_export.o src/pk/dsa/dsa_free.o src/pk/dsa/dsa_import.o \
281282
src/pk/dsa/dsa_import_radix.o src/pk/dsa/dsa_make_key.o src/pk/dsa/dsa_shared_secret.o \
282283
src/pk/dsa/dsa_sign_hash.o src/pk/dsa/dsa_verify_hash.o src/pk/dsa/dsa_verify_key.o src/pk/ecc/ecc.o \

src/headers/tomcrypt_pk.h

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,29 @@ int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
183183
/* ---- DH Routines ---- */
184184
#ifdef LTC_MDH
185185

186-
typedef struct Dh_key {
187-
int idx, type;
186+
#ifndef DH_BUF_SIZE
187+
#define DH_BUF_SIZE 2100
188+
#endif
189+
190+
typedef struct {
191+
int size;
192+
char *name, *base, *prime;
193+
} ltc_dh_set_type;
194+
195+
extern const ltc_dh_set_type ltc_dh_sets[];
196+
197+
typedef struct {
198+
int type;
188199
void *x;
189200
void *y;
201+
void *base;
202+
void *prime;
190203
} dh_key;
191204

192-
int dh_compat_test(void);
193-
void dh_sizes(int *low, int *high);
194-
int dh_get_size(dh_key *key);
205+
int dh_get_groupsize(dh_key *key);
195206

196-
int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
207+
int dh_make_key(prng_state *prng, int wprng, int groupsize, dh_key *key);
208+
int dh_make_key_dhparam(prng_state *prng, int wprng, unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
197209
void dh_free(dh_key *key);
198210

199211
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
@@ -202,26 +214,13 @@ int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
202214
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
203215
unsigned char *out, unsigned long *outlen);
204216

205-
int dh_encrypt_key(const unsigned char *in, unsigned long keylen,
206-
unsigned char *out, unsigned long *outlen,
207-
prng_state *prng, int wprng, int hash,
208-
dh_key *key);
209-
210-
int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
211-
unsigned char *out, unsigned long *outlen,
212-
dh_key *key);
213-
214-
int dh_sign_hash(const unsigned char *in, unsigned long inlen,
215-
unsigned char *out, unsigned long *outlen,
216-
prng_state *prng, int wprng, dh_key *key);
217-
218-
int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
219-
const unsigned char *hash, unsigned long hashlen,
220-
int *stat, dh_key *key);
221-
222-
217+
#ifdef LTC_SOURCE
218+
/* INTERNAL ONLY - it should be later moved to src/headers/tomcrypt_internal.h */
219+
int dh_check_pubkey(dh_key *key);
223220
#endif
224221

222+
#endif /* LTC_MDH */
223+
225224

226225
/* ---- ECC Routines ---- */
227226
#ifdef LTC_MECC

0 commit comments

Comments
 (0)