2 * Copyright 2015-2016 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
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pkcs12.h>
19 #include <openssl/kdf.h>
20 #include "internal/numbers.h"
22 /* Remove spaces from beginning and end of a string */
24 static void remove_space(char **pval)
26 unsigned char *p = (unsigned char *)*pval;
33 p = p + strlen(*pval) - 1;
35 /* Remove trailing space */
41 * Given a line of the form:
42 * name = value # comment
43 * extract name and value. NB: modifies passed buffer.
46 static int parse_line(char **pkw, char **pval, char *linebuf)
50 p = linebuf + strlen(linebuf) - 1;
53 fprintf(stderr, "FATAL: missing EOL\n");
59 p = strchr(linebuf, '#');
65 p = strchr(linebuf, '=');
76 /* Remove spaces from keyword and value */
84 * Unescape some escape sequences in string literals.
85 * Return the result in a newly allocated buffer.
86 * Currently only supports '\n'.
87 * If the input length is 0, returns a valid 1-byte buffer, but sets
90 static unsigned char* unescape(const char *input, size_t input_len,
93 unsigned char *ret, *p;
97 return OPENSSL_zalloc(1);
100 /* Escaping is non-expanding; over-allocate original size for simplicity. */
101 ret = p = OPENSSL_malloc(input_len);
105 for (i = 0; i < input_len; i++) {
106 if (input[i] == '\\') {
107 if (i == input_len - 1 || input[i+1] != 'n')
124 /* For a hex string "value" convert to a binary allocated buffer */
125 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
132 * Don't return NULL for zero length buffer.
133 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
134 * a non-NULL key buffer even if the key length is 0, in order to detect
137 *buf = OPENSSL_malloc(1);
144 /* Check for string literal */
145 if (value[0] == '"') {
148 vlen = strlen(value);
149 if (value[vlen - 1] != '"')
152 *buf = unescape(value, vlen, buflen);
158 *buf = OPENSSL_hexstr2buf(value, &len);
160 fprintf(stderr, "Value=%s\n", value);
161 ERR_print_errors_fp(stderr);
164 /* Size of input buffer means we'll never overflow */
168 #ifndef OPENSSL_NO_SCRYPT
169 /* Currently only used by scrypt tests */
170 /* Parse unsigned decimal 64 bit integer value */
171 static int test_uint64(const char *value, uint64_t *pr)
173 const char *p = value;
175 fprintf(stderr, "Invalid empty integer value\n");
180 if (*pr > UINT64_MAX/10) {
181 fprintf(stderr, "Integer string overflow value=%s\n", value);
185 if (*p < '0' || *p > '9') {
186 fprintf(stderr, "Invalid integer string value=%s\n", value);
196 /* Structure holding test information */
198 /* file being read */
200 /* temp memory BIO for reading in keys */
202 /* List of public and private keys */
203 struct key_list *private;
204 struct key_list *public;
205 /* method for this test */
206 const struct evp_test_method *meth;
207 /* current line being processed */
209 /* start line of current test */
210 unsigned int start_line;
211 /* Error string for test */
212 const char *err, *aux_err;
213 /* Expected error value of test */
215 /* Expected error function string */
217 /* Expected error reason string */
219 /* Number of tests */
223 /* Number of tests skipped */
225 /* If output mismatch expected and got value */
226 unsigned char *out_received;
227 size_t out_received_len;
228 unsigned char *out_expected;
229 size_t out_expected_len;
230 /* test specific data */
232 /* Current test should be skipped */
239 struct key_list *next;
242 /* Test method structure */
243 struct evp_test_method {
244 /* Name of test as it appears in file */
246 /* Initialise test for "alg" */
247 int (*init) (struct evp_test * t, const char *alg);
248 /* Clean up method */
249 void (*cleanup) (struct evp_test * t);
250 /* Test specific name value pair processing */
251 int (*parse) (struct evp_test * t, const char *name, const char *value);
252 /* Run the test itself */
253 int (*run_test) (struct evp_test * t);
256 static const struct evp_test_method digest_test_method, cipher_test_method;
257 static const struct evp_test_method mac_test_method;
258 static const struct evp_test_method psign_test_method, pverify_test_method;
259 static const struct evp_test_method pdecrypt_test_method;
260 static const struct evp_test_method pverify_recover_test_method;
261 static const struct evp_test_method pderive_test_method;
262 static const struct evp_test_method pbe_test_method;
263 static const struct evp_test_method encode_test_method;
264 static const struct evp_test_method kdf_test_method;
266 static const struct evp_test_method *evp_test_list[] = {
271 &pverify_test_method,
272 &pdecrypt_test_method,
273 &pverify_recover_test_method,
274 &pderive_test_method,
281 static const struct evp_test_method *evp_find_test(const char *name)
283 const struct evp_test_method **tt;
285 for (tt = evp_test_list; *tt; tt++) {
286 if (strcmp(name, (*tt)->name) == 0)
292 static void hex_print(const char *name, const unsigned char *buf, size_t len)
295 fprintf(stderr, "%s ", name);
296 for (i = 0; i < len; i++)
297 fprintf(stderr, "%02X", buf[i]);
301 static void free_expected(struct evp_test *t)
303 OPENSSL_free(t->expected_err);
304 t->expected_err = NULL;
305 OPENSSL_free(t->func);
307 OPENSSL_free(t->reason);
309 OPENSSL_free(t->out_expected);
310 OPENSSL_free(t->out_received);
311 t->out_expected = NULL;
312 t->out_received = NULL;
313 t->out_expected_len = 0;
314 t->out_received_len = 0;
319 static void print_expected(struct evp_test *t)
321 if (t->out_expected == NULL && t->out_received == NULL)
323 hex_print("Expected:", t->out_expected, t->out_expected_len);
324 hex_print("Got: ", t->out_received, t->out_received_len);
328 static int check_test_error(struct evp_test *t)
333 if (!t->err && !t->expected_err)
335 if (t->err && !t->expected_err) {
336 if (t->aux_err != NULL) {
337 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
338 t->start_line, t->aux_err, t->err);
340 fprintf(stderr, "Test line %d: unexpected error %s\n",
341 t->start_line, t->err);
346 if (!t->err && t->expected_err) {
347 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
348 t->start_line, t->expected_err);
352 if (strcmp(t->err, t->expected_err) != 0) {
353 fprintf(stderr, "Test line %d: expecting %s got %s\n",
354 t->start_line, t->expected_err, t->err);
358 if (t->func == NULL && t->reason == NULL)
361 if (t->func == NULL || t->reason == NULL) {
362 fprintf(stderr, "Test line %d: missing function or reason code\n",
367 err = ERR_peek_error();
369 fprintf(stderr, "Test line %d, expected error \"%s:%s\" not set\n",
370 t->start_line, t->func, t->reason);
374 func = ERR_func_error_string(err);
375 reason = ERR_reason_error_string(err);
377 if (func == NULL && reason == NULL) {
378 fprintf(stderr, "Test line %d: expected error \"%s:%s\", no strings available. Skipping...\n",
379 t->start_line, t->func, t->reason);
383 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
386 fprintf(stderr, "Test line %d: expected error \"%s:%s\", got \"%s:%s\"\n",
387 t->start_line, t->func, t->reason, func, reason);
392 /* Setup a new test, run any existing test */
394 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
396 /* If we already have a test set up run it */
403 if (t->err == NULL && t->meth->run_test(t) != 1) {
404 fprintf(stderr, "%s test error line %d\n",
405 t->meth->name, t->start_line);
408 if (!check_test_error(t)) {
410 ERR_print_errors_fp(stderr);
416 if (t->data != NULL) {
418 OPENSSL_free(t->data);
421 OPENSSL_free(t->expected_err);
422 t->expected_err = NULL;
429 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
431 for (; lst; lst = lst->next) {
432 if (strcmp(lst->name, name) == 0) {
441 static void free_key_list(struct key_list *lst)
443 while (lst != NULL) {
444 struct key_list *ltmp;
445 EVP_PKEY_free(lst->key);
446 OPENSSL_free(lst->name);
453 static int check_unsupported()
455 long err = ERR_peek_error();
456 if (ERR_GET_LIB(err) == ERR_LIB_EVP
457 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
465 static int read_key(struct evp_test *t)
469 t->key = BIO_new(BIO_s_mem());
470 else if (BIO_reset(t->key) <= 0)
472 if (t->key == NULL) {
473 fprintf(stderr, "Error allocating key memory BIO\n");
476 /* Read to PEM end line and place content in memory BIO */
477 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
479 if (BIO_puts(t->key, tmpbuf) <= 0) {
480 fprintf(stderr, "Error writing to key memory BIO\n");
483 if (strncmp(tmpbuf, "-----END", 8) == 0)
486 fprintf(stderr, "Can't find key end\n");
490 static int process_test(struct evp_test *t, char *buf, int verbose)
492 char *keyword = NULL, *value = NULL;
493 int rv = 0, add_key = 0;
494 struct key_list **lst = NULL, *key = NULL;
496 const struct evp_test_method *tmeth = NULL;
499 if (!parse_line(&keyword, &value, buf))
501 if (strcmp(keyword, "PrivateKey") == 0) {
504 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
505 if (pk == NULL && !check_unsupported()) {
506 fprintf(stderr, "Error reading private key %s\n", value);
507 ERR_print_errors_fp(stderr);
513 if (strcmp(keyword, "PublicKey") == 0) {
516 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
517 if (pk == NULL && !check_unsupported()) {
518 fprintf(stderr, "Error reading public key %s\n", value);
519 ERR_print_errors_fp(stderr);
525 /* If we have a key add to list */
527 if (find_key(NULL, value, *lst)) {
528 fprintf(stderr, "Duplicate key %s\n", value);
531 key = OPENSSL_malloc(sizeof(*key));
534 key->name = OPENSSL_strdup(value);
541 /* See if keyword corresponds to a test start */
542 tmeth = evp_find_test(keyword);
544 if (!setup_test(t, tmeth))
546 t->start_line = t->line;
548 if (!tmeth->init(t, value)) {
549 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
553 } else if (t->skip) {
555 } else if (strcmp(keyword, "Result") == 0) {
556 if (t->expected_err) {
557 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
560 t->expected_err = OPENSSL_strdup(value);
561 if (t->expected_err == NULL)
563 } else if (strcmp(keyword, "Function") == 0) {
564 if (t->func != NULL) {
565 fprintf(stderr, "Line %d: multiple function lines\n", t->line);
568 t->func = OPENSSL_strdup(value);
571 } else if (strcmp(keyword, "Reason") == 0) {
572 if (t->reason != NULL) {
573 fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
576 t->reason = OPENSSL_strdup(value);
577 if (t->reason == NULL)
580 /* Must be test specific line: try to parse it */
582 rv = t->meth->parse(t, keyword, value);
585 fprintf(stderr, "line %d: unexpected keyword %s\n",
589 fprintf(stderr, "line %d: error processing keyword %s\n",
597 static int check_var_length_output(struct evp_test *t,
598 const unsigned char *expected,
600 const unsigned char *received,
603 if (expected_len == received_len &&
604 memcmp(expected, received, expected_len) == 0) {
608 /* The result printing code expects a non-NULL buffer. */
609 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
610 t->out_expected_len = expected_len;
611 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
612 t->out_received_len = received_len;
613 if (t->out_expected == NULL || t->out_received == NULL) {
614 fprintf(stderr, "Memory allocation error!\n");
620 static int check_output(struct evp_test *t,
621 const unsigned char *expected,
622 const unsigned char *received,
625 return check_var_length_output(t, expected, len, received, len);
628 int main(int argc, char **argv)
635 fprintf(stderr, "usage: evp_test testfile.txt\n");
639 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
641 memset(&t, 0, sizeof(t));
643 in = BIO_new_file(argv[1], "r");
645 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
650 while (BIO_gets(in, buf, sizeof(buf))) {
652 if (!process_test(&t, buf, 0))
655 /* Run any final test we have */
656 if (!setup_test(&t, NULL))
658 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
659 t.ntests, t.errors, t.nskip);
660 free_key_list(t.public);
661 free_key_list(t.private);
665 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
666 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
674 static void test_free(void *d)
679 /* Message digest tests */
682 /* Digest this test is for */
683 const EVP_MD *digest;
684 /* Input to digest */
685 unsigned char *input;
687 /* Repeat count for input */
689 /* Expected output */
690 unsigned char *output;
694 static int digest_test_init(struct evp_test *t, const char *alg)
696 const EVP_MD *digest;
697 struct digest_data *mdat;
698 digest = EVP_get_digestbyname(alg);
700 /* If alg has an OID assume disabled algorithm */
701 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
707 mdat = OPENSSL_malloc(sizeof(*mdat));
708 mdat->digest = digest;
716 static void digest_test_cleanup(struct evp_test *t)
718 struct digest_data *mdat = t->data;
719 test_free(mdat->input);
720 test_free(mdat->output);
723 static int digest_test_parse(struct evp_test *t,
724 const char *keyword, const char *value)
726 struct digest_data *mdata = t->data;
727 if (strcmp(keyword, "Input") == 0)
728 return test_bin(value, &mdata->input, &mdata->input_len);
729 if (strcmp(keyword, "Output") == 0)
730 return test_bin(value, &mdata->output, &mdata->output_len);
731 if (strcmp(keyword, "Count") == 0) {
732 long nrpt = atoi(value);
735 mdata->nrpt = (size_t)nrpt;
741 static int digest_test_run(struct evp_test *t)
743 struct digest_data *mdata = t->data;
745 const char *err = "INTERNAL_ERROR";
747 unsigned char md[EVP_MAX_MD_SIZE];
749 mctx = EVP_MD_CTX_new();
752 err = "DIGESTINIT_ERROR";
753 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
755 err = "DIGESTUPDATE_ERROR";
756 for (i = 0; i < mdata->nrpt; i++) {
757 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
760 err = "DIGESTFINAL_ERROR";
761 if (!EVP_DigestFinal(mctx, md, &md_len))
763 err = "DIGEST_LENGTH_MISMATCH";
764 if (md_len != mdata->output_len)
766 err = "DIGEST_MISMATCH";
767 if (check_output(t, mdata->output, md, md_len))
771 EVP_MD_CTX_free(mctx);
776 static const struct evp_test_method digest_test_method = {
786 const EVP_CIPHER *cipher;
788 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
794 unsigned char *plaintext;
795 size_t plaintext_len;
796 unsigned char *ciphertext;
797 size_t ciphertext_len;
805 static int cipher_test_init(struct evp_test *t, const char *alg)
807 const EVP_CIPHER *cipher;
808 struct cipher_data *cdat = t->data;
809 cipher = EVP_get_cipherbyname(alg);
811 /* If alg has an OID assume disabled algorithm */
812 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
818 cdat = OPENSSL_malloc(sizeof(*cdat));
819 cdat->cipher = cipher;
823 cdat->ciphertext = NULL;
824 cdat->plaintext = NULL;
828 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
829 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
830 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
831 cdat->aead = EVP_CIPHER_mode(cipher);
832 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
840 static void cipher_test_cleanup(struct evp_test *t)
842 struct cipher_data *cdat = t->data;
843 test_free(cdat->key);
845 test_free(cdat->ciphertext);
846 test_free(cdat->plaintext);
847 test_free(cdat->aad);
848 test_free(cdat->tag);
851 static int cipher_test_parse(struct evp_test *t, const char *keyword,
854 struct cipher_data *cdat = t->data;
855 if (strcmp(keyword, "Key") == 0)
856 return test_bin(value, &cdat->key, &cdat->key_len);
857 if (strcmp(keyword, "IV") == 0)
858 return test_bin(value, &cdat->iv, &cdat->iv_len);
859 if (strcmp(keyword, "Plaintext") == 0)
860 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
861 if (strcmp(keyword, "Ciphertext") == 0)
862 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
864 if (strcmp(keyword, "AAD") == 0)
865 return test_bin(value, &cdat->aad, &cdat->aad_len);
866 if (strcmp(keyword, "Tag") == 0)
867 return test_bin(value, &cdat->tag, &cdat->tag_len);
870 if (strcmp(keyword, "Operation") == 0) {
871 if (strcmp(value, "ENCRYPT") == 0)
873 else if (strcmp(value, "DECRYPT") == 0)
882 static int cipher_test_enc(struct evp_test *t, int enc,
883 size_t out_misalign, size_t inp_misalign, int frag)
885 struct cipher_data *cdat = t->data;
886 unsigned char *in, *out, *tmp = NULL;
887 size_t in_len, out_len, donelen = 0;
888 int tmplen, chunklen, tmpflen;
889 EVP_CIPHER_CTX *ctx = NULL;
891 err = "INTERNAL_ERROR";
892 ctx = EVP_CIPHER_CTX_new();
895 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
897 in = cdat->plaintext;
898 in_len = cdat->plaintext_len;
899 out = cdat->ciphertext;
900 out_len = cdat->ciphertext_len;
902 in = cdat->ciphertext;
903 in_len = cdat->ciphertext_len;
904 out = cdat->plaintext;
905 out_len = cdat->plaintext_len;
907 if (inp_misalign == (size_t)-1) {
909 * Exercise in-place encryption
911 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
914 in = memcpy(tmp + out_misalign, in, in_len);
916 inp_misalign += 16 - ((out_misalign + in_len) & 15);
918 * 'tmp' will store both output and copy of input. We make the copy
919 * of input to specifically aligned part of 'tmp'. So we just
920 * figured out how much padding would ensure the required alignment,
921 * now we allocate extended buffer and finally copy the input just
922 * past inp_misalign in expression below. Output will be written
923 * past out_misalign...
925 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
926 inp_misalign + in_len);
929 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
930 inp_misalign, in, in_len);
932 err = "CIPHERINIT_ERROR";
933 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
935 err = "INVALID_IV_LENGTH";
938 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
941 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
947 * If encrypting or OCB just set tag length initially, otherwise
948 * set tag length and value.
950 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
951 err = "TAG_LENGTH_SET_ERROR";
954 err = "TAG_SET_ERROR";
957 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
958 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
964 err = "INVALID_KEY_LENGTH";
965 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
967 err = "KEY_SET_ERROR";
968 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
971 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
972 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
973 cdat->tag_len, cdat->tag)) {
974 err = "TAG_SET_ERROR";
979 if (cdat->aead == EVP_CIPH_CCM_MODE) {
980 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
981 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
986 err = "AAD_SET_ERROR";
988 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
993 * Supply the AAD in chunks less than the block size where possible
995 if (cdat->aad_len > 0) {
996 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1000 if (cdat->aad_len > 2) {
1001 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1004 donelen += cdat->aad_len - 2;
1006 if (cdat->aad_len > 1
1007 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1008 cdat->aad + donelen, 1))
1012 EVP_CIPHER_CTX_set_padding(ctx, 0);
1013 err = "CIPHERUPDATE_ERROR";
1017 /* We supply the data all in one go */
1018 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1021 /* Supply the data in chunks less than the block size where possible */
1023 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1029 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1030 in + donelen, in_len - 2))
1033 donelen += in_len - 2;
1036 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1042 if (cdat->aead == EVP_CIPH_CCM_MODE)
1045 err = "CIPHERFINAL_ERROR";
1046 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
1049 err = "LENGTH_MISMATCH";
1050 if (out_len != (size_t)(tmplen + tmpflen))
1052 err = "VALUE_MISMATCH";
1053 if (check_output(t, out, tmp + out_misalign, out_len))
1055 if (enc && cdat->aead) {
1056 unsigned char rtag[16];
1057 if (cdat->tag_len > sizeof(rtag)) {
1058 err = "TAG_LENGTH_INTERNAL_ERROR";
1061 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1062 cdat->tag_len, rtag)) {
1063 err = "TAG_RETRIEVE_ERROR";
1066 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
1067 err = "TAG_VALUE_MISMATCH";
1074 EVP_CIPHER_CTX_free(ctx);
1079 static int cipher_test_run(struct evp_test *t)
1081 struct cipher_data *cdat = t->data;
1083 size_t out_misalign, inp_misalign;
1089 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1090 /* IV is optional and usually omitted in wrap mode */
1091 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1096 if (cdat->aead && !cdat->tag) {
1100 for (out_misalign = 0; out_misalign <= 1;) {
1101 static char aux_err[64];
1102 t->aux_err = aux_err;
1103 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1104 if (inp_misalign == (size_t)-1) {
1105 /* kludge: inp_misalign == -1 means "exercise in-place" */
1106 BIO_snprintf(aux_err, sizeof(aux_err),
1107 "%s in-place, %sfragmented",
1108 out_misalign ? "misaligned" : "aligned",
1109 frag ? "" : "not ");
1111 BIO_snprintf(aux_err, sizeof(aux_err),
1112 "%s output and %s input, %sfragmented",
1113 out_misalign ? "misaligned" : "aligned",
1114 inp_misalign ? "misaligned" : "aligned",
1115 frag ? "" : "not ");
1118 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1119 /* Not fatal errors: return */
1126 if (cdat->enc != 1) {
1127 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1128 /* Not fatal errors: return */
1137 if (out_misalign == 1 && frag == 0) {
1139 * XTS, CCM and Wrap modes have special requirements about input
1140 * lengths so we don't fragment for those
1142 if (cdat->aead == EVP_CIPH_CCM_MODE
1143 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1144 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1157 static const struct evp_test_method cipher_test_method = {
1160 cipher_test_cleanup,
1168 /* Algorithm string for this MAC */
1174 unsigned char *input;
1176 /* Expected output */
1177 unsigned char *output;
1181 static int mac_test_init(struct evp_test *t, const char *alg)
1184 struct mac_data *mdat;
1185 if (strcmp(alg, "HMAC") == 0) {
1186 type = EVP_PKEY_HMAC;
1187 } else if (strcmp(alg, "CMAC") == 0) {
1188 #ifndef OPENSSL_NO_CMAC
1189 type = EVP_PKEY_CMAC;
1194 } else if (strcmp(alg, "Poly1305") == 0) {
1195 #ifndef OPENSSL_NO_POLY1305
1196 type = EVP_PKEY_POLY1305;
1204 mdat = OPENSSL_malloc(sizeof(*mdat));
1209 mdat->output = NULL;
1214 static void mac_test_cleanup(struct evp_test *t)
1216 struct mac_data *mdat = t->data;
1217 test_free(mdat->alg);
1218 test_free(mdat->key);
1219 test_free(mdat->input);
1220 test_free(mdat->output);
1223 static int mac_test_parse(struct evp_test *t,
1224 const char *keyword, const char *value)
1226 struct mac_data *mdata = t->data;
1227 if (strcmp(keyword, "Key") == 0)
1228 return test_bin(value, &mdata->key, &mdata->key_len);
1229 if (strcmp(keyword, "Algorithm") == 0) {
1230 mdata->alg = OPENSSL_strdup(value);
1235 if (strcmp(keyword, "Input") == 0)
1236 return test_bin(value, &mdata->input, &mdata->input_len);
1237 if (strcmp(keyword, "Output") == 0)
1238 return test_bin(value, &mdata->output, &mdata->output_len);
1242 static int mac_test_run(struct evp_test *t)
1244 struct mac_data *mdata = t->data;
1245 const char *err = "INTERNAL_ERROR";
1246 EVP_MD_CTX *mctx = NULL;
1247 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1248 EVP_PKEY *key = NULL;
1249 const EVP_MD *md = NULL;
1250 unsigned char *mac = NULL;
1253 #ifdef OPENSSL_NO_DES
1254 if (strstr(mdata->alg, "DES") != NULL) {
1261 err = "MAC_PKEY_CTX_ERROR";
1262 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1266 err = "MAC_KEYGEN_INIT_ERROR";
1267 if (EVP_PKEY_keygen_init(genctx) <= 0)
1269 if (mdata->type == EVP_PKEY_CMAC) {
1270 err = "MAC_ALGORITHM_SET_ERROR";
1271 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1275 err = "MAC_KEY_SET_ERROR";
1276 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1279 err = "MAC_KEY_GENERATE_ERROR";
1280 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1282 if (mdata->type == EVP_PKEY_HMAC) {
1283 err = "MAC_ALGORITHM_SET_ERROR";
1284 md = EVP_get_digestbyname(mdata->alg);
1288 mctx = EVP_MD_CTX_new();
1291 err = "DIGESTSIGNINIT_ERROR";
1292 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1295 err = "DIGESTSIGNUPDATE_ERROR";
1296 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1298 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1299 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1301 mac = OPENSSL_malloc(mac_len);
1303 fprintf(stderr, "Error allocating mac buffer!\n");
1306 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1308 err = "MAC_LENGTH_MISMATCH";
1309 if (mac_len != mdata->output_len)
1311 err = "MAC_MISMATCH";
1312 if (check_output(t, mdata->output, mac, mac_len))
1316 EVP_MD_CTX_free(mctx);
1318 EVP_PKEY_CTX_free(genctx);
1324 static const struct evp_test_method mac_test_method = {
1333 * Public key operations. These are all very similar and can share
1334 * a lot of common code.
1338 /* Context for this operation */
1340 /* Key operation to perform */
1341 int (*keyop) (EVP_PKEY_CTX *ctx,
1342 unsigned char *sig, size_t *siglen,
1343 const unsigned char *tbs, size_t tbslen);
1345 unsigned char *input;
1347 /* Expected output */
1348 unsigned char *output;
1353 * Perform public key operation setup: lookup key, allocated ctx and call
1354 * the appropriate initialisation function
1356 static int pkey_test_init(struct evp_test *t, const char *name,
1358 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1359 int (*keyop) (EVP_PKEY_CTX *ctx,
1360 unsigned char *sig, size_t *siglen,
1361 const unsigned char *tbs,
1365 struct pkey_data *kdata;
1366 EVP_PKEY *pkey = NULL;
1369 rv = find_key(&pkey, name, t->public);
1371 rv = find_key(&pkey, name, t->private);
1372 if (!rv || pkey == NULL) {
1377 kdata = OPENSSL_malloc(sizeof(*kdata));
1379 EVP_PKEY_free(pkey);
1383 kdata->input = NULL;
1384 kdata->output = NULL;
1385 kdata->keyop = keyop;
1387 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1390 if (keyopinit(kdata->ctx) <= 0)
1391 t->err = "KEYOP_INIT_ERROR";
1395 static void pkey_test_cleanup(struct evp_test *t)
1397 struct pkey_data *kdata = t->data;
1399 OPENSSL_free(kdata->input);
1400 OPENSSL_free(kdata->output);
1401 EVP_PKEY_CTX_free(kdata->ctx);
1404 static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1410 tmpval = OPENSSL_strdup(value);
1413 p = strchr(tmpval, ':');
1416 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1418 t->err = "PKEY_CTRL_INVALID";
1420 } else if (p != NULL && rv <= 0) {
1421 /* If p has an OID and lookup fails assume disabled algorithm */
1422 int nid = OBJ_sn2nid(p);
1423 if (nid == NID_undef)
1424 nid = OBJ_ln2nid(p);
1425 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1426 EVP_get_cipherbynid(nid) == NULL) {
1430 t->err = "PKEY_CTRL_ERROR";
1434 OPENSSL_free(tmpval);
1438 static int pkey_test_parse(struct evp_test *t,
1439 const char *keyword, const char *value)
1441 struct pkey_data *kdata = t->data;
1442 if (strcmp(keyword, "Input") == 0)
1443 return test_bin(value, &kdata->input, &kdata->input_len);
1444 if (strcmp(keyword, "Output") == 0)
1445 return test_bin(value, &kdata->output, &kdata->output_len);
1446 if (strcmp(keyword, "Ctrl") == 0)
1447 return pkey_test_ctrl(t, kdata->ctx, value);
1451 static int pkey_test_run(struct evp_test *t)
1453 struct pkey_data *kdata = t->data;
1454 unsigned char *out = NULL;
1456 const char *err = "KEYOP_LENGTH_ERROR";
1457 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1458 kdata->input_len) <= 0)
1460 out = OPENSSL_malloc(out_len);
1462 fprintf(stderr, "Error allocating output buffer!\n");
1465 err = "KEYOP_ERROR";
1467 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1469 err = "KEYOP_LENGTH_MISMATCH";
1470 if (out_len != kdata->output_len)
1472 err = "KEYOP_MISMATCH";
1473 if (check_output(t, kdata->output, out, out_len))
1482 static int sign_test_init(struct evp_test *t, const char *name)
1484 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1487 static const struct evp_test_method psign_test_method = {
1495 static int verify_recover_test_init(struct evp_test *t, const char *name)
1497 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1498 EVP_PKEY_verify_recover);
1501 static const struct evp_test_method pverify_recover_test_method = {
1503 verify_recover_test_init,
1509 static int decrypt_test_init(struct evp_test *t, const char *name)
1511 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1515 static const struct evp_test_method pdecrypt_test_method = {
1523 static int verify_test_init(struct evp_test *t, const char *name)
1525 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1528 static int verify_test_run(struct evp_test *t)
1530 struct pkey_data *kdata = t->data;
1531 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1532 kdata->input, kdata->input_len) <= 0)
1533 t->err = "VERIFY_ERROR";
1537 static const struct evp_test_method pverify_test_method = {
1546 static int pderive_test_init(struct evp_test *t, const char *name)
1548 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1551 static int pderive_test_parse(struct evp_test *t,
1552 const char *keyword, const char *value)
1554 struct pkey_data *kdata = t->data;
1556 if (strcmp(keyword, "PeerKey") == 0) {
1558 if (find_key(&peer, value, t->public) == 0)
1560 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1564 if (strcmp(keyword, "SharedSecret") == 0)
1565 return test_bin(value, &kdata->output, &kdata->output_len);
1566 if (strcmp(keyword, "Ctrl") == 0)
1567 return pkey_test_ctrl(t, kdata->ctx, value);
1571 static int pderive_test_run(struct evp_test *t)
1573 struct pkey_data *kdata = t->data;
1574 unsigned char *out = NULL;
1576 const char *err = "INTERNAL_ERROR";
1578 out_len = kdata->output_len;
1579 out = OPENSSL_malloc(out_len);
1581 fprintf(stderr, "Error allocating output buffer!\n");
1584 err = "DERIVE_ERROR";
1585 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1587 err = "SHARED_SECRET_LENGTH_MISMATCH";
1588 if (out_len != kdata->output_len)
1590 err = "SHARED_SECRET_MISMATCH";
1591 if (check_output(t, kdata->output, out, out_len))
1600 static const struct evp_test_method pderive_test_method = {
1610 #define PBE_TYPE_SCRYPT 1
1611 #define PBE_TYPE_PBKDF2 2
1612 #define PBE_TYPE_PKCS12 3
1618 /* scrypt parameters */
1619 uint64_t N, r, p, maxmem;
1621 /* PKCS#12 parameters */
1626 unsigned char *pass;
1630 unsigned char *salt;
1633 /* Expected output */
1638 #ifndef OPENSSL_NO_SCRYPT
1639 static int scrypt_test_parse(struct evp_test *t,
1640 const char *keyword, const char *value)
1642 struct pbe_data *pdata = t->data;
1644 if (strcmp(keyword, "N") == 0)
1645 return test_uint64(value, &pdata->N);
1646 if (strcmp(keyword, "p") == 0)
1647 return test_uint64(value, &pdata->p);
1648 if (strcmp(keyword, "r") == 0)
1649 return test_uint64(value, &pdata->r);
1650 if (strcmp(keyword, "maxmem") == 0)
1651 return test_uint64(value, &pdata->maxmem);
1656 static int pbkdf2_test_parse(struct evp_test *t,
1657 const char *keyword, const char *value)
1659 struct pbe_data *pdata = t->data;
1661 if (strcmp(keyword, "iter") == 0) {
1662 pdata->iter = atoi(value);
1663 if (pdata->iter <= 0)
1667 if (strcmp(keyword, "MD") == 0) {
1668 pdata->md = EVP_get_digestbyname(value);
1669 if (pdata->md == NULL)
1676 static int pkcs12_test_parse(struct evp_test *t,
1677 const char *keyword, const char *value)
1679 struct pbe_data *pdata = t->data;
1681 if (strcmp(keyword, "id") == 0) {
1682 pdata->id = atoi(value);
1687 return pbkdf2_test_parse(t, keyword, value);
1690 static int pbe_test_init(struct evp_test *t, const char *alg)
1692 struct pbe_data *pdat;
1695 if (strcmp(alg, "scrypt") == 0) {
1696 #ifndef OPENSSL_NO_SCRYPT
1697 pbe_type = PBE_TYPE_SCRYPT;
1702 } else if (strcmp(alg, "pbkdf2") == 0) {
1703 pbe_type = PBE_TYPE_PBKDF2;
1704 } else if (strcmp(alg, "pkcs12") == 0) {
1705 pbe_type = PBE_TYPE_PKCS12;
1707 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1709 pdat = OPENSSL_malloc(sizeof(*pdat));
1710 pdat->pbe_type = pbe_type;
1724 static void pbe_test_cleanup(struct evp_test *t)
1726 struct pbe_data *pdat = t->data;
1727 test_free(pdat->pass);
1728 test_free(pdat->salt);
1729 test_free(pdat->key);
1732 static int pbe_test_parse(struct evp_test *t,
1733 const char *keyword, const char *value)
1735 struct pbe_data *pdata = t->data;
1737 if (strcmp(keyword, "Password") == 0)
1738 return test_bin(value, &pdata->pass, &pdata->pass_len);
1739 if (strcmp(keyword, "Salt") == 0)
1740 return test_bin(value, &pdata->salt, &pdata->salt_len);
1741 if (strcmp(keyword, "Key") == 0)
1742 return test_bin(value, &pdata->key, &pdata->key_len);
1743 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1744 return pbkdf2_test_parse(t, keyword, value);
1745 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1746 return pkcs12_test_parse(t, keyword, value);
1747 #ifndef OPENSSL_NO_SCRYPT
1748 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1749 return scrypt_test_parse(t, keyword, value);
1754 static int pbe_test_run(struct evp_test *t)
1756 struct pbe_data *pdata = t->data;
1757 const char *err = "INTERNAL_ERROR";
1760 key = OPENSSL_malloc(pdata->key_len);
1763 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1764 err = "PBKDF2_ERROR";
1765 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1766 pdata->salt, pdata->salt_len,
1767 pdata->iter, pdata->md,
1768 pdata->key_len, key) == 0)
1770 #ifndef OPENSSL_NO_SCRYPT
1771 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1772 err = "SCRYPT_ERROR";
1773 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1774 pdata->salt, pdata->salt_len,
1775 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1776 key, pdata->key_len) == 0)
1779 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1780 err = "PKCS12_ERROR";
1781 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1782 pdata->salt, pdata->salt_len,
1783 pdata->id, pdata->iter, pdata->key_len,
1784 key, pdata->md) == 0)
1787 err = "KEY_MISMATCH";
1788 if (check_output(t, pdata->key, key, pdata->key_len))
1797 static const struct evp_test_method pbe_test_method = {
1808 BASE64_CANONICAL_ENCODING = 0,
1809 BASE64_VALID_ENCODING = 1,
1810 BASE64_INVALID_ENCODING = 2
1811 } base64_encoding_type;
1813 struct encode_data {
1814 /* Input to encoding */
1815 unsigned char *input;
1817 /* Expected output */
1818 unsigned char *output;
1820 base64_encoding_type encoding;
1823 static int encode_test_init(struct evp_test *t, const char *encoding)
1825 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1827 if (strcmp(encoding, "canonical") == 0) {
1828 edata->encoding = BASE64_CANONICAL_ENCODING;
1829 } else if (strcmp(encoding, "valid") == 0) {
1830 edata->encoding = BASE64_VALID_ENCODING;
1831 } else if (strcmp(encoding, "invalid") == 0) {
1832 edata->encoding = BASE64_INVALID_ENCODING;
1833 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1834 if (t->expected_err == NULL)
1837 fprintf(stderr, "Bad encoding: %s. Should be one of "
1838 "{canonical, valid, invalid}\n", encoding);
1845 static void encode_test_cleanup(struct evp_test *t)
1847 struct encode_data *edata = t->data;
1848 test_free(edata->input);
1849 test_free(edata->output);
1850 memset(edata, 0, sizeof(*edata));
1853 static int encode_test_parse(struct evp_test *t,
1854 const char *keyword, const char *value)
1856 struct encode_data *edata = t->data;
1857 if (strcmp(keyword, "Input") == 0)
1858 return test_bin(value, &edata->input, &edata->input_len);
1859 if (strcmp(keyword, "Output") == 0)
1860 return test_bin(value, &edata->output, &edata->output_len);
1864 static int encode_test_run(struct evp_test *t)
1866 struct encode_data *edata = t->data;
1867 unsigned char *encode_out = NULL, *decode_out = NULL;
1868 int output_len, chunk_len;
1869 const char *err = "INTERNAL_ERROR";
1870 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1872 if (decode_ctx == NULL)
1875 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1876 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1877 if (encode_ctx == NULL)
1879 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1880 if (encode_out == NULL)
1883 EVP_EncodeInit(encode_ctx);
1884 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1885 edata->input, edata->input_len);
1886 output_len = chunk_len;
1888 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1889 output_len += chunk_len;
1891 EVP_ENCODE_CTX_free(encode_ctx);
1893 if (check_var_length_output(t, edata->output, edata->output_len,
1894 encode_out, output_len)) {
1895 err = "BAD_ENCODING";
1900 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1901 if (decode_out == NULL)
1904 EVP_DecodeInit(decode_ctx);
1905 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1906 edata->output_len) < 0) {
1907 err = "DECODE_ERROR";
1910 output_len = chunk_len;
1912 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1913 err = "DECODE_ERROR";
1916 output_len += chunk_len;
1918 if (edata->encoding != BASE64_INVALID_ENCODING &&
1919 check_var_length_output(t, edata->input, edata->input_len,
1920 decode_out, output_len)) {
1921 err = "BAD_DECODING";
1928 OPENSSL_free(encode_out);
1929 OPENSSL_free(decode_out);
1930 EVP_ENCODE_CTX_free(decode_ctx);
1934 static const struct evp_test_method encode_test_method = {
1937 encode_test_cleanup,
1942 /* KDF operations */
1945 /* Context for this operation */
1947 /* Expected output */
1948 unsigned char *output;
1953 * Perform public key operation setup: lookup key, allocated ctx and call
1954 * the appropriate initialisation function
1956 static int kdf_test_init(struct evp_test *t, const char *name)
1958 struct kdf_data *kdata;
1960 kdata = OPENSSL_malloc(sizeof(*kdata));
1964 kdata->output = NULL;
1966 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1967 if (kdata->ctx == NULL)
1969 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1974 static void kdf_test_cleanup(struct evp_test *t)
1976 struct kdf_data *kdata = t->data;
1977 OPENSSL_free(kdata->output);
1978 EVP_PKEY_CTX_free(kdata->ctx);
1981 static int kdf_test_parse(struct evp_test *t,
1982 const char *keyword, const char *value)
1984 struct kdf_data *kdata = t->data;
1985 if (strcmp(keyword, "Output") == 0)
1986 return test_bin(value, &kdata->output, &kdata->output_len);
1987 if (strncmp(keyword, "Ctrl", 4) == 0)
1988 return pkey_test_ctrl(t, kdata->ctx, value);
1992 static int kdf_test_run(struct evp_test *t)
1994 struct kdf_data *kdata = t->data;
1995 unsigned char *out = NULL;
1996 size_t out_len = kdata->output_len;
1997 const char *err = "INTERNAL_ERROR";
1998 out = OPENSSL_malloc(out_len);
2000 fprintf(stderr, "Error allocating output buffer!\n");
2003 err = "KDF_DERIVE_ERROR";
2004 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
2006 err = "KDF_LENGTH_MISMATCH";
2007 if (out_len != kdata->output_len)
2009 err = "KDF_MISMATCH";
2010 if (check_output(t, kdata->output, out, out_len))
2019 static const struct evp_test_method kdf_test_method = {