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"
23 static const char *current_test_file = "???";
26 * Remove spaces from beginning and end of a string
28 static void remove_space(char **pval)
30 unsigned char *p = (unsigned char *)*pval, *beginning;
35 *pval = (char *)(beginning = p);
37 p = p + strlen(*pval) - 1;
39 /* Remove trailing space */
40 while (p >= beginning && isspace(*p))
45 * Given a line of the form:
46 * name = value # comment
47 * extract name and value. NB: modifies |linebuf|.
49 static int parse_line(char **pkw, char **pval, char *linebuf)
51 char *p = linebuf + strlen(linebuf) - 1;
54 TEST_error("FATAL: missing EOL");
59 p = strchr(linebuf, '#');
64 if ((p = strchr(linebuf, '=')) == NULL)
76 * Unescape some escape sequences in string literals.
77 * Return the result in a newly allocated buffer.
78 * Currently only supports '\n'.
79 * If the input length is 0, returns a valid 1-byte buffer, but sets
82 static unsigned char* unescape(const char *input, size_t input_len,
85 unsigned char *ret, *p;
90 return OPENSSL_zalloc(1);
93 /* Escaping is non-expanding; over-allocate original size for simplicity. */
94 ret = p = OPENSSL_malloc(input_len);
98 for (i = 0; i < input_len; i++) {
99 if (input[i] == '\\') {
100 if (i == input_len - 1 || input[i+1] != 'n')
117 /* For a hex string "value" convert to a binary allocated buffer */
118 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
124 /* Check for empty value */
127 * Don't return NULL for zero length buffer.
128 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
129 * a non-NULL key buffer even if the key length is 0, in order to detect
132 *buf = OPENSSL_malloc(1);
140 /* Check for NULL literal */
141 if (strcmp(value, "NULL") == 0) {
147 /* Check for string literal */
148 if (value[0] == '"') {
151 vlen = strlen(value);
152 if (value[vlen - 1] != '"')
155 *buf = unescape(value, vlen, buflen);
161 /* Otherwise assume as hex literal and convert it to binary buffer */
162 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
163 TEST_info("Cannot convert %s", value);
164 ERR_print_errors(bio_err);
167 /* Size of input buffer means we'll never overflow */
171 #ifndef OPENSSL_NO_SCRYPT
172 /* Currently only used by scrypt tests */
173 /* Parse unsigned decimal 64 bit integer value */
174 static int test_uint64(const char *value, uint64_t *pr)
176 const char *p = value;
178 if (!TEST_true(*p)) {
179 TEST_info("Invalid empty integer value");
184 if (*pr > UINT64_MAX / 10) {
185 TEST_error("Integer overflow in string %s", value);
189 if (!TEST_true(isdigit(*p))) {
190 TEST_error("Invalid character in string %s", value);
199 static int compare_mem(unsigned char *expected, size_t expected_len,
200 unsigned char *got, size_t got_len)
202 if (!TEST_mem_eq(expected, expected_len, got, got_len))
208 typedef struct evp_test_method_st EVP_TEST_METHOD;
210 /* Structure holding test information */
211 typedef struct evp_test_st {
212 /* file being read */
214 /* temp memory BIO for reading in keys */
216 /* method for this test */
217 const EVP_TEST_METHOD *meth;
218 /* current line being processed */
220 /* start line of current test */
221 unsigned int start_line;
222 /* Error string for test */
223 const char *err, *aux_err;
224 /* Expected error value of test */
226 /* Expected error function string */
228 /* Expected error reason string */
230 /* Number of tests */
234 /* Number of tests skipped */
236 /* test specific data */
238 /* Current test should be skipped */
243 * Linked list of named keys.
245 typedef struct key_list_st {
248 struct key_list_st *next;
251 /* List of public and private keys */
252 static KEY_LIST *private_keys;
253 static KEY_LIST *public_keys;
256 * Test method structure
258 struct evp_test_method_st {
259 /* Name of test as it appears in file */
261 /* Initialise test for "alg" */
262 int (*init) (EVP_TEST * t, const char *alg);
263 /* Clean up method */
264 void (*cleanup) (EVP_TEST * t);
265 /* Test specific name value pair processing */
266 int (*parse) (EVP_TEST * t, const char *name, const char *value);
267 /* Run the test itself */
268 int (*run_test) (EVP_TEST * t);
271 static const EVP_TEST_METHOD digest_test_method, cipher_test_method;
272 static const EVP_TEST_METHOD mac_test_method;
273 static const EVP_TEST_METHOD psign_test_method, pverify_test_method;
274 static const EVP_TEST_METHOD pdecrypt_test_method;
275 static const EVP_TEST_METHOD pverify_recover_test_method;
276 static const EVP_TEST_METHOD pderive_test_method;
277 static const EVP_TEST_METHOD pbe_test_method;
278 static const EVP_TEST_METHOD encode_test_method;
279 static const EVP_TEST_METHOD kdf_test_method;
280 static const EVP_TEST_METHOD keypair_test_method;
282 static const EVP_TEST_METHOD *evp_test_list[] = {
287 &pverify_test_method,
288 &pdecrypt_test_method,
289 &pverify_recover_test_method,
290 &pderive_test_method,
294 &keypair_test_method,
298 static const EVP_TEST_METHOD *evp_find_test(const char *name)
300 const EVP_TEST_METHOD **tt;
302 for (tt = evp_test_list; *tt; tt++) {
303 if (strcmp(name, (*tt)->name) == 0)
309 static void clear_test(EVP_TEST *t)
311 OPENSSL_free(t->expected_err);
312 t->expected_err = NULL;
313 OPENSSL_free(t->func);
315 OPENSSL_free(t->reason);
322 * Check for errors in the test structure; return 1 if okay, else 0.
324 static int check_test_error(EVP_TEST *t)
330 if (t->err == NULL && t->expected_err == NULL)
332 if (t->err != NULL && t->expected_err == NULL) {
333 if (t->aux_err != NULL) {
334 TEST_info("Above error from the test at %s:%d "
335 "(%s) unexpected error %s",
336 current_test_file, t->start_line, t->aux_err, t->err);
338 TEST_info("Above error from the test at %s:%d "
339 "unexpected error %s",
340 current_test_file, t->start_line, t->err);
345 if (t->err == NULL && t->expected_err != NULL) {
346 TEST_info("Test line %d: succeeded expecting %s",
347 t->start_line, t->expected_err);
351 if (strcmp(t->err, t->expected_err) != 0) {
352 TEST_info("Test line %d: expecting %s got %s",
353 t->start_line, t->expected_err, t->err);
357 if (t->func == NULL && t->reason == NULL)
360 if (t->func == NULL || t->reason == NULL) {
361 TEST_info("Test line %d: missing function or reason code",
366 err = ERR_peek_error();
368 TEST_info("Test line %d, expected error \"%s:%s\" not set",
369 t->start_line, t->func, t->reason);
373 func = ERR_func_error_string(err);
374 reason = ERR_reason_error_string(err);
375 if (func == NULL && reason == NULL) {
376 TEST_info("Test line %d: expected error \"%s:%s\","
377 " no strings available. Skipping...\n",
378 t->start_line, t->func, t->reason);
382 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
385 TEST_info("Test line %d: expected error \"%s:%s\", got \"%s:%s\"",
386 t->start_line, t->func, t->reason, func, reason);
392 * Setup a new test, run any existing test. Log a message and return 0
395 static int run_and_get_next(EVP_TEST *t, const EVP_TEST_METHOD *tmeth)
397 /* If we already have a test set up run it */
401 /*TEST_info("Line %d skipped %s test", t->start_line, t->meth->name);
406 if (t->err == NULL && t->meth->run_test(t) != 1) {
407 TEST_info("Line %d error %s", t->start_line, t->meth->name);
410 if (!check_test_error(t)) {
411 test_openssl_errors();
417 if (t->data != NULL) {
419 OPENSSL_free(t->data);
428 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
430 for (; lst; lst = lst->next) {
431 if (strcmp(lst->name, name) == 0) {
440 static void free_key_list(KEY_LIST *lst)
442 while (lst != NULL) {
445 EVP_PKEY_free(lst->key);
446 OPENSSL_free(lst->name);
453 static int check_unsupported()
455 long err = ERR_peek_error();
457 if (ERR_GET_LIB(err) == ERR_LIB_EVP
458 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
462 #ifndef OPENSSL_NO_EC
464 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
465 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
468 if (ERR_GET_LIB(err) == ERR_LIB_EC
469 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
473 #endif /* OPENSSL_NO_EC */
478 static int read_key(EVP_TEST *t)
482 if (t->key == NULL) {
483 if (!TEST_ptr(t->key = BIO_new(BIO_s_mem())))
485 } else if (!TEST_int_gt(BIO_reset(t->key), 0)) {
489 /* Read to PEM end line and place content in memory BIO */
490 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
492 if (!TEST_int_gt(BIO_puts(t->key, tmpbuf), 0))
494 if (strncmp(tmpbuf, "-----END", 8) == 0)
497 TEST_error("Can't find key end");
502 * Parse a line into the current test |t|. Return 0 on error.
504 static int parse_test_line(EVP_TEST *t, char *buf)
506 char *keyword = NULL, *value = NULL;
508 KEY_LIST **lst = NULL, *key = NULL;
510 const EVP_TEST_METHOD *tmeth = NULL;
512 if (!parse_line(&keyword, &value, buf))
514 if (strcmp(keyword, "PrivateKey") == 0) {
517 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
518 if (pk == NULL && !check_unsupported()) {
519 TEST_info("Error reading private key %s", value);
520 ERR_print_errors_fp(stderr);
526 if (strcmp(keyword, "PublicKey") == 0) {
529 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
530 if (pk == NULL && !check_unsupported()) {
531 TEST_info("Error reading public key %s", value);
532 ERR_print_errors_fp(stderr);
538 /* If we have a key add to list */
540 if (find_key(NULL, value, *lst)) {
541 TEST_info("Duplicate key %s", value);
544 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))
545 || !TEST_ptr(key->name = OPENSSL_strdup(value)))
553 /* See if keyword corresponds to a test start */
554 if ((tmeth = evp_find_test(keyword)) != NULL) {
555 if (!run_and_get_next(t, tmeth))
557 t->start_line = t->line;
559 if (!tmeth->init(t, value)) {
560 TEST_info("Unknown %s: %s", keyword, value);
567 if (strcmp(keyword, "Title") == 0) {
568 TEST_info("Starting %s tests", value);
569 set_test_title(value);
570 } else if (strcmp(keyword, "Result") == 0) {
571 if (t->expected_err != NULL) {
572 TEST_info("Line %d: multiple result lines", t->line);
575 if (!TEST_ptr(t->expected_err = OPENSSL_strdup(value)))
577 } else if (strcmp(keyword, "Function") == 0) {
578 if (t->func != NULL) {
579 TEST_info("Line %d: multiple function lines\n", t->line);
582 if (!TEST_ptr(t->func = OPENSSL_strdup(value)))
584 } else if (strcmp(keyword, "Reason") == 0) {
585 if (t->reason != NULL) {
586 TEST_info("Line %d: multiple reason lines", t->line);
589 if (!TEST_ptr(t->reason = OPENSSL_strdup(value)))
592 /* Must be test specific line: try to parse it */
593 int rv = t->meth == NULL ? 0 : t->meth->parse(t, keyword, value);
596 TEST_info("Line %d: unknown keyword %s", t->line, keyword);
600 TEST_info("Line %d: error processing keyword %s\n",
608 /* Message digest tests */
610 typedef struct digest_data_st {
611 /* Digest this test is for */
612 const EVP_MD *digest;
613 /* Input to digest */
614 unsigned char *input;
616 /* Repeat count for input */
618 /* Expected output */
619 unsigned char *output;
623 static int digest_test_init(EVP_TEST *t, const char *alg)
625 const EVP_MD *digest;
628 digest = EVP_get_digestbyname(alg);
630 /* If alg has an OID assume disabled algorithm */
631 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
637 mdat = OPENSSL_zalloc(sizeof(*mdat));
638 mdat->digest = digest;
644 static void digest_test_cleanup(EVP_TEST *t)
646 DIGEST_DATA *mdat = t->data;
648 OPENSSL_free(mdat->input);
649 OPENSSL_free(mdat->output);
652 static int digest_test_parse(EVP_TEST *t,
653 const char *keyword, const char *value)
655 DIGEST_DATA *mdata = t->data;
657 if (strcmp(keyword, "Input") == 0)
658 return test_bin(value, &mdata->input, &mdata->input_len);
659 if (strcmp(keyword, "Output") == 0)
660 return test_bin(value, &mdata->output, &mdata->output_len);
661 if (strcmp(keyword, "Count") == 0) {
662 long nrpt = atoi(value);
665 mdata->nrpt = (size_t)nrpt;
671 static int digest_test_run(EVP_TEST *t)
673 DIGEST_DATA *mdata = t->data;
676 unsigned char md[EVP_MAX_MD_SIZE];
679 t->err = "TEST_FAILURE";
680 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
683 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL)) {
684 t->err = "DIGESTINIT_ERROR";
687 for (i = 0; i < mdata->nrpt; i++)
688 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len)) {
689 t->err = "DIGESTUPDATE_ERROR";
692 if (!EVP_DigestFinal(mctx, md, &md_len)) {
693 t->err = "DIGESTFINAL_ERROR";
696 if (md_len != mdata->output_len) {
697 t->err = "DIGEST_LENGTH_MISMATCH";
700 if (!compare_mem(mdata->output, mdata->output_len, md, md_len)) {
701 t->err = "DIGEST_MISMATCH";
707 EVP_MD_CTX_free(mctx);
711 static const EVP_TEST_METHOD digest_test_method = {
720 typedef struct cipher_data_st {
721 const EVP_CIPHER *cipher;
723 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
729 unsigned char *plaintext;
730 size_t plaintext_len;
731 unsigned char *ciphertext;
732 size_t ciphertext_len;
740 static int cipher_test_init(EVP_TEST *t, const char *alg)
742 const EVP_CIPHER *cipher;
743 CIPHER_DATA *cdat = t->data;
745 cipher = EVP_get_cipherbyname(alg);
747 /* If alg has an OID assume disabled algorithm */
748 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
754 cdat = OPENSSL_malloc(sizeof(*cdat));
755 cdat->cipher = cipher;
759 cdat->ciphertext = NULL;
760 cdat->plaintext = NULL;
764 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
765 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
766 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
767 cdat->aead = EVP_CIPHER_mode(cipher);
768 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
776 static void cipher_test_cleanup(EVP_TEST *t)
778 CIPHER_DATA *cdat = t->data;
780 OPENSSL_free(cdat->key);
781 OPENSSL_free(cdat->iv);
782 OPENSSL_free(cdat->ciphertext);
783 OPENSSL_free(cdat->plaintext);
784 OPENSSL_free(cdat->aad);
785 OPENSSL_free(cdat->tag);
788 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
791 CIPHER_DATA *cdat = t->data;
793 if (strcmp(keyword, "Key") == 0)
794 return test_bin(value, &cdat->key, &cdat->key_len);
795 if (strcmp(keyword, "IV") == 0)
796 return test_bin(value, &cdat->iv, &cdat->iv_len);
797 if (strcmp(keyword, "Plaintext") == 0)
798 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
799 if (strcmp(keyword, "Ciphertext") == 0)
800 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
802 if (strcmp(keyword, "AAD") == 0)
803 return test_bin(value, &cdat->aad, &cdat->aad_len);
804 if (strcmp(keyword, "Tag") == 0)
805 return test_bin(value, &cdat->tag, &cdat->tag_len);
808 if (strcmp(keyword, "Operation") == 0) {
809 if (strcmp(value, "ENCRYPT") == 0)
811 else if (strcmp(value, "DECRYPT") == 0)
820 static int cipher_test_enc(EVP_TEST *t, int enc,
821 size_t out_misalign, size_t inp_misalign, int frag)
823 CIPHER_DATA *cdat = t->data;
824 unsigned char *in, *out, *tmp = NULL;
825 size_t in_len, out_len, donelen = 0;
826 int ok = 0, tmplen, chunklen, tmpflen;
827 EVP_CIPHER_CTX *ctx = NULL;
829 t->err = "TEST_FAILURE";
830 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
832 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
834 in = cdat->plaintext;
835 in_len = cdat->plaintext_len;
836 out = cdat->ciphertext;
837 out_len = cdat->ciphertext_len;
839 in = cdat->ciphertext;
840 in_len = cdat->ciphertext_len;
841 out = cdat->plaintext;
842 out_len = cdat->plaintext_len;
844 if (inp_misalign == (size_t)-1) {
846 * Exercise in-place encryption
848 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
851 in = memcpy(tmp + out_misalign, in, in_len);
853 inp_misalign += 16 - ((out_misalign + in_len) & 15);
855 * 'tmp' will store both output and copy of input. We make the copy
856 * of input to specifically aligned part of 'tmp'. So we just
857 * figured out how much padding would ensure the required alignment,
858 * now we allocate extended buffer and finally copy the input just
859 * past inp_misalign in expression below. Output will be written
860 * past out_misalign...
862 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
863 inp_misalign + in_len);
866 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
867 inp_misalign, in, in_len);
869 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc)) {
870 t->err = "CIPHERINIT_ERROR";
875 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
877 t->err = "INVALID_IV_LENGTH";
880 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
881 t->err = "INVALID_IV_LENGTH";
888 * If encrypting or OCB just set tag length initially, otherwise
889 * set tag length and value.
891 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
892 t->err = "TAG_LENGTH_SET_ERROR";
895 t->err = "TAG_SET_ERROR";
898 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
899 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
905 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len)) {
906 t->err = "INVALID_KEY_LENGTH";
909 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1)) {
910 t->err = "KEY_SET_ERROR";
914 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
915 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
916 cdat->tag_len, cdat->tag)) {
917 t->err = "TAG_SET_ERROR";
922 if (cdat->aead == EVP_CIPH_CCM_MODE) {
923 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
924 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
929 t->err = "AAD_SET_ERROR";
931 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
936 * Supply the AAD in chunks less than the block size where possible
938 if (cdat->aad_len > 0) {
939 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
943 if (cdat->aad_len > 2) {
944 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
947 donelen += cdat->aad_len - 2;
949 if (cdat->aad_len > 1
950 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
951 cdat->aad + donelen, 1))
955 EVP_CIPHER_CTX_set_padding(ctx, 0);
956 t->err = "CIPHERUPDATE_ERROR";
959 /* We supply the data all in one go */
960 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
963 /* Supply the data in chunks less than the block size where possible */
965 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
972 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
980 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
986 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
987 t->err = "CIPHERFINAL_ERROR";
990 if (!compare_mem(out, out_len, tmp + out_misalign, tmplen + tmpflen)) {
991 t->err = "VALUE_MISMATCH";
994 if (enc && cdat->aead) {
995 unsigned char rtag[16];
997 if (cdat->tag_len > sizeof(rtag)) {
998 t->err = "TAG_LENGTH_INTERNAL_ERROR";
1001 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1002 cdat->tag_len, rtag)) {
1003 t->err = "TAG_RETRIEVE_ERROR";
1006 if (!compare_mem(cdat->tag, cdat->tag_len, rtag, cdat->tag_len)) {
1007 t->err = "TAG_VALUE_MISMATCH";
1015 EVP_CIPHER_CTX_free(ctx);
1019 static int cipher_test_run(EVP_TEST *t)
1021 CIPHER_DATA *cdat = t->data;
1023 size_t out_misalign, inp_misalign;
1029 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1030 /* IV is optional and usually omitted in wrap mode */
1031 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1036 if (cdat->aead && !cdat->tag) {
1040 for (out_misalign = 0; out_misalign <= 1;) {
1041 static char aux_err[64];
1042 t->aux_err = aux_err;
1043 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1044 if (inp_misalign == (size_t)-1) {
1045 /* kludge: inp_misalign == -1 means "exercise in-place" */
1046 BIO_snprintf(aux_err, sizeof(aux_err),
1047 "%s in-place, %sfragmented",
1048 out_misalign ? "misaligned" : "aligned",
1049 frag ? "" : "not ");
1051 BIO_snprintf(aux_err, sizeof(aux_err),
1052 "%s output and %s input, %sfragmented",
1053 out_misalign ? "misaligned" : "aligned",
1054 inp_misalign ? "misaligned" : "aligned",
1055 frag ? "" : "not ");
1058 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1059 /* Not fatal errors: return */
1066 if (cdat->enc != 1) {
1067 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1068 /* Not fatal errors: return */
1077 if (out_misalign == 1 && frag == 0) {
1079 * XTS, CCM and Wrap modes have special requirements about input
1080 * lengths so we don't fragment for those
1082 if (cdat->aead == EVP_CIPH_CCM_MODE
1083 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1084 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1097 static const EVP_TEST_METHOD cipher_test_method = {
1100 cipher_test_cleanup,
1105 typedef struct mac_data_st {
1108 /* Algorithm string for this MAC */
1114 unsigned char *input;
1116 /* Expected output */
1117 unsigned char *output;
1121 static int mac_test_init(EVP_TEST *t, const char *alg)
1126 if (strcmp(alg, "HMAC") == 0) {
1127 type = EVP_PKEY_HMAC;
1128 } else if (strcmp(alg, "CMAC") == 0) {
1129 #ifndef OPENSSL_NO_CMAC
1130 type = EVP_PKEY_CMAC;
1135 } else if (strcmp(alg, "Poly1305") == 0) {
1136 #ifndef OPENSSL_NO_POLY1305
1137 type = EVP_PKEY_POLY1305;
1142 } else if (strcmp(alg, "SipHash") == 0) {
1143 #ifndef OPENSSL_NO_SIPHASH
1144 type = EVP_PKEY_SIPHASH;
1152 mdat = OPENSSL_zalloc(sizeof(*mdat));
1158 static void mac_test_cleanup(EVP_TEST *t)
1160 MAC_DATA *mdat = t->data;
1162 OPENSSL_free(mdat->alg);
1163 OPENSSL_free(mdat->key);
1164 OPENSSL_free(mdat->input);
1165 OPENSSL_free(mdat->output);
1168 static int mac_test_parse(EVP_TEST *t,
1169 const char *keyword, const char *value)
1171 MAC_DATA *mdata = t->data;
1173 if (strcmp(keyword, "Key") == 0)
1174 return test_bin(value, &mdata->key, &mdata->key_len);
1175 if (strcmp(keyword, "Algorithm") == 0) {
1176 mdata->alg = OPENSSL_strdup(value);
1181 if (strcmp(keyword, "Input") == 0)
1182 return test_bin(value, &mdata->input, &mdata->input_len);
1183 if (strcmp(keyword, "Output") == 0)
1184 return test_bin(value, &mdata->output, &mdata->output_len);
1188 static int mac_test_run(EVP_TEST *t)
1190 MAC_DATA *mdata = t->data;
1191 EVP_MD_CTX *mctx = NULL;
1192 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1193 EVP_PKEY *key = NULL;
1194 const EVP_MD *md = NULL;
1195 unsigned char *mac = NULL;
1198 #ifdef OPENSSL_NO_DES
1199 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1206 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL))) {
1207 t->err = "MAC_PKEY_CTX_ERROR";
1211 if (EVP_PKEY_keygen_init(genctx) <= 0) {
1212 t->err = "MAC_KEYGEN_INIT_ERROR";
1215 if (mdata->type == EVP_PKEY_CMAC
1216 && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0) {
1217 t->err = "MAC_ALGORITHM_SET_ERROR";
1221 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0) {
1222 t->err = "MAC_KEY_SET_ERROR";
1226 if (EVP_PKEY_keygen(genctx, &key) <= 0) {
1227 t->err = "MAC_KEY_GENERATE_ERROR";
1230 if (mdata->type == EVP_PKEY_HMAC) {
1231 if (!TEST_ptr(md = EVP_get_digestbyname(mdata->alg))) {
1232 t->err = "MAC_ALGORITHM_SET_ERROR";
1236 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1237 t->err = "INTERNAL_ERROR";
1240 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1241 t->err = "DIGESTSIGNINIT_ERROR";
1245 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len)) {
1246 t->err = "DIGESTSIGNUPDATE_ERROR";
1249 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len)) {
1250 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1253 if (!TEST_ptr(mac = OPENSSL_malloc(mac_len))) {
1254 t->err = "TEST_FAILURE";
1257 if (!EVP_DigestSignFinal(mctx, mac, &mac_len)
1258 || !compare_mem(mdata->output, mdata->output_len, mac, mac_len)) {
1259 t->err = "TEST_MAC_ERR";
1264 EVP_MD_CTX_free(mctx);
1266 EVP_PKEY_CTX_free(genctx);
1271 static const EVP_TEST_METHOD mac_test_method = {
1280 * Public key operations. These are all very similar and can share
1281 * a lot of common code.
1284 typedef struct pkey_data_st {
1285 /* Context for this operation */
1287 /* Key operation to perform */
1288 int (*keyop) (EVP_PKEY_CTX *ctx,
1289 unsigned char *sig, size_t *siglen,
1290 const unsigned char *tbs, size_t tbslen);
1292 unsigned char *input;
1294 /* Expected output */
1295 unsigned char *output;
1300 * Perform public key operation setup: lookup key, allocated ctx and call
1301 * the appropriate initialisation function
1303 static int pkey_test_init(EVP_TEST *t, const char *name,
1305 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1306 int (*keyop) (EVP_PKEY_CTX *ctx,
1307 unsigned char *sig, size_t *siglen,
1308 const unsigned char *tbs,
1313 EVP_PKEY *pkey = NULL;
1317 rv = find_key(&pkey, name, public_keys);
1319 rv = find_key(&pkey, name, private_keys);
1320 if (rv == 0 || pkey == NULL) {
1325 if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
1326 EVP_PKEY_free(pkey);
1330 kdata->input = NULL;
1331 kdata->output = NULL;
1332 kdata->keyop = keyop;
1334 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1336 if (keyopinit(kdata->ctx) <= 0)
1337 t->err = "KEYOP_INIT_ERROR";
1341 static void pkey_test_cleanup(EVP_TEST *t)
1343 PKEY_DATA *kdata = t->data;
1345 OPENSSL_free(kdata->input);
1346 OPENSSL_free(kdata->output);
1347 EVP_PKEY_CTX_free(kdata->ctx);
1350 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1356 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1358 p = strchr(tmpval, ':');
1361 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1363 t->err = "PKEY_CTRL_INVALID";
1365 } else if (p != NULL && rv <= 0) {
1366 /* If p has an OID and lookup fails assume disabled algorithm */
1367 int nid = OBJ_sn2nid(p);
1369 if (nid == NID_undef)
1370 nid = OBJ_ln2nid(p);
1371 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1372 EVP_get_cipherbynid(nid) == NULL) {
1376 t->err = "PKEY_CTRL_ERROR";
1380 OPENSSL_free(tmpval);
1384 static int pkey_test_parse(EVP_TEST *t,
1385 const char *keyword, const char *value)
1387 PKEY_DATA *kdata = t->data;
1388 if (strcmp(keyword, "Input") == 0)
1389 return test_bin(value, &kdata->input, &kdata->input_len);
1390 if (strcmp(keyword, "Output") == 0)
1391 return test_bin(value, &kdata->output, &kdata->output_len);
1392 if (strcmp(keyword, "Ctrl") == 0)
1393 return pkey_test_ctrl(t, kdata->ctx, value);
1397 static int pkey_test_run(EVP_TEST *t)
1399 PKEY_DATA *kdata = t->data;
1400 unsigned char *out = NULL;
1403 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1404 kdata->input_len) <= 0
1405 || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
1406 t->err = "KEYOP_LENGTH_ERROR";
1409 if (kdata->keyop(kdata->ctx, out,
1410 &out_len, kdata->input, kdata->input_len) <= 0) {
1411 t->err = "KEYOP_ERROR";
1414 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1415 t->err = "KEYOP_MISMATCH";
1424 static int sign_test_init(EVP_TEST *t, const char *name)
1426 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1429 static const EVP_TEST_METHOD psign_test_method = {
1437 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1439 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1440 EVP_PKEY_verify_recover);
1443 static const EVP_TEST_METHOD pverify_recover_test_method = {
1445 verify_recover_test_init,
1451 static int decrypt_test_init(EVP_TEST *t, const char *name)
1453 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1457 static const EVP_TEST_METHOD pdecrypt_test_method = {
1465 static int verify_test_init(EVP_TEST *t, const char *name)
1467 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1470 static int verify_test_run(EVP_TEST *t)
1472 PKEY_DATA *kdata = t->data;
1474 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1475 kdata->input, kdata->input_len) <= 0)
1476 t->err = "VERIFY_ERROR";
1480 static const EVP_TEST_METHOD pverify_test_method = {
1489 static int pderive_test_init(EVP_TEST *t, const char *name)
1491 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1494 static int pderive_test_parse(EVP_TEST *t,
1495 const char *keyword, const char *value)
1497 PKEY_DATA *kdata = t->data;
1499 if (strcmp(keyword, "PeerKey") == 0) {
1501 if (find_key(&peer, value, public_keys) == 0)
1503 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1507 if (strcmp(keyword, "SharedSecret") == 0)
1508 return test_bin(value, &kdata->output, &kdata->output_len);
1509 if (strcmp(keyword, "Ctrl") == 0)
1510 return pkey_test_ctrl(t, kdata->ctx, value);
1514 static int pderive_test_run(EVP_TEST *t)
1516 PKEY_DATA *kdata = t->data;
1517 unsigned char *out = NULL;
1520 out_len = kdata->output_len;
1521 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1522 t->err = "DERIVE_ERROR";
1525 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1526 t->err = "DERIVE_ERROR";
1529 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1530 t->err = "SHARED_SECRET_MISMATCH";
1540 static const EVP_TEST_METHOD pderive_test_method = {
1550 #define PBE_TYPE_SCRYPT 1
1551 #define PBE_TYPE_PBKDF2 2
1552 #define PBE_TYPE_PKCS12 3
1554 typedef struct pbe_data_st {
1556 /* scrypt parameters */
1557 uint64_t N, r, p, maxmem;
1558 /* PKCS#12 parameters */
1562 unsigned char *pass;
1565 unsigned char *salt;
1567 /* Expected output */
1572 #ifndef OPENSSL_NO_SCRYPT
1573 static int scrypt_test_parse(EVP_TEST *t,
1574 const char *keyword, const char *value)
1576 PBE_DATA *pdata = t->data;
1578 if (strcmp(keyword, "N") == 0)
1579 return test_uint64(value, &pdata->N);
1580 if (strcmp(keyword, "p") == 0)
1581 return test_uint64(value, &pdata->p);
1582 if (strcmp(keyword, "r") == 0)
1583 return test_uint64(value, &pdata->r);
1584 if (strcmp(keyword, "maxmem") == 0)
1585 return test_uint64(value, &pdata->maxmem);
1590 static int pbkdf2_test_parse(EVP_TEST *t,
1591 const char *keyword, const char *value)
1593 PBE_DATA *pdata = t->data;
1595 if (strcmp(keyword, "iter") == 0) {
1596 pdata->iter = atoi(value);
1597 if (pdata->iter <= 0)
1601 if (strcmp(keyword, "MD") == 0) {
1602 pdata->md = EVP_get_digestbyname(value);
1603 if (pdata->md == NULL)
1610 static int pkcs12_test_parse(EVP_TEST *t,
1611 const char *keyword, const char *value)
1613 PBE_DATA *pdata = t->data;
1615 if (strcmp(keyword, "id") == 0) {
1616 pdata->id = atoi(value);
1621 return pbkdf2_test_parse(t, keyword, value);
1624 static int pbe_test_init(EVP_TEST *t, const char *alg)
1629 if (strcmp(alg, "scrypt") == 0) {
1630 #ifndef OPENSSL_NO_SCRYPT
1631 pbe_type = PBE_TYPE_SCRYPT;
1636 } else if (strcmp(alg, "pbkdf2") == 0) {
1637 pbe_type = PBE_TYPE_PBKDF2;
1638 } else if (strcmp(alg, "pkcs12") == 0) {
1639 pbe_type = PBE_TYPE_PKCS12;
1641 TEST_error("Unknown pbe algorithm %s", alg);
1643 pdat = OPENSSL_malloc(sizeof(*pdat));
1644 pdat->pbe_type = pbe_type;
1658 static void pbe_test_cleanup(EVP_TEST *t)
1660 PBE_DATA *pdat = t->data;
1662 OPENSSL_free(pdat->pass);
1663 OPENSSL_free(pdat->salt);
1664 OPENSSL_free(pdat->key);
1667 static int pbe_test_parse(EVP_TEST *t,
1668 const char *keyword, const char *value)
1670 PBE_DATA *pdata = t->data;
1672 if (strcmp(keyword, "Password") == 0)
1673 return test_bin(value, &pdata->pass, &pdata->pass_len);
1674 if (strcmp(keyword, "Salt") == 0)
1675 return test_bin(value, &pdata->salt, &pdata->salt_len);
1676 if (strcmp(keyword, "Key") == 0)
1677 return test_bin(value, &pdata->key, &pdata->key_len);
1678 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1679 return pbkdf2_test_parse(t, keyword, value);
1680 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1681 return pkcs12_test_parse(t, keyword, value);
1682 #ifndef OPENSSL_NO_SCRYPT
1683 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1684 return scrypt_test_parse(t, keyword, value);
1689 static int pbe_test_run(EVP_TEST *t)
1691 PBE_DATA *pdata = t->data;
1694 if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
1695 t->err = "INTERNAL_ERROR";
1698 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1699 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1700 pdata->salt, pdata->salt_len,
1701 pdata->iter, pdata->md,
1702 pdata->key_len, key) == 0) {
1703 t->err = "PBKDF2_ERROR";
1706 #ifndef OPENSSL_NO_SCRYPT
1707 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1708 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1709 pdata->salt, pdata->salt_len,
1710 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1711 key, pdata->key_len) == 0) {
1712 t->err = "SCRYPT_ERROR";
1716 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1717 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1718 pdata->salt, pdata->salt_len,
1719 pdata->id, pdata->iter, pdata->key_len,
1720 key, pdata->md) == 0) {
1721 t->err = "PKCS12_ERROR";
1725 if (!compare_mem(pdata->key, pdata->key_len, key, pdata->key_len)) {
1726 t->err = "KEY_MISMATCH";
1735 static const EVP_TEST_METHOD pbe_test_method = {
1746 BASE64_CANONICAL_ENCODING = 0,
1747 BASE64_VALID_ENCODING = 1,
1748 BASE64_INVALID_ENCODING = 2
1749 } base64_encoding_type;
1751 typedef struct encode_data_st {
1752 /* Input to encoding */
1753 unsigned char *input;
1755 /* Expected output */
1756 unsigned char *output;
1758 base64_encoding_type encoding;
1761 static int encode_test_init(EVP_TEST *t, const char *encoding)
1763 ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
1765 if (strcmp(encoding, "canonical") == 0) {
1766 edata->encoding = BASE64_CANONICAL_ENCODING;
1767 } else if (strcmp(encoding, "valid") == 0) {
1768 edata->encoding = BASE64_VALID_ENCODING;
1769 } else if (strcmp(encoding, "invalid") == 0) {
1770 edata->encoding = BASE64_INVALID_ENCODING;
1771 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1772 if (t->expected_err == NULL)
1775 TEST_info("Bad encoding: %s. Should be one of "
1776 "{canonical, valid, invalid}", encoding);
1783 static void encode_test_cleanup(EVP_TEST *t)
1785 ENCODE_DATA *edata = t->data;
1787 OPENSSL_free(edata->input);
1788 OPENSSL_free(edata->output);
1789 memset(edata, 0, sizeof(*edata));
1792 static int encode_test_parse(EVP_TEST *t,
1793 const char *keyword, const char *value)
1795 ENCODE_DATA *edata = t->data;
1796 if (strcmp(keyword, "Input") == 0)
1797 return test_bin(value, &edata->input, &edata->input_len);
1798 if (strcmp(keyword, "Output") == 0)
1799 return test_bin(value, &edata->output, &edata->output_len);
1803 static int encode_test_run(EVP_TEST *t)
1805 ENCODE_DATA *edata = t->data;
1806 unsigned char *encode_out = NULL, *decode_out = NULL;
1807 int output_len, chunk_len;
1808 EVP_ENCODE_CTX *decode_ctx;
1810 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1811 t->err = "INTERNAL_ERROR";
1815 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1816 EVP_ENCODE_CTX *encode_ctx;
1818 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1819 || !TEST_ptr(encode_out =
1820 OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
1823 EVP_EncodeInit(encode_ctx);
1824 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1825 edata->input, edata->input_len);
1826 output_len = chunk_len;
1828 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1829 output_len += chunk_len;
1831 EVP_ENCODE_CTX_free(encode_ctx);
1833 if (!compare_mem(edata->output, edata->output_len,
1834 encode_out, output_len)) {
1835 t->err = "BAD_ENCODING";
1840 if (!TEST_ptr(decode_out =
1841 OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
1844 EVP_DecodeInit(decode_ctx);
1845 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1846 edata->output_len) < 0) {
1847 t->err = "DECODE_ERROR";
1850 output_len = chunk_len;
1852 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1853 t->err = "DECODE_ERROR";
1856 output_len += chunk_len;
1858 if (edata->encoding != BASE64_INVALID_ENCODING
1859 && !compare_mem(edata->input, edata->input_len,
1860 decode_out, output_len)) {
1861 t->err = "BAD_DECODING";
1867 OPENSSL_free(encode_out);
1868 OPENSSL_free(decode_out);
1869 EVP_ENCODE_CTX_free(decode_ctx);
1873 static const EVP_TEST_METHOD encode_test_method = {
1876 encode_test_cleanup,
1881 /* KDF operations */
1883 typedef struct kdf_data_st {
1884 /* Context for this operation */
1886 /* Expected output */
1887 unsigned char *output;
1892 * Perform public key operation setup: lookup key, allocated ctx and call
1893 * the appropriate initialisation function
1895 static int kdf_test_init(EVP_TEST *t, const char *name)
1899 kdata = OPENSSL_malloc(sizeof(*kdata));
1903 kdata->output = NULL;
1905 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1906 if (kdata->ctx == NULL)
1908 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1913 static void kdf_test_cleanup(EVP_TEST *t)
1915 KDF_DATA *kdata = t->data;
1916 OPENSSL_free(kdata->output);
1917 EVP_PKEY_CTX_free(kdata->ctx);
1920 static int kdf_test_parse(EVP_TEST *t,
1921 const char *keyword, const char *value)
1923 KDF_DATA *kdata = t->data;
1925 if (strcmp(keyword, "Output") == 0)
1926 return test_bin(value, &kdata->output, &kdata->output_len);
1927 if (strncmp(keyword, "Ctrl", 4) == 0)
1928 return pkey_test_ctrl(t, kdata->ctx, value);
1932 static int kdf_test_run(EVP_TEST *t)
1934 KDF_DATA *kdata = t->data;
1935 unsigned char *out = NULL;
1936 size_t out_len = kdata->output_len;
1938 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1939 t->err = "INTERNAL_ERROR";
1942 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1943 t->err = "KDF_DERIVE_ERROR";
1946 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1947 t->err = "KDF_MISMATCH";
1957 static const EVP_TEST_METHOD kdf_test_method = {
1965 typedef struct keypair_test_data_st {
1968 } KEYPAIR_TEST_DATA;
1970 static int keypair_test_init(EVP_TEST *t, const char *pair)
1973 EVP_PKEY *pk = NULL, *pubk = NULL;
1974 char *pub, *priv = NULL;
1975 KEYPAIR_TEST_DATA *data;
1977 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1978 || !TEST_ptr(pub = strchr(priv, ':'))) {
1979 t->err = "PARSING_ERROR";
1982 *pub++ = 0; /* split priv and pub strings */
1984 if (!TEST_true(find_key(&pk, priv, private_keys))) {
1985 TEST_info("Cannot find private key: %s", priv);
1986 t->err = "MISSING_PRIVATE_KEY";
1989 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
1990 TEST_info("Cannot find public key: %s", pub);
1991 t->err = "MISSING_PUBLIC_KEY";
1995 if (pk == NULL && pubk == NULL) {
1996 /* Both keys are listed but unsupported: skip this test */
2002 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2016 static void keypair_test_cleanup(EVP_TEST *t)
2018 OPENSSL_free(t->data);
2022 /* For test that do not accept any custom keyword:
2023 * return 0 if called
2025 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2030 static int keypair_test_run(EVP_TEST *t)
2033 const KEYPAIR_TEST_DATA *pair = t->data;
2035 if (pair->privk == NULL || pair->pubk == NULL) {
2037 * this can only happen if only one of the keys is not set
2038 * which means that one of them was unsupported while the
2039 * other isn't: hence a key type mismatch.
2041 t->err = "KEYPAIR_TYPE_MISMATCH";
2046 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2048 t->err = "KEYPAIR_MISMATCH";
2049 } else if ( -1 == rv ) {
2050 t->err = "KEYPAIR_TYPE_MISMATCH";
2051 } else if ( -2 == rv ) {
2052 t->err = "UNSUPPORTED_KEY_COMPARISON";
2054 TEST_error("Unexpected error in key comparison");
2069 static const EVP_TEST_METHOD keypair_test_method = {
2072 keypair_test_cleanup,
2077 static int do_test_file(const char *testfile)
2083 set_test_title(testfile);
2084 current_test_file = testfile;
2085 if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2087 memset(&t, 0, sizeof(t));
2091 while (BIO_gets(in, buf, sizeof(buf))) {
2093 if (!TEST_true(parse_test_line(&t, buf)))
2096 /* Run any final test we have */
2097 if (!run_and_get_next(&t, NULL))
2100 TEST_info("Completed %d tests with %d errors and %d skipped",
2101 t.ntests, t.errors, t.nskip);
2102 free_key_list(public_keys);
2103 free_key_list(private_keys);
2106 return t.errors == 0;
2109 static char * const *testfiles;
2111 static int run_file_tests(int i)
2113 return do_test_file(testfiles[i]);
2116 int test_main(int argc, char *argv[])
2119 TEST_error("Usage: %s file...", argv[0]);
2122 testfiles = &argv[1];
2124 ADD_ALL_TESTS(run_file_tests, argc - 1);
2126 return run_tests(argv[0]);