Don't perform full DRBG health check on all DRBG types on power up, just
[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/err.h>
59 #include <openssl/fips_rand.h>
60 #include "fips_rand_lcl.h"
61 #include "fips_locl.h"
62
63 #include "fips_drbg_selftest.h"
64
65 typedef struct {
66         int post;
67         int nid;
68         unsigned int flags;
69
70         /* KAT data for no PR */
71         const unsigned char *ent;
72         size_t entlen;
73         const unsigned char *nonce;
74         size_t noncelen;
75         const unsigned char *pers;
76         size_t perslen;
77         const unsigned char *adin;
78         size_t adinlen;
79         const unsigned char *entreseed;
80         size_t entreseedlen;
81         const unsigned char *adinreseed;
82         size_t adinreseedlen;
83         const unsigned char *adin2;
84         size_t adin2len;
85         const unsigned char *kat;
86         size_t katlen;
87         const unsigned char *kat2;
88         size_t kat2len;
89
90         /* KAT data for PR */
91         const unsigned char *ent_pr;
92         size_t entlen_pr;
93         const unsigned char *nonce_pr;
94         size_t noncelen_pr;
95         const unsigned char *pers_pr;
96         size_t perslen_pr;
97         const unsigned char *adin_pr;
98         size_t adinlen_pr;
99         const unsigned char *entpr_pr;
100         size_t entprlen_pr;
101         const unsigned char *ading_pr;
102         size_t adinglen_pr;
103         const unsigned char *entg_pr;
104         size_t entglen_pr;
105         const unsigned char *kat_pr;
106         size_t katlen_pr;
107         const unsigned char *kat2_pr;
108         size_t kat2len_pr;
109
110         } DRBG_SELFTEST_DATA;
111
112 #define make_drbg_test_data(nid, flag, pr, p) {p, nid, flag | DRBG_FLAG_TEST, \
113         pr##_entropyinput, sizeof(pr##_entropyinput), \
114         pr##_nonce, sizeof(pr##_nonce), \
115         pr##_personalizationstring, sizeof(pr##_personalizationstring), \
116         pr##_additionalinput, sizeof(pr##_additionalinput), \
117         pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
118         pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
119         pr##_additionalinput2, sizeof(pr##_additionalinput2), \
120         pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
121         pr##_returnedbits, sizeof(pr##_returnedbits), \
122         pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
123         pr##_pr_nonce, sizeof(pr##_pr_nonce), \
124         pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
125         pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
126         pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
127         pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
128         pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
129         pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
130         pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits), \
131         }
132
133 #define make_drbg_test_data_df(nid, pr, p) \
134         make_drbg_test_data(nid, DRBG_FLAG_CTR_USE_DF, pr, p)
135
136 static DRBG_SELFTEST_DATA drbg_test[] = {
137         make_drbg_test_data_df(NID_aes_128_ctr, aes_128_use_df, 0),
138         make_drbg_test_data_df(NID_aes_192_ctr, aes_192_use_df, 0),
139         make_drbg_test_data_df(NID_aes_256_ctr, aes_256_use_df, 1),
140         make_drbg_test_data(NID_aes_128_ctr, 0, aes_128_no_df, 0),
141         make_drbg_test_data(NID_aes_192_ctr, 0, aes_192_no_df, 0),
142         make_drbg_test_data(NID_aes_256_ctr, 0, aes_256_no_df, 1),
143         make_drbg_test_data(NID_sha1, 0, sha1, 0),
144         make_drbg_test_data(NID_sha224, 0, sha224, 0),
145         make_drbg_test_data(NID_sha256, 0, sha256, 1),
146         make_drbg_test_data(NID_sha384, 0, sha384, 0),
147         make_drbg_test_data(NID_sha512, 0, sha512, 0),
148         make_drbg_test_data(NID_hmacWithSHA1, 0, hmac_sha1, 0),
149         make_drbg_test_data(NID_hmacWithSHA224, 0, hmac_sha224, 0),
150         make_drbg_test_data(NID_hmacWithSHA256, 0, hmac_sha256, 1),
151         make_drbg_test_data(NID_hmacWithSHA384, 0, hmac_sha384, 0),
152         make_drbg_test_data(NID_hmacWithSHA512, 0, hmac_sha512, 0),
153         {0,0,0}
154         };
155
156 typedef struct 
157         {
158         const unsigned char *ent;
159         size_t entlen;
160         int entcnt;
161         const unsigned char *nonce;
162         size_t noncelen;
163         int noncecnt;
164         } TEST_ENT;
165
166 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
167                                 int entropy, size_t min_len, size_t max_len)
168         {
169         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
170         *pout = (unsigned char *)t->ent;
171         t->entcnt++;
172         return t->entlen;
173         }
174
175 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
176                                 int entropy, size_t min_len, size_t max_len)
177         {
178         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
179         *pout = (unsigned char *)t->nonce;
180         t->noncecnt++;
181         return t->noncelen;
182         }
183
184 static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
185         {
186         TEST_ENT t;
187         int rv = 0;
188         size_t adinlen;
189         unsigned char randout[1024];
190
191         /* Initial test without PR */
192
193         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
194                 return 0;
195         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
196                 return 0;
197
198         FIPS_drbg_set_app_data(dctx, &t);
199
200         t.ent = td->ent;
201         t.entlen = td->entlen;
202         t.nonce = td->nonce;
203         t.noncelen = td->noncelen;
204         t.entcnt = 0;
205         t.noncecnt = 0;
206
207         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
208                 goto err;
209
210         /* Note for CTR without DF some additional input values
211          * ignore bytes after the keylength: so reduce adinlen
212          * to half to ensure invalid data is fed in.
213          */
214         if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
215                 adinlen = td->adinlen / 2;
216         else
217                 adinlen = td->adinlen;
218         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
219                                 td->adin, adinlen))
220                 goto err;
221
222         if (memcmp(randout, td->kat, td->katlen))
223                 goto err;
224
225         t.ent = td->entreseed;
226         t.entlen = td->entreseedlen;
227
228         if (!FIPS_drbg_reseed(dctx, td->adinreseed, td->adinreseedlen))
229                 goto err;
230
231         if (!FIPS_drbg_generate(dctx, randout, td->kat2len, 0, 0,
232                                 td->adin2, td->adin2len))
233                 goto err;
234
235         if (memcmp(randout, td->kat2, td->kat2len))
236                 goto err;
237
238         FIPS_drbg_uninstantiate(dctx);
239
240         /* Now test with PR */
241         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
242                 return 0;
243         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
244                 return 0;
245
246         FIPS_drbg_set_app_data(dctx, &t);
247
248         t.ent = td->ent_pr;
249         t.entlen = td->entlen_pr;
250         t.nonce = td->nonce_pr;
251         t.noncelen = td->noncelen_pr;
252         t.entcnt = 0;
253         t.noncecnt = 0;
254
255         if (!FIPS_drbg_instantiate(dctx, td->pers_pr, td->perslen_pr))
256                 goto err;
257
258         t.ent = td->entpr_pr;
259         t.entlen = td->entprlen_pr;
260
261         /* Note for CTR without DF some additional input values
262          * ignore bytes after the keylength: so reduce adinlen
263          * to half to ensure invalid data is fed in.
264          */
265         if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
266                 adinlen = td->adinlen_pr / 2;
267         else
268                 adinlen = td->adinlen_pr;
269         if (!FIPS_drbg_generate(dctx, randout, td->katlen_pr, 0, 1,
270                                 td->adin_pr, adinlen))
271                 goto err;
272
273         if (memcmp(randout, td->kat_pr, td->katlen_pr))
274                 goto err;
275
276         t.ent = td->entg_pr;
277         t.entlen = td->entglen_pr;
278
279         if (!FIPS_drbg_generate(dctx, randout, td->kat2len_pr, 0, 1,
280                                 td->ading_pr, td->adinglen_pr))
281                 goto err;
282
283         if (memcmp(randout, td->kat2_pr, td->kat2len_pr))
284                 goto err;
285
286         rv = 1;
287
288         err:
289         if (rv == 0)
290                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_SELFTEST_FAILED);
291         FIPS_drbg_uninstantiate(dctx);
292         
293         return rv;
294
295         }
296
297 /* This is the "health check" function required by SP800-90. Induce several
298  * failure modes and check an error condition is set.
299  */
300
301 static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
302         {
303         unsigned char randout[1024];
304         TEST_ENT t;
305         size_t i;
306         unsigned int reseed_counter_tmp;
307         unsigned char *p = (unsigned char *)dctx;
308
309         /* Initialise DRBG */
310
311         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
312                 goto err;
313
314         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
315                 goto err;
316
317         FIPS_drbg_set_app_data(dctx, &t);
318
319         t.ent = td->ent;
320         t.entlen = td->entlen;
321         t.nonce = td->nonce;
322         t.noncelen = td->noncelen;
323         t.entcnt = 0;
324         t.noncecnt = 0;
325
326         /* Don't report induced errors */
327         dctx->flags |= DRBG_FLAG_NOERR;
328
329         /* Try too large a personalisation length */
330         if (FIPS_drbg_instantiate(dctx, td->pers, dctx->max_pers + 1) > 0)
331                 {
332                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_PERSONALISATION_ERROR_UNDETECTED);
333                 goto err;
334                 }
335
336         /* Test entropy source failure detection */
337
338         t.entlen = 0;
339         if (FIPS_drbg_instantiate(dctx, td->pers, td->perslen) > 0)
340                 {
341                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
342                 goto err;
343                 }
344
345         /* Try to generate output from uninstantiated DRBG */
346         if (FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
347                                 td->adin, td->adinlen))
348                 {
349                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_GENERATE_ERROR_UNDETECTED);
350                 goto err;
351                 }
352
353         dctx->flags &= ~DRBG_FLAG_NOERR;
354         if (!FIPS_drbg_uninstantiate(dctx))
355                 {
356                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
357                 goto err;
358                 }
359
360         /* Instantiate with valid data. NB: errors now reported again */
361         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
362                 goto err;
363         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
364                 goto err;
365         FIPS_drbg_set_app_data(dctx, &t);
366
367         t.entlen = td->entlen;
368         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
369                 goto err;
370
371         /* Check generation is now OK */
372         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
373                                 td->adin, td->adinlen))
374                 goto err;
375
376         /* Try to generate with too high a strength.
377          */
378
379         dctx->flags |= DRBG_FLAG_NOERR;
380         if (dctx->strength != 256)
381                 {
382                 if (FIPS_drbg_generate(dctx, randout, td->katlen, 256, 0,
383                                         td->adin, td->adinlen))
384                         {
385                         FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_STRENGTH_ERROR_UNDETECTED);
386
387                         goto err;
388                         }
389                 }
390
391         /* Request too much data for one request */
392         if (FIPS_drbg_generate(dctx, randout, dctx->max_request + 1, 0, 0,
393                                 td->adin, td->adinlen))
394                 {
395                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_REQUEST_LENGTH_ERROR_UNDETECTED);
396                 goto err;
397                 }
398
399         /* Check prediction resistance request fails if entropy source
400          * failure.
401          */
402
403         t.entlen = 0;
404
405         if (FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
406                                 td->adin, td->adinlen))
407                 {
408                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
409                 goto err;
410                 }
411                 
412         dctx->flags &= ~DRBG_FLAG_NOERR;
413
414         if (!FIPS_drbg_uninstantiate(dctx))
415                 {
416                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
417                 goto err;
418                 }
419
420
421         /* Instantiate again with valid data */
422
423         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
424                 goto err;
425         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
426                 goto err;
427         FIPS_drbg_set_app_data(dctx, &t);
428
429         t.entlen = td->entlen;
430         /* Test reseed counter works */
431         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
432                 goto err;
433         /* Save initial reseed counter */
434         reseed_counter_tmp = dctx->reseed_counter;
435         /* Set reseed counter to beyond interval */
436         dctx->reseed_counter = dctx->reseed_interval;
437
438         /* Generate output and check entropy has been requested for reseed */
439         t.entcnt = 0;
440         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 0,
441                                 td->adin, td->adinlen))
442                 goto err;
443         if (t.entcnt != 1)
444                 {
445                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_NOT_REQUESTED_FOR_RESEED);
446                 goto err;
447                 }
448         /* Check reseed counter has been reset */
449         if (dctx->reseed_counter != reseed_counter_tmp + 1)
450                 {
451                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_RESEED_COUNTER_ERROR);
452                 goto err;
453                 }
454
455         FIPS_drbg_uninstantiate(dctx);
456         p = (unsigned char *)&dctx->d;
457         /* Standard says we have to check uninstantiate really zeroes
458          * the data...
459          */
460         for (i = 0; i < sizeof(dctx->d); i++)
461                 {
462                 if (*p != 0)
463                         {
464                         FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ZEROISE_ERROR);
465                         goto err;
466                         }
467                 p++;
468                 }
469
470         return 1;
471
472         err:
473         /* A real error as opposed to an induced one: underlying function will
474          * indicate the error.
475          */
476         if (!(dctx->flags & DRBG_FLAG_NOERR))
477                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_FUNCTION_ERROR);
478         FIPS_drbg_uninstantiate(dctx);
479         return 0;
480
481         }
482
483
484 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags)
485         {
486         int rv;
487         DRBG_SELFTEST_DATA *td;
488         for (td = drbg_test; td->nid != 0; td++)
489                 {
490                 if (td->nid == nid && td->flags == flags)
491                         {
492                         rv = fips_drbg_single_kat(dctx, td);
493                         if (rv <= 0)
494                                 return rv;
495                         return fips_drbg_health_check(dctx, td);
496                         }
497                 }
498         return 0;
499         }
500
501 int FIPS_selftest_drbg(void)
502         {
503         DRBG_CTX *dctx;
504         DRBG_SELFTEST_DATA *td;
505         int rv = 1;
506         dctx = FIPS_drbg_new(0, 0);
507         if (!dctx)
508                 return 0;
509         for (td = drbg_test; td->nid != 0; td++)
510                 {
511                 if (td->post != 1)
512                         continue;
513                 if (!fips_post_started(FIPS_TEST_DRBG, td->nid, &td->flags))
514                         return 1;
515                 if (!fips_drbg_single_kat(dctx, td))
516                         {
517                         fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
518                         rv = 0;
519                         continue;
520                         }
521                 if (!fips_post_success(FIPS_TEST_DRBG, td->nid, &td->flags))
522                         return 0;
523                 }
524         FIPS_drbg_free(dctx);
525         return rv;
526         }
527
528
529
530