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);
423 if (t->err == NULL && t->meth->run_test(t) != 1) {
424 TEST_info("Line %d error %s", t->start_line, t->meth->name);
427 if (!check_test_error(t)) {
429 ERR_print_errors_fp(stderr);
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 || !EVP_DigestSignFinal(mctx, mac, &mac_len)
1270 || !TEST_mem_eq(mdata->output, mdata->output_len, mac, mac_len)) {
1271 t->err = "TEST_FAILURE";
1277 EVP_MD_CTX_free(mctx);
1279 EVP_PKEY_CTX_free(genctx);
1284 static const EVP_TEST_METHOD mac_test_method = {
1293 * Public key operations. These are all very similar and can share
1294 * a lot of common code.
1297 typedef struct pkey_data_st {
1298 /* Context for this operation */
1300 /* Key operation to perform */
1301 int (*keyop) (EVP_PKEY_CTX *ctx,
1302 unsigned char *sig, size_t *siglen,
1303 const unsigned char *tbs, size_t tbslen);
1305 unsigned char *input;
1307 /* Expected output */
1308 unsigned char *output;
1313 * Perform public key operation setup: lookup key, allocated ctx and call
1314 * the appropriate initialisation function
1316 static int pkey_test_init(EVP_TEST *t, const char *name,
1318 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1319 int (*keyop) (EVP_PKEY_CTX *ctx,
1320 unsigned char *sig, size_t *siglen,
1321 const unsigned char *tbs,
1326 EVP_PKEY *pkey = NULL;
1330 rv = find_key(&pkey, name, public_keys);
1332 rv = find_key(&pkey, name, private_keys);
1333 if (rv == 0 || pkey == NULL) {
1338 if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
1339 EVP_PKEY_free(pkey);
1343 kdata->input = NULL;
1344 kdata->output = NULL;
1345 kdata->keyop = keyop;
1347 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1349 if (keyopinit(kdata->ctx) <= 0)
1350 t->err = "KEYOP_INIT_ERROR";
1354 static void pkey_test_cleanup(EVP_TEST *t)
1356 PKEY_DATA *kdata = t->data;
1358 OPENSSL_free(kdata->input);
1359 OPENSSL_free(kdata->output);
1360 EVP_PKEY_CTX_free(kdata->ctx);
1363 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1369 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1371 p = strchr(tmpval, ':');
1374 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1376 t->err = "PKEY_CTRL_INVALID";
1378 } else if (p != NULL && rv <= 0) {
1379 /* If p has an OID and lookup fails assume disabled algorithm */
1380 int nid = OBJ_sn2nid(p);
1382 if (nid == NID_undef)
1383 nid = OBJ_ln2nid(p);
1384 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1385 EVP_get_cipherbynid(nid) == NULL) {
1389 t->err = "PKEY_CTRL_ERROR";
1393 OPENSSL_free(tmpval);
1397 static int pkey_test_parse(EVP_TEST *t,
1398 const char *keyword, const char *value)
1400 PKEY_DATA *kdata = t->data;
1401 if (strcmp(keyword, "Input") == 0)
1402 return test_bin(value, &kdata->input, &kdata->input_len);
1403 if (strcmp(keyword, "Output") == 0)
1404 return test_bin(value, &kdata->output, &kdata->output_len);
1405 if (strcmp(keyword, "Ctrl") == 0)
1406 return pkey_test_ctrl(t, kdata->ctx, value);
1410 static int pkey_test_run(EVP_TEST *t)
1412 PKEY_DATA *kdata = t->data;
1413 unsigned char *out = NULL;
1416 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1417 kdata->input_len) <= 0
1418 || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
1419 t->err = "KEYOP_LENGTH_ERROR";
1422 if (kdata->keyop(kdata->ctx, out,
1423 &out_len, kdata->input, kdata->input_len) <= 0) {
1424 t->err = "KEYOP_ERROR";
1427 if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1428 t->err = "KEYOP_MISMATCH";
1437 static int sign_test_init(EVP_TEST *t, const char *name)
1439 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1442 static const EVP_TEST_METHOD psign_test_method = {
1450 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1452 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1453 EVP_PKEY_verify_recover);
1456 static const EVP_TEST_METHOD pverify_recover_test_method = {
1458 verify_recover_test_init,
1464 static int decrypt_test_init(EVP_TEST *t, const char *name)
1466 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1470 static const EVP_TEST_METHOD pdecrypt_test_method = {
1478 static int verify_test_init(EVP_TEST *t, const char *name)
1480 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1483 static int verify_test_run(EVP_TEST *t)
1485 PKEY_DATA *kdata = t->data;
1487 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1488 kdata->input, kdata->input_len) <= 0)
1489 t->err = "VERIFY_ERROR";
1493 static const EVP_TEST_METHOD pverify_test_method = {
1502 static int pderive_test_init(EVP_TEST *t, const char *name)
1504 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1507 static int pderive_test_parse(EVP_TEST *t,
1508 const char *keyword, const char *value)
1510 PKEY_DATA *kdata = t->data;
1512 if (strcmp(keyword, "PeerKey") == 0) {
1514 if (find_key(&peer, value, public_keys) == 0)
1516 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1520 if (strcmp(keyword, "SharedSecret") == 0)
1521 return test_bin(value, &kdata->output, &kdata->output_len);
1522 if (strcmp(keyword, "Ctrl") == 0)
1523 return pkey_test_ctrl(t, kdata->ctx, value);
1527 static int pderive_test_run(EVP_TEST *t)
1529 PKEY_DATA *kdata = t->data;
1530 unsigned char *out = NULL;
1533 out_len = kdata->output_len;
1534 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1535 t->err = "DERIVE_ERROR";
1538 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1539 t->err = "DERIVE_ERROR";
1542 if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1543 t->err = "SHARED_SECRET_MISMATCH";
1553 static const EVP_TEST_METHOD pderive_test_method = {
1563 #define PBE_TYPE_SCRYPT 1
1564 #define PBE_TYPE_PBKDF2 2
1565 #define PBE_TYPE_PKCS12 3
1567 typedef struct pbe_data_st {
1569 /* scrypt parameters */
1570 uint64_t N, r, p, maxmem;
1571 /* PKCS#12 parameters */
1575 unsigned char *pass;
1578 unsigned char *salt;
1580 /* Expected output */
1585 #ifndef OPENSSL_NO_SCRYPT
1586 static int scrypt_test_parse(EVP_TEST *t,
1587 const char *keyword, const char *value)
1589 PBE_DATA *pdata = t->data;
1591 if (strcmp(keyword, "N") == 0)
1592 return test_uint64(value, &pdata->N);
1593 if (strcmp(keyword, "p") == 0)
1594 return test_uint64(value, &pdata->p);
1595 if (strcmp(keyword, "r") == 0)
1596 return test_uint64(value, &pdata->r);
1597 if (strcmp(keyword, "maxmem") == 0)
1598 return test_uint64(value, &pdata->maxmem);
1603 static int pbkdf2_test_parse(EVP_TEST *t,
1604 const char *keyword, const char *value)
1606 PBE_DATA *pdata = t->data;
1608 if (strcmp(keyword, "iter") == 0) {
1609 pdata->iter = atoi(value);
1610 if (pdata->iter <= 0)
1614 if (strcmp(keyword, "MD") == 0) {
1615 pdata->md = EVP_get_digestbyname(value);
1616 if (pdata->md == NULL)
1623 static int pkcs12_test_parse(EVP_TEST *t,
1624 const char *keyword, const char *value)
1626 PBE_DATA *pdata = t->data;
1628 if (strcmp(keyword, "id") == 0) {
1629 pdata->id = atoi(value);
1634 return pbkdf2_test_parse(t, keyword, value);
1637 static int pbe_test_init(EVP_TEST *t, const char *alg)
1642 if (strcmp(alg, "scrypt") == 0) {
1643 #ifndef OPENSSL_NO_SCRYPT
1644 pbe_type = PBE_TYPE_SCRYPT;
1649 } else if (strcmp(alg, "pbkdf2") == 0) {
1650 pbe_type = PBE_TYPE_PBKDF2;
1651 } else if (strcmp(alg, "pkcs12") == 0) {
1652 pbe_type = PBE_TYPE_PKCS12;
1654 TEST_error("Unknown pbe algorithm %s", alg);
1656 pdat = OPENSSL_malloc(sizeof(*pdat));
1657 pdat->pbe_type = pbe_type;
1671 static void pbe_test_cleanup(EVP_TEST *t)
1673 PBE_DATA *pdat = t->data;
1675 OPENSSL_free(pdat->pass);
1676 OPENSSL_free(pdat->salt);
1677 OPENSSL_free(pdat->key);
1680 static int pbe_test_parse(EVP_TEST *t,
1681 const char *keyword, const char *value)
1683 PBE_DATA *pdata = t->data;
1685 if (strcmp(keyword, "Password") == 0)
1686 return test_bin(value, &pdata->pass, &pdata->pass_len);
1687 if (strcmp(keyword, "Salt") == 0)
1688 return test_bin(value, &pdata->salt, &pdata->salt_len);
1689 if (strcmp(keyword, "Key") == 0)
1690 return test_bin(value, &pdata->key, &pdata->key_len);
1691 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1692 return pbkdf2_test_parse(t, keyword, value);
1693 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1694 return pkcs12_test_parse(t, keyword, value);
1695 #ifndef OPENSSL_NO_SCRYPT
1696 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1697 return scrypt_test_parse(t, keyword, value);
1702 static int pbe_test_run(EVP_TEST *t)
1704 PBE_DATA *pdata = t->data;
1707 if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
1708 t->err = "INTERNAL_ERROR";
1711 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1712 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1713 pdata->salt, pdata->salt_len,
1714 pdata->iter, pdata->md,
1715 pdata->key_len, key) == 0) {
1716 t->err = "PBKDF2_ERROR";
1719 #ifndef OPENSSL_NO_SCRYPT
1720 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1721 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1722 pdata->salt, pdata->salt_len,
1723 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1724 key, pdata->key_len) == 0) {
1725 t->err = "SCRYPT_ERROR";
1729 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1730 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1731 pdata->salt, pdata->salt_len,
1732 pdata->id, pdata->iter, pdata->key_len,
1733 key, pdata->md) == 0) {
1734 t->err = "PKCS12_ERROR";
1738 if (!TEST_mem_eq(pdata->key, pdata->key_len, key, pdata->key_len)) {
1739 t->err = "KEY_MISMATCH";
1748 static const EVP_TEST_METHOD pbe_test_method = {
1759 BASE64_CANONICAL_ENCODING = 0,
1760 BASE64_VALID_ENCODING = 1,
1761 BASE64_INVALID_ENCODING = 2
1762 } base64_encoding_type;
1764 typedef struct encode_data_st {
1765 /* Input to encoding */
1766 unsigned char *input;
1768 /* Expected output */
1769 unsigned char *output;
1771 base64_encoding_type encoding;
1774 static int encode_test_init(EVP_TEST *t, const char *encoding)
1776 ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
1778 if (strcmp(encoding, "canonical") == 0) {
1779 edata->encoding = BASE64_CANONICAL_ENCODING;
1780 } else if (strcmp(encoding, "valid") == 0) {
1781 edata->encoding = BASE64_VALID_ENCODING;
1782 } else if (strcmp(encoding, "invalid") == 0) {
1783 edata->encoding = BASE64_INVALID_ENCODING;
1784 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1785 if (t->expected_err == NULL)
1788 TEST_info("Bad encoding: %s. Should be one of "
1789 "{canonical, valid, invalid}", encoding);
1796 static void encode_test_cleanup(EVP_TEST *t)
1798 ENCODE_DATA *edata = t->data;
1800 OPENSSL_free(edata->input);
1801 OPENSSL_free(edata->output);
1802 memset(edata, 0, sizeof(*edata));
1805 static int encode_test_parse(EVP_TEST *t,
1806 const char *keyword, const char *value)
1808 ENCODE_DATA *edata = t->data;
1809 if (strcmp(keyword, "Input") == 0)
1810 return test_bin(value, &edata->input, &edata->input_len);
1811 if (strcmp(keyword, "Output") == 0)
1812 return test_bin(value, &edata->output, &edata->output_len);
1816 static int encode_test_run(EVP_TEST *t)
1818 ENCODE_DATA *edata = t->data;
1819 unsigned char *encode_out = NULL, *decode_out = NULL;
1820 int output_len, chunk_len;
1821 EVP_ENCODE_CTX *decode_ctx;
1823 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1824 t->err = "INTERNAL_ERROR";
1828 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1829 EVP_ENCODE_CTX *encode_ctx;
1831 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1832 || !TEST_ptr(encode_out =
1833 OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
1836 EVP_EncodeInit(encode_ctx);
1837 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1838 edata->input, edata->input_len);
1839 output_len = chunk_len;
1841 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1842 output_len += chunk_len;
1844 EVP_ENCODE_CTX_free(encode_ctx);
1846 if (!TEST_mem_eq(edata->output, edata->output_len,
1847 encode_out, output_len)) {
1848 t->err = "BAD_ENCODING";
1853 if (!TEST_ptr(decode_out =
1854 OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
1857 EVP_DecodeInit(decode_ctx);
1858 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1859 edata->output_len) < 0) {
1860 t->err = "DECODE_ERROR";
1863 output_len = chunk_len;
1865 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1866 t->err = "DECODE_ERROR";
1869 output_len += chunk_len;
1871 if (edata->encoding != BASE64_INVALID_ENCODING
1872 && !TEST_mem_eq(edata->input, edata->input_len,
1873 decode_out, output_len)) {
1874 t->err = "BAD_DECODING";
1880 OPENSSL_free(encode_out);
1881 OPENSSL_free(decode_out);
1882 EVP_ENCODE_CTX_free(decode_ctx);
1886 static const EVP_TEST_METHOD encode_test_method = {
1889 encode_test_cleanup,
1894 /* KDF operations */
1896 typedef struct kdf_data_st {
1897 /* Context for this operation */
1899 /* Expected output */
1900 unsigned char *output;
1905 * Perform public key operation setup: lookup key, allocated ctx and call
1906 * the appropriate initialisation function
1908 static int kdf_test_init(EVP_TEST *t, const char *name)
1912 kdata = OPENSSL_malloc(sizeof(*kdata));
1916 kdata->output = NULL;
1918 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1919 if (kdata->ctx == NULL)
1921 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1926 static void kdf_test_cleanup(EVP_TEST *t)
1928 KDF_DATA *kdata = t->data;
1929 OPENSSL_free(kdata->output);
1930 EVP_PKEY_CTX_free(kdata->ctx);
1933 static int kdf_test_parse(EVP_TEST *t,
1934 const char *keyword, const char *value)
1936 KDF_DATA *kdata = t->data;
1938 if (strcmp(keyword, "Output") == 0)
1939 return test_bin(value, &kdata->output, &kdata->output_len);
1940 if (strncmp(keyword, "Ctrl", 4) == 0)
1941 return pkey_test_ctrl(t, kdata->ctx, value);
1945 static int kdf_test_run(EVP_TEST *t)
1947 KDF_DATA *kdata = t->data;
1948 unsigned char *out = NULL;
1949 size_t out_len = kdata->output_len;
1951 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1952 t->err = "INTERNAL_ERROR";
1955 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1956 t->err = "KDF_DERIVE_ERROR";
1959 if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1960 t->err = "KDF_MISMATCH";
1970 static const EVP_TEST_METHOD kdf_test_method = {
1978 typedef struct keypair_test_data_st {
1981 } KEYPAIR_TEST_DATA;
1983 static int keypair_test_init(EVP_TEST *t, const char *pair)
1986 EVP_PKEY *pk = NULL, *pubk = NULL;
1987 char *pub, *priv = NULL;
1988 KEYPAIR_TEST_DATA *data;
1990 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1991 || !TEST_ptr(pub = strchr(priv, ':'))) {
1992 t->err = "PARSING_ERROR";
1995 *pub++ = 0; /* split priv and pub strings */
1997 if (!TEST_true(find_key(&pk, priv, private_keys))) {
1998 TEST_info("Cannot find private key: %s", priv);
1999 t->err = "MISSING_PRIVATE_KEY";
2002 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2003 TEST_info("Cannot find public key: %s", pub);
2004 t->err = "MISSING_PUBLIC_KEY";
2008 if (pk == NULL && pubk == NULL) {
2009 /* Both keys are listed but unsupported: skip this test */
2015 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2029 static void keypair_test_cleanup(EVP_TEST *t)
2031 OPENSSL_free(t->data);
2035 /* For test that do not accept any custom keyword:
2036 * return 0 if called
2038 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2043 static int keypair_test_run(EVP_TEST *t)
2046 const KEYPAIR_TEST_DATA *pair = t->data;
2048 if (pair->privk == NULL || pair->pubk == NULL) {
2050 * this can only happen if only one of the keys is not set
2051 * which means that one of them was unsupported while the
2052 * other isn't: hence a key type mismatch.
2054 t->err = "KEYPAIR_TYPE_MISMATCH";
2059 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2061 t->err = "KEYPAIR_MISMATCH";
2062 } else if ( -1 == rv ) {
2063 t->err = "KEYPAIR_TYPE_MISMATCH";
2064 } else if ( -2 == rv ) {
2065 t->err = "UNSUPPORTED_KEY_COMPARISON";
2067 TEST_error("Unexpected error in key comparison");
2082 static const EVP_TEST_METHOD keypair_test_method = {
2085 keypair_test_cleanup,
2090 static int do_test_file(const char *testfile)
2096 if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2098 memset(&t, 0, sizeof(t));
2102 while (BIO_gets(in, buf, sizeof(buf))) {
2104 if (!TEST_true(parse_test_line(&t, buf)))
2107 /* Run any final test we have */
2108 if (!run_and_get_next(&t, NULL))
2111 TEST_info("Completed %d tests with %d errors and %d skipped",
2112 t.ntests, t.errors, t.nskip);
2113 free_key_list(public_keys);
2114 free_key_list(private_keys);
2117 return t.errors == 0;
2120 static char * const *testfiles;
2122 static int run_file_tests(int i)
2124 return do_test_file(testfiles[i]);
2127 int test_main(int argc, char *argv[])
2130 TEST_error("Usage: %s file...", argv[0]);
2133 testfiles = &argv[1];
2135 ADD_ALL_TESTS(run_file_tests, argc - 1);
2137 return run_tests(argv[0]);