comparison yarrow.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children 7ed585a2c53b
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
13 13
14 #ifdef YARROW 14 #ifdef YARROW
15 15
16 const struct _prng_descriptor yarrow_desc = 16 const struct _prng_descriptor yarrow_desc =
17 { 17 {
18 "yarrow", 18 "yarrow", 64,
19 &yarrow_start, 19 &yarrow_start,
20 &yarrow_add_entropy, 20 &yarrow_add_entropy,
21 &yarrow_ready, 21 &yarrow_ready,
22 &yarrow_read 22 &yarrow_read,
23 &yarrow_done,
24 &yarrow_export,
25 &yarrow_import,
26 &yarrow_test
23 }; 27 };
24 28
25 int yarrow_start(prng_state *prng) 29 int yarrow_start(prng_state *prng)
26 { 30 {
27 int err; 31 int err;
59 prng->yarrow.cipher = register_cipher(&xtea_desc); 63 prng->yarrow.cipher = register_cipher(&xtea_desc);
60 #elif defined(SAFER) 64 #elif defined(SAFER)
61 prng->yarrow.cipher = register_cipher(&safer_sk128_desc); 65 prng->yarrow.cipher = register_cipher(&safer_sk128_desc);
62 #elif defined(DES) 66 #elif defined(DES)
63 prng->yarrow.cipher = register_cipher(&des3_desc); 67 prng->yarrow.cipher = register_cipher(&des3_desc);
64 #elif 68 #else
65 #error YARROW needs at least one CIPHER 69 #error YARROW needs at least one CIPHER
66 #endif 70 #endif
67 if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) { 71 if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
68 return err; 72 return err;
69 } 73 }
112 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { 116 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
113 return err; 117 return err;
114 } 118 }
115 119
116 /* start the hash */ 120 /* start the hash */
117 hash_descriptor[prng->yarrow.hash].init(&md); 121 if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) {
122 return err;
123 }
118 124
119 /* hash the current pool */ 125 /* hash the current pool */
120 if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool, 126 if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool,
121 hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) { 127 hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) {
122 return err; 128 return err;
178 return 0; 184 return 0;
179 } 185 }
180 return len; 186 return len;
181 } 187 }
182 188
183 #endif 189 int yarrow_done(prng_state *prng)
184 190 {
191 _ARGCHK(prng != NULL);
192 /* call cipher done when we invent one ;-) */
193
194 return CRYPT_OK;
195 }
196
197 int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
198 {
199 _ARGCHK(out != NULL);
200 _ARGCHK(outlen != NULL);
201 _ARGCHK(prng != NULL);
202
203 /* we'll write 64 bytes for s&g's */
204 if (*outlen < 64) {
205 return CRYPT_BUFFER_OVERFLOW;
206 }
207
208 if (yarrow_read(out, 64, prng) != 64) {
209 return CRYPT_ERROR_READPRNG;
210 }
211 *outlen = 64;
212
213 return CRYPT_OK;
214 }
215
216 int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
217 {
218 int err;
219
220 _ARGCHK(in != NULL);
221 _ARGCHK(prng != NULL);
222
223 if (inlen != 64) {
224 return CRYPT_INVALID_ARG;
225 }
226
227 if ((err = yarrow_start(prng)) != CRYPT_OK) {
228 return err;
229 }
230 return yarrow_add_entropy(in, 64, prng);
231 }
232
233 int yarrow_test(void)
234 {
235 #ifndef LTC_TEST
236 return CRYPT_NOP;
237 #else
238 int err;
239 prng_state prng;
240
241 if ((err = yarrow_start(&prng)) != CRYPT_OK) {
242 return err;
243 }
244
245 /* now let's test the hash/cipher that was chosen */
246 if ((err = cipher_descriptor[prng.yarrow.cipher].test()) != CRYPT_OK) {
247 return err;
248 }
249 if ((err = hash_descriptor[prng.yarrow.hash].test()) != CRYPT_OK) {
250 return err;
251 }
252
253 yarrow_done(&prng);
254 return CRYPT_OK;
255 #endif
256 }
257
258 #endif
259