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