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, "Result") == 0) {
568 if (t->expected_err) {
569 TEST_info("Line %d: multiple result lines", t->line);
572 if (!TEST_ptr(t->expected_err = OPENSSL_strdup(value)))
574 } else if (strcmp(keyword, "Function") == 0) {
575 if (t->func != NULL) {
576 TEST_info("Line %d: multiple function lines\n", t->line);
579 if (!TEST_ptr(t->func = OPENSSL_strdup(value)))
581 } else if (strcmp(keyword, "Reason") == 0) {
582 if (t->reason != NULL) {
583 TEST_info("Line %d: multiple reason lines", t->line);
586 if (!TEST_ptr(t->reason = OPENSSL_strdup(value)))
589 /* Must be test specific line: try to parse it */
590 int rv = t->meth == NULL ? 0 : t->meth->parse(t, keyword, value);
593 TEST_info("Line %d: unknown keyword %s", t->line, keyword);
597 TEST_info("Line %d: error processing keyword %s\n",
605 /* Message digest tests */
607 typedef struct digest_data_st {
608 /* Digest this test is for */
609 const EVP_MD *digest;
610 /* Input to digest */
611 unsigned char *input;
613 /* Repeat count for input */
615 /* Expected output */
616 unsigned char *output;
620 static int digest_test_init(EVP_TEST *t, const char *alg)
622 const EVP_MD *digest;
625 digest = EVP_get_digestbyname(alg);
627 /* If alg has an OID assume disabled algorithm */
628 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
634 mdat = OPENSSL_zalloc(sizeof(*mdat));
635 mdat->digest = digest;
641 static void digest_test_cleanup(EVP_TEST *t)
643 DIGEST_DATA *mdat = t->data;
645 OPENSSL_free(mdat->input);
646 OPENSSL_free(mdat->output);
649 static int digest_test_parse(EVP_TEST *t,
650 const char *keyword, const char *value)
652 DIGEST_DATA *mdata = t->data;
654 if (strcmp(keyword, "Input") == 0)
655 return test_bin(value, &mdata->input, &mdata->input_len);
656 if (strcmp(keyword, "Output") == 0)
657 return test_bin(value, &mdata->output, &mdata->output_len);
658 if (strcmp(keyword, "Count") == 0) {
659 long nrpt = atoi(value);
662 mdata->nrpt = (size_t)nrpt;
668 static int digest_test_run(EVP_TEST *t)
670 DIGEST_DATA *mdata = t->data;
673 unsigned char md[EVP_MAX_MD_SIZE];
676 t->err = "TEST_FAILURE";
677 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
680 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL)) {
681 t->err = "DIGESTINIT_ERROR";
684 for (i = 0; i < mdata->nrpt; i++)
685 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len)) {
686 t->err = "DIGESTUPDATE_ERROR";
689 if (!EVP_DigestFinal(mctx, md, &md_len)) {
690 t->err = "DIGESTFINAL_ERROR";
693 if (md_len != mdata->output_len) {
694 t->err = "DIGEST_LENGTH_MISMATCH";
697 if (!compare_mem(mdata->output, mdata->output_len, md, md_len)) {
698 t->err = "DIGEST_MISMATCH";
704 EVP_MD_CTX_free(mctx);
708 static const EVP_TEST_METHOD digest_test_method = {
717 typedef struct cipher_data_st {
718 const EVP_CIPHER *cipher;
720 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
726 unsigned char *plaintext;
727 size_t plaintext_len;
728 unsigned char *ciphertext;
729 size_t ciphertext_len;
737 static int cipher_test_init(EVP_TEST *t, const char *alg)
739 const EVP_CIPHER *cipher;
740 CIPHER_DATA *cdat = t->data;
742 cipher = EVP_get_cipherbyname(alg);
744 /* If alg has an OID assume disabled algorithm */
745 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
751 cdat = OPENSSL_malloc(sizeof(*cdat));
752 cdat->cipher = cipher;
756 cdat->ciphertext = NULL;
757 cdat->plaintext = NULL;
761 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
762 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
763 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
764 cdat->aead = EVP_CIPHER_mode(cipher);
765 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
773 static void cipher_test_cleanup(EVP_TEST *t)
775 CIPHER_DATA *cdat = t->data;
777 OPENSSL_free(cdat->key);
778 OPENSSL_free(cdat->iv);
779 OPENSSL_free(cdat->ciphertext);
780 OPENSSL_free(cdat->plaintext);
781 OPENSSL_free(cdat->aad);
782 OPENSSL_free(cdat->tag);
785 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
788 CIPHER_DATA *cdat = t->data;
790 if (strcmp(keyword, "Key") == 0)
791 return test_bin(value, &cdat->key, &cdat->key_len);
792 if (strcmp(keyword, "IV") == 0)
793 return test_bin(value, &cdat->iv, &cdat->iv_len);
794 if (strcmp(keyword, "Plaintext") == 0)
795 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
796 if (strcmp(keyword, "Ciphertext") == 0)
797 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
799 if (strcmp(keyword, "AAD") == 0)
800 return test_bin(value, &cdat->aad, &cdat->aad_len);
801 if (strcmp(keyword, "Tag") == 0)
802 return test_bin(value, &cdat->tag, &cdat->tag_len);
805 if (strcmp(keyword, "Operation") == 0) {
806 if (strcmp(value, "ENCRYPT") == 0)
808 else if (strcmp(value, "DECRYPT") == 0)
817 static int cipher_test_enc(EVP_TEST *t, int enc,
818 size_t out_misalign, size_t inp_misalign, int frag)
820 CIPHER_DATA *cdat = t->data;
821 unsigned char *in, *out, *tmp = NULL;
822 size_t in_len, out_len, donelen = 0;
823 int ok = 0, tmplen, chunklen, tmpflen;
824 EVP_CIPHER_CTX *ctx = NULL;
826 t->err = "TEST_FAILURE";
827 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
829 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
831 in = cdat->plaintext;
832 in_len = cdat->plaintext_len;
833 out = cdat->ciphertext;
834 out_len = cdat->ciphertext_len;
836 in = cdat->ciphertext;
837 in_len = cdat->ciphertext_len;
838 out = cdat->plaintext;
839 out_len = cdat->plaintext_len;
841 if (inp_misalign == (size_t)-1) {
843 * Exercise in-place encryption
845 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
848 in = memcpy(tmp + out_misalign, in, in_len);
850 inp_misalign += 16 - ((out_misalign + in_len) & 15);
852 * 'tmp' will store both output and copy of input. We make the copy
853 * of input to specifically aligned part of 'tmp'. So we just
854 * figured out how much padding would ensure the required alignment,
855 * now we allocate extended buffer and finally copy the input just
856 * past inp_misalign in expression below. Output will be written
857 * past out_misalign...
859 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
860 inp_misalign + in_len);
863 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
864 inp_misalign, in, in_len);
866 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc)) {
867 t->err = "CIPHERINIT_ERROR";
872 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
874 t->err = "INVALID_IV_LENGTH";
877 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
878 t->err = "INVALID_IV_LENGTH";
885 * If encrypting or OCB just set tag length initially, otherwise
886 * set tag length and value.
888 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
889 t->err = "TAG_LENGTH_SET_ERROR";
892 t->err = "TAG_SET_ERROR";
895 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
896 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
902 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len)) {
903 t->err = "INVALID_KEY_LENGTH";
906 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1)) {
907 t->err = "KEY_SET_ERROR";
911 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
912 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
913 cdat->tag_len, cdat->tag)) {
914 t->err = "TAG_SET_ERROR";
919 if (cdat->aead == EVP_CIPH_CCM_MODE) {
920 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
921 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
926 t->err = "AAD_SET_ERROR";
928 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
933 * Supply the AAD in chunks less than the block size where possible
935 if (cdat->aad_len > 0) {
936 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
940 if (cdat->aad_len > 2) {
941 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
944 donelen += cdat->aad_len - 2;
946 if (cdat->aad_len > 1
947 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
948 cdat->aad + donelen, 1))
952 EVP_CIPHER_CTX_set_padding(ctx, 0);
953 t->err = "CIPHERUPDATE_ERROR";
956 /* We supply the data all in one go */
957 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
960 /* Supply the data in chunks less than the block size where possible */
962 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
969 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
977 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
983 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
984 t->err = "CIPHERFINAL_ERROR";
987 if (!compare_mem(out, out_len, tmp + out_misalign, tmplen + tmpflen)) {
988 t->err = "VALUE_MISMATCH";
991 if (enc && cdat->aead) {
992 unsigned char rtag[16];
994 if (cdat->tag_len > sizeof(rtag)) {
995 t->err = "TAG_LENGTH_INTERNAL_ERROR";
998 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
999 cdat->tag_len, rtag)) {
1000 t->err = "TAG_RETRIEVE_ERROR";
1003 if (!compare_mem(cdat->tag, cdat->tag_len, rtag, cdat->tag_len)) {
1004 t->err = "TAG_VALUE_MISMATCH";
1012 EVP_CIPHER_CTX_free(ctx);
1016 static int cipher_test_run(EVP_TEST *t)
1018 CIPHER_DATA *cdat = t->data;
1020 size_t out_misalign, inp_misalign;
1026 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1027 /* IV is optional and usually omitted in wrap mode */
1028 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1033 if (cdat->aead && !cdat->tag) {
1037 for (out_misalign = 0; out_misalign <= 1;) {
1038 static char aux_err[64];
1039 t->aux_err = aux_err;
1040 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1041 if (inp_misalign == (size_t)-1) {
1042 /* kludge: inp_misalign == -1 means "exercise in-place" */
1043 BIO_snprintf(aux_err, sizeof(aux_err),
1044 "%s in-place, %sfragmented",
1045 out_misalign ? "misaligned" : "aligned",
1046 frag ? "" : "not ");
1048 BIO_snprintf(aux_err, sizeof(aux_err),
1049 "%s output and %s input, %sfragmented",
1050 out_misalign ? "misaligned" : "aligned",
1051 inp_misalign ? "misaligned" : "aligned",
1052 frag ? "" : "not ");
1055 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1056 /* Not fatal errors: return */
1063 if (cdat->enc != 1) {
1064 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1065 /* Not fatal errors: return */
1074 if (out_misalign == 1 && frag == 0) {
1076 * XTS, CCM and Wrap modes have special requirements about input
1077 * lengths so we don't fragment for those
1079 if (cdat->aead == EVP_CIPH_CCM_MODE
1080 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1081 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1094 static const EVP_TEST_METHOD cipher_test_method = {
1097 cipher_test_cleanup,
1102 typedef struct mac_data_st {
1105 /* Algorithm string for this MAC */
1111 unsigned char *input;
1113 /* Expected output */
1114 unsigned char *output;
1118 static int mac_test_init(EVP_TEST *t, const char *alg)
1123 if (strcmp(alg, "HMAC") == 0) {
1124 type = EVP_PKEY_HMAC;
1125 } else if (strcmp(alg, "CMAC") == 0) {
1126 #ifndef OPENSSL_NO_CMAC
1127 type = EVP_PKEY_CMAC;
1132 } else if (strcmp(alg, "Poly1305") == 0) {
1133 #ifndef OPENSSL_NO_POLY1305
1134 type = EVP_PKEY_POLY1305;
1139 } else if (strcmp(alg, "SipHash") == 0) {
1140 #ifndef OPENSSL_NO_SIPHASH
1141 type = EVP_PKEY_SIPHASH;
1149 mdat = OPENSSL_zalloc(sizeof(*mdat));
1155 static void mac_test_cleanup(EVP_TEST *t)
1157 MAC_DATA *mdat = t->data;
1159 OPENSSL_free(mdat->alg);
1160 OPENSSL_free(mdat->key);
1161 OPENSSL_free(mdat->input);
1162 OPENSSL_free(mdat->output);
1165 static int mac_test_parse(EVP_TEST *t,
1166 const char *keyword, const char *value)
1168 MAC_DATA *mdata = t->data;
1170 if (strcmp(keyword, "Key") == 0)
1171 return test_bin(value, &mdata->key, &mdata->key_len);
1172 if (strcmp(keyword, "Algorithm") == 0) {
1173 mdata->alg = OPENSSL_strdup(value);
1178 if (strcmp(keyword, "Input") == 0)
1179 return test_bin(value, &mdata->input, &mdata->input_len);
1180 if (strcmp(keyword, "Output") == 0)
1181 return test_bin(value, &mdata->output, &mdata->output_len);
1185 static int mac_test_run(EVP_TEST *t)
1187 MAC_DATA *mdata = t->data;
1188 EVP_MD_CTX *mctx = NULL;
1189 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1190 EVP_PKEY *key = NULL;
1191 const EVP_MD *md = NULL;
1192 unsigned char *mac = NULL;
1195 #ifdef OPENSSL_NO_DES
1196 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1203 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL))) {
1204 t->err = "MAC_PKEY_CTX_ERROR";
1208 if (EVP_PKEY_keygen_init(genctx) <= 0) {
1209 t->err = "MAC_KEYGEN_INIT_ERROR";
1212 if (mdata->type == EVP_PKEY_CMAC
1213 && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0) {
1214 t->err = "MAC_ALGORITHM_SET_ERROR";
1218 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0) {
1219 t->err = "MAC_KEY_SET_ERROR";
1223 if (EVP_PKEY_keygen(genctx, &key) <= 0) {
1224 t->err = "MAC_KEY_GENERATE_ERROR";
1227 if (mdata->type == EVP_PKEY_HMAC) {
1228 if (!TEST_ptr(md = EVP_get_digestbyname(mdata->alg))) {
1229 t->err = "MAC_ALGORITHM_SET_ERROR";
1233 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1234 t->err = "INTERNAL_ERROR";
1237 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1238 t->err = "DIGESTSIGNINIT_ERROR";
1242 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len)) {
1243 t->err = "DIGESTSIGNUPDATE_ERROR";
1246 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len)) {
1247 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1250 if (!TEST_ptr(mac = OPENSSL_malloc(mac_len))) {
1251 t->err = "TEST_FAILURE";
1254 if (!EVP_DigestSignFinal(mctx, mac, &mac_len)
1255 || !compare_mem(mdata->output, mdata->output_len, mac, mac_len)) {
1256 t->err = "TEST_MAC_ERR";
1261 EVP_MD_CTX_free(mctx);
1263 EVP_PKEY_CTX_free(genctx);
1268 static const EVP_TEST_METHOD mac_test_method = {
1277 * Public key operations. These are all very similar and can share
1278 * a lot of common code.
1281 typedef struct pkey_data_st {
1282 /* Context for this operation */
1284 /* Key operation to perform */
1285 int (*keyop) (EVP_PKEY_CTX *ctx,
1286 unsigned char *sig, size_t *siglen,
1287 const unsigned char *tbs, size_t tbslen);
1289 unsigned char *input;
1291 /* Expected output */
1292 unsigned char *output;
1297 * Perform public key operation setup: lookup key, allocated ctx and call
1298 * the appropriate initialisation function
1300 static int pkey_test_init(EVP_TEST *t, const char *name,
1302 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1303 int (*keyop) (EVP_PKEY_CTX *ctx,
1304 unsigned char *sig, size_t *siglen,
1305 const unsigned char *tbs,
1310 EVP_PKEY *pkey = NULL;
1314 rv = find_key(&pkey, name, public_keys);
1316 rv = find_key(&pkey, name, private_keys);
1317 if (rv == 0 || pkey == NULL) {
1322 if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
1323 EVP_PKEY_free(pkey);
1327 kdata->input = NULL;
1328 kdata->output = NULL;
1329 kdata->keyop = keyop;
1331 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1333 if (keyopinit(kdata->ctx) <= 0)
1334 t->err = "KEYOP_INIT_ERROR";
1338 static void pkey_test_cleanup(EVP_TEST *t)
1340 PKEY_DATA *kdata = t->data;
1342 OPENSSL_free(kdata->input);
1343 OPENSSL_free(kdata->output);
1344 EVP_PKEY_CTX_free(kdata->ctx);
1347 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1353 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1355 p = strchr(tmpval, ':');
1358 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1360 t->err = "PKEY_CTRL_INVALID";
1362 } else if (p != NULL && rv <= 0) {
1363 /* If p has an OID and lookup fails assume disabled algorithm */
1364 int nid = OBJ_sn2nid(p);
1366 if (nid == NID_undef)
1367 nid = OBJ_ln2nid(p);
1368 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1369 EVP_get_cipherbynid(nid) == NULL) {
1373 t->err = "PKEY_CTRL_ERROR";
1377 OPENSSL_free(tmpval);
1381 static int pkey_test_parse(EVP_TEST *t,
1382 const char *keyword, const char *value)
1384 PKEY_DATA *kdata = t->data;
1385 if (strcmp(keyword, "Input") == 0)
1386 return test_bin(value, &kdata->input, &kdata->input_len);
1387 if (strcmp(keyword, "Output") == 0)
1388 return test_bin(value, &kdata->output, &kdata->output_len);
1389 if (strcmp(keyword, "Ctrl") == 0)
1390 return pkey_test_ctrl(t, kdata->ctx, value);
1394 static int pkey_test_run(EVP_TEST *t)
1396 PKEY_DATA *kdata = t->data;
1397 unsigned char *out = NULL;
1400 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1401 kdata->input_len) <= 0
1402 || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
1403 t->err = "KEYOP_LENGTH_ERROR";
1406 if (kdata->keyop(kdata->ctx, out,
1407 &out_len, kdata->input, kdata->input_len) <= 0) {
1408 t->err = "KEYOP_ERROR";
1411 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1412 t->err = "KEYOP_MISMATCH";
1421 static int sign_test_init(EVP_TEST *t, const char *name)
1423 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1426 static const EVP_TEST_METHOD psign_test_method = {
1434 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1436 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1437 EVP_PKEY_verify_recover);
1440 static const EVP_TEST_METHOD pverify_recover_test_method = {
1442 verify_recover_test_init,
1448 static int decrypt_test_init(EVP_TEST *t, const char *name)
1450 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1454 static const EVP_TEST_METHOD pdecrypt_test_method = {
1462 static int verify_test_init(EVP_TEST *t, const char *name)
1464 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1467 static int verify_test_run(EVP_TEST *t)
1469 PKEY_DATA *kdata = t->data;
1471 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1472 kdata->input, kdata->input_len) <= 0)
1473 t->err = "VERIFY_ERROR";
1477 static const EVP_TEST_METHOD pverify_test_method = {
1486 static int pderive_test_init(EVP_TEST *t, const char *name)
1488 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1491 static int pderive_test_parse(EVP_TEST *t,
1492 const char *keyword, const char *value)
1494 PKEY_DATA *kdata = t->data;
1496 if (strcmp(keyword, "PeerKey") == 0) {
1498 if (find_key(&peer, value, public_keys) == 0)
1500 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1504 if (strcmp(keyword, "SharedSecret") == 0)
1505 return test_bin(value, &kdata->output, &kdata->output_len);
1506 if (strcmp(keyword, "Ctrl") == 0)
1507 return pkey_test_ctrl(t, kdata->ctx, value);
1511 static int pderive_test_run(EVP_TEST *t)
1513 PKEY_DATA *kdata = t->data;
1514 unsigned char *out = NULL;
1517 out_len = kdata->output_len;
1518 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1519 t->err = "DERIVE_ERROR";
1522 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1523 t->err = "DERIVE_ERROR";
1526 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1527 t->err = "SHARED_SECRET_MISMATCH";
1537 static const EVP_TEST_METHOD pderive_test_method = {
1547 #define PBE_TYPE_SCRYPT 1
1548 #define PBE_TYPE_PBKDF2 2
1549 #define PBE_TYPE_PKCS12 3
1551 typedef struct pbe_data_st {
1553 /* scrypt parameters */
1554 uint64_t N, r, p, maxmem;
1555 /* PKCS#12 parameters */
1559 unsigned char *pass;
1562 unsigned char *salt;
1564 /* Expected output */
1569 #ifndef OPENSSL_NO_SCRYPT
1570 static int scrypt_test_parse(EVP_TEST *t,
1571 const char *keyword, const char *value)
1573 PBE_DATA *pdata = t->data;
1575 if (strcmp(keyword, "N") == 0)
1576 return test_uint64(value, &pdata->N);
1577 if (strcmp(keyword, "p") == 0)
1578 return test_uint64(value, &pdata->p);
1579 if (strcmp(keyword, "r") == 0)
1580 return test_uint64(value, &pdata->r);
1581 if (strcmp(keyword, "maxmem") == 0)
1582 return test_uint64(value, &pdata->maxmem);
1587 static int pbkdf2_test_parse(EVP_TEST *t,
1588 const char *keyword, const char *value)
1590 PBE_DATA *pdata = t->data;
1592 if (strcmp(keyword, "iter") == 0) {
1593 pdata->iter = atoi(value);
1594 if (pdata->iter <= 0)
1598 if (strcmp(keyword, "MD") == 0) {
1599 pdata->md = EVP_get_digestbyname(value);
1600 if (pdata->md == NULL)
1607 static int pkcs12_test_parse(EVP_TEST *t,
1608 const char *keyword, const char *value)
1610 PBE_DATA *pdata = t->data;
1612 if (strcmp(keyword, "id") == 0) {
1613 pdata->id = atoi(value);
1618 return pbkdf2_test_parse(t, keyword, value);
1621 static int pbe_test_init(EVP_TEST *t, const char *alg)
1626 if (strcmp(alg, "scrypt") == 0) {
1627 #ifndef OPENSSL_NO_SCRYPT
1628 pbe_type = PBE_TYPE_SCRYPT;
1633 } else if (strcmp(alg, "pbkdf2") == 0) {
1634 pbe_type = PBE_TYPE_PBKDF2;
1635 } else if (strcmp(alg, "pkcs12") == 0) {
1636 pbe_type = PBE_TYPE_PKCS12;
1638 TEST_error("Unknown pbe algorithm %s", alg);
1640 pdat = OPENSSL_malloc(sizeof(*pdat));
1641 pdat->pbe_type = pbe_type;
1655 static void pbe_test_cleanup(EVP_TEST *t)
1657 PBE_DATA *pdat = t->data;
1659 OPENSSL_free(pdat->pass);
1660 OPENSSL_free(pdat->salt);
1661 OPENSSL_free(pdat->key);
1664 static int pbe_test_parse(EVP_TEST *t,
1665 const char *keyword, const char *value)
1667 PBE_DATA *pdata = t->data;
1669 if (strcmp(keyword, "Password") == 0)
1670 return test_bin(value, &pdata->pass, &pdata->pass_len);
1671 if (strcmp(keyword, "Salt") == 0)
1672 return test_bin(value, &pdata->salt, &pdata->salt_len);
1673 if (strcmp(keyword, "Key") == 0)
1674 return test_bin(value, &pdata->key, &pdata->key_len);
1675 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1676 return pbkdf2_test_parse(t, keyword, value);
1677 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1678 return pkcs12_test_parse(t, keyword, value);
1679 #ifndef OPENSSL_NO_SCRYPT
1680 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1681 return scrypt_test_parse(t, keyword, value);
1686 static int pbe_test_run(EVP_TEST *t)
1688 PBE_DATA *pdata = t->data;
1691 if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
1692 t->err = "INTERNAL_ERROR";
1695 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1696 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1697 pdata->salt, pdata->salt_len,
1698 pdata->iter, pdata->md,
1699 pdata->key_len, key) == 0) {
1700 t->err = "PBKDF2_ERROR";
1703 #ifndef OPENSSL_NO_SCRYPT
1704 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1705 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1706 pdata->salt, pdata->salt_len,
1707 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1708 key, pdata->key_len) == 0) {
1709 t->err = "SCRYPT_ERROR";
1713 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1714 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1715 pdata->salt, pdata->salt_len,
1716 pdata->id, pdata->iter, pdata->key_len,
1717 key, pdata->md) == 0) {
1718 t->err = "PKCS12_ERROR";
1722 if (!compare_mem(pdata->key, pdata->key_len, key, pdata->key_len)) {
1723 t->err = "KEY_MISMATCH";
1732 static const EVP_TEST_METHOD pbe_test_method = {
1743 BASE64_CANONICAL_ENCODING = 0,
1744 BASE64_VALID_ENCODING = 1,
1745 BASE64_INVALID_ENCODING = 2
1746 } base64_encoding_type;
1748 typedef struct encode_data_st {
1749 /* Input to encoding */
1750 unsigned char *input;
1752 /* Expected output */
1753 unsigned char *output;
1755 base64_encoding_type encoding;
1758 static int encode_test_init(EVP_TEST *t, const char *encoding)
1760 ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
1762 if (strcmp(encoding, "canonical") == 0) {
1763 edata->encoding = BASE64_CANONICAL_ENCODING;
1764 } else if (strcmp(encoding, "valid") == 0) {
1765 edata->encoding = BASE64_VALID_ENCODING;
1766 } else if (strcmp(encoding, "invalid") == 0) {
1767 edata->encoding = BASE64_INVALID_ENCODING;
1768 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1769 if (t->expected_err == NULL)
1772 TEST_info("Bad encoding: %s. Should be one of "
1773 "{canonical, valid, invalid}", encoding);
1780 static void encode_test_cleanup(EVP_TEST *t)
1782 ENCODE_DATA *edata = t->data;
1784 OPENSSL_free(edata->input);
1785 OPENSSL_free(edata->output);
1786 memset(edata, 0, sizeof(*edata));
1789 static int encode_test_parse(EVP_TEST *t,
1790 const char *keyword, const char *value)
1792 ENCODE_DATA *edata = t->data;
1793 if (strcmp(keyword, "Input") == 0)
1794 return test_bin(value, &edata->input, &edata->input_len);
1795 if (strcmp(keyword, "Output") == 0)
1796 return test_bin(value, &edata->output, &edata->output_len);
1800 static int encode_test_run(EVP_TEST *t)
1802 ENCODE_DATA *edata = t->data;
1803 unsigned char *encode_out = NULL, *decode_out = NULL;
1804 int output_len, chunk_len;
1805 EVP_ENCODE_CTX *decode_ctx;
1807 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1808 t->err = "INTERNAL_ERROR";
1812 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1813 EVP_ENCODE_CTX *encode_ctx;
1815 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1816 || !TEST_ptr(encode_out =
1817 OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
1820 EVP_EncodeInit(encode_ctx);
1821 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1822 edata->input, edata->input_len);
1823 output_len = chunk_len;
1825 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1826 output_len += chunk_len;
1828 EVP_ENCODE_CTX_free(encode_ctx);
1830 if (!compare_mem(edata->output, edata->output_len,
1831 encode_out, output_len)) {
1832 t->err = "BAD_ENCODING";
1837 if (!TEST_ptr(decode_out =
1838 OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
1841 EVP_DecodeInit(decode_ctx);
1842 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1843 edata->output_len) < 0) {
1844 t->err = "DECODE_ERROR";
1847 output_len = chunk_len;
1849 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1850 t->err = "DECODE_ERROR";
1853 output_len += chunk_len;
1855 if (edata->encoding != BASE64_INVALID_ENCODING
1856 && !compare_mem(edata->input, edata->input_len,
1857 decode_out, output_len)) {
1858 t->err = "BAD_DECODING";
1864 OPENSSL_free(encode_out);
1865 OPENSSL_free(decode_out);
1866 EVP_ENCODE_CTX_free(decode_ctx);
1870 static const EVP_TEST_METHOD encode_test_method = {
1873 encode_test_cleanup,
1878 /* KDF operations */
1880 typedef struct kdf_data_st {
1881 /* Context for this operation */
1883 /* Expected output */
1884 unsigned char *output;
1889 * Perform public key operation setup: lookup key, allocated ctx and call
1890 * the appropriate initialisation function
1892 static int kdf_test_init(EVP_TEST *t, const char *name)
1896 kdata = OPENSSL_malloc(sizeof(*kdata));
1900 kdata->output = NULL;
1902 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1903 if (kdata->ctx == NULL)
1905 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1910 static void kdf_test_cleanup(EVP_TEST *t)
1912 KDF_DATA *kdata = t->data;
1913 OPENSSL_free(kdata->output);
1914 EVP_PKEY_CTX_free(kdata->ctx);
1917 static int kdf_test_parse(EVP_TEST *t,
1918 const char *keyword, const char *value)
1920 KDF_DATA *kdata = t->data;
1922 if (strcmp(keyword, "Output") == 0)
1923 return test_bin(value, &kdata->output, &kdata->output_len);
1924 if (strncmp(keyword, "Ctrl", 4) == 0)
1925 return pkey_test_ctrl(t, kdata->ctx, value);
1929 static int kdf_test_run(EVP_TEST *t)
1931 KDF_DATA *kdata = t->data;
1932 unsigned char *out = NULL;
1933 size_t out_len = kdata->output_len;
1935 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1936 t->err = "INTERNAL_ERROR";
1939 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1940 t->err = "KDF_DERIVE_ERROR";
1943 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1944 t->err = "KDF_MISMATCH";
1954 static const EVP_TEST_METHOD kdf_test_method = {
1962 typedef struct keypair_test_data_st {
1965 } KEYPAIR_TEST_DATA;
1967 static int keypair_test_init(EVP_TEST *t, const char *pair)
1970 EVP_PKEY *pk = NULL, *pubk = NULL;
1971 char *pub, *priv = NULL;
1972 KEYPAIR_TEST_DATA *data;
1974 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1975 || !TEST_ptr(pub = strchr(priv, ':'))) {
1976 t->err = "PARSING_ERROR";
1979 *pub++ = 0; /* split priv and pub strings */
1981 if (!TEST_true(find_key(&pk, priv, private_keys))) {
1982 TEST_info("Cannot find private key: %s", priv);
1983 t->err = "MISSING_PRIVATE_KEY";
1986 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
1987 TEST_info("Cannot find public key: %s", pub);
1988 t->err = "MISSING_PUBLIC_KEY";
1992 if (pk == NULL && pubk == NULL) {
1993 /* Both keys are listed but unsupported: skip this test */
1999 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2013 static void keypair_test_cleanup(EVP_TEST *t)
2015 OPENSSL_free(t->data);
2019 /* For test that do not accept any custom keyword:
2020 * return 0 if called
2022 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2027 static int keypair_test_run(EVP_TEST *t)
2030 const KEYPAIR_TEST_DATA *pair = t->data;
2032 if (pair->privk == NULL || pair->pubk == NULL) {
2034 * this can only happen if only one of the keys is not set
2035 * which means that one of them was unsupported while the
2036 * other isn't: hence a key type mismatch.
2038 t->err = "KEYPAIR_TYPE_MISMATCH";
2043 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2045 t->err = "KEYPAIR_MISMATCH";
2046 } else if ( -1 == rv ) {
2047 t->err = "KEYPAIR_TYPE_MISMATCH";
2048 } else if ( -2 == rv ) {
2049 t->err = "UNSUPPORTED_KEY_COMPARISON";
2051 TEST_error("Unexpected error in key comparison");
2066 static const EVP_TEST_METHOD keypair_test_method = {
2069 keypair_test_cleanup,
2074 static int do_test_file(const char *testfile)
2080 current_test_file = testfile;
2081 if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2083 memset(&t, 0, sizeof(t));
2087 while (BIO_gets(in, buf, sizeof(buf))) {
2089 if (!TEST_true(parse_test_line(&t, buf)))
2092 /* Run any final test we have */
2093 if (!run_and_get_next(&t, NULL))
2096 TEST_info("Completed %d tests with %d errors and %d skipped",
2097 t.ntests, t.errors, t.nskip);
2098 free_key_list(public_keys);
2099 free_key_list(private_keys);
2102 return t.errors == 0;
2105 static char * const *testfiles;
2107 static int run_file_tests(int i)
2109 return do_test_file(testfiles[i]);
2112 int test_main(int argc, char *argv[])
2115 TEST_error("Usage: %s file...", argv[0]);
2118 testfiles = &argv[1];
2120 ADD_ALL_TESTS(run_file_tests, argc - 1);
2122 return run_tests(argv[0]);