Add stub for HMAC DRBG.
[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         if (rv == -2)
83                 rv = fips_drbg_hmac_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                         return 0;
102                         }
103                 }
104
105         return rv;
106         }
107
108 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
109         {
110         DRBG_CTX *dctx;
111         dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
112         if (!dctx)
113                 {
114                 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
115                 return NULL;
116                 }
117         if (type == 0)
118                 return dctx;
119
120         if (FIPS_drbg_init(dctx, type, flags) <= 0)
121                 {
122                 OPENSSL_free(dctx);
123                 return NULL;
124                 }
125                 
126         return dctx;
127         }
128
129 void FIPS_drbg_free(DRBG_CTX *dctx)
130         {
131         if (dctx->uninstantiate)
132                 dctx->uninstantiate(dctx);
133         OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
134         OPENSSL_free(dctx);
135         }
136
137 static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
138                                 int entropy, size_t min_len, size_t max_len)
139         {
140         unsigned char *tout, *p;
141         size_t bl = dctx->entropy_blocklen, rv;
142         if (dctx->flags & DRBG_FLAG_TEST || !bl)
143                 return dctx->get_entropy(dctx, pout, entropy, min_len, max_len);
144         rv = dctx->get_entropy(dctx, &tout, entropy + bl,
145                                 min_len + bl, max_len + bl);
146         *pout = tout + bl;
147         if (rv < (min_len + bl) || (rv % bl))
148                 return 0;
149         /* Compare consecutive blocks for continuous PRNG test */
150         for (p = tout; p < tout + rv - bl; p += bl)
151                 {
152                 if (!memcmp(p, p + bl, bl))
153                         {
154                         FIPSerr(FIPS_F_FIPS_GET_ENTROPY, FIPS_R_ENTROPY_SOURCE_STUCK);
155                         return 0;
156                         }
157                 }
158         rv -= bl;
159         if (rv > max_len)
160                 return max_len;
161         return rv;
162         }
163
164 static void fips_cleanup_entropy(DRBG_CTX *dctx,
165                                         unsigned char *out, size_t olen)
166         {
167         size_t bl;
168         if (dctx->flags & DRBG_FLAG_TEST)
169                 bl = 0;
170         else
171                 bl = dctx->entropy_blocklen;
172         /* Call cleanup with original arguments */
173         dctx->cleanup_entropy(dctx, out - bl, olen + bl);
174         }
175
176
177 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
178                                 const unsigned char *pers, size_t perslen)
179         {
180         size_t entlen = 0, noncelen = 0;
181         unsigned char *nonce = NULL, *entropy = NULL;
182
183 #if 0
184         /* Put here so error script picks them up */
185         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
186                                 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
187         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
188         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
189         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
190         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
191         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
192 #endif
193
194         int r = 0;
195
196         if (perslen > dctx->max_pers)
197                 {
198                 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
199                 goto end;
200                 }
201
202         if (dctx->status != DRBG_STATUS_UNINITIALISED)
203                 {
204                 if (dctx->status == DRBG_STATUS_ERROR)
205                         r = FIPS_R_IN_ERROR_STATE;
206                 else
207                         r = FIPS_R_ALREADY_INSTANTIATED;
208                 goto end;
209                 }
210
211         dctx->status = DRBG_STATUS_ERROR;
212
213         entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
214                                 dctx->min_entropy, dctx->max_entropy);
215
216         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
217                 {
218                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
219                 goto end;
220                 }
221
222         if (dctx->max_nonce > 0)
223                 {
224                 noncelen = dctx->get_nonce(dctx, &nonce,
225                                         dctx->strength / 2,
226                                         dctx->min_nonce, dctx->max_nonce);
227
228                 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
229                         {
230                         r = FIPS_R_ERROR_RETRIEVING_NONCE;
231                         goto end;
232                         }
233
234                 }
235
236         if (!dctx->instantiate(dctx, 
237                                 entropy, entlen,
238                                 nonce, noncelen,
239                                 pers, perslen))
240                 {
241                 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
242                 goto end;
243                 }
244
245
246         dctx->status = DRBG_STATUS_READY;
247         dctx->reseed_counter = 1;
248
249         end:
250
251         if (entropy && dctx->cleanup_entropy)
252                 fips_cleanup_entropy(dctx, entropy, entlen);
253
254         if (nonce && dctx->cleanup_nonce)
255                 dctx->cleanup_nonce(dctx, nonce, noncelen);
256
257         if (dctx->status == DRBG_STATUS_READY)
258                 return 1;
259
260         if (r && !(dctx->flags & DRBG_FLAG_NOERR))
261                 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
262
263         return 0;
264
265         }
266
267 int FIPS_drbg_reseed(DRBG_CTX *dctx,
268                         const unsigned char *adin, size_t adinlen)
269         {
270         unsigned char *entropy = NULL;
271         size_t entlen;
272         int r = 0;
273
274 #if 0
275         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
276         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
277 #endif
278         if (dctx->status != DRBG_STATUS_READY
279                 && dctx->status != DRBG_STATUS_RESEED)
280                 {
281                 if (dctx->status == DRBG_STATUS_ERROR)
282                         r = FIPS_R_IN_ERROR_STATE;
283                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
284                         r = FIPS_R_NOT_INSTANTIATED;
285                 goto end;
286                 }
287
288         if (!adin)
289                 adinlen = 0;
290         else if (adinlen > dctx->max_adin)
291                 {
292                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
293                 goto end;
294                 }
295
296         dctx->status = DRBG_STATUS_ERROR;
297
298         entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
299                                 dctx->min_entropy, dctx->max_entropy);
300
301         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
302                 {
303                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
304                 goto end;
305                 }
306
307         if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
308                 goto end;
309
310         dctx->status = DRBG_STATUS_READY;
311         dctx->reseed_counter = 1;
312         end:
313
314         if (entropy && dctx->cleanup_entropy)
315                 fips_cleanup_entropy(dctx, entropy, entlen);
316
317         if (dctx->status == DRBG_STATUS_READY)
318                 return 1;
319
320         if (r && !(dctx->flags & DRBG_FLAG_NOERR))
321                 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
322
323         return 0;
324         }
325
326 static int fips_drbg_check(DRBG_CTX *dctx)
327         {
328         if (dctx->flags & DRBG_FLAG_TEST)
329                 return 1;
330         dctx->health_check_cnt++;
331         if (dctx->health_check_cnt >= dctx->health_check_interval)
332                 {
333                 DRBG_CTX tctx;
334                 if (!fips_drbg_kat(&tctx, dctx->type,
335                                                 dctx->flags | DRBG_FLAG_TEST))
336                         {
337                         FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
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         }