1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* 11e1051a39Sopenharmony_ci 12e1051a39Sopenharmony_ci * These tests are setup to load null into the default library context. 13e1051a39Sopenharmony_ci * Any tests are expected to use the created 'libctx' to find algorithms. 14e1051a39Sopenharmony_ci * The framework runs the tests twice using the 'default' provider or 15e1051a39Sopenharmony_ci * 'fips' provider as inputs. 16e1051a39Sopenharmony_ci */ 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ci/* 19e1051a39Sopenharmony_ci * DSA/DH low level APIs are deprecated for public use, but still ok for 20e1051a39Sopenharmony_ci * internal use. 21e1051a39Sopenharmony_ci */ 22e1051a39Sopenharmony_ci#include "internal/deprecated.h" 23e1051a39Sopenharmony_ci#include <assert.h> 24e1051a39Sopenharmony_ci#include <openssl/evp.h> 25e1051a39Sopenharmony_ci#include <openssl/provider.h> 26e1051a39Sopenharmony_ci#include <openssl/dsa.h> 27e1051a39Sopenharmony_ci#include <openssl/dh.h> 28e1051a39Sopenharmony_ci#include <openssl/safestack.h> 29e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h> 30e1051a39Sopenharmony_ci#include <openssl/core_names.h> 31e1051a39Sopenharmony_ci#include <openssl/x509.h> 32e1051a39Sopenharmony_ci#include <openssl/encoder.h> 33e1051a39Sopenharmony_ci#include "testutil.h" 34e1051a39Sopenharmony_ci#include "internal/nelem.h" 35e1051a39Sopenharmony_ci#include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */ 36e1051a39Sopenharmony_ci 37e1051a39Sopenharmony_cistatic OSSL_LIB_CTX *libctx = NULL; 38e1051a39Sopenharmony_cistatic OSSL_PROVIDER *nullprov = NULL; 39e1051a39Sopenharmony_cistatic OSSL_PROVIDER *libprov = NULL; 40e1051a39Sopenharmony_cistatic STACK_OF(OPENSSL_STRING) *cipher_names = NULL; 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_citypedef enum OPTION_choice { 43e1051a39Sopenharmony_ci OPT_ERR = -1, 44e1051a39Sopenharmony_ci OPT_EOF = 0, 45e1051a39Sopenharmony_ci OPT_CONFIG_FILE, 46e1051a39Sopenharmony_ci OPT_PROVIDER_NAME, 47e1051a39Sopenharmony_ci OPT_TEST_ENUM 48e1051a39Sopenharmony_ci} OPTION_CHOICE; 49e1051a39Sopenharmony_ci 50e1051a39Sopenharmony_ciconst OPTIONS *test_get_options(void) 51e1051a39Sopenharmony_ci{ 52e1051a39Sopenharmony_ci static const OPTIONS test_options[] = { 53e1051a39Sopenharmony_ci OPT_TEST_OPTIONS_DEFAULT_USAGE, 54e1051a39Sopenharmony_ci { "config", OPT_CONFIG_FILE, '<', 55e1051a39Sopenharmony_ci "The configuration file to use for the libctx" }, 56e1051a39Sopenharmony_ci { "provider", OPT_PROVIDER_NAME, 's', 57e1051a39Sopenharmony_ci "The provider to load (The default value is 'default')" }, 58e1051a39Sopenharmony_ci { NULL } 59e1051a39Sopenharmony_ci }; 60e1051a39Sopenharmony_ci return test_options; 61e1051a39Sopenharmony_ci} 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 64e1051a39Sopenharmony_cistatic const char *getname(int id) 65e1051a39Sopenharmony_ci{ 66e1051a39Sopenharmony_ci const char *name[] = {"p", "q", "g" }; 67e1051a39Sopenharmony_ci 68e1051a39Sopenharmony_ci if (id >= 0 && id < 3) 69e1051a39Sopenharmony_ci return name[id]; 70e1051a39Sopenharmony_ci return "?"; 71e1051a39Sopenharmony_ci} 72e1051a39Sopenharmony_ci#endif 73e1051a39Sopenharmony_ci 74e1051a39Sopenharmony_ci/* 75e1051a39Sopenharmony_ci * We're using some DH specific values in this test, so we skip compilation if 76e1051a39Sopenharmony_ci * we're in a no-dh build. 77e1051a39Sopenharmony_ci */ 78e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH) 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_cistatic int test_dsa_param_keygen(int tstid) 81e1051a39Sopenharmony_ci{ 82e1051a39Sopenharmony_ci int ret = 0; 83e1051a39Sopenharmony_ci int expected; 84e1051a39Sopenharmony_ci EVP_PKEY_CTX *gen_ctx = NULL; 85e1051a39Sopenharmony_ci EVP_PKEY *pkey_parm = NULL; 86e1051a39Sopenharmony_ci EVP_PKEY *pkey = NULL, *dup_pk = NULL; 87e1051a39Sopenharmony_ci DSA *dsa = NULL; 88e1051a39Sopenharmony_ci int pind, qind, gind; 89e1051a39Sopenharmony_ci BIGNUM *p = NULL, *q = NULL, *g = NULL; 90e1051a39Sopenharmony_ci 91e1051a39Sopenharmony_ci /* 92e1051a39Sopenharmony_ci * Just grab some fixed dh p, q, g values for testing, 93e1051a39Sopenharmony_ci * these 'safe primes' should not be used normally for dsa *. 94e1051a39Sopenharmony_ci */ 95e1051a39Sopenharmony_ci static const BIGNUM *bn[] = { 96e1051a39Sopenharmony_ci &ossl_bignum_dh2048_256_p, &ossl_bignum_dh2048_256_q, 97e1051a39Sopenharmony_ci &ossl_bignum_dh2048_256_g 98e1051a39Sopenharmony_ci }; 99e1051a39Sopenharmony_ci 100e1051a39Sopenharmony_ci /* 101e1051a39Sopenharmony_ci * These tests are using bad values for p, q, g by reusing the values. 102e1051a39Sopenharmony_ci * A value of 0 uses p, 1 uses q and 2 uses g. 103e1051a39Sopenharmony_ci * There are 27 different combinations, with only the 1 valid combination. 104e1051a39Sopenharmony_ci */ 105e1051a39Sopenharmony_ci pind = tstid / 9; 106e1051a39Sopenharmony_ci qind = (tstid / 3) % 3; 107e1051a39Sopenharmony_ci gind = tstid % 3; 108e1051a39Sopenharmony_ci expected = (pind == 0 && qind == 1 && gind == 2); 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci TEST_note("Testing with (p, q, g) = (%s, %s, %s)\n", getname(pind), 111e1051a39Sopenharmony_ci getname(qind), getname(gind)); 112e1051a39Sopenharmony_ci 113e1051a39Sopenharmony_ci if (!TEST_ptr(pkey_parm = EVP_PKEY_new()) 114e1051a39Sopenharmony_ci || !TEST_ptr(dsa = DSA_new()) 115e1051a39Sopenharmony_ci || !TEST_ptr(p = BN_dup(bn[pind])) 116e1051a39Sopenharmony_ci || !TEST_ptr(q = BN_dup(bn[qind])) 117e1051a39Sopenharmony_ci || !TEST_ptr(g = BN_dup(bn[gind])) 118e1051a39Sopenharmony_ci || !TEST_true(DSA_set0_pqg(dsa, p, q, g))) 119e1051a39Sopenharmony_ci goto err; 120e1051a39Sopenharmony_ci p = q = g = NULL; 121e1051a39Sopenharmony_ci 122e1051a39Sopenharmony_ci if (!TEST_true(EVP_PKEY_assign_DSA(pkey_parm, dsa))) 123e1051a39Sopenharmony_ci goto err; 124e1051a39Sopenharmony_ci dsa = NULL; 125e1051a39Sopenharmony_ci 126e1051a39Sopenharmony_ci if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL)) 127e1051a39Sopenharmony_ci || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0) 128e1051a39Sopenharmony_ci || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected)) 129e1051a39Sopenharmony_ci goto err; 130e1051a39Sopenharmony_ci 131e1051a39Sopenharmony_ci if (expected) { 132e1051a39Sopenharmony_ci if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) 133e1051a39Sopenharmony_ci || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) 134e1051a39Sopenharmony_ci goto err; 135e1051a39Sopenharmony_ci } 136e1051a39Sopenharmony_ci 137e1051a39Sopenharmony_ci ret = 1; 138e1051a39Sopenharmony_cierr: 139e1051a39Sopenharmony_ci EVP_PKEY_free(pkey); 140e1051a39Sopenharmony_ci EVP_PKEY_free(dup_pk); 141e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(gen_ctx); 142e1051a39Sopenharmony_ci EVP_PKEY_free(pkey_parm); 143e1051a39Sopenharmony_ci DSA_free(dsa); 144e1051a39Sopenharmony_ci BN_free(g); 145e1051a39Sopenharmony_ci BN_free(q); 146e1051a39Sopenharmony_ci BN_free(p); 147e1051a39Sopenharmony_ci return ret; 148e1051a39Sopenharmony_ci} 149e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DSA */ 150e1051a39Sopenharmony_ci 151e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 152e1051a39Sopenharmony_cistatic int do_dh_param_keygen(int tstid, const BIGNUM **bn) 153e1051a39Sopenharmony_ci{ 154e1051a39Sopenharmony_ci int ret = 0; 155e1051a39Sopenharmony_ci int expected; 156e1051a39Sopenharmony_ci EVP_PKEY_CTX *gen_ctx = NULL; 157e1051a39Sopenharmony_ci EVP_PKEY *pkey_parm = NULL; 158e1051a39Sopenharmony_ci EVP_PKEY *pkey = NULL, *dup_pk = NULL; 159e1051a39Sopenharmony_ci DH *dh = NULL; 160e1051a39Sopenharmony_ci int pind, qind, gind; 161e1051a39Sopenharmony_ci BIGNUM *p = NULL, *q = NULL, *g = NULL; 162e1051a39Sopenharmony_ci 163e1051a39Sopenharmony_ci /* 164e1051a39Sopenharmony_ci * These tests are using bad values for p, q, g by reusing the values. 165e1051a39Sopenharmony_ci * A value of 0 uses p, 1 uses q and 2 uses g. 166e1051a39Sopenharmony_ci * There are 27 different combinations, with only the 1 valid combination. 167e1051a39Sopenharmony_ci */ 168e1051a39Sopenharmony_ci pind = tstid / 9; 169e1051a39Sopenharmony_ci qind = (tstid / 3) % 3; 170e1051a39Sopenharmony_ci gind = tstid % 3; 171e1051a39Sopenharmony_ci expected = (pind == 0 && qind == 1 && gind == 2); 172e1051a39Sopenharmony_ci 173e1051a39Sopenharmony_ci TEST_note("Testing with (p, q, g) = (%s, %s, %s)", getname(pind), 174e1051a39Sopenharmony_ci getname(qind), getname(gind)); 175e1051a39Sopenharmony_ci 176e1051a39Sopenharmony_ci if (!TEST_ptr(pkey_parm = EVP_PKEY_new()) 177e1051a39Sopenharmony_ci || !TEST_ptr(dh = DH_new()) 178e1051a39Sopenharmony_ci || !TEST_ptr(p = BN_dup(bn[pind])) 179e1051a39Sopenharmony_ci || !TEST_ptr(q = BN_dup(bn[qind])) 180e1051a39Sopenharmony_ci || !TEST_ptr(g = BN_dup(bn[gind])) 181e1051a39Sopenharmony_ci || !TEST_true(DH_set0_pqg(dh, p, q, g))) 182e1051a39Sopenharmony_ci goto err; 183e1051a39Sopenharmony_ci p = q = g = NULL; 184e1051a39Sopenharmony_ci 185e1051a39Sopenharmony_ci if (!TEST_true(EVP_PKEY_assign_DH(pkey_parm, dh))) 186e1051a39Sopenharmony_ci goto err; 187e1051a39Sopenharmony_ci dh = NULL; 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ci if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL)) 190e1051a39Sopenharmony_ci || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0) 191e1051a39Sopenharmony_ci || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected)) 192e1051a39Sopenharmony_ci goto err; 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci if (expected) { 195e1051a39Sopenharmony_ci if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) 196e1051a39Sopenharmony_ci || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) 197e1051a39Sopenharmony_ci goto err; 198e1051a39Sopenharmony_ci } 199e1051a39Sopenharmony_ci 200e1051a39Sopenharmony_ci ret = 1; 201e1051a39Sopenharmony_cierr: 202e1051a39Sopenharmony_ci EVP_PKEY_free(pkey); 203e1051a39Sopenharmony_ci EVP_PKEY_free(dup_pk); 204e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(gen_ctx); 205e1051a39Sopenharmony_ci EVP_PKEY_free(pkey_parm); 206e1051a39Sopenharmony_ci DH_free(dh); 207e1051a39Sopenharmony_ci BN_free(g); 208e1051a39Sopenharmony_ci BN_free(q); 209e1051a39Sopenharmony_ci BN_free(p); 210e1051a39Sopenharmony_ci return ret; 211e1051a39Sopenharmony_ci} 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ci/* 214e1051a39Sopenharmony_ci * Note that we get the fips186-4 path being run for most of these cases since 215e1051a39Sopenharmony_ci * the internal code will detect that the p, q, g does not match a safe prime 216e1051a39Sopenharmony_ci * group (Except for when tstid = 5, which sets the correct p, q, g) 217e1051a39Sopenharmony_ci */ 218e1051a39Sopenharmony_cistatic int test_dh_safeprime_param_keygen(int tstid) 219e1051a39Sopenharmony_ci{ 220e1051a39Sopenharmony_ci static const BIGNUM *bn[] = { 221e1051a39Sopenharmony_ci &ossl_bignum_ffdhe2048_p, &ossl_bignum_ffdhe2048_q, 222e1051a39Sopenharmony_ci &ossl_bignum_const_2 223e1051a39Sopenharmony_ci }; 224e1051a39Sopenharmony_ci return do_dh_param_keygen(tstid, bn); 225e1051a39Sopenharmony_ci} 226e1051a39Sopenharmony_ci 227e1051a39Sopenharmony_cistatic int dhx_cert_load(void) 228e1051a39Sopenharmony_ci{ 229e1051a39Sopenharmony_ci int ret = 0; 230e1051a39Sopenharmony_ci X509 *cert = NULL; 231e1051a39Sopenharmony_ci BIO *bio = NULL; 232e1051a39Sopenharmony_ci 233e1051a39Sopenharmony_ci static const unsigned char dhx_cert[] = { 234e1051a39Sopenharmony_ci 0x30,0x82,0x03,0xff,0x30,0x82,0x02,0xe7,0xa0,0x03,0x02,0x01,0x02,0x02,0x09,0x00, 235e1051a39Sopenharmony_ci 0xdb,0xf5,0x4d,0x22,0xa0,0x7a,0x67,0xa6,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86, 236e1051a39Sopenharmony_ci 0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x30,0x44,0x31,0x0b,0x30,0x09,0x06,0x03,0x55, 237e1051a39Sopenharmony_ci 0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x0a,0x0c, 238e1051a39Sopenharmony_ci 0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47,0x72,0x6f,0x75,0x70,0x31,0x1d, 239e1051a39Sopenharmony_ci 0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54,0x65,0x73,0x74,0x20,0x53,0x2f, 240e1051a39Sopenharmony_ci 0x4d,0x49,0x4d,0x45,0x20,0x52,0x53,0x41,0x20,0x52,0x6f,0x6f,0x74,0x30,0x1e,0x17, 241e1051a39Sopenharmony_ci 0x0d,0x31,0x33,0x30,0x38,0x30,0x32,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x17,0x0d, 242e1051a39Sopenharmony_ci 0x32,0x33,0x30,0x36,0x31,0x31,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x30,0x44,0x31, 243e1051a39Sopenharmony_ci 0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14, 244e1051a39Sopenharmony_ci 0x06,0x03,0x55,0x04,0x0a,0x0c,0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47, 245e1051a39Sopenharmony_ci 0x72,0x6f,0x75,0x70,0x31,0x1d,0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54, 246e1051a39Sopenharmony_ci 0x65,0x73,0x74,0x20,0x53,0x2f,0x4d,0x49,0x4d,0x45,0x20,0x45,0x45,0x20,0x44,0x48, 247e1051a39Sopenharmony_ci 0x20,0x23,0x31,0x30,0x82,0x01,0xb6,0x30,0x82,0x01,0x2b,0x06,0x07,0x2a,0x86,0x48, 248e1051a39Sopenharmony_ci 0xce,0x3e,0x02,0x01,0x30,0x82,0x01,0x1e,0x02,0x81,0x81,0x00,0xd4,0x0c,0x4a,0x0c, 249e1051a39Sopenharmony_ci 0x04,0x72,0x71,0x19,0xdf,0x59,0x19,0xc5,0xaf,0x44,0x7f,0xca,0x8e,0x2b,0xf0,0x09, 250e1051a39Sopenharmony_ci 0xf5,0xd3,0x25,0xb1,0x73,0x16,0x55,0x89,0xdf,0xfd,0x07,0xaf,0x19,0xd3,0x7f,0xd0, 251e1051a39Sopenharmony_ci 0x07,0xa2,0xfe,0x3f,0x5a,0xf1,0x01,0xc6,0xf8,0x2b,0xef,0x4e,0x6d,0x03,0x38,0x42, 252e1051a39Sopenharmony_ci 0xa1,0x37,0xd4,0x14,0xb4,0x00,0x4a,0xb1,0x86,0x5a,0x83,0xce,0xb9,0x08,0x0e,0xc1, 253e1051a39Sopenharmony_ci 0x99,0x27,0x47,0x8d,0x0b,0x85,0xa8,0x82,0xed,0xcc,0x0d,0xb9,0xb0,0x32,0x7e,0xdf, 254e1051a39Sopenharmony_ci 0xe8,0xe4,0xf6,0xf6,0xec,0xb3,0xee,0x7a,0x11,0x34,0x65,0x97,0xfc,0x1a,0xb0,0x95, 255e1051a39Sopenharmony_ci 0x4b,0x19,0xb9,0xa6,0x1c,0xd9,0x01,0x32,0xf7,0x35,0x7c,0x2d,0x5d,0xfe,0xc1,0x85, 256e1051a39Sopenharmony_ci 0x70,0x49,0xf8,0xcc,0x99,0xd0,0xbe,0xf1,0x5a,0x78,0xc8,0x03,0x02,0x81,0x80,0x69, 257e1051a39Sopenharmony_ci 0x00,0xfd,0x66,0xf2,0xfc,0x15,0x8b,0x09,0xb8,0xdc,0x4d,0xea,0xaa,0x79,0x55,0xf9, 258e1051a39Sopenharmony_ci 0xdf,0x46,0xa6,0x2f,0xca,0x2d,0x8f,0x59,0x2a,0xad,0x44,0xa3,0xc6,0x18,0x2f,0x95, 259e1051a39Sopenharmony_ci 0xb6,0x16,0x20,0xe3,0xd3,0xd1,0x8f,0x03,0xce,0x71,0x7c,0xef,0x3a,0xc7,0x44,0x39, 260e1051a39Sopenharmony_ci 0x0e,0xe2,0x1f,0xd8,0xd3,0x89,0x2b,0xe7,0x51,0xdc,0x12,0x48,0x4c,0x18,0x4d,0x99, 261e1051a39Sopenharmony_ci 0x12,0x06,0xe4,0x17,0x02,0x03,0x8c,0x24,0x05,0x8e,0xa6,0x85,0xf2,0x69,0x1b,0xe1, 262e1051a39Sopenharmony_ci 0x6a,0xdc,0xe2,0x04,0x3a,0x01,0x9d,0x64,0xbe,0xfe,0x45,0xf9,0x44,0x18,0x71,0xbd, 263e1051a39Sopenharmony_ci 0x2d,0x3e,0x7a,0x6f,0x72,0x7d,0x1a,0x80,0x42,0x57,0xae,0x18,0x6f,0x91,0xd6,0x61, 264e1051a39Sopenharmony_ci 0x03,0x8a,0x1c,0x89,0x73,0xc7,0x56,0x41,0x03,0xd3,0xf8,0xed,0x65,0xe2,0x85,0x02, 265e1051a39Sopenharmony_ci 0x15,0x00,0x89,0x94,0xab,0x10,0x67,0x45,0x41,0xad,0x63,0xc6,0x71,0x40,0x8d,0x6b, 266e1051a39Sopenharmony_ci 0x9e,0x19,0x5b,0xa4,0xc7,0xf5,0x03,0x81,0x84,0x00,0x02,0x81,0x80,0x2f,0x5b,0xde, 267e1051a39Sopenharmony_ci 0x72,0x02,0x36,0x6b,0x00,0x5e,0x24,0x7f,0x14,0x2c,0x18,0x52,0x42,0x97,0x4b,0xdb, 268e1051a39Sopenharmony_ci 0x6e,0x15,0x50,0x3c,0x45,0x3e,0x25,0xf3,0xb7,0xc5,0x6e,0xe5,0x52,0xe7,0xc4,0xfb, 269e1051a39Sopenharmony_ci 0xf4,0xa5,0xf0,0x39,0x12,0x7f,0xbc,0x54,0x1c,0x93,0xb9,0x5e,0xee,0xe9,0x14,0xb0, 270e1051a39Sopenharmony_ci 0xdf,0xfe,0xfc,0x36,0xe4,0xf2,0xaf,0xfb,0x13,0xc8,0xdf,0x18,0x94,0x1d,0x40,0xb9, 271e1051a39Sopenharmony_ci 0x71,0xdd,0x4c,0x9c,0xa7,0x03,0x52,0x02,0xb5,0xed,0x71,0x80,0x3e,0x23,0xda,0x28, 272e1051a39Sopenharmony_ci 0xe5,0xab,0xe7,0x6f,0xf2,0x0a,0x0e,0x00,0x5b,0x7d,0xc6,0x4b,0xd7,0xc7,0xb2,0xc3, 273e1051a39Sopenharmony_ci 0xba,0x62,0x7f,0x70,0x28,0xa0,0x9d,0x71,0x13,0x70,0xd1,0x9f,0x32,0x2f,0x3e,0xd2, 274e1051a39Sopenharmony_ci 0xcd,0x1b,0xa4,0xc6,0x72,0xa0,0x74,0x5d,0x71,0xef,0x03,0x43,0x6e,0xa3,0x60,0x30, 275e1051a39Sopenharmony_ci 0x5e,0x30,0x0c,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x02,0x30,0x00,0x30, 276e1051a39Sopenharmony_ci 0x0e,0x06,0x03,0x55,0x1d,0x0f,0x01,0x01,0xff,0x04,0x04,0x03,0x02,0x05,0xe0,0x30, 277e1051a39Sopenharmony_ci 0x1d,0x06,0x03,0x55,0x1d,0x0e,0x04,0x16,0x04,0x14,0x0b,0x5a,0x4d,0x5f,0x7d,0x25, 278e1051a39Sopenharmony_ci 0xc7,0xf2,0x9d,0xc1,0xaa,0xb7,0x63,0x82,0x2f,0xfa,0x8f,0x32,0xe7,0xc0,0x30,0x1f, 279e1051a39Sopenharmony_ci 0x06,0x03,0x55,0x1d,0x23,0x04,0x18,0x30,0x16,0x80,0x14,0xdf,0x7e,0x5e,0x88,0x05, 280e1051a39Sopenharmony_ci 0x24,0x33,0x08,0xdd,0x22,0x81,0x02,0x97,0xcc,0x9a,0xb7,0xb1,0x33,0x27,0x30,0x30, 281e1051a39Sopenharmony_ci 0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x03,0x82, 282e1051a39Sopenharmony_ci 0x01,0x01,0x00,0x5a,0xf2,0x63,0xef,0xd3,0x16,0xd7,0xf5,0xaa,0xdd,0x12,0x00,0x36, 283e1051a39Sopenharmony_ci 0x00,0x21,0xa2,0x7b,0x08,0xd6,0x3b,0x9f,0x62,0xac,0x53,0x1f,0xed,0x4c,0xd1,0x15, 284e1051a39Sopenharmony_ci 0x34,0x65,0x71,0xee,0x96,0x07,0xa6,0xef,0xb2,0xde,0xd8,0xbb,0x35,0x6e,0x2c,0xe2, 285e1051a39Sopenharmony_ci 0xd1,0x26,0xef,0x7e,0x94,0xe2,0x88,0x51,0xa4,0x6c,0xaa,0x27,0x2a,0xd3,0xb6,0xc2, 286e1051a39Sopenharmony_ci 0xf7,0xea,0xc3,0x0b,0xa9,0xb5,0x28,0x37,0xa2,0x63,0x08,0xe4,0x88,0xc0,0x1b,0x16, 287e1051a39Sopenharmony_ci 0x1b,0xca,0xfd,0x8a,0x07,0x32,0x29,0xa7,0x53,0xb5,0x2d,0x30,0xe4,0xf5,0x16,0xc3, 288e1051a39Sopenharmony_ci 0xe3,0xc2,0x4c,0x30,0x5d,0x35,0x80,0x1c,0xa2,0xdb,0xe3,0x4b,0x51,0x0d,0x4c,0x60, 289e1051a39Sopenharmony_ci 0x5f,0xb9,0x46,0xac,0xa8,0x46,0xa7,0x32,0xa7,0x9c,0x76,0xf8,0xe9,0xb5,0x19,0xe2, 290e1051a39Sopenharmony_ci 0x0c,0xe1,0x0f,0xc6,0x46,0xe2,0x38,0xa7,0x87,0x72,0x6d,0x6c,0xbc,0x88,0x2f,0x9d, 291e1051a39Sopenharmony_ci 0x2d,0xe5,0xd0,0x7d,0x1e,0xc7,0x5d,0xf8,0x7e,0xb4,0x0b,0xa6,0xf9,0x6c,0xe3,0x7c, 292e1051a39Sopenharmony_ci 0xb2,0x70,0x6e,0x75,0x9b,0x1e,0x63,0xe1,0x4d,0xb2,0x81,0xd3,0x55,0x38,0x94,0x1a, 293e1051a39Sopenharmony_ci 0x7a,0xfa,0xbf,0x01,0x18,0x70,0x2d,0x35,0xd3,0xe3,0x10,0x7a,0x9a,0xa7,0x8f,0xf3, 294e1051a39Sopenharmony_ci 0xbd,0x56,0x55,0x5e,0xd8,0xbd,0x4e,0x16,0x76,0xd0,0x48,0x4c,0xf9,0x51,0x54,0xdf, 295e1051a39Sopenharmony_ci 0x2d,0xb0,0xc9,0xaa,0x5e,0x42,0x38,0x50,0xbf,0x0f,0xc0,0xd9,0x84,0x44,0x4b,0x42, 296e1051a39Sopenharmony_ci 0x24,0xec,0x14,0xa3,0xde,0x11,0xdf,0x58,0x7f,0xc2,0x4d,0xb2,0xd5,0x42,0x78,0x6e, 297e1051a39Sopenharmony_ci 0x52,0x3e,0xad,0xc3,0x5f,0x04,0xc4,0xe6,0x31,0xaa,0x81,0x06,0x8b,0x13,0x4b,0x3c, 298e1051a39Sopenharmony_ci 0x0e,0x6a,0xb1 299e1051a39Sopenharmony_ci }; 300e1051a39Sopenharmony_ci 301e1051a39Sopenharmony_ci if (!TEST_ptr(bio = BIO_new_mem_buf(dhx_cert, sizeof(dhx_cert))) 302e1051a39Sopenharmony_ci || !TEST_ptr(cert = X509_new_ex(libctx, NULL)) 303e1051a39Sopenharmony_ci || !TEST_ptr(d2i_X509_bio(bio, &cert))) 304e1051a39Sopenharmony_ci goto err; 305e1051a39Sopenharmony_ci ret = 1; 306e1051a39Sopenharmony_cierr: 307e1051a39Sopenharmony_ci X509_free(cert); 308e1051a39Sopenharmony_ci BIO_free(bio); 309e1051a39Sopenharmony_ci return ret; 310e1051a39Sopenharmony_ci} 311e1051a39Sopenharmony_ci 312e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DH */ 313e1051a39Sopenharmony_ci 314e1051a39Sopenharmony_cistatic int test_cipher_reinit(int test_id) 315e1051a39Sopenharmony_ci{ 316e1051a39Sopenharmony_ci int ret = 0, diff, ccm, siv, no_null_key; 317e1051a39Sopenharmony_ci int out1_len = 0, out2_len = 0, out3_len = 0; 318e1051a39Sopenharmony_ci EVP_CIPHER *cipher = NULL; 319e1051a39Sopenharmony_ci EVP_CIPHER_CTX *ctx = NULL; 320e1051a39Sopenharmony_ci unsigned char out1[256]; 321e1051a39Sopenharmony_ci unsigned char out2[256]; 322e1051a39Sopenharmony_ci unsigned char out3[256]; 323e1051a39Sopenharmony_ci unsigned char in[16] = { 324e1051a39Sopenharmony_ci 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 325e1051a39Sopenharmony_ci 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 326e1051a39Sopenharmony_ci }; 327e1051a39Sopenharmony_ci unsigned char key[64] = { 328e1051a39Sopenharmony_ci 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 329e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 330e1051a39Sopenharmony_ci 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 331e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 332e1051a39Sopenharmony_ci 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 333e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 334e1051a39Sopenharmony_ci 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 335e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 336e1051a39Sopenharmony_ci }; 337e1051a39Sopenharmony_ci unsigned char iv[16] = { 338e1051a39Sopenharmony_ci 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 339e1051a39Sopenharmony_ci 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 340e1051a39Sopenharmony_ci }; 341e1051a39Sopenharmony_ci const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id); 342e1051a39Sopenharmony_ci 343e1051a39Sopenharmony_ci if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) 344e1051a39Sopenharmony_ci goto err; 345e1051a39Sopenharmony_ci 346e1051a39Sopenharmony_ci TEST_note("Fetching %s\n", name); 347e1051a39Sopenharmony_ci if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL))) 348e1051a39Sopenharmony_ci goto err; 349e1051a39Sopenharmony_ci 350e1051a39Sopenharmony_ci /* ccm fails on the second update - this matches OpenSSL 1_1_1 behaviour */ 351e1051a39Sopenharmony_ci ccm = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE); 352e1051a39Sopenharmony_ci 353e1051a39Sopenharmony_ci /* siv cannot be called with NULL key as the iv is irrelevant */ 354e1051a39Sopenharmony_ci siv = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_SIV_MODE); 355e1051a39Sopenharmony_ci 356e1051a39Sopenharmony_ci /* 357e1051a39Sopenharmony_ci * Skip init call with a null key for RC4 as the stream cipher does not 358e1051a39Sopenharmony_ci * handle reinit (1.1.1 behaviour). 359e1051a39Sopenharmony_ci */ 360e1051a39Sopenharmony_ci no_null_key = EVP_CIPHER_is_a(cipher, "RC4") 361e1051a39Sopenharmony_ci || EVP_CIPHER_is_a(cipher, "RC4-40") 362e1051a39Sopenharmony_ci || EVP_CIPHER_is_a(cipher, "RC4-HMAC-MD5"); 363e1051a39Sopenharmony_ci 364e1051a39Sopenharmony_ci /* DES3-WRAP uses random every update - so it will give a different value */ 365e1051a39Sopenharmony_ci diff = EVP_CIPHER_is_a(cipher, "DES3-WRAP"); 366e1051a39Sopenharmony_ci 367e1051a39Sopenharmony_ci if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv)) 368e1051a39Sopenharmony_ci || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, sizeof(in))) 369e1051a39Sopenharmony_ci || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) 370e1051a39Sopenharmony_ci || !TEST_int_eq(EVP_EncryptUpdate(ctx, out2, &out2_len, in, sizeof(in)), 371e1051a39Sopenharmony_ci ccm ? 0 : 1) 372e1051a39Sopenharmony_ci || (!no_null_key 373e1051a39Sopenharmony_ci && (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv)) 374e1051a39Sopenharmony_ci || !TEST_int_eq(EVP_EncryptUpdate(ctx, out3, &out3_len, in, sizeof(in)), 375e1051a39Sopenharmony_ci ccm || siv ? 0 : 1)))) 376e1051a39Sopenharmony_ci goto err; 377e1051a39Sopenharmony_ci 378e1051a39Sopenharmony_ci if (ccm == 0) { 379e1051a39Sopenharmony_ci if (diff) { 380e1051a39Sopenharmony_ci if (!TEST_mem_ne(out1, out1_len, out2, out2_len) 381e1051a39Sopenharmony_ci || !TEST_mem_ne(out1, out1_len, out3, out3_len) 382e1051a39Sopenharmony_ci || !TEST_mem_ne(out2, out2_len, out3, out3_len)) 383e1051a39Sopenharmony_ci goto err; 384e1051a39Sopenharmony_ci } else { 385e1051a39Sopenharmony_ci if (!TEST_mem_eq(out1, out1_len, out2, out2_len) 386e1051a39Sopenharmony_ci || (!siv && !no_null_key && !TEST_mem_eq(out1, out1_len, out3, out3_len))) 387e1051a39Sopenharmony_ci goto err; 388e1051a39Sopenharmony_ci } 389e1051a39Sopenharmony_ci } 390e1051a39Sopenharmony_ci ret = 1; 391e1051a39Sopenharmony_cierr: 392e1051a39Sopenharmony_ci EVP_CIPHER_free(cipher); 393e1051a39Sopenharmony_ci EVP_CIPHER_CTX_free(ctx); 394e1051a39Sopenharmony_ci return ret; 395e1051a39Sopenharmony_ci} 396e1051a39Sopenharmony_ci 397e1051a39Sopenharmony_ci/* 398e1051a39Sopenharmony_ci * This test only uses a partial block (half the block size) of input for each 399e1051a39Sopenharmony_ci * EVP_EncryptUpdate() in order to test that the second init/update is not using 400e1051a39Sopenharmony_ci * a leftover buffer from the first init/update. 401e1051a39Sopenharmony_ci * Note: some ciphers don't need a full block to produce output. 402e1051a39Sopenharmony_ci */ 403e1051a39Sopenharmony_cistatic int test_cipher_reinit_partialupdate(int test_id) 404e1051a39Sopenharmony_ci{ 405e1051a39Sopenharmony_ci int ret = 0, in_len; 406e1051a39Sopenharmony_ci int out1_len = 0, out2_len = 0, out3_len = 0; 407e1051a39Sopenharmony_ci EVP_CIPHER *cipher = NULL; 408e1051a39Sopenharmony_ci EVP_CIPHER_CTX *ctx = NULL; 409e1051a39Sopenharmony_ci unsigned char out1[256]; 410e1051a39Sopenharmony_ci unsigned char out2[256]; 411e1051a39Sopenharmony_ci unsigned char out3[256]; 412e1051a39Sopenharmony_ci static const unsigned char in[32] = { 413e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 414e1051a39Sopenharmony_ci 0xba, 0xbe, 0xba, 0xbe, 0x00, 0x00, 0xba, 0xbe, 415e1051a39Sopenharmony_ci 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 416e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 417e1051a39Sopenharmony_ci }; 418e1051a39Sopenharmony_ci static const unsigned char key[64] = { 419e1051a39Sopenharmony_ci 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 420e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 421e1051a39Sopenharmony_ci 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 422e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 423e1051a39Sopenharmony_ci 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 424e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 425e1051a39Sopenharmony_ci 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 426e1051a39Sopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 427e1051a39Sopenharmony_ci }; 428e1051a39Sopenharmony_ci static const unsigned char iv[16] = { 429e1051a39Sopenharmony_ci 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 430e1051a39Sopenharmony_ci 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 431e1051a39Sopenharmony_ci }; 432e1051a39Sopenharmony_ci const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id); 433e1051a39Sopenharmony_ci 434e1051a39Sopenharmony_ci if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) 435e1051a39Sopenharmony_ci goto err; 436e1051a39Sopenharmony_ci 437e1051a39Sopenharmony_ci TEST_note("Fetching %s\n", name); 438e1051a39Sopenharmony_ci if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL))) 439e1051a39Sopenharmony_ci goto err; 440e1051a39Sopenharmony_ci 441e1051a39Sopenharmony_ci in_len = EVP_CIPHER_get_block_size(cipher) / 2; 442e1051a39Sopenharmony_ci 443e1051a39Sopenharmony_ci /* skip any ciphers that don't allow partial updates */ 444e1051a39Sopenharmony_ci if (((EVP_CIPHER_get_flags(cipher) 445e1051a39Sopenharmony_ci & (EVP_CIPH_FLAG_CTS | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) != 0) 446e1051a39Sopenharmony_ci || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE 447e1051a39Sopenharmony_ci || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE 448e1051a39Sopenharmony_ci || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_WRAP_MODE) { 449e1051a39Sopenharmony_ci ret = 1; 450e1051a39Sopenharmony_ci goto err; 451e1051a39Sopenharmony_ci } 452e1051a39Sopenharmony_ci 453e1051a39Sopenharmony_ci if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv)) 454e1051a39Sopenharmony_ci || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, in_len)) 455e1051a39Sopenharmony_ci || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) 456e1051a39Sopenharmony_ci || !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, in_len))) 457e1051a39Sopenharmony_ci goto err; 458e1051a39Sopenharmony_ci 459e1051a39Sopenharmony_ci if (!TEST_mem_eq(out1, out1_len, out2, out2_len)) 460e1051a39Sopenharmony_ci goto err; 461e1051a39Sopenharmony_ci 462e1051a39Sopenharmony_ci if (EVP_CIPHER_get_mode(cipher) != EVP_CIPH_SIV_MODE) { 463e1051a39Sopenharmony_ci if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv)) 464e1051a39Sopenharmony_ci || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, in_len))) 465e1051a39Sopenharmony_ci goto err; 466e1051a39Sopenharmony_ci 467e1051a39Sopenharmony_ci if (!TEST_mem_eq(out1, out1_len, out3, out3_len)) 468e1051a39Sopenharmony_ci goto err; 469e1051a39Sopenharmony_ci } 470e1051a39Sopenharmony_ci ret = 1; 471e1051a39Sopenharmony_cierr: 472e1051a39Sopenharmony_ci EVP_CIPHER_free(cipher); 473e1051a39Sopenharmony_ci EVP_CIPHER_CTX_free(ctx); 474e1051a39Sopenharmony_ci return ret; 475e1051a39Sopenharmony_ci} 476e1051a39Sopenharmony_ci 477e1051a39Sopenharmony_ci 478e1051a39Sopenharmony_cistatic int name_cmp(const char * const *a, const char * const *b) 479e1051a39Sopenharmony_ci{ 480e1051a39Sopenharmony_ci return OPENSSL_strcasecmp(*a, *b); 481e1051a39Sopenharmony_ci} 482e1051a39Sopenharmony_ci 483e1051a39Sopenharmony_cistatic void collect_cipher_names(EVP_CIPHER *cipher, void *cipher_names_list) 484e1051a39Sopenharmony_ci{ 485e1051a39Sopenharmony_ci STACK_OF(OPENSSL_STRING) *names = cipher_names_list; 486e1051a39Sopenharmony_ci const char *name = EVP_CIPHER_get0_name(cipher); 487e1051a39Sopenharmony_ci char *namedup = NULL; 488e1051a39Sopenharmony_ci 489e1051a39Sopenharmony_ci assert(name != NULL); 490e1051a39Sopenharmony_ci /* the cipher will be freed after returning, strdup is needed */ 491e1051a39Sopenharmony_ci if ((namedup = OPENSSL_strdup(name)) != NULL 492e1051a39Sopenharmony_ci && !sk_OPENSSL_STRING_push(names, namedup)) 493e1051a39Sopenharmony_ci OPENSSL_free(namedup); 494e1051a39Sopenharmony_ci} 495e1051a39Sopenharmony_ci 496e1051a39Sopenharmony_cistatic int rsa_keygen(int bits, EVP_PKEY **pub, EVP_PKEY **priv) 497e1051a39Sopenharmony_ci{ 498e1051a39Sopenharmony_ci int ret = 0; 499e1051a39Sopenharmony_ci unsigned char *pub_der = NULL; 500e1051a39Sopenharmony_ci const unsigned char *pp = NULL; 501e1051a39Sopenharmony_ci size_t len = 0; 502e1051a39Sopenharmony_ci OSSL_ENCODER_CTX *ectx = NULL; 503e1051a39Sopenharmony_ci 504e1051a39Sopenharmony_ci if (!TEST_ptr(*priv = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", bits)) 505e1051a39Sopenharmony_ci || !TEST_ptr(ectx = 506e1051a39Sopenharmony_ci OSSL_ENCODER_CTX_new_for_pkey(*priv, 507e1051a39Sopenharmony_ci EVP_PKEY_PUBLIC_KEY, 508e1051a39Sopenharmony_ci "DER", "type-specific", 509e1051a39Sopenharmony_ci NULL)) 510e1051a39Sopenharmony_ci || !TEST_true(OSSL_ENCODER_to_data(ectx, &pub_der, &len))) 511e1051a39Sopenharmony_ci goto err; 512e1051a39Sopenharmony_ci pp = pub_der; 513e1051a39Sopenharmony_ci if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_RSA, pub, &pp, len))) 514e1051a39Sopenharmony_ci goto err; 515e1051a39Sopenharmony_ci ret = 1; 516e1051a39Sopenharmony_cierr: 517e1051a39Sopenharmony_ci OSSL_ENCODER_CTX_free(ectx); 518e1051a39Sopenharmony_ci OPENSSL_free(pub_der); 519e1051a39Sopenharmony_ci return ret; 520e1051a39Sopenharmony_ci} 521e1051a39Sopenharmony_ci 522e1051a39Sopenharmony_cistatic int kem_rsa_gen_recover(void) 523e1051a39Sopenharmony_ci{ 524e1051a39Sopenharmony_ci int ret = 0; 525e1051a39Sopenharmony_ci EVP_PKEY *pub = NULL; 526e1051a39Sopenharmony_ci EVP_PKEY *priv = NULL; 527e1051a39Sopenharmony_ci EVP_PKEY_CTX *sctx = NULL, *rctx = NULL, *dctx = NULL; 528e1051a39Sopenharmony_ci unsigned char secret[256] = { 0, }; 529e1051a39Sopenharmony_ci unsigned char ct[256] = { 0, }; 530e1051a39Sopenharmony_ci unsigned char unwrap[256] = { 0, }; 531e1051a39Sopenharmony_ci size_t ctlen = 0, unwraplen = 0, secretlen = 0; 532e1051a39Sopenharmony_ci int bits = 2048; 533e1051a39Sopenharmony_ci 534e1051a39Sopenharmony_ci ret = TEST_true(rsa_keygen(bits, &pub, &priv)) 535e1051a39Sopenharmony_ci && TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL)) 536e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), 1) 537e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(sctx, "RSASVE"), 1) 538e1051a39Sopenharmony_ci && TEST_ptr(dctx = EVP_PKEY_CTX_dup(sctx)) 539e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(dctx, NULL, &ctlen, NULL, 540e1051a39Sopenharmony_ci &secretlen), 1) 541e1051a39Sopenharmony_ci && TEST_int_eq(ctlen, secretlen) 542e1051a39Sopenharmony_ci && TEST_int_eq(ctlen, bits / 8) 543e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(dctx, ct, &ctlen, secret, 544e1051a39Sopenharmony_ci &secretlen), 1) 545e1051a39Sopenharmony_ci && TEST_ptr(rctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL)) 546e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate_init(rctx, NULL), 1) 547e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(rctx, "RSASVE"), 1) 548e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(rctx, NULL, &unwraplen, 549e1051a39Sopenharmony_ci ct, ctlen), 1) 550e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(rctx, unwrap, &unwraplen, 551e1051a39Sopenharmony_ci ct, ctlen), 1) 552e1051a39Sopenharmony_ci && TEST_mem_eq(unwrap, unwraplen, secret, secretlen); 553e1051a39Sopenharmony_ci EVP_PKEY_free(pub); 554e1051a39Sopenharmony_ci EVP_PKEY_free(priv); 555e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(rctx); 556e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(dctx); 557e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(sctx); 558e1051a39Sopenharmony_ci return ret; 559e1051a39Sopenharmony_ci} 560e1051a39Sopenharmony_ci 561e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DES 562e1051a39Sopenharmony_ci/* 563e1051a39Sopenharmony_ci * This test makes sure that EVP_CIPHER_CTX_rand_key() works correctly 564e1051a39Sopenharmony_ci * For fips mode this code would produce an error if the flag is not set. 565e1051a39Sopenharmony_ci */ 566e1051a39Sopenharmony_cistatic int test_cipher_tdes_randkey(void) 567e1051a39Sopenharmony_ci{ 568e1051a39Sopenharmony_ci int ret; 569e1051a39Sopenharmony_ci EVP_CIPHER_CTX *ctx = NULL; 570e1051a39Sopenharmony_ci EVP_CIPHER *tdes_cipher = NULL, *aes_cipher = NULL; 571e1051a39Sopenharmony_ci unsigned char key[24] = { 0 }; 572e1051a39Sopenharmony_ci 573e1051a39Sopenharmony_ci ret = TEST_ptr(aes_cipher = EVP_CIPHER_fetch(libctx, "AES-256-CBC", NULL)) 574e1051a39Sopenharmony_ci && TEST_int_eq(EVP_CIPHER_get_flags(aes_cipher) & EVP_CIPH_RAND_KEY, 0) 575e1051a39Sopenharmony_ci && TEST_ptr(tdes_cipher = EVP_CIPHER_fetch(libctx, "DES-EDE3-CBC", NULL)) 576e1051a39Sopenharmony_ci && TEST_int_ne(EVP_CIPHER_get_flags(tdes_cipher) & EVP_CIPH_RAND_KEY, 0) 577e1051a39Sopenharmony_ci && TEST_ptr(ctx = EVP_CIPHER_CTX_new()) 578e1051a39Sopenharmony_ci && TEST_true(EVP_CipherInit_ex(ctx, tdes_cipher, NULL, NULL, NULL, 1)) 579e1051a39Sopenharmony_ci && TEST_int_gt(EVP_CIPHER_CTX_rand_key(ctx, key), 0); 580e1051a39Sopenharmony_ci 581e1051a39Sopenharmony_ci EVP_CIPHER_CTX_free(ctx); 582e1051a39Sopenharmony_ci EVP_CIPHER_free(tdes_cipher); 583e1051a39Sopenharmony_ci EVP_CIPHER_free(aes_cipher); 584e1051a39Sopenharmony_ci return ret; 585e1051a39Sopenharmony_ci} 586e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DES */ 587e1051a39Sopenharmony_ci 588e1051a39Sopenharmony_cistatic int kem_rsa_params(void) 589e1051a39Sopenharmony_ci{ 590e1051a39Sopenharmony_ci int ret = 0; 591e1051a39Sopenharmony_ci EVP_PKEY *pub = NULL; 592e1051a39Sopenharmony_ci EVP_PKEY *priv = NULL; 593e1051a39Sopenharmony_ci EVP_PKEY_CTX *pubctx = NULL, *privctx = NULL; 594e1051a39Sopenharmony_ci unsigned char secret[256] = { 0, }; 595e1051a39Sopenharmony_ci unsigned char ct[256] = { 0, }; 596e1051a39Sopenharmony_ci size_t ctlen = 0, secretlen = 0; 597e1051a39Sopenharmony_ci 598e1051a39Sopenharmony_ci ret = TEST_true(rsa_keygen(2048, &pub, &priv)) 599e1051a39Sopenharmony_ci && TEST_ptr(pubctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL)) 600e1051a39Sopenharmony_ci && TEST_ptr(privctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL)) 601e1051a39Sopenharmony_ci /* Test setting kem op before the init fails */ 602e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), -2) 603e1051a39Sopenharmony_ci /* Test NULL ctx passed */ 604e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate_init(NULL, NULL), 0) 605e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(NULL, NULL, NULL, NULL, NULL), 0) 606e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate_init(NULL, NULL), 0) 607e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(NULL, NULL, NULL, NULL, 0), 0) 608e1051a39Sopenharmony_ci /* Test Invalid operation */ 609e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), -1) 610e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, NULL, 0), 0) 611e1051a39Sopenharmony_ci /* Wrong key component - no secret should be returned on failure */ 612e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate_init(pubctx, NULL), 1) 613e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1) 614e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(pubctx, secret, &secretlen, ct, 615e1051a39Sopenharmony_ci sizeof(ct)), 0) 616e1051a39Sopenharmony_ci && TEST_uchar_eq(secret[0], 0) 617e1051a39Sopenharmony_ci /* Test encapsulate fails if the mode is not set */ 618e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate_init(pubctx, NULL), 1) 619e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, &secretlen), -2) 620e1051a39Sopenharmony_ci /* Test setting a bad kem ops fail */ 621e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSA"), 0) 622e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, NULL), 0) 623e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, "RSASVE"), 0) 624e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, NULL), 0) 625e1051a39Sopenharmony_ci /* Test secretlen is optional */ 626e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1) 627e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, NULL), 1) 628e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1) 629e1051a39Sopenharmony_ci /* Test outlen is optional */ 630e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1) 631e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, &secretlen), 1) 632e1051a39Sopenharmony_ci /* test that either len must be set if out is NULL */ 633e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), 0) 634e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1) 635e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1) 636e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, &secretlen), 1) 637e1051a39Sopenharmony_ci /* Secret buffer should be set if there is an output buffer */ 638e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, NULL, NULL), 0) 639e1051a39Sopenharmony_ci /* Test that lengths are optional if ct is not NULL */ 640e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, NULL), 1) 641e1051a39Sopenharmony_ci /* Pass if secret or secret length are not NULL */ 642e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate_init(privctx, NULL), 1) 643e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(privctx, "RSASVE"), 1) 644e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, NULL, ct, sizeof(ct)), 1) 645e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, &secretlen, ct, sizeof(ct)), 1) 646e1051a39Sopenharmony_ci && TEST_int_eq(secretlen, 256) 647e1051a39Sopenharmony_ci /* Fail if passed NULL arguments */ 648e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, ct, sizeof(ct)), 0) 649e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, 0), 0) 650e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, sizeof(ct)), 0) 651e1051a39Sopenharmony_ci && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, ct, 0), 0); 652e1051a39Sopenharmony_ci 653e1051a39Sopenharmony_ci EVP_PKEY_free(pub); 654e1051a39Sopenharmony_ci EVP_PKEY_free(priv); 655e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(pubctx); 656e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(privctx); 657e1051a39Sopenharmony_ci return ret; 658e1051a39Sopenharmony_ci} 659e1051a39Sopenharmony_ci 660e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 661e1051a39Sopenharmony_cistatic EVP_PKEY *gen_dh_key(void) 662e1051a39Sopenharmony_ci{ 663e1051a39Sopenharmony_ci EVP_PKEY_CTX *gctx = NULL; 664e1051a39Sopenharmony_ci EVP_PKEY *pkey = NULL; 665e1051a39Sopenharmony_ci OSSL_PARAM params[2]; 666e1051a39Sopenharmony_ci 667e1051a39Sopenharmony_ci params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0); 668e1051a39Sopenharmony_ci params[1] = OSSL_PARAM_construct_end(); 669e1051a39Sopenharmony_ci 670e1051a39Sopenharmony_ci if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL)) 671e1051a39Sopenharmony_ci || !TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0) 672e1051a39Sopenharmony_ci || !TEST_true(EVP_PKEY_CTX_set_params(gctx, params)) 673e1051a39Sopenharmony_ci || !TEST_true(EVP_PKEY_keygen(gctx, &pkey))) 674e1051a39Sopenharmony_ci goto err; 675e1051a39Sopenharmony_cierr: 676e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(gctx); 677e1051a39Sopenharmony_ci return pkey; 678e1051a39Sopenharmony_ci} 679e1051a39Sopenharmony_ci 680e1051a39Sopenharmony_ci/* Fail if we try to use a dh key */ 681e1051a39Sopenharmony_cistatic int kem_invalid_keytype(void) 682e1051a39Sopenharmony_ci{ 683e1051a39Sopenharmony_ci int ret = 0; 684e1051a39Sopenharmony_ci EVP_PKEY *key = NULL; 685e1051a39Sopenharmony_ci EVP_PKEY_CTX *sctx = NULL; 686e1051a39Sopenharmony_ci 687e1051a39Sopenharmony_ci if (!TEST_ptr(key = gen_dh_key())) 688e1051a39Sopenharmony_ci goto done; 689e1051a39Sopenharmony_ci 690e1051a39Sopenharmony_ci if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL))) 691e1051a39Sopenharmony_ci goto done; 692e1051a39Sopenharmony_ci if (!TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), -2)) 693e1051a39Sopenharmony_ci goto done; 694e1051a39Sopenharmony_ci 695e1051a39Sopenharmony_ci ret = 1; 696e1051a39Sopenharmony_cidone: 697e1051a39Sopenharmony_ci EVP_PKEY_free(key); 698e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(sctx); 699e1051a39Sopenharmony_ci return ret; 700e1051a39Sopenharmony_ci} 701e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DH */ 702e1051a39Sopenharmony_ci 703e1051a39Sopenharmony_ciint setup_tests(void) 704e1051a39Sopenharmony_ci{ 705e1051a39Sopenharmony_ci const char *prov_name = "default"; 706e1051a39Sopenharmony_ci char *config_file = NULL; 707e1051a39Sopenharmony_ci OPTION_CHOICE o; 708e1051a39Sopenharmony_ci 709e1051a39Sopenharmony_ci while ((o = opt_next()) != OPT_EOF) { 710e1051a39Sopenharmony_ci switch (o) { 711e1051a39Sopenharmony_ci case OPT_PROVIDER_NAME: 712e1051a39Sopenharmony_ci prov_name = opt_arg(); 713e1051a39Sopenharmony_ci break; 714e1051a39Sopenharmony_ci case OPT_CONFIG_FILE: 715e1051a39Sopenharmony_ci config_file = opt_arg(); 716e1051a39Sopenharmony_ci break; 717e1051a39Sopenharmony_ci case OPT_TEST_CASES: 718e1051a39Sopenharmony_ci break; 719e1051a39Sopenharmony_ci default: 720e1051a39Sopenharmony_ci case OPT_ERR: 721e1051a39Sopenharmony_ci return 0; 722e1051a39Sopenharmony_ci } 723e1051a39Sopenharmony_ci } 724e1051a39Sopenharmony_ci 725e1051a39Sopenharmony_ci if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name)) 726e1051a39Sopenharmony_ci return 0; 727e1051a39Sopenharmony_ci 728e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH) 729e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_dsa_param_keygen, 3 * 3 * 3); 730e1051a39Sopenharmony_ci#endif 731e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 732e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_dh_safeprime_param_keygen, 3 * 3 * 3); 733e1051a39Sopenharmony_ci ADD_TEST(dhx_cert_load); 734e1051a39Sopenharmony_ci#endif 735e1051a39Sopenharmony_ci 736e1051a39Sopenharmony_ci if (!TEST_ptr(cipher_names = sk_OPENSSL_STRING_new(name_cmp))) 737e1051a39Sopenharmony_ci return 0; 738e1051a39Sopenharmony_ci EVP_CIPHER_do_all_provided(libctx, collect_cipher_names, cipher_names); 739e1051a39Sopenharmony_ci 740e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_cipher_reinit, sk_OPENSSL_STRING_num(cipher_names)); 741e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_cipher_reinit_partialupdate, 742e1051a39Sopenharmony_ci sk_OPENSSL_STRING_num(cipher_names)); 743e1051a39Sopenharmony_ci ADD_TEST(kem_rsa_gen_recover); 744e1051a39Sopenharmony_ci ADD_TEST(kem_rsa_params); 745e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 746e1051a39Sopenharmony_ci ADD_TEST(kem_invalid_keytype); 747e1051a39Sopenharmony_ci#endif 748e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DES 749e1051a39Sopenharmony_ci ADD_TEST(test_cipher_tdes_randkey); 750e1051a39Sopenharmony_ci#endif 751e1051a39Sopenharmony_ci return 1; 752e1051a39Sopenharmony_ci} 753e1051a39Sopenharmony_ci 754e1051a39Sopenharmony_ci/* Because OPENSSL_free is a macro, it can't be passed as a function pointer */ 755e1051a39Sopenharmony_cistatic void string_free(char *m) 756e1051a39Sopenharmony_ci{ 757e1051a39Sopenharmony_ci OPENSSL_free(m); 758e1051a39Sopenharmony_ci} 759e1051a39Sopenharmony_ci 760e1051a39Sopenharmony_civoid cleanup_tests(void) 761e1051a39Sopenharmony_ci{ 762e1051a39Sopenharmony_ci sk_OPENSSL_STRING_pop_free(cipher_names, string_free); 763e1051a39Sopenharmony_ci OSSL_PROVIDER_unload(libprov); 764e1051a39Sopenharmony_ci OSSL_LIB_CTX_free(libctx); 765e1051a39Sopenharmony_ci OSSL_PROVIDER_unload(nullprov); 766e1051a39Sopenharmony_ci} 767