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