diff options
author | Hans Nilsson <[email protected]> | 2019-03-07 12:19:18 +0100 |
---|---|---|
committer | Hans Nilsson <[email protected]> | 2019-03-19 12:45:54 +0100 |
commit | cd08c844693fbb26d545f9cd981bfa9ae2d08042 (patch) | |
tree | c2d1395e95d4ea0710884b9ab9ff61614af2ac18 /lib/crypto/c_src | |
parent | c209e1ecc6aac833b8a1d5da86d86ca04c7d7085 (diff) | |
download | otp-cd08c844693fbb26d545f9cd981bfa9ae2d08042.tar.gz otp-cd08c844693fbb26d545f9cd981bfa9ae2d08042.tar.bz2 otp-cd08c844693fbb26d545f9cd981bfa9ae2d08042.zip |
crypto: Use/implement new funcs for stream-api
Diffstat (limited to 'lib/crypto/c_src')
-rw-r--r-- | lib/crypto/c_src/api_ng.c | 54 | ||||
-rw-r--r-- | lib/crypto/c_src/crypto.c | 11 |
2 files changed, 38 insertions, 27 deletions
diff --git a/lib/crypto/c_src/api_ng.c b/lib/crypto/c_src/api_ng.c index 50c0e9fcf6..1a5867eaaf 100644 --- a/lib/crypto/c_src/api_ng.c +++ b/lib/crypto/c_src/api_ng.c @@ -159,12 +159,6 @@ static int get_init_args(ErlNifEnv* env, goto err; } - if (encflg == -1) - { - *return_term = atom_undefined; - goto err; // Yes... - } - /* Initialize the EVP_CIPHER_CTX */ ctx_res->ctx = EVP_CIPHER_CTX_new(); @@ -198,7 +192,7 @@ static int get_init_args(ErlNifEnv* env, } } - if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, key_bin.data, ivec_bin.data, encflg)) { + if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, key_bin.data, ivec_bin.data, -1)) { *return_term = ERROR_Str(env, "Can't initialize key and/or iv"); goto err; } @@ -254,6 +248,7 @@ static int get_update_args(ErlNifEnv* env, if (enif_get_tuple(env, newstate_and_outdata, &tuple_argc, &tuple_argv) && (tuple_argc == 2)) { /* newstate_and_outdata = {NewState, OutData} */ ctx_res->state = enif_make_copy(ctx_res->env, tuple_argv[0]); + /* Return the OutData (from the newstate_and_outdata tuple) only: */ *return_term = tuple_argv[1]; } } @@ -281,6 +276,7 @@ static int get_update_args(ErlNifEnv* env, } CONSUME_REDS(env, in_data_bin); + /* return the result text as a binary: */ *return_term = enif_make_binary(env, &out_data_bin); } @@ -301,17 +297,43 @@ ERL_NIF_TERM ng_crypto_init_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM arg struct evp_cipher_ctx *ctx_res = NULL; const struct cipher_type_t *cipherp; ERL_NIF_TERM ret; - - if ((ctx_res = enif_alloc_resource(evp_cipher_ctx_rtype, sizeof(struct evp_cipher_ctx))) == NULL) - return ERROR_Str(env, "Can't allocate resource"); - - if (!get_init_args(env, ctx_res, argv[0], argv[1], argv[2], argv[argc-1], - &cipherp, &ret)) - /* Error msg in &ret */ + int encflg; + + if (enif_is_atom(env, argv[0])) { + if ((ctx_res = enif_alloc_resource(evp_cipher_ctx_rtype, sizeof(struct evp_cipher_ctx))) == NULL) + return ERROR_Str(env, "Can't allocate resource"); + + if (!get_init_args(env, ctx_res, argv[0], argv[1], argv[2], argv[argc-1], + &cipherp, &ret)) + /* Error msg in &ret */ + goto ret; + + ret = enif_make_resource(env, ctx_res); + if(ctx_res) enif_release_resource(ctx_res); + + } else if (enif_get_resource(env, argv[0], evp_cipher_ctx_rtype, (void**)&ctx_res)) { + /* Fetch the flag telling if we are going to encrypt (=true) or decrypt (=false) */ + if (argv[3] == atom_true) + encflg = 1; + else if (argv[3] == atom_false) + encflg = 0; + else { + ret = ERROR_Str(env, "Bad enc flag"); + goto ret; + } + if (ctx_res->ctx) { + /* It is *not* a ctx_res for the compatibility handling of non-EVP aes_ctr */ + if (!EVP_CipherInit_ex(ctx_res->ctx, NULL, NULL, NULL, NULL, encflg)) { + ret = ERROR_Str(env, "Can't initialize encflag"); + goto ret; + } + } + ret = argv[0]; + } else { + ret = ERROR_Str(env, "Bad 1:st arg"); goto ret; + } - ret = enif_make_resource(env, ctx_res); - if(ctx_res) enif_release_resource(ctx_res); ret: return ret; } diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c index ac9823887a..501c1ffb42 100644 --- a/lib/crypto/c_src/crypto.c +++ b/lib/crypto/c_src/crypto.c @@ -30,7 +30,6 @@ #include "algorithms.h" #include "api_ng.h" #include "bn.h" -#include "chacha20.h" #include "cipher.h" #include "cmac.h" #include "dh.h" @@ -49,7 +48,6 @@ #include "pkey.h" #include "poly1305.h" #include "rand.h" -#include "rc4.h" #include "rsa.h" #include "srp.h" @@ -80,9 +78,6 @@ static ErlNifFunc nif_funcs[] = { {"cmac_nif", 3, cmac_nif, 0}, {"cipher_info_nif", 1, cipher_info_nif, 0}, {"aes_ige_crypt_nif", 4, aes_ige_crypt_nif, 0}, - {"aes_ctr_stream_init", 2, aes_ctr_stream_init, 0}, - {"aes_ctr_stream_encrypt", 2, aes_ctr_stream_encrypt, 0}, - {"aes_ctr_stream_decrypt", 2, aes_ctr_stream_encrypt, 0}, {"ng_crypto_init_nif", 4, ng_crypto_init_nif, 0}, {"ng_crypto_update_nif", 2, ng_crypto_update_nif, 0}, {"ng_crypto_one_shot_nif", 5, ng_crypto_one_shot_nif, 0}, @@ -91,8 +86,6 @@ static ErlNifFunc nif_funcs[] = { {"rand_uniform_nif", 2, rand_uniform_nif, 0}, {"mod_exp_nif", 4, mod_exp_nif, 0}, {"do_exor", 2, do_exor, 0}, - {"rc4_set_key", 1, rc4_set_key, 0}, - {"rc4_encrypt_with_state", 2, rc4_encrypt_with_state, 0}, {"pkey_sign_nif", 5, pkey_sign_nif, 0}, {"pkey_verify_nif", 6, pkey_verify_nif, 0}, {"pkey_crypt_nif", 6, pkey_crypt_nif, 0}, @@ -114,10 +107,6 @@ static ErlNifFunc nif_funcs[] = { {"aead_encrypt", 6, aead_encrypt, 0}, {"aead_decrypt", 6, aead_decrypt, 0}, - {"chacha20_stream_init", 2, chacha20_stream_init, 0}, - {"chacha20_stream_encrypt", 2, chacha20_stream_crypt, 0}, - {"chacha20_stream_decrypt", 2, chacha20_stream_crypt, 0}, - {"poly1305_nif", 2, poly1305_nif, 0}, {"engine_by_id_nif", 1, engine_by_id_nif, 0}, |