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"
24 * Remove spaces from beginning and end of a string
26 static void remove_space(char **pval)
28 unsigned char *p = (unsigned char *)*pval, *beginning;
33 *pval = (char *)(beginning = p);
35 p = p + strlen(*pval) - 1;
37 /* Remove trailing space */
38 while (p >= beginning && isspace(*p))
43 * Given a line of the form:
44 * name = value # comment
45 * extract name and value. NB: modifies |linebuf|.
47 static int parse_line(char **pkw, char **pval, char *linebuf)
49 char *p = linebuf + strlen(linebuf) - 1;
52 TEST_error("FATAL: missing EOL");
57 p = strchr(linebuf, '#');
62 if ((p = strchr(linebuf, '=')) == NULL)
74 * Unescape some escape sequences in string literals.
75 * Return the result in a newly allocated buffer.
76 * Currently only supports '\n'.
77 * If the input length is 0, returns a valid 1-byte buffer, but sets
80 static unsigned char* unescape(const char *input, size_t input_len,
83 unsigned char *ret, *p;
88 return OPENSSL_zalloc(1);
91 /* Escaping is non-expanding; over-allocate original size for simplicity. */
92 ret = p = OPENSSL_malloc(input_len);
96 for (i = 0; i < input_len; i++) {
97 if (input[i] == '\\') {
98 if (i == input_len - 1 || input[i+1] != 'n')
115 /* For a hex string "value" convert to a binary allocated buffer */
116 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
122 /* Check for empty value */
125 * Don't return NULL for zero length buffer.
126 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
127 * a non-NULL key buffer even if the key length is 0, in order to detect
130 *buf = OPENSSL_malloc(1);
138 /* Check for NULL literal */
139 if (strcmp(value, "NULL") == 0) {
145 /* Check for string literal */
146 if (value[0] == '"') {
149 vlen = strlen(value);
150 if (value[vlen - 1] != '"')
153 *buf = unescape(value, vlen, buflen);
159 /* Otherwise assume as hex literal and convert it to binary buffer */
160 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
161 TEST_info("Cannot convert %s", value);
162 ERR_print_errors(bio_err);
165 /* Size of input buffer means we'll never overflow */
169 #ifndef OPENSSL_NO_SCRYPT
170 /* Currently only used by scrypt tests */
171 /* Parse unsigned decimal 64 bit integer value */
172 static int test_uint64(const char *value, uint64_t *pr)
174 const char *p = value;
176 if (!TEST_true(*p)) {
177 TEST_info("Invalid empty integer value");
182 if (*pr > UINT64_MAX / 10) {
183 TEST_error("Integer overflow in string %s", value);
187 if (!TEST_true(isdigit(*p))) {
188 TEST_error("Invalid character in string %s", value);
198 typedef struct evp_test_method_st EVP_TEST_METHOD;
200 /* Structure holding test information */
201 typedef struct evp_test_st {
202 /* file being read */
204 /* temp memory BIO for reading in keys */
206 /* method for this test */
207 const EVP_TEST_METHOD *meth;
208 /* current line being processed */
210 /* start line of current test */
211 unsigned int start_line;
212 /* Error string for test */
213 const char *err, *aux_err;
214 /* Expected error value of test */
216 /* Expected error function string */
218 /* Expected error reason string */
220 /* Number of tests */
224 /* Number of tests skipped */
226 /* If output mismatch expected and got value */
227 unsigned char *out_received;
228 size_t out_received_len;
229 unsigned char *out_expected;
230 size_t out_expected_len;
231 /* test specific data */
233 /* Current test should be skipped */
238 * Linked list of named keys.
240 typedef struct key_list_st {
243 struct key_list_st *next;
246 /* List of public and private keys */
247 static KEY_LIST *private_keys;
248 static KEY_LIST *public_keys;
251 * Test method structure
253 struct evp_test_method_st {
254 /* Name of test as it appears in file */
256 /* Initialise test for "alg" */
257 int (*init) (EVP_TEST * t, const char *alg);
258 /* Clean up method */
259 void (*cleanup) (EVP_TEST * t);
260 /* Test specific name value pair processing */
261 int (*parse) (EVP_TEST * t, const char *name, const char *value);
262 /* Run the test itself */
263 int (*run_test) (EVP_TEST * t);
266 static const EVP_TEST_METHOD digest_test_method, cipher_test_method;
267 static const EVP_TEST_METHOD mac_test_method;
268 static const EVP_TEST_METHOD psign_test_method, pverify_test_method;
269 static const EVP_TEST_METHOD pdecrypt_test_method;
270 static const EVP_TEST_METHOD pverify_recover_test_method;
271 static const EVP_TEST_METHOD pderive_test_method;
272 static const EVP_TEST_METHOD pbe_test_method;
273 static const EVP_TEST_METHOD encode_test_method;
274 static const EVP_TEST_METHOD kdf_test_method;
275 static const EVP_TEST_METHOD keypair_test_method;
277 static const EVP_TEST_METHOD *evp_test_list[] = {
282 &pverify_test_method,
283 &pdecrypt_test_method,
284 &pverify_recover_test_method,
285 &pderive_test_method,
289 &keypair_test_method,
293 static const EVP_TEST_METHOD *evp_find_test(const char *name)
295 const EVP_TEST_METHOD **tt;
297 for (tt = evp_test_list; *tt; tt++) {
298 if (strcmp(name, (*tt)->name) == 0)
304 static void hex_print(const char *name, const unsigned char *buf, size_t len)
308 fprintf(stderr, "%s ", name);
309 for (i = 0; i < len; i++)
310 fprintf(stderr, "%02X", buf[i]);
314 static void clear_test(EVP_TEST *t)
316 OPENSSL_free(t->expected_err);
317 t->expected_err = NULL;
318 OPENSSL_free(t->func);
320 OPENSSL_free(t->reason);
322 OPENSSL_free(t->out_expected);
323 t->out_expected = NULL;
324 t->out_expected_len = 0;
325 OPENSSL_free(t->out_received);
326 t->out_received = NULL;
327 t->out_received_len = 0;
332 static void print_expected(EVP_TEST *t)
334 if (t->out_expected == NULL && t->out_received == NULL)
336 hex_print("Expected:", t->out_expected, t->out_expected_len);
337 hex_print("Got: ", t->out_received, t->out_received_len);
342 * Check for errors in the test structure; return 1 if okay, else 0.
344 static int check_test_error(EVP_TEST *t)
350 if (t->err == NULL && t->expected_err == NULL)
352 if (t->err != NULL && t->expected_err == NULL) {
353 if (t->aux_err != NULL) {
354 TEST_info("Test line %d(%s): unexpected error %s",
355 t->start_line, t->aux_err, t->err);
357 TEST_info("Test line %d: unexpected error %s",
358 t->start_line, t->err);
363 if (t->err == NULL && t->expected_err != NULL) {
364 TEST_info("Test line %d: succeeded expecting %s",
365 t->start_line, t->expected_err);
369 if (strcmp(t->err, t->expected_err) != 0) {
370 TEST_info("Test line %d: expecting %s got %s",
371 t->start_line, t->expected_err, t->err);
375 if (t->func == NULL && t->reason == NULL)
378 if (t->func == NULL || t->reason == NULL) {
379 TEST_info("Test line %d: missing function or reason code",
384 err = ERR_peek_error();
386 TEST_info("Test line %d, expected error \"%s:%s\" not set",
387 t->start_line, t->func, t->reason);
391 func = ERR_func_error_string(err);
392 reason = ERR_reason_error_string(err);
393 if (func == NULL && reason == NULL) {
394 TEST_info("Test line %d: expected error \"%s:%s\","
395 " no strings available. Skipping...\n",
396 t->start_line, t->func, t->reason);
400 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
403 TEST_info("Test line %d: expected error \"%s:%s\", got \"%s:%s\"",
404 t->start_line, t->func, t->reason, func, reason);
410 * Setup a new test, run any existing test. Log a message and return 0
413 static int run_and_get_next(EVP_TEST *t, const EVP_TEST_METHOD *tmeth)
415 /* If we already have a test set up run it */
419 /*TEST_info("Line %d skipped %s test", t->start_line, t->meth->name);
424 if (t->err == NULL && t->meth->run_test(t) != 1) {
425 TEST_info("Line %d error %s", t->start_line, t->meth->name);
428 if (!check_test_error(t)) {
429 test_openssl_errors();
435 if (t->data != NULL) {
437 OPENSSL_free(t->data);
446 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
448 for (; lst; lst = lst->next) {
449 if (strcmp(lst->name, name) == 0) {
458 static void free_key_list(KEY_LIST *lst)
460 while (lst != NULL) {
463 EVP_PKEY_free(lst->key);
464 OPENSSL_free(lst->name);
471 static int check_unsupported()
473 long err = ERR_peek_error();
475 if (ERR_GET_LIB(err) == ERR_LIB_EVP
476 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
480 #ifndef OPENSSL_NO_EC
482 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
483 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
486 if (ERR_GET_LIB(err) == ERR_LIB_EC
487 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
491 #endif /* OPENSSL_NO_EC */
496 static int read_key(EVP_TEST *t)
500 if (t->key == NULL) {
501 if (!TEST_ptr(t->key = BIO_new(BIO_s_mem())))
503 } else if (!TEST_int_gt(BIO_reset(t->key), 0)) {
507 /* Read to PEM end line and place content in memory BIO */
508 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
510 if (!TEST_int_gt(BIO_puts(t->key, tmpbuf), 0))
512 if (strncmp(tmpbuf, "-----END", 8) == 0)
515 TEST_error("Can't find key end");
520 * Parse a line into the current test |t|. Return 0 on error.
522 static int parse_test_line(EVP_TEST *t, char *buf)
524 char *keyword = NULL, *value = NULL;
526 KEY_LIST **lst = NULL, *key = NULL;
528 const EVP_TEST_METHOD *tmeth = NULL;
530 if (!parse_line(&keyword, &value, buf))
532 if (strcmp(keyword, "PrivateKey") == 0) {
535 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
536 if (pk == NULL && !check_unsupported()) {
537 TEST_info("Error reading private key %s", value);
538 ERR_print_errors_fp(stderr);
544 if (strcmp(keyword, "PublicKey") == 0) {
547 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
548 if (pk == NULL && !check_unsupported()) {
549 TEST_info("Error reading public key %s", value);
550 ERR_print_errors_fp(stderr);
556 /* If we have a key add to list */
558 if (find_key(NULL, value, *lst)) {
559 TEST_info("Duplicate key %s", value);
562 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))
563 || !TEST_ptr(key->name = OPENSSL_strdup(value)))
571 /* See if keyword corresponds to a test start */
572 if ((tmeth = evp_find_test(keyword)) != NULL) {
573 if (!run_and_get_next(t, tmeth))
575 t->start_line = t->line;
577 if (!tmeth->init(t, value)) {
578 TEST_info("Unknown %s: %s", keyword, value);
585 if (strcmp(keyword, "Result") == 0) {
586 if (t->expected_err) {
587 TEST_info("Line %d: multiple result lines", t->line);
590 if (!TEST_ptr(t->expected_err = OPENSSL_strdup(value)))
592 } else if (strcmp(keyword, "Function") == 0) {
593 if (t->func != NULL) {
594 TEST_info("Line %d: multiple function lines\n", t->line);
597 if (!TEST_ptr(t->func = OPENSSL_strdup(value)))
599 } else if (strcmp(keyword, "Reason") == 0) {
600 if (t->reason != NULL) {
601 TEST_info("Line %d: multiple reason lines", t->line);
604 if (!TEST_ptr(t->reason = OPENSSL_strdup(value)))
607 /* Must be test specific line: try to parse it */
608 int rv = t->meth == NULL ? 0 : t->meth->parse(t, keyword, value);
611 TEST_info("Line %d: unknown keyword %s", t->line, keyword);
615 TEST_info("Line %d: error processing keyword %s\n",
623 /* Message digest tests */
625 typedef struct digest_data_st {
626 /* Digest this test is for */
627 const EVP_MD *digest;
628 /* Input to digest */
629 unsigned char *input;
631 /* Repeat count for input */
633 /* Expected output */
634 unsigned char *output;
638 static int digest_test_init(EVP_TEST *t, const char *alg)
640 const EVP_MD *digest;
643 digest = EVP_get_digestbyname(alg);
645 /* If alg has an OID assume disabled algorithm */
646 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
652 mdat = OPENSSL_zalloc(sizeof(*mdat));
653 mdat->digest = digest;
659 static void digest_test_cleanup(EVP_TEST *t)
661 DIGEST_DATA *mdat = t->data;
663 OPENSSL_free(mdat->input);
664 OPENSSL_free(mdat->output);
667 static int digest_test_parse(EVP_TEST *t,
668 const char *keyword, const char *value)
670 DIGEST_DATA *mdata = t->data;
672 if (strcmp(keyword, "Input") == 0)
673 return test_bin(value, &mdata->input, &mdata->input_len);
674 if (strcmp(keyword, "Output") == 0)
675 return test_bin(value, &mdata->output, &mdata->output_len);
676 if (strcmp(keyword, "Count") == 0) {
677 long nrpt = atoi(value);
680 mdata->nrpt = (size_t)nrpt;
686 static int digest_test_run(EVP_TEST *t)
688 DIGEST_DATA *mdata = t->data;
691 unsigned char md[EVP_MAX_MD_SIZE];
694 t->err = "TEST_FAILURE";
695 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
698 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL)) {
699 t->err = "DIGESTINIT_ERROR";
702 for (i = 0; i < mdata->nrpt; i++)
703 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len)) {
704 t->err = "DIGESTUPDATE_ERROR";
707 if (!EVP_DigestFinal(mctx, md, &md_len)) {
708 t->err = "DIGESTFINAL_ERROR";
711 if (md_len != mdata->output_len) {
712 t->err = "DIGEST_LENGTH_MISMATCH";
715 if (!TEST_mem_eq(mdata->output, mdata->output_len, md, md_len)) {
716 t->err = "DIGEST_MISMATCH";
722 EVP_MD_CTX_free(mctx);
726 static const EVP_TEST_METHOD digest_test_method = {
735 typedef struct cipher_data_st {
736 const EVP_CIPHER *cipher;
738 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
744 unsigned char *plaintext;
745 size_t plaintext_len;
746 unsigned char *ciphertext;
747 size_t ciphertext_len;
755 static int cipher_test_init(EVP_TEST *t, const char *alg)
757 const EVP_CIPHER *cipher;
758 CIPHER_DATA *cdat = t->data;
760 cipher = EVP_get_cipherbyname(alg);
762 /* If alg has an OID assume disabled algorithm */
763 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
769 cdat = OPENSSL_malloc(sizeof(*cdat));
770 cdat->cipher = cipher;
774 cdat->ciphertext = NULL;
775 cdat->plaintext = NULL;
779 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
780 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
781 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
782 cdat->aead = EVP_CIPHER_mode(cipher);
783 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
791 static void cipher_test_cleanup(EVP_TEST *t)
793 CIPHER_DATA *cdat = t->data;
795 OPENSSL_free(cdat->key);
796 OPENSSL_free(cdat->iv);
797 OPENSSL_free(cdat->ciphertext);
798 OPENSSL_free(cdat->plaintext);
799 OPENSSL_free(cdat->aad);
800 OPENSSL_free(cdat->tag);
803 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
806 CIPHER_DATA *cdat = t->data;
808 if (strcmp(keyword, "Key") == 0)
809 return test_bin(value, &cdat->key, &cdat->key_len);
810 if (strcmp(keyword, "IV") == 0)
811 return test_bin(value, &cdat->iv, &cdat->iv_len);
812 if (strcmp(keyword, "Plaintext") == 0)
813 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
814 if (strcmp(keyword, "Ciphertext") == 0)
815 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
817 if (strcmp(keyword, "AAD") == 0)
818 return test_bin(value, &cdat->aad, &cdat->aad_len);
819 if (strcmp(keyword, "Tag") == 0)
820 return test_bin(value, &cdat->tag, &cdat->tag_len);
823 if (strcmp(keyword, "Operation") == 0) {
824 if (strcmp(value, "ENCRYPT") == 0)
826 else if (strcmp(value, "DECRYPT") == 0)
835 static int cipher_test_enc(EVP_TEST *t, int enc,
836 size_t out_misalign, size_t inp_misalign, int frag)
838 CIPHER_DATA *cdat = t->data;
839 unsigned char *in, *out, *tmp = NULL;
840 size_t in_len, out_len, donelen = 0;
841 int ok = 0, tmplen, chunklen, tmpflen;
842 EVP_CIPHER_CTX *ctx = NULL;
844 t->err = "TEST_FAILURE";
845 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
847 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
849 in = cdat->plaintext;
850 in_len = cdat->plaintext_len;
851 out = cdat->ciphertext;
852 out_len = cdat->ciphertext_len;
854 in = cdat->ciphertext;
855 in_len = cdat->ciphertext_len;
856 out = cdat->plaintext;
857 out_len = cdat->plaintext_len;
859 if (inp_misalign == (size_t)-1) {
861 * Exercise in-place encryption
863 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
866 in = memcpy(tmp + out_misalign, in, in_len);
868 inp_misalign += 16 - ((out_misalign + in_len) & 15);
870 * 'tmp' will store both output and copy of input. We make the copy
871 * of input to specifically aligned part of 'tmp'. So we just
872 * figured out how much padding would ensure the required alignment,
873 * now we allocate extended buffer and finally copy the input just
874 * past inp_misalign in expression below. Output will be written
875 * past out_misalign...
877 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
878 inp_misalign + in_len);
881 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
882 inp_misalign, in, in_len);
884 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc)) {
885 t->err = "CIPHERINIT_ERROR";
890 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
892 t->err = "INVALID_IV_LENGTH";
895 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
896 t->err = "INVALID_IV_LENGTH";
903 * If encrypting or OCB just set tag length initially, otherwise
904 * set tag length and value.
906 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
907 t->err = "TAG_LENGTH_SET_ERROR";
910 t->err = "TAG_SET_ERROR";
913 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
914 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
920 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len)) {
921 t->err = "INVALID_KEY_LENGTH";
924 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1)) {
925 t->err = "KEY_SET_ERROR";
929 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
930 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
931 cdat->tag_len, cdat->tag)) {
932 t->err = "TAG_SET_ERROR";
937 if (cdat->aead == EVP_CIPH_CCM_MODE) {
938 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
939 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
944 t->err = "AAD_SET_ERROR";
946 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
951 * Supply the AAD in chunks less than the block size where possible
953 if (cdat->aad_len > 0) {
954 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
958 if (cdat->aad_len > 2) {
959 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
962 donelen += cdat->aad_len - 2;
964 if (cdat->aad_len > 1
965 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
966 cdat->aad + donelen, 1))
970 EVP_CIPHER_CTX_set_padding(ctx, 0);
971 t->err = "CIPHERUPDATE_ERROR";
974 /* We supply the data all in one go */
975 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
978 /* Supply the data in chunks less than the block size where possible */
980 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
987 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
995 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1001 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
1002 t->err = "CIPHERFINAL_ERROR";
1005 if (!TEST_mem_eq(out, out_len, tmp + out_misalign, tmplen + tmpflen)) {
1006 t->err = "VALUE_MISMATCH";
1009 if (enc && cdat->aead) {
1010 unsigned char rtag[16];
1012 if (cdat->tag_len > sizeof(rtag)) {
1013 t->err = "TAG_LENGTH_INTERNAL_ERROR";
1016 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1017 cdat->tag_len, rtag)) {
1018 t->err = "TAG_RETRIEVE_ERROR";
1021 if (!TEST_mem_eq(cdat->tag, cdat->tag_len, rtag, cdat->tag_len)) {
1022 t->err = "TAG_VALUE_MISMATCH";
1030 EVP_CIPHER_CTX_free(ctx);
1034 static int cipher_test_run(EVP_TEST *t)
1036 CIPHER_DATA *cdat = t->data;
1038 size_t out_misalign, inp_misalign;
1044 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1045 /* IV is optional and usually omitted in wrap mode */
1046 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1051 if (cdat->aead && !cdat->tag) {
1055 for (out_misalign = 0; out_misalign <= 1;) {
1056 static char aux_err[64];
1057 t->aux_err = aux_err;
1058 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1059 if (inp_misalign == (size_t)-1) {
1060 /* kludge: inp_misalign == -1 means "exercise in-place" */
1061 BIO_snprintf(aux_err, sizeof(aux_err),
1062 "%s in-place, %sfragmented",
1063 out_misalign ? "misaligned" : "aligned",
1064 frag ? "" : "not ");
1066 BIO_snprintf(aux_err, sizeof(aux_err),
1067 "%s output and %s input, %sfragmented",
1068 out_misalign ? "misaligned" : "aligned",
1069 inp_misalign ? "misaligned" : "aligned",
1070 frag ? "" : "not ");
1073 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1074 /* Not fatal errors: return */
1081 if (cdat->enc != 1) {
1082 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1083 /* Not fatal errors: return */
1092 if (out_misalign == 1 && frag == 0) {
1094 * XTS, CCM and Wrap modes have special requirements about input
1095 * lengths so we don't fragment for those
1097 if (cdat->aead == EVP_CIPH_CCM_MODE
1098 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1099 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1112 static const EVP_TEST_METHOD cipher_test_method = {
1115 cipher_test_cleanup,
1120 typedef struct mac_data_st {
1123 /* Algorithm string for this MAC */
1129 unsigned char *input;
1131 /* Expected output */
1132 unsigned char *output;
1136 static int mac_test_init(EVP_TEST *t, const char *alg)
1141 if (strcmp(alg, "HMAC") == 0) {
1142 type = EVP_PKEY_HMAC;
1143 } else if (strcmp(alg, "CMAC") == 0) {
1144 #ifndef OPENSSL_NO_CMAC
1145 type = EVP_PKEY_CMAC;
1150 } else if (strcmp(alg, "Poly1305") == 0) {
1151 #ifndef OPENSSL_NO_POLY1305
1152 type = EVP_PKEY_POLY1305;
1157 } else if (strcmp(alg, "SipHash") == 0) {
1158 #ifndef OPENSSL_NO_SIPHASH
1159 type = EVP_PKEY_SIPHASH;
1167 mdat = OPENSSL_zalloc(sizeof(*mdat));
1173 static void mac_test_cleanup(EVP_TEST *t)
1175 MAC_DATA *mdat = t->data;
1177 OPENSSL_free(mdat->alg);
1178 OPENSSL_free(mdat->key);
1179 OPENSSL_free(mdat->input);
1180 OPENSSL_free(mdat->output);
1183 static int mac_test_parse(EVP_TEST *t,
1184 const char *keyword, const char *value)
1186 MAC_DATA *mdata = t->data;
1188 if (strcmp(keyword, "Key") == 0)
1189 return test_bin(value, &mdata->key, &mdata->key_len);
1190 if (strcmp(keyword, "Algorithm") == 0) {
1191 mdata->alg = OPENSSL_strdup(value);
1196 if (strcmp(keyword, "Input") == 0)
1197 return test_bin(value, &mdata->input, &mdata->input_len);
1198 if (strcmp(keyword, "Output") == 0)
1199 return test_bin(value, &mdata->output, &mdata->output_len);
1203 static int mac_test_run(EVP_TEST *t)
1205 MAC_DATA *mdata = t->data;
1206 EVP_MD_CTX *mctx = NULL;
1207 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1208 EVP_PKEY *key = NULL;
1209 const EVP_MD *md = NULL;
1210 unsigned char *mac = NULL;
1213 #ifdef OPENSSL_NO_DES
1214 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1221 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL))) {
1222 t->err = "MAC_PKEY_CTX_ERROR";
1226 if (EVP_PKEY_keygen_init(genctx) <= 0) {
1227 t->err = "MAC_KEYGEN_INIT_ERROR";
1230 if (mdata->type == EVP_PKEY_CMAC
1231 && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0) {
1232 t->err = "MAC_ALGORITHM_SET_ERROR";
1236 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0) {
1237 t->err = "MAC_KEY_SET_ERROR";
1241 if (EVP_PKEY_keygen(genctx, &key) <= 0) {
1242 t->err = "MAC_KEY_GENERATE_ERROR";
1245 if (mdata->type == EVP_PKEY_HMAC) {
1246 if (!TEST_ptr(md = EVP_get_digestbyname(mdata->alg))) {
1247 t->err = "MAC_ALGORITHM_SET_ERROR";
1251 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1252 t->err = "INTERNAL_ERROR";
1255 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1256 t->err = "DIGESTSIGNINIT_ERROR";
1260 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len)) {
1261 t->err = "DIGESTSIGNUPDATE_ERROR";
1264 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len)) {
1265 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1268 if (!TEST_ptr(mac = OPENSSL_malloc(mac_len))) {
1269 t->err = "TEST_FAILURE";
1272 if (!EVP_DigestSignFinal(mctx, mac, &mac_len)
1273 || !TEST_mem_eq(mdata->output, mdata->output_len, mac, mac_len)) {
1274 t->err = "TEST_MAC_ERR";
1280 EVP_MD_CTX_free(mctx);
1282 EVP_PKEY_CTX_free(genctx);
1287 static const EVP_TEST_METHOD mac_test_method = {
1296 * Public key operations. These are all very similar and can share
1297 * a lot of common code.
1300 typedef struct pkey_data_st {
1301 /* Context for this operation */
1303 /* Key operation to perform */
1304 int (*keyop) (EVP_PKEY_CTX *ctx,
1305 unsigned char *sig, size_t *siglen,
1306 const unsigned char *tbs, size_t tbslen);
1308 unsigned char *input;
1310 /* Expected output */
1311 unsigned char *output;
1316 * Perform public key operation setup: lookup key, allocated ctx and call
1317 * the appropriate initialisation function
1319 static int pkey_test_init(EVP_TEST *t, const char *name,
1321 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1322 int (*keyop) (EVP_PKEY_CTX *ctx,
1323 unsigned char *sig, size_t *siglen,
1324 const unsigned char *tbs,
1329 EVP_PKEY *pkey = NULL;
1333 rv = find_key(&pkey, name, public_keys);
1335 rv = find_key(&pkey, name, private_keys);
1336 if (rv == 0 || pkey == NULL) {
1341 if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
1342 EVP_PKEY_free(pkey);
1346 kdata->input = NULL;
1347 kdata->output = NULL;
1348 kdata->keyop = keyop;
1350 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1352 if (keyopinit(kdata->ctx) <= 0)
1353 t->err = "KEYOP_INIT_ERROR";
1357 static void pkey_test_cleanup(EVP_TEST *t)
1359 PKEY_DATA *kdata = t->data;
1361 OPENSSL_free(kdata->input);
1362 OPENSSL_free(kdata->output);
1363 EVP_PKEY_CTX_free(kdata->ctx);
1366 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1372 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1374 p = strchr(tmpval, ':');
1377 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1379 t->err = "PKEY_CTRL_INVALID";
1381 } else if (p != NULL && rv <= 0) {
1382 /* If p has an OID and lookup fails assume disabled algorithm */
1383 int nid = OBJ_sn2nid(p);
1385 if (nid == NID_undef)
1386 nid = OBJ_ln2nid(p);
1387 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1388 EVP_get_cipherbynid(nid) == NULL) {
1392 t->err = "PKEY_CTRL_ERROR";
1396 OPENSSL_free(tmpval);
1400 static int pkey_test_parse(EVP_TEST *t,
1401 const char *keyword, const char *value)
1403 PKEY_DATA *kdata = t->data;
1404 if (strcmp(keyword, "Input") == 0)
1405 return test_bin(value, &kdata->input, &kdata->input_len);
1406 if (strcmp(keyword, "Output") == 0)
1407 return test_bin(value, &kdata->output, &kdata->output_len);
1408 if (strcmp(keyword, "Ctrl") == 0)
1409 return pkey_test_ctrl(t, kdata->ctx, value);
1413 static int pkey_test_run(EVP_TEST *t)
1415 PKEY_DATA *kdata = t->data;
1416 unsigned char *out = NULL;
1419 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1420 kdata->input_len) <= 0
1421 || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
1422 t->err = "KEYOP_LENGTH_ERROR";
1425 if (kdata->keyop(kdata->ctx, out,
1426 &out_len, kdata->input, kdata->input_len) <= 0) {
1427 t->err = "KEYOP_ERROR";
1430 if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1431 t->err = "KEYOP_MISMATCH";
1440 static int sign_test_init(EVP_TEST *t, const char *name)
1442 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1445 static const EVP_TEST_METHOD psign_test_method = {
1453 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1455 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1456 EVP_PKEY_verify_recover);
1459 static const EVP_TEST_METHOD pverify_recover_test_method = {
1461 verify_recover_test_init,
1467 static int decrypt_test_init(EVP_TEST *t, const char *name)
1469 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1473 static const EVP_TEST_METHOD pdecrypt_test_method = {
1481 static int verify_test_init(EVP_TEST *t, const char *name)
1483 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1486 static int verify_test_run(EVP_TEST *t)
1488 PKEY_DATA *kdata = t->data;
1490 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1491 kdata->input, kdata->input_len) <= 0)
1492 t->err = "VERIFY_ERROR";
1496 static const EVP_TEST_METHOD pverify_test_method = {
1505 static int pderive_test_init(EVP_TEST *t, const char *name)
1507 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1510 static int pderive_test_parse(EVP_TEST *t,
1511 const char *keyword, const char *value)
1513 PKEY_DATA *kdata = t->data;
1515 if (strcmp(keyword, "PeerKey") == 0) {
1517 if (find_key(&peer, value, public_keys) == 0)
1519 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1523 if (strcmp(keyword, "SharedSecret") == 0)
1524 return test_bin(value, &kdata->output, &kdata->output_len);
1525 if (strcmp(keyword, "Ctrl") == 0)
1526 return pkey_test_ctrl(t, kdata->ctx, value);
1530 static int pderive_test_run(EVP_TEST *t)
1532 PKEY_DATA *kdata = t->data;
1533 unsigned char *out = NULL;
1536 out_len = kdata->output_len;
1537 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1538 t->err = "DERIVE_ERROR";
1541 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1542 t->err = "DERIVE_ERROR";
1545 if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1546 t->err = "SHARED_SECRET_MISMATCH";
1556 static const EVP_TEST_METHOD pderive_test_method = {
1566 #define PBE_TYPE_SCRYPT 1
1567 #define PBE_TYPE_PBKDF2 2
1568 #define PBE_TYPE_PKCS12 3
1570 typedef struct pbe_data_st {
1572 /* scrypt parameters */
1573 uint64_t N, r, p, maxmem;
1574 /* PKCS#12 parameters */
1578 unsigned char *pass;
1581 unsigned char *salt;
1583 /* Expected output */
1588 #ifndef OPENSSL_NO_SCRYPT
1589 static int scrypt_test_parse(EVP_TEST *t,
1590 const char *keyword, const char *value)
1592 PBE_DATA *pdata = t->data;
1594 if (strcmp(keyword, "N") == 0)
1595 return test_uint64(value, &pdata->N);
1596 if (strcmp(keyword, "p") == 0)
1597 return test_uint64(value, &pdata->p);
1598 if (strcmp(keyword, "r") == 0)
1599 return test_uint64(value, &pdata->r);
1600 if (strcmp(keyword, "maxmem") == 0)
1601 return test_uint64(value, &pdata->maxmem);
1606 static int pbkdf2_test_parse(EVP_TEST *t,
1607 const char *keyword, const char *value)
1609 PBE_DATA *pdata = t->data;
1611 if (strcmp(keyword, "iter") == 0) {
1612 pdata->iter = atoi(value);
1613 if (pdata->iter <= 0)
1617 if (strcmp(keyword, "MD") == 0) {
1618 pdata->md = EVP_get_digestbyname(value);
1619 if (pdata->md == NULL)
1626 static int pkcs12_test_parse(EVP_TEST *t,
1627 const char *keyword, const char *value)
1629 PBE_DATA *pdata = t->data;
1631 if (strcmp(keyword, "id") == 0) {
1632 pdata->id = atoi(value);
1637 return pbkdf2_test_parse(t, keyword, value);
1640 static int pbe_test_init(EVP_TEST *t, const char *alg)
1645 if (strcmp(alg, "scrypt") == 0) {
1646 #ifndef OPENSSL_NO_SCRYPT
1647 pbe_type = PBE_TYPE_SCRYPT;
1652 } else if (strcmp(alg, "pbkdf2") == 0) {
1653 pbe_type = PBE_TYPE_PBKDF2;
1654 } else if (strcmp(alg, "pkcs12") == 0) {
1655 pbe_type = PBE_TYPE_PKCS12;
1657 TEST_error("Unknown pbe algorithm %s", alg);
1659 pdat = OPENSSL_malloc(sizeof(*pdat));
1660 pdat->pbe_type = pbe_type;
1674 static void pbe_test_cleanup(EVP_TEST *t)
1676 PBE_DATA *pdat = t->data;
1678 OPENSSL_free(pdat->pass);
1679 OPENSSL_free(pdat->salt);
1680 OPENSSL_free(pdat->key);
1683 static int pbe_test_parse(EVP_TEST *t,
1684 const char *keyword, const char *value)
1686 PBE_DATA *pdata = t->data;
1688 if (strcmp(keyword, "Password") == 0)
1689 return test_bin(value, &pdata->pass, &pdata->pass_len);
1690 if (strcmp(keyword, "Salt") == 0)
1691 return test_bin(value, &pdata->salt, &pdata->salt_len);
1692 if (strcmp(keyword, "Key") == 0)
1693 return test_bin(value, &pdata->key, &pdata->key_len);
1694 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1695 return pbkdf2_test_parse(t, keyword, value);
1696 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1697 return pkcs12_test_parse(t, keyword, value);
1698 #ifndef OPENSSL_NO_SCRYPT
1699 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1700 return scrypt_test_parse(t, keyword, value);
1705 static int pbe_test_run(EVP_TEST *t)
1707 PBE_DATA *pdata = t->data;
1710 if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
1711 t->err = "INTERNAL_ERROR";
1714 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1715 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1716 pdata->salt, pdata->salt_len,
1717 pdata->iter, pdata->md,
1718 pdata->key_len, key) == 0) {
1719 t->err = "PBKDF2_ERROR";
1722 #ifndef OPENSSL_NO_SCRYPT
1723 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1724 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1725 pdata->salt, pdata->salt_len,
1726 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1727 key, pdata->key_len) == 0) {
1728 t->err = "SCRYPT_ERROR";
1732 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1733 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1734 pdata->salt, pdata->salt_len,
1735 pdata->id, pdata->iter, pdata->key_len,
1736 key, pdata->md) == 0) {
1737 t->err = "PKCS12_ERROR";
1741 if (!TEST_mem_eq(pdata->key, pdata->key_len, key, pdata->key_len)) {
1742 t->err = "KEY_MISMATCH";
1751 static const EVP_TEST_METHOD pbe_test_method = {
1762 BASE64_CANONICAL_ENCODING = 0,
1763 BASE64_VALID_ENCODING = 1,
1764 BASE64_INVALID_ENCODING = 2
1765 } base64_encoding_type;
1767 typedef struct encode_data_st {
1768 /* Input to encoding */
1769 unsigned char *input;
1771 /* Expected output */
1772 unsigned char *output;
1774 base64_encoding_type encoding;
1777 static int encode_test_init(EVP_TEST *t, const char *encoding)
1779 ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
1781 if (strcmp(encoding, "canonical") == 0) {
1782 edata->encoding = BASE64_CANONICAL_ENCODING;
1783 } else if (strcmp(encoding, "valid") == 0) {
1784 edata->encoding = BASE64_VALID_ENCODING;
1785 } else if (strcmp(encoding, "invalid") == 0) {
1786 edata->encoding = BASE64_INVALID_ENCODING;
1787 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1788 if (t->expected_err == NULL)
1791 TEST_info("Bad encoding: %s. Should be one of "
1792 "{canonical, valid, invalid}", encoding);
1799 static void encode_test_cleanup(EVP_TEST *t)
1801 ENCODE_DATA *edata = t->data;
1803 OPENSSL_free(edata->input);
1804 OPENSSL_free(edata->output);
1805 memset(edata, 0, sizeof(*edata));
1808 static int encode_test_parse(EVP_TEST *t,
1809 const char *keyword, const char *value)
1811 ENCODE_DATA *edata = t->data;
1812 if (strcmp(keyword, "Input") == 0)
1813 return test_bin(value, &edata->input, &edata->input_len);
1814 if (strcmp(keyword, "Output") == 0)
1815 return test_bin(value, &edata->output, &edata->output_len);
1819 static int encode_test_run(EVP_TEST *t)
1821 ENCODE_DATA *edata = t->data;
1822 unsigned char *encode_out = NULL, *decode_out = NULL;
1823 int output_len, chunk_len;
1824 EVP_ENCODE_CTX *decode_ctx;
1826 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1827 t->err = "INTERNAL_ERROR";
1831 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1832 EVP_ENCODE_CTX *encode_ctx;
1834 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1835 || !TEST_ptr(encode_out =
1836 OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
1839 EVP_EncodeInit(encode_ctx);
1840 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1841 edata->input, edata->input_len);
1842 output_len = chunk_len;
1844 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1845 output_len += chunk_len;
1847 EVP_ENCODE_CTX_free(encode_ctx);
1849 if (!TEST_mem_eq(edata->output, edata->output_len,
1850 encode_out, output_len)) {
1851 t->err = "BAD_ENCODING";
1856 if (!TEST_ptr(decode_out =
1857 OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
1860 EVP_DecodeInit(decode_ctx);
1861 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1862 edata->output_len) < 0) {
1863 t->err = "DECODE_ERROR";
1866 output_len = chunk_len;
1868 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1869 t->err = "DECODE_ERROR";
1872 output_len += chunk_len;
1874 if (edata->encoding != BASE64_INVALID_ENCODING
1875 && !TEST_mem_eq(edata->input, edata->input_len,
1876 decode_out, output_len)) {
1877 t->err = "BAD_DECODING";
1883 OPENSSL_free(encode_out);
1884 OPENSSL_free(decode_out);
1885 EVP_ENCODE_CTX_free(decode_ctx);
1889 static const EVP_TEST_METHOD encode_test_method = {
1892 encode_test_cleanup,
1897 /* KDF operations */
1899 typedef struct kdf_data_st {
1900 /* Context for this operation */
1902 /* Expected output */
1903 unsigned char *output;
1908 * Perform public key operation setup: lookup key, allocated ctx and call
1909 * the appropriate initialisation function
1911 static int kdf_test_init(EVP_TEST *t, const char *name)
1915 kdata = OPENSSL_malloc(sizeof(*kdata));
1919 kdata->output = NULL;
1921 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1922 if (kdata->ctx == NULL)
1924 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1929 static void kdf_test_cleanup(EVP_TEST *t)
1931 KDF_DATA *kdata = t->data;
1932 OPENSSL_free(kdata->output);
1933 EVP_PKEY_CTX_free(kdata->ctx);
1936 static int kdf_test_parse(EVP_TEST *t,
1937 const char *keyword, const char *value)
1939 KDF_DATA *kdata = t->data;
1941 if (strcmp(keyword, "Output") == 0)
1942 return test_bin(value, &kdata->output, &kdata->output_len);
1943 if (strncmp(keyword, "Ctrl", 4) == 0)
1944 return pkey_test_ctrl(t, kdata->ctx, value);
1948 static int kdf_test_run(EVP_TEST *t)
1950 KDF_DATA *kdata = t->data;
1951 unsigned char *out = NULL;
1952 size_t out_len = kdata->output_len;
1954 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1955 t->err = "INTERNAL_ERROR";
1958 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1959 t->err = "KDF_DERIVE_ERROR";
1962 if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1963 t->err = "KDF_MISMATCH";
1973 static const EVP_TEST_METHOD kdf_test_method = {
1981 typedef struct keypair_test_data_st {
1984 } KEYPAIR_TEST_DATA;
1986 static int keypair_test_init(EVP_TEST *t, const char *pair)
1989 EVP_PKEY *pk = NULL, *pubk = NULL;
1990 char *pub, *priv = NULL;
1991 KEYPAIR_TEST_DATA *data;
1993 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1994 || !TEST_ptr(pub = strchr(priv, ':'))) {
1995 t->err = "PARSING_ERROR";
1998 *pub++ = 0; /* split priv and pub strings */
2000 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2001 TEST_info("Cannot find private key: %s", priv);
2002 t->err = "MISSING_PRIVATE_KEY";
2005 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2006 TEST_info("Cannot find public key: %s", pub);
2007 t->err = "MISSING_PUBLIC_KEY";
2011 if (pk == NULL && pubk == NULL) {
2012 /* Both keys are listed but unsupported: skip this test */
2018 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2032 static void keypair_test_cleanup(EVP_TEST *t)
2034 OPENSSL_free(t->data);
2038 /* For test that do not accept any custom keyword:
2039 * return 0 if called
2041 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2046 static int keypair_test_run(EVP_TEST *t)
2049 const KEYPAIR_TEST_DATA *pair = t->data;
2051 if (pair->privk == NULL || pair->pubk == NULL) {
2053 * this can only happen if only one of the keys is not set
2054 * which means that one of them was unsupported while the
2055 * other isn't: hence a key type mismatch.
2057 t->err = "KEYPAIR_TYPE_MISMATCH";
2062 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2064 t->err = "KEYPAIR_MISMATCH";
2065 } else if ( -1 == rv ) {
2066 t->err = "KEYPAIR_TYPE_MISMATCH";
2067 } else if ( -2 == rv ) {
2068 t->err = "UNSUPPORTED_KEY_COMPARISON";
2070 TEST_error("Unexpected error in key comparison");
2085 static const EVP_TEST_METHOD keypair_test_method = {
2088 keypair_test_cleanup,
2093 static int do_test_file(const char *testfile)
2099 if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2101 memset(&t, 0, sizeof(t));
2105 while (BIO_gets(in, buf, sizeof(buf))) {
2107 if (!TEST_true(parse_test_line(&t, buf)))
2110 /* Run any final test we have */
2111 if (!run_and_get_next(&t, NULL))
2114 TEST_info("Completed %d tests with %d errors and %d skipped",
2115 t.ntests, t.errors, t.nskip);
2116 free_key_list(public_keys);
2117 free_key_list(private_keys);
2120 return t.errors == 0;
2123 static char * const *testfiles;
2125 static int run_file_tests(int i)
2127 return do_test_file(testfiles[i]);
2130 int test_main(int argc, char *argv[])
2133 TEST_error("Usage: %s file...", argv[0]);
2136 testfiles = &argv[1];
2138 ADD_ALL_TESTS(run_file_tests, argc - 1);
2140 return run_tests(argv[0]);