1 /* 2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <string.h> 11 #include <openssl/core_dispatch.h> 12 #include <openssl/e_os2.h> 13 #include <openssl/params.h> 14 #include <openssl/core_names.h> 15 #include <openssl/evp.h> 16 #include <openssl/err.h> 17 #include <openssl/randerr.h> 18 #include "prov/providercommon.h" 19 #include "prov/provider_ctx.h" 20 #include "prov/provider_util.h" 21 #include "prov/implementations.h" 22 23 static OSSL_FUNC_rand_newctx_fn test_rng_new; 24 static OSSL_FUNC_rand_freectx_fn test_rng_free; 25 static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate; 26 static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate; 27 static OSSL_FUNC_rand_generate_fn test_rng_generate; 28 static OSSL_FUNC_rand_reseed_fn test_rng_reseed; 29 static OSSL_FUNC_rand_nonce_fn test_rng_nonce; 30 static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params; 31 static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params; 32 static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params; 33 static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params; 34 static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization; 35 static OSSL_FUNC_rand_enable_locking_fn test_rng_enable_locking; 36 static OSSL_FUNC_rand_lock_fn test_rng_lock; 37 static OSSL_FUNC_rand_unlock_fn test_rng_unlock; 38 static OSSL_FUNC_rand_get_seed_fn test_rng_get_seed; 39 40 typedef struct { 41 void *provctx; 42 int state; 43 unsigned int strength; 44 size_t max_request; 45 unsigned char *entropy, *nonce; 46 size_t entropy_len, entropy_pos, nonce_len; 47 CRYPTO_RWLOCK *lock; 48 } PROV_TEST_RNG; 49 50 static void *test_rng_new(void *provctx, void *parent, 51 const OSSL_DISPATCH *parent_dispatch) 52 { 53 PROV_TEST_RNG *t; 54 55 t = OPENSSL_zalloc(sizeof(*t)); 56 if (t == NULL) 57 return NULL; 58 59 t->max_request = INT_MAX; 60 t->provctx = provctx; 61 t->state = EVP_RAND_STATE_UNINITIALISED; 62 return t; 63 } 64 65 static void test_rng_free(void *vtest) 66 { 67 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 68 69 if (t == NULL) 70 return; 71 OPENSSL_free(t->entropy); 72 OPENSSL_free(t->nonce); 73 CRYPTO_THREAD_lock_free(t->lock); 74 OPENSSL_free(t); 75 } 76 77 static int test_rng_instantiate(void *vtest, unsigned int strength, 78 int prediction_resistance, 79 const unsigned char *pstr, size_t pstr_len, 80 const OSSL_PARAM params[]) 81 { 82 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 83 84 if (!test_rng_set_ctx_params(t, params) || strength > t->strength) 85 return 0; 86 87 t->state = EVP_RAND_STATE_READY; 88 t->entropy_pos = 0; 89 90 return 1; 91 } 92 93 static int test_rng_uninstantiate(void *vtest) 94 { 95 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 96 97 t->entropy_pos = 0; 98 t->state = EVP_RAND_STATE_UNINITIALISED; 99 return 1; 100 } 101 102 static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen, 103 unsigned int strength, int prediction_resistance, 104 const unsigned char *adin, size_t adin_len) 105 { 106 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 107 108 if (strength > t->strength || t->entropy_len - t->entropy_pos < outlen) 109 return 0; 110 memcpy(out, t->entropy + t->entropy_pos, outlen); 111 t->entropy_pos += outlen; 112 return 1; 113 } 114 115 static int test_rng_reseed(ossl_unused void *vtest, 116 ossl_unused int prediction_resistance, 117 ossl_unused const unsigned char *ent, 118 ossl_unused size_t ent_len, 119 ossl_unused const unsigned char *adin, 120 ossl_unused size_t adin_len) 121 { 122 return 1; 123 } 124 125 static size_t test_rng_nonce(void *vtest, unsigned char *out, 126 unsigned int strength, 127 ossl_unused size_t min_noncelen, 128 ossl_unused size_t max_noncelen) 129 { 130 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 131 132 if (t->nonce == NULL || strength > t->strength) 133 return 0; 134 135 if (out != NULL) 136 memcpy(out, t->nonce, t->nonce_len); 137 return t->nonce_len; 138 } 139 140 static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[]) 141 { 142 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 143 OSSL_PARAM *p; 144 145 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE); 146 if (p != NULL && !OSSL_PARAM_set_int(p, t->state)) 147 return 0; 148 149 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH); 150 if (p != NULL && !OSSL_PARAM_set_int(p, t->strength)) 151 return 0; 152 153 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); 154 if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request)) 155 return 0; 156 return 1; 157 } 158 159 static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *vtest, 160 ossl_unused void *provctx) 161 { 162 static const OSSL_PARAM known_gettable_ctx_params[] = { 163 OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), 164 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), 165 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), 166 OSSL_PARAM_END 167 }; 168 return known_gettable_ctx_params; 169 } 170 171 static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[]) 172 { 173 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 174 const OSSL_PARAM *p; 175 void *ptr = NULL; 176 size_t size = 0; 177 178 if (params == NULL) 179 return 1; 180 181 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH); 182 if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength)) 183 return 0; 184 185 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY); 186 if (p != NULL) { 187 if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size)) 188 return 0; 189 OPENSSL_free(t->entropy); 190 t->entropy = ptr; 191 t->entropy_len = size; 192 t->entropy_pos = 0; 193 ptr = NULL; 194 } 195 196 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE); 197 if (p != NULL) { 198 if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size)) 199 return 0; 200 OPENSSL_free(t->nonce); 201 t->nonce = ptr; 202 t->nonce_len = size; 203 } 204 205 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST); 206 if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request)) 207 return 0; 208 209 return 1; 210 } 211 212 static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *vtest, 213 ossl_unused void *provctx) 214 { 215 static const OSSL_PARAM known_settable_ctx_params[] = { 216 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0), 217 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0), 218 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), 219 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), 220 OSSL_PARAM_END 221 }; 222 return known_settable_ctx_params; 223 } 224 225 static int test_rng_verify_zeroization(ossl_unused void *vtest) 226 { 227 return 1; 228 } 229 230 static size_t test_rng_get_seed(void *vtest, unsigned char **pout, 231 int entropy, size_t min_len, size_t max_len, 232 ossl_unused int prediction_resistance, 233 ossl_unused const unsigned char *adin, 234 ossl_unused size_t adin_len) 235 { 236 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 237 238 *pout = t->entropy; 239 return t->entropy_len > max_len ? max_len : t->entropy_len; 240 } 241 242 static int test_rng_enable_locking(void *vtest) 243 { 244 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 245 246 if (t != NULL && t->lock == NULL) { 247 t->lock = CRYPTO_THREAD_lock_new(); 248 if (t->lock == NULL) { 249 ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK); 250 return 0; 251 } 252 } 253 return 1; 254 } 255 256 static int test_rng_lock(void *vtest) 257 { 258 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 259 260 if (t == NULL || t->lock == NULL) 261 return 1; 262 return CRYPTO_THREAD_write_lock(t->lock); 263 } 264 265 static void test_rng_unlock(void *vtest) 266 { 267 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest; 268 269 if (t != NULL && t->lock != NULL) 270 CRYPTO_THREAD_unlock(t->lock); 271 } 272 273 const OSSL_DISPATCH ossl_test_rng_functions[] = { 274 { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new }, 275 { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free }, 276 { OSSL_FUNC_RAND_INSTANTIATE, 277 (void(*)(void))test_rng_instantiate }, 278 { OSSL_FUNC_RAND_UNINSTANTIATE, 279 (void(*)(void))test_rng_uninstantiate }, 280 { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate }, 281 { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed }, 282 { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce }, 283 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking }, 284 { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock }, 285 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock }, 286 { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS, 287 (void(*)(void))test_rng_settable_ctx_params }, 288 { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params }, 289 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, 290 (void(*)(void))test_rng_gettable_ctx_params }, 291 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params }, 292 { OSSL_FUNC_RAND_VERIFY_ZEROIZATION, 293 (void(*)(void))test_rng_verify_zeroization }, 294 { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed }, 295 { 0, NULL } 296 }; 297