Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[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 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 "internal/cryptlib.h"
16 #include "internal/nelem.h"
17 #include "self_test.h"
18 #include "self_test_data.inc"
19
20 static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_SELF_TEST *st,
21                             OSSL_LIB_CTX *libctx)
22 {
23     int ok = 0;
24     unsigned char out[EVP_MAX_MD_SIZE];
25     unsigned int out_len = 0;
26     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
27     EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
28
29     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
30
31     if (ctx == NULL
32             || md == NULL
33             || !EVP_DigestInit_ex(ctx, md, NULL)
34             || !EVP_DigestUpdate(ctx, t->pt, t->pt_len)
35             || !EVP_DigestFinal(ctx, out, &out_len))
36         goto err;
37
38     /* Optional corruption */
39     OSSL_SELF_TEST_oncorrupt_byte(st, out);
40
41     if (out_len != t->expected_len
42             || memcmp(out, t->expected, out_len) != 0)
43         goto err;
44     ok = 1;
45 err:
46     EVP_MD_free(md);
47     EVP_MD_CTX_free(ctx);
48     OSSL_SELF_TEST_onend(st, ok);
49     return ok;
50 }
51
52 /*
53  * Helper function to setup a EVP_CipherInit
54  * Used to hide the complexity of Authenticated ciphers.
55  */
56 static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
57                        const ST_KAT_CIPHER *t, int enc)
58 {
59     unsigned char *in_tag = NULL;
60     int pad = 0, tmp;
61
62     /* Flag required for Key wrapping */
63     EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
64     if (t->tag == NULL) {
65         /* Use a normal cipher init */
66         return EVP_CipherInit_ex(ctx, cipher, NULL, t->key, t->iv, enc)
67                && EVP_CIPHER_CTX_set_padding(ctx, pad);
68     }
69
70     /* The authenticated cipher init */
71     if (!enc)
72         in_tag = (unsigned char *)t->tag;
73
74     return EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)
75            && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, t->iv_len, NULL)
76            && (in_tag == NULL
77                || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->tag_len,
78                                       in_tag))
79            && EVP_CipherInit_ex(ctx, NULL, NULL, t->key, t->iv, enc)
80            && EVP_CIPHER_CTX_set_padding(ctx, pad)
81            && EVP_CipherUpdate(ctx, NULL, &tmp, t->aad, t->aad_len);
82 }
83
84 /* Test a single KAT for encrypt/decrypt */
85 static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_SELF_TEST *st,
86                             OSSL_LIB_CTX *libctx)
87 {
88     int ret = 0, encrypt = 1, len, ct_len = 0, pt_len = 0;
89     EVP_CIPHER_CTX *ctx = NULL;
90     EVP_CIPHER *cipher = NULL;
91     unsigned char ct_buf[256] = { 0 };
92     unsigned char pt_buf[256] = { 0 };
93
94     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
95
96     ctx = EVP_CIPHER_CTX_new();
97     if (ctx == NULL)
98         goto err;
99     cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, "");
100     if (cipher == NULL)
101         goto err;
102
103     /* Encrypt plain text message */
104     if (!cipher_init(ctx, cipher, t, encrypt)
105             || !EVP_CipherUpdate(ctx, ct_buf, &len, t->base.pt, t->base.pt_len)
106             || !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
107         goto err;
108
109     OSSL_SELF_TEST_oncorrupt_byte(st, ct_buf);
110     ct_len += len;
111     if (ct_len != (int)t->base.expected_len
112         || memcmp(t->base.expected, ct_buf, ct_len) != 0)
113         goto err;
114
115     if (t->tag != NULL) {
116         unsigned char tag[16] = { 0 };
117
118         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, t->tag_len, tag)
119             || memcmp(tag, t->tag, t->tag_len) != 0)
120             goto err;
121     }
122
123     if (!(cipher_init(ctx, cipher, t, !encrypt)
124           && EVP_CipherUpdate(ctx, pt_buf, &len, ct_buf, ct_len)
125           && EVP_CipherFinal_ex(ctx, pt_buf + len, &pt_len)))
126         goto err;
127     pt_len += len;
128
129     if (pt_len != (int)t->base.pt_len
130             || memcmp(pt_buf, t->base.pt, pt_len) != 0)
131         goto err;
132
133     ret = 1;
134 err:
135     EVP_CIPHER_free(cipher);
136     EVP_CIPHER_CTX_free(ctx);
137     OSSL_SELF_TEST_onend(st, ret);
138     return ret;
139 }
140
141 static int add_params(OSSL_PARAM_BLD *bld, const ST_KAT_PARAM *params,
142                       BN_CTX *ctx)
143 {
144     int ret = 0;
145     const ST_KAT_PARAM *p;
146
147     if (params == NULL)
148         return 1;
149     for (p = params; p->data != NULL; ++p)
150     {
151         switch (p->type) {
152         case OSSL_PARAM_UNSIGNED_INTEGER: {
153             BIGNUM *bn = BN_CTX_get(ctx);
154
155             if (bn == NULL
156                 || (BN_bin2bn(p->data, p->data_len, bn) == NULL)
157                 || !OSSL_PARAM_BLD_push_BN(bld, p->name, bn))
158                 goto err;
159             break;
160         }
161         case OSSL_PARAM_UTF8_STRING: {
162             if (!OSSL_PARAM_BLD_push_utf8_string(bld, p->name, p->data, 0))
163                 goto err;
164             break;
165         }
166         case OSSL_PARAM_OCTET_STRING: {
167             if (!OSSL_PARAM_BLD_push_octet_string(bld, p->name, p->data,
168                                                   p->data_len))
169                 goto err;
170             break;
171         }
172         default:
173             break;
174         }
175     }
176     ret = 1;
177 err:
178     return ret;
179 }
180
181 static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
182                          OSSL_LIB_CTX *libctx)
183 {
184     int ret = 0;
185     unsigned char out[64];
186     EVP_KDF *kdf = NULL;
187     EVP_KDF_CTX *ctx = NULL;
188     BN_CTX *bnctx = NULL;
189     OSSL_PARAM *params  = NULL;
190     OSSL_PARAM_BLD *bld = NULL;
191
192     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
193
194     bld = OSSL_PARAM_BLD_new();
195     if (bld == NULL)
196         goto err;
197
198     kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
199     if (kdf == NULL)
200         goto err;
201
202     ctx = EVP_KDF_CTX_new(kdf);
203     if (ctx == NULL)
204         goto err;
205
206     bnctx = BN_CTX_new_ex(libctx);
207     if (bnctx == NULL)
208         goto err;
209     if (!add_params(bld, t->params, bnctx))
210         goto err;
211     params = OSSL_PARAM_BLD_to_param(bld);
212     if (params == NULL)
213         goto err;
214     if (!EVP_KDF_CTX_set_params(ctx, params))
215         goto err;
216
217     if (t->expected_len > sizeof(out))
218         goto err;
219     if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
220         goto err;
221
222     OSSL_SELF_TEST_oncorrupt_byte(st, out);
223
224     if (memcmp(out, t->expected,  t->expected_len) != 0)
225         goto err;
226
227     ret = 1;
228 err:
229     EVP_KDF_free(kdf);
230     EVP_KDF_CTX_free(ctx);
231     BN_CTX_free(bnctx);
232     OSSL_PARAM_BLD_free_params(params);
233     OSSL_PARAM_BLD_free(bld);
234     OSSL_SELF_TEST_onend(st, ret);
235     return ret;
236 }
237
238 static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
239                           OSSL_LIB_CTX *libctx)
240 {
241     int ret = 0;
242     unsigned char out[256];
243     EVP_RAND *rand;
244     EVP_RAND_CTX *test = NULL, *drbg = NULL;
245     unsigned int strength = 256;
246     int prediction_resistance = 1; /* Causes a reseed */
247     OSSL_PARAM drbg_params[3] = {
248         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
249     };
250
251     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
252
253     rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
254     if (rand == NULL)
255         goto err;
256
257     test = EVP_RAND_CTX_new(rand, NULL);
258     EVP_RAND_free(rand);
259     if (test == NULL)
260         goto err;
261
262     drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
263                                                &strength);
264     if (!EVP_RAND_set_ctx_params(test, drbg_params))
265         goto err;
266
267     rand = EVP_RAND_fetch(libctx, t->algorithm, NULL);
268     if (rand == NULL)
269         goto err;
270
271     drbg = EVP_RAND_CTX_new(rand, test);
272     EVP_RAND_free(rand);
273     if (drbg == NULL)
274         goto err;
275
276     strength = EVP_RAND_strength(drbg);
277
278     drbg_params[0] = OSSL_PARAM_construct_utf8_string(t->param_name,
279                                                       t->param_value, 0);
280     /* This is only used by HMAC-DRBG but it is ignored by the others */
281     drbg_params[1] =
282         OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
283     if (!EVP_RAND_set_ctx_params(drbg, drbg_params))
284         goto err;
285
286     drbg_params[0] =
287         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
288                                           (void *)t->entropyin,
289                                           t->entropyinlen);
290     drbg_params[1] =
291         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
292                                           (void *)t->nonce, t->noncelen);
293     if (!EVP_RAND_set_ctx_params(test, drbg_params)
294             || !EVP_RAND_instantiate(test, strength, 0, NULL, 0))
295         goto err;
296     if (!EVP_RAND_instantiate(drbg, strength, 0, t->persstr, t->persstrlen))
297         goto err;
298
299     drbg_params[0] =
300         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
301                                           (void *)t->entropyinpr1,
302                                           t->entropyinpr1len);
303     if (!EVP_RAND_set_ctx_params(test, drbg_params))
304         goto err;
305
306     if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
307                            prediction_resistance,
308                            t->entropyaddin1, t->entropyaddin1len))
309         goto err;
310
311     drbg_params[0] =
312         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
313                                          (void *)t->entropyinpr2,
314                                          t->entropyinpr2len);
315     if (!EVP_RAND_set_ctx_params(test, drbg_params))
316         goto err;
317
318     /*
319      * This calls ossl_prov_drbg_reseed() internally when
320      * prediction_resistance = 1
321      */
322     if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
323                            prediction_resistance,
324                            t->entropyaddin2, t->entropyaddin2len))
325         goto err;
326
327     OSSL_SELF_TEST_oncorrupt_byte(st, out);
328
329     if (memcmp(out, t->expected, t->expectedlen) != 0)
330         goto err;
331
332     if (!EVP_RAND_uninstantiate(drbg))
333         goto err;
334     /*
335      * Check that the DRBG data has been zeroized after
336      * ossl_prov_drbg_uninstantiate.
337      */
338     if (!EVP_RAND_verify_zeroization(drbg))
339         goto err;
340
341     ret = 1;
342 err:
343     EVP_RAND_CTX_free(drbg);
344     EVP_RAND_CTX_free(test);
345     OSSL_SELF_TEST_onend(st, ret);
346     return ret;
347 }
348
349 static int self_test_ka(const ST_KAT_KAS *t,
350                         OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
351 {
352     int ret = 0;
353     EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
354     EVP_PKEY *pkey = NULL, *peerkey = NULL;
355     OSSL_PARAM *params = NULL;
356     OSSL_PARAM *params_peer = NULL;
357     unsigned char secret[256];
358     size_t secret_len;
359     OSSL_PARAM_BLD *bld = NULL;
360     BN_CTX *bnctx = NULL;
361
362     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
363
364     bnctx = BN_CTX_new_ex(libctx);
365     if (bnctx == NULL)
366         goto err;
367
368     bld = OSSL_PARAM_BLD_new();
369     if (bld == NULL)
370         goto err;
371
372     if (!add_params(bld, t->key_group, bnctx)
373         || !add_params(bld, t->key_host_data, bnctx))
374         goto err;
375     params = OSSL_PARAM_BLD_to_param(bld);
376
377     if (!add_params(bld, t->key_group, bnctx)
378         || !add_params(bld, t->key_peer_data, bnctx))
379         goto err;
380
381     params_peer = OSSL_PARAM_BLD_to_param(bld);
382     if (params == NULL || params_peer == NULL)
383         goto err;
384
385     /* Create a EVP_PKEY_CTX to load the DH keys into */
386     kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
387     if (kactx == NULL)
388         goto err;
389     if (EVP_PKEY_key_fromdata_init(kactx) <= 0
390         || EVP_PKEY_fromdata(kactx, &pkey, params) <= 0)
391         goto err;
392     if (EVP_PKEY_key_fromdata_init(kactx) <= 0
393         || EVP_PKEY_fromdata(kactx, &peerkey, params_peer) <= 0)
394         goto err;
395
396     /* Create a EVP_PKEY_CTX to perform key derivation */
397     dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
398     if (dctx == NULL)
399         goto err;
400
401     if (EVP_PKEY_derive_init(dctx) <= 0
402         || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
403         || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
404         goto err;
405
406     OSSL_SELF_TEST_oncorrupt_byte(st, secret);
407
408     if (secret_len != t->expected_len
409         || memcmp(secret, t->expected, t->expected_len) != 0)
410         goto err;
411     ret = 1;
412 err:
413     BN_CTX_free(bnctx);
414     EVP_PKEY_free(pkey);
415     EVP_PKEY_free(peerkey);
416     EVP_PKEY_CTX_free(kactx);
417     EVP_PKEY_CTX_free(dctx);
418     OSSL_PARAM_BLD_free_params(params_peer);
419     OSSL_PARAM_BLD_free_params(params);
420     OSSL_PARAM_BLD_free(bld);
421     OSSL_SELF_TEST_onend(st, ret);
422     return ret;
423 }
424
425 static int self_test_sign(const ST_KAT_SIGN *t,
426                          OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
427 {
428     int ret = 0;
429     OSSL_PARAM *params = NULL, *params_sig = NULL;
430     OSSL_PARAM_BLD *bld = NULL;
431     EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
432     EVP_PKEY *pkey = NULL;
433     unsigned char sig[256];
434     BN_CTX *bnctx = NULL;
435     size_t siglen = 0;
436     static const unsigned char dgst[] = {
437         0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
438         0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
439         0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
440     };
441
442     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE, t->desc);
443
444     bnctx = BN_CTX_new_ex(libctx);
445     if (bnctx == NULL)
446         goto err;
447
448     bld = OSSL_PARAM_BLD_new();
449     if (bld == NULL)
450         goto err;
451
452     if (!add_params(bld, t->key, bnctx))
453         goto err;
454     params = OSSL_PARAM_BLD_to_param(bld);
455
456     /* Create a EVP_PKEY_CTX to load the DSA key into */
457     kctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
458     if (kctx == NULL || params == NULL)
459         goto err;
460     if (EVP_PKEY_key_fromdata_init(kctx) <= 0
461         || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
462         goto err;
463
464     /* Create a EVP_PKEY_CTX to use for the signing operation */
465     sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
466     if (sctx == NULL
467         || EVP_PKEY_sign_init(sctx) <= 0)
468         goto err;
469
470     /* set signature parameters */
471     if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_SIGNATURE_PARAM_DIGEST,
472                                          t->mdalgorithm,
473                                          strlen(t->mdalgorithm) + 1))
474         goto err;
475     params_sig = OSSL_PARAM_BLD_to_param(bld);
476     if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
477         goto err;
478
479     if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
480         || EVP_PKEY_verify_init(sctx) <= 0
481         || EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
482         goto err;
483
484     /*
485      * Used by RSA, for other key types where the signature changes, we
486      * can only use the verify.
487      */
488     if (t->sig_expected != NULL
489         && (siglen != t->sig_expected_len
490             || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
491         goto err;
492
493     OSSL_SELF_TEST_oncorrupt_byte(st, sig);
494     if (EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
495         goto err;
496     ret = 1;
497 err:
498     BN_CTX_free(bnctx);
499     EVP_PKEY_free(pkey);
500     EVP_PKEY_CTX_free(kctx);
501     EVP_PKEY_CTX_free(sctx);
502     OSSL_PARAM_BLD_free_params(params);
503     OSSL_PARAM_BLD_free_params(params_sig);
504     OSSL_PARAM_BLD_free(bld);
505     OSSL_SELF_TEST_onend(st, ret);
506     return ret;
507 }
508
509 /*
510  * Test an encrypt or decrypt KAT..
511  *
512  * FIPS 140-2 IG D.9 states that separate KAT tests are needed for encrypt
513  * and decrypt..
514  */
515 static int self_test_asym_cipher(const ST_KAT_ASYM_CIPHER *t, OSSL_SELF_TEST *st,
516                                  OSSL_LIB_CTX *libctx)
517 {
518     int ret = 0;
519     OSSL_PARAM *keyparams = NULL, *initparams = NULL;
520     OSSL_PARAM_BLD *keybld = NULL, *initbld = NULL;
521     EVP_PKEY_CTX *encctx = NULL, *keyctx = NULL;
522     EVP_PKEY *key = NULL;
523     BN_CTX *bnctx = NULL;
524     unsigned char out[256];
525     size_t outlen = sizeof(out);
526
527     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER, t->desc);
528
529     bnctx = BN_CTX_new_ex(libctx);
530     if (bnctx == NULL)
531         goto err;
532
533     /* Load a public or private key from data */
534     keybld = OSSL_PARAM_BLD_new();
535     if (keybld == NULL
536         || !add_params(keybld, t->key, bnctx))
537         goto err;
538     keyparams = OSSL_PARAM_BLD_to_param(keybld);
539     keyctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, NULL);
540     if (keyctx == NULL || keyparams == NULL)
541         goto err;
542     if (EVP_PKEY_key_fromdata_init(keyctx) <= 0
543         || EVP_PKEY_fromdata(keyctx, &key, keyparams) <= 0)
544         goto err;
545
546     /* Create a EVP_PKEY_CTX to use for the encrypt or decrypt operation */
547     encctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL);
548     if (encctx == NULL
549         || (t->encrypt && EVP_PKEY_encrypt_init(encctx) <= 0)
550         || (!t->encrypt && EVP_PKEY_decrypt_init(encctx) <= 0))
551         goto err;
552
553     /* Add any additional parameters such as padding */
554     if (t->postinit != NULL) {
555         initbld = OSSL_PARAM_BLD_new();
556         if (initbld == NULL)
557             goto err;
558         if (!add_params(initbld, t->postinit, bnctx))
559             goto err;
560         initparams = OSSL_PARAM_BLD_to_param(initbld);
561         if (initparams == NULL)
562             goto err;
563         if (EVP_PKEY_CTX_set_params(encctx, initparams) <= 0)
564             goto err;
565     }
566
567     if (t->encrypt) {
568         if (EVP_PKEY_encrypt(encctx, out, &outlen,
569                              t->in, t->in_len) <= 0)
570             goto err;
571     } else {
572         if (EVP_PKEY_decrypt(encctx, out, &outlen,
573                              t->in, t->in_len) <= 0)
574             goto err;
575     }
576     /* Check the KAT */
577     OSSL_SELF_TEST_oncorrupt_byte(st, out);
578     if (outlen != t->expected_len
579         || memcmp(out, t->expected, t->expected_len) != 0)
580         goto err;
581
582     ret = 1;
583 err:
584     BN_CTX_free(bnctx);
585     EVP_PKEY_free(key);
586     EVP_PKEY_CTX_free(encctx);
587     EVP_PKEY_CTX_free(keyctx);
588     OSSL_PARAM_BLD_free_params(keyparams);
589     OSSL_PARAM_BLD_free(keybld);
590     OSSL_PARAM_BLD_free_params(initparams);
591     OSSL_PARAM_BLD_free(initbld);
592     OSSL_SELF_TEST_onend(st, ret);
593     return ret;
594 }
595
596 /*
597  * Test a data driven list of KAT's for digest algorithms.
598  * All tests are run regardless of if they fail or not.
599  * Return 0 if any test fails.
600  */
601 static int self_test_digests(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_digest_tests); ++i) {
606         if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
607             ret = 0;
608     }
609     return ret;
610 }
611
612 static int self_test_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
613 {
614     int i, ret = 1;
615
616     for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
617         if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
618             ret = 0;
619     }
620     return ret;
621 }
622
623 static int self_test_asym_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
624 {
625     int i, ret = 1;
626
627     for (i = 0; i < (int)OSSL_NELEM(st_kat_asym_cipher_tests); ++i) {
628         if (!self_test_asym_cipher(&st_kat_asym_cipher_tests[i], st, libctx))
629             ret = 0;
630     }
631     return ret;
632 }
633
634 static int self_test_kdfs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
635 {
636     int i, ret = 1;
637
638     for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
639         if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
640             ret = 0;
641     }
642     return ret;
643 }
644
645 static int self_test_drbgs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
646 {
647     int i, ret = 1;
648
649     for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
650         if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
651             ret = 0;
652     }
653     return ret;
654 }
655
656 static int self_test_kas(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
657 {
658     int i, ret = 1;
659
660     for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
661         if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
662             ret = 0;
663     }
664     return ret;
665 }
666
667 static int self_test_signatures(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
668 {
669     int i, ret = 1;
670
671     for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
672         if (!self_test_sign(&st_kat_sign_tests[i], st, libctx))
673             ret = 0;
674     }
675     return ret;
676 }
677
678 /*
679  * Run the algorithm KAT's.
680  * Return 1 is successful, otherwise return 0.
681  * This runs all the tests regardless of if any fail.
682  */
683 int SELF_TEST_kats(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
684 {
685     int ret = 1;
686
687     if (!self_test_digests(st, libctx))
688         ret = 0;
689     if (!self_test_ciphers(st, libctx))
690         ret = 0;
691     if (!self_test_signatures(st, libctx))
692         ret = 0;
693     if (!self_test_kdfs(st, libctx))
694         ret = 0;
695     if (!self_test_drbgs(st, libctx))
696         ret = 0;
697     if (!self_test_kas(st, libctx))
698         ret = 0;
699     if (!self_test_asym_ciphers(st, libctx))
700         ret = 0;
701
702     return ret;
703 }