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"
22 /* Remove spaces from beginning and end of a string */
24 static void remove_space(char **pval)
26 unsigned char *p = (unsigned char *)*pval, *beginning;
31 *pval = (char *)(beginning = p);
33 p = p + strlen(*pval) - 1;
35 /* Remove trailing space */
36 while (p >= beginning && isspace(*p))
41 * Given a line of the form:
42 * name = value # comment
43 * extract name and value. NB: modifies passed buffer.
46 static int parse_line(char **pkw, char **pval, char *linebuf)
50 p = linebuf + strlen(linebuf) - 1;
53 fprintf(stderr, "FATAL: missing EOL\n");
59 p = strchr(linebuf, '#');
65 p = strchr(linebuf, '=');
76 /* Remove spaces from keyword and value */
84 * Unescape some escape sequences in string literals.
85 * Return the result in a newly allocated buffer.
86 * Currently only supports '\n'.
87 * If the input length is 0, returns a valid 1-byte buffer, but sets
90 static unsigned char* unescape(const char *input, size_t input_len,
93 unsigned char *ret, *p;
97 return OPENSSL_zalloc(1);
100 /* Escaping is non-expanding; over-allocate original size for simplicity. */
101 ret = p = OPENSSL_malloc(input_len);
105 for (i = 0; i < input_len; i++) {
106 if (input[i] == '\\') {
107 if (i == input_len - 1 || input[i+1] != 'n')
124 /* For a hex string "value" convert to a binary allocated buffer */
125 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
131 /* Check for empty value */
134 * Don't return NULL for zero length buffer.
135 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
136 * a non-NULL key buffer even if the key length is 0, in order to detect
139 *buf = OPENSSL_malloc(1);
147 /* Check for NULL literal */
148 if (strcmp(value, "NULL") == 0) {
154 /* Check for string literal */
155 if (value[0] == '"') {
158 vlen = strlen(value);
159 if (value[vlen - 1] != '"')
162 *buf = unescape(value, vlen, buflen);
168 /* Otherwise assume as hex literal and convert it to binary buffer */
169 *buf = OPENSSL_hexstr2buf(value, &len);
171 fprintf(stderr, "Value=%s\n", value);
172 ERR_print_errors_fp(stderr);
175 /* Size of input buffer means we'll never overflow */
179 #ifndef OPENSSL_NO_SCRYPT
180 /* Currently only used by scrypt tests */
181 /* Parse unsigned decimal 64 bit integer value */
182 static int test_uint64(const char *value, uint64_t *pr)
184 const char *p = value;
186 fprintf(stderr, "Invalid empty integer value\n");
191 if (*pr > UINT64_MAX/10) {
192 fprintf(stderr, "Integer string overflow value=%s\n", value);
196 if (*p < '0' || *p > '9') {
197 fprintf(stderr, "Invalid integer string value=%s\n", value);
207 /* Structure holding test information */
209 /* file being read */
211 /* temp memory BIO for reading in keys */
213 /* List of public and private keys */
214 struct key_list *private;
215 struct key_list *public;
216 /* method for this test */
217 const struct 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 /* If output mismatch expected and got value */
237 unsigned char *out_received;
238 size_t out_received_len;
239 unsigned char *out_expected;
240 size_t out_expected_len;
241 /* test specific data */
243 /* Current test should be skipped */
250 struct key_list *next;
253 /* Test method structure */
254 struct evp_test_method {
255 /* Name of test as it appears in file */
257 /* Initialise test for "alg" */
258 int (*init) (struct evp_test * t, const char *alg);
259 /* Clean up method */
260 void (*cleanup) (struct evp_test * t);
261 /* Test specific name value pair processing */
262 int (*parse) (struct evp_test * t, const char *name, const char *value);
263 /* Run the test itself */
264 int (*run_test) (struct evp_test * t);
267 static const struct evp_test_method digest_test_method, cipher_test_method;
268 static const struct evp_test_method mac_test_method;
269 static const struct evp_test_method psign_test_method, pverify_test_method;
270 static const struct evp_test_method pdecrypt_test_method;
271 static const struct evp_test_method pverify_recover_test_method;
272 static const struct evp_test_method pderive_test_method;
273 static const struct evp_test_method pbe_test_method;
274 static const struct evp_test_method encode_test_method;
275 static const struct evp_test_method kdf_test_method;
276 static const struct evp_test_method keypair_test_method;
278 static const struct evp_test_method *evp_test_list[] = {
283 &pverify_test_method,
284 &pdecrypt_test_method,
285 &pverify_recover_test_method,
286 &pderive_test_method,
290 &keypair_test_method,
294 static const struct evp_test_method *evp_find_test(const char *name)
296 const struct evp_test_method **tt;
298 for (tt = evp_test_list; *tt; tt++) {
299 if (strcmp(name, (*tt)->name) == 0)
305 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 free_expected(struct 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 OPENSSL_free(t->out_received);
324 t->out_expected = NULL;
325 t->out_received = NULL;
326 t->out_expected_len = 0;
327 t->out_received_len = 0;
332 static void print_expected(struct 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);
341 static int check_test_error(struct evp_test *t)
346 if (!t->err && !t->expected_err)
348 if (t->err && !t->expected_err) {
349 if (t->aux_err != NULL) {
350 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
351 t->start_line, t->aux_err, t->err);
353 fprintf(stderr, "Test line %d: unexpected error %s\n",
354 t->start_line, t->err);
359 if (!t->err && t->expected_err) {
360 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
361 t->start_line, t->expected_err);
365 if (strcmp(t->err, t->expected_err) != 0) {
366 fprintf(stderr, "Test line %d: expecting %s got %s\n",
367 t->start_line, t->expected_err, t->err);
371 if (t->func == NULL && t->reason == NULL)
374 if (t->func == NULL || t->reason == NULL) {
375 fprintf(stderr, "Test line %d: missing function or reason code\n",
380 err = ERR_peek_error();
382 fprintf(stderr, "Test line %d, expected error \"%s:%s\" not set\n",
383 t->start_line, t->func, t->reason);
387 func = ERR_func_error_string(err);
388 reason = ERR_reason_error_string(err);
390 if (func == NULL && reason == NULL) {
391 fprintf(stderr, "Test line %d: expected error \"%s:%s\", no strings available. Skipping...\n",
392 t->start_line, t->func, t->reason);
396 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
399 fprintf(stderr, "Test line %d: expected error \"%s:%s\", got \"%s:%s\"\n",
400 t->start_line, t->func, t->reason, func, reason);
405 /* Setup a new test, run any existing test */
407 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
409 /* If we already have a test set up run it */
416 if (t->err == NULL && t->meth->run_test(t) != 1) {
417 fprintf(stderr, "%s test error line %d\n",
418 t->meth->name, t->start_line);
421 if (!check_test_error(t)) {
423 ERR_print_errors_fp(stderr);
429 if (t->data != NULL) {
431 OPENSSL_free(t->data);
434 OPENSSL_free(t->expected_err);
435 t->expected_err = NULL;
442 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
444 for (; lst; lst = lst->next) {
445 if (strcmp(lst->name, name) == 0) {
454 static void free_key_list(struct key_list *lst)
456 while (lst != NULL) {
457 struct key_list *ltmp;
458 EVP_PKEY_free(lst->key);
459 OPENSSL_free(lst->name);
466 static int check_unsupported()
468 long err = ERR_peek_error();
469 if (ERR_GET_LIB(err) == ERR_LIB_EVP
470 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
474 #ifndef OPENSSL_NO_EC
476 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
477 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
480 if (ERR_GET_LIB(err) == ERR_LIB_EC
481 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
485 #endif /* OPENSSL_NO_EC */
490 static int read_key(struct evp_test *t)
494 t->key = BIO_new(BIO_s_mem());
495 else if (BIO_reset(t->key) <= 0)
497 if (t->key == NULL) {
498 fprintf(stderr, "Error allocating key memory BIO\n");
501 /* Read to PEM end line and place content in memory BIO */
502 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
504 if (BIO_puts(t->key, tmpbuf) <= 0) {
505 fprintf(stderr, "Error writing to key memory BIO\n");
508 if (strncmp(tmpbuf, "-----END", 8) == 0)
511 fprintf(stderr, "Can't find key end\n");
515 static int process_test(struct evp_test *t, char *buf, int verbose)
517 char *keyword = NULL, *value = NULL;
518 int rv = 0, add_key = 0;
519 struct key_list **lst = NULL, *key = NULL;
521 const struct evp_test_method *tmeth = NULL;
524 if (!parse_line(&keyword, &value, buf))
526 if (strcmp(keyword, "PrivateKey") == 0) {
529 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
530 if (pk == NULL && !check_unsupported()) {
531 fprintf(stderr, "Error reading private key %s\n", value);
532 ERR_print_errors_fp(stderr);
538 if (strcmp(keyword, "PublicKey") == 0) {
541 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
542 if (pk == NULL && !check_unsupported()) {
543 fprintf(stderr, "Error reading public key %s\n", value);
544 ERR_print_errors_fp(stderr);
550 /* If we have a key add to list */
552 if (find_key(NULL, value, *lst)) {
553 fprintf(stderr, "Duplicate key %s\n", value);
556 key = OPENSSL_malloc(sizeof(*key));
559 key->name = OPENSSL_strdup(value);
566 /* See if keyword corresponds to a test start */
567 tmeth = evp_find_test(keyword);
569 if (!setup_test(t, tmeth))
571 t->start_line = t->line;
573 if (!tmeth->init(t, value)) {
574 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
578 } else if (t->skip) {
580 } else if (strcmp(keyword, "Result") == 0) {
581 if (t->expected_err) {
582 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
585 t->expected_err = OPENSSL_strdup(value);
586 if (t->expected_err == NULL)
588 } else if (strcmp(keyword, "Function") == 0) {
589 if (t->func != NULL) {
590 fprintf(stderr, "Line %d: multiple function lines\n", t->line);
593 t->func = OPENSSL_strdup(value);
596 } else if (strcmp(keyword, "Reason") == 0) {
597 if (t->reason != NULL) {
598 fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
601 t->reason = OPENSSL_strdup(value);
602 if (t->reason == NULL)
605 /* Must be test specific line: try to parse it */
607 rv = t->meth->parse(t, keyword, value);
610 fprintf(stderr, "line %d: unexpected keyword %s\n",
614 fprintf(stderr, "line %d: error processing keyword %s\n",
622 static int check_var_length_output(struct evp_test *t,
623 const unsigned char *expected,
625 const unsigned char *received,
628 if (expected_len == received_len &&
629 memcmp(expected, received, expected_len) == 0) {
633 /* The result printing code expects a non-NULL buffer. */
634 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
635 t->out_expected_len = expected_len;
636 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
637 t->out_received_len = received_len;
638 if (t->out_expected == NULL || t->out_received == NULL) {
639 fprintf(stderr, "Memory allocation error!\n");
645 static int check_output(struct evp_test *t,
646 const unsigned char *expected,
647 const unsigned char *received,
650 return check_var_length_output(t, expected, len, received, len);
653 int main(int argc, char **argv)
660 fprintf(stderr, "usage: evp_test testfile.txt\n");
664 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
666 memset(&t, 0, sizeof(t));
668 in = BIO_new_file(argv[1], "rb");
670 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
675 while (BIO_gets(in, buf, sizeof(buf))) {
677 if (!process_test(&t, buf, 0))
680 /* Run any final test we have */
681 if (!setup_test(&t, NULL))
683 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
684 t.ntests, t.errors, t.nskip);
685 free_key_list(t.public);
686 free_key_list(t.private);
690 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
691 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
699 static void test_free(void *d)
704 /* Message digest tests */
707 /* Digest this test is for */
708 const EVP_MD *digest;
709 /* Input to digest */
710 unsigned char *input;
712 /* Repeat count for input */
714 /* Expected output */
715 unsigned char *output;
719 static int digest_test_init(struct evp_test *t, const char *alg)
721 const EVP_MD *digest;
722 struct digest_data *mdat;
723 digest = EVP_get_digestbyname(alg);
725 /* If alg has an OID assume disabled algorithm */
726 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
732 mdat = OPENSSL_malloc(sizeof(*mdat));
733 mdat->digest = digest;
741 static void digest_test_cleanup(struct evp_test *t)
743 struct digest_data *mdat = t->data;
744 test_free(mdat->input);
745 test_free(mdat->output);
748 static int digest_test_parse(struct evp_test *t,
749 const char *keyword, const char *value)
751 struct digest_data *mdata = t->data;
752 if (strcmp(keyword, "Input") == 0)
753 return test_bin(value, &mdata->input, &mdata->input_len);
754 if (strcmp(keyword, "Output") == 0)
755 return test_bin(value, &mdata->output, &mdata->output_len);
756 if (strcmp(keyword, "Count") == 0) {
757 long nrpt = atoi(value);
760 mdata->nrpt = (size_t)nrpt;
766 static int digest_test_run(struct evp_test *t)
768 struct digest_data *mdata = t->data;
770 const char *err = "INTERNAL_ERROR";
772 unsigned char md[EVP_MAX_MD_SIZE];
774 mctx = EVP_MD_CTX_new();
777 err = "DIGESTINIT_ERROR";
778 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
780 err = "DIGESTUPDATE_ERROR";
781 for (i = 0; i < mdata->nrpt; i++) {
782 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
785 err = "DIGESTFINAL_ERROR";
786 if (!EVP_DigestFinal(mctx, md, &md_len))
788 err = "DIGEST_LENGTH_MISMATCH";
789 if (md_len != mdata->output_len)
791 err = "DIGEST_MISMATCH";
792 if (check_output(t, mdata->output, md, md_len))
796 EVP_MD_CTX_free(mctx);
801 static const struct evp_test_method digest_test_method = {
811 const EVP_CIPHER *cipher;
813 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
819 unsigned char *plaintext;
820 size_t plaintext_len;
821 unsigned char *ciphertext;
822 size_t ciphertext_len;
830 static int cipher_test_init(struct evp_test *t, const char *alg)
832 const EVP_CIPHER *cipher;
833 struct cipher_data *cdat = t->data;
834 cipher = EVP_get_cipherbyname(alg);
836 /* If alg has an OID assume disabled algorithm */
837 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
843 cdat = OPENSSL_malloc(sizeof(*cdat));
844 cdat->cipher = cipher;
848 cdat->ciphertext = NULL;
849 cdat->plaintext = NULL;
853 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
854 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
855 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
856 cdat->aead = EVP_CIPHER_mode(cipher);
857 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
865 static void cipher_test_cleanup(struct evp_test *t)
867 struct cipher_data *cdat = t->data;
868 test_free(cdat->key);
870 test_free(cdat->ciphertext);
871 test_free(cdat->plaintext);
872 test_free(cdat->aad);
873 test_free(cdat->tag);
876 static int cipher_test_parse(struct evp_test *t, const char *keyword,
879 struct cipher_data *cdat = t->data;
880 if (strcmp(keyword, "Key") == 0)
881 return test_bin(value, &cdat->key, &cdat->key_len);
882 if (strcmp(keyword, "IV") == 0)
883 return test_bin(value, &cdat->iv, &cdat->iv_len);
884 if (strcmp(keyword, "Plaintext") == 0)
885 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
886 if (strcmp(keyword, "Ciphertext") == 0)
887 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
889 if (strcmp(keyword, "AAD") == 0)
890 return test_bin(value, &cdat->aad, &cdat->aad_len);
891 if (strcmp(keyword, "Tag") == 0)
892 return test_bin(value, &cdat->tag, &cdat->tag_len);
895 if (strcmp(keyword, "Operation") == 0) {
896 if (strcmp(value, "ENCRYPT") == 0)
898 else if (strcmp(value, "DECRYPT") == 0)
907 static int cipher_test_enc(struct evp_test *t, int enc,
908 size_t out_misalign, size_t inp_misalign, int frag)
910 struct cipher_data *cdat = t->data;
911 unsigned char *in, *out, *tmp = NULL;
912 size_t in_len, out_len, donelen = 0;
913 int tmplen, chunklen, tmpflen;
914 EVP_CIPHER_CTX *ctx = NULL;
916 err = "INTERNAL_ERROR";
917 ctx = EVP_CIPHER_CTX_new();
920 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
922 in = cdat->plaintext;
923 in_len = cdat->plaintext_len;
924 out = cdat->ciphertext;
925 out_len = cdat->ciphertext_len;
927 in = cdat->ciphertext;
928 in_len = cdat->ciphertext_len;
929 out = cdat->plaintext;
930 out_len = cdat->plaintext_len;
932 if (inp_misalign == (size_t)-1) {
934 * Exercise in-place encryption
936 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
939 in = memcpy(tmp + out_misalign, in, in_len);
941 inp_misalign += 16 - ((out_misalign + in_len) & 15);
943 * 'tmp' will store both output and copy of input. We make the copy
944 * of input to specifically aligned part of 'tmp'. So we just
945 * figured out how much padding would ensure the required alignment,
946 * now we allocate extended buffer and finally copy the input just
947 * past inp_misalign in expression below. Output will be written
948 * past out_misalign...
950 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
951 inp_misalign + in_len);
954 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
955 inp_misalign, in, in_len);
957 err = "CIPHERINIT_ERROR";
958 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
960 err = "INVALID_IV_LENGTH";
963 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
966 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
972 * If encrypting or OCB just set tag length initially, otherwise
973 * set tag length and value.
975 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
976 err = "TAG_LENGTH_SET_ERROR";
979 err = "TAG_SET_ERROR";
982 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
983 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
989 err = "INVALID_KEY_LENGTH";
990 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
992 err = "KEY_SET_ERROR";
993 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
996 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
997 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
998 cdat->tag_len, cdat->tag)) {
999 err = "TAG_SET_ERROR";
1004 if (cdat->aead == EVP_CIPH_CCM_MODE) {
1005 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
1006 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
1011 err = "AAD_SET_ERROR";
1013 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
1018 * Supply the AAD in chunks less than the block size where possible
1020 if (cdat->aad_len > 0) {
1021 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1025 if (cdat->aad_len > 2) {
1026 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1029 donelen += cdat->aad_len - 2;
1031 if (cdat->aad_len > 1
1032 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1033 cdat->aad + donelen, 1))
1037 EVP_CIPHER_CTX_set_padding(ctx, 0);
1038 err = "CIPHERUPDATE_ERROR";
1041 /* We supply the data all in one go */
1042 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1045 /* Supply the data in chunks less than the block size where possible */
1047 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1054 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1062 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1068 err = "CIPHERFINAL_ERROR";
1069 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
1071 err = "LENGTH_MISMATCH";
1072 if (out_len != (size_t)(tmplen + tmpflen))
1074 err = "VALUE_MISMATCH";
1075 if (check_output(t, out, tmp + out_misalign, out_len))
1077 if (enc && cdat->aead) {
1078 unsigned char rtag[16];
1079 if (cdat->tag_len > sizeof(rtag)) {
1080 err = "TAG_LENGTH_INTERNAL_ERROR";
1083 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1084 cdat->tag_len, rtag)) {
1085 err = "TAG_RETRIEVE_ERROR";
1088 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
1089 err = "TAG_VALUE_MISMATCH";
1096 EVP_CIPHER_CTX_free(ctx);
1101 static int cipher_test_run(struct evp_test *t)
1103 struct cipher_data *cdat = t->data;
1105 size_t out_misalign, inp_misalign;
1111 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1112 /* IV is optional and usually omitted in wrap mode */
1113 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1118 if (cdat->aead && !cdat->tag) {
1122 for (out_misalign = 0; out_misalign <= 1;) {
1123 static char aux_err[64];
1124 t->aux_err = aux_err;
1125 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1126 if (inp_misalign == (size_t)-1) {
1127 /* kludge: inp_misalign == -1 means "exercise in-place" */
1128 BIO_snprintf(aux_err, sizeof(aux_err),
1129 "%s in-place, %sfragmented",
1130 out_misalign ? "misaligned" : "aligned",
1131 frag ? "" : "not ");
1133 BIO_snprintf(aux_err, sizeof(aux_err),
1134 "%s output and %s input, %sfragmented",
1135 out_misalign ? "misaligned" : "aligned",
1136 inp_misalign ? "misaligned" : "aligned",
1137 frag ? "" : "not ");
1140 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1141 /* Not fatal errors: return */
1148 if (cdat->enc != 1) {
1149 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1150 /* Not fatal errors: return */
1159 if (out_misalign == 1 && frag == 0) {
1161 * XTS, CCM and Wrap modes have special requirements about input
1162 * lengths so we don't fragment for those
1164 if (cdat->aead == EVP_CIPH_CCM_MODE
1165 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1166 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1179 static const struct evp_test_method cipher_test_method = {
1182 cipher_test_cleanup,
1190 /* Algorithm string for this MAC */
1196 unsigned char *input;
1198 /* Expected output */
1199 unsigned char *output;
1203 static int mac_test_init(struct evp_test *t, const char *alg)
1206 struct mac_data *mdat;
1207 if (strcmp(alg, "HMAC") == 0) {
1208 type = EVP_PKEY_HMAC;
1209 } else if (strcmp(alg, "CMAC") == 0) {
1210 #ifndef OPENSSL_NO_CMAC
1211 type = EVP_PKEY_CMAC;
1216 } else if (strcmp(alg, "Poly1305") == 0) {
1217 #ifndef OPENSSL_NO_POLY1305
1218 type = EVP_PKEY_POLY1305;
1223 } else if (strcmp(alg, "SipHash") == 0) {
1224 #ifndef OPENSSL_NO_SIPHASH
1225 type = EVP_PKEY_SIPHASH;
1233 mdat = OPENSSL_malloc(sizeof(*mdat));
1238 mdat->output = NULL;
1243 static void mac_test_cleanup(struct evp_test *t)
1245 struct mac_data *mdat = t->data;
1246 test_free(mdat->alg);
1247 test_free(mdat->key);
1248 test_free(mdat->input);
1249 test_free(mdat->output);
1252 static int mac_test_parse(struct evp_test *t,
1253 const char *keyword, const char *value)
1255 struct mac_data *mdata = t->data;
1256 if (strcmp(keyword, "Key") == 0)
1257 return test_bin(value, &mdata->key, &mdata->key_len);
1258 if (strcmp(keyword, "Algorithm") == 0) {
1259 mdata->alg = OPENSSL_strdup(value);
1264 if (strcmp(keyword, "Input") == 0)
1265 return test_bin(value, &mdata->input, &mdata->input_len);
1266 if (strcmp(keyword, "Output") == 0)
1267 return test_bin(value, &mdata->output, &mdata->output_len);
1271 static int mac_test_run(struct evp_test *t)
1273 struct mac_data *mdata = t->data;
1274 const char *err = "INTERNAL_ERROR";
1275 EVP_MD_CTX *mctx = NULL;
1276 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1277 EVP_PKEY *key = NULL;
1278 const EVP_MD *md = NULL;
1279 unsigned char *mac = NULL;
1282 #ifdef OPENSSL_NO_DES
1283 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1290 err = "MAC_PKEY_CTX_ERROR";
1291 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1295 err = "MAC_KEYGEN_INIT_ERROR";
1296 if (EVP_PKEY_keygen_init(genctx) <= 0)
1298 if (mdata->type == EVP_PKEY_CMAC) {
1299 err = "MAC_ALGORITHM_SET_ERROR";
1300 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1304 err = "MAC_KEY_SET_ERROR";
1305 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1308 err = "MAC_KEY_GENERATE_ERROR";
1309 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1311 if (mdata->type == EVP_PKEY_HMAC) {
1312 err = "MAC_ALGORITHM_SET_ERROR";
1313 md = EVP_get_digestbyname(mdata->alg);
1317 mctx = EVP_MD_CTX_new();
1320 err = "DIGESTSIGNINIT_ERROR";
1321 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1324 err = "DIGESTSIGNUPDATE_ERROR";
1325 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1327 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1328 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1330 mac = OPENSSL_malloc(mac_len);
1332 fprintf(stderr, "Error allocating mac buffer!\n");
1335 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1337 err = "MAC_LENGTH_MISMATCH";
1338 if (mac_len != mdata->output_len)
1340 err = "MAC_MISMATCH";
1341 if (check_output(t, mdata->output, mac, mac_len))
1345 EVP_MD_CTX_free(mctx);
1347 EVP_PKEY_CTX_free(genctx);
1353 static const struct evp_test_method mac_test_method = {
1362 * Public key operations. These are all very similar and can share
1363 * a lot of common code.
1367 /* Context for this operation */
1369 /* Key operation to perform */
1370 int (*keyop) (EVP_PKEY_CTX *ctx,
1371 unsigned char *sig, size_t *siglen,
1372 const unsigned char *tbs, size_t tbslen);
1374 unsigned char *input;
1376 /* Expected output */
1377 unsigned char *output;
1382 * Perform public key operation setup: lookup key, allocated ctx and call
1383 * the appropriate initialisation function
1385 static int pkey_test_init(struct evp_test *t, const char *name,
1387 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1388 int (*keyop) (EVP_PKEY_CTX *ctx,
1389 unsigned char *sig, size_t *siglen,
1390 const unsigned char *tbs,
1394 struct pkey_data *kdata;
1395 EVP_PKEY *pkey = NULL;
1398 rv = find_key(&pkey, name, t->public);
1400 rv = find_key(&pkey, name, t->private);
1401 if (!rv || pkey == NULL) {
1406 kdata = OPENSSL_malloc(sizeof(*kdata));
1408 EVP_PKEY_free(pkey);
1412 kdata->input = NULL;
1413 kdata->output = NULL;
1414 kdata->keyop = keyop;
1416 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1419 if (keyopinit(kdata->ctx) <= 0)
1420 t->err = "KEYOP_INIT_ERROR";
1424 static void pkey_test_cleanup(struct evp_test *t)
1426 struct pkey_data *kdata = t->data;
1428 OPENSSL_free(kdata->input);
1429 OPENSSL_free(kdata->output);
1430 EVP_PKEY_CTX_free(kdata->ctx);
1433 static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1439 tmpval = OPENSSL_strdup(value);
1442 p = strchr(tmpval, ':');
1445 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1447 t->err = "PKEY_CTRL_INVALID";
1449 } else if (p != NULL && rv <= 0) {
1450 /* If p has an OID and lookup fails assume disabled algorithm */
1451 int nid = OBJ_sn2nid(p);
1452 if (nid == NID_undef)
1453 nid = OBJ_ln2nid(p);
1454 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1455 EVP_get_cipherbynid(nid) == NULL) {
1459 t->err = "PKEY_CTRL_ERROR";
1463 OPENSSL_free(tmpval);
1467 static int pkey_test_parse(struct evp_test *t,
1468 const char *keyword, const char *value)
1470 struct pkey_data *kdata = t->data;
1471 if (strcmp(keyword, "Input") == 0)
1472 return test_bin(value, &kdata->input, &kdata->input_len);
1473 if (strcmp(keyword, "Output") == 0)
1474 return test_bin(value, &kdata->output, &kdata->output_len);
1475 if (strcmp(keyword, "Ctrl") == 0)
1476 return pkey_test_ctrl(t, kdata->ctx, value);
1480 static int pkey_test_run(struct evp_test *t)
1482 struct pkey_data *kdata = t->data;
1483 unsigned char *out = NULL;
1485 const char *err = "KEYOP_LENGTH_ERROR";
1486 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1487 kdata->input_len) <= 0)
1489 out = OPENSSL_malloc(out_len);
1491 fprintf(stderr, "Error allocating output buffer!\n");
1494 err = "KEYOP_ERROR";
1496 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1498 err = "KEYOP_LENGTH_MISMATCH";
1499 if (out_len != kdata->output_len)
1501 err = "KEYOP_MISMATCH";
1502 if (check_output(t, kdata->output, out, out_len))
1511 static int sign_test_init(struct evp_test *t, const char *name)
1513 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1516 static const struct evp_test_method psign_test_method = {
1524 static int verify_recover_test_init(struct evp_test *t, const char *name)
1526 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1527 EVP_PKEY_verify_recover);
1530 static const struct evp_test_method pverify_recover_test_method = {
1532 verify_recover_test_init,
1538 static int decrypt_test_init(struct evp_test *t, const char *name)
1540 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1544 static const struct evp_test_method pdecrypt_test_method = {
1552 static int verify_test_init(struct evp_test *t, const char *name)
1554 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1557 static int verify_test_run(struct evp_test *t)
1559 struct pkey_data *kdata = t->data;
1560 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1561 kdata->input, kdata->input_len) <= 0)
1562 t->err = "VERIFY_ERROR";
1566 static const struct evp_test_method pverify_test_method = {
1575 static int pderive_test_init(struct evp_test *t, const char *name)
1577 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1580 static int pderive_test_parse(struct evp_test *t,
1581 const char *keyword, const char *value)
1583 struct pkey_data *kdata = t->data;
1585 if (strcmp(keyword, "PeerKey") == 0) {
1587 if (find_key(&peer, value, t->public) == 0)
1589 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1593 if (strcmp(keyword, "SharedSecret") == 0)
1594 return test_bin(value, &kdata->output, &kdata->output_len);
1595 if (strcmp(keyword, "Ctrl") == 0)
1596 return pkey_test_ctrl(t, kdata->ctx, value);
1600 static int pderive_test_run(struct evp_test *t)
1602 struct pkey_data *kdata = t->data;
1603 unsigned char *out = NULL;
1605 const char *err = "INTERNAL_ERROR";
1607 out_len = kdata->output_len;
1608 out = OPENSSL_malloc(out_len);
1610 fprintf(stderr, "Error allocating output buffer!\n");
1613 err = "DERIVE_ERROR";
1614 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1616 err = "SHARED_SECRET_LENGTH_MISMATCH";
1617 if (out_len != kdata->output_len)
1619 err = "SHARED_SECRET_MISMATCH";
1620 if (check_output(t, kdata->output, out, out_len))
1629 static const struct evp_test_method pderive_test_method = {
1639 #define PBE_TYPE_SCRYPT 1
1640 #define PBE_TYPE_PBKDF2 2
1641 #define PBE_TYPE_PKCS12 3
1647 /* scrypt parameters */
1648 uint64_t N, r, p, maxmem;
1650 /* PKCS#12 parameters */
1655 unsigned char *pass;
1659 unsigned char *salt;
1662 /* Expected output */
1667 #ifndef OPENSSL_NO_SCRYPT
1668 static int scrypt_test_parse(struct evp_test *t,
1669 const char *keyword, const char *value)
1671 struct pbe_data *pdata = t->data;
1673 if (strcmp(keyword, "N") == 0)
1674 return test_uint64(value, &pdata->N);
1675 if (strcmp(keyword, "p") == 0)
1676 return test_uint64(value, &pdata->p);
1677 if (strcmp(keyword, "r") == 0)
1678 return test_uint64(value, &pdata->r);
1679 if (strcmp(keyword, "maxmem") == 0)
1680 return test_uint64(value, &pdata->maxmem);
1685 static int pbkdf2_test_parse(struct evp_test *t,
1686 const char *keyword, const char *value)
1688 struct pbe_data *pdata = t->data;
1690 if (strcmp(keyword, "iter") == 0) {
1691 pdata->iter = atoi(value);
1692 if (pdata->iter <= 0)
1696 if (strcmp(keyword, "MD") == 0) {
1697 pdata->md = EVP_get_digestbyname(value);
1698 if (pdata->md == NULL)
1705 static int pkcs12_test_parse(struct evp_test *t,
1706 const char *keyword, const char *value)
1708 struct pbe_data *pdata = t->data;
1710 if (strcmp(keyword, "id") == 0) {
1711 pdata->id = atoi(value);
1716 return pbkdf2_test_parse(t, keyword, value);
1719 static int pbe_test_init(struct evp_test *t, const char *alg)
1721 struct pbe_data *pdat;
1724 if (strcmp(alg, "scrypt") == 0) {
1725 #ifndef OPENSSL_NO_SCRYPT
1726 pbe_type = PBE_TYPE_SCRYPT;
1731 } else if (strcmp(alg, "pbkdf2") == 0) {
1732 pbe_type = PBE_TYPE_PBKDF2;
1733 } else if (strcmp(alg, "pkcs12") == 0) {
1734 pbe_type = PBE_TYPE_PKCS12;
1736 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1738 pdat = OPENSSL_malloc(sizeof(*pdat));
1739 pdat->pbe_type = pbe_type;
1753 static void pbe_test_cleanup(struct evp_test *t)
1755 struct pbe_data *pdat = t->data;
1756 test_free(pdat->pass);
1757 test_free(pdat->salt);
1758 test_free(pdat->key);
1761 static int pbe_test_parse(struct evp_test *t,
1762 const char *keyword, const char *value)
1764 struct pbe_data *pdata = t->data;
1766 if (strcmp(keyword, "Password") == 0)
1767 return test_bin(value, &pdata->pass, &pdata->pass_len);
1768 if (strcmp(keyword, "Salt") == 0)
1769 return test_bin(value, &pdata->salt, &pdata->salt_len);
1770 if (strcmp(keyword, "Key") == 0)
1771 return test_bin(value, &pdata->key, &pdata->key_len);
1772 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1773 return pbkdf2_test_parse(t, keyword, value);
1774 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1775 return pkcs12_test_parse(t, keyword, value);
1776 #ifndef OPENSSL_NO_SCRYPT
1777 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1778 return scrypt_test_parse(t, keyword, value);
1783 static int pbe_test_run(struct evp_test *t)
1785 struct pbe_data *pdata = t->data;
1786 const char *err = "INTERNAL_ERROR";
1789 key = OPENSSL_malloc(pdata->key_len);
1792 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1793 err = "PBKDF2_ERROR";
1794 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1795 pdata->salt, pdata->salt_len,
1796 pdata->iter, pdata->md,
1797 pdata->key_len, key) == 0)
1799 #ifndef OPENSSL_NO_SCRYPT
1800 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1801 err = "SCRYPT_ERROR";
1802 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1803 pdata->salt, pdata->salt_len,
1804 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1805 key, pdata->key_len) == 0)
1808 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1809 err = "PKCS12_ERROR";
1810 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1811 pdata->salt, pdata->salt_len,
1812 pdata->id, pdata->iter, pdata->key_len,
1813 key, pdata->md) == 0)
1816 err = "KEY_MISMATCH";
1817 if (check_output(t, pdata->key, key, pdata->key_len))
1826 static const struct evp_test_method pbe_test_method = {
1837 BASE64_CANONICAL_ENCODING = 0,
1838 BASE64_VALID_ENCODING = 1,
1839 BASE64_INVALID_ENCODING = 2
1840 } base64_encoding_type;
1842 struct encode_data {
1843 /* Input to encoding */
1844 unsigned char *input;
1846 /* Expected output */
1847 unsigned char *output;
1849 base64_encoding_type encoding;
1852 static int encode_test_init(struct evp_test *t, const char *encoding)
1854 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1856 if (strcmp(encoding, "canonical") == 0) {
1857 edata->encoding = BASE64_CANONICAL_ENCODING;
1858 } else if (strcmp(encoding, "valid") == 0) {
1859 edata->encoding = BASE64_VALID_ENCODING;
1860 } else if (strcmp(encoding, "invalid") == 0) {
1861 edata->encoding = BASE64_INVALID_ENCODING;
1862 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1863 if (t->expected_err == NULL)
1866 fprintf(stderr, "Bad encoding: %s. Should be one of "
1867 "{canonical, valid, invalid}\n", encoding);
1874 static void encode_test_cleanup(struct evp_test *t)
1876 struct encode_data *edata = t->data;
1877 test_free(edata->input);
1878 test_free(edata->output);
1879 memset(edata, 0, sizeof(*edata));
1882 static int encode_test_parse(struct evp_test *t,
1883 const char *keyword, const char *value)
1885 struct encode_data *edata = t->data;
1886 if (strcmp(keyword, "Input") == 0)
1887 return test_bin(value, &edata->input, &edata->input_len);
1888 if (strcmp(keyword, "Output") == 0)
1889 return test_bin(value, &edata->output, &edata->output_len);
1893 static int encode_test_run(struct evp_test *t)
1895 struct encode_data *edata = t->data;
1896 unsigned char *encode_out = NULL, *decode_out = NULL;
1897 int output_len, chunk_len;
1898 const char *err = "INTERNAL_ERROR";
1899 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1901 if (decode_ctx == NULL)
1904 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1905 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1906 if (encode_ctx == NULL)
1908 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1909 if (encode_out == NULL)
1912 EVP_EncodeInit(encode_ctx);
1913 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1914 edata->input, edata->input_len);
1915 output_len = chunk_len;
1917 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1918 output_len += chunk_len;
1920 EVP_ENCODE_CTX_free(encode_ctx);
1922 if (check_var_length_output(t, edata->output, edata->output_len,
1923 encode_out, output_len)) {
1924 err = "BAD_ENCODING";
1929 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1930 if (decode_out == NULL)
1933 EVP_DecodeInit(decode_ctx);
1934 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1935 edata->output_len) < 0) {
1936 err = "DECODE_ERROR";
1939 output_len = chunk_len;
1941 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1942 err = "DECODE_ERROR";
1945 output_len += chunk_len;
1947 if (edata->encoding != BASE64_INVALID_ENCODING &&
1948 check_var_length_output(t, edata->input, edata->input_len,
1949 decode_out, output_len)) {
1950 err = "BAD_DECODING";
1957 OPENSSL_free(encode_out);
1958 OPENSSL_free(decode_out);
1959 EVP_ENCODE_CTX_free(decode_ctx);
1963 static const struct evp_test_method encode_test_method = {
1966 encode_test_cleanup,
1971 /* KDF operations */
1974 /* Context for this operation */
1976 /* Expected output */
1977 unsigned char *output;
1982 * Perform public key operation setup: lookup key, allocated ctx and call
1983 * the appropriate initialisation function
1985 static int kdf_test_init(struct evp_test *t, const char *name)
1987 struct kdf_data *kdata;
1989 kdata = OPENSSL_malloc(sizeof(*kdata));
1993 kdata->output = NULL;
1995 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1996 if (kdata->ctx == NULL)
1998 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
2003 static void kdf_test_cleanup(struct evp_test *t)
2005 struct kdf_data *kdata = t->data;
2006 OPENSSL_free(kdata->output);
2007 EVP_PKEY_CTX_free(kdata->ctx);
2010 static int kdf_test_parse(struct evp_test *t,
2011 const char *keyword, const char *value)
2013 struct kdf_data *kdata = t->data;
2014 if (strcmp(keyword, "Output") == 0)
2015 return test_bin(value, &kdata->output, &kdata->output_len);
2016 if (strncmp(keyword, "Ctrl", 4) == 0)
2017 return pkey_test_ctrl(t, kdata->ctx, value);
2021 static int kdf_test_run(struct evp_test *t)
2023 struct kdf_data *kdata = t->data;
2024 unsigned char *out = NULL;
2025 size_t out_len = kdata->output_len;
2026 const char *err = "INTERNAL_ERROR";
2027 out = OPENSSL_malloc(out_len);
2029 fprintf(stderr, "Error allocating output buffer!\n");
2032 err = "KDF_DERIVE_ERROR";
2033 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
2035 err = "KDF_LENGTH_MISMATCH";
2036 if (out_len != kdata->output_len)
2038 err = "KDF_MISMATCH";
2039 if (check_output(t, kdata->output, out, out_len))
2048 static const struct evp_test_method kdf_test_method = {
2056 struct keypair_test_data {
2061 static int keypair_test_init(struct evp_test *t, const char *pair)
2064 EVP_PKEY *pk = NULL, *pubk = NULL;
2065 char *pub, *priv = NULL;
2066 const char *err = "INTERNAL_ERROR";
2067 struct keypair_test_data *data;
2069 priv = OPENSSL_strdup(pair);
2072 pub = strchr(priv, ':');
2073 if ( pub == NULL ) {
2074 fprintf(stderr, "Wrong syntax \"%s\"\n", pair);
2077 *pub++ = 0; /* split priv and pub strings */
2079 if (find_key(&pk, priv, t->private) == 0) {
2080 fprintf(stderr, "Cannot find private key: %s\n", priv);
2081 err = "MISSING_PRIVATE_KEY";
2084 if (find_key(&pubk, pub, t->public) == 0) {
2085 fprintf(stderr, "Cannot find public key: %s\n", pub);
2086 err = "MISSING_PUBLIC_KEY";
2090 if (pk == NULL && pubk == NULL) {
2091 /* Both keys are listed but unsupported: skip this test */
2097 data = OPENSSL_malloc(sizeof(*data));
2115 static void keypair_test_cleanup(struct evp_test *t)
2117 struct keypair_test_data *data = t->data;
2124 /* For test that do not accept any custom keyword:
2125 * return 0 if called
2127 static int void_test_parse(struct evp_test *t, const char *keyword, const char *value)
2132 static int keypair_test_run(struct evp_test *t)
2135 const struct keypair_test_data *pair = t->data;
2136 const char *err = "INTERNAL_ERROR";
2141 if (pair->privk == NULL || pair->pubk == NULL) {
2142 /* this can only happen if only one of the keys is not set
2143 * which means that one of them was unsupported while the
2144 * other isn't: hence a key type mismatch.
2146 err = "KEYPAIR_TYPE_MISMATCH";
2151 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2153 err = "KEYPAIR_MISMATCH";
2154 } else if ( -1 == rv ) {
2155 err = "KEYPAIR_TYPE_MISMATCH";
2156 } else if ( -2 == rv ) {
2157 err = "UNSUPPORTED_KEY_COMPARISON";
2159 fprintf(stderr, "Unexpected error in key comparison\n");
2175 static const struct evp_test_method keypair_test_method = {
2178 keypair_test_cleanup,