Skip to content

Commit bede8d4

Browse files
committed
remove prng registry
1 parent d9c4e20 commit bede8d4

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

+261
-324
lines changed

demos/ltcrypt.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ int main(int argc, char *argv[])
4747
/* register algs, so they can be printed */
4848
register_all_ciphers();
4949
register_all_hashes();
50-
register_all_prngs();
5150

5251
if (argc < 4) {
5352
if ((argc > 2) && (!strcmp(argv[1], "-t"))) {
@@ -159,7 +158,7 @@ int main(int argc, char *argv[])
159158
} else { /* encrypt */
160159
/* Setup yarrow for random bytes for IV */
161160

162-
if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
161+
if ((err = rng_make_prng(128, &prng, NULL)) != CRYPT_OK) {
163162
printf("Error setting up PRNG, %s\n", error_to_string(err));
164163
}
165164

demos/small.c

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
int main(void)
1313
{
1414
register_cipher(&rijndael_enc_desc);
15-
register_prng(&yarrow_desc);
1615
register_hash(&sha256_desc);
1716
return 0;
1817
}

demos/timing.c

+46-20
Original file line numberDiff line numberDiff line change
@@ -594,22 +594,49 @@ static void time_prng(void)
594594
unsigned long x, y;
595595
int err;
596596

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

600628
/* sanity check on prng */
601-
if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
602-
fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
629+
if ((err = tprng.desc.test()) != CRYPT_OK) {
630+
fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", tprng.desc.name, error_to_string(err));
603631
exit(EXIT_FAILURE);
604632
}
605633

606-
prng_descriptor[x].start(&tprng);
607634
zeromem(buf, 256);
608-
prng_descriptor[x].add_entropy(buf, 256, &tprng);
609-
prng_descriptor[x].ready(&tprng);
635+
tprng.desc.add_entropy(buf, 256, &tprng);
636+
tprng.desc.ready(&tprng);
610637
t2 = -1;
611638

612-
#define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
639+
#define DO1 if (tprng.desc.read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
613640
#define DO2 DO1 DO1
614641
for (y = 0; y < 10000; y++) {
615642
t_start();
@@ -618,11 +645,11 @@ static void time_prng(void)
618645
t1 = (t_read() - t1)>>1;
619646
if (t1 < t2) t2 = t1;
620647
}
621-
fprintf(stderr, "%20s: %5"PRI64"u ", prng_descriptor[x].name, t2>>12);
648+
fprintf(stderr, "%20s: %5"PRI64"u ", tprng.desc.name, t2>>12);
622649
#undef DO2
623650
#undef DO1
624651

625-
#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);
652+
#define DO1 tprng.desc.start(&tprng); tprng.desc.add_entropy(buf, 32, &tprng); tprng.desc.ready(&tprng); tprng.desc.done(&tprng);
626653
#define DO2 DO1 DO1
627654
for (y = 0; y < 10000; y++) {
628655
t_start();
@@ -665,11 +692,11 @@ static const struct {
665692
for (y = 0; y < 4; y++) {
666693
t_start();
667694
t1 = t_read();
668-
if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
695+
if ((err = dsa_generate_pqg(&yarrow_prng, groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
669696
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));
670697
exit(EXIT_FAILURE);
671698
}
672-
if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
699+
if ((err = dsa_generate_key(&yarrow_prng, &key)) != CRYPT_OK) {
673700
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));
674701
exit(EXIT_FAILURE);
675702
}
@@ -712,7 +739,7 @@ static void time_rsa(void)
712739
for (y = 0; y < 4; y++) {
713740
t_start();
714741
t1 = t_read();
715-
if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
742+
if ((err = rsa_make_key(&yarrow_prng, x/8, 65537, &key)) != CRYPT_OK) {
716743
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));
717744
exit(EXIT_FAILURE);
718745
}
@@ -737,7 +764,7 @@ static void time_rsa(void)
737764
t1 = t_read();
738765
z = sizeof(buf[1]);
739766
if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
740-
find_prng("yarrow"), find_hash("sha1"),
767+
find_hash("sha1"),
741768
&key)) != CRYPT_OK) {
742769
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));
743770
exit(EXIT_FAILURE);
@@ -778,7 +805,7 @@ static void time_rsa(void)
778805
t1 = t_read();
779806
z = sizeof(buf[1]);
780807
if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
781-
find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
808+
find_hash("sha1"), 8, &key)) != CRYPT_OK) {
782809
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));
783810
exit(EXIT_FAILURE);
784811
}
@@ -848,7 +875,7 @@ static void time_dh(void)
848875

849876
t_start();
850877
t1 = t_read();
851-
if ((err = dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
878+
if ((err = dh_generate_key(&yarrow_prng, &key)) != CRYPT_OK) {
852879
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));
853880
exit(EXIT_FAILURE);
854881
}
@@ -908,7 +935,7 @@ static void time_ecc(void)
908935
for (y = 0; y < 256; y++) {
909936
t_start();
910937
t1 = t_read();
911-
if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
938+
if ((err = ecc_make_key(&yarrow_prng, x, &key)) != CRYPT_OK) {
912939
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));
913940
exit(EXIT_FAILURE);
914941
}
@@ -932,7 +959,7 @@ static void time_ecc(void)
932959
t_start();
933960
t1 = t_read();
934961
z = sizeof(buf[1]);
935-
if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
962+
if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_hash("sha1"),
936963
&key)) != CRYPT_OK) {
937964
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));
938965
exit(EXIT_FAILURE);
@@ -972,7 +999,7 @@ static void time_ecc(void)
972999
t1 = t_read();
9731000
z = sizeof(buf[1]);
9741001
if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
975-
find_prng("yarrow"), &key)) != CRYPT_OK) {
1002+
&key)) != CRYPT_OK) {
9761003
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));
9771004
exit(EXIT_FAILURE);
9781005
}
@@ -1360,7 +1387,6 @@ const char* mpi_provider = NULL;
13601387
init_timer();
13611388
register_all_ciphers();
13621389
register_all_hashes();
1363-
register_all_prngs();
13641390

13651391
#ifdef USE_LTM
13661392
mpi_provider = "ltm";
@@ -1378,7 +1404,7 @@ register_all_prngs();
13781404

13791405
crypt_mp_init(mpi_provider);
13801406

1381-
if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
1407+
if ((err = rng_make_prng(128, &yarrow_prng, NULL)) != CRYPT_OK) {
13821408
fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
13831409
exit(EXIT_FAILURE);
13841410
}

demos/tv_gen.c

-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,6 @@ int main(void)
776776
{
777777
register_all_ciphers();
778778
register_all_hashes();
779-
register_all_prngs();
780779
#ifdef USE_LTM
781780
ltc_mp = ltm_desc;
782781
#elif defined(USE_TFM)

src/headers/tomcrypt_math.h

-2
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,13 @@ typedef struct {
449449

450450
/** RSA Key Generation
451451
@param prng An active PRNG state
452-
@param wprng The index of the PRNG desired
453452
@param size The size of the key in octets
454453
@param e The "e" value (public key).
455454
e==65537 is a good choice
456455
@param key [out] Destination of a newly created private key pair
457456
@return CRYPT_OK if successful, upon error all allocated ram is freed
458457
*/
459458
int (*rsa_keygen)(prng_state *prng,
460-
int wprng,
461459
int size,
462460
long e,
463461
rsa_key *key);

src/headers/tomcrypt_pk.h

+27-27
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum public_key_type {
2323
PK_CURVEOID = 0x4000
2424
};
2525

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

2828
/* ---- RSA ---- */
2929
#ifdef LTC_MRSA
@@ -50,7 +50,7 @@ typedef struct Rsa_key {
5050
void *dQ;
5151
} rsa_key;
5252

53-
int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
53+
int rsa_make_key(prng_state *prng, int size, long e, rsa_key *key);
5454

5555
int rsa_get_size(const rsa_key *key);
5656

@@ -61,14 +61,14 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
6161
void rsa_free(rsa_key *key);
6262

6363
/* These use PKCS #1 v2.0 padding */
64-
#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
65-
rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
64+
#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _hash_idx, _key) \
65+
rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _hash_idx, LTC_PKCS_1_OAEP, _key)
6666

6767
#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
6868
rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
6969

70-
#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
71-
rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
70+
#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _hash_idx, _saltlen, _key) \
71+
rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _hash_idx, _saltlen, _key)
7272

7373
#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
7474
rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
@@ -80,7 +80,7 @@ void rsa_free(rsa_key *key);
8080
int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
8181
unsigned char *out, unsigned long *outlen,
8282
const unsigned char *lparam, unsigned long lparamlen,
83-
prng_state *prng, int prng_idx,
83+
prng_state *prng,
8484
int hash_idx, int padding,
8585
const rsa_key *key);
8686

@@ -93,7 +93,7 @@ int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen
9393
int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
9494
unsigned char *out, unsigned long *outlen,
9595
int padding,
96-
prng_state *prng, int prng_idx,
96+
prng_state *prng,
9797
int hash_idx, unsigned long saltlen,
9898
const rsa_key *key);
9999

@@ -149,7 +149,7 @@ int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh
149149
int dh_set_pg_groupsize(int groupsize, dh_key *key);
150150

151151
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
152-
int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
152+
int dh_generate_key(prng_state *prng, dh_key *key);
153153

154154
int dh_shared_secret(const dh_key *private_key, const dh_key *public_key,
155155
unsigned char *out, unsigned long *outlen);
@@ -264,13 +264,13 @@ int ecc_get_size(const ecc_key *key);
264264

265265
int ecc_find_curve(const char* name_or_oid, const ltc_ecc_curve** cu);
266266
int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key);
267-
int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
267+
int ecc_generate_key(prng_state *prng, ecc_key *key);
268268
int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
269269
int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
270270
int ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key);
271271

272-
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
273-
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu);
272+
int ecc_make_key(prng_state *prng, int keysize, ecc_key *key);
273+
int ecc_make_key_ex(prng_state *prng, ecc_key *key, const ltc_ecc_curve *cu);
274274
void ecc_free(ecc_key *key);
275275

276276
int ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
@@ -291,18 +291,18 @@ int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
291291

292292
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
293293
unsigned char *out, unsigned long *outlen,
294-
prng_state *prng, int wprng, int hash,
294+
prng_state *prng, int hash,
295295
const ecc_key *key);
296296

297297
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
298298
unsigned char *out, unsigned long *outlen,
299299
const ecc_key *key);
300300

301-
#define ecc_sign_hash_rfc7518(in_, inlen_, out_, outlen_, prng_, wprng_, key_) \
302-
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, wprng_, LTC_ECCSIG_RFC7518, NULL, key_)
301+
#define ecc_sign_hash_rfc7518(in_, inlen_, out_, outlen_, prng_, key_) \
302+
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, LTC_ECCSIG_RFC7518, NULL, key_)
303303

304-
#define ecc_sign_hash(in_, inlen_, out_, outlen_, prng_, wprng_, key_) \
305-
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, wprng_, LTC_ECCSIG_ANSIX962, NULL, key_)
304+
#define ecc_sign_hash(in_, inlen_, out_, outlen_, prng_, key_) \
305+
ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, LTC_ECCSIG_ANSIX962, NULL, key_)
306306

307307
#define ecc_verify_hash_rfc7518(sig_, siglen_, hash_, hashlen_, stat_, key_) \
308308
ecc_verify_hash_ex(sig_, siglen_, hash_, hashlen_, LTC_ECCSIG_RFC7518, stat_, key_)
@@ -312,7 +312,7 @@ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
312312

313313
int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen,
314314
unsigned char *out, unsigned long *outlen,
315-
prng_state *prng, int wprng, ecc_signature_type sigformat,
315+
prng_state *prng, ecc_signature_type sigformat,
316316
int *recid, const ecc_key *key);
317317

318318
int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
@@ -347,7 +347,7 @@ typedef struct {
347347

348348

349349
/** Ed25519 Signature API */
350-
int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
350+
int ed25519_make_key(prng_state *prng, curve25519_key *key);
351351

352352
int ed25519_export( unsigned char *out, unsigned long *outlen,
353353
int which,
@@ -369,7 +369,7 @@ int ed25519_verify(const unsigned char *msg, unsigned long msglen,
369369
int *stat, const curve25519_key *public_key);
370370

371371
/** X25519 Key-Exchange API */
372-
int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
372+
int x25519_make_key(prng_state *prng, curve25519_key *key);
373373

374374
int x25519_export( unsigned char *out, unsigned long *outlen,
375375
int which,
@@ -420,27 +420,27 @@ typedef struct {
420420
void *y;
421421
} dsa_key;
422422

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

425425
int dsa_set_pqg(const unsigned char *p, unsigned long plen,
426426
const unsigned char *q, unsigned long qlen,
427427
const unsigned char *g, unsigned long glen,
428428
dsa_key *key);
429429
int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
430-
int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
430+
int dsa_generate_pqg(prng_state *prng, int group_size, int modulus_size, dsa_key *key);
431431

432432
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
433-
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
433+
int dsa_generate_key(prng_state *prng, dsa_key *key);
434434

435435
void dsa_free(dsa_key *key);
436436

437437
int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
438-
void *r, void *s,
439-
prng_state *prng, int wprng, const dsa_key *key);
438+
void *r, void *s,
439+
prng_state *prng, const dsa_key *key);
440440

441441
int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
442442
unsigned char *out, unsigned long *outlen,
443-
prng_state *prng, int wprng, const dsa_key *key);
443+
prng_state *prng, const dsa_key *key);
444444

445445
int dsa_verify_hash_raw( void *r, void *s,
446446
const unsigned char *hash, unsigned long hashlen,
@@ -452,7 +452,7 @@ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
452452

453453
int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
454454
unsigned char *out, unsigned long *outlen,
455-
prng_state *prng, int wprng, int hash,
455+
prng_state *prng, int hash,
456456
const dsa_key *key);
457457

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

0 commit comments

Comments
 (0)