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