2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 #define DRBG_PARAM_ENTROPY "DRBG-ENTROPY"
21 #define DRBG_PARAM_NONCE "DRBG-NONCE"
23 static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_ST_EVENT *event,
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);
32 SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
36 || !EVP_DigestInit_ex(ctx, md, NULL)
37 || !EVP_DigestUpdate(ctx, t->pt, t->pt_len)
38 || !EVP_DigestFinal(ctx, out, &out_len))
41 /* Optional corruption */
42 SELF_TEST_EVENT_oncorrupt_byte(event, out);
44 if (out_len != t->expected_len
45 || memcmp(out, t->expected, out_len) != 0)
49 SELF_TEST_EVENT_onend(event, ok);
57 * Helper function to setup a EVP_CipherInit
58 * Used to hide the complexity of Authenticated ciphers.
60 static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
61 const ST_KAT_CIPHER *t, int enc)
63 unsigned char *in_tag = NULL;
66 /* Flag required for Key wrapping */
67 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
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);
74 /* The authenticated cipher init */
76 in_tag = (unsigned char *)t->tag;
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)
81 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->tag_len,
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);
88 /* Test a single KAT for encrypt/decrypt */
89 static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_ST_EVENT *event,
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 };
98 SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
100 ctx = EVP_CIPHER_CTX_new();
103 cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, "");
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))
113 SELF_TEST_EVENT_oncorrupt_byte(event, ct_buf);
115 if (ct_len != (int)t->base.expected_len
116 || memcmp(t->base.expected, ct_buf, ct_len) != 0)
119 if (t->tag != NULL) {
120 unsigned char tag[16] = { 0 };
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)
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)))
133 if (pt_len != (int)t->base.pt_len
134 || memcmp(pt_buf, t->base.pt, pt_len) != 0)
139 EVP_CIPHER_free(cipher);
140 EVP_CIPHER_CTX_free(ctx);
141 SELF_TEST_EVENT_onend(event, ret);
145 static int self_test_kdf(const ST_KAT_KDF *t, OSSL_ST_EVENT *event,
150 unsigned char out[64];
152 EVP_KDF_CTX *ctx = NULL;
153 OSSL_PARAM params[16];
154 const OSSL_PARAM *settables = NULL;
156 numparams = OSSL_NELEM(params);
157 SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
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();
163 kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
164 ctx = EVP_KDF_CTX_new(kdf);
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)))
172 if (!OSSL_PARAM_allocate_from_text(¶ms[i], settables,
175 strlen(t->ctrls[i].value), NULL))
178 if (!EVP_KDF_CTX_set_params(ctx, params))
181 if (t->expected_len > sizeof(out))
183 if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
186 SELF_TEST_EVENT_oncorrupt_byte(event, out);
188 if (memcmp(out, t->expected, t->expected_len) != 0)
193 for (i = 0; params[i].key != NULL; ++i)
194 OPENSSL_free(params[i].data);
196 EVP_KDF_CTX_free(ctx);
197 SELF_TEST_EVENT_onend(event, ret);
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)
205 OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
206 OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_ENTROPY);
208 if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
210 *pout = (unsigned char *)p->data;
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)
217 OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
218 OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_NONCE);
220 if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
222 *pout = (unsigned char *)p->data;
226 static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_ST_EVENT *event,
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
238 SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
240 if (strcmp(t->desc, OSSL_SELF_TEST_DESC_DRBG_HMAC) == 0)
241 flags |= RAND_DRBG_FLAG_HMAC;
243 drbg = RAND_DRBG_new_ex(libctx, t->nid, flags, NULL);
247 if (!RAND_DRBG_set_callback_data(drbg, drbg_params))
250 if (!RAND_DRBG_set_callbacks(drbg, drbg_kat_entropy_cb, NULL,
251 drbg_kat_nonce_cb, NULL))
255 OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
256 (void *)t->entropyin, t->entropyinlen);
258 OSSL_PARAM_construct_octet_string(DRBG_PARAM_NONCE,
259 (void *)t->nonce, t->noncelen);
261 if (!RAND_DRBG_instantiate(drbg, t->persstr, t->persstrlen))
265 OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
266 (void *)t->entropyinpr1,
269 if (!RAND_DRBG_generate(drbg, out, t->expectedlen, prediction_resistance,
270 t->entropyaddin1, t->entropyaddin1len))
274 OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
275 (void *)t->entropyinpr2,
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))
282 SELF_TEST_EVENT_oncorrupt_byte(event, out);
284 if (memcmp(out, t->expected, t->expectedlen) != 0)
287 if (!RAND_DRBG_uninstantiate(drbg))
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..
296 size_t i, sz = sizeof(drbg->data);
297 unsigned char *p = (unsigned char *)&drbg->data;
299 for (i = 0; i < sz; ++i)
306 RAND_DRBG_free(drbg);
307 SELF_TEST_EVENT_onend(event, ret);
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.
316 static int self_test_digests(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
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))
327 static int self_test_ciphers(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
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))
338 static int self_test_kdfs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
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))
349 static int self_test_drbgs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
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))
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.
365 * TODO(3.0) Add self tests for KA, Sign/Verify when they become available
367 int SELF_TEST_kats(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
371 if (!self_test_digests(event, libctx))
373 if (!self_test_ciphers(event, libctx))
375 if (!self_test_kdfs(event, libctx))
377 if (!self_test_drbgs(event, libctx))