Last active
July 5, 2021 04:24
-
-
Save voluntas/a0d42ddd6c8c4e7806e774ec92258174 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(benchmark). | |
-export([main/0]). | |
%% 1100 バイト中 1000 バイトを暗号化する | |
%% AES-128-CTR + HMAC-SHA1 はお尻に 10 バイトつけるので 1110 バイトになる | |
%% AES-128-GCM はお尻に 16 バイトつけるので 1116 バイトになる | |
%% TODO: IV は固定ではなく SeqNum にすべき | |
%% 1> c(benchmark). | |
%% {ok,benchmark} | |
%% 2> benchmark:main(). | |
%% AES-128-CTR+HMAC-SHA1: 7109782 | |
%% AES-128-GCM : 1632003 | |
%% AES-256-GCM : 1744986 | |
-define(TIMES, 1000000). | |
main() -> | |
<<Header:100/binary, Payload:1000/binary>> = crypto:strong_rand_bytes(1100), | |
F1 = fun() -> | |
Key = crypto:strong_rand_bytes(16), | |
IV = crypto:strong_rand_bytes(16), | |
Salt = crypto:strong_rand_bytes(20), | |
ok = lists:foreach(fun(_) -> | |
EncryptedPacket = encrypt_aes_128_ctr_hmac_sha1(Key, IV, Salt, Header, Payload), | |
{Header, Payload} = decrypt_aes_128_ctr_hmac_sha1(Key, IV, Salt, EncryptedPacket) | |
end, lists:seq(0, ?TIMES)) | |
end, | |
{Time1, _} = timer:tc(F1), | |
F2 = fun() -> | |
Key = crypto:strong_rand_bytes(16), | |
IV = crypto:strong_rand_bytes(12), | |
ok = lists:foreach(fun(_) -> | |
EncryptedPacket = encrypt_aes_128_gcm(Key, IV, Header, Payload), | |
{Header, Payload} = decrypt_aes_128_gcm(Key, IV, EncryptedPacket) | |
end, lists:seq(0, ?TIMES)) | |
end, | |
{Time2, _} = timer:tc(F2), | |
F3 = fun() -> | |
Key = crypto:strong_rand_bytes(32), | |
IV = crypto:strong_rand_bytes(12), | |
ok = lists:foreach(fun(_) -> | |
EncryptedPacket = encrypt_aes_256_gcm(Key, IV, Header, Payload), | |
{Header, Payload} = decrypt_aes_256_gcm(Key, IV, EncryptedPacket) | |
end, lists:seq(0, ?TIMES)) | |
end, | |
{Time3, _} = timer:tc(F3), | |
io:format("~p~n", [crypto:info_lib()]), | |
io:format("AES-128-CTR+HMAC-SHA1: ~p~n" | |
"AES-128-GCM : ~p~n" | |
"AES-256-GCM : ~p~n", | |
[Time1, Time2, Time3]), | |
ok. | |
%% AES-CTR + HMAC-SHA1 | |
encrypt_aes_128_ctr_hmac_sha1(Key, IV, Salt, Header, Payload) -> | |
AuthTag = crypto:macN(hmac, sha, Salt, <<Header/binary, Payload/binary>>, 10), | |
EncryptedPayload = crypto:crypto_one_time(aes_128_ctr, Key, IV, Payload, true), | |
<<Header/binary, EncryptedPayload/binary, AuthTag/binary>>. | |
decrypt_aes_128_ctr_hmac_sha1(Key, IV, Salt, <<Header:100/binary, EncryptedPayload:1000/binary, AuthTag:10/binary>>) -> | |
Payload = crypto:crypto_one_time(aes_128_ctr, Key, IV, EncryptedPayload, false), | |
AuthTag = crypto:macN(hmac, sha, Salt, <<Header:100/binary, Payload:1000/binary>>, 10), | |
{Header, Payload}. | |
%% AES-GCM | |
encrypt_aes_128_gcm(Key, IV, Header, Payload) -> | |
{EncryptedPayload, Tag} = crypto:crypto_one_time_aead(aes_128_gcm, Key, IV, Payload, Header, 16, true), | |
<<Header/binary, EncryptedPayload/binary, Tag/binary>>. | |
decrypt_aes_128_gcm(Key, IV, <<Header:100/binary, EncryptedPayload:1000/binary, Tag:16/binary>>) -> | |
Payload = crypto:crypto_one_time_aead(aes_128_gcm, Key, IV, EncryptedPayload, Header, Tag, false), | |
{Header, Payload}. | |
encrypt_aes_256_gcm(Key, IV, Header, Payload) -> | |
{EncryptedPayload, Tag} = crypto:crypto_one_time_aead(aes_256_gcm, Key, IV, Payload, Header, 16, true), | |
<<Header/binary, EncryptedPayload/binary, Tag/binary>>. | |
decrypt_aes_256_gcm(Key, IV, <<Header:100/binary, EncryptedPayload:1000/binary, Tag:16/binary>>) -> | |
Payload = crypto:crypto_one_time_aead(aes_256_gcm, Key, IV, EncryptedPayload, Header, Tag, false), | |
{Header, Payload}. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MacBookAir6,2 Intel Core i5 1.3 GHz