2 * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/provider.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pkcs12.h>
20 #include <openssl/kdf.h>
21 #include <openssl/params.h>
22 #include <openssl/core_names.h>
23 #include "internal/numbers.h"
24 #include "internal/nelem.h"
30 typedef struct evp_test_method_st EVP_TEST_METHOD;
33 * Structure holding test information
35 typedef struct evp_test_st {
36 STANZA s; /* Common test stanza */
38 int skip; /* Current test should be skipped */
39 const EVP_TEST_METHOD *meth; /* method for this test */
40 const char *err, *aux_err; /* Error string for test */
41 char *expected_err; /* Expected error value of test */
42 char *reason; /* Expected error reason string */
43 void *data; /* test specific data */
47 * Test method structure
49 struct evp_test_method_st {
50 /* Name of test as it appears in file */
52 /* Initialise test for "alg" */
53 int (*init) (EVP_TEST * t, const char *alg);
55 void (*cleanup) (EVP_TEST * t);
56 /* Test specific name value pair processing */
57 int (*parse) (EVP_TEST * t, const char *name, const char *value);
58 /* Run the test itself */
59 int (*run_test) (EVP_TEST * t);
64 * Linked list of named keys.
66 typedef struct key_list_st {
69 struct key_list_st *next;
73 * List of public and private keys
75 static KEY_LIST *private_keys;
76 static KEY_LIST *public_keys;
77 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
79 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
82 * Compare two memory regions for equality, returning zero if they differ.
83 * However, if there is expected to be an error and the actual error
84 * matches then the memory is expected to be different so handle this
85 * case without producing unnecessary test framework output.
87 static int memory_err_compare(EVP_TEST *t, const char *err,
88 const void *expected, size_t expected_len,
89 const void *got, size_t got_len)
93 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
94 r = !TEST_mem_ne(expected, expected_len, got, got_len);
96 r = TEST_mem_eq(expected, expected_len, got, got_len);
103 * Structure used to hold a list of blocks of memory to test
104 * calls to "update" like functions.
106 struct evp_test_buffer_st {
113 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
116 OPENSSL_free(db->buf);
122 * append buffer to a list
124 static int evp_test_buffer_append(const char *value,
125 STACK_OF(EVP_TEST_BUFFER) **sk)
127 EVP_TEST_BUFFER *db = NULL;
129 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
132 if (!parse_bin(value, &db->buf, &db->buflen))
137 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
139 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
145 evp_test_buffer_free(db);
150 * replace last buffer in list with copies of itself
152 static int evp_test_buffer_ncopy(const char *value,
153 STACK_OF(EVP_TEST_BUFFER) *sk)
156 unsigned char *tbuf, *p;
158 int ncopy = atoi(value);
163 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
165 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
167 tbuflen = db->buflen * ncopy;
168 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
170 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
171 memcpy(p, db->buf, db->buflen);
173 OPENSSL_free(db->buf);
175 db->buflen = tbuflen;
180 * set repeat count for last buffer in list
182 static int evp_test_buffer_set_count(const char *value,
183 STACK_OF(EVP_TEST_BUFFER) *sk)
186 int count = atoi(value);
191 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
194 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
195 if (db->count_set != 0)
198 db->count = (size_t)count;
204 * call "fn" with each element of the list in turn
206 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
208 const unsigned char *buf,
214 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
215 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
218 for (j = 0; j < tb->count; j++) {
219 if (fn(ctx, tb->buf, tb->buflen) <= 0)
227 * Unescape some sequences in string literals (only \n for now).
228 * Return an allocated buffer, set |out_len|. If |input_len|
229 * is zero, get an empty buffer but set length to zero.
231 static unsigned char* unescape(const char *input, size_t input_len,
234 unsigned char *ret, *p;
237 if (input_len == 0) {
239 return OPENSSL_zalloc(1);
242 /* Escaping is non-expanding; over-allocate original size for simplicity. */
243 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
246 for (i = 0; i < input_len; i++) {
247 if (*input == '\\') {
248 if (i == input_len - 1 || *++input != 'n') {
249 TEST_error("Bad escape sequence in file");
269 * For a hex string "value" convert to a binary allocated buffer.
270 * Return 1 on success or 0 on failure.
272 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
276 /* Check for NULL literal */
277 if (strcmp(value, "NULL") == 0) {
283 /* Check for empty value */
284 if (*value == '\0') {
286 * Don't return NULL for zero length buffer. This is needed for
287 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
288 * buffer even if the key length is 0, in order to detect key reset.
290 *buf = OPENSSL_malloc(1);
298 /* Check for string literal */
299 if (value[0] == '"') {
300 size_t vlen = strlen(++value);
302 if (vlen == 0 || value[vlen - 1] != '"')
305 *buf = unescape(value, vlen, buflen);
306 return *buf == NULL ? 0 : 1;
309 /* Otherwise assume as hex literal and convert it to binary buffer */
310 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
311 TEST_info("Can't convert %s", value);
312 TEST_openssl_errors();
315 /* Size of input buffer means we'll never overflow */
322 *** MESSAGE DIGEST TESTS
325 typedef struct digest_data_st {
326 /* Digest this test is for */
327 const EVP_MD *digest;
328 EVP_MD *fetched_digest;
329 /* Input to digest */
330 STACK_OF(EVP_TEST_BUFFER) *input;
331 /* Expected output */
332 unsigned char *output;
336 static int digest_test_init(EVP_TEST *t, const char *alg)
339 const EVP_MD *digest;
340 EVP_MD *fetched_digest;
342 if ((digest = fetched_digest = EVP_MD_fetch(NULL, alg, NULL)) == NULL
343 && (digest = EVP_get_digestbyname(alg)) == NULL) {
344 /* If alg has an OID assume disabled algorithm */
345 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
351 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
354 mdat->digest = digest;
355 mdat->fetched_digest = fetched_digest;
356 if (fetched_digest != NULL)
357 TEST_info("%s is fetched", alg);
361 static void digest_test_cleanup(EVP_TEST *t)
363 DIGEST_DATA *mdat = t->data;
365 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
366 OPENSSL_free(mdat->output);
367 EVP_MD_meth_free(mdat->fetched_digest);
370 static int digest_test_parse(EVP_TEST *t,
371 const char *keyword, const char *value)
373 DIGEST_DATA *mdata = t->data;
375 if (strcmp(keyword, "Input") == 0)
376 return evp_test_buffer_append(value, &mdata->input);
377 if (strcmp(keyword, "Output") == 0)
378 return parse_bin(value, &mdata->output, &mdata->output_len);
379 if (strcmp(keyword, "Count") == 0)
380 return evp_test_buffer_set_count(value, mdata->input);
381 if (strcmp(keyword, "Ncopy") == 0)
382 return evp_test_buffer_ncopy(value, mdata->input);
386 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
388 return EVP_DigestUpdate(ctx, buf, buflen);
391 static int digest_test_run(EVP_TEST *t)
393 DIGEST_DATA *expected = t->data;
395 unsigned char *got = NULL;
396 unsigned int got_len;
398 t->err = "TEST_FAILURE";
399 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
402 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
403 expected->output_len : EVP_MAX_MD_SIZE);
407 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
408 t->err = "DIGESTINIT_ERROR";
411 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
412 t->err = "DIGESTUPDATE_ERROR";
416 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
417 EVP_MD_CTX *mctx_cpy;
418 char dont[] = "touch";
420 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
423 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
424 EVP_MD_CTX_free(mctx_cpy);
427 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
428 EVP_MD_CTX_free(mctx_cpy);
429 t->err = "DIGESTFINALXOF_ERROR";
432 if (!TEST_str_eq(dont, "touch")) {
433 EVP_MD_CTX_free(mctx_cpy);
434 t->err = "DIGESTFINALXOF_ERROR";
437 EVP_MD_CTX_free(mctx_cpy);
439 got_len = expected->output_len;
440 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
441 t->err = "DIGESTFINALXOF_ERROR";
445 if (!EVP_DigestFinal(mctx, got, &got_len)) {
446 t->err = "DIGESTFINAL_ERROR";
450 if (!TEST_int_eq(expected->output_len, got_len)) {
451 t->err = "DIGEST_LENGTH_MISMATCH";
454 if (!memory_err_compare(t, "DIGEST_MISMATCH",
455 expected->output, expected->output_len,
463 EVP_MD_CTX_free(mctx);
467 static const EVP_TEST_METHOD digest_test_method = {
480 typedef struct cipher_data_st {
481 const EVP_CIPHER *cipher;
482 EVP_CIPHER *fetched_cipher;
484 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
491 unsigned char *plaintext;
492 size_t plaintext_len;
493 unsigned char *ciphertext;
494 size_t ciphertext_len;
495 /* GCM, CCM, OCB and SIV only */
496 unsigned char *aad[AAD_NUM];
497 size_t aad_len[AAD_NUM];
503 static int cipher_test_init(EVP_TEST *t, const char *alg)
505 const EVP_CIPHER *cipher;
506 EVP_CIPHER *fetched_cipher;
510 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(NULL, alg, NULL)) == NULL
511 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
512 /* If alg has an OID assume disabled algorithm */
513 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
519 cdat = OPENSSL_zalloc(sizeof(*cdat));
520 cdat->cipher = cipher;
521 cdat->fetched_cipher = fetched_cipher;
523 m = EVP_CIPHER_mode(cipher);
524 if (m == EVP_CIPH_GCM_MODE
525 || m == EVP_CIPH_OCB_MODE
526 || m == EVP_CIPH_SIV_MODE
527 || m == EVP_CIPH_CCM_MODE)
529 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
535 if (fetched_cipher != NULL)
536 TEST_info("%s is fetched", alg);
540 static void cipher_test_cleanup(EVP_TEST *t)
543 CIPHER_DATA *cdat = t->data;
545 OPENSSL_free(cdat->key);
546 OPENSSL_free(cdat->iv);
547 OPENSSL_free(cdat->ciphertext);
548 OPENSSL_free(cdat->plaintext);
549 for (i = 0; i < AAD_NUM; i++)
550 OPENSSL_free(cdat->aad[i]);
551 OPENSSL_free(cdat->tag);
552 EVP_CIPHER_meth_free(cdat->fetched_cipher);
555 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
558 CIPHER_DATA *cdat = t->data;
561 if (strcmp(keyword, "Key") == 0)
562 return parse_bin(value, &cdat->key, &cdat->key_len);
563 if (strcmp(keyword, "Rounds") == 0) {
567 cdat->rounds = (unsigned int)i;
570 if (strcmp(keyword, "IV") == 0)
571 return parse_bin(value, &cdat->iv, &cdat->iv_len);
572 if (strcmp(keyword, "Plaintext") == 0)
573 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
574 if (strcmp(keyword, "Ciphertext") == 0)
575 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
577 if (strcmp(keyword, "AAD") == 0) {
578 for (i = 0; i < AAD_NUM; i++) {
579 if (cdat->aad[i] == NULL)
580 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
584 if (strcmp(keyword, "Tag") == 0)
585 return parse_bin(value, &cdat->tag, &cdat->tag_len);
586 if (strcmp(keyword, "SetTagLate") == 0) {
587 if (strcmp(value, "TRUE") == 0)
589 else if (strcmp(value, "FALSE") == 0)
597 if (strcmp(keyword, "Operation") == 0) {
598 if (strcmp(value, "ENCRYPT") == 0)
600 else if (strcmp(value, "DECRYPT") == 0)
609 static int cipher_test_enc(EVP_TEST *t, int enc,
610 size_t out_misalign, size_t inp_misalign, int frag)
612 CIPHER_DATA *expected = t->data;
613 unsigned char *in, *expected_out, *tmp = NULL;
614 size_t in_len, out_len, donelen = 0;
615 int ok = 0, tmplen, chunklen, tmpflen, i;
616 EVP_CIPHER_CTX *ctx = NULL;
618 t->err = "TEST_FAILURE";
619 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
621 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
623 in = expected->plaintext;
624 in_len = expected->plaintext_len;
625 expected_out = expected->ciphertext;
626 out_len = expected->ciphertext_len;
628 in = expected->ciphertext;
629 in_len = expected->ciphertext_len;
630 expected_out = expected->plaintext;
631 out_len = expected->plaintext_len;
633 if (inp_misalign == (size_t)-1) {
635 * Exercise in-place encryption
637 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
640 in = memcpy(tmp + out_misalign, in, in_len);
642 inp_misalign += 16 - ((out_misalign + in_len) & 15);
644 * 'tmp' will store both output and copy of input. We make the copy
645 * of input to specifically aligned part of 'tmp'. So we just
646 * figured out how much padding would ensure the required alignment,
647 * now we allocate extended buffer and finally copy the input just
648 * past inp_misalign in expression below. Output will be written
649 * past out_misalign...
651 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
652 inp_misalign + in_len);
655 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
656 inp_misalign, in, in_len);
658 if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
659 t->err = "CIPHERINIT_ERROR";
663 if (expected->aead) {
664 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
665 expected->iv_len, 0)) {
666 t->err = "INVALID_IV_LENGTH";
669 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
670 t->err = "INVALID_IV_LENGTH";
674 if (expected->aead) {
677 * If encrypting or OCB just set tag length initially, otherwise
678 * set tag length and value.
680 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
681 t->err = "TAG_LENGTH_SET_ERROR";
684 t->err = "TAG_SET_ERROR";
687 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
688 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
689 expected->tag_len, tag))
694 if (expected->rounds > 0) {
695 int rounds = (int)expected->rounds;
697 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
698 t->err = "INVALID_ROUNDS";
703 if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
704 t->err = "INVALID_KEY_LENGTH";
707 if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
708 t->err = "KEY_SET_ERROR";
711 /* Check that we get the same IV back */
712 if (expected->iv != NULL
713 && (EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
714 && !TEST_mem_eq(expected->iv, expected->iv_len,
715 EVP_CIPHER_CTX_iv(ctx), expected->iv_len)) {
716 t->err = "INVALID_IV";
720 if (expected->aead == EVP_CIPH_CCM_MODE) {
721 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
722 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
726 if (expected->aad[0] != NULL) {
727 t->err = "AAD_SET_ERROR";
729 for (i = 0; expected->aad[i] != NULL; i++) {
730 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
731 expected->aad_len[i]))
736 * Supply the AAD in chunks less than the block size where possible
738 for (i = 0; expected->aad[i] != NULL; i++) {
739 if (expected->aad_len[i] > 0) {
740 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
744 if (expected->aad_len[i] > 2) {
745 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
746 expected->aad[i] + donelen,
747 expected->aad_len[i] - 2))
749 donelen += expected->aad_len[i] - 2;
751 if (expected->aad_len[i] > 1
752 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
753 expected->aad[i] + donelen, 1))
759 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
760 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
761 expected->tag_len, expected->tag)) {
762 t->err = "TAG_SET_ERROR";
767 EVP_CIPHER_CTX_set_padding(ctx, 0);
768 t->err = "CIPHERUPDATE_ERROR";
771 /* We supply the data all in one go */
772 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
775 /* Supply the data in chunks less than the block size where possible */
777 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
784 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
792 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
798 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
799 t->err = "CIPHERFINAL_ERROR";
802 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
803 tmp + out_misalign, tmplen + tmpflen))
805 if (enc && expected->aead) {
806 unsigned char rtag[16];
808 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
809 t->err = "TAG_LENGTH_INTERNAL_ERROR";
812 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
813 expected->tag_len, rtag)) {
814 t->err = "TAG_RETRIEVE_ERROR";
817 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
818 expected->tag, expected->tag_len,
819 rtag, expected->tag_len))
826 EVP_CIPHER_CTX_free(ctx);
830 static int cipher_test_run(EVP_TEST *t)
832 CIPHER_DATA *cdat = t->data;
834 size_t out_misalign, inp_misalign;
840 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
841 /* IV is optional and usually omitted in wrap mode */
842 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
847 if (cdat->aead && !cdat->tag) {
851 for (out_misalign = 0; out_misalign <= 1;) {
852 static char aux_err[64];
853 t->aux_err = aux_err;
854 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
855 if (inp_misalign == (size_t)-1) {
856 /* kludge: inp_misalign == -1 means "exercise in-place" */
857 BIO_snprintf(aux_err, sizeof(aux_err),
858 "%s in-place, %sfragmented",
859 out_misalign ? "misaligned" : "aligned",
862 BIO_snprintf(aux_err, sizeof(aux_err),
863 "%s output and %s input, %sfragmented",
864 out_misalign ? "misaligned" : "aligned",
865 inp_misalign ? "misaligned" : "aligned",
869 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
870 /* Not fatal errors: return */
877 if (cdat->enc != 1) {
878 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
879 /* Not fatal errors: return */
888 if (out_misalign == 1 && frag == 0) {
890 * XTS, SIV, CCM and Wrap modes have special requirements about input
891 * lengths so we don't fragment for those
893 if (cdat->aead == EVP_CIPH_CCM_MODE
894 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
895 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
896 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
909 static const EVP_TEST_METHOD cipher_test_method = {
922 typedef struct mac_data_st {
923 /* MAC type in one form or another */
924 EVP_MAC *mac; /* for mac_test_run_mac */
925 int type; /* for mac_test_run_pkey */
926 /* Algorithm string for this MAC */
935 unsigned char *input;
937 /* Expected output */
938 unsigned char *output;
940 unsigned char *custom;
942 /* MAC salt (blake2) */
945 /* Collection of controls */
946 STACK_OF(OPENSSL_STRING) *controls;
949 static int mac_test_init(EVP_TEST *t, const char *alg)
952 int type = NID_undef;
955 if ((mac = EVP_MAC_fetch(NULL, alg, NULL)) == NULL) {
957 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
958 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
959 * the EVP_PKEY method.
961 size_t sz = strlen(alg);
962 static const char epilogue[] = " by EVP_PKEY";
964 if (sz >= sizeof(epilogue)
965 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
966 sz -= sizeof(epilogue) - 1;
968 if (strncmp(alg, "HMAC", sz) == 0) {
969 type = EVP_PKEY_HMAC;
970 } else if (strncmp(alg, "CMAC", sz) == 0) {
971 #ifndef OPENSSL_NO_CMAC
972 type = EVP_PKEY_CMAC;
977 } else if (strncmp(alg, "Poly1305", sz) == 0) {
978 #ifndef OPENSSL_NO_POLY1305
979 type = EVP_PKEY_POLY1305;
984 } else if (strncmp(alg, "SipHash", sz) == 0) {
985 #ifndef OPENSSL_NO_SIPHASH
986 type = EVP_PKEY_SIPHASH;
993 * Not a known EVP_PKEY method either. If it's a known OID, then
994 * assume it's been disabled.
996 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
1005 mdat = OPENSSL_zalloc(sizeof(*mdat));
1008 mdat->controls = sk_OPENSSL_STRING_new_null();
1013 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1014 static void openssl_free(char *m)
1019 static void mac_test_cleanup(EVP_TEST *t)
1021 MAC_DATA *mdat = t->data;
1023 EVP_MAC_free(mdat->mac);
1024 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1025 OPENSSL_free(mdat->alg);
1026 OPENSSL_free(mdat->key);
1027 OPENSSL_free(mdat->iv);
1028 OPENSSL_free(mdat->custom);
1029 OPENSSL_free(mdat->salt);
1030 OPENSSL_free(mdat->input);
1031 OPENSSL_free(mdat->output);
1034 static int mac_test_parse(EVP_TEST *t,
1035 const char *keyword, const char *value)
1037 MAC_DATA *mdata = t->data;
1039 if (strcmp(keyword, "Key") == 0)
1040 return parse_bin(value, &mdata->key, &mdata->key_len);
1041 if (strcmp(keyword, "IV") == 0)
1042 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1043 if (strcmp(keyword, "Custom") == 0)
1044 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1045 if (strcmp(keyword, "Salt") == 0)
1046 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1047 if (strcmp(keyword, "Algorithm") == 0) {
1048 mdata->alg = OPENSSL_strdup(value);
1053 if (strcmp(keyword, "Input") == 0)
1054 return parse_bin(value, &mdata->input, &mdata->input_len);
1055 if (strcmp(keyword, "Output") == 0)
1056 return parse_bin(value, &mdata->output, &mdata->output_len);
1057 if (strcmp(keyword, "Ctrl") == 0)
1058 return sk_OPENSSL_STRING_push(mdata->controls,
1059 OPENSSL_strdup(value)) != 0;
1063 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1069 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1071 p = strchr(tmpval, ':');
1074 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1076 t->err = "PKEY_CTRL_INVALID";
1078 t->err = "PKEY_CTRL_ERROR";
1081 OPENSSL_free(tmpval);
1085 static int mac_test_run_pkey(EVP_TEST *t)
1087 MAC_DATA *expected = t->data;
1088 EVP_MD_CTX *mctx = NULL;
1089 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1090 EVP_PKEY *key = NULL;
1091 const EVP_MD *md = NULL;
1092 unsigned char *got = NULL;
1096 if (expected->alg == NULL)
1097 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1099 TEST_info("Trying the EVP_PKEY %s test with %s",
1100 OBJ_nid2sn(expected->type), expected->alg);
1102 #ifdef OPENSSL_NO_DES
1103 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1110 if (expected->type == EVP_PKEY_CMAC)
1111 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1112 EVP_get_cipherbyname(expected->alg));
1114 key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
1117 t->err = "MAC_KEY_CREATE_ERROR";
1121 if (expected->type == EVP_PKEY_HMAC) {
1122 if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
1123 t->err = "MAC_ALGORITHM_SET_ERROR";
1127 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1128 t->err = "INTERNAL_ERROR";
1131 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1132 t->err = "DIGESTSIGNINIT_ERROR";
1135 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1136 if (!mac_test_ctrl_pkey(t, pctx,
1137 sk_OPENSSL_STRING_value(expected->controls,
1139 t->err = "EVPPKEYCTXCTRL_ERROR";
1142 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1143 t->err = "DIGESTSIGNUPDATE_ERROR";
1146 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1147 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1150 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1151 t->err = "TEST_FAILURE";
1154 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1155 || !memory_err_compare(t, "TEST_MAC_ERR",
1156 expected->output, expected->output_len,
1158 t->err = "TEST_MAC_ERR";
1163 EVP_MD_CTX_free(mctx);
1165 EVP_PKEY_CTX_free(genctx);
1170 static int mac_test_run_mac(EVP_TEST *t)
1172 MAC_DATA *expected = t->data;
1173 EVP_MAC_CTX *ctx = NULL;
1174 unsigned char *got = NULL;
1177 OSSL_PARAM params[21];
1178 size_t params_n = 0;
1179 size_t params_n_allocstart = 0;
1180 const OSSL_PARAM *defined_params =
1181 EVP_MAC_settable_ctx_params(expected->mac);
1183 if (expected->alg == NULL)
1184 TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected->mac));
1186 TEST_info("Trying the EVP_MAC %s test with %s",
1187 EVP_MAC_name(expected->mac), expected->alg);
1189 #ifdef OPENSSL_NO_DES
1190 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1197 if (expected->alg != NULL) {
1199 * The underlying algorithm may be a cipher or a digest.
1200 * We don't know which it is, but we can ask the MAC what it
1201 * should be and bet on that.
1203 if (OSSL_PARAM_locate_const(defined_params,
1204 OSSL_MAC_PARAM_CIPHER) != NULL) {
1205 params[params_n++] =
1206 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1208 } else if (OSSL_PARAM_locate_const(defined_params,
1209 OSSL_MAC_PARAM_DIGEST) != NULL) {
1210 params[params_n++] =
1211 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1214 t->err = "MAC_BAD_PARAMS";
1218 if (expected->key != NULL)
1219 params[params_n++] =
1220 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1223 if (expected->custom != NULL)
1224 params[params_n++] =
1225 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1227 expected->custom_len);
1228 if (expected->salt != NULL)
1229 params[params_n++] =
1230 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1232 expected->salt_len);
1233 if (expected->iv != NULL)
1234 params[params_n++] =
1235 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1240 * Unknown controls. They must match parameters that the MAC recognises
1242 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1243 >= OSSL_NELEM(params)) {
1244 t->err = "MAC_TOO_MANY_PARAMETERS";
1247 params_n_allocstart = params_n;
1248 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1249 char *tmpkey, *tmpval;
1250 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1252 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1253 t->err = "MAC_PARAM_ERROR";
1256 tmpval = strchr(tmpkey, ':');
1261 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1265 OPENSSL_free(tmpkey);
1266 t->err = "MAC_PARAM_ERROR";
1271 OPENSSL_free(tmpkey);
1273 params[params_n] = OSSL_PARAM_construct_end();
1275 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1276 t->err = "MAC_CREATE_ERROR";
1280 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1281 t->err = "MAC_BAD_PARAMS";
1284 if (!EVP_MAC_init(ctx)) {
1285 t->err = "MAC_INIT_ERROR";
1288 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1289 t->err = "MAC_UPDATE_ERROR";
1292 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1293 t->err = "MAC_FINAL_LENGTH_ERROR";
1296 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1297 t->err = "TEST_FAILURE";
1300 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1301 || !memory_err_compare(t, "TEST_MAC_ERR",
1302 expected->output, expected->output_len,
1304 t->err = "TEST_MAC_ERR";
1309 while (params_n-- > params_n_allocstart) {
1310 OPENSSL_free(params[params_n].data);
1312 EVP_MAC_CTX_free(ctx);
1317 static int mac_test_run(EVP_TEST *t)
1319 MAC_DATA *expected = t->data;
1321 if (expected->mac != NULL)
1322 return mac_test_run_mac(t);
1323 return mac_test_run_pkey(t);
1326 static const EVP_TEST_METHOD mac_test_method = {
1336 *** PUBLIC KEY TESTS
1337 *** These are all very similar and share much common code.
1340 typedef struct pkey_data_st {
1341 /* Context for this operation */
1343 /* Key operation to perform */
1344 int (*keyop) (EVP_PKEY_CTX *ctx,
1345 unsigned char *sig, size_t *siglen,
1346 const unsigned char *tbs, size_t tbslen);
1348 unsigned char *input;
1350 /* Expected output */
1351 unsigned char *output;
1356 * Perform public key operation setup: lookup key, allocated ctx and call
1357 * the appropriate initialisation function
1359 static int pkey_test_init(EVP_TEST *t, const char *name,
1361 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1362 int (*keyop)(EVP_PKEY_CTX *ctx,
1363 unsigned char *sig, size_t *siglen,
1364 const unsigned char *tbs,
1368 EVP_PKEY *pkey = NULL;
1372 rv = find_key(&pkey, name, public_keys);
1374 rv = find_key(&pkey, name, private_keys);
1375 if (rv == 0 || pkey == NULL) {
1380 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1381 EVP_PKEY_free(pkey);
1384 kdata->keyop = keyop;
1385 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
1386 EVP_PKEY_free(pkey);
1387 OPENSSL_free(kdata);
1390 if (keyopinit(kdata->ctx) <= 0)
1391 t->err = "KEYOP_INIT_ERROR";
1396 static void pkey_test_cleanup(EVP_TEST *t)
1398 PKEY_DATA *kdata = t->data;
1400 OPENSSL_free(kdata->input);
1401 OPENSSL_free(kdata->output);
1402 EVP_PKEY_CTX_free(kdata->ctx);
1405 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1411 if (!TEST_ptr(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);
1424 if (nid == NID_undef)
1425 nid = OBJ_ln2nid(p);
1426 if (nid != NID_undef
1427 && EVP_get_digestbynid(nid) == NULL
1428 && EVP_get_cipherbynid(nid) == NULL) {
1432 t->err = "PKEY_CTRL_ERROR";
1436 OPENSSL_free(tmpval);
1440 static int pkey_test_parse(EVP_TEST *t,
1441 const char *keyword, const char *value)
1443 PKEY_DATA *kdata = t->data;
1444 if (strcmp(keyword, "Input") == 0)
1445 return parse_bin(value, &kdata->input, &kdata->input_len);
1446 if (strcmp(keyword, "Output") == 0)
1447 return parse_bin(value, &kdata->output, &kdata->output_len);
1448 if (strcmp(keyword, "Ctrl") == 0)
1449 return pkey_test_ctrl(t, kdata->ctx, value);
1453 static int pkey_test_run(EVP_TEST *t)
1455 PKEY_DATA *expected = t->data;
1456 unsigned char *got = NULL;
1458 EVP_PKEY_CTX *copy = NULL;
1460 if (expected->keyop(expected->ctx, NULL, &got_len,
1461 expected->input, expected->input_len) <= 0
1462 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1463 t->err = "KEYOP_LENGTH_ERROR";
1466 if (expected->keyop(expected->ctx, got, &got_len,
1467 expected->input, expected->input_len) <= 0) {
1468 t->err = "KEYOP_ERROR";
1471 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1472 expected->output, expected->output_len,
1480 /* Repeat the test on a copy. */
1481 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1482 t->err = "INTERNAL_ERROR";
1485 if (expected->keyop(copy, NULL, &got_len, expected->input,
1486 expected->input_len) <= 0
1487 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1488 t->err = "KEYOP_LENGTH_ERROR";
1491 if (expected->keyop(copy, got, &got_len, expected->input,
1492 expected->input_len) <= 0) {
1493 t->err = "KEYOP_ERROR";
1496 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1497 expected->output, expected->output_len,
1503 EVP_PKEY_CTX_free(copy);
1507 static int sign_test_init(EVP_TEST *t, const char *name)
1509 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1512 static const EVP_TEST_METHOD psign_test_method = {
1520 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1522 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1523 EVP_PKEY_verify_recover);
1526 static const EVP_TEST_METHOD pverify_recover_test_method = {
1528 verify_recover_test_init,
1534 static int decrypt_test_init(EVP_TEST *t, const char *name)
1536 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1540 static const EVP_TEST_METHOD pdecrypt_test_method = {
1548 static int verify_test_init(EVP_TEST *t, const char *name)
1550 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1553 static int verify_test_run(EVP_TEST *t)
1555 PKEY_DATA *kdata = t->data;
1557 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1558 kdata->input, kdata->input_len) <= 0)
1559 t->err = "VERIFY_ERROR";
1563 static const EVP_TEST_METHOD pverify_test_method = {
1572 static int pderive_test_init(EVP_TEST *t, const char *name)
1574 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1577 static int pderive_test_parse(EVP_TEST *t,
1578 const char *keyword, const char *value)
1580 PKEY_DATA *kdata = t->data;
1582 if (strcmp(keyword, "PeerKey") == 0) {
1584 if (find_key(&peer, value, public_keys) == 0)
1586 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1590 if (strcmp(keyword, "SharedSecret") == 0)
1591 return parse_bin(value, &kdata->output, &kdata->output_len);
1592 if (strcmp(keyword, "Ctrl") == 0)
1593 return pkey_test_ctrl(t, kdata->ctx, value);
1597 static int pderive_test_run(EVP_TEST *t)
1599 PKEY_DATA *expected = t->data;
1600 unsigned char *got = NULL;
1603 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1604 t->err = "DERIVE_ERROR";
1607 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1608 t->err = "DERIVE_ERROR";
1611 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1612 t->err = "DERIVE_ERROR";
1615 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1616 expected->output, expected->output_len,
1626 static const EVP_TEST_METHOD pderive_test_method = {
1639 typedef enum pbe_type_enum {
1640 PBE_TYPE_INVALID = 0,
1641 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1644 typedef struct pbe_data_st {
1646 /* scrypt parameters */
1647 uint64_t N, r, p, maxmem;
1648 /* PKCS#12 parameters */
1652 unsigned char *pass;
1655 unsigned char *salt;
1657 /* Expected output */
1662 #ifndef OPENSSL_NO_SCRYPT
1664 * Parse unsigned decimal 64 bit integer value
1666 static int parse_uint64(const char *value, uint64_t *pr)
1668 const char *p = value;
1670 if (!TEST_true(*p)) {
1671 TEST_info("Invalid empty integer value");
1674 for (*pr = 0; *p; ) {
1675 if (*pr > UINT64_MAX / 10) {
1676 TEST_error("Integer overflow in string %s", value);
1680 if (!TEST_true(isdigit((unsigned char)*p))) {
1681 TEST_error("Invalid character in string %s", value);
1690 static int scrypt_test_parse(EVP_TEST *t,
1691 const char *keyword, const char *value)
1693 PBE_DATA *pdata = t->data;
1695 if (strcmp(keyword, "N") == 0)
1696 return parse_uint64(value, &pdata->N);
1697 if (strcmp(keyword, "p") == 0)
1698 return parse_uint64(value, &pdata->p);
1699 if (strcmp(keyword, "r") == 0)
1700 return parse_uint64(value, &pdata->r);
1701 if (strcmp(keyword, "maxmem") == 0)
1702 return parse_uint64(value, &pdata->maxmem);
1707 static int pbkdf2_test_parse(EVP_TEST *t,
1708 const char *keyword, const char *value)
1710 PBE_DATA *pdata = t->data;
1712 if (strcmp(keyword, "iter") == 0) {
1713 pdata->iter = atoi(value);
1714 if (pdata->iter <= 0)
1718 if (strcmp(keyword, "MD") == 0) {
1719 pdata->md = EVP_get_digestbyname(value);
1720 if (pdata->md == NULL)
1727 static int pkcs12_test_parse(EVP_TEST *t,
1728 const char *keyword, const char *value)
1730 PBE_DATA *pdata = t->data;
1732 if (strcmp(keyword, "id") == 0) {
1733 pdata->id = atoi(value);
1738 return pbkdf2_test_parse(t, keyword, value);
1741 static int pbe_test_init(EVP_TEST *t, const char *alg)
1744 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1746 if (strcmp(alg, "scrypt") == 0) {
1747 #ifndef OPENSSL_NO_SCRYPT
1748 pbe_type = PBE_TYPE_SCRYPT;
1753 } else if (strcmp(alg, "pbkdf2") == 0) {
1754 pbe_type = PBE_TYPE_PBKDF2;
1755 } else if (strcmp(alg, "pkcs12") == 0) {
1756 pbe_type = PBE_TYPE_PKCS12;
1758 TEST_error("Unknown pbe algorithm %s", alg);
1760 pdat = OPENSSL_zalloc(sizeof(*pdat));
1761 pdat->pbe_type = pbe_type;
1766 static void pbe_test_cleanup(EVP_TEST *t)
1768 PBE_DATA *pdat = t->data;
1770 OPENSSL_free(pdat->pass);
1771 OPENSSL_free(pdat->salt);
1772 OPENSSL_free(pdat->key);
1775 static int pbe_test_parse(EVP_TEST *t,
1776 const char *keyword, const char *value)
1778 PBE_DATA *pdata = t->data;
1780 if (strcmp(keyword, "Password") == 0)
1781 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1782 if (strcmp(keyword, "Salt") == 0)
1783 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1784 if (strcmp(keyword, "Key") == 0)
1785 return parse_bin(value, &pdata->key, &pdata->key_len);
1786 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1787 return pbkdf2_test_parse(t, keyword, value);
1788 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1789 return pkcs12_test_parse(t, keyword, value);
1790 #ifndef OPENSSL_NO_SCRYPT
1791 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1792 return scrypt_test_parse(t, keyword, value);
1797 static int pbe_test_run(EVP_TEST *t)
1799 PBE_DATA *expected = t->data;
1802 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1803 t->err = "INTERNAL_ERROR";
1806 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1807 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1808 expected->salt, expected->salt_len,
1809 expected->iter, expected->md,
1810 expected->key_len, key) == 0) {
1811 t->err = "PBKDF2_ERROR";
1814 #ifndef OPENSSL_NO_SCRYPT
1815 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1816 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1817 expected->salt, expected->salt_len, expected->N,
1818 expected->r, expected->p, expected->maxmem,
1819 key, expected->key_len) == 0) {
1820 t->err = "SCRYPT_ERROR";
1824 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1825 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1826 expected->salt, expected->salt_len,
1827 expected->id, expected->iter, expected->key_len,
1828 key, expected->md) == 0) {
1829 t->err = "PKCS12_ERROR";
1833 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1834 key, expected->key_len))
1843 static const EVP_TEST_METHOD pbe_test_method = {
1857 BASE64_CANONICAL_ENCODING = 0,
1858 BASE64_VALID_ENCODING = 1,
1859 BASE64_INVALID_ENCODING = 2
1860 } base64_encoding_type;
1862 typedef struct encode_data_st {
1863 /* Input to encoding */
1864 unsigned char *input;
1866 /* Expected output */
1867 unsigned char *output;
1869 base64_encoding_type encoding;
1872 static int encode_test_init(EVP_TEST *t, const char *encoding)
1876 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1878 if (strcmp(encoding, "canonical") == 0) {
1879 edata->encoding = BASE64_CANONICAL_ENCODING;
1880 } else if (strcmp(encoding, "valid") == 0) {
1881 edata->encoding = BASE64_VALID_ENCODING;
1882 } else if (strcmp(encoding, "invalid") == 0) {
1883 edata->encoding = BASE64_INVALID_ENCODING;
1884 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1887 TEST_error("Bad encoding: %s."
1888 " Should be one of {canonical, valid, invalid}",
1895 OPENSSL_free(edata);
1899 static void encode_test_cleanup(EVP_TEST *t)
1901 ENCODE_DATA *edata = t->data;
1903 OPENSSL_free(edata->input);
1904 OPENSSL_free(edata->output);
1905 memset(edata, 0, sizeof(*edata));
1908 static int encode_test_parse(EVP_TEST *t,
1909 const char *keyword, const char *value)
1911 ENCODE_DATA *edata = t->data;
1913 if (strcmp(keyword, "Input") == 0)
1914 return parse_bin(value, &edata->input, &edata->input_len);
1915 if (strcmp(keyword, "Output") == 0)
1916 return parse_bin(value, &edata->output, &edata->output_len);
1920 static int encode_test_run(EVP_TEST *t)
1922 ENCODE_DATA *expected = t->data;
1923 unsigned char *encode_out = NULL, *decode_out = NULL;
1924 int output_len, chunk_len;
1925 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1927 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1928 t->err = "INTERNAL_ERROR";
1932 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1934 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1935 || !TEST_ptr(encode_out =
1936 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1939 EVP_EncodeInit(encode_ctx);
1940 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1941 expected->input, expected->input_len)))
1944 output_len = chunk_len;
1946 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1947 output_len += chunk_len;
1949 if (!memory_err_compare(t, "BAD_ENCODING",
1950 expected->output, expected->output_len,
1951 encode_out, output_len))
1955 if (!TEST_ptr(decode_out =
1956 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
1959 EVP_DecodeInit(decode_ctx);
1960 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
1961 expected->output_len) < 0) {
1962 t->err = "DECODE_ERROR";
1965 output_len = chunk_len;
1967 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1968 t->err = "DECODE_ERROR";
1971 output_len += chunk_len;
1973 if (expected->encoding != BASE64_INVALID_ENCODING
1974 && !memory_err_compare(t, "BAD_DECODING",
1975 expected->input, expected->input_len,
1976 decode_out, output_len)) {
1977 t->err = "BAD_DECODING";
1983 OPENSSL_free(encode_out);
1984 OPENSSL_free(decode_out);
1985 EVP_ENCODE_CTX_free(decode_ctx);
1986 EVP_ENCODE_CTX_free(encode_ctx);
1990 static const EVP_TEST_METHOD encode_test_method = {
1993 encode_test_cleanup,
2003 typedef struct kdf_data_st {
2004 /* Context for this operation */
2006 /* Expected output */
2007 unsigned char *output;
2009 OSSL_PARAM params[20];
2014 * Perform public key operation setup: lookup key, allocated ctx and call
2015 * the appropriate initialisation function
2017 static int kdf_test_init(EVP_TEST *t, const char *name)
2022 #ifdef OPENSSL_NO_SCRYPT
2023 /* TODO(3.0) Replace with "scrypt" once aliases are supported */
2024 if (strcmp(name, "id-scrypt") == 0) {
2028 #endif /* OPENSSL_NO_SCRYPT */
2030 #ifdef OPENSSL_NO_CMS
2031 if (strcmp(name, "X942KDF") == 0) {
2035 #endif /* OPENSSL_NO_CMS */
2037 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2039 kdata->p = kdata->params;
2040 *kdata->p = OSSL_PARAM_construct_end();
2042 kdf = EVP_KDF_fetch(NULL, name, NULL);
2044 OPENSSL_free(kdata);
2047 kdata->ctx = EVP_KDF_CTX_new(kdf);
2049 if (kdata->ctx == NULL) {
2050 OPENSSL_free(kdata);
2057 static void kdf_test_cleanup(EVP_TEST *t)
2059 KDF_DATA *kdata = t->data;
2062 for (p = kdata->params; p->key != NULL; p++)
2063 OPENSSL_free(p->data);
2064 OPENSSL_free(kdata->output);
2065 EVP_KDF_CTX_free(kdata->ctx);
2068 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2071 KDF_DATA *kdata = t->data;
2074 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2076 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2078 p = strchr(name, ':');
2082 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2083 p != NULL ? strlen(p) : 0);
2084 *++kdata->p = OSSL_PARAM_construct_end();
2086 t->err = "KDF_PARAM_ERROR";
2090 if (p != NULL && strcmp(name, "digest") == 0) {
2091 /* If p has an OID and lookup fails assume disabled algorithm */
2092 int nid = OBJ_sn2nid(p);
2094 if (nid == NID_undef)
2095 nid = OBJ_ln2nid(p);
2096 if (nid != NID_undef && EVP_get_digestbynid(nid) == NULL)
2103 static int kdf_test_parse(EVP_TEST *t,
2104 const char *keyword, const char *value)
2106 KDF_DATA *kdata = t->data;
2108 if (strcmp(keyword, "Output") == 0)
2109 return parse_bin(value, &kdata->output, &kdata->output_len);
2110 if (strncmp(keyword, "Ctrl", 4) == 0)
2111 return kdf_test_ctrl(t, kdata->ctx, value);
2115 static int kdf_test_run(EVP_TEST *t)
2117 KDF_DATA *expected = t->data;
2118 unsigned char *got = NULL;
2119 size_t got_len = expected->output_len;
2121 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2122 t->err = "KDF_CTRL_ERROR";
2125 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2126 t->err = "INTERNAL_ERROR";
2129 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2130 t->err = "KDF_DERIVE_ERROR";
2133 if (!memory_err_compare(t, "KDF_MISMATCH",
2134 expected->output, expected->output_len,
2145 static const EVP_TEST_METHOD kdf_test_method = {
2158 typedef struct pkey_kdf_data_st {
2159 /* Context for this operation */
2161 /* Expected output */
2162 unsigned char *output;
2167 * Perform public key operation setup: lookup key, allocated ctx and call
2168 * the appropriate initialisation function
2170 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2172 PKEY_KDF_DATA *kdata;
2173 int kdf_nid = OBJ_sn2nid(name);
2175 #ifdef OPENSSL_NO_SCRYPT
2176 if (strcmp(name, "scrypt") == 0) {
2180 #endif /* OPENSSL_NO_SCRYPT */
2182 #ifdef OPENSSL_NO_CMS
2183 if (strcmp(name, "X942KDF") == 0) {
2187 #endif /* OPENSSL_NO_CMS */
2189 if (kdf_nid == NID_undef)
2190 kdf_nid = OBJ_ln2nid(name);
2192 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2194 kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
2195 if (kdata->ctx == NULL) {
2196 OPENSSL_free(kdata);
2199 if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
2200 EVP_PKEY_CTX_free(kdata->ctx);
2201 OPENSSL_free(kdata);
2208 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2210 PKEY_KDF_DATA *kdata = t->data;
2212 OPENSSL_free(kdata->output);
2213 EVP_PKEY_CTX_free(kdata->ctx);
2216 static int pkey_kdf_test_parse(EVP_TEST *t,
2217 const char *keyword, const char *value)
2219 PKEY_KDF_DATA *kdata = t->data;
2221 if (strcmp(keyword, "Output") == 0)
2222 return parse_bin(value, &kdata->output, &kdata->output_len);
2223 if (strncmp(keyword, "Ctrl", 4) == 0)
2224 return pkey_test_ctrl(t, kdata->ctx, value);
2228 static int pkey_kdf_test_run(EVP_TEST *t)
2230 PKEY_KDF_DATA *expected = t->data;
2231 unsigned char *got = NULL;
2232 size_t got_len = expected->output_len;
2234 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2235 t->err = "INTERNAL_ERROR";
2238 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2239 t->err = "KDF_DERIVE_ERROR";
2242 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2243 t->err = "KDF_MISMATCH";
2253 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2256 pkey_kdf_test_cleanup,
2257 pkey_kdf_test_parse,
2266 typedef struct keypair_test_data_st {
2269 } KEYPAIR_TEST_DATA;
2271 static int keypair_test_init(EVP_TEST *t, const char *pair)
2273 KEYPAIR_TEST_DATA *data;
2275 EVP_PKEY *pk = NULL, *pubk = NULL;
2276 char *pub, *priv = NULL;
2278 /* Split private and public names. */
2279 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2280 || !TEST_ptr(pub = strchr(priv, ':'))) {
2281 t->err = "PARSING_ERROR";
2286 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2287 TEST_info("Can't find private key: %s", priv);
2288 t->err = "MISSING_PRIVATE_KEY";
2291 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2292 TEST_info("Can't find public key: %s", pub);
2293 t->err = "MISSING_PUBLIC_KEY";
2297 if (pk == NULL && pubk == NULL) {
2298 /* Both keys are listed but unsupported: skip this test */
2304 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2317 static void keypair_test_cleanup(EVP_TEST *t)
2319 OPENSSL_free(t->data);
2324 * For tests that do not accept any custom keywords.
2326 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2331 static int keypair_test_run(EVP_TEST *t)
2334 const KEYPAIR_TEST_DATA *pair = t->data;
2336 if (pair->privk == NULL || pair->pubk == NULL) {
2338 * this can only happen if only one of the keys is not set
2339 * which means that one of them was unsupported while the
2340 * other isn't: hence a key type mismatch.
2342 t->err = "KEYPAIR_TYPE_MISMATCH";
2347 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2349 t->err = "KEYPAIR_MISMATCH";
2350 } else if ( -1 == rv ) {
2351 t->err = "KEYPAIR_TYPE_MISMATCH";
2352 } else if ( -2 == rv ) {
2353 t->err = "UNSUPPORTED_KEY_COMPARISON";
2355 TEST_error("Unexpected error in key comparison");
2370 static const EVP_TEST_METHOD keypair_test_method = {
2373 keypair_test_cleanup,
2382 typedef struct keygen_test_data_st {
2383 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2384 char *keyname; /* Key name to store key or NULL */
2387 static int keygen_test_init(EVP_TEST *t, const char *alg)
2389 KEYGEN_TEST_DATA *data;
2390 EVP_PKEY_CTX *genctx;
2391 int nid = OBJ_sn2nid(alg);
2393 if (nid == NID_undef) {
2394 nid = OBJ_ln2nid(alg);
2395 if (nid == NID_undef)
2399 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
2400 /* assume algorithm disabled */
2405 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2406 t->err = "KEYGEN_INIT_ERROR";
2410 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2412 data->genctx = genctx;
2413 data->keyname = NULL;
2419 EVP_PKEY_CTX_free(genctx);
2423 static void keygen_test_cleanup(EVP_TEST *t)
2425 KEYGEN_TEST_DATA *keygen = t->data;
2427 EVP_PKEY_CTX_free(keygen->genctx);
2428 OPENSSL_free(keygen->keyname);
2429 OPENSSL_free(t->data);
2433 static int keygen_test_parse(EVP_TEST *t,
2434 const char *keyword, const char *value)
2436 KEYGEN_TEST_DATA *keygen = t->data;
2438 if (strcmp(keyword, "KeyName") == 0)
2439 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2440 if (strcmp(keyword, "Ctrl") == 0)
2441 return pkey_test_ctrl(t, keygen->genctx, value);
2445 static int keygen_test_run(EVP_TEST *t)
2447 KEYGEN_TEST_DATA *keygen = t->data;
2448 EVP_PKEY *pkey = NULL;
2451 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2452 t->err = "KEYGEN_GENERATE_ERROR";
2456 if (keygen->keyname != NULL) {
2459 if (find_key(NULL, keygen->keyname, private_keys)) {
2460 TEST_info("Duplicate key %s", keygen->keyname);
2464 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2466 key->name = keygen->keyname;
2467 keygen->keyname = NULL;
2469 key->next = private_keys;
2472 EVP_PKEY_free(pkey);
2478 EVP_PKEY_free(pkey);
2482 static const EVP_TEST_METHOD keygen_test_method = {
2485 keygen_test_cleanup,
2491 *** DIGEST SIGN+VERIFY TESTS
2495 int is_verify; /* Set to 1 if verifying */
2496 int is_oneshot; /* Set to 1 for one shot operation */
2497 const EVP_MD *md; /* Digest to use */
2498 EVP_MD_CTX *ctx; /* Digest context */
2500 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2501 unsigned char *osin; /* Input data if one shot */
2502 size_t osin_len; /* Input length data if one shot */
2503 unsigned char *output; /* Expected output */
2504 size_t output_len; /* Expected output length */
2507 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2510 const EVP_MD *md = NULL;
2511 DIGESTSIGN_DATA *mdat;
2513 if (strcmp(alg, "NULL") != 0) {
2514 if ((md = EVP_get_digestbyname(alg)) == NULL) {
2515 /* If alg has an OID assume disabled algorithm */
2516 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
2523 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2526 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2530 mdat->is_verify = is_verify;
2531 mdat->is_oneshot = is_oneshot;
2536 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2538 return digestsigver_test_init(t, alg, 0, 0);
2541 static void digestsigver_test_cleanup(EVP_TEST *t)
2543 DIGESTSIGN_DATA *mdata = t->data;
2545 EVP_MD_CTX_free(mdata->ctx);
2546 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2547 OPENSSL_free(mdata->osin);
2548 OPENSSL_free(mdata->output);
2549 OPENSSL_free(mdata);
2553 static int digestsigver_test_parse(EVP_TEST *t,
2554 const char *keyword, const char *value)
2556 DIGESTSIGN_DATA *mdata = t->data;
2558 if (strcmp(keyword, "Key") == 0) {
2559 EVP_PKEY *pkey = NULL;
2562 if (mdata->is_verify)
2563 rv = find_key(&pkey, value, public_keys);
2565 rv = find_key(&pkey, value, private_keys);
2566 if (rv == 0 || pkey == NULL) {
2570 if (mdata->is_verify) {
2571 if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
2573 t->err = "DIGESTVERIFYINIT_ERROR";
2576 if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
2578 t->err = "DIGESTSIGNINIT_ERROR";
2582 if (strcmp(keyword, "Input") == 0) {
2583 if (mdata->is_oneshot)
2584 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2585 return evp_test_buffer_append(value, &mdata->input);
2587 if (strcmp(keyword, "Output") == 0)
2588 return parse_bin(value, &mdata->output, &mdata->output_len);
2590 if (!mdata->is_oneshot) {
2591 if (strcmp(keyword, "Count") == 0)
2592 return evp_test_buffer_set_count(value, mdata->input);
2593 if (strcmp(keyword, "Ncopy") == 0)
2594 return evp_test_buffer_ncopy(value, mdata->input);
2596 if (strcmp(keyword, "Ctrl") == 0) {
2597 if (mdata->pctx == NULL)
2599 return pkey_test_ctrl(t, mdata->pctx, value);
2604 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2607 return EVP_DigestSignUpdate(ctx, buf, buflen);
2610 static int digestsign_test_run(EVP_TEST *t)
2612 DIGESTSIGN_DATA *expected = t->data;
2613 unsigned char *got = NULL;
2616 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2618 t->err = "DIGESTUPDATE_ERROR";
2622 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2623 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2626 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2627 t->err = "MALLOC_FAILURE";
2630 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2631 t->err = "DIGESTSIGNFINAL_ERROR";
2634 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2635 expected->output, expected->output_len,
2645 static const EVP_TEST_METHOD digestsign_test_method = {
2647 digestsign_test_init,
2648 digestsigver_test_cleanup,
2649 digestsigver_test_parse,
2653 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2655 return digestsigver_test_init(t, alg, 1, 0);
2658 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2661 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2664 static int digestverify_test_run(EVP_TEST *t)
2666 DIGESTSIGN_DATA *mdata = t->data;
2668 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2669 t->err = "DIGESTUPDATE_ERROR";
2673 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2674 mdata->output_len) <= 0)
2675 t->err = "VERIFY_ERROR";
2679 static const EVP_TEST_METHOD digestverify_test_method = {
2681 digestverify_test_init,
2682 digestsigver_test_cleanup,
2683 digestsigver_test_parse,
2684 digestverify_test_run
2687 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
2689 return digestsigver_test_init(t, alg, 0, 1);
2692 static int oneshot_digestsign_test_run(EVP_TEST *t)
2694 DIGESTSIGN_DATA *expected = t->data;
2695 unsigned char *got = NULL;
2698 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
2699 expected->osin, expected->osin_len)) {
2700 t->err = "DIGESTSIGN_LENGTH_ERROR";
2703 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2704 t->err = "MALLOC_FAILURE";
2707 if (!EVP_DigestSign(expected->ctx, got, &got_len,
2708 expected->osin, expected->osin_len)) {
2709 t->err = "DIGESTSIGN_ERROR";
2712 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2713 expected->output, expected->output_len,
2723 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
2724 "OneShotDigestSign",
2725 oneshot_digestsign_test_init,
2726 digestsigver_test_cleanup,
2727 digestsigver_test_parse,
2728 oneshot_digestsign_test_run
2731 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
2733 return digestsigver_test_init(t, alg, 1, 1);
2736 static int oneshot_digestverify_test_run(EVP_TEST *t)
2738 DIGESTSIGN_DATA *mdata = t->data;
2740 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
2741 mdata->osin, mdata->osin_len) <= 0)
2742 t->err = "VERIFY_ERROR";
2746 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
2747 "OneShotDigestVerify",
2748 oneshot_digestverify_test_init,
2749 digestsigver_test_cleanup,
2750 digestsigver_test_parse,
2751 oneshot_digestverify_test_run
2756 *** PARSING AND DISPATCH
2759 static const EVP_TEST_METHOD *evp_test_list[] = {
2760 &cipher_test_method,
2761 &digest_test_method,
2762 &digestsign_test_method,
2763 &digestverify_test_method,
2764 &encode_test_method,
2766 &pkey_kdf_test_method,
2767 &keypair_test_method,
2768 &keygen_test_method,
2770 &oneshot_digestsign_test_method,
2771 &oneshot_digestverify_test_method,
2773 &pdecrypt_test_method,
2774 &pderive_test_method,
2776 &pverify_recover_test_method,
2777 &pverify_test_method,
2781 static const EVP_TEST_METHOD *find_test(const char *name)
2783 const EVP_TEST_METHOD **tt;
2785 for (tt = evp_test_list; *tt; tt++) {
2786 if (strcmp(name, (*tt)->name) == 0)
2792 static void clear_test(EVP_TEST *t)
2794 test_clearstanza(&t->s);
2796 if (t->data != NULL) {
2797 if (t->meth != NULL)
2798 t->meth->cleanup(t);
2799 OPENSSL_free(t->data);
2802 OPENSSL_free(t->expected_err);
2803 t->expected_err = NULL;
2804 OPENSSL_free(t->reason);
2814 * Check for errors in the test structure; return 1 if okay, else 0.
2816 static int check_test_error(EVP_TEST *t)
2821 if (t->err == NULL && t->expected_err == NULL)
2823 if (t->err != NULL && t->expected_err == NULL) {
2824 if (t->aux_err != NULL) {
2825 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2826 t->s.test_file, t->s.start, t->aux_err, t->err);
2828 TEST_info("%s:%d: Source of above error; unexpected error %s",
2829 t->s.test_file, t->s.start, t->err);
2833 if (t->err == NULL && t->expected_err != NULL) {
2834 TEST_info("%s:%d: Succeeded but was expecting %s",
2835 t->s.test_file, t->s.start, t->expected_err);
2839 if (strcmp(t->err, t->expected_err) != 0) {
2840 TEST_info("%s:%d: Expected %s got %s",
2841 t->s.test_file, t->s.start, t->expected_err, t->err);
2845 if (t->reason == NULL)
2848 if (t->reason == NULL) {
2849 TEST_info("%s:%d: Test is missing function or reason code",
2850 t->s.test_file, t->s.start);
2854 err = ERR_peek_error();
2856 TEST_info("%s:%d: Expected error \"%s\" not set",
2857 t->s.test_file, t->s.start, t->reason);
2861 reason = ERR_reason_error_string(err);
2862 if (reason == NULL) {
2863 TEST_info("%s:%d: Expected error \"%s\", no strings available."
2865 t->s.test_file, t->s.start, t->reason);
2869 if (strcmp(reason, t->reason) == 0)
2872 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
2873 t->s.test_file, t->s.start, t->reason, reason);
2879 * Run a parsed test. Log a message and return 0 on error.
2881 static int run_test(EVP_TEST *t)
2883 if (t->meth == NULL)
2890 if (t->err == NULL && t->meth->run_test(t) != 1) {
2891 TEST_info("%s:%d %s error",
2892 t->s.test_file, t->s.start, t->meth->name);
2895 if (!check_test_error(t)) {
2896 TEST_openssl_errors();
2905 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
2907 for (; lst != NULL; lst = lst->next) {
2908 if (strcmp(lst->name, name) == 0) {
2917 static void free_key_list(KEY_LIST *lst)
2919 while (lst != NULL) {
2920 KEY_LIST *next = lst->next;
2922 EVP_PKEY_free(lst->key);
2923 OPENSSL_free(lst->name);
2930 * Is the key type an unsupported algorithm?
2932 static int key_unsupported(void)
2934 long err = ERR_peek_error();
2936 if (ERR_GET_LIB(err) == ERR_LIB_EVP
2937 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
2941 #ifndef OPENSSL_NO_EC
2943 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2944 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2947 if (ERR_GET_LIB(err) == ERR_LIB_EC
2948 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
2952 #endif /* OPENSSL_NO_EC */
2957 * NULL out the value from |pp| but return it. This "steals" a pointer.
2959 static char *take_value(PAIR *pp)
2961 char *p = pp->value;
2968 * Return 1 if one of the providers named in the string is available.
2969 * The provider names are separated with whitespace.
2970 * NOTE: destructive function, it inserts '\0' after each provider name.
2972 static int prov_available(char *providers)
2978 for (; isspace(*providers); providers++)
2980 if (*providers == '\0')
2981 break; /* End of the road */
2982 for (p = providers; *p != '\0' && !isspace(*p); p++)
2988 if (OSSL_PROVIDER_available(NULL, providers))
2989 return 1; /* Found one */
2995 * Read and parse one test. Return 0 if failure, 1 if okay.
2997 static int parse(EVP_TEST *t)
2999 KEY_LIST *key, **klist;
3006 if (BIO_eof(t->s.fp))
3009 if (!test_readstanza(&t->s))
3011 } while (t->s.numpairs == 0);
3012 pp = &t->s.pairs[0];
3014 /* Are we adding a key? */
3017 if (strcmp(pp->key, "PrivateKey") == 0) {
3018 pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
3019 if (pkey == NULL && !key_unsupported()) {
3020 EVP_PKEY_free(pkey);
3021 TEST_info("Can't read private key %s", pp->value);
3022 TEST_openssl_errors();
3025 klist = &private_keys;
3026 } else if (strcmp(pp->key, "PublicKey") == 0) {
3027 pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
3028 if (pkey == NULL && !key_unsupported()) {
3029 EVP_PKEY_free(pkey);
3030 TEST_info("Can't read public key %s", pp->value);
3031 TEST_openssl_errors();
3034 klist = &public_keys;
3035 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3036 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3037 char *strnid = NULL, *keydata = NULL;
3038 unsigned char *keybin;
3042 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3043 klist = &private_keys;
3045 klist = &public_keys;
3047 strnid = strchr(pp->value, ':');
3048 if (strnid != NULL) {
3050 keydata = strchr(strnid, ':');
3051 if (keydata != NULL)
3054 if (keydata == NULL) {
3055 TEST_info("Failed to parse %s value", pp->key);
3059 nid = OBJ_txt2nid(strnid);
3060 if (nid == NID_undef) {
3061 TEST_info("Uncrecognised algorithm NID");
3064 if (!parse_bin(keydata, &keybin, &keylen)) {
3065 TEST_info("Failed to create binary key");
3068 if (klist == &private_keys)
3069 pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
3071 pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
3072 if (pkey == NULL && !key_unsupported()) {
3073 TEST_info("Can't read %s data", pp->key);
3074 OPENSSL_free(keybin);
3075 TEST_openssl_errors();
3078 OPENSSL_free(keybin);
3081 /* If we have a key add to list */
3082 if (klist != NULL) {
3083 if (find_key(NULL, pp->value, *klist)) {
3084 TEST_info("Duplicate key %s", pp->value);
3087 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3089 key->name = take_value(pp);
3091 /* Hack to detect SM2 keys */
3092 if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
3093 #ifdef OPENSSL_NO_SM2
3094 EVP_PKEY_free(pkey);
3097 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
3105 /* Go back and start a new stanza. */
3106 if (t->s.numpairs != 1)
3107 TEST_info("Line %d: missing blank line\n", t->s.curr);
3111 /* Find the test, based on first keyword. */
3112 if (!TEST_ptr(t->meth = find_test(pp->key)))
3114 if (!t->meth->init(t, pp->value)) {
3115 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3119 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3123 for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
3124 if (strcmp(pp->key, "Availablein") == 0) {
3125 if (!prov_available(pp->value)) {
3126 TEST_info("skipping, providers not available: %s:%d",
3127 t->s.test_file, t->s.start);
3131 } else if (strcmp(pp->key, "Result") == 0) {
3132 if (t->expected_err != NULL) {
3133 TEST_info("Line %d: multiple result lines", t->s.curr);
3136 t->expected_err = take_value(pp);
3137 } else if (strcmp(pp->key, "Function") == 0) {
3138 /* Ignore old line. */
3139 } else if (strcmp(pp->key, "Reason") == 0) {
3140 if (t->reason != NULL) {
3141 TEST_info("Line %d: multiple reason lines", t->s.curr);
3144 t->reason = take_value(pp);
3146 /* Must be test specific line: try to parse it */
3147 int rv = t->meth->parse(t, pp->key, pp->value);
3150 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3154 TEST_info("Line %d: error processing keyword %s = %s\n",
3155 t->s.curr, pp->key, pp->value);
3164 static int run_file_tests(int i)
3167 const char *testfile = test_get_argument(i);
3170 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3172 if (!test_start_file(&t->s, testfile)) {
3177 while (!BIO_eof(t->s.fp)) {
3183 if (c == 0 || !run_test(t)) {
3188 test_end_file(&t->s);
3191 free_key_list(public_keys);
3192 free_key_list(private_keys);
3199 OPT_TEST_DECLARE_USAGE("file...\n")
3201 int setup_tests(void)
3203 size_t n = test_get_argument_count();
3208 ADD_ALL_TESTS(run_file_tests, n);