From 86cd8b95b704ac236d02f31a92545ca996816c2c Mon Sep 17 00:00:00 2001 From: Hans Nilsson Date: Thu, 14 Mar 2019 19:56:51 +0100 Subject: crypto: Fix bug in ng_api --- lib/crypto/c_src/api_ng.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'lib/crypto/c_src') diff --git a/lib/crypto/c_src/api_ng.c b/lib/crypto/c_src/api_ng.c index 2823b2714b..f727b5e90f 100644 --- a/lib/crypto/c_src/api_ng.c +++ b/lib/crypto/c_src/api_ng.c @@ -218,18 +218,22 @@ static int get_init_args(ErlNifEnv* env, goto err; } } - - if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, key_bin.data, NULL, -1)) { - *return_term = EXCP_ERROR(env, "Can't initialize key"); - goto err; - } - - if (ivec_arg != atom_undefined) { - if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, NULL, ivec_bin.data, -1)) { - *return_term = EXCP_ERROR(env, "Can't initialize iv"); - goto err; + // enif_fprintf(stderr, "%s:%u %T %T %u\r\n", __FILE__, __LINE__, cipher_arg, ivec_arg, ivec_len); + if (ivec_arg == atom_undefined || ivec_len == 0) + { + if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, key_bin.data, NULL, -1)) { + *return_term = EXCP_ERROR(env, "Can't initialize key"); + goto err; + } } - } + else + if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, key_bin.data, ivec_bin.data, -1)) + { + *return_term = EXCP_ERROR(env, "Can't initialize key or iv"); + goto err; + } + + // enif_fprintf(stderr, "%s:%u\r\n", __FILE__, __LINE__); EVP_CIPHER_CTX_set_padding(ctx_res->ctx, 0); -- cgit v1.2.3