Skip to content

Commit d032c5c

Browse files
committed
remove prng registry
1 parent 36e5294 commit d032c5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+277
-341
lines changed

demos/ltcrypt.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ int main(int argc, char *argv[])
4141
/* register algs, so they can be printed */
4242
register_all_ciphers();
4343
register_all_hashes();
44-
register_all_prngs();
4544

4645
if (argc < 4) {
4746
if ((argc > 2) && (!strcmp(argv[1], "-t"))) {
@@ -153,7 +152,7 @@ int main(int argc, char *argv[])
153152
} else { /* encrypt */
154153
/* Setup yarrow for random bytes for IV */
155154

156-
if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
155+
if ((err = rng_make_prng(128, &prng, NULL)) != CRYPT_OK) {
157156
printf("Error setting up PRNG, %s\n", error_to_string(err));
158157
}
159158

demos/small.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
int main(void)
77
{
88
register_cipher(&rijndael_enc_desc);
9-
register_prng(&yarrow_desc);
109
register_hash(&sha256_desc);
1110
return 0;
1211
}

demos/timing.c

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -592,22 +592,49 @@ static void time_prng(void)
592592
unsigned long x, y;
593593
int err;
594594

595+
596+
597+
typedef int (*fp_prng_start)(prng_state*);
598+
599+
fp_prng_start prng_start[] = {
600+
#ifdef LTC_YARROW
601+
yarrow_start,
602+
#endif
603+
#ifdef LTC_FORTUNA
604+
fortuna_start,
605+
#endif
606+
#ifdef LTC_RC4
607+
rc4_start,
608+
#endif
609+
#ifdef LTC_CHACHA20_PRNG
610+
chacha20_prng_start,
611+
#endif
612+
#ifdef LTC_SOBER128
613+
sober128_start,
614+
#endif
615+
#ifdef LTC_SPRNG
616+
sprng_start,
617+
#endif
618+
NULL
619+
};
620+
595621
fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
596-
for (x = 0; prng_descriptor[x].name != NULL; x++) {
622+
for (x = 0; prng_start[x] != NULL; x++) {
623+
624+
prng_start[x](&tprng);
597625

598626
/* sanity check on prng */
599-
if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
600-
fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
627+
if ((err = tprng.desc.test()) != CRYPT_OK) {
628+
fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", tprng.desc.name, error_to_string(err));
601629
exit(EXIT_FAILURE);
602630
}
603631

604-
prng_descriptor[x].start(&tprng);
605632
zeromem(buf, 256);
606-
prng_descriptor[x].add_entropy(buf, 256, &tprng);
607-
prng_descriptor[x].ready(&tprng);
633+
tprng.desc.add_entropy(buf, 256, &tprng);
634+
tprng.desc.ready(&tprng);
608635
t2 = -1;
609636

610-
#define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
637+
#define DO1 if (tprng.desc.read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
611638
#define DO2 DO1 DO1
612639
for (y = 0; y < 10000; y++) {
613640
t_start();
@@ -616,11 +643,11 @@ static void time_prng(void)
616643
t1 = (t_read() - t1)>>1;
617644
if (t1 < t2) t2 = t1;
618645
}
619-
fprintf(stderr, "%20s: %5"PRI64"u ", prng_descriptor[x].name, t2>>12);
646+
fprintf(stderr, "%20s: %5"PRI64"u ", tprng.desc.name, t2>>12);
620647
#undef DO2
621648
#undef DO1
622649

623-
#define DO1 prng_descriptor[x].start(&tprng); prng_descriptor[x].add_entropy(buf, 32, &tprng); prng_descriptor[x].ready(&tprng); prng_descriptor[x].done(&tprng);
650+
#define DO1 tprng.desc.start(&tprng); tprng.desc.add_entropy(buf, 32, &tprng); tprng.desc.ready(&tprng); tprng.desc.done(&tprng);
624651
#define DO2 DO1 DO1
625652
for (y = 0; y < 10000; y++) {
626653
t_start();
@@ -663,11 +690,11 @@ static const struct {
663690
for (y = 0; y < 4; y++) {
664691
t_start();
665692
t1 = t_read();
666-
if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
693+
if ((err = dsa_generate_pqg(&yarrow_prng, groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
667694
fprintf(stderr, "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
668695
exit(EXIT_FAILURE);
669696
}
670-
if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
697+
if ((err = dsa_generate_key(&yarrow_prng, &key)) != CRYPT_OK) {
671698
fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
672699
exit(EXIT_FAILURE);
673700
}
@@ -710,7 +737,7 @@ static void time_rsa(void)
710737
for (y = 0; y < 4; y++) {
711738
t_start();
712739
t1 = t_read();
713-
if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
740+
if ((err = rsa_make_key(&yarrow_prng, x/8, 65537, &key)) != CRYPT_OK) {
714741
fprintf(stderr, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
715742
exit(EXIT_FAILURE);
716743
}
@@ -735,7 +762,7 @@ static void time_rsa(void)
735762
t1 = t_read();
736763
z = sizeof(buf[1]);
737764
if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
738-
find_prng("yarrow"), find_hash("sha1"),
765+
find_hash("sha1"),
739766
&key)) != CRYPT_OK) {
740767
fprintf(stderr, "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
741768
exit(EXIT_FAILURE);
@@ -776,7 +803,7 @@ static void time_rsa(void)
776803
t1 = t_read();
777804
z = sizeof(buf[1]);
778805
if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
779-
find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
806+
find_hash("sha1"), 8, &key)) != CRYPT_OK) {
780807
fprintf(stderr, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
781808
exit(EXIT_FAILURE);
782809
}
@@ -846,7 +873,7 @@ static void time_dh(void)
846873

847874
t_start();
848875
t1 = t_read();
849-
if ((err = dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
876+
if ((err = dh_generate_key(&yarrow_prng, &key)) != CRYPT_OK) {
850877
fprintf(stderr, "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
851878
exit(EXIT_FAILURE);
852879
}
@@ -906,7 +933,7 @@ static void time_ecc(void)
906933
for (y = 0; y < 256; y++) {
907934
t_start();
908935
t1 = t_read();
909-
if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
936+
if ((err = ecc_make_key(&yarrow_prng, x, &key)) != CRYPT_OK) {
910937
fprintf(stderr, "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
911938
exit(EXIT_FAILURE);
912939
}
@@ -930,7 +957,7 @@ static void time_ecc(void)
930957
t_start();
931958
t1 = t_read();
932959
z = sizeof(buf[1]);
933-
if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
960+
if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_hash("sha1"),
934961
&key)) != CRYPT_OK) {
935962
fprintf(stderr, "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
936963
exit(EXIT_FAILURE);
@@ -970,7 +997,7 @@ static void time_ecc(void)
970997
t1 = t_read();
971998
z = sizeof(buf[1]);
972999
if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
973-
find_prng("yarrow"), &key)) != CRYPT_OK) {
1000+
&key)) != CRYPT_OK) {
9741001
fprintf(stderr, "\n\necc_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
9751002
exit(EXIT_FAILURE);
9761003
}
@@ -1358,7 +1385,6 @@ const char* mpi_provider = NULL;
13581385
init_timer();
13591386
register_all_ciphers();
13601387
register_all_hashes();
1361-
register_all_prngs();
13621388

13631389
#ifdef USE_LTM
13641390
mpi_provider = "ltm";
@@ -1376,7 +1402,9 @@ register_all_prngs();
13761402

13771403
crypt_mp_init(mpi_provider);
13781404

1379-
if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
1405+
yarrow_start(&yarrow_prng);
1406+
1407+
if ((err = rng_make_prng(128, &yarrow_prng, NULL)) != CRYPT_OK) {
13801408
fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
13811409
exit(EXIT_FAILURE);
13821410
}

demos/tv_gen.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,6 @@ int main(void)
770770
{
771771
register_all_ciphers();
772772
register_all_hashes();
773-
register_all_prngs();
774773
#ifdef USE_LTM
775774
ltc_mp = ltm_desc;
776775
#elif defined(USE_TFM)

src/headers/tomcrypt_math.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,13 @@ typedef struct {
443443

444444
/** RSA Key Generation
445445
@param prng An active PRNG state
446-
@param wprng The index of the PRNG desired
447446
@param size The size of the key in octets
448447
@param e The "e" value (public key).
449448
e==65537 is a good choice
450449
@param key [out] Destination of a newly created private key pair
451450
@return CRYPT_OK if successful, upon error all allocated ram is freed
452451
*/
453452
int (*rsa_keygen)(prng_state *prng,
454-
int wprng,
455453
int size,
456454
long e,
457455
rsa_key *key);

src/headers/tomcrypt_pk.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum public_key_type {
5757
PK_CURVEOID = 0x4000
5858
};
5959

60-
int rand_prime(void *N, long len, prng_state *prng, int wprng);
60+
int rand_prime(void *N, long len, prng_state *prng);
6161

6262
/* ---- RSA ---- */
6363
#ifdef LTC_MRSA
@@ -84,8 +84,8 @@ typedef struct Rsa_key {
8484
void *dQ;
8585
} rsa_key;
8686

87-
int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
88-
int rsa_make_key_ubin_e(prng_state *prng, int wprng, int size,
87+
int rsa_make_key(prng_state *prng, int size, long e, rsa_key *key);
88+
int rsa_make_key_ubin_e(prng_state *prng, int size,
8989
const unsigned char *e, unsigned long elen, rsa_key *key);
9090
int rsa_get_size(const rsa_key *key);
9191

@@ -96,14 +96,14 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
9696
void rsa_free(rsa_key *key);
9797

9898
/* These use PKCS #1 v2.0 padding */
99-
#define rsa_encrypt_key(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, key) \
100-
rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, -1, LTC_PKCS_1_OAEP, key)
99+
#define rsa_encrypt_key(in, inlen, out, outlen, lparam, lparamlen, prng, hash_idx, key) \
100+
rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, hash_idx, -1, LTC_PKCS_1_OAEP, key)
101101

102102
#define rsa_decrypt_key(in, inlen, out, outlen, lparam, lparamlen, hash_idx, stat, key) \
103103
rsa_decrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, hash_idx, -1, LTC_PKCS_1_OAEP, stat, key)
104104

105-
#define rsa_sign_hash(in, inlen, out, outlen, prng, prng_idx, hash_idx, saltlen, key) \
106-
rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, saltlen, key)
105+
#define rsa_sign_hash(in, inlen, out, outlen, prng, hash_idx, saltlen, key) \
106+
rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, hash_idx, saltlen, key)
107107

108108
#define rsa_verify_hash(sig, siglen, hash, hashlen, hash_idx, saltlen, stat, key) \
109109
rsa_verify_hash_ex(sig, siglen, hash, hashlen, LTC_PKCS_1_PSS, hash_idx, saltlen, stat, key)
@@ -115,7 +115,7 @@ void rsa_free(rsa_key *key);
115115
int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
116116
unsigned char *out, unsigned long *outlen,
117117
const unsigned char *lparam, unsigned long lparamlen,
118-
prng_state *prng, int prng_idx,
118+
prng_state *prng,
119119
int mgf_hash, int lparam_hash,
120120
int padding,
121121
const rsa_key *key);
@@ -130,7 +130,7 @@ int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen
130130
int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
131131
unsigned char *out, unsigned long *outlen,
132132
int padding,
133-
prng_state *prng, int prng_idx,
133+
prng_state *prng,
134134
int hash_idx, unsigned long saltlen,
135135
const rsa_key *key);
136136

@@ -188,7 +188,7 @@ int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh
188188
int dh_set_pg_groupsize(int groupsize, dh_key *key);
189189

190190
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
191-
int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
191+
int dh_generate_key(prng_state *prng, dh_key *key);
192192

193193
int dh_shared_secret(const dh_key *private_key, const dh_key *public_key,
194194
unsigned char *out, unsigned long *outlen);
@@ -303,13 +303,13 @@ int ecc_get_size(const ecc_key *key);
303303

304304
int ecc_find_curve(const char* name_or_oid, const ltc_ecc_curve** cu);
305305
int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key);
306-
int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
306+
int ecc_generate_key(prng_state *prng, ecc_key *key);
307307
int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
308308
int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
309309
int ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key);
310310

311-
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
312-
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu);
311+
int ecc_make_key(prng_state *prng, int keysize, ecc_key *key);
312+
int ecc_make_key_ex(prng_state *prng, ecc_key *key, const ltc_ecc_curve *cu);
313313
void ecc_free(ecc_key *key);
314314

315315
int ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
@@ -330,18 +330,18 @@ int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
330330

331331
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
332332
unsigned char *out, unsigned long *outlen,
333-
prng_state *prng, int wprng, int hash,
333+
prng_state *prng, int hash,
334334
const ecc_key *key);
335335

336336
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
337337
unsigned char *out, unsigned long *outlen,
338338
const ecc_key *key);
339339

340-
#define ecc_sign_hash_rfc7518(in_, inlen_, out_, outlen_, prng_, wprng_, key_) \
341-
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, wprng_, LTC_ECCSIG_RFC7518, NULL, key_)
340+
#define ecc_sign_hash_rfc7518(in_, inlen_, out_, outlen_, prng_, key_) \
341+
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, LTC_ECCSIG_RFC7518, NULL, key_)
342342

343-
#define ecc_sign_hash(in_, inlen_, out_, outlen_, prng_, wprng_, key_) \
344-
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, wprng_, LTC_ECCSIG_ANSIX962, NULL, key_)
343+
#define ecc_sign_hash(in_, inlen_, out_, outlen_, prng_, key_) \
344+
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, LTC_ECCSIG_ANSIX962, NULL, key_)
345345

346346
#define ecc_verify_hash_rfc7518(sig_, siglen_, hash_, hashlen_, stat_, key_) \
347347
ecc_verify_hash_ex(sig_, siglen_, hash_, hashlen_, LTC_ECCSIG_RFC7518, stat_, key_)
@@ -351,7 +351,7 @@ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
351351

352352
int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen,
353353
unsigned char *out, unsigned long *outlen,
354-
prng_state *prng, int wprng, ecc_signature_type sigformat,
354+
prng_state *prng, ecc_signature_type sigformat,
355355
int *recid, const ecc_key *key);
356356

357357
int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
@@ -382,7 +382,7 @@ typedef struct {
382382

383383

384384
/** Ed25519 Signature API */
385-
int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
385+
int ed25519_make_key(prng_state *prng, curve25519_key *key);
386386

387387
int ed25519_export( unsigned char *out, unsigned long *outlen,
388388
int which,
@@ -422,7 +422,7 @@ int ed25519ph_verify(const unsigned char *msg, unsigned long msglen,
422422
const curve25519_key *public_key);
423423

424424
/** X25519 Key-Exchange API */
425-
int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
425+
int x25519_make_key(prng_state *prng, curve25519_key *key);
426426

427427
int x25519_export( unsigned char *out, unsigned long *outlen,
428428
int which,
@@ -476,27 +476,27 @@ typedef struct {
476476
void *y;
477477
} dsa_key;
478478

479-
int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
479+
int dsa_make_key(prng_state *prng, int group_size, int modulus_size, dsa_key *key);
480480

481481
int dsa_set_pqg(const unsigned char *p, unsigned long plen,
482482
const unsigned char *q, unsigned long qlen,
483483
const unsigned char *g, unsigned long glen,
484484
dsa_key *key);
485485
int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
486-
int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
486+
int dsa_generate_pqg(prng_state *prng, int group_size, int modulus_size, dsa_key *key);
487487

488488
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
489-
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
489+
int dsa_generate_key(prng_state *prng, dsa_key *key);
490490

491491
void dsa_free(dsa_key *key);
492492

493493
int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
494-
void *r, void *s,
495-
prng_state *prng, int wprng, const dsa_key *key);
494+
void *r, void *s,
495+
prng_state *prng, const dsa_key *key);
496496

497497
int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
498498
unsigned char *out, unsigned long *outlen,
499-
prng_state *prng, int wprng, const dsa_key *key);
499+
prng_state *prng, const dsa_key *key);
500500

501501
int dsa_verify_hash_raw( void *r, void *s,
502502
const unsigned char *hash, unsigned long hashlen,
@@ -508,7 +508,7 @@ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
508508

509509
int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
510510
unsigned char *out, unsigned long *outlen,
511-
prng_state *prng, int wprng, int hash,
511+
prng_state *prng, int hash,
512512
const dsa_key *key);
513513

514514
int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,

0 commit comments

Comments
 (0)