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 /* Expected error function string */
215 /* Expected error reason string */
217 /* Number of tests */
221 /* Number of tests skipped */
223 /* If output mismatch expected and got value */
224 unsigned char *out_received;
225 size_t out_received_len;
226 unsigned char *out_expected;
227 size_t out_expected_len;
228 /* test specific data */
230 /* Current test should be skipped */
237 struct key_list *next;
240 /* Test method structure */
241 struct evp_test_method {
242 /* Name of test as it appears in file */
244 /* Initialise test for "alg" */
245 int (*init) (struct evp_test * t, const char *alg);
246 /* Clean up method */
247 void (*cleanup) (struct evp_test * t);
248 /* Test specific name value pair processing */
249 int (*parse) (struct evp_test * t, const char *name, const char *value);
250 /* Run the test itself */
251 int (*run_test) (struct evp_test * t);
254 static const struct evp_test_method digest_test_method, cipher_test_method;
255 static const struct evp_test_method mac_test_method;
256 static const struct evp_test_method psign_test_method, pverify_test_method;
257 static const struct evp_test_method pdecrypt_test_method;
258 static const struct evp_test_method pverify_recover_test_method;
259 static const struct evp_test_method pderive_test_method;
260 static const struct evp_test_method pbe_test_method;
261 static const struct evp_test_method encode_test_method;
262 static const struct evp_test_method kdf_test_method;
264 static const struct evp_test_method *evp_test_list[] = {
269 &pverify_test_method,
270 &pdecrypt_test_method,
271 &pverify_recover_test_method,
272 &pderive_test_method,
279 static const struct evp_test_method *evp_find_test(const char *name)
281 const struct evp_test_method **tt;
283 for (tt = evp_test_list; *tt; tt++) {
284 if (strcmp(name, (*tt)->name) == 0)
290 static void hex_print(const char *name, const unsigned char *buf, size_t len)
293 fprintf(stderr, "%s ", name);
294 for (i = 0; i < len; i++)
295 fprintf(stderr, "%02X", buf[i]);
299 static void free_expected(struct evp_test *t)
301 OPENSSL_free(t->expected_err);
302 t->expected_err = NULL;
303 OPENSSL_free(t->func);
305 OPENSSL_free(t->reason);
307 OPENSSL_free(t->out_expected);
308 OPENSSL_free(t->out_received);
309 t->out_expected = NULL;
310 t->out_received = NULL;
311 t->out_expected_len = 0;
312 t->out_received_len = 0;
317 static void print_expected(struct evp_test *t)
319 if (t->out_expected == NULL && t->out_received == NULL)
321 hex_print("Expected:", t->out_expected, t->out_expected_len);
322 hex_print("Got: ", t->out_received, t->out_received_len);
326 static int check_test_error(struct evp_test *t)
331 if (!t->err && !t->expected_err)
333 if (t->err && !t->expected_err) {
334 if (t->aux_err != NULL) {
335 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
336 t->start_line, t->aux_err, t->err);
338 fprintf(stderr, "Test line %d: unexpected error %s\n",
339 t->start_line, t->err);
344 if (!t->err && t->expected_err) {
345 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
346 t->start_line, t->expected_err);
350 if (strcmp(t->err, t->expected_err) != 0) {
351 fprintf(stderr, "Test line %d: expecting %s got %s\n",
352 t->start_line, t->expected_err, t->err);
356 if (t->func == NULL && t->reason == NULL)
359 if (t->func == NULL || t->reason == NULL) {
360 fprintf(stderr, "Test line %d: missing function or reason code\n",
365 err = ERR_peek_error();
367 fprintf(stderr, "Test line %d, expected error \"%s:%s\" not set\n",
368 t->start_line, t->func, t->reason);
372 func = ERR_func_error_string(err);
373 reason = ERR_reason_error_string(err);
375 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
378 fprintf(stderr, "Test line %d: expected error \"%s:%s\", got \"%s:%s\"\n",
379 t->start_line, t->func, t->reason, func, reason);
384 /* Setup a new test, run any existing test */
386 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
388 /* If we already have a test set up run it */
395 if (t->err == NULL && t->meth->run_test(t) != 1) {
396 fprintf(stderr, "%s test error line %d\n",
397 t->meth->name, t->start_line);
400 if (!check_test_error(t)) {
402 ERR_print_errors_fp(stderr);
408 if (t->data != NULL) {
410 OPENSSL_free(t->data);
413 OPENSSL_free(t->expected_err);
414 t->expected_err = NULL;
421 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
423 for (; lst; lst = lst->next) {
424 if (strcmp(lst->name, name) == 0) {
433 static void free_key_list(struct key_list *lst)
435 while (lst != NULL) {
436 struct key_list *ltmp;
437 EVP_PKEY_free(lst->key);
438 OPENSSL_free(lst->name);
445 static int check_unsupported()
447 long err = ERR_peek_error();
448 if (ERR_GET_LIB(err) == ERR_LIB_EVP
449 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
456 static int process_test(struct evp_test *t, char *buf, int verbose)
458 char *keyword = NULL, *value = NULL;
459 int rv = 0, add_key = 0;
461 struct key_list **lst = NULL, *key = NULL;
463 const struct evp_test_method *tmeth = NULL;
466 if (!parse_line(&keyword, &value, buf))
468 if (strcmp(keyword, "PrivateKey") == 0) {
469 save_pos = BIO_tell(t->in);
470 pk = PEM_read_bio_PrivateKey(t->in, NULL, 0, NULL);
471 if (pk == NULL && !check_unsupported()) {
472 fprintf(stderr, "Error reading private key %s\n", value);
473 ERR_print_errors_fp(stderr);
479 if (strcmp(keyword, "PublicKey") == 0) {
480 save_pos = BIO_tell(t->in);
481 pk = PEM_read_bio_PUBKEY(t->in, NULL, 0, NULL);
482 if (pk == NULL && !check_unsupported()) {
483 fprintf(stderr, "Error reading public key %s\n", value);
484 ERR_print_errors_fp(stderr);
490 /* If we have a key add to list */
493 if (find_key(NULL, value, *lst)) {
494 fprintf(stderr, "Duplicate key %s\n", value);
497 key = OPENSSL_malloc(sizeof(*key));
500 key->name = OPENSSL_strdup(value);
504 /* Rewind input, read to end and update line numbers */
505 (void)BIO_seek(t->in, save_pos);
506 while (BIO_gets(t->in,tmpbuf, sizeof(tmpbuf))) {
508 if (strncmp(tmpbuf, "-----END", 8) == 0)
511 fprintf(stderr, "Can't find key end\n");
515 /* See if keyword corresponds to a test start */
516 tmeth = evp_find_test(keyword);
518 if (!setup_test(t, tmeth))
520 t->start_line = t->line;
522 if (!tmeth->init(t, value)) {
523 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
527 } else if (t->skip) {
529 } else if (strcmp(keyword, "Result") == 0) {
530 if (t->expected_err) {
531 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
534 t->expected_err = OPENSSL_strdup(value);
535 if (t->expected_err == NULL)
537 } else if (strcmp(keyword, "Function") == 0) {
538 if (t->func != NULL) {
539 fprintf(stderr, "Line %d: multiple function lines\n", t->line);
542 t->func = OPENSSL_strdup(value);
545 } else if (strcmp(keyword, "Reason") == 0) {
546 if (t->reason != NULL) {
547 fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
550 t->reason = OPENSSL_strdup(value);
551 if (t->reason == NULL)
554 /* Must be test specific line: try to parse it */
556 rv = t->meth->parse(t, keyword, value);
559 fprintf(stderr, "line %d: unexpected keyword %s\n",
563 fprintf(stderr, "line %d: error processing keyword %s\n",
571 static int check_var_length_output(struct evp_test *t,
572 const unsigned char *expected,
574 const unsigned char *received,
577 if (expected_len == received_len &&
578 memcmp(expected, received, expected_len) == 0) {
582 /* The result printing code expects a non-NULL buffer. */
583 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
584 t->out_expected_len = expected_len;
585 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
586 t->out_received_len = received_len;
587 if (t->out_expected == NULL || t->out_received == NULL) {
588 fprintf(stderr, "Memory allocation error!\n");
594 static int check_output(struct evp_test *t,
595 const unsigned char *expected,
596 const unsigned char *received,
599 return check_var_length_output(t, expected, len, received, len);
602 int main(int argc, char **argv)
609 fprintf(stderr, "usage: evp_test testfile.txt\n");
613 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
615 memset(&t, 0, sizeof(t));
617 in = BIO_new_file(argv[1], "r");
619 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
624 while (BIO_gets(in, buf, sizeof(buf))) {
626 if (!process_test(&t, buf, 0))
629 /* Run any final test we have */
630 if (!setup_test(&t, NULL))
632 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
633 t.ntests, t.errors, t.nskip);
634 free_key_list(t.public);
635 free_key_list(t.private);
638 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
639 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
647 static void test_free(void *d)
652 /* Message digest tests */
655 /* Digest this test is for */
656 const EVP_MD *digest;
657 /* Input to digest */
658 unsigned char *input;
660 /* Repeat count for input */
662 /* Expected output */
663 unsigned char *output;
667 static int digest_test_init(struct evp_test *t, const char *alg)
669 const EVP_MD *digest;
670 struct digest_data *mdat;
671 digest = EVP_get_digestbyname(alg);
673 /* If alg has an OID assume disabled algorithm */
674 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
680 mdat = OPENSSL_malloc(sizeof(*mdat));
681 mdat->digest = digest;
689 static void digest_test_cleanup(struct evp_test *t)
691 struct digest_data *mdat = t->data;
692 test_free(mdat->input);
693 test_free(mdat->output);
696 static int digest_test_parse(struct evp_test *t,
697 const char *keyword, const char *value)
699 struct digest_data *mdata = t->data;
700 if (strcmp(keyword, "Input") == 0)
701 return test_bin(value, &mdata->input, &mdata->input_len);
702 if (strcmp(keyword, "Output") == 0)
703 return test_bin(value, &mdata->output, &mdata->output_len);
704 if (strcmp(keyword, "Count") == 0) {
705 long nrpt = atoi(value);
708 mdata->nrpt = (size_t)nrpt;
714 static int digest_test_run(struct evp_test *t)
716 struct digest_data *mdata = t->data;
718 const char *err = "INTERNAL_ERROR";
720 unsigned char md[EVP_MAX_MD_SIZE];
722 mctx = EVP_MD_CTX_new();
725 err = "DIGESTINIT_ERROR";
726 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
728 err = "DIGESTUPDATE_ERROR";
729 for (i = 0; i < mdata->nrpt; i++) {
730 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
733 err = "DIGESTFINAL_ERROR";
734 if (!EVP_DigestFinal(mctx, md, &md_len))
736 err = "DIGEST_LENGTH_MISMATCH";
737 if (md_len != mdata->output_len)
739 err = "DIGEST_MISMATCH";
740 if (check_output(t, mdata->output, md, md_len))
744 EVP_MD_CTX_free(mctx);
749 static const struct evp_test_method digest_test_method = {
759 const EVP_CIPHER *cipher;
761 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
767 unsigned char *plaintext;
768 size_t plaintext_len;
769 unsigned char *ciphertext;
770 size_t ciphertext_len;
778 static int cipher_test_init(struct evp_test *t, const char *alg)
780 const EVP_CIPHER *cipher;
781 struct cipher_data *cdat = t->data;
782 cipher = EVP_get_cipherbyname(alg);
784 /* If alg has an OID assume disabled algorithm */
785 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
791 cdat = OPENSSL_malloc(sizeof(*cdat));
792 cdat->cipher = cipher;
796 cdat->ciphertext = NULL;
797 cdat->plaintext = NULL;
801 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
802 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
803 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
804 cdat->aead = EVP_CIPHER_mode(cipher);
805 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
813 static void cipher_test_cleanup(struct evp_test *t)
815 struct cipher_data *cdat = t->data;
816 test_free(cdat->key);
818 test_free(cdat->ciphertext);
819 test_free(cdat->plaintext);
820 test_free(cdat->aad);
821 test_free(cdat->tag);
824 static int cipher_test_parse(struct evp_test *t, const char *keyword,
827 struct cipher_data *cdat = t->data;
828 if (strcmp(keyword, "Key") == 0)
829 return test_bin(value, &cdat->key, &cdat->key_len);
830 if (strcmp(keyword, "IV") == 0)
831 return test_bin(value, &cdat->iv, &cdat->iv_len);
832 if (strcmp(keyword, "Plaintext") == 0)
833 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
834 if (strcmp(keyword, "Ciphertext") == 0)
835 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
837 if (strcmp(keyword, "AAD") == 0)
838 return test_bin(value, &cdat->aad, &cdat->aad_len);
839 if (strcmp(keyword, "Tag") == 0)
840 return test_bin(value, &cdat->tag, &cdat->tag_len);
843 if (strcmp(keyword, "Operation") == 0) {
844 if (strcmp(value, "ENCRYPT") == 0)
846 else if (strcmp(value, "DECRYPT") == 0)
855 static int cipher_test_enc(struct evp_test *t, int enc,
856 size_t out_misalign, size_t inp_misalign)
858 struct cipher_data *cdat = t->data;
859 unsigned char *in, *out, *tmp = NULL;
860 size_t in_len, out_len;
862 EVP_CIPHER_CTX *ctx = NULL;
864 err = "INTERNAL_ERROR";
865 ctx = EVP_CIPHER_CTX_new();
868 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
870 in = cdat->plaintext;
871 in_len = cdat->plaintext_len;
872 out = cdat->ciphertext;
873 out_len = cdat->ciphertext_len;
875 in = cdat->ciphertext;
876 in_len = cdat->ciphertext_len;
877 out = cdat->plaintext;
878 out_len = cdat->plaintext_len;
880 if (inp_misalign == (size_t)-1) {
882 * Exercise in-place encryption
884 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
887 in = memcpy(tmp + out_misalign, in, in_len);
889 inp_misalign += 16 - ((out_misalign + in_len) & 15);
891 * 'tmp' will store both output and copy of input. We make the copy
892 * of input to specifically aligned part of 'tmp'. So we just
893 * figured out how much padding would ensure the required alignment,
894 * now we allocate extended buffer and finally copy the input just
895 * past inp_misalign in expression below. Output will be written
896 * past out_misalign...
898 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
899 inp_misalign + in_len);
902 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
903 inp_misalign, in, in_len);
905 err = "CIPHERINIT_ERROR";
906 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
908 err = "INVALID_IV_LENGTH";
911 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
914 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
920 * If encrypting or OCB just set tag length initially, otherwise
921 * set tag length and value.
923 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
924 err = "TAG_LENGTH_SET_ERROR";
927 err = "TAG_SET_ERROR";
930 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
931 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
937 err = "INVALID_KEY_LENGTH";
938 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
940 err = "KEY_SET_ERROR";
941 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
944 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
945 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
946 cdat->tag_len, cdat->tag)) {
947 err = "TAG_SET_ERROR";
952 if (cdat->aead == EVP_CIPH_CCM_MODE) {
953 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
954 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
959 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
960 err = "AAD_SET_ERROR";
964 EVP_CIPHER_CTX_set_padding(ctx, 0);
965 err = "CIPHERUPDATE_ERROR";
966 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
968 if (cdat->aead == EVP_CIPH_CCM_MODE)
971 err = "CIPHERFINAL_ERROR";
972 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
975 err = "LENGTH_MISMATCH";
976 if (out_len != (size_t)(tmplen + tmpflen))
978 err = "VALUE_MISMATCH";
979 if (check_output(t, out, tmp + out_misalign, out_len))
981 if (enc && cdat->aead) {
982 unsigned char rtag[16];
983 if (cdat->tag_len > sizeof(rtag)) {
984 err = "TAG_LENGTH_INTERNAL_ERROR";
987 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
988 cdat->tag_len, rtag)) {
989 err = "TAG_RETRIEVE_ERROR";
992 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
993 err = "TAG_VALUE_MISMATCH";
1000 EVP_CIPHER_CTX_free(ctx);
1005 static int cipher_test_run(struct evp_test *t)
1007 struct cipher_data *cdat = t->data;
1009 size_t out_misalign, inp_misalign;
1015 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1016 /* IV is optional and usually omitted in wrap mode */
1017 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1022 if (cdat->aead && !cdat->tag) {
1026 for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
1027 static char aux_err[64];
1028 t->aux_err = aux_err;
1029 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1030 if (inp_misalign == (size_t)-1) {
1031 /* kludge: inp_misalign == -1 means "exercise in-place" */
1032 BIO_snprintf(aux_err, sizeof(aux_err), "%s in-place",
1033 out_misalign ? "misaligned" : "aligned");
1035 BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input",
1036 out_misalign ? "misaligned" : "aligned",
1037 inp_misalign ? "misaligned" : "aligned");
1040 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign);
1041 /* Not fatal errors: return */
1048 if (cdat->enc != 1) {
1049 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign);
1050 /* Not fatal errors: return */
1064 static const struct evp_test_method cipher_test_method = {
1067 cipher_test_cleanup,
1075 /* Algorithm string for this MAC */
1081 unsigned char *input;
1083 /* Expected output */
1084 unsigned char *output;
1088 static int mac_test_init(struct evp_test *t, const char *alg)
1091 struct mac_data *mdat;
1092 if (strcmp(alg, "HMAC") == 0) {
1093 type = EVP_PKEY_HMAC;
1094 } else if (strcmp(alg, "CMAC") == 0) {
1095 #ifndef OPENSSL_NO_CMAC
1096 type = EVP_PKEY_CMAC;
1104 mdat = OPENSSL_malloc(sizeof(*mdat));
1109 mdat->output = NULL;
1114 static void mac_test_cleanup(struct evp_test *t)
1116 struct mac_data *mdat = t->data;
1117 test_free(mdat->alg);
1118 test_free(mdat->key);
1119 test_free(mdat->input);
1120 test_free(mdat->output);
1123 static int mac_test_parse(struct evp_test *t,
1124 const char *keyword, const char *value)
1126 struct mac_data *mdata = t->data;
1127 if (strcmp(keyword, "Key") == 0)
1128 return test_bin(value, &mdata->key, &mdata->key_len);
1129 if (strcmp(keyword, "Algorithm") == 0) {
1130 mdata->alg = OPENSSL_strdup(value);
1135 if (strcmp(keyword, "Input") == 0)
1136 return test_bin(value, &mdata->input, &mdata->input_len);
1137 if (strcmp(keyword, "Output") == 0)
1138 return test_bin(value, &mdata->output, &mdata->output_len);
1142 static int mac_test_run(struct evp_test *t)
1144 struct mac_data *mdata = t->data;
1145 const char *err = "INTERNAL_ERROR";
1146 EVP_MD_CTX *mctx = NULL;
1147 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1148 EVP_PKEY *key = NULL;
1149 const EVP_MD *md = NULL;
1150 unsigned char *mac = NULL;
1153 #ifdef OPENSSL_NO_DES
1154 if (strstr(mdata->alg, "DES") != NULL) {
1161 err = "MAC_PKEY_CTX_ERROR";
1162 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1166 err = "MAC_KEYGEN_INIT_ERROR";
1167 if (EVP_PKEY_keygen_init(genctx) <= 0)
1169 if (mdata->type == EVP_PKEY_CMAC) {
1170 err = "MAC_ALGORITHM_SET_ERROR";
1171 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1175 err = "MAC_KEY_SET_ERROR";
1176 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1179 err = "MAC_KEY_GENERATE_ERROR";
1180 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1182 if (mdata->type == EVP_PKEY_HMAC) {
1183 err = "MAC_ALGORITHM_SET_ERROR";
1184 md = EVP_get_digestbyname(mdata->alg);
1188 mctx = EVP_MD_CTX_new();
1191 err = "DIGESTSIGNINIT_ERROR";
1192 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1195 err = "DIGESTSIGNUPDATE_ERROR";
1196 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1198 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1199 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1201 mac = OPENSSL_malloc(mac_len);
1203 fprintf(stderr, "Error allocating mac buffer!\n");
1206 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1208 err = "MAC_LENGTH_MISMATCH";
1209 if (mac_len != mdata->output_len)
1211 err = "MAC_MISMATCH";
1212 if (check_output(t, mdata->output, mac, mac_len))
1216 EVP_MD_CTX_free(mctx);
1218 EVP_PKEY_CTX_free(genctx);
1224 static const struct evp_test_method mac_test_method = {
1233 * Public key operations. These are all very similar and can share
1234 * a lot of common code.
1238 /* Context for this operation */
1240 /* Key operation to perform */
1241 int (*keyop) (EVP_PKEY_CTX *ctx,
1242 unsigned char *sig, size_t *siglen,
1243 const unsigned char *tbs, size_t tbslen);
1245 unsigned char *input;
1247 /* Expected output */
1248 unsigned char *output;
1253 * Perform public key operation setup: lookup key, allocated ctx and call
1254 * the appropriate initialisation function
1256 static int pkey_test_init(struct evp_test *t, const char *name,
1258 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1259 int (*keyop) (EVP_PKEY_CTX *ctx,
1260 unsigned char *sig, size_t *siglen,
1261 const unsigned char *tbs,
1265 struct pkey_data *kdata;
1266 EVP_PKEY *pkey = NULL;
1269 rv = find_key(&pkey, name, t->public);
1271 rv = find_key(&pkey, name, t->private);
1272 if (!rv || pkey == NULL) {
1277 kdata = OPENSSL_malloc(sizeof(*kdata));
1279 EVP_PKEY_free(pkey);
1283 kdata->input = NULL;
1284 kdata->output = NULL;
1285 kdata->keyop = keyop;
1287 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1290 if (keyopinit(kdata->ctx) <= 0)
1291 t->err = "KEYOP_INIT_ERROR";
1295 static void pkey_test_cleanup(struct evp_test *t)
1297 struct pkey_data *kdata = t->data;
1299 OPENSSL_free(kdata->input);
1300 OPENSSL_free(kdata->output);
1301 EVP_PKEY_CTX_free(kdata->ctx);
1304 static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1310 tmpval = OPENSSL_strdup(value);
1313 p = strchr(tmpval, ':');
1316 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1318 t->err = "PKEY_CTRL_INVALID";
1320 } else if (p != NULL && rv <= 0) {
1321 /* If p has an OID and lookup fails assume disabled algorithm */
1322 int nid = OBJ_sn2nid(p);
1323 if (nid == NID_undef)
1324 nid = OBJ_ln2nid(p);
1325 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1326 EVP_get_cipherbynid(nid) == NULL) {
1330 t->err = "PKEY_CTRL_ERROR";
1334 OPENSSL_free(tmpval);
1338 static int pkey_test_parse(struct evp_test *t,
1339 const char *keyword, const char *value)
1341 struct pkey_data *kdata = t->data;
1342 if (strcmp(keyword, "Input") == 0)
1343 return test_bin(value, &kdata->input, &kdata->input_len);
1344 if (strcmp(keyword, "Output") == 0)
1345 return test_bin(value, &kdata->output, &kdata->output_len);
1346 if (strcmp(keyword, "Ctrl") == 0)
1347 return pkey_test_ctrl(t, kdata->ctx, value);
1351 static int pkey_test_run(struct evp_test *t)
1353 struct pkey_data *kdata = t->data;
1354 unsigned char *out = NULL;
1356 const char *err = "KEYOP_LENGTH_ERROR";
1357 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1358 kdata->input_len) <= 0)
1360 out = OPENSSL_malloc(out_len);
1362 fprintf(stderr, "Error allocating output buffer!\n");
1365 err = "KEYOP_ERROR";
1367 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1369 err = "KEYOP_LENGTH_MISMATCH";
1370 if (out_len != kdata->output_len)
1372 err = "KEYOP_MISMATCH";
1373 if (check_output(t, kdata->output, out, out_len))
1382 static int sign_test_init(struct evp_test *t, const char *name)
1384 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1387 static const struct evp_test_method psign_test_method = {
1395 static int verify_recover_test_init(struct evp_test *t, const char *name)
1397 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1398 EVP_PKEY_verify_recover);
1401 static const struct evp_test_method pverify_recover_test_method = {
1403 verify_recover_test_init,
1409 static int decrypt_test_init(struct evp_test *t, const char *name)
1411 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1415 static const struct evp_test_method pdecrypt_test_method = {
1423 static int verify_test_init(struct evp_test *t, const char *name)
1425 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1428 static int verify_test_run(struct evp_test *t)
1430 struct pkey_data *kdata = t->data;
1431 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1432 kdata->input, kdata->input_len) <= 0)
1433 t->err = "VERIFY_ERROR";
1437 static const struct evp_test_method pverify_test_method = {
1446 static int pderive_test_init(struct evp_test *t, const char *name)
1448 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1451 static int pderive_test_parse(struct evp_test *t,
1452 const char *keyword, const char *value)
1454 struct pkey_data *kdata = t->data;
1456 if (strcmp(keyword, "PeerKey") == 0) {
1458 if (find_key(&peer, value, t->public) == 0)
1460 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1464 if (strcmp(keyword, "SharedSecret") == 0)
1465 return test_bin(value, &kdata->output, &kdata->output_len);
1466 if (strcmp(keyword, "Ctrl") == 0)
1467 return pkey_test_ctrl(t, kdata->ctx, value);
1471 static int pderive_test_run(struct evp_test *t)
1473 struct pkey_data *kdata = t->data;
1474 unsigned char *out = NULL;
1476 const char *err = "INTERNAL_ERROR";
1478 out_len = kdata->output_len;
1479 out = OPENSSL_malloc(out_len);
1481 fprintf(stderr, "Error allocating output buffer!\n");
1484 err = "DERIVE_ERROR";
1485 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1487 err = "SHARED_SECRET_LENGTH_MISMATCH";
1488 if (out_len != kdata->output_len)
1490 err = "SHARED_SECRET_MISMATCH";
1491 if (check_output(t, kdata->output, out, out_len))
1500 static const struct evp_test_method pderive_test_method = {
1510 #define PBE_TYPE_SCRYPT 1
1511 #define PBE_TYPE_PBKDF2 2
1512 #define PBE_TYPE_PKCS12 3
1518 /* scrypt parameters */
1519 uint64_t N, r, p, maxmem;
1521 /* PKCS#12 parameters */
1526 unsigned char *pass;
1530 unsigned char *salt;
1533 /* Expected output */
1538 #ifndef OPENSSL_NO_SCRYPT
1539 static int scrypt_test_parse(struct evp_test *t,
1540 const char *keyword, const char *value)
1542 struct pbe_data *pdata = t->data;
1544 if (strcmp(keyword, "N") == 0)
1545 return test_uint64(value, &pdata->N);
1546 if (strcmp(keyword, "p") == 0)
1547 return test_uint64(value, &pdata->p);
1548 if (strcmp(keyword, "r") == 0)
1549 return test_uint64(value, &pdata->r);
1550 if (strcmp(keyword, "maxmem") == 0)
1551 return test_uint64(value, &pdata->maxmem);
1556 static int pbkdf2_test_parse(struct evp_test *t,
1557 const char *keyword, const char *value)
1559 struct pbe_data *pdata = t->data;
1561 if (strcmp(keyword, "iter") == 0) {
1562 pdata->iter = atoi(value);
1563 if (pdata->iter <= 0)
1567 if (strcmp(keyword, "MD") == 0) {
1568 pdata->md = EVP_get_digestbyname(value);
1569 if (pdata->md == NULL)
1576 static int pkcs12_test_parse(struct evp_test *t,
1577 const char *keyword, const char *value)
1579 struct pbe_data *pdata = t->data;
1581 if (strcmp(keyword, "id") == 0) {
1582 pdata->id = atoi(value);
1587 return pbkdf2_test_parse(t, keyword, value);
1590 static int pbe_test_init(struct evp_test *t, const char *alg)
1592 struct pbe_data *pdat;
1595 if (strcmp(alg, "scrypt") == 0) {
1596 #ifndef OPENSSL_NO_SCRYPT
1597 pbe_type = PBE_TYPE_SCRYPT;
1602 } else if (strcmp(alg, "pbkdf2") == 0) {
1603 pbe_type = PBE_TYPE_PBKDF2;
1604 } else if (strcmp(alg, "pkcs12") == 0) {
1605 pbe_type = PBE_TYPE_PKCS12;
1607 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1609 pdat = OPENSSL_malloc(sizeof(*pdat));
1610 pdat->pbe_type = pbe_type;
1624 static void pbe_test_cleanup(struct evp_test *t)
1626 struct pbe_data *pdat = t->data;
1627 test_free(pdat->pass);
1628 test_free(pdat->salt);
1629 test_free(pdat->key);
1632 static int pbe_test_parse(struct evp_test *t,
1633 const char *keyword, const char *value)
1635 struct pbe_data *pdata = t->data;
1637 if (strcmp(keyword, "Password") == 0)
1638 return test_bin(value, &pdata->pass, &pdata->pass_len);
1639 if (strcmp(keyword, "Salt") == 0)
1640 return test_bin(value, &pdata->salt, &pdata->salt_len);
1641 if (strcmp(keyword, "Key") == 0)
1642 return test_bin(value, &pdata->key, &pdata->key_len);
1643 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1644 return pbkdf2_test_parse(t, keyword, value);
1645 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1646 return pkcs12_test_parse(t, keyword, value);
1647 #ifndef OPENSSL_NO_SCRYPT
1648 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1649 return scrypt_test_parse(t, keyword, value);
1654 static int pbe_test_run(struct evp_test *t)
1656 struct pbe_data *pdata = t->data;
1657 const char *err = "INTERNAL_ERROR";
1660 key = OPENSSL_malloc(pdata->key_len);
1663 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1664 err = "PBKDF2_ERROR";
1665 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1666 pdata->salt, pdata->salt_len,
1667 pdata->iter, pdata->md,
1668 pdata->key_len, key) == 0)
1670 #ifndef OPENSSL_NO_SCRYPT
1671 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1672 err = "SCRYPT_ERROR";
1673 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1674 pdata->salt, pdata->salt_len,
1675 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1676 key, pdata->key_len) == 0)
1679 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1680 err = "PKCS12_ERROR";
1681 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1682 pdata->salt, pdata->salt_len,
1683 pdata->id, pdata->iter, pdata->key_len,
1684 key, pdata->md) == 0)
1687 err = "KEY_MISMATCH";
1688 if (check_output(t, pdata->key, key, pdata->key_len))
1697 static const struct evp_test_method pbe_test_method = {
1708 BASE64_CANONICAL_ENCODING = 0,
1709 BASE64_VALID_ENCODING = 1,
1710 BASE64_INVALID_ENCODING = 2
1711 } base64_encoding_type;
1713 struct encode_data {
1714 /* Input to encoding */
1715 unsigned char *input;
1717 /* Expected output */
1718 unsigned char *output;
1720 base64_encoding_type encoding;
1723 static int encode_test_init(struct evp_test *t, const char *encoding)
1725 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1727 if (strcmp(encoding, "canonical") == 0) {
1728 edata->encoding = BASE64_CANONICAL_ENCODING;
1729 } else if (strcmp(encoding, "valid") == 0) {
1730 edata->encoding = BASE64_VALID_ENCODING;
1731 } else if (strcmp(encoding, "invalid") == 0) {
1732 edata->encoding = BASE64_INVALID_ENCODING;
1733 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1734 if (t->expected_err == NULL)
1737 fprintf(stderr, "Bad encoding: %s. Should be one of "
1738 "{canonical, valid, invalid}\n", encoding);
1745 static void encode_test_cleanup(struct evp_test *t)
1747 struct encode_data *edata = t->data;
1748 test_free(edata->input);
1749 test_free(edata->output);
1750 memset(edata, 0, sizeof(*edata));
1753 static int encode_test_parse(struct evp_test *t,
1754 const char *keyword, const char *value)
1756 struct encode_data *edata = t->data;
1757 if (strcmp(keyword, "Input") == 0)
1758 return test_bin(value, &edata->input, &edata->input_len);
1759 if (strcmp(keyword, "Output") == 0)
1760 return test_bin(value, &edata->output, &edata->output_len);
1764 static int encode_test_run(struct evp_test *t)
1766 struct encode_data *edata = t->data;
1767 unsigned char *encode_out = NULL, *decode_out = NULL;
1768 int output_len, chunk_len;
1769 const char *err = "INTERNAL_ERROR";
1770 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1772 if (decode_ctx == NULL)
1775 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1776 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1777 if (encode_ctx == NULL)
1779 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1780 if (encode_out == NULL)
1783 EVP_EncodeInit(encode_ctx);
1784 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1785 edata->input, edata->input_len);
1786 output_len = chunk_len;
1788 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1789 output_len += chunk_len;
1791 EVP_ENCODE_CTX_free(encode_ctx);
1793 if (check_var_length_output(t, edata->output, edata->output_len,
1794 encode_out, output_len)) {
1795 err = "BAD_ENCODING";
1800 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1801 if (decode_out == NULL)
1804 EVP_DecodeInit(decode_ctx);
1805 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1806 edata->output_len) < 0) {
1807 err = "DECODE_ERROR";
1810 output_len = chunk_len;
1812 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1813 err = "DECODE_ERROR";
1816 output_len += chunk_len;
1818 if (edata->encoding != BASE64_INVALID_ENCODING &&
1819 check_var_length_output(t, edata->input, edata->input_len,
1820 decode_out, output_len)) {
1821 err = "BAD_DECODING";
1828 OPENSSL_free(encode_out);
1829 OPENSSL_free(decode_out);
1830 EVP_ENCODE_CTX_free(decode_ctx);
1834 static const struct evp_test_method encode_test_method = {
1837 encode_test_cleanup,
1842 /* KDF operations */
1845 /* Context for this operation */
1847 /* Expected output */
1848 unsigned char *output;
1853 * Perform public key operation setup: lookup key, allocated ctx and call
1854 * the appropriate initialisation function
1856 static int kdf_test_init(struct evp_test *t, const char *name)
1858 struct kdf_data *kdata;
1860 kdata = OPENSSL_malloc(sizeof(*kdata));
1864 kdata->output = NULL;
1866 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1867 if (kdata->ctx == NULL)
1869 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1874 static void kdf_test_cleanup(struct evp_test *t)
1876 struct kdf_data *kdata = t->data;
1877 OPENSSL_free(kdata->output);
1878 EVP_PKEY_CTX_free(kdata->ctx);
1881 static int kdf_test_parse(struct evp_test *t,
1882 const char *keyword, const char *value)
1884 struct kdf_data *kdata = t->data;
1885 if (strcmp(keyword, "Output") == 0)
1886 return test_bin(value, &kdata->output, &kdata->output_len);
1887 if (strncmp(keyword, "Ctrl", 4) == 0)
1888 return pkey_test_ctrl(t, kdata->ctx, value);
1892 static int kdf_test_run(struct evp_test *t)
1894 struct kdf_data *kdata = t->data;
1895 unsigned char *out = NULL;
1896 size_t out_len = kdata->output_len;
1897 const char *err = "INTERNAL_ERROR";
1898 out = OPENSSL_malloc(out_len);
1900 fprintf(stderr, "Error allocating output buffer!\n");
1903 err = "KDF_DERIVE_ERROR";
1904 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1906 err = "KDF_LENGTH_MISMATCH";
1907 if (out_len != kdata->output_len)
1909 err = "KDF_MISMATCH";
1910 if (check_output(t, kdata->output, out, out_len))
1919 static const struct evp_test_method kdf_test_method = {