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