comparison libtomcrypt/tests/prng_test.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9 #include <tomcrypt_test.h>
10
11 #ifdef LTC_PRNG_ENABLE_LTC_RNG
12
13 static unsigned long my_test_rng_read;
14
15 static unsigned long my_test_rng(unsigned char *buf, unsigned long len,
16 void (*callback)(void))
17 {
18 unsigned long n;
19 LTC_UNUSED_PARAM(callback);
20 for (n = 0; n < len; ++n) {
21 buf[n] = 4;
22 }
23 my_test_rng_read += n;
24 return n;
25 }
26
27 #endif
28
29 int prng_test(void)
30 {
31 int err = CRYPT_NOP;
32 int x;
33 unsigned char buf[4096] = { 0 };
34 unsigned long n, one;
35 prng_state nprng;
36
37 #ifdef LTC_PRNG_ENABLE_LTC_RNG
38 unsigned long before;
39
40 unsigned long (*previous)(unsigned char *, unsigned long , void (*)(void)) = ltc_rng;
41 ltc_rng = my_test_rng;
42
43 before = my_test_rng_read;
44
45 if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
46 fprintf(stderr, "rng_make_prng with 'my_test_rng' failed: %s\n", error_to_string(err));
47 exit(EXIT_FAILURE);
48 }
49
50 if (before == my_test_rng_read) {
51 fprintf(stderr, "somehow there was no read from the ltc_rng! %lu == %lu\n", before, my_test_rng_read);
52 exit(EXIT_FAILURE);
53 }
54
55 ltc_rng = previous;
56 #endif
57
58 /* test prngs (test, import/export) */
59 for (x = 0; prng_descriptor[x].name != NULL; x++) {
60 if(strstr(prng_descriptor[x].name, "no_prng") == prng_descriptor[x].name) continue;
61 err = CRYPT_OK;
62 DOX(prng_descriptor[x].test(), prng_descriptor[x].name);
63 DOX(prng_descriptor[x].start(&nprng), prng_descriptor[x].name);
64 DOX(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng), prng_descriptor[x].name);
65 DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
66 n = sizeof(buf);
67 if (strcmp(prng_descriptor[x].name, "sprng")) {
68 one = 1;
69 if (prng_descriptor[x].pexport(buf, &one, &nprng) != CRYPT_BUFFER_OVERFLOW) {
70 fprintf(stderr, "Error testing pexport with a short buffer (%s)\n", prng_descriptor[x].name);
71 return CRYPT_ERROR;
72 }
73 }
74 DOX(prng_descriptor[x].pexport(buf, &n, &nprng), prng_descriptor[x].name);
75 prng_descriptor[x].done(&nprng);
76 DOX(prng_descriptor[x].pimport(buf, n, &nprng), prng_descriptor[x].name);
77 DOX(prng_descriptor[x].pimport(buf, 4096, &nprng), prng_descriptor[x].name); /* try to import larger data */
78 DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
79 if (prng_descriptor[x].read(buf, 100, &nprng) != 100) {
80 fprintf(stderr, "Error reading from imported PRNG (%s)!\n", prng_descriptor[x].name);
81 return CRYPT_ERROR;
82 }
83 prng_descriptor[x].done(&nprng);
84 }
85 return err;
86 }
87
88 /* ref: $Format:%D$ */
89 /* git commit: $Format:%H$ */
90 /* commit time: $Format:%ai$ */