Updates to DRBG: fix bugs in infrastructure. Add initial experimental
[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/fips_rand.h>
61 #include "fips_rand_lcl.h"
62
63 /* Support framework for SP800-90 DRBGs */
64
65 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
66         {
67         int rv;
68         DRBG_CTX *dctx;
69         dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
70         memset(dctx, 0, sizeof(DRBG_CTX));
71         dctx->status = DRBG_STATUS_UNINITIALISED;
72         dctx->flags = flags;
73         dctx->type = type;
74         rv = fips_drbg_hash_init(dctx);
75         if (rv == -2)
76                 rv = fips_drbg_ctr_init(dctx);
77         if (rv <= 0)
78                 {
79                 /* Fatal: cannot initialiase DRBG */
80                 goto err;
81                 }
82
83         return dctx;
84
85         err:
86         if (dctx)
87                 OPENSSL_free(dctx);
88         return NULL;
89         }
90
91 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
92                                 int strength,
93                                 const unsigned char *pers, size_t perslen)
94         {
95         size_t entlen, noncelen;
96
97         if (perslen > dctx->max_pers)
98                 return 0;
99
100         if (dctx->status != DRBG_STATUS_UNINITIALISED)
101                 {
102                 /* error */
103                 return 0;
104                 }
105
106         dctx->status = DRBG_STATUS_ERROR;
107
108         entlen = dctx->get_entropy(dctx, dctx->entropy, dctx->strength,
109                                 dctx->min_entropy, dctx->max_entropy);
110
111         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
112                 goto end;
113
114         if (dctx->max_nonce > 0)
115                 {
116
117                 noncelen = dctx->get_nonce(dctx, dctx->nonce,
118                                         dctx->strength / 2,
119                                         dctx->min_nonce, dctx->max_nonce);
120
121                 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
122                         goto end;
123
124                 }
125         else
126                 noncelen = 0;
127
128         if (!dctx->instantiate(dctx, 
129                                 dctx->entropy, entlen,
130                                 dctx->nonce, noncelen,
131                                 pers, perslen))
132                 goto end;
133
134
135         dctx->status = DRBG_STATUS_READY;
136         dctx->reseed_counter = 1;
137         /* Initial test value for reseed interval */
138         dctx->reseed_interval = 1<<24;
139
140         end:
141
142         OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy));
143         OPENSSL_cleanse(dctx->nonce, sizeof(dctx->nonce));
144
145         if (dctx->status == DRBG_STATUS_READY)
146                 return 1;
147
148         return 0;
149
150         }
151
152 int FIPS_drbg_reseed(DRBG_CTX *dctx,
153                         const unsigned char *adin, size_t adinlen)
154         {
155         size_t entlen;
156         if (dctx->status != DRBG_STATUS_READY
157                 && dctx->status != DRBG_STATUS_RESEED)
158                 {
159                 /* error */
160                 return 0;
161                 }
162
163         if (!adin)
164                 adinlen = 0;
165         else if (adinlen > dctx->max_adin)
166                 {
167                 /* error */
168                 return 0;
169                 }
170
171         dctx->status = DRBG_STATUS_ERROR;
172
173         entlen = dctx->get_entropy(dctx, dctx->entropy, dctx->strength,
174                                 dctx->min_entropy, dctx->max_entropy);
175
176         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
177                 goto end;
178
179         if (!dctx->reseed(dctx, dctx->entropy, entlen, adin, adinlen))
180                 goto end;
181
182         dctx->status = DRBG_STATUS_READY;
183         dctx->reseed_counter = 1;
184         end:
185         OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy));
186         if (dctx->status == DRBG_STATUS_READY)
187                 return 1;
188         return 0;
189         }
190         
191
192 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
193                         int prediction_resistance,
194                         const unsigned char *adin, size_t adinlen)
195         {
196         if (outlen > dctx->max_request)
197                 {
198                 /* Too large */
199                 return 0;
200                 }
201         if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
202                 {
203                 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
204                         return 0;
205                 adin = NULL;
206                 adinlen = 0;
207                 }
208         if (dctx->status != DRBG_STATUS_READY)
209                 {
210                 /* Bad error */
211                 return 0;
212                 }
213         if (!dctx->generate(dctx, out, outlen, adin, adinlen))
214                 {
215                 /* Bad error */
216                 dctx->status = DRBG_STATUS_ERROR;
217                 return 0;
218                 }
219         if (dctx->reseed_counter > dctx->reseed_interval)
220                 dctx->status = DRBG_STATUS_RESEED;
221         else
222                 dctx->reseed_counter++;
223
224         return 1;
225         }
226
227
228 int FIPS_drbg_set_test_mode(DRBG_CTX *dctx,
229         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char *out,
230                                 int entropy, size_t min_len, size_t max_len),
231         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char *out,
232                                 int entropy, size_t min_len, size_t max_len))
233         {
234         if (dctx->status != DRBG_STATUS_UNINITIALISED)
235                 return 0;
236         dctx->flags |= DRBG_FLAG_TEST;
237         dctx->get_entropy = get_entropy;
238         dctx->get_nonce = get_nonce;
239         return 1;
240         }
241
242 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
243         {
244         return dctx->app_data;
245         }
246
247 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
248         {
249         dctx->app_data = app_data;
250         }
251
252 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
253         {
254         return dctx->blocklength;
255         }