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