32809d41ac111343deac92b29245f5e248eed2a8
[openssl.git] / fips / rand / fips_rand_lcl.h
1 /* fips/rand/fips_rand_lcl.h */
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 typedef struct drbg_hash_ctx_st DRBG_HASH_CTX;
55 typedef struct drbg_hmac_ctx_st DRBG_HMAC_CTX;
56 typedef struct drbg_ctr_ctx_st DRBG_CTR_CTX;
57 typedef struct drbg_ec_ctx_st DRBG_EC_CTX;
58
59 /* 888 bits from 10.1 table 2 */
60 #define HASH_PRNG_MAX_SEEDLEN   111
61
62 struct drbg_hash_ctx_st
63         {
64         const EVP_MD *md;
65         EVP_MD_CTX mctx;
66         unsigned char V[HASH_PRNG_MAX_SEEDLEN];
67         unsigned char C[HASH_PRNG_MAX_SEEDLEN];
68         /* Temporary value storage: should always exceed max digest length */
69         unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN];
70         };
71
72 struct drbg_hmac_ctx_st
73         {
74         const EVP_MD *md;
75         HMAC_CTX hctx;
76         unsigned char K[EVP_MAX_MD_SIZE];
77         unsigned char V[EVP_MAX_MD_SIZE];
78         };
79
80 struct drbg_ctr_ctx_st
81         {
82         AES_KEY ks;
83         size_t keylen;
84         unsigned char K[32];
85         unsigned char V[16];
86         /* Temp variables used by derivation function */
87         AES_KEY df_ks;
88         AES_KEY df_kxks;
89         /* Temporary block storage used by ctr_df */
90         unsigned char bltmp[16];
91         size_t bltmp_pos;
92         unsigned char KX[48];
93         };
94
95 /* Maximum seed length */
96 #define EC_PRNG_MAX_SEEDLEN     66
97
98 struct drbg_ec_ctx_st
99         {
100         /* Message digest to use */
101         const EVP_MD *md;
102         /* Curve to use: generator is point P */
103         EC_GROUP *curve;
104         /* Point Q */
105         EC_POINT *Q;
106         /* Temporary point */
107         EC_POINT *ptmp;
108         size_t exbits;
109         /* Secret s value */
110         BIGNUM *s;
111         /* Buffer to store byte version of s value */
112         unsigned char sbuf[EC_PRNG_MAX_SEEDLEN];
113         /* Buffer to store byte version of t value */
114         unsigned char tbuf[EC_PRNG_MAX_SEEDLEN];
115         /* Digest context */
116         EVP_MD_CTX mctx;
117         /* Temporary value storage: should always exceed max digest length */
118         unsigned char vtmp[EC_PRNG_MAX_SEEDLEN];
119         /* Flag to indicate s = s * P has been deferred */
120         int sp_defer;
121         /* Temp BN context */
122         BN_CTX *bctx;
123         };
124
125 /* DRBG flags */
126
127 /* Functions shouldn't call err library */
128 #define DRBG_FLAG_NOERR                 0x4
129 /* Custom reseed checking */
130 #define DRBG_CUSTOM_RESEED              0x8
131
132 /* DRBG status values */
133 /* not initialised */
134 #define DRBG_STATUS_UNINITIALISED       0
135 /* ok and ready to generate random bits */
136 #define DRBG_STATUS_READY               1
137 /* reseed required */
138 #define DRBG_STATUS_RESEED              2
139 /* fatal error condition */
140 #define DRBG_STATUS_ERROR               3
141
142 /* A default maximum length: larger than any reasonable value used in pratice */
143
144 #define DRBG_MAX_LENGTH                 0x7ffffff0
145 /* Maximum DRBG block length: all md sizes are bigger than cipher blocks sizes
146  * so use max digest length.
147  */
148 #define DRBG_MAX_BLOCK                  EVP_MAX_MD_SIZE
149
150 #define DRBG_HEALTH_INTERVAL            (1 << 24)
151
152 /* DRBG context structure */
153
154 struct drbg_ctx_st
155         {
156         /* First types common to all implementations */
157         /* DRBG type: a NID for the underlying algorithm */
158         int type;
159         /* Various flags */
160         unsigned int flags;
161         /* Used for periodic health checks */
162         int health_check_cnt, health_check_interval;
163
164         /* The following parameters are setup by mechanism drbg_init() call */
165         int strength;
166         size_t blocklength;
167         size_t max_request;
168
169         size_t min_entropy, max_entropy;
170         size_t min_nonce, max_nonce;
171         size_t max_pers, max_adin;
172         unsigned int reseed_counter;
173         unsigned int reseed_interval;
174         size_t seedlen;
175         int status;
176         /* Application data: typically used by test get_entropy */
177         void *app_data;
178         /* Implementation specific structures */
179         union 
180                 {
181                 DRBG_HASH_CTX hash;
182                 DRBG_HMAC_CTX hmac;
183                 DRBG_CTR_CTX  ctr;
184                 DRBG_EC_CTX  ec;
185                 } d;
186         /* Initialiase PRNG and setup callbacks below */
187         int (*init)(DRBG_CTX *ctx, int nid, int security, unsigned int flags);
188         /* Intantiate PRNG */
189         int (*instantiate)(DRBG_CTX *ctx,
190                                 const unsigned char *ent, size_t entlen,
191                                 const unsigned char *nonce, size_t noncelen,
192                                 const unsigned char *pers, size_t perslen);
193         /* reseed */
194         int (*reseed)(DRBG_CTX *ctx,
195                                 const unsigned char *ent, size_t entlen,
196                                 const unsigned char *adin, size_t adinlen);
197         /* generat output */
198         int (*generate)(DRBG_CTX *ctx,
199                                 unsigned char *out, size_t outlen,
200                                 const unsigned char *adin, size_t adinlen);
201         /* uninstantiate */
202         int (*uninstantiate)(DRBG_CTX *ctx);
203
204         /* Entropy source block length */
205         size_t entropy_blocklen;
206
207         /* entropy gathering function */
208         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
209                                 int entropy, size_t min_len, size_t max_len);
210         /* Indicates we have finished with entropy buffer */
211         void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
212
213         /* nonce gathering function */
214         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
215                                 int entropy, size_t min_len, size_t max_len);
216         /* Indicates we have finished with nonce buffer */
217         void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
218
219         /* Continuous random number test temporary area */
220         /* Last block */        
221         unsigned char lb[EVP_MAX_MD_SIZE];
222         /* set if lb is valid */
223         int lb_valid;
224
225         /* Callbacks used when called through RAND interface */
226         /* Get any additional input for generate */
227         size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout);
228         void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
229         /* Callback for RAND_seed(), RAND_add() */
230         int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num);
231         int (*rand_add_cb)(DRBG_CTX *ctx,
232                                 const void *buf, int num, double entropy);
233         };
234
235
236 int fips_drbg_ctr_init(DRBG_CTX *dctx);
237 int fips_drbg_hash_init(DRBG_CTX *dctx);
238 int fips_drbg_hmac_init(DRBG_CTX *dctx);
239 int fips_drbg_ec_init(DRBG_CTX *dctx);
240 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags);
241 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out);