From da3831bbe75f68626e362fdadd653becf230cf1f Mon Sep 17 00:00:00 2001 From: Doug Hogan Date: Fri, 21 Dec 2018 07:44:02 -0800 Subject: Make HMAC ctx init internal to hmac.c per PR feedback --- lib/crypto/c_src/crypto.c | 8 ++------ lib/crypto/c_src/hmac.c | 25 +++++++++++++++++++++++-- lib/crypto/c_src/hmac.h | 10 +--------- 3 files changed, 26 insertions(+), 17 deletions(-) (limited to 'lib/crypto') diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c index 09171433cc..5b697ba5aa 100644 --- a/lib/crypto/c_src/crypto.c +++ b/lib/crypto/c_src/crypto.c @@ -181,14 +181,10 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info) return __LINE__; } - hmac_context_rtype = enif_open_resource_type(env, NULL, "hmac_context", - (ErlNifResourceDtor*) hmac_context_dtor, - ERL_NIF_RT_CREATE|ERL_NIF_RT_TAKEOVER, - NULL); - if (!hmac_context_rtype) { - PRINTF_ERR0("CRYPTO: Could not open resource type 'hmac_context'"); + if (!init_hmac_ctx(env)) { return __LINE__; } + #if OPENSSL_VERSION_NUMBER >= PACKED_OPENSSL_VERSION_PLAIN(1,0,0) evp_md_ctx_rtype = enif_open_resource_type(env, NULL, "EVP_MD_CTX", (ErlNifResourceDtor*) evp_md_ctx_dtor, diff --git a/lib/crypto/c_src/hmac.c b/lib/crypto/c_src/hmac.c index 0774dbfb48..143cde90e1 100644 --- a/lib/crypto/c_src/hmac.c +++ b/lib/crypto/c_src/hmac.c @@ -21,7 +21,28 @@ #include "hmac.h" #include "digest.h" -ErlNifResourceType* hmac_context_rtype; +struct hmac_context +{ + ErlNifMutex* mtx; + int alive; + HMAC_CTX* ctx; +}; + +static ErlNifResourceType* hmac_context_rtype; + +static void hmac_context_dtor(ErlNifEnv* env, struct hmac_context*); + +int init_hmac_ctx(ErlNifEnv *env) { + hmac_context_rtype = enif_open_resource_type(env, NULL, "hmac_context", + (ErlNifResourceDtor*) hmac_context_dtor, + ERL_NIF_RT_CREATE|ERL_NIF_RT_TAKEOVER, + NULL); + if (hmac_context_rtype == NULL) { + PRINTF_ERR0("CRYPTO: Could not open resource type 'hmac_context'"); + return 0; + } + return 1; +} ERL_NIF_TERM hmac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {/* (Type, Key, Data) or (Type, Key, Data, MacSize) */ @@ -61,7 +82,7 @@ ERL_NIF_TERM hmac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) return ret; } -void hmac_context_dtor(ErlNifEnv* env, struct hmac_context *obj) +static void hmac_context_dtor(ErlNifEnv* env, struct hmac_context *obj) { if (obj->alive) { HMAC_CTX_free(obj->ctx); diff --git a/lib/crypto/c_src/hmac.h b/lib/crypto/c_src/hmac.h index b27aa521be..1f0e0ca632 100644 --- a/lib/crypto/c_src/hmac.h +++ b/lib/crypto/c_src/hmac.h @@ -23,15 +23,7 @@ #include "common.h" -struct hmac_context -{ - ErlNifMutex* mtx; - int alive; - HMAC_CTX* ctx; -}; - -extern ErlNifResourceType* hmac_context_rtype; -void hmac_context_dtor(ErlNifEnv* env, struct hmac_context*); +int init_hmac_ctx(ErlNifEnv *env); ERL_NIF_TERM hmac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); ERL_NIF_TERM hmac_init_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); -- cgit v1.2.3