Add DRBG self tests
[openssl.git] / providers / fips / self_test_kats.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <openssl/evp.h>
12 #include <openssl/kdf.h>
13 #include <openssl/rand_drbg.h>
14 #include "internal/cryptlib.h"
15 #include "internal/nelem.h"
16 #include "self_test.h"
17 #include "self_test_data.inc"
18 #include "../../crypto/rand/rand_local.h"
19
20 #define DRBG_PARAM_ENTROPY "DRBG-ENTROPY"
21 #define DRBG_PARAM_NONCE   "DRBG-NONCE"
22
23 static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_ST_EVENT *event,
24                             OPENSSL_CTX *libctx)
25 {
26     int ok = 0;
27     unsigned char out[EVP_MAX_MD_SIZE];
28     unsigned int out_len = 0;
29     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
30     EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
31
32     SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
33
34     if (ctx == NULL
35             || md == NULL
36             || !EVP_DigestInit_ex(ctx, md, NULL)
37             || !EVP_DigestUpdate(ctx, t->pt, t->pt_len)
38             || !EVP_DigestFinal(ctx, out, &out_len))
39         goto err;
40
41     /* Optional corruption */
42     SELF_TEST_EVENT_oncorrupt_byte(event, out);
43
44     if (out_len != t->expected_len
45             || memcmp(out, t->expected, out_len) != 0)
46         goto err;
47     ok = 1;
48 err:
49     SELF_TEST_EVENT_onend(event, ok);
50     EVP_MD_free(md);
51     EVP_MD_CTX_free(ctx);
52
53     return ok;
54 }
55
56 /*
57  * Helper function to setup a EVP_CipherInit
58  * Used to hide the complexity of Authenticated ciphers.
59  */
60 static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
61                        const ST_KAT_CIPHER *t, int enc)
62 {
63     unsigned char *in_tag = NULL;
64     int pad = 0, tmp;
65
66     /* Flag required for Key wrapping */
67     EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
68     if (t->tag == NULL) {
69         /* Use a normal cipher init */
70         return EVP_CipherInit_ex(ctx, cipher, NULL, t->key, t->iv, enc)
71                && EVP_CIPHER_CTX_set_padding(ctx, pad);
72     }
73
74     /* The authenticated cipher init */
75     if (!enc)
76         in_tag = (unsigned char *)t->tag;
77
78     return EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)
79            && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, t->iv_len, NULL)
80            && (in_tag == NULL
81                || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->tag_len,
82                                       in_tag))
83            && EVP_CipherInit_ex(ctx, NULL, NULL, t->key, t->iv, enc)
84            && EVP_CIPHER_CTX_set_padding(ctx, pad)
85            && EVP_CipherUpdate(ctx, NULL, &tmp, t->aad, t->aad_len);
86 }
87
88 /* Test a single KAT for encrypt/decrypt */
89 static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_ST_EVENT *event,
90                             OPENSSL_CTX *libctx)
91 {
92     int ret = 0, encrypt = 1, len, ct_len = 0, pt_len = 0;
93     EVP_CIPHER_CTX *ctx = NULL;
94     EVP_CIPHER *cipher = NULL;
95     unsigned char ct_buf[256] = { 0 };
96     unsigned char pt_buf[256] = { 0 };
97
98     SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
99
100     ctx = EVP_CIPHER_CTX_new();
101     if (ctx == NULL)
102         goto err;
103     cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, "");
104     if (cipher == NULL)
105         goto err;
106
107     /* Encrypt plain text message */
108     if (!cipher_init(ctx, cipher, t, encrypt)
109             || !EVP_CipherUpdate(ctx, ct_buf, &len, t->base.pt, t->base.pt_len)
110             || !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
111         goto err;
112
113     SELF_TEST_EVENT_oncorrupt_byte(event, ct_buf);
114     ct_len += len;
115     if (ct_len != (int)t->base.expected_len
116         || memcmp(t->base.expected, ct_buf, ct_len) != 0)
117         goto err;
118
119     if (t->tag != NULL) {
120         unsigned char tag[16] = { 0 };
121
122         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, t->tag_len, tag)
123             || memcmp(tag, t->tag, t->tag_len) != 0)
124             goto err;
125     }
126
127     if (!(cipher_init(ctx, cipher, t, !encrypt)
128           && EVP_CipherUpdate(ctx, pt_buf, &len, ct_buf, ct_len)
129           && EVP_CipherFinal_ex(ctx, pt_buf + len, &pt_len)))
130         goto err;
131     pt_len += len;
132
133     if (pt_len != (int)t->base.pt_len
134             || memcmp(pt_buf, t->base.pt, pt_len) != 0)
135         goto err;
136
137     ret = 1;
138 err:
139     EVP_CIPHER_free(cipher);
140     EVP_CIPHER_CTX_free(ctx);
141     SELF_TEST_EVENT_onend(event, ret);
142     return ret;
143 }
144
145 static int self_test_kdf(const ST_KAT_KDF *t, OSSL_ST_EVENT *event,
146                          OPENSSL_CTX *libctx)
147 {
148     int ret = 0;
149     int i, numparams;
150     unsigned char out[64];
151     EVP_KDF *kdf = NULL;
152     EVP_KDF_CTX *ctx = NULL;
153     OSSL_PARAM params[16];
154     const OSSL_PARAM *settables = NULL;
155
156     numparams = OSSL_NELEM(params);
157     SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
158
159     /* Zeroize the params array to avoid mem leaks on error */
160     for (i = 0; i < numparams; ++i)
161         params[i] = OSSL_PARAM_construct_end();
162
163     kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
164     ctx = EVP_KDF_CTX_new(kdf);
165     if (ctx == NULL)
166         goto err;
167
168     settables = EVP_KDF_settable_ctx_params(kdf);
169     for (i = 0; t->ctrls[i].name != NULL; ++i) {
170         if (!ossl_assert(i < (numparams - 1)))
171             goto err;
172         if (!OSSL_PARAM_allocate_from_text(&params[i], settables,
173                                            t->ctrls[i].name,
174                                            t->ctrls[i].value,
175                                            strlen(t->ctrls[i].value), NULL))
176             goto err;
177     }
178     if (!EVP_KDF_CTX_set_params(ctx, params))
179         goto err;
180
181     if (t->expected_len > sizeof(out))
182         goto err;
183     if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
184         goto err;
185
186     SELF_TEST_EVENT_oncorrupt_byte(event, out);
187
188     if (memcmp(out, t->expected,  t->expected_len) != 0)
189         goto err;
190
191     ret = 1;
192 err:
193     for (i = 0; params[i].key != NULL; ++i)
194         OPENSSL_free(params[i].data);
195     EVP_KDF_free(kdf);
196     EVP_KDF_CTX_free(ctx);
197     SELF_TEST_EVENT_onend(event, ret);
198     return ret;
199 }
200
201 static size_t drbg_kat_entropy_cb(RAND_DRBG *drbg, unsigned char **pout,
202                                   int entropy, size_t min_len, size_t max_len,
203                                   int prediction_resistance)
204 {
205     OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
206     OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_ENTROPY);
207
208     if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
209         return 0;
210     *pout = (unsigned char *)p->data;
211     return p->data_size;
212 }
213
214 static size_t drbg_kat_nonce_cb(RAND_DRBG *drbg, unsigned char **pout,
215                                 int entropy, size_t min_len, size_t max_len)
216 {
217     OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
218     OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_NONCE);
219
220     if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
221         return 0;
222     *pout = (unsigned char *)p->data;
223     return p->data_size;
224 }
225
226 static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_ST_EVENT *event,
227                           OPENSSL_CTX *libctx)
228 {
229     int ret = 0;
230     unsigned char out[256];
231     RAND_DRBG *drbg = NULL;
232     unsigned int flags = 0;
233     int prediction_resistance = 1; /* Causes a reseed */
234     OSSL_PARAM drbg_params[3] = {
235         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
236     };
237
238     SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
239
240     if (strcmp(t->desc, OSSL_SELF_TEST_DESC_DRBG_HMAC) == 0)
241         flags |= RAND_DRBG_FLAG_HMAC;
242
243     drbg = RAND_DRBG_new_ex(libctx, t->nid, flags, NULL);
244     if (drbg == NULL)
245         goto err;
246
247     if (!RAND_DRBG_set_callback_data(drbg, drbg_params))
248         goto err;
249
250     if (!RAND_DRBG_set_callbacks(drbg, drbg_kat_entropy_cb, NULL,
251                                  drbg_kat_nonce_cb, NULL))
252         goto err;
253
254     drbg_params[0] =
255         OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
256                                           (void *)t->entropyin, t->entropyinlen);
257     drbg_params[1] =
258         OSSL_PARAM_construct_octet_string(DRBG_PARAM_NONCE,
259                                           (void *)t->nonce, t->noncelen);
260
261     if (!RAND_DRBG_instantiate(drbg, t->persstr, t->persstrlen))
262         goto err;
263
264     drbg_params[0] =
265         OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
266                                           (void *)t->entropyinpr1,
267                                           t->entropyinpr1len);
268
269     if (!RAND_DRBG_generate(drbg, out, t->expectedlen, prediction_resistance,
270                             t->entropyaddin1, t->entropyaddin1len))
271         goto err;
272
273     drbg_params[0] =
274         OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
275                                          (void *)t->entropyinpr2,
276                                          t->entropyinpr2len);
277     /* This calls RAND_DRBG_reseed() internally when prediction_resistance = 1 */
278     if (!RAND_DRBG_generate(drbg, out,  t->expectedlen, prediction_resistance,
279                             t->entropyaddin2, t->entropyaddin2len))
280         goto err;
281
282     SELF_TEST_EVENT_oncorrupt_byte(event, out);
283
284     if (memcmp(out, t->expected, t->expectedlen) != 0)
285         goto err;
286
287     if (!RAND_DRBG_uninstantiate(drbg))
288         goto err;
289     /*
290      * TODO(3.0) : Check that the DRBG data has been zeroed after
291      * RAND_DRBG_uninstantiate. Its a bit hard currently to do this when
292      * the drbg->data is reinitialized by this call..
293      */
294 #if 0
295     {
296         size_t i, sz = sizeof(drbg->data);
297         unsigned char *p = (unsigned char *)&drbg->data;
298
299         for (i = 0; i < sz; ++i)
300             if (*p++ != 0)
301                 goto err;
302     }
303 #endif
304     ret = 1;
305 err:
306     RAND_DRBG_free(drbg);
307     SELF_TEST_EVENT_onend(event, ret);
308     return ret;
309 }
310
311 /*
312  * Test a data driven list of KAT's for digest algorithms.
313  * All tests are run regardless of if they fail or not.
314  * Return 0 if any test fails.
315  */
316 static int self_test_digests(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
317 {
318     int i, ret = 1;
319
320     for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
321         if (!self_test_digest(&st_kat_digest_tests[i], event, libctx))
322             ret = 0;
323     }
324     return ret;
325 }
326
327 static int self_test_ciphers(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
328 {
329     int i, ret = 1;
330
331     for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
332         if (!self_test_cipher(&st_kat_cipher_tests[i], event, libctx))
333             ret = 0;
334     }
335     return ret;
336 }
337
338 static int self_test_kdfs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
339 {
340     int i, ret = 1;
341
342     for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
343         if (!self_test_kdf(&st_kat_kdf_tests[i], event, libctx))
344             ret = 0;
345     }
346     return ret;
347 }
348
349 static int self_test_drbgs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
350 {
351     int i, ret = 1;
352
353     for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
354         if (!self_test_drbg(&st_kat_drbg_tests[i], event, libctx))
355             ret = 0;
356     }
357     return ret;
358 }
359
360 /*
361  * Run the algorithm KAT's.
362  * Return 1 is successful, otherwise return 0.
363  * This runs all the tests regardless of if any fail.
364  *
365  * TODO(3.0) Add self tests for KA, Sign/Verify when they become available
366  */
367 int SELF_TEST_kats(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
368 {
369     int ret = 1;
370
371     if (!self_test_digests(event, libctx))
372         ret = 0;
373     if (!self_test_ciphers(event, libctx))
374         ret = 0;
375     if (!self_test_kdfs(event, libctx))
376         ret = 0;
377     if (!self_test_drbgs(event, libctx))
378         ret = 0;
379
380     return ret;
381 }