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 */
358 if (t->meth->run_test(t) != 1) {
359 fprintf(stderr, "%s test error line %d\n",
360 t->meth->name, t->start_line);
363 if (!check_test_error(t)) {
365 ERR_print_errors_fp(stderr);
371 if (t->data != NULL) {
373 OPENSSL_free(t->data);
376 OPENSSL_free(t->expected_err);
377 t->expected_err = NULL;
384 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
386 for (; lst; lst = lst->next) {
387 if (strcmp(lst->name, name) == 0) {
396 static void free_key_list(struct key_list *lst)
398 while (lst != NULL) {
399 struct key_list *ltmp;
400 EVP_PKEY_free(lst->key);
401 OPENSSL_free(lst->name);
408 static int check_unsupported()
410 long err = ERR_peek_error();
411 if (ERR_GET_LIB(err) == ERR_LIB_EVP
412 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
419 static int process_test(struct evp_test *t, char *buf, int verbose)
421 char *keyword = NULL, *value = NULL;
422 int rv = 0, add_key = 0;
424 struct key_list **lst = NULL, *key = NULL;
426 const struct evp_test_method *tmeth = NULL;
429 if (!parse_line(&keyword, &value, buf))
431 if (strcmp(keyword, "PrivateKey") == 0) {
432 save_pos = BIO_tell(t->in);
433 pk = PEM_read_bio_PrivateKey(t->in, NULL, 0, NULL);
434 if (pk == NULL && !check_unsupported()) {
435 fprintf(stderr, "Error reading private key %s\n", value);
436 ERR_print_errors_fp(stderr);
442 if (strcmp(keyword, "PublicKey") == 0) {
443 save_pos = BIO_tell(t->in);
444 pk = PEM_read_bio_PUBKEY(t->in, NULL, 0, NULL);
445 if (pk == NULL && !check_unsupported()) {
446 fprintf(stderr, "Error reading public key %s\n", value);
447 ERR_print_errors_fp(stderr);
453 /* If we have a key add to list */
456 if (find_key(NULL, value, *lst)) {
457 fprintf(stderr, "Duplicate key %s\n", value);
460 key = OPENSSL_malloc(sizeof(*key));
463 key->name = OPENSSL_strdup(value);
467 /* Rewind input, read to end and update line numbers */
468 (void)BIO_seek(t->in, save_pos);
469 while (BIO_gets(t->in,tmpbuf, sizeof(tmpbuf))) {
471 if (strncmp(tmpbuf, "-----END", 8) == 0)
474 fprintf(stderr, "Can't find key end\n");
478 /* See if keyword corresponds to a test start */
479 tmeth = evp_find_test(keyword);
481 if (!setup_test(t, tmeth))
483 t->start_line = t->line;
485 if (!tmeth->init(t, value)) {
486 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
490 } else if (t->skip) {
492 } else if (strcmp(keyword, "Result") == 0) {
493 if (t->expected_err) {
494 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
497 t->expected_err = OPENSSL_strdup(value);
498 if (!t->expected_err)
501 /* Must be test specific line: try to parse it */
503 rv = t->meth->parse(t, keyword, value);
506 fprintf(stderr, "line %d: unexpected keyword %s\n",
510 fprintf(stderr, "line %d: error processing keyword %s\n",
518 static int check_var_length_output(struct evp_test *t,
519 const unsigned char *expected,
521 const unsigned char *received,
524 if (expected_len == received_len &&
525 memcmp(expected, received, expected_len) == 0) {
529 /* The result printing code expects a non-NULL buffer. */
530 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
531 t->out_expected_len = expected_len;
532 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
533 t->out_received_len = received_len;
534 if (t->out_expected == NULL || t->out_received == NULL) {
535 fprintf(stderr, "Memory allocation error!\n");
541 static int check_output(struct evp_test *t,
542 const unsigned char *expected,
543 const unsigned char *received,
546 return check_var_length_output(t, expected, len, received, len);
549 int main(int argc, char **argv)
556 fprintf(stderr, "usage: evp_test testfile.txt\n");
560 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
562 memset(&t, 0, sizeof(t));
564 in = BIO_new_file(argv[1], "r");
566 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
570 while (BIO_gets(in, buf, sizeof(buf))) {
572 if (!process_test(&t, buf, 0))
575 /* Run any final test we have */
576 if (!setup_test(&t, NULL))
578 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
579 t.ntests, t.errors, t.nskip);
580 free_key_list(t.public);
581 free_key_list(t.private);
584 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
585 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
593 static void test_free(void *d)
598 /* Message digest tests */
601 /* Digest this test is for */
602 const EVP_MD *digest;
603 /* Input to digest */
604 unsigned char *input;
606 /* Repeat count for input */
608 /* Expected output */
609 unsigned char *output;
613 static int digest_test_init(struct evp_test *t, const char *alg)
615 const EVP_MD *digest;
616 struct digest_data *mdat;
617 digest = EVP_get_digestbyname(alg);
619 /* If alg has an OID assume disabled algorithm */
620 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
626 mdat = OPENSSL_malloc(sizeof(*mdat));
627 mdat->digest = digest;
635 static void digest_test_cleanup(struct evp_test *t)
637 struct digest_data *mdat = t->data;
638 test_free(mdat->input);
639 test_free(mdat->output);
642 static int digest_test_parse(struct evp_test *t,
643 const char *keyword, const char *value)
645 struct digest_data *mdata = t->data;
646 if (strcmp(keyword, "Input") == 0)
647 return test_bin(value, &mdata->input, &mdata->input_len);
648 if (strcmp(keyword, "Output") == 0)
649 return test_bin(value, &mdata->output, &mdata->output_len);
650 if (strcmp(keyword, "Count") == 0) {
651 long nrpt = atoi(value);
654 mdata->nrpt = (size_t)nrpt;
660 static int digest_test_run(struct evp_test *t)
662 struct digest_data *mdata = t->data;
664 const char *err = "INTERNAL_ERROR";
666 unsigned char md[EVP_MAX_MD_SIZE];
668 mctx = EVP_MD_CTX_new();
671 err = "DIGESTINIT_ERROR";
672 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
674 err = "DIGESTUPDATE_ERROR";
675 for (i = 0; i < mdata->nrpt; i++) {
676 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
679 err = "DIGESTFINAL_ERROR";
680 if (!EVP_DigestFinal(mctx, md, &md_len))
682 err = "DIGEST_LENGTH_MISMATCH";
683 if (md_len != mdata->output_len)
685 err = "DIGEST_MISMATCH";
686 if (check_output(t, mdata->output, md, md_len))
690 EVP_MD_CTX_free(mctx);
695 static const struct evp_test_method digest_test_method = {
705 const EVP_CIPHER *cipher;
707 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
713 unsigned char *plaintext;
714 size_t plaintext_len;
715 unsigned char *ciphertext;
716 size_t ciphertext_len;
724 static int cipher_test_init(struct evp_test *t, const char *alg)
726 const EVP_CIPHER *cipher;
727 struct cipher_data *cdat = t->data;
728 cipher = EVP_get_cipherbyname(alg);
730 /* If alg has an OID assume disabled algorithm */
731 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
737 cdat = OPENSSL_malloc(sizeof(*cdat));
738 cdat->cipher = cipher;
742 cdat->ciphertext = NULL;
743 cdat->plaintext = NULL;
747 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
748 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
749 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
750 cdat->aead = EVP_CIPHER_mode(cipher);
751 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
759 static void cipher_test_cleanup(struct evp_test *t)
761 struct cipher_data *cdat = t->data;
762 test_free(cdat->key);
764 test_free(cdat->ciphertext);
765 test_free(cdat->plaintext);
766 test_free(cdat->aad);
767 test_free(cdat->tag);
770 static int cipher_test_parse(struct evp_test *t, const char *keyword,
773 struct cipher_data *cdat = t->data;
774 if (strcmp(keyword, "Key") == 0)
775 return test_bin(value, &cdat->key, &cdat->key_len);
776 if (strcmp(keyword, "IV") == 0)
777 return test_bin(value, &cdat->iv, &cdat->iv_len);
778 if (strcmp(keyword, "Plaintext") == 0)
779 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
780 if (strcmp(keyword, "Ciphertext") == 0)
781 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
783 if (strcmp(keyword, "AAD") == 0)
784 return test_bin(value, &cdat->aad, &cdat->aad_len);
785 if (strcmp(keyword, "Tag") == 0)
786 return test_bin(value, &cdat->tag, &cdat->tag_len);
789 if (strcmp(keyword, "Operation") == 0) {
790 if (strcmp(value, "ENCRYPT") == 0)
792 else if (strcmp(value, "DECRYPT") == 0)
801 static int cipher_test_enc(struct evp_test *t, int enc,
802 size_t out_misalign, size_t inp_misalign)
804 struct cipher_data *cdat = t->data;
805 unsigned char *in, *out, *tmp = NULL;
806 size_t in_len, out_len;
808 EVP_CIPHER_CTX *ctx = NULL;
810 err = "INTERNAL_ERROR";
811 ctx = EVP_CIPHER_CTX_new();
814 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
816 in = cdat->plaintext;
817 in_len = cdat->plaintext_len;
818 out = cdat->ciphertext;
819 out_len = cdat->ciphertext_len;
821 in = cdat->ciphertext;
822 in_len = cdat->ciphertext_len;
823 out = cdat->plaintext;
824 out_len = cdat->plaintext_len;
826 if (inp_misalign == (size_t)-1) {
828 * Exercise in-place encryption
830 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
833 in = memcpy(tmp + out_misalign, in, in_len);
835 inp_misalign += 16 - ((out_misalign + in_len) & 15);
837 * 'tmp' will store both output and copy of input. We make the copy
838 * of input to specifically aligned part of 'tmp'. So we just
839 * figured out how much padding would ensure the required alignment,
840 * now we allocate extended buffer and finally copy the input just
841 * past inp_misalign in expression below. Output will be written
842 * past out_misalign...
844 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
845 inp_misalign + in_len);
848 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
849 inp_misalign, in, in_len);
851 err = "CIPHERINIT_ERROR";
852 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
854 err = "INVALID_IV_LENGTH";
857 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
860 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
866 * If encrypting or OCB just set tag length initially, otherwise
867 * set tag length and value.
869 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
870 err = "TAG_LENGTH_SET_ERROR";
873 err = "TAG_SET_ERROR";
876 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
877 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
883 err = "INVALID_KEY_LENGTH";
884 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
886 err = "KEY_SET_ERROR";
887 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
890 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
891 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
892 cdat->tag_len, cdat->tag)) {
893 err = "TAG_SET_ERROR";
898 if (cdat->aead == EVP_CIPH_CCM_MODE) {
899 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
900 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
905 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
906 err = "AAD_SET_ERROR";
910 EVP_CIPHER_CTX_set_padding(ctx, 0);
911 err = "CIPHERUPDATE_ERROR";
912 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
914 if (cdat->aead == EVP_CIPH_CCM_MODE)
917 err = "CIPHERFINAL_ERROR";
918 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
921 err = "LENGTH_MISMATCH";
922 if (out_len != (size_t)(tmplen + tmpflen))
924 err = "VALUE_MISMATCH";
925 if (check_output(t, out, tmp + out_misalign, out_len))
927 if (enc && cdat->aead) {
928 unsigned char rtag[16];
929 if (cdat->tag_len > sizeof(rtag)) {
930 err = "TAG_LENGTH_INTERNAL_ERROR";
933 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
934 cdat->tag_len, rtag)) {
935 err = "TAG_RETRIEVE_ERROR";
938 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
939 err = "TAG_VALUE_MISMATCH";
946 EVP_CIPHER_CTX_free(ctx);
951 static int cipher_test_run(struct evp_test *t)
953 struct cipher_data *cdat = t->data;
955 size_t out_misalign, inp_misalign;
961 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
962 /* IV is optional and usually omitted in wrap mode */
963 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
968 if (cdat->aead && !cdat->tag) {
972 for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
973 static char aux_err[64];
974 t->aux_err = aux_err;
975 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
976 if (inp_misalign == (size_t)-1) {
977 /* kludge: inp_misalign == -1 means "exercise in-place" */
978 BIO_snprintf(aux_err, sizeof(aux_err), "%s in-place",
979 out_misalign ? "misaligned" : "aligned");
981 BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input",
982 out_misalign ? "misaligned" : "aligned",
983 inp_misalign ? "misaligned" : "aligned");
986 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign);
987 /* Not fatal errors: return */
994 if (cdat->enc != 1) {
995 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign);
996 /* Not fatal errors: return */
1010 static const struct evp_test_method cipher_test_method = {
1013 cipher_test_cleanup,
1021 /* Algorithm string for this MAC */
1027 unsigned char *input;
1029 /* Expected output */
1030 unsigned char *output;
1034 static int mac_test_init(struct evp_test *t, const char *alg)
1037 struct mac_data *mdat;
1038 if (strcmp(alg, "HMAC") == 0) {
1039 type = EVP_PKEY_HMAC;
1040 } else if (strcmp(alg, "CMAC") == 0) {
1041 #ifndef OPENSSL_NO_CMAC
1042 type = EVP_PKEY_CMAC;
1050 mdat = OPENSSL_malloc(sizeof(*mdat));
1055 mdat->output = NULL;
1060 static void mac_test_cleanup(struct evp_test *t)
1062 struct mac_data *mdat = t->data;
1063 test_free(mdat->alg);
1064 test_free(mdat->key);
1065 test_free(mdat->input);
1066 test_free(mdat->output);
1069 static int mac_test_parse(struct evp_test *t,
1070 const char *keyword, const char *value)
1072 struct mac_data *mdata = t->data;
1073 if (strcmp(keyword, "Key") == 0)
1074 return test_bin(value, &mdata->key, &mdata->key_len);
1075 if (strcmp(keyword, "Algorithm") == 0) {
1076 mdata->alg = OPENSSL_strdup(value);
1081 if (strcmp(keyword, "Input") == 0)
1082 return test_bin(value, &mdata->input, &mdata->input_len);
1083 if (strcmp(keyword, "Output") == 0)
1084 return test_bin(value, &mdata->output, &mdata->output_len);
1088 static int mac_test_run(struct evp_test *t)
1090 struct mac_data *mdata = t->data;
1091 const char *err = "INTERNAL_ERROR";
1092 EVP_MD_CTX *mctx = NULL;
1093 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1094 EVP_PKEY *key = NULL;
1095 const EVP_MD *md = NULL;
1096 unsigned char *mac = NULL;
1099 #ifdef OPENSSL_NO_DES
1100 if (strstr(mdata->alg, "DES") != NULL) {
1107 err = "MAC_PKEY_CTX_ERROR";
1108 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1112 err = "MAC_KEYGEN_INIT_ERROR";
1113 if (EVP_PKEY_keygen_init(genctx) <= 0)
1115 if (mdata->type == EVP_PKEY_CMAC) {
1116 err = "MAC_ALGORITHM_SET_ERROR";
1117 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1121 err = "MAC_KEY_SET_ERROR";
1122 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1125 err = "MAC_KEY_GENERATE_ERROR";
1126 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1128 if (mdata->type == EVP_PKEY_HMAC) {
1129 err = "MAC_ALGORITHM_SET_ERROR";
1130 md = EVP_get_digestbyname(mdata->alg);
1134 mctx = EVP_MD_CTX_new();
1137 err = "DIGESTSIGNINIT_ERROR";
1138 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1141 err = "DIGESTSIGNUPDATE_ERROR";
1142 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1144 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1145 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1147 mac = OPENSSL_malloc(mac_len);
1149 fprintf(stderr, "Error allocating mac buffer!\n");
1152 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1154 err = "MAC_LENGTH_MISMATCH";
1155 if (mac_len != mdata->output_len)
1157 err = "MAC_MISMATCH";
1158 if (check_output(t, mdata->output, mac, mac_len))
1162 EVP_MD_CTX_free(mctx);
1164 EVP_PKEY_CTX_free(genctx);
1170 static const struct evp_test_method mac_test_method = {
1179 * Public key operations. These are all very similar and can share
1180 * a lot of common code.
1184 /* Context for this operation */
1186 /* Key operation to perform */
1187 int (*keyop) (EVP_PKEY_CTX *ctx,
1188 unsigned char *sig, size_t *siglen,
1189 const unsigned char *tbs, size_t tbslen);
1191 unsigned char *input;
1193 /* Expected output */
1194 unsigned char *output;
1199 * Perform public key operation setup: lookup key, allocated ctx and call
1200 * the appropriate initialisation function
1202 static int pkey_test_init(struct evp_test *t, const char *name,
1204 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1205 int (*keyop) (EVP_PKEY_CTX *ctx,
1206 unsigned char *sig, size_t *siglen,
1207 const unsigned char *tbs,
1211 struct pkey_data *kdata;
1212 EVP_PKEY *pkey = NULL;
1215 rv = find_key(&pkey, name, t->public);
1217 rv = find_key(&pkey, name, t->private);
1218 if (!rv || pkey == NULL) {
1223 kdata = OPENSSL_malloc(sizeof(*kdata));
1225 EVP_PKEY_free(pkey);
1229 kdata->input = NULL;
1230 kdata->output = NULL;
1231 kdata->keyop = keyop;
1233 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1236 if (keyopinit(kdata->ctx) <= 0)
1241 static void pkey_test_cleanup(struct evp_test *t)
1243 struct pkey_data *kdata = t->data;
1245 OPENSSL_free(kdata->input);
1246 OPENSSL_free(kdata->output);
1247 EVP_PKEY_CTX_free(kdata->ctx);
1250 static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1256 tmpval = OPENSSL_strdup(value);
1259 p = strchr(tmpval, ':');
1262 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1263 if (p != NULL && rv <= 0 && rv != -2) {
1264 /* If p has an OID assume disabled algorithm */
1265 if (OBJ_sn2nid(p) != NID_undef || OBJ_ln2nid(p) != NID_undef) {
1270 OPENSSL_free(tmpval);
1274 static int pkey_test_parse(struct evp_test *t,
1275 const char *keyword, const char *value)
1277 struct pkey_data *kdata = t->data;
1278 if (strcmp(keyword, "Input") == 0)
1279 return test_bin(value, &kdata->input, &kdata->input_len);
1280 if (strcmp(keyword, "Output") == 0)
1281 return test_bin(value, &kdata->output, &kdata->output_len);
1282 if (strcmp(keyword, "Ctrl") == 0)
1283 return pkey_test_ctrl(t, kdata->ctx, value);
1287 static int pkey_test_run(struct evp_test *t)
1289 struct pkey_data *kdata = t->data;
1290 unsigned char *out = NULL;
1292 const char *err = "KEYOP_LENGTH_ERROR";
1293 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1294 kdata->input_len) <= 0)
1296 out = OPENSSL_malloc(out_len);
1298 fprintf(stderr, "Error allocating output buffer!\n");
1301 err = "KEYOP_ERROR";
1303 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1305 err = "KEYOP_LENGTH_MISMATCH";
1306 if (out_len != kdata->output_len)
1308 err = "KEYOP_MISMATCH";
1309 if (check_output(t, kdata->output, out, out_len))
1318 static int sign_test_init(struct evp_test *t, const char *name)
1320 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1323 static const struct evp_test_method psign_test_method = {
1331 static int verify_recover_test_init(struct evp_test *t, const char *name)
1333 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1334 EVP_PKEY_verify_recover);
1337 static const struct evp_test_method pverify_recover_test_method = {
1339 verify_recover_test_init,
1345 static int decrypt_test_init(struct evp_test *t, const char *name)
1347 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1351 static const struct evp_test_method pdecrypt_test_method = {
1359 static int verify_test_init(struct evp_test *t, const char *name)
1361 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1364 static int verify_test_run(struct evp_test *t)
1366 struct pkey_data *kdata = t->data;
1367 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1368 kdata->input, kdata->input_len) <= 0)
1369 t->err = "VERIFY_ERROR";
1373 static const struct evp_test_method pverify_test_method = {
1382 static int pderive_test_init(struct evp_test *t, const char *name)
1384 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1387 static int pderive_test_parse(struct evp_test *t,
1388 const char *keyword, const char *value)
1390 struct pkey_data *kdata = t->data;
1392 if (strcmp(keyword, "PeerKey") == 0) {
1394 if (find_key(&peer, value, t->public) == 0)
1396 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1400 if (strcmp(keyword, "SharedSecret") == 0)
1401 return test_bin(value, &kdata->output, &kdata->output_len);
1402 if (strcmp(keyword, "Ctrl") == 0)
1403 return pkey_test_ctrl(t, kdata->ctx, value);
1407 static int pderive_test_run(struct evp_test *t)
1409 struct pkey_data *kdata = t->data;
1410 unsigned char *out = NULL;
1412 const char *err = "INTERNAL_ERROR";
1414 out_len = kdata->output_len;
1415 out = OPENSSL_malloc(out_len);
1417 fprintf(stderr, "Error allocating output buffer!\n");
1420 err = "DERIVE_ERROR";
1421 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1423 err = "SHARED_SECRET_LENGTH_MISMATCH";
1424 if (out_len != kdata->output_len)
1426 err = "SHARED_SECRET_MISMATCH";
1427 if (check_output(t, kdata->output, out, out_len))
1436 static const struct evp_test_method pderive_test_method = {
1446 #define PBE_TYPE_SCRYPT 1
1447 #define PBE_TYPE_PBKDF2 2
1448 #define PBE_TYPE_PKCS12 3
1454 /* scrypt parameters */
1455 uint64_t N, r, p, maxmem;
1457 /* PKCS#12 parameters */
1462 unsigned char *pass;
1466 unsigned char *salt;
1469 /* Expected output */
1474 #ifndef OPENSSL_NO_SCRYPT
1475 static int scrypt_test_parse(struct evp_test *t,
1476 const char *keyword, const char *value)
1478 struct pbe_data *pdata = t->data;
1480 if (strcmp(keyword, "N") == 0)
1481 return test_uint64(value, &pdata->N);
1482 if (strcmp(keyword, "p") == 0)
1483 return test_uint64(value, &pdata->p);
1484 if (strcmp(keyword, "r") == 0)
1485 return test_uint64(value, &pdata->r);
1486 if (strcmp(keyword, "maxmem") == 0)
1487 return test_uint64(value, &pdata->maxmem);
1492 static int pbkdf2_test_parse(struct evp_test *t,
1493 const char *keyword, const char *value)
1495 struct pbe_data *pdata = t->data;
1497 if (strcmp(keyword, "iter") == 0) {
1498 pdata->iter = atoi(value);
1499 if (pdata->iter <= 0)
1503 if (strcmp(keyword, "MD") == 0) {
1504 pdata->md = EVP_get_digestbyname(value);
1505 if (pdata->md == NULL)
1512 static int pkcs12_test_parse(struct evp_test *t,
1513 const char *keyword, const char *value)
1515 struct pbe_data *pdata = t->data;
1517 if (strcmp(keyword, "id") == 0) {
1518 pdata->id = atoi(value);
1523 return pbkdf2_test_parse(t, keyword, value);
1526 static int pbe_test_init(struct evp_test *t, const char *alg)
1528 struct pbe_data *pdat;
1531 if (strcmp(alg, "scrypt") == 0) {
1532 #ifndef OPENSSL_NO_SCRYPT
1533 pbe_type = PBE_TYPE_SCRYPT;
1538 } else if (strcmp(alg, "pbkdf2") == 0) {
1539 pbe_type = PBE_TYPE_PBKDF2;
1540 } else if (strcmp(alg, "pkcs12") == 0) {
1541 pbe_type = PBE_TYPE_PKCS12;
1543 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1545 pdat = OPENSSL_malloc(sizeof(*pdat));
1546 pdat->pbe_type = pbe_type;
1560 static void pbe_test_cleanup(struct evp_test *t)
1562 struct pbe_data *pdat = t->data;
1563 test_free(pdat->pass);
1564 test_free(pdat->salt);
1565 test_free(pdat->key);
1568 static int pbe_test_parse(struct evp_test *t,
1569 const char *keyword, const char *value)
1571 struct pbe_data *pdata = t->data;
1573 if (strcmp(keyword, "Password") == 0)
1574 return test_bin(value, &pdata->pass, &pdata->pass_len);
1575 if (strcmp(keyword, "Salt") == 0)
1576 return test_bin(value, &pdata->salt, &pdata->salt_len);
1577 if (strcmp(keyword, "Key") == 0)
1578 return test_bin(value, &pdata->key, &pdata->key_len);
1579 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1580 return pbkdf2_test_parse(t, keyword, value);
1581 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1582 return pkcs12_test_parse(t, keyword, value);
1583 #ifndef OPENSSL_NO_SCRYPT
1584 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1585 return scrypt_test_parse(t, keyword, value);
1590 static int pbe_test_run(struct evp_test *t)
1592 struct pbe_data *pdata = t->data;
1593 const char *err = "INTERNAL_ERROR";
1596 key = OPENSSL_malloc(pdata->key_len);
1599 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1600 err = "PBKDF2_ERROR";
1601 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1602 pdata->salt, pdata->salt_len,
1603 pdata->iter, pdata->md,
1604 pdata->key_len, key) == 0)
1606 #ifndef OPENSSL_NO_SCRYPT
1607 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1608 err = "SCRYPT_ERROR";
1609 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1610 pdata->salt, pdata->salt_len,
1611 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1612 key, pdata->key_len) == 0)
1615 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1616 err = "PKCS12_ERROR";
1617 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1618 pdata->salt, pdata->salt_len,
1619 pdata->id, pdata->iter, pdata->key_len,
1620 key, pdata->md) == 0)
1623 err = "KEY_MISMATCH";
1624 if (check_output(t, pdata->key, key, pdata->key_len))
1633 static const struct evp_test_method pbe_test_method = {
1644 BASE64_CANONICAL_ENCODING = 0,
1645 BASE64_VALID_ENCODING = 1,
1646 BASE64_INVALID_ENCODING = 2
1647 } base64_encoding_type;
1649 struct encode_data {
1650 /* Input to encoding */
1651 unsigned char *input;
1653 /* Expected output */
1654 unsigned char *output;
1656 base64_encoding_type encoding;
1659 static int encode_test_init(struct evp_test *t, const char *encoding)
1661 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1663 if (strcmp(encoding, "canonical") == 0) {
1664 edata->encoding = BASE64_CANONICAL_ENCODING;
1665 } else if (strcmp(encoding, "valid") == 0) {
1666 edata->encoding = BASE64_VALID_ENCODING;
1667 } else if (strcmp(encoding, "invalid") == 0) {
1668 edata->encoding = BASE64_INVALID_ENCODING;
1669 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1670 if (t->expected_err == NULL)
1673 fprintf(stderr, "Bad encoding: %s. Should be one of "
1674 "{canonical, valid, invalid}\n", encoding);
1681 static void encode_test_cleanup(struct evp_test *t)
1683 struct encode_data *edata = t->data;
1684 test_free(edata->input);
1685 test_free(edata->output);
1686 memset(edata, 0, sizeof(*edata));
1689 static int encode_test_parse(struct evp_test *t,
1690 const char *keyword, const char *value)
1692 struct encode_data *edata = t->data;
1693 if (strcmp(keyword, "Input") == 0)
1694 return test_bin(value, &edata->input, &edata->input_len);
1695 if (strcmp(keyword, "Output") == 0)
1696 return test_bin(value, &edata->output, &edata->output_len);
1700 static int encode_test_run(struct evp_test *t)
1702 struct encode_data *edata = t->data;
1703 unsigned char *encode_out = NULL, *decode_out = NULL;
1704 int output_len, chunk_len;
1705 const char *err = "INTERNAL_ERROR";
1706 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1708 if (decode_ctx == NULL)
1711 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1712 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1713 if (encode_ctx == NULL)
1715 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1716 if (encode_out == NULL)
1719 EVP_EncodeInit(encode_ctx);
1720 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1721 edata->input, edata->input_len);
1722 output_len = chunk_len;
1724 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1725 output_len += chunk_len;
1727 EVP_ENCODE_CTX_free(encode_ctx);
1729 if (check_var_length_output(t, edata->output, edata->output_len,
1730 encode_out, output_len)) {
1731 err = "BAD_ENCODING";
1736 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1737 if (decode_out == NULL)
1740 EVP_DecodeInit(decode_ctx);
1741 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1742 edata->output_len) < 0) {
1743 err = "DECODE_ERROR";
1746 output_len = chunk_len;
1748 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1749 err = "DECODE_ERROR";
1752 output_len += chunk_len;
1754 if (edata->encoding != BASE64_INVALID_ENCODING &&
1755 check_var_length_output(t, edata->input, edata->input_len,
1756 decode_out, output_len)) {
1757 err = "BAD_DECODING";
1764 OPENSSL_free(encode_out);
1765 OPENSSL_free(decode_out);
1766 EVP_ENCODE_CTX_free(decode_ctx);
1770 static const struct evp_test_method encode_test_method = {
1773 encode_test_cleanup,
1778 /* KDF operations */
1781 /* Context for this operation */
1783 /* Expected output */
1784 unsigned char *output;
1789 * Perform public key operation setup: lookup key, allocated ctx and call
1790 * the appropriate initialisation function
1792 static int kdf_test_init(struct evp_test *t, const char *name)
1794 struct kdf_data *kdata;
1796 kdata = OPENSSL_malloc(sizeof(*kdata));
1800 kdata->output = NULL;
1802 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1803 if (kdata->ctx == NULL)
1805 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1810 static void kdf_test_cleanup(struct evp_test *t)
1812 struct kdf_data *kdata = t->data;
1813 OPENSSL_free(kdata->output);
1814 EVP_PKEY_CTX_free(kdata->ctx);
1817 static int kdf_test_parse(struct evp_test *t,
1818 const char *keyword, const char *value)
1820 struct kdf_data *kdata = t->data;
1821 if (strcmp(keyword, "Output") == 0)
1822 return test_bin(value, &kdata->output, &kdata->output_len);
1823 if (strncmp(keyword, "Ctrl", 4) == 0)
1824 return pkey_test_ctrl(t, kdata->ctx, value);
1828 static int kdf_test_run(struct evp_test *t)
1830 struct kdf_data *kdata = t->data;
1831 unsigned char *out = NULL;
1832 size_t out_len = kdata->output_len;
1833 const char *err = "INTERNAL_ERROR";
1834 out = OPENSSL_malloc(out_len);
1836 fprintf(stderr, "Error allocating output buffer!\n");
1839 err = "KDF_DERIVE_ERROR";
1840 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1842 err = "KDF_LENGTH_MISMATCH";
1843 if (out_len != kdata->output_len)
1845 err = "KDF_MISMATCH";
1846 if (check_output(t, kdata->output, out, out_len))
1855 static const struct evp_test_method kdf_test_method = {