diff options
author | Hans Nilsson <hans@erlang.org> | 2021-09-22 13:07:45 +0200 |
---|---|---|
committer | Hans Nilsson <hans@erlang.org> | 2021-10-06 10:27:09 +0200 |
commit | 951e07fc8e3418c926f1d1ae31188fe8bd9a324e (patch) | |
tree | dd1a65d43bc2b94994150cd815641176fcd45048 | |
parent | bb05db8656f73e6b7edddc0e746dda4e339b1439 (diff) | |
download | erlang-951e07fc8e3418c926f1d1ae31188fe8bd9a324e.tar.gz |
crypto: Remove iolist_to binary
It must be faster to do it in the nif instead of an extra bif call
on the erlang level.
-rw-r--r-- | lib/crypto/c_src/api_ng.c | 6 | ||||
-rw-r--r-- | lib/crypto/src/crypto.erl | 41 |
2 files changed, 23 insertions, 24 deletions
diff --git a/lib/crypto/c_src/api_ng.c b/lib/crypto/c_src/api_ng.c index d4f819c8d1..8492ce1e5b 100644 --- a/lib/crypto/c_src/api_ng.c +++ b/lib/crypto/c_src/api_ng.c @@ -340,7 +340,7 @@ static int get_update_args(ErlNifEnv* env, ErlNifBinary in_data_bin, out_data_bin; int out_len, block_size; - if (!enif_inspect_binary(env, argv[indata_arg_num], &in_data_bin) ) + if (!enif_inspect_iolist_as_binary(env, argv[indata_arg_num], &in_data_bin) ) { *return_term = EXCP_BADARG_N(env, indata_arg_num, "Expected binary"); goto err0; @@ -715,7 +715,7 @@ ERL_NIF_TERM ng_crypto_update_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM a {/* (Context, Data [, IV]) */ ErlNifBinary data_bin; - if (!enif_inspect_binary(env, argv[1], &data_bin)) + if (!enif_inspect_iolist_as_binary(env, argv[1], &data_bin)) return EXCP_BADARG_N(env, 1, "expected binary"); if (data_bin.size > INT_MAX) @@ -821,7 +821,7 @@ ERL_NIF_TERM ng_crypto_one_time_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM */ ErlNifBinary data_bin; - if (!enif_inspect_binary(env, argv[3], &data_bin)) + if (!enif_inspect_iolist_as_binary(env, argv[3], &data_bin)) return EXCP_BADARG_N(env, 3, "expected binary as data"); if (data_bin.size > INT_MAX) diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index b7f3acb57c..9e83d50616 100644 --- a/lib/crypto/src/crypto.erl +++ b/lib/crypto/src/crypto.erl @@ -800,8 +800,7 @@ cipher_info(Type) -> Key :: iodata(), FlagOrOptions :: crypto_opts() | boolean(), State :: crypto_state() . -crypto_init(Cipher, Key0, FlagOrOptions0) -> - Key = iolist_to_binary(Key0), +crypto_init(Cipher, Key, FlagOrOptions0) -> FlagOrOptions = get_crypto_opts(FlagOrOptions0), ?nif_call(ng_crypto_init_nif(alias(Cipher,Key), Key, <<>>, FlagOrOptions), {1,2,-1,3}). @@ -814,8 +813,8 @@ crypto_init(Cipher, Key0, FlagOrOptions0) -> State :: crypto_state() . crypto_init(Cipher, Key, IV, FlagOrOptions) -> ?nif_call(ng_crypto_init_nif(alias(Cipher,Key), - iolist_to_binary(Key), - iolist_to_binary(IV), + Key, + IV, get_crypto_opts(FlagOrOptions))). %%%---------------------------------------------------------------- @@ -858,7 +857,7 @@ crypto_dyn_iv_init(Cipher, Key, FlagOrOptions) -> %% The IV is supposed to be supplied by calling crypto_update/3 ?nif_call( ng_crypto_init_nif(alias(Cipher,Key), - iolist_to_binary(Key), + Key, undefined, get_crypto_opts(FlagOrOptions)), {1,2,-1,3} @@ -877,7 +876,7 @@ crypto_dyn_iv_init(Cipher, Key, FlagOrOptions) -> Data :: iodata(), Result :: binary() . crypto_update(State, Data) -> - ?nif_call(ng_crypto_update_nif(State, iolist_to_binary(Data))). + ?nif_call(ng_crypto_update_nif(State, Data)). %%%---------------------------------------------------------------- -spec crypto_dyn_iv_update(State, Data, IV) -> Result | descriptive_error() @@ -886,7 +885,7 @@ crypto_update(State, Data) -> IV :: iodata(), Result :: binary() . crypto_dyn_iv_update(State, Data, IV) -> - ?nif_call(ng_crypto_update_nif(State, iolist_to_binary(Data), iolist_to_binary(IV))). + ?nif_call(ng_crypto_update_nif(State, Data, IV)). %%%---------------------------------------------------------------- %%% @@ -926,9 +925,9 @@ crypto_get_data(State) -> crypto_one_time(Cipher, Key, Data, FlagOrOptions) -> ?nif_call(ng_crypto_one_time_nif(alias(Cipher,Key), - iolist_to_binary(Key), + Key, <<>>, - iolist_to_binary(Data), + Data, get_crypto_opts(FlagOrOptions)), [Cipher, Key, Data, FlagOrOptions], {1,2,-1,3,4}). @@ -945,9 +944,9 @@ crypto_one_time(Cipher, Key, Data, FlagOrOptions) -> crypto_one_time(Cipher, Key, IV, Data, FlagOrOptions) -> ?nif_call(ng_crypto_one_time_nif(alias(Cipher,Key), - iolist_to_binary(Key), - iolist_to_binary(IV), - iolist_to_binary(Data), + Key, + IV, + Data, get_crypto_opts(FlagOrOptions))). %%%---------------------------------------------------------------- @@ -964,7 +963,7 @@ crypto_one_time(Cipher, Key, IV, Data, FlagOrOptions) -> OutTag :: binary(). crypto_one_time_aead(Cipher, Key, IV, PlainText, AAD, true) -> - ?nif_call(aead_cipher_nif(alias(Cipher,Key), iolist_to_binary(Key), IV, + ?nif_call(aead_cipher_nif(alias(Cipher,Key), Key, IV, PlainText, AAD, aead_tag_len(Cipher), true), {1,2,3,4,5,-1,6}). @@ -988,7 +987,7 @@ crypto_one_time_aead(Cipher, Key, IV, PlainText, AAD, true) -> OutPlainText :: binary(). crypto_one_time_aead(Cipher, Key, IV, TextIn, AAD, TagOrTagLength, EncFlg) -> - ?nif_call(aead_cipher_nif(alias(Cipher,Key), iolist_to_binary(Key), IV, + ?nif_call(aead_cipher_nif(alias(Cipher,Key), Key, IV, TextIn, AAD, TagOrTagLength, EncFlg), [Cipher, Key, IV, TextIn, AAD, TagOrTagLength, EncFlg], {}). @@ -1028,13 +1027,13 @@ cipher_info_nif(_Type) -> ?nif_stub. %%% Cipher aliases %%% -alias(aes_cbc, Key) -> alias1(aes_cbc, size(Key)); -alias(aes_cfb8, Key) -> alias1(aes_cfb8, size(Key)); -alias(aes_cfb128, Key) -> alias1(aes_cfb128, size(Key)); -alias(aes_ctr, Key) -> alias1(aes_ctr, size(Key)); -alias(aes_ecb, Key) -> alias1(aes_ecb, size(Key)); -alias(aes_gcm, Key) -> alias1(aes_gcm, size(Key)); -alias(aes_ccm, Key) -> alias1(aes_ccm, size(Key)); +alias(aes_cbc, Key) -> alias1(aes_cbc, iolist_size(Key)); +alias(aes_cfb8, Key) -> alias1(aes_cfb8, iolist_size(Key)); +alias(aes_cfb128, Key) -> alias1(aes_cfb128, iolist_size(Key)); +alias(aes_ctr, Key) -> alias1(aes_ctr, iolist_size(Key)); +alias(aes_ecb, Key) -> alias1(aes_ecb, iolist_size(Key)); +alias(aes_gcm, Key) -> alias1(aes_gcm, iolist_size(Key)); +alias(aes_ccm, Key) -> alias1(aes_ccm, iolist_size(Key)); alias(Alg, _) -> Alg. |