fips_rand_selftest.c revision 225736
1193323Sed/* ====================================================================
2193323Sed * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
3193323Sed *
4193323Sed * Redistribution and use in source and binary forms, with or without
5193323Sed * modification, are permitted provided that the following conditions
6193323Sed * are met:
7193323Sed *
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed *
11193323Sed * 2. Redistributions in binary form must reproduce the above copyright
12261991Sdim *    notice, this list of conditions and the following disclaimer in
13193323Sed *    the documentation and/or other materials provided with the
14193323Sed *    distribution.
15193323Sed *
16193323Sed * 3. All advertising materials mentioning features or use of this
17193323Sed *    software must display the following acknowledgment:
18193323Sed *    "This product includes software developed by the OpenSSL Project
19193323Sed *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20193323Sed *
21193323Sed * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22193323Sed *    endorse or promote products derived from this software without
23193323Sed *    prior written permission. For written permission, please contact
24249423Sdim *    openssl-core@openssl.org.
25249423Sdim *
26249423Sdim * 5. Products derived from this software may not be called "OpenSSL"
27276479Sdim *    nor may "OpenSSL" appear in their names without prior written
28249423Sdim *    permission of the OpenSSL Project.
29249423Sdim *
30249423Sdim * 6. Redistributions of any form whatsoever must retain the following
31249423Sdim *    acknowledgment:
32249423Sdim *    "This product includes software developed by the OpenSSL Project
33193323Sed *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34193323Sed *
35193323Sed * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36193323Sed * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38198090Srdivacky * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39193323Sed * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40193323Sed * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42261991Sdim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43218893Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44218893Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45218893Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46193323Sed * OF THE POSSIBILITY OF SUCH DAMAGE.
47276479Sdim *
48193323Sed */
49276479Sdim
50193323Sed#include <string.h>
51193323Sed#include <openssl/err.h>
52193323Sed#include <openssl/fips.h>
53193323Sed#include <openssl/rand.h>
54198090Srdivacky#include <openssl/fips_rand.h>
55193323Sed
56193323Sed#ifdef OPENSSL_FIPS
57193323Sed
58218893Sdim
59218893Sdim
60218893Sdimtypedef struct
61193323Sed	{
62276479Sdim	unsigned char DT[16];
63193323Sed	unsigned char V[16];
64276479Sdim	unsigned char R[16];
65193323Sed	} AES_PRNG_TV;
66193323Sed
67193323Sed/* The following test vectors are taken directly from the RGNVS spec */
68193323Sed
69198090Srdivackystatic unsigned char aes_128_key[16] =
70193323Sed		{0xf3,0xb1,0x66,0x6d,0x13,0x60,0x72,0x42,
71193323Sed		 0xed,0x06,0x1c,0xab,0xb8,0xd4,0x62,0x02};
72193323Sed
73218893Sdimstatic AES_PRNG_TV aes_128_tv[] = {
74218893Sdim	{
75218893Sdim				/* DT */
76193323Sed		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
77276479Sdim		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x22,0xf9},
78193323Sed				/* V */
79276479Sdim		{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
80193323Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
81193323Sed				/* R */
82193323Sed		{0x59,0x53,0x1e,0xd1,0x3b,0xb0,0xc0,0x55,
83210299Sed		 0x84,0x79,0x66,0x85,0xc1,0x2f,0x76,0x41}
84210299Sed	},
85210299Sed	{
86210299Sed				/* DT */
87210299Sed		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
88218893Sdim		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x22,0xfa},
89218893Sdim				/* V */
90218893Sdim		{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
91210299Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
92276479Sdim				/* R */
93210299Sed		{0x7c,0x22,0x2c,0xf4,0xca,0x8f,0xa2,0x4c,
94276479Sdim		 0x1c,0x9c,0xb6,0x41,0xa9,0xf3,0x22,0x0d}
95210299Sed	},
96210299Sed	{
97210299Sed				/* DT */
98193323Sed		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
99193323Sed		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x22,0xfb},
100193323Sed				/* V */
101212904Sdim		{0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
102218893Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
103193323Sed				/* R */
104193323Sed		{0x8a,0xaa,0x00,0x39,0x66,0x67,0x5b,0xe5,
105193323Sed		 0x29,0x14,0x28,0x81,0xa9,0x4d,0x4e,0xc7}
106193323Sed	},
107193323Sed	{
108193323Sed				/* DT */
109212904Sdim		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
110212904Sdim		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x22,0xfc},
111218893Sdim				/* V */
112193323Sed		{0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
113193323Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
114193323Sed				/* R */
115193323Sed		{0x88,0xdd,0xa4,0x56,0x30,0x24,0x23,0xe5,
116193323Sed		 0xf6,0x9d,0xa5,0x7e,0x7b,0x95,0xc7,0x3a}
117193323Sed	},
118212904Sdim	{
119218893Sdim				/* DT */
120193323Sed		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
121193323Sed		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x22,0xfd},
122193323Sed				/* V */
123193323Sed		{0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
124193323Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
125210299Sed				/* R */
126212904Sdim		{0x05,0x25,0x92,0x46,0x61,0x79,0xd2,0xcb,
127218893Sdim		 0x78,0xc4,0x0b,0x14,0x0a,0x5a,0x9a,0xc8}
128210299Sed	},
129210299Sed	{
130210299Sed				/* DT */
131210299Sed		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
132210299Sed		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x23,0x77},
133193323Sed				/* V */
134193323Sed		{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
135276479Sdim		 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe},
136193323Sed				/* R */
137193323Sed		{0x0d,0xd5,0xa0,0x36,0x7a,0x59,0x26,0xbc,
138276479Sdim		 0x48,0xd9,0x38,0xbf,0xf0,0x85,0x8f,0xea}
139193323Sed	},
140193323Sed	{
141193323Sed				/* DT */
142193323Sed		{0xe6,0xb3,0xbe,0x78,0x2a,0x23,0xfa,0x62,
143193323Sed		 0xd7,0x1d,0x4a,0xfb,0xb0,0xe9,0x23,0x78},
144198892Srdivacky				/* V */
145288943Sdim		{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
146288943Sdim		 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
147288943Sdim				/* R */
148193323Sed		{0xae,0x53,0x87,0xee,0x8c,0xd9,0x12,0xf5,
149276479Sdim		 0x73,0x53,0xae,0x03,0xf9,0xd5,0x13,0x33}
150193323Sed	},
151193323Sed};
152193323Sed
153193323Sedstatic unsigned char aes_192_key[24] =
154193323Sed		{0x15,0xd8,0x78,0x0d,0x62,0xd3,0x25,0x6e,
155193323Sed		 0x44,0x64,0x10,0x13,0x60,0x2b,0xa9,0xbc,
156193323Sed		 0x4a,0xfb,0xca,0xeb,0x4c,0x8b,0x99,0x3b};
157280031Sdim
158280031Sdimstatic AES_PRNG_TV aes_192_tv[] = {
159193323Sed	{
160193323Sed				/* DT */
161193323Sed		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
162193323Sed		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0x4b},
163193323Sed				/* V */
164193323Sed		{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
165193323Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
166193323Sed				/* R */
167193323Sed		{0x17,0x07,0xd5,0x28,0x19,0x79,0x1e,0xef,
168198090Srdivacky		 0xa5,0x0c,0xbf,0x25,0xe5,0x56,0xb4,0x93}
169193323Sed	},
170193323Sed	{
171193323Sed				/* DT */
172193323Sed		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
173193323Sed		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0x4c},
174193323Sed				/* V */
175224145Sdim		{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
176224145Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
177239462Sdim				/* R */
178239462Sdim		{0x92,0x8d,0xbe,0x07,0xdd,0xc7,0x58,0xc0,
179224145Sdim		 0x6f,0x35,0x41,0x9b,0x17,0xc9,0xbd,0x9b}
180224145Sdim	},
181224145Sdim	{
182226633Sdim				/* DT */
183261991Sdim		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
184224145Sdim		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0x4d},
185224145Sdim				/* V */
186224145Sdim		{0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
187224145Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
188193323Sed				/* R */
189193323Sed		{0xd5,0xde,0xf4,0x50,0xf3,0xb7,0x10,0x4e,
190193323Sed		 0xb8,0xc6,0xf8,0xcf,0xe2,0xb1,0xca,0xa2}
191193323Sed	},
192198090Srdivacky	{
193280031Sdim				/* DT */
194276479Sdim		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
195198090Srdivacky		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0x4e},
196251662Sdim				/* V */
197251662Sdim		{0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
198251662Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
199198090Srdivacky				/* R */
200261991Sdim		{0xce,0x29,0x08,0x43,0xfc,0x34,0x41,0xe7,
201198090Srdivacky		 0x47,0x8f,0xb3,0x66,0x2b,0x46,0xb1,0xbb}
202198090Srdivacky	},
203193323Sed	{
204193323Sed				/* DT */
205193323Sed		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
206198090Srdivacky		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0x4f},
207193323Sed				/* V */
208193323Sed		{0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
209198090Srdivacky		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
210198090Srdivacky				/* R */
211193323Sed		{0xb3,0x26,0x0f,0xf5,0xd6,0xca,0xa8,0xbf,
212193323Sed		 0x89,0xb8,0x5e,0x2f,0x22,0x56,0x92,0x2f}
213193323Sed	},
214296417Sdim	{
215198090Srdivacky				/* DT */
216193323Sed		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
217193323Sed		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0xc9},
218261991Sdim				/* V */
219193323Sed		{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
220296417Sdim		 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe},
221198090Srdivacky				/* R */
222193323Sed		{0x05,0xeb,0x18,0x52,0x34,0x43,0x00,0x43,
223193323Sed		 0x6e,0x5a,0xa5,0xfe,0x7b,0x32,0xc4,0x2d}
224193323Sed	},
225261991Sdim	{
226193323Sed				/* DT */
227224145Sdim		{0x3f,0xd8,0xff,0xe8,0x80,0x69,0x8b,0xc1,
228193323Sed		 0xbf,0x99,0x7d,0xa4,0x24,0x78,0xf3,0xca},
229193323Sed				/* V */
230193323Sed		{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
231193323Sed		 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
232193323Sed				/* R */
233193323Sed		{0x15,0x3c,0xe8,0xd1,0x04,0xc7,0xad,0x50,
234193323Sed		 0x0b,0xf0,0x07,0x16,0xe7,0x56,0x7a,0xea}
235193323Sed	},
236193323Sed};
237193323Sed
238193323Sedstatic unsigned char aes_256_key[32] =
239193323Sed		{0x6d,0x14,0x06,0x6c,0xb6,0xd8,0x21,0x2d,
240193323Sed		 0x82,0x8d,0xfa,0xf2,0x7a,0x03,0xb7,0x9f,
241193323Sed		 0x0c,0xc7,0x3e,0xcd,0x76,0xeb,0xee,0xb5,
242193323Sed		 0x21,0x05,0x8c,0x4f,0x31,0x7a,0x80,0xbb};
243193323Sed
244193323Sedstatic AES_PRNG_TV aes_256_tv[] = {
245193323Sed	{
246193323Sed				/* DT */
247193323Sed		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
248193323Sed		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9e,0x88},
249193323Sed				/* V */
250193323Sed		{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
251276479Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
252210299Sed				/* R */
253210299Sed		{0x35,0xc7,0xef,0xa7,0x78,0x4d,0x29,0xbc,
254193323Sed		 0x82,0x79,0x99,0xfb,0xd0,0xb3,0x3b,0x72}
255193323Sed	},
256193323Sed	{
257261991Sdim				/* DT */
258193323Sed		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
259261991Sdim		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9e,0x89},
260193323Sed				/* V */
261193323Sed		{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
262193323Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
263261991Sdim				/* R */
264193323Sed		{0x6c,0xf4,0x42,0x5d,0xc7,0x04,0x1a,0x41,
265193323Sed		 0x28,0x2a,0x78,0xa9,0xb0,0x12,0xc4,0x95}
266193323Sed	},
267193323Sed	{
268193323Sed				/* DT */
269193323Sed		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
270193323Sed		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9e,0x8a},
271193323Sed				/* V */
272193323Sed		{0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
273193323Sed		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
274193323Sed				/* R */
275198892Srdivacky		{0x16,0x90,0xa4,0xff,0x7b,0x7e,0xb9,0x30,
276193323Sed		 0xdb,0x67,0x4b,0xac,0x2d,0xe1,0xd1,0x75}
277193323Sed	},
278193323Sed	{
279193323Sed				/* DT */
280193323Sed		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
281210299Sed		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9e,0x8b},
282261991Sdim				/* V */
283261991Sdim		{0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
284261991Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
285261991Sdim				/* R */
286261991Sdim		{0x14,0x6f,0xf5,0x95,0xa1,0x46,0x65,0x30,
287261991Sdim		 0xbc,0x57,0xe2,0x4a,0xf7,0x45,0x62,0x05}
288261991Sdim	},
289210299Sed	{
290210299Sed				/* DT */
291210299Sed		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
292261991Sdim		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9e,0x8c},
293210299Sed				/* V */
294261991Sdim		{0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
295261991Sdim		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
296261991Sdim				/* R */
297261991Sdim		{0x96,0xe2,0xb4,0x1e,0x66,0x5e,0x0f,0xa4,
298261991Sdim		 0xc5,0xcd,0xa2,0x07,0xcc,0xb7,0x94,0x40}
299261991Sdim	},
300210299Sed	{
301261991Sdim				/* DT */
302261991Sdim		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
303261991Sdim		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9f,0x06},
304280031Sdim				/* V */
305280031Sdim		{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
306261991Sdim		 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe},
307261991Sdim				/* R */
308296417Sdim		{0x61,0xce,0x1d,0x6a,0x48,0x75,0x97,0x28,
309296417Sdim		 0x4b,0x41,0xde,0x18,0x44,0x4f,0x56,0xec}
310296417Sdim	},
311296417Sdim	{
312296417Sdim				/* DT */
313296417Sdim		{0xda,0x3a,0x41,0xec,0x1d,0xa3,0xb0,0xd5,
314288943Sdim		 0xf2,0xa9,0x4e,0x34,0x74,0x8e,0x9f,0x07},
315261991Sdim				/* V */
316261991Sdim		{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
317288943Sdim		 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
318261991Sdim				/* R */
319261991Sdim		{0x52,0x89,0x59,0x79,0x2d,0xaa,0x28,0xb3,
320261991Sdim		 0xb0,0x8a,0x3e,0x70,0xfa,0x71,0x59,0x84}
321261991Sdim	},
322261991Sdim};
323296417Sdim
324261991Sdim
325210299Sedvoid FIPS_corrupt_rng()
326261991Sdim    {
327210299Sed    aes_192_tv[0].V[0]++;
328210299Sed    }
329261991Sdim
330261991Sdim#define fips_rand_test(key, tv) \
331288943Sdim	do_rand_test(key, sizeof key, tv, sizeof(tv)/sizeof(AES_PRNG_TV))
332261991Sdim
333261991Sdimstatic int do_rand_test(unsigned char *key, int keylen,
334261991Sdim			AES_PRNG_TV *tv, int ntv)
335261991Sdim	{
336261991Sdim	unsigned char R[16];
337261991Sdim	int i;
338288943Sdim	if (!FIPS_rand_set_key(key, keylen))
339261991Sdim		return 0;
340210299Sed	for (i = 0; i < ntv; i++)
341261991Sdim		{
342261991Sdim		FIPS_rand_seed(tv[i].V, 16);
343210299Sed		FIPS_rand_set_dt(tv[i].DT);
344261991Sdim		FIPS_rand_bytes(R, 16);
345261991Sdim		if (memcmp(R, tv[i].R, 16))
346261991Sdim			return 0;
347261991Sdim		}
348288943Sdim	return 1;
349261991Sdim	}
350210299Sed
351261991Sdim
352261991Sdimint FIPS_selftest_rng()
353288943Sdim	{
354261991Sdim	FIPS_rand_reset();
355261991Sdim	if (!FIPS_rand_test_mode())
356261991Sdim		{
357261991Sdim		FIPSerr(FIPS_F_FIPS_SELFTEST_RNG,FIPS_R_SELFTEST_FAILED);
358261991Sdim		return 0;
359261991Sdim		}
360210299Sed	if (!fips_rand_test(aes_128_key,aes_128_tv)
361210299Sed		|| !fips_rand_test(aes_192_key, aes_192_tv)
362210299Sed		|| !fips_rand_test(aes_256_key, aes_256_tv))
363210299Sed		{
364		FIPSerr(FIPS_F_FIPS_SELFTEST_RNG,FIPS_R_SELFTEST_FAILED);
365		return 0;
366		}
367	FIPS_rand_reset();
368	return 1;
369	}
370
371#endif
372