3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
59 #include <openssl/evp.h>
60 #include <openssl/pem.h>
61 #include <openssl/err.h>
62 #include <openssl/x509v3.h>
63 #include <openssl/pkcs12.h>
64 #include <openssl/kdf.h>
65 #include "internal/numbers.h"
67 /* Remove spaces from beginning and end of a string */
69 static void remove_space(char **pval)
71 unsigned char *p = (unsigned char *)*pval;
78 p = p + strlen(*pval) - 1;
80 /* Remove trailing space */
86 * Given a line of the form:
87 * name = value # comment
88 * extract name and value. NB: modifies passed buffer.
91 static int parse_line(char **pkw, char **pval, char *linebuf)
95 p = linebuf + strlen(linebuf) - 1;
98 fprintf(stderr, "FATAL: missing EOL\n");
104 p = strchr(linebuf, '#');
109 /* Look for = sign */
110 p = strchr(linebuf, '=');
121 /* Remove spaces from keyword and value */
129 * Unescape some escape sequences in string literals.
130 * Return the result in a newly allocated buffer.
131 * Currently only supports '\n'.
132 * If the input length is 0, returns a valid 1-byte buffer, but sets
135 static unsigned char* unescape(const char *input, size_t input_len,
138 unsigned char *ret, *p;
140 if (input_len == 0) {
142 return OPENSSL_zalloc(1);
145 /* Escaping is non-expanding; over-allocate original size for simplicity. */
146 ret = p = OPENSSL_malloc(input_len);
150 for (i = 0; i < input_len; i++) {
151 if (input[i] == '\\') {
152 if (i == input_len - 1 || input[i+1] != 'n')
169 /* For a hex string "value" convert to a binary allocated buffer */
170 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
175 * Don't return NULL for zero length buffer.
176 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
177 * a non-NULL key buffer even if the key length is 0, in order to detect
180 *buf = OPENSSL_malloc(1);
187 /* Check for string literal */
188 if (value[0] == '"') {
191 vlen = strlen(value);
192 if (value[vlen - 1] != '"')
195 *buf = unescape(value, vlen, buflen);
201 *buf = string_to_hex(value, &len);
203 fprintf(stderr, "Value=%s\n", value);
204 ERR_print_errors_fp(stderr);
207 /* Size of input buffer means we'll never overflow */
211 /* Parse unsigned decimal 64 bit integer value */
212 static int test_uint64(const char *value, uint64_t *pr)
214 const char *p = value;
216 fprintf(stderr, "Invalid empty integer value\n");
221 if (*pr > UINT64_MAX/10) {
222 fprintf(stderr, "Integer string overflow value=%s\n", value);
226 if (*p < '0' || *p > '9') {
227 fprintf(stderr, "Invalid integer string value=%s\n", value);
236 /* Structure holding test information */
238 /* file being read */
240 /* List of public and private keys */
241 struct key_list *private;
242 struct key_list *public;
243 /* method for this test */
244 const struct evp_test_method *meth;
245 /* current line being processed */
247 /* start line of current test */
248 unsigned int start_line;
249 /* Error string for test */
251 /* Expected error value of test */
253 /* Number of tests */
257 /* Number of tests skipped */
259 /* If output mismatch expected and got value */
260 unsigned char *out_received;
261 size_t out_received_len;
262 unsigned char *out_expected;
263 size_t out_expected_len;
264 /* test specific data */
266 /* Current test should be skipped */
273 struct key_list *next;
276 /* Test method structure */
277 struct evp_test_method {
278 /* Name of test as it appears in file */
280 /* Initialise test for "alg" */
281 int (*init) (struct evp_test * t, const char *alg);
282 /* Clean up method */
283 void (*cleanup) (struct evp_test * t);
284 /* Test specific name value pair processing */
285 int (*parse) (struct evp_test * t, const char *name, const char *value);
286 /* Run the test itself */
287 int (*run_test) (struct evp_test * t);
290 static const struct evp_test_method digest_test_method, cipher_test_method;
291 static const struct evp_test_method mac_test_method;
292 static const struct evp_test_method psign_test_method, pverify_test_method;
293 static const struct evp_test_method pdecrypt_test_method;
294 static const struct evp_test_method pverify_recover_test_method;
295 static const struct evp_test_method pbe_test_method;
296 static const struct evp_test_method encode_test_method;
297 static const struct evp_test_method kdf_test_method;
299 static const struct evp_test_method *evp_test_list[] = {
304 &pverify_test_method,
305 &pdecrypt_test_method,
306 &pverify_recover_test_method,
313 static const struct evp_test_method *evp_find_test(const char *name)
315 const struct evp_test_method **tt;
317 for (tt = evp_test_list; *tt; tt++) {
318 if (strcmp(name, (*tt)->name) == 0)
324 static void hex_print(const char *name, const unsigned char *buf, size_t len)
327 fprintf(stderr, "%s ", name);
328 for (i = 0; i < len; i++)
329 fprintf(stderr, "%02X", buf[i]);
333 static void free_expected(struct evp_test *t)
335 OPENSSL_free(t->expected_err);
336 t->expected_err = NULL;
337 OPENSSL_free(t->out_expected);
338 OPENSSL_free(t->out_received);
339 t->out_expected = NULL;
340 t->out_received = NULL;
341 t->out_expected_len = 0;
342 t->out_received_len = 0;
347 static void print_expected(struct evp_test *t)
349 if (t->out_expected == NULL && t->out_received == NULL)
351 hex_print("Expected:", t->out_expected, t->out_expected_len);
352 hex_print("Got: ", t->out_received, t->out_received_len);
356 static int check_test_error(struct evp_test *t)
358 if (!t->err && !t->expected_err)
360 if (t->err && !t->expected_err) {
361 fprintf(stderr, "Test line %d: unexpected error %s\n",
362 t->start_line, t->err);
366 if (!t->err && t->expected_err) {
367 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
368 t->start_line, t->expected_err);
371 if (strcmp(t->err, t->expected_err) == 0)
374 fprintf(stderr, "Test line %d: expecting %s got %s\n",
375 t->start_line, t->expected_err, t->err);
379 /* Setup a new test, run any existing test */
381 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
383 /* If we already have a test set up run it */
392 if (t->meth->run_test(t) != 1) {
393 fprintf(stderr, "%s test error line %d\n",
394 t->meth->name, t->start_line);
397 if (!check_test_error(t)) {
399 ERR_print_errors_fp(stderr);
404 OPENSSL_free(t->data);
406 OPENSSL_free(t->expected_err);
407 t->expected_err = NULL;
414 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
416 for (; lst; lst = lst->next) {
417 if (strcmp(lst->name, name) == 0) {
426 static void free_key_list(struct key_list *lst)
428 while (lst != NULL) {
429 struct key_list *ltmp;
430 EVP_PKEY_free(lst->key);
431 OPENSSL_free(lst->name);
438 static int check_unsupported()
440 long err = ERR_peek_error();
441 if (ERR_GET_LIB(err) == ERR_LIB_EVP
442 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
449 static int process_test(struct evp_test *t, char *buf, int verbose)
451 char *keyword = NULL, *value = NULL;
452 int rv = 0, add_key = 0;
454 struct key_list **lst = NULL, *key = NULL;
456 const struct evp_test_method *tmeth = NULL;
459 if (!parse_line(&keyword, &value, buf))
461 if (strcmp(keyword, "PrivateKey") == 0) {
462 save_pos = ftell(t->in);
463 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
464 if (pk == NULL && !check_unsupported()) {
465 fprintf(stderr, "Error reading private key %s\n", value);
466 ERR_print_errors_fp(stderr);
472 if (strcmp(keyword, "PublicKey") == 0) {
473 save_pos = ftell(t->in);
474 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
475 if (pk == NULL && !check_unsupported()) {
476 fprintf(stderr, "Error reading public key %s\n", value);
477 ERR_print_errors_fp(stderr);
483 /* If we have a key add to list */
486 if (find_key(NULL, value, *lst)) {
487 fprintf(stderr, "Duplicate key %s\n", value);
490 key = OPENSSL_malloc(sizeof(*key));
493 key->name = OPENSSL_strdup(value);
497 /* Rewind input, read to end and update line numbers */
498 fseek(t->in, save_pos, SEEK_SET);
499 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
501 if (strncmp(tmpbuf, "-----END", 8) == 0)
504 fprintf(stderr, "Can't find key end\n");
508 /* See if keyword corresponds to a test start */
509 tmeth = evp_find_test(keyword);
511 if (!setup_test(t, tmeth))
513 t->start_line = t->line;
515 if (!tmeth->init(t, value)) {
516 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
520 } else if (t->skip) {
522 } else if (strcmp(keyword, "Result") == 0) {
523 if (t->expected_err) {
524 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
527 t->expected_err = OPENSSL_strdup(value);
528 if (!t->expected_err)
531 /* Must be test specific line: try to parse it */
533 rv = t->meth->parse(t, keyword, value);
536 fprintf(stderr, "line %d: unexpected keyword %s\n",
540 fprintf(stderr, "line %d: error processing keyword %s\n",
548 static int check_var_length_output(struct evp_test *t,
549 const unsigned char *expected,
551 const unsigned char *received,
554 if (expected_len == received_len &&
555 memcmp(expected, received, expected_len) == 0) {
559 /* The result printing code expects a non-NULL buffer. */
560 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
561 t->out_expected_len = expected_len;
562 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
563 t->out_received_len = received_len;
564 if (t->out_expected == NULL || t->out_received == NULL) {
565 fprintf(stderr, "Memory allocation error!\n");
571 static int check_output(struct evp_test *t,
572 const unsigned char *expected,
573 const unsigned char *received,
576 return check_var_length_output(t, expected, len, received, len);
579 int main(int argc, char **argv)
586 fprintf(stderr, "usage: evp_test testfile.txt\n");
590 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
592 ERR_load_crypto_strings();
593 OpenSSL_add_all_algorithms();
595 memset(&t, 0, sizeof(t));
597 in = fopen(argv[1], "r");
599 while (fgets(buf, sizeof(buf), in)) {
601 if (!process_test(&t, buf, 0))
604 /* Run any final test we have */
605 if (!setup_test(&t, NULL))
607 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
608 t.ntests, t.errors, t.nskip);
609 free_key_list(t.public);
610 free_key_list(t.private);
613 CRYPTO_cleanup_all_ex_data();
614 ERR_remove_thread_state(NULL);
616 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
617 CRYPTO_mem_leaks_fp(stderr);
624 static void test_free(void *d)
629 /* Message digest tests */
632 /* Digest this test is for */
633 const EVP_MD *digest;
634 /* Input to digest */
635 unsigned char *input;
637 /* Repeat count for input */
639 /* Expected output */
640 unsigned char *output;
644 static int digest_test_init(struct evp_test *t, const char *alg)
646 const EVP_MD *digest;
647 struct digest_data *mdat;
648 digest = EVP_get_digestbyname(alg);
650 /* If alg has an OID assume disabled algorithm */
651 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
657 mdat = OPENSSL_malloc(sizeof(*mdat));
658 mdat->digest = digest;
666 static void digest_test_cleanup(struct evp_test *t)
668 struct digest_data *mdat = t->data;
669 test_free(mdat->input);
670 test_free(mdat->output);
673 static int digest_test_parse(struct evp_test *t,
674 const char *keyword, const char *value)
676 struct digest_data *mdata = t->data;
677 if (strcmp(keyword, "Input") == 0)
678 return test_bin(value, &mdata->input, &mdata->input_len);
679 if (strcmp(keyword, "Output") == 0)
680 return test_bin(value, &mdata->output, &mdata->output_len);
681 if (strcmp(keyword, "Count") == 0) {
682 long nrpt = atoi(value);
685 mdata->nrpt = (size_t)nrpt;
691 static int digest_test_run(struct evp_test *t)
693 struct digest_data *mdata = t->data;
695 const char *err = "INTERNAL_ERROR";
697 unsigned char md[EVP_MAX_MD_SIZE];
699 mctx = EVP_MD_CTX_new();
702 err = "DIGESTINIT_ERROR";
703 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
705 err = "DIGESTUPDATE_ERROR";
706 for (i = 0; i < mdata->nrpt; i++) {
707 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
710 err = "DIGESTFINAL_ERROR";
711 if (!EVP_DigestFinal(mctx, md, &md_len))
713 err = "DIGEST_LENGTH_MISMATCH";
714 if (md_len != mdata->output_len)
716 err = "DIGEST_MISMATCH";
717 if (check_output(t, mdata->output, md, md_len))
721 EVP_MD_CTX_free(mctx);
726 static const struct evp_test_method digest_test_method = {
736 const EVP_CIPHER *cipher;
738 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
744 unsigned char *plaintext;
745 size_t plaintext_len;
746 unsigned char *ciphertext;
747 size_t ciphertext_len;
755 static int cipher_test_init(struct evp_test *t, const char *alg)
757 const EVP_CIPHER *cipher;
758 struct cipher_data *cdat = t->data;
759 cipher = EVP_get_cipherbyname(alg);
761 /* If alg has an OID assume disabled algorithm */
762 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
768 cdat = OPENSSL_malloc(sizeof(*cdat));
769 cdat->cipher = cipher;
773 cdat->ciphertext = NULL;
774 cdat->plaintext = NULL;
778 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
779 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
780 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
781 cdat->aead = EVP_CIPHER_mode(cipher);
782 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
790 static void cipher_test_cleanup(struct evp_test *t)
792 struct cipher_data *cdat = t->data;
793 test_free(cdat->key);
795 test_free(cdat->ciphertext);
796 test_free(cdat->plaintext);
797 test_free(cdat->aad);
798 test_free(cdat->tag);
801 static int cipher_test_parse(struct evp_test *t, const char *keyword,
804 struct cipher_data *cdat = t->data;
805 if (strcmp(keyword, "Key") == 0)
806 return test_bin(value, &cdat->key, &cdat->key_len);
807 if (strcmp(keyword, "IV") == 0)
808 return test_bin(value, &cdat->iv, &cdat->iv_len);
809 if (strcmp(keyword, "Plaintext") == 0)
810 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
811 if (strcmp(keyword, "Ciphertext") == 0)
812 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
814 if (strcmp(keyword, "AAD") == 0)
815 return test_bin(value, &cdat->aad, &cdat->aad_len);
816 if (strcmp(keyword, "Tag") == 0)
817 return test_bin(value, &cdat->tag, &cdat->tag_len);
820 if (strcmp(keyword, "Operation") == 0) {
821 if (strcmp(value, "ENCRYPT") == 0)
823 else if (strcmp(value, "DECRYPT") == 0)
832 static int cipher_test_enc(struct evp_test *t, int enc)
834 struct cipher_data *cdat = t->data;
835 unsigned char *in, *out, *tmp = NULL;
836 size_t in_len, out_len;
838 EVP_CIPHER_CTX *ctx = NULL;
840 err = "INTERNAL_ERROR";
841 ctx = EVP_CIPHER_CTX_new();
844 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
846 in = cdat->plaintext;
847 in_len = cdat->plaintext_len;
848 out = cdat->ciphertext;
849 out_len = cdat->ciphertext_len;
851 in = cdat->ciphertext;
852 in_len = cdat->ciphertext_len;
853 out = cdat->plaintext;
854 out_len = cdat->plaintext_len;
856 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
859 err = "CIPHERINIT_ERROR";
860 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
862 err = "INVALID_IV_LENGTH";
865 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
868 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
874 * If encrypting or OCB just set tag length initially, otherwise
875 * set tag length and value.
877 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
878 err = "TAG_LENGTH_SET_ERROR";
881 err = "TAG_SET_ERROR";
884 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
885 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
891 err = "INVALID_KEY_LENGTH";
892 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
894 err = "KEY_SET_ERROR";
895 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
898 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
899 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
900 cdat->tag_len, cdat->tag)) {
901 err = "TAG_SET_ERROR";
906 if (cdat->aead == EVP_CIPH_CCM_MODE) {
907 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
908 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
913 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
914 err = "AAD_SET_ERROR";
918 EVP_CIPHER_CTX_set_padding(ctx, 0);
919 err = "CIPHERUPDATE_ERROR";
920 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
922 if (cdat->aead == EVP_CIPH_CCM_MODE)
925 err = "CIPHERFINAL_ERROR";
926 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
929 err = "LENGTH_MISMATCH";
930 if (out_len != (size_t)(tmplen + tmpflen))
932 err = "VALUE_MISMATCH";
933 if (check_output(t, out, tmp, out_len))
935 if (enc && cdat->aead) {
936 unsigned char rtag[16];
937 if (cdat->tag_len > sizeof(rtag)) {
938 err = "TAG_LENGTH_INTERNAL_ERROR";
941 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
942 cdat->tag_len, rtag)) {
943 err = "TAG_RETRIEVE_ERROR";
946 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
947 err = "TAG_VALUE_MISMATCH";
954 EVP_CIPHER_CTX_free(ctx);
959 static int cipher_test_run(struct evp_test *t)
961 struct cipher_data *cdat = t->data;
967 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
968 /* IV is optional and usually omitted in wrap mode */
969 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
974 if (cdat->aead && !cdat->tag) {
979 rv = cipher_test_enc(t, 1);
980 /* Not fatal errors: return */
987 if (cdat->enc != 1) {
988 rv = cipher_test_enc(t, 0);
989 /* Not fatal errors: return */
999 static const struct evp_test_method cipher_test_method = {
1002 cipher_test_cleanup,
1010 /* Algorithm string for this MAC */
1016 unsigned char *input;
1018 /* Expected output */
1019 unsigned char *output;
1023 static int mac_test_init(struct evp_test *t, const char *alg)
1026 struct mac_data *mdat;
1027 if (strcmp(alg, "HMAC") == 0)
1028 type = EVP_PKEY_HMAC;
1029 else if (strcmp(alg, "CMAC") == 0)
1030 type = EVP_PKEY_CMAC;
1034 mdat = OPENSSL_malloc(sizeof(*mdat));
1039 mdat->output = NULL;
1044 static void mac_test_cleanup(struct evp_test *t)
1046 struct mac_data *mdat = t->data;
1047 test_free(mdat->alg);
1048 test_free(mdat->key);
1049 test_free(mdat->input);
1050 test_free(mdat->output);
1053 static int mac_test_parse(struct evp_test *t,
1054 const char *keyword, const char *value)
1056 struct mac_data *mdata = t->data;
1057 if (strcmp(keyword, "Key") == 0)
1058 return test_bin(value, &mdata->key, &mdata->key_len);
1059 if (strcmp(keyword, "Algorithm") == 0) {
1060 mdata->alg = OPENSSL_strdup(value);
1065 if (strcmp(keyword, "Input") == 0)
1066 return test_bin(value, &mdata->input, &mdata->input_len);
1067 if (strcmp(keyword, "Output") == 0)
1068 return test_bin(value, &mdata->output, &mdata->output_len);
1072 static int mac_test_run(struct evp_test *t)
1074 struct mac_data *mdata = t->data;
1075 const char *err = "INTERNAL_ERROR";
1076 EVP_MD_CTX *mctx = NULL;
1077 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1078 EVP_PKEY *key = NULL;
1079 const EVP_MD *md = NULL;
1080 unsigned char *mac = NULL;
1083 err = "MAC_PKEY_CTX_ERROR";
1084 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1088 err = "MAC_KEYGEN_INIT_ERROR";
1089 if (EVP_PKEY_keygen_init(genctx) <= 0)
1091 if (mdata->type == EVP_PKEY_CMAC) {
1092 err = "MAC_ALGORITHM_SET_ERROR";
1093 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1097 err = "MAC_KEY_SET_ERROR";
1098 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1101 err = "MAC_KEY_GENERATE_ERROR";
1102 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1104 if (mdata->type == EVP_PKEY_HMAC) {
1105 err = "MAC_ALGORITHM_SET_ERROR";
1106 md = EVP_get_digestbyname(mdata->alg);
1110 mctx = EVP_MD_CTX_new();
1113 err = "DIGESTSIGNINIT_ERROR";
1114 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1117 err = "DIGESTSIGNUPDATE_ERROR";
1118 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1120 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1121 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1123 mac = OPENSSL_malloc(mac_len);
1125 fprintf(stderr, "Error allocating mac buffer!\n");
1128 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1130 err = "MAC_LENGTH_MISMATCH";
1131 if (mac_len != mdata->output_len)
1133 err = "MAC_MISMATCH";
1134 if (check_output(t, mdata->output, mac, mac_len))
1138 EVP_MD_CTX_free(mctx);
1140 EVP_PKEY_CTX_free(genctx);
1146 static const struct evp_test_method mac_test_method = {
1155 * Public key operations. These are all very similar and can share
1156 * a lot of common code.
1160 /* Context for this operation */
1162 /* Key operation to perform */
1163 int (*keyop) (EVP_PKEY_CTX *ctx,
1164 unsigned char *sig, size_t *siglen,
1165 const unsigned char *tbs, size_t tbslen);
1167 unsigned char *input;
1169 /* Expected output */
1170 unsigned char *output;
1175 * Perform public key operation setup: lookup key, allocated ctx and call
1176 * the appropriate initialisation function
1178 static int pkey_test_init(struct evp_test *t, const char *name,
1180 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1181 int (*keyop) (EVP_PKEY_CTX *ctx,
1182 unsigned char *sig, size_t *siglen,
1183 const unsigned char *tbs,
1187 struct pkey_data *kdata;
1188 EVP_PKEY *pkey = NULL;
1191 rv = find_key(&pkey, name, t->public);
1193 rv = find_key(&pkey, name, t->private);
1201 kdata = OPENSSL_malloc(sizeof(*kdata));
1203 EVP_PKEY_free(pkey);
1207 kdata->input = NULL;
1208 kdata->output = NULL;
1209 kdata->keyop = keyop;
1211 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1214 if (keyopinit(kdata->ctx) <= 0)
1219 static void pkey_test_cleanup(struct evp_test *t)
1221 struct pkey_data *kdata = t->data;
1223 OPENSSL_free(kdata->input);
1224 OPENSSL_free(kdata->output);
1225 EVP_PKEY_CTX_free(kdata->ctx);
1228 static int pkey_test_parse(struct evp_test *t,
1229 const char *keyword, const char *value)
1231 struct pkey_data *kdata = t->data;
1232 if (strcmp(keyword, "Input") == 0)
1233 return test_bin(value, &kdata->input, &kdata->input_len);
1234 if (strcmp(keyword, "Output") == 0)
1235 return test_bin(value, &kdata->output, &kdata->output_len);
1236 if (strcmp(keyword, "Ctrl") == 0) {
1237 char *p = strchr(value, ':');
1240 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1247 static int pkey_test_run(struct evp_test *t)
1249 struct pkey_data *kdata = t->data;
1250 unsigned char *out = NULL;
1252 const char *err = "KEYOP_LENGTH_ERROR";
1253 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1254 kdata->input_len) <= 0)
1256 out = OPENSSL_malloc(out_len);
1258 fprintf(stderr, "Error allocating output buffer!\n");
1261 err = "KEYOP_ERROR";
1263 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1265 err = "KEYOP_LENGTH_MISMATCH";
1266 if (out_len != kdata->output_len)
1268 err = "KEYOP_MISMATCH";
1269 if (check_output(t, kdata->output, out, out_len))
1278 static int sign_test_init(struct evp_test *t, const char *name)
1280 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1283 static const struct evp_test_method psign_test_method = {
1291 static int verify_recover_test_init(struct evp_test *t, const char *name)
1293 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1294 EVP_PKEY_verify_recover);
1297 static const struct evp_test_method pverify_recover_test_method = {
1299 verify_recover_test_init,
1305 static int decrypt_test_init(struct evp_test *t, const char *name)
1307 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1311 static const struct evp_test_method pdecrypt_test_method = {
1319 static int verify_test_init(struct evp_test *t, const char *name)
1321 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1324 static int verify_test_run(struct evp_test *t)
1326 struct pkey_data *kdata = t->data;
1327 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1328 kdata->input, kdata->input_len) <= 0)
1329 t->err = "VERIFY_ERROR";
1333 static const struct evp_test_method pverify_test_method = {
1343 #define PBE_TYPE_SCRYPT 1
1344 #define PBE_TYPE_PBKDF2 2
1345 #define PBE_TYPE_PKCS12 3
1351 /* scrypt parameters */
1352 uint64_t N, r, p, maxmem;
1354 /* PKCS#12 parameters */
1359 unsigned char *pass;
1363 unsigned char *salt;
1366 /* Expected output */
1371 #ifndef OPENSSL_NO_SCRYPT
1372 static int scrypt_test_parse(struct evp_test *t,
1373 const char *keyword, const char *value)
1375 struct pbe_data *pdata = t->data;
1377 if (strcmp(keyword, "N") == 0)
1378 return test_uint64(value, &pdata->N);
1379 if (strcmp(keyword, "p") == 0)
1380 return test_uint64(value, &pdata->p);
1381 if (strcmp(keyword, "r") == 0)
1382 return test_uint64(value, &pdata->r);
1383 if (strcmp(keyword, "maxmem") == 0)
1384 return test_uint64(value, &pdata->maxmem);
1389 static int pbkdf2_test_parse(struct evp_test *t,
1390 const char *keyword, const char *value)
1392 struct pbe_data *pdata = t->data;
1394 if (strcmp(keyword, "iter") == 0) {
1395 pdata->iter = atoi(value);
1396 if (pdata->iter <= 0)
1400 if (strcmp(keyword, "MD") == 0) {
1401 pdata->md = EVP_get_digestbyname(value);
1402 if (pdata->md == NULL)
1409 static int pkcs12_test_parse(struct evp_test *t,
1410 const char *keyword, const char *value)
1412 struct pbe_data *pdata = t->data;
1414 if (strcmp(keyword, "id") == 0) {
1415 pdata->id = atoi(value);
1420 return pbkdf2_test_parse(t, keyword, value);
1423 static int pbe_test_init(struct evp_test *t, const char *alg)
1425 struct pbe_data *pdat;
1428 #ifndef OPENSSL_NO_SCRYPT
1429 if (strcmp(alg, "scrypt") == 0)
1430 pbe_type = PBE_TYPE_SCRYPT;
1432 else if (strcmp(alg, "pbkdf2") == 0)
1433 pbe_type = PBE_TYPE_PBKDF2;
1434 else if (strcmp(alg, "pkcs12") == 0)
1435 pbe_type = PBE_TYPE_PKCS12;
1437 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1438 pdat = OPENSSL_malloc(sizeof(*pdat));
1439 pdat->pbe_type = pbe_type;
1453 static void pbe_test_cleanup(struct evp_test *t)
1455 struct pbe_data *pdat = t->data;
1456 test_free(pdat->pass);
1457 test_free(pdat->salt);
1458 test_free(pdat->key);
1461 static int pbe_test_parse(struct evp_test *t,
1462 const char *keyword, const char *value)
1464 struct pbe_data *pdata = t->data;
1466 if (strcmp(keyword, "Password") == 0)
1467 return test_bin(value, &pdata->pass, &pdata->pass_len);
1468 if (strcmp(keyword, "Salt") == 0)
1469 return test_bin(value, &pdata->salt, &pdata->salt_len);
1470 if (strcmp(keyword, "Key") == 0)
1471 return test_bin(value, &pdata->key, &pdata->key_len);
1472 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1473 return pbkdf2_test_parse(t, keyword, value);
1474 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1475 return pkcs12_test_parse(t, keyword, value);
1476 #ifndef OPENSSL_NO_SCRYPT
1477 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1478 return scrypt_test_parse(t, keyword, value);
1483 static int pbe_test_run(struct evp_test *t)
1485 struct pbe_data *pdata = t->data;
1486 const char *err = "INTERNAL_ERROR";
1489 key = OPENSSL_malloc(pdata->key_len);
1492 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1493 err = "PBKDF2_ERROR";
1494 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1495 pdata->salt, pdata->salt_len,
1496 pdata->iter, pdata->md,
1497 pdata->key_len, key) == 0)
1499 #ifndef OPENSSL_NO_SCRYPT
1500 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1501 err = "SCRYPT_ERROR";
1502 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1503 pdata->salt, pdata->salt_len,
1504 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1505 key, pdata->key_len) == 0)
1508 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1509 err = "PKCS12_ERROR";
1510 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1511 pdata->salt, pdata->salt_len,
1512 pdata->id, pdata->iter, pdata->key_len,
1513 key, pdata->md) == 0)
1516 err = "KEY_MISMATCH";
1517 if (check_output(t, pdata->key, key, pdata->key_len))
1526 static const struct evp_test_method pbe_test_method = {
1537 BASE64_CANONICAL_ENCODING = 0,
1538 BASE64_VALID_ENCODING = 1,
1539 BASE64_INVALID_ENCODING = 2
1540 } base64_encoding_type;
1542 struct encode_data {
1543 /* Input to encoding */
1544 unsigned char *input;
1546 /* Expected output */
1547 unsigned char *output;
1549 base64_encoding_type encoding;
1552 static int encode_test_init(struct evp_test *t, const char *encoding)
1554 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1556 if (strcmp(encoding, "canonical") == 0) {
1557 edata->encoding = BASE64_CANONICAL_ENCODING;
1558 } else if (strcmp(encoding, "valid") == 0) {
1559 edata->encoding = BASE64_VALID_ENCODING;
1560 } else if (strcmp(encoding, "invalid") == 0) {
1561 edata->encoding = BASE64_INVALID_ENCODING;
1562 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1563 if (t->expected_err == NULL)
1566 fprintf(stderr, "Bad encoding: %s. Should be one of "
1567 "{canonical, valid, invalid}\n", encoding);
1574 static void encode_test_cleanup(struct evp_test *t)
1576 struct encode_data *edata = t->data;
1577 test_free(edata->input);
1578 test_free(edata->output);
1579 memset(edata, 0, sizeof(*edata));
1582 static int encode_test_parse(struct evp_test *t,
1583 const char *keyword, const char *value)
1585 struct encode_data *edata = t->data;
1586 if (strcmp(keyword, "Input") == 0)
1587 return test_bin(value, &edata->input, &edata->input_len);
1588 if (strcmp(keyword, "Output") == 0)
1589 return test_bin(value, &edata->output, &edata->output_len);
1593 static int encode_test_run(struct evp_test *t)
1595 struct encode_data *edata = t->data;
1596 unsigned char *encode_out = NULL, *decode_out = NULL;
1597 int output_len, chunk_len;
1598 const char *err = "INTERNAL_ERROR";
1599 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1601 if (decode_ctx == NULL)
1604 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1605 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1606 if (encode_ctx == NULL)
1608 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1609 if (encode_out == NULL)
1612 EVP_EncodeInit(encode_ctx);
1613 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1614 edata->input, edata->input_len);
1615 output_len = chunk_len;
1617 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1618 output_len += chunk_len;
1620 EVP_ENCODE_CTX_free(encode_ctx);
1622 if (check_var_length_output(t, edata->output, edata->output_len,
1623 encode_out, output_len)) {
1624 err = "BAD_ENCODING";
1629 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1630 if (decode_out == NULL)
1633 EVP_DecodeInit(decode_ctx);
1634 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1635 edata->output_len) < 0) {
1636 err = "DECODE_ERROR";
1639 output_len = chunk_len;
1641 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1642 err = "DECODE_ERROR";
1645 output_len += chunk_len;
1647 if (edata->encoding != BASE64_INVALID_ENCODING &&
1648 check_var_length_output(t, edata->input, edata->input_len,
1649 decode_out, output_len)) {
1650 err = "BAD_DECODING";
1657 OPENSSL_free(encode_out);
1658 OPENSSL_free(decode_out);
1659 EVP_ENCODE_CTX_free(decode_ctx);
1663 static const struct evp_test_method encode_test_method = {
1666 encode_test_cleanup,
1672 * KDF operations: initially just TLS1 PRF but can be adapted.
1676 /* Context for this operation */
1678 /* Expected output */
1679 unsigned char *output;
1684 * Perform public key operation setup: lookup key, allocated ctx and call
1685 * the appropriate initialisation function
1687 static int kdf_test_init(struct evp_test *t, const char *name)
1689 struct kdf_data *kdata;
1691 kdata = OPENSSL_malloc(sizeof(*kdata));
1695 kdata->output = NULL;
1697 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1698 if (kdata->ctx == NULL)
1700 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1705 static void kdf_test_cleanup(struct evp_test *t)
1707 struct kdf_data *kdata = t->data;
1708 OPENSSL_free(kdata->output);
1709 EVP_PKEY_CTX_free(kdata->ctx);
1712 static int kdf_ctrl(EVP_PKEY_CTX *ctx, int op, const char *value)
1714 unsigned char *buf = NULL;
1717 if (test_bin(value, &buf, &buf_len) == 0)
1719 if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, op, buf_len, buf) <= 0)
1727 static int kdf_test_parse(struct evp_test *t,
1728 const char *keyword, const char *value)
1730 struct kdf_data *kdata = t->data;
1731 if (strcmp(keyword, "Output") == 0)
1732 return test_bin(value, &kdata->output, &kdata->output_len);
1733 else if (strcmp(keyword, "MD") == 0) {
1734 const EVP_MD *md = EVP_get_digestbyname(value);
1737 if (EVP_PKEY_CTX_set_tls1_prf_md(kdata->ctx, md) <= 0)
1740 } else if (strcmp(keyword, "Secret") == 0) {
1741 return kdf_ctrl(kdata->ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
1742 } else if (strncmp("Seed", keyword, 4) == 0) {
1743 return kdf_ctrl(kdata->ctx, EVP_PKEY_CTRL_TLS_SEED, value);
1748 static int kdf_test_run(struct evp_test *t)
1750 struct kdf_data *kdata = t->data;
1751 unsigned char *out = NULL;
1752 size_t out_len = kdata->output_len;
1753 const char *err = "INTERNAL_ERROR";
1754 out = OPENSSL_malloc(out_len);
1756 fprintf(stderr, "Error allocating output buffer!\n");
1759 err = "KDF_DERIVE_ERROR";
1760 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1762 err = "KDF_LENGTH_MISMATCH";
1763 if (out_len != kdata->output_len)
1765 err = "KDF_MISMATCH";
1766 if (check_output(t, kdata->output, out, out_len))
1775 static const struct evp_test_method kdf_test_method = {