aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src/chacha20.c
diff options
context:
space:
mode:
authorDoug Hogan <[email protected]>2019-01-03 18:09:20 -0800
committerDoug Hogan <[email protected]>2019-01-08 00:08:22 -0800
commitbf063a86501f47cbfa89475aefe5bfacde56a3c9 (patch)
treed7062d6b332f91da75f2d565a0809a7ba946282b /lib/crypto/c_src/chacha20.c
parent5727586180313ec6ea361fdadc2367b35e5b46a5 (diff)
downloadotp-bf063a86501f47cbfa89475aefe5bfacde56a3c9.tar.gz
otp-bf063a86501f47cbfa89475aefe5bfacde56a3c9.tar.bz2
otp-bf063a86501f47cbfa89475aefe5bfacde56a3c9.zip
Revamp chacha20_stream_init()
* Check return values on all OpenSSL calls. * Remove trailing semicolon after function definition.
Diffstat (limited to 'lib/crypto/c_src/chacha20.c')
-rw-r--r--lib/crypto/c_src/chacha20.c44
1 files changed, 31 insertions, 13 deletions
diff --git a/lib/crypto/c_src/chacha20.c b/lib/crypto/c_src/chacha20.c
index 8b21a0c7af..587c0cea53 100644
--- a/lib/crypto/c_src/chacha20.c
+++ b/lib/crypto/c_src/chacha20.c
@@ -25,33 +25,51 @@ ERL_NIF_TERM chacha20_stream_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM a
{/* (Key, IV) */
#if defined(HAVE_CHACHA20)
ErlNifBinary key_bin, ivec_bin;
- struct evp_cipher_ctx *ctx;
+ struct evp_cipher_ctx *ctx = NULL;
const EVP_CIPHER *cipher;
ERL_NIF_TERM ret;
- if (!enif_inspect_iolist_as_binary(env, argv[0], &key_bin)
- || !enif_inspect_binary(env, argv[1], &ivec_bin)
- || key_bin.size != 32
- || ivec_bin.size != 16) {
- return enif_make_badarg(env);
- }
+ if (argc != 2)
+ goto bad_arg;
+ if (!enif_inspect_iolist_as_binary(env, argv[0], &key_bin))
+ goto bad_arg;
+ if (key_bin.size != 32)
+ goto bad_arg;
+ if (!enif_inspect_binary(env, argv[1], &ivec_bin))
+ goto bad_arg;
+ if (ivec_bin.size != 16)
+ goto bad_arg;
cipher = EVP_chacha20();
- ctx = enif_alloc_resource(evp_cipher_ctx_rtype, sizeof(struct evp_cipher_ctx));
- ctx->ctx = EVP_CIPHER_CTX_new();
+ if ((ctx = enif_alloc_resource(evp_cipher_ctx_rtype, sizeof(struct evp_cipher_ctx))) == NULL)
+ goto err;
+ if ((ctx->ctx = EVP_CIPHER_CTX_new()) == NULL)
+ goto err;
+ if (EVP_CipherInit_ex(ctx->ctx, cipher, NULL,
+ key_bin.data, ivec_bin.data, 1) != 1)
+ goto err;
+ if (EVP_CIPHER_CTX_set_padding(ctx->ctx, 0) != 1)
+ goto err;
- EVP_CipherInit_ex(ctx->ctx, cipher, NULL,
- key_bin.data, ivec_bin.data, 1);
- EVP_CIPHER_CTX_set_padding(ctx->ctx, 0);
ret = enif_make_resource(env, ctx);
+ goto done;
+
+ bad_arg:
+ return enif_make_badarg(env);
+
+ err:
+ ret = enif_make_badarg(env);
+
+ done:
enif_release_resource(ctx);
return ret;
+
#else
return enif_raise_exception(env, atom_notsup);
#endif
-};
+}
ERL_NIF_TERM chacha20_stream_crypt(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (State, Data) */