Initial, provisional, subject to wholesale change, untested, probably
[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_ctr_ctx_st DRBG_CTR_CTX;
56
57 /* 888 bits from 10.1 table 2 */
58 #define HASH_PRNG_MAX_SEEDLEN   111
59
60 struct drbg_hash_ctx_st
61         {
62         const EVP_MD *md;
63         EVP_MD_CTX mctx;
64         unsigned char V[HASH_PRNG_MAX_SEEDLEN];
65         unsigned char C[HASH_PRNG_MAX_SEEDLEN];
66         /* Temporary value storage: should always exceed max digest length */
67         unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN];
68         };
69
70 struct drbg_ctr_ctx_st
71         {
72         AES_KEY ks;
73         size_t keylen;
74         unsigned char K[32];
75         unsigned char V[16];
76         /* Temp variables used by derivation function */
77         AES_KEY df_ks;
78         AES_KEY df_kxks;
79         /* Temporary block storage used by ctr_df */
80         unsigned char bltmp[16];
81         size_t bltmp_pos;
82         unsigned char KX[48];
83         };
84
85 /* DRBG flags */
86
87 /* Enable prediction resistance */
88 #define DRBG_FLAG_PREDICTION_RESISTANCE 0x1
89 /* CTR only: use derivation function */
90 #define DRBG_FLAG_CTR_USE_DF            0x2
91 /* PRNG is in test state */
92 #define DRBG_FLAG_TEST                  0x4
93
94 /* DRBG status values */
95 /* not initialised */
96 #define DRBG_STATUS_UNINITIALISED       0
97 /* ok and ready to generate random bits */
98 #define DRBG_STATUS_READY               1
99 /* reseed required */
100 #define DRBG_STATUS_RESEED              2
101 /* fatal error condition */
102 #define DRBG_STATUS_ERROR               3
103
104 /* Maximum values for temp entropy and nonce */
105 #define DRBG_MAX_ENTROPY                1024
106 #define DRBG_MAX_NONCE                  1024
107
108 /* A default maximum length: larger than any reasonable value used in pratice */
109
110 #define DRBG_MAX_LENGTH                 0x7fffffff
111
112 /* DRBG context structure */
113
114 struct drbg_ctx_st
115         {
116         /* First types common to all implementations */
117         /* DRBG type: a NID for the underlying algorithm */
118         int type;
119         /* Various flags */
120         unsigned int flags;
121
122         /* The following parameters are setup by mechanism drbg_init() call */
123         int strength;
124         size_t blocklength;
125         size_t max_request;
126
127         size_t min_entropy, max_entropy;
128         size_t min_nonce, max_nonce;
129         size_t max_pers, max_adin;
130         unsigned int reseed_counter;
131         unsigned int reseed_interval;
132         size_t seedlen;
133         int status;
134         /* Application data: typically used by test get_entropy */
135         void *app_data;
136         /* Implementation specific structures */
137         union 
138                 {
139                 DRBG_HASH_CTX hash;
140                 DRBG_CTR_CTX  ctr;
141                 } d;
142         /* Initialiase PRNG and setup callbacks below */
143         int (*init)(DRBG_CTX *ctx, int nid, int security, unsigned int flags);
144         /* Intantiate PRNG */
145         int (*instantiate)(DRBG_CTX *ctx,
146                                 const unsigned char *ent, size_t entlen,
147                                 const unsigned char *nonce, size_t noncelen,
148                                 const unsigned char *pers, size_t perslen);
149         /* reseed */
150         int (*reseed)(DRBG_CTX *ctx,
151                                 const unsigned char *ent, size_t entlen,
152                                 const unsigned char *adin, size_t adinlen);
153         /* generat output */
154         int (*generate)(DRBG_CTX *ctx,
155                                 unsigned char *out, size_t outlen,
156                                 const unsigned char *adin, size_t adinlen);
157         /* uninstantiate */
158         int (*uninstantiate)(DRBG_CTX *ctx);
159
160         unsigned char entropy[DRBG_MAX_ENTROPY];
161
162         /* entropy gathering function */
163         size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char *out,
164                                 int entropy, size_t min_len, size_t max_len);
165
166         unsigned char nonce[DRBG_MAX_NONCE];
167
168         /* nonce gathering function */
169         size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char *out,
170                                 int entropy, size_t min_len, size_t max_len);
171
172         };
173
174
175 int fips_drbg_ctr_init(DRBG_CTX *dctx);
176 int fips_drbg_hash_init(DRBG_CTX *dctx);