/*
- * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* https://www.openssl.org/source/license.html
*/
-#include <openssl/evp.h>
-
#include <stdio.h>
#include <stdlib.h>
-#include <openssl/engine.h>
#include <openssl/evp.h>
-#include <openssl/x509v3.h>
#include <openssl/rand.h>
#include <openssl/core.h>
#include <openssl/core_names.h>
#include "internal/numbers.h"
#include "internal/provider.h"
#include "internal/core.h"
-#include "crypto/asn1.h"
#include "crypto/evp.h"
#include "evp_local.h"
char *type_name;
const char *description;
CRYPTO_REF_COUNT refcnt;
- CRYPTO_RWLOCK *refcnt_lock;
const OSSL_DISPATCH *dispatch;
OSSL_FUNC_rand_newctx_fn *newctx;
int ref = 0;
if (rand != NULL)
- return CRYPTO_UP_REF(&rand->refcnt, &ref, rand->refcnt_lock);
+ return CRYPTO_UP_REF(&rand->refcnt, &ref);
return 1;
}
if (rand == NULL)
return;
- CRYPTO_DOWN_REF(&rand->refcnt, &ref, rand->refcnt_lock);
+ CRYPTO_DOWN_REF(&rand->refcnt, &ref);
if (ref > 0)
return;
OPENSSL_free(rand->type_name);
ossl_provider_free(rand->prov);
- CRYPTO_THREAD_lock_free(rand->refcnt_lock);
+ CRYPTO_FREE_REF(&rand->refcnt);
OPENSSL_free(rand);
}
{
EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
- if (rand == NULL
- || (rand->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
+ if (rand == NULL)
+ return NULL;
+
+ if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
OPENSSL_free(rand);
return NULL;
}
- rand->refcnt = 1;
return rand;
}
#endif
if ((rand = evp_rand_new()) == NULL) {
- ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
return NULL;
}
rand->name_id = name_id;
int EVP_RAND_is_a(const EVP_RAND *rand, const char *name)
{
- return evp_is_a(rand->prov, rand->name_id, NULL, name);
+ return rand != NULL && evp_is_a(rand->prov, rand->name_id, NULL, name);
}
const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand)
return 1;
}
-static int evp_rand_ctx_up_ref(EVP_RAND_CTX *ctx)
+int EVP_RAND_CTX_up_ref(EVP_RAND_CTX *ctx)
{
int ref = 0;
- return CRYPTO_UP_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
+ return CRYPTO_UP_REF(&ctx->refcnt, &ref);
}
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
}
ctx = OPENSSL_zalloc(sizeof(*ctx));
- if (ctx == NULL || (ctx->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
+ if (ctx == NULL)
+ return NULL;
+ if (!CRYPTO_NEW_REF(&ctx->refcnt, 1)) {
OPENSSL_free(ctx);
- ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
return NULL;
}
if (parent != NULL) {
- if (!evp_rand_ctx_up_ref(parent)) {
+ if (!EVP_RAND_CTX_up_ref(parent)) {
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
- CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
+ CRYPTO_FREE_REF(&ctx->refcnt);
OPENSSL_free(ctx);
return NULL;
}
if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
parent_dispatch)) == NULL
|| !EVP_RAND_up_ref(rand)) {
- ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
rand->freectx(ctx->algctx);
- CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
+ CRYPTO_FREE_REF(&ctx->refcnt);
OPENSSL_free(ctx);
EVP_RAND_CTX_free(parent);
return NULL;
}
ctx->meth = rand;
ctx->parent = parent;
- ctx->refcnt = 1;
return ctx;
}
if (ctx == NULL)
return;
- CRYPTO_DOWN_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
+ CRYPTO_DOWN_REF(&ctx->refcnt, &ref);
if (ref > 0)
return;
parent = ctx->parent;
ctx->meth->freectx(ctx->algctx);
ctx->algctx = NULL;
EVP_RAND_free(ctx->meth);
- CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
+ CRYPTO_FREE_REF(&ctx->refcnt);
OPENSSL_free(ctx);
EVP_RAND_CTX_free(parent);
}