1 /* fips/rand/fips_drbg_selftest.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
54 #define OPENSSL_FIPSAPI
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"
68 const unsigned char *ent;
70 const unsigned char *nonce;
72 const unsigned char *pers;
74 const unsigned char *adin;
76 const unsigned char *entpr;
78 const unsigned char *ading;
80 const unsigned char *entg;
82 const unsigned char *kat;
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), \
97 #define make_drbg_test_data_df(nid, pr) \
98 make_drbg_test_data(nid, DRBG_FLAG_CTR_USE_DF, pr)
100 /* AES-128 use df PR */
101 static const unsigned char aes_128_use_df_entropyinput[] =
103 0x98,0x38,0x99,0x81,0x1d,0x56,0x1a,0x04,0xb0,0x50,0xcd,0x14,
107 static const unsigned char aes_128_use_df_nonce[] =
109 0xa8,0xa0,0x80,0x8a,0x65,0xb7,0x38,0x22
112 static const unsigned char aes_128_use_df_personalizationstring[] =
114 0x67,0x4f,0x85,0x01,0x15,0x51,0x85,0xdd,0x97,0xda,0xf7,0x09,
118 static const unsigned char aes_128_use_df_additionalinput[] =
120 0x01,0xba,0xa8,0x13,0x9e,0xd4,0xb7,0xff,0x86,0x34,0x01,0xa0,
124 static const unsigned char aes_128_use_df_entropyinputpr[] =
126 0x60,0x76,0xf6,0x12,0x6b,0x92,0xbe,0xd7,0x75,0x6e,0x78,0x1f,
130 static const unsigned char aes_128_use_df_additionalinput2[] =
132 0xf0,0xd6,0x5b,0xa3,0x7c,0x1e,0xa3,0x65,0x08,0xf9,0xdd,0x90,
136 static const unsigned char aes_128_use_df_entropyinputpr2[] =
138 0x34,0x55,0x02,0xa9,0x30,0xf0,0x78,0x0a,0xa2,0xae,0x74,0x46,
142 static const unsigned char aes_128_use_df_returnedbits[] =
144 0x48,0x52,0xb6,0x9f,0xf2,0xfe,0xe1,0x12,0xaf,0x22,0x87,0xd7,
149 /* AES-192 use df PR */
150 static const unsigned char aes_192_use_df_entropyinput[] =
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
156 static const unsigned char aes_192_use_df_nonce[] =
158 0x35,0xc8,0x16,0x8c,0xbd,0x1f,0x53,0xc4,0x6e,0x47,0x3a,0x74,
162 static const unsigned char aes_192_use_df_personalizationstring[] =
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
169 static const unsigned char aes_192_use_df_additionalinput[] =
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
176 static const unsigned char aes_192_use_df_entropyinputpr[] =
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
182 static const unsigned char aes_192_use_df_additionalinput2[] =
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
189 static const unsigned char aes_192_use_df_entropyinputpr2[] =
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
195 static const unsigned char aes_192_use_df_returnedbits[] =
197 0x5b,0x8a,0xca,0x2e,0x74,0xb6,0x6f,0x96,0x48,0xb0,0xe4,0xc1,
202 /* AES-256 use df PR */
203 static const unsigned char aes_256_use_df_entropyinput[] =
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
210 static const unsigned char aes_256_use_df_nonce[] =
212 0x2c,0x86,0xa2,0xf9,0x70,0xb5,0xca,0xd3,0x9a,0x08,0xdc,0xb6,
216 static const unsigned char aes_256_use_df_personalizationstring[] =
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
223 static const unsigned char aes_256_use_df_additionalinput[] =
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
230 static const unsigned char aes_256_use_df_entropyinputpr[] =
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
237 static const unsigned char aes_256_use_df_additionalinput2[] =
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
244 static const unsigned char aes_256_use_df_entropyinputpr2[] =
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
251 static const unsigned char aes_256_use_df_returnedbits[] =
253 0x75,0x6d,0x16,0xef,0x14,0xae,0xd9,0xc2,0x28,0x0b,0x66,0xff,
258 /* AES-128 no df PR */
259 static const unsigned char aes_128_no_df_entropyinput[] =
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
266 static const unsigned char aes_128_no_df_nonce[] =
268 0x39,0xeb,0x7a,0x42,0x0b,0x7f,0x4f,0xd5
271 static const unsigned char aes_128_no_df_personalizationstring[] =
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
278 static const unsigned char aes_128_no_df_additionalinput[] =
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
285 static const unsigned char aes_128_no_df_entropyinputpr[] =
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
292 static const unsigned char aes_128_no_df_additionalinput2[] =
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
299 static const unsigned char aes_128_no_df_entropyinputpr2[] =
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
306 static const unsigned char aes_128_no_df_returnedbits[] =
308 0x8c,0x7f,0x69,0xbf,0xb8,0x07,0x17,0xa6,0x09,0xef,0xd2,0x0a,
313 /* AES-192 no df PR */
314 static const unsigned char aes_192_no_df_entropyinput[] =
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,
322 static const unsigned char aes_192_no_df_nonce[] =
324 0x74,0x7a,0x38,0x81,0xef,0xca,0xd1,0xb6,0x7b,0xb5,0x1e,0x62,
328 static const unsigned char aes_192_no_df_personalizationstring[] =
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,
336 static const unsigned char aes_192_no_df_additionalinput[] =
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,
344 static const unsigned char aes_192_no_df_entropyinputpr[] =
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,
352 static const unsigned char aes_192_no_df_additionalinput2[] =
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,
360 static const unsigned char aes_192_no_df_entropyinputpr2[] =
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,
368 static const unsigned char aes_192_no_df_returnedbits[] =
370 0x48,0xd6,0x66,0x61,0x93,0x8d,0xff,0x7d,0x42,0xf4,0x41,0x9a,
375 /* AES-256 no df PR */
376 static const unsigned char aes_256_no_df_entropyinput[] =
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
384 static const unsigned char aes_256_no_df_nonce[] =
386 0xa3,0x8a,0xa4,0x6d,0xa6,0xc1,0x40,0xf8,0xa3,0x02,0xf1,0xac,
390 static const unsigned char aes_256_no_df_personalizationstring[] =
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
398 static const unsigned char aes_256_no_df_additionalinput[] =
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
406 static const unsigned char aes_256_no_df_entropyinputpr[] =
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
414 static const unsigned char aes_256_no_df_additionalinput2[] =
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
422 static const unsigned char aes_256_no_df_entropyinputpr2[] =
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
430 static const unsigned char aes_256_no_df_returnedbits[] =
432 0x1c,0xba,0xfd,0x48,0x0f,0xf4,0x85,0x63,0xd6,0x7d,0x91,0x14,
437 static const unsigned char sha1_entropyinput[] =
439 0x5b,0xaf,0x30,0x1a,0xdc,0xd1,0x04,0xd7,0x95,0x72,0xd2,0xfb,
443 static const unsigned char sha1_nonce[] =
445 0xf3,0xd9,0xcb,0x92,0x5f,0x50,0x4c,0x99
448 static const unsigned char sha1_personalizationstring[] =
450 0x8f,0x56,0x70,0xd9,0x27,0xa2,0xb4,0xf1,0xb3,0xad,0xcf,0x10,
454 static const unsigned char sha1_additionalinput[] =
456 0x49,0xdd,0x0c,0xb4,0xab,0x84,0xe1,0x7e,0x94,0x20,0xad,0x6c,
460 static const unsigned char sha1_entropyinputpr[] =
462 0x23,0x4a,0xaf,0xf7,0x1a,0x0b,0x7e,0x51,0xdd,0x23,0x51,0x82,
466 static const unsigned char sha1_additionalinput2[] =
468 0x59,0xe6,0x93,0xcb,0x38,0x23,0xf5,0x7b,0x93,0x5a,0x4d,0xfa,
472 static const unsigned char sha1_entropyinputpr2[] =
474 0x2e,0x00,0x78,0x5a,0xcd,0x30,0xea,0x73,0x37,0x8a,0x0d,0x12,
478 static const unsigned char sha1_returnedbits[] =
480 0xe7,0x87,0x8b,0x01,0xc1,0xd3,0xd8,0x43,0xd4,0x8f,0xcd,0x24,
481 0x54,0x67,0xa2,0x6e,0x17,0x94,0x73,0x1c
486 static const unsigned char sha224_entropyinput[] =
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
492 static const unsigned char sha224_nonce[] =
494 0xfd,0xba,0x25,0x2e,0xc1,0x7c,0x4e,0xa1,0x4d,0xef,0xeb,0x5d
497 static const unsigned char sha224_personalizationstring[] =
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
503 static const unsigned char sha224_additionalinput[] =
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
509 static const unsigned char sha224_entropyinputpr[] =
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
515 static const unsigned char sha224_additionalinput2[] =
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
521 static const unsigned char sha224_entropyinputpr2[] =
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
527 static const unsigned char sha224_returnedbits[] =
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,
536 static const unsigned char sha256_entropyinput[] =
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
543 static const unsigned char sha256_nonce[] =
545 0x44,0x06,0xa7,0x61,0x15,0x0a,0x6a,0x2d,0xa9,0x18,0x10,0xb5,
549 static const unsigned char sha256_personalizationstring[] =
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
556 static const unsigned char sha256_additionalinput[] =
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
563 static const unsigned char sha256_entropyinputpr[] =
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
570 static const unsigned char sha256_additionalinput2[] =
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
577 static const unsigned char sha256_entropyinputpr2[] =
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
584 static const unsigned char sha256_returnedbits[] =
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
593 static const unsigned char sha384_entropyinput[] =
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
600 static const unsigned char sha384_nonce[] =
602 0xfb,0xe1,0xb2,0x81,0x77,0xb0,0x14,0x94,0xae,0xbb,0x8d,0x01,
606 static const unsigned char sha384_personalizationstring[] =
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
613 static const unsigned char sha384_additionalinput[] =
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
620 static const unsigned char sha384_entropyinputpr[] =
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
627 static const unsigned char sha384_additionalinput2[] =
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
634 static const unsigned char sha384_entropyinputpr2[] =
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
641 static const unsigned char sha384_returnedbits[] =
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
651 static const unsigned char sha512_entropyinput[] =
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
658 static const unsigned char sha512_nonce[] =
660 0xcc,0x8b,0x86,0x21,0xa7,0xbe,0xd3,0xe1,0xde,0xd2,0x47,0xfc,
664 static const unsigned char sha512_personalizationstring[] =
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
671 static const unsigned char sha512_additionalinput[] =
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
678 static const unsigned char sha512_entropyinputpr[] =
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
685 static const unsigned char sha512_additionalinput2[] =
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
691 /* NB: not constant so we can corrupt it */
692 static const unsigned char sha512_entropyinputpr2[] =
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
699 static const unsigned char sha512_returnedbits[] =
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,
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),
728 const unsigned char *ent;
731 const unsigned char *nonce;
736 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
737 int entropy, size_t min_len, size_t max_len)
739 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
740 *pout = (unsigned char *)t->ent;
745 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
746 int entropy, size_t min_len, size_t max_len)
748 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
749 *pout = (unsigned char *)t->nonce;
754 static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
759 unsigned char randout[1024];
760 if (!FIPS_drbg_init(dctx, td->nid, td->flags))
762 if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, test_nonce, 0))
765 FIPS_drbg_set_app_data(dctx, &t);
768 t.entlen = td->entlen;
770 t.noncelen = td->noncelen;
774 if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
778 t.entlen = td->entprlen;
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.
784 if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
785 adinlen = td->adinlen / 2;
787 adinlen = td->adinlen;
788 if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
793 t.entlen = td->entglen;
795 if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
796 td->ading, td->adinglen))
799 if (memcmp(randout, td->kat, td->katlen))
806 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_SELFTEST_FAILED);
807 FIPS_drbg_uninstantiate(dctx);
812 /* This is the "health check" function required by SP800-90. Induce several
813 * failure modes and check an error condition is set.
816 static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
818 unsigned char randout[1024];
821 unsigned char *p = (unsigned char *)dctx;
823 /* Initialise DRBG */
825 if (!FIPS_drbg_init(dctx, td->nid, td->flags))
828 if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, test_nonce, 0))
831 FIPS_drbg_set_app_data(dctx, &t);
834 t.entlen = td->entlen;
836 t.noncelen = td->noncelen;
840 /* Don't report induced errors */
841 dctx->flags |= DRBG_FLAG_NOERR;
843 /* Try too large a personalisation length */
844 if (FIPS_drbg_instantiate(dctx, td->pers, dctx->max_pers + 1) > 0)
846 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_PERSONALISATION_ERROR_UNDETECTED);
850 /* Test entropy source failure detection */
853 if (FIPS_drbg_instantiate(dctx, td->pers, td->perslen) > 0)
855 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
859 /* Try to generate output from uninstantiated DRBG */
860 if (FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
861 td->adin, td->adinlen))
863 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_GENERATE_ERROR_UNDETECTED);
867 dctx->flags &= ~DRBG_FLAG_NOERR;
868 if (!FIPS_drbg_uninstantiate(dctx))
870 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
874 /* Instantiate with valid data. NB: errors now reported again */
875 if (!FIPS_drbg_init(dctx, td->nid, td->flags))
877 if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, test_nonce, 0))
879 FIPS_drbg_set_app_data(dctx, &t);
881 t.entlen = td->entlen;
882 if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
885 /* Check generation is now OK */
886 if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
887 td->adin, td->adinlen))
890 /* Try to generate with too high a strength.
893 dctx->flags |= DRBG_FLAG_NOERR;
894 if (dctx->strength != 256)
896 if (FIPS_drbg_generate(dctx, randout, td->katlen, 256, 0,
897 td->adin, td->adinlen))
899 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_STRENGTH_ERROR_UNDETECTED);
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))
909 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_REQUEST_LENGTH_ERROR_UNDETECTED);
913 /* Check prediction resistance request fails if entropy source
919 if (FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
920 td->adin, td->adinlen))
922 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
926 dctx->flags &= ~DRBG_FLAG_NOERR;
928 if (!FIPS_drbg_uninstantiate(dctx))
930 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
935 /* Instantiate again with valid data */
937 if (!FIPS_drbg_init(dctx, td->nid, td->flags))
939 if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, test_nonce, 0))
941 FIPS_drbg_set_app_data(dctx, &t);
943 t.entlen = td->entlen;
944 /* Test reseeding works */
945 dctx->reseed_interval = 2;
946 if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
949 /* Check generation is now OK */
950 if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
951 td->adin, td->adinlen))
953 if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
954 td->adin, td->adinlen))
957 /* DRBG should now require a reseed */
958 if (dctx->status != DRBG_STATUS_RESEED)
960 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_RESEED_COUNTER_ERROR);
965 /* Generate again and check entropy has been requested for reseed */
967 if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
968 td->adin, td->adinlen))
972 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_NOT_REQUESTED_FOR_RESEED);
976 FIPS_drbg_uninstantiate(dctx);
977 p = (unsigned char *)&dctx->d;
978 /* Standard says we have to check uninstantiate really zeroes
981 for (i = 0; i < sizeof(dctx->d); i++)
985 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ZEROISE_ERROR);
994 /* A real error as opposed to an induced one: underlying function will
995 * indicate the error.
997 if (!(dctx->flags & DRBG_FLAG_NOERR))
998 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_FUNCTION_ERROR);
999 FIPS_drbg_uninstantiate(dctx);
1005 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags)
1008 DRBG_SELFTEST_DATA *td;
1009 for (td = drbg_test; td->nid != 0; td++)
1011 if (td->nid == nid && td->flags == flags)
1013 rv = fips_drbg_single_kat(dctx, td);
1016 return fips_drbg_health_check(dctx, td);
1022 int FIPS_selftest_drbg(void)
1025 DRBG_SELFTEST_DATA *td;
1027 dctx = FIPS_drbg_new(0, 0);
1030 for (td = drbg_test; td->nid != 0; td++)
1032 if (!fips_post_started(FIPS_TEST_DRBG, td->nid, &td->flags))
1034 if (!fips_drbg_single_kat(dctx, td))
1036 fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
1040 if (!fips_drbg_health_check(dctx, td))
1042 fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
1046 if (!fips_post_success(FIPS_TEST_DRBG, td->nid, &td->flags))
1049 FIPS_drbg_free(dctx);