Perform health check on all reseed operations not associated with
[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->xflags = flags;
70         dctx->type = type;
71
72         dctx->iflags = 0;
73         dctx->entropy_blocklen = 0;
74         dctx->health_check_cnt = 0;
75         dctx->health_check_interval = DRBG_HEALTH_INTERVAL;
76
77         rv = fips_drbg_hash_init(dctx);
78
79         if (rv == -2)
80                 rv = fips_drbg_ctr_init(dctx);
81         if (rv == -2)
82                 rv = fips_drbg_hmac_init(dctx);
83         if (rv == -2)
84                 rv = fips_drbg_ec_init(dctx);
85
86         if (rv <= 0)
87                 {
88                 if (rv == -2)
89                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_UNSUPPORTED_DRBG_TYPE);
90                 else
91                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_ERROR_INITIALISING_DRBG);
92                 }
93
94         /* If not in test mode run selftests on DRBG of the same type */
95
96         if (!(dctx->xflags & DRBG_FLAG_TEST))
97                 {
98                 DRBG_CTX tctx;
99                 if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
100                         {
101                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
102                         dctx->status = DRBG_STATUS_ERROR;
103                         return 0;
104                         }
105                 }
106
107         return rv;
108         }
109
110 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
111         {
112         DRBG_CTX *dctx;
113         dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
114         if (!dctx)
115                 {
116                 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
117                 return NULL;
118                 }
119
120         if (type == 0)
121                 {
122                 memset(dctx, 0, sizeof(DRBG_CTX));
123                 dctx->type = 0;
124                 dctx->status = DRBG_STATUS_UNINITIALISED;
125                 return dctx;
126                 }
127
128         if (FIPS_drbg_init(dctx, type, flags) <= 0)
129                 {
130                 OPENSSL_free(dctx);
131                 return NULL;
132                 }
133                 
134         return dctx;
135         }
136
137 void FIPS_drbg_free(DRBG_CTX *dctx)
138         {
139         if (dctx->uninstantiate)
140                 dctx->uninstantiate(dctx);
141         OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
142         OPENSSL_free(dctx);
143         }
144
145 static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
146                                 int entropy, size_t min_len, size_t max_len)
147         {
148         unsigned char *tout, *p;
149         size_t bl = dctx->entropy_blocklen, rv;
150         if (dctx->xflags & DRBG_FLAG_TEST || !bl)
151                 return dctx->get_entropy(dctx, pout, entropy, min_len, max_len);
152         rv = dctx->get_entropy(dctx, &tout, entropy + bl,
153                                 min_len + bl, max_len + bl);
154         *pout = tout + bl;
155         if (rv < (min_len + bl) || (rv % bl))
156                 return 0;
157         /* Compare consecutive blocks for continuous PRNG test */
158         for (p = tout; p < tout + rv - bl; p += bl)
159                 {
160                 if (!memcmp(p, p + bl, bl))
161                         {
162                         FIPSerr(FIPS_F_FIPS_GET_ENTROPY, FIPS_R_ENTROPY_SOURCE_STUCK);
163                         return 0;
164                         }
165                 }
166         rv -= bl;
167         if (rv > max_len)
168                 return max_len;
169         return rv;
170         }
171
172 static void fips_cleanup_entropy(DRBG_CTX *dctx,
173                                         unsigned char *out, size_t olen)
174         {
175         size_t bl;
176         if (dctx->xflags & DRBG_FLAG_TEST)
177                 bl = 0;
178         else
179                 bl = dctx->entropy_blocklen;
180         /* Call cleanup with original arguments */
181         dctx->cleanup_entropy(dctx, out - bl, olen + bl);
182         }
183
184
185 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
186                                 const unsigned char *pers, size_t perslen)
187         {
188         size_t entlen = 0, noncelen = 0;
189         unsigned char *nonce = NULL, *entropy = NULL;
190
191 #if 0
192         /* Put here so error script picks them up */
193         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
194                                 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
195         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
196         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
197         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
198         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
199         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
200 #endif
201
202         int r = 0;
203
204         if (perslen > dctx->max_pers)
205                 {
206                 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
207                 goto end;
208                 }
209
210         if (dctx->status != DRBG_STATUS_UNINITIALISED)
211                 {
212                 if (dctx->status == DRBG_STATUS_ERROR)
213                         r = FIPS_R_IN_ERROR_STATE;
214                 else
215                         r = FIPS_R_ALREADY_INSTANTIATED;
216                 goto end;
217                 }
218
219         dctx->status = DRBG_STATUS_ERROR;
220
221         entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
222                                 dctx->min_entropy, dctx->max_entropy);
223
224         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
225                 {
226                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
227                 goto end;
228                 }
229
230         if (dctx->max_nonce > 0)
231                 {
232                 noncelen = dctx->get_nonce(dctx, &nonce,
233                                         dctx->strength / 2,
234                                         dctx->min_nonce, dctx->max_nonce);
235
236                 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
237                         {
238                         r = FIPS_R_ERROR_RETRIEVING_NONCE;
239                         goto end;
240                         }
241
242                 }
243
244         if (!dctx->instantiate(dctx, 
245                                 entropy, entlen,
246                                 nonce, noncelen,
247                                 pers, perslen))
248                 {
249                 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
250                 goto end;
251                 }
252
253
254         dctx->status = DRBG_STATUS_READY;
255         if (!(dctx->iflags & DRBG_CUSTOM_RESEED))
256                 dctx->reseed_counter = 1;
257
258         end:
259
260         if (entropy && dctx->cleanup_entropy)
261                 fips_cleanup_entropy(dctx, entropy, entlen);
262
263         if (nonce && dctx->cleanup_nonce)
264                 dctx->cleanup_nonce(dctx, nonce, noncelen);
265
266         if (dctx->status == DRBG_STATUS_READY)
267                 return 1;
268
269         if (r && !(dctx->iflags & DRBG_FLAG_NOERR))
270                 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
271
272         return 0;
273
274         }
275
276 static int drbg_reseed(DRBG_CTX *dctx,
277                         const unsigned char *adin, size_t adinlen, int hcheck)
278         {
279         unsigned char *entropy = NULL;
280         size_t entlen;
281         int r = 0;
282
283 #if 0
284         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
285         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
286 #endif
287         if (dctx->status != DRBG_STATUS_READY
288                 && dctx->status != DRBG_STATUS_RESEED)
289                 {
290                 if (dctx->status == DRBG_STATUS_ERROR)
291                         r = FIPS_R_IN_ERROR_STATE;
292                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
293                         r = FIPS_R_NOT_INSTANTIATED;
294                 goto end;
295                 }
296
297         if (!adin)
298                 adinlen = 0;
299         else if (adinlen > dctx->max_adin)
300                 {
301                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
302                 goto end;
303                 }
304
305         dctx->status = DRBG_STATUS_ERROR;
306         /* Peform health check on all reseed operations if not a prediction
307          * resistance request and not in test mode.
308          */
309         if (hcheck && !(dctx->xflags & DRBG_FLAG_TEST))
310                 {
311                 if (!FIPS_drbg_test(dctx))
312                         {
313                         r = FIPS_R_SELFTEST_FAILURE;
314                         goto end;
315                         }
316                 }
317
318         entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
319                                 dctx->min_entropy, dctx->max_entropy);
320
321         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
322                 {
323                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
324                 goto end;
325                 }
326
327         if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
328                 goto end;
329
330         dctx->status = DRBG_STATUS_READY;
331         if (!(dctx->iflags & DRBG_CUSTOM_RESEED))
332                 dctx->reseed_counter = 1;
333         end:
334
335         if (entropy && dctx->cleanup_entropy)
336                 fips_cleanup_entropy(dctx, entropy, entlen);
337
338         if (dctx->status == DRBG_STATUS_READY)
339                 return 1;
340
341         if (r && !(dctx->iflags & DRBG_FLAG_NOERR))
342                 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
343
344         return 0;
345         }
346
347 int FIPS_drbg_reseed(DRBG_CTX *dctx,
348                         const unsigned char *adin, size_t adinlen)
349         {
350         return drbg_reseed(dctx, adin, adinlen, 1);
351         }
352
353 static int fips_drbg_check(DRBG_CTX *dctx)
354         {
355         if (dctx->xflags & DRBG_FLAG_TEST)
356                 return 1;
357         dctx->health_check_cnt++;
358         if (dctx->health_check_cnt >= dctx->health_check_interval)
359                 {
360                 if (!FIPS_drbg_test(dctx))
361                         {
362                         FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
363                         dctx->status = DRBG_STATUS_ERROR;
364                         return 0;
365                         }
366                 dctx->health_check_cnt = 0;
367                 }
368         return 1;
369         }
370
371 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
372                         int prediction_resistance,
373                         const unsigned char *adin, size_t adinlen)
374         {
375         int r = 0;
376
377         if (!fips_drbg_check(dctx))
378                 return 0;
379
380         if (dctx->status != DRBG_STATUS_READY
381                 && dctx->status != DRBG_STATUS_RESEED)
382                 {
383                 if (dctx->status == DRBG_STATUS_ERROR)
384                         r = FIPS_R_IN_ERROR_STATE;
385                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
386                         r = FIPS_R_NOT_INSTANTIATED;
387                 goto end;
388                 }
389
390         if (outlen > dctx->max_request)
391                 {
392                 r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG;
393                 return 0;
394                 }
395
396         if (adinlen > dctx->max_adin)
397                 {
398                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
399                 goto end;
400                 }
401
402         if (dctx->iflags & DRBG_CUSTOM_RESEED)
403                 dctx->generate(dctx, NULL, outlen, NULL, 0);
404         else if (dctx->reseed_counter >= dctx->reseed_interval)
405                 dctx->status = DRBG_STATUS_RESEED;
406
407         if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
408                 {
409                 /* If prediction resistance request don't do health check */
410                 int hcheck = prediction_resistance ? 0 : 1;
411                 
412                 if (!drbg_reseed(dctx, adin, adinlen, hcheck))
413                         {
414                         r = FIPS_R_RESEED_ERROR;
415                         goto end;
416                         }
417                 adin = NULL;
418                 adinlen = 0;
419                 }
420
421         if (!dctx->generate(dctx, out, outlen, adin, adinlen))
422                 {
423                 r = FIPS_R_GENERATE_ERROR;
424                 dctx->status = DRBG_STATUS_ERROR;
425                 goto end;
426                 }
427         if (!(dctx->iflags & DRBG_CUSTOM_RESEED))
428                 {
429                 if (dctx->reseed_counter >= dctx->reseed_interval)
430                         dctx->status = DRBG_STATUS_RESEED;
431                 else
432                         dctx->reseed_counter++;
433                 }
434
435         end:
436         if (r)
437                 {
438                 if (!(dctx->iflags & DRBG_FLAG_NOERR))
439                         FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
440                 return 0;
441                 }
442
443         return 1;
444         }
445
446 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
447         {
448         int rv;
449         if (!dctx->uninstantiate)
450                 rv = 1;
451         else
452                 rv = dctx->uninstantiate(dctx);
453         /* Although we'd like to cleanse here we can't because we have to
454          * test the uninstantiate really zeroes the data.
455          */
456         memset(&dctx->d, 0, sizeof(dctx->d));
457         dctx->status = DRBG_STATUS_UNINITIALISED;
458         /* If method has problems uninstantiating, return error */
459         return rv;
460         }
461
462 int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
463         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
464                                 int entropy, size_t min_len, size_t max_len),
465         void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
466         size_t entropy_blocklen,
467         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
468                                 int entropy, size_t min_len, size_t max_len),
469         void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
470         {
471         if (dctx->status != DRBG_STATUS_UNINITIALISED)
472                 return 0;
473         dctx->entropy_blocklen = entropy_blocklen;
474         dctx->get_entropy = get_entropy;
475         dctx->cleanup_entropy = cleanup_entropy;
476         dctx->get_nonce = get_nonce;
477         dctx->cleanup_nonce = cleanup_nonce;
478         return 1;
479         }
480
481 int FIPS_drbg_set_rand_callbacks(DRBG_CTX *dctx,
482         size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout),
483         void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
484         int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num),
485         int (*rand_add_cb)(DRBG_CTX *ctx,
486                                 const void *buf, int num, double entropy))
487         {
488         if (dctx->status != DRBG_STATUS_UNINITIALISED)
489                 return 0;
490         dctx->get_adin = get_adin;
491         dctx->cleanup_adin = cleanup_adin;
492         dctx->rand_seed_cb = rand_seed_cb;
493         dctx->rand_add_cb = rand_add_cb;
494         return 1;
495         }
496
497 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
498         {
499         return dctx->app_data;
500         }
501
502 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
503         {
504         dctx->app_data = app_data;
505         }
506
507 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
508         {
509         return dctx->blocklength;
510         }
511
512 int FIPS_drbg_get_strength(DRBG_CTX *dctx)
513         {
514         return dctx->strength;
515         }
516
517 void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
518         {
519         dctx->health_check_interval = interval;
520         }
521
522 void FIPS_drbg_set_reseed_interval(DRBG_CTX *dctx, int interval)
523         {
524         dctx->reseed_interval = interval;
525         }
526
527 static int drbg_stick = 0;
528
529 void FIPS_drbg_stick(void)
530         {
531         drbg_stick = 1;
532         }
533
534 /* Continuous DRBG utility function */
535 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
536         {
537         /* No CPRNG in test mode */
538         if (dctx->xflags & DRBG_FLAG_TEST)
539                 return 1;
540         /* Check block is valid: should never happen */
541         if (dctx->lb_valid == 0)
542                 {
543                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
544                 fips_set_selftest_fail();
545                 return 0;
546                 }
547         if (drbg_stick)
548                 memcpy(dctx->lb, out, dctx->blocklength);
549         /* Check against last block: fail if match */
550         if (!memcmp(dctx->lb, out, dctx->blocklength))
551                 {
552                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
553                 fips_set_selftest_fail();
554                 return 0;
555                 }
556         /* Save last block for next comparison */
557         memcpy(dctx->lb, out, dctx->blocklength);
558         return 1;
559         }