Delete strength parameter from FIPS_drbg_generate. It isn't very useful
[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 #define make_drbg_test_data_ec(curve, md, pr, p) \
137         make_drbg_test_data((curve << 16) | md , 0, pr, p)
138
139 static DRBG_SELFTEST_DATA drbg_test[] = {
140         make_drbg_test_data_df(NID_aes_128_ctr, aes_128_use_df, 0),
141         make_drbg_test_data_df(NID_aes_192_ctr, aes_192_use_df, 0),
142         make_drbg_test_data_df(NID_aes_256_ctr, aes_256_use_df, 1),
143         make_drbg_test_data(NID_aes_128_ctr, 0, aes_128_no_df, 0),
144         make_drbg_test_data(NID_aes_192_ctr, 0, aes_192_no_df, 0),
145         make_drbg_test_data(NID_aes_256_ctr, 0, aes_256_no_df, 1),
146         make_drbg_test_data(NID_sha1, 0, sha1, 0),
147         make_drbg_test_data(NID_sha224, 0, sha224, 0),
148         make_drbg_test_data(NID_sha256, 0, sha256, 1),
149         make_drbg_test_data(NID_sha384, 0, sha384, 0),
150         make_drbg_test_data(NID_sha512, 0, sha512, 0),
151         make_drbg_test_data(NID_hmacWithSHA1, 0, hmac_sha1, 0),
152         make_drbg_test_data(NID_hmacWithSHA224, 0, hmac_sha224, 0),
153         make_drbg_test_data(NID_hmacWithSHA256, 0, hmac_sha256, 1),
154         make_drbg_test_data(NID_hmacWithSHA384, 0, hmac_sha384, 0),
155         make_drbg_test_data(NID_hmacWithSHA512, 0, hmac_sha512, 0),
156         make_drbg_test_data_ec(NID_X9_62_prime256v1, NID_sha1, p_256_sha1, 0),
157         make_drbg_test_data_ec(NID_X9_62_prime256v1, NID_sha224, p_256_sha224, 0),
158         make_drbg_test_data_ec(NID_X9_62_prime256v1, NID_sha256, p_256_sha256, 1),
159         make_drbg_test_data_ec(NID_X9_62_prime256v1, NID_sha384, p_256_sha384, 0),
160         make_drbg_test_data_ec(NID_X9_62_prime256v1, NID_sha512, p_256_sha512, 0),
161         make_drbg_test_data_ec(NID_secp384r1, NID_sha224, p_384_sha224, 0),
162         make_drbg_test_data_ec(NID_secp384r1, NID_sha256, p_384_sha256, 0),
163         make_drbg_test_data_ec(NID_secp384r1, NID_sha384, p_384_sha384, 0),
164         make_drbg_test_data_ec(NID_secp384r1, NID_sha512, p_384_sha512, 0),
165         make_drbg_test_data_ec(NID_secp521r1, NID_sha256, p_521_sha256, 0),
166         make_drbg_test_data_ec(NID_secp521r1, NID_sha384, p_521_sha384, 0),
167         make_drbg_test_data_ec(NID_secp521r1, NID_sha512, p_521_sha512, 0),
168         {0,0,0}
169         };
170
171 typedef struct 
172         {
173         const unsigned char *ent;
174         size_t entlen;
175         int entcnt;
176         const unsigned char *nonce;
177         size_t noncelen;
178         int noncecnt;
179         } TEST_ENT;
180
181 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
182                                 int entropy, size_t min_len, size_t max_len)
183         {
184         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
185         *pout = (unsigned char *)t->ent;
186         t->entcnt++;
187         return t->entlen;
188         }
189
190 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
191                                 int entropy, size_t min_len, size_t max_len)
192         {
193         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
194         *pout = (unsigned char *)t->nonce;
195         t->noncecnt++;
196         return t->noncelen;
197         }
198
199 static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td,
200                                                                 int quick)
201         {
202         TEST_ENT t;
203         int rv = 0;
204         size_t adinlen;
205         unsigned char randout[1024];
206
207         /* Initial test without PR */
208
209         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
210                 return 0;
211         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
212                 return 0;
213
214         FIPS_drbg_set_app_data(dctx, &t);
215
216         t.ent = td->ent;
217         t.entlen = td->entlen;
218         t.nonce = td->nonce;
219         t.noncelen = td->noncelen;
220         t.entcnt = 0;
221         t.noncecnt = 0;
222
223         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
224                 goto err;
225
226         /* Note for CTR without DF some additional input values
227          * ignore bytes after the keylength: so reduce adinlen
228          * to half to ensure invalid data is fed in.
229          */
230         if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
231                 adinlen = td->adinlen / 2;
232         else
233                 adinlen = td->adinlen;
234         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0,
235                                 td->adin, adinlen))
236                 goto err;
237
238         if (memcmp(randout, td->kat, td->katlen))
239                 {
240                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_NOPR_TEST1_FAILURE);
241                 goto err2;
242                 }
243
244         if (quick)
245                 {
246                 rv = 1;
247                 goto err;
248                 }
249
250         t.ent = td->entreseed;
251         t.entlen = td->entreseedlen;
252
253         if (!FIPS_drbg_reseed(dctx, td->adinreseed, td->adinreseedlen))
254                 goto err;
255
256         if (!FIPS_drbg_generate(dctx, randout, td->kat2len, 0,
257                                 td->adin2, td->adin2len))
258                 goto err;
259
260         if (memcmp(randout, td->kat2, td->kat2len))
261                 {
262                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_NOPR_TEST2_FAILURE);
263                 goto err2;
264                 }
265
266         FIPS_drbg_uninstantiate(dctx);
267
268         /* Now test with PR */
269         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
270                 return 0;
271         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
272                 return 0;
273
274         FIPS_drbg_set_app_data(dctx, &t);
275
276         t.ent = td->ent_pr;
277         t.entlen = td->entlen_pr;
278         t.nonce = td->nonce_pr;
279         t.noncelen = td->noncelen_pr;
280         t.entcnt = 0;
281         t.noncecnt = 0;
282
283         if (!FIPS_drbg_instantiate(dctx, td->pers_pr, td->perslen_pr))
284                 goto err;
285
286         t.ent = td->entpr_pr;
287         t.entlen = td->entprlen_pr;
288
289         /* Note for CTR without DF some additional input values
290          * ignore bytes after the keylength: so reduce adinlen
291          * to half to ensure invalid data is fed in.
292          */
293         if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
294                 adinlen = td->adinlen_pr / 2;
295         else
296                 adinlen = td->adinlen_pr;
297         if (!FIPS_drbg_generate(dctx, randout, td->katlen_pr, 1,
298                                 td->adin_pr, adinlen))
299                 goto err;
300
301         if (memcmp(randout, td->kat_pr, td->katlen_pr))
302                 {
303                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_PR_TEST1_FAILURE);
304                 goto err2;
305                 }
306
307         t.ent = td->entg_pr;
308         t.entlen = td->entglen_pr;
309
310         if (!FIPS_drbg_generate(dctx, randout, td->kat2len_pr, 1,
311                                 td->ading_pr, td->adinglen_pr))
312                 goto err;
313
314         if (memcmp(randout, td->kat2_pr, td->kat2len_pr))
315                 {
316                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_PR_TEST2_FAILURE);
317                 goto err2;
318                 }
319
320         rv = 1;
321
322         err:
323         if (rv == 0)
324                 FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_SELFTEST_FAILED);
325         err2:
326         FIPS_drbg_uninstantiate(dctx);
327         
328         return rv;
329
330         }
331
332 /* This is the "health check" function required by SP800-90. Induce several
333  * failure modes and check an error condition is set.
334  */
335
336 static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
337         {
338         unsigned char randout[1024];
339         TEST_ENT t;
340         size_t i;
341         unsigned int reseed_counter_tmp;
342         unsigned char *p = (unsigned char *)dctx;
343
344         /* Initialise DRBG */
345
346         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
347                 goto err;
348
349         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
350                 goto err;
351
352         FIPS_drbg_set_app_data(dctx, &t);
353
354         t.ent = td->ent;
355         t.entlen = td->entlen;
356         t.nonce = td->nonce;
357         t.noncelen = td->noncelen;
358         t.entcnt = 0;
359         t.noncecnt = 0;
360
361         /* Don't report induced errors */
362         dctx->flags |= DRBG_FLAG_NOERR;
363
364         /* Try too large a personalisation length */
365         if (FIPS_drbg_instantiate(dctx, td->pers, dctx->max_pers + 1) > 0)
366                 {
367                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_PERSONALISATION_ERROR_UNDETECTED);
368                 goto err;
369                 }
370
371         /* Test entropy source failure detection */
372
373         t.entlen = 0;
374         if (FIPS_drbg_instantiate(dctx, td->pers, td->perslen) > 0)
375                 {
376                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
377                 goto err;
378                 }
379
380         /* Try to generate output from uninstantiated DRBG */
381         if (FIPS_drbg_generate(dctx, randout, td->katlen, 0,
382                                 td->adin, td->adinlen))
383                 {
384                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_GENERATE_ERROR_UNDETECTED);
385                 goto err;
386                 }
387
388         dctx->flags &= ~DRBG_FLAG_NOERR;
389         if (!FIPS_drbg_uninstantiate(dctx))
390                 {
391                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
392                 goto err;
393                 }
394
395         /* Instantiate with valid data. NB: errors now reported again */
396         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
397                 goto err;
398         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
399                 goto err;
400         FIPS_drbg_set_app_data(dctx, &t);
401
402         t.entlen = td->entlen;
403         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
404                 goto err;
405
406         /* Check generation is now OK */
407         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0,
408                                 td->adin, td->adinlen))
409                 goto err;
410
411         /* Try to generate with too high a strength.
412          */
413
414         dctx->flags |= DRBG_FLAG_NOERR;
415
416         /* Request too much data for one request */
417         if (FIPS_drbg_generate(dctx, randout, dctx->max_request + 1, 0,
418                                 td->adin, td->adinlen))
419                 {
420                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_REQUEST_LENGTH_ERROR_UNDETECTED);
421                 goto err;
422                 }
423
424         /* Check prediction resistance request fails if entropy source
425          * failure.
426          */
427
428         t.entlen = 0;
429
430         if (FIPS_drbg_generate(dctx, randout, td->katlen, 1,
431                                 td->adin, td->adinlen))
432                 {
433                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_ERROR_UNDETECTED);
434                 goto err;
435                 }
436                 
437         dctx->flags &= ~DRBG_FLAG_NOERR;
438
439         if (!FIPS_drbg_uninstantiate(dctx))
440                 {
441                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
442                 goto err;
443                 }
444
445
446         /* Instantiate again with valid data */
447
448         if (!FIPS_drbg_init(dctx, td->nid, td->flags))
449                 goto err;
450         if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
451                 goto err;
452         FIPS_drbg_set_app_data(dctx, &t);
453
454         t.entlen = td->entlen;
455         /* Test reseed counter works */
456         if (!FIPS_drbg_instantiate(dctx, td->pers, td->perslen))
457                 goto err;
458         /* Save initial reseed counter */
459         reseed_counter_tmp = dctx->reseed_counter;
460         /* Set reseed counter to beyond interval */
461         dctx->reseed_counter = dctx->reseed_interval;
462
463         /* Generate output and check entropy has been requested for reseed */
464         t.entcnt = 0;
465         if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0,
466                                 td->adin, td->adinlen))
467                 goto err;
468         if (t.entcnt != 1)
469                 {
470                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_ENTROPY_NOT_REQUESTED_FOR_RESEED);
471                 goto err;
472                 }
473         /* Check reseed counter has been reset */
474         if (dctx->reseed_counter != reseed_counter_tmp + 1)
475                 {
476                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_RESEED_COUNTER_ERROR);
477                 goto err;
478                 }
479
480         FIPS_drbg_uninstantiate(dctx);
481         p = (unsigned char *)&dctx->d;
482         /* Standard says we have to check uninstantiate really zeroes
483          * the data...
484          */
485         for (i = 0; i < sizeof(dctx->d); i++)
486                 {
487                 if (*p != 0)
488                         {
489                         FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ZEROISE_ERROR);
490                         goto err;
491                         }
492                 p++;
493                 }
494
495         return 1;
496
497         err:
498         /* A real error as opposed to an induced one: underlying function will
499          * indicate the error.
500          */
501         if (!(dctx->flags & DRBG_FLAG_NOERR))
502                 FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_FUNCTION_ERROR);
503         FIPS_drbg_uninstantiate(dctx);
504         return 0;
505
506         }
507
508
509 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags)
510         {
511         int rv;
512         DRBG_SELFTEST_DATA *td;
513         for (td = drbg_test; td->nid != 0; td++)
514                 {
515                 if (td->nid == nid && td->flags == flags)
516                         {
517                         rv = fips_drbg_single_kat(dctx, td, 0);
518                         if (rv <= 0)
519                                 return rv;
520                         return fips_drbg_health_check(dctx, td);
521                         }
522                 }
523         return 0;
524         }
525
526 int FIPS_selftest_drbg(void)
527         {
528         DRBG_CTX *dctx;
529         DRBG_SELFTEST_DATA *td;
530         int rv = 1;
531         dctx = FIPS_drbg_new(0, 0);
532         if (!dctx)
533                 return 0;
534         for (td = drbg_test; td->nid != 0; td++)
535                 {
536                 if (td->post != 1)
537                         continue;
538                 if (!fips_post_started(FIPS_TEST_DRBG, td->nid, &td->flags))
539                         return 1;
540                 if (!fips_drbg_single_kat(dctx, td, 1))
541                         {
542                         fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
543                         rv = 0;
544                         continue;
545                         }
546                 if (!fips_post_success(FIPS_TEST_DRBG, td->nid, &td->flags))
547                         return 0;
548                 }
549         FIPS_drbg_free(dctx);
550         return rv;
551         }
552
553
554
555