2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pkcs12.h>
19 #include <openssl/kdf.h>
20 #include "internal/numbers.h"
24 static const char *current_test_file = "???";
27 * Remove spaces from beginning and end of a string
29 static void remove_space(char **pval)
31 unsigned char *p = (unsigned char *)*pval, *beginning;
36 *pval = (char *)(beginning = p);
38 p = p + strlen(*pval) - 1;
40 /* Remove trailing space */
41 while (p >= beginning && isspace(*p))
46 * Given a line of the form:
47 * name = value # comment
48 * extract name and value. NB: modifies |linebuf|.
50 static int parse_line(char **pkw, char **pval, char *linebuf)
52 char *p = linebuf + strlen(linebuf) - 1;
55 TEST_error("FATAL: missing EOL");
60 p = strchr(linebuf, '#');
65 if ((p = strchr(linebuf, '=')) == NULL)
77 * Unescape some escape sequences in string literals.
78 * Return the result in a newly allocated buffer.
79 * Currently only supports '\n'.
80 * If the input length is 0, returns a valid 1-byte buffer, but sets
83 static unsigned char* unescape(const char *input, size_t input_len,
86 unsigned char *ret, *p;
91 return OPENSSL_zalloc(1);
94 /* Escaping is non-expanding; over-allocate original size for simplicity. */
95 ret = p = OPENSSL_malloc(input_len);
99 for (i = 0; i < input_len; i++) {
100 if (input[i] == '\\') {
101 if (i == input_len - 1 || input[i+1] != 'n')
118 /* For a hex string "value" convert to a binary allocated buffer */
119 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
125 /* Check for empty value */
128 * Don't return NULL for zero length buffer.
129 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
130 * a non-NULL key buffer even if the key length is 0, in order to detect
133 *buf = OPENSSL_malloc(1);
141 /* Check for NULL literal */
142 if (strcmp(value, "NULL") == 0) {
148 /* Check for string literal */
149 if (value[0] == '"') {
152 vlen = strlen(value);
153 if (value[vlen - 1] != '"')
156 *buf = unescape(value, vlen, buflen);
162 /* Otherwise assume as hex literal and convert it to binary buffer */
163 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
164 TEST_info("Cannot convert %s", value);
165 ERR_print_errors(bio_err);
168 /* Size of input buffer means we'll never overflow */
174 * Structure used to hold a list of blocks of memory to test
175 * calls to "update" like functions.
178 struct evp_test_buffer_st {
185 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
188 OPENSSL_free(db->buf);
193 /* append buffer to a list */
195 static int evp_test_buffer_append(const char *value,
196 STACK_OF(EVP_TEST_BUFFER) **sk)
198 EVP_TEST_BUFFER *db = NULL;
200 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
203 if (!test_bin(value, &db->buf, &db->buflen))
208 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
211 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
217 evp_test_buffer_free(db);
223 * replace last buffer in list with copies of itself
225 static int evp_test_buffer_ncopy(const char *value,
226 STACK_OF(EVP_TEST_BUFFER) *sk)
229 unsigned char *tbuf, *p;
231 int ncopy = atoi(value);
236 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
238 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
240 tbuflen = db->buflen * ncopy;
241 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
243 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
244 memcpy(p, db->buf, db->buflen);
246 OPENSSL_free(db->buf);
248 db->buflen = tbuflen;
252 /* set repeat count for last buffer in list */
253 static int evp_test_buffer_set_count(const char *value,
254 STACK_OF(EVP_TEST_BUFFER) *sk)
257 int count = atoi(value);
262 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
265 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
269 db->count = (size_t)count;
275 * call "fn" with each element of the list in turn
277 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
279 const unsigned char *buf,
285 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
286 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
289 for (j = 0; j < tb->count; j++) {
290 if (fn(ctx, tb->buf, tb->buflen) <= 0)
297 #ifndef OPENSSL_NO_SCRYPT
298 /* Currently only used by scrypt tests */
299 /* Parse unsigned decimal 64 bit integer value */
300 static int test_uint64(const char *value, uint64_t *pr)
302 const char *p = value;
304 if (!TEST_true(*p)) {
305 TEST_info("Invalid empty integer value");
310 if (*pr > UINT64_MAX / 10) {
311 TEST_error("Integer overflow in string %s", value);
315 if (!TEST_true(isdigit(*p))) {
316 TEST_error("Invalid character in string %s", value);
325 static int compare_mem(unsigned char *expected, size_t expected_len,
326 unsigned char *got, size_t got_len)
328 if (!TEST_mem_eq(expected, expected_len, got, got_len))
334 typedef struct evp_test_method_st EVP_TEST_METHOD;
336 /* Structure holding test information */
337 typedef struct evp_test_st {
338 /* file being read */
340 /* temp memory BIO for reading in keys */
342 /* method for this test */
343 const EVP_TEST_METHOD *meth;
344 /* current line being processed */
346 /* start line of current test */
347 unsigned int start_line;
348 /* Error string for test */
349 const char *err, *aux_err;
350 /* Expected error value of test */
352 /* Expected error function string */
354 /* Expected error reason string */
356 /* Number of tests */
360 /* Number of tests skipped */
362 /* test specific data */
364 /* Current test should be skipped */
369 * Linked list of named keys.
371 typedef struct key_list_st {
374 struct key_list_st *next;
377 /* List of public and private keys */
378 static KEY_LIST *private_keys;
379 static KEY_LIST *public_keys;
382 * Test method structure
384 struct evp_test_method_st {
385 /* Name of test as it appears in file */
387 /* Initialise test for "alg" */
388 int (*init) (EVP_TEST * t, const char *alg);
389 /* Clean up method */
390 void (*cleanup) (EVP_TEST * t);
391 /* Test specific name value pair processing */
392 int (*parse) (EVP_TEST * t, const char *name, const char *value);
393 /* Run the test itself */
394 int (*run_test) (EVP_TEST * t);
397 static const EVP_TEST_METHOD digest_test_method, cipher_test_method;
398 static const EVP_TEST_METHOD mac_test_method;
399 static const EVP_TEST_METHOD psign_test_method, pverify_test_method;
400 static const EVP_TEST_METHOD pdecrypt_test_method;
401 static const EVP_TEST_METHOD pverify_recover_test_method;
402 static const EVP_TEST_METHOD pderive_test_method;
403 static const EVP_TEST_METHOD pbe_test_method;
404 static const EVP_TEST_METHOD encode_test_method;
405 static const EVP_TEST_METHOD kdf_test_method;
406 static const EVP_TEST_METHOD keypair_test_method;
408 static const EVP_TEST_METHOD *evp_test_list[] = {
413 &pverify_test_method,
414 &pdecrypt_test_method,
415 &pverify_recover_test_method,
416 &pderive_test_method,
420 &keypair_test_method,
424 static const EVP_TEST_METHOD *evp_find_test(const char *name)
426 const EVP_TEST_METHOD **tt;
428 for (tt = evp_test_list; *tt; tt++) {
429 if (strcmp(name, (*tt)->name) == 0)
435 static void clear_test(EVP_TEST *t)
437 OPENSSL_free(t->expected_err);
438 t->expected_err = NULL;
439 OPENSSL_free(t->func);
441 OPENSSL_free(t->reason);
448 * Check for errors in the test structure; return 1 if okay, else 0.
450 static int check_test_error(EVP_TEST *t)
456 if (t->err == NULL && t->expected_err == NULL)
458 if (t->err != NULL && t->expected_err == NULL) {
459 if (t->aux_err != NULL) {
460 TEST_info("Above error from the test at %s:%d "
461 "(%s) unexpected error %s",
462 current_test_file, t->start_line, t->aux_err, t->err);
464 TEST_info("Above error from the test at %s:%d "
465 "unexpected error %s",
466 current_test_file, t->start_line, t->err);
471 if (t->err == NULL && t->expected_err != NULL) {
472 TEST_info("Test line %d: succeeded expecting %s",
473 t->start_line, t->expected_err);
477 if (strcmp(t->err, t->expected_err) != 0) {
478 TEST_info("Test line %d: expecting %s got %s",
479 t->start_line, t->expected_err, t->err);
483 if (t->func == NULL && t->reason == NULL)
486 if (t->func == NULL || t->reason == NULL) {
487 TEST_info("Test line %d: missing function or reason code",
492 err = ERR_peek_error();
494 TEST_info("Test line %d, expected error \"%s:%s\" not set",
495 t->start_line, t->func, t->reason);
499 func = ERR_func_error_string(err);
500 reason = ERR_reason_error_string(err);
501 if (func == NULL && reason == NULL) {
502 TEST_info("Test line %d: expected error \"%s:%s\","
503 " no strings available. Skipping...\n",
504 t->start_line, t->func, t->reason);
508 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
511 TEST_info("Test line %d: expected error \"%s:%s\", got \"%s:%s\"",
512 t->start_line, t->func, t->reason, func, reason);
518 * Setup a new test, run any existing test. Log a message and return 0
521 static int run_and_get_next(EVP_TEST *t, const EVP_TEST_METHOD *tmeth)
523 /* If we already have a test set up run it */
527 /*TEST_info("Line %d skipped %s test", t->start_line, t->meth->name);
532 if (t->err == NULL && t->meth->run_test(t) != 1) {
533 TEST_info("Line %d error %s", t->start_line, t->meth->name);
536 if (!check_test_error(t)) {
537 test_openssl_errors();
543 if (t->data != NULL) {
545 OPENSSL_free(t->data);
554 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
556 for (; lst; lst = lst->next) {
557 if (strcmp(lst->name, name) == 0) {
566 static void free_key_list(KEY_LIST *lst)
568 while (lst != NULL) {
571 EVP_PKEY_free(lst->key);
572 OPENSSL_free(lst->name);
579 static int check_unsupported()
581 long err = ERR_peek_error();
583 if (ERR_GET_LIB(err) == ERR_LIB_EVP
584 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
588 #ifndef OPENSSL_NO_EC
590 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
591 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
594 if (ERR_GET_LIB(err) == ERR_LIB_EC
595 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
599 #endif /* OPENSSL_NO_EC */
604 static int read_key(EVP_TEST *t)
608 if (t->key == NULL) {
609 if (!TEST_ptr(t->key = BIO_new(BIO_s_mem())))
611 } else if (!TEST_int_gt(BIO_reset(t->key), 0)) {
615 /* Read to PEM end line and place content in memory BIO */
616 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
618 if (!TEST_int_gt(BIO_puts(t->key, tmpbuf), 0))
620 if (strncmp(tmpbuf, "-----END", 8) == 0)
623 TEST_error("Can't find key end");
628 * Parse a line into the current test |t|. Return 0 on error.
630 static int parse_test_line(EVP_TEST *t, char *buf)
632 char *keyword = NULL, *value = NULL;
634 KEY_LIST **lst = NULL, *key = NULL;
636 const EVP_TEST_METHOD *tmeth = NULL;
638 if (!parse_line(&keyword, &value, buf))
640 if (strcmp(keyword, "PrivateKey") == 0) {
643 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
644 if (pk == NULL && !check_unsupported()) {
645 TEST_info("Error reading private key %s", value);
646 ERR_print_errors_fp(stderr);
652 if (strcmp(keyword, "PublicKey") == 0) {
655 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
656 if (pk == NULL && !check_unsupported()) {
657 TEST_info("Error reading public key %s", value);
658 ERR_print_errors_fp(stderr);
664 /* If we have a key add to list */
666 if (find_key(NULL, value, *lst)) {
667 TEST_info("Duplicate key %s", value);
670 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))
671 || !TEST_ptr(key->name = OPENSSL_strdup(value)))
679 /* See if keyword corresponds to a test start */
680 if ((tmeth = evp_find_test(keyword)) != NULL) {
681 if (!run_and_get_next(t, tmeth))
683 t->start_line = t->line;
685 if (!tmeth->init(t, value)) {
686 TEST_info("Unknown %s: %s", keyword, value);
693 if (strcmp(keyword, "Title") == 0) {
694 TEST_info("Starting %s tests", value);
695 set_test_title(value);
696 } else if (strcmp(keyword, "Result") == 0) {
697 if (t->expected_err != NULL) {
698 TEST_info("Line %d: multiple result lines", t->line);
701 if (!TEST_ptr(t->expected_err = OPENSSL_strdup(value)))
703 } else if (strcmp(keyword, "Function") == 0) {
704 if (t->func != NULL) {
705 TEST_info("Line %d: multiple function lines\n", t->line);
708 if (!TEST_ptr(t->func = OPENSSL_strdup(value)))
710 } else if (strcmp(keyword, "Reason") == 0) {
711 if (t->reason != NULL) {
712 TEST_info("Line %d: multiple reason lines", t->line);
715 if (!TEST_ptr(t->reason = OPENSSL_strdup(value)))
718 /* Must be test specific line: try to parse it */
719 int rv = t->meth == NULL ? 0 : t->meth->parse(t, keyword, value);
722 TEST_info("Line %d: unknown keyword %s", t->line, keyword);
726 TEST_info("Line %d: error processing keyword %s\n",
734 /* Message digest tests */
736 typedef struct digest_data_st {
737 /* Digest this test is for */
738 const EVP_MD *digest;
739 /* Input to digest */
740 STACK_OF(EVP_TEST_BUFFER) *input;
741 /* Expected output */
742 unsigned char *output;
746 static int digest_test_init(EVP_TEST *t, const char *alg)
748 const EVP_MD *digest;
751 digest = EVP_get_digestbyname(alg);
753 /* If alg has an OID assume disabled algorithm */
754 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
760 mdat = OPENSSL_zalloc(sizeof(*mdat));
761 mdat->digest = digest;
766 static void digest_test_cleanup(EVP_TEST *t)
768 DIGEST_DATA *mdat = t->data;
770 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
771 OPENSSL_free(mdat->output);
774 static int digest_test_parse(EVP_TEST *t,
775 const char *keyword, const char *value)
777 DIGEST_DATA *mdata = t->data;
779 if (strcmp(keyword, "Input") == 0)
780 return evp_test_buffer_append(value, &mdata->input);
781 if (strcmp(keyword, "Output") == 0)
782 return test_bin(value, &mdata->output, &mdata->output_len);
783 if (strcmp(keyword, "Count") == 0)
784 return evp_test_buffer_set_count(value, mdata->input);
785 if (strcmp(keyword, "Ncopy") == 0)
786 return evp_test_buffer_ncopy(value, mdata->input);
790 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
792 return EVP_DigestUpdate(ctx, buf, buflen);
795 static int digest_test_run(EVP_TEST *t)
797 DIGEST_DATA *mdata = t->data;
799 unsigned char md[EVP_MAX_MD_SIZE];
802 t->err = "TEST_FAILURE";
803 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
806 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL)) {
807 t->err = "DIGESTINIT_ERROR";
810 if (!evp_test_buffer_do(mdata->input, digest_update_fn, mctx)) {
811 t->err = "DIGESTUPDATE_ERROR";
815 if (!EVP_DigestFinal(mctx, md, &md_len)) {
816 t->err = "DIGESTFINAL_ERROR";
819 if (md_len != mdata->output_len) {
820 t->err = "DIGEST_LENGTH_MISMATCH";
823 if (!compare_mem(mdata->output, mdata->output_len, md, md_len)) {
824 t->err = "DIGEST_MISMATCH";
830 EVP_MD_CTX_free(mctx);
834 static const EVP_TEST_METHOD digest_test_method = {
843 typedef struct cipher_data_st {
844 const EVP_CIPHER *cipher;
846 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
852 unsigned char *plaintext;
853 size_t plaintext_len;
854 unsigned char *ciphertext;
855 size_t ciphertext_len;
863 static int cipher_test_init(EVP_TEST *t, const char *alg)
865 const EVP_CIPHER *cipher;
866 CIPHER_DATA *cdat = t->data;
868 cipher = EVP_get_cipherbyname(alg);
870 /* If alg has an OID assume disabled algorithm */
871 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
877 cdat = OPENSSL_malloc(sizeof(*cdat));
878 cdat->cipher = cipher;
882 cdat->ciphertext = NULL;
883 cdat->plaintext = NULL;
887 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
888 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
889 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
890 cdat->aead = EVP_CIPHER_mode(cipher);
891 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
899 static void cipher_test_cleanup(EVP_TEST *t)
901 CIPHER_DATA *cdat = t->data;
903 OPENSSL_free(cdat->key);
904 OPENSSL_free(cdat->iv);
905 OPENSSL_free(cdat->ciphertext);
906 OPENSSL_free(cdat->plaintext);
907 OPENSSL_free(cdat->aad);
908 OPENSSL_free(cdat->tag);
911 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
914 CIPHER_DATA *cdat = t->data;
916 if (strcmp(keyword, "Key") == 0)
917 return test_bin(value, &cdat->key, &cdat->key_len);
918 if (strcmp(keyword, "IV") == 0)
919 return test_bin(value, &cdat->iv, &cdat->iv_len);
920 if (strcmp(keyword, "Plaintext") == 0)
921 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
922 if (strcmp(keyword, "Ciphertext") == 0)
923 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
925 if (strcmp(keyword, "AAD") == 0)
926 return test_bin(value, &cdat->aad, &cdat->aad_len);
927 if (strcmp(keyword, "Tag") == 0)
928 return test_bin(value, &cdat->tag, &cdat->tag_len);
931 if (strcmp(keyword, "Operation") == 0) {
932 if (strcmp(value, "ENCRYPT") == 0)
934 else if (strcmp(value, "DECRYPT") == 0)
943 static int cipher_test_enc(EVP_TEST *t, int enc,
944 size_t out_misalign, size_t inp_misalign, int frag)
946 CIPHER_DATA *cdat = t->data;
947 unsigned char *in, *out, *tmp = NULL;
948 size_t in_len, out_len, donelen = 0;
949 int ok = 0, tmplen, chunklen, tmpflen;
950 EVP_CIPHER_CTX *ctx = NULL;
952 t->err = "TEST_FAILURE";
953 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
955 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
957 in = cdat->plaintext;
958 in_len = cdat->plaintext_len;
959 out = cdat->ciphertext;
960 out_len = cdat->ciphertext_len;
962 in = cdat->ciphertext;
963 in_len = cdat->ciphertext_len;
964 out = cdat->plaintext;
965 out_len = cdat->plaintext_len;
967 if (inp_misalign == (size_t)-1) {
969 * Exercise in-place encryption
971 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
974 in = memcpy(tmp + out_misalign, in, in_len);
976 inp_misalign += 16 - ((out_misalign + in_len) & 15);
978 * 'tmp' will store both output and copy of input. We make the copy
979 * of input to specifically aligned part of 'tmp'. So we just
980 * figured out how much padding would ensure the required alignment,
981 * now we allocate extended buffer and finally copy the input just
982 * past inp_misalign in expression below. Output will be written
983 * past out_misalign...
985 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
986 inp_misalign + in_len);
989 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
990 inp_misalign, in, in_len);
992 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc)) {
993 t->err = "CIPHERINIT_ERROR";
998 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
1000 t->err = "INVALID_IV_LENGTH";
1003 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
1004 t->err = "INVALID_IV_LENGTH";
1011 * If encrypting or OCB just set tag length initially, otherwise
1012 * set tag length and value.
1014 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
1015 t->err = "TAG_LENGTH_SET_ERROR";
1018 t->err = "TAG_SET_ERROR";
1021 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
1022 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
1023 cdat->tag_len, tag))
1028 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len)) {
1029 t->err = "INVALID_KEY_LENGTH";
1032 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1)) {
1033 t->err = "KEY_SET_ERROR";
1037 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
1038 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
1039 cdat->tag_len, cdat->tag)) {
1040 t->err = "TAG_SET_ERROR";
1045 if (cdat->aead == EVP_CIPH_CCM_MODE) {
1046 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
1047 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
1052 t->err = "AAD_SET_ERROR";
1054 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
1059 * Supply the AAD in chunks less than the block size where possible
1061 if (cdat->aad_len > 0) {
1062 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1066 if (cdat->aad_len > 2) {
1067 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1070 donelen += cdat->aad_len - 2;
1072 if (cdat->aad_len > 1
1073 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1074 cdat->aad + donelen, 1))
1078 EVP_CIPHER_CTX_set_padding(ctx, 0);
1079 t->err = "CIPHERUPDATE_ERROR";
1082 /* We supply the data all in one go */
1083 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1086 /* Supply the data in chunks less than the block size where possible */
1088 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1095 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1103 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1109 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
1110 t->err = "CIPHERFINAL_ERROR";
1113 if (!compare_mem(out, out_len, tmp + out_misalign, tmplen + tmpflen)) {
1114 t->err = "VALUE_MISMATCH";
1117 if (enc && cdat->aead) {
1118 unsigned char rtag[16];
1120 if (cdat->tag_len > sizeof(rtag)) {
1121 t->err = "TAG_LENGTH_INTERNAL_ERROR";
1124 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1125 cdat->tag_len, rtag)) {
1126 t->err = "TAG_RETRIEVE_ERROR";
1129 if (!compare_mem(cdat->tag, cdat->tag_len, rtag, cdat->tag_len)) {
1130 t->err = "TAG_VALUE_MISMATCH";
1138 EVP_CIPHER_CTX_free(ctx);
1142 static int cipher_test_run(EVP_TEST *t)
1144 CIPHER_DATA *cdat = t->data;
1146 size_t out_misalign, inp_misalign;
1152 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1153 /* IV is optional and usually omitted in wrap mode */
1154 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1159 if (cdat->aead && !cdat->tag) {
1163 for (out_misalign = 0; out_misalign <= 1;) {
1164 static char aux_err[64];
1165 t->aux_err = aux_err;
1166 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1167 if (inp_misalign == (size_t)-1) {
1168 /* kludge: inp_misalign == -1 means "exercise in-place" */
1169 BIO_snprintf(aux_err, sizeof(aux_err),
1170 "%s in-place, %sfragmented",
1171 out_misalign ? "misaligned" : "aligned",
1172 frag ? "" : "not ");
1174 BIO_snprintf(aux_err, sizeof(aux_err),
1175 "%s output and %s input, %sfragmented",
1176 out_misalign ? "misaligned" : "aligned",
1177 inp_misalign ? "misaligned" : "aligned",
1178 frag ? "" : "not ");
1181 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1182 /* Not fatal errors: return */
1189 if (cdat->enc != 1) {
1190 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1191 /* Not fatal errors: return */
1200 if (out_misalign == 1 && frag == 0) {
1202 * XTS, CCM and Wrap modes have special requirements about input
1203 * lengths so we don't fragment for those
1205 if (cdat->aead == EVP_CIPH_CCM_MODE
1206 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1207 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1220 static const EVP_TEST_METHOD cipher_test_method = {
1223 cipher_test_cleanup,
1228 typedef struct mac_data_st {
1231 /* Algorithm string for this MAC */
1237 unsigned char *input;
1239 /* Expected output */
1240 unsigned char *output;
1244 static int mac_test_init(EVP_TEST *t, const char *alg)
1249 if (strcmp(alg, "HMAC") == 0) {
1250 type = EVP_PKEY_HMAC;
1251 } else if (strcmp(alg, "CMAC") == 0) {
1252 #ifndef OPENSSL_NO_CMAC
1253 type = EVP_PKEY_CMAC;
1258 } else if (strcmp(alg, "Poly1305") == 0) {
1259 #ifndef OPENSSL_NO_POLY1305
1260 type = EVP_PKEY_POLY1305;
1265 } else if (strcmp(alg, "SipHash") == 0) {
1266 #ifndef OPENSSL_NO_SIPHASH
1267 type = EVP_PKEY_SIPHASH;
1275 mdat = OPENSSL_zalloc(sizeof(*mdat));
1281 static void mac_test_cleanup(EVP_TEST *t)
1283 MAC_DATA *mdat = t->data;
1285 OPENSSL_free(mdat->alg);
1286 OPENSSL_free(mdat->key);
1287 OPENSSL_free(mdat->input);
1288 OPENSSL_free(mdat->output);
1291 static int mac_test_parse(EVP_TEST *t,
1292 const char *keyword, const char *value)
1294 MAC_DATA *mdata = t->data;
1296 if (strcmp(keyword, "Key") == 0)
1297 return test_bin(value, &mdata->key, &mdata->key_len);
1298 if (strcmp(keyword, "Algorithm") == 0) {
1299 mdata->alg = OPENSSL_strdup(value);
1304 if (strcmp(keyword, "Input") == 0)
1305 return test_bin(value, &mdata->input, &mdata->input_len);
1306 if (strcmp(keyword, "Output") == 0)
1307 return test_bin(value, &mdata->output, &mdata->output_len);
1311 static int mac_test_run(EVP_TEST *t)
1313 MAC_DATA *mdata = t->data;
1314 EVP_MD_CTX *mctx = NULL;
1315 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1316 EVP_PKEY *key = NULL;
1317 const EVP_MD *md = NULL;
1318 unsigned char *mac = NULL;
1321 #ifdef OPENSSL_NO_DES
1322 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1329 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL))) {
1330 t->err = "MAC_PKEY_CTX_ERROR";
1334 if (EVP_PKEY_keygen_init(genctx) <= 0) {
1335 t->err = "MAC_KEYGEN_INIT_ERROR";
1338 if (mdata->type == EVP_PKEY_CMAC
1339 && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0) {
1340 t->err = "MAC_ALGORITHM_SET_ERROR";
1344 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0) {
1345 t->err = "MAC_KEY_SET_ERROR";
1349 if (EVP_PKEY_keygen(genctx, &key) <= 0) {
1350 t->err = "MAC_KEY_GENERATE_ERROR";
1353 if (mdata->type == EVP_PKEY_HMAC) {
1354 if (!TEST_ptr(md = EVP_get_digestbyname(mdata->alg))) {
1355 t->err = "MAC_ALGORITHM_SET_ERROR";
1359 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1360 t->err = "INTERNAL_ERROR";
1363 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1364 t->err = "DIGESTSIGNINIT_ERROR";
1368 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len)) {
1369 t->err = "DIGESTSIGNUPDATE_ERROR";
1372 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len)) {
1373 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1376 if (!TEST_ptr(mac = OPENSSL_malloc(mac_len))) {
1377 t->err = "TEST_FAILURE";
1380 if (!EVP_DigestSignFinal(mctx, mac, &mac_len)
1381 || !compare_mem(mdata->output, mdata->output_len, mac, mac_len)) {
1382 t->err = "TEST_MAC_ERR";
1387 EVP_MD_CTX_free(mctx);
1389 EVP_PKEY_CTX_free(genctx);
1394 static const EVP_TEST_METHOD mac_test_method = {
1403 * Public key operations. These are all very similar and can share
1404 * a lot of common code.
1407 typedef struct pkey_data_st {
1408 /* Context for this operation */
1410 /* Key operation to perform */
1411 int (*keyop) (EVP_PKEY_CTX *ctx,
1412 unsigned char *sig, size_t *siglen,
1413 const unsigned char *tbs, size_t tbslen);
1415 unsigned char *input;
1417 /* Expected output */
1418 unsigned char *output;
1423 * Perform public key operation setup: lookup key, allocated ctx and call
1424 * the appropriate initialisation function
1426 static int pkey_test_init(EVP_TEST *t, const char *name,
1428 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1429 int (*keyop) (EVP_PKEY_CTX *ctx,
1430 unsigned char *sig, size_t *siglen,
1431 const unsigned char *tbs,
1436 EVP_PKEY *pkey = NULL;
1440 rv = find_key(&pkey, name, public_keys);
1442 rv = find_key(&pkey, name, private_keys);
1443 if (rv == 0 || pkey == NULL) {
1448 if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
1449 EVP_PKEY_free(pkey);
1453 kdata->input = NULL;
1454 kdata->output = NULL;
1455 kdata->keyop = keyop;
1457 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1459 if (keyopinit(kdata->ctx) <= 0)
1460 t->err = "KEYOP_INIT_ERROR";
1464 static void pkey_test_cleanup(EVP_TEST *t)
1466 PKEY_DATA *kdata = t->data;
1468 OPENSSL_free(kdata->input);
1469 OPENSSL_free(kdata->output);
1470 EVP_PKEY_CTX_free(kdata->ctx);
1473 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1479 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1481 p = strchr(tmpval, ':');
1484 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1486 t->err = "PKEY_CTRL_INVALID";
1488 } else if (p != NULL && rv <= 0) {
1489 /* If p has an OID and lookup fails assume disabled algorithm */
1490 int nid = OBJ_sn2nid(p);
1492 if (nid == NID_undef)
1493 nid = OBJ_ln2nid(p);
1494 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1495 EVP_get_cipherbynid(nid) == NULL) {
1499 t->err = "PKEY_CTRL_ERROR";
1503 OPENSSL_free(tmpval);
1507 static int pkey_test_parse(EVP_TEST *t,
1508 const char *keyword, const char *value)
1510 PKEY_DATA *kdata = t->data;
1511 if (strcmp(keyword, "Input") == 0)
1512 return test_bin(value, &kdata->input, &kdata->input_len);
1513 if (strcmp(keyword, "Output") == 0)
1514 return test_bin(value, &kdata->output, &kdata->output_len);
1515 if (strcmp(keyword, "Ctrl") == 0)
1516 return pkey_test_ctrl(t, kdata->ctx, value);
1520 static int pkey_test_run(EVP_TEST *t)
1522 PKEY_DATA *kdata = t->data;
1523 unsigned char *out = NULL;
1526 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1527 kdata->input_len) <= 0
1528 || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
1529 t->err = "KEYOP_LENGTH_ERROR";
1532 if (kdata->keyop(kdata->ctx, out,
1533 &out_len, kdata->input, kdata->input_len) <= 0) {
1534 t->err = "KEYOP_ERROR";
1537 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1538 t->err = "KEYOP_MISMATCH";
1547 static int sign_test_init(EVP_TEST *t, const char *name)
1549 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1552 static const EVP_TEST_METHOD psign_test_method = {
1560 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1562 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1563 EVP_PKEY_verify_recover);
1566 static const EVP_TEST_METHOD pverify_recover_test_method = {
1568 verify_recover_test_init,
1574 static int decrypt_test_init(EVP_TEST *t, const char *name)
1576 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1580 static const EVP_TEST_METHOD pdecrypt_test_method = {
1588 static int verify_test_init(EVP_TEST *t, const char *name)
1590 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1593 static int verify_test_run(EVP_TEST *t)
1595 PKEY_DATA *kdata = t->data;
1597 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1598 kdata->input, kdata->input_len) <= 0)
1599 t->err = "VERIFY_ERROR";
1603 static const EVP_TEST_METHOD pverify_test_method = {
1612 static int pderive_test_init(EVP_TEST *t, const char *name)
1614 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1617 static int pderive_test_parse(EVP_TEST *t,
1618 const char *keyword, const char *value)
1620 PKEY_DATA *kdata = t->data;
1622 if (strcmp(keyword, "PeerKey") == 0) {
1624 if (find_key(&peer, value, public_keys) == 0)
1626 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1630 if (strcmp(keyword, "SharedSecret") == 0)
1631 return test_bin(value, &kdata->output, &kdata->output_len);
1632 if (strcmp(keyword, "Ctrl") == 0)
1633 return pkey_test_ctrl(t, kdata->ctx, value);
1637 static int pderive_test_run(EVP_TEST *t)
1639 PKEY_DATA *kdata = t->data;
1640 unsigned char *out = NULL;
1643 out_len = kdata->output_len;
1644 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1645 t->err = "DERIVE_ERROR";
1648 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1649 t->err = "DERIVE_ERROR";
1652 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
1653 t->err = "SHARED_SECRET_MISMATCH";
1663 static const EVP_TEST_METHOD pderive_test_method = {
1673 #define PBE_TYPE_SCRYPT 1
1674 #define PBE_TYPE_PBKDF2 2
1675 #define PBE_TYPE_PKCS12 3
1677 typedef struct pbe_data_st {
1679 /* scrypt parameters */
1680 uint64_t N, r, p, maxmem;
1681 /* PKCS#12 parameters */
1685 unsigned char *pass;
1688 unsigned char *salt;
1690 /* Expected output */
1695 #ifndef OPENSSL_NO_SCRYPT
1696 static int scrypt_test_parse(EVP_TEST *t,
1697 const char *keyword, const char *value)
1699 PBE_DATA *pdata = t->data;
1701 if (strcmp(keyword, "N") == 0)
1702 return test_uint64(value, &pdata->N);
1703 if (strcmp(keyword, "p") == 0)
1704 return test_uint64(value, &pdata->p);
1705 if (strcmp(keyword, "r") == 0)
1706 return test_uint64(value, &pdata->r);
1707 if (strcmp(keyword, "maxmem") == 0)
1708 return test_uint64(value, &pdata->maxmem);
1713 static int pbkdf2_test_parse(EVP_TEST *t,
1714 const char *keyword, const char *value)
1716 PBE_DATA *pdata = t->data;
1718 if (strcmp(keyword, "iter") == 0) {
1719 pdata->iter = atoi(value);
1720 if (pdata->iter <= 0)
1724 if (strcmp(keyword, "MD") == 0) {
1725 pdata->md = EVP_get_digestbyname(value);
1726 if (pdata->md == NULL)
1733 static int pkcs12_test_parse(EVP_TEST *t,
1734 const char *keyword, const char *value)
1736 PBE_DATA *pdata = t->data;
1738 if (strcmp(keyword, "id") == 0) {
1739 pdata->id = atoi(value);
1744 return pbkdf2_test_parse(t, keyword, value);
1747 static int pbe_test_init(EVP_TEST *t, const char *alg)
1752 if (strcmp(alg, "scrypt") == 0) {
1753 #ifndef OPENSSL_NO_SCRYPT
1754 pbe_type = PBE_TYPE_SCRYPT;
1759 } else if (strcmp(alg, "pbkdf2") == 0) {
1760 pbe_type = PBE_TYPE_PBKDF2;
1761 } else if (strcmp(alg, "pkcs12") == 0) {
1762 pbe_type = PBE_TYPE_PKCS12;
1764 TEST_error("Unknown pbe algorithm %s", alg);
1766 pdat = OPENSSL_malloc(sizeof(*pdat));
1767 pdat->pbe_type = pbe_type;
1781 static void pbe_test_cleanup(EVP_TEST *t)
1783 PBE_DATA *pdat = t->data;
1785 OPENSSL_free(pdat->pass);
1786 OPENSSL_free(pdat->salt);
1787 OPENSSL_free(pdat->key);
1790 static int pbe_test_parse(EVP_TEST *t,
1791 const char *keyword, const char *value)
1793 PBE_DATA *pdata = t->data;
1795 if (strcmp(keyword, "Password") == 0)
1796 return test_bin(value, &pdata->pass, &pdata->pass_len);
1797 if (strcmp(keyword, "Salt") == 0)
1798 return test_bin(value, &pdata->salt, &pdata->salt_len);
1799 if (strcmp(keyword, "Key") == 0)
1800 return test_bin(value, &pdata->key, &pdata->key_len);
1801 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1802 return pbkdf2_test_parse(t, keyword, value);
1803 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1804 return pkcs12_test_parse(t, keyword, value);
1805 #ifndef OPENSSL_NO_SCRYPT
1806 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1807 return scrypt_test_parse(t, keyword, value);
1812 static int pbe_test_run(EVP_TEST *t)
1814 PBE_DATA *pdata = t->data;
1817 if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
1818 t->err = "INTERNAL_ERROR";
1821 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1822 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1823 pdata->salt, pdata->salt_len,
1824 pdata->iter, pdata->md,
1825 pdata->key_len, key) == 0) {
1826 t->err = "PBKDF2_ERROR";
1829 #ifndef OPENSSL_NO_SCRYPT
1830 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1831 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1832 pdata->salt, pdata->salt_len,
1833 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1834 key, pdata->key_len) == 0) {
1835 t->err = "SCRYPT_ERROR";
1839 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1840 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1841 pdata->salt, pdata->salt_len,
1842 pdata->id, pdata->iter, pdata->key_len,
1843 key, pdata->md) == 0) {
1844 t->err = "PKCS12_ERROR";
1848 if (!compare_mem(pdata->key, pdata->key_len, key, pdata->key_len)) {
1849 t->err = "KEY_MISMATCH";
1858 static const EVP_TEST_METHOD pbe_test_method = {
1869 BASE64_CANONICAL_ENCODING = 0,
1870 BASE64_VALID_ENCODING = 1,
1871 BASE64_INVALID_ENCODING = 2
1872 } base64_encoding_type;
1874 typedef struct encode_data_st {
1875 /* Input to encoding */
1876 unsigned char *input;
1878 /* Expected output */
1879 unsigned char *output;
1881 base64_encoding_type encoding;
1884 static int encode_test_init(EVP_TEST *t, const char *encoding)
1886 ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
1888 if (strcmp(encoding, "canonical") == 0) {
1889 edata->encoding = BASE64_CANONICAL_ENCODING;
1890 } else if (strcmp(encoding, "valid") == 0) {
1891 edata->encoding = BASE64_VALID_ENCODING;
1892 } else if (strcmp(encoding, "invalid") == 0) {
1893 edata->encoding = BASE64_INVALID_ENCODING;
1894 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1895 if (t->expected_err == NULL)
1898 TEST_info("Bad encoding: %s. Should be one of "
1899 "{canonical, valid, invalid}", encoding);
1906 static void encode_test_cleanup(EVP_TEST *t)
1908 ENCODE_DATA *edata = t->data;
1910 OPENSSL_free(edata->input);
1911 OPENSSL_free(edata->output);
1912 memset(edata, 0, sizeof(*edata));
1915 static int encode_test_parse(EVP_TEST *t,
1916 const char *keyword, const char *value)
1918 ENCODE_DATA *edata = t->data;
1919 if (strcmp(keyword, "Input") == 0)
1920 return test_bin(value, &edata->input, &edata->input_len);
1921 if (strcmp(keyword, "Output") == 0)
1922 return test_bin(value, &edata->output, &edata->output_len);
1926 static int encode_test_run(EVP_TEST *t)
1928 ENCODE_DATA *edata = t->data;
1929 unsigned char *encode_out = NULL, *decode_out = NULL;
1930 int output_len, chunk_len;
1931 EVP_ENCODE_CTX *decode_ctx;
1933 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1934 t->err = "INTERNAL_ERROR";
1938 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1939 EVP_ENCODE_CTX *encode_ctx;
1941 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1942 || !TEST_ptr(encode_out =
1943 OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
1946 EVP_EncodeInit(encode_ctx);
1947 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1948 edata->input, edata->input_len);
1949 output_len = chunk_len;
1951 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1952 output_len += chunk_len;
1954 EVP_ENCODE_CTX_free(encode_ctx);
1956 if (!compare_mem(edata->output, edata->output_len,
1957 encode_out, output_len)) {
1958 t->err = "BAD_ENCODING";
1963 if (!TEST_ptr(decode_out =
1964 OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
1967 EVP_DecodeInit(decode_ctx);
1968 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1969 edata->output_len) < 0) {
1970 t->err = "DECODE_ERROR";
1973 output_len = chunk_len;
1975 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1976 t->err = "DECODE_ERROR";
1979 output_len += chunk_len;
1981 if (edata->encoding != BASE64_INVALID_ENCODING
1982 && !compare_mem(edata->input, edata->input_len,
1983 decode_out, output_len)) {
1984 t->err = "BAD_DECODING";
1990 OPENSSL_free(encode_out);
1991 OPENSSL_free(decode_out);
1992 EVP_ENCODE_CTX_free(decode_ctx);
1996 static const EVP_TEST_METHOD encode_test_method = {
1999 encode_test_cleanup,
2004 /* KDF operations */
2006 typedef struct kdf_data_st {
2007 /* Context for this operation */
2009 /* Expected output */
2010 unsigned char *output;
2015 * Perform public key operation setup: lookup key, allocated ctx and call
2016 * the appropriate initialisation function
2018 static int kdf_test_init(EVP_TEST *t, const char *name)
2022 kdata = OPENSSL_malloc(sizeof(*kdata));
2026 kdata->output = NULL;
2028 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
2029 if (kdata->ctx == NULL)
2031 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
2036 static void kdf_test_cleanup(EVP_TEST *t)
2038 KDF_DATA *kdata = t->data;
2039 OPENSSL_free(kdata->output);
2040 EVP_PKEY_CTX_free(kdata->ctx);
2043 static int kdf_test_parse(EVP_TEST *t,
2044 const char *keyword, const char *value)
2046 KDF_DATA *kdata = t->data;
2048 if (strcmp(keyword, "Output") == 0)
2049 return test_bin(value, &kdata->output, &kdata->output_len);
2050 if (strncmp(keyword, "Ctrl", 4) == 0)
2051 return pkey_test_ctrl(t, kdata->ctx, value);
2055 static int kdf_test_run(EVP_TEST *t)
2057 KDF_DATA *kdata = t->data;
2058 unsigned char *out = NULL;
2059 size_t out_len = kdata->output_len;
2061 if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
2062 t->err = "INTERNAL_ERROR";
2065 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
2066 t->err = "KDF_DERIVE_ERROR";
2069 if (!compare_mem(kdata->output, kdata->output_len, out, out_len)) {
2070 t->err = "KDF_MISMATCH";
2080 static const EVP_TEST_METHOD kdf_test_method = {
2088 typedef struct keypair_test_buffer_st {
2091 } KEYPAIR_TEST_DATA;
2093 static int keypair_test_init(EVP_TEST *t, const char *pair)
2096 EVP_PKEY *pk = NULL, *pubk = NULL;
2097 char *pub, *priv = NULL;
2098 KEYPAIR_TEST_DATA *data;
2100 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2101 || !TEST_ptr(pub = strchr(priv, ':'))) {
2102 t->err = "PARSING_ERROR";
2105 *pub++ = 0; /* split priv and pub strings */
2107 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2108 TEST_info("Cannot find private key: %s", priv);
2109 t->err = "MISSING_PRIVATE_KEY";
2112 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2113 TEST_info("Cannot find public key: %s", pub);
2114 t->err = "MISSING_PUBLIC_KEY";
2118 if (pk == NULL && pubk == NULL) {
2119 /* Both keys are listed but unsupported: skip this test */
2125 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2139 static void keypair_test_cleanup(EVP_TEST *t)
2141 OPENSSL_free(t->data);
2145 /* For test that do not accept any custom keyword:
2146 * return 0 if called
2148 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2153 static int keypair_test_run(EVP_TEST *t)
2156 const KEYPAIR_TEST_DATA *pair = t->data;
2158 if (pair->privk == NULL || pair->pubk == NULL) {
2160 * this can only happen if only one of the keys is not set
2161 * which means that one of them was unsupported while the
2162 * other isn't: hence a key type mismatch.
2164 t->err = "KEYPAIR_TYPE_MISMATCH";
2169 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2171 t->err = "KEYPAIR_MISMATCH";
2172 } else if ( -1 == rv ) {
2173 t->err = "KEYPAIR_TYPE_MISMATCH";
2174 } else if ( -2 == rv ) {
2175 t->err = "UNSUPPORTED_KEY_COMPARISON";
2177 TEST_error("Unexpected error in key comparison");
2192 static const EVP_TEST_METHOD keypair_test_method = {
2195 keypair_test_cleanup,
2200 static int do_test_file(const char *testfile)
2206 set_test_title(testfile);
2207 current_test_file = testfile;
2208 if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2210 memset(&t, 0, sizeof(t));
2214 while (BIO_gets(in, buf, sizeof(buf))) {
2216 if (!TEST_true(parse_test_line(&t, buf)))
2219 /* Run any final test we have */
2220 if (!run_and_get_next(&t, NULL))
2223 TEST_info("Completed %d tests with %d errors and %d skipped",
2224 t.ntests, t.errors, t.nskip);
2225 free_key_list(public_keys);
2226 free_key_list(private_keys);
2229 return t.errors == 0;
2232 static char * const *testfiles;
2234 static int run_file_tests(int i)
2236 return do_test_file(testfiles[i]);
2239 int test_main(int argc, char *argv[])
2242 TEST_error("Usage: %s file...", argv[0]);
2245 testfiles = &argv[1];
2247 ADD_ALL_TESTS(run_file_tests, argc - 1);
2249 return run_tests(argv[0]);