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 /* Input to digest */
329 STACK_OF(EVP_TEST_BUFFER) *input;
330 /* Expected output */
331 unsigned char *output;
335 static int digest_test_init(EVP_TEST *t, const char *alg)
338 const EVP_MD *digest;
340 if ((digest = EVP_get_digestbyname(alg)) == NULL) {
341 /* If alg has an OID assume disabled algorithm */
342 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
348 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
351 mdat->digest = digest;
355 static void digest_test_cleanup(EVP_TEST *t)
357 DIGEST_DATA *mdat = t->data;
359 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
360 OPENSSL_free(mdat->output);
363 static int digest_test_parse(EVP_TEST *t,
364 const char *keyword, const char *value)
366 DIGEST_DATA *mdata = t->data;
368 if (strcmp(keyword, "Input") == 0)
369 return evp_test_buffer_append(value, &mdata->input);
370 if (strcmp(keyword, "Output") == 0)
371 return parse_bin(value, &mdata->output, &mdata->output_len);
372 if (strcmp(keyword, "Count") == 0)
373 return evp_test_buffer_set_count(value, mdata->input);
374 if (strcmp(keyword, "Ncopy") == 0)
375 return evp_test_buffer_ncopy(value, mdata->input);
379 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
381 return EVP_DigestUpdate(ctx, buf, buflen);
384 static int digest_test_run(EVP_TEST *t)
386 DIGEST_DATA *expected = t->data;
388 unsigned char *got = NULL;
389 unsigned int got_len;
391 t->err = "TEST_FAILURE";
392 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
395 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
396 expected->output_len : EVP_MAX_MD_SIZE);
400 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
401 t->err = "DIGESTINIT_ERROR";
404 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
405 t->err = "DIGESTUPDATE_ERROR";
409 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
410 EVP_MD_CTX *mctx_cpy;
411 char dont[] = "touch";
413 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
416 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
417 EVP_MD_CTX_free(mctx_cpy);
420 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
421 EVP_MD_CTX_free(mctx_cpy);
422 t->err = "DIGESTFINALXOF_ERROR";
425 if (!TEST_str_eq(dont, "touch")) {
426 EVP_MD_CTX_free(mctx_cpy);
427 t->err = "DIGESTFINALXOF_ERROR";
430 EVP_MD_CTX_free(mctx_cpy);
432 got_len = expected->output_len;
433 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
434 t->err = "DIGESTFINALXOF_ERROR";
438 if (!EVP_DigestFinal(mctx, got, &got_len)) {
439 t->err = "DIGESTFINAL_ERROR";
443 if (!TEST_int_eq(expected->output_len, got_len)) {
444 t->err = "DIGEST_LENGTH_MISMATCH";
447 if (!memory_err_compare(t, "DIGEST_MISMATCH",
448 expected->output, expected->output_len,
456 EVP_MD_CTX_free(mctx);
460 static const EVP_TEST_METHOD digest_test_method = {
473 typedef struct cipher_data_st {
474 const EVP_CIPHER *cipher;
476 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
482 unsigned char *plaintext;
483 size_t plaintext_len;
484 unsigned char *ciphertext;
485 size_t ciphertext_len;
486 /* GCM, CCM, OCB and SIV only */
487 unsigned char *aad[AAD_NUM];
488 size_t aad_len[AAD_NUM];
494 static int cipher_test_init(EVP_TEST *t, const char *alg)
496 const EVP_CIPHER *cipher;
500 if ((cipher = EVP_get_cipherbyname(alg)) == NULL) {
501 /* If alg has an OID assume disabled algorithm */
502 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
508 cdat = OPENSSL_zalloc(sizeof(*cdat));
509 cdat->cipher = cipher;
511 m = EVP_CIPHER_mode(cipher);
512 if (m == EVP_CIPH_GCM_MODE
513 || m == EVP_CIPH_OCB_MODE
514 || m == EVP_CIPH_SIV_MODE
515 || m == EVP_CIPH_CCM_MODE)
517 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
526 static void cipher_test_cleanup(EVP_TEST *t)
529 CIPHER_DATA *cdat = t->data;
531 OPENSSL_free(cdat->key);
532 OPENSSL_free(cdat->iv);
533 OPENSSL_free(cdat->ciphertext);
534 OPENSSL_free(cdat->plaintext);
535 for (i = 0; i < AAD_NUM; i++)
536 OPENSSL_free(cdat->aad[i]);
537 OPENSSL_free(cdat->tag);
540 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
543 CIPHER_DATA *cdat = t->data;
546 if (strcmp(keyword, "Key") == 0)
547 return parse_bin(value, &cdat->key, &cdat->key_len);
548 if (strcmp(keyword, "IV") == 0)
549 return parse_bin(value, &cdat->iv, &cdat->iv_len);
550 if (strcmp(keyword, "Plaintext") == 0)
551 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
552 if (strcmp(keyword, "Ciphertext") == 0)
553 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
555 if (strcmp(keyword, "AAD") == 0) {
556 for (i = 0; i < AAD_NUM; i++) {
557 if (cdat->aad[i] == NULL)
558 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
562 if (strcmp(keyword, "Tag") == 0)
563 return parse_bin(value, &cdat->tag, &cdat->tag_len);
564 if (strcmp(keyword, "SetTagLate") == 0) {
565 if (strcmp(value, "TRUE") == 0)
567 else if (strcmp(value, "FALSE") == 0)
575 if (strcmp(keyword, "Operation") == 0) {
576 if (strcmp(value, "ENCRYPT") == 0)
578 else if (strcmp(value, "DECRYPT") == 0)
587 static int cipher_test_enc(EVP_TEST *t, int enc,
588 size_t out_misalign, size_t inp_misalign, int frag)
590 CIPHER_DATA *expected = t->data;
591 unsigned char *in, *expected_out, *tmp = NULL;
592 size_t in_len, out_len, donelen = 0;
593 int ok = 0, tmplen, chunklen, tmpflen, i;
594 EVP_CIPHER_CTX *ctx = NULL;
596 t->err = "TEST_FAILURE";
597 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
599 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
601 in = expected->plaintext;
602 in_len = expected->plaintext_len;
603 expected_out = expected->ciphertext;
604 out_len = expected->ciphertext_len;
606 in = expected->ciphertext;
607 in_len = expected->ciphertext_len;
608 expected_out = expected->plaintext;
609 out_len = expected->plaintext_len;
611 if (inp_misalign == (size_t)-1) {
613 * Exercise in-place encryption
615 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
618 in = memcpy(tmp + out_misalign, in, in_len);
620 inp_misalign += 16 - ((out_misalign + in_len) & 15);
622 * 'tmp' will store both output and copy of input. We make the copy
623 * of input to specifically aligned part of 'tmp'. So we just
624 * figured out how much padding would ensure the required alignment,
625 * now we allocate extended buffer and finally copy the input just
626 * past inp_misalign in expression below. Output will be written
627 * past out_misalign...
629 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
630 inp_misalign + in_len);
633 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
634 inp_misalign, in, in_len);
636 if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
637 t->err = "CIPHERINIT_ERROR";
641 if (expected->aead) {
642 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
643 expected->iv_len, 0)) {
644 t->err = "INVALID_IV_LENGTH";
647 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
648 t->err = "INVALID_IV_LENGTH";
652 if (expected->aead) {
655 * If encrypting or OCB just set tag length initially, otherwise
656 * set tag length and value.
658 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
659 t->err = "TAG_LENGTH_SET_ERROR";
662 t->err = "TAG_SET_ERROR";
665 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
666 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
667 expected->tag_len, tag))
672 if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
673 t->err = "INVALID_KEY_LENGTH";
676 if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
677 t->err = "KEY_SET_ERROR";
680 /* Check that we get the same IV back */
681 if (expected->iv != NULL
682 && (EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
683 && !TEST_mem_eq(expected->iv, expected->iv_len,
684 EVP_CIPHER_CTX_iv(ctx), expected->iv_len)) {
685 t->err = "INVALID_IV";
689 if (expected->aead == EVP_CIPH_CCM_MODE) {
690 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
691 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
695 if (expected->aad[0] != NULL) {
696 t->err = "AAD_SET_ERROR";
698 for (i = 0; expected->aad[i] != NULL; i++) {
699 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
700 expected->aad_len[i]))
705 * Supply the AAD in chunks less than the block size where possible
707 for (i = 0; expected->aad[i] != NULL; i++) {
708 if (expected->aad_len[i] > 0) {
709 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
713 if (expected->aad_len[i] > 2) {
714 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
715 expected->aad[i] + donelen,
716 expected->aad_len[i] - 2))
718 donelen += expected->aad_len[i] - 2;
720 if (expected->aad_len[i] > 1
721 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
722 expected->aad[i] + donelen, 1))
728 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
729 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
730 expected->tag_len, expected->tag)) {
731 t->err = "TAG_SET_ERROR";
736 EVP_CIPHER_CTX_set_padding(ctx, 0);
737 t->err = "CIPHERUPDATE_ERROR";
740 /* We supply the data all in one go */
741 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
744 /* Supply the data in chunks less than the block size where possible */
746 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
753 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
761 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
767 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
768 t->err = "CIPHERFINAL_ERROR";
771 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
772 tmp + out_misalign, tmplen + tmpflen))
774 if (enc && expected->aead) {
775 unsigned char rtag[16];
777 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
778 t->err = "TAG_LENGTH_INTERNAL_ERROR";
781 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
782 expected->tag_len, rtag)) {
783 t->err = "TAG_RETRIEVE_ERROR";
786 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
787 expected->tag, expected->tag_len,
788 rtag, expected->tag_len))
795 EVP_CIPHER_CTX_free(ctx);
799 static int cipher_test_run(EVP_TEST *t)
801 CIPHER_DATA *cdat = t->data;
803 size_t out_misalign, inp_misalign;
809 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
810 /* IV is optional and usually omitted in wrap mode */
811 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
816 if (cdat->aead && !cdat->tag) {
820 for (out_misalign = 0; out_misalign <= 1;) {
821 static char aux_err[64];
822 t->aux_err = aux_err;
823 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
824 if (inp_misalign == (size_t)-1) {
825 /* kludge: inp_misalign == -1 means "exercise in-place" */
826 BIO_snprintf(aux_err, sizeof(aux_err),
827 "%s in-place, %sfragmented",
828 out_misalign ? "misaligned" : "aligned",
831 BIO_snprintf(aux_err, sizeof(aux_err),
832 "%s output and %s input, %sfragmented",
833 out_misalign ? "misaligned" : "aligned",
834 inp_misalign ? "misaligned" : "aligned",
838 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
839 /* Not fatal errors: return */
846 if (cdat->enc != 1) {
847 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
848 /* Not fatal errors: return */
857 if (out_misalign == 1 && frag == 0) {
859 * XTS, SIV, CCM and Wrap modes have special requirements about input
860 * lengths so we don't fragment for those
862 if (cdat->aead == EVP_CIPH_CCM_MODE
863 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
864 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
865 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
878 static const EVP_TEST_METHOD cipher_test_method = {
891 typedef struct mac_data_st {
892 /* MAC type in one form or another */
893 EVP_MAC *mac; /* for mac_test_run_mac */
894 int type; /* for mac_test_run_pkey */
895 /* Algorithm string for this MAC */
904 unsigned char *input;
906 /* Expected output */
907 unsigned char *output;
909 unsigned char *custom;
911 /* MAC salt (blake2) */
914 /* Collection of controls */
915 STACK_OF(OPENSSL_STRING) *controls;
918 static int mac_test_init(EVP_TEST *t, const char *alg)
921 int type = NID_undef;
924 if ((mac = EVP_MAC_fetch(NULL, alg, NULL)) == NULL) {
926 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
927 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
928 * the EVP_PKEY method.
930 size_t sz = strlen(alg);
931 static const char epilogue[] = " by EVP_PKEY";
933 if (sz >= sizeof(epilogue)
934 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
935 sz -= sizeof(epilogue) - 1;
937 if (strncmp(alg, "HMAC", sz) == 0) {
938 type = EVP_PKEY_HMAC;
939 } else if (strncmp(alg, "CMAC", sz) == 0) {
940 #ifndef OPENSSL_NO_CMAC
941 type = EVP_PKEY_CMAC;
946 } else if (strncmp(alg, "Poly1305", sz) == 0) {
947 #ifndef OPENSSL_NO_POLY1305
948 type = EVP_PKEY_POLY1305;
953 } else if (strncmp(alg, "SipHash", sz) == 0) {
954 #ifndef OPENSSL_NO_SIPHASH
955 type = EVP_PKEY_SIPHASH;
962 * Not a known EVP_PKEY method either. If it's a known OID, then
963 * assume it's been disabled.
965 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
974 mdat = OPENSSL_zalloc(sizeof(*mdat));
977 mdat->controls = sk_OPENSSL_STRING_new_null();
982 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
983 static void openssl_free(char *m)
988 static void mac_test_cleanup(EVP_TEST *t)
990 MAC_DATA *mdat = t->data;
992 EVP_MAC_free(mdat->mac);
993 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
994 OPENSSL_free(mdat->alg);
995 OPENSSL_free(mdat->key);
996 OPENSSL_free(mdat->iv);
997 OPENSSL_free(mdat->custom);
998 OPENSSL_free(mdat->salt);
999 OPENSSL_free(mdat->input);
1000 OPENSSL_free(mdat->output);
1003 static int mac_test_parse(EVP_TEST *t,
1004 const char *keyword, const char *value)
1006 MAC_DATA *mdata = t->data;
1008 if (strcmp(keyword, "Key") == 0)
1009 return parse_bin(value, &mdata->key, &mdata->key_len);
1010 if (strcmp(keyword, "IV") == 0)
1011 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1012 if (strcmp(keyword, "Custom") == 0)
1013 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1014 if (strcmp(keyword, "Salt") == 0)
1015 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1016 if (strcmp(keyword, "Algorithm") == 0) {
1017 mdata->alg = OPENSSL_strdup(value);
1022 if (strcmp(keyword, "Input") == 0)
1023 return parse_bin(value, &mdata->input, &mdata->input_len);
1024 if (strcmp(keyword, "Output") == 0)
1025 return parse_bin(value, &mdata->output, &mdata->output_len);
1026 if (strcmp(keyword, "Ctrl") == 0)
1027 return sk_OPENSSL_STRING_push(mdata->controls,
1028 OPENSSL_strdup(value)) != 0;
1032 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1038 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1040 p = strchr(tmpval, ':');
1043 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1045 t->err = "PKEY_CTRL_INVALID";
1047 t->err = "PKEY_CTRL_ERROR";
1050 OPENSSL_free(tmpval);
1054 static int mac_test_run_pkey(EVP_TEST *t)
1056 MAC_DATA *expected = t->data;
1057 EVP_MD_CTX *mctx = NULL;
1058 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1059 EVP_PKEY *key = NULL;
1060 const EVP_MD *md = NULL;
1061 unsigned char *got = NULL;
1065 if (expected->alg == NULL)
1066 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1068 TEST_info("Trying the EVP_PKEY %s test with %s",
1069 OBJ_nid2sn(expected->type), expected->alg);
1071 #ifdef OPENSSL_NO_DES
1072 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1079 if (expected->type == EVP_PKEY_CMAC)
1080 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1081 EVP_get_cipherbyname(expected->alg));
1083 key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
1086 t->err = "MAC_KEY_CREATE_ERROR";
1090 if (expected->type == EVP_PKEY_HMAC) {
1091 if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
1092 t->err = "MAC_ALGORITHM_SET_ERROR";
1096 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1097 t->err = "INTERNAL_ERROR";
1100 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1101 t->err = "DIGESTSIGNINIT_ERROR";
1104 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1105 if (!mac_test_ctrl_pkey(t, pctx,
1106 sk_OPENSSL_STRING_value(expected->controls,
1108 t->err = "EVPPKEYCTXCTRL_ERROR";
1111 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1112 t->err = "DIGESTSIGNUPDATE_ERROR";
1115 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1116 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1119 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1120 t->err = "TEST_FAILURE";
1123 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1124 || !memory_err_compare(t, "TEST_MAC_ERR",
1125 expected->output, expected->output_len,
1127 t->err = "TEST_MAC_ERR";
1132 EVP_MD_CTX_free(mctx);
1134 EVP_PKEY_CTX_free(genctx);
1139 static int mac_test_run_mac(EVP_TEST *t)
1141 MAC_DATA *expected = t->data;
1142 EVP_MAC_CTX *ctx = NULL;
1143 unsigned char *got = NULL;
1146 OSSL_PARAM params[21];
1147 size_t params_n = 0;
1148 size_t params_n_allocstart = 0;
1149 const OSSL_PARAM *defined_params =
1150 EVP_MAC_CTX_settable_params(expected->mac);
1152 if (expected->alg == NULL)
1153 TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected->mac));
1155 TEST_info("Trying the EVP_MAC %s test with %s",
1156 EVP_MAC_name(expected->mac), expected->alg);
1158 #ifdef OPENSSL_NO_DES
1159 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1166 if (expected->alg != NULL)
1167 params[params_n++] =
1168 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ALGORITHM,
1170 strlen(expected->alg) + 1);
1171 if (expected->key != NULL)
1172 params[params_n++] =
1173 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1176 if (expected->custom != NULL)
1177 params[params_n++] =
1178 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1180 expected->custom_len);
1181 if (expected->salt != NULL)
1182 params[params_n++] =
1183 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1185 expected->salt_len);
1186 if (expected->iv != NULL)
1187 params[params_n++] =
1188 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1193 * Unknown controls. They must match parameters that the MAC recognises
1195 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1196 >= OSSL_NELEM(params)) {
1197 t->err = "MAC_TOO_MANY_PARAMETERS";
1200 params_n_allocstart = params_n;
1201 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1202 char *tmpkey, *tmpval;
1203 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1205 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1206 t->err = "MAC_PARAM_ERROR";
1209 tmpval = strchr(tmpkey, ':');
1213 if (!OSSL_PARAM_allocate_from_text(¶ms[params_n], defined_params,
1216 OPENSSL_free(tmpkey);
1217 t->err = "MAC_PARAM_ERROR";
1222 OPENSSL_free(tmpkey);
1224 params[params_n] = OSSL_PARAM_construct_end();
1226 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1227 t->err = "MAC_CREATE_ERROR";
1231 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1232 t->err = "MAC_BAD_PARAMS";
1235 if (!EVP_MAC_init(ctx)) {
1236 t->err = "MAC_INIT_ERROR";
1239 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1240 t->err = "MAC_UPDATE_ERROR";
1243 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1244 t->err = "MAC_FINAL_LENGTH_ERROR";
1247 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1248 t->err = "TEST_FAILURE";
1251 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1252 || !memory_err_compare(t, "TEST_MAC_ERR",
1253 expected->output, expected->output_len,
1255 t->err = "TEST_MAC_ERR";
1260 while (params_n-- > params_n_allocstart) {
1261 OPENSSL_free(params[params_n].data);
1263 EVP_MAC_CTX_free(ctx);
1268 static int mac_test_run(EVP_TEST *t)
1270 MAC_DATA *expected = t->data;
1272 if (expected->mac != NULL)
1273 return mac_test_run_mac(t);
1274 return mac_test_run_pkey(t);
1277 static const EVP_TEST_METHOD mac_test_method = {
1287 *** PUBLIC KEY TESTS
1288 *** These are all very similar and share much common code.
1291 typedef struct pkey_data_st {
1292 /* Context for this operation */
1294 /* Key operation to perform */
1295 int (*keyop) (EVP_PKEY_CTX *ctx,
1296 unsigned char *sig, size_t *siglen,
1297 const unsigned char *tbs, size_t tbslen);
1299 unsigned char *input;
1301 /* Expected output */
1302 unsigned char *output;
1307 * Perform public key operation setup: lookup key, allocated ctx and call
1308 * the appropriate initialisation function
1310 static int pkey_test_init(EVP_TEST *t, const char *name,
1312 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1313 int (*keyop)(EVP_PKEY_CTX *ctx,
1314 unsigned char *sig, size_t *siglen,
1315 const unsigned char *tbs,
1319 EVP_PKEY *pkey = NULL;
1323 rv = find_key(&pkey, name, public_keys);
1325 rv = find_key(&pkey, name, private_keys);
1326 if (rv == 0 || pkey == NULL) {
1331 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1332 EVP_PKEY_free(pkey);
1335 kdata->keyop = keyop;
1336 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
1337 EVP_PKEY_free(pkey);
1338 OPENSSL_free(kdata);
1341 if (keyopinit(kdata->ctx) <= 0)
1342 t->err = "KEYOP_INIT_ERROR";
1347 static void pkey_test_cleanup(EVP_TEST *t)
1349 PKEY_DATA *kdata = t->data;
1351 OPENSSL_free(kdata->input);
1352 OPENSSL_free(kdata->output);
1353 EVP_PKEY_CTX_free(kdata->ctx);
1356 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1362 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1364 p = strchr(tmpval, ':');
1367 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1369 t->err = "PKEY_CTRL_INVALID";
1371 } else if (p != NULL && rv <= 0) {
1372 /* If p has an OID and lookup fails assume disabled algorithm */
1373 int nid = OBJ_sn2nid(p);
1375 if (nid == NID_undef)
1376 nid = OBJ_ln2nid(p);
1377 if (nid != NID_undef
1378 && EVP_get_digestbynid(nid) == NULL
1379 && EVP_get_cipherbynid(nid) == NULL) {
1383 t->err = "PKEY_CTRL_ERROR";
1387 OPENSSL_free(tmpval);
1391 static int pkey_test_parse(EVP_TEST *t,
1392 const char *keyword, const char *value)
1394 PKEY_DATA *kdata = t->data;
1395 if (strcmp(keyword, "Input") == 0)
1396 return parse_bin(value, &kdata->input, &kdata->input_len);
1397 if (strcmp(keyword, "Output") == 0)
1398 return parse_bin(value, &kdata->output, &kdata->output_len);
1399 if (strcmp(keyword, "Ctrl") == 0)
1400 return pkey_test_ctrl(t, kdata->ctx, value);
1404 static int pkey_test_run(EVP_TEST *t)
1406 PKEY_DATA *expected = t->data;
1407 unsigned char *got = NULL;
1409 EVP_PKEY_CTX *copy = NULL;
1411 if (expected->keyop(expected->ctx, NULL, &got_len,
1412 expected->input, expected->input_len) <= 0
1413 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1414 t->err = "KEYOP_LENGTH_ERROR";
1417 if (expected->keyop(expected->ctx, got, &got_len,
1418 expected->input, expected->input_len) <= 0) {
1419 t->err = "KEYOP_ERROR";
1422 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1423 expected->output, expected->output_len,
1431 /* Repeat the test on a copy. */
1432 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1433 t->err = "INTERNAL_ERROR";
1436 if (expected->keyop(copy, NULL, &got_len, expected->input,
1437 expected->input_len) <= 0
1438 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1439 t->err = "KEYOP_LENGTH_ERROR";
1442 if (expected->keyop(copy, got, &got_len, expected->input,
1443 expected->input_len) <= 0) {
1444 t->err = "KEYOP_ERROR";
1447 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1448 expected->output, expected->output_len,
1454 EVP_PKEY_CTX_free(copy);
1458 static int sign_test_init(EVP_TEST *t, const char *name)
1460 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1463 static const EVP_TEST_METHOD psign_test_method = {
1471 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1473 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1474 EVP_PKEY_verify_recover);
1477 static const EVP_TEST_METHOD pverify_recover_test_method = {
1479 verify_recover_test_init,
1485 static int decrypt_test_init(EVP_TEST *t, const char *name)
1487 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1491 static const EVP_TEST_METHOD pdecrypt_test_method = {
1499 static int verify_test_init(EVP_TEST *t, const char *name)
1501 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1504 static int verify_test_run(EVP_TEST *t)
1506 PKEY_DATA *kdata = t->data;
1508 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1509 kdata->input, kdata->input_len) <= 0)
1510 t->err = "VERIFY_ERROR";
1514 static const EVP_TEST_METHOD pverify_test_method = {
1523 static int pderive_test_init(EVP_TEST *t, const char *name)
1525 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1528 static int pderive_test_parse(EVP_TEST *t,
1529 const char *keyword, const char *value)
1531 PKEY_DATA *kdata = t->data;
1533 if (strcmp(keyword, "PeerKey") == 0) {
1535 if (find_key(&peer, value, public_keys) == 0)
1537 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1541 if (strcmp(keyword, "SharedSecret") == 0)
1542 return parse_bin(value, &kdata->output, &kdata->output_len);
1543 if (strcmp(keyword, "Ctrl") == 0)
1544 return pkey_test_ctrl(t, kdata->ctx, value);
1548 static int pderive_test_run(EVP_TEST *t)
1550 PKEY_DATA *expected = t->data;
1551 unsigned char *got = NULL;
1554 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1555 t->err = "DERIVE_ERROR";
1558 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1559 t->err = "DERIVE_ERROR";
1562 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1563 t->err = "DERIVE_ERROR";
1566 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1567 expected->output, expected->output_len,
1577 static const EVP_TEST_METHOD pderive_test_method = {
1590 typedef enum pbe_type_enum {
1591 PBE_TYPE_INVALID = 0,
1592 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1595 typedef struct pbe_data_st {
1597 /* scrypt parameters */
1598 uint64_t N, r, p, maxmem;
1599 /* PKCS#12 parameters */
1603 unsigned char *pass;
1606 unsigned char *salt;
1608 /* Expected output */
1613 #ifndef OPENSSL_NO_SCRYPT
1615 * Parse unsigned decimal 64 bit integer value
1617 static int parse_uint64(const char *value, uint64_t *pr)
1619 const char *p = value;
1621 if (!TEST_true(*p)) {
1622 TEST_info("Invalid empty integer value");
1625 for (*pr = 0; *p; ) {
1626 if (*pr > UINT64_MAX / 10) {
1627 TEST_error("Integer overflow in string %s", value);
1631 if (!TEST_true(isdigit((unsigned char)*p))) {
1632 TEST_error("Invalid character in string %s", value);
1641 static int scrypt_test_parse(EVP_TEST *t,
1642 const char *keyword, const char *value)
1644 PBE_DATA *pdata = t->data;
1646 if (strcmp(keyword, "N") == 0)
1647 return parse_uint64(value, &pdata->N);
1648 if (strcmp(keyword, "p") == 0)
1649 return parse_uint64(value, &pdata->p);
1650 if (strcmp(keyword, "r") == 0)
1651 return parse_uint64(value, &pdata->r);
1652 if (strcmp(keyword, "maxmem") == 0)
1653 return parse_uint64(value, &pdata->maxmem);
1658 static int pbkdf2_test_parse(EVP_TEST *t,
1659 const char *keyword, const char *value)
1661 PBE_DATA *pdata = t->data;
1663 if (strcmp(keyword, "iter") == 0) {
1664 pdata->iter = atoi(value);
1665 if (pdata->iter <= 0)
1669 if (strcmp(keyword, "MD") == 0) {
1670 pdata->md = EVP_get_digestbyname(value);
1671 if (pdata->md == NULL)
1678 static int pkcs12_test_parse(EVP_TEST *t,
1679 const char *keyword, const char *value)
1681 PBE_DATA *pdata = t->data;
1683 if (strcmp(keyword, "id") == 0) {
1684 pdata->id = atoi(value);
1689 return pbkdf2_test_parse(t, keyword, value);
1692 static int pbe_test_init(EVP_TEST *t, const char *alg)
1695 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1697 if (strcmp(alg, "scrypt") == 0) {
1698 #ifndef OPENSSL_NO_SCRYPT
1699 pbe_type = PBE_TYPE_SCRYPT;
1704 } else if (strcmp(alg, "pbkdf2") == 0) {
1705 pbe_type = PBE_TYPE_PBKDF2;
1706 } else if (strcmp(alg, "pkcs12") == 0) {
1707 pbe_type = PBE_TYPE_PKCS12;
1709 TEST_error("Unknown pbe algorithm %s", alg);
1711 pdat = OPENSSL_zalloc(sizeof(*pdat));
1712 pdat->pbe_type = pbe_type;
1717 static void pbe_test_cleanup(EVP_TEST *t)
1719 PBE_DATA *pdat = t->data;
1721 OPENSSL_free(pdat->pass);
1722 OPENSSL_free(pdat->salt);
1723 OPENSSL_free(pdat->key);
1726 static int pbe_test_parse(EVP_TEST *t,
1727 const char *keyword, const char *value)
1729 PBE_DATA *pdata = t->data;
1731 if (strcmp(keyword, "Password") == 0)
1732 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1733 if (strcmp(keyword, "Salt") == 0)
1734 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1735 if (strcmp(keyword, "Key") == 0)
1736 return parse_bin(value, &pdata->key, &pdata->key_len);
1737 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1738 return pbkdf2_test_parse(t, keyword, value);
1739 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1740 return pkcs12_test_parse(t, keyword, value);
1741 #ifndef OPENSSL_NO_SCRYPT
1742 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1743 return scrypt_test_parse(t, keyword, value);
1748 static int pbe_test_run(EVP_TEST *t)
1750 PBE_DATA *expected = t->data;
1753 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1754 t->err = "INTERNAL_ERROR";
1757 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1758 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1759 expected->salt, expected->salt_len,
1760 expected->iter, expected->md,
1761 expected->key_len, key) == 0) {
1762 t->err = "PBKDF2_ERROR";
1765 #ifndef OPENSSL_NO_SCRYPT
1766 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1767 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1768 expected->salt, expected->salt_len, expected->N,
1769 expected->r, expected->p, expected->maxmem,
1770 key, expected->key_len) == 0) {
1771 t->err = "SCRYPT_ERROR";
1775 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1776 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1777 expected->salt, expected->salt_len,
1778 expected->id, expected->iter, expected->key_len,
1779 key, expected->md) == 0) {
1780 t->err = "PKCS12_ERROR";
1784 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1785 key, expected->key_len))
1794 static const EVP_TEST_METHOD pbe_test_method = {
1808 BASE64_CANONICAL_ENCODING = 0,
1809 BASE64_VALID_ENCODING = 1,
1810 BASE64_INVALID_ENCODING = 2
1811 } base64_encoding_type;
1813 typedef struct encode_data_st {
1814 /* Input to encoding */
1815 unsigned char *input;
1817 /* Expected output */
1818 unsigned char *output;
1820 base64_encoding_type encoding;
1823 static int encode_test_init(EVP_TEST *t, const char *encoding)
1827 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1829 if (strcmp(encoding, "canonical") == 0) {
1830 edata->encoding = BASE64_CANONICAL_ENCODING;
1831 } else if (strcmp(encoding, "valid") == 0) {
1832 edata->encoding = BASE64_VALID_ENCODING;
1833 } else if (strcmp(encoding, "invalid") == 0) {
1834 edata->encoding = BASE64_INVALID_ENCODING;
1835 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1838 TEST_error("Bad encoding: %s."
1839 " Should be one of {canonical, valid, invalid}",
1846 OPENSSL_free(edata);
1850 static void encode_test_cleanup(EVP_TEST *t)
1852 ENCODE_DATA *edata = t->data;
1854 OPENSSL_free(edata->input);
1855 OPENSSL_free(edata->output);
1856 memset(edata, 0, sizeof(*edata));
1859 static int encode_test_parse(EVP_TEST *t,
1860 const char *keyword, const char *value)
1862 ENCODE_DATA *edata = t->data;
1864 if (strcmp(keyword, "Input") == 0)
1865 return parse_bin(value, &edata->input, &edata->input_len);
1866 if (strcmp(keyword, "Output") == 0)
1867 return parse_bin(value, &edata->output, &edata->output_len);
1871 static int encode_test_run(EVP_TEST *t)
1873 ENCODE_DATA *expected = t->data;
1874 unsigned char *encode_out = NULL, *decode_out = NULL;
1875 int output_len, chunk_len;
1876 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1878 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1879 t->err = "INTERNAL_ERROR";
1883 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1885 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1886 || !TEST_ptr(encode_out =
1887 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1890 EVP_EncodeInit(encode_ctx);
1891 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1892 expected->input, expected->input_len)))
1895 output_len = chunk_len;
1897 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1898 output_len += chunk_len;
1900 if (!memory_err_compare(t, "BAD_ENCODING",
1901 expected->output, expected->output_len,
1902 encode_out, output_len))
1906 if (!TEST_ptr(decode_out =
1907 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
1910 EVP_DecodeInit(decode_ctx);
1911 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
1912 expected->output_len) < 0) {
1913 t->err = "DECODE_ERROR";
1916 output_len = chunk_len;
1918 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1919 t->err = "DECODE_ERROR";
1922 output_len += chunk_len;
1924 if (expected->encoding != BASE64_INVALID_ENCODING
1925 && !memory_err_compare(t, "BAD_DECODING",
1926 expected->input, expected->input_len,
1927 decode_out, output_len)) {
1928 t->err = "BAD_DECODING";
1934 OPENSSL_free(encode_out);
1935 OPENSSL_free(decode_out);
1936 EVP_ENCODE_CTX_free(decode_ctx);
1937 EVP_ENCODE_CTX_free(encode_ctx);
1941 static const EVP_TEST_METHOD encode_test_method = {
1944 encode_test_cleanup,
1954 typedef struct kdf_data_st {
1955 /* Context for this operation */
1957 /* Expected output */
1958 unsigned char *output;
1963 * Perform public key operation setup: lookup key, allocated ctx and call
1964 * the appropriate initialisation function
1966 static int kdf_test_init(EVP_TEST *t, const char *name)
1971 #ifdef OPENSSL_NO_SCRYPT
1972 if (strcmp(name, "scrypt") == 0) {
1976 #endif /* OPENSSL_NO_SCRYPT */
1978 #ifdef OPENSSL_NO_CMS
1979 if (strcmp(name, "X942KDF") == 0) {
1983 #endif /* OPENSSL_NO_CMS */
1985 kdf = EVP_get_kdfbyname(name);
1989 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
1991 kdata->ctx = EVP_KDF_CTX_new(kdf);
1992 if (kdata->ctx == NULL) {
1993 OPENSSL_free(kdata);
2000 static void kdf_test_cleanup(EVP_TEST *t)
2002 KDF_DATA *kdata = t->data;
2003 OPENSSL_free(kdata->output);
2004 EVP_KDF_CTX_free(kdata->ctx);
2007 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2013 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
2015 p = strchr(tmpval, ':');
2018 rv = EVP_KDF_ctrl_str(kctx, tmpval, p);
2020 t->err = "KDF_CTRL_INVALID";
2022 } else if (p != NULL && rv <= 0) {
2023 /* If p has an OID and lookup fails assume disabled algorithm */
2024 int nid = OBJ_sn2nid(p);
2026 if (nid == NID_undef)
2027 nid = OBJ_ln2nid(p);
2028 if (nid != NID_undef
2029 && EVP_get_digestbynid(nid) == NULL
2030 && EVP_get_cipherbynid(nid) == NULL) {
2034 t->err = "KDF_CTRL_ERROR";
2038 OPENSSL_free(tmpval);
2042 static int kdf_test_parse(EVP_TEST *t,
2043 const char *keyword, const char *value)
2045 KDF_DATA *kdata = t->data;
2047 if (strcmp(keyword, "Output") == 0)
2048 return parse_bin(value, &kdata->output, &kdata->output_len);
2049 if (strncmp(keyword, "Ctrl", 4) == 0)
2050 return kdf_test_ctrl(t, kdata->ctx, value);
2054 static int kdf_test_run(EVP_TEST *t)
2056 KDF_DATA *expected = t->data;
2057 unsigned char *got = NULL;
2058 size_t got_len = expected->output_len;
2060 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2061 t->err = "INTERNAL_ERROR";
2064 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2065 t->err = "KDF_DERIVE_ERROR";
2068 if (!memory_err_compare(t, "KDF_MISMATCH",
2069 expected->output, expected->output_len,
2080 static const EVP_TEST_METHOD kdf_test_method = {
2093 typedef struct pkey_kdf_data_st {
2094 /* Context for this operation */
2096 /* Expected output */
2097 unsigned char *output;
2102 * Perform public key operation setup: lookup key, allocated ctx and call
2103 * the appropriate initialisation function
2105 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2107 PKEY_KDF_DATA *kdata;
2108 int kdf_nid = OBJ_sn2nid(name);
2110 #ifdef OPENSSL_NO_SCRYPT
2111 if (strcmp(name, "scrypt") == 0) {
2115 #endif /* OPENSSL_NO_SCRYPT */
2117 #ifdef OPENSSL_NO_CMS
2118 if (strcmp(name, "X942KDF") == 0) {
2122 #endif /* OPENSSL_NO_CMS */
2124 if (kdf_nid == NID_undef)
2125 kdf_nid = OBJ_ln2nid(name);
2127 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2129 kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
2130 if (kdata->ctx == NULL) {
2131 OPENSSL_free(kdata);
2134 if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
2135 EVP_PKEY_CTX_free(kdata->ctx);
2136 OPENSSL_free(kdata);
2143 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2145 PKEY_KDF_DATA *kdata = t->data;
2146 OPENSSL_free(kdata->output);
2147 EVP_PKEY_CTX_free(kdata->ctx);
2150 static int pkey_kdf_test_parse(EVP_TEST *t,
2151 const char *keyword, const char *value)
2153 PKEY_KDF_DATA *kdata = t->data;
2155 if (strcmp(keyword, "Output") == 0)
2156 return parse_bin(value, &kdata->output, &kdata->output_len);
2157 if (strncmp(keyword, "Ctrl", 4) == 0)
2158 return pkey_test_ctrl(t, kdata->ctx, value);
2162 static int pkey_kdf_test_run(EVP_TEST *t)
2164 PKEY_KDF_DATA *expected = t->data;
2165 unsigned char *got = NULL;
2166 size_t got_len = expected->output_len;
2168 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2169 t->err = "INTERNAL_ERROR";
2172 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2173 t->err = "KDF_DERIVE_ERROR";
2176 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2177 t->err = "KDF_MISMATCH";
2187 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2190 pkey_kdf_test_cleanup,
2191 pkey_kdf_test_parse,
2200 typedef struct keypair_test_data_st {
2203 } KEYPAIR_TEST_DATA;
2205 static int keypair_test_init(EVP_TEST *t, const char *pair)
2207 KEYPAIR_TEST_DATA *data;
2209 EVP_PKEY *pk = NULL, *pubk = NULL;
2210 char *pub, *priv = NULL;
2212 /* Split private and public names. */
2213 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2214 || !TEST_ptr(pub = strchr(priv, ':'))) {
2215 t->err = "PARSING_ERROR";
2220 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2221 TEST_info("Can't find private key: %s", priv);
2222 t->err = "MISSING_PRIVATE_KEY";
2225 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2226 TEST_info("Can't find public key: %s", pub);
2227 t->err = "MISSING_PUBLIC_KEY";
2231 if (pk == NULL && pubk == NULL) {
2232 /* Both keys are listed but unsupported: skip this test */
2238 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2251 static void keypair_test_cleanup(EVP_TEST *t)
2253 OPENSSL_free(t->data);
2258 * For tests that do not accept any custom keywords.
2260 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2265 static int keypair_test_run(EVP_TEST *t)
2268 const KEYPAIR_TEST_DATA *pair = t->data;
2270 if (pair->privk == NULL || pair->pubk == NULL) {
2272 * this can only happen if only one of the keys is not set
2273 * which means that one of them was unsupported while the
2274 * other isn't: hence a key type mismatch.
2276 t->err = "KEYPAIR_TYPE_MISMATCH";
2281 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2283 t->err = "KEYPAIR_MISMATCH";
2284 } else if ( -1 == rv ) {
2285 t->err = "KEYPAIR_TYPE_MISMATCH";
2286 } else if ( -2 == rv ) {
2287 t->err = "UNSUPPORTED_KEY_COMPARISON";
2289 TEST_error("Unexpected error in key comparison");
2304 static const EVP_TEST_METHOD keypair_test_method = {
2307 keypair_test_cleanup,
2316 typedef struct keygen_test_data_st {
2317 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2318 char *keyname; /* Key name to store key or NULL */
2321 static int keygen_test_init(EVP_TEST *t, const char *alg)
2323 KEYGEN_TEST_DATA *data;
2324 EVP_PKEY_CTX *genctx;
2325 int nid = OBJ_sn2nid(alg);
2327 if (nid == NID_undef) {
2328 nid = OBJ_ln2nid(alg);
2329 if (nid == NID_undef)
2333 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
2334 /* assume algorithm disabled */
2339 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2340 t->err = "KEYGEN_INIT_ERROR";
2344 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2346 data->genctx = genctx;
2347 data->keyname = NULL;
2353 EVP_PKEY_CTX_free(genctx);
2357 static void keygen_test_cleanup(EVP_TEST *t)
2359 KEYGEN_TEST_DATA *keygen = t->data;
2361 EVP_PKEY_CTX_free(keygen->genctx);
2362 OPENSSL_free(keygen->keyname);
2363 OPENSSL_free(t->data);
2367 static int keygen_test_parse(EVP_TEST *t,
2368 const char *keyword, const char *value)
2370 KEYGEN_TEST_DATA *keygen = t->data;
2372 if (strcmp(keyword, "KeyName") == 0)
2373 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2374 if (strcmp(keyword, "Ctrl") == 0)
2375 return pkey_test_ctrl(t, keygen->genctx, value);
2379 static int keygen_test_run(EVP_TEST *t)
2381 KEYGEN_TEST_DATA *keygen = t->data;
2382 EVP_PKEY *pkey = NULL;
2385 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2386 t->err = "KEYGEN_GENERATE_ERROR";
2390 if (keygen->keyname != NULL) {
2393 if (find_key(NULL, keygen->keyname, private_keys)) {
2394 TEST_info("Duplicate key %s", keygen->keyname);
2398 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2400 key->name = keygen->keyname;
2401 keygen->keyname = NULL;
2403 key->next = private_keys;
2406 EVP_PKEY_free(pkey);
2412 EVP_PKEY_free(pkey);
2416 static const EVP_TEST_METHOD keygen_test_method = {
2419 keygen_test_cleanup,
2425 *** DIGEST SIGN+VERIFY TESTS
2429 int is_verify; /* Set to 1 if verifying */
2430 int is_oneshot; /* Set to 1 for one shot operation */
2431 const EVP_MD *md; /* Digest to use */
2432 EVP_MD_CTX *ctx; /* Digest context */
2434 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2435 unsigned char *osin; /* Input data if one shot */
2436 size_t osin_len; /* Input length data if one shot */
2437 unsigned char *output; /* Expected output */
2438 size_t output_len; /* Expected output length */
2441 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2444 const EVP_MD *md = NULL;
2445 DIGESTSIGN_DATA *mdat;
2447 if (strcmp(alg, "NULL") != 0) {
2448 if ((md = EVP_get_digestbyname(alg)) == NULL) {
2449 /* If alg has an OID assume disabled algorithm */
2450 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
2457 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2460 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2464 mdat->is_verify = is_verify;
2465 mdat->is_oneshot = is_oneshot;
2470 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2472 return digestsigver_test_init(t, alg, 0, 0);
2475 static void digestsigver_test_cleanup(EVP_TEST *t)
2477 DIGESTSIGN_DATA *mdata = t->data;
2479 EVP_MD_CTX_free(mdata->ctx);
2480 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2481 OPENSSL_free(mdata->osin);
2482 OPENSSL_free(mdata->output);
2483 OPENSSL_free(mdata);
2487 static int digestsigver_test_parse(EVP_TEST *t,
2488 const char *keyword, const char *value)
2490 DIGESTSIGN_DATA *mdata = t->data;
2492 if (strcmp(keyword, "Key") == 0) {
2493 EVP_PKEY *pkey = NULL;
2496 if (mdata->is_verify)
2497 rv = find_key(&pkey, value, public_keys);
2499 rv = find_key(&pkey, value, private_keys);
2500 if (rv == 0 || pkey == NULL) {
2504 if (mdata->is_verify) {
2505 if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
2507 t->err = "DIGESTVERIFYINIT_ERROR";
2510 if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
2512 t->err = "DIGESTSIGNINIT_ERROR";
2516 if (strcmp(keyword, "Input") == 0) {
2517 if (mdata->is_oneshot)
2518 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2519 return evp_test_buffer_append(value, &mdata->input);
2521 if (strcmp(keyword, "Output") == 0)
2522 return parse_bin(value, &mdata->output, &mdata->output_len);
2524 if (!mdata->is_oneshot) {
2525 if (strcmp(keyword, "Count") == 0)
2526 return evp_test_buffer_set_count(value, mdata->input);
2527 if (strcmp(keyword, "Ncopy") == 0)
2528 return evp_test_buffer_ncopy(value, mdata->input);
2530 if (strcmp(keyword, "Ctrl") == 0) {
2531 if (mdata->pctx == NULL)
2533 return pkey_test_ctrl(t, mdata->pctx, value);
2538 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2541 return EVP_DigestSignUpdate(ctx, buf, buflen);
2544 static int digestsign_test_run(EVP_TEST *t)
2546 DIGESTSIGN_DATA *expected = t->data;
2547 unsigned char *got = NULL;
2550 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2552 t->err = "DIGESTUPDATE_ERROR";
2556 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2557 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2560 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2561 t->err = "MALLOC_FAILURE";
2564 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2565 t->err = "DIGESTSIGNFINAL_ERROR";
2568 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2569 expected->output, expected->output_len,
2579 static const EVP_TEST_METHOD digestsign_test_method = {
2581 digestsign_test_init,
2582 digestsigver_test_cleanup,
2583 digestsigver_test_parse,
2587 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2589 return digestsigver_test_init(t, alg, 1, 0);
2592 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2595 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2598 static int digestverify_test_run(EVP_TEST *t)
2600 DIGESTSIGN_DATA *mdata = t->data;
2602 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2603 t->err = "DIGESTUPDATE_ERROR";
2607 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2608 mdata->output_len) <= 0)
2609 t->err = "VERIFY_ERROR";
2613 static const EVP_TEST_METHOD digestverify_test_method = {
2615 digestverify_test_init,
2616 digestsigver_test_cleanup,
2617 digestsigver_test_parse,
2618 digestverify_test_run
2621 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
2623 return digestsigver_test_init(t, alg, 0, 1);
2626 static int oneshot_digestsign_test_run(EVP_TEST *t)
2628 DIGESTSIGN_DATA *expected = t->data;
2629 unsigned char *got = NULL;
2632 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
2633 expected->osin, expected->osin_len)) {
2634 t->err = "DIGESTSIGN_LENGTH_ERROR";
2637 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2638 t->err = "MALLOC_FAILURE";
2641 if (!EVP_DigestSign(expected->ctx, got, &got_len,
2642 expected->osin, expected->osin_len)) {
2643 t->err = "DIGESTSIGN_ERROR";
2646 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2647 expected->output, expected->output_len,
2657 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
2658 "OneShotDigestSign",
2659 oneshot_digestsign_test_init,
2660 digestsigver_test_cleanup,
2661 digestsigver_test_parse,
2662 oneshot_digestsign_test_run
2665 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
2667 return digestsigver_test_init(t, alg, 1, 1);
2670 static int oneshot_digestverify_test_run(EVP_TEST *t)
2672 DIGESTSIGN_DATA *mdata = t->data;
2674 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
2675 mdata->osin, mdata->osin_len) <= 0)
2676 t->err = "VERIFY_ERROR";
2680 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
2681 "OneShotDigestVerify",
2682 oneshot_digestverify_test_init,
2683 digestsigver_test_cleanup,
2684 digestsigver_test_parse,
2685 oneshot_digestverify_test_run
2690 *** PARSING AND DISPATCH
2693 static const EVP_TEST_METHOD *evp_test_list[] = {
2694 &cipher_test_method,
2695 &digest_test_method,
2696 &digestsign_test_method,
2697 &digestverify_test_method,
2698 &encode_test_method,
2700 &pkey_kdf_test_method,
2701 &keypair_test_method,
2702 &keygen_test_method,
2704 &oneshot_digestsign_test_method,
2705 &oneshot_digestverify_test_method,
2707 &pdecrypt_test_method,
2708 &pderive_test_method,
2710 &pverify_recover_test_method,
2711 &pverify_test_method,
2715 static const EVP_TEST_METHOD *find_test(const char *name)
2717 const EVP_TEST_METHOD **tt;
2719 for (tt = evp_test_list; *tt; tt++) {
2720 if (strcmp(name, (*tt)->name) == 0)
2726 static void clear_test(EVP_TEST *t)
2728 test_clearstanza(&t->s);
2730 if (t->data != NULL) {
2731 if (t->meth != NULL)
2732 t->meth->cleanup(t);
2733 OPENSSL_free(t->data);
2736 OPENSSL_free(t->expected_err);
2737 t->expected_err = NULL;
2738 OPENSSL_free(t->reason);
2748 * Check for errors in the test structure; return 1 if okay, else 0.
2750 static int check_test_error(EVP_TEST *t)
2756 if (t->err == NULL && t->expected_err == NULL)
2758 if (t->err != NULL && t->expected_err == NULL) {
2759 if (t->aux_err != NULL) {
2760 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2761 t->s.test_file, t->s.start, t->aux_err, t->err);
2763 TEST_info("%s:%d: Source of above error; unexpected error %s",
2764 t->s.test_file, t->s.start, t->err);
2768 if (t->err == NULL && t->expected_err != NULL) {
2769 TEST_info("%s:%d: Succeeded but was expecting %s",
2770 t->s.test_file, t->s.start, t->expected_err);
2774 if (strcmp(t->err, t->expected_err) != 0) {
2775 TEST_info("%s:%d: Expected %s got %s",
2776 t->s.test_file, t->s.start, t->expected_err, t->err);
2780 if (t->reason == NULL)
2783 if (t->reason == NULL) {
2784 TEST_info("%s:%d: Test is missing function or reason code",
2785 t->s.test_file, t->s.start);
2789 err = ERR_peek_error();
2791 TEST_info("%s:%d: Expected error \"%s\" not set",
2792 t->s.test_file, t->s.start, t->reason);
2796 func = ERR_func_error_string(err);
2797 reason = ERR_reason_error_string(err);
2798 if (func == NULL && reason == NULL) {
2799 TEST_info("%s:%d: Expected error \"%s\", no strings available."
2801 t->s.test_file, t->s.start, t->reason);
2805 if (strcmp(reason, t->reason) == 0)
2808 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
2809 t->s.test_file, t->s.start, t->reason, reason);
2815 * Run a parsed test. Log a message and return 0 on error.
2817 static int run_test(EVP_TEST *t)
2819 if (t->meth == NULL)
2826 if (t->err == NULL && t->meth->run_test(t) != 1) {
2827 TEST_info("%s:%d %s error",
2828 t->s.test_file, t->s.start, t->meth->name);
2831 if (!check_test_error(t)) {
2832 TEST_openssl_errors();
2841 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
2843 for (; lst != NULL; lst = lst->next) {
2844 if (strcmp(lst->name, name) == 0) {
2853 static void free_key_list(KEY_LIST *lst)
2855 while (lst != NULL) {
2856 KEY_LIST *next = lst->next;
2858 EVP_PKEY_free(lst->key);
2859 OPENSSL_free(lst->name);
2866 * Is the key type an unsupported algorithm?
2868 static int key_unsupported(void)
2870 long err = ERR_peek_error();
2872 if (ERR_GET_LIB(err) == ERR_LIB_EVP
2873 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
2877 #ifndef OPENSSL_NO_EC
2879 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2880 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2883 if (ERR_GET_LIB(err) == ERR_LIB_EC
2884 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
2888 #endif /* OPENSSL_NO_EC */
2893 * NULL out the value from |pp| but return it. This "steals" a pointer.
2895 static char *take_value(PAIR *pp)
2897 char *p = pp->value;
2904 * Return 1 if one of the providers named in the string is available.
2905 * The provider names are separated with whitespace.
2906 * NOTE: destructive function, it inserts '\0' after each provider name.
2908 static int prov_available(char *providers)
2914 for (; isspace(*providers); providers++)
2916 if (*providers == '\0')
2917 break; /* End of the road */
2918 for (p = providers; *p != '\0' && !isspace(*p); p++)
2924 if (OSSL_PROVIDER_available(NULL, providers))
2925 return 1; /* Found one */
2931 * Read and parse one test. Return 0 if failure, 1 if okay.
2933 static int parse(EVP_TEST *t)
2935 KEY_LIST *key, **klist;
2942 if (BIO_eof(t->s.fp))
2945 if (!test_readstanza(&t->s))
2947 } while (t->s.numpairs == 0);
2948 pp = &t->s.pairs[0];
2950 /* Are we adding a key? */
2953 if (strcmp(pp->key, "PrivateKey") == 0) {
2954 pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
2955 if (pkey == NULL && !key_unsupported()) {
2956 EVP_PKEY_free(pkey);
2957 TEST_info("Can't read private key %s", pp->value);
2958 TEST_openssl_errors();
2961 klist = &private_keys;
2962 } else if (strcmp(pp->key, "PublicKey") == 0) {
2963 pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
2964 if (pkey == NULL && !key_unsupported()) {
2965 EVP_PKEY_free(pkey);
2966 TEST_info("Can't read public key %s", pp->value);
2967 TEST_openssl_errors();
2970 klist = &public_keys;
2971 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
2972 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
2973 char *strnid = NULL, *keydata = NULL;
2974 unsigned char *keybin;
2978 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
2979 klist = &private_keys;
2981 klist = &public_keys;
2983 strnid = strchr(pp->value, ':');
2984 if (strnid != NULL) {
2986 keydata = strchr(strnid, ':');
2987 if (keydata != NULL)
2990 if (keydata == NULL) {
2991 TEST_info("Failed to parse %s value", pp->key);
2995 nid = OBJ_txt2nid(strnid);
2996 if (nid == NID_undef) {
2997 TEST_info("Uncrecognised algorithm NID");
3000 if (!parse_bin(keydata, &keybin, &keylen)) {
3001 TEST_info("Failed to create binary key");
3004 if (klist == &private_keys)
3005 pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
3007 pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
3008 if (pkey == NULL && !key_unsupported()) {
3009 TEST_info("Can't read %s data", pp->key);
3010 OPENSSL_free(keybin);
3011 TEST_openssl_errors();
3014 OPENSSL_free(keybin);
3017 /* If we have a key add to list */
3018 if (klist != NULL) {
3019 if (find_key(NULL, pp->value, *klist)) {
3020 TEST_info("Duplicate key %s", pp->value);
3023 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3025 key->name = take_value(pp);
3027 /* Hack to detect SM2 keys */
3028 if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
3029 #ifdef OPENSSL_NO_SM2
3030 EVP_PKEY_free(pkey);
3033 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
3041 /* Go back and start a new stanza. */
3042 if (t->s.numpairs != 1)
3043 TEST_info("Line %d: missing blank line\n", t->s.curr);
3047 /* Find the test, based on first keyword. */
3048 if (!TEST_ptr(t->meth = find_test(pp->key)))
3050 if (!t->meth->init(t, pp->value)) {
3051 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3055 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3059 for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
3060 if (strcmp(pp->key, "Availablein") == 0) {
3061 if (!prov_available(pp->value)) {
3062 TEST_info("skipping, providers not available: %s:%d",
3063 t->s.test_file, t->s.start);
3067 } else if (strcmp(pp->key, "Result") == 0) {
3068 if (t->expected_err != NULL) {
3069 TEST_info("Line %d: multiple result lines", t->s.curr);
3072 t->expected_err = take_value(pp);
3073 } else if (strcmp(pp->key, "Function") == 0) {
3074 /* Ignore old line. */
3075 } else if (strcmp(pp->key, "Reason") == 0) {
3076 if (t->reason != NULL) {
3077 TEST_info("Line %d: multiple reason lines", t->s.curr);
3080 t->reason = take_value(pp);
3082 /* Must be test specific line: try to parse it */
3083 int rv = t->meth->parse(t, pp->key, pp->value);
3086 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3090 TEST_info("Line %d: error processing keyword %s = %s\n",
3091 t->s.curr, pp->key, pp->value);
3100 static int run_file_tests(int i)
3103 const char *testfile = test_get_argument(i);
3106 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3108 if (!test_start_file(&t->s, testfile)) {
3113 while (!BIO_eof(t->s.fp)) {
3119 if (c == 0 || !run_test(t)) {
3124 test_end_file(&t->s);
3127 free_key_list(public_keys);
3128 free_key_list(private_keys);
3135 OPT_TEST_DECLARE_USAGE("file...\n")
3137 int setup_tests(void)
3139 size_t n = test_get_argument_count();
3144 ADD_ALL_TESTS(run_file_tests, n);