Home | History | Annotate | Line # | Download | only in test
      1   1.2  christos /*
      2  1.12  christos  * Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
      3   1.2  christos  *
      4  1.10  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5   1.2  christos  * this file except in compliance with the License.  You can obtain a copy
      6   1.2  christos  * in the file LICENSE in the source distribution or at
      7   1.2  christos  * https://www.openssl.org/source/license.html
      8   1.2  christos  */
      9   1.2  christos 
     10  1.10  christos #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */
     11   1.2  christos #include <stdio.h>
     12   1.2  christos #include <string.h>
     13   1.2  christos #include <stdlib.h>
     14   1.2  christos #include <ctype.h>
     15   1.2  christos #include <openssl/evp.h>
     16   1.2  christos #include <openssl/pem.h>
     17   1.2  christos #include <openssl/err.h>
     18  1.10  christos #include <openssl/provider.h>
     19   1.2  christos #include <openssl/x509v3.h>
     20   1.2  christos #include <openssl/pkcs12.h>
     21   1.2  christos #include <openssl/kdf.h>
     22  1.10  christos #include <openssl/params.h>
     23  1.10  christos #include <openssl/core_names.h>
     24  1.10  christos #include <openssl/fips_names.h>
     25   1.2  christos #include "internal/numbers.h"
     26  1.10  christos #include "internal/nelem.h"
     27  1.10  christos #include "crypto/evp.h"
     28   1.4  christos #include "testutil.h"
     29   1.2  christos 
     30  1.10  christos typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
     31  1.10  christos DEFINE_STACK_OF(EVP_TEST_BUFFER)
     32  1.10  christos 
     33  1.10  christos #define AAD_NUM 4
     34   1.2  christos 
     35   1.4  christos typedef struct evp_test_method_st EVP_TEST_METHOD;
     36   1.4  christos 
     37  1.10  christos /* Structure holding test information */
     38   1.4  christos typedef struct evp_test_st {
     39   1.4  christos     STANZA s;                     /* Common test stanza */
     40   1.4  christos     char *name;
     41   1.4  christos     int skip;                     /* Current test should be skipped */
     42   1.4  christos     const EVP_TEST_METHOD *meth;  /* method for this test */
     43   1.4  christos     const char *err, *aux_err;    /* Error string for test */
     44   1.4  christos     char *expected_err;           /* Expected error value of test */
     45   1.4  christos     char *reason;                 /* Expected error reason string */
     46   1.4  christos     void *data;                   /* test specific data */
     47   1.4  christos } EVP_TEST;
     48   1.4  christos 
     49  1.10  christos /* Test method structure */
     50   1.4  christos struct evp_test_method_st {
     51   1.4  christos     /* Name of test as it appears in file */
     52   1.4  christos     const char *name;
     53   1.4  christos     /* Initialise test for "alg" */
     54   1.4  christos     int (*init) (EVP_TEST * t, const char *alg);
     55   1.4  christos     /* Clean up method */
     56   1.4  christos     void (*cleanup) (EVP_TEST * t);
     57   1.4  christos     /* Test specific name value pair processing */
     58   1.4  christos     int (*parse) (EVP_TEST * t, const char *name, const char *value);
     59   1.4  christos     /* Run the test itself */
     60   1.4  christos     int (*run_test) (EVP_TEST * t);
     61   1.4  christos };
     62   1.4  christos 
     63  1.10  christos /* Linked list of named keys. */
     64   1.4  christos typedef struct key_list_st {
     65   1.4  christos     char *name;
     66   1.4  christos     EVP_PKEY *key;
     67   1.4  christos     struct key_list_st *next;
     68   1.4  christos } KEY_LIST;
     69   1.2  christos 
     70  1.10  christos typedef enum OPTION_choice {
     71  1.10  christos     OPT_ERR = -1,
     72  1.10  christos     OPT_EOF = 0,
     73  1.10  christos     OPT_CONFIG_FILE,
     74  1.10  christos     OPT_TEST_ENUM
     75  1.10  christos } OPTION_CHOICE;
     76  1.10  christos 
     77  1.10  christos static OSSL_PROVIDER *prov_null = NULL;
     78  1.10  christos static OSSL_LIB_CTX *libctx = NULL;
     79  1.10  christos 
     80  1.10  christos /* List of public and private keys */
     81   1.4  christos static KEY_LIST *private_keys;
     82   1.4  christos static KEY_LIST *public_keys;
     83  1.10  christos 
     84   1.4  christos static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
     85   1.4  christos static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
     86  1.10  christos static int is_digest_disabled(const char *name);
     87  1.10  christos static int is_pkey_disabled(const char *name);
     88  1.10  christos static int is_mac_disabled(const char *name);
     89  1.10  christos static int is_cipher_disabled(const char *name);
     90  1.10  christos static int is_kdf_disabled(const char *name);
     91   1.2  christos 
     92   1.4  christos /*
     93   1.4  christos  * Compare two memory regions for equality, returning zero if they differ.
     94   1.4  christos  * However, if there is expected to be an error and the actual error
     95   1.4  christos  * matches then the memory is expected to be different so handle this
     96   1.4  christos  * case without producing unnecessary test framework output.
     97   1.4  christos  */
     98   1.4  christos static int memory_err_compare(EVP_TEST *t, const char *err,
     99   1.4  christos                               const void *expected, size_t expected_len,
    100   1.4  christos                               const void *got, size_t got_len)
    101   1.4  christos {
    102   1.4  christos     int r;
    103   1.2  christos 
    104   1.4  christos     if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
    105   1.4  christos         r = !TEST_mem_ne(expected, expected_len, got, got_len);
    106   1.4  christos     else
    107   1.4  christos         r = TEST_mem_eq(expected, expected_len, got, got_len);
    108   1.4  christos     if (!r)
    109   1.4  christos         t->err = err;
    110   1.4  christos     return r;
    111   1.2  christos }
    112   1.2  christos 
    113   1.2  christos /*
    114   1.4  christos  * Structure used to hold a list of blocks of memory to test
    115   1.4  christos  * calls to "update" like functions.
    116   1.2  christos  */
    117   1.4  christos struct evp_test_buffer_st {
    118   1.4  christos     unsigned char *buf;
    119   1.4  christos     size_t buflen;
    120   1.4  christos     size_t count;
    121   1.4  christos     int count_set;
    122   1.4  christos };
    123   1.2  christos 
    124   1.4  christos static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
    125   1.4  christos {
    126   1.4  christos     if (db != NULL) {
    127   1.4  christos         OPENSSL_free(db->buf);
    128   1.4  christos         OPENSSL_free(db);
    129   1.4  christos     }
    130   1.4  christos }
    131   1.4  christos 
    132  1.10  christos /* append buffer to a list */
    133   1.4  christos static int evp_test_buffer_append(const char *value,
    134   1.4  christos                                   STACK_OF(EVP_TEST_BUFFER) **sk)
    135   1.2  christos {
    136   1.4  christos     EVP_TEST_BUFFER *db = NULL;
    137   1.4  christos 
    138   1.4  christos     if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
    139   1.4  christos         goto err;
    140   1.4  christos 
    141   1.4  christos     if (!parse_bin(value, &db->buf, &db->buflen))
    142   1.4  christos         goto err;
    143   1.4  christos     db->count = 1;
    144   1.4  christos     db->count_set = 0;
    145   1.4  christos 
    146   1.4  christos     if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
    147   1.4  christos         goto err;
    148   1.4  christos     if (!sk_EVP_TEST_BUFFER_push(*sk, db))
    149   1.4  christos         goto err;
    150   1.2  christos 
    151   1.4  christos     return 1;
    152   1.4  christos 
    153   1.4  christos err:
    154   1.4  christos     evp_test_buffer_free(db);
    155   1.4  christos     return 0;
    156   1.4  christos }
    157   1.4  christos 
    158  1.10  christos /* replace last buffer in list with copies of itself */
    159   1.4  christos static int evp_test_buffer_ncopy(const char *value,
    160   1.4  christos                                  STACK_OF(EVP_TEST_BUFFER) *sk)
    161   1.4  christos {
    162   1.4  christos     EVP_TEST_BUFFER *db;
    163   1.4  christos     unsigned char *tbuf, *p;
    164   1.4  christos     size_t tbuflen;
    165   1.4  christos     int ncopy = atoi(value);
    166   1.4  christos     int i;
    167   1.2  christos 
    168   1.4  christos     if (ncopy <= 0)
    169   1.4  christos         return 0;
    170   1.4  christos     if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
    171   1.4  christos         return 0;
    172   1.4  christos     db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
    173   1.2  christos 
    174   1.4  christos     tbuflen = db->buflen * ncopy;
    175   1.4  christos     if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
    176   1.4  christos         return 0;
    177   1.4  christos     for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
    178   1.4  christos         memcpy(p, db->buf, db->buflen);
    179   1.2  christos 
    180   1.4  christos     OPENSSL_free(db->buf);
    181   1.4  christos     db->buf = tbuf;
    182   1.4  christos     db->buflen = tbuflen;
    183   1.4  christos     return 1;
    184   1.4  christos }
    185   1.2  christos 
    186  1.10  christos /* set repeat count for last buffer in list */
    187   1.4  christos static int evp_test_buffer_set_count(const char *value,
    188   1.4  christos                                      STACK_OF(EVP_TEST_BUFFER) *sk)
    189   1.4  christos {
    190   1.4  christos     EVP_TEST_BUFFER *db;
    191   1.4  christos     int count = atoi(value);
    192   1.2  christos 
    193   1.4  christos     if (count <= 0)
    194   1.4  christos         return 0;
    195   1.2  christos 
    196   1.4  christos     if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
    197   1.2  christos         return 0;
    198   1.2  christos 
    199   1.4  christos     db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
    200   1.4  christos     if (db->count_set != 0)
    201   1.4  christos         return 0;
    202   1.2  christos 
    203   1.4  christos     db->count = (size_t)count;
    204   1.4  christos     db->count_set = 1;
    205   1.4  christos     return 1;
    206   1.4  christos }
    207   1.2  christos 
    208  1.10  christos /* call "fn" with each element of the list in turn */
    209   1.4  christos static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
    210   1.4  christos                               int (*fn)(void *ctx,
    211   1.4  christos                                         const unsigned char *buf,
    212   1.4  christos                                         size_t buflen),
    213   1.4  christos                               void *ctx)
    214   1.4  christos {
    215   1.4  christos     int i;
    216   1.4  christos 
    217   1.4  christos     for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
    218   1.4  christos         EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
    219   1.4  christos         size_t j;
    220   1.2  christos 
    221   1.4  christos         for (j = 0; j < tb->count; j++) {
    222   1.4  christos             if (fn(ctx, tb->buf, tb->buflen) <= 0)
    223   1.4  christos                 return 0;
    224   1.4  christos         }
    225   1.4  christos     }
    226   1.2  christos     return 1;
    227   1.2  christos }
    228   1.2  christos 
    229   1.2  christos /*
    230   1.4  christos  * Unescape some sequences in string literals (only \n for now).
    231   1.4  christos  * Return an allocated buffer, set |out_len|.  If |input_len|
    232   1.4  christos  * is zero, get an empty buffer but set length to zero.
    233   1.2  christos  */
    234   1.2  christos static unsigned char* unescape(const char *input, size_t input_len,
    235   1.2  christos                                size_t *out_len)
    236   1.2  christos {
    237   1.2  christos     unsigned char *ret, *p;
    238   1.2  christos     size_t i;
    239   1.4  christos 
    240   1.2  christos     if (input_len == 0) {
    241   1.2  christos         *out_len = 0;
    242   1.2  christos         return OPENSSL_zalloc(1);
    243   1.2  christos     }
    244   1.2  christos 
    245   1.2  christos     /* Escaping is non-expanding; over-allocate original size for simplicity. */
    246   1.4  christos     if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
    247   1.2  christos         return NULL;
    248   1.2  christos 
    249   1.2  christos     for (i = 0; i < input_len; i++) {
    250   1.4  christos         if (*input == '\\') {
    251   1.4  christos             if (i == input_len - 1 || *++input != 'n') {
    252   1.4  christos                 TEST_error("Bad escape sequence in file");
    253   1.2  christos                 goto err;
    254   1.4  christos             }
    255   1.2  christos             *p++ = '\n';
    256   1.2  christos             i++;
    257   1.4  christos             input++;
    258   1.2  christos         } else {
    259   1.4  christos             *p++ = *input++;
    260   1.2  christos         }
    261   1.2  christos     }
    262   1.2  christos 
    263   1.2  christos     *out_len = p - ret;
    264   1.2  christos     return ret;
    265   1.2  christos 
    266   1.2  christos  err:
    267   1.2  christos     OPENSSL_free(ret);
    268   1.2  christos     return NULL;
    269   1.2  christos }
    270   1.2  christos 
    271   1.4  christos /*
    272   1.4  christos  * For a hex string "value" convert to a binary allocated buffer.
    273   1.4  christos  * Return 1 on success or 0 on failure.
    274   1.4  christos  */
    275   1.4  christos static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
    276   1.2  christos {
    277   1.2  christos     long len;
    278   1.2  christos 
    279   1.4  christos     /* Check for NULL literal */
    280   1.4  christos     if (strcmp(value, "NULL") == 0) {
    281   1.4  christos         *buf = NULL;
    282   1.4  christos         *buflen = 0;
    283   1.4  christos         return 1;
    284   1.4  christos     }
    285   1.2  christos 
    286   1.2  christos     /* Check for empty value */
    287   1.4  christos     if (*value == '\0') {
    288   1.2  christos         /*
    289   1.4  christos          * Don't return NULL for zero length buffer. This is needed for
    290   1.4  christos          * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
    291   1.4  christos          * buffer even if the key length is 0, in order to detect key reset.
    292   1.2  christos          */
    293   1.2  christos         *buf = OPENSSL_malloc(1);
    294   1.4  christos         if (*buf == NULL)
    295   1.2  christos             return 0;
    296   1.2  christos         **buf = 0;
    297   1.2  christos         *buflen = 0;
    298   1.2  christos         return 1;
    299   1.2  christos     }
    300   1.2  christos 
    301   1.2  christos     /* Check for string literal */
    302   1.2  christos     if (value[0] == '"') {
    303   1.4  christos         size_t vlen = strlen(++value);
    304   1.4  christos 
    305   1.4  christos         if (vlen == 0 || value[vlen - 1] != '"')
    306   1.2  christos             return 0;
    307   1.2  christos         vlen--;
    308   1.2  christos         *buf = unescape(value, vlen, buflen);
    309   1.4  christos         return *buf == NULL ? 0 : 1;
    310   1.2  christos     }
    311   1.2  christos 
    312   1.2  christos     /* Otherwise assume as hex literal and convert it to binary buffer */
    313   1.4  christos     if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
    314   1.4  christos         TEST_info("Can't convert %s", value);
    315   1.4  christos         TEST_openssl_errors();
    316   1.2  christos         return -1;
    317   1.2  christos     }
    318   1.2  christos     /* Size of input buffer means we'll never overflow */
    319   1.2  christos     *buflen = len;
    320   1.2  christos     return 1;
    321   1.2  christos }
    322   1.2  christos 
    323   1.4  christos /**
    324  1.10  christos  **  MESSAGE DIGEST TESTS
    325  1.10  christos  **/
    326   1.2  christos 
    327   1.4  christos typedef struct digest_data_st {
    328   1.4  christos     /* Digest this test is for */
    329   1.4  christos     const EVP_MD *digest;
    330  1.10  christos     EVP_MD *fetched_digest;
    331   1.4  christos     /* Input to digest */
    332   1.4  christos     STACK_OF(EVP_TEST_BUFFER) *input;
    333   1.4  christos     /* Expected output */
    334   1.4  christos     unsigned char *output;
    335   1.4  christos     size_t output_len;
    336  1.10  christos     /* Padding type */
    337  1.10  christos     int pad_type;
    338   1.4  christos } DIGEST_DATA;
    339   1.2  christos 
    340   1.4  christos static int digest_test_init(EVP_TEST *t, const char *alg)
    341   1.2  christos {
    342   1.4  christos     DIGEST_DATA *mdat;
    343   1.4  christos     const EVP_MD *digest;
    344  1.10  christos     EVP_MD *fetched_digest;
    345   1.2  christos 
    346  1.10  christos     if (is_digest_disabled(alg)) {
    347  1.10  christos         TEST_info("skipping, '%s' is disabled", alg);
    348  1.10  christos         t->skip = 1;
    349  1.10  christos         return 1;
    350  1.10  christos     }
    351  1.10  christos 
    352  1.10  christos     if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
    353  1.10  christos         && (digest = EVP_get_digestbyname(alg)) == NULL)
    354   1.4  christos         return 0;
    355   1.4  christos     if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
    356   1.4  christos         return 0;
    357   1.4  christos     t->data = mdat;
    358   1.4  christos     mdat->digest = digest;
    359  1.10  christos     mdat->fetched_digest = fetched_digest;
    360  1.10  christos     mdat->pad_type = 0;
    361  1.10  christos     if (fetched_digest != NULL)
    362  1.10  christos         TEST_info("%s is fetched", alg);
    363   1.4  christos     return 1;
    364   1.2  christos }
    365   1.2  christos 
    366   1.4  christos static void digest_test_cleanup(EVP_TEST *t)
    367   1.2  christos {
    368   1.4  christos     DIGEST_DATA *mdat = t->data;
    369   1.4  christos 
    370   1.4  christos     sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
    371   1.4  christos     OPENSSL_free(mdat->output);
    372  1.10  christos     EVP_MD_free(mdat->fetched_digest);
    373   1.2  christos }
    374   1.2  christos 
    375   1.4  christos static int digest_test_parse(EVP_TEST *t,
    376   1.4  christos                              const char *keyword, const char *value)
    377   1.2  christos {
    378   1.4  christos     DIGEST_DATA *mdata = t->data;
    379   1.4  christos 
    380   1.4  christos     if (strcmp(keyword, "Input") == 0)
    381   1.4  christos         return evp_test_buffer_append(value, &mdata->input);
    382   1.4  christos     if (strcmp(keyword, "Output") == 0)
    383   1.4  christos         return parse_bin(value, &mdata->output, &mdata->output_len);
    384   1.4  christos     if (strcmp(keyword, "Count") == 0)
    385   1.4  christos         return evp_test_buffer_set_count(value, mdata->input);
    386   1.4  christos     if (strcmp(keyword, "Ncopy") == 0)
    387   1.4  christos         return evp_test_buffer_ncopy(value, mdata->input);
    388  1.10  christos     if (strcmp(keyword, "Padding") == 0)
    389  1.10  christos         return (mdata->pad_type = atoi(value)) > 0;
    390   1.4  christos     return 0;
    391   1.2  christos }
    392   1.2  christos 
    393   1.4  christos static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
    394   1.2  christos {
    395   1.4  christos     return EVP_DigestUpdate(ctx, buf, buflen);
    396   1.2  christos }
    397   1.2  christos 
    398   1.4  christos static int digest_test_run(EVP_TEST *t)
    399   1.2  christos {
    400   1.4  christos     DIGEST_DATA *expected = t->data;
    401  1.10  christos     EVP_TEST_BUFFER *inbuf;
    402   1.4  christos     EVP_MD_CTX *mctx;
    403   1.4  christos     unsigned char *got = NULL;
    404   1.4  christos     unsigned int got_len;
    405  1.10  christos     size_t size = 0;
    406  1.10  christos     int xof = 0;
    407  1.10  christos     OSSL_PARAM params[2];
    408   1.2  christos 
    409   1.4  christos     t->err = "TEST_FAILURE";
    410   1.4  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
    411   1.4  christos         goto err;
    412   1.2  christos 
    413   1.4  christos     got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
    414   1.4  christos                          expected->output_len : EVP_MAX_MD_SIZE);
    415   1.4  christos     if (!TEST_ptr(got))
    416   1.4  christos         goto err;
    417   1.2  christos 
    418   1.4  christos     if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
    419   1.4  christos         t->err = "DIGESTINIT_ERROR";
    420   1.4  christos         goto err;
    421   1.4  christos     }
    422  1.10  christos     if (expected->pad_type > 0) {
    423  1.10  christos         params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
    424  1.10  christos                                               &expected->pad_type);
    425  1.10  christos         params[1] = OSSL_PARAM_construct_end();
    426  1.10  christos         if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
    427  1.10  christos             t->err = "PARAMS_ERROR";
    428  1.10  christos             goto err;
    429  1.10  christos         }
    430  1.10  christos     }
    431   1.4  christos     if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
    432   1.4  christos         t->err = "DIGESTUPDATE_ERROR";
    433   1.4  christos         goto err;
    434   1.2  christos     }
    435   1.2  christos 
    436  1.10  christos     xof = (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0;
    437  1.10  christos     if (xof) {
    438   1.8  christos         EVP_MD_CTX *mctx_cpy;
    439   1.8  christos         char dont[] = "touch";
    440   1.8  christos 
    441   1.8  christos         if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
    442   1.8  christos             goto err;
    443   1.8  christos         }
    444   1.8  christos         if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
    445   1.8  christos             EVP_MD_CTX_free(mctx_cpy);
    446   1.8  christos             goto err;
    447   1.8  christos         }
    448   1.8  christos         if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
    449   1.8  christos             EVP_MD_CTX_free(mctx_cpy);
    450   1.8  christos             t->err = "DIGESTFINALXOF_ERROR";
    451   1.8  christos             goto err;
    452   1.8  christos         }
    453   1.8  christos         if (!TEST_str_eq(dont, "touch")) {
    454   1.8  christos             EVP_MD_CTX_free(mctx_cpy);
    455   1.8  christos             t->err = "DIGESTFINALXOF_ERROR";
    456   1.8  christos             goto err;
    457   1.8  christos         }
    458   1.8  christos         EVP_MD_CTX_free(mctx_cpy);
    459   1.8  christos 
    460   1.4  christos         got_len = expected->output_len;
    461   1.4  christos         if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
    462   1.4  christos             t->err = "DIGESTFINALXOF_ERROR";
    463   1.4  christos             goto err;
    464   1.4  christos         }
    465   1.4  christos     } else {
    466   1.4  christos         if (!EVP_DigestFinal(mctx, got, &got_len)) {
    467   1.4  christos             t->err = "DIGESTFINAL_ERROR";
    468   1.4  christos             goto err;
    469   1.4  christos         }
    470   1.4  christos     }
    471   1.4  christos     if (!TEST_int_eq(expected->output_len, got_len)) {
    472   1.4  christos         t->err = "DIGEST_LENGTH_MISMATCH";
    473   1.4  christos         goto err;
    474   1.2  christos     }
    475   1.4  christos     if (!memory_err_compare(t, "DIGEST_MISMATCH",
    476   1.4  christos                             expected->output, expected->output_len,
    477   1.4  christos                             got, got_len))
    478   1.4  christos         goto err;
    479   1.2  christos 
    480   1.4  christos     t->err = NULL;
    481   1.2  christos 
    482  1.10  christos     /* Test the EVP_Q_digest interface as well */
    483  1.10  christos     if (sk_EVP_TEST_BUFFER_num(expected->input) == 1
    484  1.10  christos             && !xof
    485  1.10  christos             /* This should never fail but we need the returned pointer now */
    486  1.10  christos             && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0))
    487  1.10  christos             && !inbuf->count_set) {
    488  1.10  christos         OPENSSL_cleanse(got, got_len);
    489  1.10  christos         if (!TEST_true(EVP_Q_digest(libctx,
    490  1.10  christos                                     EVP_MD_get0_name(expected->fetched_digest),
    491  1.10  christos                                     NULL, inbuf->buf, inbuf->buflen,
    492  1.10  christos                                     got, &size))
    493  1.10  christos                 || !TEST_mem_eq(got, size,
    494  1.10  christos                                 expected->output, expected->output_len)) {
    495  1.10  christos             t->err = "EVP_Q_digest failed";
    496  1.10  christos             goto err;
    497  1.10  christos         }
    498  1.10  christos     }
    499  1.10  christos 
    500   1.4  christos  err:
    501   1.4  christos     OPENSSL_free(got);
    502   1.4  christos     EVP_MD_CTX_free(mctx);
    503   1.4  christos     return 1;
    504   1.4  christos }
    505   1.2  christos 
    506   1.4  christos static const EVP_TEST_METHOD digest_test_method = {
    507   1.4  christos     "Digest",
    508   1.4  christos     digest_test_init,
    509   1.4  christos     digest_test_cleanup,
    510   1.4  christos     digest_test_parse,
    511   1.4  christos     digest_test_run
    512   1.4  christos };
    513   1.2  christos 
    514   1.4  christos /**
    515   1.4  christos ***  CIPHER TESTS
    516   1.4  christos **/
    517   1.2  christos 
    518   1.4  christos typedef struct cipher_data_st {
    519   1.4  christos     const EVP_CIPHER *cipher;
    520  1.10  christos     EVP_CIPHER *fetched_cipher;
    521   1.4  christos     int enc;
    522   1.4  christos     /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
    523   1.4  christos     int aead;
    524   1.4  christos     unsigned char *key;
    525   1.4  christos     size_t key_len;
    526  1.10  christos     size_t key_bits; /* Used by RC2 */
    527   1.4  christos     unsigned char *iv;
    528  1.10  christos     unsigned char *next_iv; /* Expected IV state after operation */
    529  1.10  christos     unsigned int rounds;
    530   1.4  christos     size_t iv_len;
    531   1.4  christos     unsigned char *plaintext;
    532   1.4  christos     size_t plaintext_len;
    533   1.4  christos     unsigned char *ciphertext;
    534   1.4  christos     size_t ciphertext_len;
    535  1.10  christos     /* AEAD ciphers only */
    536  1.10  christos     unsigned char *aad[AAD_NUM];
    537  1.10  christos     size_t aad_len[AAD_NUM];
    538  1.10  christos     int tls_aad;
    539  1.10  christos     int tls_version;
    540   1.4  christos     unsigned char *tag;
    541  1.10  christos     const char *cts_mode;
    542   1.4  christos     size_t tag_len;
    543   1.7  christos     int tag_late;
    544  1.10  christos     unsigned char *mac_key;
    545  1.10  christos     size_t mac_key_len;
    546   1.4  christos } CIPHER_DATA;
    547   1.2  christos 
    548   1.4  christos static int cipher_test_init(EVP_TEST *t, const char *alg)
    549   1.2  christos {
    550   1.4  christos     const EVP_CIPHER *cipher;
    551  1.10  christos     EVP_CIPHER *fetched_cipher;
    552   1.4  christos     CIPHER_DATA *cdat;
    553   1.4  christos     int m;
    554   1.2  christos 
    555  1.10  christos     if (is_cipher_disabled(alg)) {
    556  1.10  christos         t->skip = 1;
    557  1.10  christos         TEST_info("skipping, '%s' is disabled", alg);
    558  1.10  christos         return 1;
    559  1.10  christos     }
    560  1.10  christos 
    561  1.10  christos     ERR_set_mark();
    562  1.10  christos     if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
    563  1.10  christos         && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
    564  1.10  christos         /* a stitched cipher might not be available */
    565  1.10  christos         if (strstr(alg, "HMAC") != NULL) {
    566  1.10  christos             ERR_pop_to_mark();
    567   1.4  christos             t->skip = 1;
    568  1.10  christos             TEST_info("skipping, '%s' is not available", alg);
    569   1.2  christos             return 1;
    570   1.2  christos         }
    571  1.10  christos         ERR_clear_last_mark();
    572   1.2  christos         return 0;
    573   1.2  christos     }
    574  1.10  christos     ERR_clear_last_mark();
    575  1.10  christos 
    576  1.10  christos     if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat))))
    577  1.10  christos         return 0;
    578  1.10  christos 
    579   1.2  christos     cdat->cipher = cipher;
    580  1.10  christos     cdat->fetched_cipher = fetched_cipher;
    581   1.2  christos     cdat->enc = -1;
    582  1.10  christos     m = EVP_CIPHER_get_mode(cipher);
    583  1.10  christos     if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
    584  1.10  christos         cdat->aead = m != 0 ? m : -1;
    585   1.2  christos     else
    586   1.2  christos         cdat->aead = 0;
    587   1.2  christos 
    588   1.4  christos     t->data = cdat;
    589  1.10  christos     if (fetched_cipher != NULL)
    590  1.10  christos         TEST_info("%s is fetched", alg);
    591   1.2  christos     return 1;
    592   1.2  christos }
    593   1.2  christos 
    594   1.4  christos static void cipher_test_cleanup(EVP_TEST *t)
    595   1.2  christos {
    596  1.10  christos     int i;
    597   1.4  christos     CIPHER_DATA *cdat = t->data;
    598   1.4  christos 
    599   1.4  christos     OPENSSL_free(cdat->key);
    600   1.4  christos     OPENSSL_free(cdat->iv);
    601  1.10  christos     OPENSSL_free(cdat->next_iv);
    602   1.4  christos     OPENSSL_free(cdat->ciphertext);
    603   1.4  christos     OPENSSL_free(cdat->plaintext);
    604  1.10  christos     for (i = 0; i < AAD_NUM; i++)
    605  1.10  christos         OPENSSL_free(cdat->aad[i]);
    606   1.4  christos     OPENSSL_free(cdat->tag);
    607  1.10  christos     OPENSSL_free(cdat->mac_key);
    608  1.10  christos     EVP_CIPHER_free(cdat->fetched_cipher);
    609   1.2  christos }
    610   1.2  christos 
    611   1.4  christos static int cipher_test_parse(EVP_TEST *t, const char *keyword,
    612   1.2  christos                              const char *value)
    613   1.2  christos {
    614   1.4  christos     CIPHER_DATA *cdat = t->data;
    615  1.10  christos     int i;
    616   1.4  christos 
    617   1.2  christos     if (strcmp(keyword, "Key") == 0)
    618   1.4  christos         return parse_bin(value, &cdat->key, &cdat->key_len);
    619  1.10  christos     if (strcmp(keyword, "Rounds") == 0) {
    620  1.10  christos         i = atoi(value);
    621  1.10  christos         if (i < 0)
    622  1.10  christos             return -1;
    623  1.10  christos         cdat->rounds = (unsigned int)i;
    624  1.10  christos         return 1;
    625  1.10  christos     }
    626   1.2  christos     if (strcmp(keyword, "IV") == 0)
    627   1.4  christos         return parse_bin(value, &cdat->iv, &cdat->iv_len);
    628  1.10  christos     if (strcmp(keyword, "NextIV") == 0)
    629  1.10  christos         return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
    630   1.2  christos     if (strcmp(keyword, "Plaintext") == 0)
    631   1.4  christos         return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
    632   1.2  christos     if (strcmp(keyword, "Ciphertext") == 0)
    633   1.4  christos         return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
    634  1.10  christos     if (strcmp(keyword, "KeyBits") == 0) {
    635  1.10  christos         i = atoi(value);
    636  1.10  christos         if (i < 0)
    637  1.10  christos             return -1;
    638  1.10  christos         cdat->key_bits = (size_t)i;
    639  1.10  christos         return 1;
    640  1.10  christos     }
    641   1.2  christos     if (cdat->aead) {
    642  1.10  christos         int tls_aad = 0;
    643  1.10  christos 
    644  1.10  christos         if (strcmp(keyword, "TLSAAD") == 0)
    645  1.10  christos             cdat->tls_aad = tls_aad = 1;
    646  1.10  christos         if (strcmp(keyword, "AAD") == 0 || tls_aad) {
    647  1.10  christos             for (i = 0; i < AAD_NUM; i++) {
    648  1.10  christos                 if (cdat->aad[i] == NULL)
    649  1.10  christos                     return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
    650  1.10  christos             }
    651  1.10  christos             return -1;
    652  1.10  christos         }
    653   1.2  christos         if (strcmp(keyword, "Tag") == 0)
    654   1.4  christos             return parse_bin(value, &cdat->tag, &cdat->tag_len);
    655   1.7  christos         if (strcmp(keyword, "SetTagLate") == 0) {
    656   1.7  christos             if (strcmp(value, "TRUE") == 0)
    657   1.7  christos                 cdat->tag_late = 1;
    658   1.7  christos             else if (strcmp(value, "FALSE") == 0)
    659   1.7  christos                 cdat->tag_late = 0;
    660   1.7  christos             else
    661   1.8  christos                 return -1;
    662   1.7  christos             return 1;
    663   1.7  christos         }
    664  1.10  christos         if (strcmp(keyword, "MACKey") == 0)
    665  1.10  christos             return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
    666  1.10  christos         if (strcmp(keyword, "TLSVersion") == 0) {
    667  1.10  christos             char *endptr;
    668  1.10  christos 
    669  1.10  christos             cdat->tls_version = (int)strtol(value, &endptr, 0);
    670  1.10  christos             return value[0] != '\0' && endptr[0] == '\0';
    671  1.10  christos         }
    672   1.2  christos     }
    673   1.2  christos 
    674   1.2  christos     if (strcmp(keyword, "Operation") == 0) {
    675   1.2  christos         if (strcmp(value, "ENCRYPT") == 0)
    676   1.2  christos             cdat->enc = 1;
    677   1.2  christos         else if (strcmp(value, "DECRYPT") == 0)
    678   1.2  christos             cdat->enc = 0;
    679   1.2  christos         else
    680   1.8  christos             return -1;
    681   1.2  christos         return 1;
    682   1.2  christos     }
    683  1.10  christos     if (strcmp(keyword, "CTSMode") == 0) {
    684  1.10  christos         cdat->cts_mode = value;
    685  1.10  christos         return 1;
    686  1.10  christos     }
    687   1.2  christos     return 0;
    688   1.2  christos }
    689   1.2  christos 
    690   1.4  christos static int cipher_test_enc(EVP_TEST *t, int enc,
    691   1.2  christos                            size_t out_misalign, size_t inp_misalign, int frag)
    692   1.2  christos {
    693   1.4  christos     CIPHER_DATA *expected = t->data;
    694   1.4  christos     unsigned char *in, *expected_out, *tmp = NULL;
    695   1.2  christos     size_t in_len, out_len, donelen = 0;
    696  1.10  christos     int ok = 0, tmplen, chunklen, tmpflen, i;
    697  1.10  christos     EVP_CIPHER_CTX *ctx_base = NULL;
    698   1.2  christos     EVP_CIPHER_CTX *ctx = NULL;
    699  1.12  christos     int fips_dupctx_supported = (fips_provider_version_gt(libctx, 3, 0, 12)
    700  1.12  christos                                 && fips_provider_version_lt(libctx, 3, 1, 0))
    701  1.12  christos                                 || fips_provider_version_ge(libctx, 3, 1, 3);
    702   1.4  christos 
    703   1.4  christos     t->err = "TEST_FAILURE";
    704  1.10  christos     if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
    705  1.10  christos         goto err;
    706   1.4  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
    707   1.2  christos         goto err;
    708  1.10  christos     EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
    709   1.2  christos     if (enc) {
    710   1.4  christos         in = expected->plaintext;
    711   1.4  christos         in_len = expected->plaintext_len;
    712   1.4  christos         expected_out = expected->ciphertext;
    713   1.4  christos         out_len = expected->ciphertext_len;
    714   1.2  christos     } else {
    715   1.4  christos         in = expected->ciphertext;
    716   1.4  christos         in_len = expected->ciphertext_len;
    717   1.4  christos         expected_out = expected->plaintext;
    718   1.4  christos         out_len = expected->plaintext_len;
    719   1.2  christos     }
    720   1.2  christos     if (inp_misalign == (size_t)-1) {
    721  1.10  christos         /* Exercise in-place encryption */
    722   1.2  christos         tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
    723   1.2  christos         if (!tmp)
    724   1.2  christos             goto err;
    725   1.2  christos         in = memcpy(tmp + out_misalign, in, in_len);
    726   1.2  christos     } else {
    727   1.2  christos         inp_misalign += 16 - ((out_misalign + in_len) & 15);
    728   1.2  christos         /*
    729   1.2  christos          * 'tmp' will store both output and copy of input. We make the copy
    730   1.2  christos          * of input to specifically aligned part of 'tmp'. So we just
    731   1.2  christos          * figured out how much padding would ensure the required alignment,
    732   1.2  christos          * now we allocate extended buffer and finally copy the input just
    733   1.2  christos          * past inp_misalign in expression below. Output will be written
    734   1.2  christos          * past out_misalign...
    735   1.2  christos          */
    736   1.2  christos         tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
    737   1.2  christos                              inp_misalign + in_len);
    738   1.2  christos         if (!tmp)
    739   1.2  christos             goto err;
    740   1.2  christos         in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
    741   1.2  christos                     inp_misalign, in, in_len);
    742   1.2  christos     }
    743  1.10  christos     if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
    744   1.4  christos         t->err = "CIPHERINIT_ERROR";
    745   1.2  christos         goto err;
    746   1.4  christos     }
    747  1.10  christos     if (expected->cts_mode != NULL) {
    748  1.10  christos         OSSL_PARAM params[2];
    749  1.10  christos 
    750  1.10  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
    751  1.10  christos                                                      (char *)(intptr_t)expected->cts_mode,
    752  1.10  christos                                                      0);
    753  1.10  christos         params[1] = OSSL_PARAM_construct_end();
    754  1.10  christos         if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
    755  1.10  christos             t->err = "INVALID_CTS_MODE";
    756  1.10  christos             goto err;
    757  1.10  christos         }
    758  1.10  christos     }
    759   1.4  christos     if (expected->iv) {
    760   1.4  christos         if (expected->aead) {
    761  1.10  christos             if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
    762  1.10  christos                                      expected->iv_len, 0) <= 0) {
    763   1.4  christos                 t->err = "INVALID_IV_LENGTH";
    764   1.2  christos                 goto err;
    765   1.4  christos             }
    766  1.10  christos         } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
    767   1.4  christos             t->err = "INVALID_IV_LENGTH";
    768   1.2  christos             goto err;
    769   1.4  christos         }
    770   1.2  christos     }
    771  1.10  christos     if (expected->aead && !expected->tls_aad) {
    772   1.2  christos         unsigned char *tag;
    773   1.2  christos         /*
    774   1.2  christos          * If encrypting or OCB just set tag length initially, otherwise
    775   1.2  christos          * set tag length and value.
    776   1.2  christos          */
    777   1.7  christos         if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
    778   1.4  christos             t->err = "TAG_LENGTH_SET_ERROR";
    779   1.2  christos             tag = NULL;
    780   1.2  christos         } else {
    781   1.4  christos             t->err = "TAG_SET_ERROR";
    782   1.4  christos             tag = expected->tag;
    783   1.2  christos         }
    784   1.4  christos         if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
    785  1.10  christos             if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
    786  1.10  christos                                      expected->tag_len, tag) <= 0)
    787   1.2  christos                 goto err;
    788   1.2  christos         }
    789   1.2  christos     }
    790   1.2  christos 
    791  1.10  christos     if (expected->rounds > 0) {
    792  1.10  christos         int  rounds = (int)expected->rounds;
    793  1.10  christos 
    794  1.10  christos         if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL) <= 0) {
    795  1.10  christos             t->err = "INVALID_ROUNDS";
    796  1.10  christos             goto err;
    797  1.10  christos         }
    798  1.10  christos     }
    799  1.10  christos 
    800  1.10  christos     if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
    801   1.4  christos         t->err = "INVALID_KEY_LENGTH";
    802   1.2  christos         goto err;
    803   1.4  christos     }
    804  1.10  christos     if (expected->key_bits > 0) {
    805  1.10  christos         int bits = (int)expected->key_bits;
    806  1.10  christos 
    807  1.10  christos         if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL) <= 0) {
    808  1.10  christos             t->err = "INVALID KEY BITS";
    809  1.10  christos             goto err;
    810  1.10  christos         }
    811  1.10  christos     }
    812  1.10  christos     if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
    813   1.4  christos         t->err = "KEY_SET_ERROR";
    814   1.2  christos         goto err;
    815   1.4  christos     }
    816   1.2  christos 
    817  1.10  christos     /* Check that we get the same IV back */
    818  1.10  christos     if (expected->iv != NULL) {
    819  1.10  christos         /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
    820  1.10  christos         unsigned char iv[128];
    821  1.10  christos         if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
    822  1.10  christos             || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
    823  1.10  christos                 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
    824  1.10  christos                                 expected->iv_len))) {
    825  1.10  christos             t->err = "INVALID_IV";
    826  1.10  christos             goto err;
    827  1.10  christos         }
    828  1.10  christos     }
    829  1.10  christos 
    830  1.10  christos     /* Test that the cipher dup functions correctly if it is supported */
    831  1.10  christos     ERR_set_mark();
    832  1.12  christos     if (!EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
    833  1.12  christos         if (fips_dupctx_supported) {
    834  1.12  christos             TEST_info("Doing a copy of Cipher %s Fails!\n",
    835  1.12  christos                       EVP_CIPHER_get0_name(expected->cipher));
    836  1.12  christos             ERR_print_errors_fp(stderr);
    837  1.12  christos             goto err;
    838  1.12  christos         } else {
    839  1.12  christos             TEST_info("Allowing copy fail as an old fips provider is in use.");
    840  1.12  christos         }
    841  1.12  christos         EVP_CIPHER_CTX_free(ctx);
    842  1.12  christos         ctx = ctx_base;
    843  1.12  christos     } else {
    844  1.10  christos         EVP_CIPHER_CTX_free(ctx_base);
    845  1.10  christos         ctx_base = NULL;
    846  1.10  christos     }
    847  1.10  christos     ERR_pop_to_mark();
    848  1.10  christos 
    849  1.10  christos     if (expected->mac_key != NULL
    850  1.10  christos         && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
    851  1.10  christos                                 (int)expected->mac_key_len,
    852  1.10  christos                                 (void *)expected->mac_key) <= 0) {
    853  1.10  christos         t->err = "SET_MAC_KEY_ERROR";
    854  1.10  christos         goto err;
    855  1.10  christos     }
    856  1.10  christos 
    857  1.10  christos     if (expected->tls_version) {
    858  1.10  christos         OSSL_PARAM params[2];
    859  1.10  christos 
    860  1.10  christos         params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
    861  1.10  christos                                              &expected->tls_version);
    862  1.10  christos         params[1] = OSSL_PARAM_construct_end();
    863  1.10  christos         if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
    864  1.10  christos             t->err = "SET_TLS_VERSION_ERROR";
    865  1.10  christos             goto err;
    866  1.10  christos         }
    867  1.10  christos     }
    868  1.10  christos 
    869   1.4  christos     if (expected->aead == EVP_CIPH_CCM_MODE) {
    870   1.2  christos         if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
    871   1.4  christos             t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
    872   1.2  christos             goto err;
    873   1.2  christos         }
    874   1.2  christos     }
    875  1.10  christos     if (expected->aad[0] != NULL && !expected->tls_aad) {
    876   1.4  christos         t->err = "AAD_SET_ERROR";
    877   1.2  christos         if (!frag) {
    878  1.10  christos             for (i = 0; expected->aad[i] != NULL; i++) {
    879  1.10  christos                 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
    880  1.10  christos                                       expected->aad_len[i]))
    881  1.10  christos                     goto err;
    882  1.10  christos             }
    883   1.2  christos         } else {
    884   1.2  christos             /*
    885   1.2  christos              * Supply the AAD in chunks less than the block size where possible
    886   1.2  christos              */
    887  1.10  christos             for (i = 0; expected->aad[i] != NULL; i++) {
    888  1.10  christos                 if (expected->aad_len[i] > 0) {
    889  1.10  christos                     if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
    890  1.10  christos                         goto err;
    891  1.10  christos                     donelen++;
    892  1.10  christos                 }
    893  1.10  christos                 if (expected->aad_len[i] > 2) {
    894  1.10  christos                     if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
    895  1.10  christos                                           expected->aad[i] + donelen,
    896  1.10  christos                                           expected->aad_len[i] - 2))
    897  1.10  christos                         goto err;
    898  1.10  christos                     donelen += expected->aad_len[i] - 2;
    899  1.10  christos                 }
    900  1.10  christos                 if (expected->aad_len[i] > 1
    901  1.10  christos                     && !EVP_CipherUpdate(ctx, NULL, &chunklen,
    902  1.10  christos                                          expected->aad[i] + donelen, 1))
    903   1.2  christos                     goto err;
    904   1.2  christos             }
    905   1.2  christos         }
    906   1.2  christos     }
    907   1.7  christos 
    908  1.10  christos     if (expected->tls_aad) {
    909  1.10  christos         OSSL_PARAM params[2];
    910  1.10  christos         char *tls_aad;
    911  1.10  christos 
    912  1.10  christos         /* duplicate the aad as the implementation might modify it */
    913  1.10  christos         if ((tls_aad = OPENSSL_memdup(expected->aad[0],
    914  1.10  christos                                       expected->aad_len[0])) == NULL)
    915  1.10  christos             goto err;
    916  1.10  christos         params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
    917  1.10  christos                                                       tls_aad,
    918  1.10  christos                                                       expected->aad_len[0]);
    919  1.10  christos         params[1] = OSSL_PARAM_construct_end();
    920  1.10  christos         if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
    921  1.10  christos             OPENSSL_free(tls_aad);
    922  1.10  christos             t->err = "TLS1_AAD_ERROR";
    923  1.10  christos             goto err;
    924  1.10  christos         }
    925  1.10  christos         OPENSSL_free(tls_aad);
    926  1.10  christos     } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
    927  1.10  christos                         || expected->tag_late)) {
    928  1.10  christos         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
    929  1.10  christos                                  expected->tag_len, expected->tag) <= 0) {
    930   1.7  christos             t->err = "TAG_SET_ERROR";
    931   1.7  christos             goto err;
    932   1.7  christos         }
    933   1.7  christos     }
    934   1.7  christos 
    935   1.2  christos     EVP_CIPHER_CTX_set_padding(ctx, 0);
    936   1.4  christos     t->err = "CIPHERUPDATE_ERROR";
    937   1.2  christos     tmplen = 0;
    938   1.2  christos     if (!frag) {
    939   1.2  christos         /* We supply the data all in one go */
    940   1.2  christos         if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
    941   1.2  christos             goto err;
    942   1.2  christos     } else {
    943   1.2  christos         /* Supply the data in chunks less than the block size where possible */
    944   1.2  christos         if (in_len > 0) {
    945   1.2  christos             if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
    946   1.2  christos                 goto err;
    947   1.2  christos             tmplen += chunklen;
    948   1.2  christos             in++;
    949   1.2  christos             in_len--;
    950   1.2  christos         }
    951   1.2  christos         if (in_len > 1) {
    952   1.2  christos             if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
    953   1.2  christos                                   in, in_len - 1))
    954   1.2  christos                 goto err;
    955   1.2  christos             tmplen += chunklen;
    956   1.2  christos             in += in_len - 1;
    957   1.2  christos             in_len = 1;
    958   1.2  christos         }
    959   1.2  christos         if (in_len > 0 ) {
    960   1.2  christos             if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
    961   1.2  christos                                   in, 1))
    962   1.2  christos                 goto err;
    963   1.2  christos             tmplen += chunklen;
    964   1.2  christos         }
    965   1.2  christos     }
    966   1.4  christos     if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
    967   1.4  christos         t->err = "CIPHERFINAL_ERROR";
    968   1.4  christos         goto err;
    969   1.2  christos     }
    970  1.10  christos     if (!enc && expected->tls_aad) {
    971  1.10  christos         if (expected->tls_version >= TLS1_1_VERSION
    972  1.10  christos             && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
    973  1.10  christos                 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
    974  1.10  christos             tmplen -= expected->iv_len;
    975  1.10  christos             expected_out += expected->iv_len;
    976  1.10  christos             out_misalign += expected->iv_len;
    977  1.10  christos         }
    978  1.10  christos         if ((int)out_len > tmplen + tmpflen)
    979  1.10  christos             out_len = tmplen + tmpflen;
    980  1.10  christos     }
    981   1.4  christos     if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
    982   1.4  christos                             tmp + out_misalign, tmplen + tmpflen))
    983   1.2  christos         goto err;
    984  1.10  christos     if (enc && expected->aead && !expected->tls_aad) {
    985   1.2  christos         unsigned char rtag[16];
    986   1.4  christos 
    987   1.4  christos         if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
    988   1.4  christos             t->err = "TAG_LENGTH_INTERNAL_ERROR";
    989   1.2  christos             goto err;
    990   1.2  christos         }
    991  1.10  christos         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
    992  1.10  christos                                  expected->tag_len, rtag) <= 0) {
    993   1.4  christos             t->err = "TAG_RETRIEVE_ERROR";
    994   1.2  christos             goto err;
    995   1.2  christos         }
    996   1.4  christos         if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
    997   1.4  christos                                 expected->tag, expected->tag_len,
    998   1.4  christos                                 rtag, expected->tag_len))
    999   1.2  christos             goto err;
   1000   1.2  christos     }
   1001  1.10  christos     /* Check the updated IV */
   1002  1.10  christos     if (expected->next_iv != NULL) {
   1003  1.10  christos         /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
   1004  1.10  christos         unsigned char iv[128];
   1005  1.10  christos         if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
   1006  1.10  christos             || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
   1007  1.10  christos                 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
   1008  1.10  christos                                 expected->iv_len))) {
   1009  1.10  christos             t->err = "INVALID_NEXT_IV";
   1010  1.10  christos             goto err;
   1011  1.10  christos         }
   1012  1.10  christos     }
   1013  1.10  christos 
   1014   1.4  christos     t->err = NULL;
   1015   1.4  christos     ok = 1;
   1016   1.2  christos  err:
   1017   1.2  christos     OPENSSL_free(tmp);
   1018  1.10  christos     if (ctx != ctx_base)
   1019  1.10  christos         EVP_CIPHER_CTX_free(ctx_base);
   1020   1.2  christos     EVP_CIPHER_CTX_free(ctx);
   1021   1.4  christos     return ok;
   1022   1.2  christos }
   1023   1.2  christos 
   1024   1.4  christos static int cipher_test_run(EVP_TEST *t)
   1025   1.2  christos {
   1026   1.4  christos     CIPHER_DATA *cdat = t->data;
   1027   1.2  christos     int rv, frag = 0;
   1028   1.2  christos     size_t out_misalign, inp_misalign;
   1029   1.2  christos 
   1030  1.12  christos     TEST_info("RUNNING TEST FOR CIPHER %s\n", EVP_CIPHER_get0_name(cdat->cipher));
   1031   1.2  christos     if (!cdat->key) {
   1032   1.2  christos         t->err = "NO_KEY";
   1033   1.2  christos         return 0;
   1034   1.2  christos     }
   1035  1.10  christos     if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
   1036   1.2  christos         /* IV is optional and usually omitted in wrap mode */
   1037  1.10  christos         if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
   1038   1.2  christos             t->err = "NO_IV";
   1039   1.2  christos             return 0;
   1040   1.2  christos         }
   1041   1.2  christos     }
   1042  1.10  christos     if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
   1043   1.2  christos         t->err = "NO_TAG";
   1044   1.2  christos         return 0;
   1045   1.2  christos     }
   1046   1.2  christos     for (out_misalign = 0; out_misalign <= 1;) {
   1047   1.2  christos         static char aux_err[64];
   1048   1.2  christos         t->aux_err = aux_err;
   1049   1.2  christos         for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
   1050   1.2  christos             if (inp_misalign == (size_t)-1) {
   1051   1.2  christos                 /* kludge: inp_misalign == -1 means "exercise in-place" */
   1052   1.2  christos                 BIO_snprintf(aux_err, sizeof(aux_err),
   1053   1.2  christos                              "%s in-place, %sfragmented",
   1054   1.2  christos                              out_misalign ? "misaligned" : "aligned",
   1055   1.2  christos                              frag ? "" : "not ");
   1056   1.2  christos             } else {
   1057   1.2  christos                 BIO_snprintf(aux_err, sizeof(aux_err),
   1058   1.2  christos                              "%s output and %s input, %sfragmented",
   1059   1.2  christos                              out_misalign ? "misaligned" : "aligned",
   1060   1.2  christos                              inp_misalign ? "misaligned" : "aligned",
   1061   1.2  christos                              frag ? "" : "not ");
   1062   1.2  christos             }
   1063   1.2  christos             if (cdat->enc) {
   1064   1.2  christos                 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
   1065   1.2  christos                 /* Not fatal errors: return */
   1066   1.2  christos                 if (rv != 1) {
   1067   1.2  christos                     if (rv < 0)
   1068   1.2  christos                         return 0;
   1069   1.2  christos                     return 1;
   1070   1.2  christos                 }
   1071   1.2  christos             }
   1072   1.2  christos             if (cdat->enc != 1) {
   1073   1.2  christos                 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
   1074   1.2  christos                 /* Not fatal errors: return */
   1075   1.2  christos                 if (rv != 1) {
   1076   1.2  christos                     if (rv < 0)
   1077   1.2  christos                         return 0;
   1078   1.2  christos                     return 1;
   1079   1.2  christos                 }
   1080   1.2  christos             }
   1081   1.2  christos         }
   1082   1.2  christos 
   1083   1.2  christos         if (out_misalign == 1 && frag == 0) {
   1084   1.2  christos             /*
   1085  1.10  christos              * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
   1086  1.10  christos              * requirements about input lengths so we don't fragment for those
   1087   1.2  christos              */
   1088   1.2  christos             if (cdat->aead == EVP_CIPH_CCM_MODE
   1089  1.10  christos                 || cdat->aead == EVP_CIPH_CBC_MODE
   1090  1.10  christos                 || (cdat->aead == -1
   1091  1.10  christos                     && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
   1092  1.10  christos                 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
   1093  1.10  christos                 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
   1094  1.10  christos                 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
   1095  1.10  christos                 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
   1096   1.2  christos                 break;
   1097   1.2  christos             out_misalign = 0;
   1098   1.2  christos             frag++;
   1099   1.2  christos         } else {
   1100   1.2  christos             out_misalign++;
   1101   1.2  christos         }
   1102   1.2  christos     }
   1103   1.2  christos     t->aux_err = NULL;
   1104   1.2  christos 
   1105   1.2  christos     return 1;
   1106   1.2  christos }
   1107   1.2  christos 
   1108   1.4  christos static const EVP_TEST_METHOD cipher_test_method = {
   1109   1.2  christos     "Cipher",
   1110   1.2  christos     cipher_test_init,
   1111   1.2  christos     cipher_test_cleanup,
   1112   1.2  christos     cipher_test_parse,
   1113   1.2  christos     cipher_test_run
   1114   1.2  christos };
   1115   1.2  christos 
   1116   1.4  christos 
   1117   1.4  christos /**
   1118  1.10  christos  **  MAC TESTS
   1119  1.10  christos  **/
   1120   1.4  christos 
   1121   1.4  christos typedef struct mac_data_st {
   1122  1.10  christos     /* MAC type in one form or another */
   1123  1.10  christos     char *mac_name;
   1124  1.10  christos     EVP_MAC *mac;                /* for mac_test_run_mac */
   1125  1.10  christos     int type;                    /* for mac_test_run_pkey */
   1126   1.2  christos     /* Algorithm string for this MAC */
   1127   1.2  christos     char *alg;
   1128   1.2  christos     /* MAC key */
   1129   1.2  christos     unsigned char *key;
   1130   1.2  christos     size_t key_len;
   1131  1.10  christos     /* MAC IV (GMAC) */
   1132  1.10  christos     unsigned char *iv;
   1133  1.10  christos     size_t iv_len;
   1134   1.2  christos     /* Input to MAC */
   1135   1.2  christos     unsigned char *input;
   1136   1.2  christos     size_t input_len;
   1137   1.2  christos     /* Expected output */
   1138   1.2  christos     unsigned char *output;
   1139   1.2  christos     size_t output_len;
   1140  1.10  christos     unsigned char *custom;
   1141  1.10  christos     size_t custom_len;
   1142  1.10  christos     /* MAC salt (blake2) */
   1143  1.10  christos     unsigned char *salt;
   1144  1.10  christos     size_t salt_len;
   1145  1.10  christos     /* XOF mode? */
   1146  1.10  christos     int xof;
   1147  1.10  christos     /* Reinitialization fails */
   1148  1.10  christos     int no_reinit;
   1149   1.4  christos     /* Collection of controls */
   1150   1.4  christos     STACK_OF(OPENSSL_STRING) *controls;
   1151  1.10  christos     /* Output size */
   1152  1.10  christos     int output_size;
   1153  1.10  christos     /* Block size */
   1154  1.10  christos     int block_size;
   1155   1.4  christos } MAC_DATA;
   1156   1.2  christos 
   1157   1.4  christos static int mac_test_init(EVP_TEST *t, const char *alg)
   1158   1.2  christos {
   1159  1.10  christos     EVP_MAC *mac = NULL;
   1160  1.10  christos     int type = NID_undef;
   1161   1.4  christos     MAC_DATA *mdat;
   1162   1.4  christos 
   1163  1.10  christos     if (is_mac_disabled(alg)) {
   1164  1.10  christos         TEST_info("skipping, '%s' is disabled", alg);
   1165   1.2  christos         t->skip = 1;
   1166   1.2  christos         return 1;
   1167  1.10  christos     }
   1168  1.10  christos     if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
   1169  1.10  christos         /*
   1170  1.10  christos          * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
   1171  1.10  christos          * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
   1172  1.10  christos          * the EVP_PKEY method.
   1173  1.10  christos          */
   1174  1.10  christos         size_t sz = strlen(alg);
   1175  1.10  christos         static const char epilogue[] = " by EVP_PKEY";
   1176  1.10  christos 
   1177  1.10  christos         if (sz >= sizeof(epilogue)
   1178  1.10  christos             && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
   1179  1.10  christos             sz -= sizeof(epilogue) - 1;
   1180  1.10  christos 
   1181  1.10  christos         if (strncmp(alg, "HMAC", sz) == 0)
   1182  1.10  christos             type = EVP_PKEY_HMAC;
   1183  1.10  christos         else if (strncmp(alg, "CMAC", sz) == 0)
   1184  1.10  christos             type = EVP_PKEY_CMAC;
   1185  1.10  christos         else if (strncmp(alg, "Poly1305", sz) == 0)
   1186  1.10  christos             type = EVP_PKEY_POLY1305;
   1187  1.10  christos         else if (strncmp(alg, "SipHash", sz) == 0)
   1188  1.10  christos             type = EVP_PKEY_SIPHASH;
   1189  1.10  christos         else
   1190  1.10  christos             return 0;
   1191  1.10  christos     }
   1192  1.10  christos 
   1193  1.10  christos     if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
   1194   1.2  christos         return 0;
   1195   1.2  christos 
   1196   1.2  christos     mdat->type = type;
   1197  1.10  christos     if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) {
   1198  1.10  christos         OPENSSL_free(mdat);
   1199  1.10  christos         return 0;
   1200  1.10  christos     }
   1201  1.10  christos 
   1202  1.10  christos     mdat->mac = mac;
   1203  1.10  christos     if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) {
   1204  1.10  christos         OPENSSL_free(mdat->mac_name);
   1205  1.10  christos         OPENSSL_free(mdat);
   1206  1.10  christos         return 0;
   1207  1.10  christos     }
   1208  1.10  christos 
   1209  1.10  christos     mdat->output_size = mdat->block_size = -1;
   1210   1.2  christos     t->data = mdat;
   1211   1.2  christos     return 1;
   1212   1.2  christos }
   1213   1.2  christos 
   1214   1.4  christos /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
   1215   1.4  christos static void openssl_free(char *m)
   1216   1.2  christos {
   1217   1.4  christos     OPENSSL_free(m);
   1218   1.4  christos }
   1219   1.4  christos 
   1220   1.4  christos static void mac_test_cleanup(EVP_TEST *t)
   1221   1.4  christos {
   1222   1.4  christos     MAC_DATA *mdat = t->data;
   1223   1.4  christos 
   1224  1.10  christos     EVP_MAC_free(mdat->mac);
   1225  1.10  christos     OPENSSL_free(mdat->mac_name);
   1226   1.4  christos     sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
   1227   1.4  christos     OPENSSL_free(mdat->alg);
   1228   1.4  christos     OPENSSL_free(mdat->key);
   1229  1.10  christos     OPENSSL_free(mdat->iv);
   1230  1.10  christos     OPENSSL_free(mdat->custom);
   1231  1.10  christos     OPENSSL_free(mdat->salt);
   1232   1.4  christos     OPENSSL_free(mdat->input);
   1233   1.4  christos     OPENSSL_free(mdat->output);
   1234   1.2  christos }
   1235   1.2  christos 
   1236   1.4  christos static int mac_test_parse(EVP_TEST *t,
   1237   1.2  christos                           const char *keyword, const char *value)
   1238   1.2  christos {
   1239   1.4  christos     MAC_DATA *mdata = t->data;
   1240   1.4  christos 
   1241   1.2  christos     if (strcmp(keyword, "Key") == 0)
   1242   1.4  christos         return parse_bin(value, &mdata->key, &mdata->key_len);
   1243  1.10  christos     if (strcmp(keyword, "IV") == 0)
   1244  1.10  christos         return parse_bin(value, &mdata->iv, &mdata->iv_len);
   1245  1.10  christos     if (strcmp(keyword, "Custom") == 0)
   1246  1.10  christos         return parse_bin(value, &mdata->custom, &mdata->custom_len);
   1247  1.10  christos     if (strcmp(keyword, "Salt") == 0)
   1248  1.10  christos         return parse_bin(value, &mdata->salt, &mdata->salt_len);
   1249   1.2  christos     if (strcmp(keyword, "Algorithm") == 0) {
   1250   1.2  christos         mdata->alg = OPENSSL_strdup(value);
   1251  1.10  christos         if (mdata->alg == NULL)
   1252   1.8  christos             return -1;
   1253   1.2  christos         return 1;
   1254   1.2  christos     }
   1255   1.2  christos     if (strcmp(keyword, "Input") == 0)
   1256   1.4  christos         return parse_bin(value, &mdata->input, &mdata->input_len);
   1257   1.2  christos     if (strcmp(keyword, "Output") == 0)
   1258   1.4  christos         return parse_bin(value, &mdata->output, &mdata->output_len);
   1259  1.10  christos     if (strcmp(keyword, "XOF") == 0)
   1260  1.10  christos         return mdata->xof = 1;
   1261  1.10  christos     if (strcmp(keyword, "NoReinit") == 0)
   1262  1.10  christos         return mdata->no_reinit = 1;
   1263  1.10  christos     if (strcmp(keyword, "Ctrl") == 0) {
   1264  1.10  christos         char *data = OPENSSL_strdup(value);
   1265  1.10  christos 
   1266  1.10  christos         if (data == NULL)
   1267  1.10  christos             return -1;
   1268  1.10  christos         return sk_OPENSSL_STRING_push(mdata->controls, data) != 0;
   1269  1.10  christos     }
   1270  1.10  christos     if (strcmp(keyword, "OutputSize") == 0) {
   1271  1.10  christos         mdata->output_size = atoi(value);
   1272  1.10  christos         if (mdata->output_size < 0)
   1273  1.10  christos             return -1;
   1274  1.10  christos         return 1;
   1275  1.10  christos     }
   1276  1.10  christos     if (strcmp(keyword, "BlockSize") == 0) {
   1277  1.10  christos         mdata->block_size = atoi(value);
   1278  1.10  christos         if (mdata->block_size < 0)
   1279  1.10  christos             return -1;
   1280  1.10  christos         return 1;
   1281  1.10  christos     }
   1282   1.2  christos     return 0;
   1283   1.2  christos }
   1284   1.2  christos 
   1285   1.5  christos static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
   1286   1.5  christos                               const char *value)
   1287   1.5  christos {
   1288  1.10  christos     int rv = 0;
   1289   1.5  christos     char *p, *tmpval;
   1290   1.5  christos 
   1291   1.5  christos     if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
   1292   1.5  christos         return 0;
   1293   1.5  christos     p = strchr(tmpval, ':');
   1294  1.10  christos     if (p != NULL) {
   1295   1.5  christos         *p++ = '\0';
   1296  1.10  christos         rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
   1297  1.10  christos     }
   1298   1.5  christos     if (rv == -2)
   1299   1.5  christos         t->err = "PKEY_CTRL_INVALID";
   1300   1.5  christos     else if (rv <= 0)
   1301   1.5  christos         t->err = "PKEY_CTRL_ERROR";
   1302   1.5  christos     else
   1303   1.5  christos         rv = 1;
   1304   1.5  christos     OPENSSL_free(tmpval);
   1305   1.5  christos     return rv > 0;
   1306   1.5  christos }
   1307   1.5  christos 
   1308  1.10  christos static int mac_test_run_pkey(EVP_TEST *t)
   1309   1.2  christos {
   1310   1.4  christos     MAC_DATA *expected = t->data;
   1311   1.2  christos     EVP_MD_CTX *mctx = NULL;
   1312   1.2  christos     EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
   1313   1.2  christos     EVP_PKEY *key = NULL;
   1314  1.10  christos     const char *mdname = NULL;
   1315  1.10  christos     EVP_CIPHER *cipher = NULL;
   1316   1.4  christos     unsigned char *got = NULL;
   1317   1.4  christos     size_t got_len;
   1318   1.4  christos     int i;
   1319   1.2  christos 
   1320  1.10  christos     /* We don't do XOF mode via PKEY */
   1321  1.10  christos     if (expected->xof)
   1322  1.10  christos         return 1;
   1323  1.10  christos 
   1324  1.10  christos     if (expected->alg == NULL)
   1325  1.10  christos         TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
   1326  1.10  christos     else
   1327  1.10  christos         TEST_info("Trying the EVP_PKEY %s test with %s",
   1328  1.10  christos                   OBJ_nid2sn(expected->type), expected->alg);
   1329  1.10  christos 
   1330  1.10  christos     if (expected->type == EVP_PKEY_CMAC) {
   1331  1.10  christos #ifdef OPENSSL_NO_DEPRECATED_3_0
   1332  1.10  christos         TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
   1333  1.10  christos         t->skip = 1;
   1334   1.4  christos         t->err = NULL;
   1335   1.2  christos         goto err;
   1336  1.10  christos #else
   1337  1.10  christos         OSSL_LIB_CTX *tmpctx;
   1338   1.2  christos 
   1339  1.10  christos         if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
   1340  1.10  christos             TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
   1341  1.10  christos             t->skip = 1;
   1342  1.10  christos             t->err = NULL;
   1343  1.10  christos             goto err;
   1344  1.10  christos         }
   1345  1.10  christos         if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
   1346  1.10  christos             t->err = "MAC_KEY_CREATE_ERROR";
   1347  1.10  christos             goto err;
   1348  1.10  christos         }
   1349  1.10  christos         tmpctx = OSSL_LIB_CTX_set0_default(libctx);
   1350   1.4  christos         key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
   1351  1.10  christos                                     cipher);
   1352  1.10  christos         OSSL_LIB_CTX_set0_default(tmpctx);
   1353  1.10  christos #endif
   1354  1.10  christos     } else {
   1355  1.10  christos         key = EVP_PKEY_new_raw_private_key_ex(libctx,
   1356  1.10  christos                                               OBJ_nid2sn(expected->type), NULL,
   1357  1.10  christos                                               expected->key, expected->key_len);
   1358  1.10  christos     }
   1359   1.4  christos     if (key == NULL) {
   1360   1.4  christos         t->err = "MAC_KEY_CREATE_ERROR";
   1361   1.2  christos         goto err;
   1362   1.4  christos     }
   1363   1.2  christos 
   1364  1.10  christos     if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
   1365  1.10  christos         if (is_digest_disabled(expected->alg)) {
   1366  1.10  christos             TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
   1367  1.10  christos             t->skip = 1;
   1368  1.10  christos             t->err = NULL;
   1369   1.2  christos             goto err;
   1370   1.4  christos         }
   1371  1.10  christos         mdname = expected->alg;
   1372   1.2  christos     }
   1373   1.4  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
   1374   1.4  christos         t->err = "INTERNAL_ERROR";
   1375   1.2  christos         goto err;
   1376   1.4  christos     }
   1377  1.10  christos     if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
   1378   1.4  christos         t->err = "DIGESTSIGNINIT_ERROR";
   1379   1.2  christos         goto err;
   1380   1.4  christos     }
   1381   1.4  christos     for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
   1382   1.5  christos         if (!mac_test_ctrl_pkey(t, pctx,
   1383   1.5  christos                                 sk_OPENSSL_STRING_value(expected->controls,
   1384   1.5  christos                                                         i))) {
   1385   1.4  christos             t->err = "EVPPKEYCTXCTRL_ERROR";
   1386   1.2  christos             goto err;
   1387   1.4  christos         }
   1388   1.4  christos     if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
   1389   1.4  christos         t->err = "DIGESTSIGNUPDATE_ERROR";
   1390   1.4  christos         goto err;
   1391   1.2  christos     }
   1392   1.4  christos     if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
   1393   1.4  christos         t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
   1394   1.2  christos         goto err;
   1395   1.4  christos     }
   1396   1.4  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
   1397   1.4  christos         t->err = "TEST_FAILURE";
   1398   1.2  christos         goto err;
   1399   1.4  christos     }
   1400   1.4  christos     if (!EVP_DigestSignFinal(mctx, got, &got_len)
   1401   1.4  christos             || !memory_err_compare(t, "TEST_MAC_ERR",
   1402   1.4  christos                                    expected->output, expected->output_len,
   1403   1.4  christos                                    got, got_len)) {
   1404   1.4  christos         t->err = "TEST_MAC_ERR";
   1405   1.2  christos         goto err;
   1406   1.2  christos     }
   1407   1.4  christos     t->err = NULL;
   1408   1.2  christos  err:
   1409  1.10  christos     EVP_CIPHER_free(cipher);
   1410   1.2  christos     EVP_MD_CTX_free(mctx);
   1411   1.4  christos     OPENSSL_free(got);
   1412   1.2  christos     EVP_PKEY_CTX_free(genctx);
   1413   1.2  christos     EVP_PKEY_free(key);
   1414   1.2  christos     return 1;
   1415   1.2  christos }
   1416   1.2  christos 
   1417  1.10  christos static int mac_test_run_mac(EVP_TEST *t)
   1418  1.10  christos {
   1419  1.10  christos     MAC_DATA *expected = t->data;
   1420  1.10  christos     EVP_MAC_CTX *ctx = NULL;
   1421  1.10  christos     unsigned char *got = NULL;
   1422  1.10  christos     size_t got_len = 0, size = 0;
   1423  1.12  christos     size_t size_before_init = 0, size_after_init, size_val = 0;
   1424  1.10  christos     int i, block_size = -1, output_size = -1;
   1425  1.10  christos     OSSL_PARAM params[21], sizes[3], *psizes = sizes;
   1426  1.10  christos     size_t params_n = 0;
   1427  1.10  christos     size_t params_n_allocstart = 0;
   1428  1.10  christos     const OSSL_PARAM *defined_params =
   1429  1.10  christos         EVP_MAC_settable_ctx_params(expected->mac);
   1430  1.10  christos     int xof;
   1431  1.10  christos     int reinit = 1;
   1432  1.10  christos 
   1433  1.10  christos     if (expected->alg == NULL)
   1434  1.10  christos         TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
   1435  1.10  christos     else
   1436  1.10  christos         TEST_info("Trying the EVP_MAC %s test with %s",
   1437  1.10  christos                   expected->mac_name, expected->alg);
   1438   1.2  christos 
   1439  1.10  christos     if (expected->alg != NULL) {
   1440  1.10  christos         int skip = 0;
   1441   1.2  christos 
   1442  1.10  christos         /*
   1443  1.10  christos          * The underlying algorithm may be a cipher or a digest.
   1444  1.10  christos          * We don't know which it is, but we can ask the MAC what it
   1445  1.10  christos          * should be and bet on that.
   1446  1.10  christos          */
   1447  1.10  christos         if (OSSL_PARAM_locate_const(defined_params,
   1448  1.10  christos                                     OSSL_MAC_PARAM_CIPHER) != NULL) {
   1449  1.10  christos             if (is_cipher_disabled(expected->alg))
   1450  1.10  christos                 skip = 1;
   1451  1.10  christos             else
   1452  1.10  christos                 params[params_n++] =
   1453  1.10  christos                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
   1454  1.10  christos                                                      expected->alg, 0);
   1455  1.10  christos         } else if (OSSL_PARAM_locate_const(defined_params,
   1456  1.10  christos                                            OSSL_MAC_PARAM_DIGEST) != NULL) {
   1457  1.10  christos             if (is_digest_disabled(expected->alg))
   1458  1.10  christos                 skip = 1;
   1459  1.10  christos             else
   1460  1.10  christos                 params[params_n++] =
   1461  1.10  christos                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
   1462  1.10  christos                                                      expected->alg, 0);
   1463  1.10  christos         } else {
   1464  1.10  christos             t->err = "MAC_BAD_PARAMS";
   1465  1.10  christos             goto err;
   1466  1.10  christos         }
   1467  1.10  christos         if (skip) {
   1468  1.10  christos             TEST_info("skipping, algorithm '%s' is disabled", expected->alg);
   1469  1.10  christos             t->skip = 1;
   1470  1.10  christos             t->err = NULL;
   1471  1.10  christos             goto err;
   1472  1.10  christos         }
   1473  1.10  christos     }
   1474  1.10  christos     if (expected->custom != NULL)
   1475  1.10  christos         params[params_n++] =
   1476  1.10  christos             OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
   1477  1.10  christos                                               expected->custom,
   1478  1.10  christos                                               expected->custom_len);
   1479  1.10  christos     if (expected->salt != NULL)
   1480  1.10  christos         params[params_n++] =
   1481  1.10  christos             OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
   1482  1.10  christos                                               expected->salt,
   1483  1.10  christos                                               expected->salt_len);
   1484  1.10  christos     if (expected->iv != NULL)
   1485  1.10  christos         params[params_n++] =
   1486  1.10  christos             OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
   1487  1.10  christos                                               expected->iv,
   1488  1.10  christos                                               expected->iv_len);
   1489  1.10  christos 
   1490  1.10  christos     /* Unknown controls.  They must match parameters that the MAC recognizes */
   1491  1.10  christos     if (params_n + sk_OPENSSL_STRING_num(expected->controls)
   1492  1.10  christos         >= OSSL_NELEM(params)) {
   1493  1.10  christos         t->err = "MAC_TOO_MANY_PARAMETERS";
   1494  1.10  christos         goto err;
   1495  1.10  christos     }
   1496  1.10  christos     params_n_allocstart = params_n;
   1497  1.10  christos     for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
   1498  1.10  christos         char *tmpkey, *tmpval;
   1499  1.10  christos         char *value = sk_OPENSSL_STRING_value(expected->controls, i);
   1500   1.4  christos 
   1501  1.10  christos         if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
   1502  1.10  christos             t->err = "MAC_PARAM_ERROR";
   1503  1.10  christos             goto err;
   1504  1.10  christos         }
   1505  1.10  christos         tmpval = strchr(tmpkey, ':');
   1506  1.10  christos         if (tmpval != NULL)
   1507  1.10  christos             *tmpval++ = '\0';
   1508  1.10  christos 
   1509  1.10  christos         if (tmpval == NULL
   1510  1.10  christos             || !OSSL_PARAM_allocate_from_text(&params[params_n],
   1511  1.10  christos                                               defined_params,
   1512  1.10  christos                                               tmpkey, tmpval,
   1513  1.10  christos                                               strlen(tmpval), NULL)) {
   1514  1.10  christos             OPENSSL_free(tmpkey);
   1515  1.10  christos             t->err = "MAC_PARAM_ERROR";
   1516  1.10  christos             goto err;
   1517  1.10  christos         }
   1518  1.10  christos         params_n++;
   1519   1.2  christos 
   1520  1.12  christos         if (strcmp(tmpkey, "size") == 0)
   1521  1.12  christos             size_val = (size_t)strtoul(tmpval, NULL, 0);
   1522  1.12  christos 
   1523  1.10  christos         OPENSSL_free(tmpkey);
   1524  1.10  christos     }
   1525  1.10  christos     params[params_n] = OSSL_PARAM_construct_end();
   1526   1.4  christos 
   1527  1.10  christos     if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
   1528  1.10  christos         t->err = "MAC_CREATE_ERROR";
   1529  1.10  christos         goto err;
   1530   1.2  christos     }
   1531  1.12  christos     if (fips_provider_version_gt(libctx, 3, 1, 4)
   1532  1.12  christos         || (fips_provider_version_lt(libctx, 3, 1, 0)
   1533  1.12  christos             && fips_provider_version_gt(libctx, 3, 0, 12)))
   1534  1.12  christos         size_before_init = EVP_MAC_CTX_get_mac_size(ctx);
   1535  1.10  christos     if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
   1536  1.10  christos         t->err = "MAC_INIT_ERROR";
   1537  1.10  christos         goto err;
   1538  1.10  christos     }
   1539  1.12  christos     size_after_init = EVP_MAC_CTX_get_mac_size(ctx);
   1540  1.12  christos     if (!TEST_false(size_before_init == 0 && size_after_init == 0)) {
   1541  1.12  christos         t->err = "MAC SIZE not set";
   1542  1.12  christos         goto err;
   1543  1.12  christos     }
   1544  1.12  christos     if (size_before_init != 0) {
   1545  1.12  christos         /* mac-size not modified by init params */
   1546  1.12  christos         if (size_val == 0 && !TEST_size_t_eq(size_before_init, size_after_init)) {
   1547  1.12  christos             t->err = "MAC SIZE check failed";
   1548  1.12  christos             goto err;
   1549  1.12  christos         }
   1550  1.12  christos         /* mac-size modified by init params */
   1551  1.12  christos         if (size_val != 0 && !TEST_size_t_eq(size_val, size_after_init)) {
   1552  1.12  christos             t->err = "MAC SIZE check failed";
   1553  1.12  christos             goto err;
   1554  1.12  christos         }
   1555  1.12  christos     }
   1556  1.10  christos     if (expected->output_size >= 0)
   1557  1.10  christos         *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
   1558  1.10  christos                                              &output_size);
   1559  1.10  christos     if (expected->block_size >= 0)
   1560  1.10  christos         *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
   1561  1.10  christos                                              &block_size);
   1562  1.10  christos     if (psizes != sizes) {
   1563  1.10  christos         *psizes = OSSL_PARAM_construct_end();
   1564  1.10  christos         if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
   1565  1.10  christos             t->err = "INTERNAL_ERROR";
   1566  1.10  christos             goto err;
   1567  1.10  christos         }
   1568  1.10  christos         if (expected->output_size >= 0
   1569  1.10  christos                 && !TEST_int_eq(output_size, expected->output_size)) {
   1570  1.10  christos             t->err = "TEST_FAILURE";
   1571  1.10  christos             goto err;
   1572  1.10  christos         }
   1573  1.10  christos         if (expected->block_size >= 0
   1574  1.10  christos                 && !TEST_int_eq(block_size, expected->block_size)) {
   1575  1.10  christos             t->err = "TEST_FAILURE";
   1576  1.10  christos             goto err;
   1577  1.10  christos         }
   1578  1.10  christos     }
   1579  1.10  christos  retry:
   1580  1.10  christos     if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
   1581  1.10  christos         t->err = "MAC_UPDATE_ERROR";
   1582  1.10  christos         goto err;
   1583  1.10  christos     }
   1584  1.10  christos     xof = expected->xof;
   1585  1.10  christos     if (xof) {
   1586  1.10  christos         if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
   1587  1.10  christos             t->err = "TEST_FAILURE";
   1588  1.10  christos             goto err;
   1589  1.10  christos         }
   1590  1.10  christos         if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
   1591  1.10  christos             || !memory_err_compare(t, "TEST_MAC_ERR",
   1592  1.10  christos                                    expected->output, expected->output_len,
   1593  1.10  christos                                    got, expected->output_len)) {
   1594  1.10  christos             t->err = "MAC_FINAL_ERROR";
   1595  1.10  christos             goto err;
   1596  1.10  christos         }
   1597  1.10  christos     } else {
   1598  1.10  christos         if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
   1599  1.10  christos             t->err = "MAC_FINAL_LENGTH_ERROR";
   1600  1.10  christos             goto err;
   1601  1.10  christos         }
   1602  1.10  christos         if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
   1603  1.10  christos             t->err = "TEST_FAILURE";
   1604  1.10  christos             goto err;
   1605  1.10  christos         }
   1606  1.10  christos         if (!EVP_MAC_final(ctx, got, &got_len, got_len)
   1607  1.10  christos             || !memory_err_compare(t, "TEST_MAC_ERR",
   1608  1.10  christos                                    expected->output, expected->output_len,
   1609  1.10  christos                                    got, got_len)) {
   1610  1.10  christos             t->err = "TEST_MAC_ERR";
   1611  1.10  christos             goto err;
   1612  1.10  christos         }
   1613  1.10  christos     }
   1614  1.10  christos     /* FIPS(3.0.0): can't reinitialise MAC contexts #18100 */
   1615  1.10  christos     if (reinit-- && fips_provider_version_gt(libctx, 3, 0, 0)) {
   1616  1.10  christos         OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
   1617  1.10  christos         int ret;
   1618  1.10  christos 
   1619  1.10  christos         /* If the MAC uses IV, we have to set it again */
   1620  1.10  christos         if (expected->iv != NULL) {
   1621  1.10  christos             ivparams[0] =
   1622  1.10  christos                 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
   1623  1.10  christos                                                   expected->iv,
   1624  1.10  christos                                                   expected->iv_len);
   1625  1.10  christos             ivparams[1] = OSSL_PARAM_construct_end();
   1626  1.10  christos         }
   1627  1.10  christos         ERR_set_mark();
   1628  1.10  christos         ret = EVP_MAC_init(ctx, NULL, 0, ivparams);
   1629  1.10  christos         if (expected->no_reinit) {
   1630  1.10  christos             if (ret) {
   1631  1.10  christos                 ERR_clear_last_mark();
   1632  1.10  christos                 t->err = "MAC_REINIT_SHOULD_FAIL";
   1633  1.10  christos                 goto err;
   1634  1.10  christos             }
   1635  1.10  christos         } else if (ret) {
   1636  1.10  christos             ERR_clear_last_mark();
   1637  1.10  christos             OPENSSL_free(got);
   1638  1.10  christos             got = NULL;
   1639  1.10  christos             goto retry;
   1640  1.10  christos         } else {
   1641  1.10  christos             ERR_clear_last_mark();
   1642  1.10  christos             t->err = "MAC_REINIT_ERROR";
   1643  1.10  christos             goto err;
   1644  1.10  christos         }
   1645  1.10  christos         /* If reinitialization fails, it is unsupported by the algorithm */
   1646  1.10  christos         ERR_pop_to_mark();
   1647  1.10  christos     }
   1648  1.10  christos     t->err = NULL;
   1649  1.10  christos 
   1650  1.10  christos     /* Test the EVP_Q_mac interface as well */
   1651  1.10  christos     if (!xof) {
   1652  1.10  christos         OPENSSL_cleanse(got, got_len);
   1653  1.10  christos         if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
   1654  1.10  christos                                  expected->alg, params,
   1655  1.10  christos                                  expected->key, expected->key_len,
   1656  1.10  christos                                  expected->input, expected->input_len,
   1657  1.10  christos                                  got, got_len, &size))
   1658  1.10  christos                 || !TEST_mem_eq(got, size,
   1659  1.10  christos                                 expected->output, expected->output_len)) {
   1660  1.10  christos             t->err = "EVP_Q_mac failed";
   1661  1.10  christos             goto err;
   1662  1.10  christos         }
   1663  1.10  christos     }
   1664  1.10  christos  err:
   1665  1.10  christos     while (params_n-- > params_n_allocstart) {
   1666  1.10  christos         OPENSSL_free(params[params_n].data);
   1667  1.10  christos     }
   1668  1.10  christos     EVP_MAC_CTX_free(ctx);
   1669  1.10  christos     OPENSSL_free(got);
   1670  1.10  christos     return 1;
   1671  1.10  christos }
   1672  1.10  christos 
   1673  1.10  christos static int mac_test_run(EVP_TEST *t)
   1674  1.10  christos {
   1675  1.10  christos     MAC_DATA *expected = t->data;
   1676  1.10  christos 
   1677  1.10  christos     if (expected->mac != NULL)
   1678  1.10  christos         return mac_test_run_mac(t);
   1679  1.10  christos     return mac_test_run_pkey(t);
   1680  1.10  christos }
   1681  1.10  christos 
   1682  1.10  christos static const EVP_TEST_METHOD mac_test_method = {
   1683  1.10  christos     "MAC",
   1684  1.10  christos     mac_test_init,
   1685  1.10  christos     mac_test_cleanup,
   1686  1.10  christos     mac_test_parse,
   1687  1.10  christos     mac_test_run
   1688  1.10  christos };
   1689  1.10  christos 
   1690  1.10  christos 
   1691  1.10  christos /**
   1692  1.10  christos  **  PUBLIC KEY TESTS
   1693  1.10  christos  **  These are all very similar and share much common code.
   1694  1.10  christos  **/
   1695  1.10  christos 
   1696  1.10  christos typedef struct pkey_data_st {
   1697  1.10  christos     /* Context for this operation */
   1698  1.10  christos     EVP_PKEY_CTX *ctx;
   1699  1.10  christos     /* Key operation to perform */
   1700  1.10  christos     int (*keyop) (EVP_PKEY_CTX *ctx,
   1701  1.10  christos                   unsigned char *sig, size_t *siglen,
   1702  1.10  christos                   const unsigned char *tbs, size_t tbslen);
   1703  1.10  christos     /* Input to MAC */
   1704  1.10  christos     unsigned char *input;
   1705  1.10  christos     size_t input_len;
   1706  1.10  christos     /* Expected output */
   1707  1.10  christos     unsigned char *output;
   1708  1.10  christos     size_t output_len;
   1709  1.10  christos } PKEY_DATA;
   1710  1.10  christos 
   1711  1.10  christos /*
   1712  1.10  christos  * Perform public key operation setup: lookup key, allocated ctx and call
   1713  1.10  christos  * the appropriate initialisation function
   1714  1.10  christos  */
   1715  1.10  christos static int pkey_test_init(EVP_TEST *t, const char *name,
   1716  1.10  christos                           int use_public,
   1717  1.10  christos                           int (*keyopinit) (EVP_PKEY_CTX *ctx),
   1718  1.10  christos                           int (*keyop)(EVP_PKEY_CTX *ctx,
   1719  1.10  christos                                        unsigned char *sig, size_t *siglen,
   1720  1.10  christos                                        const unsigned char *tbs,
   1721  1.10  christos                                        size_t tbslen))
   1722  1.10  christos {
   1723  1.10  christos     PKEY_DATA *kdata;
   1724  1.10  christos     EVP_PKEY *pkey = NULL;
   1725  1.10  christos     int rv = 0;
   1726  1.10  christos 
   1727  1.10  christos     if (use_public)
   1728  1.10  christos         rv = find_key(&pkey, name, public_keys);
   1729  1.10  christos     if (rv == 0)
   1730  1.10  christos         rv = find_key(&pkey, name, private_keys);
   1731  1.10  christos     if (rv == 0 || pkey == NULL) {
   1732  1.10  christos         TEST_info("skipping, key '%s' is disabled", name);
   1733  1.10  christos         t->skip = 1;
   1734  1.10  christos         return 1;
   1735  1.10  christos     }
   1736  1.10  christos 
   1737  1.10  christos     if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
   1738  1.10  christos         EVP_PKEY_free(pkey);
   1739  1.10  christos         return 0;
   1740   1.2  christos     }
   1741   1.2  christos     kdata->keyop = keyop;
   1742  1.10  christos     if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
   1743   1.4  christos         EVP_PKEY_free(pkey);
   1744   1.4  christos         OPENSSL_free(kdata);
   1745   1.2  christos         return 0;
   1746   1.4  christos     }
   1747   1.2  christos     if (keyopinit(kdata->ctx) <= 0)
   1748   1.2  christos         t->err = "KEYOP_INIT_ERROR";
   1749   1.4  christos     t->data = kdata;
   1750   1.2  christos     return 1;
   1751   1.2  christos }
   1752   1.2  christos 
   1753   1.4  christos static void pkey_test_cleanup(EVP_TEST *t)
   1754   1.2  christos {
   1755   1.4  christos     PKEY_DATA *kdata = t->data;
   1756   1.2  christos 
   1757   1.2  christos     OPENSSL_free(kdata->input);
   1758   1.2  christos     OPENSSL_free(kdata->output);
   1759   1.2  christos     EVP_PKEY_CTX_free(kdata->ctx);
   1760   1.2  christos }
   1761   1.2  christos 
   1762   1.4  christos static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
   1763   1.2  christos                           const char *value)
   1764   1.2  christos {
   1765  1.10  christos     int rv = 0;
   1766   1.2  christos     char *p, *tmpval;
   1767   1.2  christos 
   1768   1.4  christos     if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
   1769   1.2  christos         return 0;
   1770   1.2  christos     p = strchr(tmpval, ':');
   1771  1.10  christos     if (p != NULL) {
   1772   1.4  christos         *p++ = '\0';
   1773  1.10  christos         rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
   1774  1.10  christos     }
   1775   1.2  christos     if (rv == -2) {
   1776   1.2  christos         t->err = "PKEY_CTRL_INVALID";
   1777   1.2  christos         rv = 1;
   1778   1.2  christos     } else if (p != NULL && rv <= 0) {
   1779  1.10  christos         if (is_digest_disabled(p) || is_cipher_disabled(p)) {
   1780  1.10  christos             TEST_info("skipping, '%s' is disabled", p);
   1781   1.2  christos             t->skip = 1;
   1782   1.2  christos             rv = 1;
   1783   1.2  christos         } else {
   1784   1.2  christos             t->err = "PKEY_CTRL_ERROR";
   1785   1.2  christos             rv = 1;
   1786   1.2  christos         }
   1787   1.2  christos     }
   1788   1.2  christos     OPENSSL_free(tmpval);
   1789   1.2  christos     return rv > 0;
   1790   1.2  christos }
   1791   1.2  christos 
   1792   1.4  christos static int pkey_test_parse(EVP_TEST *t,
   1793   1.2  christos                            const char *keyword, const char *value)
   1794   1.2  christos {
   1795   1.4  christos     PKEY_DATA *kdata = t->data;
   1796   1.2  christos     if (strcmp(keyword, "Input") == 0)
   1797   1.4  christos         return parse_bin(value, &kdata->input, &kdata->input_len);
   1798   1.2  christos     if (strcmp(keyword, "Output") == 0)
   1799   1.4  christos         return parse_bin(value, &kdata->output, &kdata->output_len);
   1800   1.2  christos     if (strcmp(keyword, "Ctrl") == 0)
   1801   1.2  christos         return pkey_test_ctrl(t, kdata->ctx, value);
   1802   1.2  christos     return 0;
   1803   1.2  christos }
   1804   1.2  christos 
   1805   1.4  christos static int pkey_test_run(EVP_TEST *t)
   1806   1.2  christos {
   1807   1.4  christos     PKEY_DATA *expected = t->data;
   1808   1.4  christos     unsigned char *got = NULL;
   1809   1.4  christos     size_t got_len;
   1810  1.10  christos     EVP_PKEY_CTX *copy = NULL;
   1811   1.4  christos 
   1812   1.4  christos     if (expected->keyop(expected->ctx, NULL, &got_len,
   1813   1.4  christos                         expected->input, expected->input_len) <= 0
   1814   1.4  christos             || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
   1815   1.4  christos         t->err = "KEYOP_LENGTH_ERROR";
   1816   1.2  christos         goto err;
   1817   1.4  christos     }
   1818   1.4  christos     if (expected->keyop(expected->ctx, got, &got_len,
   1819   1.4  christos                         expected->input, expected->input_len) <= 0) {
   1820   1.4  christos         t->err = "KEYOP_ERROR";
   1821   1.2  christos         goto err;
   1822   1.4  christos     }
   1823   1.4  christos     if (!memory_err_compare(t, "KEYOP_MISMATCH",
   1824   1.4  christos                             expected->output, expected->output_len,
   1825   1.4  christos                             got, got_len))
   1826   1.2  christos         goto err;
   1827   1.4  christos 
   1828   1.4  christos     t->err = NULL;
   1829  1.10  christos     OPENSSL_free(got);
   1830  1.10  christos     got = NULL;
   1831  1.10  christos 
   1832  1.10  christos     /* Repeat the test on a copy. */
   1833  1.10  christos     if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
   1834  1.10  christos         t->err = "INTERNAL_ERROR";
   1835  1.10  christos         goto err;
   1836  1.10  christos     }
   1837  1.10  christos     if (expected->keyop(copy, NULL, &got_len, expected->input,
   1838  1.10  christos                         expected->input_len) <= 0
   1839  1.10  christos             || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
   1840  1.10  christos         t->err = "KEYOP_LENGTH_ERROR";
   1841  1.10  christos         goto err;
   1842  1.10  christos     }
   1843  1.10  christos     if (expected->keyop(copy, got, &got_len, expected->input,
   1844  1.10  christos                         expected->input_len) <= 0) {
   1845  1.10  christos         t->err = "KEYOP_ERROR";
   1846  1.10  christos         goto err;
   1847  1.10  christos     }
   1848  1.10  christos     if (!memory_err_compare(t, "KEYOP_MISMATCH",
   1849  1.10  christos                             expected->output, expected->output_len,
   1850  1.10  christos                             got, got_len))
   1851  1.10  christos         goto err;
   1852  1.10  christos 
   1853   1.2  christos  err:
   1854   1.4  christos     OPENSSL_free(got);
   1855  1.10  christos     EVP_PKEY_CTX_free(copy);
   1856   1.2  christos     return 1;
   1857   1.2  christos }
   1858   1.2  christos 
   1859   1.4  christos static int sign_test_init(EVP_TEST *t, const char *name)
   1860   1.2  christos {
   1861   1.2  christos     return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
   1862   1.2  christos }
   1863   1.2  christos 
   1864   1.4  christos static const EVP_TEST_METHOD psign_test_method = {
   1865   1.2  christos     "Sign",
   1866   1.2  christos     sign_test_init,
   1867   1.2  christos     pkey_test_cleanup,
   1868   1.2  christos     pkey_test_parse,
   1869   1.2  christos     pkey_test_run
   1870   1.2  christos };
   1871   1.2  christos 
   1872   1.4  christos static int verify_recover_test_init(EVP_TEST *t, const char *name)
   1873   1.2  christos {
   1874   1.2  christos     return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
   1875   1.2  christos                           EVP_PKEY_verify_recover);
   1876   1.2  christos }
   1877   1.2  christos 
   1878   1.4  christos static const EVP_TEST_METHOD pverify_recover_test_method = {
   1879   1.2  christos     "VerifyRecover",
   1880   1.2  christos     verify_recover_test_init,
   1881   1.2  christos     pkey_test_cleanup,
   1882   1.2  christos     pkey_test_parse,
   1883   1.2  christos     pkey_test_run
   1884   1.2  christos };
   1885   1.2  christos 
   1886   1.4  christos static int decrypt_test_init(EVP_TEST *t, const char *name)
   1887   1.2  christos {
   1888   1.2  christos     return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
   1889   1.2  christos                           EVP_PKEY_decrypt);
   1890   1.2  christos }
   1891   1.2  christos 
   1892   1.4  christos static const EVP_TEST_METHOD pdecrypt_test_method = {
   1893   1.2  christos     "Decrypt",
   1894   1.2  christos     decrypt_test_init,
   1895   1.2  christos     pkey_test_cleanup,
   1896   1.2  christos     pkey_test_parse,
   1897   1.2  christos     pkey_test_run
   1898   1.2  christos };
   1899   1.2  christos 
   1900   1.4  christos static int verify_test_init(EVP_TEST *t, const char *name)
   1901   1.2  christos {
   1902   1.2  christos     return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
   1903   1.2  christos }
   1904   1.2  christos 
   1905   1.4  christos static int verify_test_run(EVP_TEST *t)
   1906   1.2  christos {
   1907   1.4  christos     PKEY_DATA *kdata = t->data;
   1908   1.4  christos 
   1909   1.2  christos     if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
   1910   1.2  christos                         kdata->input, kdata->input_len) <= 0)
   1911   1.2  christos         t->err = "VERIFY_ERROR";
   1912   1.2  christos     return 1;
   1913   1.2  christos }
   1914   1.2  christos 
   1915   1.4  christos static const EVP_TEST_METHOD pverify_test_method = {
   1916   1.2  christos     "Verify",
   1917   1.2  christos     verify_test_init,
   1918   1.2  christos     pkey_test_cleanup,
   1919   1.2  christos     pkey_test_parse,
   1920   1.2  christos     verify_test_run
   1921   1.2  christos };
   1922   1.2  christos 
   1923   1.4  christos static int pderive_test_init(EVP_TEST *t, const char *name)
   1924   1.2  christos {
   1925   1.2  christos     return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
   1926   1.2  christos }
   1927   1.2  christos 
   1928   1.4  christos static int pderive_test_parse(EVP_TEST *t,
   1929   1.2  christos                               const char *keyword, const char *value)
   1930   1.2  christos {
   1931   1.4  christos     PKEY_DATA *kdata = t->data;
   1932  1.10  christos     int validate = 0;
   1933  1.10  christos 
   1934  1.10  christos     if (strcmp(keyword, "PeerKeyValidate") == 0)
   1935  1.10  christos         validate = 1;
   1936   1.2  christos 
   1937  1.10  christos     if (validate || strcmp(keyword, "PeerKey") == 0) {
   1938   1.2  christos         EVP_PKEY *peer;
   1939   1.4  christos         if (find_key(&peer, value, public_keys) == 0)
   1940   1.8  christos             return -1;
   1941  1.10  christos         if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
   1942  1.10  christos             t->err = "DERIVE_SET_PEER_ERROR";
   1943  1.10  christos             return 1;
   1944  1.10  christos         }
   1945  1.10  christos         t->err = NULL;
   1946   1.2  christos         return 1;
   1947   1.2  christos     }
   1948   1.2  christos     if (strcmp(keyword, "SharedSecret") == 0)
   1949   1.4  christos         return parse_bin(value, &kdata->output, &kdata->output_len);
   1950   1.2  christos     if (strcmp(keyword, "Ctrl") == 0)
   1951   1.2  christos         return pkey_test_ctrl(t, kdata->ctx, value);
   1952  1.10  christos     if (strcmp(keyword, "KDFType") == 0) {
   1953  1.10  christos         OSSL_PARAM params[2];
   1954  1.10  christos 
   1955  1.10  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
   1956  1.10  christos                                                      (char *)(intptr_t)value, 0);
   1957  1.10  christos         params[1] = OSSL_PARAM_construct_end();
   1958  1.10  christos         if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
   1959  1.10  christos             return -1;
   1960  1.10  christos         return 1;
   1961  1.10  christos     }
   1962  1.10  christos     if (strcmp(keyword, "KDFDigest") == 0) {
   1963  1.10  christos         OSSL_PARAM params[2];
   1964  1.10  christos 
   1965  1.10  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
   1966  1.10  christos                                                      (char *)(intptr_t)value, 0);
   1967  1.10  christos         params[1] = OSSL_PARAM_construct_end();
   1968  1.10  christos         if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
   1969  1.10  christos             return -1;
   1970  1.10  christos         return 1;
   1971  1.10  christos     }
   1972  1.10  christos     if (strcmp(keyword, "CEKAlg") == 0) {
   1973  1.10  christos         OSSL_PARAM params[2];
   1974  1.10  christos 
   1975  1.10  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
   1976  1.10  christos                                                      (char *)(intptr_t)value, 0);
   1977  1.10  christos         params[1] = OSSL_PARAM_construct_end();
   1978  1.10  christos         if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
   1979  1.10  christos             return -1;
   1980  1.10  christos         return 1;
   1981  1.10  christos     }
   1982  1.10  christos     if (strcmp(keyword, "KDFOutlen") == 0) {
   1983  1.10  christos         OSSL_PARAM params[2];
   1984  1.10  christos         char *endptr;
   1985  1.10  christos         size_t outlen = (size_t)strtoul(value, &endptr, 0);
   1986  1.10  christos 
   1987  1.10  christos         if (endptr[0] != '\0')
   1988  1.10  christos             return -1;
   1989  1.10  christos 
   1990  1.10  christos         params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
   1991  1.10  christos                                                 &outlen);
   1992  1.10  christos         params[1] = OSSL_PARAM_construct_end();
   1993  1.10  christos         if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
   1994  1.10  christos             return -1;
   1995  1.10  christos         return 1;
   1996  1.10  christos     }
   1997   1.2  christos     return 0;
   1998   1.2  christos }
   1999   1.2  christos 
   2000   1.4  christos static int pderive_test_run(EVP_TEST *t)
   2001   1.2  christos {
   2002  1.10  christos     EVP_PKEY_CTX *dctx = NULL;
   2003   1.4  christos     PKEY_DATA *expected = t->data;
   2004   1.4  christos     unsigned char *got = NULL;
   2005   1.4  christos     size_t got_len;
   2006   1.2  christos 
   2007  1.10  christos     if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) {
   2008  1.10  christos         t->err = "DERIVE_ERROR";
   2009  1.10  christos         goto err;
   2010  1.10  christos     }
   2011  1.10  christos 
   2012  1.10  christos     if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0
   2013  1.10  christos         || !TEST_size_t_ne(got_len, 0)) {
   2014   1.4  christos         t->err = "DERIVE_ERROR";
   2015   1.3  christos         goto err;
   2016   1.2  christos     }
   2017   1.4  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
   2018   1.4  christos         t->err = "DERIVE_ERROR";
   2019   1.2  christos         goto err;
   2020   1.4  christos     }
   2021  1.10  christos     if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) {
   2022   1.4  christos         t->err = "DERIVE_ERROR";
   2023   1.2  christos         goto err;
   2024   1.4  christos     }
   2025   1.4  christos     if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
   2026   1.4  christos                             expected->output, expected->output_len,
   2027   1.4  christos                             got, got_len))
   2028   1.2  christos         goto err;
   2029   1.4  christos 
   2030   1.4  christos     t->err = NULL;
   2031   1.2  christos  err:
   2032   1.4  christos     OPENSSL_free(got);
   2033  1.10  christos     EVP_PKEY_CTX_free(dctx);
   2034   1.2  christos     return 1;
   2035   1.2  christos }
   2036   1.2  christos 
   2037   1.4  christos static const EVP_TEST_METHOD pderive_test_method = {
   2038   1.2  christos     "Derive",
   2039   1.2  christos     pderive_test_init,
   2040   1.2  christos     pkey_test_cleanup,
   2041   1.2  christos     pderive_test_parse,
   2042   1.2  christos     pderive_test_run
   2043   1.2  christos };
   2044   1.2  christos 
   2045   1.2  christos 
   2046   1.4  christos /**
   2047  1.10  christos  **  PBE TESTS
   2048  1.10  christos  **/
   2049   1.4  christos 
   2050   1.4  christos typedef enum pbe_type_enum {
   2051   1.4  christos     PBE_TYPE_INVALID = 0,
   2052   1.4  christos     PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
   2053   1.4  christos } PBE_TYPE;
   2054   1.4  christos 
   2055   1.4  christos typedef struct pbe_data_st {
   2056   1.4  christos     PBE_TYPE pbe_type;
   2057   1.4  christos         /* scrypt parameters */
   2058   1.2  christos     uint64_t N, r, p, maxmem;
   2059   1.4  christos         /* PKCS#12 parameters */
   2060   1.2  christos     int id, iter;
   2061   1.2  christos     const EVP_MD *md;
   2062   1.4  christos         /* password */
   2063   1.2  christos     unsigned char *pass;
   2064   1.2  christos     size_t pass_len;
   2065   1.4  christos         /* salt */
   2066   1.2  christos     unsigned char *salt;
   2067   1.2  christos     size_t salt_len;
   2068   1.4  christos         /* Expected output */
   2069   1.2  christos     unsigned char *key;
   2070   1.2  christos     size_t key_len;
   2071   1.4  christos } PBE_DATA;
   2072   1.2  christos 
   2073   1.2  christos #ifndef OPENSSL_NO_SCRYPT
   2074  1.10  christos /* Parse unsigned decimal 64 bit integer value */
   2075   1.4  christos static int parse_uint64(const char *value, uint64_t *pr)
   2076   1.4  christos {
   2077   1.4  christos     const char *p = value;
   2078   1.4  christos 
   2079   1.4  christos     if (!TEST_true(*p)) {
   2080   1.4  christos         TEST_info("Invalid empty integer value");
   2081   1.4  christos         return -1;
   2082   1.4  christos     }
   2083   1.4  christos     for (*pr = 0; *p; ) {
   2084   1.4  christos         if (*pr > UINT64_MAX / 10) {
   2085   1.4  christos             TEST_error("Integer overflow in string %s", value);
   2086   1.4  christos             return -1;
   2087   1.4  christos         }
   2088   1.4  christos         *pr *= 10;
   2089   1.4  christos         if (!TEST_true(isdigit((unsigned char)*p))) {
   2090   1.4  christos             TEST_error("Invalid character in string %s", value);
   2091   1.4  christos             return -1;
   2092   1.4  christos         }
   2093   1.4  christos         *pr += *p - '0';
   2094   1.4  christos         p++;
   2095   1.4  christos     }
   2096   1.4  christos     return 1;
   2097   1.4  christos }
   2098   1.4  christos 
   2099   1.4  christos static int scrypt_test_parse(EVP_TEST *t,
   2100   1.2  christos                              const char *keyword, const char *value)
   2101   1.2  christos {
   2102   1.4  christos     PBE_DATA *pdata = t->data;
   2103   1.2  christos 
   2104   1.2  christos     if (strcmp(keyword, "N") == 0)
   2105   1.4  christos         return parse_uint64(value, &pdata->N);
   2106   1.2  christos     if (strcmp(keyword, "p") == 0)
   2107   1.4  christos         return parse_uint64(value, &pdata->p);
   2108   1.2  christos     if (strcmp(keyword, "r") == 0)
   2109   1.4  christos         return parse_uint64(value, &pdata->r);
   2110   1.2  christos     if (strcmp(keyword, "maxmem") == 0)
   2111   1.4  christos         return parse_uint64(value, &pdata->maxmem);
   2112   1.2  christos     return 0;
   2113   1.2  christos }
   2114   1.2  christos #endif
   2115   1.2  christos 
   2116   1.4  christos static int pbkdf2_test_parse(EVP_TEST *t,
   2117   1.2  christos                              const char *keyword, const char *value)
   2118   1.2  christos {
   2119   1.4  christos     PBE_DATA *pdata = t->data;
   2120   1.2  christos 
   2121   1.2  christos     if (strcmp(keyword, "iter") == 0) {
   2122   1.2  christos         pdata->iter = atoi(value);
   2123   1.2  christos         if (pdata->iter <= 0)
   2124   1.4  christos             return -1;
   2125   1.2  christos         return 1;
   2126   1.2  christos     }
   2127   1.2  christos     if (strcmp(keyword, "MD") == 0) {
   2128   1.2  christos         pdata->md = EVP_get_digestbyname(value);
   2129   1.2  christos         if (pdata->md == NULL)
   2130   1.4  christos             return -1;
   2131   1.2  christos         return 1;
   2132   1.2  christos     }
   2133   1.2  christos     return 0;
   2134   1.2  christos }
   2135   1.2  christos 
   2136   1.4  christos static int pkcs12_test_parse(EVP_TEST *t,
   2137   1.2  christos                              const char *keyword, const char *value)
   2138   1.2  christos {
   2139   1.4  christos     PBE_DATA *pdata = t->data;
   2140   1.2  christos 
   2141   1.2  christos     if (strcmp(keyword, "id") == 0) {
   2142   1.2  christos         pdata->id = atoi(value);
   2143   1.2  christos         if (pdata->id <= 0)
   2144   1.4  christos             return -1;
   2145   1.2  christos         return 1;
   2146   1.2  christos     }
   2147   1.2  christos     return pbkdf2_test_parse(t, keyword, value);
   2148   1.2  christos }
   2149   1.2  christos 
   2150   1.4  christos static int pbe_test_init(EVP_TEST *t, const char *alg)
   2151   1.2  christos {
   2152   1.4  christos     PBE_DATA *pdat;
   2153   1.4  christos     PBE_TYPE pbe_type = PBE_TYPE_INVALID;
   2154   1.2  christos 
   2155  1.10  christos     if (is_kdf_disabled(alg)) {
   2156  1.10  christos         TEST_info("skipping, '%s' is disabled", alg);
   2157  1.10  christos         t->skip = 1;
   2158  1.10  christos         return 1;
   2159  1.10  christos     }
   2160   1.2  christos     if (strcmp(alg, "scrypt") == 0) {
   2161   1.2  christos         pbe_type = PBE_TYPE_SCRYPT;
   2162   1.2  christos     } else if (strcmp(alg, "pbkdf2") == 0) {
   2163   1.2  christos         pbe_type = PBE_TYPE_PBKDF2;
   2164   1.2  christos     } else if (strcmp(alg, "pkcs12") == 0) {
   2165   1.2  christos         pbe_type = PBE_TYPE_PKCS12;
   2166   1.2  christos     } else {
   2167   1.4  christos         TEST_error("Unknown pbe algorithm %s", alg);
   2168  1.10  christos         return 0;
   2169   1.2  christos     }
   2170  1.10  christos     if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
   2171  1.10  christos         return 0;
   2172   1.2  christos     pdat->pbe_type = pbe_type;
   2173   1.2  christos     t->data = pdat;
   2174   1.2  christos     return 1;
   2175   1.2  christos }
   2176   1.2  christos 
   2177   1.4  christos static void pbe_test_cleanup(EVP_TEST *t)
   2178   1.2  christos {
   2179   1.4  christos     PBE_DATA *pdat = t->data;
   2180   1.4  christos 
   2181   1.4  christos     OPENSSL_free(pdat->pass);
   2182   1.4  christos     OPENSSL_free(pdat->salt);
   2183   1.4  christos     OPENSSL_free(pdat->key);
   2184   1.2  christos }
   2185   1.2  christos 
   2186   1.4  christos static int pbe_test_parse(EVP_TEST *t,
   2187   1.4  christos                           const char *keyword, const char *value)
   2188   1.2  christos {
   2189   1.4  christos     PBE_DATA *pdata = t->data;
   2190   1.2  christos 
   2191   1.2  christos     if (strcmp(keyword, "Password") == 0)
   2192   1.4  christos         return parse_bin(value, &pdata->pass, &pdata->pass_len);
   2193   1.2  christos     if (strcmp(keyword, "Salt") == 0)
   2194   1.4  christos         return parse_bin(value, &pdata->salt, &pdata->salt_len);
   2195   1.2  christos     if (strcmp(keyword, "Key") == 0)
   2196   1.4  christos         return parse_bin(value, &pdata->key, &pdata->key_len);
   2197   1.2  christos     if (pdata->pbe_type == PBE_TYPE_PBKDF2)
   2198   1.2  christos         return pbkdf2_test_parse(t, keyword, value);
   2199   1.2  christos     else if (pdata->pbe_type == PBE_TYPE_PKCS12)
   2200   1.2  christos         return pkcs12_test_parse(t, keyword, value);
   2201   1.2  christos #ifndef OPENSSL_NO_SCRYPT
   2202   1.2  christos     else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
   2203   1.2  christos         return scrypt_test_parse(t, keyword, value);
   2204   1.2  christos #endif
   2205   1.2  christos     return 0;
   2206   1.2  christos }
   2207   1.2  christos 
   2208   1.4  christos static int pbe_test_run(EVP_TEST *t)
   2209   1.2  christos {
   2210   1.4  christos     PBE_DATA *expected = t->data;
   2211   1.2  christos     unsigned char *key;
   2212  1.10  christos     EVP_MD *fetched_digest = NULL;
   2213  1.10  christos     OSSL_LIB_CTX *save_libctx;
   2214  1.10  christos 
   2215  1.10  christos     save_libctx = OSSL_LIB_CTX_set0_default(libctx);
   2216   1.2  christos 
   2217   1.4  christos     if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
   2218   1.4  christos         t->err = "INTERNAL_ERROR";
   2219   1.2  christos         goto err;
   2220   1.4  christos     }
   2221   1.4  christos     if (expected->pbe_type == PBE_TYPE_PBKDF2) {
   2222   1.4  christos         if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
   2223   1.4  christos                               expected->salt, expected->salt_len,
   2224   1.4  christos                               expected->iter, expected->md,
   2225   1.4  christos                               expected->key_len, key) == 0) {
   2226   1.4  christos             t->err = "PBKDF2_ERROR";
   2227   1.2  christos             goto err;
   2228   1.4  christos         }
   2229   1.2  christos #ifndef OPENSSL_NO_SCRYPT
   2230   1.4  christos     } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
   2231   1.4  christos         if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
   2232  1.10  christos                             expected->salt, expected->salt_len,
   2233  1.10  christos                             expected->N, expected->r, expected->p,
   2234  1.10  christos                             expected->maxmem, key, expected->key_len) == 0) {
   2235   1.4  christos             t->err = "SCRYPT_ERROR";
   2236   1.2  christos             goto err;
   2237   1.4  christos         }
   2238   1.2  christos #endif
   2239   1.4  christos     } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
   2240  1.10  christos         fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
   2241  1.10  christos                                       NULL);
   2242  1.10  christos         if (fetched_digest == NULL) {
   2243  1.10  christos             t->err = "PKCS12_ERROR";
   2244  1.10  christos             goto err;
   2245  1.10  christos         }
   2246   1.4  christos         if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
   2247   1.4  christos                                expected->salt, expected->salt_len,
   2248   1.4  christos                                expected->id, expected->iter, expected->key_len,
   2249  1.10  christos                                key, fetched_digest) == 0) {
   2250   1.4  christos             t->err = "PKCS12_ERROR";
   2251   1.2  christos             goto err;
   2252   1.4  christos         }
   2253   1.2  christos     }
   2254   1.4  christos     if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
   2255   1.4  christos                             key, expected->key_len))
   2256   1.2  christos         goto err;
   2257   1.4  christos 
   2258   1.4  christos     t->err = NULL;
   2259   1.4  christos err:
   2260  1.10  christos     EVP_MD_free(fetched_digest);
   2261   1.2  christos     OPENSSL_free(key);
   2262  1.10  christos     OSSL_LIB_CTX_set0_default(save_libctx);
   2263   1.2  christos     return 1;
   2264   1.2  christos }
   2265   1.2  christos 
   2266   1.4  christos static const EVP_TEST_METHOD pbe_test_method = {
   2267   1.2  christos     "PBE",
   2268   1.2  christos     pbe_test_init,
   2269   1.2  christos     pbe_test_cleanup,
   2270   1.2  christos     pbe_test_parse,
   2271   1.2  christos     pbe_test_run
   2272   1.2  christos };
   2273   1.2  christos 
   2274   1.4  christos 
   2275   1.4  christos /**
   2276  1.10  christos  **  BASE64 TESTS
   2277  1.10  christos  **/
   2278   1.2  christos 
   2279   1.2  christos typedef enum {
   2280   1.2  christos     BASE64_CANONICAL_ENCODING = 0,
   2281   1.2  christos     BASE64_VALID_ENCODING = 1,
   2282   1.2  christos     BASE64_INVALID_ENCODING = 2
   2283   1.2  christos } base64_encoding_type;
   2284   1.2  christos 
   2285   1.4  christos typedef struct encode_data_st {
   2286   1.2  christos     /* Input to encoding */
   2287   1.2  christos     unsigned char *input;
   2288   1.2  christos     size_t input_len;
   2289   1.2  christos     /* Expected output */
   2290   1.2  christos     unsigned char *output;
   2291   1.2  christos     size_t output_len;
   2292   1.2  christos     base64_encoding_type encoding;
   2293   1.4  christos } ENCODE_DATA;
   2294   1.2  christos 
   2295   1.4  christos static int encode_test_init(EVP_TEST *t, const char *encoding)
   2296   1.2  christos {
   2297   1.4  christos     ENCODE_DATA *edata;
   2298   1.2  christos 
   2299   1.4  christos     if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
   2300   1.4  christos         return 0;
   2301   1.2  christos     if (strcmp(encoding, "canonical") == 0) {
   2302   1.2  christos         edata->encoding = BASE64_CANONICAL_ENCODING;
   2303   1.2  christos     } else if (strcmp(encoding, "valid") == 0) {
   2304   1.2  christos         edata->encoding = BASE64_VALID_ENCODING;
   2305   1.2  christos     } else if (strcmp(encoding, "invalid") == 0) {
   2306   1.2  christos         edata->encoding = BASE64_INVALID_ENCODING;
   2307   1.4  christos         if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
   2308   1.6  christos             goto err;
   2309   1.2  christos     } else {
   2310   1.4  christos         TEST_error("Bad encoding: %s."
   2311   1.4  christos                    " Should be one of {canonical, valid, invalid}",
   2312   1.4  christos                    encoding);
   2313   1.6  christos         goto err;
   2314   1.2  christos     }
   2315   1.2  christos     t->data = edata;
   2316   1.2  christos     return 1;
   2317   1.6  christos err:
   2318   1.6  christos     OPENSSL_free(edata);
   2319   1.6  christos     return 0;
   2320   1.2  christos }
   2321   1.2  christos 
   2322   1.4  christos static void encode_test_cleanup(EVP_TEST *t)
   2323   1.2  christos {
   2324   1.4  christos     ENCODE_DATA *edata = t->data;
   2325   1.4  christos 
   2326   1.4  christos     OPENSSL_free(edata->input);
   2327   1.4  christos     OPENSSL_free(edata->output);
   2328   1.2  christos     memset(edata, 0, sizeof(*edata));
   2329   1.2  christos }
   2330   1.2  christos 
   2331   1.4  christos static int encode_test_parse(EVP_TEST *t,
   2332   1.2  christos                              const char *keyword, const char *value)
   2333   1.2  christos {
   2334   1.4  christos     ENCODE_DATA *edata = t->data;
   2335   1.4  christos 
   2336   1.2  christos     if (strcmp(keyword, "Input") == 0)
   2337   1.4  christos         return parse_bin(value, &edata->input, &edata->input_len);
   2338   1.2  christos     if (strcmp(keyword, "Output") == 0)
   2339   1.4  christos         return parse_bin(value, &edata->output, &edata->output_len);
   2340   1.2  christos     return 0;
   2341   1.2  christos }
   2342   1.2  christos 
   2343   1.4  christos static int encode_test_run(EVP_TEST *t)
   2344   1.2  christos {
   2345   1.4  christos     ENCODE_DATA *expected = t->data;
   2346   1.2  christos     unsigned char *encode_out = NULL, *decode_out = NULL;
   2347   1.2  christos     int output_len, chunk_len;
   2348   1.6  christos     EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
   2349   1.2  christos 
   2350   1.4  christos     if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
   2351   1.4  christos         t->err = "INTERNAL_ERROR";
   2352   1.2  christos         goto err;
   2353   1.4  christos     }
   2354   1.4  christos 
   2355   1.4  christos     if (expected->encoding == BASE64_CANONICAL_ENCODING) {
   2356   1.2  christos 
   2357   1.4  christos         if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
   2358   1.4  christos                 || !TEST_ptr(encode_out =
   2359   1.4  christos                         OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
   2360   1.2  christos             goto err;
   2361   1.2  christos 
   2362   1.2  christos         EVP_EncodeInit(encode_ctx);
   2363   1.6  christos         if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
   2364   1.6  christos                                         expected->input, expected->input_len)))
   2365   1.6  christos             goto err;
   2366   1.6  christos 
   2367   1.2  christos         output_len = chunk_len;
   2368   1.2  christos 
   2369   1.2  christos         EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
   2370   1.2  christos         output_len += chunk_len;
   2371   1.2  christos 
   2372   1.4  christos         if (!memory_err_compare(t, "BAD_ENCODING",
   2373   1.4  christos                                 expected->output, expected->output_len,
   2374   1.4  christos                                 encode_out, output_len))
   2375   1.2  christos             goto err;
   2376   1.2  christos     }
   2377   1.2  christos 
   2378   1.4  christos     if (!TEST_ptr(decode_out =
   2379   1.4  christos                 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
   2380   1.2  christos         goto err;
   2381   1.2  christos 
   2382   1.2  christos     EVP_DecodeInit(decode_ctx);
   2383  1.10  christos     if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
   2384  1.10  christos                          expected->output_len) < 0) {
   2385  1.10  christos         t->err = "DECODE_ERROR";
   2386  1.10  christos         goto err;
   2387  1.10  christos     }
   2388  1.10  christos     output_len = chunk_len;
   2389  1.10  christos 
   2390  1.10  christos     if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
   2391  1.10  christos         t->err = "DECODE_ERROR";
   2392  1.10  christos         goto err;
   2393  1.10  christos     }
   2394  1.10  christos     output_len += chunk_len;
   2395  1.10  christos 
   2396  1.10  christos     if (expected->encoding != BASE64_INVALID_ENCODING
   2397  1.10  christos             && !memory_err_compare(t, "BAD_DECODING",
   2398  1.10  christos                                    expected->input, expected->input_len,
   2399  1.10  christos                                    decode_out, output_len)) {
   2400  1.10  christos         t->err = "BAD_DECODING";
   2401  1.10  christos         goto err;
   2402  1.10  christos     }
   2403  1.10  christos 
   2404  1.10  christos     t->err = NULL;
   2405  1.10  christos  err:
   2406  1.10  christos     OPENSSL_free(encode_out);
   2407  1.10  christos     OPENSSL_free(decode_out);
   2408  1.10  christos     EVP_ENCODE_CTX_free(decode_ctx);
   2409  1.10  christos     EVP_ENCODE_CTX_free(encode_ctx);
   2410  1.10  christos     return 1;
   2411  1.10  christos }
   2412  1.10  christos 
   2413  1.10  christos static const EVP_TEST_METHOD encode_test_method = {
   2414  1.10  christos     "Encoding",
   2415  1.10  christos     encode_test_init,
   2416  1.10  christos     encode_test_cleanup,
   2417  1.10  christos     encode_test_parse,
   2418  1.10  christos     encode_test_run,
   2419  1.10  christos };
   2420  1.10  christos 
   2421  1.10  christos 
   2422  1.10  christos /**
   2423  1.10  christos  **  RAND TESTS
   2424  1.10  christos  **/
   2425  1.10  christos #define MAX_RAND_REPEATS    15
   2426  1.10  christos 
   2427  1.10  christos typedef struct rand_data_pass_st {
   2428  1.10  christos     unsigned char *entropy;
   2429  1.10  christos     unsigned char *reseed_entropy;
   2430  1.10  christos     unsigned char *nonce;
   2431  1.10  christos     unsigned char *pers;
   2432  1.10  christos     unsigned char *reseed_addin;
   2433  1.10  christos     unsigned char *addinA;
   2434  1.10  christos     unsigned char *addinB;
   2435  1.10  christos     unsigned char *pr_entropyA;
   2436  1.10  christos     unsigned char *pr_entropyB;
   2437  1.10  christos     unsigned char *output;
   2438  1.10  christos     size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
   2439  1.10  christos            pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
   2440  1.10  christos            reseed_addin_len;
   2441  1.10  christos } RAND_DATA_PASS;
   2442  1.10  christos 
   2443  1.10  christos typedef struct rand_data_st {
   2444  1.10  christos     /* Context for this operation */
   2445  1.10  christos     EVP_RAND_CTX *ctx;
   2446  1.10  christos     EVP_RAND_CTX *parent;
   2447  1.10  christos     int n;
   2448  1.10  christos     int prediction_resistance;
   2449  1.10  christos     int use_df;
   2450  1.10  christos     unsigned int generate_bits;
   2451  1.10  christos     char *cipher;
   2452  1.10  christos     char *digest;
   2453  1.10  christos 
   2454  1.10  christos     /* Expected output */
   2455  1.10  christos     RAND_DATA_PASS data[MAX_RAND_REPEATS];
   2456  1.10  christos } RAND_DATA;
   2457  1.10  christos 
   2458  1.10  christos static int rand_test_init(EVP_TEST *t, const char *name)
   2459  1.10  christos {
   2460  1.10  christos     RAND_DATA *rdata;
   2461  1.10  christos     EVP_RAND *rand;
   2462  1.10  christos     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
   2463  1.10  christos     unsigned int strength = 256;
   2464  1.10  christos 
   2465  1.10  christos     if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
   2466  1.10  christos         return 0;
   2467  1.10  christos 
   2468  1.10  christos     /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
   2469  1.10  christos     rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
   2470  1.10  christos     if (rand == NULL)
   2471  1.10  christos         goto err;
   2472  1.10  christos     rdata->parent = EVP_RAND_CTX_new(rand, NULL);
   2473  1.10  christos     EVP_RAND_free(rand);
   2474  1.10  christos     if (rdata->parent == NULL)
   2475  1.10  christos         goto err;
   2476  1.10  christos 
   2477  1.10  christos     *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
   2478  1.10  christos     if (!EVP_RAND_CTX_set_params(rdata->parent, params))
   2479  1.10  christos         goto err;
   2480  1.10  christos 
   2481  1.10  christos     rand = EVP_RAND_fetch(libctx, name, NULL);
   2482  1.10  christos     if (rand == NULL)
   2483  1.10  christos         goto err;
   2484  1.10  christos     rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
   2485  1.10  christos     EVP_RAND_free(rand);
   2486  1.10  christos     if (rdata->ctx == NULL)
   2487  1.10  christos         goto err;
   2488  1.10  christos 
   2489  1.10  christos     rdata->n = -1;
   2490  1.10  christos     t->data = rdata;
   2491  1.10  christos     return 1;
   2492  1.10  christos  err:
   2493  1.10  christos     EVP_RAND_CTX_free(rdata->parent);
   2494  1.10  christos     OPENSSL_free(rdata);
   2495  1.10  christos     return 0;
   2496  1.10  christos }
   2497  1.10  christos 
   2498  1.10  christos static void rand_test_cleanup(EVP_TEST *t)
   2499  1.10  christos {
   2500  1.10  christos     RAND_DATA *rdata = t->data;
   2501  1.10  christos     int i;
   2502  1.10  christos 
   2503  1.10  christos     OPENSSL_free(rdata->cipher);
   2504  1.10  christos     OPENSSL_free(rdata->digest);
   2505  1.10  christos 
   2506  1.10  christos     for (i = 0; i <= rdata->n; i++) {
   2507  1.10  christos         OPENSSL_free(rdata->data[i].entropy);
   2508  1.10  christos         OPENSSL_free(rdata->data[i].reseed_entropy);
   2509  1.10  christos         OPENSSL_free(rdata->data[i].nonce);
   2510  1.10  christos         OPENSSL_free(rdata->data[i].pers);
   2511  1.10  christos         OPENSSL_free(rdata->data[i].reseed_addin);
   2512  1.10  christos         OPENSSL_free(rdata->data[i].addinA);
   2513  1.10  christos         OPENSSL_free(rdata->data[i].addinB);
   2514  1.10  christos         OPENSSL_free(rdata->data[i].pr_entropyA);
   2515  1.10  christos         OPENSSL_free(rdata->data[i].pr_entropyB);
   2516  1.10  christos         OPENSSL_free(rdata->data[i].output);
   2517  1.10  christos     }
   2518  1.10  christos     EVP_RAND_CTX_free(rdata->ctx);
   2519  1.10  christos     EVP_RAND_CTX_free(rdata->parent);
   2520  1.10  christos }
   2521  1.10  christos 
   2522  1.10  christos static int rand_test_parse(EVP_TEST *t,
   2523  1.10  christos                           const char *keyword, const char *value)
   2524  1.10  christos {
   2525  1.10  christos     RAND_DATA *rdata = t->data;
   2526  1.10  christos     RAND_DATA_PASS *item;
   2527  1.10  christos     const char *p;
   2528  1.10  christos     int n;
   2529  1.10  christos 
   2530  1.10  christos     if ((p = strchr(keyword, '.')) != NULL) {
   2531  1.10  christos         n = atoi(++p);
   2532  1.10  christos         if (n >= MAX_RAND_REPEATS)
   2533  1.10  christos             return 0;
   2534  1.10  christos         if (n > rdata->n)
   2535  1.10  christos             rdata->n = n;
   2536  1.10  christos         item = rdata->data + n;
   2537  1.10  christos         if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
   2538  1.10  christos             return parse_bin(value, &item->entropy, &item->entropy_len);
   2539  1.10  christos         if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
   2540  1.10  christos             return parse_bin(value, &item->reseed_entropy,
   2541  1.10  christos                              &item->reseed_entropy_len);
   2542  1.10  christos         if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
   2543  1.10  christos             return parse_bin(value, &item->nonce, &item->nonce_len);
   2544  1.10  christos         if (strncmp(keyword, "PersonalisationString.",
   2545  1.10  christos                     sizeof("PersonalisationString")) == 0)
   2546  1.10  christos             return parse_bin(value, &item->pers, &item->pers_len);
   2547  1.10  christos         if (strncmp(keyword, "ReseedAdditionalInput.",
   2548  1.10  christos                     sizeof("ReseedAdditionalInput")) == 0)
   2549  1.10  christos             return parse_bin(value, &item->reseed_addin,
   2550  1.10  christos                              &item->reseed_addin_len);
   2551  1.10  christos         if (strncmp(keyword, "AdditionalInputA.",
   2552  1.10  christos                     sizeof("AdditionalInputA")) == 0)
   2553  1.10  christos             return parse_bin(value, &item->addinA, &item->addinA_len);
   2554  1.10  christos         if (strncmp(keyword, "AdditionalInputB.",
   2555  1.10  christos                     sizeof("AdditionalInputB")) == 0)
   2556  1.10  christos             return parse_bin(value, &item->addinB, &item->addinB_len);
   2557  1.10  christos         if (strncmp(keyword, "EntropyPredictionResistanceA.",
   2558  1.10  christos                     sizeof("EntropyPredictionResistanceA")) == 0)
   2559  1.10  christos             return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
   2560  1.10  christos         if (strncmp(keyword, "EntropyPredictionResistanceB.",
   2561  1.10  christos                     sizeof("EntropyPredictionResistanceB")) == 0)
   2562  1.10  christos             return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
   2563  1.10  christos         if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
   2564  1.10  christos             return parse_bin(value, &item->output, &item->output_len);
   2565  1.10  christos     } else {
   2566  1.10  christos         if (strcmp(keyword, "Cipher") == 0)
   2567  1.10  christos             return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
   2568  1.10  christos         if (strcmp(keyword, "Digest") == 0)
   2569  1.10  christos             return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
   2570  1.10  christos         if (strcmp(keyword, "DerivationFunction") == 0) {
   2571  1.10  christos             rdata->use_df = atoi(value) != 0;
   2572  1.10  christos             return 1;
   2573  1.10  christos         }
   2574  1.10  christos         if (strcmp(keyword, "GenerateBits") == 0) {
   2575  1.10  christos             if ((n = atoi(value)) <= 0 || n % 8 != 0)
   2576  1.10  christos                 return 0;
   2577  1.10  christos             rdata->generate_bits = (unsigned int)n;
   2578  1.10  christos             return 1;
   2579  1.10  christos         }
   2580  1.10  christos         if (strcmp(keyword, "PredictionResistance") == 0) {
   2581  1.10  christos             rdata->prediction_resistance = atoi(value) != 0;
   2582  1.10  christos             return 1;
   2583  1.10  christos         }
   2584  1.10  christos     }
   2585  1.10  christos     return 0;
   2586  1.10  christos }
   2587  1.10  christos 
   2588  1.10  christos static int rand_test_run(EVP_TEST *t)
   2589  1.10  christos {
   2590  1.10  christos     RAND_DATA *expected = t->data;
   2591  1.10  christos     RAND_DATA_PASS *item;
   2592  1.10  christos     unsigned char *got;
   2593  1.10  christos     size_t got_len = expected->generate_bits / 8;
   2594  1.10  christos     OSSL_PARAM params[5], *p = params;
   2595  1.10  christos     int i = -1, ret = 0;
   2596  1.10  christos     unsigned int strength;
   2597  1.10  christos     unsigned char *z;
   2598  1.10  christos 
   2599  1.10  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
   2600  1.10  christos         return 0;
   2601  1.10  christos 
   2602  1.10  christos     *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
   2603  1.10  christos     if (expected->cipher != NULL)
   2604  1.10  christos         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
   2605  1.10  christos                                                 expected->cipher, 0);
   2606  1.10  christos     if (expected->digest != NULL)
   2607  1.10  christos         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
   2608  1.10  christos                                                 expected->digest, 0);
   2609  1.10  christos     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, (char *)(intptr_t)"HMAC", 0);
   2610  1.10  christos     *p = OSSL_PARAM_construct_end();
   2611  1.10  christos     if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
   2612  1.10  christos         goto err;
   2613  1.10  christos 
   2614  1.10  christos     strength = EVP_RAND_get_strength(expected->ctx);
   2615  1.10  christos     for (i = 0; i <= expected->n; i++) {
   2616  1.10  christos         item = expected->data + i;
   2617  1.10  christos 
   2618  1.10  christos         p = params;
   2619  1.10  christos         z = item->entropy != NULL ? item->entropy : (unsigned char *)(intptr_t)"";
   2620  1.10  christos         *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
   2621  1.10  christos                                                  z, item->entropy_len);
   2622  1.10  christos         z = item->nonce != NULL ? item->nonce : (unsigned char *)(intptr_t)"";
   2623  1.10  christos         *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
   2624  1.10  christos                                                  z, item->nonce_len);
   2625  1.10  christos         *p = OSSL_PARAM_construct_end();
   2626  1.10  christos         if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
   2627  1.10  christos                                             0, NULL, 0, params)))
   2628  1.10  christos             goto err;
   2629  1.10  christos 
   2630  1.10  christos         z = item->pers != NULL ? item->pers : (unsigned char *)(intptr_t)"";
   2631  1.10  christos         if (!TEST_true(EVP_RAND_instantiate
   2632  1.10  christos                            (expected->ctx, strength,
   2633  1.10  christos                             expected->prediction_resistance, z,
   2634  1.10  christos                             item->pers_len, NULL)))
   2635  1.10  christos             goto err;
   2636  1.10  christos 
   2637  1.10  christos         if (item->reseed_entropy != NULL) {
   2638  1.10  christos             params[0] = OSSL_PARAM_construct_octet_string
   2639  1.10  christos                            (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
   2640  1.10  christos                             item->reseed_entropy_len);
   2641  1.10  christos             params[1] = OSSL_PARAM_construct_end();
   2642  1.10  christos             if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
   2643  1.10  christos                 goto err;
   2644  1.10  christos 
   2645  1.10  christos             if (!TEST_true(EVP_RAND_reseed
   2646  1.10  christos                                (expected->ctx, expected->prediction_resistance,
   2647  1.10  christos                                 NULL, 0, item->reseed_addin,
   2648  1.10  christos                                 item->reseed_addin_len)))
   2649  1.10  christos                 goto err;
   2650  1.10  christos         }
   2651  1.10  christos         if (item->pr_entropyA != NULL) {
   2652  1.10  christos             params[0] = OSSL_PARAM_construct_octet_string
   2653  1.10  christos                            (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
   2654  1.10  christos                             item->pr_entropyA_len);
   2655  1.10  christos             params[1] = OSSL_PARAM_construct_end();
   2656  1.10  christos             if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
   2657  1.10  christos                 goto err;
   2658  1.10  christos         }
   2659  1.10  christos         if (!TEST_true(EVP_RAND_generate
   2660  1.10  christos                            (expected->ctx, got, got_len,
   2661  1.10  christos                             strength, expected->prediction_resistance,
   2662  1.10  christos                             item->addinA, item->addinA_len)))
   2663  1.10  christos             goto err;
   2664  1.10  christos 
   2665  1.10  christos         if (item->pr_entropyB != NULL) {
   2666  1.10  christos             params[0] = OSSL_PARAM_construct_octet_string
   2667  1.10  christos                            (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
   2668  1.10  christos                             item->pr_entropyB_len);
   2669  1.10  christos             params[1] = OSSL_PARAM_construct_end();
   2670  1.10  christos             if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
   2671  1.10  christos                 goto err;
   2672  1.10  christos         }
   2673  1.10  christos         if (!TEST_true(EVP_RAND_generate
   2674  1.10  christos                            (expected->ctx, got, got_len,
   2675  1.10  christos                             strength, expected->prediction_resistance,
   2676  1.10  christos                             item->addinB, item->addinB_len)))
   2677  1.10  christos             goto err;
   2678  1.10  christos         if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
   2679  1.10  christos             goto err;
   2680  1.10  christos         if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
   2681  1.10  christos                 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
   2682  1.10  christos                 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
   2683  1.10  christos                 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
   2684  1.10  christos                                 EVP_RAND_STATE_UNINITIALISED))
   2685  1.10  christos             goto err;
   2686  1.10  christos     }
   2687  1.10  christos     t->err = NULL;
   2688  1.10  christos     ret = 1;
   2689  1.10  christos 
   2690  1.10  christos  err:
   2691  1.10  christos     if (ret == 0 && i >= 0)
   2692  1.10  christos         TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
   2693  1.10  christos     OPENSSL_free(got);
   2694  1.10  christos     return ret;
   2695  1.10  christos }
   2696  1.10  christos 
   2697  1.10  christos static const EVP_TEST_METHOD rand_test_method = {
   2698  1.10  christos     "RAND",
   2699  1.10  christos     rand_test_init,
   2700  1.10  christos     rand_test_cleanup,
   2701  1.10  christos     rand_test_parse,
   2702  1.10  christos     rand_test_run
   2703  1.10  christos };
   2704  1.10  christos 
   2705  1.10  christos 
   2706  1.10  christos /**
   2707  1.10  christos  **  KDF TESTS
   2708  1.10  christos  **/
   2709  1.10  christos typedef struct kdf_data_st {
   2710  1.10  christos     /* Context for this operation */
   2711  1.10  christos     EVP_KDF_CTX *ctx;
   2712  1.10  christos     /* Expected output */
   2713  1.10  christos     unsigned char *output;
   2714  1.10  christos     size_t output_len;
   2715  1.10  christos     OSSL_PARAM params[20];
   2716  1.10  christos     OSSL_PARAM *p;
   2717  1.10  christos } KDF_DATA;
   2718  1.10  christos 
   2719  1.10  christos /*
   2720  1.10  christos  * Perform public key operation setup: lookup key, allocated ctx and call
   2721  1.10  christos  * the appropriate initialisation function
   2722  1.10  christos  */
   2723  1.10  christos static int kdf_test_init(EVP_TEST *t, const char *name)
   2724  1.10  christos {
   2725  1.10  christos     KDF_DATA *kdata;
   2726  1.10  christos     EVP_KDF *kdf;
   2727  1.10  christos 
   2728  1.10  christos     if (is_kdf_disabled(name)) {
   2729  1.10  christos         TEST_info("skipping, '%s' is disabled", name);
   2730  1.10  christos         t->skip = 1;
   2731  1.10  christos         return 1;
   2732  1.10  christos     }
   2733  1.10  christos 
   2734  1.10  christos     if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
   2735  1.10  christos         return 0;
   2736  1.10  christos     kdata->p = kdata->params;
   2737  1.10  christos     *kdata->p = OSSL_PARAM_construct_end();
   2738  1.10  christos 
   2739  1.10  christos     kdf = EVP_KDF_fetch(libctx, name, NULL);
   2740  1.10  christos     if (kdf == NULL) {
   2741  1.10  christos         OPENSSL_free(kdata);
   2742  1.10  christos         return 0;
   2743  1.10  christos     }
   2744  1.10  christos     kdata->ctx = EVP_KDF_CTX_new(kdf);
   2745  1.10  christos     EVP_KDF_free(kdf);
   2746  1.10  christos     if (kdata->ctx == NULL) {
   2747  1.10  christos         OPENSSL_free(kdata);
   2748  1.10  christos         return 0;
   2749  1.10  christos     }
   2750  1.10  christos     t->data = kdata;
   2751  1.10  christos     return 1;
   2752  1.10  christos }
   2753  1.10  christos 
   2754  1.10  christos static void kdf_test_cleanup(EVP_TEST *t)
   2755  1.10  christos {
   2756  1.10  christos     KDF_DATA *kdata = t->data;
   2757  1.10  christos     OSSL_PARAM *p;
   2758  1.10  christos 
   2759  1.10  christos     for (p = kdata->params; p->key != NULL; p++)
   2760  1.10  christos         OPENSSL_free(p->data);
   2761  1.10  christos     OPENSSL_free(kdata->output);
   2762  1.10  christos     EVP_KDF_CTX_free(kdata->ctx);
   2763  1.10  christos }
   2764  1.10  christos 
   2765  1.10  christos static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
   2766  1.10  christos                          const char *value)
   2767  1.10  christos {
   2768  1.10  christos     KDF_DATA *kdata = t->data;
   2769  1.10  christos     int rv;
   2770  1.10  christos     char *p, *name;
   2771  1.10  christos     const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
   2772  1.10  christos 
   2773  1.10  christos     if (!TEST_ptr(name = OPENSSL_strdup(value)))
   2774  1.10  christos         return 0;
   2775  1.10  christos     p = strchr(name, ':');
   2776  1.12  christos     if (p == NULL)
   2777  1.12  christos         p = "";
   2778  1.12  christos     else
   2779  1.10  christos         *p++ = '\0';
   2780  1.10  christos 
   2781  1.10  christos     rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
   2782  1.12  christos                                        strlen(p), NULL);
   2783  1.10  christos     *++kdata->p = OSSL_PARAM_construct_end();
   2784  1.10  christos     if (!rv) {
   2785  1.10  christos         t->err = "KDF_PARAM_ERROR";
   2786  1.10  christos         OPENSSL_free(name);
   2787  1.10  christos         return 0;
   2788  1.10  christos     }
   2789  1.12  christos     if (strcmp(name, "digest") == 0) {
   2790  1.10  christos         if (is_digest_disabled(p)) {
   2791  1.10  christos             TEST_info("skipping, '%s' is disabled", p);
   2792  1.10  christos             t->skip = 1;
   2793  1.10  christos         }
   2794  1.10  christos     }
   2795  1.12  christos 
   2796  1.12  christos     if ((strcmp(name, "cipher") == 0
   2797  1.12  christos         || strcmp(name, "cekalg") == 0)
   2798  1.10  christos         && is_cipher_disabled(p)) {
   2799  1.10  christos         TEST_info("skipping, '%s' is disabled", p);
   2800  1.10  christos         t->skip = 1;
   2801  1.10  christos     }
   2802  1.12  christos 
   2803  1.10  christos     OPENSSL_free(name);
   2804  1.10  christos     return 1;
   2805  1.10  christos }
   2806  1.10  christos 
   2807  1.10  christos static int kdf_test_parse(EVP_TEST *t,
   2808  1.10  christos                           const char *keyword, const char *value)
   2809  1.10  christos {
   2810  1.10  christos     KDF_DATA *kdata = t->data;
   2811  1.10  christos 
   2812  1.10  christos     if (strcmp(keyword, "Output") == 0)
   2813  1.10  christos         return parse_bin(value, &kdata->output, &kdata->output_len);
   2814  1.10  christos     if (strncmp(keyword, "Ctrl", 4) == 0)
   2815  1.10  christos         return kdf_test_ctrl(t, kdata->ctx, value);
   2816  1.10  christos     return 0;
   2817  1.10  christos }
   2818  1.10  christos 
   2819  1.10  christos static int kdf_test_run(EVP_TEST *t)
   2820  1.10  christos {
   2821  1.10  christos     KDF_DATA *expected = t->data;
   2822  1.10  christos     unsigned char *got = NULL;
   2823  1.10  christos     size_t got_len = expected->output_len;
   2824  1.10  christos 
   2825  1.10  christos     if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
   2826  1.10  christos         t->err = "KDF_CTRL_ERROR";
   2827  1.10  christos         return 1;
   2828  1.10  christos     }
   2829  1.10  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
   2830  1.10  christos         t->err = "INTERNAL_ERROR";
   2831   1.2  christos         goto err;
   2832   1.2  christos     }
   2833  1.10  christos     if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
   2834  1.10  christos         t->err = "KDF_DERIVE_ERROR";
   2835   1.2  christos         goto err;
   2836   1.2  christos     }
   2837  1.10  christos     if (!memory_err_compare(t, "KDF_MISMATCH",
   2838  1.10  christos                             expected->output, expected->output_len,
   2839  1.10  christos                             got, got_len))
   2840   1.2  christos         goto err;
   2841   1.2  christos 
   2842   1.4  christos     t->err = NULL;
   2843  1.10  christos 
   2844   1.2  christos  err:
   2845  1.10  christos     OPENSSL_free(got);
   2846   1.2  christos     return 1;
   2847   1.2  christos }
   2848   1.2  christos 
   2849  1.10  christos static const EVP_TEST_METHOD kdf_test_method = {
   2850  1.10  christos     "KDF",
   2851  1.10  christos     kdf_test_init,
   2852  1.10  christos     kdf_test_cleanup,
   2853  1.10  christos     kdf_test_parse,
   2854  1.10  christos     kdf_test_run
   2855   1.2  christos };
   2856   1.2  christos 
   2857   1.4  christos /**
   2858  1.10  christos  **  PKEY KDF TESTS
   2859  1.10  christos  **/
   2860   1.2  christos 
   2861  1.10  christos typedef struct pkey_kdf_data_st {
   2862   1.2  christos     /* Context for this operation */
   2863   1.2  christos     EVP_PKEY_CTX *ctx;
   2864   1.2  christos     /* Expected output */
   2865   1.2  christos     unsigned char *output;
   2866   1.2  christos     size_t output_len;
   2867  1.10  christos } PKEY_KDF_DATA;
   2868   1.2  christos 
   2869   1.2  christos /*
   2870   1.2  christos  * Perform public key operation setup: lookup key, allocated ctx and call
   2871   1.2  christos  * the appropriate initialisation function
   2872   1.2  christos  */
   2873  1.10  christos static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
   2874   1.2  christos {
   2875  1.10  christos     PKEY_KDF_DATA *kdata = NULL;
   2876   1.2  christos 
   2877  1.10  christos     if (is_kdf_disabled(name)) {
   2878  1.10  christos         TEST_info("skipping, '%s' is disabled", name);
   2879   1.4  christos         t->skip = 1;
   2880   1.4  christos         return 1;
   2881   1.4  christos     }
   2882   1.4  christos 
   2883   1.4  christos     if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
   2884   1.2  christos         return 0;
   2885  1.10  christos 
   2886  1.10  christos     kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
   2887  1.10  christos     if (kdata->ctx == NULL
   2888  1.10  christos         || EVP_PKEY_derive_init(kdata->ctx) <= 0)
   2889  1.10  christos         goto err;
   2890  1.10  christos 
   2891   1.4  christos     t->data = kdata;
   2892   1.2  christos     return 1;
   2893  1.10  christos err:
   2894  1.10  christos     EVP_PKEY_CTX_free(kdata->ctx);
   2895  1.10  christos     OPENSSL_free(kdata);
   2896  1.10  christos     return 0;
   2897   1.2  christos }
   2898   1.2  christos 
   2899  1.10  christos static void pkey_kdf_test_cleanup(EVP_TEST *t)
   2900   1.2  christos {
   2901  1.10  christos     PKEY_KDF_DATA *kdata = t->data;
   2902  1.10  christos 
   2903   1.2  christos     OPENSSL_free(kdata->output);
   2904   1.2  christos     EVP_PKEY_CTX_free(kdata->ctx);
   2905   1.2  christos }
   2906   1.2  christos 
   2907  1.10  christos static int pkey_kdf_test_parse(EVP_TEST *t,
   2908  1.10  christos                                const char *keyword, const char *value)
   2909   1.2  christos {
   2910  1.10  christos     PKEY_KDF_DATA *kdata = t->data;
   2911   1.4  christos 
   2912   1.2  christos     if (strcmp(keyword, "Output") == 0)
   2913   1.4  christos         return parse_bin(value, &kdata->output, &kdata->output_len);
   2914   1.2  christos     if (strncmp(keyword, "Ctrl", 4) == 0)
   2915   1.2  christos         return pkey_test_ctrl(t, kdata->ctx, value);
   2916   1.2  christos     return 0;
   2917   1.2  christos }
   2918   1.2  christos 
   2919  1.10  christos static int pkey_kdf_test_run(EVP_TEST *t)
   2920   1.2  christos {
   2921  1.10  christos     PKEY_KDF_DATA *expected = t->data;
   2922   1.4  christos     unsigned char *got = NULL;
   2923  1.10  christos     size_t got_len = 0;
   2924  1.10  christos 
   2925  1.10  christos     if (fips_provider_version_eq(libctx, 3, 0, 0)) {
   2926  1.10  christos         /* FIPS(3.0.0): can't deal with oversized output buffers #18533 */
   2927  1.10  christos         got_len = expected->output_len;
   2928  1.10  christos     } else {
   2929  1.10  christos         /* Find out the KDF output size */
   2930  1.10  christos         if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
   2931  1.10  christos             t->err = "INTERNAL_ERROR";
   2932  1.10  christos             goto err;
   2933  1.10  christos         }
   2934  1.10  christos 
   2935  1.10  christos         /*
   2936  1.10  christos          * We may get an absurd output size, which signals that anything goes.
   2937  1.10  christos          * If not, we specify a too big buffer for the output, to test that
   2938  1.10  christos          * EVP_PKEY_derive() can cope with it.
   2939  1.10  christos          */
   2940  1.10  christos         if (got_len == SIZE_MAX || got_len == 0)
   2941  1.10  christos             got_len = expected->output_len;
   2942  1.10  christos         else
   2943  1.10  christos             got_len = expected->output_len * 2;
   2944  1.10  christos     }
   2945   1.4  christos 
   2946   1.9  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
   2947   1.4  christos         t->err = "INTERNAL_ERROR";
   2948   1.2  christos         goto err;
   2949   1.4  christos     }
   2950   1.4  christos     if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
   2951   1.4  christos         t->err = "KDF_DERIVE_ERROR";
   2952   1.2  christos         goto err;
   2953   1.4  christos     }
   2954  1.10  christos     if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
   2955  1.10  christos         t->err = "KDF_MISMATCH";
   2956   1.2  christos         goto err;
   2957  1.10  christos     }
   2958   1.4  christos     t->err = NULL;
   2959   1.4  christos 
   2960   1.2  christos  err:
   2961   1.4  christos     OPENSSL_free(got);
   2962   1.2  christos     return 1;
   2963   1.2  christos }
   2964   1.2  christos 
   2965  1.10  christos static const EVP_TEST_METHOD pkey_kdf_test_method = {
   2966  1.10  christos     "PKEYKDF",
   2967  1.10  christos     pkey_kdf_test_init,
   2968  1.10  christos     pkey_kdf_test_cleanup,
   2969  1.10  christos     pkey_kdf_test_parse,
   2970  1.10  christos     pkey_kdf_test_run
   2971   1.2  christos };
   2972   1.2  christos 
   2973   1.4  christos /**
   2974  1.10  christos  **  KEYPAIR TESTS
   2975  1.10  christos  **/
   2976   1.4  christos 
   2977   1.4  christos typedef struct keypair_test_data_st {
   2978   1.2  christos     EVP_PKEY *privk;
   2979   1.2  christos     EVP_PKEY *pubk;
   2980   1.4  christos } KEYPAIR_TEST_DATA;
   2981   1.2  christos 
   2982   1.4  christos static int keypair_test_init(EVP_TEST *t, const char *pair)
   2983   1.2  christos {
   2984   1.4  christos     KEYPAIR_TEST_DATA *data;
   2985   1.2  christos     int rv = 0;
   2986   1.2  christos     EVP_PKEY *pk = NULL, *pubk = NULL;
   2987   1.2  christos     char *pub, *priv = NULL;
   2988   1.2  christos 
   2989   1.4  christos     /* Split private and public names. */
   2990   1.4  christos     if (!TEST_ptr(priv = OPENSSL_strdup(pair))
   2991   1.4  christos             || !TEST_ptr(pub = strchr(priv, ':'))) {
   2992   1.4  christos         t->err = "PARSING_ERROR";
   2993   1.2  christos         goto end;
   2994   1.2  christos     }
   2995   1.4  christos     *pub++ = '\0';
   2996   1.2  christos 
   2997   1.4  christos     if (!TEST_true(find_key(&pk, priv, private_keys))) {
   2998   1.4  christos         TEST_info("Can't find private key: %s", priv);
   2999   1.4  christos         t->err = "MISSING_PRIVATE_KEY";
   3000   1.2  christos         goto end;
   3001   1.2  christos     }
   3002   1.4  christos     if (!TEST_true(find_key(&pubk, pub, public_keys))) {
   3003   1.4  christos         TEST_info("Can't find public key: %s", pub);
   3004   1.4  christos         t->err = "MISSING_PUBLIC_KEY";
   3005   1.2  christos         goto end;
   3006   1.2  christos     }
   3007   1.2  christos 
   3008   1.2  christos     if (pk == NULL && pubk == NULL) {
   3009   1.2  christos         /* Both keys are listed but unsupported: skip this test */
   3010   1.2  christos         t->skip = 1;
   3011   1.2  christos         rv = 1;
   3012   1.2  christos         goto end;
   3013   1.2  christos     }
   3014   1.2  christos 
   3015   1.4  christos     if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
   3016   1.2  christos         goto end;
   3017   1.2  christos     data->privk = pk;
   3018   1.2  christos     data->pubk = pubk;
   3019   1.2  christos     t->data = data;
   3020   1.2  christos     rv = 1;
   3021   1.4  christos     t->err = NULL;
   3022   1.2  christos 
   3023   1.2  christos end:
   3024   1.4  christos     OPENSSL_free(priv);
   3025   1.2  christos     return rv;
   3026   1.2  christos }
   3027   1.2  christos 
   3028   1.4  christos static void keypair_test_cleanup(EVP_TEST *t)
   3029   1.2  christos {
   3030   1.4  christos     OPENSSL_free(t->data);
   3031   1.2  christos     t->data = NULL;
   3032   1.2  christos }
   3033   1.2  christos 
   3034   1.4  christos /*
   3035   1.4  christos  * For tests that do not accept any custom keywords.
   3036   1.2  christos  */
   3037   1.4  christos static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
   3038   1.2  christos {
   3039   1.2  christos     return 0;
   3040   1.2  christos }
   3041   1.2  christos 
   3042   1.4  christos static int keypair_test_run(EVP_TEST *t)
   3043   1.2  christos {
   3044   1.2  christos     int rv = 0;
   3045   1.4  christos     const KEYPAIR_TEST_DATA *pair = t->data;
   3046   1.2  christos 
   3047   1.2  christos     if (pair->privk == NULL || pair->pubk == NULL) {
   3048   1.4  christos         /*
   3049   1.4  christos          * this can only happen if only one of the keys is not set
   3050   1.2  christos          * which means that one of them was unsupported while the
   3051   1.2  christos          * other isn't: hence a key type mismatch.
   3052   1.2  christos          */
   3053   1.4  christos         t->err = "KEYPAIR_TYPE_MISMATCH";
   3054   1.2  christos         rv = 1;
   3055   1.2  christos         goto end;
   3056   1.2  christos     }
   3057   1.2  christos 
   3058  1.10  christos     if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
   3059   1.2  christos         if ( 0 == rv ) {
   3060   1.4  christos             t->err = "KEYPAIR_MISMATCH";
   3061   1.2  christos         } else if ( -1 == rv ) {
   3062   1.4  christos             t->err = "KEYPAIR_TYPE_MISMATCH";
   3063   1.2  christos         } else if ( -2 == rv ) {
   3064   1.4  christos             t->err = "UNSUPPORTED_KEY_COMPARISON";
   3065   1.2  christos         } else {
   3066   1.4  christos             TEST_error("Unexpected error in key comparison");
   3067   1.2  christos             rv = 0;
   3068   1.2  christos             goto end;
   3069   1.2  christos         }
   3070   1.2  christos         rv = 1;
   3071   1.2  christos         goto end;
   3072   1.2  christos     }
   3073   1.2  christos 
   3074   1.2  christos     rv = 1;
   3075   1.4  christos     t->err = NULL;
   3076   1.2  christos 
   3077   1.2  christos end:
   3078   1.2  christos     return rv;
   3079   1.2  christos }
   3080   1.2  christos 
   3081   1.4  christos static const EVP_TEST_METHOD keypair_test_method = {
   3082   1.2  christos     "PrivPubKeyPair",
   3083   1.2  christos     keypair_test_init,
   3084   1.2  christos     keypair_test_cleanup,
   3085   1.2  christos     void_test_parse,
   3086   1.2  christos     keypair_test_run
   3087   1.2  christos };
   3088   1.2  christos 
   3089   1.4  christos /**
   3090  1.10  christos  **  KEYGEN TEST
   3091  1.10  christos  **/
   3092   1.4  christos 
   3093   1.4  christos typedef struct keygen_test_data_st {
   3094   1.4  christos     EVP_PKEY_CTX *genctx; /* Keygen context to use */
   3095   1.4  christos     char *keyname; /* Key name to store key or NULL */
   3096   1.4  christos } KEYGEN_TEST_DATA;
   3097   1.4  christos 
   3098   1.4  christos static int keygen_test_init(EVP_TEST *t, const char *alg)
   3099   1.4  christos {
   3100   1.4  christos     KEYGEN_TEST_DATA *data;
   3101   1.4  christos     EVP_PKEY_CTX *genctx;
   3102   1.4  christos     int nid = OBJ_sn2nid(alg);
   3103   1.4  christos 
   3104   1.4  christos     if (nid == NID_undef) {
   3105   1.4  christos         nid = OBJ_ln2nid(alg);
   3106   1.4  christos         if (nid == NID_undef)
   3107   1.4  christos             return 0;
   3108   1.4  christos     }
   3109   1.4  christos 
   3110  1.10  christos     if (is_pkey_disabled(alg)) {
   3111   1.4  christos         t->skip = 1;
   3112   1.4  christos         return 1;
   3113   1.4  christos     }
   3114  1.10  christos     if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
   3115  1.10  christos         goto err;
   3116   1.4  christos 
   3117   1.4  christos     if (EVP_PKEY_keygen_init(genctx) <= 0) {
   3118   1.4  christos         t->err = "KEYGEN_INIT_ERROR";
   3119   1.4  christos         goto err;
   3120   1.4  christos     }
   3121   1.4  christos 
   3122   1.4  christos     if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
   3123   1.4  christos         goto err;
   3124   1.4  christos     data->genctx = genctx;
   3125   1.4  christos     data->keyname = NULL;
   3126   1.4  christos     t->data = data;
   3127   1.4  christos     t->err = NULL;
   3128   1.4  christos     return 1;
   3129   1.4  christos 
   3130   1.4  christos err:
   3131   1.4  christos     EVP_PKEY_CTX_free(genctx);
   3132   1.4  christos     return 0;
   3133   1.4  christos }
   3134   1.4  christos 
   3135   1.4  christos static void keygen_test_cleanup(EVP_TEST *t)
   3136   1.4  christos {
   3137   1.4  christos     KEYGEN_TEST_DATA *keygen = t->data;
   3138   1.4  christos 
   3139   1.4  christos     EVP_PKEY_CTX_free(keygen->genctx);
   3140   1.4  christos     OPENSSL_free(keygen->keyname);
   3141   1.4  christos     OPENSSL_free(t->data);
   3142   1.4  christos     t->data = NULL;
   3143   1.4  christos }
   3144   1.4  christos 
   3145   1.4  christos static int keygen_test_parse(EVP_TEST *t,
   3146   1.4  christos                              const char *keyword, const char *value)
   3147   1.4  christos {
   3148   1.4  christos     KEYGEN_TEST_DATA *keygen = t->data;
   3149   1.4  christos 
   3150   1.4  christos     if (strcmp(keyword, "KeyName") == 0)
   3151   1.4  christos         return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
   3152   1.4  christos     if (strcmp(keyword, "Ctrl") == 0)
   3153   1.4  christos         return pkey_test_ctrl(t, keygen->genctx, value);
   3154   1.4  christos     return 0;
   3155   1.4  christos }
   3156   1.4  christos 
   3157   1.4  christos static int keygen_test_run(EVP_TEST *t)
   3158   1.4  christos {
   3159   1.4  christos     KEYGEN_TEST_DATA *keygen = t->data;
   3160   1.4  christos     EVP_PKEY *pkey = NULL;
   3161  1.10  christos     int rv = 1;
   3162   1.4  christos 
   3163   1.4  christos     if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
   3164   1.4  christos         t->err = "KEYGEN_GENERATE_ERROR";
   3165   1.4  christos         goto err;
   3166   1.4  christos     }
   3167   1.4  christos 
   3168  1.10  christos     if (!evp_pkey_is_provided(pkey)) {
   3169  1.10  christos         TEST_info("Warning: legacy key generated %s", keygen->keyname);
   3170  1.10  christos         goto err;
   3171  1.10  christos     }
   3172   1.4  christos     if (keygen->keyname != NULL) {
   3173   1.4  christos         KEY_LIST *key;
   3174   1.4  christos 
   3175  1.10  christos         rv = 0;
   3176   1.4  christos         if (find_key(NULL, keygen->keyname, private_keys)) {
   3177   1.4  christos             TEST_info("Duplicate key %s", keygen->keyname);
   3178   1.4  christos             goto err;
   3179   1.4  christos         }
   3180   1.4  christos 
   3181   1.4  christos         if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
   3182   1.4  christos             goto err;
   3183   1.4  christos         key->name = keygen->keyname;
   3184   1.4  christos         keygen->keyname = NULL;
   3185   1.4  christos         key->key = pkey;
   3186   1.4  christos         key->next = private_keys;
   3187   1.4  christos         private_keys = key;
   3188  1.10  christos         rv = 1;
   3189   1.4  christos     } else {
   3190   1.4  christos         EVP_PKEY_free(pkey);
   3191   1.4  christos     }
   3192   1.4  christos 
   3193  1.10  christos     t->err = NULL;
   3194   1.4  christos 
   3195   1.4  christos err:
   3196  1.10  christos     return rv;
   3197   1.4  christos }
   3198   1.4  christos 
   3199   1.4  christos static const EVP_TEST_METHOD keygen_test_method = {
   3200   1.4  christos     "KeyGen",
   3201   1.4  christos     keygen_test_init,
   3202   1.4  christos     keygen_test_cleanup,
   3203   1.4  christos     keygen_test_parse,
   3204   1.4  christos     keygen_test_run,
   3205   1.4  christos };
   3206   1.4  christos 
   3207   1.4  christos /**
   3208  1.10  christos  **  DIGEST SIGN+VERIFY TESTS
   3209  1.10  christos  **/
   3210   1.4  christos 
   3211   1.4  christos typedef struct {
   3212   1.4  christos     int is_verify; /* Set to 1 if verifying */
   3213   1.4  christos     int is_oneshot; /* Set to 1 for one shot operation */
   3214   1.4  christos     const EVP_MD *md; /* Digest to use */
   3215   1.4  christos     EVP_MD_CTX *ctx; /* Digest context */
   3216   1.4  christos     EVP_PKEY_CTX *pctx;
   3217   1.4  christos     STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
   3218   1.4  christos     unsigned char *osin; /* Input data if one shot */
   3219   1.4  christos     size_t osin_len; /* Input length data if one shot */
   3220   1.4  christos     unsigned char *output; /* Expected output */
   3221   1.4  christos     size_t output_len; /* Expected output length */
   3222   1.4  christos } DIGESTSIGN_DATA;
   3223   1.4  christos 
   3224   1.4  christos static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
   3225   1.4  christos                                   int is_oneshot)
   3226   1.4  christos {
   3227   1.4  christos     const EVP_MD *md = NULL;
   3228   1.4  christos     DIGESTSIGN_DATA *mdat;
   3229   1.4  christos 
   3230   1.4  christos     if (strcmp(alg, "NULL") != 0) {
   3231  1.10  christos         if (is_digest_disabled(alg)) {
   3232  1.10  christos             t->skip = 1;
   3233  1.10  christos             return 1;
   3234  1.10  christos         }
   3235  1.10  christos         md = EVP_get_digestbyname(alg);
   3236  1.10  christos         if (md == NULL)
   3237   1.4  christos             return 0;
   3238   1.4  christos     }
   3239   1.4  christos     if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
   3240   1.4  christos         return 0;
   3241   1.4  christos     mdat->md = md;
   3242   1.4  christos     if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
   3243   1.4  christos         OPENSSL_free(mdat);
   3244   1.4  christos         return 0;
   3245   1.4  christos     }
   3246   1.4  christos     mdat->is_verify = is_verify;
   3247   1.4  christos     mdat->is_oneshot = is_oneshot;
   3248   1.4  christos     t->data = mdat;
   3249   1.4  christos     return 1;
   3250   1.4  christos }
   3251   1.4  christos 
   3252   1.4  christos static int digestsign_test_init(EVP_TEST *t, const char *alg)
   3253   1.4  christos {
   3254   1.4  christos     return digestsigver_test_init(t, alg, 0, 0);
   3255   1.4  christos }
   3256   1.4  christos 
   3257   1.4  christos static void digestsigver_test_cleanup(EVP_TEST *t)
   3258   1.4  christos {
   3259   1.4  christos     DIGESTSIGN_DATA *mdata = t->data;
   3260   1.4  christos 
   3261   1.4  christos     EVP_MD_CTX_free(mdata->ctx);
   3262   1.4  christos     sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
   3263   1.4  christos     OPENSSL_free(mdata->osin);
   3264   1.4  christos     OPENSSL_free(mdata->output);
   3265   1.4  christos     OPENSSL_free(mdata);
   3266   1.4  christos     t->data = NULL;
   3267   1.4  christos }
   3268   1.4  christos 
   3269   1.4  christos static int digestsigver_test_parse(EVP_TEST *t,
   3270   1.4  christos                                    const char *keyword, const char *value)
   3271   1.4  christos {
   3272   1.4  christos     DIGESTSIGN_DATA *mdata = t->data;
   3273   1.4  christos 
   3274   1.4  christos     if (strcmp(keyword, "Key") == 0) {
   3275   1.4  christos         EVP_PKEY *pkey = NULL;
   3276   1.4  christos         int rv = 0;
   3277  1.10  christos         const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
   3278   1.4  christos 
   3279   1.4  christos         if (mdata->is_verify)
   3280   1.4  christos             rv = find_key(&pkey, value, public_keys);
   3281   1.4  christos         if (rv == 0)
   3282   1.4  christos             rv = find_key(&pkey, value, private_keys);
   3283   1.4  christos         if (rv == 0 || pkey == NULL) {
   3284   1.4  christos             t->skip = 1;
   3285   1.4  christos             return 1;
   3286   1.4  christos         }
   3287   1.4  christos         if (mdata->is_verify) {
   3288  1.10  christos             if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
   3289  1.10  christos                                          NULL, pkey, NULL))
   3290   1.4  christos                 t->err = "DIGESTVERIFYINIT_ERROR";
   3291   1.4  christos             return 1;
   3292   1.4  christos         }
   3293  1.10  christos         if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
   3294  1.10  christos                                    pkey, NULL))
   3295   1.4  christos             t->err = "DIGESTSIGNINIT_ERROR";
   3296   1.4  christos         return 1;
   3297   1.4  christos     }
   3298   1.4  christos 
   3299   1.4  christos     if (strcmp(keyword, "Input") == 0) {
   3300   1.4  christos         if (mdata->is_oneshot)
   3301   1.4  christos             return parse_bin(value, &mdata->osin, &mdata->osin_len);
   3302   1.4  christos         return evp_test_buffer_append(value, &mdata->input);
   3303   1.4  christos     }
   3304   1.4  christos     if (strcmp(keyword, "Output") == 0)
   3305   1.4  christos         return parse_bin(value, &mdata->output, &mdata->output_len);
   3306   1.4  christos 
   3307   1.4  christos     if (!mdata->is_oneshot) {
   3308   1.4  christos         if (strcmp(keyword, "Count") == 0)
   3309   1.4  christos             return evp_test_buffer_set_count(value, mdata->input);
   3310   1.4  christos         if (strcmp(keyword, "Ncopy") == 0)
   3311   1.4  christos             return evp_test_buffer_ncopy(value, mdata->input);
   3312   1.4  christos     }
   3313   1.4  christos     if (strcmp(keyword, "Ctrl") == 0) {
   3314   1.4  christos         if (mdata->pctx == NULL)
   3315   1.8  christos             return -1;
   3316   1.4  christos         return pkey_test_ctrl(t, mdata->pctx, value);
   3317   1.4  christos     }
   3318   1.4  christos     return 0;
   3319   1.4  christos }
   3320   1.4  christos 
   3321   1.4  christos static int digestsign_update_fn(void *ctx, const unsigned char *buf,
   3322   1.4  christos                                 size_t buflen)
   3323   1.4  christos {
   3324   1.4  christos     return EVP_DigestSignUpdate(ctx, buf, buflen);
   3325   1.4  christos }
   3326   1.4  christos 
   3327   1.4  christos static int digestsign_test_run(EVP_TEST *t)
   3328   1.4  christos {
   3329   1.4  christos     DIGESTSIGN_DATA *expected = t->data;
   3330   1.4  christos     unsigned char *got = NULL;
   3331   1.4  christos     size_t got_len;
   3332   1.4  christos 
   3333   1.4  christos     if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
   3334   1.4  christos                             expected->ctx)) {
   3335   1.4  christos         t->err = "DIGESTUPDATE_ERROR";
   3336   1.4  christos         goto err;
   3337   1.4  christos     }
   3338   1.4  christos 
   3339   1.4  christos     if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
   3340   1.4  christos         t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
   3341   1.4  christos         goto err;
   3342   1.4  christos     }
   3343   1.4  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
   3344   1.4  christos         t->err = "MALLOC_FAILURE";
   3345   1.4  christos         goto err;
   3346   1.4  christos     }
   3347  1.10  christos     got_len *= 2;
   3348   1.4  christos     if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
   3349   1.4  christos         t->err = "DIGESTSIGNFINAL_ERROR";
   3350   1.4  christos         goto err;
   3351   1.4  christos     }
   3352   1.4  christos     if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
   3353   1.4  christos                             expected->output, expected->output_len,
   3354   1.4  christos                             got, got_len))
   3355   1.4  christos         goto err;
   3356   1.4  christos 
   3357   1.4  christos     t->err = NULL;
   3358   1.4  christos  err:
   3359   1.4  christos     OPENSSL_free(got);
   3360   1.4  christos     return 1;
   3361   1.4  christos }
   3362   1.4  christos 
   3363   1.4  christos static const EVP_TEST_METHOD digestsign_test_method = {
   3364   1.4  christos     "DigestSign",
   3365   1.4  christos     digestsign_test_init,
   3366   1.4  christos     digestsigver_test_cleanup,
   3367   1.4  christos     digestsigver_test_parse,
   3368   1.4  christos     digestsign_test_run
   3369   1.4  christos };
   3370   1.4  christos 
   3371   1.4  christos static int digestverify_test_init(EVP_TEST *t, const char *alg)
   3372   1.4  christos {
   3373   1.4  christos     return digestsigver_test_init(t, alg, 1, 0);
   3374   1.4  christos }
   3375   1.4  christos 
   3376   1.4  christos static int digestverify_update_fn(void *ctx, const unsigned char *buf,
   3377   1.4  christos                                   size_t buflen)
   3378   1.4  christos {
   3379   1.4  christos     return EVP_DigestVerifyUpdate(ctx, buf, buflen);
   3380   1.4  christos }
   3381   1.4  christos 
   3382   1.4  christos static int digestverify_test_run(EVP_TEST *t)
   3383   1.4  christos {
   3384   1.4  christos     DIGESTSIGN_DATA *mdata = t->data;
   3385   1.4  christos 
   3386   1.4  christos     if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
   3387   1.4  christos         t->err = "DIGESTUPDATE_ERROR";
   3388   1.4  christos         return 1;
   3389   1.4  christos     }
   3390   1.4  christos 
   3391   1.4  christos     if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
   3392   1.4  christos                               mdata->output_len) <= 0)
   3393   1.4  christos         t->err = "VERIFY_ERROR";
   3394   1.4  christos     return 1;
   3395   1.4  christos }
   3396   1.4  christos 
   3397   1.4  christos static const EVP_TEST_METHOD digestverify_test_method = {
   3398   1.4  christos     "DigestVerify",
   3399   1.4  christos     digestverify_test_init,
   3400   1.4  christos     digestsigver_test_cleanup,
   3401   1.4  christos     digestsigver_test_parse,
   3402   1.4  christos     digestverify_test_run
   3403   1.4  christos };
   3404   1.4  christos 
   3405   1.4  christos static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
   3406   1.4  christos {
   3407   1.4  christos     return digestsigver_test_init(t, alg, 0, 1);
   3408   1.4  christos }
   3409   1.4  christos 
   3410   1.4  christos static int oneshot_digestsign_test_run(EVP_TEST *t)
   3411   1.4  christos {
   3412   1.4  christos     DIGESTSIGN_DATA *expected = t->data;
   3413   1.4  christos     unsigned char *got = NULL;
   3414   1.4  christos     size_t got_len;
   3415   1.4  christos 
   3416   1.4  christos     if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
   3417   1.4  christos                         expected->osin, expected->osin_len)) {
   3418   1.4  christos         t->err = "DIGESTSIGN_LENGTH_ERROR";
   3419   1.4  christos         goto err;
   3420   1.4  christos     }
   3421   1.4  christos     if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
   3422   1.4  christos         t->err = "MALLOC_FAILURE";
   3423   1.4  christos         goto err;
   3424   1.4  christos     }
   3425  1.10  christos     got_len *= 2;
   3426   1.4  christos     if (!EVP_DigestSign(expected->ctx, got, &got_len,
   3427   1.4  christos                         expected->osin, expected->osin_len)) {
   3428   1.4  christos         t->err = "DIGESTSIGN_ERROR";
   3429   1.4  christos         goto err;
   3430   1.4  christos     }
   3431   1.4  christos     if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
   3432   1.4  christos                             expected->output, expected->output_len,
   3433   1.4  christos                             got, got_len))
   3434   1.4  christos         goto err;
   3435   1.4  christos 
   3436   1.4  christos     t->err = NULL;
   3437   1.4  christos  err:
   3438   1.4  christos     OPENSSL_free(got);
   3439   1.4  christos     return 1;
   3440   1.4  christos }
   3441   1.4  christos 
   3442   1.4  christos static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
   3443   1.4  christos     "OneShotDigestSign",
   3444   1.4  christos     oneshot_digestsign_test_init,
   3445   1.4  christos     digestsigver_test_cleanup,
   3446   1.4  christos     digestsigver_test_parse,
   3447   1.4  christos     oneshot_digestsign_test_run
   3448   1.4  christos };
   3449   1.4  christos 
   3450   1.4  christos static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
   3451   1.4  christos {
   3452   1.4  christos     return digestsigver_test_init(t, alg, 1, 1);
   3453   1.4  christos }
   3454   1.4  christos 
   3455   1.4  christos static int oneshot_digestverify_test_run(EVP_TEST *t)
   3456   1.4  christos {
   3457   1.4  christos     DIGESTSIGN_DATA *mdata = t->data;
   3458   1.4  christos 
   3459   1.4  christos     if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
   3460   1.4  christos                          mdata->osin, mdata->osin_len) <= 0)
   3461   1.4  christos         t->err = "VERIFY_ERROR";
   3462   1.4  christos     return 1;
   3463   1.4  christos }
   3464   1.4  christos 
   3465   1.4  christos static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
   3466   1.4  christos     "OneShotDigestVerify",
   3467   1.4  christos     oneshot_digestverify_test_init,
   3468   1.4  christos     digestsigver_test_cleanup,
   3469   1.4  christos     digestsigver_test_parse,
   3470   1.4  christos     oneshot_digestverify_test_run
   3471   1.4  christos };
   3472   1.4  christos 
   3473   1.4  christos 
   3474   1.4  christos /**
   3475  1.10  christos  **  PARSING AND DISPATCH
   3476  1.10  christos  **/
   3477   1.4  christos 
   3478   1.4  christos static const EVP_TEST_METHOD *evp_test_list[] = {
   3479  1.10  christos     &rand_test_method,
   3480   1.4  christos     &cipher_test_method,
   3481   1.4  christos     &digest_test_method,
   3482   1.4  christos     &digestsign_test_method,
   3483   1.4  christos     &digestverify_test_method,
   3484   1.4  christos     &encode_test_method,
   3485   1.4  christos     &kdf_test_method,
   3486  1.10  christos     &pkey_kdf_test_method,
   3487   1.4  christos     &keypair_test_method,
   3488   1.4  christos     &keygen_test_method,
   3489   1.4  christos     &mac_test_method,
   3490   1.4  christos     &oneshot_digestsign_test_method,
   3491   1.4  christos     &oneshot_digestverify_test_method,
   3492   1.4  christos     &pbe_test_method,
   3493   1.4  christos     &pdecrypt_test_method,
   3494   1.4  christos     &pderive_test_method,
   3495   1.4  christos     &psign_test_method,
   3496   1.4  christos     &pverify_recover_test_method,
   3497   1.4  christos     &pverify_test_method,
   3498   1.4  christos     NULL
   3499   1.4  christos };
   3500   1.4  christos 
   3501   1.4  christos static const EVP_TEST_METHOD *find_test(const char *name)
   3502   1.4  christos {
   3503   1.4  christos     const EVP_TEST_METHOD **tt;
   3504   1.4  christos 
   3505   1.4  christos     for (tt = evp_test_list; *tt; tt++) {
   3506   1.4  christos         if (strcmp(name, (*tt)->name) == 0)
   3507   1.4  christos             return *tt;
   3508   1.4  christos     }
   3509   1.4  christos     return NULL;
   3510   1.4  christos }
   3511   1.4  christos 
   3512   1.4  christos static void clear_test(EVP_TEST *t)
   3513   1.4  christos {
   3514   1.4  christos     test_clearstanza(&t->s);
   3515   1.4  christos     ERR_clear_error();
   3516   1.4  christos     if (t->data != NULL) {
   3517   1.4  christos         if (t->meth != NULL)
   3518   1.4  christos             t->meth->cleanup(t);
   3519   1.4  christos         OPENSSL_free(t->data);
   3520   1.4  christos         t->data = NULL;
   3521   1.4  christos     }
   3522   1.4  christos     OPENSSL_free(t->expected_err);
   3523   1.4  christos     t->expected_err = NULL;
   3524   1.4  christos     OPENSSL_free(t->reason);
   3525   1.4  christos     t->reason = NULL;
   3526   1.4  christos 
   3527   1.4  christos     /* Text literal. */
   3528   1.4  christos     t->err = NULL;
   3529   1.4  christos     t->skip = 0;
   3530   1.4  christos     t->meth = NULL;
   3531   1.4  christos }
   3532   1.4  christos 
   3533  1.10  christos /* Check for errors in the test structure; return 1 if okay, else 0. */
   3534   1.4  christos static int check_test_error(EVP_TEST *t)
   3535   1.4  christos {
   3536   1.4  christos     unsigned long err;
   3537   1.4  christos     const char *reason;
   3538   1.4  christos 
   3539   1.4  christos     if (t->err == NULL && t->expected_err == NULL)
   3540   1.4  christos         return 1;
   3541   1.4  christos     if (t->err != NULL && t->expected_err == NULL) {
   3542   1.4  christos         if (t->aux_err != NULL) {
   3543   1.4  christos             TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
   3544   1.4  christos                       t->s.test_file, t->s.start, t->aux_err, t->err);
   3545   1.4  christos         } else {
   3546   1.4  christos             TEST_info("%s:%d: Source of above error; unexpected error %s",
   3547   1.4  christos                       t->s.test_file, t->s.start, t->err);
   3548   1.4  christos         }
   3549   1.4  christos         return 0;
   3550   1.4  christos     }
   3551   1.4  christos     if (t->err == NULL && t->expected_err != NULL) {
   3552   1.4  christos         TEST_info("%s:%d: Succeeded but was expecting %s",
   3553   1.4  christos                   t->s.test_file, t->s.start, t->expected_err);
   3554   1.4  christos         return 0;
   3555   1.4  christos     }
   3556   1.4  christos 
   3557   1.4  christos     if (strcmp(t->err, t->expected_err) != 0) {
   3558   1.4  christos         TEST_info("%s:%d: Expected %s got %s",
   3559   1.4  christos                   t->s.test_file, t->s.start, t->expected_err, t->err);
   3560   1.4  christos         return 0;
   3561   1.4  christos     }
   3562   1.4  christos 
   3563  1.10  christos     if (t->reason == NULL)
   3564   1.4  christos         return 1;
   3565   1.4  christos 
   3566  1.10  christos     if (t->reason == NULL) {
   3567   1.4  christos         TEST_info("%s:%d: Test is missing function or reason code",
   3568   1.4  christos                   t->s.test_file, t->s.start);
   3569   1.4  christos         return 0;
   3570   1.4  christos     }
   3571   1.4  christos 
   3572   1.4  christos     err = ERR_peek_error();
   3573   1.4  christos     if (err == 0) {
   3574  1.10  christos         TEST_info("%s:%d: Expected error \"%s\" not set",
   3575  1.10  christos                   t->s.test_file, t->s.start, t->reason);
   3576   1.4  christos         return 0;
   3577   1.4  christos     }
   3578   1.4  christos 
   3579   1.4  christos     reason = ERR_reason_error_string(err);
   3580  1.10  christos     if (reason == NULL) {
   3581  1.10  christos         TEST_info("%s:%d: Expected error \"%s\", no strings available."
   3582   1.4  christos                   " Assuming ok.",
   3583  1.10  christos                   t->s.test_file, t->s.start, t->reason);
   3584   1.4  christos         return 1;
   3585   1.4  christos     }
   3586   1.4  christos 
   3587  1.10  christos     if (strcmp(reason, t->reason) == 0)
   3588   1.4  christos         return 1;
   3589   1.4  christos 
   3590  1.10  christos     TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
   3591  1.10  christos               t->s.test_file, t->s.start, t->reason, reason);
   3592   1.4  christos 
   3593   1.4  christos     return 0;
   3594   1.4  christos }
   3595   1.4  christos 
   3596  1.10  christos /* Run a parsed test. Log a message and return 0 on error. */
   3597   1.4  christos static int run_test(EVP_TEST *t)
   3598   1.4  christos {
   3599   1.4  christos     if (t->meth == NULL)
   3600   1.4  christos         return 1;
   3601   1.4  christos     t->s.numtests++;
   3602   1.4  christos     if (t->skip) {
   3603   1.4  christos         t->s.numskip++;
   3604   1.4  christos     } else {
   3605   1.4  christos         /* run the test */
   3606   1.4  christos         if (t->err == NULL && t->meth->run_test(t) != 1) {
   3607   1.4  christos             TEST_info("%s:%d %s error",
   3608   1.4  christos                       t->s.test_file, t->s.start, t->meth->name);
   3609   1.4  christos             return 0;
   3610   1.4  christos         }
   3611   1.4  christos         if (!check_test_error(t)) {
   3612   1.4  christos             TEST_openssl_errors();
   3613   1.4  christos             t->s.errors++;
   3614   1.4  christos         }
   3615   1.4  christos     }
   3616   1.4  christos 
   3617   1.4  christos     /* clean it up */
   3618   1.4  christos     return 1;
   3619   1.4  christos }
   3620   1.4  christos 
   3621   1.4  christos static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
   3622   1.4  christos {
   3623   1.4  christos     for (; lst != NULL; lst = lst->next) {
   3624   1.4  christos         if (strcmp(lst->name, name) == 0) {
   3625   1.4  christos             if (ppk != NULL)
   3626   1.4  christos                 *ppk = lst->key;
   3627   1.4  christos             return 1;
   3628   1.4  christos         }
   3629   1.4  christos     }
   3630   1.4  christos     return 0;
   3631   1.4  christos }
   3632   1.4  christos 
   3633   1.4  christos static void free_key_list(KEY_LIST *lst)
   3634   1.4  christos {
   3635   1.4  christos     while (lst != NULL) {
   3636   1.4  christos         KEY_LIST *next = lst->next;
   3637   1.4  christos 
   3638   1.4  christos         EVP_PKEY_free(lst->key);
   3639   1.4  christos         OPENSSL_free(lst->name);
   3640   1.4  christos         OPENSSL_free(lst);
   3641   1.4  christos         lst = next;
   3642   1.4  christos     }
   3643   1.4  christos }
   3644   1.4  christos 
   3645   1.4  christos /*
   3646   1.4  christos  * Is the key type an unsupported algorithm?
   3647   1.4  christos  */
   3648   1.4  christos static int key_unsupported(void)
   3649   1.4  christos {
   3650  1.10  christos     long err = ERR_peek_last_error();
   3651  1.10  christos     int lib = ERR_GET_LIB(err);
   3652  1.10  christos     long reason = ERR_GET_REASON(err);
   3653  1.10  christos 
   3654  1.10  christos     if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
   3655  1.10  christos         || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
   3656  1.10  christos         || reason == ERR_R_UNSUPPORTED) {
   3657   1.4  christos         ERR_clear_error();
   3658   1.4  christos         return 1;
   3659   1.4  christos     }
   3660   1.4  christos #ifndef OPENSSL_NO_EC
   3661   1.4  christos     /*
   3662   1.4  christos      * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
   3663   1.4  christos      * hint to an unsupported algorithm/curve (e.g. if binary EC support is
   3664   1.4  christos      * disabled).
   3665   1.4  christos      */
   3666  1.10  christos     if (lib == ERR_LIB_EC
   3667  1.10  christos         && (reason == EC_R_UNKNOWN_GROUP
   3668  1.10  christos             || reason == EC_R_INVALID_CURVE)) {
   3669   1.4  christos         ERR_clear_error();
   3670   1.4  christos         return 1;
   3671   1.4  christos     }
   3672   1.4  christos #endif /* OPENSSL_NO_EC */
   3673   1.4  christos     return 0;
   3674   1.4  christos }
   3675   1.4  christos 
   3676  1.10  christos /* NULL out the value from |pp| but return it.  This "steals" a pointer. */
   3677   1.4  christos static char *take_value(PAIR *pp)
   3678   1.4  christos {
   3679   1.4  christos     char *p = pp->value;
   3680   1.4  christos 
   3681   1.4  christos     pp->value = NULL;
   3682   1.4  christos     return p;
   3683   1.4  christos }
   3684   1.4  christos 
   3685  1.10  christos #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
   3686  1.10  christos static int securitycheck_enabled(void)
   3687  1.10  christos {
   3688  1.10  christos     static int enabled = -1;
   3689  1.10  christos 
   3690  1.10  christos     if (enabled == -1) {
   3691  1.10  christos         if (OSSL_PROVIDER_available(libctx, "fips")) {
   3692  1.10  christos             OSSL_PARAM params[2];
   3693  1.10  christos             OSSL_PROVIDER *prov = NULL;
   3694  1.10  christos             int check = 1;
   3695  1.10  christos 
   3696  1.10  christos             prov = OSSL_PROVIDER_load(libctx, "fips");
   3697  1.10  christos             if (prov != NULL) {
   3698  1.10  christos                 params[0] =
   3699  1.10  christos                     OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
   3700  1.10  christos                                              &check);
   3701  1.10  christos                 params[1] = OSSL_PARAM_construct_end();
   3702  1.10  christos                 OSSL_PROVIDER_get_params(prov, params);
   3703  1.10  christos                 OSSL_PROVIDER_unload(prov);
   3704  1.10  christos             }
   3705  1.10  christos             enabled = check;
   3706  1.10  christos             return enabled;
   3707  1.10  christos         }
   3708  1.10  christos         enabled = 0;
   3709  1.10  christos     }
   3710  1.10  christos     return enabled;
   3711  1.10  christos }
   3712  1.10  christos #endif
   3713  1.10  christos 
   3714   1.4  christos /*
   3715  1.10  christos  * Return 1 if one of the providers named in the string is available.
   3716  1.10  christos  * The provider names are separated with whitespace.
   3717  1.10  christos  * NOTE: destructive function, it inserts '\0' after each provider name.
   3718   1.4  christos  */
   3719  1.10  christos static int prov_available(char *providers)
   3720  1.10  christos {
   3721  1.10  christos     char *p;
   3722  1.10  christos     int more = 1;
   3723  1.10  christos 
   3724  1.10  christos     while (more) {
   3725  1.11  christos         for (; isspace((unsigned char)(*providers)); providers++)
   3726  1.10  christos             continue;
   3727  1.10  christos         if (*providers == '\0')
   3728  1.10  christos             break;               /* End of the road */
   3729  1.11  christos         for (p = providers; *p != '\0' && !isspace((unsigned char)(*p)); p++)
   3730  1.10  christos             continue;
   3731  1.10  christos         if (*p == '\0')
   3732  1.10  christos             more = 0;
   3733  1.10  christos         else
   3734  1.10  christos             *p = '\0';
   3735  1.10  christos         if (OSSL_PROVIDER_available(libctx, providers))
   3736  1.10  christos             return 1;            /* Found one */
   3737  1.10  christos     }
   3738  1.10  christos     return 0;
   3739  1.10  christos }
   3740  1.10  christos 
   3741  1.10  christos /* Read and parse one test.  Return 0 if failure, 1 if okay. */
   3742   1.4  christos static int parse(EVP_TEST *t)
   3743   1.4  christos {
   3744   1.4  christos     KEY_LIST *key, **klist;
   3745   1.4  christos     EVP_PKEY *pkey;
   3746   1.4  christos     PAIR *pp;
   3747  1.10  christos     int i, j, skipped = 0;
   3748   1.4  christos 
   3749   1.4  christos top:
   3750   1.4  christos     do {
   3751   1.4  christos         if (BIO_eof(t->s.fp))
   3752   1.4  christos             return EOF;
   3753   1.4  christos         clear_test(t);
   3754   1.4  christos         if (!test_readstanza(&t->s))
   3755   1.4  christos             return 0;
   3756   1.4  christos     } while (t->s.numpairs == 0);
   3757   1.4  christos     pp = &t->s.pairs[0];
   3758   1.4  christos 
   3759   1.4  christos     /* Are we adding a key? */
   3760   1.4  christos     klist = NULL;
   3761   1.4  christos     pkey = NULL;
   3762  1.10  christos start:
   3763   1.4  christos     if (strcmp(pp->key, "PrivateKey") == 0) {
   3764  1.10  christos         pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
   3765   1.4  christos         if (pkey == NULL && !key_unsupported()) {
   3766   1.4  christos             EVP_PKEY_free(pkey);
   3767   1.4  christos             TEST_info("Can't read private key %s", pp->value);
   3768   1.4  christos             TEST_openssl_errors();
   3769   1.4  christos             return 0;
   3770   1.4  christos         }
   3771   1.4  christos         klist = &private_keys;
   3772   1.4  christos     } else if (strcmp(pp->key, "PublicKey") == 0) {
   3773  1.10  christos         pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
   3774   1.4  christos         if (pkey == NULL && !key_unsupported()) {
   3775   1.4  christos             EVP_PKEY_free(pkey);
   3776   1.4  christos             TEST_info("Can't read public key %s", pp->value);
   3777   1.4  christos             TEST_openssl_errors();
   3778   1.4  christos             return 0;
   3779   1.4  christos         }
   3780   1.4  christos         klist = &public_keys;
   3781   1.4  christos     } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
   3782   1.4  christos                || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
   3783   1.4  christos         char *strnid = NULL, *keydata = NULL;
   3784   1.4  christos         unsigned char *keybin;
   3785   1.4  christos         size_t keylen;
   3786   1.4  christos         int nid;
   3787   1.4  christos 
   3788   1.4  christos         if (strcmp(pp->key, "PrivateKeyRaw") == 0)
   3789   1.4  christos             klist = &private_keys;
   3790   1.4  christos         else
   3791   1.4  christos             klist = &public_keys;
   3792   1.4  christos 
   3793   1.4  christos         strnid = strchr(pp->value, ':');
   3794   1.4  christos         if (strnid != NULL) {
   3795   1.4  christos             *strnid++ = '\0';
   3796   1.4  christos             keydata = strchr(strnid, ':');
   3797   1.4  christos             if (keydata != NULL)
   3798   1.4  christos                 *keydata++ = '\0';
   3799   1.4  christos         }
   3800   1.4  christos         if (keydata == NULL) {
   3801   1.4  christos             TEST_info("Failed to parse %s value", pp->key);
   3802   1.4  christos             return 0;
   3803   1.4  christos         }
   3804   1.4  christos 
   3805   1.4  christos         nid = OBJ_txt2nid(strnid);
   3806   1.4  christos         if (nid == NID_undef) {
   3807  1.10  christos             TEST_info("Unrecognised algorithm NID");
   3808   1.4  christos             return 0;
   3809   1.4  christos         }
   3810   1.4  christos         if (!parse_bin(keydata, &keybin, &keylen)) {
   3811   1.4  christos             TEST_info("Failed to create binary key");
   3812   1.4  christos             return 0;
   3813   1.4  christos         }
   3814   1.4  christos         if (klist == &private_keys)
   3815  1.10  christos             pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
   3816  1.10  christos                                                    keylen);
   3817   1.4  christos         else
   3818  1.10  christos             pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
   3819  1.10  christos                                                   keylen);
   3820   1.4  christos         if (pkey == NULL && !key_unsupported()) {
   3821   1.4  christos             TEST_info("Can't read %s data", pp->key);
   3822   1.4  christos             OPENSSL_free(keybin);
   3823   1.4  christos             TEST_openssl_errors();
   3824   1.4  christos             return 0;
   3825   1.4  christos         }
   3826   1.4  christos         OPENSSL_free(keybin);
   3827  1.10  christos     } else if (strcmp(pp->key, "Availablein") == 0) {
   3828  1.10  christos         if (!prov_available(pp->value)) {
   3829  1.10  christos             TEST_info("skipping, '%s' provider not available: %s:%d",
   3830  1.10  christos                       pp->value, t->s.test_file, t->s.start);
   3831  1.10  christos                 t->skip = 1;
   3832  1.10  christos                 return 0;
   3833  1.10  christos         }
   3834  1.10  christos         skipped++;
   3835  1.10  christos         pp++;
   3836  1.10  christos         goto start;
   3837  1.10  christos     } else if (strcmp(pp->key, "FIPSversion") == 0) {
   3838  1.10  christos         if (prov_available((char *)(intptr_t)"fips")) {
   3839  1.10  christos             j = fips_provider_version_match(libctx, pp->value);
   3840  1.10  christos             if (j < 0) {
   3841  1.10  christos                 TEST_info("Line %d: error matching FIPS versions\n", t->s.curr);
   3842  1.10  christos                 return 0;
   3843  1.10  christos             } else if (j == 0) {
   3844  1.10  christos                 TEST_info("skipping, FIPS provider incompatible version: %s:%d",
   3845  1.10  christos                           t->s.test_file, t->s.start);
   3846  1.10  christos                     t->skip = 1;
   3847  1.10  christos                     return 0;
   3848  1.10  christos             }
   3849  1.10  christos         }
   3850  1.10  christos         skipped++;
   3851  1.10  christos         pp++;
   3852  1.10  christos         goto start;
   3853   1.4  christos     }
   3854   1.4  christos 
   3855   1.4  christos     /* If we have a key add to list */
   3856   1.4  christos     if (klist != NULL) {
   3857   1.4  christos         if (find_key(NULL, pp->value, *klist)) {
   3858   1.4  christos             TEST_info("Duplicate key %s", pp->value);
   3859   1.4  christos             return 0;
   3860   1.4  christos         }
   3861   1.4  christos         if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
   3862   1.4  christos             return 0;
   3863   1.4  christos         key->name = take_value(pp);
   3864   1.4  christos         key->key = pkey;
   3865   1.4  christos         key->next = *klist;
   3866   1.4  christos         *klist = key;
   3867   1.4  christos 
   3868   1.4  christos         /* Go back and start a new stanza. */
   3869  1.10  christos         if ((t->s.numpairs - skipped) != 1)
   3870   1.4  christos             TEST_info("Line %d: missing blank line\n", t->s.curr);
   3871   1.4  christos         goto top;
   3872   1.4  christos     }
   3873   1.4  christos 
   3874   1.4  christos     /* Find the test, based on first keyword. */
   3875   1.4  christos     if (!TEST_ptr(t->meth = find_test(pp->key)))
   3876   1.4  christos         return 0;
   3877   1.4  christos     if (!t->meth->init(t, pp->value)) {
   3878   1.4  christos         TEST_error("unknown %s: %s\n", pp->key, pp->value);
   3879   1.4  christos         return 0;
   3880   1.4  christos     }
   3881   1.4  christos     if (t->skip == 1) {
   3882   1.4  christos         /* TEST_info("skipping %s %s", pp->key, pp->value); */
   3883   1.4  christos         return 0;
   3884   1.4  christos     }
   3885   1.4  christos 
   3886  1.10  christos     for (pp++, i = 1; i < (t->s.numpairs - skipped); pp++, i++) {
   3887  1.10  christos         if (strcmp(pp->key, "Securitycheck") == 0) {
   3888  1.10  christos #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
   3889  1.10  christos #else
   3890  1.10  christos             if (!securitycheck_enabled())
   3891  1.10  christos #endif
   3892  1.10  christos             {
   3893  1.10  christos                 TEST_info("skipping, Securitycheck is disabled: %s:%d",
   3894  1.10  christos                           t->s.test_file, t->s.start);
   3895  1.10  christos                 t->skip = 1;
   3896  1.10  christos                 return 0;
   3897  1.10  christos             }
   3898  1.10  christos         } else if (strcmp(pp->key, "Availablein") == 0) {
   3899  1.10  christos             TEST_info("Line %d: 'Availablein' should be the first option",
   3900  1.10  christos                       t->s.curr);
   3901  1.10  christos             return 0;
   3902  1.10  christos         } else if (strcmp(pp->key, "Result") == 0) {
   3903   1.4  christos             if (t->expected_err != NULL) {
   3904   1.4  christos                 TEST_info("Line %d: multiple result lines", t->s.curr);
   3905   1.4  christos                 return 0;
   3906   1.4  christos             }
   3907   1.4  christos             t->expected_err = take_value(pp);
   3908   1.4  christos         } else if (strcmp(pp->key, "Function") == 0) {
   3909  1.10  christos             /* Ignore old line. */
   3910   1.4  christos         } else if (strcmp(pp->key, "Reason") == 0) {
   3911   1.4  christos             if (t->reason != NULL) {
   3912   1.4  christos                 TEST_info("Line %d: multiple reason lines", t->s.curr);
   3913   1.4  christos                 return 0;
   3914   1.4  christos             }
   3915   1.4  christos             t->reason = take_value(pp);
   3916   1.4  christos         } else {
   3917   1.4  christos             /* Must be test specific line: try to parse it */
   3918   1.4  christos             int rv = t->meth->parse(t, pp->key, pp->value);
   3919   1.4  christos 
   3920   1.4  christos             if (rv == 0) {
   3921   1.4  christos                 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
   3922   1.4  christos                 return 0;
   3923   1.4  christos             }
   3924   1.4  christos             if (rv < 0) {
   3925   1.5  christos                 TEST_info("Line %d: error processing keyword %s = %s\n",
   3926   1.5  christos                           t->s.curr, pp->key, pp->value);
   3927   1.4  christos                 return 0;
   3928   1.4  christos             }
   3929   1.4  christos         }
   3930   1.4  christos     }
   3931   1.4  christos 
   3932   1.4  christos     return 1;
   3933   1.4  christos }
   3934   1.4  christos 
   3935   1.4  christos static int run_file_tests(int i)
   3936   1.4  christos {
   3937   1.4  christos     EVP_TEST *t;
   3938   1.4  christos     const char *testfile = test_get_argument(i);
   3939   1.4  christos     int c;
   3940   1.4  christos 
   3941   1.4  christos     if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
   3942   1.4  christos         return 0;
   3943   1.4  christos     if (!test_start_file(&t->s, testfile)) {
   3944   1.4  christos         OPENSSL_free(t);
   3945   1.4  christos         return 0;
   3946   1.4  christos     }
   3947   1.4  christos 
   3948   1.4  christos     while (!BIO_eof(t->s.fp)) {
   3949   1.4  christos         c = parse(t);
   3950  1.10  christos         if (t->skip) {
   3951  1.10  christos             t->s.numskip++;
   3952   1.4  christos             continue;
   3953  1.10  christos         }
   3954   1.4  christos         if (c == 0 || !run_test(t)) {
   3955   1.4  christos             t->s.errors++;
   3956   1.4  christos             break;
   3957   1.4  christos         }
   3958   1.4  christos     }
   3959   1.4  christos     test_end_file(&t->s);
   3960   1.4  christos     clear_test(t);
   3961   1.4  christos 
   3962   1.4  christos     free_key_list(public_keys);
   3963   1.4  christos     free_key_list(private_keys);
   3964   1.4  christos     BIO_free(t->s.key);
   3965   1.4  christos     c = t->s.errors;
   3966   1.4  christos     OPENSSL_free(t);
   3967   1.4  christos     return c == 0;
   3968   1.4  christos }
   3969   1.4  christos 
   3970  1.10  christos const OPTIONS *test_get_options(void)
   3971  1.10  christos {
   3972  1.10  christos     static const OPTIONS test_options[] = {
   3973  1.10  christos         OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
   3974  1.10  christos         { "config", OPT_CONFIG_FILE, '<',
   3975  1.10  christos           "The configuration file to use for the libctx" },
   3976  1.10  christos         { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
   3977  1.10  christos         { NULL }
   3978  1.10  christos     };
   3979  1.10  christos     return test_options;
   3980  1.10  christos }
   3981  1.10  christos 
   3982   1.4  christos int setup_tests(void)
   3983   1.4  christos {
   3984  1.10  christos     size_t n;
   3985  1.10  christos     char *config_file = NULL;
   3986  1.10  christos 
   3987  1.10  christos     OPTION_CHOICE o;
   3988  1.10  christos 
   3989  1.10  christos     while ((o = opt_next()) != OPT_EOF) {
   3990  1.10  christos         switch (o) {
   3991  1.10  christos         case OPT_CONFIG_FILE:
   3992  1.10  christos             config_file = opt_arg();
   3993  1.10  christos             break;
   3994  1.10  christos         case OPT_TEST_CASES:
   3995  1.10  christos            break;
   3996  1.10  christos         default:
   3997  1.10  christos         case OPT_ERR:
   3998  1.10  christos             return 0;
   3999  1.10  christos         }
   4000  1.10  christos     }
   4001  1.10  christos 
   4002  1.10  christos     /*
   4003  1.10  christos      * Load the provider via configuration into the created library context.
   4004  1.10  christos      * Load the 'null' provider into the default library context to ensure that
   4005  1.10  christos      * the tests do not fallback to using the default provider.
   4006  1.10  christos      */
   4007  1.10  christos     if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
   4008  1.10  christos         return 0;
   4009   1.4  christos 
   4010  1.10  christos     n = test_get_argument_count();
   4011  1.10  christos     if (n == 0)
   4012   1.4  christos         return 0;
   4013   1.4  christos 
   4014   1.4  christos     ADD_ALL_TESTS(run_file_tests, n);
   4015   1.4  christos     return 1;
   4016   1.4  christos }
   4017  1.10  christos 
   4018  1.10  christos void cleanup_tests(void)
   4019  1.10  christos {
   4020  1.10  christos     OSSL_PROVIDER_unload(prov_null);
   4021  1.10  christos     OSSL_LIB_CTX_free(libctx);
   4022  1.10  christos }
   4023  1.10  christos 
   4024  1.10  christos #define STR_STARTS_WITH(str, pre) OPENSSL_strncasecmp(pre, str, strlen(pre)) == 0
   4025  1.10  christos #define STR_ENDS_WITH(str, pre)                                                \
   4026  1.10  christos strlen(str) < strlen(pre) ? 0 : (OPENSSL_strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
   4027  1.10  christos 
   4028  1.10  christos static int is_digest_disabled(const char *name)
   4029  1.10  christos {
   4030  1.10  christos #ifdef OPENSSL_NO_BLAKE2
   4031  1.10  christos     if (STR_STARTS_WITH(name, "BLAKE"))
   4032  1.10  christos         return 1;
   4033  1.10  christos #endif
   4034  1.10  christos #ifdef OPENSSL_NO_MD2
   4035  1.10  christos     if (OPENSSL_strcasecmp(name, "MD2") == 0)
   4036  1.10  christos         return 1;
   4037  1.10  christos #endif
   4038  1.10  christos #ifdef OPENSSL_NO_MDC2
   4039  1.10  christos     if (OPENSSL_strcasecmp(name, "MDC2") == 0)
   4040  1.10  christos         return 1;
   4041  1.10  christos #endif
   4042  1.10  christos #ifdef OPENSSL_NO_MD4
   4043  1.10  christos     if (OPENSSL_strcasecmp(name, "MD4") == 0)
   4044  1.10  christos         return 1;
   4045  1.10  christos #endif
   4046  1.10  christos #ifdef OPENSSL_NO_MD5
   4047  1.10  christos     if (OPENSSL_strcasecmp(name, "MD5") == 0)
   4048  1.10  christos         return 1;
   4049  1.10  christos #endif
   4050  1.10  christos #ifdef OPENSSL_NO_RMD160
   4051  1.10  christos     if (OPENSSL_strcasecmp(name, "RIPEMD160") == 0)
   4052  1.10  christos         return 1;
   4053  1.10  christos #endif
   4054  1.10  christos #ifdef OPENSSL_NO_SM3
   4055  1.10  christos     if (OPENSSL_strcasecmp(name, "SM3") == 0)
   4056  1.10  christos         return 1;
   4057  1.10  christos #endif
   4058  1.10  christos #ifdef OPENSSL_NO_WHIRLPOOL
   4059  1.10  christos     if (OPENSSL_strcasecmp(name, "WHIRLPOOL") == 0)
   4060  1.10  christos         return 1;
   4061  1.10  christos #endif
   4062  1.10  christos     return 0;
   4063  1.10  christos }
   4064  1.10  christos 
   4065  1.10  christos static int is_pkey_disabled(const char *name)
   4066  1.10  christos {
   4067  1.10  christos #ifdef OPENSSL_NO_EC
   4068  1.10  christos     if (STR_STARTS_WITH(name, "EC"))
   4069  1.10  christos         return 1;
   4070  1.10  christos #endif
   4071  1.10  christos #ifdef OPENSSL_NO_DH
   4072  1.10  christos     if (STR_STARTS_WITH(name, "DH"))
   4073  1.10  christos         return 1;
   4074  1.10  christos #endif
   4075  1.10  christos #ifdef OPENSSL_NO_DSA
   4076  1.10  christos     if (STR_STARTS_WITH(name, "DSA"))
   4077  1.10  christos         return 1;
   4078  1.10  christos #endif
   4079  1.10  christos     return 0;
   4080  1.10  christos }
   4081  1.10  christos 
   4082  1.10  christos static int is_mac_disabled(const char *name)
   4083  1.10  christos {
   4084  1.10  christos #ifdef OPENSSL_NO_BLAKE2
   4085  1.10  christos     if (STR_STARTS_WITH(name, "BLAKE2BMAC")
   4086  1.10  christos         || STR_STARTS_WITH(name, "BLAKE2SMAC"))
   4087  1.10  christos         return 1;
   4088  1.10  christos #endif
   4089  1.10  christos #ifdef OPENSSL_NO_CMAC
   4090  1.10  christos     if (STR_STARTS_WITH(name, "CMAC"))
   4091  1.10  christos         return 1;
   4092  1.10  christos #endif
   4093  1.10  christos #ifdef OPENSSL_NO_POLY1305
   4094  1.10  christos     if (STR_STARTS_WITH(name, "Poly1305"))
   4095  1.10  christos         return 1;
   4096  1.10  christos #endif
   4097  1.10  christos #ifdef OPENSSL_NO_SIPHASH
   4098  1.10  christos     if (STR_STARTS_WITH(name, "SipHash"))
   4099  1.10  christos         return 1;
   4100  1.10  christos #endif
   4101  1.10  christos     return 0;
   4102  1.10  christos }
   4103  1.10  christos static int is_kdf_disabled(const char *name)
   4104  1.10  christos {
   4105  1.10  christos #ifdef OPENSSL_NO_SCRYPT
   4106  1.10  christos     if (STR_ENDS_WITH(name, "SCRYPT"))
   4107  1.10  christos         return 1;
   4108  1.10  christos #endif
   4109  1.10  christos     return 0;
   4110  1.10  christos }
   4111  1.10  christos 
   4112  1.10  christos static int is_cipher_disabled(const char *name)
   4113  1.10  christos {
   4114  1.10  christos #ifdef OPENSSL_NO_ARIA
   4115  1.10  christos     if (STR_STARTS_WITH(name, "ARIA"))
   4116  1.10  christos         return 1;
   4117  1.10  christos #endif
   4118  1.10  christos #ifdef OPENSSL_NO_BF
   4119  1.10  christos     if (STR_STARTS_WITH(name, "BF"))
   4120  1.10  christos         return 1;
   4121  1.10  christos #endif
   4122  1.10  christos #ifdef OPENSSL_NO_CAMELLIA
   4123  1.10  christos     if (STR_STARTS_WITH(name, "CAMELLIA"))
   4124  1.10  christos         return 1;
   4125  1.10  christos #endif
   4126  1.10  christos #ifdef OPENSSL_NO_CAST
   4127  1.10  christos     if (STR_STARTS_WITH(name, "CAST"))
   4128  1.10  christos         return 1;
   4129  1.10  christos #endif
   4130  1.10  christos #ifdef OPENSSL_NO_CHACHA
   4131  1.10  christos     if (STR_STARTS_WITH(name, "CHACHA"))
   4132  1.10  christos         return 1;
   4133  1.10  christos #endif
   4134  1.10  christos #ifdef OPENSSL_NO_POLY1305
   4135  1.10  christos     if (STR_ENDS_WITH(name, "Poly1305"))
   4136  1.10  christos         return 1;
   4137  1.10  christos #endif
   4138  1.10  christos #ifdef OPENSSL_NO_DES
   4139  1.10  christos     if (STR_STARTS_WITH(name, "DES"))
   4140  1.10  christos         return 1;
   4141  1.10  christos     if (STR_ENDS_WITH(name, "3DESwrap"))
   4142  1.10  christos         return 1;
   4143  1.10  christos #endif
   4144  1.10  christos #ifdef OPENSSL_NO_OCB
   4145  1.10  christos     if (STR_ENDS_WITH(name, "OCB"))
   4146  1.10  christos         return 1;
   4147  1.10  christos #endif
   4148  1.10  christos #ifdef OPENSSL_NO_IDEA
   4149  1.10  christos     if (STR_STARTS_WITH(name, "IDEA"))
   4150  1.10  christos         return 1;
   4151  1.10  christos #endif
   4152  1.10  christos #ifdef OPENSSL_NO_RC2
   4153  1.10  christos     if (STR_STARTS_WITH(name, "RC2"))
   4154  1.10  christos         return 1;
   4155  1.10  christos #endif
   4156  1.10  christos #ifdef OPENSSL_NO_RC4
   4157  1.10  christos     if (STR_STARTS_WITH(name, "RC4"))
   4158  1.10  christos         return 1;
   4159  1.10  christos #endif
   4160  1.10  christos #ifdef OPENSSL_NO_RC5
   4161  1.10  christos     if (STR_STARTS_WITH(name, "RC5"))
   4162  1.10  christos         return 1;
   4163  1.10  christos #endif
   4164  1.10  christos #ifdef OPENSSL_NO_SEED
   4165  1.10  christos     if (STR_STARTS_WITH(name, "SEED"))
   4166  1.10  christos         return 1;
   4167  1.10  christos #endif
   4168  1.10  christos #ifdef OPENSSL_NO_SIV
   4169  1.10  christos     if (STR_ENDS_WITH(name, "SIV"))
   4170  1.10  christos         return 1;
   4171  1.10  christos #endif
   4172  1.10  christos #ifdef OPENSSL_NO_SM4
   4173  1.10  christos     if (STR_STARTS_WITH(name, "SM4"))
   4174  1.10  christos         return 1;
   4175  1.10  christos #endif
   4176  1.10  christos     return 0;
   4177  1.10  christos }
   4178