comparison libtomcrypt/tests/no_prng.c @ 1511:5916af64acd4 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Sat, 17 Feb 2018 19:29:51 +0800
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1457:32f990cc96b1 1511:5916af64acd4
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.h"
10
11 /**
12 @file no_prng.c
13 NO PRNG, Steffen Jaeckel
14 */
15
16 #ifdef LTC_PKCS_1
17
18 typedef struct
19 {
20 struct ltc_prng_descriptor desc;
21 char name[64];
22 unsigned char entropy[1024];
23 unsigned long len;
24 unsigned long offset;
25 } no_prng_desc_t;
26
27 /**
28 Start the PRNG
29 @param prng [out] The PRNG state to initialize
30 @return CRYPT_OK if successful
31 */
32 int no_prng_start(prng_state *prng)
33 {
34 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
35 LTC_ARGCHK(no_prng != NULL);
36 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
37 no_prng->len = 0;
38 no_prng->offset = 0;
39
40 return CRYPT_OK;
41 }
42
43 /**
44 Add entropy to the PRNG state
45 @param in The data to add
46 @param inlen Length of the data to add
47 @param prng PRNG state to update
48 @return CRYPT_OK if successful
49 */
50 int no_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
51 {
52 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
53 LTC_ARGCHK(no_prng != NULL);
54 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
55 LTC_ARGCHK(in != NULL);
56 LTC_ARGCHK(inlen <= sizeof(no_prng->entropy));
57
58 no_prng->len = MIN(inlen, sizeof(no_prng->entropy));
59 memcpy(no_prng->entropy, in, no_prng->len);
60 no_prng->offset = 0;
61
62 return CRYPT_OK;
63
64 }
65
66 /**
67 Make the PRNG ready to read from
68 @param prng The PRNG to make active
69 @return CRYPT_OK if successful
70 */
71 int no_prng_ready(prng_state *prng)
72 {
73 LTC_ARGCHK(prng != NULL);
74
75 return CRYPT_OK;
76 }
77
78 /**
79 Read from the PRNG
80 @param out Destination
81 @param outlen Length of output
82 @param prng The active PRNG to read from
83 @return Number of octets read
84 */
85 unsigned long no_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
86 {
87 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
88 LTC_ARGCHK(no_prng != NULL);
89 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
90 LTC_ARGCHK(out != NULL);
91
92 outlen = MIN(outlen, no_prng->len - no_prng->offset);
93 memcpy(out, &no_prng->entropy[no_prng->offset], outlen);
94 no_prng->offset += outlen;
95
96 return outlen;
97 }
98
99 /**
100 Terminate the PRNG
101 @param prng The PRNG to terminate
102 @return CRYPT_OK if successful
103 */
104 int no_prng_done(prng_state *prng)
105 {
106 LTC_UNUSED_PARAM(prng);
107 return CRYPT_OK;
108 }
109
110 /**
111 Export the PRNG state
112 @param out [out] Destination
113 @param outlen [in/out] Max size and resulting size of the state
114 @param prng The PRNG to export
115 @return CRYPT_OK if successful
116 */
117 int no_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
118 {
119 LTC_UNUSED_PARAM(out);
120 LTC_UNUSED_PARAM(outlen);
121 LTC_UNUSED_PARAM(prng);
122 return CRYPT_OK;
123 }
124
125 /**
126 Import a PRNG state
127 @param in The PRNG state
128 @param inlen Size of the state
129 @param prng The PRNG to import
130 @return CRYPT_OK if successful
131 */
132 int no_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
133 {
134 LTC_UNUSED_PARAM(in);
135 LTC_UNUSED_PARAM(inlen);
136 LTC_UNUSED_PARAM(prng);
137 return CRYPT_OK;
138 }
139
140 /**
141 PRNG self-test
142 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
143 */
144 int no_prng_test(void)
145 {
146 return CRYPT_OK;
147 }
148
149 static const struct ltc_prng_descriptor no_prng_desc =
150 {
151 NULL, 0,
152 &no_prng_start,
153 &no_prng_add_entropy,
154 &no_prng_ready,
155 &no_prng_read,
156 &no_prng_done,
157 &no_prng_export,
158 &no_prng_import,
159 &no_prng_test
160 };
161
162 struct ltc_prng_descriptor* no_prng_desc_get(void)
163 {
164 no_prng_desc_t* no_prng = XMALLOC(sizeof(*no_prng));
165 LTC_ARGCHK(no_prng != NULL);
166 XMEMCPY(&no_prng->desc, &no_prng_desc, sizeof(no_prng_desc));
167 LTC_ARGCHK(snprintf(no_prng->name, sizeof(no_prng->name), "no_prng@%p", no_prng) < (int)sizeof(no_prng->name));
168 no_prng->desc.name = no_prng->name;
169 return &no_prng->desc;
170 }
171
172 void no_prng_desc_free(struct ltc_prng_descriptor* prng)
173 {
174 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
175 LTC_ARGCHK(no_prng != NULL);
176 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
177 XFREE(no_prng);
178 }
179
180 #endif
181
182
183 /* ref: $Format:%D$ */
184 /* git commit: $Format:%H$ */
185 /* commit time: $Format:%ai$ */