2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
58 #include <openssl/evp.h>
59 #include <openssl/pem.h>
60 #include <openssl/err.h>
61 #include <openssl/x509v3.h>
62 #include <openssl/pkcs12.h>
63 #include <openssl/kdf.h>
64 #include "internal/numbers.h"
66 /* Remove spaces from beginning and end of a string */
68 static void remove_space(char **pval)
70 unsigned char *p = (unsigned char *)*pval;
77 p = p + strlen(*pval) - 1;
79 /* Remove trailing space */
85 * Given a line of the form:
86 * name = value # comment
87 * extract name and value. NB: modifies passed buffer.
90 static int parse_line(char **pkw, char **pval, char *linebuf)
94 p = linebuf + strlen(linebuf) - 1;
97 fprintf(stderr, "FATAL: missing EOL\n");
103 p = strchr(linebuf, '#');
108 /* Look for = sign */
109 p = strchr(linebuf, '=');
120 /* Remove spaces from keyword and value */
128 * Unescape some escape sequences in string literals.
129 * Return the result in a newly allocated buffer.
130 * Currently only supports '\n'.
131 * If the input length is 0, returns a valid 1-byte buffer, but sets
134 static unsigned char* unescape(const char *input, size_t input_len,
137 unsigned char *ret, *p;
139 if (input_len == 0) {
141 return OPENSSL_zalloc(1);
144 /* Escaping is non-expanding; over-allocate original size for simplicity. */
145 ret = p = OPENSSL_malloc(input_len);
149 for (i = 0; i < input_len; i++) {
150 if (input[i] == '\\') {
151 if (i == input_len - 1 || input[i+1] != 'n')
168 /* For a hex string "value" convert to a binary allocated buffer */
169 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
176 * Don't return NULL for zero length buffer.
177 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
178 * a non-NULL key buffer even if the key length is 0, in order to detect
181 *buf = OPENSSL_malloc(1);
188 /* Check for string literal */
189 if (value[0] == '"') {
192 vlen = strlen(value);
193 if (value[vlen - 1] != '"')
196 *buf = unescape(value, vlen, buflen);
202 *buf = string_to_hex(value, &len);
204 fprintf(stderr, "Value=%s\n", value);
205 ERR_print_errors_fp(stderr);
208 /* Size of input buffer means we'll never overflow */
212 /* Parse unsigned decimal 64 bit integer value */
213 static int test_uint64(const char *value, uint64_t *pr)
215 const char *p = value;
217 fprintf(stderr, "Invalid empty integer value\n");
222 if (*pr > UINT64_MAX/10) {
223 fprintf(stderr, "Integer string overflow value=%s\n", value);
227 if (*p < '0' || *p > '9') {
228 fprintf(stderr, "Invalid integer string value=%s\n", value);
237 /* Structure holding test information */
239 /* file being read */
241 /* List of public and private keys */
242 struct key_list *private;
243 struct key_list *public;
244 /* method for this test */
245 const struct evp_test_method *meth;
246 /* current line being processed */
248 /* start line of current test */
249 unsigned int start_line;
250 /* Error string for test */
252 /* Expected error value of test */
254 /* Number of tests */
258 /* Number of tests skipped */
260 /* If output mismatch expected and got value */
261 unsigned char *out_received;
262 size_t out_received_len;
263 unsigned char *out_expected;
264 size_t out_expected_len;
265 /* test specific data */
267 /* Current test should be skipped */
274 struct key_list *next;
277 /* Test method structure */
278 struct evp_test_method {
279 /* Name of test as it appears in file */
281 /* Initialise test for "alg" */
282 int (*init) (struct evp_test * t, const char *alg);
283 /* Clean up method */
284 void (*cleanup) (struct evp_test * t);
285 /* Test specific name value pair processing */
286 int (*parse) (struct evp_test * t, const char *name, const char *value);
287 /* Run the test itself */
288 int (*run_test) (struct evp_test * t);
291 static const struct evp_test_method digest_test_method, cipher_test_method;
292 static const struct evp_test_method mac_test_method;
293 static const struct evp_test_method psign_test_method, pverify_test_method;
294 static const struct evp_test_method pdecrypt_test_method;
295 static const struct evp_test_method pverify_recover_test_method;
296 static const struct evp_test_method pderive_test_method;
297 static const struct evp_test_method pbe_test_method;
298 static const struct evp_test_method encode_test_method;
299 static const struct evp_test_method kdf_test_method;
301 static const struct evp_test_method *evp_test_list[] = {
306 &pverify_test_method,
307 &pdecrypt_test_method,
308 &pverify_recover_test_method,
309 &pderive_test_method,
316 static const struct evp_test_method *evp_find_test(const char *name)
318 const struct evp_test_method **tt;
320 for (tt = evp_test_list; *tt; tt++) {
321 if (strcmp(name, (*tt)->name) == 0)
327 static void hex_print(const char *name, const unsigned char *buf, size_t len)
330 fprintf(stderr, "%s ", name);
331 for (i = 0; i < len; i++)
332 fprintf(stderr, "%02X", buf[i]);
336 static void free_expected(struct evp_test *t)
338 OPENSSL_free(t->expected_err);
339 t->expected_err = NULL;
340 OPENSSL_free(t->out_expected);
341 OPENSSL_free(t->out_received);
342 t->out_expected = NULL;
343 t->out_received = NULL;
344 t->out_expected_len = 0;
345 t->out_received_len = 0;
350 static void print_expected(struct evp_test *t)
352 if (t->out_expected == NULL && t->out_received == NULL)
354 hex_print("Expected:", t->out_expected, t->out_expected_len);
355 hex_print("Got: ", t->out_received, t->out_received_len);
359 static int check_test_error(struct evp_test *t)
361 if (!t->err && !t->expected_err)
363 if (t->err && !t->expected_err) {
364 fprintf(stderr, "Test line %d: unexpected error %s\n",
365 t->start_line, t->err);
369 if (!t->err && t->expected_err) {
370 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
371 t->start_line, t->expected_err);
374 if (strcmp(t->err, t->expected_err) == 0)
377 fprintf(stderr, "Test line %d: expecting %s got %s\n",
378 t->start_line, t->expected_err, t->err);
382 /* Setup a new test, run any existing test */
384 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
386 /* If we already have a test set up run it */
395 if (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);
407 OPENSSL_free(t->data);
409 OPENSSL_free(t->expected_err);
410 t->expected_err = NULL;
417 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
419 for (; lst; lst = lst->next) {
420 if (strcmp(lst->name, name) == 0) {
429 static void free_key_list(struct key_list *lst)
431 while (lst != NULL) {
432 struct key_list *ltmp;
433 EVP_PKEY_free(lst->key);
434 OPENSSL_free(lst->name);
441 static int check_unsupported()
443 long err = ERR_peek_error();
444 if (ERR_GET_LIB(err) == ERR_LIB_EVP
445 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
452 static int process_test(struct evp_test *t, char *buf, int verbose)
454 char *keyword = NULL, *value = NULL;
455 int rv = 0, add_key = 0;
457 struct key_list **lst = NULL, *key = NULL;
459 const struct evp_test_method *tmeth = NULL;
462 if (!parse_line(&keyword, &value, buf))
464 if (strcmp(keyword, "PrivateKey") == 0) {
465 save_pos = ftell(t->in);
466 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
467 if (pk == NULL && !check_unsupported()) {
468 fprintf(stderr, "Error reading private key %s\n", value);
469 ERR_print_errors_fp(stderr);
475 if (strcmp(keyword, "PublicKey") == 0) {
476 save_pos = ftell(t->in);
477 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
478 if (pk == NULL && !check_unsupported()) {
479 fprintf(stderr, "Error reading public key %s\n", value);
480 ERR_print_errors_fp(stderr);
486 /* If we have a key add to list */
489 if (find_key(NULL, value, *lst)) {
490 fprintf(stderr, "Duplicate key %s\n", value);
493 key = OPENSSL_malloc(sizeof(*key));
496 key->name = OPENSSL_strdup(value);
500 /* Rewind input, read to end and update line numbers */
501 fseek(t->in, save_pos, SEEK_SET);
502 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
504 if (strncmp(tmpbuf, "-----END", 8) == 0)
507 fprintf(stderr, "Can't find key end\n");
511 /* See if keyword corresponds to a test start */
512 tmeth = evp_find_test(keyword);
514 if (!setup_test(t, tmeth))
516 t->start_line = t->line;
518 if (!tmeth->init(t, value)) {
519 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
523 } else if (t->skip) {
525 } else if (strcmp(keyword, "Result") == 0) {
526 if (t->expected_err) {
527 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
530 t->expected_err = OPENSSL_strdup(value);
531 if (!t->expected_err)
534 /* Must be test specific line: try to parse it */
536 rv = t->meth->parse(t, keyword, value);
539 fprintf(stderr, "line %d: unexpected keyword %s\n",
543 fprintf(stderr, "line %d: error processing keyword %s\n",
551 static int check_var_length_output(struct evp_test *t,
552 const unsigned char *expected,
554 const unsigned char *received,
557 if (expected_len == received_len &&
558 memcmp(expected, received, expected_len) == 0) {
562 /* The result printing code expects a non-NULL buffer. */
563 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
564 t->out_expected_len = expected_len;
565 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
566 t->out_received_len = received_len;
567 if (t->out_expected == NULL || t->out_received == NULL) {
568 fprintf(stderr, "Memory allocation error!\n");
574 static int check_output(struct evp_test *t,
575 const unsigned char *expected,
576 const unsigned char *received,
579 return check_var_length_output(t, expected, len, received, len);
582 int main(int argc, char **argv)
589 fprintf(stderr, "usage: evp_test testfile.txt\n");
593 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
595 ERR_load_crypto_strings();
596 OpenSSL_add_all_algorithms();
598 memset(&t, 0, sizeof(t));
600 in = fopen(argv[1], "r");
602 while (fgets(buf, sizeof(buf), in)) {
604 if (!process_test(&t, buf, 0))
607 /* Run any final test we have */
608 if (!setup_test(&t, NULL))
610 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
611 t.ntests, t.errors, t.nskip);
612 free_key_list(t.public);
613 free_key_list(t.private);
616 CRYPTO_cleanup_all_ex_data();
617 ERR_remove_thread_state(NULL);
619 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
620 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
628 static void test_free(void *d)
633 /* Message digest tests */
636 /* Digest this test is for */
637 const EVP_MD *digest;
638 /* Input to digest */
639 unsigned char *input;
641 /* Repeat count for input */
643 /* Expected output */
644 unsigned char *output;
648 static int digest_test_init(struct evp_test *t, const char *alg)
650 const EVP_MD *digest;
651 struct digest_data *mdat;
652 digest = EVP_get_digestbyname(alg);
654 /* If alg has an OID assume disabled algorithm */
655 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
661 mdat = OPENSSL_malloc(sizeof(*mdat));
662 mdat->digest = digest;
670 static void digest_test_cleanup(struct evp_test *t)
672 struct digest_data *mdat = t->data;
673 test_free(mdat->input);
674 test_free(mdat->output);
677 static int digest_test_parse(struct evp_test *t,
678 const char *keyword, const char *value)
680 struct digest_data *mdata = t->data;
681 if (strcmp(keyword, "Input") == 0)
682 return test_bin(value, &mdata->input, &mdata->input_len);
683 if (strcmp(keyword, "Output") == 0)
684 return test_bin(value, &mdata->output, &mdata->output_len);
685 if (strcmp(keyword, "Count") == 0) {
686 long nrpt = atoi(value);
689 mdata->nrpt = (size_t)nrpt;
695 static int digest_test_run(struct evp_test *t)
697 struct digest_data *mdata = t->data;
699 const char *err = "INTERNAL_ERROR";
701 unsigned char md[EVP_MAX_MD_SIZE];
703 mctx = EVP_MD_CTX_new();
706 err = "DIGESTINIT_ERROR";
707 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
709 err = "DIGESTUPDATE_ERROR";
710 for (i = 0; i < mdata->nrpt; i++) {
711 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
714 err = "DIGESTFINAL_ERROR";
715 if (!EVP_DigestFinal(mctx, md, &md_len))
717 err = "DIGEST_LENGTH_MISMATCH";
718 if (md_len != mdata->output_len)
720 err = "DIGEST_MISMATCH";
721 if (check_output(t, mdata->output, md, md_len))
725 EVP_MD_CTX_free(mctx);
730 static const struct evp_test_method digest_test_method = {
740 const EVP_CIPHER *cipher;
742 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
748 unsigned char *plaintext;
749 size_t plaintext_len;
750 unsigned char *ciphertext;
751 size_t ciphertext_len;
759 static int cipher_test_init(struct evp_test *t, const char *alg)
761 const EVP_CIPHER *cipher;
762 struct cipher_data *cdat = t->data;
763 cipher = EVP_get_cipherbyname(alg);
765 /* If alg has an OID assume disabled algorithm */
766 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
772 cdat = OPENSSL_malloc(sizeof(*cdat));
773 cdat->cipher = cipher;
777 cdat->ciphertext = NULL;
778 cdat->plaintext = NULL;
782 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
783 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
784 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
785 cdat->aead = EVP_CIPHER_mode(cipher);
786 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
794 static void cipher_test_cleanup(struct evp_test *t)
796 struct cipher_data *cdat = t->data;
797 test_free(cdat->key);
799 test_free(cdat->ciphertext);
800 test_free(cdat->plaintext);
801 test_free(cdat->aad);
802 test_free(cdat->tag);
805 static int cipher_test_parse(struct evp_test *t, const char *keyword,
808 struct cipher_data *cdat = t->data;
809 if (strcmp(keyword, "Key") == 0)
810 return test_bin(value, &cdat->key, &cdat->key_len);
811 if (strcmp(keyword, "IV") == 0)
812 return test_bin(value, &cdat->iv, &cdat->iv_len);
813 if (strcmp(keyword, "Plaintext") == 0)
814 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
815 if (strcmp(keyword, "Ciphertext") == 0)
816 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
818 if (strcmp(keyword, "AAD") == 0)
819 return test_bin(value, &cdat->aad, &cdat->aad_len);
820 if (strcmp(keyword, "Tag") == 0)
821 return test_bin(value, &cdat->tag, &cdat->tag_len);
824 if (strcmp(keyword, "Operation") == 0) {
825 if (strcmp(value, "ENCRYPT") == 0)
827 else if (strcmp(value, "DECRYPT") == 0)
836 static int cipher_test_enc(struct evp_test *t, int enc)
838 struct cipher_data *cdat = t->data;
839 unsigned char *in, *out, *tmp = NULL;
840 size_t in_len, out_len;
842 EVP_CIPHER_CTX *ctx = NULL;
844 err = "INTERNAL_ERROR";
845 ctx = EVP_CIPHER_CTX_new();
848 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
850 in = cdat->plaintext;
851 in_len = cdat->plaintext_len;
852 out = cdat->ciphertext;
853 out_len = cdat->ciphertext_len;
855 in = cdat->ciphertext;
856 in_len = cdat->ciphertext_len;
857 out = cdat->plaintext;
858 out_len = cdat->plaintext_len;
860 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
863 err = "CIPHERINIT_ERROR";
864 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
866 err = "INVALID_IV_LENGTH";
869 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
872 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
878 * If encrypting or OCB just set tag length initially, otherwise
879 * set tag length and value.
881 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
882 err = "TAG_LENGTH_SET_ERROR";
885 err = "TAG_SET_ERROR";
888 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
889 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
895 err = "INVALID_KEY_LENGTH";
896 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
898 err = "KEY_SET_ERROR";
899 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
902 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
903 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
904 cdat->tag_len, cdat->tag)) {
905 err = "TAG_SET_ERROR";
910 if (cdat->aead == EVP_CIPH_CCM_MODE) {
911 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
912 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
917 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
918 err = "AAD_SET_ERROR";
922 EVP_CIPHER_CTX_set_padding(ctx, 0);
923 err = "CIPHERUPDATE_ERROR";
924 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
926 if (cdat->aead == EVP_CIPH_CCM_MODE)
929 err = "CIPHERFINAL_ERROR";
930 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
933 err = "LENGTH_MISMATCH";
934 if (out_len != (size_t)(tmplen + tmpflen))
936 err = "VALUE_MISMATCH";
937 if (check_output(t, out, tmp, out_len))
939 if (enc && cdat->aead) {
940 unsigned char rtag[16];
941 if (cdat->tag_len > sizeof(rtag)) {
942 err = "TAG_LENGTH_INTERNAL_ERROR";
945 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
946 cdat->tag_len, rtag)) {
947 err = "TAG_RETRIEVE_ERROR";
950 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
951 err = "TAG_VALUE_MISMATCH";
958 EVP_CIPHER_CTX_free(ctx);
963 static int cipher_test_run(struct evp_test *t)
965 struct cipher_data *cdat = t->data;
971 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
972 /* IV is optional and usually omitted in wrap mode */
973 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
978 if (cdat->aead && !cdat->tag) {
983 rv = cipher_test_enc(t, 1);
984 /* Not fatal errors: return */
991 if (cdat->enc != 1) {
992 rv = cipher_test_enc(t, 0);
993 /* Not fatal errors: return */
1003 static const struct evp_test_method cipher_test_method = {
1006 cipher_test_cleanup,
1014 /* Algorithm string for this MAC */
1020 unsigned char *input;
1022 /* Expected output */
1023 unsigned char *output;
1027 static int mac_test_init(struct evp_test *t, const char *alg)
1030 struct mac_data *mdat;
1031 if (strcmp(alg, "HMAC") == 0)
1032 type = EVP_PKEY_HMAC;
1033 else if (strcmp(alg, "CMAC") == 0)
1034 type = EVP_PKEY_CMAC;
1038 mdat = OPENSSL_malloc(sizeof(*mdat));
1043 mdat->output = NULL;
1048 static void mac_test_cleanup(struct evp_test *t)
1050 struct mac_data *mdat = t->data;
1051 test_free(mdat->alg);
1052 test_free(mdat->key);
1053 test_free(mdat->input);
1054 test_free(mdat->output);
1057 static int mac_test_parse(struct evp_test *t,
1058 const char *keyword, const char *value)
1060 struct mac_data *mdata = t->data;
1061 if (strcmp(keyword, "Key") == 0)
1062 return test_bin(value, &mdata->key, &mdata->key_len);
1063 if (strcmp(keyword, "Algorithm") == 0) {
1064 mdata->alg = OPENSSL_strdup(value);
1069 if (strcmp(keyword, "Input") == 0)
1070 return test_bin(value, &mdata->input, &mdata->input_len);
1071 if (strcmp(keyword, "Output") == 0)
1072 return test_bin(value, &mdata->output, &mdata->output_len);
1076 static int mac_test_run(struct evp_test *t)
1078 struct mac_data *mdata = t->data;
1079 const char *err = "INTERNAL_ERROR";
1080 EVP_MD_CTX *mctx = NULL;
1081 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1082 EVP_PKEY *key = NULL;
1083 const EVP_MD *md = NULL;
1084 unsigned char *mac = NULL;
1087 err = "MAC_PKEY_CTX_ERROR";
1088 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1092 err = "MAC_KEYGEN_INIT_ERROR";
1093 if (EVP_PKEY_keygen_init(genctx) <= 0)
1095 if (mdata->type == EVP_PKEY_CMAC) {
1096 err = "MAC_ALGORITHM_SET_ERROR";
1097 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1101 err = "MAC_KEY_SET_ERROR";
1102 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1105 err = "MAC_KEY_GENERATE_ERROR";
1106 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1108 if (mdata->type == EVP_PKEY_HMAC) {
1109 err = "MAC_ALGORITHM_SET_ERROR";
1110 md = EVP_get_digestbyname(mdata->alg);
1114 mctx = EVP_MD_CTX_new();
1117 err = "DIGESTSIGNINIT_ERROR";
1118 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1121 err = "DIGESTSIGNUPDATE_ERROR";
1122 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1124 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1125 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1127 mac = OPENSSL_malloc(mac_len);
1129 fprintf(stderr, "Error allocating mac buffer!\n");
1132 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1134 err = "MAC_LENGTH_MISMATCH";
1135 if (mac_len != mdata->output_len)
1137 err = "MAC_MISMATCH";
1138 if (check_output(t, mdata->output, mac, mac_len))
1142 EVP_MD_CTX_free(mctx);
1144 EVP_PKEY_CTX_free(genctx);
1150 static const struct evp_test_method mac_test_method = {
1159 * Public key operations. These are all very similar and can share
1160 * a lot of common code.
1164 /* Context for this operation */
1166 /* Key operation to perform */
1167 int (*keyop) (EVP_PKEY_CTX *ctx,
1168 unsigned char *sig, size_t *siglen,
1169 const unsigned char *tbs, size_t tbslen);
1171 unsigned char *input;
1173 /* Expected output */
1174 unsigned char *output;
1179 * Perform public key operation setup: lookup key, allocated ctx and call
1180 * the appropriate initialisation function
1182 static int pkey_test_init(struct evp_test *t, const char *name,
1184 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1185 int (*keyop) (EVP_PKEY_CTX *ctx,
1186 unsigned char *sig, size_t *siglen,
1187 const unsigned char *tbs,
1191 struct pkey_data *kdata;
1192 EVP_PKEY *pkey = NULL;
1195 rv = find_key(&pkey, name, t->public);
1197 rv = find_key(&pkey, name, t->private);
1205 kdata = OPENSSL_malloc(sizeof(*kdata));
1207 EVP_PKEY_free(pkey);
1211 kdata->input = NULL;
1212 kdata->output = NULL;
1213 kdata->keyop = keyop;
1215 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1218 if (keyopinit(kdata->ctx) <= 0)
1223 static void pkey_test_cleanup(struct evp_test *t)
1225 struct pkey_data *kdata = t->data;
1227 OPENSSL_free(kdata->input);
1228 OPENSSL_free(kdata->output);
1229 EVP_PKEY_CTX_free(kdata->ctx);
1232 static int pkey_test_ctrl(EVP_PKEY_CTX *pctx, const char *value)
1237 tmpval = OPENSSL_strdup(value);
1240 p = strchr(tmpval, ':');
1243 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1244 OPENSSL_free(tmpval);
1248 static int pkey_test_parse(struct evp_test *t,
1249 const char *keyword, const char *value)
1251 struct pkey_data *kdata = t->data;
1252 if (strcmp(keyword, "Input") == 0)
1253 return test_bin(value, &kdata->input, &kdata->input_len);
1254 if (strcmp(keyword, "Output") == 0)
1255 return test_bin(value, &kdata->output, &kdata->output_len);
1256 if (strcmp(keyword, "Ctrl") == 0)
1257 return pkey_test_ctrl(kdata->ctx, value);
1261 static int pkey_test_run(struct evp_test *t)
1263 struct pkey_data *kdata = t->data;
1264 unsigned char *out = NULL;
1266 const char *err = "KEYOP_LENGTH_ERROR";
1267 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1268 kdata->input_len) <= 0)
1270 out = OPENSSL_malloc(out_len);
1272 fprintf(stderr, "Error allocating output buffer!\n");
1275 err = "KEYOP_ERROR";
1277 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1279 err = "KEYOP_LENGTH_MISMATCH";
1280 if (out_len != kdata->output_len)
1282 err = "KEYOP_MISMATCH";
1283 if (check_output(t, kdata->output, out, out_len))
1292 static int sign_test_init(struct evp_test *t, const char *name)
1294 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1297 static const struct evp_test_method psign_test_method = {
1305 static int verify_recover_test_init(struct evp_test *t, const char *name)
1307 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1308 EVP_PKEY_verify_recover);
1311 static const struct evp_test_method pverify_recover_test_method = {
1313 verify_recover_test_init,
1319 static int decrypt_test_init(struct evp_test *t, const char *name)
1321 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1325 static const struct evp_test_method pdecrypt_test_method = {
1333 static int verify_test_init(struct evp_test *t, const char *name)
1335 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1338 static int verify_test_run(struct evp_test *t)
1340 struct pkey_data *kdata = t->data;
1341 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1342 kdata->input, kdata->input_len) <= 0)
1343 t->err = "VERIFY_ERROR";
1347 static const struct evp_test_method pverify_test_method = {
1356 static int pderive_test_init(struct evp_test *t, const char *name)
1358 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1361 static int pderive_test_parse(struct evp_test *t,
1362 const char *keyword, const char *value)
1364 struct pkey_data *kdata = t->data;
1366 if (strcmp(keyword, "PeerKey") == 0) {
1368 if (find_key(&peer, value, t->public) == 0)
1370 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1374 if (strcmp(keyword, "SharedSecret") == 0)
1375 return test_bin(value, &kdata->output, &kdata->output_len);
1376 if (strcmp(keyword, "Ctrl") == 0)
1377 return pkey_test_ctrl(kdata->ctx, value);
1381 static int pderive_test_run(struct evp_test *t)
1383 struct pkey_data *kdata = t->data;
1384 unsigned char *out = NULL;
1386 const char *err = "INTERNAL_ERROR";
1388 out_len = kdata->output_len;
1389 out = OPENSSL_malloc(out_len);
1391 fprintf(stderr, "Error allocating output buffer!\n");
1394 err = "DERIVE_ERROR";
1395 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1397 err = "SHARED_SECRET_LENGTH_MISMATCH";
1398 if (out_len != kdata->output_len)
1400 err = "SHARED_SECRET_MISMATCH";
1401 if (check_output(t, kdata->output, out, out_len))
1410 static const struct evp_test_method pderive_test_method = {
1420 #define PBE_TYPE_SCRYPT 1
1421 #define PBE_TYPE_PBKDF2 2
1422 #define PBE_TYPE_PKCS12 3
1428 /* scrypt parameters */
1429 uint64_t N, r, p, maxmem;
1431 /* PKCS#12 parameters */
1436 unsigned char *pass;
1440 unsigned char *salt;
1443 /* Expected output */
1448 #ifndef OPENSSL_NO_SCRYPT
1449 static int scrypt_test_parse(struct evp_test *t,
1450 const char *keyword, const char *value)
1452 struct pbe_data *pdata = t->data;
1454 if (strcmp(keyword, "N") == 0)
1455 return test_uint64(value, &pdata->N);
1456 if (strcmp(keyword, "p") == 0)
1457 return test_uint64(value, &pdata->p);
1458 if (strcmp(keyword, "r") == 0)
1459 return test_uint64(value, &pdata->r);
1460 if (strcmp(keyword, "maxmem") == 0)
1461 return test_uint64(value, &pdata->maxmem);
1466 static int pbkdf2_test_parse(struct evp_test *t,
1467 const char *keyword, const char *value)
1469 struct pbe_data *pdata = t->data;
1471 if (strcmp(keyword, "iter") == 0) {
1472 pdata->iter = atoi(value);
1473 if (pdata->iter <= 0)
1477 if (strcmp(keyword, "MD") == 0) {
1478 pdata->md = EVP_get_digestbyname(value);
1479 if (pdata->md == NULL)
1486 static int pkcs12_test_parse(struct evp_test *t,
1487 const char *keyword, const char *value)
1489 struct pbe_data *pdata = t->data;
1491 if (strcmp(keyword, "id") == 0) {
1492 pdata->id = atoi(value);
1497 return pbkdf2_test_parse(t, keyword, value);
1500 static int pbe_test_init(struct evp_test *t, const char *alg)
1502 struct pbe_data *pdat;
1505 #ifndef OPENSSL_NO_SCRYPT
1506 if (strcmp(alg, "scrypt") == 0)
1507 pbe_type = PBE_TYPE_SCRYPT;
1509 else if (strcmp(alg, "pbkdf2") == 0)
1510 pbe_type = PBE_TYPE_PBKDF2;
1511 else if (strcmp(alg, "pkcs12") == 0)
1512 pbe_type = PBE_TYPE_PKCS12;
1514 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1515 pdat = OPENSSL_malloc(sizeof(*pdat));
1516 pdat->pbe_type = pbe_type;
1530 static void pbe_test_cleanup(struct evp_test *t)
1532 struct pbe_data *pdat = t->data;
1533 test_free(pdat->pass);
1534 test_free(pdat->salt);
1535 test_free(pdat->key);
1538 static int pbe_test_parse(struct evp_test *t,
1539 const char *keyword, const char *value)
1541 struct pbe_data *pdata = t->data;
1543 if (strcmp(keyword, "Password") == 0)
1544 return test_bin(value, &pdata->pass, &pdata->pass_len);
1545 if (strcmp(keyword, "Salt") == 0)
1546 return test_bin(value, &pdata->salt, &pdata->salt_len);
1547 if (strcmp(keyword, "Key") == 0)
1548 return test_bin(value, &pdata->key, &pdata->key_len);
1549 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1550 return pbkdf2_test_parse(t, keyword, value);
1551 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1552 return pkcs12_test_parse(t, keyword, value);
1553 #ifndef OPENSSL_NO_SCRYPT
1554 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1555 return scrypt_test_parse(t, keyword, value);
1560 static int pbe_test_run(struct evp_test *t)
1562 struct pbe_data *pdata = t->data;
1563 const char *err = "INTERNAL_ERROR";
1566 key = OPENSSL_malloc(pdata->key_len);
1569 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1570 err = "PBKDF2_ERROR";
1571 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1572 pdata->salt, pdata->salt_len,
1573 pdata->iter, pdata->md,
1574 pdata->key_len, key) == 0)
1576 #ifndef OPENSSL_NO_SCRYPT
1577 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1578 err = "SCRYPT_ERROR";
1579 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1580 pdata->salt, pdata->salt_len,
1581 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1582 key, pdata->key_len) == 0)
1585 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1586 err = "PKCS12_ERROR";
1587 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1588 pdata->salt, pdata->salt_len,
1589 pdata->id, pdata->iter, pdata->key_len,
1590 key, pdata->md) == 0)
1593 err = "KEY_MISMATCH";
1594 if (check_output(t, pdata->key, key, pdata->key_len))
1603 static const struct evp_test_method pbe_test_method = {
1614 BASE64_CANONICAL_ENCODING = 0,
1615 BASE64_VALID_ENCODING = 1,
1616 BASE64_INVALID_ENCODING = 2
1617 } base64_encoding_type;
1619 struct encode_data {
1620 /* Input to encoding */
1621 unsigned char *input;
1623 /* Expected output */
1624 unsigned char *output;
1626 base64_encoding_type encoding;
1629 static int encode_test_init(struct evp_test *t, const char *encoding)
1631 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1633 if (strcmp(encoding, "canonical") == 0) {
1634 edata->encoding = BASE64_CANONICAL_ENCODING;
1635 } else if (strcmp(encoding, "valid") == 0) {
1636 edata->encoding = BASE64_VALID_ENCODING;
1637 } else if (strcmp(encoding, "invalid") == 0) {
1638 edata->encoding = BASE64_INVALID_ENCODING;
1639 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1640 if (t->expected_err == NULL)
1643 fprintf(stderr, "Bad encoding: %s. Should be one of "
1644 "{canonical, valid, invalid}\n", encoding);
1651 static void encode_test_cleanup(struct evp_test *t)
1653 struct encode_data *edata = t->data;
1654 test_free(edata->input);
1655 test_free(edata->output);
1656 memset(edata, 0, sizeof(*edata));
1659 static int encode_test_parse(struct evp_test *t,
1660 const char *keyword, const char *value)
1662 struct encode_data *edata = t->data;
1663 if (strcmp(keyword, "Input") == 0)
1664 return test_bin(value, &edata->input, &edata->input_len);
1665 if (strcmp(keyword, "Output") == 0)
1666 return test_bin(value, &edata->output, &edata->output_len);
1670 static int encode_test_run(struct evp_test *t)
1672 struct encode_data *edata = t->data;
1673 unsigned char *encode_out = NULL, *decode_out = NULL;
1674 int output_len, chunk_len;
1675 const char *err = "INTERNAL_ERROR";
1676 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1678 if (decode_ctx == NULL)
1681 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1682 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1683 if (encode_ctx == NULL)
1685 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1686 if (encode_out == NULL)
1689 EVP_EncodeInit(encode_ctx);
1690 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1691 edata->input, edata->input_len);
1692 output_len = chunk_len;
1694 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1695 output_len += chunk_len;
1697 EVP_ENCODE_CTX_free(encode_ctx);
1699 if (check_var_length_output(t, edata->output, edata->output_len,
1700 encode_out, output_len)) {
1701 err = "BAD_ENCODING";
1706 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1707 if (decode_out == NULL)
1710 EVP_DecodeInit(decode_ctx);
1711 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1712 edata->output_len) < 0) {
1713 err = "DECODE_ERROR";
1716 output_len = chunk_len;
1718 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1719 err = "DECODE_ERROR";
1722 output_len += chunk_len;
1724 if (edata->encoding != BASE64_INVALID_ENCODING &&
1725 check_var_length_output(t, edata->input, edata->input_len,
1726 decode_out, output_len)) {
1727 err = "BAD_DECODING";
1734 OPENSSL_free(encode_out);
1735 OPENSSL_free(decode_out);
1736 EVP_ENCODE_CTX_free(decode_ctx);
1740 static const struct evp_test_method encode_test_method = {
1743 encode_test_cleanup,
1749 * KDF operations: initially just TLS1 PRF but can be adapted.
1753 /* Context for this operation */
1755 /* Expected output */
1756 unsigned char *output;
1761 * Perform public key operation setup: lookup key, allocated ctx and call
1762 * the appropriate initialisation function
1764 static int kdf_test_init(struct evp_test *t, const char *name)
1766 struct kdf_data *kdata;
1768 kdata = OPENSSL_malloc(sizeof(*kdata));
1772 kdata->output = NULL;
1774 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1775 if (kdata->ctx == NULL)
1777 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1782 static void kdf_test_cleanup(struct evp_test *t)
1784 struct kdf_data *kdata = t->data;
1785 OPENSSL_free(kdata->output);
1786 EVP_PKEY_CTX_free(kdata->ctx);
1789 static int kdf_ctrl(EVP_PKEY_CTX *ctx, int op, const char *value)
1791 unsigned char *buf = NULL;
1794 if (test_bin(value, &buf, &buf_len) == 0)
1796 if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, op, buf_len, buf) <= 0)
1804 static int kdf_test_parse(struct evp_test *t,
1805 const char *keyword, const char *value)
1807 struct kdf_data *kdata = t->data;
1808 if (strcmp(keyword, "Output") == 0)
1809 return test_bin(value, &kdata->output, &kdata->output_len);
1810 else if (strcmp(keyword, "MD") == 0) {
1811 const EVP_MD *md = EVP_get_digestbyname(value);
1814 if (EVP_PKEY_CTX_set_tls1_prf_md(kdata->ctx, md) <= 0)
1817 } else if (strcmp(keyword, "Secret") == 0) {
1818 return kdf_ctrl(kdata->ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
1819 } else if (strncmp("Seed", keyword, 4) == 0) {
1820 return kdf_ctrl(kdata->ctx, EVP_PKEY_CTRL_TLS_SEED, value);
1825 static int kdf_test_run(struct evp_test *t)
1827 struct kdf_data *kdata = t->data;
1828 unsigned char *out = NULL;
1829 size_t out_len = kdata->output_len;
1830 const char *err = "INTERNAL_ERROR";
1831 out = OPENSSL_malloc(out_len);
1833 fprintf(stderr, "Error allocating output buffer!\n");
1836 err = "KDF_DERIVE_ERROR";
1837 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1839 err = "KDF_LENGTH_MISMATCH";
1840 if (out_len != kdata->output_len)
1842 err = "KDF_MISMATCH";
1843 if (check_output(t, kdata->output, out, out_len))
1852 static const struct evp_test_method kdf_test_method = {