2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pkcs12.h>
19 #include <openssl/kdf.h>
20 #include "internal/numbers.h"
25 typedef struct evp_test_method_st EVP_TEST_METHOD;
28 * Structure holding test information
30 typedef struct evp_test_st {
31 BIO *in; /* file being read */
32 int line; /* current line being processed */
33 int start_line; /* start line of current test */
34 int ntests; /* Number of tests */
35 int errors; /* Error count */
36 int skip; /* Current test should be skipped */
37 int nskip; /* Number of tests skipped */
38 char buf[10240]; /* Input buffer */
39 BIO *key; /* temp memory BIO for reading in keys */
40 const EVP_TEST_METHOD *meth; /* method for this test */
41 const char *err, *aux_err; /* Error string for test */
42 char *expected_err; /* Expected error value of test */
43 char *func; /* Expected error function string */
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);
83 static const char *current_test_file = "???";
86 * Structure used to hold a list of blocks of memory to test
87 * calls to "update" like functions.
89 struct evp_test_buffer_st {
96 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
99 OPENSSL_free(db->buf);
105 * append buffer to a list
107 static int evp_test_buffer_append(const char *value,
108 STACK_OF(EVP_TEST_BUFFER) **sk)
110 EVP_TEST_BUFFER *db = NULL;
112 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
115 if (!parse_bin(value, &db->buf, &db->buflen))
120 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
122 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
128 evp_test_buffer_free(db);
133 * replace last buffer in list with copies of itself
135 static int evp_test_buffer_ncopy(const char *value,
136 STACK_OF(EVP_TEST_BUFFER) *sk)
139 unsigned char *tbuf, *p;
141 int ncopy = atoi(value);
146 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
148 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
150 tbuflen = db->buflen * ncopy;
151 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
153 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
154 memcpy(p, db->buf, db->buflen);
156 OPENSSL_free(db->buf);
158 db->buflen = tbuflen;
163 * set repeat count for last buffer in list
165 static int evp_test_buffer_set_count(const char *value,
166 STACK_OF(EVP_TEST_BUFFER) *sk)
169 int count = atoi(value);
174 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
177 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
178 if (db->count_set != 0)
181 db->count = (size_t)count;
187 * call "fn" with each element of the list in turn
189 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
191 const unsigned char *buf,
197 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
198 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
201 for (j = 0; j < tb->count; j++) {
202 if (fn(ctx, tb->buf, tb->buflen) <= 0)
210 * Unescape some sequences in string literals (only \n for now).
211 * Return an allocated buffer, set |out_len|. If |input_len|
212 * is zero, get an empty buffer but set length to zero.
214 static unsigned char* unescape(const char *input, size_t input_len,
217 unsigned char *ret, *p;
220 if (input_len == 0) {
222 return OPENSSL_zalloc(1);
225 /* Escaping is non-expanding; over-allocate original size for simplicity. */
226 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
229 for (i = 0; i < input_len; i++) {
230 if (*input == '\\') {
231 if (i == input_len - 1 || *++input != 'n') {
232 TEST_error("Bad escape sequence in file");
252 * For a hex string "value" convert to a binary allocated buffer.
253 * Return 1 on success or 0 on failure.
255 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
259 /* Check for NULL literal */
260 if (strcmp(value, "NULL") == 0) {
266 /* Check for empty value */
267 if (*value == '\0') {
269 * Don't return NULL for zero length buffer. This is needed for
270 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
271 * buffer even if the key length is 0, in order to detect key reset.
273 *buf = OPENSSL_malloc(1);
281 /* Check for string literal */
282 if (value[0] == '"') {
283 size_t vlen = strlen(++value);
285 if (vlen == 0 || value[vlen - 1] != '"')
288 *buf = unescape(value, vlen, buflen);
289 return *buf == NULL ? 0 : 1;
292 /* Otherwise assume as hex literal and convert it to binary buffer */
293 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
294 TEST_info("Can't convert %s", value);
295 ERR_print_errors(bio_err);
298 /* Size of input buffer means we'll never overflow */
305 *** MESSAGE DIGEST TESTS
308 typedef struct digest_data_st {
309 /* Digest this test is for */
310 const EVP_MD *digest;
311 /* Input to digest */
312 STACK_OF(EVP_TEST_BUFFER) *input;
313 /* Expected output */
314 unsigned char *output;
318 static int digest_test_init(EVP_TEST *t, const char *alg)
321 const EVP_MD *digest;
323 if ((digest = EVP_get_digestbyname(alg)) == NULL) {
324 /* If alg has an OID assume disabled algorithm */
325 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
331 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
334 mdat->digest = digest;
338 static void digest_test_cleanup(EVP_TEST *t)
340 DIGEST_DATA *mdat = t->data;
342 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
343 OPENSSL_free(mdat->output);
346 static int digest_test_parse(EVP_TEST *t,
347 const char *keyword, const char *value)
349 DIGEST_DATA *mdata = t->data;
351 if (strcmp(keyword, "Input") == 0)
352 return evp_test_buffer_append(value, &mdata->input);
353 if (strcmp(keyword, "Output") == 0)
354 return parse_bin(value, &mdata->output, &mdata->output_len);
355 if (strcmp(keyword, "Count") == 0)
356 return evp_test_buffer_set_count(value, mdata->input);
357 if (strcmp(keyword, "Ncopy") == 0)
358 return evp_test_buffer_ncopy(value, mdata->input);
362 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
364 return EVP_DigestUpdate(ctx, buf, buflen);
367 static int digest_test_run(EVP_TEST *t)
369 DIGEST_DATA *expected = t->data;
371 unsigned char got[EVP_MAX_MD_SIZE];
372 unsigned int got_len;
374 t->err = "TEST_FAILURE";
375 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
378 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
379 t->err = "DIGESTINIT_ERROR";
382 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
383 t->err = "DIGESTUPDATE_ERROR";
387 if (!EVP_DigestFinal(mctx, got, &got_len)) {
388 t->err = "DIGESTFINAL_ERROR";
391 if (!TEST_int_eq(expected->output_len, got_len)) {
392 t->err = "DIGEST_LENGTH_MISMATCH";
395 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
396 t->err = "DIGEST_MISMATCH";
402 EVP_MD_CTX_free(mctx);
406 static const EVP_TEST_METHOD digest_test_method = {
419 typedef struct cipher_data_st {
420 const EVP_CIPHER *cipher;
422 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
428 unsigned char *plaintext;
429 size_t plaintext_len;
430 unsigned char *ciphertext;
431 size_t ciphertext_len;
439 static int cipher_test_init(EVP_TEST *t, const char *alg)
441 const EVP_CIPHER *cipher;
445 if ((cipher = EVP_get_cipherbyname(alg)) == NULL) {
446 /* If alg has an OID assume disabled algorithm */
447 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
453 cdat = OPENSSL_zalloc(sizeof(*cdat));
454 cdat->cipher = cipher;
456 m = EVP_CIPHER_mode(cipher);
457 if (m == EVP_CIPH_GCM_MODE
458 || m == EVP_CIPH_OCB_MODE
459 || m == EVP_CIPH_CCM_MODE)
460 cdat->aead = EVP_CIPHER_mode(cipher);
461 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
470 static void cipher_test_cleanup(EVP_TEST *t)
472 CIPHER_DATA *cdat = t->data;
474 OPENSSL_free(cdat->key);
475 OPENSSL_free(cdat->iv);
476 OPENSSL_free(cdat->ciphertext);
477 OPENSSL_free(cdat->plaintext);
478 OPENSSL_free(cdat->aad);
479 OPENSSL_free(cdat->tag);
482 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
485 CIPHER_DATA *cdat = t->data;
487 if (strcmp(keyword, "Key") == 0)
488 return parse_bin(value, &cdat->key, &cdat->key_len);
489 if (strcmp(keyword, "IV") == 0)
490 return parse_bin(value, &cdat->iv, &cdat->iv_len);
491 if (strcmp(keyword, "Plaintext") == 0)
492 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
493 if (strcmp(keyword, "Ciphertext") == 0)
494 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
496 if (strcmp(keyword, "AAD") == 0)
497 return parse_bin(value, &cdat->aad, &cdat->aad_len);
498 if (strcmp(keyword, "Tag") == 0)
499 return parse_bin(value, &cdat->tag, &cdat->tag_len);
502 if (strcmp(keyword, "Operation") == 0) {
503 if (strcmp(value, "ENCRYPT") == 0)
505 else if (strcmp(value, "DECRYPT") == 0)
514 static int cipher_test_enc(EVP_TEST *t, int enc,
515 size_t out_misalign, size_t inp_misalign, int frag)
517 CIPHER_DATA *expected = t->data;
518 unsigned char *in, *expected_out, *tmp = NULL;
519 size_t in_len, out_len, donelen = 0;
520 int ok = 0, tmplen, chunklen, tmpflen;
521 EVP_CIPHER_CTX *ctx = NULL;
523 t->err = "TEST_FAILURE";
524 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
526 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
528 in = expected->plaintext;
529 in_len = expected->plaintext_len;
530 expected_out = expected->ciphertext;
531 out_len = expected->ciphertext_len;
533 in = expected->ciphertext;
534 in_len = expected->ciphertext_len;
535 expected_out = expected->plaintext;
536 out_len = expected->plaintext_len;
538 if (inp_misalign == (size_t)-1) {
540 * Exercise in-place encryption
542 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
545 in = memcpy(tmp + out_misalign, in, in_len);
547 inp_misalign += 16 - ((out_misalign + in_len) & 15);
549 * 'tmp' will store both output and copy of input. We make the copy
550 * of input to specifically aligned part of 'tmp'. So we just
551 * figured out how much padding would ensure the required alignment,
552 * now we allocate extended buffer and finally copy the input just
553 * past inp_misalign in expression below. Output will be written
554 * past out_misalign...
556 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
557 inp_misalign + in_len);
560 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
561 inp_misalign, in, in_len);
563 if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
564 t->err = "CIPHERINIT_ERROR";
568 if (expected->aead) {
569 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
570 expected->iv_len, 0)) {
571 t->err = "INVALID_IV_LENGTH";
574 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
575 t->err = "INVALID_IV_LENGTH";
579 if (expected->aead) {
582 * If encrypting or OCB just set tag length initially, otherwise
583 * set tag length and value.
585 if (enc || expected->aead == EVP_CIPH_OCB_MODE) {
586 t->err = "TAG_LENGTH_SET_ERROR";
589 t->err = "TAG_SET_ERROR";
592 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
593 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
594 expected->tag_len, tag))
599 if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
600 t->err = "INVALID_KEY_LENGTH";
603 if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
604 t->err = "KEY_SET_ERROR";
608 if (!enc && expected->aead == EVP_CIPH_OCB_MODE) {
609 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
610 expected->tag_len, expected->tag)) {
611 t->err = "TAG_SET_ERROR";
616 if (expected->aead == EVP_CIPH_CCM_MODE) {
617 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
618 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
623 t->err = "AAD_SET_ERROR";
625 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad,
630 * Supply the AAD in chunks less than the block size where possible
632 if (expected->aad_len > 0) {
633 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, 1))
637 if (expected->aad_len > 2) {
638 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
639 expected->aad + donelen,
640 expected->aad_len - 2))
642 donelen += expected->aad_len - 2;
644 if (expected->aad_len > 1
645 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
646 expected->aad + donelen, 1))
650 EVP_CIPHER_CTX_set_padding(ctx, 0);
651 t->err = "CIPHERUPDATE_ERROR";
654 /* We supply the data all in one go */
655 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
658 /* Supply the data in chunks less than the block size where possible */
660 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
667 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
675 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
681 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
682 t->err = "CIPHERFINAL_ERROR";
685 if (!TEST_mem_eq(expected_out, out_len,
686 tmp + out_misalign, tmplen + tmpflen)) {
687 t->err = "VALUE_MISMATCH";
690 if (enc && expected->aead) {
691 unsigned char rtag[16];
693 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
694 t->err = "TAG_LENGTH_INTERNAL_ERROR";
697 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
698 expected->tag_len, rtag)) {
699 t->err = "TAG_RETRIEVE_ERROR";
702 if (!TEST_mem_eq(expected->tag, expected->tag_len,
703 rtag, expected->tag_len)) {
704 t->err = "TAG_VALUE_MISMATCH";
712 EVP_CIPHER_CTX_free(ctx);
716 static int cipher_test_run(EVP_TEST *t)
718 CIPHER_DATA *cdat = t->data;
720 size_t out_misalign, inp_misalign;
726 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
727 /* IV is optional and usually omitted in wrap mode */
728 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
733 if (cdat->aead && !cdat->tag) {
737 for (out_misalign = 0; out_misalign <= 1;) {
738 static char aux_err[64];
739 t->aux_err = aux_err;
740 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
741 if (inp_misalign == (size_t)-1) {
742 /* kludge: inp_misalign == -1 means "exercise in-place" */
743 BIO_snprintf(aux_err, sizeof(aux_err),
744 "%s in-place, %sfragmented",
745 out_misalign ? "misaligned" : "aligned",
748 BIO_snprintf(aux_err, sizeof(aux_err),
749 "%s output and %s input, %sfragmented",
750 out_misalign ? "misaligned" : "aligned",
751 inp_misalign ? "misaligned" : "aligned",
755 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
756 /* Not fatal errors: return */
763 if (cdat->enc != 1) {
764 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
765 /* Not fatal errors: return */
774 if (out_misalign == 1 && frag == 0) {
776 * XTS, CCM and Wrap modes have special requirements about input
777 * lengths so we don't fragment for those
779 if (cdat->aead == EVP_CIPH_CCM_MODE
780 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
781 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
794 static const EVP_TEST_METHOD cipher_test_method = {
807 typedef struct mac_data_st {
810 /* Algorithm string for this MAC */
816 unsigned char *input;
818 /* Expected output */
819 unsigned char *output;
823 static int mac_test_init(EVP_TEST *t, const char *alg)
828 if (strcmp(alg, "HMAC") == 0) {
829 type = EVP_PKEY_HMAC;
830 } else if (strcmp(alg, "CMAC") == 0) {
831 #ifndef OPENSSL_NO_CMAC
832 type = EVP_PKEY_CMAC;
837 } else if (strcmp(alg, "Poly1305") == 0) {
838 #ifndef OPENSSL_NO_POLY1305
839 type = EVP_PKEY_POLY1305;
844 } else if (strcmp(alg, "SipHash") == 0) {
845 #ifndef OPENSSL_NO_SIPHASH
846 type = EVP_PKEY_SIPHASH;
854 mdat = OPENSSL_zalloc(sizeof(*mdat));
860 static void mac_test_cleanup(EVP_TEST *t)
862 MAC_DATA *mdat = t->data;
864 OPENSSL_free(mdat->alg);
865 OPENSSL_free(mdat->key);
866 OPENSSL_free(mdat->input);
867 OPENSSL_free(mdat->output);
870 static int mac_test_parse(EVP_TEST *t,
871 const char *keyword, const char *value)
873 MAC_DATA *mdata = t->data;
875 if (strcmp(keyword, "Key") == 0)
876 return parse_bin(value, &mdata->key, &mdata->key_len);
877 if (strcmp(keyword, "Algorithm") == 0) {
878 mdata->alg = OPENSSL_strdup(value);
883 if (strcmp(keyword, "Input") == 0)
884 return parse_bin(value, &mdata->input, &mdata->input_len);
885 if (strcmp(keyword, "Output") == 0)
886 return parse_bin(value, &mdata->output, &mdata->output_len);
890 static int mac_test_run(EVP_TEST *t)
892 MAC_DATA *expected = t->data;
893 EVP_MD_CTX *mctx = NULL;
894 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
895 EVP_PKEY *key = NULL;
896 const EVP_MD *md = NULL;
897 unsigned char *got = NULL;
900 #ifdef OPENSSL_NO_DES
901 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
908 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(expected->type, NULL))) {
909 t->err = "MAC_PKEY_CTX_ERROR";
913 if (EVP_PKEY_keygen_init(genctx) <= 0) {
914 t->err = "MAC_KEYGEN_INIT_ERROR";
917 if (expected->type == EVP_PKEY_CMAC
918 && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", expected->alg) <= 0) {
919 t->err = "MAC_ALGORITHM_SET_ERROR";
923 if (EVP_PKEY_CTX_set_mac_key(genctx, expected->key,
924 expected->key_len) <= 0) {
925 t->err = "MAC_KEY_SET_ERROR";
929 if (EVP_PKEY_keygen(genctx, &key) <= 0) {
930 t->err = "MAC_KEY_GENERATE_ERROR";
933 if (expected->type == EVP_PKEY_HMAC) {
934 if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
935 t->err = "MAC_ALGORITHM_SET_ERROR";
939 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
940 t->err = "INTERNAL_ERROR";
943 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
944 t->err = "DIGESTSIGNINIT_ERROR";
948 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
949 t->err = "DIGESTSIGNUPDATE_ERROR";
952 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
953 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
956 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
957 t->err = "TEST_FAILURE";
960 if (!EVP_DigestSignFinal(mctx, got, &got_len)
961 || !TEST_mem_eq(expected->output, expected->output_len,
963 t->err = "TEST_MAC_ERR";
968 EVP_MD_CTX_free(mctx);
970 EVP_PKEY_CTX_free(genctx);
975 static const EVP_TEST_METHOD mac_test_method = {
986 *** These are all very similar and share much common code.
989 typedef struct pkey_data_st {
990 /* Context for this operation */
992 /* Key operation to perform */
993 int (*keyop) (EVP_PKEY_CTX *ctx,
994 unsigned char *sig, size_t *siglen,
995 const unsigned char *tbs, size_t tbslen);
997 unsigned char *input;
999 /* Expected output */
1000 unsigned char *output;
1005 * Perform public key operation setup: lookup key, allocated ctx and call
1006 * the appropriate initialisation function
1008 static int pkey_test_init(EVP_TEST *t, const char *name,
1010 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1011 int (*keyop)(EVP_PKEY_CTX *ctx,
1012 unsigned char *sig, size_t *siglen,
1013 const unsigned char *tbs,
1017 EVP_PKEY *pkey = NULL;
1021 rv = find_key(&pkey, name, public_keys);
1023 rv = find_key(&pkey, name, private_keys);
1024 if (rv == 0 || pkey == NULL) {
1029 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1030 EVP_PKEY_free(pkey);
1033 kdata->keyop = keyop;
1034 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1036 if (keyopinit(kdata->ctx) <= 0)
1037 t->err = "KEYOP_INIT_ERROR";
1042 static void pkey_test_cleanup(EVP_TEST *t)
1044 PKEY_DATA *kdata = t->data;
1046 OPENSSL_free(kdata->input);
1047 OPENSSL_free(kdata->output);
1048 EVP_PKEY_CTX_free(kdata->ctx);
1051 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1057 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1059 p = strchr(tmpval, ':');
1062 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1064 t->err = "PKEY_CTRL_INVALID";
1066 } else if (p != NULL && rv <= 0) {
1067 /* If p has an OID and lookup fails assume disabled algorithm */
1068 int nid = OBJ_sn2nid(p);
1070 if (nid == NID_undef)
1071 nid = OBJ_ln2nid(p);
1072 if (nid != NID_undef
1073 && EVP_get_digestbynid(nid) == NULL
1074 && EVP_get_cipherbynid(nid) == NULL) {
1078 t->err = "PKEY_CTRL_ERROR";
1082 OPENSSL_free(tmpval);
1086 static int pkey_test_parse(EVP_TEST *t,
1087 const char *keyword, const char *value)
1089 PKEY_DATA *kdata = t->data;
1090 if (strcmp(keyword, "Input") == 0)
1091 return parse_bin(value, &kdata->input, &kdata->input_len);
1092 if (strcmp(keyword, "Output") == 0)
1093 return parse_bin(value, &kdata->output, &kdata->output_len);
1094 if (strcmp(keyword, "Ctrl") == 0)
1095 return pkey_test_ctrl(t, kdata->ctx, value);
1099 static int pkey_test_run(EVP_TEST *t)
1101 PKEY_DATA *expected = t->data;
1102 unsigned char *got = NULL;
1105 if (expected->keyop(expected->ctx, NULL, &got_len,
1106 expected->input, expected->input_len) <= 0
1107 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1108 t->err = "KEYOP_LENGTH_ERROR";
1111 if (expected->keyop(expected->ctx, got, &got_len,
1112 expected->input, expected->input_len) <= 0) {
1113 t->err = "KEYOP_ERROR";
1116 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
1117 t->err = "KEYOP_MISMATCH";
1126 static int sign_test_init(EVP_TEST *t, const char *name)
1128 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1131 static const EVP_TEST_METHOD psign_test_method = {
1139 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1141 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1142 EVP_PKEY_verify_recover);
1145 static const EVP_TEST_METHOD pverify_recover_test_method = {
1147 verify_recover_test_init,
1153 static int decrypt_test_init(EVP_TEST *t, const char *name)
1155 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1159 static const EVP_TEST_METHOD pdecrypt_test_method = {
1167 static int verify_test_init(EVP_TEST *t, const char *name)
1169 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1172 static int verify_test_run(EVP_TEST *t)
1174 PKEY_DATA *kdata = t->data;
1176 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1177 kdata->input, kdata->input_len) <= 0)
1178 t->err = "VERIFY_ERROR";
1182 static const EVP_TEST_METHOD pverify_test_method = {
1191 static int pderive_test_init(EVP_TEST *t, const char *name)
1193 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1196 static int pderive_test_parse(EVP_TEST *t,
1197 const char *keyword, const char *value)
1199 PKEY_DATA *kdata = t->data;
1201 if (strcmp(keyword, "PeerKey") == 0) {
1203 if (find_key(&peer, value, public_keys) == 0)
1205 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1209 if (strcmp(keyword, "SharedSecret") == 0)
1210 return parse_bin(value, &kdata->output, &kdata->output_len);
1211 if (strcmp(keyword, "Ctrl") == 0)
1212 return pkey_test_ctrl(t, kdata->ctx, value);
1216 static int pderive_test_run(EVP_TEST *t)
1218 PKEY_DATA *expected = t->data;
1219 unsigned char *got = NULL;
1222 got_len = expected->output_len;
1223 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1224 t->err = "DERIVE_ERROR";
1227 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1228 t->err = "DERIVE_ERROR";
1231 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
1232 t->err = "SHARED_SECRET_MISMATCH";
1242 static const EVP_TEST_METHOD pderive_test_method = {
1255 typedef enum pbe_type_enum {
1256 PBE_TYPE_INVALID = 0,
1257 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1260 typedef struct pbe_data_st {
1262 /* scrypt parameters */
1263 uint64_t N, r, p, maxmem;
1264 /* PKCS#12 parameters */
1268 unsigned char *pass;
1271 unsigned char *salt;
1273 /* Expected output */
1278 #ifndef OPENSSL_NO_SCRYPT
1280 * Parse unsigned decimal 64 bit integer value
1282 static int parse_uint64(const char *value, uint64_t *pr)
1284 const char *p = value;
1286 if (!TEST_true(*p)) {
1287 TEST_info("Invalid empty integer value");
1290 for (*pr = 0; *p; ) {
1291 if (*pr > UINT64_MAX / 10) {
1292 TEST_error("Integer overflow in string %s", value);
1296 if (!TEST_true(isdigit(*p))) {
1297 TEST_error("Invalid character in string %s", value);
1306 static int scrypt_test_parse(EVP_TEST *t,
1307 const char *keyword, const char *value)
1309 PBE_DATA *pdata = t->data;
1311 if (strcmp(keyword, "N") == 0)
1312 return parse_uint64(value, &pdata->N);
1313 if (strcmp(keyword, "p") == 0)
1314 return parse_uint64(value, &pdata->p);
1315 if (strcmp(keyword, "r") == 0)
1316 return parse_uint64(value, &pdata->r);
1317 if (strcmp(keyword, "maxmem") == 0)
1318 return parse_uint64(value, &pdata->maxmem);
1323 static int pbkdf2_test_parse(EVP_TEST *t,
1324 const char *keyword, const char *value)
1326 PBE_DATA *pdata = t->data;
1328 if (strcmp(keyword, "iter") == 0) {
1329 pdata->iter = atoi(value);
1330 if (pdata->iter <= 0)
1334 if (strcmp(keyword, "MD") == 0) {
1335 pdata->md = EVP_get_digestbyname(value);
1336 if (pdata->md == NULL)
1343 static int pkcs12_test_parse(EVP_TEST *t,
1344 const char *keyword, const char *value)
1346 PBE_DATA *pdata = t->data;
1348 if (strcmp(keyword, "id") == 0) {
1349 pdata->id = atoi(value);
1354 return pbkdf2_test_parse(t, keyword, value);
1357 static int pbe_test_init(EVP_TEST *t, const char *alg)
1360 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1362 if (strcmp(alg, "scrypt") == 0) {
1363 #ifndef OPENSSL_NO_SCRYPT
1364 pbe_type = PBE_TYPE_SCRYPT;
1369 } else if (strcmp(alg, "pbkdf2") == 0) {
1370 pbe_type = PBE_TYPE_PBKDF2;
1371 } else if (strcmp(alg, "pkcs12") == 0) {
1372 pbe_type = PBE_TYPE_PKCS12;
1374 TEST_error("Unknown pbe algorithm %s", alg);
1376 pdat = OPENSSL_zalloc(sizeof(*pdat));
1377 pdat->pbe_type = pbe_type;
1382 static void pbe_test_cleanup(EVP_TEST *t)
1384 PBE_DATA *pdat = t->data;
1386 OPENSSL_free(pdat->pass);
1387 OPENSSL_free(pdat->salt);
1388 OPENSSL_free(pdat->key);
1391 static int pbe_test_parse(EVP_TEST *t,
1392 const char *keyword, const char *value)
1394 PBE_DATA *pdata = t->data;
1396 if (strcmp(keyword, "Password") == 0)
1397 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1398 if (strcmp(keyword, "Salt") == 0)
1399 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1400 if (strcmp(keyword, "Key") == 0)
1401 return parse_bin(value, &pdata->key, &pdata->key_len);
1402 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1403 return pbkdf2_test_parse(t, keyword, value);
1404 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1405 return pkcs12_test_parse(t, keyword, value);
1406 #ifndef OPENSSL_NO_SCRYPT
1407 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1408 return scrypt_test_parse(t, keyword, value);
1413 static int pbe_test_run(EVP_TEST *t)
1415 PBE_DATA *expected = t->data;
1418 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1419 t->err = "INTERNAL_ERROR";
1422 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1423 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1424 expected->salt, expected->salt_len,
1425 expected->iter, expected->md,
1426 expected->key_len, key) == 0) {
1427 t->err = "PBKDF2_ERROR";
1430 #ifndef OPENSSL_NO_SCRYPT
1431 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1432 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1433 expected->salt, expected->salt_len, expected->N,
1434 expected->r, expected->p, expected->maxmem,
1435 key, expected->key_len) == 0) {
1436 t->err = "SCRYPT_ERROR";
1440 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1441 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1442 expected->salt, expected->salt_len,
1443 expected->id, expected->iter, expected->key_len,
1444 key, expected->md) == 0) {
1445 t->err = "PKCS12_ERROR";
1449 if (!TEST_mem_eq(expected->key, expected->key_len,
1450 key, expected->key_len)) {
1451 t->err = "KEY_MISMATCH";
1460 static const EVP_TEST_METHOD pbe_test_method = {
1474 BASE64_CANONICAL_ENCODING = 0,
1475 BASE64_VALID_ENCODING = 1,
1476 BASE64_INVALID_ENCODING = 2
1477 } base64_encoding_type;
1479 typedef struct encode_data_st {
1480 /* Input to encoding */
1481 unsigned char *input;
1483 /* Expected output */
1484 unsigned char *output;
1486 base64_encoding_type encoding;
1489 static int encode_test_init(EVP_TEST *t, const char *encoding)
1493 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1495 if (strcmp(encoding, "canonical") == 0) {
1496 edata->encoding = BASE64_CANONICAL_ENCODING;
1497 } else if (strcmp(encoding, "valid") == 0) {
1498 edata->encoding = BASE64_VALID_ENCODING;
1499 } else if (strcmp(encoding, "invalid") == 0) {
1500 edata->encoding = BASE64_INVALID_ENCODING;
1501 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1504 TEST_error("Bad encoding: %s."
1505 " Should be one of {canonical, valid, invalid}",
1513 static void encode_test_cleanup(EVP_TEST *t)
1515 ENCODE_DATA *edata = t->data;
1517 OPENSSL_free(edata->input);
1518 OPENSSL_free(edata->output);
1519 memset(edata, 0, sizeof(*edata));
1522 static int encode_test_parse(EVP_TEST *t,
1523 const char *keyword, const char *value)
1525 ENCODE_DATA *edata = t->data;
1527 if (strcmp(keyword, "Input") == 0)
1528 return parse_bin(value, &edata->input, &edata->input_len);
1529 if (strcmp(keyword, "Output") == 0)
1530 return parse_bin(value, &edata->output, &edata->output_len);
1534 static int encode_test_run(EVP_TEST *t)
1536 ENCODE_DATA *expected = t->data;
1537 unsigned char *encode_out = NULL, *decode_out = NULL;
1538 int output_len, chunk_len;
1539 EVP_ENCODE_CTX *decode_ctx;
1541 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1542 t->err = "INTERNAL_ERROR";
1546 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1547 EVP_ENCODE_CTX *encode_ctx;
1549 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1550 || !TEST_ptr(encode_out =
1551 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1554 EVP_EncodeInit(encode_ctx);
1555 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1556 expected->input, expected->input_len);
1557 output_len = chunk_len;
1559 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1560 output_len += chunk_len;
1562 EVP_ENCODE_CTX_free(encode_ctx);
1564 if (!TEST_mem_eq(expected->output, expected->output_len,
1565 encode_out, output_len)) {
1566 t->err = "BAD_ENCODING";
1571 if (!TEST_ptr(decode_out =
1572 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
1575 EVP_DecodeInit(decode_ctx);
1576 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
1577 expected->output_len) < 0) {
1578 t->err = "DECODE_ERROR";
1581 output_len = chunk_len;
1583 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1584 t->err = "DECODE_ERROR";
1587 output_len += chunk_len;
1589 if (expected->encoding != BASE64_INVALID_ENCODING
1590 && !TEST_mem_eq(expected->input, expected->input_len,
1591 decode_out, output_len)) {
1592 t->err = "BAD_DECODING";
1598 OPENSSL_free(encode_out);
1599 OPENSSL_free(decode_out);
1600 EVP_ENCODE_CTX_free(decode_ctx);
1604 static const EVP_TEST_METHOD encode_test_method = {
1607 encode_test_cleanup,
1616 typedef struct kdf_data_st {
1617 /* Context for this operation */
1619 /* Expected output */
1620 unsigned char *output;
1625 * Perform public key operation setup: lookup key, allocated ctx and call
1626 * the appropriate initialisation function
1628 static int kdf_test_init(EVP_TEST *t, const char *name)
1632 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
1634 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1635 if (kdata->ctx == NULL)
1637 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1643 static void kdf_test_cleanup(EVP_TEST *t)
1645 KDF_DATA *kdata = t->data;
1646 OPENSSL_free(kdata->output);
1647 EVP_PKEY_CTX_free(kdata->ctx);
1650 static int kdf_test_parse(EVP_TEST *t,
1651 const char *keyword, const char *value)
1653 KDF_DATA *kdata = t->data;
1655 if (strcmp(keyword, "Output") == 0)
1656 return parse_bin(value, &kdata->output, &kdata->output_len);
1657 if (strncmp(keyword, "Ctrl", 4) == 0)
1658 return pkey_test_ctrl(t, kdata->ctx, value);
1662 static int kdf_test_run(EVP_TEST *t)
1664 KDF_DATA *expected = t->data;
1665 unsigned char *got = NULL;
1666 size_t got_len = expected->output_len;
1668 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1669 t->err = "INTERNAL_ERROR";
1672 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1673 t->err = "KDF_DERIVE_ERROR";
1676 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
1677 t->err = "KDF_MISMATCH";
1687 static const EVP_TEST_METHOD kdf_test_method = {
1700 typedef struct keypair_test_data_st {
1703 } KEYPAIR_TEST_DATA;
1705 static int keypair_test_init(EVP_TEST *t, const char *pair)
1707 KEYPAIR_TEST_DATA *data;
1709 EVP_PKEY *pk = NULL, *pubk = NULL;
1710 char *pub, *priv = NULL;
1712 /* Split private and public names. */
1713 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1714 || !TEST_ptr(pub = strchr(priv, ':'))) {
1715 t->err = "PARSING_ERROR";
1720 if (!TEST_true(find_key(&pk, priv, private_keys))) {
1721 TEST_info("Can't find private key: %s", priv);
1722 t->err = "MISSING_PRIVATE_KEY";
1725 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
1726 TEST_info("Can't find public key: %s", pub);
1727 t->err = "MISSING_PUBLIC_KEY";
1731 if (pk == NULL && pubk == NULL) {
1732 /* Both keys are listed but unsupported: skip this test */
1738 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
1751 static void keypair_test_cleanup(EVP_TEST *t)
1753 OPENSSL_free(t->data);
1758 * For tests that do not accept any custom keywords.
1760 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
1765 static int keypair_test_run(EVP_TEST *t)
1768 const KEYPAIR_TEST_DATA *pair = t->data;
1770 if (pair->privk == NULL || pair->pubk == NULL) {
1772 * this can only happen if only one of the keys is not set
1773 * which means that one of them was unsupported while the
1774 * other isn't: hence a key type mismatch.
1776 t->err = "KEYPAIR_TYPE_MISMATCH";
1781 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
1783 t->err = "KEYPAIR_MISMATCH";
1784 } else if ( -1 == rv ) {
1785 t->err = "KEYPAIR_TYPE_MISMATCH";
1786 } else if ( -2 == rv ) {
1787 t->err = "UNSUPPORTED_KEY_COMPARISON";
1789 TEST_error("Unexpected error in key comparison");
1804 static const EVP_TEST_METHOD keypair_test_method = {
1807 keypair_test_cleanup,
1814 *** DIGEST SIGN+VERIFY TESTS
1818 int is_verify; /* Set to 1 if verifying */
1819 int is_oneshot; /* Set to 1 for one shot operation */
1820 const EVP_MD *md; /* Digest to use */
1821 EVP_MD_CTX *ctx; /* Digest context */
1823 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
1824 unsigned char *osin; /* Input data if one shot */
1825 size_t osin_len; /* Input length data if one shot */
1826 unsigned char *output; /* Expected output */
1827 size_t output_len; /* Expected output length */
1830 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
1833 const EVP_MD *md = NULL;
1834 DIGESTSIGN_DATA *mdat;
1836 if (strcmp(alg, "NULL") != 0) {
1837 if ((md = EVP_get_digestbyname(alg)) == NULL) {
1838 /* If alg has an OID assume disabled algorithm */
1839 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
1846 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
1849 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
1853 mdat->is_verify = is_verify;
1854 mdat->is_oneshot = is_oneshot;
1859 static int digestsign_test_init(EVP_TEST *t, const char *alg)
1861 return digestsigver_test_init(t, alg, 0, 0);
1864 static void digestsigver_test_cleanup(EVP_TEST *t)
1866 DIGESTSIGN_DATA *mdata = t->data;
1868 EVP_MD_CTX_free(mdata->ctx);
1869 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
1870 OPENSSL_free(mdata->osin);
1871 OPENSSL_free(mdata->output);
1872 OPENSSL_free(mdata);
1876 static int digestsigver_test_parse(EVP_TEST *t,
1877 const char *keyword, const char *value)
1879 DIGESTSIGN_DATA *mdata = t->data;
1881 if (strcmp(keyword, "Key") == 0) {
1882 EVP_PKEY *pkey = NULL;
1885 if (mdata->is_verify)
1886 rv = find_key(&pkey, value, public_keys);
1888 rv = find_key(&pkey, value, private_keys);
1889 if (rv == 0 || pkey == NULL) {
1893 if (mdata->is_verify) {
1894 if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
1896 t->err = "DIGESTVERIFYINIT_ERROR";
1899 if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
1901 t->err = "DIGESTSIGNINIT_ERROR";
1905 if (strcmp(keyword, "Input") == 0) {
1906 if (mdata->is_oneshot)
1907 return parse_bin(value, &mdata->osin, &mdata->osin_len);
1908 return evp_test_buffer_append(value, &mdata->input);
1910 if (strcmp(keyword, "Output") == 0)
1911 return parse_bin(value, &mdata->output, &mdata->output_len);
1913 if (!mdata->is_oneshot) {
1914 if (strcmp(keyword, "Count") == 0)
1915 return evp_test_buffer_set_count(value, mdata->input);
1916 if (strcmp(keyword, "Ncopy") == 0)
1917 return evp_test_buffer_ncopy(value, mdata->input);
1919 if (strcmp(keyword, "Ctrl") == 0) {
1920 if (mdata->pctx == NULL)
1922 return pkey_test_ctrl(t, mdata->pctx, value);
1927 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
1930 return EVP_DigestSignUpdate(ctx, buf, buflen);
1933 static int digestsign_test_run(EVP_TEST *t)
1935 DIGESTSIGN_DATA *expected = t->data;
1936 unsigned char *got = NULL;
1939 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
1941 t->err = "DIGESTUPDATE_ERROR";
1945 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
1946 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1949 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1950 t->err = "MALLOC_FAILURE";
1953 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
1954 t->err = "DIGESTSIGNFINAL_ERROR";
1957 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
1958 t->err = "SIGNATURE_MISMATCH";
1967 static const EVP_TEST_METHOD digestsign_test_method = {
1969 digestsign_test_init,
1970 digestsigver_test_cleanup,
1971 digestsigver_test_parse,
1975 static int digestverify_test_init(EVP_TEST *t, const char *alg)
1977 return digestsigver_test_init(t, alg, 1, 0);
1980 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
1983 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
1986 static int digestverify_test_run(EVP_TEST *t)
1988 DIGESTSIGN_DATA *mdata = t->data;
1990 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
1991 t->err = "DIGESTUPDATE_ERROR";
1995 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
1996 mdata->output_len) <= 0)
1997 t->err = "VERIFY_ERROR";
2001 static const EVP_TEST_METHOD digestverify_test_method = {
2003 digestverify_test_init,
2004 digestsigver_test_cleanup,
2005 digestsigver_test_parse,
2006 digestverify_test_run
2009 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
2011 return digestsigver_test_init(t, alg, 0, 1);
2014 static int oneshot_digestsign_test_run(EVP_TEST *t)
2016 DIGESTSIGN_DATA *expected = t->data;
2017 unsigned char *got = NULL;
2020 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
2021 expected->osin, expected->osin_len)) {
2022 t->err = "DIGESTSIGN_LENGTH_ERROR";
2025 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2026 t->err = "MALLOC_FAILURE";
2029 if (!EVP_DigestSign(expected->ctx, got, &got_len,
2030 expected->osin, expected->osin_len)) {
2031 t->err = "DIGESTSIGN_ERROR";
2034 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2035 t->err = "SIGNATURE_MISMATCH";
2044 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
2045 "OneShotDigestSign",
2046 oneshot_digestsign_test_init,
2047 digestsigver_test_cleanup,
2048 digestsigver_test_parse,
2049 oneshot_digestsign_test_run
2052 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
2054 return digestsigver_test_init(t, alg, 1, 1);
2057 static int oneshot_digestverify_test_run(EVP_TEST *t)
2059 DIGESTSIGN_DATA *mdata = t->data;
2061 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
2062 mdata->osin, mdata->osin_len) <= 0)
2063 t->err = "VERIFY_ERROR";
2067 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
2068 "OneShotDigestVerify",
2069 oneshot_digestverify_test_init,
2070 digestsigver_test_cleanup,
2071 digestsigver_test_parse,
2072 oneshot_digestverify_test_run
2077 *** PARSING AND DISPATCH
2080 static const EVP_TEST_METHOD *evp_test_list[] = {
2081 &cipher_test_method,
2082 &digest_test_method,
2083 &digestsign_test_method,
2084 &digestverify_test_method,
2085 &encode_test_method,
2087 &keypair_test_method,
2089 &oneshot_digestsign_test_method,
2090 &oneshot_digestverify_test_method,
2092 &pdecrypt_test_method,
2093 &pderive_test_method,
2095 &pverify_recover_test_method,
2096 &pverify_test_method,
2100 static const EVP_TEST_METHOD *find_test(const char *name)
2102 const EVP_TEST_METHOD **tt;
2104 for (tt = evp_test_list; *tt; tt++) {
2105 if (strcmp(name, (*tt)->name) == 0)
2111 static void clear_test(EVP_TEST *t)
2114 if (t->data != NULL) {
2115 if (t->meth != NULL)
2116 t->meth->cleanup(t);
2117 OPENSSL_free(t->data);
2120 OPENSSL_free(t->expected_err);
2121 t->expected_err = NULL;
2122 OPENSSL_free(t->func);
2124 OPENSSL_free(t->reason);
2133 * Check for errors in the test structure; return 1 if okay, else 0.
2135 static int check_test_error(EVP_TEST *t)
2141 if (t->err == NULL && t->expected_err == NULL)
2143 if (t->err != NULL && t->expected_err == NULL) {
2144 if (t->aux_err != NULL) {
2145 TEST_info("Above error from the test at %s:%d "
2146 "(%s) unexpected error %s",
2147 current_test_file, t->start_line, t->aux_err, t->err);
2149 TEST_info("Above error from the test at %s:%d "
2150 "unexpected error %s",
2151 current_test_file, t->start_line, t->err);
2155 if (t->err == NULL && t->expected_err != NULL) {
2156 TEST_info("Test line %d: succeeded but was expecting %s",
2157 t->start_line, t->expected_err);
2161 if (strcmp(t->err, t->expected_err) != 0) {
2162 TEST_info("Test line %d: expecting %s got %s",
2163 t->start_line, t->expected_err, t->err);
2167 if (t->func == NULL && t->reason == NULL)
2170 if (t->func == NULL || t->reason == NULL) {
2171 TEST_info("Test line %d: missing function or reason code",
2176 err = ERR_peek_error();
2178 TEST_info("Test line %d, expected error \"%s:%s\" not set",
2179 t->start_line, t->func, t->reason);
2183 func = ERR_func_error_string(err);
2184 reason = ERR_reason_error_string(err);
2185 if (func == NULL && reason == NULL) {
2186 TEST_info("Test line %d: expected error \"%s:%s\","
2187 " no strings available. Skipping...\n",
2188 t->start_line, t->func, t->reason);
2192 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
2195 TEST_info("Test line %d: expected error \"%s:%s\", got \"%s:%s\"",
2196 t->start_line, t->func, t->reason, func, reason);
2202 * Run a parsed test. Log a message and return 0 on error.
2204 static int run_test(EVP_TEST *t)
2206 if (t->meth == NULL)
2213 if (t->err == NULL && t->meth->run_test(t) != 1) {
2214 TEST_info("Line %d error %s", t->start_line, t->meth->name);
2217 if (!check_test_error(t)) {
2218 test_openssl_errors();
2227 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
2229 for (; lst != NULL; lst = lst->next) {
2230 if (strcmp(lst->name, name) == 0) {
2239 static void free_key_list(KEY_LIST *lst)
2241 while (lst != NULL) {
2242 KEY_LIST *next = lst->next;
2244 EVP_PKEY_free(lst->key);
2245 OPENSSL_free(lst->name);
2253 * Read a line, remove the newline, return EOF or first char.
2254 * Comment lines are treated like empty lines.
2256 static int read_line(EVP_TEST *t)
2260 if (!BIO_gets(t->in, t->buf, sizeof(t->buf)))
2263 if ((p = strchr(t->buf, '\n')) != NULL)
2265 if (t->buf[0] == '#')
2271 * Skip leading spaces and remove trailing spaces from string.
2273 static char *strip_spaces(char *pval)
2277 for (start = pval; isspace(*start); )
2282 for (p = start + strlen(start); --p >= start && isspace(*p); )
2288 * Split line into 'key = value'; return 1 if okay, 0 on error.
2290 static int split_line(EVP_TEST *t, char **keyword, char **value)
2294 /* Look for = sign */
2295 if ((p = strchr(t->buf, '=')) == NULL) {
2296 TEST_error("Line %d: Missing '=' in test file", t->line);
2300 *keyword = strip_spaces(t->buf);
2301 *value = strip_spaces(p);
2302 if (**keyword == '\0') {
2303 TEST_error("Line %d: Missing key; malformed input line", t->line);
2310 * Read a PEM block. Return 1 if okay, 0 on error.
2312 static int read_key(EVP_TEST *t)
2316 if (t->key == NULL) {
2317 if (!TEST_ptr(t->key = BIO_new(BIO_s_mem())))
2319 } else if (!TEST_int_gt(BIO_reset(t->key), 0)) {
2323 /* Read to PEM end line and place content in memory BIO */
2324 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
2326 if (!TEST_int_gt(BIO_puts(t->key, tmpbuf), 0))
2328 if (strncmp(tmpbuf, "-----END", 8) == 0)
2331 TEST_error("Can't find key end");
2336 * Is the key type an unsupported algorithm?
2338 static int key_unsupported()
2340 long err = ERR_peek_error();
2342 if (ERR_GET_LIB(err) == ERR_LIB_EVP
2343 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
2347 #ifndef OPENSSL_NO_EC
2349 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2350 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2353 if (ERR_GET_LIB(err) == ERR_LIB_EC
2354 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
2358 #endif /* OPENSSL_NO_EC */
2363 * Read, parse, and execute one test. Return EOF; 0 if failure, 1 if okay.
2365 static int read_stanza(EVP_TEST *t)
2368 char *keyword, *value;
2369 KEY_LIST **klist, *key;
2374 /* Find the first line of a stanza. */
2383 if (!split_line(t, &keyword, &value))
2386 /* Handle a few special cases here. */
2387 if (strcmp(keyword, "Title") == 0) {
2388 TEST_info("Starting \"%s\" tests", value);
2394 if (strcmp(keyword, "PrivateKey") == 0) {
2397 pkey = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
2398 if (pkey == NULL && !key_unsupported()) {
2399 TEST_info("Can't read private key %s", value);
2400 ERR_print_errors_fp(stderr);
2403 klist = &private_keys;
2405 else if (strcmp(keyword, "PublicKey") == 0) {
2408 pkey = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
2409 if (pkey == NULL && !key_unsupported()) {
2410 TEST_info("Can't read public key %s", value);
2411 ERR_print_errors_fp(stderr);
2414 klist = &public_keys;
2417 /* If we have a key add to list */
2418 if (klist != NULL) {
2419 if (find_key(NULL, value, *klist)) {
2420 TEST_info("Duplicate key %s", value);
2423 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))
2424 || !TEST_ptr(key->name = OPENSSL_strdup(value)))
2430 /* Go back and start a new stanza. */
2434 /* Start of a new text. Look it up. */
2435 if (!TEST_ptr(t->meth = find_test(keyword)))
2437 t->start_line = t->line;
2438 if (!t->meth->init(t, value)) {
2439 TEST_error("unknown %s: %s\n", keyword, value);
2443 /* TEST_info("skipping %s %s", keyword, value); */
2447 /* Read rest of stanza. */
2454 if (!split_line(t, &keyword, &value))
2456 if (strcmp(keyword, "Result") == 0) {
2457 if (t->expected_err != NULL) {
2458 TEST_info("Line %d: multiple result lines", t->line);
2461 if (!TEST_ptr(t->expected_err = OPENSSL_strdup(value)))
2463 } else if (strcmp(keyword, "Function") == 0) {
2464 if (t->func != NULL) {
2465 TEST_info("Line %d: multiple function lines\n", t->line);
2468 if (!TEST_ptr(t->func = OPENSSL_strdup(value)))
2470 } else if (strcmp(keyword, "Reason") == 0) {
2471 if (t->reason != NULL) {
2472 TEST_info("Line %d: multiple reason lines", t->line);
2475 if (!TEST_ptr(t->reason = OPENSSL_strdup(value)))
2478 /* Must be test specific line: try to parse it */
2479 int rv = t->meth->parse(t, keyword, value);
2482 TEST_info("Line %d: unknown keyword %s", t->line, keyword);
2486 TEST_info("Line %d: error processing keyword %s\n",
2496 /* Read to end of stanza and return failure */
2507 static int do_test_file(const char *testfile)
2513 set_test_title(testfile);
2514 current_test_file = testfile;
2515 if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2517 memset(&t, 0, sizeof(t));
2520 TEST_info("Reading %s", testfile);
2522 c = read_stanza(&t);
2525 if (c == 0 || !run_test(&t)) {
2534 TEST_info("Completed %d tests with %d errors and %d skipped",
2535 t.ntests, t.errors, t.nskip);
2536 free_key_list(public_keys);
2537 free_key_list(private_keys);
2540 return t.errors == 0;
2543 static char * const *testfiles;
2545 static int run_file_tests(int i)
2547 return do_test_file(testfiles[i]);
2550 int test_main(int argc, char *argv[])
2553 TEST_error("Usage: %s file...", argv[0]);
2556 testfiles = &argv[1];
2558 ADD_ALL_TESTS(run_file_tests, argc - 1);
2560 return run_tests(argv[0]);