Packages

otpbp

6.3.2
7.18.0 7.16.1 7.15.0 7.14.0 7.13.0 7.12.0 7.11.0 7.10.2 7.9.3 7.9.2 7.9.1 7.8.7 7.8.6 7.8.5 7.8.4 7.7.1 7.7.0 7.6.3 7.6.2 7.6.1 7.6.0 7.5.1 7.5.0 7.4.0 7.3.4 7.3.3 7.3.2 7.3.1 7.3.0 7.2.0 7.1.1 7.1.0 7.0.4 7.0.3 7.0.2 7.0.1 6.26.0 6.25.0 6.24.0 6.23.0 6.22.2 6.21.3 6.21.2 6.21.1 6.20.2 6.19.1 6.19.0 6.18.3 6.18.2 6.18.1 6.18.0 6.17.1 6.17.0 6.16.0 6.15.4 6.15.3 6.15.2 6.15.1 6.15.0 6.14.0 6.13.5 6.13.4 6.13.3 6.13.2 6.13.1 6.13.0 6.12.0 6.11.2 6.11.1 6.10.0 6.9.2 6.9.1 6.9.0 6.8.0 6.7.1 6.7.0 6.6.4 6.6.3 6.6.2 6.6.1 6.6.0 6.5.0 6.4.0 6.3.3 6.3.2 6.3.1 6.3.0 6.2.1 6.2.0 6.1.0 6.0.0 5.39.0 5.38.0 5.37.0 5.36.0 5.35.2 5.34.3 5.34.2 5.34.1 5.33.1 5.32.1 5.32.0 5.31.3 5.31.2 5.31.1 5.31.0 5.30.1 5.30.0 5.29.0 5.28.4 5.28.3 5.28.2 5.28.1 5.28.0 5.27.0 5.26.5 5.26.4 5.26.3 5.26.2 5.26.1 5.26.0 5.25.0 5.24.2 5.24.1 5.23.0 5.22.2 5.22.1 5.22.0 5.21.0 5.20.1 5.20.0 5.19.4 5.19.3 5.19.2 5.19.1 5.19.0 5.18.0 5.17.0 5.16.3 5.16.1 5.16.0 5.15.1 5.15.0 5.14.0 5.13.1 5.13.0 5.12.1 5.12.0 5.11.2 5.11.1 5.10.1 5.10.0 5.9.0 5.8.0 5.7.0 5.6.1 5.6.0 5.5.0 5.4.2 5.4.1 5.4.0 5.3.0 5.2.2 5.2.1 5.2.0 5.1.2 5.1.1 4.79.0 4.78.0 4.77.0 4.76.2 4.75.3 4.75.2 4.75.1 4.74.0 4.73.1 4.73.0 4.72.3 4.72.2 4.72.1 4.72.0 4.71.1 4.71.0 4.70.0 4.69.4 4.69.3 4.69.2 4.69.1 4.69.0 4.68.0 4.67.5 4.67.4 4.67.3 4.67.2 4.67.0 4.66.0 4.65.2 4.65.1 4.64.0 4.63.2 4.63.1 4.63.0 4.62.0 4.61.1 4.61.0 4.60.4 4.60.3 4.60.2 4.60.1 4.60.0 4.59.0 4.58.0 4.57.2 4.57.1 4.57.0 4.56.2 4.56.1 4.56.0 4.55.0 4.54.0 4.53.0 4.52.1 4.52.0 4.51.3 4.51.2 4.51.0 4.50.0 4.49.3 4.49.2

OTP backports

Current section

Files

Jump to
otpbp src otpbp_crypto.erl
Raw

src/otpbp_crypto.erl

-module(otpbp_crypto).
-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE =:= 23).
-compile({nowarn_deprecated_function, [{crypto, hmac, 3}, {crypto, hmac, 4}]}).
-endif.
-endif.
-ifndef(HAVE_crypto__dss_sign_2).
-export([dss_sign/2]).
-endif.
-ifndef(HAVE_crypto__dss_sign_3).
-export([dss_sign/3]).
-endif.
-ifndef(HAVE_crypto__dss_verify_3).
-export([dss_verify/3]).
-endif.
-ifndef(HAVE_crypto__dss_verify_4).
-export([dss_verify/4]).
-endif.
-ifndef(HAVE_crypto__rsa_sign_2).
-export([rsa_sign/2]).
-endif.
-ifndef(HAVE_crypto__rsa_sign_3).
-export([rsa_sign/3]).
-endif.
-ifndef(HAVE_crypto__rsa_verify_3).
-export([rsa_verify/3]).
-endif.
-ifndef(HAVE_crypto__rsa_verify_4).
-export([rsa_verify/4]).
-endif.
-ifndef(HAVE_crypto__sha224_1).
-export([sha224/1]).
-endif.
-ifndef(HAVE_crypto__sha256_1).
-export([sha256/1]).
-endif.
-ifndef(HAVE_crypto__sha384_1).
-export([sha384/1]).
-endif.
-ifndef(HAVE_crypto__sha512_1).
-export([sha512/1]).
-endif.
-ifndef(HAVE_crypto__sha224_init_0).
-export([sha224_init/0]).
-endif.
-ifndef(HAVE_crypto__sha256_init_0).
-export([sha256_init/0]).
-endif.
-ifndef(HAVE_crypto__sha384_init_0).
-export([sha384_init/0]).
-endif.
-ifndef(HAVE_crypto__sha512_init_0).
-export([sha512_init/0]).
-endif.
-ifndef(HAVE_crypto__sha224_mac_2).
-export([sha224_mac/2]).
-endif.
-ifndef(HAVE_crypto__sha224_mac_3).
-export([sha224_mac/3]).
-endif.
-ifndef(HAVE_crypto__sha256_mac_2).
-export([sha256_mac/2]).
-endif.
-ifndef(HAVE_crypto__sha256_mac_3).
-export([sha256_mac/3]).
-endif.
-ifndef(HAVE_crypto__sha384_mac_2).
-export([sha384_mac/2]).
-endif.
-ifndef(HAVE_crypto__sha384_mac_3).
-export([sha384_mac/3]).
-endif.
-ifndef(HAVE_crypto__sha512_mac_2).
-export([sha512_mac/2]).
-endif.
-ifndef(HAVE_crypto__sha512_mac_3).
-export([sha512_mac/3]).
-endif.
-ifndef(HAVE_crypto__md4_1).
-export([md4/1]).
-endif.
-ifndef(HAVE_crypto__md4_init_0).
-export([md4_init/0]).
-endif.
-ifndef(HAVE_crypto__md5_1).
-export([md5/1]).
-endif.
-ifndef(HAVE_crypto__md5_init_0).
-export([md5_init/0]).
-endif.
-ifndef(HAVE_crypto__sha_1).
-export([sha/1]).
-endif.
-ifndef(HAVE_crypto__sha_init_0).
-export([sha_init/0]).
-endif.
-ifndef(HAVE_crypto__supports_1).
% OTP 22.0
-export([supports/1]).
-endif.
-ifndef(HAVE_crypto__mac_3).
% OTP 22.1
-export([mac/3]).
-endif.
-ifndef(HAVE_crypto__mac_4).
% OTP 22.1
-export([mac/4]).
-endif.
-ifndef(HAVE_crypto__macN_5).
% OTP 22.1
-export([macN/5]).
-endif.
-ifndef(HAVE_crypto__macN_4).
% OTP 22.1
-export([macN/4]).
-endif.
-ifndef(HAVE_crypto__hmac_3).
% OTP < 24
-export([hmac/3]).
-endif.
-ifndef(HAVE_crypto__hmac_4).
% OTP < 24
-export([hmac/4]).
-endif.
-ifndef(HAVE_crypto__hmac_init_2).
% OTP < 24
-export([hmac_init/2]).
-endif.
-ifndef(HAVE_crypto__hmac_update_2).
% OTP < 24
-export([hmac_update/2]).
-endif.
-ifndef(HAVE_crypto__hmac_final_1).
% OTP < 24
-export([hmac_final/1]).
-endif.
-ifndef(HAVE_crypto__hmac_final_n_2).
% OTP < 24
-export([hmac_final_n/2]).
-endif.
-ifndef(HAVE_crypto__cmac_3).
-ifdef(HAVE_crypto__mac_4).
% OTP < 24
-export([cmac/3]).
-endif.
-endif.
-ifndef(HAVE_crypto__cmac_4).
-ifdef(HAVE_crypto__macN_5).
% OTP < 24
-export([cmac/4]).
-endif.
-endif.
-ifndef(HAVE_crypto__poly1305_2).
-ifdef(HAVE_crypto__mac_3).
% OTP < 24
-export([poly1305/2]).
-endif.
-endif.
-ifndef(HAVE_crypto__block_decrypt_3).
% OTP < 24
-export([block_decrypt/3]).
-endif.
-ifndef(HAVE_crypto__block_decrypt_4).
% OTP < 24
-export([block_decrypt/4]).
-endif.
-ifndef(HAVE_crypto__block_encrypt_3).
% OTP < 24
-export([block_encrypt/3]).
-endif.
-ifndef(HAVE_crypto__block_encrypt_4).
% OTP < 24
-export([block_encrypt/4]).
-endif.
-ifndef(HAVE_crypto__stream_init_2).
% OTP < 24
-export([stream_init/2]).
-endif.
-ifndef(HAVE_crypto__stream_init_3).
% OTP < 24
-export([stream_init/3]).
-endif.
-ifndef(HAVE_crypto__stream_decrypt_2).
% OTP < 24
-export([stream_decrypt/2]).
-endif.
-ifndef(HAVE_crypto__stream_encrypt_2).
% OTP < 24
-export([stream_encrypt/2]).
-endif.
-ifndef(HAVE_crypto__hash_equals_2).
% OTP 25.0
-export([hash_equals/2]).
-endif.
-ifndef(HAVE_crypto__mac_3).
-ifdef(HAVE_crypto__mac_4).
-import(crypto, [mac/4]).
-endif.
-endif.
-ifndef(HAVE_crypto__macN_5).
-ifdef(HAVE_crypto__mac_4).
-import(crypto, [mac/4]).
-endif.
-endif.
-ifndef(HAVE_crypto__macN_4).
-ifdef(HAVE_crypto__macN_5).
-import(crypto, [macN/5]).
-endif.
-endif.
-ifdef(HAVE_crypto__hmac_3).
-import(crypto, [hmac/3]).
-endif.
-ifdef(HAVE_crypto__hmac_4).
-import(crypto, [hmac/4]).
-endif.
-ifndef(HAVE_crypto__dss_sign_2).
dss_sign(Data, Key) -> crypto:sign(dss, sha, Data, Key).
-endif.
-ifndef(HAVE_crypto__dss_sign_3).
dss_sign(Type, Data, Key) -> crypto:sign(dss, Type, Data, Key).
-endif.
-ifndef(HAVE_crypto__dss_verify_3).
dss_verify(Data, Signature, Key) -> crypto:verify(dss, sha, Data, Signature, Key).
-endif.
-ifndef(HAVE_crypto__dss_verify_4).
dss_verify(Type, Data, Signature, Key) -> crypto:verify(dss, Type, Data, Signature, Key).
-endif.
-ifndef(HAVE_crypto__rsa_sign_2).
rsa_sign(Data, Key) -> crypto:sign(rsa, sha, Data, Key).
-endif.
-ifndef(HAVE_crypto__rsa_sign_3).
rsa_sign(Type, Data, Key) -> crypto:sign(rsa, Type, Data, Key).
-endif.
-ifndef(HAVE_crypto__rsa_verify_3).
rsa_verify(Data, Signature, Key) -> crypto:verify(rsa, sha, Data, Signature, Key).
-endif.
-ifndef(HAVE_crypto__rsa_verify_4).
rsa_verify(Type, Data, Signature, Key) -> crypto:verify(rsa, Type, Data, Signature, Key).
-endif.
-ifndef(HAVE_crypto__sha224_1).
sha224(Data) -> crypto:hash(sha224, Data).
-endif.
-ifndef(HAVE_crypto__sha256_1).
sha256(Data) -> crypto:hash(sha256, Data).
-endif.
-ifndef(HAVE_crypto__sha384_1).
sha384(Data) -> crypto:hash(sha384, Data).
-endif.
-ifndef(HAVE_crypto__sha512_1).
sha512(Data) -> crypto:hash(sha512, Data).
-endif.
-ifndef(HAVE_crypto__sha224_mac_2).
sha224_mac(Key, Data) -> hmac(sha224, Key, Data).
-endif.
-ifndef(HAVE_crypto__sha224_mac_3).
sha224_mac(Key, Data, Size) -> hmac(sha224, Key, Data, Size).
-endif.
-ifndef(HAVE_crypto__sha256_mac_2).
sha256_mac(Key, Data) -> hmac(sha256, Key, Data).
-endif.
-ifndef(HAVE_crypto__sha256_mac_3).
sha256_mac(Key, Data, Size) -> hmac(sha256, Key, Data, Size).
-endif.
-ifndef(HAVE_crypto__sha384_mac_2).
sha384_mac(Key, Data) -> hmac(sha384, Key, Data).
-endif.
-ifndef(HAVE_crypto__sha384_mac_3).
sha384_mac(Key, Data, Size) -> hmac(sha384, Key, Data, Size).
-endif.
-ifndef(HAVE_crypto__sha512_mac_2).
sha512_mac(Key, Data) -> hmac(sha512, Key, Data).
-endif.
-ifndef(HAVE_crypto__sha512_mac_3).
sha512_mac(Key, Data, Size) -> hmac(sha512, Key, Data, Size).
-endif.
-ifndef(HAVE_crypto__sha224_init_0).
sha224_init() -> crypto:hash_init(sha224).
-endif.
-ifndef(HAVE_crypto__sha256_init_0).
sha256_init() -> crypto:hash_init(sha256).
-endif.
-ifndef(HAVE_crypto__sha384_init_0).
sha384_init() -> crypto:hash_init(sha384).
-endif.
-ifndef(HAVE_crypto__sha512_init_0).
sha512_init() -> crypto:hash_init(sha512).
-endif.
-ifndef(HAVE_crypto__md4_1).
md4(Data) -> crypto:hash(md4, Data).
-endif.
-ifndef(HAVE_crypto__md4_init_0).
md4_init() -> crypto:hash_init(md4).
-endif.
-ifndef(HAVE_crypto__md5_1).
md5(Data) -> crypto:hash(md5, Data).
-endif.
-ifndef(HAVE_crypto__md5_init_0).
md5_init() -> crypto:hash_init(md5).
-endif.
-ifndef(HAVE_crypto__sha_1).
sha(Data) -> crypto:hash(sha, Data).
-endif.
-ifndef(HAVE_crypto__sha_init_0).
sha_init() -> crypto:hash_init(sha).
-endif.
-ifndef(HAVE_crypto__supports_1).
supports(T) when T =:= hashs; T =:= ciphers; T =:= public_keys; T =:= macs; T =:= curves; T =:= rsa_opts ->
case lists:keyfind(T, 1, crypto:supports()) of
{_, Support} -> Support;
_ -> []
end.
-endif.
-ifndef(HAVE_crypto__mac_3).
mac(Type, Key, Data) -> mac(Type, undefined, Key, Data).
-endif.
-ifndef(HAVE_crypto__mac_4).
-ifdef(HAVE_crypto__poly1305_2).
mac(poly1305, T, Key, Data) ->
try
crypto:poly1305(Key, Data)
catch
error:badarg ->
error(case is_binary(Data) orelse is_list(Data) andalso lists:all(fun is_iodata/1, Data) of
true ->
case is_binary(Key) orelse is_list(Key) andalso lists:all(fun is_iodata/1, Key) of
true -> {badarg,{"mac.c",231},"Bad key length"};
_false -> {badarg, {"mac.c", 216}, "Bad key"}
end;
_false -> {badarg, {"mac.c", 179}, "Bad text"}
end,
[poly1305, T, Key, Data]);
C:R -> erlang:C(R, [poly1305, T, Key, Data])
end;
mac(hmac, T, Key, Data) -> mac_hmac(T, Key, Data);
mac(cmac, T, Key, Data) -> mac_cmac(T, Key, Data);
mac(T, SubType, Key, Data) -> mac_unknown(T, SubType, Key, Data).
-else.
mac(hmac, T, Key, Data) -> mac_hmac(T, Key, Data);
mac(cmac, T, Key, Data) -> mac_cmac(T, Key, Data);
mac(T, SubType, Key, Data) -> mac_unknown(T, SubType, Key, Data).
-endif.
is_iodata(E) ->
is_binary(E) orelse try iolist_size(E) of
_ -> true
catch
_:_ -> false
end.
mac_hmac(T, Key, Data) ->
try
crypto:hmac(T, Key, Data)
catch
error:badarg ->
error(case is_iodata(Data) of
true ->
case is_iodata(Key) of
true -> {badarg, {"mac.c", 259}, "Bad digest algorithm for HMAC"};
_false -> {badarg, {"mac.c", 216}, "Bad key"}
end;
_false -> {badarg, {"mac.c", 179}, "Bad text"}
end,
[hmac, T, Key, Data]);
C:R -> erlang:C(R, [hmac, T, Key, Data])
end.
-ifdef(HAVE_crypto__cmac_3).
mac_cmac(T, Key, Data) ->
try
crypto:cmac(T, Key, Data)
catch
error:badarg ->
error(case is_iodata(Data) of
true ->
case is_iodata(Key) of
true ->
case lists:member(T, ciphers()) of
true -> {badarg, {"mac.c", 306}, "Bad key size"};
_false -> {badarg, {"mac.c", 303}, "Unknown cipher"}
end;
_false -> {badarg, {"mac.c", 216}, "Bad key"}
end;
_false -> {badarg, {"mac.c", 179}, "Bad text"}
end,
[cmac, T, Key, Data]);
C:R -> erlang:C(R, [cmac, T, Key, Data])
end.
-compile({inline, ciphers/0}).
ciphers() ->
{_, Ciphers} = lists:keyfind(ciphers, 1, crypto:supports()),
Ciphers.
-else.
mac_cmac(T, Key, Data) -> mac_unknown(cmac, T, Key, Data).
-endif.
mac_unknown(T, SubType, Key, Data) -> error({badarg, {"mac.c", 229}, "Unknown mac algorithm"}, [T, SubType, Key, Data]).
-compile({inline, [mac_hmac/3, mac_cmac/3, mac_unknown/4]}).
-endif.
-ifndef(HAVE_crypto__macN_5).
macN(Type, SubType, Key, Data, MacLength) -> binary:part(mac(Type, SubType, Key, Data), 0, MacLength).
-endif.
-ifndef(HAVE_crypto__macN_4).
macN(Type, Key, Data, MacLength) -> macN(Type, undefined, Key, Data, MacLength).
-endif.
-ifndef(HAVE_crypto__hmac_3).
hmac(Type, Key, Data) ->
try
crypto:mac(hmac, Type, Key, Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__hmac_4).
hmac(Type, Key, Data, MacLength) ->
try
crypto:macN(hmac, Type, Key, Data, MacLength)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__hmac_init_2).
hmac_init(Type, Key) ->
try
crypto:mac_init(hmac, Type, Key)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__hmac_update_2).
hmac_update(State, HashLen) ->
try
crypto:mac_update(State, HashLen)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__hmac_final_1).
hmac_final(Context) ->
try
crypto:mac_final(Context)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__hmac_final_n_2).
hmac_final_n(Context, HashLen) ->
try
crypto:mac_finalN(Context, HashLen)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__cmac_3).
-ifdef(HAVE_crypto__mac_4).
cmac(Type, Key, Data) ->
Alias = alias(Type),
try
crypto:mac(cmac, Alias, Key, Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_1).
-define(NEED_ALIAS_1, true).
-endif.
-endif.
-endif.
-ifndef(HAVE_crypto__cmac_4).
-ifdef(HAVE_crypto__macN_5).
cmac(Type, Key, Data, MacLength) ->
Alias = alias(Type),
try
crypto:macN(cmac, Alias, Key, Data, MacLength)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_1).
-define(NEED_ALIAS_1, true).
-endif.
-endif.
-endif.
-ifndef(HAVE_crypto__poly1305_2).
-ifdef(HAVE_crypto__mac_3).
poly1305(Key, Data) ->
try
crypto:mac(poly1305, Key, Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-endif.
-ifdef(NEED_ALIAS_1).
-compile({no_auto_import, [alias/1]}).
alias(aes_cbc128) -> aes_128_cbc;
alias(aes_cbc256) -> aes_256_cbc;
alias(A) when A =:= des3_cbc; A =:= des_ede3 -> des_ede3_cbc;
alias(A) when A =:= des_ede3_cbf; A =:= des3_cbf; A =:= des3_cfb -> des_ede3_cfb;
alias(A) -> A.
-endif.
-ifndef(HAVE_crypto__hash_equals_2).
hash_equals(A, B) when is_binary(A), is_binary(B) -> A =:= B;
hash_equals(A, B) -> error(badarg, [A, B]).
-endif.
-ifndef(HAVE_crypto__block_decrypt_4).
block_decrypt(aes_ige256, _Key, _IV, _Data) -> error(notsup);
block_decrypt(Type, Key, IV, {AAD, Data, TagLength}) ->
try
crypto:crypto_one_time_aead(alias(Type, Key), Key, IV, Data, AAD, TagLength, false)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end;
block_decrypt(Type, Key, IV, Data) ->
try
crypto:crypto_update(crypto:crypto_init(alias(Type, Key), Key, IV, false), Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_2).
-define(NEED_ALIAS_2, true).
-endif.
-endif.
-ifndef(HAVE_crypto__block_decrypt_3).
-ifdef(HAVE_crypto__block_decrypt_4).
block_decrypt(Type, Key, Data) -> crypto:block_decrypt(Type, Key, <<>>, Data).
-else.
block_decrypt(Type, Key, Data) ->
try
crypto:crypto_update(crypto:crypto_init(alias(Type, Key), Key, false), Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_2).
-define(NEED_ALIAS_2, true).
-endif.
-endif.
-endif.
-ifndef(HAVE_crypto__block_encrypt_4).
block_encrypt(aes_ige256, _Key, _IV, _Data) -> error(notsup);
block_encrypt(Type, Key, IV, {AAD, Data, TagLength}) ->
try
crypto:crypto_one_time_aead(alias(Type, Key), Key, IV, Data, AAD, TagLength, true)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end;
block_encrypt(Type, Key, IV, {AAD, Data}) ->
try
crypto:crypto_one_time_aead(alias(Type, Key), Key, IV, Data, AAD, true)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end;
block_encrypt(Type, Key, IV, Data) ->
try
crypto:crypto_update(crypto:crypto_init(alias(Type, Key), Key, IV, true), Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_2).
-define(NEED_ALIAS_2, true).
-endif.
-endif.
-ifndef(HAVE_crypto__block_encrypt_3).
-ifdef(HAVE_crypto__block_encrypt_4).
block_encrypt(Type, Key, Data) -> crypto:block_encrypt(Type, Key, <<>>, Data).
-else.
block_encrypt(Type, Key, Data) ->
try
crypto:crypto_update(crypto:crypto_init(alias(Type, Key), Key, true), Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_2).
-define(NEED_ALIAS_2, true).
-endif.
-endif.
-endif.
-ifndef(HAVE_crypto__stream_init_2).
stream_init(rc4, Key) ->
try crypto:crypto_init(rc4, Key, [{encrypt, undefined}]) of
Ref -> {rc4, {Ref, flg_undefined}}
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__stream_init_3).
stream_init(Type, Key, IVec) when is_binary(IVec) ->
Cypher = alias(Type, Key),
try crypto:crypto_init(Cypher, Key, IVec, [{encrypt, undefined}]) of
Ref -> {Cypher, {Ref, flg_undefined}}
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-ifndef(NEED_ALIAS_2).
-define(NEED_ALIAS_2, true).
-endif.
-endif.
-ifndef(HAVE_crypto__stream_decrypt_2).
stream_decrypt({Cipher, {Ref, flg_undefined}}, Data) ->
try crypto:crypto_init(Cipher, <<>>, [{encrypt, false}]) of
Ref -> stream_decrypt({Cipher, Ref}, Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end;
stream_decrypt({_Cipher, Ref} = State, Data) ->
try crypto:crypto_update(Ref, Data) of
Text -> {State, Text}
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifndef(HAVE_crypto__stream_encrypt_2).
stream_encrypt({Cipher, {Ref0, flg_undefined}}, Data) ->
try crypto:crypto_init(Ref0, <<>>, [{encrypt, true}]) of
Ref -> stream_encrypt({Cipher, Ref}, Data)
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end;
stream_encrypt({_Cipher, Ref} = State, Data) ->
try crypto:crypto_update(Ref, Data) of
Text -> {State, Text}
catch
error:{error, {_File, _Line}, _Reason} -> error(badarg);
error:{E, {_File, _Line}, _Reason} when E =:= notsup; E =:= badarg -> error(E)
end.
-endif.
-ifdef(NEED_ALIAS_2).
alias(aes_cbc128, _) -> aes_128_cbc;
alias(aes_cbc256, _) -> aes_256_cbc;
alias(aes_cbc, Key) ->
case iolist_size(Key) of
16 -> aes_128_cbc;
24 -> aes_192_cbc;
32 -> aes_256_cbc
end;
alias(aes_cfb8, Key) ->
case iolist_size(Key) of
16 -> aes_128_cfb8;
24 -> aes_192_cfb8;
32 -> aes_256_cfb8
end;
alias(aes_cfb128, Key) ->
case iolist_size(Key) of
16 -> aes_128_cfb128;
24 -> aes_192_cfb128;
32 -> aes_256_cfb128
end;
alias(aes_ctr, Key) ->
case iolist_size(Key) of
16 -> aes_128_ctr;
24 -> aes_192_ctr;
32 -> aes_256_ctr
end;
alias(aes_ebc, Key) ->
case iolist_size(Key) of
16 -> aes_128_ebc;
24 -> aes_192_ebc;
32 -> aes_256_ebc
end;
alias(aes_gcm, Key) ->
case iolist_size(Key) of
16 -> aes_128_gcm;
24 -> aes_192_gcm;
32 -> aes_256_gcm
end;
alias(aes_ccm, Key) ->
case iolist_size(Key) of
16 -> aes_128_ccm;
24 -> aes_192_ccm;
32 -> aes_256_ccm
end;
alias(A, _) when A =:= des3_cbc; A =:= des_ede3 -> des_ede3_cbc;
alias(A, _) when A =:= des_ede3_cbf; A =:= des3_cbf; A =:= des3_cfb -> des_ede3_cfb;
alias(A, _) -> A.
-endif.