Mercurial > dropbear
comparison libtomcrypt/src/prngs/sober128.c @ 1478:3a933956437e coverity
update coverity
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 23:49:22 +0800 |
parents | 6dba84798cd5 |
children | e9dba7abd939 |
comparison
equal
deleted
inserted
replaced
1439:8d24733026c5 | 1478:3a933956437e |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
9 | |
11 #include "tomcrypt.h" | 10 #include "tomcrypt.h" |
12 | 11 |
13 /** | 12 /** |
14 @file sober128.c | 13 @file prngs/sober128.c |
15 Implementation of SOBER-128 by Tom St Denis. | 14 Implementation of SOBER-128 by Tom St Denis. |
16 Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM. | 15 Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM. |
17 */ | 16 */ |
18 | 17 |
19 #ifdef LTC_SOBER128 | 18 #ifdef LTC_SOBER128 |
20 | 19 |
21 #include "sober128tab.c" | 20 const struct ltc_prng_descriptor sober128_desc = |
22 | 21 { |
23 const struct ltc_prng_descriptor sober128_desc = | 22 "sober128", |
24 { | 23 40, |
25 "sober128", 64, | 24 &sober128_start, |
26 &sober128_start, | 25 &sober128_add_entropy, |
27 &sober128_add_entropy, | 26 &sober128_ready, |
28 &sober128_ready, | 27 &sober128_read, |
29 &sober128_read, | 28 &sober128_done, |
30 &sober128_done, | 29 &sober128_export, |
31 &sober128_export, | 30 &sober128_import, |
32 &sober128_import, | 31 &sober128_test |
33 &sober128_test | |
34 }; | 32 }; |
35 | |
36 /* don't change these... */ | |
37 #define N 17 | |
38 #define FOLD N /* how many iterations of folding to do */ | |
39 #define INITKONST 0x6996c53a /* value of KONST to use during key loading */ | |
40 #define KEYP 15 /* where to insert key words */ | |
41 #define FOLDP 4 /* where to insert non-linear feedback */ | |
42 | |
43 #define B(x,i) ((unsigned char)(((x) >> (8*i)) & 0xFF)) | |
44 | |
45 static ulong32 BYTE2WORD(unsigned char *b) | |
46 { | |
47 ulong32 t; | |
48 LOAD32L(t, b); | |
49 return t; | |
50 } | |
51 | |
52 #define WORD2BYTE(w, b) STORE32L(b, w) | |
53 | |
54 static void XORWORD(ulong32 w, unsigned char *b) | |
55 { | |
56 ulong32 t; | |
57 LOAD32L(t, b); | |
58 t ^= w; | |
59 STORE32L(t, b); | |
60 } | |
61 | |
62 /* give correct offset for the current position of the register, | |
63 * where logically R[0] is at position "zero". | |
64 */ | |
65 #define OFF(zero, i) (((zero)+(i)) % N) | |
66 | |
67 /* step the LFSR */ | |
68 /* After stepping, "zero" moves right one place */ | |
69 #define STEP(R,z) \ | |
70 R[OFF(z,0)] = R[OFF(z,15)] ^ R[OFF(z,4)] ^ (R[OFF(z,0)] << 8) ^ Multab[(R[OFF(z,0)] >> 24) & 0xFF]; | |
71 | |
72 static void cycle(ulong32 *R) | |
73 { | |
74 ulong32 t; | |
75 int i; | |
76 | |
77 STEP(R,0); | |
78 t = R[0]; | |
79 for (i = 1; i < N; ++i) { | |
80 R[i-1] = R[i]; | |
81 } | |
82 R[N-1] = t; | |
83 } | |
84 | |
85 /* Return a non-linear function of some parts of the register. | |
86 */ | |
87 #define NLFUNC(c,z) \ | |
88 { \ | |
89 t = c->R[OFF(z,0)] + c->R[OFF(z,16)]; \ | |
90 t ^= Sbox[(t >> 24) & 0xFF]; \ | |
91 t = RORc(t, 8); \ | |
92 t = ((t + c->R[OFF(z,1)]) ^ c->konst) + c->R[OFF(z,6)]; \ | |
93 t ^= Sbox[(t >> 24) & 0xFF]; \ | |
94 t = t + c->R[OFF(z,13)]; \ | |
95 } | |
96 | |
97 static ulong32 nltap(struct sober128_prng *c) | |
98 { | |
99 ulong32 t; | |
100 NLFUNC(c, 0); | |
101 return t; | |
102 } | |
103 | 33 |
104 /** | 34 /** |
105 Start the PRNG | 35 Start the PRNG |
106 @param prng [out] The PRNG state to initialize | 36 @param prng [out] The PRNG state to initialize |
107 @return CRYPT_OK if successful | 37 @return CRYPT_OK if successful |
108 */ | 38 */ |
109 int sober128_start(prng_state *prng) | 39 int sober128_start(prng_state *prng) |
110 { | 40 { |
111 int i; | 41 LTC_ARGCHK(prng != NULL); |
112 struct sober128_prng *c; | 42 prng->ready = 0; |
113 | 43 XMEMSET(&prng->sober128.ent, 0, sizeof(prng->sober128.ent)); |
114 LTC_ARGCHK(prng != NULL); | 44 prng->sober128.idx = 0; |
115 | 45 LTC_MUTEX_INIT(&prng->lock) |
116 c = &(prng->sober128); | 46 return CRYPT_OK; |
117 | |
118 /* Register initialised to Fibonacci numbers */ | |
119 c->R[0] = 1; | |
120 c->R[1] = 1; | |
121 for (i = 2; i < N; ++i) { | |
122 c->R[i] = c->R[i-1] + c->R[i-2]; | |
123 } | |
124 c->konst = INITKONST; | |
125 | |
126 /* next add_entropy will be the key */ | |
127 c->flag = 1; | |
128 c->set = 0; | |
129 | |
130 return CRYPT_OK; | |
131 } | |
132 | |
133 /* Save the current register state | |
134 */ | |
135 static void s128_savestate(struct sober128_prng *c) | |
136 { | |
137 int i; | |
138 for (i = 0; i < N; ++i) { | |
139 c->initR[i] = c->R[i]; | |
140 } | |
141 } | |
142 | |
143 /* initialise to previously saved register state | |
144 */ | |
145 static void s128_reloadstate(struct sober128_prng *c) | |
146 { | |
147 int i; | |
148 | |
149 for (i = 0; i < N; ++i) { | |
150 c->R[i] = c->initR[i]; | |
151 } | |
152 } | |
153 | |
154 /* Initialise "konst" | |
155 */ | |
156 static void s128_genkonst(struct sober128_prng *c) | |
157 { | |
158 ulong32 newkonst; | |
159 | |
160 do { | |
161 cycle(c->R); | |
162 newkonst = nltap(c); | |
163 } while ((newkonst & 0xFF000000) == 0); | |
164 c->konst = newkonst; | |
165 } | |
166 | |
167 /* Load key material into the register | |
168 */ | |
169 #define ADDKEY(k) \ | |
170 c->R[KEYP] += (k); | |
171 | |
172 #define XORNL(nl) \ | |
173 c->R[FOLDP] ^= (nl); | |
174 | |
175 /* nonlinear diffusion of register for key */ | |
176 #define DROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); c->R[OFF((z+1),FOLDP)] ^= t; | |
177 static void s128_diffuse(struct sober128_prng *c) | |
178 { | |
179 ulong32 t; | |
180 /* relies on FOLD == N == 17! */ | |
181 DROUND(0); | |
182 DROUND(1); | |
183 DROUND(2); | |
184 DROUND(3); | |
185 DROUND(4); | |
186 DROUND(5); | |
187 DROUND(6); | |
188 DROUND(7); | |
189 DROUND(8); | |
190 DROUND(9); | |
191 DROUND(10); | |
192 DROUND(11); | |
193 DROUND(12); | |
194 DROUND(13); | |
195 DROUND(14); | |
196 DROUND(15); | |
197 DROUND(16); | |
198 } | 47 } |
199 | 48 |
200 /** | 49 /** |
201 Add entropy to the PRNG state | 50 Add entropy to the PRNG state |
202 @param in The data to add | 51 @param in The data to add |
203 @param inlen Length of the data to add | 52 @param inlen Length of the data to add |
204 @param prng PRNG state to update | 53 @param prng PRNG state to update |
205 @return CRYPT_OK if successful | 54 @return CRYPT_OK if successful |
206 */ | 55 */ |
207 int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) | 56 int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) |
208 { | 57 { |
209 struct sober128_prng *c; | 58 unsigned char buf[40]; |
210 ulong32 i, k; | 59 unsigned long i; |
211 | 60 int err; |
212 LTC_ARGCHK(in != NULL); | 61 |
213 LTC_ARGCHK(prng != NULL); | 62 LTC_ARGCHK(prng != NULL); |
214 c = &(prng->sober128); | 63 LTC_ARGCHK(in != NULL); |
215 | 64 LTC_ARGCHK(inlen > 0); |
216 if (c->flag == 1) { | 65 |
217 /* this is the first call to the add_entropy so this input is the key */ | 66 LTC_MUTEX_LOCK(&prng->lock); |
218 /* inlen must be multiple of 4 bytes */ | 67 if (prng->ready) { |
219 if ((inlen & 3) != 0) { | 68 /* sober128_ready() was already called, do "rekey" operation */ |
220 return CRYPT_INVALID_KEYSIZE; | 69 if ((err = sober128_stream_keystream(&prng->sober128.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK; |
221 } | 70 for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i]; |
222 | 71 /* key 32 bytes, 20 rounds */ |
223 for (i = 0; i < inlen; i += 4) { | 72 if ((err = sober128_stream_setup(&prng->sober128.s, buf, 32)) != CRYPT_OK) goto LBL_UNLOCK; |
224 k = BYTE2WORD((unsigned char *)&in[i]); | 73 /* iv 8 bytes */ |
225 ADDKEY(k); | 74 if ((err = sober128_stream_setiv(&prng->sober128.s, buf + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK; |
226 cycle(c->R); | 75 /* clear KEY + IV */ |
227 XORNL(nltap(c)); | 76 zeromem(buf, sizeof(buf)); |
228 } | 77 } |
229 | 78 else { |
230 /* also fold in the length of the key */ | 79 /* sober128_ready() was not called yet, add entropy to ent buffer */ |
231 ADDKEY(inlen); | 80 while (inlen--) prng->sober128.ent[prng->sober128.idx++ % sizeof(prng->sober128.ent)] ^= *in++; |
232 | 81 } |
233 /* now diffuse */ | 82 err = CRYPT_OK; |
234 s128_diffuse(c); | 83 LBL_UNLOCK: |
235 | 84 LTC_MUTEX_UNLOCK(&prng->lock); |
236 s128_genkonst(c); | 85 return err; |
237 s128_savestate(c); | |
238 c->nbuf = 0; | |
239 c->flag = 0; | |
240 c->set = 1; | |
241 } else { | |
242 /* ok we are adding an IV then... */ | |
243 s128_reloadstate(c); | |
244 | |
245 /* inlen must be multiple of 4 bytes */ | |
246 if ((inlen & 3) != 0) { | |
247 return CRYPT_INVALID_KEYSIZE; | |
248 } | |
249 | |
250 for (i = 0; i < inlen; i += 4) { | |
251 k = BYTE2WORD((unsigned char *)&in[i]); | |
252 ADDKEY(k); | |
253 cycle(c->R); | |
254 XORNL(nltap(c)); | |
255 } | |
256 | |
257 /* also fold in the length of the key */ | |
258 ADDKEY(inlen); | |
259 | |
260 /* now diffuse */ | |
261 s128_diffuse(c); | |
262 c->nbuf = 0; | |
263 } | |
264 | |
265 return CRYPT_OK; | |
266 } | 86 } |
267 | 87 |
268 /** | 88 /** |
269 Make the PRNG ready to read from | 89 Make the PRNG ready to read from |
270 @param prng The PRNG to make active | 90 @param prng The PRNG to make active |
271 @return CRYPT_OK if successful | 91 @return CRYPT_OK if successful |
272 */ | 92 */ |
273 int sober128_ready(prng_state *prng) | 93 int sober128_ready(prng_state *prng) |
274 { | 94 { |
275 return prng->sober128.set == 1 ? CRYPT_OK : CRYPT_ERROR; | 95 int err; |
276 } | 96 |
277 | 97 LTC_ARGCHK(prng != NULL); |
278 /* XOR pseudo-random bytes into buffer | 98 |
279 */ | 99 LTC_MUTEX_LOCK(&prng->lock); |
280 #define SROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); XORWORD(t, out+(z*4)); | 100 if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; } |
101 /* key 32 bytes, 20 rounds */ | |
102 if ((err = sober128_stream_setup(&prng->sober128.s, prng->sober128.ent, 32)) != CRYPT_OK) goto LBL_UNLOCK; | |
103 /* iv 8 bytes */ | |
104 if ((err = sober128_stream_setiv(&prng->sober128.s, prng->sober128.ent + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK; | |
105 XMEMSET(&prng->sober128.ent, 0, sizeof(prng->sober128.ent)); | |
106 prng->sober128.idx = 0; | |
107 prng->ready = 1; | |
108 LBL_UNLOCK: | |
109 LTC_MUTEX_UNLOCK(&prng->lock); | |
110 return err; | |
111 } | |
281 | 112 |
282 /** | 113 /** |
283 Read from the PRNG | 114 Read from the PRNG |
284 @param out Destination | 115 @param out Destination |
285 @param outlen Length of output | 116 @param outlen Length of output |
286 @param prng The active PRNG to read from | 117 @param prng The active PRNG to read from |
287 @return Number of octets read | 118 @return Number of octets read |
288 */ | 119 */ |
289 unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng) | 120 unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng) |
290 { | 121 { |
291 struct sober128_prng *c; | 122 if (outlen == 0 || prng == NULL || out == NULL) return 0; |
292 ulong32 t, tlen; | 123 LTC_MUTEX_LOCK(&prng->lock); |
293 | 124 if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; } |
294 LTC_ARGCHK(out != NULL); | 125 if (sober128_stream_keystream(&prng->sober128.s, out, outlen) != CRYPT_OK) outlen = 0; |
295 LTC_ARGCHK(prng != NULL); | 126 LBL_UNLOCK: |
296 | 127 LTC_MUTEX_UNLOCK(&prng->lock); |
297 #ifdef LTC_VALGRIND | 128 return outlen; |
298 zeromem(out, outlen); | |
299 #endif | |
300 | |
301 c = &(prng->sober128); | |
302 t = 0; | |
303 tlen = outlen; | |
304 | |
305 /* handle any previously buffered bytes */ | |
306 while (c->nbuf != 0 && outlen != 0) { | |
307 *out++ ^= c->sbuf & 0xFF; | |
308 c->sbuf >>= 8; | |
309 c->nbuf -= 8; | |
310 --outlen; | |
311 } | |
312 | |
313 #ifndef LTC_SMALL_CODE | |
314 /* do lots at a time, if there's enough to do */ | |
315 while (outlen >= N*4) { | |
316 SROUND(0); | |
317 SROUND(1); | |
318 SROUND(2); | |
319 SROUND(3); | |
320 SROUND(4); | |
321 SROUND(5); | |
322 SROUND(6); | |
323 SROUND(7); | |
324 SROUND(8); | |
325 SROUND(9); | |
326 SROUND(10); | |
327 SROUND(11); | |
328 SROUND(12); | |
329 SROUND(13); | |
330 SROUND(14); | |
331 SROUND(15); | |
332 SROUND(16); | |
333 out += 4*N; | |
334 outlen -= 4*N; | |
335 } | |
336 #endif | |
337 | |
338 /* do small or odd size buffers the slow way */ | |
339 while (4 <= outlen) { | |
340 cycle(c->R); | |
341 t = nltap(c); | |
342 XORWORD(t, out); | |
343 out += 4; | |
344 outlen -= 4; | |
345 } | |
346 | |
347 /* handle any trailing bytes */ | |
348 if (outlen != 0) { | |
349 cycle(c->R); | |
350 c->sbuf = nltap(c); | |
351 c->nbuf = 32; | |
352 while (c->nbuf != 0 && outlen != 0) { | |
353 *out++ ^= c->sbuf & 0xFF; | |
354 c->sbuf >>= 8; | |
355 c->nbuf -= 8; | |
356 --outlen; | |
357 } | |
358 } | |
359 | |
360 return tlen; | |
361 } | 129 } |
362 | 130 |
363 /** | 131 /** |
364 Terminate the PRNG | 132 Terminate the PRNG |
365 @param prng The PRNG to terminate | 133 @param prng The PRNG to terminate |
366 @return CRYPT_OK if successful | 134 @return CRYPT_OK if successful |
367 */ | 135 */ |
368 int sober128_done(prng_state *prng) | 136 int sober128_done(prng_state *prng) |
369 { | 137 { |
370 LTC_ARGCHK(prng != NULL); | 138 int err; |
371 return CRYPT_OK; | 139 LTC_ARGCHK(prng != NULL); |
140 LTC_MUTEX_LOCK(&prng->lock); | |
141 prng->ready = 0; | |
142 err = sober128_stream_done(&prng->sober128.s); | |
143 LTC_MUTEX_UNLOCK(&prng->lock); | |
144 LTC_MUTEX_DESTROY(&prng->lock); | |
145 return err; | |
372 } | 146 } |
373 | 147 |
374 /** | 148 /** |
375 Export the PRNG state | 149 Export the PRNG state |
376 @param out [out] Destination | 150 @param out [out] Destination |
377 @param outlen [in/out] Max size and resulting size of the state | 151 @param outlen [in/out] Max size and resulting size of the state |
378 @param prng The PRNG to export | 152 @param prng The PRNG to export |
379 @return CRYPT_OK if successful | 153 @return CRYPT_OK if successful |
380 */ | 154 */ |
381 int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng) | 155 int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng) |
382 { | 156 { |
157 unsigned long len = sober128_desc.export_size; | |
158 | |
159 LTC_ARGCHK(prng != NULL); | |
160 LTC_ARGCHK(out != NULL); | |
383 LTC_ARGCHK(outlen != NULL); | 161 LTC_ARGCHK(outlen != NULL); |
384 LTC_ARGCHK(out != NULL); | 162 |
385 LTC_ARGCHK(prng != NULL); | 163 if (*outlen < len) { |
386 | 164 *outlen = len; |
387 if (*outlen < 64) { | |
388 *outlen = 64; | |
389 return CRYPT_BUFFER_OVERFLOW; | 165 return CRYPT_BUFFER_OVERFLOW; |
390 } | 166 } |
391 | 167 |
392 if (sober128_read(out, 64, prng) != 64) { | 168 if (sober128_read(out, len, prng) != len) { |
393 return CRYPT_ERROR_READPRNG; | 169 return CRYPT_ERROR_READPRNG; |
394 } | 170 } |
395 *outlen = 64; | 171 |
396 | 172 *outlen = len; |
397 return CRYPT_OK; | 173 return CRYPT_OK; |
398 } | 174 } |
399 | 175 |
400 /** | 176 /** |
401 Import a PRNG state | 177 Import a PRNG state |
402 @param in The PRNG state | 178 @param in The PRNG state |
403 @param inlen Size of the state | 179 @param inlen Size of the state |
404 @param prng The PRNG to import | 180 @param prng The PRNG to import |
405 @return CRYPT_OK if successful | 181 @return CRYPT_OK if successful |
406 */ | 182 */ |
407 int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng) | 183 int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng) |
408 { | 184 { |
409 int err; | 185 int err; |
186 | |
187 LTC_ARGCHK(prng != NULL); | |
410 LTC_ARGCHK(in != NULL); | 188 LTC_ARGCHK(in != NULL); |
411 LTC_ARGCHK(prng != NULL); | 189 if (inlen < (unsigned long)sober128_desc.export_size) return CRYPT_INVALID_ARG; |
412 | 190 |
413 if (inlen != 64) { | 191 if ((err = sober128_start(prng)) != CRYPT_OK) return err; |
414 return CRYPT_INVALID_ARG; | 192 if ((err = sober128_add_entropy(in, sober128_desc.export_size, prng)) != CRYPT_OK) return err; |
415 } | 193 return CRYPT_OK; |
416 | |
417 if ((err = sober128_start(prng)) != CRYPT_OK) { | |
418 return err; | |
419 } | |
420 if ((err = sober128_add_entropy(in, 64, prng)) != CRYPT_OK) { | |
421 return err; | |
422 } | |
423 return sober128_ready(prng); | |
424 } | 194 } |
425 | 195 |
426 /** | 196 /** |
427 PRNG self-test | 197 PRNG self-test |
428 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled | 198 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled |
429 */ | 199 */ |
430 int sober128_test(void) | 200 int sober128_test(void) |
431 { | 201 { |
432 #ifndef LTC_TEST | 202 #ifndef LTC_TEST |
433 return CRYPT_NOP; | 203 return CRYPT_NOP; |
434 #else | 204 #else |
435 static const struct { | 205 prng_state st; |
436 int keylen, ivlen, len; | 206 unsigned char en[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, |
437 unsigned char key[16], iv[4], out[20]; | 207 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, |
438 } tests[] = { | 208 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, |
439 | 209 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
440 { | 210 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32 }; |
441 16, 4, 20, | 211 unsigned char dmp[300]; |
442 | 212 unsigned long dmplen = sizeof(dmp); |
443 /* key */ | 213 unsigned char out[500]; |
444 { 0x74, 0x65, 0x73, 0x74, 0x20, 0x6b, 0x65, 0x79, | 214 unsigned char t1[] = { 0x31, 0x82, 0xA7, 0xA5, 0x8B, 0xD7, 0xCB, 0x39, 0x86, 0x1A }; |
445 0x20, 0x31, 0x32, 0x38, 0x62, 0x69, 0x74, 0x73 }, | 215 unsigned char t2[] = { 0x6B, 0x43, 0x9E, 0xBC, 0xE7, 0x62, 0x9B, 0xE6, 0x9B, 0x83 }; |
446 | 216 unsigned char t3[] = { 0x4A, 0x0E, 0x6C, 0xC1, 0xCF, 0xB4, 0x73, 0x49, 0x99, 0x05 }; |
447 /* IV */ | 217 int err; |
448 { 0x00, 0x00, 0x00, 0x00 }, | 218 |
449 | 219 if ((err = sober128_start(&st)) != CRYPT_OK) return err; |
450 /* expected output */ | 220 /* add entropy to uninitialized prng */ |
451 { 0x43, 0x50, 0x0c, 0xcf, 0x89, 0x91, 0x9f, 0x1d, | 221 if ((err = sober128_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err; |
452 0xaa, 0x37, 0x74, 0x95, 0xf4, 0xb4, 0x58, 0xc2, | 222 if ((err = sober128_ready(&st)) != CRYPT_OK) return err; |
453 0x40, 0x37, 0x8b, 0xbb } | 223 if (sober128_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */ |
454 } | 224 if (compare_testvector(out, 10, t1, sizeof(t1), "SOBER128-PRNG", 1)) return CRYPT_FAIL_TESTVECTOR; |
455 | 225 if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */ |
456 }; | 226 /* add entropy to already initialized prng */ |
457 prng_state prng; | 227 if ((err = sober128_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err; |
458 unsigned char dst[20]; | 228 if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */ |
459 int err, x; | 229 if ((err = sober128_export(dmp, &dmplen, &st)) != CRYPT_OK) return err; |
460 | 230 if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */ |
461 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { | 231 if (sober128_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */ |
462 if ((err = sober128_start(&prng)) != CRYPT_OK) { | 232 if (compare_testvector(out, 10, t2, sizeof(t2), "SOBER128-PRNG", 2)) return CRYPT_FAIL_TESTVECTOR; |
463 return err; | 233 if ((err = sober128_done(&st)) != CRYPT_OK) return err; |
464 } | 234 if ((err = sober128_import(dmp, dmplen, &st)) != CRYPT_OK) return err; |
465 if ((err = sober128_add_entropy(tests[x].key, tests[x].keylen, &prng)) != CRYPT_OK) { | 235 if ((err = sober128_ready(&st)) != CRYPT_OK) return err; |
466 return err; | 236 if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */ |
467 } | 237 if (sober128_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */ |
468 /* add IV */ | 238 if (compare_testvector(out, 10, t3, sizeof(t3), "SOBER128-PRNG", 3)) return CRYPT_FAIL_TESTVECTOR; |
469 if ((err = sober128_add_entropy(tests[x].iv, tests[x].ivlen, &prng)) != CRYPT_OK) { | 239 if ((err = sober128_done(&st)) != CRYPT_OK) return err; |
470 return err; | 240 |
471 } | 241 return CRYPT_OK; |
472 | |
473 /* ready up */ | |
474 if ((err = sober128_ready(&prng)) != CRYPT_OK) { | |
475 return err; | |
476 } | |
477 XMEMSET(dst, 0, tests[x].len); | |
478 if (sober128_read(dst, tests[x].len, &prng) != (unsigned long)tests[x].len) { | |
479 return CRYPT_ERROR_READPRNG; | |
480 } | |
481 sober128_done(&prng); | |
482 if (XMEMCMP(dst, tests[x].out, tests[x].len)) { | |
483 #if 0 | |
484 printf("\n\nLTC_SOBER128 failed, I got:\n"); | |
485 for (y = 0; y < tests[x].len; y++) printf("%02x ", dst[y]); | |
486 printf("\n"); | |
487 #endif | 242 #endif |
488 return CRYPT_FAIL_TESTVECTOR; | 243 } |
489 } | 244 |
490 } | |
491 return CRYPT_OK; | |
492 #endif | 245 #endif |
493 } | 246 |
494 | 247 /* ref: $Format:%D$ */ |
495 #endif | 248 /* git commit: $Format:%H$ */ |
496 | 249 /* commit time: $Format:%ai$ */ |
497 | |
498 /* $Source$ */ | |
499 /* $Revision$ */ | |
500 /* $Date$ */ |