a0bb9eda64d53e949a8ff88f82543414108c1b91
[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->reseed_counter >= dctx->reseed_interval)
377                 dctx->status = DRBG_STATUS_RESEED;
378
379         if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
380                 {
381                 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
382                         {
383                         r = FIPS_R_RESEED_ERROR;
384                         goto end;
385                         }
386                 adin = NULL;
387                 adinlen = 0;
388                 }
389
390         if (!dctx->generate(dctx, out, outlen, adin, adinlen))
391                 {
392                 r = FIPS_R_GENERATE_ERROR;
393                 dctx->status = DRBG_STATUS_ERROR;
394                 goto end;
395                 }
396         if (dctx->reseed_counter >= dctx->reseed_interval)
397                 dctx->status = DRBG_STATUS_RESEED;
398         else
399                 dctx->reseed_counter++;
400
401         end:
402         if (r)
403                 {
404                 if (!(dctx->flags & DRBG_FLAG_NOERR))
405                         FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
406                 return 0;
407                 }
408
409         return 1;
410         }
411
412 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
413         {
414         int rv;
415         if (!dctx->uninstantiate)
416                 rv = 1;
417         else
418                 rv = dctx->uninstantiate(dctx);
419         /* Although we'd like to cleanse here we can't because we have to
420          * test the uninstantiate really zeroes the data.
421          */
422         memset(&dctx->d, 0, sizeof(dctx->d));
423         dctx->status = DRBG_STATUS_UNINITIALISED;
424         /* If method has problems uninstantiating, return error */
425         return rv;
426         }
427
428 int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
429         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
430                                 int entropy, size_t min_len, size_t max_len),
431         void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
432         size_t entropy_blocklen,
433         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
434                                 int entropy, size_t min_len, size_t max_len),
435         void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
436         {
437         if (dctx->status != DRBG_STATUS_UNINITIALISED)
438                 return 0;
439         dctx->entropy_blocklen = entropy_blocklen;
440         dctx->get_entropy = get_entropy;
441         dctx->cleanup_entropy = cleanup_entropy;
442         dctx->get_nonce = get_nonce;
443         dctx->cleanup_nonce = cleanup_nonce;
444         return 1;
445         }
446
447 int FIPS_drbg_set_rand_callbacks(DRBG_CTX *dctx,
448         size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout),
449         void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
450         int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num),
451         int (*rand_add_cb)(DRBG_CTX *ctx,
452                                 const void *buf, int num, double entropy))
453         {
454         if (dctx->status != DRBG_STATUS_UNINITIALISED)
455                 return 0;
456         dctx->get_adin = get_adin;
457         dctx->cleanup_adin = cleanup_adin;
458         dctx->rand_seed_cb = rand_seed_cb;
459         dctx->rand_add_cb = rand_add_cb;
460         return 1;
461         }
462
463 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
464         {
465         return dctx->app_data;
466         }
467
468 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
469         {
470         dctx->app_data = app_data;
471         }
472
473 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
474         {
475         return dctx->blocklength;
476         }
477
478 int FIPS_drbg_get_strength(DRBG_CTX *dctx)
479         {
480         return dctx->strength;
481         }
482
483 void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
484         {
485         dctx->health_check_interval = interval;
486         }
487
488 static int drbg_stick = 0;
489
490 void FIPS_drbg_stick(void)
491         {
492         drbg_stick = 1;
493         }
494
495 /* Continuous DRBG utility function */
496 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
497         {
498         /* No CPRNG in test mode */
499         if (dctx->flags & DRBG_FLAG_TEST)
500                 return 1;
501         /* Check block is valid: should never happen */
502         if (dctx->lb_valid == 0)
503                 {
504                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
505                 fips_set_selftest_fail();
506                 return 0;
507                 }
508         if (drbg_stick)
509                 memcpy(dctx->lb, out, dctx->blocklength);
510         /* Check against last block: fail if match */
511         if (!memcmp(dctx->lb, out, dctx->blocklength))
512                 {
513                 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
514                 fips_set_selftest_fail();
515                 return 0;
516                 }
517         /* Save last block for next comparison */
518         memcpy(dctx->lb, out, dctx->blocklength);
519         return 1;
520         }