2 * Copyright 2015-2016 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;
33 p = p + strlen(*pval) - 1;
35 /* Remove trailing space */
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)
132 * Don't return NULL for zero length buffer.
133 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
134 * a non-NULL key buffer even if the key length is 0, in order to detect
137 *buf = OPENSSL_malloc(1);
144 /* Check for string literal */
145 if (value[0] == '"') {
148 vlen = strlen(value);
149 if (value[vlen - 1] != '"')
152 *buf = unescape(value, vlen, buflen);
158 *buf = OPENSSL_hexstr2buf(value, &len);
160 fprintf(stderr, "Value=%s\n", value);
161 ERR_print_errors_fp(stderr);
164 /* Size of input buffer means we'll never overflow */
168 #ifndef OPENSSL_NO_SCRYPT
169 /* Currently only used by scrypt tests */
170 /* Parse unsigned decimal 64 bit integer value */
171 static int test_uint64(const char *value, uint64_t *pr)
173 const char *p = value;
175 fprintf(stderr, "Invalid empty integer value\n");
180 if (*pr > UINT64_MAX/10) {
181 fprintf(stderr, "Integer string overflow value=%s\n", value);
185 if (*p < '0' || *p > '9') {
186 fprintf(stderr, "Invalid integer string value=%s\n", value);
196 /* Structure holding test information */
198 /* file being read */
200 /* List of public and private keys */
201 struct key_list *private;
202 struct key_list *public;
203 /* method for this test */
204 const struct evp_test_method *meth;
205 /* current line being processed */
207 /* start line of current test */
208 unsigned int start_line;
209 /* Error string for test */
210 const char *err, *aux_err;
211 /* Expected error value of test */
213 /* Number of tests */
217 /* Number of tests skipped */
219 /* If output mismatch expected and got value */
220 unsigned char *out_received;
221 size_t out_received_len;
222 unsigned char *out_expected;
223 size_t out_expected_len;
224 /* test specific data */
226 /* Current test should be skipped */
233 struct key_list *next;
236 /* Test method structure */
237 struct evp_test_method {
238 /* Name of test as it appears in file */
240 /* Initialise test for "alg" */
241 int (*init) (struct evp_test * t, const char *alg);
242 /* Clean up method */
243 void (*cleanup) (struct evp_test * t);
244 /* Test specific name value pair processing */
245 int (*parse) (struct evp_test * t, const char *name, const char *value);
246 /* Run the test itself */
247 int (*run_test) (struct evp_test * t);
250 static const struct evp_test_method digest_test_method, cipher_test_method;
251 static const struct evp_test_method mac_test_method;
252 static const struct evp_test_method psign_test_method, pverify_test_method;
253 static const struct evp_test_method pdecrypt_test_method;
254 static const struct evp_test_method pverify_recover_test_method;
255 static const struct evp_test_method pderive_test_method;
256 static const struct evp_test_method pbe_test_method;
257 static const struct evp_test_method encode_test_method;
258 static const struct evp_test_method kdf_test_method;
260 static const struct evp_test_method *evp_test_list[] = {
265 &pverify_test_method,
266 &pdecrypt_test_method,
267 &pverify_recover_test_method,
268 &pderive_test_method,
275 static const struct evp_test_method *evp_find_test(const char *name)
277 const struct evp_test_method **tt;
279 for (tt = evp_test_list; *tt; tt++) {
280 if (strcmp(name, (*tt)->name) == 0)
286 static void hex_print(const char *name, const unsigned char *buf, size_t len)
289 fprintf(stderr, "%s ", name);
290 for (i = 0; i < len; i++)
291 fprintf(stderr, "%02X", buf[i]);
295 static void free_expected(struct evp_test *t)
297 OPENSSL_free(t->expected_err);
298 t->expected_err = NULL;
299 OPENSSL_free(t->out_expected);
300 OPENSSL_free(t->out_received);
301 t->out_expected = NULL;
302 t->out_received = NULL;
303 t->out_expected_len = 0;
304 t->out_received_len = 0;
309 static void print_expected(struct evp_test *t)
311 if (t->out_expected == NULL && t->out_received == NULL)
313 hex_print("Expected:", t->out_expected, t->out_expected_len);
314 hex_print("Got: ", t->out_received, t->out_received_len);
318 static int check_test_error(struct evp_test *t)
320 if (!t->err && !t->expected_err)
322 if (t->err && !t->expected_err) {
323 if (t->aux_err != NULL) {
324 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
325 t->start_line, t->aux_err, t->err);
327 fprintf(stderr, "Test line %d: unexpected error %s\n",
328 t->start_line, t->err);
333 if (!t->err && t->expected_err) {
334 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
335 t->start_line, t->expected_err);
338 if (strcmp(t->err, t->expected_err) == 0)
341 fprintf(stderr, "Test line %d: expecting %s got %s\n",
342 t->start_line, t->expected_err, t->err);
346 /* Setup a new test, run any existing test */
348 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
350 /* If we already have a test set up run it */
359 if (t->meth->run_test(t) != 1) {
360 fprintf(stderr, "%s test error line %d\n",
361 t->meth->name, t->start_line);
364 if (!check_test_error(t)) {
366 ERR_print_errors_fp(stderr);
371 OPENSSL_free(t->data);
373 OPENSSL_free(t->expected_err);
374 t->expected_err = NULL;
381 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
383 for (; lst; lst = lst->next) {
384 if (strcmp(lst->name, name) == 0) {
393 static void free_key_list(struct key_list *lst)
395 while (lst != NULL) {
396 struct key_list *ltmp;
397 EVP_PKEY_free(lst->key);
398 OPENSSL_free(lst->name);
405 static int check_unsupported()
407 long err = ERR_peek_error();
408 if (ERR_GET_LIB(err) == ERR_LIB_EVP
409 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
416 static int process_test(struct evp_test *t, char *buf, int verbose)
418 char *keyword = NULL, *value = NULL;
419 int rv = 0, add_key = 0;
421 struct key_list **lst = NULL, *key = NULL;
423 const struct evp_test_method *tmeth = NULL;
426 if (!parse_line(&keyword, &value, buf))
428 if (strcmp(keyword, "PrivateKey") == 0) {
429 save_pos = BIO_tell(t->in);
430 pk = PEM_read_bio_PrivateKey(t->in, NULL, 0, NULL);
431 if (pk == NULL && !check_unsupported()) {
432 fprintf(stderr, "Error reading private key %s\n", value);
433 ERR_print_errors_fp(stderr);
439 if (strcmp(keyword, "PublicKey") == 0) {
440 save_pos = BIO_tell(t->in);
441 pk = PEM_read_bio_PUBKEY(t->in, NULL, 0, NULL);
442 if (pk == NULL && !check_unsupported()) {
443 fprintf(stderr, "Error reading public key %s\n", value);
444 ERR_print_errors_fp(stderr);
450 /* If we have a key add to list */
453 if (find_key(NULL, value, *lst)) {
454 fprintf(stderr, "Duplicate key %s\n", value);
457 key = OPENSSL_malloc(sizeof(*key));
460 key->name = OPENSSL_strdup(value);
464 /* Rewind input, read to end and update line numbers */
465 (void)BIO_seek(t->in, save_pos);
466 while (BIO_gets(t->in,tmpbuf, sizeof(tmpbuf))) {
468 if (strncmp(tmpbuf, "-----END", 8) == 0)
471 fprintf(stderr, "Can't find key end\n");
475 /* See if keyword corresponds to a test start */
476 tmeth = evp_find_test(keyword);
478 if (!setup_test(t, tmeth))
480 t->start_line = t->line;
482 if (!tmeth->init(t, value)) {
483 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
487 } else if (t->skip) {
489 } else if (strcmp(keyword, "Result") == 0) {
490 if (t->expected_err) {
491 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
494 t->expected_err = OPENSSL_strdup(value);
495 if (!t->expected_err)
498 /* Must be test specific line: try to parse it */
500 rv = t->meth->parse(t, keyword, value);
503 fprintf(stderr, "line %d: unexpected keyword %s\n",
507 fprintf(stderr, "line %d: error processing keyword %s\n",
515 static int check_var_length_output(struct evp_test *t,
516 const unsigned char *expected,
518 const unsigned char *received,
521 if (expected_len == received_len &&
522 memcmp(expected, received, expected_len) == 0) {
526 /* The result printing code expects a non-NULL buffer. */
527 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
528 t->out_expected_len = expected_len;
529 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
530 t->out_received_len = received_len;
531 if (t->out_expected == NULL || t->out_received == NULL) {
532 fprintf(stderr, "Memory allocation error!\n");
538 static int check_output(struct evp_test *t,
539 const unsigned char *expected,
540 const unsigned char *received,
543 return check_var_length_output(t, expected, len, received, len);
546 int main(int argc, char **argv)
553 fprintf(stderr, "usage: evp_test testfile.txt\n");
557 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
559 memset(&t, 0, sizeof(t));
561 in = BIO_new_file(argv[1], "r");
563 while (BIO_gets(in, buf, sizeof(buf))) {
565 if (!process_test(&t, buf, 0))
568 /* Run any final test we have */
569 if (!setup_test(&t, NULL))
571 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
572 t.ntests, t.errors, t.nskip);
573 free_key_list(t.public);
574 free_key_list(t.private);
577 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
578 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
586 static void test_free(void *d)
591 /* Message digest tests */
594 /* Digest this test is for */
595 const EVP_MD *digest;
596 /* Input to digest */
597 unsigned char *input;
599 /* Repeat count for input */
601 /* Expected output */
602 unsigned char *output;
606 static int digest_test_init(struct evp_test *t, const char *alg)
608 const EVP_MD *digest;
609 struct digest_data *mdat;
610 digest = EVP_get_digestbyname(alg);
612 /* If alg has an OID assume disabled algorithm */
613 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
619 mdat = OPENSSL_malloc(sizeof(*mdat));
620 mdat->digest = digest;
628 static void digest_test_cleanup(struct evp_test *t)
630 struct digest_data *mdat = t->data;
631 test_free(mdat->input);
632 test_free(mdat->output);
635 static int digest_test_parse(struct evp_test *t,
636 const char *keyword, const char *value)
638 struct digest_data *mdata = t->data;
639 if (strcmp(keyword, "Input") == 0)
640 return test_bin(value, &mdata->input, &mdata->input_len);
641 if (strcmp(keyword, "Output") == 0)
642 return test_bin(value, &mdata->output, &mdata->output_len);
643 if (strcmp(keyword, "Count") == 0) {
644 long nrpt = atoi(value);
647 mdata->nrpt = (size_t)nrpt;
653 static int digest_test_run(struct evp_test *t)
655 struct digest_data *mdata = t->data;
657 const char *err = "INTERNAL_ERROR";
659 unsigned char md[EVP_MAX_MD_SIZE];
661 mctx = EVP_MD_CTX_new();
664 err = "DIGESTINIT_ERROR";
665 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
667 err = "DIGESTUPDATE_ERROR";
668 for (i = 0; i < mdata->nrpt; i++) {
669 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
672 err = "DIGESTFINAL_ERROR";
673 if (!EVP_DigestFinal(mctx, md, &md_len))
675 err = "DIGEST_LENGTH_MISMATCH";
676 if (md_len != mdata->output_len)
678 err = "DIGEST_MISMATCH";
679 if (check_output(t, mdata->output, md, md_len))
683 EVP_MD_CTX_free(mctx);
688 static const struct evp_test_method digest_test_method = {
698 const EVP_CIPHER *cipher;
700 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
706 unsigned char *plaintext;
707 size_t plaintext_len;
708 unsigned char *ciphertext;
709 size_t ciphertext_len;
717 static int cipher_test_init(struct evp_test *t, const char *alg)
719 const EVP_CIPHER *cipher;
720 struct cipher_data *cdat = t->data;
721 cipher = EVP_get_cipherbyname(alg);
723 /* If alg has an OID assume disabled algorithm */
724 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
730 cdat = OPENSSL_malloc(sizeof(*cdat));
731 cdat->cipher = cipher;
735 cdat->ciphertext = NULL;
736 cdat->plaintext = NULL;
740 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
741 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
742 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
743 cdat->aead = EVP_CIPHER_mode(cipher);
744 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
752 static void cipher_test_cleanup(struct evp_test *t)
754 struct cipher_data *cdat = t->data;
755 test_free(cdat->key);
757 test_free(cdat->ciphertext);
758 test_free(cdat->plaintext);
759 test_free(cdat->aad);
760 test_free(cdat->tag);
763 static int cipher_test_parse(struct evp_test *t, const char *keyword,
766 struct cipher_data *cdat = t->data;
767 if (strcmp(keyword, "Key") == 0)
768 return test_bin(value, &cdat->key, &cdat->key_len);
769 if (strcmp(keyword, "IV") == 0)
770 return test_bin(value, &cdat->iv, &cdat->iv_len);
771 if (strcmp(keyword, "Plaintext") == 0)
772 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
773 if (strcmp(keyword, "Ciphertext") == 0)
774 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
776 if (strcmp(keyword, "AAD") == 0)
777 return test_bin(value, &cdat->aad, &cdat->aad_len);
778 if (strcmp(keyword, "Tag") == 0)
779 return test_bin(value, &cdat->tag, &cdat->tag_len);
782 if (strcmp(keyword, "Operation") == 0) {
783 if (strcmp(value, "ENCRYPT") == 0)
785 else if (strcmp(value, "DECRYPT") == 0)
794 static int cipher_test_enc(struct evp_test *t, int enc,
795 size_t out_misalign, size_t inp_misalign)
797 struct cipher_data *cdat = t->data;
798 unsigned char *in, *out, *tmp = NULL;
799 size_t in_len, out_len;
801 EVP_CIPHER_CTX *ctx = NULL;
803 err = "INTERNAL_ERROR";
804 ctx = EVP_CIPHER_CTX_new();
807 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
809 in = cdat->plaintext;
810 in_len = cdat->plaintext_len;
811 out = cdat->ciphertext;
812 out_len = cdat->ciphertext_len;
814 in = cdat->ciphertext;
815 in_len = cdat->ciphertext_len;
816 out = cdat->plaintext;
817 out_len = cdat->plaintext_len;
819 if (inp_misalign == (size_t)-1) {
821 * Exercise in-place encryption
823 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
826 in = memcpy(tmp + out_misalign, in, in_len);
828 inp_misalign += 16 - ((out_misalign + in_len) & 15);
830 * 'tmp' will store both output and copy of input. We make the copy
831 * of input to specifically aligned part of 'tmp'. So we just
832 * figured out how much padding would ensure the required alignment,
833 * now we allocate extended buffer and finally copy the input just
834 * past inp_misalign in expression below. Output will be written
835 * past out_misalign...
837 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
838 inp_misalign + in_len);
841 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
842 inp_misalign, in, in_len);
844 err = "CIPHERINIT_ERROR";
845 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
847 err = "INVALID_IV_LENGTH";
850 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
853 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
859 * If encrypting or OCB just set tag length initially, otherwise
860 * set tag length and value.
862 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
863 err = "TAG_LENGTH_SET_ERROR";
866 err = "TAG_SET_ERROR";
869 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
870 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
876 err = "INVALID_KEY_LENGTH";
877 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
879 err = "KEY_SET_ERROR";
880 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
883 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
884 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
885 cdat->tag_len, cdat->tag)) {
886 err = "TAG_SET_ERROR";
891 if (cdat->aead == EVP_CIPH_CCM_MODE) {
892 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
893 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
898 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
899 err = "AAD_SET_ERROR";
903 EVP_CIPHER_CTX_set_padding(ctx, 0);
904 err = "CIPHERUPDATE_ERROR";
905 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
907 if (cdat->aead == EVP_CIPH_CCM_MODE)
910 err = "CIPHERFINAL_ERROR";
911 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
914 err = "LENGTH_MISMATCH";
915 if (out_len != (size_t)(tmplen + tmpflen))
917 err = "VALUE_MISMATCH";
918 if (check_output(t, out, tmp + out_misalign, out_len))
920 if (enc && cdat->aead) {
921 unsigned char rtag[16];
922 if (cdat->tag_len > sizeof(rtag)) {
923 err = "TAG_LENGTH_INTERNAL_ERROR";
926 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
927 cdat->tag_len, rtag)) {
928 err = "TAG_RETRIEVE_ERROR";
931 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
932 err = "TAG_VALUE_MISMATCH";
939 EVP_CIPHER_CTX_free(ctx);
944 static int cipher_test_run(struct evp_test *t)
946 struct cipher_data *cdat = t->data;
948 size_t out_misalign, inp_misalign;
954 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
955 /* IV is optional and usually omitted in wrap mode */
956 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
961 if (cdat->aead && !cdat->tag) {
965 for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
966 static char aux_err[64];
967 t->aux_err = aux_err;
968 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
969 if (inp_misalign == (size_t)-1) {
970 /* kludge: inp_misalign == -1 means "exercise in-place" */
971 BIO_snprintf(aux_err, sizeof(aux_err), "%s in-place",
972 out_misalign ? "misaligned" : "aligned");
974 BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input",
975 out_misalign ? "misaligned" : "aligned",
976 inp_misalign ? "misaligned" : "aligned");
979 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign);
980 /* Not fatal errors: return */
987 if (cdat->enc != 1) {
988 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign);
989 /* Not fatal errors: return */
1003 static const struct evp_test_method cipher_test_method = {
1006 cipher_test_cleanup,
1014 /* Algorithm string for this MAC */
1020 unsigned char *input;
1022 /* Expected output */
1023 unsigned char *output;
1027 static int mac_test_init(struct evp_test *t, const char *alg)
1030 struct mac_data *mdat;
1031 if (strcmp(alg, "HMAC") == 0) {
1032 type = EVP_PKEY_HMAC;
1033 } else if (strcmp(alg, "CMAC") == 0) {
1034 #ifndef OPENSSL_NO_CMAC
1035 type = EVP_PKEY_CMAC;
1043 mdat = OPENSSL_malloc(sizeof(*mdat));
1048 mdat->output = NULL;
1053 static void mac_test_cleanup(struct evp_test *t)
1055 struct mac_data *mdat = t->data;
1056 test_free(mdat->alg);
1057 test_free(mdat->key);
1058 test_free(mdat->input);
1059 test_free(mdat->output);
1062 static int mac_test_parse(struct evp_test *t,
1063 const char *keyword, const char *value)
1065 struct mac_data *mdata = t->data;
1066 if (strcmp(keyword, "Key") == 0)
1067 return test_bin(value, &mdata->key, &mdata->key_len);
1068 if (strcmp(keyword, "Algorithm") == 0) {
1069 mdata->alg = OPENSSL_strdup(value);
1074 if (strcmp(keyword, "Input") == 0)
1075 return test_bin(value, &mdata->input, &mdata->input_len);
1076 if (strcmp(keyword, "Output") == 0)
1077 return test_bin(value, &mdata->output, &mdata->output_len);
1081 static int mac_test_run(struct evp_test *t)
1083 struct mac_data *mdata = t->data;
1084 const char *err = "INTERNAL_ERROR";
1085 EVP_MD_CTX *mctx = NULL;
1086 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1087 EVP_PKEY *key = NULL;
1088 const EVP_MD *md = NULL;
1089 unsigned char *mac = NULL;
1092 #ifdef OPENSSL_NO_DES
1093 if (strstr(mdata->alg, "DES") != NULL) {
1100 err = "MAC_PKEY_CTX_ERROR";
1101 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1105 err = "MAC_KEYGEN_INIT_ERROR";
1106 if (EVP_PKEY_keygen_init(genctx) <= 0)
1108 if (mdata->type == EVP_PKEY_CMAC) {
1109 err = "MAC_ALGORITHM_SET_ERROR";
1110 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1114 err = "MAC_KEY_SET_ERROR";
1115 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1118 err = "MAC_KEY_GENERATE_ERROR";
1119 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1121 if (mdata->type == EVP_PKEY_HMAC) {
1122 err = "MAC_ALGORITHM_SET_ERROR";
1123 md = EVP_get_digestbyname(mdata->alg);
1127 mctx = EVP_MD_CTX_new();
1130 err = "DIGESTSIGNINIT_ERROR";
1131 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1134 err = "DIGESTSIGNUPDATE_ERROR";
1135 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1137 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1138 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1140 mac = OPENSSL_malloc(mac_len);
1142 fprintf(stderr, "Error allocating mac buffer!\n");
1145 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1147 err = "MAC_LENGTH_MISMATCH";
1148 if (mac_len != mdata->output_len)
1150 err = "MAC_MISMATCH";
1151 if (check_output(t, mdata->output, mac, mac_len))
1155 EVP_MD_CTX_free(mctx);
1157 EVP_PKEY_CTX_free(genctx);
1163 static const struct evp_test_method mac_test_method = {
1172 * Public key operations. These are all very similar and can share
1173 * a lot of common code.
1177 /* Context for this operation */
1179 /* Key operation to perform */
1180 int (*keyop) (EVP_PKEY_CTX *ctx,
1181 unsigned char *sig, size_t *siglen,
1182 const unsigned char *tbs, size_t tbslen);
1184 unsigned char *input;
1186 /* Expected output */
1187 unsigned char *output;
1192 * Perform public key operation setup: lookup key, allocated ctx and call
1193 * the appropriate initialisation function
1195 static int pkey_test_init(struct evp_test *t, const char *name,
1197 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1198 int (*keyop) (EVP_PKEY_CTX *ctx,
1199 unsigned char *sig, size_t *siglen,
1200 const unsigned char *tbs,
1204 struct pkey_data *kdata;
1205 EVP_PKEY *pkey = NULL;
1208 rv = find_key(&pkey, name, t->public);
1210 rv = find_key(&pkey, name, t->private);
1218 kdata = OPENSSL_malloc(sizeof(*kdata));
1220 EVP_PKEY_free(pkey);
1224 kdata->input = NULL;
1225 kdata->output = NULL;
1226 kdata->keyop = keyop;
1228 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1231 if (keyopinit(kdata->ctx) <= 0)
1236 static void pkey_test_cleanup(struct evp_test *t)
1238 struct pkey_data *kdata = t->data;
1240 OPENSSL_free(kdata->input);
1241 OPENSSL_free(kdata->output);
1242 EVP_PKEY_CTX_free(kdata->ctx);
1245 static int pkey_test_ctrl(EVP_PKEY_CTX *pctx, const char *value)
1250 tmpval = OPENSSL_strdup(value);
1253 p = strchr(tmpval, ':');
1256 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1257 OPENSSL_free(tmpval);
1261 static int pkey_test_parse(struct evp_test *t,
1262 const char *keyword, const char *value)
1264 struct pkey_data *kdata = t->data;
1265 if (strcmp(keyword, "Input") == 0)
1266 return test_bin(value, &kdata->input, &kdata->input_len);
1267 if (strcmp(keyword, "Output") == 0)
1268 return test_bin(value, &kdata->output, &kdata->output_len);
1269 if (strcmp(keyword, "Ctrl") == 0)
1270 return pkey_test_ctrl(kdata->ctx, value);
1274 static int pkey_test_run(struct evp_test *t)
1276 struct pkey_data *kdata = t->data;
1277 unsigned char *out = NULL;
1279 const char *err = "KEYOP_LENGTH_ERROR";
1280 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1281 kdata->input_len) <= 0)
1283 out = OPENSSL_malloc(out_len);
1285 fprintf(stderr, "Error allocating output buffer!\n");
1288 err = "KEYOP_ERROR";
1290 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1292 err = "KEYOP_LENGTH_MISMATCH";
1293 if (out_len != kdata->output_len)
1295 err = "KEYOP_MISMATCH";
1296 if (check_output(t, kdata->output, out, out_len))
1305 static int sign_test_init(struct evp_test *t, const char *name)
1307 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1310 static const struct evp_test_method psign_test_method = {
1318 static int verify_recover_test_init(struct evp_test *t, const char *name)
1320 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1321 EVP_PKEY_verify_recover);
1324 static const struct evp_test_method pverify_recover_test_method = {
1326 verify_recover_test_init,
1332 static int decrypt_test_init(struct evp_test *t, const char *name)
1334 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1338 static const struct evp_test_method pdecrypt_test_method = {
1346 static int verify_test_init(struct evp_test *t, const char *name)
1348 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1351 static int verify_test_run(struct evp_test *t)
1353 struct pkey_data *kdata = t->data;
1354 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1355 kdata->input, kdata->input_len) <= 0)
1356 t->err = "VERIFY_ERROR";
1360 static const struct evp_test_method pverify_test_method = {
1369 static int pderive_test_init(struct evp_test *t, const char *name)
1371 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1374 static int pderive_test_parse(struct evp_test *t,
1375 const char *keyword, const char *value)
1377 struct pkey_data *kdata = t->data;
1379 if (strcmp(keyword, "PeerKey") == 0) {
1381 if (find_key(&peer, value, t->public) == 0)
1383 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1387 if (strcmp(keyword, "SharedSecret") == 0)
1388 return test_bin(value, &kdata->output, &kdata->output_len);
1389 if (strcmp(keyword, "Ctrl") == 0)
1390 return pkey_test_ctrl(kdata->ctx, value);
1394 static int pderive_test_run(struct evp_test *t)
1396 struct pkey_data *kdata = t->data;
1397 unsigned char *out = NULL;
1399 const char *err = "INTERNAL_ERROR";
1401 out_len = kdata->output_len;
1402 out = OPENSSL_malloc(out_len);
1404 fprintf(stderr, "Error allocating output buffer!\n");
1407 err = "DERIVE_ERROR";
1408 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1410 err = "SHARED_SECRET_LENGTH_MISMATCH";
1411 if (out_len != kdata->output_len)
1413 err = "SHARED_SECRET_MISMATCH";
1414 if (check_output(t, kdata->output, out, out_len))
1423 static const struct evp_test_method pderive_test_method = {
1433 #define PBE_TYPE_SCRYPT 1
1434 #define PBE_TYPE_PBKDF2 2
1435 #define PBE_TYPE_PKCS12 3
1441 /* scrypt parameters */
1442 uint64_t N, r, p, maxmem;
1444 /* PKCS#12 parameters */
1449 unsigned char *pass;
1453 unsigned char *salt;
1456 /* Expected output */
1461 #ifndef OPENSSL_NO_SCRYPT
1462 static int scrypt_test_parse(struct evp_test *t,
1463 const char *keyword, const char *value)
1465 struct pbe_data *pdata = t->data;
1467 if (strcmp(keyword, "N") == 0)
1468 return test_uint64(value, &pdata->N);
1469 if (strcmp(keyword, "p") == 0)
1470 return test_uint64(value, &pdata->p);
1471 if (strcmp(keyword, "r") == 0)
1472 return test_uint64(value, &pdata->r);
1473 if (strcmp(keyword, "maxmem") == 0)
1474 return test_uint64(value, &pdata->maxmem);
1479 static int pbkdf2_test_parse(struct evp_test *t,
1480 const char *keyword, const char *value)
1482 struct pbe_data *pdata = t->data;
1484 if (strcmp(keyword, "iter") == 0) {
1485 pdata->iter = atoi(value);
1486 if (pdata->iter <= 0)
1490 if (strcmp(keyword, "MD") == 0) {
1491 pdata->md = EVP_get_digestbyname(value);
1492 if (pdata->md == NULL)
1499 static int pkcs12_test_parse(struct evp_test *t,
1500 const char *keyword, const char *value)
1502 struct pbe_data *pdata = t->data;
1504 if (strcmp(keyword, "id") == 0) {
1505 pdata->id = atoi(value);
1510 return pbkdf2_test_parse(t, keyword, value);
1513 static int pbe_test_init(struct evp_test *t, const char *alg)
1515 struct pbe_data *pdat;
1518 if (strcmp(alg, "scrypt") == 0) {
1519 #ifndef OPENSSL_NO_SCRYPT
1520 pbe_type = PBE_TYPE_SCRYPT;
1525 } else if (strcmp(alg, "pbkdf2") == 0) {
1526 pbe_type = PBE_TYPE_PBKDF2;
1527 } else if (strcmp(alg, "pkcs12") == 0) {
1528 pbe_type = PBE_TYPE_PKCS12;
1530 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1532 pdat = OPENSSL_malloc(sizeof(*pdat));
1533 pdat->pbe_type = pbe_type;
1547 static void pbe_test_cleanup(struct evp_test *t)
1549 struct pbe_data *pdat = t->data;
1550 test_free(pdat->pass);
1551 test_free(pdat->salt);
1552 test_free(pdat->key);
1555 static int pbe_test_parse(struct evp_test *t,
1556 const char *keyword, const char *value)
1558 struct pbe_data *pdata = t->data;
1560 if (strcmp(keyword, "Password") == 0)
1561 return test_bin(value, &pdata->pass, &pdata->pass_len);
1562 if (strcmp(keyword, "Salt") == 0)
1563 return test_bin(value, &pdata->salt, &pdata->salt_len);
1564 if (strcmp(keyword, "Key") == 0)
1565 return test_bin(value, &pdata->key, &pdata->key_len);
1566 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1567 return pbkdf2_test_parse(t, keyword, value);
1568 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1569 return pkcs12_test_parse(t, keyword, value);
1570 #ifndef OPENSSL_NO_SCRYPT
1571 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1572 return scrypt_test_parse(t, keyword, value);
1577 static int pbe_test_run(struct evp_test *t)
1579 struct pbe_data *pdata = t->data;
1580 const char *err = "INTERNAL_ERROR";
1583 key = OPENSSL_malloc(pdata->key_len);
1586 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1587 err = "PBKDF2_ERROR";
1588 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1589 pdata->salt, pdata->salt_len,
1590 pdata->iter, pdata->md,
1591 pdata->key_len, key) == 0)
1593 #ifndef OPENSSL_NO_SCRYPT
1594 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1595 err = "SCRYPT_ERROR";
1596 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1597 pdata->salt, pdata->salt_len,
1598 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1599 key, pdata->key_len) == 0)
1602 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1603 err = "PKCS12_ERROR";
1604 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1605 pdata->salt, pdata->salt_len,
1606 pdata->id, pdata->iter, pdata->key_len,
1607 key, pdata->md) == 0)
1610 err = "KEY_MISMATCH";
1611 if (check_output(t, pdata->key, key, pdata->key_len))
1620 static const struct evp_test_method pbe_test_method = {
1631 BASE64_CANONICAL_ENCODING = 0,
1632 BASE64_VALID_ENCODING = 1,
1633 BASE64_INVALID_ENCODING = 2
1634 } base64_encoding_type;
1636 struct encode_data {
1637 /* Input to encoding */
1638 unsigned char *input;
1640 /* Expected output */
1641 unsigned char *output;
1643 base64_encoding_type encoding;
1646 static int encode_test_init(struct evp_test *t, const char *encoding)
1648 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1650 if (strcmp(encoding, "canonical") == 0) {
1651 edata->encoding = BASE64_CANONICAL_ENCODING;
1652 } else if (strcmp(encoding, "valid") == 0) {
1653 edata->encoding = BASE64_VALID_ENCODING;
1654 } else if (strcmp(encoding, "invalid") == 0) {
1655 edata->encoding = BASE64_INVALID_ENCODING;
1656 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1657 if (t->expected_err == NULL)
1660 fprintf(stderr, "Bad encoding: %s. Should be one of "
1661 "{canonical, valid, invalid}\n", encoding);
1668 static void encode_test_cleanup(struct evp_test *t)
1670 struct encode_data *edata = t->data;
1671 test_free(edata->input);
1672 test_free(edata->output);
1673 memset(edata, 0, sizeof(*edata));
1676 static int encode_test_parse(struct evp_test *t,
1677 const char *keyword, const char *value)
1679 struct encode_data *edata = t->data;
1680 if (strcmp(keyword, "Input") == 0)
1681 return test_bin(value, &edata->input, &edata->input_len);
1682 if (strcmp(keyword, "Output") == 0)
1683 return test_bin(value, &edata->output, &edata->output_len);
1687 static int encode_test_run(struct evp_test *t)
1689 struct encode_data *edata = t->data;
1690 unsigned char *encode_out = NULL, *decode_out = NULL;
1691 int output_len, chunk_len;
1692 const char *err = "INTERNAL_ERROR";
1693 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1695 if (decode_ctx == NULL)
1698 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1699 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1700 if (encode_ctx == NULL)
1702 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1703 if (encode_out == NULL)
1706 EVP_EncodeInit(encode_ctx);
1707 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1708 edata->input, edata->input_len);
1709 output_len = chunk_len;
1711 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1712 output_len += chunk_len;
1714 EVP_ENCODE_CTX_free(encode_ctx);
1716 if (check_var_length_output(t, edata->output, edata->output_len,
1717 encode_out, output_len)) {
1718 err = "BAD_ENCODING";
1723 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1724 if (decode_out == NULL)
1727 EVP_DecodeInit(decode_ctx);
1728 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1729 edata->output_len) < 0) {
1730 err = "DECODE_ERROR";
1733 output_len = chunk_len;
1735 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1736 err = "DECODE_ERROR";
1739 output_len += chunk_len;
1741 if (edata->encoding != BASE64_INVALID_ENCODING &&
1742 check_var_length_output(t, edata->input, edata->input_len,
1743 decode_out, output_len)) {
1744 err = "BAD_DECODING";
1751 OPENSSL_free(encode_out);
1752 OPENSSL_free(decode_out);
1753 EVP_ENCODE_CTX_free(decode_ctx);
1757 static const struct evp_test_method encode_test_method = {
1760 encode_test_cleanup,
1765 /* KDF operations */
1768 /* Context for this operation */
1770 /* Expected output */
1771 unsigned char *output;
1776 * Perform public key operation setup: lookup key, allocated ctx and call
1777 * the appropriate initialisation function
1779 static int kdf_test_init(struct evp_test *t, const char *name)
1781 struct kdf_data *kdata;
1783 kdata = OPENSSL_malloc(sizeof(*kdata));
1787 kdata->output = NULL;
1789 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1790 if (kdata->ctx == NULL)
1792 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1797 static void kdf_test_cleanup(struct evp_test *t)
1799 struct kdf_data *kdata = t->data;
1800 OPENSSL_free(kdata->output);
1801 EVP_PKEY_CTX_free(kdata->ctx);
1804 static int kdf_test_parse(struct evp_test *t,
1805 const char *keyword, const char *value)
1807 struct kdf_data *kdata = t->data;
1808 if (strcmp(keyword, "Output") == 0)
1809 return test_bin(value, &kdata->output, &kdata->output_len);
1810 if (strncmp(keyword, "Ctrl", 4) == 0)
1811 return pkey_test_ctrl(kdata->ctx, value);
1815 static int kdf_test_run(struct evp_test *t)
1817 struct kdf_data *kdata = t->data;
1818 unsigned char *out = NULL;
1819 size_t out_len = kdata->output_len;
1820 const char *err = "INTERNAL_ERROR";
1821 out = OPENSSL_malloc(out_len);
1823 fprintf(stderr, "Error allocating output buffer!\n");
1826 err = "KDF_DERIVE_ERROR";
1827 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1829 err = "KDF_LENGTH_MISMATCH";
1830 if (out_len != kdata->output_len)
1832 err = "KDF_MISMATCH";
1833 if (check_output(t, kdata->output, out, out_len))
1842 static const struct evp_test_method kdf_test_method = {