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