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