2 * Copyright 2015-2017 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, *beginning;
31 *pval = (char *)(beginning = p);
33 p = p + strlen(*pval) - 1;
35 /* Remove trailing space */
36 while (p >= beginning && isspace(*p))
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)
131 /* Check for empty value */
134 * Don't return NULL for zero length buffer.
135 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
136 * a non-NULL key buffer even if the key length is 0, in order to detect
139 *buf = OPENSSL_malloc(1);
147 /* Check for NULL literal */
148 if (strcmp(value, "NULL") == 0) {
154 /* Check for string literal */
155 if (value[0] == '"') {
158 vlen = strlen(value);
159 if (value[vlen - 1] != '"')
162 *buf = unescape(value, vlen, buflen);
168 /* Otherwise assume as hex literal and convert it to binary buffer */
169 *buf = OPENSSL_hexstr2buf(value, &len);
171 fprintf(stderr, "Value=%s\n", value);
172 ERR_print_errors_fp(stderr);
175 /* Size of input buffer means we'll never overflow */
179 #ifndef OPENSSL_NO_SCRYPT
180 /* Currently only used by scrypt tests */
181 /* Parse unsigned decimal 64 bit integer value */
182 static int test_uint64(const char *value, uint64_t *pr)
184 const char *p = value;
186 fprintf(stderr, "Invalid empty integer value\n");
191 if (*pr > UINT64_MAX/10) {
192 fprintf(stderr, "Integer string overflow value=%s\n", value);
196 if (*p < '0' || *p > '9') {
197 fprintf(stderr, "Invalid integer string value=%s\n", value);
207 /* Structure holding test information */
209 /* file being read */
211 /* temp memory BIO for reading in keys */
213 /* List of public and private keys */
214 struct key_list *private;
215 struct key_list *public;
216 /* method for this test */
217 const struct evp_test_method *meth;
218 /* current line being processed */
220 /* start line of current test */
221 unsigned int start_line;
222 /* Error string for test */
223 const char *err, *aux_err;
224 /* Expected error value of test */
226 /* Expected error function string */
228 /* Expected error reason string */
230 /* Number of tests */
234 /* Number of tests skipped */
236 /* If output mismatch expected and got value */
237 unsigned char *out_received;
238 size_t out_received_len;
239 unsigned char *out_expected;
240 size_t out_expected_len;
241 /* test specific data */
243 /* Current test should be skipped */
250 struct key_list *next;
253 /* Test method structure */
254 struct evp_test_method {
255 /* Name of test as it appears in file */
257 /* Initialise test for "alg" */
258 int (*init) (struct evp_test * t, const char *alg);
259 /* Clean up method */
260 void (*cleanup) (struct evp_test * t);
261 /* Test specific name value pair processing */
262 int (*parse) (struct evp_test * t, const char *name, const char *value);
263 /* Run the test itself */
264 int (*run_test) (struct evp_test * t);
267 static const struct evp_test_method digest_test_method, cipher_test_method;
268 static const struct evp_test_method mac_test_method;
269 static const struct evp_test_method psign_test_method, pverify_test_method;
270 static const struct evp_test_method pdecrypt_test_method;
271 static const struct evp_test_method pverify_recover_test_method;
272 static const struct evp_test_method pderive_test_method;
273 static const struct evp_test_method pbe_test_method;
274 static const struct evp_test_method encode_test_method;
275 static const struct evp_test_method kdf_test_method;
277 static const struct evp_test_method *evp_test_list[] = {
282 &pverify_test_method,
283 &pdecrypt_test_method,
284 &pverify_recover_test_method,
285 &pderive_test_method,
292 static const struct evp_test_method *evp_find_test(const char *name)
294 const struct evp_test_method **tt;
296 for (tt = evp_test_list; *tt; tt++) {
297 if (strcmp(name, (*tt)->name) == 0)
303 static void hex_print(const char *name, const unsigned char *buf, size_t len)
306 fprintf(stderr, "%s ", name);
307 for (i = 0; i < len; i++)
308 fprintf(stderr, "%02X", buf[i]);
312 static void free_expected(struct evp_test *t)
314 OPENSSL_free(t->expected_err);
315 t->expected_err = NULL;
316 OPENSSL_free(t->func);
318 OPENSSL_free(t->reason);
320 OPENSSL_free(t->out_expected);
321 OPENSSL_free(t->out_received);
322 t->out_expected = NULL;
323 t->out_received = NULL;
324 t->out_expected_len = 0;
325 t->out_received_len = 0;
330 static void print_expected(struct evp_test *t)
332 if (t->out_expected == NULL && t->out_received == NULL)
334 hex_print("Expected:", t->out_expected, t->out_expected_len);
335 hex_print("Got: ", t->out_received, t->out_received_len);
339 static int check_test_error(struct evp_test *t)
344 if (!t->err && !t->expected_err)
346 if (t->err && !t->expected_err) {
347 if (t->aux_err != NULL) {
348 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
349 t->start_line, t->aux_err, t->err);
351 fprintf(stderr, "Test line %d: unexpected error %s\n",
352 t->start_line, t->err);
357 if (!t->err && t->expected_err) {
358 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
359 t->start_line, t->expected_err);
363 if (strcmp(t->err, t->expected_err) != 0) {
364 fprintf(stderr, "Test line %d: expecting %s got %s\n",
365 t->start_line, t->expected_err, t->err);
369 if (t->func == NULL && t->reason == NULL)
372 if (t->func == NULL || t->reason == NULL) {
373 fprintf(stderr, "Test line %d: missing function or reason code\n",
378 err = ERR_peek_error();
380 fprintf(stderr, "Test line %d, expected error \"%s:%s\" not set\n",
381 t->start_line, t->func, t->reason);
385 func = ERR_func_error_string(err);
386 reason = ERR_reason_error_string(err);
388 if (func == NULL && reason == NULL) {
389 fprintf(stderr, "Test line %d: expected error \"%s:%s\", no strings available. Skipping...\n",
390 t->start_line, t->func, t->reason);
394 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
397 fprintf(stderr, "Test line %d: expected error \"%s:%s\", got \"%s:%s\"\n",
398 t->start_line, t->func, t->reason, func, reason);
403 /* Setup a new test, run any existing test */
405 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
407 /* If we already have a test set up run it */
414 if (t->err == NULL && t->meth->run_test(t) != 1) {
415 fprintf(stderr, "%s test error line %d\n",
416 t->meth->name, t->start_line);
419 if (!check_test_error(t)) {
421 ERR_print_errors_fp(stderr);
427 if (t->data != NULL) {
429 OPENSSL_free(t->data);
432 OPENSSL_free(t->expected_err);
433 t->expected_err = NULL;
440 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
442 for (; lst; lst = lst->next) {
443 if (strcmp(lst->name, name) == 0) {
452 static void free_key_list(struct key_list *lst)
454 while (lst != NULL) {
455 struct key_list *ltmp;
456 EVP_PKEY_free(lst->key);
457 OPENSSL_free(lst->name);
464 static int check_unsupported()
466 long err = ERR_peek_error();
467 if (ERR_GET_LIB(err) == ERR_LIB_EVP
468 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
472 #ifndef OPENSSL_NO_EC
474 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
475 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
478 if (ERR_GET_LIB(err) == ERR_LIB_EC
479 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
483 #endif /* OPENSSL_NO_EC */
488 static int read_key(struct evp_test *t)
492 t->key = BIO_new(BIO_s_mem());
493 else if (BIO_reset(t->key) <= 0)
495 if (t->key == NULL) {
496 fprintf(stderr, "Error allocating key memory BIO\n");
499 /* Read to PEM end line and place content in memory BIO */
500 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
502 if (BIO_puts(t->key, tmpbuf) <= 0) {
503 fprintf(stderr, "Error writing to key memory BIO\n");
506 if (strncmp(tmpbuf, "-----END", 8) == 0)
509 fprintf(stderr, "Can't find key end\n");
513 static int process_test(struct evp_test *t, char *buf, int verbose)
515 char *keyword = NULL, *value = NULL;
516 int rv = 0, add_key = 0;
517 struct key_list **lst = NULL, *key = NULL;
519 const struct evp_test_method *tmeth = NULL;
522 if (!parse_line(&keyword, &value, buf))
524 if (strcmp(keyword, "PrivateKey") == 0) {
527 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
528 if (pk == NULL && !check_unsupported()) {
529 fprintf(stderr, "Error reading private key %s\n", value);
530 ERR_print_errors_fp(stderr);
536 if (strcmp(keyword, "PublicKey") == 0) {
539 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
540 if (pk == NULL && !check_unsupported()) {
541 fprintf(stderr, "Error reading public key %s\n", value);
542 ERR_print_errors_fp(stderr);
548 /* If we have a key add to list */
550 if (find_key(NULL, value, *lst)) {
551 fprintf(stderr, "Duplicate key %s\n", value);
554 key = OPENSSL_malloc(sizeof(*key));
557 key->name = OPENSSL_strdup(value);
564 /* See if keyword corresponds to a test start */
565 tmeth = evp_find_test(keyword);
567 if (!setup_test(t, tmeth))
569 t->start_line = t->line;
571 if (!tmeth->init(t, value)) {
572 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
576 } else if (t->skip) {
578 } else if (strcmp(keyword, "Result") == 0) {
579 if (t->expected_err) {
580 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
583 t->expected_err = OPENSSL_strdup(value);
584 if (t->expected_err == NULL)
586 } else if (strcmp(keyword, "Function") == 0) {
587 if (t->func != NULL) {
588 fprintf(stderr, "Line %d: multiple function lines\n", t->line);
591 t->func = OPENSSL_strdup(value);
594 } else if (strcmp(keyword, "Reason") == 0) {
595 if (t->reason != NULL) {
596 fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
599 t->reason = OPENSSL_strdup(value);
600 if (t->reason == NULL)
603 /* Must be test specific line: try to parse it */
605 rv = t->meth->parse(t, keyword, value);
608 fprintf(stderr, "line %d: unexpected keyword %s\n",
612 fprintf(stderr, "line %d: error processing keyword %s\n",
620 static int check_var_length_output(struct evp_test *t,
621 const unsigned char *expected,
623 const unsigned char *received,
626 if (expected_len == received_len &&
627 memcmp(expected, received, expected_len) == 0) {
631 /* The result printing code expects a non-NULL buffer. */
632 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
633 t->out_expected_len = expected_len;
634 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
635 t->out_received_len = received_len;
636 if (t->out_expected == NULL || t->out_received == NULL) {
637 fprintf(stderr, "Memory allocation error!\n");
643 static int check_output(struct evp_test *t,
644 const unsigned char *expected,
645 const unsigned char *received,
648 return check_var_length_output(t, expected, len, received, len);
651 int main(int argc, char **argv)
658 fprintf(stderr, "usage: evp_test testfile.txt\n");
662 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
664 memset(&t, 0, sizeof(t));
666 in = BIO_new_file(argv[1], "rb");
668 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
673 while (BIO_gets(in, buf, sizeof(buf))) {
675 if (!process_test(&t, buf, 0))
678 /* Run any final test we have */
679 if (!setup_test(&t, NULL))
681 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
682 t.ntests, t.errors, t.nskip);
683 free_key_list(t.public);
684 free_key_list(t.private);
688 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
689 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
697 static void test_free(void *d)
702 /* Message digest tests */
705 /* Digest this test is for */
706 const EVP_MD *digest;
707 /* Input to digest */
708 unsigned char *input;
710 /* Repeat count for input */
712 /* Expected output */
713 unsigned char *output;
717 static int digest_test_init(struct evp_test *t, const char *alg)
719 const EVP_MD *digest;
720 struct digest_data *mdat;
721 digest = EVP_get_digestbyname(alg);
723 /* If alg has an OID assume disabled algorithm */
724 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
730 mdat = OPENSSL_malloc(sizeof(*mdat));
731 mdat->digest = digest;
739 static void digest_test_cleanup(struct evp_test *t)
741 struct digest_data *mdat = t->data;
742 test_free(mdat->input);
743 test_free(mdat->output);
746 static int digest_test_parse(struct evp_test *t,
747 const char *keyword, const char *value)
749 struct digest_data *mdata = t->data;
750 if (strcmp(keyword, "Input") == 0)
751 return test_bin(value, &mdata->input, &mdata->input_len);
752 if (strcmp(keyword, "Output") == 0)
753 return test_bin(value, &mdata->output, &mdata->output_len);
754 if (strcmp(keyword, "Count") == 0) {
755 long nrpt = atoi(value);
758 mdata->nrpt = (size_t)nrpt;
764 static int digest_test_run(struct evp_test *t)
766 struct digest_data *mdata = t->data;
768 const char *err = "INTERNAL_ERROR";
770 unsigned char md[EVP_MAX_MD_SIZE];
772 mctx = EVP_MD_CTX_new();
775 err = "DIGESTINIT_ERROR";
776 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
778 err = "DIGESTUPDATE_ERROR";
779 for (i = 0; i < mdata->nrpt; i++) {
780 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
783 err = "DIGESTFINAL_ERROR";
784 if (!EVP_DigestFinal(mctx, md, &md_len))
786 err = "DIGEST_LENGTH_MISMATCH";
787 if (md_len != mdata->output_len)
789 err = "DIGEST_MISMATCH";
790 if (check_output(t, mdata->output, md, md_len))
794 EVP_MD_CTX_free(mctx);
799 static const struct evp_test_method digest_test_method = {
809 const EVP_CIPHER *cipher;
811 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
817 unsigned char *plaintext;
818 size_t plaintext_len;
819 unsigned char *ciphertext;
820 size_t ciphertext_len;
828 static int cipher_test_init(struct evp_test *t, const char *alg)
830 const EVP_CIPHER *cipher;
831 struct cipher_data *cdat = t->data;
832 cipher = EVP_get_cipherbyname(alg);
834 /* If alg has an OID assume disabled algorithm */
835 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
841 cdat = OPENSSL_malloc(sizeof(*cdat));
842 cdat->cipher = cipher;
846 cdat->ciphertext = NULL;
847 cdat->plaintext = NULL;
851 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
852 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
853 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
854 cdat->aead = EVP_CIPHER_mode(cipher);
855 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
863 static void cipher_test_cleanup(struct evp_test *t)
865 struct cipher_data *cdat = t->data;
866 test_free(cdat->key);
868 test_free(cdat->ciphertext);
869 test_free(cdat->plaintext);
870 test_free(cdat->aad);
871 test_free(cdat->tag);
874 static int cipher_test_parse(struct evp_test *t, const char *keyword,
877 struct cipher_data *cdat = t->data;
878 if (strcmp(keyword, "Key") == 0)
879 return test_bin(value, &cdat->key, &cdat->key_len);
880 if (strcmp(keyword, "IV") == 0)
881 return test_bin(value, &cdat->iv, &cdat->iv_len);
882 if (strcmp(keyword, "Plaintext") == 0)
883 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
884 if (strcmp(keyword, "Ciphertext") == 0)
885 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
887 if (strcmp(keyword, "AAD") == 0)
888 return test_bin(value, &cdat->aad, &cdat->aad_len);
889 if (strcmp(keyword, "Tag") == 0)
890 return test_bin(value, &cdat->tag, &cdat->tag_len);
893 if (strcmp(keyword, "Operation") == 0) {
894 if (strcmp(value, "ENCRYPT") == 0)
896 else if (strcmp(value, "DECRYPT") == 0)
905 static int cipher_test_enc(struct evp_test *t, int enc,
906 size_t out_misalign, size_t inp_misalign, int frag)
908 struct cipher_data *cdat = t->data;
909 unsigned char *in, *out, *tmp = NULL;
910 size_t in_len, out_len, donelen = 0;
911 int tmplen, chunklen, tmpflen;
912 EVP_CIPHER_CTX *ctx = NULL;
914 err = "INTERNAL_ERROR";
915 ctx = EVP_CIPHER_CTX_new();
918 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
920 in = cdat->plaintext;
921 in_len = cdat->plaintext_len;
922 out = cdat->ciphertext;
923 out_len = cdat->ciphertext_len;
925 in = cdat->ciphertext;
926 in_len = cdat->ciphertext_len;
927 out = cdat->plaintext;
928 out_len = cdat->plaintext_len;
930 if (inp_misalign == (size_t)-1) {
932 * Exercise in-place encryption
934 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
937 in = memcpy(tmp + out_misalign, in, in_len);
939 inp_misalign += 16 - ((out_misalign + in_len) & 15);
941 * 'tmp' will store both output and copy of input. We make the copy
942 * of input to specifically aligned part of 'tmp'. So we just
943 * figured out how much padding would ensure the required alignment,
944 * now we allocate extended buffer and finally copy the input just
945 * past inp_misalign in expression below. Output will be written
946 * past out_misalign...
948 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
949 inp_misalign + in_len);
952 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
953 inp_misalign, in, in_len);
955 err = "CIPHERINIT_ERROR";
956 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
958 err = "INVALID_IV_LENGTH";
961 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
964 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
970 * If encrypting or OCB just set tag length initially, otherwise
971 * set tag length and value.
973 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
974 err = "TAG_LENGTH_SET_ERROR";
977 err = "TAG_SET_ERROR";
980 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
981 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
987 err = "INVALID_KEY_LENGTH";
988 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
990 err = "KEY_SET_ERROR";
991 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
994 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
995 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
996 cdat->tag_len, cdat->tag)) {
997 err = "TAG_SET_ERROR";
1002 if (cdat->aead == EVP_CIPH_CCM_MODE) {
1003 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
1004 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
1009 err = "AAD_SET_ERROR";
1011 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
1016 * Supply the AAD in chunks less than the block size where possible
1018 if (cdat->aad_len > 0) {
1019 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1023 if (cdat->aad_len > 2) {
1024 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1027 donelen += cdat->aad_len - 2;
1029 if (cdat->aad_len > 1
1030 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1031 cdat->aad + donelen, 1))
1035 EVP_CIPHER_CTX_set_padding(ctx, 0);
1036 err = "CIPHERUPDATE_ERROR";
1039 /* We supply the data all in one go */
1040 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1043 /* Supply the data in chunks less than the block size where possible */
1045 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1052 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1060 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1066 err = "CIPHERFINAL_ERROR";
1067 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
1069 err = "LENGTH_MISMATCH";
1070 if (out_len != (size_t)(tmplen + tmpflen))
1072 err = "VALUE_MISMATCH";
1073 if (check_output(t, out, tmp + out_misalign, out_len))
1075 if (enc && cdat->aead) {
1076 unsigned char rtag[16];
1077 if (cdat->tag_len > sizeof(rtag)) {
1078 err = "TAG_LENGTH_INTERNAL_ERROR";
1081 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1082 cdat->tag_len, rtag)) {
1083 err = "TAG_RETRIEVE_ERROR";
1086 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
1087 err = "TAG_VALUE_MISMATCH";
1094 EVP_CIPHER_CTX_free(ctx);
1099 static int cipher_test_run(struct evp_test *t)
1101 struct cipher_data *cdat = t->data;
1103 size_t out_misalign, inp_misalign;
1109 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1110 /* IV is optional and usually omitted in wrap mode */
1111 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1116 if (cdat->aead && !cdat->tag) {
1120 for (out_misalign = 0; out_misalign <= 1;) {
1121 static char aux_err[64];
1122 t->aux_err = aux_err;
1123 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1124 if (inp_misalign == (size_t)-1) {
1125 /* kludge: inp_misalign == -1 means "exercise in-place" */
1126 BIO_snprintf(aux_err, sizeof(aux_err),
1127 "%s in-place, %sfragmented",
1128 out_misalign ? "misaligned" : "aligned",
1129 frag ? "" : "not ");
1131 BIO_snprintf(aux_err, sizeof(aux_err),
1132 "%s output and %s input, %sfragmented",
1133 out_misalign ? "misaligned" : "aligned",
1134 inp_misalign ? "misaligned" : "aligned",
1135 frag ? "" : "not ");
1138 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1139 /* Not fatal errors: return */
1146 if (cdat->enc != 1) {
1147 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1148 /* Not fatal errors: return */
1157 if (out_misalign == 1 && frag == 0) {
1159 * XTS, CCM and Wrap modes have special requirements about input
1160 * lengths so we don't fragment for those
1162 if (cdat->aead == EVP_CIPH_CCM_MODE
1163 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1164 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1177 static const struct evp_test_method cipher_test_method = {
1180 cipher_test_cleanup,
1188 /* Algorithm string for this MAC */
1194 unsigned char *input;
1196 /* Expected output */
1197 unsigned char *output;
1201 static int mac_test_init(struct evp_test *t, const char *alg)
1204 struct mac_data *mdat;
1205 if (strcmp(alg, "HMAC") == 0) {
1206 type = EVP_PKEY_HMAC;
1207 } else if (strcmp(alg, "CMAC") == 0) {
1208 #ifndef OPENSSL_NO_CMAC
1209 type = EVP_PKEY_CMAC;
1214 } else if (strcmp(alg, "Poly1305") == 0) {
1215 #ifndef OPENSSL_NO_POLY1305
1216 type = EVP_PKEY_POLY1305;
1221 } else if (strcmp(alg, "SipHash") == 0) {
1222 #ifndef OPENSSL_NO_SIPHASH
1223 type = EVP_PKEY_SIPHASH;
1231 mdat = OPENSSL_malloc(sizeof(*mdat));
1236 mdat->output = NULL;
1241 static void mac_test_cleanup(struct evp_test *t)
1243 struct mac_data *mdat = t->data;
1244 test_free(mdat->alg);
1245 test_free(mdat->key);
1246 test_free(mdat->input);
1247 test_free(mdat->output);
1250 static int mac_test_parse(struct evp_test *t,
1251 const char *keyword, const char *value)
1253 struct mac_data *mdata = t->data;
1254 if (strcmp(keyword, "Key") == 0)
1255 return test_bin(value, &mdata->key, &mdata->key_len);
1256 if (strcmp(keyword, "Algorithm") == 0) {
1257 mdata->alg = OPENSSL_strdup(value);
1262 if (strcmp(keyword, "Input") == 0)
1263 return test_bin(value, &mdata->input, &mdata->input_len);
1264 if (strcmp(keyword, "Output") == 0)
1265 return test_bin(value, &mdata->output, &mdata->output_len);
1269 static int mac_test_run(struct evp_test *t)
1271 struct mac_data *mdata = t->data;
1272 const char *err = "INTERNAL_ERROR";
1273 EVP_MD_CTX *mctx = NULL;
1274 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1275 EVP_PKEY *key = NULL;
1276 const EVP_MD *md = NULL;
1277 unsigned char *mac = NULL;
1280 #ifdef OPENSSL_NO_DES
1281 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1288 err = "MAC_PKEY_CTX_ERROR";
1289 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1293 err = "MAC_KEYGEN_INIT_ERROR";
1294 if (EVP_PKEY_keygen_init(genctx) <= 0)
1296 if (mdata->type == EVP_PKEY_CMAC) {
1297 err = "MAC_ALGORITHM_SET_ERROR";
1298 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1302 err = "MAC_KEY_SET_ERROR";
1303 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1306 err = "MAC_KEY_GENERATE_ERROR";
1307 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1309 if (mdata->type == EVP_PKEY_HMAC) {
1310 err = "MAC_ALGORITHM_SET_ERROR";
1311 md = EVP_get_digestbyname(mdata->alg);
1315 mctx = EVP_MD_CTX_new();
1318 err = "DIGESTSIGNINIT_ERROR";
1319 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1322 err = "DIGESTSIGNUPDATE_ERROR";
1323 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1325 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1326 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1328 mac = OPENSSL_malloc(mac_len);
1330 fprintf(stderr, "Error allocating mac buffer!\n");
1333 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1335 err = "MAC_LENGTH_MISMATCH";
1336 if (mac_len != mdata->output_len)
1338 err = "MAC_MISMATCH";
1339 if (check_output(t, mdata->output, mac, mac_len))
1343 EVP_MD_CTX_free(mctx);
1345 EVP_PKEY_CTX_free(genctx);
1351 static const struct evp_test_method mac_test_method = {
1360 * Public key operations. These are all very similar and can share
1361 * a lot of common code.
1365 /* Context for this operation */
1367 /* Key operation to perform */
1368 int (*keyop) (EVP_PKEY_CTX *ctx,
1369 unsigned char *sig, size_t *siglen,
1370 const unsigned char *tbs, size_t tbslen);
1372 unsigned char *input;
1374 /* Expected output */
1375 unsigned char *output;
1380 * Perform public key operation setup: lookup key, allocated ctx and call
1381 * the appropriate initialisation function
1383 static int pkey_test_init(struct evp_test *t, const char *name,
1385 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1386 int (*keyop) (EVP_PKEY_CTX *ctx,
1387 unsigned char *sig, size_t *siglen,
1388 const unsigned char *tbs,
1392 struct pkey_data *kdata;
1393 EVP_PKEY *pkey = NULL;
1396 rv = find_key(&pkey, name, t->public);
1398 rv = find_key(&pkey, name, t->private);
1399 if (!rv || pkey == NULL) {
1404 kdata = OPENSSL_malloc(sizeof(*kdata));
1406 EVP_PKEY_free(pkey);
1410 kdata->input = NULL;
1411 kdata->output = NULL;
1412 kdata->keyop = keyop;
1414 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1417 if (keyopinit(kdata->ctx) <= 0)
1418 t->err = "KEYOP_INIT_ERROR";
1422 static void pkey_test_cleanup(struct evp_test *t)
1424 struct pkey_data *kdata = t->data;
1426 OPENSSL_free(kdata->input);
1427 OPENSSL_free(kdata->output);
1428 EVP_PKEY_CTX_free(kdata->ctx);
1431 static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1437 tmpval = OPENSSL_strdup(value);
1440 p = strchr(tmpval, ':');
1443 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1445 t->err = "PKEY_CTRL_INVALID";
1447 } else if (p != NULL && rv <= 0) {
1448 /* If p has an OID and lookup fails assume disabled algorithm */
1449 int nid = OBJ_sn2nid(p);
1450 if (nid == NID_undef)
1451 nid = OBJ_ln2nid(p);
1452 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1453 EVP_get_cipherbynid(nid) == NULL) {
1457 t->err = "PKEY_CTRL_ERROR";
1461 OPENSSL_free(tmpval);
1465 static int pkey_test_parse(struct evp_test *t,
1466 const char *keyword, const char *value)
1468 struct pkey_data *kdata = t->data;
1469 if (strcmp(keyword, "Input") == 0)
1470 return test_bin(value, &kdata->input, &kdata->input_len);
1471 if (strcmp(keyword, "Output") == 0)
1472 return test_bin(value, &kdata->output, &kdata->output_len);
1473 if (strcmp(keyword, "Ctrl") == 0)
1474 return pkey_test_ctrl(t, kdata->ctx, value);
1478 static int pkey_test_run(struct evp_test *t)
1480 struct pkey_data *kdata = t->data;
1481 unsigned char *out = NULL;
1483 const char *err = "KEYOP_LENGTH_ERROR";
1484 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1485 kdata->input_len) <= 0)
1487 out = OPENSSL_malloc(out_len);
1489 fprintf(stderr, "Error allocating output buffer!\n");
1492 err = "KEYOP_ERROR";
1494 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1496 err = "KEYOP_LENGTH_MISMATCH";
1497 if (out_len != kdata->output_len)
1499 err = "KEYOP_MISMATCH";
1500 if (check_output(t, kdata->output, out, out_len))
1509 static int sign_test_init(struct evp_test *t, const char *name)
1511 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1514 static const struct evp_test_method psign_test_method = {
1522 static int verify_recover_test_init(struct evp_test *t, const char *name)
1524 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1525 EVP_PKEY_verify_recover);
1528 static const struct evp_test_method pverify_recover_test_method = {
1530 verify_recover_test_init,
1536 static int decrypt_test_init(struct evp_test *t, const char *name)
1538 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1542 static const struct evp_test_method pdecrypt_test_method = {
1550 static int verify_test_init(struct evp_test *t, const char *name)
1552 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1555 static int verify_test_run(struct evp_test *t)
1557 struct pkey_data *kdata = t->data;
1558 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1559 kdata->input, kdata->input_len) <= 0)
1560 t->err = "VERIFY_ERROR";
1564 static const struct evp_test_method pverify_test_method = {
1573 static int pderive_test_init(struct evp_test *t, const char *name)
1575 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1578 static int pderive_test_parse(struct evp_test *t,
1579 const char *keyword, const char *value)
1581 struct pkey_data *kdata = t->data;
1583 if (strcmp(keyword, "PeerKey") == 0) {
1585 if (find_key(&peer, value, t->public) == 0)
1587 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1591 if (strcmp(keyword, "SharedSecret") == 0)
1592 return test_bin(value, &kdata->output, &kdata->output_len);
1593 if (strcmp(keyword, "Ctrl") == 0)
1594 return pkey_test_ctrl(t, kdata->ctx, value);
1598 static int pderive_test_run(struct evp_test *t)
1600 struct pkey_data *kdata = t->data;
1601 unsigned char *out = NULL;
1603 const char *err = "INTERNAL_ERROR";
1605 out_len = kdata->output_len;
1606 out = OPENSSL_malloc(out_len);
1608 fprintf(stderr, "Error allocating output buffer!\n");
1611 err = "DERIVE_ERROR";
1612 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1614 err = "SHARED_SECRET_LENGTH_MISMATCH";
1615 if (out_len != kdata->output_len)
1617 err = "SHARED_SECRET_MISMATCH";
1618 if (check_output(t, kdata->output, out, out_len))
1627 static const struct evp_test_method pderive_test_method = {
1637 #define PBE_TYPE_SCRYPT 1
1638 #define PBE_TYPE_PBKDF2 2
1639 #define PBE_TYPE_PKCS12 3
1645 /* scrypt parameters */
1646 uint64_t N, r, p, maxmem;
1648 /* PKCS#12 parameters */
1653 unsigned char *pass;
1657 unsigned char *salt;
1660 /* Expected output */
1665 #ifndef OPENSSL_NO_SCRYPT
1666 static int scrypt_test_parse(struct evp_test *t,
1667 const char *keyword, const char *value)
1669 struct pbe_data *pdata = t->data;
1671 if (strcmp(keyword, "N") == 0)
1672 return test_uint64(value, &pdata->N);
1673 if (strcmp(keyword, "p") == 0)
1674 return test_uint64(value, &pdata->p);
1675 if (strcmp(keyword, "r") == 0)
1676 return test_uint64(value, &pdata->r);
1677 if (strcmp(keyword, "maxmem") == 0)
1678 return test_uint64(value, &pdata->maxmem);
1683 static int pbkdf2_test_parse(struct evp_test *t,
1684 const char *keyword, const char *value)
1686 struct pbe_data *pdata = t->data;
1688 if (strcmp(keyword, "iter") == 0) {
1689 pdata->iter = atoi(value);
1690 if (pdata->iter <= 0)
1694 if (strcmp(keyword, "MD") == 0) {
1695 pdata->md = EVP_get_digestbyname(value);
1696 if (pdata->md == NULL)
1703 static int pkcs12_test_parse(struct evp_test *t,
1704 const char *keyword, const char *value)
1706 struct pbe_data *pdata = t->data;
1708 if (strcmp(keyword, "id") == 0) {
1709 pdata->id = atoi(value);
1714 return pbkdf2_test_parse(t, keyword, value);
1717 static int pbe_test_init(struct evp_test *t, const char *alg)
1719 struct pbe_data *pdat;
1722 if (strcmp(alg, "scrypt") == 0) {
1723 #ifndef OPENSSL_NO_SCRYPT
1724 pbe_type = PBE_TYPE_SCRYPT;
1729 } else if (strcmp(alg, "pbkdf2") == 0) {
1730 pbe_type = PBE_TYPE_PBKDF2;
1731 } else if (strcmp(alg, "pkcs12") == 0) {
1732 pbe_type = PBE_TYPE_PKCS12;
1734 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1736 pdat = OPENSSL_malloc(sizeof(*pdat));
1737 pdat->pbe_type = pbe_type;
1751 static void pbe_test_cleanup(struct evp_test *t)
1753 struct pbe_data *pdat = t->data;
1754 test_free(pdat->pass);
1755 test_free(pdat->salt);
1756 test_free(pdat->key);
1759 static int pbe_test_parse(struct evp_test *t,
1760 const char *keyword, const char *value)
1762 struct pbe_data *pdata = t->data;
1764 if (strcmp(keyword, "Password") == 0)
1765 return test_bin(value, &pdata->pass, &pdata->pass_len);
1766 if (strcmp(keyword, "Salt") == 0)
1767 return test_bin(value, &pdata->salt, &pdata->salt_len);
1768 if (strcmp(keyword, "Key") == 0)
1769 return test_bin(value, &pdata->key, &pdata->key_len);
1770 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1771 return pbkdf2_test_parse(t, keyword, value);
1772 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1773 return pkcs12_test_parse(t, keyword, value);
1774 #ifndef OPENSSL_NO_SCRYPT
1775 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1776 return scrypt_test_parse(t, keyword, value);
1781 static int pbe_test_run(struct evp_test *t)
1783 struct pbe_data *pdata = t->data;
1784 const char *err = "INTERNAL_ERROR";
1787 key = OPENSSL_malloc(pdata->key_len);
1790 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1791 err = "PBKDF2_ERROR";
1792 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1793 pdata->salt, pdata->salt_len,
1794 pdata->iter, pdata->md,
1795 pdata->key_len, key) == 0)
1797 #ifndef OPENSSL_NO_SCRYPT
1798 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1799 err = "SCRYPT_ERROR";
1800 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1801 pdata->salt, pdata->salt_len,
1802 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1803 key, pdata->key_len) == 0)
1806 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1807 err = "PKCS12_ERROR";
1808 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1809 pdata->salt, pdata->salt_len,
1810 pdata->id, pdata->iter, pdata->key_len,
1811 key, pdata->md) == 0)
1814 err = "KEY_MISMATCH";
1815 if (check_output(t, pdata->key, key, pdata->key_len))
1824 static const struct evp_test_method pbe_test_method = {
1835 BASE64_CANONICAL_ENCODING = 0,
1836 BASE64_VALID_ENCODING = 1,
1837 BASE64_INVALID_ENCODING = 2
1838 } base64_encoding_type;
1840 struct encode_data {
1841 /* Input to encoding */
1842 unsigned char *input;
1844 /* Expected output */
1845 unsigned char *output;
1847 base64_encoding_type encoding;
1850 static int encode_test_init(struct evp_test *t, const char *encoding)
1852 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1854 if (strcmp(encoding, "canonical") == 0) {
1855 edata->encoding = BASE64_CANONICAL_ENCODING;
1856 } else if (strcmp(encoding, "valid") == 0) {
1857 edata->encoding = BASE64_VALID_ENCODING;
1858 } else if (strcmp(encoding, "invalid") == 0) {
1859 edata->encoding = BASE64_INVALID_ENCODING;
1860 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1861 if (t->expected_err == NULL)
1864 fprintf(stderr, "Bad encoding: %s. Should be one of "
1865 "{canonical, valid, invalid}\n", encoding);
1872 static void encode_test_cleanup(struct evp_test *t)
1874 struct encode_data *edata = t->data;
1875 test_free(edata->input);
1876 test_free(edata->output);
1877 memset(edata, 0, sizeof(*edata));
1880 static int encode_test_parse(struct evp_test *t,
1881 const char *keyword, const char *value)
1883 struct encode_data *edata = t->data;
1884 if (strcmp(keyword, "Input") == 0)
1885 return test_bin(value, &edata->input, &edata->input_len);
1886 if (strcmp(keyword, "Output") == 0)
1887 return test_bin(value, &edata->output, &edata->output_len);
1891 static int encode_test_run(struct evp_test *t)
1893 struct encode_data *edata = t->data;
1894 unsigned char *encode_out = NULL, *decode_out = NULL;
1895 int output_len, chunk_len;
1896 const char *err = "INTERNAL_ERROR";
1897 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1899 if (decode_ctx == NULL)
1902 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1903 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1904 if (encode_ctx == NULL)
1906 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1907 if (encode_out == NULL)
1910 EVP_EncodeInit(encode_ctx);
1911 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1912 edata->input, edata->input_len);
1913 output_len = chunk_len;
1915 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1916 output_len += chunk_len;
1918 EVP_ENCODE_CTX_free(encode_ctx);
1920 if (check_var_length_output(t, edata->output, edata->output_len,
1921 encode_out, output_len)) {
1922 err = "BAD_ENCODING";
1927 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1928 if (decode_out == NULL)
1931 EVP_DecodeInit(decode_ctx);
1932 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1933 edata->output_len) < 0) {
1934 err = "DECODE_ERROR";
1937 output_len = chunk_len;
1939 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1940 err = "DECODE_ERROR";
1943 output_len += chunk_len;
1945 if (edata->encoding != BASE64_INVALID_ENCODING &&
1946 check_var_length_output(t, edata->input, edata->input_len,
1947 decode_out, output_len)) {
1948 err = "BAD_DECODING";
1955 OPENSSL_free(encode_out);
1956 OPENSSL_free(decode_out);
1957 EVP_ENCODE_CTX_free(decode_ctx);
1961 static const struct evp_test_method encode_test_method = {
1964 encode_test_cleanup,
1969 /* KDF operations */
1972 /* Context for this operation */
1974 /* Expected output */
1975 unsigned char *output;
1980 * Perform public key operation setup: lookup key, allocated ctx and call
1981 * the appropriate initialisation function
1983 static int kdf_test_init(struct evp_test *t, const char *name)
1985 struct kdf_data *kdata;
1987 kdata = OPENSSL_malloc(sizeof(*kdata));
1991 kdata->output = NULL;
1993 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1994 if (kdata->ctx == NULL)
1996 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
2001 static void kdf_test_cleanup(struct evp_test *t)
2003 struct kdf_data *kdata = t->data;
2004 OPENSSL_free(kdata->output);
2005 EVP_PKEY_CTX_free(kdata->ctx);
2008 static int kdf_test_parse(struct evp_test *t,
2009 const char *keyword, const char *value)
2011 struct kdf_data *kdata = t->data;
2012 if (strcmp(keyword, "Output") == 0)
2013 return test_bin(value, &kdata->output, &kdata->output_len);
2014 if (strncmp(keyword, "Ctrl", 4) == 0)
2015 return pkey_test_ctrl(t, kdata->ctx, value);
2019 static int kdf_test_run(struct evp_test *t)
2021 struct kdf_data *kdata = t->data;
2022 unsigned char *out = NULL;
2023 size_t out_len = kdata->output_len;
2024 const char *err = "INTERNAL_ERROR";
2025 out = OPENSSL_malloc(out_len);
2027 fprintf(stderr, "Error allocating output buffer!\n");
2030 err = "KDF_DERIVE_ERROR";
2031 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
2033 err = "KDF_LENGTH_MISMATCH";
2034 if (out_len != kdata->output_len)
2036 err = "KDF_MISMATCH";
2037 if (check_output(t, kdata->output, out, out_len))
2046 static const struct evp_test_method kdf_test_method = {