Add meaningful error codes to DRBG.
[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 static 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         return rv;
80         }
81
82 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
83         {
84         int rv;
85         DRBG_CTX *dctx;
86         dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
87         if (!dctx)
88                 {
89                 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
90                 return NULL;
91                 }
92         rv = fips_drbg_init(dctx, type, flags);
93
94         if (rv <= 0)
95                 {
96                 if (rv == -2)
97                         FIPSerr(FIPS_F_FIPS_DRBG_NEW, FIPS_R_UNSUPPORTED_DRBG_TYPE);
98                 else
99                         FIPSerr(FIPS_F_FIPS_DRBG_NEW, FIPS_R_ERROR_INITIALISING_DRBG);
100
101                 OPENSSL_free(dctx);
102                 return NULL;
103                 }
104         return dctx;
105         }
106
107 void FIPS_drbg_free(DRBG_CTX *dctx)
108         {
109         dctx->uninstantiate(dctx);
110         OPENSSL_cleanse(dctx, sizeof(DRBG_CTX));
111         OPENSSL_free(dctx);
112         }
113
114 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
115                                 int strength,
116                                 const unsigned char *pers, size_t perslen)
117         {
118         size_t entlen, noncelen;
119
120 #if 0
121         /* Put here so error script picks them up */
122         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
123                                 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
124         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
125         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
126         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
127         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
128         FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
129 #endif
130
131         int r = 0;
132
133         if (perslen > dctx->max_pers)
134                 {
135                 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
136                 goto end;
137                 }
138
139         if (dctx->status != DRBG_STATUS_UNINITIALISED)
140                 {
141                 if (dctx->status == DRBG_STATUS_ERROR)
142                         r = FIPS_R_IN_ERROR_STATE;
143                 else
144                         r = FIPS_R_ALREADY_INSTANTIATED;
145                 goto end;
146                 }
147
148         dctx->status = DRBG_STATUS_ERROR;
149
150         entlen = dctx->get_entropy(dctx, dctx->entropy, dctx->strength,
151                                 dctx->min_entropy, dctx->max_entropy);
152
153         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
154                 {
155                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
156                 goto end;
157                 }
158
159         if (dctx->max_nonce > 0)
160                 {
161
162                 noncelen = dctx->get_nonce(dctx, dctx->nonce,
163                                         dctx->strength / 2,
164                                         dctx->min_nonce, dctx->max_nonce);
165
166                 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
167                         {
168                         r = FIPS_R_ERROR_RETRIEVING_NONCE;
169                         goto end;
170                         }
171
172                 }
173         else
174                 noncelen = 0;
175
176         if (!dctx->instantiate(dctx, 
177                                 dctx->entropy, entlen,
178                                 dctx->nonce, noncelen,
179                                 pers, perslen))
180                 {
181                 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
182                 goto end;
183                 }
184
185
186         dctx->status = DRBG_STATUS_READY;
187         dctx->reseed_counter = 1;
188         /* Initial test value for reseed interval */
189         dctx->reseed_interval = 1<<24;
190
191         end:
192
193         OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy));
194         OPENSSL_cleanse(dctx->nonce, sizeof(dctx->nonce));
195
196         if (dctx->status == DRBG_STATUS_READY)
197                 return 1;
198
199         if (r && !(dctx->flags & DRBG_FLAG_TEST))
200                 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
201
202         return 0;
203
204         }
205
206 int FIPS_drbg_reseed(DRBG_CTX *dctx,
207                         const unsigned char *adin, size_t adinlen)
208         {
209         size_t entlen;
210         int r = 0;
211
212 #if 0
213         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
214         FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
215 #endif
216         if (dctx->status != DRBG_STATUS_READY
217                 && dctx->status != DRBG_STATUS_RESEED)
218                 {
219                 if (dctx->status == DRBG_STATUS_ERROR)
220                         r = FIPS_R_IN_ERROR_STATE;
221                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
222                         r = FIPS_R_NOT_INSTANTIATED;
223                 goto end;
224                 }
225
226         if (!adin)
227                 adinlen = 0;
228         else if (adinlen > dctx->max_adin)
229                 {
230                 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
231                 goto end;
232                 }
233
234         dctx->status = DRBG_STATUS_ERROR;
235
236         entlen = dctx->get_entropy(dctx, dctx->entropy, dctx->strength,
237                                 dctx->min_entropy, dctx->max_entropy);
238
239         if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
240                 {
241                 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
242                 goto end;
243                 }
244
245         if (!dctx->reseed(dctx, dctx->entropy, entlen, adin, adinlen))
246                 goto end;
247
248         dctx->status = DRBG_STATUS_READY;
249         dctx->reseed_counter = 1;
250         end:
251         OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy));
252
253         if (dctx->status == DRBG_STATUS_READY)
254                 return 1;
255
256         if (r && !(dctx->flags & DRBG_FLAG_TEST))
257                 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
258
259         return 0;
260         }
261
262
263 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
264                         int prediction_resistance,
265                         const unsigned char *adin, size_t adinlen)
266         {
267         int r = 0;
268         if (outlen > dctx->max_request)
269                 {
270                 r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG;
271                 return 0;
272                 }
273         if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
274                 {
275                 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
276                         {
277                         r = FIPS_R_RESEED_ERROR;
278                         goto end;
279                         }
280                 adin = NULL;
281                 adinlen = 0;
282                 }
283         if (dctx->status != DRBG_STATUS_READY)
284                 {
285                 if (dctx->status == DRBG_STATUS_ERROR)
286                         r = FIPS_R_IN_ERROR_STATE;
287                 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
288                         r = FIPS_R_NOT_INSTANTIATED;
289                 goto end;
290                 }
291         if (!dctx->generate(dctx, out, outlen, adin, adinlen))
292                 {
293                 r = FIPS_R_GENERATE_ERROR;
294                 dctx->status = DRBG_STATUS_ERROR;
295                 goto end;
296                 }
297         if (dctx->reseed_counter > dctx->reseed_interval)
298                 dctx->status = DRBG_STATUS_RESEED;
299         else
300                 dctx->reseed_counter++;
301
302         end:
303         if (r)
304                 {
305                 if (!(dctx->flags & DRBG_FLAG_TEST))
306                         FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
307                 return 0;
308                 }
309
310         return 1;
311         }
312
313 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
314         {
315         int save_type, save_flags, rv;
316         save_type = dctx->type;
317         save_flags = dctx->flags;
318         rv = dctx->uninstantiate(dctx);
319         OPENSSL_cleanse(dctx, sizeof(DRBG_CTX));
320         /* If method has problems uninstantiating, return error */
321         if (rv <= 0)
322                 return rv;
323         return fips_drbg_init(dctx, save_type, save_flags);
324         }
325
326 int FIPS_drbg_set_test_mode(DRBG_CTX *dctx,
327         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char *out,
328                                 int entropy, size_t min_len, size_t max_len),
329         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char *out,
330                                 int entropy, size_t min_len, size_t max_len))
331         {
332         if (dctx->status != DRBG_STATUS_UNINITIALISED)
333                 return 0;
334         dctx->flags |= DRBG_FLAG_TEST;
335         dctx->get_entropy = get_entropy;
336         dctx->get_nonce = get_nonce;
337         return 1;
338         }
339
340 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
341         {
342         return dctx->app_data;
343         }
344
345 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
346         {
347         dctx->app_data = app_data;
348         }
349
350 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
351         {
352         return dctx->blocklength;
353         }