2 * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pkcs12.h>
19 #include <openssl/kdf.h>
20 #include "internal/numbers.h"
25 typedef struct evp_test_method_st EVP_TEST_METHOD;
28 * Structure holding test information
30 typedef struct evp_test_st {
31 STANZA s; /* Common test stanza */
33 int skip; /* Current test should be skipped */
34 const EVP_TEST_METHOD *meth; /* method for this test */
35 const char *err, *aux_err; /* Error string for test */
36 char *expected_err; /* Expected error value of test */
37 char *func; /* Expected error function string */
38 char *reason; /* Expected error reason string */
39 void *data; /* test specific data */
43 * Test method structure
45 struct evp_test_method_st {
46 /* Name of test as it appears in file */
48 /* Initialise test for "alg" */
49 int (*init) (EVP_TEST * t, const char *alg);
51 void (*cleanup) (EVP_TEST * t);
52 /* Test specific name value pair processing */
53 int (*parse) (EVP_TEST * t, const char *name, const char *value);
54 /* Run the test itself */
55 int (*run_test) (EVP_TEST * t);
60 * Linked list of named keys.
62 typedef struct key_list_st {
65 struct key_list_st *next;
69 * List of public and private keys
71 static KEY_LIST *private_keys;
72 static KEY_LIST *public_keys;
73 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
75 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
78 * Compare two memory regions for equality, returning zero if they differ.
79 * However, if there is expected to be an error and the actual error
80 * matches then the memory is expected to be different so handle this
81 * case without producing unnecessary test framework output.
83 static int memory_err_compare(EVP_TEST *t, const char *err,
84 const void *expected, size_t expected_len,
85 const void *got, size_t got_len)
89 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
90 r = !TEST_mem_ne(expected, expected_len, got, got_len);
92 r = TEST_mem_eq(expected, expected_len, got, got_len);
99 * Structure used to hold a list of blocks of memory to test
100 * calls to "update" like functions.
102 struct evp_test_buffer_st {
109 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
112 OPENSSL_free(db->buf);
118 * append buffer to a list
120 static int evp_test_buffer_append(const char *value,
121 STACK_OF(EVP_TEST_BUFFER) **sk)
123 EVP_TEST_BUFFER *db = NULL;
125 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
128 if (!parse_bin(value, &db->buf, &db->buflen))
133 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
135 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
141 evp_test_buffer_free(db);
146 * replace last buffer in list with copies of itself
148 static int evp_test_buffer_ncopy(const char *value,
149 STACK_OF(EVP_TEST_BUFFER) *sk)
152 unsigned char *tbuf, *p;
154 int ncopy = atoi(value);
159 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
161 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
163 tbuflen = db->buflen * ncopy;
164 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
166 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
167 memcpy(p, db->buf, db->buflen);
169 OPENSSL_free(db->buf);
171 db->buflen = tbuflen;
176 * set repeat count for last buffer in list
178 static int evp_test_buffer_set_count(const char *value,
179 STACK_OF(EVP_TEST_BUFFER) *sk)
182 int count = atoi(value);
187 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
190 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
191 if (db->count_set != 0)
194 db->count = (size_t)count;
200 * call "fn" with each element of the list in turn
202 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
204 const unsigned char *buf,
210 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
211 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
214 for (j = 0; j < tb->count; j++) {
215 if (fn(ctx, tb->buf, tb->buflen) <= 0)
223 * Unescape some sequences in string literals (only \n for now).
224 * Return an allocated buffer, set |out_len|. If |input_len|
225 * is zero, get an empty buffer but set length to zero.
227 static unsigned char* unescape(const char *input, size_t input_len,
230 unsigned char *ret, *p;
233 if (input_len == 0) {
235 return OPENSSL_zalloc(1);
238 /* Escaping is non-expanding; over-allocate original size for simplicity. */
239 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
242 for (i = 0; i < input_len; i++) {
243 if (*input == '\\') {
244 if (i == input_len - 1 || *++input != 'n') {
245 TEST_error("Bad escape sequence in file");
265 * For a hex string "value" convert to a binary allocated buffer.
266 * Return 1 on success or 0 on failure.
268 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
272 /* Check for NULL literal */
273 if (strcmp(value, "NULL") == 0) {
279 /* Check for empty value */
280 if (*value == '\0') {
282 * Don't return NULL for zero length buffer. This is needed for
283 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
284 * buffer even if the key length is 0, in order to detect key reset.
286 *buf = OPENSSL_malloc(1);
294 /* Check for string literal */
295 if (value[0] == '"') {
296 size_t vlen = strlen(++value);
298 if (vlen == 0 || value[vlen - 1] != '"')
301 *buf = unescape(value, vlen, buflen);
302 return *buf == NULL ? 0 : 1;
305 /* Otherwise assume as hex literal and convert it to binary buffer */
306 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
307 TEST_info("Can't convert %s", value);
308 TEST_openssl_errors();
311 /* Size of input buffer means we'll never overflow */
318 *** MESSAGE DIGEST TESTS
321 typedef struct digest_data_st {
322 /* Digest this test is for */
323 const EVP_MD *digest;
324 /* Input to digest */
325 STACK_OF(EVP_TEST_BUFFER) *input;
326 /* Expected output */
327 unsigned char *output;
331 static int digest_test_init(EVP_TEST *t, const char *alg)
334 const EVP_MD *digest;
336 if ((digest = EVP_get_digestbyname(alg)) == NULL) {
337 /* If alg has an OID assume disabled algorithm */
338 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
344 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
347 mdat->digest = digest;
351 static void digest_test_cleanup(EVP_TEST *t)
353 DIGEST_DATA *mdat = t->data;
355 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
356 OPENSSL_free(mdat->output);
359 static int digest_test_parse(EVP_TEST *t,
360 const char *keyword, const char *value)
362 DIGEST_DATA *mdata = t->data;
364 if (strcmp(keyword, "Input") == 0)
365 return evp_test_buffer_append(value, &mdata->input);
366 if (strcmp(keyword, "Output") == 0)
367 return parse_bin(value, &mdata->output, &mdata->output_len);
368 if (strcmp(keyword, "Count") == 0)
369 return evp_test_buffer_set_count(value, mdata->input);
370 if (strcmp(keyword, "Ncopy") == 0)
371 return evp_test_buffer_ncopy(value, mdata->input);
375 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
377 return EVP_DigestUpdate(ctx, buf, buflen);
380 static int digest_test_run(EVP_TEST *t)
382 DIGEST_DATA *expected = t->data;
384 unsigned char *got = NULL;
385 unsigned int got_len;
387 t->err = "TEST_FAILURE";
388 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
391 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
392 expected->output_len : EVP_MAX_MD_SIZE);
396 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
397 t->err = "DIGESTINIT_ERROR";
400 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
401 t->err = "DIGESTUPDATE_ERROR";
405 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
406 got_len = expected->output_len;
407 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
408 t->err = "DIGESTFINALXOF_ERROR";
412 if (!EVP_DigestFinal(mctx, got, &got_len)) {
413 t->err = "DIGESTFINAL_ERROR";
417 if (!TEST_int_eq(expected->output_len, got_len)) {
418 t->err = "DIGEST_LENGTH_MISMATCH";
421 if (!memory_err_compare(t, "DIGEST_MISMATCH",
422 expected->output, expected->output_len,
430 EVP_MD_CTX_free(mctx);
434 static const EVP_TEST_METHOD digest_test_method = {
447 typedef struct cipher_data_st {
448 const EVP_CIPHER *cipher;
450 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
456 unsigned char *plaintext;
457 size_t plaintext_len;
458 unsigned char *ciphertext;
459 size_t ciphertext_len;
460 /* GCM, CCM and OCB only */
467 static int cipher_test_init(EVP_TEST *t, const char *alg)
469 const EVP_CIPHER *cipher;
473 if ((cipher = EVP_get_cipherbyname(alg)) == NULL) {
474 /* If alg has an OID assume disabled algorithm */
475 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
481 cdat = OPENSSL_zalloc(sizeof(*cdat));
482 cdat->cipher = cipher;
484 m = EVP_CIPHER_mode(cipher);
485 if (m == EVP_CIPH_GCM_MODE
486 || m == EVP_CIPH_OCB_MODE
487 || m == EVP_CIPH_CCM_MODE)
489 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
498 static void cipher_test_cleanup(EVP_TEST *t)
500 CIPHER_DATA *cdat = t->data;
502 OPENSSL_free(cdat->key);
503 OPENSSL_free(cdat->iv);
504 OPENSSL_free(cdat->ciphertext);
505 OPENSSL_free(cdat->plaintext);
506 OPENSSL_free(cdat->aad);
507 OPENSSL_free(cdat->tag);
510 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
513 CIPHER_DATA *cdat = t->data;
515 if (strcmp(keyword, "Key") == 0)
516 return parse_bin(value, &cdat->key, &cdat->key_len);
517 if (strcmp(keyword, "IV") == 0)
518 return parse_bin(value, &cdat->iv, &cdat->iv_len);
519 if (strcmp(keyword, "Plaintext") == 0)
520 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
521 if (strcmp(keyword, "Ciphertext") == 0)
522 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
524 if (strcmp(keyword, "AAD") == 0)
525 return parse_bin(value, &cdat->aad, &cdat->aad_len);
526 if (strcmp(keyword, "Tag") == 0)
527 return parse_bin(value, &cdat->tag, &cdat->tag_len);
530 if (strcmp(keyword, "Operation") == 0) {
531 if (strcmp(value, "ENCRYPT") == 0)
533 else if (strcmp(value, "DECRYPT") == 0)
542 static int cipher_test_enc(EVP_TEST *t, int enc,
543 size_t out_misalign, size_t inp_misalign, int frag)
545 CIPHER_DATA *expected = t->data;
546 unsigned char *in, *expected_out, *tmp = NULL;
547 size_t in_len, out_len, donelen = 0;
548 int ok = 0, tmplen, chunklen, tmpflen;
549 EVP_CIPHER_CTX *ctx = NULL;
551 t->err = "TEST_FAILURE";
552 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
554 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
556 in = expected->plaintext;
557 in_len = expected->plaintext_len;
558 expected_out = expected->ciphertext;
559 out_len = expected->ciphertext_len;
561 in = expected->ciphertext;
562 in_len = expected->ciphertext_len;
563 expected_out = expected->plaintext;
564 out_len = expected->plaintext_len;
566 if (inp_misalign == (size_t)-1) {
568 * Exercise in-place encryption
570 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
573 in = memcpy(tmp + out_misalign, in, in_len);
575 inp_misalign += 16 - ((out_misalign + in_len) & 15);
577 * 'tmp' will store both output and copy of input. We make the copy
578 * of input to specifically aligned part of 'tmp'. So we just
579 * figured out how much padding would ensure the required alignment,
580 * now we allocate extended buffer and finally copy the input just
581 * past inp_misalign in expression below. Output will be written
582 * past out_misalign...
584 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
585 inp_misalign + in_len);
588 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
589 inp_misalign, in, in_len);
591 if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
592 t->err = "CIPHERINIT_ERROR";
596 if (expected->aead) {
597 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
598 expected->iv_len, 0)) {
599 t->err = "INVALID_IV_LENGTH";
602 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
603 t->err = "INVALID_IV_LENGTH";
607 if (expected->aead) {
610 * If encrypting or OCB just set tag length initially, otherwise
611 * set tag length and value.
613 if (enc || expected->aead == EVP_CIPH_OCB_MODE) {
614 t->err = "TAG_LENGTH_SET_ERROR";
617 t->err = "TAG_SET_ERROR";
620 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
621 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
622 expected->tag_len, tag))
627 if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
628 t->err = "INVALID_KEY_LENGTH";
631 if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
632 t->err = "KEY_SET_ERROR";
636 if (!enc && expected->aead == EVP_CIPH_OCB_MODE) {
637 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
638 expected->tag_len, expected->tag)) {
639 t->err = "TAG_SET_ERROR";
644 if (expected->aead == EVP_CIPH_CCM_MODE) {
645 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
646 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
651 t->err = "AAD_SET_ERROR";
653 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad,
658 * Supply the AAD in chunks less than the block size where possible
660 if (expected->aad_len > 0) {
661 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, 1))
665 if (expected->aad_len > 2) {
666 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
667 expected->aad + donelen,
668 expected->aad_len - 2))
670 donelen += expected->aad_len - 2;
672 if (expected->aad_len > 1
673 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
674 expected->aad + donelen, 1))
678 EVP_CIPHER_CTX_set_padding(ctx, 0);
679 t->err = "CIPHERUPDATE_ERROR";
682 /* We supply the data all in one go */
683 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
686 /* Supply the data in chunks less than the block size where possible */
688 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
695 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
703 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
709 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
710 t->err = "CIPHERFINAL_ERROR";
713 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
714 tmp + out_misalign, tmplen + tmpflen))
716 if (enc && expected->aead) {
717 unsigned char rtag[16];
719 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
720 t->err = "TAG_LENGTH_INTERNAL_ERROR";
723 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
724 expected->tag_len, rtag)) {
725 t->err = "TAG_RETRIEVE_ERROR";
728 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
729 expected->tag, expected->tag_len,
730 rtag, expected->tag_len))
737 EVP_CIPHER_CTX_free(ctx);
741 static int cipher_test_run(EVP_TEST *t)
743 CIPHER_DATA *cdat = t->data;
745 size_t out_misalign, inp_misalign;
751 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
752 /* IV is optional and usually omitted in wrap mode */
753 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
758 if (cdat->aead && !cdat->tag) {
762 for (out_misalign = 0; out_misalign <= 1;) {
763 static char aux_err[64];
764 t->aux_err = aux_err;
765 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
766 if (inp_misalign == (size_t)-1) {
767 /* kludge: inp_misalign == -1 means "exercise in-place" */
768 BIO_snprintf(aux_err, sizeof(aux_err),
769 "%s in-place, %sfragmented",
770 out_misalign ? "misaligned" : "aligned",
773 BIO_snprintf(aux_err, sizeof(aux_err),
774 "%s output and %s input, %sfragmented",
775 out_misalign ? "misaligned" : "aligned",
776 inp_misalign ? "misaligned" : "aligned",
780 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
781 /* Not fatal errors: return */
788 if (cdat->enc != 1) {
789 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
790 /* Not fatal errors: return */
799 if (out_misalign == 1 && frag == 0) {
801 * XTS, CCM and Wrap modes have special requirements about input
802 * lengths so we don't fragment for those
804 if (cdat->aead == EVP_CIPH_CCM_MODE
805 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
806 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
819 static const EVP_TEST_METHOD cipher_test_method = {
832 typedef struct mac_data_st {
835 /* Algorithm string for this MAC */
841 unsigned char *input;
843 /* Expected output */
844 unsigned char *output;
846 /* Collection of controls */
847 STACK_OF(OPENSSL_STRING) *controls;
850 static int mac_test_init(EVP_TEST *t, const char *alg)
855 if (strcmp(alg, "HMAC") == 0) {
856 type = EVP_PKEY_HMAC;
857 } else if (strcmp(alg, "CMAC") == 0) {
858 #ifndef OPENSSL_NO_CMAC
859 type = EVP_PKEY_CMAC;
864 } else if (strcmp(alg, "Poly1305") == 0) {
865 #ifndef OPENSSL_NO_POLY1305
866 type = EVP_PKEY_POLY1305;
871 } else if (strcmp(alg, "SipHash") == 0) {
872 #ifndef OPENSSL_NO_SIPHASH
873 type = EVP_PKEY_SIPHASH;
881 mdat = OPENSSL_zalloc(sizeof(*mdat));
883 mdat->controls = sk_OPENSSL_STRING_new_null();
888 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
889 static void openssl_free(char *m)
894 static void mac_test_cleanup(EVP_TEST *t)
896 MAC_DATA *mdat = t->data;
898 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
899 OPENSSL_free(mdat->alg);
900 OPENSSL_free(mdat->key);
901 OPENSSL_free(mdat->input);
902 OPENSSL_free(mdat->output);
905 static int mac_test_parse(EVP_TEST *t,
906 const char *keyword, const char *value)
908 MAC_DATA *mdata = t->data;
910 if (strcmp(keyword, "Key") == 0)
911 return parse_bin(value, &mdata->key, &mdata->key_len);
912 if (strcmp(keyword, "Algorithm") == 0) {
913 mdata->alg = OPENSSL_strdup(value);
918 if (strcmp(keyword, "Input") == 0)
919 return parse_bin(value, &mdata->input, &mdata->input_len);
920 if (strcmp(keyword, "Output") == 0)
921 return parse_bin(value, &mdata->output, &mdata->output_len);
922 if (strcmp(keyword, "Ctrl") == 0)
923 return sk_OPENSSL_STRING_push(mdata->controls,
924 OPENSSL_strdup(value)) != 0;
928 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
934 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
936 p = strchr(tmpval, ':');
939 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
941 t->err = "PKEY_CTRL_INVALID";
943 t->err = "PKEY_CTRL_ERROR";
946 OPENSSL_free(tmpval);
950 static int mac_test_run(EVP_TEST *t)
952 MAC_DATA *expected = t->data;
953 EVP_MD_CTX *mctx = NULL;
954 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
955 EVP_PKEY *key = NULL;
956 const EVP_MD *md = NULL;
957 unsigned char *got = NULL;
961 #ifdef OPENSSL_NO_DES
962 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
969 if (expected->type == EVP_PKEY_CMAC)
970 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
971 EVP_get_cipherbyname(expected->alg));
973 key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
976 t->err = "MAC_KEY_CREATE_ERROR";
980 if (expected->type == EVP_PKEY_HMAC) {
981 if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
982 t->err = "MAC_ALGORITHM_SET_ERROR";
986 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
987 t->err = "INTERNAL_ERROR";
990 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
991 t->err = "DIGESTSIGNINIT_ERROR";
994 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
995 if (!mac_test_ctrl_pkey(t, pctx,
996 sk_OPENSSL_STRING_value(expected->controls,
998 t->err = "EVPPKEYCTXCTRL_ERROR";
1001 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1002 t->err = "DIGESTSIGNUPDATE_ERROR";
1005 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1006 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1009 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1010 t->err = "TEST_FAILURE";
1013 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1014 || !memory_err_compare(t, "TEST_MAC_ERR",
1015 expected->output, expected->output_len,
1017 t->err = "TEST_MAC_ERR";
1022 EVP_MD_CTX_free(mctx);
1024 EVP_PKEY_CTX_free(genctx);
1029 static const EVP_TEST_METHOD mac_test_method = {
1039 *** PUBLIC KEY TESTS
1040 *** These are all very similar and share much common code.
1043 typedef struct pkey_data_st {
1044 /* Context for this operation */
1046 /* Key operation to perform */
1047 int (*keyop) (EVP_PKEY_CTX *ctx,
1048 unsigned char *sig, size_t *siglen,
1049 const unsigned char *tbs, size_t tbslen);
1051 unsigned char *input;
1053 /* Expected output */
1054 unsigned char *output;
1059 * Perform public key operation setup: lookup key, allocated ctx and call
1060 * the appropriate initialisation function
1062 static int pkey_test_init(EVP_TEST *t, const char *name,
1064 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1065 int (*keyop)(EVP_PKEY_CTX *ctx,
1066 unsigned char *sig, size_t *siglen,
1067 const unsigned char *tbs,
1071 EVP_PKEY *pkey = NULL;
1075 rv = find_key(&pkey, name, public_keys);
1077 rv = find_key(&pkey, name, private_keys);
1078 if (rv == 0 || pkey == NULL) {
1083 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1084 EVP_PKEY_free(pkey);
1087 kdata->keyop = keyop;
1088 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
1089 EVP_PKEY_free(pkey);
1090 OPENSSL_free(kdata);
1093 if (keyopinit(kdata->ctx) <= 0)
1094 t->err = "KEYOP_INIT_ERROR";
1099 static void pkey_test_cleanup(EVP_TEST *t)
1101 PKEY_DATA *kdata = t->data;
1103 OPENSSL_free(kdata->input);
1104 OPENSSL_free(kdata->output);
1105 EVP_PKEY_CTX_free(kdata->ctx);
1108 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1114 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1116 p = strchr(tmpval, ':');
1119 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1121 t->err = "PKEY_CTRL_INVALID";
1123 } else if (p != NULL && rv <= 0) {
1124 /* If p has an OID and lookup fails assume disabled algorithm */
1125 int nid = OBJ_sn2nid(p);
1127 if (nid == NID_undef)
1128 nid = OBJ_ln2nid(p);
1129 if (nid != NID_undef
1130 && EVP_get_digestbynid(nid) == NULL
1131 && EVP_get_cipherbynid(nid) == NULL) {
1135 t->err = "PKEY_CTRL_ERROR";
1139 OPENSSL_free(tmpval);
1143 static int pkey_test_parse(EVP_TEST *t,
1144 const char *keyword, const char *value)
1146 PKEY_DATA *kdata = t->data;
1147 if (strcmp(keyword, "Input") == 0)
1148 return parse_bin(value, &kdata->input, &kdata->input_len);
1149 if (strcmp(keyword, "Output") == 0)
1150 return parse_bin(value, &kdata->output, &kdata->output_len);
1151 if (strcmp(keyword, "Ctrl") == 0)
1152 return pkey_test_ctrl(t, kdata->ctx, value);
1156 static int pkey_test_run(EVP_TEST *t)
1158 PKEY_DATA *expected = t->data;
1159 unsigned char *got = NULL;
1162 if (expected->keyop(expected->ctx, NULL, &got_len,
1163 expected->input, expected->input_len) <= 0
1164 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1165 t->err = "KEYOP_LENGTH_ERROR";
1168 if (expected->keyop(expected->ctx, got, &got_len,
1169 expected->input, expected->input_len) <= 0) {
1170 t->err = "KEYOP_ERROR";
1173 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1174 expected->output, expected->output_len,
1184 static int sign_test_init(EVP_TEST *t, const char *name)
1186 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1189 static const EVP_TEST_METHOD psign_test_method = {
1197 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1199 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1200 EVP_PKEY_verify_recover);
1203 static const EVP_TEST_METHOD pverify_recover_test_method = {
1205 verify_recover_test_init,
1211 static int decrypt_test_init(EVP_TEST *t, const char *name)
1213 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1217 static const EVP_TEST_METHOD pdecrypt_test_method = {
1225 static int verify_test_init(EVP_TEST *t, const char *name)
1227 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1230 static int verify_test_run(EVP_TEST *t)
1232 PKEY_DATA *kdata = t->data;
1234 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1235 kdata->input, kdata->input_len) <= 0)
1236 t->err = "VERIFY_ERROR";
1240 static const EVP_TEST_METHOD pverify_test_method = {
1249 static int pderive_test_init(EVP_TEST *t, const char *name)
1251 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1254 static int pderive_test_parse(EVP_TEST *t,
1255 const char *keyword, const char *value)
1257 PKEY_DATA *kdata = t->data;
1259 if (strcmp(keyword, "PeerKey") == 0) {
1261 if (find_key(&peer, value, public_keys) == 0)
1263 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1267 if (strcmp(keyword, "SharedSecret") == 0)
1268 return parse_bin(value, &kdata->output, &kdata->output_len);
1269 if (strcmp(keyword, "Ctrl") == 0)
1270 return pkey_test_ctrl(t, kdata->ctx, value);
1274 static int pderive_test_run(EVP_TEST *t)
1276 PKEY_DATA *expected = t->data;
1277 unsigned char *got = NULL;
1280 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1281 t->err = "DERIVE_ERROR";
1284 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1285 t->err = "DERIVE_ERROR";
1288 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1289 t->err = "DERIVE_ERROR";
1292 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1293 expected->output, expected->output_len,
1303 static const EVP_TEST_METHOD pderive_test_method = {
1316 typedef enum pbe_type_enum {
1317 PBE_TYPE_INVALID = 0,
1318 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1321 typedef struct pbe_data_st {
1323 /* scrypt parameters */
1324 uint64_t N, r, p, maxmem;
1325 /* PKCS#12 parameters */
1329 unsigned char *pass;
1332 unsigned char *salt;
1334 /* Expected output */
1339 #ifndef OPENSSL_NO_SCRYPT
1341 * Parse unsigned decimal 64 bit integer value
1343 static int parse_uint64(const char *value, uint64_t *pr)
1345 const char *p = value;
1347 if (!TEST_true(*p)) {
1348 TEST_info("Invalid empty integer value");
1351 for (*pr = 0; *p; ) {
1352 if (*pr > UINT64_MAX / 10) {
1353 TEST_error("Integer overflow in string %s", value);
1357 if (!TEST_true(isdigit((unsigned char)*p))) {
1358 TEST_error("Invalid character in string %s", value);
1367 static int scrypt_test_parse(EVP_TEST *t,
1368 const char *keyword, const char *value)
1370 PBE_DATA *pdata = t->data;
1372 if (strcmp(keyword, "N") == 0)
1373 return parse_uint64(value, &pdata->N);
1374 if (strcmp(keyword, "p") == 0)
1375 return parse_uint64(value, &pdata->p);
1376 if (strcmp(keyword, "r") == 0)
1377 return parse_uint64(value, &pdata->r);
1378 if (strcmp(keyword, "maxmem") == 0)
1379 return parse_uint64(value, &pdata->maxmem);
1384 static int pbkdf2_test_parse(EVP_TEST *t,
1385 const char *keyword, const char *value)
1387 PBE_DATA *pdata = t->data;
1389 if (strcmp(keyword, "iter") == 0) {
1390 pdata->iter = atoi(value);
1391 if (pdata->iter <= 0)
1395 if (strcmp(keyword, "MD") == 0) {
1396 pdata->md = EVP_get_digestbyname(value);
1397 if (pdata->md == NULL)
1404 static int pkcs12_test_parse(EVP_TEST *t,
1405 const char *keyword, const char *value)
1407 PBE_DATA *pdata = t->data;
1409 if (strcmp(keyword, "id") == 0) {
1410 pdata->id = atoi(value);
1415 return pbkdf2_test_parse(t, keyword, value);
1418 static int pbe_test_init(EVP_TEST *t, const char *alg)
1421 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1423 if (strcmp(alg, "scrypt") == 0) {
1424 #ifndef OPENSSL_NO_SCRYPT
1425 pbe_type = PBE_TYPE_SCRYPT;
1430 } else if (strcmp(alg, "pbkdf2") == 0) {
1431 pbe_type = PBE_TYPE_PBKDF2;
1432 } else if (strcmp(alg, "pkcs12") == 0) {
1433 pbe_type = PBE_TYPE_PKCS12;
1435 TEST_error("Unknown pbe algorithm %s", alg);
1437 pdat = OPENSSL_zalloc(sizeof(*pdat));
1438 pdat->pbe_type = pbe_type;
1443 static void pbe_test_cleanup(EVP_TEST *t)
1445 PBE_DATA *pdat = t->data;
1447 OPENSSL_free(pdat->pass);
1448 OPENSSL_free(pdat->salt);
1449 OPENSSL_free(pdat->key);
1452 static int pbe_test_parse(EVP_TEST *t,
1453 const char *keyword, const char *value)
1455 PBE_DATA *pdata = t->data;
1457 if (strcmp(keyword, "Password") == 0)
1458 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1459 if (strcmp(keyword, "Salt") == 0)
1460 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1461 if (strcmp(keyword, "Key") == 0)
1462 return parse_bin(value, &pdata->key, &pdata->key_len);
1463 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1464 return pbkdf2_test_parse(t, keyword, value);
1465 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1466 return pkcs12_test_parse(t, keyword, value);
1467 #ifndef OPENSSL_NO_SCRYPT
1468 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1469 return scrypt_test_parse(t, keyword, value);
1474 static int pbe_test_run(EVP_TEST *t)
1476 PBE_DATA *expected = t->data;
1479 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1480 t->err = "INTERNAL_ERROR";
1483 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1484 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1485 expected->salt, expected->salt_len,
1486 expected->iter, expected->md,
1487 expected->key_len, key) == 0) {
1488 t->err = "PBKDF2_ERROR";
1491 #ifndef OPENSSL_NO_SCRYPT
1492 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1493 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1494 expected->salt, expected->salt_len, expected->N,
1495 expected->r, expected->p, expected->maxmem,
1496 key, expected->key_len) == 0) {
1497 t->err = "SCRYPT_ERROR";
1501 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1502 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1503 expected->salt, expected->salt_len,
1504 expected->id, expected->iter, expected->key_len,
1505 key, expected->md) == 0) {
1506 t->err = "PKCS12_ERROR";
1510 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1511 key, expected->key_len))
1520 static const EVP_TEST_METHOD pbe_test_method = {
1534 BASE64_CANONICAL_ENCODING = 0,
1535 BASE64_VALID_ENCODING = 1,
1536 BASE64_INVALID_ENCODING = 2
1537 } base64_encoding_type;
1539 typedef struct encode_data_st {
1540 /* Input to encoding */
1541 unsigned char *input;
1543 /* Expected output */
1544 unsigned char *output;
1546 base64_encoding_type encoding;
1549 static int encode_test_init(EVP_TEST *t, const char *encoding)
1553 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1555 if (strcmp(encoding, "canonical") == 0) {
1556 edata->encoding = BASE64_CANONICAL_ENCODING;
1557 } else if (strcmp(encoding, "valid") == 0) {
1558 edata->encoding = BASE64_VALID_ENCODING;
1559 } else if (strcmp(encoding, "invalid") == 0) {
1560 edata->encoding = BASE64_INVALID_ENCODING;
1561 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1564 TEST_error("Bad encoding: %s."
1565 " Should be one of {canonical, valid, invalid}",
1572 OPENSSL_free(edata);
1576 static void encode_test_cleanup(EVP_TEST *t)
1578 ENCODE_DATA *edata = t->data;
1580 OPENSSL_free(edata->input);
1581 OPENSSL_free(edata->output);
1582 memset(edata, 0, sizeof(*edata));
1585 static int encode_test_parse(EVP_TEST *t,
1586 const char *keyword, const char *value)
1588 ENCODE_DATA *edata = t->data;
1590 if (strcmp(keyword, "Input") == 0)
1591 return parse_bin(value, &edata->input, &edata->input_len);
1592 if (strcmp(keyword, "Output") == 0)
1593 return parse_bin(value, &edata->output, &edata->output_len);
1597 static int encode_test_run(EVP_TEST *t)
1599 ENCODE_DATA *expected = t->data;
1600 unsigned char *encode_out = NULL, *decode_out = NULL;
1601 int output_len, chunk_len;
1602 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1604 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1605 t->err = "INTERNAL_ERROR";
1609 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1611 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1612 || !TEST_ptr(encode_out =
1613 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1616 EVP_EncodeInit(encode_ctx);
1617 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1618 expected->input, expected->input_len)))
1621 output_len = chunk_len;
1623 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1624 output_len += chunk_len;
1626 if (!memory_err_compare(t, "BAD_ENCODING",
1627 expected->output, expected->output_len,
1628 encode_out, output_len))
1632 if (!TEST_ptr(decode_out =
1633 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
1636 EVP_DecodeInit(decode_ctx);
1637 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
1638 expected->output_len) < 0) {
1639 t->err = "DECODE_ERROR";
1642 output_len = chunk_len;
1644 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1645 t->err = "DECODE_ERROR";
1648 output_len += chunk_len;
1650 if (expected->encoding != BASE64_INVALID_ENCODING
1651 && !memory_err_compare(t, "BAD_DECODING",
1652 expected->input, expected->input_len,
1653 decode_out, output_len)) {
1654 t->err = "BAD_DECODING";
1660 OPENSSL_free(encode_out);
1661 OPENSSL_free(decode_out);
1662 EVP_ENCODE_CTX_free(decode_ctx);
1663 EVP_ENCODE_CTX_free(encode_ctx);
1667 static const EVP_TEST_METHOD encode_test_method = {
1670 encode_test_cleanup,
1679 typedef struct kdf_data_st {
1680 /* Context for this operation */
1682 /* Expected output */
1683 unsigned char *output;
1688 * Perform public key operation setup: lookup key, allocated ctx and call
1689 * the appropriate initialisation function
1691 static int kdf_test_init(EVP_TEST *t, const char *name)
1694 int kdf_nid = OBJ_sn2nid(name);
1696 #ifdef OPENSSL_NO_SCRYPT
1697 if (strcmp(name, "scrypt") == 0) {
1703 if (kdf_nid == NID_undef)
1704 kdf_nid = OBJ_ln2nid(name);
1706 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
1708 kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
1709 if (kdata->ctx == NULL) {
1710 OPENSSL_free(kdata);
1713 if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
1714 EVP_PKEY_CTX_free(kdata->ctx);
1715 OPENSSL_free(kdata);
1722 static void kdf_test_cleanup(EVP_TEST *t)
1724 KDF_DATA *kdata = t->data;
1725 OPENSSL_free(kdata->output);
1726 EVP_PKEY_CTX_free(kdata->ctx);
1729 static int kdf_test_parse(EVP_TEST *t,
1730 const char *keyword, const char *value)
1732 KDF_DATA *kdata = t->data;
1734 if (strcmp(keyword, "Output") == 0)
1735 return parse_bin(value, &kdata->output, &kdata->output_len);
1736 if (strncmp(keyword, "Ctrl", 4) == 0)
1737 return pkey_test_ctrl(t, kdata->ctx, value);
1741 static int kdf_test_run(EVP_TEST *t)
1743 KDF_DATA *expected = t->data;
1744 unsigned char *got = NULL;
1745 size_t got_len = expected->output_len;
1747 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1748 t->err = "INTERNAL_ERROR";
1751 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1752 t->err = "KDF_DERIVE_ERROR";
1755 if (!memory_err_compare(t, "KDF_MISMATCH",
1756 expected->output, expected->output_len,
1767 static const EVP_TEST_METHOD kdf_test_method = {
1780 typedef struct keypair_test_data_st {
1783 } KEYPAIR_TEST_DATA;
1785 static int keypair_test_init(EVP_TEST *t, const char *pair)
1787 KEYPAIR_TEST_DATA *data;
1789 EVP_PKEY *pk = NULL, *pubk = NULL;
1790 char *pub, *priv = NULL;
1792 /* Split private and public names. */
1793 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1794 || !TEST_ptr(pub = strchr(priv, ':'))) {
1795 t->err = "PARSING_ERROR";
1800 if (!TEST_true(find_key(&pk, priv, private_keys))) {
1801 TEST_info("Can't find private key: %s", priv);
1802 t->err = "MISSING_PRIVATE_KEY";
1805 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
1806 TEST_info("Can't find public key: %s", pub);
1807 t->err = "MISSING_PUBLIC_KEY";
1811 if (pk == NULL && pubk == NULL) {
1812 /* Both keys are listed but unsupported: skip this test */
1818 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
1831 static void keypair_test_cleanup(EVP_TEST *t)
1833 OPENSSL_free(t->data);
1838 * For tests that do not accept any custom keywords.
1840 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
1845 static int keypair_test_run(EVP_TEST *t)
1848 const KEYPAIR_TEST_DATA *pair = t->data;
1850 if (pair->privk == NULL || pair->pubk == NULL) {
1852 * this can only happen if only one of the keys is not set
1853 * which means that one of them was unsupported while the
1854 * other isn't: hence a key type mismatch.
1856 t->err = "KEYPAIR_TYPE_MISMATCH";
1861 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
1863 t->err = "KEYPAIR_MISMATCH";
1864 } else if ( -1 == rv ) {
1865 t->err = "KEYPAIR_TYPE_MISMATCH";
1866 } else if ( -2 == rv ) {
1867 t->err = "UNSUPPORTED_KEY_COMPARISON";
1869 TEST_error("Unexpected error in key comparison");
1884 static const EVP_TEST_METHOD keypair_test_method = {
1887 keypair_test_cleanup,
1896 typedef struct keygen_test_data_st {
1897 EVP_PKEY_CTX *genctx; /* Keygen context to use */
1898 char *keyname; /* Key name to store key or NULL */
1901 static int keygen_test_init(EVP_TEST *t, const char *alg)
1903 KEYGEN_TEST_DATA *data;
1904 EVP_PKEY_CTX *genctx;
1905 int nid = OBJ_sn2nid(alg);
1907 if (nid == NID_undef) {
1908 nid = OBJ_ln2nid(alg);
1909 if (nid == NID_undef)
1913 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
1914 /* assume algorithm disabled */
1919 if (EVP_PKEY_keygen_init(genctx) <= 0) {
1920 t->err = "KEYGEN_INIT_ERROR";
1924 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
1926 data->genctx = genctx;
1927 data->keyname = NULL;
1933 EVP_PKEY_CTX_free(genctx);
1937 static void keygen_test_cleanup(EVP_TEST *t)
1939 KEYGEN_TEST_DATA *keygen = t->data;
1941 EVP_PKEY_CTX_free(keygen->genctx);
1942 OPENSSL_free(keygen->keyname);
1943 OPENSSL_free(t->data);
1947 static int keygen_test_parse(EVP_TEST *t,
1948 const char *keyword, const char *value)
1950 KEYGEN_TEST_DATA *keygen = t->data;
1952 if (strcmp(keyword, "KeyName") == 0)
1953 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
1954 if (strcmp(keyword, "Ctrl") == 0)
1955 return pkey_test_ctrl(t, keygen->genctx, value);
1959 static int keygen_test_run(EVP_TEST *t)
1961 KEYGEN_TEST_DATA *keygen = t->data;
1962 EVP_PKEY *pkey = NULL;
1965 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
1966 t->err = "KEYGEN_GENERATE_ERROR";
1970 if (keygen->keyname != NULL) {
1973 if (find_key(NULL, keygen->keyname, private_keys)) {
1974 TEST_info("Duplicate key %s", keygen->keyname);
1978 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
1980 key->name = keygen->keyname;
1981 keygen->keyname = NULL;
1983 key->next = private_keys;
1986 EVP_PKEY_free(pkey);
1992 EVP_PKEY_free(pkey);
1996 static const EVP_TEST_METHOD keygen_test_method = {
1999 keygen_test_cleanup,
2005 *** DIGEST SIGN+VERIFY TESTS
2009 int is_verify; /* Set to 1 if verifying */
2010 int is_oneshot; /* Set to 1 for one shot operation */
2011 const EVP_MD *md; /* Digest to use */
2012 EVP_MD_CTX *ctx; /* Digest context */
2014 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2015 unsigned char *osin; /* Input data if one shot */
2016 size_t osin_len; /* Input length data if one shot */
2017 unsigned char *output; /* Expected output */
2018 size_t output_len; /* Expected output length */
2021 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2024 const EVP_MD *md = NULL;
2025 DIGESTSIGN_DATA *mdat;
2027 if (strcmp(alg, "NULL") != 0) {
2028 if ((md = EVP_get_digestbyname(alg)) == NULL) {
2029 /* If alg has an OID assume disabled algorithm */
2030 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
2037 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2040 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2044 mdat->is_verify = is_verify;
2045 mdat->is_oneshot = is_oneshot;
2050 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2052 return digestsigver_test_init(t, alg, 0, 0);
2055 static void digestsigver_test_cleanup(EVP_TEST *t)
2057 DIGESTSIGN_DATA *mdata = t->data;
2059 EVP_MD_CTX_free(mdata->ctx);
2060 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2061 OPENSSL_free(mdata->osin);
2062 OPENSSL_free(mdata->output);
2063 OPENSSL_free(mdata);
2067 static int digestsigver_test_parse(EVP_TEST *t,
2068 const char *keyword, const char *value)
2070 DIGESTSIGN_DATA *mdata = t->data;
2072 if (strcmp(keyword, "Key") == 0) {
2073 EVP_PKEY *pkey = NULL;
2076 if (mdata->is_verify)
2077 rv = find_key(&pkey, value, public_keys);
2079 rv = find_key(&pkey, value, private_keys);
2080 if (rv == 0 || pkey == NULL) {
2084 if (mdata->is_verify) {
2085 if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
2087 t->err = "DIGESTVERIFYINIT_ERROR";
2090 if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
2092 t->err = "DIGESTSIGNINIT_ERROR";
2096 if (strcmp(keyword, "Input") == 0) {
2097 if (mdata->is_oneshot)
2098 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2099 return evp_test_buffer_append(value, &mdata->input);
2101 if (strcmp(keyword, "Output") == 0)
2102 return parse_bin(value, &mdata->output, &mdata->output_len);
2104 if (!mdata->is_oneshot) {
2105 if (strcmp(keyword, "Count") == 0)
2106 return evp_test_buffer_set_count(value, mdata->input);
2107 if (strcmp(keyword, "Ncopy") == 0)
2108 return evp_test_buffer_ncopy(value, mdata->input);
2110 if (strcmp(keyword, "Ctrl") == 0) {
2111 if (mdata->pctx == NULL)
2113 return pkey_test_ctrl(t, mdata->pctx, value);
2118 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2121 return EVP_DigestSignUpdate(ctx, buf, buflen);
2124 static int digestsign_test_run(EVP_TEST *t)
2126 DIGESTSIGN_DATA *expected = t->data;
2127 unsigned char *got = NULL;
2130 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2132 t->err = "DIGESTUPDATE_ERROR";
2136 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2137 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2140 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2141 t->err = "MALLOC_FAILURE";
2144 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2145 t->err = "DIGESTSIGNFINAL_ERROR";
2148 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2149 expected->output, expected->output_len,
2159 static const EVP_TEST_METHOD digestsign_test_method = {
2161 digestsign_test_init,
2162 digestsigver_test_cleanup,
2163 digestsigver_test_parse,
2167 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2169 return digestsigver_test_init(t, alg, 1, 0);
2172 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2175 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2178 static int digestverify_test_run(EVP_TEST *t)
2180 DIGESTSIGN_DATA *mdata = t->data;
2182 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2183 t->err = "DIGESTUPDATE_ERROR";
2187 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2188 mdata->output_len) <= 0)
2189 t->err = "VERIFY_ERROR";
2193 static const EVP_TEST_METHOD digestverify_test_method = {
2195 digestverify_test_init,
2196 digestsigver_test_cleanup,
2197 digestsigver_test_parse,
2198 digestverify_test_run
2201 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
2203 return digestsigver_test_init(t, alg, 0, 1);
2206 static int oneshot_digestsign_test_run(EVP_TEST *t)
2208 DIGESTSIGN_DATA *expected = t->data;
2209 unsigned char *got = NULL;
2212 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
2213 expected->osin, expected->osin_len)) {
2214 t->err = "DIGESTSIGN_LENGTH_ERROR";
2217 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2218 t->err = "MALLOC_FAILURE";
2221 if (!EVP_DigestSign(expected->ctx, got, &got_len,
2222 expected->osin, expected->osin_len)) {
2223 t->err = "DIGESTSIGN_ERROR";
2226 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2227 expected->output, expected->output_len,
2237 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
2238 "OneShotDigestSign",
2239 oneshot_digestsign_test_init,
2240 digestsigver_test_cleanup,
2241 digestsigver_test_parse,
2242 oneshot_digestsign_test_run
2245 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
2247 return digestsigver_test_init(t, alg, 1, 1);
2250 static int oneshot_digestverify_test_run(EVP_TEST *t)
2252 DIGESTSIGN_DATA *mdata = t->data;
2254 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
2255 mdata->osin, mdata->osin_len) <= 0)
2256 t->err = "VERIFY_ERROR";
2260 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
2261 "OneShotDigestVerify",
2262 oneshot_digestverify_test_init,
2263 digestsigver_test_cleanup,
2264 digestsigver_test_parse,
2265 oneshot_digestverify_test_run
2270 *** PARSING AND DISPATCH
2273 static const EVP_TEST_METHOD *evp_test_list[] = {
2274 &cipher_test_method,
2275 &digest_test_method,
2276 &digestsign_test_method,
2277 &digestverify_test_method,
2278 &encode_test_method,
2280 &keypair_test_method,
2281 &keygen_test_method,
2283 &oneshot_digestsign_test_method,
2284 &oneshot_digestverify_test_method,
2286 &pdecrypt_test_method,
2287 &pderive_test_method,
2289 &pverify_recover_test_method,
2290 &pverify_test_method,
2294 static const EVP_TEST_METHOD *find_test(const char *name)
2296 const EVP_TEST_METHOD **tt;
2298 for (tt = evp_test_list; *tt; tt++) {
2299 if (strcmp(name, (*tt)->name) == 0)
2305 static void clear_test(EVP_TEST *t)
2307 test_clearstanza(&t->s);
2309 if (t->data != NULL) {
2310 if (t->meth != NULL)
2311 t->meth->cleanup(t);
2312 OPENSSL_free(t->data);
2315 OPENSSL_free(t->expected_err);
2316 t->expected_err = NULL;
2317 OPENSSL_free(t->func);
2319 OPENSSL_free(t->reason);
2329 * Check for errors in the test structure; return 1 if okay, else 0.
2331 static int check_test_error(EVP_TEST *t)
2337 if (t->err == NULL && t->expected_err == NULL)
2339 if (t->err != NULL && t->expected_err == NULL) {
2340 if (t->aux_err != NULL) {
2341 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2342 t->s.test_file, t->s.start, t->aux_err, t->err);
2344 TEST_info("%s:%d: Source of above error; unexpected error %s",
2345 t->s.test_file, t->s.start, t->err);
2349 if (t->err == NULL && t->expected_err != NULL) {
2350 TEST_info("%s:%d: Succeeded but was expecting %s",
2351 t->s.test_file, t->s.start, t->expected_err);
2355 if (strcmp(t->err, t->expected_err) != 0) {
2356 TEST_info("%s:%d: Expected %s got %s",
2357 t->s.test_file, t->s.start, t->expected_err, t->err);
2361 if (t->func == NULL && t->reason == NULL)
2364 if (t->func == NULL || t->reason == NULL) {
2365 TEST_info("%s:%d: Test is missing function or reason code",
2366 t->s.test_file, t->s.start);
2370 err = ERR_peek_error();
2372 TEST_info("%s:%d: Expected error \"%s:%s\" not set",
2373 t->s.test_file, t->s.start, t->func, t->reason);
2377 func = ERR_func_error_string(err);
2378 reason = ERR_reason_error_string(err);
2379 if (func == NULL && reason == NULL) {
2380 TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
2382 t->s.test_file, t->s.start, t->func, t->reason);
2386 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
2389 TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
2390 t->s.test_file, t->s.start, t->func, t->reason, func, reason);
2396 * Run a parsed test. Log a message and return 0 on error.
2398 static int run_test(EVP_TEST *t)
2400 if (t->meth == NULL)
2407 if (t->err == NULL && t->meth->run_test(t) != 1) {
2408 TEST_info("%s:%d %s error",
2409 t->s.test_file, t->s.start, t->meth->name);
2412 if (!check_test_error(t)) {
2413 TEST_openssl_errors();
2422 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
2424 for (; lst != NULL; lst = lst->next) {
2425 if (strcmp(lst->name, name) == 0) {
2434 static void free_key_list(KEY_LIST *lst)
2436 while (lst != NULL) {
2437 KEY_LIST *next = lst->next;
2439 EVP_PKEY_free(lst->key);
2440 OPENSSL_free(lst->name);
2447 * Is the key type an unsupported algorithm?
2449 static int key_unsupported(void)
2451 long err = ERR_peek_error();
2453 if (ERR_GET_LIB(err) == ERR_LIB_EVP
2454 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
2458 #ifndef OPENSSL_NO_EC
2460 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2461 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2464 if (ERR_GET_LIB(err) == ERR_LIB_EC
2465 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
2469 #endif /* OPENSSL_NO_EC */
2474 * NULL out the value from |pp| but return it. This "steals" a pointer.
2476 static char *take_value(PAIR *pp)
2478 char *p = pp->value;
2485 * Read and parse one test. Return 0 if failure, 1 if okay.
2487 static int parse(EVP_TEST *t)
2489 KEY_LIST *key, **klist;
2496 if (BIO_eof(t->s.fp))
2499 if (!test_readstanza(&t->s))
2501 } while (t->s.numpairs == 0);
2502 pp = &t->s.pairs[0];
2504 /* Are we adding a key? */
2507 if (strcmp(pp->key, "PrivateKey") == 0) {
2508 pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
2509 if (pkey == NULL && !key_unsupported()) {
2510 EVP_PKEY_free(pkey);
2511 TEST_info("Can't read private key %s", pp->value);
2512 TEST_openssl_errors();
2515 klist = &private_keys;
2516 } else if (strcmp(pp->key, "PublicKey") == 0) {
2517 pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
2518 if (pkey == NULL && !key_unsupported()) {
2519 EVP_PKEY_free(pkey);
2520 TEST_info("Can't read public key %s", pp->value);
2521 TEST_openssl_errors();
2524 klist = &public_keys;
2525 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
2526 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
2527 char *strnid = NULL, *keydata = NULL;
2528 unsigned char *keybin;
2532 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
2533 klist = &private_keys;
2535 klist = &public_keys;
2537 strnid = strchr(pp->value, ':');
2538 if (strnid != NULL) {
2540 keydata = strchr(strnid, ':');
2541 if (keydata != NULL)
2544 if (keydata == NULL) {
2545 TEST_info("Failed to parse %s value", pp->key);
2549 nid = OBJ_txt2nid(strnid);
2550 if (nid == NID_undef) {
2551 TEST_info("Uncrecognised algorithm NID");
2554 if (!parse_bin(keydata, &keybin, &keylen)) {
2555 TEST_info("Failed to create binary key");
2558 if (klist == &private_keys)
2559 pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
2561 pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
2562 if (pkey == NULL && !key_unsupported()) {
2563 TEST_info("Can't read %s data", pp->key);
2564 OPENSSL_free(keybin);
2565 TEST_openssl_errors();
2568 OPENSSL_free(keybin);
2571 /* If we have a key add to list */
2572 if (klist != NULL) {
2573 if (find_key(NULL, pp->value, *klist)) {
2574 TEST_info("Duplicate key %s", pp->value);
2577 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2579 key->name = take_value(pp);
2581 /* Hack to detect SM2 keys */
2582 if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
2583 #ifdef OPENSSL_NO_SM2
2584 EVP_PKEY_free(pkey);
2587 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
2595 /* Go back and start a new stanza. */
2596 if (t->s.numpairs != 1)
2597 TEST_info("Line %d: missing blank line\n", t->s.curr);
2601 /* Find the test, based on first keyword. */
2602 if (!TEST_ptr(t->meth = find_test(pp->key)))
2604 if (!t->meth->init(t, pp->value)) {
2605 TEST_error("unknown %s: %s\n", pp->key, pp->value);
2609 /* TEST_info("skipping %s %s", pp->key, pp->value); */
2613 for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
2614 if (strcmp(pp->key, "Result") == 0) {
2615 if (t->expected_err != NULL) {
2616 TEST_info("Line %d: multiple result lines", t->s.curr);
2619 t->expected_err = take_value(pp);
2620 } else if (strcmp(pp->key, "Function") == 0) {
2621 if (t->func != NULL) {
2622 TEST_info("Line %d: multiple function lines\n", t->s.curr);
2625 t->func = take_value(pp);
2626 } else if (strcmp(pp->key, "Reason") == 0) {
2627 if (t->reason != NULL) {
2628 TEST_info("Line %d: multiple reason lines", t->s.curr);
2631 t->reason = take_value(pp);
2633 /* Must be test specific line: try to parse it */
2634 int rv = t->meth->parse(t, pp->key, pp->value);
2637 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
2641 TEST_info("Line %d: error processing keyword %s = %s\n",
2642 t->s.curr, pp->key, pp->value);
2651 static int run_file_tests(int i)
2654 const char *testfile = test_get_argument(i);
2657 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
2659 if (!test_start_file(&t->s, testfile)) {
2664 while (!BIO_eof(t->s.fp)) {
2668 if (c == 0 || !run_test(t)) {
2673 test_end_file(&t->s);
2676 free_key_list(public_keys);
2677 free_key_list(private_keys);
2684 int setup_tests(void)
2686 size_t n = test_get_argument_count();
2689 TEST_error("Usage: %s file...", test_get_program_name());
2693 ADD_ALL_TESTS(run_file_tests, n);