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