1 /*
2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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/core_names.h>
14 #include <openssl/param_build.h>
15 #include <openssl/rand.h>
16 #include "crypto/rand.h"
17 #include "internal/cryptlib.h"
18 #include "internal/nelem.h"
19 #include "self_test.h"
20 #include "self_test_data.inc"
21
22 static int set_kat_drbg(OSSL_LIB_CTX *ctx,
23 const unsigned char *entropy, size_t entropy_len,
24 const unsigned char *nonce, size_t nonce_len,
25 const unsigned char *persstr, size_t persstr_len);
26 static int reset_main_drbg(OSSL_LIB_CTX *ctx);
27
self_test_digest(const ST_KAT_DIGEST * t,OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)28 static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_SELF_TEST *st,
29 OSSL_LIB_CTX *libctx)
30 {
31 int ok = 0;
32 unsigned char out[EVP_MAX_MD_SIZE];
33 unsigned int out_len = 0;
34 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
35 EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
36
37 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
38
39 if (ctx == NULL
40 || md == NULL
41 || !EVP_DigestInit_ex(ctx, md, NULL)
42 || !EVP_DigestUpdate(ctx, t->pt, t->pt_len)
43 || !EVP_DigestFinal(ctx, out, &out_len))
44 goto err;
45
46 /* Optional corruption */
47 OSSL_SELF_TEST_oncorrupt_byte(st, out);
48
49 if (out_len != t->expected_len
50 || memcmp(out, t->expected, out_len) != 0)
51 goto err;
52 ok = 1;
53 err:
54 EVP_MD_free(md);
55 EVP_MD_CTX_free(ctx);
56 OSSL_SELF_TEST_onend(st, ok);
57 return ok;
58 }
59
60 /*
61 * Helper function to setup a EVP_CipherInit
62 * Used to hide the complexity of Authenticated ciphers.
63 */
cipher_init(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const ST_KAT_CIPHER * t,int enc)64 static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
65 const ST_KAT_CIPHER *t, int enc)
66 {
67 unsigned char *in_tag = NULL;
68 int pad = 0, tmp;
69
70 /* Flag required for Key wrapping */
71 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
72 if (t->tag == NULL) {
73 /* Use a normal cipher init */
74 return EVP_CipherInit_ex(ctx, cipher, NULL, t->key, t->iv, enc)
75 && EVP_CIPHER_CTX_set_padding(ctx, pad);
76 }
77
78 /* The authenticated cipher init */
79 if (!enc)
80 in_tag = (unsigned char *)t->tag;
81
82 return EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)
83 && (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, t->iv_len, NULL) > 0)
84 && (in_tag == NULL
85 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->tag_len,
86 in_tag) > 0)
87 && EVP_CipherInit_ex(ctx, NULL, NULL, t->key, t->iv, enc)
88 && EVP_CIPHER_CTX_set_padding(ctx, pad)
89 && EVP_CipherUpdate(ctx, NULL, &tmp, t->aad, t->aad_len);
90 }
91
92 /* Test a single KAT for encrypt/decrypt */
self_test_cipher(const ST_KAT_CIPHER * t,OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)93 static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_SELF_TEST *st,
94 OSSL_LIB_CTX *libctx)
95 {
96 int ret = 0, encrypt = 1, len = 0, ct_len = 0, pt_len = 0;
97 EVP_CIPHER_CTX *ctx = NULL;
98 EVP_CIPHER *cipher = NULL;
99 unsigned char ct_buf[256] = { 0 };
100 unsigned char pt_buf[256] = { 0 };
101
102 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
103
104 ctx = EVP_CIPHER_CTX_new();
105 if (ctx == NULL)
106 goto err;
107 cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, NULL);
108 if (cipher == NULL)
109 goto err;
110
111 /* Encrypt plain text message */
112 if ((t->mode & CIPHER_MODE_ENCRYPT) != 0) {
113 if (!cipher_init(ctx, cipher, t, encrypt)
114 || !EVP_CipherUpdate(ctx, ct_buf, &len, t->base.pt,
115 t->base.pt_len)
116 || !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
117 goto err;
118
119 OSSL_SELF_TEST_oncorrupt_byte(st, ct_buf);
120 ct_len += len;
121 if (ct_len != (int)t->base.expected_len
122 || memcmp(t->base.expected, ct_buf, ct_len) != 0)
123 goto err;
124
125 if (t->tag != NULL) {
126 unsigned char tag[16] = { 0 };
127
128 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, t->tag_len,
129 tag) <= 0
130 || memcmp(tag, t->tag, t->tag_len) != 0)
131 goto err;
132 }
133 }
134
135 /* Decrypt cipher text */
136 if ((t->mode & CIPHER_MODE_DECRYPT) != 0) {
137 if (!(cipher_init(ctx, cipher, t, !encrypt)
138 && EVP_CipherUpdate(ctx, pt_buf, &len,
139 t->base.expected, t->base.expected_len)
140 && EVP_CipherFinal_ex(ctx, pt_buf + len, &pt_len)))
141 goto err;
142 OSSL_SELF_TEST_oncorrupt_byte(st, pt_buf);
143 pt_len += len;
144 if (pt_len != (int)t->base.pt_len
145 || memcmp(pt_buf, t->base.pt, pt_len) != 0)
146 goto err;
147 }
148
149 ret = 1;
150 err:
151 EVP_CIPHER_free(cipher);
152 EVP_CIPHER_CTX_free(ctx);
153 OSSL_SELF_TEST_onend(st, ret);
154 return ret;
155 }
156
add_params(OSSL_PARAM_BLD * bld,const ST_KAT_PARAM * params,BN_CTX * ctx)157 static int add_params(OSSL_PARAM_BLD *bld, const ST_KAT_PARAM *params,
158 BN_CTX *ctx)
159 {
160 int ret = 0;
161 const ST_KAT_PARAM *p;
162
163 if (params == NULL)
164 return 1;
165 for (p = params; p->data != NULL; ++p) {
166 switch (p->type) {
167 case OSSL_PARAM_UNSIGNED_INTEGER: {
168 BIGNUM *bn = BN_CTX_get(ctx);
169
170 if (bn == NULL
171 || (BN_bin2bn(p->data, p->data_len, bn) == NULL)
172 || !OSSL_PARAM_BLD_push_BN(bld, p->name, bn))
173 goto err;
174 break;
175 }
176 case OSSL_PARAM_UTF8_STRING: {
177 if (!OSSL_PARAM_BLD_push_utf8_string(bld, p->name, p->data,
178 p->data_len))
179 goto err;
180 break;
181 }
182 case OSSL_PARAM_OCTET_STRING: {
183 if (!OSSL_PARAM_BLD_push_octet_string(bld, p->name, p->data,
184 p->data_len))
185 goto err;
186 break;
187 }
188 case OSSL_PARAM_INTEGER: {
189 if (!OSSL_PARAM_BLD_push_int(bld, p->name, *(int *)p->data))
190 goto err;
191 break;
192 }
193 default:
194 break;
195 }
196 }
197 ret = 1;
198 err:
199 return ret;
200 }
201
self_test_kdf(const ST_KAT_KDF * t,OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)202 static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
203 OSSL_LIB_CTX *libctx)
204 {
205 int ret = 0;
206 unsigned char out[128];
207 EVP_KDF *kdf = NULL;
208 EVP_KDF_CTX *ctx = NULL;
209 BN_CTX *bnctx = NULL;
210 OSSL_PARAM *params = NULL;
211 OSSL_PARAM_BLD *bld = NULL;
212
213 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
214
215 bld = OSSL_PARAM_BLD_new();
216 if (bld == NULL)
217 goto err;
218
219 kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
220 if (kdf == NULL)
221 goto err;
222
223 ctx = EVP_KDF_CTX_new(kdf);
224 if (ctx == NULL)
225 goto err;
226
227 bnctx = BN_CTX_new_ex(libctx);
228 if (bnctx == NULL)
229 goto err;
230 if (!add_params(bld, t->params, bnctx))
231 goto err;
232 params = OSSL_PARAM_BLD_to_param(bld);
233 if (params == NULL)
234 goto err;
235
236 if (t->expected_len > sizeof(out))
237 goto err;
238 if (EVP_KDF_derive(ctx, out, t->expected_len, params) <= 0)
239 goto err;
240
241 OSSL_SELF_TEST_oncorrupt_byte(st, out);
242
243 if (memcmp(out, t->expected, t->expected_len) != 0)
244 goto err;
245
246 ret = 1;
247 err:
248 EVP_KDF_free(kdf);
249 EVP_KDF_CTX_free(ctx);
250 BN_CTX_free(bnctx);
251 OSSL_PARAM_free(params);
252 OSSL_PARAM_BLD_free(bld);
253 OSSL_SELF_TEST_onend(st, ret);
254 return ret;
255 }
256
self_test_drbg(const ST_KAT_DRBG * t,OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)257 static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
258 OSSL_LIB_CTX *libctx)
259 {
260 int ret = 0;
261 unsigned char out[256];
262 EVP_RAND *rand;
263 EVP_RAND_CTX *test = NULL, *drbg = NULL;
264 unsigned int strength = 256;
265 int prediction_resistance = 1; /* Causes a reseed */
266 OSSL_PARAM drbg_params[3] = {
267 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
268 };
269
270 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
271
272 rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
273 if (rand == NULL)
274 goto err;
275
276 test = EVP_RAND_CTX_new(rand, NULL);
277 EVP_RAND_free(rand);
278 if (test == NULL)
279 goto err;
280
281 drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
282 &strength);
283 if (!EVP_RAND_CTX_set_params(test, drbg_params))
284 goto err;
285
286 rand = EVP_RAND_fetch(libctx, t->algorithm, NULL);
287 if (rand == NULL)
288 goto err;
289
290 drbg = EVP_RAND_CTX_new(rand, test);
291 EVP_RAND_free(rand);
292 if (drbg == NULL)
293 goto err;
294
295 strength = EVP_RAND_get_strength(drbg);
296
297 drbg_params[0] = OSSL_PARAM_construct_utf8_string(t->param_name,
298 t->param_value, 0);
299 /* This is only used by HMAC-DRBG but it is ignored by the others */
300 drbg_params[1] =
301 OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
302 if (!EVP_RAND_CTX_set_params(drbg, drbg_params))
303 goto err;
304
305 drbg_params[0] =
306 OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
307 (void *)t->entropyin,
308 t->entropyinlen);
309 drbg_params[1] =
310 OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
311 (void *)t->nonce, t->noncelen);
312 if (!EVP_RAND_instantiate(test, strength, 0, NULL, 0, drbg_params))
313 goto err;
314 if (!EVP_RAND_instantiate(drbg, strength, 0, t->persstr, t->persstrlen,
315 NULL))
316 goto err;
317
318 drbg_params[0] =
319 OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
320 (void *)t->entropyinpr1,
321 t->entropyinpr1len);
322 if (!EVP_RAND_CTX_set_params(test, drbg_params))
323 goto err;
324
325 if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
326 prediction_resistance,
327 t->entropyaddin1, t->entropyaddin1len))
328 goto err;
329
330 drbg_params[0] =
331 OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
332 (void *)t->entropyinpr2,
333 t->entropyinpr2len);
334 if (!EVP_RAND_CTX_set_params(test, drbg_params))
335 goto err;
336
337 /*
338 * This calls ossl_prov_drbg_reseed() internally when
339 * prediction_resistance = 1
340 */
341 if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
342 prediction_resistance,
343 t->entropyaddin2, t->entropyaddin2len))
344 goto err;
345
346 OSSL_SELF_TEST_oncorrupt_byte(st, out);
347
348 if (memcmp(out, t->expected, t->expectedlen) != 0)
349 goto err;
350
351 if (!EVP_RAND_uninstantiate(drbg))
352 goto err;
353 /*
354 * Check that the DRBG data has been zeroized after
355 * ossl_prov_drbg_uninstantiate.
356 */
357 if (!EVP_RAND_verify_zeroization(drbg))
358 goto err;
359
360 ret = 1;
361 err:
362 EVP_RAND_CTX_free(drbg);
363 EVP_RAND_CTX_free(test);
364 OSSL_SELF_TEST_onend(st, ret);
365 return ret;
366 }
367
368 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
self_test_ka(const ST_KAT_KAS * t,OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)369 static int self_test_ka(const ST_KAT_KAS *t,
370 OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
371 {
372 int ret = 0;
373 EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
374 EVP_PKEY *pkey = NULL, *peerkey = NULL;
375 OSSL_PARAM *params = NULL;
376 OSSL_PARAM *params_peer = NULL;
377 unsigned char secret[256];
378 size_t secret_len = t->expected_len;
379 OSSL_PARAM_BLD *bld = NULL;
380 BN_CTX *bnctx = NULL;
381
382 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
383
384 if (secret_len > sizeof(secret))
385 goto err;
386
387 bnctx = BN_CTX_new_ex(libctx);
388 if (bnctx == NULL)
389 goto err;
390
391 bld = OSSL_PARAM_BLD_new();
392 if (bld == NULL)
393 goto err;
394
395 if (!add_params(bld, t->key_group, bnctx)
396 || !add_params(bld, t->key_host_data, bnctx))
397 goto err;
398 params = OSSL_PARAM_BLD_to_param(bld);
399
400 if (!add_params(bld, t->key_group, bnctx)
401 || !add_params(bld, t->key_peer_data, bnctx))
402 goto err;
403
404 params_peer = OSSL_PARAM_BLD_to_param(bld);
405 if (params == NULL || params_peer == NULL)
406 goto err;
407
408 /* Create a EVP_PKEY_CTX to load the DH keys into */
409 kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
410 if (kactx == NULL)
411 goto err;
412 if (EVP_PKEY_fromdata_init(kactx) <= 0
413 || EVP_PKEY_fromdata(kactx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
414 goto err;
415 if (EVP_PKEY_fromdata_init(kactx) <= 0
416 || EVP_PKEY_fromdata(kactx, &peerkey, EVP_PKEY_KEYPAIR, params_peer) <= 0)
417 goto err;
418
419 /* Create a EVP_PKEY_CTX to perform key derivation */
420 dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
421 if (dctx == NULL)
422 goto err;
423
424 if (EVP_PKEY_derive_init(dctx) <= 0
425 || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
426 || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
427 goto err;
428
429 OSSL_SELF_TEST_oncorrupt_byte(st, secret);
430
431 if (secret_len != t->expected_len
432 || memcmp(secret, t->expected, t->expected_len) != 0)
433 goto err;
434 ret = 1;
435 err:
436 BN_CTX_free(bnctx);
437 EVP_PKEY_free(pkey);
438 EVP_PKEY_free(peerkey);
439 EVP_PKEY_CTX_free(kactx);
440 EVP_PKEY_CTX_free(dctx);
441 OSSL_PARAM_free(params_peer);
442 OSSL_PARAM_free(params);
443 OSSL_PARAM_BLD_free(bld);
444 OSSL_SELF_TEST_onend(st, ret);
445 return ret;
446 }
447 #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
448
self_test_digest_sign(const ST_KAT_SIGN * t,OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)449 static int self_test_digest_sign(const ST_KAT_SIGN *t,
450 OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
451 {
452 int ret = 0;
453 OSSL_PARAM *paramskey = NULL, *paramsinit = NULL;
454 OSSL_PARAM_BLD *bldkey = NULL, *bldinit = NULL;
455 EVP_SIGNATURE *sigalg = NULL;
456 EVP_PKEY_CTX *ctx = NULL;
457 EVP_PKEY_CTX *fromctx = NULL;
458 EVP_PKEY *pkey = NULL;
459 unsigned char sig[256];
460 BN_CTX *bnctx = NULL;
461 size_t siglen = sizeof(sig);
462 int digested = 0;
463 const char *typ = OSSL_SELF_TEST_TYPE_KAT_SIGNATURE;
464
465 if (t->sig_expected_len > sizeof(sig))
466 goto err;
467
468 if (t->sig_expected == NULL)
469 typ = OSSL_SELF_TEST_TYPE_PCT_SIGNATURE;
470
471 OSSL_SELF_TEST_onbegin(st, typ, t->desc);
472
473 if (t->entropy != NULL) {
474 if (!set_kat_drbg(libctx, t->entropy, t->entropy_len,
475 t->nonce, t->nonce_len, t->persstr, t->persstr_len))
476 goto err;
477 }
478
479 bnctx = BN_CTX_new_ex(libctx);
480 if (bnctx == NULL)
481 goto err;
482
483 bldkey = OSSL_PARAM_BLD_new();
484 bldinit = OSSL_PARAM_BLD_new();
485 if (bldkey == NULL || bldinit == NULL)
486 goto err;
487
488 if (!add_params(bldkey, t->key, bnctx)
489 || !add_params(bldinit, t->init, bnctx))
490 goto err;
491 paramskey = OSSL_PARAM_BLD_to_param(bldkey);
492 paramsinit = OSSL_PARAM_BLD_to_param(bldinit);
493
494 fromctx = EVP_PKEY_CTX_new_from_name(libctx, t->keytype, NULL);
495 if (fromctx == NULL
496 || paramskey == NULL
497 || paramsinit == NULL)
498 goto err;
499 if (EVP_PKEY_fromdata_init(fromctx) <= 0
500 || EVP_PKEY_fromdata(fromctx, &pkey, EVP_PKEY_KEYPAIR, paramskey) <= 0)
501 goto err;
502
503 sigalg = EVP_SIGNATURE_fetch(libctx, t->sigalgorithm, NULL);
504 if (sigalg == NULL)
505 goto err;
506 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
507 if (ctx == NULL)
508 goto err;
509
510 digested = ((t->mode & SIGNATURE_MODE_DIGESTED) != 0);
511
512 if ((t->mode & SIGNATURE_MODE_VERIFY_ONLY) != 0) {
513 memcpy(sig, t->sig_expected, t->sig_expected_len);
514 siglen = t->sig_expected_len;
515 } else {
516 if (digested) {
517 if (EVP_PKEY_sign_init_ex2(ctx, sigalg, paramsinit) <= 0)
518 goto err;
519 } else {
520 if (EVP_PKEY_sign_message_init(ctx, sigalg, paramsinit) <= 0)
521 goto err;
522 }
523 if (EVP_PKEY_sign(ctx, sig, &siglen, t->msg, t->msg_len) <= 0)
524 goto err;
525
526 if (t->sig_expected != NULL
527 && (siglen != t->sig_expected_len
528 || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
529 goto err;
530 }
531
532 if ((t->mode & SIGNATURE_MODE_SIGN_ONLY) == 0) {
533 if (digested) {
534 if (EVP_PKEY_verify_init_ex2(ctx, sigalg, NULL) <= 0)
535 goto err;
536 } else {
537 if (EVP_PKEY_verify_message_init(ctx, sigalg, NULL) <= 0)
538 goto err;
539 }
540 OSSL_SELF_TEST_oncorrupt_byte(st, sig);
541 if (EVP_PKEY_verify(ctx, sig, siglen, t->msg, t->msg_len) <= 0)
542 goto err;
543 }
544 ret = 1;
545 err:
546 BN_CTX_free(bnctx);
547 EVP_PKEY_free(pkey);
548 EVP_PKEY_CTX_free(fromctx);
549 EVP_PKEY_CTX_free(ctx);
550 EVP_SIGNATURE_free(sigalg);
551 OSSL_PARAM_free(paramskey);
552 OSSL_PARAM_free(paramsinit);
553 OSSL_PARAM_BLD_free(bldkey);
554 OSSL_PARAM_BLD_free(bldinit);
555 if (t->entropy != NULL) {
556 if (!reset_main_drbg(libctx))
557 ret = 0;
558 }
559 OSSL_SELF_TEST_onend(st, ret);
560 return ret;
561 }
562
563 /*
564 * Test a data driven list of KAT's for digest algorithms.
565 * All tests are run regardless of if they fail or not.
566 * Return 0 if any test fails.
567 */
self_test_digests(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)568 static int self_test_digests(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
569 {
570 int i, ret = 1;
571
572 for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
573 if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
574 ret = 0;
575 }
576 return ret;
577 }
578
self_test_ciphers(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)579 static int self_test_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
580 {
581 int i, ret = 1;
582
583 for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
584 if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
585 ret = 0;
586 }
587 return ret;
588 }
589
self_test_kdfs(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)590 static int self_test_kdfs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
591 {
592 int i, ret = 1;
593
594 for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
595 if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
596 ret = 0;
597 }
598 return ret;
599 }
600
self_test_drbgs(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)601 static int self_test_drbgs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
602 {
603 int i, ret = 1;
604
605 for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
606 if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
607 ret = 0;
608 }
609 return ret;
610 }
611
self_test_kas(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)612 static int self_test_kas(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
613 {
614 int ret = 1;
615 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
616 int i;
617
618 for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
619 if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
620 ret = 0;
621 }
622 #endif
623
624 return ret;
625 }
626
self_test_signatures(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)627 static int self_test_signatures(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
628 {
629 int i, ret = 1;
630
631 for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
632 if (!self_test_digest_sign(&st_kat_sign_tests[i], st, libctx))
633 ret = 0;
634 }
635 return ret;
636 }
637
638 /*
639 * Swap the library context DRBG for KAT testing
640 *
641 * In FIPS 140-3, the asymmetric POST must be a KAT, not a PCT. For DSA and ECDSA,
642 * the sign operation includes the random value 'k'. For a KAT to work, we
643 * have to have control of the DRBG to make sure it is in a "test" state, where
644 * its output is truly deterministic.
645 *
646 */
647
648 /*
649 * Replacement "random" sources
650 * main_rand is used for most tests and it's set to generate mode.
651 * kat_rand is used for KATs where specific input is mandated.
652 */
653 static EVP_RAND_CTX *kat_rand = NULL;
654 static EVP_RAND_CTX *main_rand = NULL;
655
set_kat_drbg(OSSL_LIB_CTX * ctx,const unsigned char * entropy,size_t entropy_len,const unsigned char * nonce,size_t nonce_len,const unsigned char * persstr,size_t persstr_len)656 static int set_kat_drbg(OSSL_LIB_CTX *ctx,
657 const unsigned char *entropy, size_t entropy_len,
658 const unsigned char *nonce, size_t nonce_len,
659 const unsigned char *persstr, size_t persstr_len) {
660 EVP_RAND *rand;
661 unsigned int strength = 256;
662 EVP_RAND_CTX *parent_rand = NULL;
663 OSSL_PARAM drbg_params[3] = {
664 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
665 };
666
667 /* If not NULL, we didn't cleanup from last call: BAD */
668 if (kat_rand != NULL)
669 return 0;
670
671 rand = EVP_RAND_fetch(ctx, "TEST-RAND", NULL);
672 if (rand == NULL)
673 return 0;
674
675 parent_rand = EVP_RAND_CTX_new(rand, NULL);
676 EVP_RAND_free(rand);
677 if (parent_rand == NULL)
678 goto err;
679
680 drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
681 &strength);
682 if (!EVP_RAND_CTX_set_params(parent_rand, drbg_params))
683 goto err;
684
685 rand = EVP_RAND_fetch(ctx, "HASH-DRBG", NULL);
686 if (rand == NULL)
687 goto err;
688
689 kat_rand = EVP_RAND_CTX_new(rand, parent_rand);
690 EVP_RAND_free(rand);
691 if (kat_rand == NULL)
692 goto err;
693
694 drbg_params[0] = OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0);
695 if (!EVP_RAND_CTX_set_params(kat_rand, drbg_params))
696 goto err;
697
698 /* Instantiate the RNGs */
699 drbg_params[0] =
700 OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
701 (void *)entropy, entropy_len);
702 drbg_params[1] =
703 OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
704 (void *)nonce, nonce_len);
705 if (!EVP_RAND_instantiate(parent_rand, strength, 0, NULL, 0, drbg_params))
706 goto err;
707
708 EVP_RAND_CTX_free(parent_rand);
709 parent_rand = NULL;
710
711 if (!EVP_RAND_instantiate(kat_rand, strength, 0, persstr, persstr_len, NULL))
712 goto err;
713
714 /* When we set the new private generator this one is freed, so upref it */
715 if (!EVP_RAND_CTX_up_ref(main_rand))
716 goto err;
717
718 /* Update the library context DRBG */
719 if (RAND_set0_private(ctx, kat_rand) > 0) {
720 /* Keeping a copy to verify zeroization */
721 if (EVP_RAND_CTX_up_ref(kat_rand))
722 return 1;
723 RAND_set0_private(ctx, main_rand);
724 }
725
726 err:
727 EVP_RAND_CTX_free(parent_rand);
728 EVP_RAND_CTX_free(kat_rand);
729 kat_rand = NULL;
730 return 0;
731 }
732
reset_main_drbg(OSSL_LIB_CTX * ctx)733 static int reset_main_drbg(OSSL_LIB_CTX *ctx) {
734 int ret = 1;
735
736 if (!RAND_set0_private(ctx, main_rand))
737 ret = 0;
738 if (kat_rand != NULL) {
739 if (!EVP_RAND_uninstantiate(kat_rand)
740 || !EVP_RAND_verify_zeroization(kat_rand))
741 ret = 0;
742 EVP_RAND_CTX_free(kat_rand);
743 kat_rand = NULL;
744 }
745 return ret;
746 }
747
setup_main_random(OSSL_LIB_CTX * libctx)748 static int setup_main_random(OSSL_LIB_CTX *libctx)
749 {
750 OSSL_PARAM drbg_params[3] = {
751 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
752 };
753 unsigned int strength = 256, generate = 1;
754 EVP_RAND *rand;
755
756 rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
757 if (rand == NULL)
758 return 0;
759
760 main_rand = EVP_RAND_CTX_new(rand, NULL);
761 EVP_RAND_free(rand);
762 if (main_rand == NULL)
763 goto err;
764
765 drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_GENERATE,
766 &generate);
767 drbg_params[1] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
768 &strength);
769
770 if (!EVP_RAND_instantiate(main_rand, strength, 0, NULL, 0, drbg_params))
771 goto err;
772 return 1;
773 err:
774 EVP_RAND_CTX_free(main_rand);
775 return 0;
776 }
777
778 /*
779 * Run the algorithm KAT's.
780 * Return 1 is successful, otherwise return 0.
781 * This runs all the tests regardless of if any fail.
782 */
SELF_TEST_kats(OSSL_SELF_TEST * st,OSSL_LIB_CTX * libctx)783 int SELF_TEST_kats(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
784 {
785 EVP_RAND_CTX *saved_rand = ossl_rand_get0_private_noncreating(libctx);
786 int ret = 1;
787
788 if (saved_rand != NULL && !EVP_RAND_CTX_up_ref(saved_rand))
789 return 0;
790 if (!setup_main_random(libctx)
791 || !RAND_set0_private(libctx, main_rand)) {
792 /* Decrement saved_rand reference counter */
793 EVP_RAND_CTX_free(saved_rand);
794 EVP_RAND_CTX_free(main_rand);
795 return 0;
796 }
797
798 if (!self_test_digests(st, libctx))
799 ret = 0;
800 if (!self_test_ciphers(st, libctx))
801 ret = 0;
802 if (!self_test_signatures(st, libctx))
803 ret = 0;
804 if (!self_test_kdfs(st, libctx))
805 ret = 0;
806 if (!self_test_drbgs(st, libctx))
807 ret = 0;
808 if (!self_test_kas(st, libctx))
809 ret = 0;
810
811 RAND_set0_private(libctx, saved_rand);
812 return ret;
813 }
814
815