Allow the sshkdf type to be passed as a single character
[openssl.git] / providers / fips / self_test_kats.c
1 /*
2  * Copyright 2019-2021 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,
163                                                  p->data_len))
164                 goto err;
165             break;
166         }
167         case OSSL_PARAM_OCTET_STRING: {
168             if (!OSSL_PARAM_BLD_push_octet_string(bld, p->name, p->data,
169                                                   p->data_len))
170                 goto err;
171             break;
172         }
173         case OSSL_PARAM_INTEGER: {
174             if (!OSSL_PARAM_BLD_push_int(bld, p->name, *(int *)p->data))
175                 goto err;
176             break;
177         }
178         default:
179             break;
180         }
181     }
182     ret = 1;
183 err:
184     return ret;
185 }
186
187 static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
188                          OSSL_LIB_CTX *libctx)
189 {
190     int ret = 0;
191     unsigned char out[128];
192     EVP_KDF *kdf = NULL;
193     EVP_KDF_CTX *ctx = NULL;
194     BN_CTX *bnctx = NULL;
195     OSSL_PARAM *params  = NULL;
196     OSSL_PARAM_BLD *bld = NULL;
197
198     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
199
200     bld = OSSL_PARAM_BLD_new();
201     if (bld == NULL)
202         goto err;
203
204     kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
205     if (kdf == NULL)
206         goto err;
207
208     ctx = EVP_KDF_CTX_new(kdf);
209     if (ctx == NULL)
210         goto err;
211
212     bnctx = BN_CTX_new_ex(libctx);
213     if (bnctx == NULL)
214         goto err;
215     if (!add_params(bld, t->params, bnctx))
216         goto err;
217     params = OSSL_PARAM_BLD_to_param(bld);
218     if (params == NULL)
219         goto err;
220     if (!EVP_KDF_CTX_set_params(ctx, params))
221         goto err;
222
223     if (t->expected_len > sizeof(out))
224         goto err;
225     if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
226         goto err;
227
228     OSSL_SELF_TEST_oncorrupt_byte(st, out);
229
230     if (memcmp(out, t->expected,  t->expected_len) != 0)
231         goto err;
232
233     ret = 1;
234 err:
235     EVP_KDF_free(kdf);
236     EVP_KDF_CTX_free(ctx);
237     BN_CTX_free(bnctx);
238     OSSL_PARAM_BLD_free_params(params);
239     OSSL_PARAM_BLD_free(bld);
240     OSSL_SELF_TEST_onend(st, ret);
241     return ret;
242 }
243
244 static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
245                           OSSL_LIB_CTX *libctx)
246 {
247     int ret = 0;
248     unsigned char out[256];
249     EVP_RAND *rand;
250     EVP_RAND_CTX *test = NULL, *drbg = NULL;
251     unsigned int strength = 256;
252     int prediction_resistance = 1; /* Causes a reseed */
253     OSSL_PARAM drbg_params[3] = {
254         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
255     };
256
257     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
258
259     rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
260     if (rand == NULL)
261         goto err;
262
263     test = EVP_RAND_CTX_new(rand, NULL);
264     EVP_RAND_free(rand);
265     if (test == NULL)
266         goto err;
267
268     drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
269                                                &strength);
270     if (!EVP_RAND_set_ctx_params(test, drbg_params))
271         goto err;
272
273     rand = EVP_RAND_fetch(libctx, t->algorithm, NULL);
274     if (rand == NULL)
275         goto err;
276
277     drbg = EVP_RAND_CTX_new(rand, test);
278     EVP_RAND_free(rand);
279     if (drbg == NULL)
280         goto err;
281
282     strength = EVP_RAND_strength(drbg);
283
284     drbg_params[0] = OSSL_PARAM_construct_utf8_string(t->param_name,
285                                                       t->param_value, 0);
286     /* This is only used by HMAC-DRBG but it is ignored by the others */
287     drbg_params[1] =
288         OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
289     if (!EVP_RAND_set_ctx_params(drbg, drbg_params))
290         goto err;
291
292     drbg_params[0] =
293         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
294                                           (void *)t->entropyin,
295                                           t->entropyinlen);
296     drbg_params[1] =
297         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
298                                           (void *)t->nonce, t->noncelen);
299     if (!EVP_RAND_set_ctx_params(test, drbg_params)
300             || !EVP_RAND_instantiate(test, strength, 0, NULL, 0))
301         goto err;
302     if (!EVP_RAND_instantiate(drbg, strength, 0, t->persstr, t->persstrlen))
303         goto err;
304
305     drbg_params[0] =
306         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
307                                           (void *)t->entropyinpr1,
308                                           t->entropyinpr1len);
309     if (!EVP_RAND_set_ctx_params(test, drbg_params))
310         goto err;
311
312     if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
313                            prediction_resistance,
314                            t->entropyaddin1, t->entropyaddin1len))
315         goto err;
316
317     drbg_params[0] =
318         OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
319                                          (void *)t->entropyinpr2,
320                                          t->entropyinpr2len);
321     if (!EVP_RAND_set_ctx_params(test, drbg_params))
322         goto err;
323
324     /*
325      * This calls ossl_prov_drbg_reseed() internally when
326      * prediction_resistance = 1
327      */
328     if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
329                            prediction_resistance,
330                            t->entropyaddin2, t->entropyaddin2len))
331         goto err;
332
333     OSSL_SELF_TEST_oncorrupt_byte(st, out);
334
335     if (memcmp(out, t->expected, t->expectedlen) != 0)
336         goto err;
337
338     if (!EVP_RAND_uninstantiate(drbg))
339         goto err;
340     /*
341      * Check that the DRBG data has been zeroized after
342      * ossl_prov_drbg_uninstantiate.
343      */
344     if (!EVP_RAND_verify_zeroization(drbg))
345         goto err;
346
347     ret = 1;
348 err:
349     EVP_RAND_CTX_free(drbg);
350     EVP_RAND_CTX_free(test);
351     OSSL_SELF_TEST_onend(st, ret);
352     return ret;
353 }
354
355 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
356 static int self_test_ka(const ST_KAT_KAS *t,
357                         OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
358 {
359     int ret = 0;
360     EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
361     EVP_PKEY *pkey = NULL, *peerkey = NULL;
362     OSSL_PARAM *params = NULL;
363     OSSL_PARAM *params_peer = NULL;
364     unsigned char secret[256];
365     size_t secret_len = sizeof(secret);
366     OSSL_PARAM_BLD *bld = NULL;
367     BN_CTX *bnctx = NULL;
368
369     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
370
371     bnctx = BN_CTX_new_ex(libctx);
372     if (bnctx == NULL)
373         goto err;
374
375     bld = OSSL_PARAM_BLD_new();
376     if (bld == NULL)
377         goto err;
378
379     if (!add_params(bld, t->key_group, bnctx)
380         || !add_params(bld, t->key_host_data, bnctx))
381         goto err;
382     params = OSSL_PARAM_BLD_to_param(bld);
383
384     if (!add_params(bld, t->key_group, bnctx)
385         || !add_params(bld, t->key_peer_data, bnctx))
386         goto err;
387
388     params_peer = OSSL_PARAM_BLD_to_param(bld);
389     if (params == NULL || params_peer == NULL)
390         goto err;
391
392     /* Create a EVP_PKEY_CTX to load the DH keys into */
393     kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
394     if (kactx == NULL)
395         goto err;
396     if (EVP_PKEY_fromdata_init(kactx) <= 0
397         || EVP_PKEY_fromdata(kactx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
398         goto err;
399     if (EVP_PKEY_fromdata_init(kactx) <= 0
400         || EVP_PKEY_fromdata(kactx, &peerkey, EVP_PKEY_KEYPAIR, params_peer) <= 0)
401         goto err;
402
403     /* Create a EVP_PKEY_CTX to perform key derivation */
404     dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
405     if (dctx == NULL)
406         goto err;
407
408     if (EVP_PKEY_derive_init(dctx) <= 0
409         || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
410         || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
411         goto err;
412
413     OSSL_SELF_TEST_oncorrupt_byte(st, secret);
414
415     if (secret_len != t->expected_len
416         || memcmp(secret, t->expected, t->expected_len) != 0)
417         goto err;
418     ret = 1;
419 err:
420     BN_CTX_free(bnctx);
421     EVP_PKEY_free(pkey);
422     EVP_PKEY_free(peerkey);
423     EVP_PKEY_CTX_free(kactx);
424     EVP_PKEY_CTX_free(dctx);
425     OSSL_PARAM_BLD_free_params(params_peer);
426     OSSL_PARAM_BLD_free_params(params);
427     OSSL_PARAM_BLD_free(bld);
428     OSSL_SELF_TEST_onend(st, ret);
429     return ret;
430 }
431 #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
432
433 static int self_test_sign(const ST_KAT_SIGN *t,
434                          OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
435 {
436     int ret = 0;
437     OSSL_PARAM *params = NULL, *params_sig = NULL;
438     OSSL_PARAM_BLD *bld = NULL;
439     EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
440     EVP_PKEY *pkey = NULL;
441     unsigned char sig[256];
442     BN_CTX *bnctx = NULL;
443     size_t siglen = 0;
444     static const unsigned char dgst[] = {
445         0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
446         0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
447         0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
448     };
449
450     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE, t->desc);
451
452     bnctx = BN_CTX_new_ex(libctx);
453     if (bnctx == NULL)
454         goto err;
455
456     bld = OSSL_PARAM_BLD_new();
457     if (bld == NULL)
458         goto err;
459
460     if (!add_params(bld, t->key, bnctx))
461         goto err;
462     params = OSSL_PARAM_BLD_to_param(bld);
463
464     /* Create a EVP_PKEY_CTX to load the DSA key into */
465     kctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
466     if (kctx == NULL || params == NULL)
467         goto err;
468     if (EVP_PKEY_fromdata_init(kctx) <= 0
469         || EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
470         goto err;
471
472     /* Create a EVP_PKEY_CTX to use for the signing operation */
473     sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
474     if (sctx == NULL
475         || EVP_PKEY_sign_init(sctx) <= 0)
476         goto err;
477
478     /* set signature parameters */
479     if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_SIGNATURE_PARAM_DIGEST,
480                                          t->mdalgorithm,
481                                          strlen(t->mdalgorithm) + 1))
482         goto err;
483     params_sig = OSSL_PARAM_BLD_to_param(bld);
484     if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
485         goto err;
486
487     if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
488         || EVP_PKEY_verify_init(sctx) <= 0
489         || EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
490         goto err;
491
492     /*
493      * Used by RSA, for other key types where the signature changes, we
494      * can only use the verify.
495      */
496     if (t->sig_expected != NULL
497         && (siglen != t->sig_expected_len
498             || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
499         goto err;
500
501     OSSL_SELF_TEST_oncorrupt_byte(st, sig);
502     if (EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
503         goto err;
504     ret = 1;
505 err:
506     BN_CTX_free(bnctx);
507     EVP_PKEY_free(pkey);
508     EVP_PKEY_CTX_free(kctx);
509     EVP_PKEY_CTX_free(sctx);
510     OSSL_PARAM_BLD_free_params(params);
511     OSSL_PARAM_BLD_free_params(params_sig);
512     OSSL_PARAM_BLD_free(bld);
513     OSSL_SELF_TEST_onend(st, ret);
514     return ret;
515 }
516
517 /*
518  * Test an encrypt or decrypt KAT..
519  *
520  * FIPS 140-2 IG D.9 states that separate KAT tests are needed for encrypt
521  * and decrypt..
522  */
523 static int self_test_asym_cipher(const ST_KAT_ASYM_CIPHER *t, OSSL_SELF_TEST *st,
524                                  OSSL_LIB_CTX *libctx)
525 {
526     int ret = 0;
527     OSSL_PARAM *keyparams = NULL, *initparams = NULL;
528     OSSL_PARAM_BLD *keybld = NULL, *initbld = NULL;
529     EVP_PKEY_CTX *encctx = NULL, *keyctx = NULL;
530     EVP_PKEY *key = NULL;
531     BN_CTX *bnctx = NULL;
532     unsigned char out[256];
533     size_t outlen = sizeof(out);
534
535     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER, t->desc);
536
537     bnctx = BN_CTX_new_ex(libctx);
538     if (bnctx == NULL)
539         goto err;
540
541     /* Load a public or private key from data */
542     keybld = OSSL_PARAM_BLD_new();
543     if (keybld == NULL
544         || !add_params(keybld, t->key, bnctx))
545         goto err;
546     keyparams = OSSL_PARAM_BLD_to_param(keybld);
547     keyctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, NULL);
548     if (keyctx == NULL || keyparams == NULL)
549         goto err;
550     if (EVP_PKEY_fromdata_init(keyctx) <= 0
551         || EVP_PKEY_fromdata(keyctx, &key, EVP_PKEY_KEYPAIR, keyparams) <= 0)
552         goto err;
553
554     /* Create a EVP_PKEY_CTX to use for the encrypt or decrypt operation */
555     encctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL);
556     if (encctx == NULL
557         || (t->encrypt && EVP_PKEY_encrypt_init(encctx) <= 0)
558         || (!t->encrypt && EVP_PKEY_decrypt_init(encctx) <= 0))
559         goto err;
560
561     /* Add any additional parameters such as padding */
562     if (t->postinit != NULL) {
563         initbld = OSSL_PARAM_BLD_new();
564         if (initbld == NULL)
565             goto err;
566         if (!add_params(initbld, t->postinit, bnctx))
567             goto err;
568         initparams = OSSL_PARAM_BLD_to_param(initbld);
569         if (initparams == NULL)
570             goto err;
571         if (EVP_PKEY_CTX_set_params(encctx, initparams) <= 0)
572             goto err;
573     }
574
575     if (t->encrypt) {
576         if (EVP_PKEY_encrypt(encctx, out, &outlen,
577                              t->in, t->in_len) <= 0)
578             goto err;
579     } else {
580         if (EVP_PKEY_decrypt(encctx, out, &outlen,
581                              t->in, t->in_len) <= 0)
582             goto err;
583     }
584     /* Check the KAT */
585     OSSL_SELF_TEST_oncorrupt_byte(st, out);
586     if (outlen != t->expected_len
587         || memcmp(out, t->expected, t->expected_len) != 0)
588         goto err;
589
590     ret = 1;
591 err:
592     BN_CTX_free(bnctx);
593     EVP_PKEY_free(key);
594     EVP_PKEY_CTX_free(encctx);
595     EVP_PKEY_CTX_free(keyctx);
596     OSSL_PARAM_BLD_free_params(keyparams);
597     OSSL_PARAM_BLD_free(keybld);
598     OSSL_PARAM_BLD_free_params(initparams);
599     OSSL_PARAM_BLD_free(initbld);
600     OSSL_SELF_TEST_onend(st, ret);
601     return ret;
602 }
603
604 /*
605  * Test a data driven list of KAT's for digest algorithms.
606  * All tests are run regardless of if they fail or not.
607  * Return 0 if any test fails.
608  */
609 static int self_test_digests(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
610 {
611     int i, ret = 1;
612
613     for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
614         if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
615             ret = 0;
616     }
617     return ret;
618 }
619
620 static int self_test_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
621 {
622     int i, ret = 1;
623
624     for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
625         if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
626             ret = 0;
627     }
628     return ret;
629 }
630
631 static int self_test_asym_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
632 {
633     int i, ret = 1;
634
635     for (i = 0; i < (int)OSSL_NELEM(st_kat_asym_cipher_tests); ++i) {
636         if (!self_test_asym_cipher(&st_kat_asym_cipher_tests[i], st, libctx))
637             ret = 0;
638     }
639     return ret;
640 }
641
642 static int self_test_kdfs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
643 {
644     int i, ret = 1;
645
646     for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
647         if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
648             ret = 0;
649     }
650     return ret;
651 }
652
653 static int self_test_drbgs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
654 {
655     int i, ret = 1;
656
657     for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
658         if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
659             ret = 0;
660     }
661     return ret;
662 }
663
664 static int self_test_kas(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
665 {
666     int ret = 1;
667 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
668     int i;
669
670     for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
671         if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
672             ret = 0;
673     }
674 #endif
675
676     return ret;
677 }
678
679 static int self_test_signatures(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
680 {
681     int i, ret = 1;
682
683     for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
684         if (!self_test_sign(&st_kat_sign_tests[i], st, libctx))
685             ret = 0;
686     }
687     return ret;
688 }
689
690 /*
691  * Run the algorithm KAT's.
692  * Return 1 is successful, otherwise return 0.
693  * This runs all the tests regardless of if any fail.
694  */
695 int SELF_TEST_kats(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
696 {
697     int ret = 1;
698
699     if (!self_test_digests(st, libctx))
700         ret = 0;
701     if (!self_test_ciphers(st, libctx))
702         ret = 0;
703     if (!self_test_signatures(st, libctx))
704         ret = 0;
705     if (!self_test_kdfs(st, libctx))
706         ret = 0;
707     if (!self_test_drbgs(st, libctx))
708         ret = 0;
709     if (!self_test_kas(st, libctx))
710         ret = 0;
711     if (!self_test_asym_ciphers(st, libctx))
712         ret = 0;
713
714     return ret;
715 }