2 * Copyright 2015-2020 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"
28 DEFINE_STACK_OF_STRING()
32 typedef struct evp_test_method_st EVP_TEST_METHOD;
35 * Structure holding test information
37 typedef struct evp_test_st {
38 STANZA s; /* Common test stanza */
40 int skip; /* Current test should be skipped */
41 const EVP_TEST_METHOD *meth; /* method for this test */
42 const char *err, *aux_err; /* Error string for test */
43 char *expected_err; /* Expected error value of test */
44 char *reason; /* Expected error reason string */
45 void *data; /* test specific data */
49 * Test method structure
51 struct evp_test_method_st {
52 /* Name of test as it appears in file */
54 /* Initialise test for "alg" */
55 int (*init) (EVP_TEST * t, const char *alg);
57 void (*cleanup) (EVP_TEST * t);
58 /* Test specific name value pair processing */
59 int (*parse) (EVP_TEST * t, const char *name, const char *value);
60 /* Run the test itself */
61 int (*run_test) (EVP_TEST * t);
66 * Linked list of named keys.
68 typedef struct key_list_st {
71 struct key_list_st *next;
75 * List of public and private keys
77 static KEY_LIST *private_keys;
78 static KEY_LIST *public_keys;
79 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
81 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
84 * Compare two memory regions for equality, returning zero if they differ.
85 * However, if there is expected to be an error and the actual error
86 * matches then the memory is expected to be different so handle this
87 * case without producing unnecessary test framework output.
89 static int memory_err_compare(EVP_TEST *t, const char *err,
90 const void *expected, size_t expected_len,
91 const void *got, size_t got_len)
95 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
96 r = !TEST_mem_ne(expected, expected_len, got, got_len);
98 r = TEST_mem_eq(expected, expected_len, got, got_len);
105 * Structure used to hold a list of blocks of memory to test
106 * calls to "update" like functions.
108 struct evp_test_buffer_st {
115 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
118 OPENSSL_free(db->buf);
124 * append buffer to a list
126 static int evp_test_buffer_append(const char *value,
127 STACK_OF(EVP_TEST_BUFFER) **sk)
129 EVP_TEST_BUFFER *db = NULL;
131 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
134 if (!parse_bin(value, &db->buf, &db->buflen))
139 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
141 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
147 evp_test_buffer_free(db);
152 * replace last buffer in list with copies of itself
154 static int evp_test_buffer_ncopy(const char *value,
155 STACK_OF(EVP_TEST_BUFFER) *sk)
158 unsigned char *tbuf, *p;
160 int ncopy = atoi(value);
165 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
167 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
169 tbuflen = db->buflen * ncopy;
170 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
172 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
173 memcpy(p, db->buf, db->buflen);
175 OPENSSL_free(db->buf);
177 db->buflen = tbuflen;
182 * set repeat count for last buffer in list
184 static int evp_test_buffer_set_count(const char *value,
185 STACK_OF(EVP_TEST_BUFFER) *sk)
188 int count = atoi(value);
193 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
196 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
197 if (db->count_set != 0)
200 db->count = (size_t)count;
206 * call "fn" with each element of the list in turn
208 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
210 const unsigned char *buf,
216 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
217 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
220 for (j = 0; j < tb->count; j++) {
221 if (fn(ctx, tb->buf, tb->buflen) <= 0)
229 * Unescape some sequences in string literals (only \n for now).
230 * Return an allocated buffer, set |out_len|. If |input_len|
231 * is zero, get an empty buffer but set length to zero.
233 static unsigned char* unescape(const char *input, size_t input_len,
236 unsigned char *ret, *p;
239 if (input_len == 0) {
241 return OPENSSL_zalloc(1);
244 /* Escaping is non-expanding; over-allocate original size for simplicity. */
245 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
248 for (i = 0; i < input_len; i++) {
249 if (*input == '\\') {
250 if (i == input_len - 1 || *++input != 'n') {
251 TEST_error("Bad escape sequence in file");
271 * For a hex string "value" convert to a binary allocated buffer.
272 * Return 1 on success or 0 on failure.
274 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
278 /* Check for NULL literal */
279 if (strcmp(value, "NULL") == 0) {
285 /* Check for empty value */
286 if (*value == '\0') {
288 * Don't return NULL for zero length buffer. This is needed for
289 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
290 * buffer even if the key length is 0, in order to detect key reset.
292 *buf = OPENSSL_malloc(1);
300 /* Check for string literal */
301 if (value[0] == '"') {
302 size_t vlen = strlen(++value);
304 if (vlen == 0 || value[vlen - 1] != '"')
307 *buf = unescape(value, vlen, buflen);
308 return *buf == NULL ? 0 : 1;
311 /* Otherwise assume as hex literal and convert it to binary buffer */
312 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
313 TEST_info("Can't convert %s", value);
314 TEST_openssl_errors();
317 /* Size of input buffer means we'll never overflow */
324 *** MESSAGE DIGEST TESTS
327 typedef struct digest_data_st {
328 /* Digest this test is for */
329 const EVP_MD *digest;
330 EVP_MD *fetched_digest;
331 /* Input to digest */
332 STACK_OF(EVP_TEST_BUFFER) *input;
333 /* Expected output */
334 unsigned char *output;
340 static int digest_test_init(EVP_TEST *t, const char *alg)
343 const EVP_MD *digest;
344 EVP_MD *fetched_digest;
346 if ((digest = fetched_digest = EVP_MD_fetch(NULL, alg, NULL)) == NULL
347 && (digest = EVP_get_digestbyname(alg)) == NULL) {
348 /* If alg has an OID assume disabled algorithm */
349 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
355 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
358 mdat->digest = digest;
359 mdat->fetched_digest = fetched_digest;
361 if (fetched_digest != NULL)
362 TEST_info("%s is fetched", alg);
366 static void digest_test_cleanup(EVP_TEST *t)
368 DIGEST_DATA *mdat = t->data;
370 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
371 OPENSSL_free(mdat->output);
372 EVP_MD_free(mdat->fetched_digest);
375 static int digest_test_parse(EVP_TEST *t,
376 const char *keyword, const char *value)
378 DIGEST_DATA *mdata = t->data;
380 if (strcmp(keyword, "Input") == 0)
381 return evp_test_buffer_append(value, &mdata->input);
382 if (strcmp(keyword, "Output") == 0)
383 return parse_bin(value, &mdata->output, &mdata->output_len);
384 if (strcmp(keyword, "Count") == 0)
385 return evp_test_buffer_set_count(value, mdata->input);
386 if (strcmp(keyword, "Ncopy") == 0)
387 return evp_test_buffer_ncopy(value, mdata->input);
388 if (strcmp(keyword, "Padding") == 0)
389 return (mdata->pad_type = atoi(value)) > 0;
393 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
395 return EVP_DigestUpdate(ctx, buf, buflen);
398 static int digest_test_run(EVP_TEST *t)
400 DIGEST_DATA *expected = t->data;
402 unsigned char *got = NULL;
403 unsigned int got_len;
404 OSSL_PARAM params[2];
406 t->err = "TEST_FAILURE";
407 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
410 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
411 expected->output_len : EVP_MAX_MD_SIZE);
415 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
416 t->err = "DIGESTINIT_ERROR";
419 if (expected->pad_type > 0) {
420 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
421 &expected->pad_type);
422 params[1] = OSSL_PARAM_construct_end();
423 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
424 t->err = "PARAMS_ERROR";
428 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
429 t->err = "DIGESTUPDATE_ERROR";
433 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
434 EVP_MD_CTX *mctx_cpy;
435 char dont[] = "touch";
437 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
440 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
441 EVP_MD_CTX_free(mctx_cpy);
444 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
445 EVP_MD_CTX_free(mctx_cpy);
446 t->err = "DIGESTFINALXOF_ERROR";
449 if (!TEST_str_eq(dont, "touch")) {
450 EVP_MD_CTX_free(mctx_cpy);
451 t->err = "DIGESTFINALXOF_ERROR";
454 EVP_MD_CTX_free(mctx_cpy);
456 got_len = expected->output_len;
457 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
458 t->err = "DIGESTFINALXOF_ERROR";
462 if (!EVP_DigestFinal(mctx, got, &got_len)) {
463 t->err = "DIGESTFINAL_ERROR";
467 if (!TEST_int_eq(expected->output_len, got_len)) {
468 t->err = "DIGEST_LENGTH_MISMATCH";
471 if (!memory_err_compare(t, "DIGEST_MISMATCH",
472 expected->output, expected->output_len,
480 EVP_MD_CTX_free(mctx);
484 static const EVP_TEST_METHOD digest_test_method = {
497 typedef struct cipher_data_st {
498 const EVP_CIPHER *cipher;
499 EVP_CIPHER *fetched_cipher;
501 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
505 size_t key_bits; /* Used by RC2 */
509 unsigned char *plaintext;
510 size_t plaintext_len;
511 unsigned char *ciphertext;
512 size_t ciphertext_len;
513 /* GCM, CCM, OCB and SIV only */
514 unsigned char *aad[AAD_NUM];
515 size_t aad_len[AAD_NUM];
517 const char *cts_mode;
522 static int cipher_test_init(EVP_TEST *t, const char *alg)
524 const EVP_CIPHER *cipher;
525 EVP_CIPHER *fetched_cipher;
529 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(NULL, alg, NULL)) == NULL
530 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
531 /* If alg has an OID assume disabled algorithm */
532 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
538 cdat = OPENSSL_zalloc(sizeof(*cdat));
539 cdat->cipher = cipher;
540 cdat->fetched_cipher = fetched_cipher;
542 m = EVP_CIPHER_mode(cipher);
543 if (m == EVP_CIPH_GCM_MODE
544 || m == EVP_CIPH_OCB_MODE
545 || m == EVP_CIPH_SIV_MODE
546 || m == EVP_CIPH_CCM_MODE)
548 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
554 if (fetched_cipher != NULL)
555 TEST_info("%s is fetched", alg);
559 static void cipher_test_cleanup(EVP_TEST *t)
562 CIPHER_DATA *cdat = t->data;
564 OPENSSL_free(cdat->key);
565 OPENSSL_free(cdat->iv);
566 OPENSSL_free(cdat->ciphertext);
567 OPENSSL_free(cdat->plaintext);
568 for (i = 0; i < AAD_NUM; i++)
569 OPENSSL_free(cdat->aad[i]);
570 OPENSSL_free(cdat->tag);
571 EVP_CIPHER_free(cdat->fetched_cipher);
574 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
577 CIPHER_DATA *cdat = t->data;
580 if (strcmp(keyword, "Key") == 0)
581 return parse_bin(value, &cdat->key, &cdat->key_len);
582 if (strcmp(keyword, "Rounds") == 0) {
586 cdat->rounds = (unsigned int)i;
589 if (strcmp(keyword, "IV") == 0)
590 return parse_bin(value, &cdat->iv, &cdat->iv_len);
591 if (strcmp(keyword, "Plaintext") == 0)
592 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
593 if (strcmp(keyword, "Ciphertext") == 0)
594 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
595 if (strcmp(keyword, "KeyBits") == 0) {
599 cdat->key_bits = (size_t)i;
603 if (strcmp(keyword, "AAD") == 0) {
604 for (i = 0; i < AAD_NUM; i++) {
605 if (cdat->aad[i] == NULL)
606 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
610 if (strcmp(keyword, "Tag") == 0)
611 return parse_bin(value, &cdat->tag, &cdat->tag_len);
612 if (strcmp(keyword, "SetTagLate") == 0) {
613 if (strcmp(value, "TRUE") == 0)
615 else if (strcmp(value, "FALSE") == 0)
623 if (strcmp(keyword, "Operation") == 0) {
624 if (strcmp(value, "ENCRYPT") == 0)
626 else if (strcmp(value, "DECRYPT") == 0)
632 if (strcmp(keyword, "CTSMode") == 0) {
633 cdat->cts_mode = value;
639 static int cipher_test_enc(EVP_TEST *t, int enc,
640 size_t out_misalign, size_t inp_misalign, int frag)
642 CIPHER_DATA *expected = t->data;
643 unsigned char *in, *expected_out, *tmp = NULL;
644 size_t in_len, out_len, donelen = 0;
645 int ok = 0, tmplen, chunklen, tmpflen, i;
646 EVP_CIPHER_CTX *ctx_base = NULL;
647 EVP_CIPHER_CTX *ctx = NULL;
649 t->err = "TEST_FAILURE";
650 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
652 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
654 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
656 in = expected->plaintext;
657 in_len = expected->plaintext_len;
658 expected_out = expected->ciphertext;
659 out_len = expected->ciphertext_len;
661 in = expected->ciphertext;
662 in_len = expected->ciphertext_len;
663 expected_out = expected->plaintext;
664 out_len = expected->plaintext_len;
666 if (inp_misalign == (size_t)-1) {
668 * Exercise in-place encryption
670 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
673 in = memcpy(tmp + out_misalign, in, in_len);
675 inp_misalign += 16 - ((out_misalign + in_len) & 15);
677 * 'tmp' will store both output and copy of input. We make the copy
678 * of input to specifically aligned part of 'tmp'. So we just
679 * figured out how much padding would ensure the required alignment,
680 * now we allocate extended buffer and finally copy the input just
681 * past inp_misalign in expression below. Output will be written
682 * past out_misalign...
684 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
685 inp_misalign + in_len);
688 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
689 inp_misalign, in, in_len);
691 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
692 t->err = "CIPHERINIT_ERROR";
695 if (expected->cts_mode != NULL) {
696 OSSL_PARAM params[2];
698 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
699 (char *)expected->cts_mode,
701 params[1] = OSSL_PARAM_construct_end();
702 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
703 t->err = "INVALID_CTS_MODE";
708 if (expected->aead) {
709 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
710 expected->iv_len, 0)) {
711 t->err = "INVALID_IV_LENGTH";
714 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
715 t->err = "INVALID_IV_LENGTH";
719 if (expected->aead) {
722 * If encrypting or OCB just set tag length initially, otherwise
723 * set tag length and value.
725 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
726 t->err = "TAG_LENGTH_SET_ERROR";
729 t->err = "TAG_SET_ERROR";
732 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
733 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
734 expected->tag_len, tag))
739 if (expected->rounds > 0) {
740 int rounds = (int)expected->rounds;
742 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
743 t->err = "INVALID_ROUNDS";
748 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
749 t->err = "INVALID_KEY_LENGTH";
752 if (expected->key_bits > 0) {
753 int bits = (int)expected->key_bits;
755 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
756 t->err = "INVALID KEY BITS";
760 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
761 t->err = "KEY_SET_ERROR";
765 /* Check that we get the same IV back */
766 if (expected->iv != NULL
767 && (EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
768 && !TEST_mem_eq(expected->iv, expected->iv_len,
769 EVP_CIPHER_CTX_iv(ctx_base), expected->iv_len)) {
770 t->err = "INVALID_IV";
774 /* Test that the cipher dup functions correctly if it is supported */
775 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
776 EVP_CIPHER_CTX_free(ctx_base);
779 EVP_CIPHER_CTX_free(ctx);
783 if (expected->aead == EVP_CIPH_CCM_MODE) {
784 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
785 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
789 if (expected->aad[0] != NULL) {
790 t->err = "AAD_SET_ERROR";
792 for (i = 0; expected->aad[i] != NULL; i++) {
793 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
794 expected->aad_len[i]))
799 * Supply the AAD in chunks less than the block size where possible
801 for (i = 0; expected->aad[i] != NULL; i++) {
802 if (expected->aad_len[i] > 0) {
803 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
807 if (expected->aad_len[i] > 2) {
808 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
809 expected->aad[i] + donelen,
810 expected->aad_len[i] - 2))
812 donelen += expected->aad_len[i] - 2;
814 if (expected->aad_len[i] > 1
815 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
816 expected->aad[i] + donelen, 1))
822 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
823 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
824 expected->tag_len, expected->tag)) {
825 t->err = "TAG_SET_ERROR";
830 EVP_CIPHER_CTX_set_padding(ctx, 0);
831 t->err = "CIPHERUPDATE_ERROR";
834 /* We supply the data all in one go */
835 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
838 /* Supply the data in chunks less than the block size where possible */
840 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
847 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
855 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
861 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
862 t->err = "CIPHERFINAL_ERROR";
865 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
866 tmp + out_misalign, tmplen + tmpflen))
868 if (enc && expected->aead) {
869 unsigned char rtag[16];
871 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
872 t->err = "TAG_LENGTH_INTERNAL_ERROR";
875 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
876 expected->tag_len, rtag)) {
877 t->err = "TAG_RETRIEVE_ERROR";
880 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
881 expected->tag, expected->tag_len,
882 rtag, expected->tag_len))
890 EVP_CIPHER_CTX_free(ctx_base);
891 EVP_CIPHER_CTX_free(ctx);
895 static int cipher_test_run(EVP_TEST *t)
897 CIPHER_DATA *cdat = t->data;
899 size_t out_misalign, inp_misalign;
905 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
906 /* IV is optional and usually omitted in wrap mode */
907 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
912 if (cdat->aead && !cdat->tag) {
916 for (out_misalign = 0; out_misalign <= 1;) {
917 static char aux_err[64];
918 t->aux_err = aux_err;
919 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
920 if (inp_misalign == (size_t)-1) {
921 /* kludge: inp_misalign == -1 means "exercise in-place" */
922 BIO_snprintf(aux_err, sizeof(aux_err),
923 "%s in-place, %sfragmented",
924 out_misalign ? "misaligned" : "aligned",
927 BIO_snprintf(aux_err, sizeof(aux_err),
928 "%s output and %s input, %sfragmented",
929 out_misalign ? "misaligned" : "aligned",
930 inp_misalign ? "misaligned" : "aligned",
934 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
935 /* Not fatal errors: return */
942 if (cdat->enc != 1) {
943 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
944 /* Not fatal errors: return */
953 if (out_misalign == 1 && frag == 0) {
955 * XTS, SIV, CCM and Wrap modes have special requirements about input
956 * lengths so we don't fragment for those
958 if (cdat->aead == EVP_CIPH_CCM_MODE
959 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
960 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
961 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
962 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
975 static const EVP_TEST_METHOD cipher_test_method = {
988 typedef struct mac_data_st {
989 /* MAC type in one form or another */
991 EVP_MAC *mac; /* for mac_test_run_mac */
992 int type; /* for mac_test_run_pkey */
993 /* Algorithm string for this MAC */
1002 unsigned char *input;
1004 /* Expected output */
1005 unsigned char *output;
1007 unsigned char *custom;
1009 /* MAC salt (blake2) */
1010 unsigned char *salt;
1012 /* Collection of controls */
1013 STACK_OF(OPENSSL_STRING) *controls;
1016 static int mac_test_init(EVP_TEST *t, const char *alg)
1018 EVP_MAC *mac = NULL;
1019 int type = NID_undef;
1022 if ((mac = EVP_MAC_fetch(NULL, alg, NULL)) == NULL) {
1024 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1025 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1026 * the EVP_PKEY method.
1028 size_t sz = strlen(alg);
1029 static const char epilogue[] = " by EVP_PKEY";
1031 if (sz >= sizeof(epilogue)
1032 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1033 sz -= sizeof(epilogue) - 1;
1035 if (strncmp(alg, "HMAC", sz) == 0) {
1036 type = EVP_PKEY_HMAC;
1037 } else if (strncmp(alg, "CMAC", sz) == 0) {
1038 #ifndef OPENSSL_NO_CMAC
1039 type = EVP_PKEY_CMAC;
1044 } else if (strncmp(alg, "Poly1305", sz) == 0) {
1045 #ifndef OPENSSL_NO_POLY1305
1046 type = EVP_PKEY_POLY1305;
1051 } else if (strncmp(alg, "SipHash", sz) == 0) {
1052 #ifndef OPENSSL_NO_SIPHASH
1053 type = EVP_PKEY_SIPHASH;
1060 * Not a known EVP_PKEY method either. If it's a known OID, then
1061 * assume it's been disabled.
1063 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
1072 mdat = OPENSSL_zalloc(sizeof(*mdat));
1074 mdat->mac_name = OPENSSL_strdup(alg);
1076 mdat->controls = sk_OPENSSL_STRING_new_null();
1081 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1082 static void openssl_free(char *m)
1087 static void mac_test_cleanup(EVP_TEST *t)
1089 MAC_DATA *mdat = t->data;
1091 EVP_MAC_free(mdat->mac);
1092 OPENSSL_free(mdat->mac_name);
1093 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1094 OPENSSL_free(mdat->alg);
1095 OPENSSL_free(mdat->key);
1096 OPENSSL_free(mdat->iv);
1097 OPENSSL_free(mdat->custom);
1098 OPENSSL_free(mdat->salt);
1099 OPENSSL_free(mdat->input);
1100 OPENSSL_free(mdat->output);
1103 static int mac_test_parse(EVP_TEST *t,
1104 const char *keyword, const char *value)
1106 MAC_DATA *mdata = t->data;
1108 if (strcmp(keyword, "Key") == 0)
1109 return parse_bin(value, &mdata->key, &mdata->key_len);
1110 if (strcmp(keyword, "IV") == 0)
1111 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1112 if (strcmp(keyword, "Custom") == 0)
1113 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1114 if (strcmp(keyword, "Salt") == 0)
1115 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1116 if (strcmp(keyword, "Algorithm") == 0) {
1117 mdata->alg = OPENSSL_strdup(value);
1122 if (strcmp(keyword, "Input") == 0)
1123 return parse_bin(value, &mdata->input, &mdata->input_len);
1124 if (strcmp(keyword, "Output") == 0)
1125 return parse_bin(value, &mdata->output, &mdata->output_len);
1126 if (strcmp(keyword, "Ctrl") == 0)
1127 return sk_OPENSSL_STRING_push(mdata->controls,
1128 OPENSSL_strdup(value)) != 0;
1132 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1138 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1140 p = strchr(tmpval, ':');
1143 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1145 t->err = "PKEY_CTRL_INVALID";
1147 t->err = "PKEY_CTRL_ERROR";
1150 OPENSSL_free(tmpval);
1154 static int mac_test_run_pkey(EVP_TEST *t)
1156 MAC_DATA *expected = t->data;
1157 EVP_MD_CTX *mctx = NULL;
1158 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1159 EVP_PKEY *key = NULL;
1160 const EVP_MD *md = NULL;
1161 unsigned char *got = NULL;
1165 if (expected->alg == NULL)
1166 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1168 TEST_info("Trying the EVP_PKEY %s test with %s",
1169 OBJ_nid2sn(expected->type), expected->alg);
1171 #ifdef OPENSSL_NO_DES
1172 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1179 if (expected->type == EVP_PKEY_CMAC)
1180 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1181 EVP_get_cipherbyname(expected->alg));
1183 key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
1186 t->err = "MAC_KEY_CREATE_ERROR";
1190 if (expected->type == EVP_PKEY_HMAC) {
1191 if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
1192 t->err = "MAC_ALGORITHM_SET_ERROR";
1196 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1197 t->err = "INTERNAL_ERROR";
1200 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1201 t->err = "DIGESTSIGNINIT_ERROR";
1204 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1205 if (!mac_test_ctrl_pkey(t, pctx,
1206 sk_OPENSSL_STRING_value(expected->controls,
1208 t->err = "EVPPKEYCTXCTRL_ERROR";
1211 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1212 t->err = "DIGESTSIGNUPDATE_ERROR";
1215 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1216 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1219 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1220 t->err = "TEST_FAILURE";
1223 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1224 || !memory_err_compare(t, "TEST_MAC_ERR",
1225 expected->output, expected->output_len,
1227 t->err = "TEST_MAC_ERR";
1232 EVP_MD_CTX_free(mctx);
1234 EVP_PKEY_CTX_free(genctx);
1239 static int mac_test_run_mac(EVP_TEST *t)
1241 MAC_DATA *expected = t->data;
1242 EVP_MAC_CTX *ctx = NULL;
1243 unsigned char *got = NULL;
1246 OSSL_PARAM params[21];
1247 size_t params_n = 0;
1248 size_t params_n_allocstart = 0;
1249 const OSSL_PARAM *defined_params =
1250 EVP_MAC_settable_ctx_params(expected->mac);
1252 if (expected->alg == NULL)
1253 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1255 TEST_info("Trying the EVP_MAC %s test with %s",
1256 expected->mac_name, expected->alg);
1258 #ifdef OPENSSL_NO_DES
1259 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1266 if (expected->alg != NULL) {
1268 * The underlying algorithm may be a cipher or a digest.
1269 * We don't know which it is, but we can ask the MAC what it
1270 * should be and bet on that.
1272 if (OSSL_PARAM_locate_const(defined_params,
1273 OSSL_MAC_PARAM_CIPHER) != NULL) {
1274 params[params_n++] =
1275 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1277 } else if (OSSL_PARAM_locate_const(defined_params,
1278 OSSL_MAC_PARAM_DIGEST) != NULL) {
1279 params[params_n++] =
1280 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1283 t->err = "MAC_BAD_PARAMS";
1287 if (expected->key != NULL)
1288 params[params_n++] =
1289 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1292 if (expected->custom != NULL)
1293 params[params_n++] =
1294 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1296 expected->custom_len);
1297 if (expected->salt != NULL)
1298 params[params_n++] =
1299 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1301 expected->salt_len);
1302 if (expected->iv != NULL)
1303 params[params_n++] =
1304 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1309 * Unknown controls. They must match parameters that the MAC recognises
1311 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1312 >= OSSL_NELEM(params)) {
1313 t->err = "MAC_TOO_MANY_PARAMETERS";
1316 params_n_allocstart = params_n;
1317 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1318 char *tmpkey, *tmpval;
1319 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1321 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1322 t->err = "MAC_PARAM_ERROR";
1325 tmpval = strchr(tmpkey, ':');
1330 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1333 strlen(tmpval), NULL)) {
1334 OPENSSL_free(tmpkey);
1335 t->err = "MAC_PARAM_ERROR";
1340 OPENSSL_free(tmpkey);
1342 params[params_n] = OSSL_PARAM_construct_end();
1344 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1345 t->err = "MAC_CREATE_ERROR";
1349 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1350 t->err = "MAC_BAD_PARAMS";
1353 if (!EVP_MAC_init(ctx)) {
1354 t->err = "MAC_INIT_ERROR";
1357 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1358 t->err = "MAC_UPDATE_ERROR";
1361 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1362 t->err = "MAC_FINAL_LENGTH_ERROR";
1365 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1366 t->err = "TEST_FAILURE";
1369 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1370 || !memory_err_compare(t, "TEST_MAC_ERR",
1371 expected->output, expected->output_len,
1373 t->err = "TEST_MAC_ERR";
1378 while (params_n-- > params_n_allocstart) {
1379 OPENSSL_free(params[params_n].data);
1381 EVP_MAC_CTX_free(ctx);
1386 static int mac_test_run(EVP_TEST *t)
1388 MAC_DATA *expected = t->data;
1390 if (expected->mac != NULL)
1391 return mac_test_run_mac(t);
1392 return mac_test_run_pkey(t);
1395 static const EVP_TEST_METHOD mac_test_method = {
1405 *** PUBLIC KEY TESTS
1406 *** These are all very similar and share much common code.
1409 typedef struct pkey_data_st {
1410 /* Context for this operation */
1412 /* Key operation to perform */
1413 int (*keyop) (EVP_PKEY_CTX *ctx,
1414 unsigned char *sig, size_t *siglen,
1415 const unsigned char *tbs, size_t tbslen);
1417 unsigned char *input;
1419 /* Expected output */
1420 unsigned char *output;
1425 * Perform public key operation setup: lookup key, allocated ctx and call
1426 * the appropriate initialisation function
1428 static int pkey_test_init(EVP_TEST *t, const char *name,
1430 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1431 int (*keyop)(EVP_PKEY_CTX *ctx,
1432 unsigned char *sig, size_t *siglen,
1433 const unsigned char *tbs,
1437 EVP_PKEY *pkey = NULL;
1441 rv = find_key(&pkey, name, public_keys);
1443 rv = find_key(&pkey, name, private_keys);
1444 if (rv == 0 || pkey == NULL) {
1449 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1450 EVP_PKEY_free(pkey);
1453 kdata->keyop = keyop;
1454 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
1455 EVP_PKEY_free(pkey);
1456 OPENSSL_free(kdata);
1459 if (keyopinit(kdata->ctx) <= 0)
1460 t->err = "KEYOP_INIT_ERROR";
1465 static void pkey_test_cleanup(EVP_TEST *t)
1467 PKEY_DATA *kdata = t->data;
1469 OPENSSL_free(kdata->input);
1470 OPENSSL_free(kdata->output);
1471 EVP_PKEY_CTX_free(kdata->ctx);
1474 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1480 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1482 p = strchr(tmpval, ':');
1485 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1487 t->err = "PKEY_CTRL_INVALID";
1489 } else if (p != NULL && rv <= 0) {
1490 /* If p has an OID and lookup fails assume disabled algorithm */
1491 int nid = OBJ_sn2nid(p);
1493 if (nid == NID_undef)
1494 nid = OBJ_ln2nid(p);
1495 if (nid != NID_undef
1496 && EVP_get_digestbynid(nid) == NULL
1497 && EVP_get_cipherbynid(nid) == NULL) {
1501 t->err = "PKEY_CTRL_ERROR";
1505 OPENSSL_free(tmpval);
1509 static int pkey_test_parse(EVP_TEST *t,
1510 const char *keyword, const char *value)
1512 PKEY_DATA *kdata = t->data;
1513 if (strcmp(keyword, "Input") == 0)
1514 return parse_bin(value, &kdata->input, &kdata->input_len);
1515 if (strcmp(keyword, "Output") == 0)
1516 return parse_bin(value, &kdata->output, &kdata->output_len);
1517 if (strcmp(keyword, "Ctrl") == 0)
1518 return pkey_test_ctrl(t, kdata->ctx, value);
1522 static int pkey_test_run(EVP_TEST *t)
1524 PKEY_DATA *expected = t->data;
1525 unsigned char *got = NULL;
1527 EVP_PKEY_CTX *copy = NULL;
1529 if (expected->keyop(expected->ctx, NULL, &got_len,
1530 expected->input, expected->input_len) <= 0
1531 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1532 t->err = "KEYOP_LENGTH_ERROR";
1535 if (expected->keyop(expected->ctx, got, &got_len,
1536 expected->input, expected->input_len) <= 0) {
1537 t->err = "KEYOP_ERROR";
1540 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1541 expected->output, expected->output_len,
1549 /* Repeat the test on a copy. */
1550 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1551 t->err = "INTERNAL_ERROR";
1554 if (expected->keyop(copy, NULL, &got_len, expected->input,
1555 expected->input_len) <= 0
1556 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1557 t->err = "KEYOP_LENGTH_ERROR";
1560 if (expected->keyop(copy, got, &got_len, expected->input,
1561 expected->input_len) <= 0) {
1562 t->err = "KEYOP_ERROR";
1565 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1566 expected->output, expected->output_len,
1572 EVP_PKEY_CTX_free(copy);
1576 static int sign_test_init(EVP_TEST *t, const char *name)
1578 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1581 static const EVP_TEST_METHOD psign_test_method = {
1589 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1591 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1592 EVP_PKEY_verify_recover);
1595 static const EVP_TEST_METHOD pverify_recover_test_method = {
1597 verify_recover_test_init,
1603 static int decrypt_test_init(EVP_TEST *t, const char *name)
1605 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1609 static const EVP_TEST_METHOD pdecrypt_test_method = {
1617 static int verify_test_init(EVP_TEST *t, const char *name)
1619 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1622 static int verify_test_run(EVP_TEST *t)
1624 PKEY_DATA *kdata = t->data;
1626 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1627 kdata->input, kdata->input_len) <= 0)
1628 t->err = "VERIFY_ERROR";
1632 static const EVP_TEST_METHOD pverify_test_method = {
1641 static int pderive_test_init(EVP_TEST *t, const char *name)
1643 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1646 static int pderive_test_parse(EVP_TEST *t,
1647 const char *keyword, const char *value)
1649 PKEY_DATA *kdata = t->data;
1651 if (strcmp(keyword, "PeerKey") == 0) {
1653 if (find_key(&peer, value, public_keys) == 0)
1655 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1659 if (strcmp(keyword, "SharedSecret") == 0)
1660 return parse_bin(value, &kdata->output, &kdata->output_len);
1661 if (strcmp(keyword, "Ctrl") == 0)
1662 return pkey_test_ctrl(t, kdata->ctx, value);
1666 static int pderive_test_run(EVP_TEST *t)
1668 PKEY_DATA *expected = t->data;
1669 unsigned char *got = NULL;
1672 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1673 t->err = "DERIVE_ERROR";
1676 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1677 t->err = "DERIVE_ERROR";
1680 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1681 t->err = "DERIVE_ERROR";
1684 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1685 expected->output, expected->output_len,
1695 static const EVP_TEST_METHOD pderive_test_method = {
1708 typedef enum pbe_type_enum {
1709 PBE_TYPE_INVALID = 0,
1710 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1713 typedef struct pbe_data_st {
1715 /* scrypt parameters */
1716 uint64_t N, r, p, maxmem;
1717 /* PKCS#12 parameters */
1721 unsigned char *pass;
1724 unsigned char *salt;
1726 /* Expected output */
1731 #ifndef OPENSSL_NO_SCRYPT
1733 * Parse unsigned decimal 64 bit integer value
1735 static int parse_uint64(const char *value, uint64_t *pr)
1737 const char *p = value;
1739 if (!TEST_true(*p)) {
1740 TEST_info("Invalid empty integer value");
1743 for (*pr = 0; *p; ) {
1744 if (*pr > UINT64_MAX / 10) {
1745 TEST_error("Integer overflow in string %s", value);
1749 if (!TEST_true(isdigit((unsigned char)*p))) {
1750 TEST_error("Invalid character in string %s", value);
1759 static int scrypt_test_parse(EVP_TEST *t,
1760 const char *keyword, const char *value)
1762 PBE_DATA *pdata = t->data;
1764 if (strcmp(keyword, "N") == 0)
1765 return parse_uint64(value, &pdata->N);
1766 if (strcmp(keyword, "p") == 0)
1767 return parse_uint64(value, &pdata->p);
1768 if (strcmp(keyword, "r") == 0)
1769 return parse_uint64(value, &pdata->r);
1770 if (strcmp(keyword, "maxmem") == 0)
1771 return parse_uint64(value, &pdata->maxmem);
1776 static int pbkdf2_test_parse(EVP_TEST *t,
1777 const char *keyword, const char *value)
1779 PBE_DATA *pdata = t->data;
1781 if (strcmp(keyword, "iter") == 0) {
1782 pdata->iter = atoi(value);
1783 if (pdata->iter <= 0)
1787 if (strcmp(keyword, "MD") == 0) {
1788 pdata->md = EVP_get_digestbyname(value);
1789 if (pdata->md == NULL)
1796 static int pkcs12_test_parse(EVP_TEST *t,
1797 const char *keyword, const char *value)
1799 PBE_DATA *pdata = t->data;
1801 if (strcmp(keyword, "id") == 0) {
1802 pdata->id = atoi(value);
1807 return pbkdf2_test_parse(t, keyword, value);
1810 static int pbe_test_init(EVP_TEST *t, const char *alg)
1813 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1815 if (strcmp(alg, "scrypt") == 0) {
1816 #ifndef OPENSSL_NO_SCRYPT
1817 pbe_type = PBE_TYPE_SCRYPT;
1822 } else if (strcmp(alg, "pbkdf2") == 0) {
1823 pbe_type = PBE_TYPE_PBKDF2;
1824 } else if (strcmp(alg, "pkcs12") == 0) {
1825 pbe_type = PBE_TYPE_PKCS12;
1827 TEST_error("Unknown pbe algorithm %s", alg);
1829 pdat = OPENSSL_zalloc(sizeof(*pdat));
1830 pdat->pbe_type = pbe_type;
1835 static void pbe_test_cleanup(EVP_TEST *t)
1837 PBE_DATA *pdat = t->data;
1839 OPENSSL_free(pdat->pass);
1840 OPENSSL_free(pdat->salt);
1841 OPENSSL_free(pdat->key);
1844 static int pbe_test_parse(EVP_TEST *t,
1845 const char *keyword, const char *value)
1847 PBE_DATA *pdata = t->data;
1849 if (strcmp(keyword, "Password") == 0)
1850 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1851 if (strcmp(keyword, "Salt") == 0)
1852 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1853 if (strcmp(keyword, "Key") == 0)
1854 return parse_bin(value, &pdata->key, &pdata->key_len);
1855 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1856 return pbkdf2_test_parse(t, keyword, value);
1857 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1858 return pkcs12_test_parse(t, keyword, value);
1859 #ifndef OPENSSL_NO_SCRYPT
1860 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1861 return scrypt_test_parse(t, keyword, value);
1866 static int pbe_test_run(EVP_TEST *t)
1868 PBE_DATA *expected = t->data;
1871 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1872 t->err = "INTERNAL_ERROR";
1875 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1876 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1877 expected->salt, expected->salt_len,
1878 expected->iter, expected->md,
1879 expected->key_len, key) == 0) {
1880 t->err = "PBKDF2_ERROR";
1883 #ifndef OPENSSL_NO_SCRYPT
1884 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1885 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1886 expected->salt, expected->salt_len, expected->N,
1887 expected->r, expected->p, expected->maxmem,
1888 key, expected->key_len) == 0) {
1889 t->err = "SCRYPT_ERROR";
1893 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1894 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1895 expected->salt, expected->salt_len,
1896 expected->id, expected->iter, expected->key_len,
1897 key, expected->md) == 0) {
1898 t->err = "PKCS12_ERROR";
1902 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1903 key, expected->key_len))
1912 static const EVP_TEST_METHOD pbe_test_method = {
1926 BASE64_CANONICAL_ENCODING = 0,
1927 BASE64_VALID_ENCODING = 1,
1928 BASE64_INVALID_ENCODING = 2
1929 } base64_encoding_type;
1931 typedef struct encode_data_st {
1932 /* Input to encoding */
1933 unsigned char *input;
1935 /* Expected output */
1936 unsigned char *output;
1938 base64_encoding_type encoding;
1941 static int encode_test_init(EVP_TEST *t, const char *encoding)
1945 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1947 if (strcmp(encoding, "canonical") == 0) {
1948 edata->encoding = BASE64_CANONICAL_ENCODING;
1949 } else if (strcmp(encoding, "valid") == 0) {
1950 edata->encoding = BASE64_VALID_ENCODING;
1951 } else if (strcmp(encoding, "invalid") == 0) {
1952 edata->encoding = BASE64_INVALID_ENCODING;
1953 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1956 TEST_error("Bad encoding: %s."
1957 " Should be one of {canonical, valid, invalid}",
1964 OPENSSL_free(edata);
1968 static void encode_test_cleanup(EVP_TEST *t)
1970 ENCODE_DATA *edata = t->data;
1972 OPENSSL_free(edata->input);
1973 OPENSSL_free(edata->output);
1974 memset(edata, 0, sizeof(*edata));
1977 static int encode_test_parse(EVP_TEST *t,
1978 const char *keyword, const char *value)
1980 ENCODE_DATA *edata = t->data;
1982 if (strcmp(keyword, "Input") == 0)
1983 return parse_bin(value, &edata->input, &edata->input_len);
1984 if (strcmp(keyword, "Output") == 0)
1985 return parse_bin(value, &edata->output, &edata->output_len);
1989 static int encode_test_run(EVP_TEST *t)
1991 ENCODE_DATA *expected = t->data;
1992 unsigned char *encode_out = NULL, *decode_out = NULL;
1993 int output_len, chunk_len;
1994 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1996 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1997 t->err = "INTERNAL_ERROR";
2001 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2003 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2004 || !TEST_ptr(encode_out =
2005 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2008 EVP_EncodeInit(encode_ctx);
2009 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2010 expected->input, expected->input_len)))
2013 output_len = chunk_len;
2015 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2016 output_len += chunk_len;
2018 if (!memory_err_compare(t, "BAD_ENCODING",
2019 expected->output, expected->output_len,
2020 encode_out, output_len))
2024 if (!TEST_ptr(decode_out =
2025 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2028 EVP_DecodeInit(decode_ctx);
2029 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2030 expected->output_len) < 0) {
2031 t->err = "DECODE_ERROR";
2034 output_len = chunk_len;
2036 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2037 t->err = "DECODE_ERROR";
2040 output_len += chunk_len;
2042 if (expected->encoding != BASE64_INVALID_ENCODING
2043 && !memory_err_compare(t, "BAD_DECODING",
2044 expected->input, expected->input_len,
2045 decode_out, output_len)) {
2046 t->err = "BAD_DECODING";
2052 OPENSSL_free(encode_out);
2053 OPENSSL_free(decode_out);
2054 EVP_ENCODE_CTX_free(decode_ctx);
2055 EVP_ENCODE_CTX_free(encode_ctx);
2059 static const EVP_TEST_METHOD encode_test_method = {
2062 encode_test_cleanup,
2072 #define MAX_RAND_REPEATS 15
2074 typedef struct rand_data_pass_st {
2075 unsigned char *entropy;
2076 unsigned char *reseed_entropy;
2077 unsigned char *nonce;
2078 unsigned char *pers;
2079 unsigned char *reseed_addin;
2080 unsigned char *addinA;
2081 unsigned char *addinB;
2082 unsigned char *pr_entropyA;
2083 unsigned char *pr_entropyB;
2084 unsigned char *output;
2085 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2086 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2090 typedef struct rand_data_st {
2091 /* Context for this operation */
2093 EVP_RAND_CTX *parent;
2095 int prediction_resistance;
2097 unsigned int generate_bits;
2101 /* Expected output */
2102 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2105 static int rand_test_init(EVP_TEST *t, const char *name)
2109 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2110 unsigned int strength = 256;
2112 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2115 rand = EVP_RAND_fetch(NULL, "TEST-RAND", NULL);
2118 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2119 EVP_RAND_free(rand);
2120 if (rdata->parent == NULL)
2123 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2124 if (!EVP_RAND_set_ctx_params(rdata->parent, params))
2127 rand = EVP_RAND_fetch(NULL, name, NULL);
2130 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2131 EVP_RAND_free(rand);
2132 if (rdata->ctx == NULL)
2139 EVP_RAND_CTX_free(rdata->parent);
2140 OPENSSL_free(rdata);
2144 static void rand_test_cleanup(EVP_TEST *t)
2146 RAND_DATA *rdata = t->data;
2149 OPENSSL_free(rdata->cipher);
2150 OPENSSL_free(rdata->digest);
2152 for (i = 0; i <= rdata->n; i++) {
2153 OPENSSL_free(rdata->data[i].entropy);
2154 OPENSSL_free(rdata->data[i].reseed_entropy);
2155 OPENSSL_free(rdata->data[i].nonce);
2156 OPENSSL_free(rdata->data[i].pers);
2157 OPENSSL_free(rdata->data[i].reseed_addin);
2158 OPENSSL_free(rdata->data[i].addinA);
2159 OPENSSL_free(rdata->data[i].addinB);
2160 OPENSSL_free(rdata->data[i].pr_entropyA);
2161 OPENSSL_free(rdata->data[i].pr_entropyB);
2162 OPENSSL_free(rdata->data[i].output);
2164 EVP_RAND_CTX_free(rdata->ctx);
2165 EVP_RAND_CTX_free(rdata->parent);
2168 static int rand_test_parse(EVP_TEST *t,
2169 const char *keyword, const char *value)
2171 RAND_DATA *rdata = t->data;
2172 RAND_DATA_PASS *item;
2176 if ((p = strchr(keyword, '.')) != NULL) {
2178 if (n >= MAX_RAND_REPEATS)
2182 item = rdata->data + n;
2183 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2184 return parse_bin(value, &item->entropy, &item->entropy_len);
2185 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2186 return parse_bin(value, &item->reseed_entropy,
2187 &item->reseed_entropy_len);
2188 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2189 return parse_bin(value, &item->nonce, &item->nonce_len);
2190 if (strncmp(keyword, "PersonalisationString.",
2191 sizeof("PersonalisationString")) == 0)
2192 return parse_bin(value, &item->pers, &item->pers_len);
2193 if (strncmp(keyword, "ReseedAdditionalInput.",
2194 sizeof("ReseedAdditionalInput")) == 0)
2195 return parse_bin(value, &item->reseed_addin,
2196 &item->reseed_addin_len);
2197 if (strncmp(keyword, "AdditionalInputA.",
2198 sizeof("AdditionalInputA")) == 0)
2199 return parse_bin(value, &item->addinA, &item->addinA_len);
2200 if (strncmp(keyword, "AdditionalInputB.",
2201 sizeof("AdditionalInputB")) == 0)
2202 return parse_bin(value, &item->addinB, &item->addinB_len);
2203 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2204 sizeof("EntropyPredictionResistanceA")) == 0)
2205 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2206 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2207 sizeof("EntropyPredictionResistanceB")) == 0)
2208 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2209 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2210 return parse_bin(value, &item->output, &item->output_len);
2212 if (strcmp(keyword, "Cipher") == 0)
2213 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2214 if (strcmp(keyword, "Digest") == 0)
2215 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2216 if (strcmp(keyword, "DerivationFunction") == 0) {
2217 rdata->use_df = atoi(value) != 0;
2220 if (strcmp(keyword, "GenerateBits") == 0) {
2221 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2223 rdata->generate_bits = (unsigned int)n;
2226 if (strcmp(keyword, "PredictionResistance") == 0) {
2227 rdata->prediction_resistance = atoi(value) != 0;
2234 static int rand_test_run(EVP_TEST *t)
2236 RAND_DATA *expected = t->data;
2237 RAND_DATA_PASS *item;
2239 size_t got_len = expected->generate_bits / 8;
2240 OSSL_PARAM params[5], *p = params;
2241 int i = -1, ret = 0;
2242 unsigned int strength;
2245 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2248 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2249 if (expected->cipher != NULL)
2250 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2251 expected->cipher, 0);
2252 if (expected->digest != NULL)
2253 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2254 expected->digest, 0);
2255 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2256 *p = OSSL_PARAM_construct_end();
2257 if (!TEST_true(EVP_RAND_set_ctx_params(expected->ctx, params)))
2260 strength = EVP_RAND_strength(expected->ctx);
2261 for (i = 0; i <= expected->n; i++) {
2262 item = expected->data + i;
2265 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2266 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2267 z, item->entropy_len);
2268 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2269 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2270 z, item->nonce_len);
2271 *p = OSSL_PARAM_construct_end();
2272 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params))
2273 || !TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2277 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2278 if (!TEST_true(EVP_RAND_instantiate
2279 (expected->ctx, strength,
2280 expected->prediction_resistance, z,
2284 if (item->reseed_entropy != NULL) {
2285 params[0] = OSSL_PARAM_construct_octet_string
2286 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2287 item->reseed_entropy_len);
2288 params[1] = OSSL_PARAM_construct_end();
2289 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2292 if (!TEST_true(EVP_RAND_reseed
2293 (expected->ctx, expected->prediction_resistance,
2294 NULL, 0, item->reseed_addin,
2295 item->reseed_addin_len)))
2298 if (item->pr_entropyA != NULL) {
2299 params[0] = OSSL_PARAM_construct_octet_string
2300 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2301 item->pr_entropyA_len);
2302 params[1] = OSSL_PARAM_construct_end();
2303 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2306 if (!TEST_true(EVP_RAND_generate
2307 (expected->ctx, got, got_len,
2308 strength, expected->prediction_resistance,
2309 item->addinA, item->addinA_len)))
2312 if (item->pr_entropyB != NULL) {
2313 params[0] = OSSL_PARAM_construct_octet_string
2314 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2315 item->pr_entropyB_len);
2316 params[1] = OSSL_PARAM_construct_end();
2317 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2320 if (!TEST_true(EVP_RAND_generate
2321 (expected->ctx, got, got_len,
2322 strength, expected->prediction_resistance,
2323 item->addinB, item->addinB_len)))
2325 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2327 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2328 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2329 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2330 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2331 EVP_RAND_STATE_UNINITIALISED))
2338 if (ret == 0 && i >= 0)
2339 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2344 static const EVP_TEST_METHOD rand_test_method = {
2357 typedef struct kdf_data_st {
2358 /* Context for this operation */
2360 /* Expected output */
2361 unsigned char *output;
2363 OSSL_PARAM params[20];
2368 * Perform public key operation setup: lookup key, allocated ctx and call
2369 * the appropriate initialisation function
2371 static int kdf_test_init(EVP_TEST *t, const char *name)
2376 #ifdef OPENSSL_NO_SCRYPT
2377 /* TODO(3.0) Replace with "scrypt" once aliases are supported */
2378 if (strcmp(name, "id-scrypt") == 0) {
2382 #endif /* OPENSSL_NO_SCRYPT */
2384 #ifdef OPENSSL_NO_CMS
2385 if (strcmp(name, "X942KDF") == 0) {
2389 #endif /* OPENSSL_NO_CMS */
2391 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2393 kdata->p = kdata->params;
2394 *kdata->p = OSSL_PARAM_construct_end();
2396 kdf = EVP_KDF_fetch(NULL, name, NULL);
2398 OPENSSL_free(kdata);
2401 kdata->ctx = EVP_KDF_CTX_new(kdf);
2403 if (kdata->ctx == NULL) {
2404 OPENSSL_free(kdata);
2411 static void kdf_test_cleanup(EVP_TEST *t)
2413 KDF_DATA *kdata = t->data;
2416 for (p = kdata->params; p->key != NULL; p++)
2417 OPENSSL_free(p->data);
2418 OPENSSL_free(kdata->output);
2419 EVP_KDF_CTX_free(kdata->ctx);
2422 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2425 KDF_DATA *kdata = t->data;
2428 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2430 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2432 p = strchr(name, ':');
2436 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2437 p != NULL ? strlen(p) : 0, NULL);
2438 *++kdata->p = OSSL_PARAM_construct_end();
2440 t->err = "KDF_PARAM_ERROR";
2444 if (p != NULL && strcmp(name, "digest") == 0) {
2445 /* If p has an OID and lookup fails assume disabled algorithm */
2446 int nid = OBJ_sn2nid(p);
2448 if (nid == NID_undef)
2449 nid = OBJ_ln2nid(p);
2450 if (nid != NID_undef && EVP_get_digestbynid(nid) == NULL)
2453 if (p != NULL && strcmp(name, "cipher") == 0) {
2454 /* If p has an OID and lookup fails assume disabled algorithm */
2455 int nid = OBJ_sn2nid(p);
2457 if (nid == NID_undef)
2458 nid = OBJ_ln2nid(p);
2459 if (nid != NID_undef && EVP_get_cipherbynid(nid) == NULL)
2466 static int kdf_test_parse(EVP_TEST *t,
2467 const char *keyword, const char *value)
2469 KDF_DATA *kdata = t->data;
2471 if (strcmp(keyword, "Output") == 0)
2472 return parse_bin(value, &kdata->output, &kdata->output_len);
2473 if (strncmp(keyword, "Ctrl", 4) == 0)
2474 return kdf_test_ctrl(t, kdata->ctx, value);
2478 static int kdf_test_run(EVP_TEST *t)
2480 KDF_DATA *expected = t->data;
2481 unsigned char *got = NULL;
2482 size_t got_len = expected->output_len;
2484 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2485 t->err = "KDF_CTRL_ERROR";
2488 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2489 t->err = "INTERNAL_ERROR";
2492 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2493 t->err = "KDF_DERIVE_ERROR";
2496 if (!memory_err_compare(t, "KDF_MISMATCH",
2497 expected->output, expected->output_len,
2508 static const EVP_TEST_METHOD kdf_test_method = {
2521 typedef struct pkey_kdf_data_st {
2522 /* Context for this operation */
2524 /* Expected output */
2525 unsigned char *output;
2530 * Perform public key operation setup: lookup key, allocated ctx and call
2531 * the appropriate initialisation function
2533 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2535 PKEY_KDF_DATA *kdata;
2536 int kdf_nid = OBJ_sn2nid(name);
2538 #ifdef OPENSSL_NO_SCRYPT
2539 if (strcmp(name, "scrypt") == 0) {
2543 #endif /* OPENSSL_NO_SCRYPT */
2545 #ifdef OPENSSL_NO_CMS
2546 if (strcmp(name, "X942KDF") == 0) {
2550 #endif /* OPENSSL_NO_CMS */
2552 if (kdf_nid == NID_undef)
2553 kdf_nid = OBJ_ln2nid(name);
2555 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2557 kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
2558 if (kdata->ctx == NULL) {
2559 OPENSSL_free(kdata);
2562 if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
2563 EVP_PKEY_CTX_free(kdata->ctx);
2564 OPENSSL_free(kdata);
2571 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2573 PKEY_KDF_DATA *kdata = t->data;
2575 OPENSSL_free(kdata->output);
2576 EVP_PKEY_CTX_free(kdata->ctx);
2579 static int pkey_kdf_test_parse(EVP_TEST *t,
2580 const char *keyword, const char *value)
2582 PKEY_KDF_DATA *kdata = t->data;
2584 if (strcmp(keyword, "Output") == 0)
2585 return parse_bin(value, &kdata->output, &kdata->output_len);
2586 if (strncmp(keyword, "Ctrl", 4) == 0)
2587 return pkey_test_ctrl(t, kdata->ctx, value);
2591 static int pkey_kdf_test_run(EVP_TEST *t)
2593 PKEY_KDF_DATA *expected = t->data;
2594 unsigned char *got = NULL;
2595 size_t got_len = expected->output_len;
2597 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2598 t->err = "INTERNAL_ERROR";
2601 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2602 t->err = "KDF_DERIVE_ERROR";
2605 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2606 t->err = "KDF_MISMATCH";
2616 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2619 pkey_kdf_test_cleanup,
2620 pkey_kdf_test_parse,
2629 typedef struct keypair_test_data_st {
2632 } KEYPAIR_TEST_DATA;
2634 static int keypair_test_init(EVP_TEST *t, const char *pair)
2636 KEYPAIR_TEST_DATA *data;
2638 EVP_PKEY *pk = NULL, *pubk = NULL;
2639 char *pub, *priv = NULL;
2641 /* Split private and public names. */
2642 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2643 || !TEST_ptr(pub = strchr(priv, ':'))) {
2644 t->err = "PARSING_ERROR";
2649 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2650 TEST_info("Can't find private key: %s", priv);
2651 t->err = "MISSING_PRIVATE_KEY";
2654 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2655 TEST_info("Can't find public key: %s", pub);
2656 t->err = "MISSING_PUBLIC_KEY";
2660 if (pk == NULL && pubk == NULL) {
2661 /* Both keys are listed but unsupported: skip this test */
2667 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2680 static void keypair_test_cleanup(EVP_TEST *t)
2682 OPENSSL_free(t->data);
2687 * For tests that do not accept any custom keywords.
2689 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2694 static int keypair_test_run(EVP_TEST *t)
2697 const KEYPAIR_TEST_DATA *pair = t->data;
2699 if (pair->privk == NULL || pair->pubk == NULL) {
2701 * this can only happen if only one of the keys is not set
2702 * which means that one of them was unsupported while the
2703 * other isn't: hence a key type mismatch.
2705 t->err = "KEYPAIR_TYPE_MISMATCH";
2710 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2712 t->err = "KEYPAIR_MISMATCH";
2713 } else if ( -1 == rv ) {
2714 t->err = "KEYPAIR_TYPE_MISMATCH";
2715 } else if ( -2 == rv ) {
2716 t->err = "UNSUPPORTED_KEY_COMPARISON";
2718 TEST_error("Unexpected error in key comparison");
2733 static const EVP_TEST_METHOD keypair_test_method = {
2736 keypair_test_cleanup,
2745 typedef struct keygen_test_data_st {
2746 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2747 char *keyname; /* Key name to store key or NULL */
2750 static int keygen_test_init(EVP_TEST *t, const char *alg)
2752 KEYGEN_TEST_DATA *data;
2753 EVP_PKEY_CTX *genctx;
2754 int nid = OBJ_sn2nid(alg);
2756 if (nid == NID_undef) {
2757 nid = OBJ_ln2nid(alg);
2758 if (nid == NID_undef)
2762 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
2763 /* assume algorithm disabled */
2768 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2769 t->err = "KEYGEN_INIT_ERROR";
2773 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2775 data->genctx = genctx;
2776 data->keyname = NULL;
2782 EVP_PKEY_CTX_free(genctx);
2786 static void keygen_test_cleanup(EVP_TEST *t)
2788 KEYGEN_TEST_DATA *keygen = t->data;
2790 EVP_PKEY_CTX_free(keygen->genctx);
2791 OPENSSL_free(keygen->keyname);
2792 OPENSSL_free(t->data);
2796 static int keygen_test_parse(EVP_TEST *t,
2797 const char *keyword, const char *value)
2799 KEYGEN_TEST_DATA *keygen = t->data;
2801 if (strcmp(keyword, "KeyName") == 0)
2802 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2803 if (strcmp(keyword, "Ctrl") == 0)
2804 return pkey_test_ctrl(t, keygen->genctx, value);
2808 static int keygen_test_run(EVP_TEST *t)
2810 KEYGEN_TEST_DATA *keygen = t->data;
2811 EVP_PKEY *pkey = NULL;
2814 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2815 t->err = "KEYGEN_GENERATE_ERROR";
2819 if (keygen->keyname != NULL) {
2823 if (find_key(NULL, keygen->keyname, private_keys)) {
2824 TEST_info("Duplicate key %s", keygen->keyname);
2828 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2830 key->name = keygen->keyname;
2831 keygen->keyname = NULL;
2833 key->next = private_keys;
2837 EVP_PKEY_free(pkey);
2846 static const EVP_TEST_METHOD keygen_test_method = {
2849 keygen_test_cleanup,
2855 *** DIGEST SIGN+VERIFY TESTS
2859 int is_verify; /* Set to 1 if verifying */
2860 int is_oneshot; /* Set to 1 for one shot operation */
2861 const EVP_MD *md; /* Digest to use */
2862 EVP_MD_CTX *ctx; /* Digest context */
2864 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2865 unsigned char *osin; /* Input data if one shot */
2866 size_t osin_len; /* Input length data if one shot */
2867 unsigned char *output; /* Expected output */
2868 size_t output_len; /* Expected output length */
2871 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2874 const EVP_MD *md = NULL;
2875 DIGESTSIGN_DATA *mdat;
2877 if (strcmp(alg, "NULL") != 0) {
2878 if ((md = EVP_get_digestbyname(alg)) == NULL) {
2879 /* If alg has an OID assume disabled algorithm */
2880 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
2887 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2890 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2894 mdat->is_verify = is_verify;
2895 mdat->is_oneshot = is_oneshot;
2900 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2902 return digestsigver_test_init(t, alg, 0, 0);
2905 static void digestsigver_test_cleanup(EVP_TEST *t)
2907 DIGESTSIGN_DATA *mdata = t->data;
2909 EVP_MD_CTX_free(mdata->ctx);
2910 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2911 OPENSSL_free(mdata->osin);
2912 OPENSSL_free(mdata->output);
2913 OPENSSL_free(mdata);
2917 static int digestsigver_test_parse(EVP_TEST *t,
2918 const char *keyword, const char *value)
2920 DIGESTSIGN_DATA *mdata = t->data;
2922 if (strcmp(keyword, "Key") == 0) {
2923 EVP_PKEY *pkey = NULL;
2926 if (mdata->is_verify)
2927 rv = find_key(&pkey, value, public_keys);
2929 rv = find_key(&pkey, value, private_keys);
2930 if (rv == 0 || pkey == NULL) {
2934 if (mdata->is_verify) {
2935 if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
2937 t->err = "DIGESTVERIFYINIT_ERROR";
2940 if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
2942 t->err = "DIGESTSIGNINIT_ERROR";
2946 if (strcmp(keyword, "Input") == 0) {
2947 if (mdata->is_oneshot)
2948 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2949 return evp_test_buffer_append(value, &mdata->input);
2951 if (strcmp(keyword, "Output") == 0)
2952 return parse_bin(value, &mdata->output, &mdata->output_len);
2954 if (!mdata->is_oneshot) {
2955 if (strcmp(keyword, "Count") == 0)
2956 return evp_test_buffer_set_count(value, mdata->input);
2957 if (strcmp(keyword, "Ncopy") == 0)
2958 return evp_test_buffer_ncopy(value, mdata->input);
2960 if (strcmp(keyword, "Ctrl") == 0) {
2961 if (mdata->pctx == NULL)
2963 return pkey_test_ctrl(t, mdata->pctx, value);
2968 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2971 return EVP_DigestSignUpdate(ctx, buf, buflen);
2974 static int digestsign_test_run(EVP_TEST *t)
2976 DIGESTSIGN_DATA *expected = t->data;
2977 unsigned char *got = NULL;
2980 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2982 t->err = "DIGESTUPDATE_ERROR";
2986 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2987 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2990 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2991 t->err = "MALLOC_FAILURE";
2994 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2995 t->err = "DIGESTSIGNFINAL_ERROR";
2998 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2999 expected->output, expected->output_len,
3009 static const EVP_TEST_METHOD digestsign_test_method = {
3011 digestsign_test_init,
3012 digestsigver_test_cleanup,
3013 digestsigver_test_parse,
3017 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3019 return digestsigver_test_init(t, alg, 1, 0);
3022 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3025 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3028 static int digestverify_test_run(EVP_TEST *t)
3030 DIGESTSIGN_DATA *mdata = t->data;
3032 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3033 t->err = "DIGESTUPDATE_ERROR";
3037 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3038 mdata->output_len) <= 0)
3039 t->err = "VERIFY_ERROR";
3043 static const EVP_TEST_METHOD digestverify_test_method = {
3045 digestverify_test_init,
3046 digestsigver_test_cleanup,
3047 digestsigver_test_parse,
3048 digestverify_test_run
3051 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3053 return digestsigver_test_init(t, alg, 0, 1);
3056 static int oneshot_digestsign_test_run(EVP_TEST *t)
3058 DIGESTSIGN_DATA *expected = t->data;
3059 unsigned char *got = NULL;
3062 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3063 expected->osin, expected->osin_len)) {
3064 t->err = "DIGESTSIGN_LENGTH_ERROR";
3067 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3068 t->err = "MALLOC_FAILURE";
3071 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3072 expected->osin, expected->osin_len)) {
3073 t->err = "DIGESTSIGN_ERROR";
3076 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3077 expected->output, expected->output_len,
3087 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3088 "OneShotDigestSign",
3089 oneshot_digestsign_test_init,
3090 digestsigver_test_cleanup,
3091 digestsigver_test_parse,
3092 oneshot_digestsign_test_run
3095 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3097 return digestsigver_test_init(t, alg, 1, 1);
3100 static int oneshot_digestverify_test_run(EVP_TEST *t)
3102 DIGESTSIGN_DATA *mdata = t->data;
3104 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3105 mdata->osin, mdata->osin_len) <= 0)
3106 t->err = "VERIFY_ERROR";
3110 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3111 "OneShotDigestVerify",
3112 oneshot_digestverify_test_init,
3113 digestsigver_test_cleanup,
3114 digestsigver_test_parse,
3115 oneshot_digestverify_test_run
3120 *** PARSING AND DISPATCH
3123 static const EVP_TEST_METHOD *evp_test_list[] = {
3125 &cipher_test_method,
3126 &digest_test_method,
3127 &digestsign_test_method,
3128 &digestverify_test_method,
3129 &encode_test_method,
3131 &pkey_kdf_test_method,
3132 &keypair_test_method,
3133 &keygen_test_method,
3135 &oneshot_digestsign_test_method,
3136 &oneshot_digestverify_test_method,
3138 &pdecrypt_test_method,
3139 &pderive_test_method,
3141 &pverify_recover_test_method,
3142 &pverify_test_method,
3146 static const EVP_TEST_METHOD *find_test(const char *name)
3148 const EVP_TEST_METHOD **tt;
3150 for (tt = evp_test_list; *tt; tt++) {
3151 if (strcmp(name, (*tt)->name) == 0)
3157 static void clear_test(EVP_TEST *t)
3159 test_clearstanza(&t->s);
3161 if (t->data != NULL) {
3162 if (t->meth != NULL)
3163 t->meth->cleanup(t);
3164 OPENSSL_free(t->data);
3167 OPENSSL_free(t->expected_err);
3168 t->expected_err = NULL;
3169 OPENSSL_free(t->reason);
3179 * Check for errors in the test structure; return 1 if okay, else 0.
3181 static int check_test_error(EVP_TEST *t)
3186 if (t->err == NULL && t->expected_err == NULL)
3188 if (t->err != NULL && t->expected_err == NULL) {
3189 if (t->aux_err != NULL) {
3190 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3191 t->s.test_file, t->s.start, t->aux_err, t->err);
3193 TEST_info("%s:%d: Source of above error; unexpected error %s",
3194 t->s.test_file, t->s.start, t->err);
3198 if (t->err == NULL && t->expected_err != NULL) {
3199 TEST_info("%s:%d: Succeeded but was expecting %s",
3200 t->s.test_file, t->s.start, t->expected_err);
3204 if (strcmp(t->err, t->expected_err) != 0) {
3205 TEST_info("%s:%d: Expected %s got %s",
3206 t->s.test_file, t->s.start, t->expected_err, t->err);
3210 if (t->reason == NULL)
3213 if (t->reason == NULL) {
3214 TEST_info("%s:%d: Test is missing function or reason code",
3215 t->s.test_file, t->s.start);
3219 err = ERR_peek_error();
3221 TEST_info("%s:%d: Expected error \"%s\" not set",
3222 t->s.test_file, t->s.start, t->reason);
3226 reason = ERR_reason_error_string(err);
3227 if (reason == NULL) {
3228 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3230 t->s.test_file, t->s.start, t->reason);
3234 if (strcmp(reason, t->reason) == 0)
3237 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3238 t->s.test_file, t->s.start, t->reason, reason);
3244 * Run a parsed test. Log a message and return 0 on error.
3246 static int run_test(EVP_TEST *t)
3248 if (t->meth == NULL)
3255 if (t->err == NULL && t->meth->run_test(t) != 1) {
3256 TEST_info("%s:%d %s error",
3257 t->s.test_file, t->s.start, t->meth->name);
3260 if (!check_test_error(t)) {
3261 TEST_openssl_errors();
3270 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3272 for (; lst != NULL; lst = lst->next) {
3273 if (strcmp(lst->name, name) == 0) {
3282 static void free_key_list(KEY_LIST *lst)
3284 while (lst != NULL) {
3285 KEY_LIST *next = lst->next;
3287 EVP_PKEY_free(lst->key);
3288 OPENSSL_free(lst->name);
3295 * Is the key type an unsupported algorithm?
3297 static int key_unsupported(void)
3299 long err = ERR_peek_error();
3301 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3302 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM
3303 || ERR_GET_REASON(err) == EVP_R_FETCH_FAILED)) {
3307 #ifndef OPENSSL_NO_EC
3309 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3310 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3313 if (ERR_GET_LIB(err) == ERR_LIB_EC
3314 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
3318 #endif /* OPENSSL_NO_EC */
3323 * NULL out the value from |pp| but return it. This "steals" a pointer.
3325 static char *take_value(PAIR *pp)
3327 char *p = pp->value;
3334 * Return 1 if one of the providers named in the string is available.
3335 * The provider names are separated with whitespace.
3336 * NOTE: destructive function, it inserts '\0' after each provider name.
3338 static int prov_available(char *providers)
3344 for (; isspace(*providers); providers++)
3346 if (*providers == '\0')
3347 break; /* End of the road */
3348 for (p = providers; *p != '\0' && !isspace(*p); p++)
3354 if (OSSL_PROVIDER_available(NULL, providers))
3355 return 1; /* Found one */
3361 * Read and parse one test. Return 0 if failure, 1 if okay.
3363 static int parse(EVP_TEST *t)
3365 KEY_LIST *key, **klist;
3372 if (BIO_eof(t->s.fp))
3375 if (!test_readstanza(&t->s))
3377 } while (t->s.numpairs == 0);
3378 pp = &t->s.pairs[0];
3380 /* Are we adding a key? */
3383 if (strcmp(pp->key, "PrivateKey") == 0) {
3384 pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
3385 if (pkey == NULL && !key_unsupported()) {
3386 EVP_PKEY_free(pkey);
3387 TEST_info("Can't read private key %s", pp->value);
3388 TEST_openssl_errors();
3391 klist = &private_keys;
3392 } else if (strcmp(pp->key, "PublicKey") == 0) {
3393 pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
3394 if (pkey == NULL && !key_unsupported()) {
3395 EVP_PKEY_free(pkey);
3396 TEST_info("Can't read public key %s", pp->value);
3397 TEST_openssl_errors();
3400 klist = &public_keys;
3401 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3402 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3403 char *strnid = NULL, *keydata = NULL;
3404 unsigned char *keybin;
3408 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3409 klist = &private_keys;
3411 klist = &public_keys;
3413 strnid = strchr(pp->value, ':');
3414 if (strnid != NULL) {
3416 keydata = strchr(strnid, ':');
3417 if (keydata != NULL)
3420 if (keydata == NULL) {
3421 TEST_info("Failed to parse %s value", pp->key);
3425 nid = OBJ_txt2nid(strnid);
3426 if (nid == NID_undef) {
3427 TEST_info("Uncrecognised algorithm NID");
3430 if (!parse_bin(keydata, &keybin, &keylen)) {
3431 TEST_info("Failed to create binary key");
3434 if (klist == &private_keys)
3435 pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
3437 pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
3438 if (pkey == NULL && !key_unsupported()) {
3439 TEST_info("Can't read %s data", pp->key);
3440 OPENSSL_free(keybin);
3441 TEST_openssl_errors();
3444 OPENSSL_free(keybin);
3447 /* If we have a key add to list */
3448 if (klist != NULL) {
3449 if (find_key(NULL, pp->value, *klist)) {
3450 TEST_info("Duplicate key %s", pp->value);
3453 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3455 key->name = take_value(pp);
3460 /* Go back and start a new stanza. */
3461 if (t->s.numpairs != 1)
3462 TEST_info("Line %d: missing blank line\n", t->s.curr);
3466 /* Find the test, based on first keyword. */
3467 if (!TEST_ptr(t->meth = find_test(pp->key)))
3469 if (!t->meth->init(t, pp->value)) {
3470 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3474 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3478 for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
3479 if (strcmp(pp->key, "Availablein") == 0) {
3480 if (!prov_available(pp->value)) {
3481 TEST_info("skipping, providers not available: %s:%d",
3482 t->s.test_file, t->s.start);
3486 } else if (strcmp(pp->key, "Result") == 0) {
3487 if (t->expected_err != NULL) {
3488 TEST_info("Line %d: multiple result lines", t->s.curr);
3491 t->expected_err = take_value(pp);
3492 } else if (strcmp(pp->key, "Function") == 0) {
3493 /* Ignore old line. */
3494 } else if (strcmp(pp->key, "Reason") == 0) {
3495 if (t->reason != NULL) {
3496 TEST_info("Line %d: multiple reason lines", t->s.curr);
3499 t->reason = take_value(pp);
3501 /* Must be test specific line: try to parse it */
3502 int rv = t->meth->parse(t, pp->key, pp->value);
3505 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3509 TEST_info("Line %d: error processing keyword %s = %s\n",
3510 t->s.curr, pp->key, pp->value);
3519 static int run_file_tests(int i)
3522 const char *testfile = test_get_argument(i);
3525 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3527 if (!test_start_file(&t->s, testfile)) {
3532 while (!BIO_eof(t->s.fp)) {
3538 if (c == 0 || !run_test(t)) {
3543 test_end_file(&t->s);
3546 free_key_list(public_keys);
3547 free_key_list(private_keys);
3554 OPT_TEST_DECLARE_USAGE("file...\n")
3556 int setup_tests(void)
3560 if (!test_skip_common_options()) {
3561 TEST_error("Error parsing test options\n");
3565 n = test_get_argument_count();
3569 ADD_ALL_TESTS(run_file_tests, n);