Allow reseed interval to be set.
[openssl.git] / fips / rand / fips_drbg_lib.c
1 /* fips/rand/fips_drbg_lib.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #define OPENSSL_FIPSAPI
55
56 #include <string.h>
57 #include <openssl/crypto.h>
58 #include <openssl/err.h>
59 #include <openssl/fips_rand.h>
60 #include "fips_rand_lcl.h"
61
62 /* Support framework for SP800-90 DRBGs */
63
64 int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
65         {
66         int rv;
67         memset(dctx, 0, sizeof(DRBG_CTX));
68         dctx->status = DRBG_STATUS_UNINITIALISED;
69         dctx->flags = flags;
70         dctx->type = type;
71
72         dctx->entropy_blocklen = 0;
73         dctx->health_check_cnt = 0;
74         dctx->health_check_interval = DRBG_HEALTH_INTERVAL;
75
76         rv = fips_drbg_hash_init(dctx);
77
78         if (rv == -2)
79                 rv = fips_drbg_ctr_init(dctx);
80         if (rv == -2)
81                 rv = fips_drbg_hmac_init(dctx);
82         if (rv == -2)
83                 rv = fips_drbg_ec_init(dctx);
84
85         if (rv <= 0)
86                 {
87                 if (rv == -2)
88                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_UNSUPPORTED_DRBG_TYPE);
89                 else
90                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_ERROR_INITIALISING_DRBG);
91                 }
92
93         /* If not in test mode run selftests on DRBG of the same type */
94
95         if (!(dctx->flags & DRBG_FLAG_TEST))
96                 {
97                 DRBG_CTX tctx;
98                 if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
99                         {
100                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
101                         dctx->status = DRBG_STATUS_ERROR;
102                         return 0;
103                         }
104                 }
105
106         return rv;
107         }
108
109 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
110         {
111         DRBG_CTX *dctx;
112         dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
113         if (!dctx)
114                 {
115                 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
116                 return NULL;
117                 }
118
119         if (type == 0)
120                 {
121                 memset(dctx, 0, sizeof(DRBG_CTX));
122                 dctx->type = 0;
123                 dctx->status = DRBG_STATUS_UNINITIALISED;
124                 return dctx;
125                 }
126
127         if (FIPS_drbg_init(dctx, type, flags) <= 0)
128                 {
129                 OPENSSL_free(dctx);
130                 return NULL;
131                 }
132                 
133         return dctx;
134         }
135
136 void FIPS_drbg_free(DRBG_CTX *dctx)
137         {
138         if (dctx->uninstantiate)
139                 dctx->uninstantiate(dctx);
140         OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
141         OPENSSL_free(dctx);
142         }
143
144 static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
145                                 int entropy, size_t min_len, size_t max_len)
146         {
147         unsigned char *tout, *p;
148         size_t bl = dctx->entropy_blocklen, rv;
149         if (dctx->flags & DRBG_FLAG_TEST || !bl)
150                 return dctx->get_entropy(dctx, pout, entropy, min_len, max_len);
151         rv = dctx->get_entropy(dctx, &tout, entropy + bl,
152                                 min_len + bl, max_len + bl);
153         *pout = tout + bl;
154         if (rv < (min_len + bl) || (rv % bl))
155                 return 0;
156         /* Compare consecutive blocks for continuous PRNG test */
157         for (p = tout; p < tout + rv - bl; p += bl)
158                 {
159                 if (!memcmp(p, p + bl, bl))
160                         {
161                         FIPSerr(FIPS_F_FIPS_GET_ENTROPY, FIPS_R_ENTROPY_SOURCE_STUCK);
162                         return 0;
163                         }
164                 }
165         rv -= bl;
166         if (rv > max_len)
167                 return max_len;
168         return rv;
169         }
170
171 static void fips_cleanup_entropy(DRBG_CTX *dctx,
172                                         unsigned char *out, size_t olen)
173         {
174         size_t bl;
175         if (dctx->flags & DRBG_FLAG_TEST)
176                 bl = 0;
177         else
178                 bl = dctx->entropy_blocklen;
179         /* Call cleanup with original arguments */
180         dctx->cleanup_entropy(dctx, out - bl, olen + bl);
181         }
182
183
184 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
185                                 const unsigned char *pers, size_t perslen)
186         {
187         size_t entlen = 0, noncelen = 0;
188         unsigned char *nonce = NULL, *entropy = NULL;
189
190 #if 0
191         /* Put here so error script picks them up */
192         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
193                                 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
194         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
195         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
196         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
197         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
198         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
199 #endif
200
201         int r = 0;
202
203         if (perslen > dctx->max_pers)
204                 {
205                 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
206                 goto end;
207                 }
208
209         if (dctx->status != DRBG_STATUS_UNINITIALISED)
210                 {
211                 if (dctx->status == DRBG_STATUS_ERROR)
212                         r = FIPS_R_IN_ERROR_STATE;
213                 else
214                         r = FIPS_R_ALREADY_INSTANTIATED;
215                 goto end;
216                 }
217
218         dctx->status = DRBG_STATUS_ERROR;
219
220         entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
221                                 dctx->min_entropy, dctx->max_entropy);
222
223         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
224                 {
225                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
226                 goto end;
227                 }
228
229         if (dctx->max_nonce > 0)
230                 {
231                 noncelen = dctx->get_nonce(dctx, &nonce,
232                                         dctx->strength / 2,
233                                         dctx->min_nonce, dctx->max_nonce);
234
235                 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
236                         {
237                         r = FIPS_R_ERROR_RETRIEVING_NONCE;
238                         goto end;
239                         }
240
241                 }
242
243         if (!dctx->instantiate(dctx, 
244                                 entropy, entlen,
245                                 nonce, noncelen,
246                                 pers, perslen))
247                 {
248                 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
249                 goto end;
250                 }
251
252
253         dctx->status = DRBG_STATUS_READY;
254         if (!(dctx->flags & DRBG_CUSTOM_RESEED))
255                 dctx->reseed_counter = 1;
256
257         end:
258
259         if (entropy && dctx->cleanup_entropy)
260                 fips_cleanup_entropy(dctx, entropy, entlen);
261
262         if (nonce && dctx->cleanup_nonce)
263                 dctx->cleanup_nonce(dctx, nonce, noncelen);
264
265         if (dctx->status == DRBG_STATUS_READY)
266                 return 1;
267
268         if (r && !(dctx->flags & DRBG_FLAG_NOERR))
269                 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
270
271         return 0;
272
273         }
274
275 int FIPS_drbg_reseed(DRBG_CTX *dctx,
276                         const unsigned char *adin, size_t adinlen)
277         {
278         unsigned char *entropy = NULL;
279         size_t entlen;
280         int r = 0;
281
282 #if 0
283         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
284         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
285 #endif
286         if (dctx->status != DRBG_STATUS_READY
287                 && dctx->status != DRBG_STATUS_RESEED)
288                 {
289                 if (dctx->status == DRBG_STATUS_ERROR)
290                         r = FIPS_R_IN_ERROR_STATE;
291                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
292                         r = FIPS_R_NOT_INSTANTIATED;
293                 goto end;
294                 }
295
296         if (!adin)
297                 adinlen = 0;
298         else if (adinlen > dctx->max_adin)
299                 {
300                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
301                 goto end;
302                 }
303
304         dctx->status = DRBG_STATUS_ERROR;
305
306         entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
307                                 dctx->min_entropy, dctx->max_entropy);
308
309         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
310                 {
311                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
312                 goto end;
313                 }
314
315         if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
316                 goto end;
317
318         dctx->status = DRBG_STATUS_READY;
319         if (!(dctx->flags & DRBG_CUSTOM_RESEED))
320                 dctx->reseed_counter = 1;
321         end:
322
323         if (entropy && dctx->cleanup_entropy)
324                 fips_cleanup_entropy(dctx, entropy, entlen);
325
326         if (dctx->status == DRBG_STATUS_READY)
327                 return 1;
328
329         if (r && !(dctx->flags & DRBG_FLAG_NOERR))
330                 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
331
332         return 0;
333         }
334
335 static int fips_drbg_check(DRBG_CTX *dctx)
336         {
337         if (dctx->flags & DRBG_FLAG_TEST)
338                 return 1;
339         dctx->health_check_cnt++;
340         if (dctx->health_check_cnt >= dctx->health_check_interval)
341                 {
342                 DRBG_CTX tctx;
343                 if (!fips_drbg_kat(&tctx, dctx->type,
344                                                 dctx->flags | DRBG_FLAG_TEST))
345                         {
346                         FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
347                         dctx->status = DRBG_STATUS_ERROR;
348                         return 0;
349                         }
350                 dctx->health_check_cnt = 0;
351                 }
352         return 1;
353         }
354
355 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
356                         int prediction_resistance,
357                         const unsigned char *adin, size_t adinlen)
358         {
359         int r = 0;
360
361         if (!fips_drbg_check(dctx))
362                 return 0;
363
364         if (dctx->status != DRBG_STATUS_READY
365                 && dctx->status != DRBG_STATUS_RESEED)
366                 {
367                 if (dctx->status == DRBG_STATUS_ERROR)
368                         r = FIPS_R_IN_ERROR_STATE;
369                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
370                         r = FIPS_R_NOT_INSTANTIATED;
371                 goto end;
372                 }
373
374         if (outlen > dctx->max_request)
375                 {
376                 r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG;
377                 return 0;
378                 }
379
380         if (adinlen > dctx->max_adin)
381                 {
382                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
383                 goto end;
384                 }
385
386         if (dctx->flags & DRBG_CUSTOM_RESEED)
387                 dctx->generate(dctx, NULL, outlen, NULL, 0);
388         else if (dctx->reseed_counter >= dctx->reseed_interval)
389                 dctx->status = DRBG_STATUS_RESEED;
390
391         if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
392                 {
393                 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
394                         {
395                         r = FIPS_R_RESEED_ERROR;
396                         goto end;
397                         }
398                 adin = NULL;
399                 adinlen = 0;
400                 }
401
402         if (!dctx->generate(dctx, out, outlen, adin, adinlen))
403                 {
404                 r = FIPS_R_GENERATE_ERROR;
405                 dctx->status = DRBG_STATUS_ERROR;
406                 goto end;
407                 }
408         if (!(dctx->flags & DRBG_CUSTOM_RESEED))
409                 {
410                 if (dctx->reseed_counter >= dctx->reseed_interval)
411                         dctx->status = DRBG_STATUS_RESEED;
412                 else
413                         dctx->reseed_counter++;
414                 }
415
416         end:
417         if (r)
418                 {
419                 if (!(dctx->flags & DRBG_FLAG_NOERR))
420                         FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
421                 return 0;
422                 }
423
424         return 1;
425         }
426
427 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
428         {
429         int rv;
430         if (!dctx->uninstantiate)
431                 rv = 1;
432         else
433                 rv = dctx->uninstantiate(dctx);
434         /* Although we'd like to cleanse here we can't because we have to
435          * test the uninstantiate really zeroes the data.
436          */
437         memset(&dctx->d, 0, sizeof(dctx->d));
438         dctx->status = DRBG_STATUS_UNINITIALISED;
439         /* If method has problems uninstantiating, return error */
440         return rv;
441         }
442
443 int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
444         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
445                                 int entropy, size_t min_len, size_t max_len),
446         void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
447         size_t entropy_blocklen,
448         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
449                                 int entropy, size_t min_len, size_t max_len),
450         void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
451         {
452         if (dctx->status != DRBG_STATUS_UNINITIALISED)
453                 return 0;
454         dctx->entropy_blocklen = entropy_blocklen;
455         dctx->get_entropy = get_entropy;
456         dctx->cleanup_entropy = cleanup_entropy;
457         dctx->get_nonce = get_nonce;
458         dctx->cleanup_nonce = cleanup_nonce;
459         return 1;
460         }
461
462 int FIPS_drbg_set_rand_callbacks(DRBG_CTX *dctx,
463         size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout),
464         void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
465         int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num),
466         int (*rand_add_cb)(DRBG_CTX *ctx,
467                                 const void *buf, int num, double entropy))
468         {
469         if (dctx->status != DRBG_STATUS_UNINITIALISED)
470                 return 0;
471         dctx->get_adin = get_adin;
472         dctx->cleanup_adin = cleanup_adin;
473         dctx->rand_seed_cb = rand_seed_cb;
474         dctx->rand_add_cb = rand_add_cb;
475         return 1;
476         }
477
478 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
479         {
480         return dctx->app_data;
481         }
482
483 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
484         {
485         dctx->app_data = app_data;
486         }
487
488 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
489         {
490         return dctx->blocklength;
491         }
492
493 int FIPS_drbg_get_strength(DRBG_CTX *dctx)
494         {
495         return dctx->strength;
496         }
497
498 void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
499         {
500         dctx->health_check_interval = interval;
501         }
502
503 void FIPS_drbg_set_reseed_interval(DRBG_CTX *dctx, int interval)
504         {
505         dctx->reseed_interval = interval;
506         }
507
508 static int drbg_stick = 0;
509
510 void FIPS_drbg_stick(void)
511         {
512         drbg_stick = 1;
513         }
514
515 /* Continuous DRBG utility function */
516 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
517         {
518         /* No CPRNG in test mode */
519         if (dctx->flags & DRBG_FLAG_TEST)
520                 return 1;
521         /* Check block is valid: should never happen */
522         if (dctx->lb_valid == 0)
523                 {
524                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
525                 fips_set_selftest_fail();
526                 return 0;
527                 }
528         if (drbg_stick)
529                 memcpy(dctx->lb, out, dctx->blocklength);
530         /* Check against last block: fail if match */
531         if (!memcmp(dctx->lb, out, dctx->blocklength))
532                 {
533                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
534                 fips_set_selftest_fail();
535                 return 0;
536                 }
537         /* Save last block for next comparison */
538         memcpy(dctx->lb, out, dctx->blocklength);
539         return 1;
540         }