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