Add periodic DRBG health checks as required by SP800-90.
[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/evp.h>
59 #include <openssl/aes.h>
60 #include <openssl/err.h>
61 #include <openssl/fips_rand.h>
62 #include "fips_rand_lcl.h"
63
64 /* Support framework for SP800-90 DRBGs */
65
66 int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
67         {
68         int rv;
69         memset(dctx, 0, sizeof(DRBG_CTX));
70         dctx->status = DRBG_STATUS_UNINITIALISED;
71         dctx->flags = flags;
72         dctx->type = type;
73
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
82         if (rv <= 0)
83                 {
84                 if (rv == -2)
85                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_UNSUPPORTED_DRBG_TYPE);
86                 else
87                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_ERROR_INITIALISING_DRBG);
88                 }
89
90         /* If not in test mode run selftests on DRBG of the same type */
91
92         if (!(dctx->flags & DRBG_FLAG_TEST))
93                 {
94                 DRBG_CTX tctx;
95                 if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
96                         {
97                         FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
98                         return 0;
99                         }
100                 }
101
102         return rv;
103         }
104
105 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
106         {
107         DRBG_CTX *dctx;
108         dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
109         if (!dctx)
110                 {
111                 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
112                 return NULL;
113                 }
114         if (type == 0)
115                 return dctx;
116
117         if (FIPS_drbg_init(dctx, type, flags) <= 0)
118                 {
119                 OPENSSL_free(dctx);
120                 return NULL;
121                 }
122                 
123         return dctx;
124         }
125
126 void FIPS_drbg_free(DRBG_CTX *dctx)
127         {
128         if (dctx->uninstantiate)
129                 dctx->uninstantiate(dctx);
130         OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
131         OPENSSL_free(dctx);
132         }
133
134 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
135                                 const unsigned char *pers, size_t perslen)
136         {
137         size_t entlen = 0, noncelen = 0;
138         unsigned char *nonce = NULL, *entropy = NULL;
139
140 #if 0
141         /* Put here so error script picks them up */
142         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
143                                 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
144         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
145         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
146         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
147         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
148         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
149 #endif
150
151         int r = 0;
152
153         if (perslen > dctx->max_pers)
154                 {
155                 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
156                 goto end;
157                 }
158
159         if (dctx->status != DRBG_STATUS_UNINITIALISED)
160                 {
161                 if (dctx->status == DRBG_STATUS_ERROR)
162                         r = FIPS_R_IN_ERROR_STATE;
163                 else
164                         r = FIPS_R_ALREADY_INSTANTIATED;
165                 goto end;
166                 }
167
168         dctx->status = DRBG_STATUS_ERROR;
169
170         entlen = dctx->get_entropy(dctx, &entropy, dctx->strength,
171                                 dctx->min_entropy, dctx->max_entropy);
172
173         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
174                 {
175                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
176                 goto end;
177                 }
178
179         if (dctx->max_nonce > 0)
180                 {
181                 noncelen = dctx->get_nonce(dctx, &nonce,
182                                         dctx->strength / 2,
183                                         dctx->min_nonce, dctx->max_nonce);
184
185                 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
186                         {
187                         r = FIPS_R_ERROR_RETRIEVING_NONCE;
188                         goto end;
189                         }
190
191                 }
192
193         if (!dctx->instantiate(dctx, 
194                                 entropy, entlen,
195                                 nonce, noncelen,
196                                 pers, perslen))
197                 {
198                 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
199                 goto end;
200                 }
201
202
203         dctx->status = DRBG_STATUS_READY;
204         dctx->reseed_counter = 1;
205
206         end:
207
208         if (entropy && dctx->cleanup_entropy)
209                 dctx->cleanup_entropy(dctx, entropy, entlen);
210
211         if (nonce && dctx->cleanup_nonce)
212                 dctx->cleanup_nonce(dctx, nonce, noncelen);
213
214         if (dctx->status == DRBG_STATUS_READY)
215                 return 1;
216
217         if (r && !(dctx->flags & DRBG_FLAG_NOERR))
218                 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
219
220         return 0;
221
222         }
223
224 int FIPS_drbg_reseed(DRBG_CTX *dctx,
225                         const unsigned char *adin, size_t adinlen)
226         {
227         unsigned char *entropy = NULL;
228         size_t entlen;
229         int r = 0;
230
231 #if 0
232         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
233         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
234 #endif
235         if (dctx->status != DRBG_STATUS_READY
236                 && dctx->status != DRBG_STATUS_RESEED)
237                 {
238                 if (dctx->status == DRBG_STATUS_ERROR)
239                         r = FIPS_R_IN_ERROR_STATE;
240                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
241                         r = FIPS_R_NOT_INSTANTIATED;
242                 goto end;
243                 }
244
245         if (!adin)
246                 adinlen = 0;
247         else if (adinlen > dctx->max_adin)
248                 {
249                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
250                 goto end;
251                 }
252
253         dctx->status = DRBG_STATUS_ERROR;
254
255         entlen = dctx->get_entropy(dctx, &entropy, dctx->strength,
256                                 dctx->min_entropy, dctx->max_entropy);
257
258         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
259                 {
260                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
261                 goto end;
262                 }
263
264         if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
265                 goto end;
266
267         dctx->status = DRBG_STATUS_READY;
268         dctx->reseed_counter = 1;
269         end:
270
271         if (entropy && dctx->cleanup_entropy)
272                 dctx->cleanup_entropy(dctx, entropy, entlen);
273
274         if (dctx->status == DRBG_STATUS_READY)
275                 return 1;
276
277         if (r && !(dctx->flags & DRBG_FLAG_NOERR))
278                 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
279
280         return 0;
281         }
282
283 static int fips_drbg_check(DRBG_CTX *dctx)
284         {
285         if (dctx->flags & DRBG_FLAG_TEST)
286                 return 1;
287         dctx->health_check_cnt++;
288         if (dctx->health_check_cnt >= dctx->health_check_interval)
289                 {
290                 DRBG_CTX tctx;
291                 if (!fips_drbg_kat(&tctx, dctx->type,
292                                                 dctx->flags | DRBG_FLAG_TEST))
293                         {
294                         FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
295                         return 0;
296                         }
297                 dctx->health_check_cnt = 0;
298                 }
299         return 1;
300         }
301
302 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
303                         int strength, int prediction_resistance,
304                         const unsigned char *adin, size_t adinlen)
305         {
306         int r = 0;
307
308         if (!fips_drbg_check(dctx))
309                 return 0;
310
311         if (dctx->status != DRBG_STATUS_READY
312                 && dctx->status != DRBG_STATUS_RESEED)
313                 {
314                 if (dctx->status == DRBG_STATUS_ERROR)
315                         r = FIPS_R_IN_ERROR_STATE;
316                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
317                         r = FIPS_R_NOT_INSTANTIATED;
318                 goto end;
319                 }
320
321         if (outlen > dctx->max_request)
322                 {
323                 r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG;
324                 return 0;
325                 }
326
327         if (strength > dctx->strength)
328                 {
329                 r = FIPS_R_INSUFFICIENT_SECURITY_STRENGTH;
330                 goto end;
331                 }
332
333         if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
334                 {
335                 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
336                         {
337                         r = FIPS_R_RESEED_ERROR;
338                         goto end;
339                         }
340                 adin = NULL;
341                 adinlen = 0;
342                 }
343
344         if (!dctx->generate(dctx, out, outlen, adin, adinlen))
345                 {
346                 r = FIPS_R_GENERATE_ERROR;
347                 dctx->status = DRBG_STATUS_ERROR;
348                 goto end;
349                 }
350         if (dctx->reseed_counter >= dctx->reseed_interval)
351                 dctx->status = DRBG_STATUS_RESEED;
352         else
353                 dctx->reseed_counter++;
354
355         end:
356         if (r)
357                 {
358                 if (!(dctx->flags & DRBG_FLAG_NOERR))
359                         FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
360                 return 0;
361                 }
362
363         return 1;
364         }
365
366 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
367         {
368         int rv;
369         if (!dctx->uninstantiate)
370                 rv = 1;
371         else
372                 rv = dctx->uninstantiate(dctx);
373         /* Although we'd like to cleanse here we can't because we have to
374          * test the uninstantiate really zeroes the data.
375          */
376         memset(&dctx->d, 0, sizeof(dctx->d));
377         dctx->status = DRBG_STATUS_UNINITIALISED;
378         /* If method has problems uninstantiating, return error */
379         return rv;
380         }
381
382 int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
383         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
384                                 int entropy, size_t min_len, size_t max_len),
385         void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
386         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
387                                 int entropy, size_t min_len, size_t max_len),
388         void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
389         {
390         if (dctx->status != DRBG_STATUS_UNINITIALISED)
391                 return 0;
392         dctx->get_entropy = get_entropy;
393         dctx->cleanup_entropy = cleanup_entropy;
394         dctx->get_nonce = get_nonce;
395         dctx->cleanup_nonce = cleanup_nonce;
396         return 1;
397         }
398
399 int FIPS_drbg_set_rand_callbacks(DRBG_CTX *dctx,
400         size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout),
401         void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
402         int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num),
403         int (*rand_add_cb)(DRBG_CTX *ctx,
404                                 const void *buf, int num, double entropy))
405         {
406         if (dctx->status != DRBG_STATUS_UNINITIALISED)
407                 return 0;
408         dctx->get_adin = get_adin;
409         dctx->cleanup_adin = cleanup_adin;
410         dctx->rand_seed_cb = rand_seed_cb;
411         dctx->rand_add_cb = rand_add_cb;
412         return 1;
413         }
414
415 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
416         {
417         return dctx->app_data;
418         }
419
420 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
421         {
422         dctx->app_data = app_data;
423         }
424
425 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
426         {
427         return dctx->blocklength;
428         }
429
430 int FIPS_drbg_get_strength(DRBG_CTX *dctx)
431         {
432         return dctx->strength;
433         }
434
435 void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
436         {
437         dctx->health_check_interval = interval;
438         }
439
440 static int drbg_stick = 0;
441
442 void FIPS_drbg_stick(void)
443         {
444         drbg_stick = 1;
445         }
446
447 /* Continuous DRBG utility function */
448 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
449         {
450         /* No CPRNG in test mode */
451         if (dctx->flags & DRBG_FLAG_TEST)
452                 return 1;
453         /* Check block is valid: should never happen */
454         if (dctx->lb_valid == 0)
455                 {
456                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
457                 fips_set_selftest_fail();
458                 return 0;
459                 }
460         if (drbg_stick)
461                 memcpy(dctx->lb, out, dctx->blocklength);
462         /* Check against last block: fail if match */
463         if (!memcmp(dctx->lb, out, dctx->blocklength))
464                 {
465                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
466                 fips_set_selftest_fail();
467                 return 0;
468                 }
469         /* Save last block for next comparison */
470         memcpy(dctx->lb, out, dctx->blocklength);
471         return 1;
472         }