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