Update symbol translation table.
[openssl.git] / fips / rand / fips_drbg_selftest.c
1 /* fips/rand/fips_drbg_selftest.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 #include "fips_locl.h"
64
65 typedef struct {
66         int nid;
67         unsigned int flags;
68         const unsigned char *ent;
69         size_t entlen;
70         const unsigned char *nonce;
71         size_t noncelen;
72         const unsigned char *pers;
73         size_t perslen;
74         const unsigned char *adin;
75         size_t adinlen;
76         const unsigned char *entpr;
77         size_t entprlen;
78         const unsigned char *ading;
79         size_t adinglen;
80         const unsigned char *entg;
81         size_t entglen;
82         const unsigned char *kat;
83         size_t katlen;
84         } DRBG_SELFTEST_DATA;
85
86 #define make_drbg_test_data(nid, flag, pr) { nid, flag | DRBG_FLAG_TEST, \
87         pr##_entropyinput, sizeof(pr##_entropyinput), \
88         pr##_nonce, sizeof(pr##_nonce), \
89         pr##_personalizationstring, sizeof(pr##_personalizationstring), \
90         pr##_additionalinput, sizeof(pr##_additionalinput), \
91         pr##_entropyinputpr, sizeof(pr##_entropyinputpr), \
92         pr##_additionalinput2, sizeof(pr##_additionalinput2), \
93         pr##_entropyinputpr2, sizeof(pr##_entropyinputpr2), \
94         pr##_returnedbits, sizeof(pr##_returnedbits), \
95         }
96
97 #define make_drbg_test_data_df(nid, pr) \
98         make_drbg_test_data(nid, DRBG_FLAG_CTR_USE_DF, pr)
99
100 /* AES-128 use df PR */
101 static const unsigned char aes_128_use_df_entropyinput[] =
102         {
103         0x98,0x38,0x99,0x81,0x1d,0x56,0x1a,0x04,0xb0,0x50,0xcd,0x14,
104         0xc3,0x90,0x0b,0x4f
105         };
106
107 static const unsigned char aes_128_use_df_nonce[] =
108         {
109         0xa8,0xa0,0x80,0x8a,0x65,0xb7,0x38,0x22
110         };
111
112 static const unsigned char aes_128_use_df_personalizationstring[] =
113         {
114         0x67,0x4f,0x85,0x01,0x15,0x51,0x85,0xdd,0x97,0xda,0xf7,0x09,
115         0xbc,0x61,0xaf,0x23
116         };
117
118 static const unsigned char aes_128_use_df_additionalinput[] =
119         {
120         0x01,0xba,0xa8,0x13,0x9e,0xd4,0xb7,0xff,0x86,0x34,0x01,0xa0,
121         0xb6,0x17,0x96,0x55
122         };
123
124 static const unsigned char aes_128_use_df_entropyinputpr[] =
125         {
126         0x60,0x76,0xf6,0x12,0x6b,0x92,0xbe,0xd7,0x75,0x6e,0x78,0x1f,
127         0x0d,0xc1,0x0d,0x56
128         };
129
130 static const unsigned char aes_128_use_df_additionalinput2[] =
131         {
132         0xf0,0xd6,0x5b,0xa3,0x7c,0x1e,0xa3,0x65,0x08,0xf9,0xdd,0x90,
133         0xde,0x5f,0xb4,0x27
134         };
135
136 static const unsigned char aes_128_use_df_entropyinputpr2[] =
137         {
138         0x34,0x55,0x02,0xa9,0x30,0xf0,0x78,0x0a,0xa2,0xae,0x74,0x46,
139         0xe5,0xad,0xbb,0xd6
140         };
141
142 static const unsigned char aes_128_use_df_returnedbits[] =
143         {
144         0x48,0x52,0xb6,0x9f,0xf2,0xfe,0xe1,0x12,0xaf,0x22,0x87,0xd7,
145         0x46,0x64,0x96,0xec
146         };
147
148
149 /* AES-192 use df PR */
150 static const unsigned char aes_192_use_df_entropyinput[] =
151         {
152         0x12,0xf6,0xff,0xc5,0x81,0x8c,0x15,0xd7,0x33,0x0c,0x4f,0x45,
153         0xbf,0x2a,0x97,0xd2,0xe0,0xe0,0xbd,0x48,0x4e,0x83,0x76,0x25
154         };
155
156 static const unsigned char aes_192_use_df_nonce[] =
157         {
158         0x35,0xc8,0x16,0x8c,0xbd,0x1f,0x53,0xc4,0x6e,0x47,0x3a,0x74,
159         0x83,0xe6,0xe4,0x78
160         };
161
162 static const unsigned char aes_192_use_df_personalizationstring[] =
163         {
164         0xd6,0xe2,0x27,0x88,0xf4,0xce,0x9d,0xfc,0x92,0xde,0x07,0x57,
165         0x43,0x74,0x17,0x6e,0x63,0x54,0xaf,0x5a,0x3c,0xf8,0x23,0x65,
166         0x5a,0x15,0xb0,0x35,0x2a,0x6c,0x3c,0x3a
167         };
168
169 static const unsigned char aes_192_use_df_additionalinput[] =
170         {
171         0xad,0xa4,0x47,0xa4,0xcf,0x46,0x7b,0xf7,0x19,0xcc,0xda,0xbe,
172         0x11,0x42,0x85,0xaa,0x21,0x16,0x27,0xe6,0x35,0xdf,0xb5,0x87,
173         0x96,0x68,0x64,0x35,0x08,0x02,0xe9,0x19
174         };
175
176 static const unsigned char aes_192_use_df_entropyinputpr[] =
177         {
178         0x6f,0x41,0x2d,0x5e,0xd6,0xc9,0xf8,0x6a,0x22,0x00,0xe0,0xfb,
179         0x4b,0xcd,0xbe,0x2d,0x98,0xff,0x1b,0xe2,0xb9,0x95,0x73,0xac
180         };
181
182 static const unsigned char aes_192_use_df_additionalinput2[] =
183         {
184         0x51,0xea,0xd8,0x8e,0xa0,0xd7,0x9c,0x22,0x3c,0x01,0xf6,0xdb,
185         0xe9,0xe4,0x60,0x1e,0x54,0x56,0x3b,0x5c,0xd2,0xf3,0xa0,0x1d,
186         0x5c,0xd0,0x85,0x48,0xc9,0x5f,0x12,0xb7
187         };
188
189 static const unsigned char aes_192_use_df_entropyinputpr2[] =
190         {
191         0xf7,0x1f,0x9f,0x0e,0x14,0x30,0xde,0x4c,0xf9,0x34,0x49,0xc5,
192         0x24,0x91,0xe3,0x30,0xfd,0x5f,0x1e,0x79,0x30,0xf5,0x58,0xe6
193         };
194
195 static const unsigned char aes_192_use_df_returnedbits[] =
196         {
197         0x5b,0x8a,0xca,0x2e,0x74,0xb6,0x6f,0x96,0x48,0xb0,0xe4,0xc1,
198         0x68,0x40,0xac,0xc7
199         };
200
201
202 /* AES-256 use df PR */
203 static const unsigned char aes_256_use_df_entropyinput[] =
204         {
205         0x2a,0x02,0xbe,0xaa,0xba,0xb4,0x6a,0x73,0x53,0x85,0xa9,0x2a,
206         0xae,0x4a,0xdc,0xeb,0xe8,0x07,0xfb,0xf3,0xbc,0xe3,0xf4,0x2e,
207         0x00,0x53,0x46,0x00,0x64,0x80,0xdd,0x57
208         };
209
210 static const unsigned char aes_256_use_df_nonce[] =
211         {
212         0x2c,0x86,0xa2,0xf9,0x70,0xb5,0xca,0xd3,0x9a,0x08,0xdc,0xb6,
213         0x6b,0xce,0xe5,0x05
214         };
215
216 static const unsigned char aes_256_use_df_personalizationstring[] =
217         {
218         0xdb,0x6c,0xe1,0x84,0xbe,0x07,0xae,0x55,0x4e,0x34,0x5d,0xb8,
219         0x47,0x98,0x85,0xe0,0x3d,0x3e,0x9f,0x60,0xfa,0x1c,0x7d,0x57,
220         0x19,0xe5,0x09,0xdc,0xe2,0x10,0x41,0xab
221         };
222
223 static const unsigned char aes_256_use_df_additionalinput[] =
224         {
225         0x1d,0xc3,0x11,0x93,0xcb,0xc4,0xf6,0xbb,0x57,0xb0,0x09,0x70,
226         0xb9,0xc6,0x05,0x86,0x4e,0x75,0x95,0x7d,0x3d,0xec,0xce,0xb4,
227         0x0b,0xe4,0xef,0xd1,0x7b,0xab,0x56,0x6f
228         };
229
230 static const unsigned char aes_256_use_df_entropyinputpr[] =
231         {
232         0x8f,0xb9,0xab,0xf9,0x33,0xcc,0xbe,0xc6,0xbd,0x8b,0x61,0x5a,
233         0xec,0xc6,0x4a,0x5b,0x03,0x21,0xe7,0x37,0x03,0x02,0xbc,0xa5,
234         0x28,0xb9,0xfe,0x7a,0xa8,0xef,0x6f,0xb0
235         };
236
237 static const unsigned char aes_256_use_df_additionalinput2[] =
238         {
239         0xd6,0x98,0x63,0x48,0x94,0x9f,0x26,0xf7,0x1f,0x44,0x13,0x23,
240         0xa7,0xde,0x09,0x12,0x90,0x04,0xce,0xbc,0xac,0x82,0x70,0x58,
241         0xba,0x7d,0xdc,0x25,0x1e,0xe4,0xbf,0x7c
242         };
243
244 static const unsigned char aes_256_use_df_entropyinputpr2[] =
245         {
246         0xe5,0x04,0xef,0x7c,0x8d,0x02,0xd7,0x68,0x95,0x4c,0x64,0x34,
247         0x30,0x3a,0xcb,0x07,0xc9,0x0a,0xef,0x26,0xc6,0x57,0x43,0xfb,
248         0x7d,0xbe,0xe2,0x61,0x75,0xcd,0xee,0x34
249         };
250
251 static const unsigned char aes_256_use_df_returnedbits[] =
252         {
253         0x75,0x6d,0x16,0xef,0x14,0xae,0xd9,0xc2,0x28,0x0b,0x66,0xff,
254         0x20,0x1f,0x21,0x33
255         };
256
257
258 /* AES-128 no df PR */
259 static const unsigned char aes_128_no_df_entropyinput[] =
260         {
261         0xbe,0x91,0xb9,0x09,0x91,0x13,0x0b,0xbd,0x7b,0x95,0x77,0xed,
262         0xf2,0x00,0xff,0x2a,0xec,0xbd,0x7a,0x11,0x59,0xe1,0x32,0x1a,
263         0xe3,0x9a,0xbd,0xa2,0xe4,0xd9,0x1a,0x39
264         };
265
266 static const unsigned char aes_128_no_df_nonce[] =
267         {
268         0x39,0xeb,0x7a,0x42,0x0b,0x7f,0x4f,0xd5
269         };
270
271 static const unsigned char aes_128_no_df_personalizationstring[] =
272         {
273         0xd0,0xe4,0x9c,0xf6,0x2f,0xc8,0xba,0x6d,0xb9,0x91,0x8f,0xc1,
274         0x45,0x5b,0xb9,0x4f,0xdb,0x36,0xd6,0x71,0x2c,0x4b,0x2a,0x4c,
275         0x50,0x4c,0x74,0xdb,0xc5,0x20,0x0b,0x3b
276         };
277
278 static const unsigned char aes_128_no_df_additionalinput[] =
279         {
280         0x7c,0x35,0x81,0x03,0x58,0x93,0x24,0xf7,0x9c,0x98,0x4a,0x9d,
281         0x94,0xbd,0x9d,0x77,0x64,0xda,0xa4,0x67,0x66,0xb7,0x43,0xde,
282         0xc5,0xd5,0x72,0x42,0x5a,0x7c,0x41,0x9f
283         };
284
285 static const unsigned char aes_128_no_df_entropyinputpr[] =
286         {
287         0x63,0xf6,0x0e,0xfe,0x56,0xad,0x8f,0x37,0xa8,0xa1,0x6a,0x83,
288         0x01,0xac,0x51,0xe0,0x86,0x26,0xce,0x5c,0x57,0x14,0xd8,0xde,
289         0x4d,0x93,0xb6,0x35,0xf4,0x85,0x18,0x60
290         };
291
292 static const unsigned char aes_128_no_df_additionalinput2[] =
293         {
294         0x90,0x0f,0x35,0x81,0xc5,0xf5,0xc8,0x1b,0x80,0x99,0xcd,0xe2,
295         0xbb,0xe2,0xc7,0x65,0x40,0x74,0x50,0x2b,0x89,0xb4,0x16,0x60,
296         0xd7,0x1e,0x15,0xbf,0x91,0xc9,0x15,0xc2
297         };
298
299 static const unsigned char aes_128_no_df_entropyinputpr2[] =
300         {
301         0xc7,0x9f,0xd6,0x9b,0xe2,0x74,0x3e,0x8c,0x12,0xdd,0x41,0xcd,
302         0x51,0x6b,0xd4,0x71,0x3e,0xd0,0x36,0xc7,0xb9,0xa6,0xaf,0xca,
303         0xc0,0x7e,0x89,0xc4,0x88,0x2b,0x4e,0x43
304         };
305
306 static const unsigned char aes_128_no_df_returnedbits[] =
307         {
308         0x8c,0x7f,0x69,0xbf,0xb8,0x07,0x17,0xa6,0x09,0xef,0xd2,0x0a,
309         0x5f,0x20,0x18,0x2f
310         };
311
312
313 /* AES-192 no df PR */
314 static const unsigned char aes_192_no_df_entropyinput[] =
315         {
316         0xd5,0xcb,0x5b,0xc5,0x5b,0xa6,0x97,0xb6,0x1e,0x57,0x92,0xbb,
317         0x14,0x72,0xeb,0xae,0x44,0x85,0x99,0xa3,0xa3,0x24,0xe5,0x91,
318         0x2e,0x34,0xa7,0x3f,0x48,0x7a,0xc4,0x72,0x54,0x65,0xe6,0x57,
319         0x94,0x1a,0x7c,0x2d
320         };
321
322 static const unsigned char aes_192_no_df_nonce[] =
323         {
324         0x74,0x7a,0x38,0x81,0xef,0xca,0xd1,0xb6,0x7b,0xb5,0x1e,0x62,
325         0xf9,0x80,0x2c,0xe5
326         };
327
328 static const unsigned char aes_192_no_df_personalizationstring[] =
329         {
330         0x03,0xf8,0xbe,0xe8,0x6a,0x90,0x2a,0x4f,0xbd,0x80,0xd0,0x31,
331         0xf0,0x59,0xa3,0xf6,0x87,0xd8,0x8d,0x0d,0xac,0x27,0xa2,0xd2,
332         0x91,0x72,0xa5,0xc1,0x07,0xac,0xbf,0xdb,0x5d,0xa1,0x7d,0x56,
333         0x7d,0x3f,0x09,0x8b
334         };
335
336 static const unsigned char aes_192_no_df_additionalinput[] =
337         {
338         0x3e,0x89,0x1b,0x17,0xcb,0xe3,0xc8,0x76,0x71,0x0d,0xaf,0x97,
339         0x1e,0x73,0xa6,0xc4,0x88,0x3d,0x46,0xad,0xf0,0xba,0xc3,0x7e,
340         0x17,0x10,0x0d,0x20,0x80,0x23,0x26,0xcc,0xe6,0xc4,0xc4,0xd8,
341         0xfe,0x1d,0x2a,0xbc
342         };
343
344 static const unsigned char aes_192_no_df_entropyinputpr[] =
345         {
346         0x3f,0x33,0xb8,0x1b,0xe1,0x1b,0xe7,0xbe,0x68,0x6f,0xd2,0xd8,
347         0x6f,0xb6,0xf0,0xd2,0xa1,0x1c,0x83,0x24,0xfe,0x5d,0xf2,0xe9,
348         0x4b,0xf0,0x63,0xa2,0xd8,0x76,0x9e,0x49,0x78,0x64,0x1f,0x98,
349         0xbc,0xee,0x7c,0x99
350         };
351
352 static const unsigned char aes_192_no_df_additionalinput2[] =
353         {
354         0x54,0x48,0xf9,0x6a,0x86,0x93,0xf3,0x7b,0x02,0x1b,0xf6,0x46,
355         0x3a,0x49,0x02,0x87,0x3f,0x54,0x82,0x7f,0xa1,0x45,0x41,0xa5,
356         0x88,0x4b,0xaa,0x90,0x12,0x40,0x46,0x22,0xed,0x7a,0x72,0xf7,
357         0x36,0xd5,0x5f,0x0f
358         };
359
360 static const unsigned char aes_192_no_df_entropyinputpr2[] =
361         {
362         0x00,0xdf,0xa1,0x50,0xc1,0xb9,0x82,0x7f,0x65,0xea,0x0f,0x14,
363         0x79,0xfe,0x6a,0x95,0x4b,0x96,0xae,0x89,0x28,0x52,0x49,0x05,
364         0xd9,0x00,0x9e,0x79,0x5e,0x04,0xdb,0xbb,0xec,0x09,0x16,0x53,
365         0x23,0xe9,0xac,0x08
366         };
367
368 static const unsigned char aes_192_no_df_returnedbits[] =
369         {
370         0x48,0xd6,0x66,0x61,0x93,0x8d,0xff,0x7d,0x42,0xf4,0x41,0x9a,
371         0x01,0x2a,0x34,0x09
372         };
373
374
375 /* AES-256 no df PR */
376 static const unsigned char aes_256_no_df_entropyinput[] =
377         {
378         0x7e,0x83,0x3f,0xa6,0x39,0xdc,0xcb,0x38,0x17,0x6a,0xa3,0x59,
379         0xa9,0x8c,0x1f,0x50,0xd3,0xdb,0x34,0xdd,0xa4,0x39,0x65,0xe4,
380         0x77,0x17,0x08,0x57,0x49,0x04,0xbd,0x68,0x5c,0x7d,0x2a,0xee,
381         0x0c,0xf2,0xfb,0x16,0xef,0x16,0x18,0x4d,0x32,0x6a,0x26,0x6c
382         };
383
384 static const unsigned char aes_256_no_df_nonce[] =
385         {
386         0xa3,0x8a,0xa4,0x6d,0xa6,0xc1,0x40,0xf8,0xa3,0x02,0xf1,0xac,
387         0xf3,0xea,0x7f,0x2d
388         };
389
390 static const unsigned char aes_256_no_df_personalizationstring[] =
391         {
392         0xc0,0x54,0x1e,0xa5,0x93,0xd9,0x8b,0x2b,0x43,0x15,0x2c,0x07,
393         0x26,0x25,0xc7,0x08,0xf0,0xb3,0x4b,0x44,0x96,0xfe,0xc7,0xc5,
394         0x64,0x27,0xaa,0x78,0x5b,0xbc,0x40,0x51,0xce,0x89,0x6b,0xc1,
395         0x3f,0x9c,0xa0,0x5c,0x75,0x98,0x24,0xc5,0xe1,0x3e,0x86,0xdb
396         };
397
398 static const unsigned char aes_256_no_df_additionalinput[] =
399         {
400         0x0e,0xe3,0x0f,0x07,0x90,0xe2,0xde,0x20,0xb6,0xf7,0x6f,0xef,
401         0x87,0xdc,0x7f,0xc4,0x0d,0x9d,0x05,0x31,0x91,0x87,0x8c,0x9a,
402         0x19,0x53,0xd2,0xf8,0x20,0x91,0xa0,0xef,0x97,0x59,0xea,0x12,
403         0x1b,0x2f,0x29,0x74,0x76,0x35,0xf7,0x71,0x5a,0x96,0xeb,0xbc
404         };
405
406 static const unsigned char aes_256_no_df_entropyinputpr[] =
407         {
408         0x37,0x26,0x9a,0xa6,0x28,0xe0,0x35,0x78,0x12,0x42,0x44,0x5c,
409         0x55,0xbc,0xc8,0xb6,0x1f,0x24,0xf3,0x32,0x88,0x02,0x69,0xa7,
410         0xed,0x1d,0xb7,0x4d,0x8b,0x44,0x12,0x21,0x5e,0x60,0x53,0x96,
411         0x3b,0xb9,0x31,0x7f,0x2a,0x87,0xbf,0x3c,0x07,0xbb,0x27,0x22
412         };
413
414 static const unsigned char aes_256_no_df_additionalinput2[] =
415         {
416         0xf1,0x24,0x35,0xa6,0x8c,0x93,0x28,0x7e,0x84,0xea,0x3d,0x27,
417         0x44,0x18,0xc9,0x13,0x73,0x49,0xb9,0x83,0x79,0x15,0x29,0x53,
418         0x2f,0xef,0x43,0x06,0xe7,0xcb,0x5c,0x0f,0x9f,0x10,0x4c,0x60,
419         0x7f,0xbf,0x0c,0x37,0x9b,0xe4,0x94,0x26,0xe5,0x3b,0xf5,0x63
420         };
421
422 static const unsigned char aes_256_no_df_entropyinputpr2[] =
423         {
424         0xdc,0x91,0x48,0x11,0x63,0x7b,0x79,0x41,0x36,0x8c,0x4f,0xe2,
425         0xc9,0x84,0x04,0x9c,0xdc,0x5b,0x6c,0x8d,0x61,0x52,0xea,0xfa,
426         0x92,0x3b,0xb4,0x36,0x4c,0x06,0x4a,0xd1,0xb1,0x8e,0x32,0x03,
427         0xfd,0xa4,0xf7,0x5a,0xa6,0x5c,0x63,0xa1,0xb9,0x96,0xfa,0x12
428         };
429
430 static const unsigned char aes_256_no_df_returnedbits[] =
431         {
432         0x1c,0xba,0xfd,0x48,0x0f,0xf4,0x85,0x63,0xd6,0x7d,0x91,0x14,
433         0xef,0x67,0x6b,0x7f
434         };
435
436 /* SHA-1 PR */
437 static const unsigned char sha1_entropyinput[] =
438         {
439         0x5b,0xaf,0x30,0x1a,0xdc,0xd1,0x04,0xd7,0x95,0x72,0xd2,0xfb,
440         0xec,0x2d,0x62,0x2b
441         };
442
443 static const unsigned char sha1_nonce[] =
444         {
445         0xf3,0xd9,0xcb,0x92,0x5f,0x50,0x4c,0x99
446         };
447
448 static const unsigned char sha1_personalizationstring[] =
449         {
450         0x8f,0x56,0x70,0xd9,0x27,0xa2,0xb4,0xf1,0xb3,0xad,0xcf,0x10,
451         0x06,0x16,0x5c,0x11
452         };
453
454 static const unsigned char sha1_additionalinput[] =
455         {
456         0x49,0xdd,0x0c,0xb4,0xab,0x84,0xe1,0x7e,0x94,0x20,0xad,0x6c,
457         0xd7,0xd2,0x0b,0x84
458         };
459
460 static const unsigned char sha1_entropyinputpr[] =
461         {
462         0x23,0x4a,0xaf,0xf7,0x1a,0x0b,0x7e,0x51,0xdd,0x23,0x51,0x82,
463         0x2c,0x8c,0xa6,0xc5
464         };
465
466 static const unsigned char sha1_additionalinput2[] =
467         {
468         0x59,0xe6,0x93,0xcb,0x38,0x23,0xf5,0x7b,0x93,0x5a,0x4d,0xfa,
469         0x11,0xb8,0x88,0xde
470         };
471
472 static const unsigned char sha1_entropyinputpr2[] =
473         {
474         0x2e,0x00,0x78,0x5a,0xcd,0x30,0xea,0x73,0x37,0x8a,0x0d,0x12,
475         0x50,0x28,0x28,0x03
476         };
477
478 static const unsigned char sha1_returnedbits[] =
479         {
480         0xe7,0x87,0x8b,0x01,0xc1,0xd3,0xd8,0x43,0xd4,0x8f,0xcd,0x24,
481         0x54,0x67,0xa2,0x6e,0x17,0x94,0x73,0x1c
482         };
483
484
485 /* SHA-224 PR */
486 static const unsigned char sha224_entropyinput[] =
487         {
488         0xfc,0x31,0xc1,0x87,0x43,0x07,0xb1,0xe5,0x71,0x48,0x5d,0x0e,
489         0xad,0xf8,0x68,0x09,0x6f,0xfe,0x80,0x2a,0xc1,0x12,0xb8,0xa6
490         };
491
492 static const unsigned char sha224_nonce[] =
493         {
494         0xfd,0xba,0x25,0x2e,0xc1,0x7c,0x4e,0xa1,0x4d,0xef,0xeb,0x5d
495         };
496
497 static const unsigned char sha224_personalizationstring[] =
498         {
499         0xc9,0x15,0xe4,0x8c,0x2a,0x4c,0xc9,0xe6,0x23,0x5c,0xb8,0x5a,
500         0x97,0x89,0x6a,0x10,0x75,0x68,0x27,0x00,0x0e,0x6f,0x44,0x1e
501         };
502
503 static const unsigned char sha224_additionalinput[] =
504         {
505         0xd3,0xab,0x74,0x74,0xe7,0x80,0x87,0x9e,0x89,0x08,0xbe,0xf1,
506         0x99,0x09,0x26,0xa4,0x2b,0x8c,0xb7,0xa0,0xc2,0xcc,0xae,0x0a
507         };
508
509 static const unsigned char sha224_entropyinputpr[] =
510         {
511         0xbd,0xc1,0x21,0x62,0x43,0x19,0x25,0x15,0x19,0xc5,0xcd,0x53,
512         0x9e,0xb4,0x17,0xff,0xaa,0x03,0xf6,0x5a,0x4d,0x69,0x28,0x0b
513         };
514
515 static const unsigned char sha224_additionalinput2[] =
516         {
517         0xdb,0xf5,0x57,0xea,0x5b,0xc8,0x0a,0xa9,0x32,0x72,0xcf,0x7d,
518         0xa4,0xeb,0x4f,0xbf,0x64,0x5d,0x74,0x04,0x0e,0x4e,0x0f,0xed
519         };
520
521 static const unsigned char sha224_entropyinputpr2[] =
522         {
523         0xab,0xce,0xe1,0xfd,0xaa,0x35,0x5c,0x0a,0xfe,0xd8,0x18,0xac,
524         0x92,0x79,0x79,0x53,0xbc,0xb5,0x45,0xf6,0xf9,0x73,0x7f,0x24
525         };
526
527 static const unsigned char sha224_returnedbits[] =
528         {
529         0xb2,0xc2,0x40,0xc4,0x2a,0x25,0x63,0xdb,0x99,0x59,0x7b,0x7b,
530         0xee,0xdb,0x51,0x8d,0x18,0x4c,0x09,0x26,0x22,0x1a,0xe9,0x76,
531         0x54,0x5f,0xb5,0x28
532         };
533
534
535 /* SHA-256 PR */
536 static const unsigned char sha256_entropyinput[] =
537         {
538         0xbc,0x67,0x4e,0x95,0xf1,0xca,0x71,0xdd,0xd3,0x97,0x3a,0x39,
539         0x3f,0x3d,0x7f,0xf2,0x99,0x02,0xcf,0x12,0x02,0xea,0xcc,0xf3,
540         0xd7,0xe7,0xcc,0x08,0x6c,0x41,0xb1,0xed
541         };
542
543 static const unsigned char sha256_nonce[] =
544         {
545         0x44,0x06,0xa7,0x61,0x15,0x0a,0x6a,0x2d,0xa9,0x18,0x10,0xb5,
546         0x6d,0xf0,0xd4,0xf7
547         };
548
549 static const unsigned char sha256_personalizationstring[] =
550         {
551         0x8f,0x39,0xd5,0x6a,0x46,0xde,0xa2,0x57,0xdf,0x39,0xdb,0xca,
552         0x13,0xca,0x51,0x0f,0x43,0x2a,0x77,0x3a,0x38,0x7a,0x3b,0x35,
553         0x1e,0x13,0x26,0x0e,0xc1,0x6b,0xb6,0x81
554         };
555
556 static const unsigned char sha256_additionalinput[] =
557         {
558         0x95,0x01,0xbe,0x52,0xaa,0xc4,0x32,0x5a,0x3c,0xea,0x57,0xc4,
559         0x5c,0xfa,0x25,0x4e,0xc5,0xf3,0xc2,0xa6,0x39,0xce,0x00,0x97,
560         0x19,0x50,0x17,0x71,0x44,0x13,0xa5,0xbd
561         };
562
563 static const unsigned char sha256_entropyinputpr[] =
564         {
565         0x8e,0x8a,0x19,0x03,0xa7,0x77,0xaa,0x64,0x4f,0x11,0x45,0x1d,
566         0x66,0x74,0x88,0xdf,0x2c,0x9b,0xc3,0xc8,0xbb,0x8c,0x99,0x34,
567         0xc6,0xc7,0xdb,0xc1,0x92,0xef,0xa3,0xa3
568         };
569
570 static const unsigned char sha256_additionalinput2[] =
571         {
572         0x2b,0x91,0x7f,0xf3,0x78,0x3f,0x18,0x73,0x7c,0x5f,0xc2,0xda,
573         0x1d,0x8c,0xc4,0xcd,0x74,0x4d,0xc1,0x7a,0x6c,0xe2,0x73,0x07,
574         0x9d,0x55,0xa8,0x42,0x69,0xc0,0x7c,0x85
575         };
576
577 static const unsigned char sha256_entropyinputpr2[] =
578         {
579         0x4c,0x3f,0xee,0x8b,0x98,0x0e,0x55,0x7e,0xab,0xc3,0xd3,0x0e,
580         0x35,0x33,0x72,0x75,0x9f,0x4b,0x87,0xce,0x05,0xbe,0xd4,0x6b,
581         0x70,0xec,0xdb,0x5a,0x57,0x14,0x83,0x34
582         };
583
584 static const unsigned char sha256_returnedbits[] =
585         {
586         0xa5,0x2c,0xab,0x93,0x63,0x57,0x5d,0x60,0x80,0x4c,0x71,0xbb,
587         0xc2,0x3d,0x43,0x13,0xd8,0xe1,0x60,0x63,0x5e,0xf8,0xb1,0x4c,
588         0x93,0x06,0x86,0x9e,0x03,0x0a,0x16,0x75
589         };
590
591
592 /* SHA-384 PR */
593 static const unsigned char sha384_entropyinput[] =
594         {
595         0xad,0x6c,0xfb,0xdd,0x40,0xd9,0xf1,0x0a,0xc6,0xe4,0x28,0xf9,
596         0x8c,0xb1,0x66,0xce,0x7e,0x7f,0xbb,0xea,0xcd,0x79,0x3d,0x54,
597         0xc6,0xc0,0x07,0x68,0xf0,0xb7,0x73,0xc5
598         };
599
600 static const unsigned char sha384_nonce[] =
601         {
602         0xfb,0xe1,0xb2,0x81,0x77,0xb0,0x14,0x94,0xae,0xbb,0x8d,0x01,
603         0xfb,0x74,0xc9,0xa1
604         };
605
606 static const unsigned char sha384_personalizationstring[] =
607         {
608         0x02,0x8e,0xa9,0xc2,0x7e,0x0e,0x78,0xea,0x29,0xca,0x19,0xd4,
609         0x58,0x89,0x71,0x45,0x18,0xd9,0x1f,0xc0,0x8f,0x92,0x02,0xb8,
610         0x90,0xa7,0xec,0xf6,0x7f,0x33,0xa6,0x47
611         };
612
613 static const unsigned char sha384_additionalinput[] =
614         {
615         0x98,0x0e,0xe3,0x3c,0x8e,0x6b,0x82,0xc0,0x56,0xd0,0x93,0x14,
616         0x6a,0x79,0xa8,0xec,0x09,0xb7,0x49,0x01,0x71,0xdb,0x58,0x97,
617         0x5a,0x61,0xa5,0x4e,0xb4,0x5f,0xce,0x2b
618         };
619
620 static const unsigned char sha384_entropyinputpr[] =
621         {
622         0x50,0xef,0xaa,0x65,0x95,0x0d,0x4f,0x97,0x3e,0x57,0x59,0x48,
623         0xf9,0x4e,0xee,0x51,0xf8,0x46,0xec,0x4c,0x2d,0x55,0x47,0x23,
624         0xc5,0x7b,0xa3,0xda,0xe5,0x12,0x34,0x9a
625         };
626
627 static const unsigned char sha384_additionalinput2[] =
628         {
629         0x1c,0xcd,0xe0,0xc1,0x15,0xd4,0x7f,0xfa,0x9e,0x16,0xe7,0x6d,
630         0x22,0x55,0xfd,0x34,0x3f,0xec,0x1d,0x40,0x9e,0xdd,0x15,0x07,
631         0x13,0x1c,0x65,0x6e,0xf7,0x1c,0xb6,0xf8
632         };
633
634 static const unsigned char sha384_entropyinputpr2[] =
635         {
636         0xa0,0x8b,0x48,0xdc,0x7b,0x74,0x54,0xd0,0x0a,0x10,0x0e,0xc9,
637         0xf2,0xe0,0xf0,0x30,0x38,0xf5,0x46,0x27,0xf4,0x54,0x06,0x95,
638         0x56,0xab,0xf4,0x74,0xd8,0x34,0xf5,0x5d
639         };
640
641 static const unsigned char sha384_returnedbits[] =
642         {
643         0x03,0x54,0x62,0xaa,0x5c,0x61,0x28,0xfc,0x96,0x04,0xd6,0x4f,
644         0x50,0x5c,0x9e,0x7c,0x9e,0x1d,0x41,0x76,0x41,0xa0,0x60,0x70,
645         0x62,0x4f,0x42,0x1a,0x69,0xce,0x30,0xc4,0xf7,0x89,0xc8,0x93,
646         0xed,0xe9,0x42,0xf4,0x59,0x55,0x7c,0x6c,0xd3,0x4e,0xff,0x05
647         };
648
649
650 /* SHA-512 PR */
651 static const unsigned char sha512_entropyinput[] =
652         {
653         0x22,0xb1,0x72,0xe3,0xc4,0x87,0xe7,0x76,0x4e,0x85,0xb5,0xca,
654         0x86,0x4f,0x21,0x2b,0x4f,0x29,0x8e,0x8a,0xfc,0x88,0xfc,0xa1,
655         0xf6,0xd7,0xc1,0x63,0x90,0x8d,0x85,0xa9
656         };
657
658 static const unsigned char sha512_nonce[] =
659         {
660         0xcc,0x8b,0x86,0x21,0xa7,0xbe,0xd3,0xe1,0xde,0xd2,0x47,0xfc,
661         0x9c,0x4a,0xdb,0x85
662         };
663
664 static const unsigned char sha512_personalizationstring[] =
665         {
666         0xb7,0x7c,0xb3,0x4f,0xf8,0xcd,0x19,0x89,0xdb,0x0c,0xcf,0xc9,
667         0xce,0xcd,0x48,0xcd,0x62,0x9c,0x51,0x38,0x85,0xe4,0x6c,0x17,
668         0x02,0x1b,0x6b,0xb5,0x3c,0x31,0x4f,0xa1
669         };
670
671 static const unsigned char sha512_additionalinput[] =
672         {
673         0x69,0x3f,0xcf,0xf5,0x38,0x09,0x0d,0x3c,0xfb,0xea,0x94,0xa6,
674         0xf3,0xdc,0xb3,0xa8,0xcb,0x61,0x3b,0x8d,0x8e,0x31,0x94,0xc2,
675         0xe8,0x20,0x1c,0x62,0xa0,0x54,0xc2,0x03
676         };
677
678 static const unsigned char sha512_entropyinputpr[] =
679         {
680         0xa0,0xcf,0x6f,0x0f,0x55,0x88,0x84,0xad,0x8d,0x2e,0x08,0x91,
681         0x8a,0x65,0xc0,0xb4,0xc9,0xbe,0x21,0x29,0xbe,0x23,0x2d,0x2b,
682         0xd1,0x81,0x90,0x66,0x97,0xb6,0xfa,0x84
683         };
684
685 static const unsigned char sha512_additionalinput2[] =
686         {
687         0x1f,0x5e,0x49,0xb5,0xa3,0xfa,0xe8,0x89,0xc5,0x1b,0x39,0x2b,
688         0x9e,0xc7,0x36,0x85,0x5b,0xa9,0x9f,0x91,0x79,0xfe,0x5c,0xe6,
689         0x41,0xbe,0x14,0x87,0x81,0x08,0x0d,0xee
690         };
691 /* NB: not constant so we can corrupt it */
692 static const unsigned char sha512_entropyinputpr2[] =
693         {
694         0xed,0x22,0x42,0x61,0xa7,0x4c,0xed,0xc7,0x10,0x82,0x61,0x17,
695         0xaa,0x7d,0xdb,0x4e,0x1c,0x96,0x61,0x23,0xcd,0x8f,0x84,0x77,
696         0xc3,0xa2,0x55,0xff,0xbb,0xc9,0xa6,0x2f
697         };
698
699 static const unsigned char sha512_returnedbits[] =
700         {
701         0x79,0x60,0x41,0xaa,0x6c,0xdd,0x17,0x28,0xc0,0x4d,0xc0,0x17,
702         0xc0,0x66,0x46,0x67,0x0d,0x20,0xe2,0x67,0x96,0xd5,0x2a,0xf4,
703         0x58,0x0a,0x06,0xab,0xc1,0x4c,0x70,0xc1,0xb8,0x9d,0x68,0x79,
704         0x28,0x07,0x38,0x4a,0xc3,0xec,0x3b,0x19,0x02,0xe7,0x13,0x82,
705         0x8f,0xc3,0xed,0x59,0x88,0xdd,0x88,0xaf,0xac,0xf0,0x57,0x6c,
706         0x14,0x0b,0x50,0x11
707         };
708
709
710
711 static DRBG_SELFTEST_DATA drbg_test[] = {
712         make_drbg_test_data_df(NID_aes_128_ctr, aes_128_use_df),
713         make_drbg_test_data_df(NID_aes_192_ctr, aes_192_use_df),
714         make_drbg_test_data_df(NID_aes_256_ctr, aes_256_use_df),
715         make_drbg_test_data(NID_aes_128_ctr, 0, aes_128_no_df),
716         make_drbg_test_data(NID_aes_192_ctr, 0, aes_192_no_df),
717         make_drbg_test_data(NID_aes_256_ctr, 0, aes_256_no_df),
718         make_drbg_test_data(NID_sha1, 0, sha1),
719         make_drbg_test_data(NID_sha224, 0, sha224),
720         make_drbg_test_data(NID_sha256, 0, sha256),
721         make_drbg_test_data(NID_sha384, 0, sha384),
722         make_drbg_test_data(NID_sha512, 0, sha512),
723         {0,0,0}
724         };
725
726 typedef struct 
727         {
728         const unsigned char *ent;
729         size_t entlen;
730         int entcnt;
731         const unsigned char *nonce;
732         size_t noncelen;
733         int noncecnt;
734         } TEST_ENT;
735
736 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
737                                 int entropy, size_t min_len, size_t max_len)
738         {
739         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
740         *pout = (unsigned char *)t->ent;
741         t->entcnt++;
742         return t->entlen;
743         }
744
745 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
746                                 int entropy, size_t min_len, size_t max_len)
747         {
748         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
749         *pout = (unsigned char *)t->nonce;
750         t->noncecnt++;
751         return t->noncelen;
752         }
753
754 static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
755         {
756         TEST_ENT t;
757         int rv = 0;
758         size_t adinlen;
759         unsigned char randout[1024];
760         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
761                 return 0;
762         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
763                 return 0;
764
765         FIPS_drbg_set_app_data(dctx, &t);
766
767         t.ent = td->ent;
768         t.entlen = td->entlen;
769         t.nonce = td->nonce;
770         t.noncelen = td->noncelen;
771         t.entcnt = 0;
772         t.noncecnt = 0;
773
774         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
775                 goto err;
776
777         t.ent = td->entpr;
778         t.entlen = td->entprlen;
779
780         /* Note for CTR without DF some additional input values
781          * ignore bytes after the keylength: so reduce adinlen
782          * to half to ensure invalid data is fed in.
783          */
784         if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
785                 adinlen = td->adinlen / 2;
786         else
787                 adinlen = td->adinlen;
788         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
789                                 td->adin, adinlen))
790                 goto err;
791
792         t.ent = td->entg;
793         t.entlen = td->entglen;
794
795         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
796                                 td->ading, td->adinglen))
797                 goto err;
798
799         if (memcmp(randout, td->kat, td->katlen))
800                 goto err;
801
802         rv = 1;
803
804         err:
805         if (rv == 0)
806                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_SELFTEST_FAILED);
807         FIPS_drbg_uninstantiate(dctx);
808         
809         return rv;
810         }
811
812 /* This is the "health check" function required by SP800-90. Induce several
813  * failure modes and check an error condition is set.
814  */
815
816 static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
817         {
818         unsigned char randout[1024];
819         TEST_ENT t;
820         size_t i;
821         unsigned char *p = (unsigned char *)dctx;
822
823         /* Initialise DRBG */
824
825         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
826                 goto err;
827
828         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
829                 goto err;
830
831         FIPS_drbg_set_app_data(dctx, &t);
832
833         t.ent = td->ent;
834         t.entlen = td->entlen;
835         t.nonce = td->nonce;
836         t.noncelen = td->noncelen;
837         t.entcnt = 0;
838         t.noncecnt = 0;
839
840         /* Don't report induced errors */
841         dctx->flags |= DRBG_FLAG_NOERR;
842
843         /* Try too large a personalisation length */
844         if (FIPS_drbg_instantiate(dctx, td->pers, dctx->max_pers + 1) > 0)
845                 {
846                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_PERSONALISATION_ERROR_UNDETECTED);
847                 goto err;
848                 }
849
850         /* Test entropy source failure detection */
851
852         t.entlen = 0;
853         if (FIPS_drbg_instantiate(dctx, td->pers, td->perslen) > 0)
854                 {
855                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
856                 goto err;
857                 }
858
859         /* Try to generate output from uninstantiated DRBG */
860         if (FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
861                                 td->adin, td->adinlen))
862                 {
863                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_GENERATE_ERROR_UNDETECTED);
864                 goto err;
865                 }
866
867         dctx->flags &= ~DRBG_FLAG_NOERR;
868         if (!FIPS_drbg_uninstantiate(dctx))
869                 {
870                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
871                 goto err;
872                 }
873
874         /* Instantiate with valid data. NB: errors now reported again */
875         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
876                 goto err;
877         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
878                 goto err;
879         FIPS_drbg_set_app_data(dctx, &t);
880
881         t.entlen = td->entlen;
882         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
883                 goto err;
884
885         /* Check generation is now OK */
886         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
887                                 td->adin, td->adinlen))
888                 goto err;
889
890         /* Try to generate with too high a strength.
891          */
892
893         dctx->flags |= DRBG_FLAG_NOERR;
894         if (dctx->strength != 256)
895                 {
896                 if (FIPS_drbg_generate(dctx, randout, td->katlen, 256, 0,
897                                         td->adin, td->adinlen))
898                         {
899                         FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_STRENGTH_ERROR_UNDETECTED);
900
901                         goto err;
902                         }
903                 }
904
905         /* Request too much data for one request */
906         if (FIPS_drbg_generate(dctx, randout, dctx->max_request + 1, 0, 0,
907                                 td->adin, td->adinlen))
908                 {
909                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_REQUEST_LENGTH_ERROR_UNDETECTED);
910                 goto err;
911                 }
912
913         /* Check prediction resistance request fails if entropy source
914          * failure.
915          */
916
917         t.entlen = 0;
918
919         if (FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
920                                 td->adin, td->adinlen))
921                 {
922                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
923                 goto err;
924                 }
925                 
926         dctx->flags &= ~DRBG_FLAG_NOERR;
927
928         if (!FIPS_drbg_uninstantiate(dctx))
929                 {
930                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
931                 goto err;
932                 }
933
934
935         /* Instantiate again with valid data */
936
937         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
938                 goto err;
939         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
940                 goto err;
941         FIPS_drbg_set_app_data(dctx, &t);
942
943         t.entlen = td->entlen;
944         /* Test reseeding works */
945         dctx->reseed_interval = 2;
946         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
947                 goto err;
948
949         /* Check generation is now OK */
950         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
951                                 td->adin, td->adinlen))
952                 goto err;
953         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
954                                 td->adin, td->adinlen))
955                 goto err;
956
957         /* DRBG should now require a reseed */
958         if (dctx->status != DRBG_STATUS_RESEED)
959                 {
960                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_RESEED_COUNTER_ERROR);
961                 goto err;
962                 }
963
964
965         /* Generate again and check entropy has been requested for reseed */
966         t.entcnt = 0;
967         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
968                                 td->adin, td->adinlen))
969                 goto err;
970         if (t.entcnt != 1)
971                 {
972                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_NOT_REQUESTED_FOR_RESEED);
973                 goto err;
974                 }
975
976         FIPS_drbg_uninstantiate(dctx);
977         p = (unsigned char *)&dctx->d;
978         /* Standard says we have to check uninstantiate really zeroes
979          * the data...
980          */
981         for (i = 0; i < sizeof(dctx->d); i++)
982                 {
983                 if (*p != 0)
984                         {
985                         FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ZEROISE_ERROR);
986                         goto err;
987                         }
988                 p++;
989                 }
990
991         return 1;
992
993         err:
994         /* A real error as opposed to an induced one: underlying function will
995          * indicate the error.
996          */
997         if (!(dctx->flags & DRBG_FLAG_NOERR))
998                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_FUNCTION_ERROR);
999         FIPS_drbg_uninstantiate(dctx);
1000         return 0;
1001
1002         }
1003
1004
1005 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags)
1006         {
1007         int rv;
1008         DRBG_SELFTEST_DATA *td;
1009         for (td = drbg_test; td->nid != 0; td++)
1010                 {
1011                 if (td->nid == nid && td->flags == flags)
1012                         {
1013                         rv = fips_drbg_single_kat(dctx, td);
1014                         if (rv <= 0)
1015                                 return rv;
1016                         return fips_drbg_health_check(dctx, td);
1017                         }
1018                 }
1019         return 0;
1020         }
1021
1022 int FIPS_selftest_drbg(void)
1023         {
1024         DRBG_CTX *dctx;
1025         DRBG_SELFTEST_DATA *td;
1026         int rv = 1;
1027         dctx = FIPS_drbg_new(0, 0);
1028         if (!dctx)
1029                 return 0;
1030         for (td = drbg_test; td->nid != 0; td++)
1031                 {
1032                 if (!fips_post_started(FIPS_TEST_DRBG, td->nid, &td->flags))
1033                         return 1;
1034                 if (!fips_drbg_single_kat(dctx, td))
1035                         {
1036                         fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
1037                         rv = 0;
1038                         continue;
1039                         }
1040                 if (!fips_drbg_health_check(dctx, td))
1041                         {
1042                         fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
1043                         rv = 0;
1044                         continue;
1045                         }
1046                 if (!fips_post_success(FIPS_TEST_DRBG, td->nid, &td->flags))
1047                         return 0;
1048                 }
1049         FIPS_drbg_free(dctx);
1050         return rv;
1051         }
1052
1053
1054
1055