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 = OPENSSL_hexstr2buf(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 #ifndef OPENSSL_NO_SCRYPT
213 /* Currently only used by scrypt tests */
214 /* Parse unsigned decimal 64 bit integer value */
215 static int test_uint64(const char *value, uint64_t *pr)
217 const char *p = value;
219 fprintf(stderr, "Invalid empty integer value\n");
224 if (*pr > UINT64_MAX/10) {
225 fprintf(stderr, "Integer string overflow value=%s\n", value);
229 if (*p < '0' || *p > '9') {
230 fprintf(stderr, "Invalid integer string value=%s\n", value);
240 /* Structure holding test information */
242 /* file being read */
244 /* List of public and private keys */
245 struct key_list *private;
246 struct key_list *public;
247 /* method for this test */
248 const struct evp_test_method *meth;
249 /* current line being processed */
251 /* start line of current test */
252 unsigned int start_line;
253 /* Error string for test */
254 const char *err, *aux_err;
255 /* Expected error value of test */
257 /* Number of tests */
261 /* Number of tests skipped */
263 /* If output mismatch expected and got value */
264 unsigned char *out_received;
265 size_t out_received_len;
266 unsigned char *out_expected;
267 size_t out_expected_len;
268 /* test specific data */
270 /* Current test should be skipped */
277 struct key_list *next;
280 /* Test method structure */
281 struct evp_test_method {
282 /* Name of test as it appears in file */
284 /* Initialise test for "alg" */
285 int (*init) (struct evp_test * t, const char *alg);
286 /* Clean up method */
287 void (*cleanup) (struct evp_test * t);
288 /* Test specific name value pair processing */
289 int (*parse) (struct evp_test * t, const char *name, const char *value);
290 /* Run the test itself */
291 int (*run_test) (struct evp_test * t);
294 static const struct evp_test_method digest_test_method, cipher_test_method;
295 static const struct evp_test_method mac_test_method;
296 static const struct evp_test_method psign_test_method, pverify_test_method;
297 static const struct evp_test_method pdecrypt_test_method;
298 static const struct evp_test_method pverify_recover_test_method;
299 static const struct evp_test_method pderive_test_method;
300 static const struct evp_test_method pbe_test_method;
301 static const struct evp_test_method encode_test_method;
302 static const struct evp_test_method kdf_test_method;
304 static const struct evp_test_method *evp_test_list[] = {
309 &pverify_test_method,
310 &pdecrypt_test_method,
311 &pverify_recover_test_method,
312 &pderive_test_method,
319 static const struct evp_test_method *evp_find_test(const char *name)
321 const struct evp_test_method **tt;
323 for (tt = evp_test_list; *tt; tt++) {
324 if (strcmp(name, (*tt)->name) == 0)
330 static void hex_print(const char *name, const unsigned char *buf, size_t len)
333 fprintf(stderr, "%s ", name);
334 for (i = 0; i < len; i++)
335 fprintf(stderr, "%02X", buf[i]);
339 static void free_expected(struct evp_test *t)
341 OPENSSL_free(t->expected_err);
342 t->expected_err = NULL;
343 OPENSSL_free(t->out_expected);
344 OPENSSL_free(t->out_received);
345 t->out_expected = NULL;
346 t->out_received = NULL;
347 t->out_expected_len = 0;
348 t->out_received_len = 0;
353 static void print_expected(struct evp_test *t)
355 if (t->out_expected == NULL && t->out_received == NULL)
357 hex_print("Expected:", t->out_expected, t->out_expected_len);
358 hex_print("Got: ", t->out_received, t->out_received_len);
362 static int check_test_error(struct evp_test *t)
364 if (!t->err && !t->expected_err)
366 if (t->err && !t->expected_err) {
367 if (t->aux_err != NULL) {
368 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
369 t->start_line, t->aux_err, t->err);
371 fprintf(stderr, "Test line %d: unexpected error %s\n",
372 t->start_line, t->err);
377 if (!t->err && t->expected_err) {
378 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
379 t->start_line, t->expected_err);
382 if (strcmp(t->err, t->expected_err) == 0)
385 fprintf(stderr, "Test line %d: expecting %s got %s\n",
386 t->start_line, t->expected_err, t->err);
390 /* Setup a new test, run any existing test */
392 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
394 /* If we already have a test set up run it */
403 if (t->meth->run_test(t) != 1) {
404 fprintf(stderr, "%s test error line %d\n",
405 t->meth->name, t->start_line);
408 if (!check_test_error(t)) {
410 ERR_print_errors_fp(stderr);
415 OPENSSL_free(t->data);
417 OPENSSL_free(t->expected_err);
418 t->expected_err = NULL;
425 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
427 for (; lst; lst = lst->next) {
428 if (strcmp(lst->name, name) == 0) {
437 static void free_key_list(struct key_list *lst)
439 while (lst != NULL) {
440 struct key_list *ltmp;
441 EVP_PKEY_free(lst->key);
442 OPENSSL_free(lst->name);
449 static int check_unsupported()
451 long err = ERR_peek_error();
452 if (ERR_GET_LIB(err) == ERR_LIB_EVP
453 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
460 static int process_test(struct evp_test *t, char *buf, int verbose)
462 char *keyword = NULL, *value = NULL;
463 int rv = 0, add_key = 0;
465 struct key_list **lst = NULL, *key = NULL;
467 const struct evp_test_method *tmeth = NULL;
470 if (!parse_line(&keyword, &value, buf))
472 if (strcmp(keyword, "PrivateKey") == 0) {
473 save_pos = BIO_tell(t->in);
474 pk = PEM_read_bio_PrivateKey(t->in, NULL, 0, NULL);
475 if (pk == NULL && !check_unsupported()) {
476 fprintf(stderr, "Error reading private key %s\n", value);
477 ERR_print_errors_fp(stderr);
483 if (strcmp(keyword, "PublicKey") == 0) {
484 save_pos = BIO_tell(t->in);
485 pk = PEM_read_bio_PUBKEY(t->in, NULL, 0, NULL);
486 if (pk == NULL && !check_unsupported()) {
487 fprintf(stderr, "Error reading public key %s\n", value);
488 ERR_print_errors_fp(stderr);
494 /* If we have a key add to list */
497 if (find_key(NULL, value, *lst)) {
498 fprintf(stderr, "Duplicate key %s\n", value);
501 key = OPENSSL_malloc(sizeof(*key));
504 key->name = OPENSSL_strdup(value);
508 /* Rewind input, read to end and update line numbers */
509 (void)BIO_seek(t->in, save_pos);
510 while (BIO_gets(t->in,tmpbuf, sizeof(tmpbuf))) {
512 if (strncmp(tmpbuf, "-----END", 8) == 0)
515 fprintf(stderr, "Can't find key end\n");
519 /* See if keyword corresponds to a test start */
520 tmeth = evp_find_test(keyword);
522 if (!setup_test(t, tmeth))
524 t->start_line = t->line;
526 if (!tmeth->init(t, value)) {
527 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
531 } else if (t->skip) {
533 } else if (strcmp(keyword, "Result") == 0) {
534 if (t->expected_err) {
535 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
538 t->expected_err = OPENSSL_strdup(value);
539 if (!t->expected_err)
542 /* Must be test specific line: try to parse it */
544 rv = t->meth->parse(t, keyword, value);
547 fprintf(stderr, "line %d: unexpected keyword %s\n",
551 fprintf(stderr, "line %d: error processing keyword %s\n",
559 static int check_var_length_output(struct evp_test *t,
560 const unsigned char *expected,
562 const unsigned char *received,
565 if (expected_len == received_len &&
566 memcmp(expected, received, expected_len) == 0) {
570 /* The result printing code expects a non-NULL buffer. */
571 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
572 t->out_expected_len = expected_len;
573 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
574 t->out_received_len = received_len;
575 if (t->out_expected == NULL || t->out_received == NULL) {
576 fprintf(stderr, "Memory allocation error!\n");
582 static int check_output(struct evp_test *t,
583 const unsigned char *expected,
584 const unsigned char *received,
587 return check_var_length_output(t, expected, len, received, len);
590 int main(int argc, char **argv)
597 fprintf(stderr, "usage: evp_test testfile.txt\n");
601 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
603 memset(&t, 0, sizeof(t));
605 in = BIO_new_file(argv[1], "r");
607 while (BIO_gets(in, buf, sizeof(buf))) {
609 if (!process_test(&t, buf, 0))
612 /* Run any final test we have */
613 if (!setup_test(&t, NULL))
615 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
616 t.ntests, t.errors, t.nskip);
617 free_key_list(t.public);
618 free_key_list(t.private);
621 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
622 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
630 static void test_free(void *d)
635 /* Message digest tests */
638 /* Digest this test is for */
639 const EVP_MD *digest;
640 /* Input to digest */
641 unsigned char *input;
643 /* Repeat count for input */
645 /* Expected output */
646 unsigned char *output;
650 static int digest_test_init(struct evp_test *t, const char *alg)
652 const EVP_MD *digest;
653 struct digest_data *mdat;
654 digest = EVP_get_digestbyname(alg);
656 /* If alg has an OID assume disabled algorithm */
657 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
663 mdat = OPENSSL_malloc(sizeof(*mdat));
664 mdat->digest = digest;
672 static void digest_test_cleanup(struct evp_test *t)
674 struct digest_data *mdat = t->data;
675 test_free(mdat->input);
676 test_free(mdat->output);
679 static int digest_test_parse(struct evp_test *t,
680 const char *keyword, const char *value)
682 struct digest_data *mdata = t->data;
683 if (strcmp(keyword, "Input") == 0)
684 return test_bin(value, &mdata->input, &mdata->input_len);
685 if (strcmp(keyword, "Output") == 0)
686 return test_bin(value, &mdata->output, &mdata->output_len);
687 if (strcmp(keyword, "Count") == 0) {
688 long nrpt = atoi(value);
691 mdata->nrpt = (size_t)nrpt;
697 static int digest_test_run(struct evp_test *t)
699 struct digest_data *mdata = t->data;
701 const char *err = "INTERNAL_ERROR";
703 unsigned char md[EVP_MAX_MD_SIZE];
705 mctx = EVP_MD_CTX_new();
708 err = "DIGESTINIT_ERROR";
709 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
711 err = "DIGESTUPDATE_ERROR";
712 for (i = 0; i < mdata->nrpt; i++) {
713 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
716 err = "DIGESTFINAL_ERROR";
717 if (!EVP_DigestFinal(mctx, md, &md_len))
719 err = "DIGEST_LENGTH_MISMATCH";
720 if (md_len != mdata->output_len)
722 err = "DIGEST_MISMATCH";
723 if (check_output(t, mdata->output, md, md_len))
727 EVP_MD_CTX_free(mctx);
732 static const struct evp_test_method digest_test_method = {
742 const EVP_CIPHER *cipher;
744 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
750 unsigned char *plaintext;
751 size_t plaintext_len;
752 unsigned char *ciphertext;
753 size_t ciphertext_len;
761 static int cipher_test_init(struct evp_test *t, const char *alg)
763 const EVP_CIPHER *cipher;
764 struct cipher_data *cdat = t->data;
765 cipher = EVP_get_cipherbyname(alg);
767 /* If alg has an OID assume disabled algorithm */
768 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
774 cdat = OPENSSL_malloc(sizeof(*cdat));
775 cdat->cipher = cipher;
779 cdat->ciphertext = NULL;
780 cdat->plaintext = NULL;
784 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
785 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
786 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
787 cdat->aead = EVP_CIPHER_mode(cipher);
788 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
796 static void cipher_test_cleanup(struct evp_test *t)
798 struct cipher_data *cdat = t->data;
799 test_free(cdat->key);
801 test_free(cdat->ciphertext);
802 test_free(cdat->plaintext);
803 test_free(cdat->aad);
804 test_free(cdat->tag);
807 static int cipher_test_parse(struct evp_test *t, const char *keyword,
810 struct cipher_data *cdat = t->data;
811 if (strcmp(keyword, "Key") == 0)
812 return test_bin(value, &cdat->key, &cdat->key_len);
813 if (strcmp(keyword, "IV") == 0)
814 return test_bin(value, &cdat->iv, &cdat->iv_len);
815 if (strcmp(keyword, "Plaintext") == 0)
816 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
817 if (strcmp(keyword, "Ciphertext") == 0)
818 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
820 if (strcmp(keyword, "AAD") == 0)
821 return test_bin(value, &cdat->aad, &cdat->aad_len);
822 if (strcmp(keyword, "Tag") == 0)
823 return test_bin(value, &cdat->tag, &cdat->tag_len);
826 if (strcmp(keyword, "Operation") == 0) {
827 if (strcmp(value, "ENCRYPT") == 0)
829 else if (strcmp(value, "DECRYPT") == 0)
838 static int cipher_test_enc(struct evp_test *t, int enc,
839 size_t out_misalign, size_t inp_misalign)
841 struct cipher_data *cdat = t->data;
842 unsigned char *in, *out, *tmp = NULL;
843 size_t in_len, out_len;
845 EVP_CIPHER_CTX *ctx = NULL;
847 err = "INTERNAL_ERROR";
848 ctx = EVP_CIPHER_CTX_new();
851 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
853 in = cdat->plaintext;
854 in_len = cdat->plaintext_len;
855 out = cdat->ciphertext;
856 out_len = cdat->ciphertext_len;
858 in = cdat->ciphertext;
859 in_len = cdat->ciphertext_len;
860 out = cdat->plaintext;
861 out_len = cdat->plaintext_len;
863 inp_misalign += 16 - ((out_misalign + in_len) & 15);
865 * 'tmp' will store both output and copy of input. We make the copy
866 * of input to specifically aligned part of 'tmp'. So we just
867 * figured out how much padding would ensure the required alignment,
868 * now we allocate extended buffer and finally copy the input just
869 * past inp_misalign in expression below. Output will be written
870 * past out_misalign...
872 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
873 inp_misalign + in_len);
876 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
877 inp_misalign, in, in_len);
878 err = "CIPHERINIT_ERROR";
879 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
881 err = "INVALID_IV_LENGTH";
884 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
887 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
893 * If encrypting or OCB just set tag length initially, otherwise
894 * set tag length and value.
896 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
897 err = "TAG_LENGTH_SET_ERROR";
900 err = "TAG_SET_ERROR";
903 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
904 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
910 err = "INVALID_KEY_LENGTH";
911 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
913 err = "KEY_SET_ERROR";
914 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
917 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
918 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
919 cdat->tag_len, cdat->tag)) {
920 err = "TAG_SET_ERROR";
925 if (cdat->aead == EVP_CIPH_CCM_MODE) {
926 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
927 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
932 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
933 err = "AAD_SET_ERROR";
937 EVP_CIPHER_CTX_set_padding(ctx, 0);
938 err = "CIPHERUPDATE_ERROR";
939 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
941 if (cdat->aead == EVP_CIPH_CCM_MODE)
944 err = "CIPHERFINAL_ERROR";
945 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
948 err = "LENGTH_MISMATCH";
949 if (out_len != (size_t)(tmplen + tmpflen))
951 err = "VALUE_MISMATCH";
952 if (check_output(t, out, tmp + out_misalign, out_len))
954 if (enc && cdat->aead) {
955 unsigned char rtag[16];
956 if (cdat->tag_len > sizeof(rtag)) {
957 err = "TAG_LENGTH_INTERNAL_ERROR";
960 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
961 cdat->tag_len, rtag)) {
962 err = "TAG_RETRIEVE_ERROR";
965 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
966 err = "TAG_VALUE_MISMATCH";
973 EVP_CIPHER_CTX_free(ctx);
978 static int cipher_test_run(struct evp_test *t)
980 struct cipher_data *cdat = t->data;
982 size_t out_misalign, inp_misalign;
988 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
989 /* IV is optional and usually omitted in wrap mode */
990 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
995 if (cdat->aead && !cdat->tag) {
999 for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
1000 static char aux_err[64];
1001 t->aux_err = aux_err;
1002 for (inp_misalign = 0; inp_misalign <= 1; inp_misalign++) {
1003 BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input",
1004 out_misalign ? "misaligned" : "aligned",
1005 inp_misalign ? "misaligned" : "aligned");
1007 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign);
1008 /* Not fatal errors: return */
1015 if (cdat->enc != 1) {
1016 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign);
1017 /* Not fatal errors: return */
1031 static const struct evp_test_method cipher_test_method = {
1034 cipher_test_cleanup,
1042 /* Algorithm string for this MAC */
1048 unsigned char *input;
1050 /* Expected output */
1051 unsigned char *output;
1055 static int mac_test_init(struct evp_test *t, const char *alg)
1058 struct mac_data *mdat;
1059 if (strcmp(alg, "HMAC") == 0) {
1060 type = EVP_PKEY_HMAC;
1061 } else if (strcmp(alg, "CMAC") == 0) {
1062 #ifndef OPENSSL_NO_CMAC
1063 type = EVP_PKEY_CMAC;
1071 mdat = OPENSSL_malloc(sizeof(*mdat));
1076 mdat->output = NULL;
1081 static void mac_test_cleanup(struct evp_test *t)
1083 struct mac_data *mdat = t->data;
1084 test_free(mdat->alg);
1085 test_free(mdat->key);
1086 test_free(mdat->input);
1087 test_free(mdat->output);
1090 static int mac_test_parse(struct evp_test *t,
1091 const char *keyword, const char *value)
1093 struct mac_data *mdata = t->data;
1094 if (strcmp(keyword, "Key") == 0)
1095 return test_bin(value, &mdata->key, &mdata->key_len);
1096 if (strcmp(keyword, "Algorithm") == 0) {
1097 mdata->alg = OPENSSL_strdup(value);
1102 if (strcmp(keyword, "Input") == 0)
1103 return test_bin(value, &mdata->input, &mdata->input_len);
1104 if (strcmp(keyword, "Output") == 0)
1105 return test_bin(value, &mdata->output, &mdata->output_len);
1109 static int mac_test_run(struct evp_test *t)
1111 struct mac_data *mdata = t->data;
1112 const char *err = "INTERNAL_ERROR";
1113 EVP_MD_CTX *mctx = NULL;
1114 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1115 EVP_PKEY *key = NULL;
1116 const EVP_MD *md = NULL;
1117 unsigned char *mac = NULL;
1120 #ifdef OPENSSL_NO_DES
1121 if (strstr(mdata->alg, "DES") != NULL) {
1128 err = "MAC_PKEY_CTX_ERROR";
1129 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1133 err = "MAC_KEYGEN_INIT_ERROR";
1134 if (EVP_PKEY_keygen_init(genctx) <= 0)
1136 if (mdata->type == EVP_PKEY_CMAC) {
1137 err = "MAC_ALGORITHM_SET_ERROR";
1138 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1142 err = "MAC_KEY_SET_ERROR";
1143 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1146 err = "MAC_KEY_GENERATE_ERROR";
1147 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1149 if (mdata->type == EVP_PKEY_HMAC) {
1150 err = "MAC_ALGORITHM_SET_ERROR";
1151 md = EVP_get_digestbyname(mdata->alg);
1155 mctx = EVP_MD_CTX_new();
1158 err = "DIGESTSIGNINIT_ERROR";
1159 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1162 err = "DIGESTSIGNUPDATE_ERROR";
1163 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1165 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1166 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1168 mac = OPENSSL_malloc(mac_len);
1170 fprintf(stderr, "Error allocating mac buffer!\n");
1173 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1175 err = "MAC_LENGTH_MISMATCH";
1176 if (mac_len != mdata->output_len)
1178 err = "MAC_MISMATCH";
1179 if (check_output(t, mdata->output, mac, mac_len))
1183 EVP_MD_CTX_free(mctx);
1185 EVP_PKEY_CTX_free(genctx);
1191 static const struct evp_test_method mac_test_method = {
1200 * Public key operations. These are all very similar and can share
1201 * a lot of common code.
1205 /* Context for this operation */
1207 /* Key operation to perform */
1208 int (*keyop) (EVP_PKEY_CTX *ctx,
1209 unsigned char *sig, size_t *siglen,
1210 const unsigned char *tbs, size_t tbslen);
1212 unsigned char *input;
1214 /* Expected output */
1215 unsigned char *output;
1220 * Perform public key operation setup: lookup key, allocated ctx and call
1221 * the appropriate initialisation function
1223 static int pkey_test_init(struct evp_test *t, const char *name,
1225 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1226 int (*keyop) (EVP_PKEY_CTX *ctx,
1227 unsigned char *sig, size_t *siglen,
1228 const unsigned char *tbs,
1232 struct pkey_data *kdata;
1233 EVP_PKEY *pkey = NULL;
1236 rv = find_key(&pkey, name, t->public);
1238 rv = find_key(&pkey, name, t->private);
1246 kdata = OPENSSL_malloc(sizeof(*kdata));
1248 EVP_PKEY_free(pkey);
1252 kdata->input = NULL;
1253 kdata->output = NULL;
1254 kdata->keyop = keyop;
1256 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1259 if (keyopinit(kdata->ctx) <= 0)
1264 static void pkey_test_cleanup(struct evp_test *t)
1266 struct pkey_data *kdata = t->data;
1268 OPENSSL_free(kdata->input);
1269 OPENSSL_free(kdata->output);
1270 EVP_PKEY_CTX_free(kdata->ctx);
1273 static int pkey_test_ctrl(EVP_PKEY_CTX *pctx, const char *value)
1278 tmpval = OPENSSL_strdup(value);
1281 p = strchr(tmpval, ':');
1284 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1285 OPENSSL_free(tmpval);
1289 static int pkey_test_parse(struct evp_test *t,
1290 const char *keyword, const char *value)
1292 struct pkey_data *kdata = t->data;
1293 if (strcmp(keyword, "Input") == 0)
1294 return test_bin(value, &kdata->input, &kdata->input_len);
1295 if (strcmp(keyword, "Output") == 0)
1296 return test_bin(value, &kdata->output, &kdata->output_len);
1297 if (strcmp(keyword, "Ctrl") == 0)
1298 return pkey_test_ctrl(kdata->ctx, value);
1302 static int pkey_test_run(struct evp_test *t)
1304 struct pkey_data *kdata = t->data;
1305 unsigned char *out = NULL;
1307 const char *err = "KEYOP_LENGTH_ERROR";
1308 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1309 kdata->input_len) <= 0)
1311 out = OPENSSL_malloc(out_len);
1313 fprintf(stderr, "Error allocating output buffer!\n");
1316 err = "KEYOP_ERROR";
1318 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1320 err = "KEYOP_LENGTH_MISMATCH";
1321 if (out_len != kdata->output_len)
1323 err = "KEYOP_MISMATCH";
1324 if (check_output(t, kdata->output, out, out_len))
1333 static int sign_test_init(struct evp_test *t, const char *name)
1335 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1338 static const struct evp_test_method psign_test_method = {
1346 static int verify_recover_test_init(struct evp_test *t, const char *name)
1348 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1349 EVP_PKEY_verify_recover);
1352 static const struct evp_test_method pverify_recover_test_method = {
1354 verify_recover_test_init,
1360 static int decrypt_test_init(struct evp_test *t, const char *name)
1362 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1366 static const struct evp_test_method pdecrypt_test_method = {
1374 static int verify_test_init(struct evp_test *t, const char *name)
1376 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1379 static int verify_test_run(struct evp_test *t)
1381 struct pkey_data *kdata = t->data;
1382 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1383 kdata->input, kdata->input_len) <= 0)
1384 t->err = "VERIFY_ERROR";
1388 static const struct evp_test_method pverify_test_method = {
1397 static int pderive_test_init(struct evp_test *t, const char *name)
1399 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1402 static int pderive_test_parse(struct evp_test *t,
1403 const char *keyword, const char *value)
1405 struct pkey_data *kdata = t->data;
1407 if (strcmp(keyword, "PeerKey") == 0) {
1409 if (find_key(&peer, value, t->public) == 0)
1411 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1415 if (strcmp(keyword, "SharedSecret") == 0)
1416 return test_bin(value, &kdata->output, &kdata->output_len);
1417 if (strcmp(keyword, "Ctrl") == 0)
1418 return pkey_test_ctrl(kdata->ctx, value);
1422 static int pderive_test_run(struct evp_test *t)
1424 struct pkey_data *kdata = t->data;
1425 unsigned char *out = NULL;
1427 const char *err = "INTERNAL_ERROR";
1429 out_len = kdata->output_len;
1430 out = OPENSSL_malloc(out_len);
1432 fprintf(stderr, "Error allocating output buffer!\n");
1435 err = "DERIVE_ERROR";
1436 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1438 err = "SHARED_SECRET_LENGTH_MISMATCH";
1439 if (out_len != kdata->output_len)
1441 err = "SHARED_SECRET_MISMATCH";
1442 if (check_output(t, kdata->output, out, out_len))
1451 static const struct evp_test_method pderive_test_method = {
1461 #define PBE_TYPE_SCRYPT 1
1462 #define PBE_TYPE_PBKDF2 2
1463 #define PBE_TYPE_PKCS12 3
1469 /* scrypt parameters */
1470 uint64_t N, r, p, maxmem;
1472 /* PKCS#12 parameters */
1477 unsigned char *pass;
1481 unsigned char *salt;
1484 /* Expected output */
1489 #ifndef OPENSSL_NO_SCRYPT
1490 static int scrypt_test_parse(struct evp_test *t,
1491 const char *keyword, const char *value)
1493 struct pbe_data *pdata = t->data;
1495 if (strcmp(keyword, "N") == 0)
1496 return test_uint64(value, &pdata->N);
1497 if (strcmp(keyword, "p") == 0)
1498 return test_uint64(value, &pdata->p);
1499 if (strcmp(keyword, "r") == 0)
1500 return test_uint64(value, &pdata->r);
1501 if (strcmp(keyword, "maxmem") == 0)
1502 return test_uint64(value, &pdata->maxmem);
1507 static int pbkdf2_test_parse(struct evp_test *t,
1508 const char *keyword, const char *value)
1510 struct pbe_data *pdata = t->data;
1512 if (strcmp(keyword, "iter") == 0) {
1513 pdata->iter = atoi(value);
1514 if (pdata->iter <= 0)
1518 if (strcmp(keyword, "MD") == 0) {
1519 pdata->md = EVP_get_digestbyname(value);
1520 if (pdata->md == NULL)
1527 static int pkcs12_test_parse(struct evp_test *t,
1528 const char *keyword, const char *value)
1530 struct pbe_data *pdata = t->data;
1532 if (strcmp(keyword, "id") == 0) {
1533 pdata->id = atoi(value);
1538 return pbkdf2_test_parse(t, keyword, value);
1541 static int pbe_test_init(struct evp_test *t, const char *alg)
1543 struct pbe_data *pdat;
1546 if (strcmp(alg, "scrypt") == 0) {
1547 #ifndef OPENSSL_NO_SCRYPT
1548 pbe_type = PBE_TYPE_SCRYPT;
1553 } else if (strcmp(alg, "pbkdf2") == 0) {
1554 pbe_type = PBE_TYPE_PBKDF2;
1555 } else if (strcmp(alg, "pkcs12") == 0) {
1556 pbe_type = PBE_TYPE_PKCS12;
1558 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1560 pdat = OPENSSL_malloc(sizeof(*pdat));
1561 pdat->pbe_type = pbe_type;
1575 static void pbe_test_cleanup(struct evp_test *t)
1577 struct pbe_data *pdat = t->data;
1578 test_free(pdat->pass);
1579 test_free(pdat->salt);
1580 test_free(pdat->key);
1583 static int pbe_test_parse(struct evp_test *t,
1584 const char *keyword, const char *value)
1586 struct pbe_data *pdata = t->data;
1588 if (strcmp(keyword, "Password") == 0)
1589 return test_bin(value, &pdata->pass, &pdata->pass_len);
1590 if (strcmp(keyword, "Salt") == 0)
1591 return test_bin(value, &pdata->salt, &pdata->salt_len);
1592 if (strcmp(keyword, "Key") == 0)
1593 return test_bin(value, &pdata->key, &pdata->key_len);
1594 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1595 return pbkdf2_test_parse(t, keyword, value);
1596 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1597 return pkcs12_test_parse(t, keyword, value);
1598 #ifndef OPENSSL_NO_SCRYPT
1599 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1600 return scrypt_test_parse(t, keyword, value);
1605 static int pbe_test_run(struct evp_test *t)
1607 struct pbe_data *pdata = t->data;
1608 const char *err = "INTERNAL_ERROR";
1611 key = OPENSSL_malloc(pdata->key_len);
1614 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1615 err = "PBKDF2_ERROR";
1616 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1617 pdata->salt, pdata->salt_len,
1618 pdata->iter, pdata->md,
1619 pdata->key_len, key) == 0)
1621 #ifndef OPENSSL_NO_SCRYPT
1622 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1623 err = "SCRYPT_ERROR";
1624 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1625 pdata->salt, pdata->salt_len,
1626 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1627 key, pdata->key_len) == 0)
1630 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1631 err = "PKCS12_ERROR";
1632 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1633 pdata->salt, pdata->salt_len,
1634 pdata->id, pdata->iter, pdata->key_len,
1635 key, pdata->md) == 0)
1638 err = "KEY_MISMATCH";
1639 if (check_output(t, pdata->key, key, pdata->key_len))
1648 static const struct evp_test_method pbe_test_method = {
1659 BASE64_CANONICAL_ENCODING = 0,
1660 BASE64_VALID_ENCODING = 1,
1661 BASE64_INVALID_ENCODING = 2
1662 } base64_encoding_type;
1664 struct encode_data {
1665 /* Input to encoding */
1666 unsigned char *input;
1668 /* Expected output */
1669 unsigned char *output;
1671 base64_encoding_type encoding;
1674 static int encode_test_init(struct evp_test *t, const char *encoding)
1676 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1678 if (strcmp(encoding, "canonical") == 0) {
1679 edata->encoding = BASE64_CANONICAL_ENCODING;
1680 } else if (strcmp(encoding, "valid") == 0) {
1681 edata->encoding = BASE64_VALID_ENCODING;
1682 } else if (strcmp(encoding, "invalid") == 0) {
1683 edata->encoding = BASE64_INVALID_ENCODING;
1684 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1685 if (t->expected_err == NULL)
1688 fprintf(stderr, "Bad encoding: %s. Should be one of "
1689 "{canonical, valid, invalid}\n", encoding);
1696 static void encode_test_cleanup(struct evp_test *t)
1698 struct encode_data *edata = t->data;
1699 test_free(edata->input);
1700 test_free(edata->output);
1701 memset(edata, 0, sizeof(*edata));
1704 static int encode_test_parse(struct evp_test *t,
1705 const char *keyword, const char *value)
1707 struct encode_data *edata = t->data;
1708 if (strcmp(keyword, "Input") == 0)
1709 return test_bin(value, &edata->input, &edata->input_len);
1710 if (strcmp(keyword, "Output") == 0)
1711 return test_bin(value, &edata->output, &edata->output_len);
1715 static int encode_test_run(struct evp_test *t)
1717 struct encode_data *edata = t->data;
1718 unsigned char *encode_out = NULL, *decode_out = NULL;
1719 int output_len, chunk_len;
1720 const char *err = "INTERNAL_ERROR";
1721 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1723 if (decode_ctx == NULL)
1726 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1727 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1728 if (encode_ctx == NULL)
1730 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1731 if (encode_out == NULL)
1734 EVP_EncodeInit(encode_ctx);
1735 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1736 edata->input, edata->input_len);
1737 output_len = chunk_len;
1739 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1740 output_len += chunk_len;
1742 EVP_ENCODE_CTX_free(encode_ctx);
1744 if (check_var_length_output(t, edata->output, edata->output_len,
1745 encode_out, output_len)) {
1746 err = "BAD_ENCODING";
1751 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1752 if (decode_out == NULL)
1755 EVP_DecodeInit(decode_ctx);
1756 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1757 edata->output_len) < 0) {
1758 err = "DECODE_ERROR";
1761 output_len = chunk_len;
1763 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1764 err = "DECODE_ERROR";
1767 output_len += chunk_len;
1769 if (edata->encoding != BASE64_INVALID_ENCODING &&
1770 check_var_length_output(t, edata->input, edata->input_len,
1771 decode_out, output_len)) {
1772 err = "BAD_DECODING";
1779 OPENSSL_free(encode_out);
1780 OPENSSL_free(decode_out);
1781 EVP_ENCODE_CTX_free(decode_ctx);
1785 static const struct evp_test_method encode_test_method = {
1788 encode_test_cleanup,
1793 /* KDF operations */
1796 /* Context for this operation */
1798 /* Expected output */
1799 unsigned char *output;
1804 * Perform public key operation setup: lookup key, allocated ctx and call
1805 * the appropriate initialisation function
1807 static int kdf_test_init(struct evp_test *t, const char *name)
1809 struct kdf_data *kdata;
1811 kdata = OPENSSL_malloc(sizeof(*kdata));
1815 kdata->output = NULL;
1817 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1818 if (kdata->ctx == NULL)
1820 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1825 static void kdf_test_cleanup(struct evp_test *t)
1827 struct kdf_data *kdata = t->data;
1828 OPENSSL_free(kdata->output);
1829 EVP_PKEY_CTX_free(kdata->ctx);
1832 static int kdf_test_parse(struct evp_test *t,
1833 const char *keyword, const char *value)
1835 struct kdf_data *kdata = t->data;
1836 if (strcmp(keyword, "Output") == 0)
1837 return test_bin(value, &kdata->output, &kdata->output_len);
1838 if (strncmp(keyword, "Ctrl", 4) == 0)
1839 return pkey_test_ctrl(kdata->ctx, value);
1843 static int kdf_test_run(struct evp_test *t)
1845 struct kdf_data *kdata = t->data;
1846 unsigned char *out = NULL;
1847 size_t out_len = kdata->output_len;
1848 const char *err = "INTERNAL_ERROR";
1849 out = OPENSSL_malloc(out_len);
1851 fprintf(stderr, "Error allocating output buffer!\n");
1854 err = "KDF_DERIVE_ERROR";
1855 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1857 err = "KDF_LENGTH_MISMATCH";
1858 if (out_len != kdata->output_len)
1860 err = "KDF_MISMATCH";
1861 if (check_output(t, kdata->output, out, out_len))
1870 static const struct evp_test_method kdf_test_method = {