f179bd82a9259c48675123a797f45fe3e8dea688
[openssl.git] / fips / rand / fips_drbg_rand.c
1 /* fips/rand/fips_drbg_rand.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/rand.h>
62 #include <openssl/fips_rand.h>
63 #include "fips_rand_lcl.h"
64
65 /* Mapping of SP800-90 DRBGs to OpenSSL RAND_METHOD */
66
67 /* Since we only have one global PRNG used at any time in OpenSSL use a global
68  * variable to store contexts.
69  */
70
71 static DRBG_CTX ossl_dctx;
72
73 DRBG_CTX *FIPS_get_default_drbg(void)
74         {
75         return &ossl_dctx;
76         }
77
78 static int fips_drbg_bytes(unsigned char *out, int count)
79         {
80         DRBG_CTX *dctx = &ossl_dctx;
81         int rv = 0;
82         unsigned char *adin = NULL;
83         size_t adinlen = 0;
84         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
85         do 
86                 {
87                 size_t rcnt;
88                 if (count > (int)dctx->max_request)
89                         rcnt = dctx->max_request;
90                 else
91                         rcnt = count;
92                 if (dctx->get_adin)
93                         {
94                         adinlen = dctx->get_adin(dctx, &adin);
95                         if (adinlen && !adin)
96                                 {
97                                 /* ERROR */
98                                 goto err;
99                                 }
100                         }
101                 rv = FIPS_drbg_generate(dctx, out, rcnt, 0, 0, adin, adinlen);
102                 if (adin)
103                         {
104                         if (dctx->cleanup_adin)
105                                 dctx->cleanup_adin(dctx, adin, adinlen);
106                         adin = NULL;
107                         }
108                 if (!rv)
109                         goto err;
110                 out += rcnt;
111                 count -= rcnt;
112                 }
113         while (count);
114         rv = 1;
115         err:
116         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
117         return rv;
118         }
119
120 static int fips_drbg_status(void)
121         {
122         DRBG_CTX *dctx = &ossl_dctx;
123         int rv;
124         CRYPTO_r_lock(CRYPTO_LOCK_RAND);
125         rv = dctx->status == DRBG_STATUS_READY ? 1 : 0;
126         CRYPTO_r_unlock(CRYPTO_LOCK_RAND);
127         return rv;
128         }
129
130 static void fips_drbg_cleanup(void)
131         {
132         DRBG_CTX *dctx = &ossl_dctx;
133         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
134         FIPS_drbg_uninstantiate(dctx);
135         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
136         }
137
138 static int fips_drbg_seed(const void *seed, int seedlen)
139         {
140         DRBG_CTX *dctx = &ossl_dctx;
141         int rv = 1;
142         if (dctx->rand_seed_cb)
143                 {
144                 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
145                 rv = dctx->rand_seed_cb(dctx, seed, seedlen);
146                 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
147                 }
148         return rv;
149         }
150
151 static int fips_drbg_add(const void *seed, int seedlen,
152                                         double add_entropy)
153         {
154         DRBG_CTX *dctx = &ossl_dctx;
155         int rv = 1;
156         if (dctx->rand_add_cb)
157                 {
158                 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
159                 rv = dctx->rand_add_cb(dctx, seed, seedlen, add_entropy);
160                 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
161                 }
162         return rv;
163         }
164
165 static const RAND_METHOD rand_drbg_meth =
166         {
167         fips_drbg_seed,
168         fips_drbg_bytes,
169         fips_drbg_cleanup,
170         fips_drbg_add,
171         fips_drbg_bytes,
172         fips_drbg_status
173         };
174
175 const RAND_METHOD *FIPS_drbg_method(void)
176         {
177         return &rand_drbg_meth;
178         }
179