Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active July 5, 2021 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voluntas/a0d42ddd6c8c4e7806e774ec92258174 to your computer and use it in GitHub Desktop.
Save voluntas/a0d42ddd6c8c4e7806e774ec92258174 to your computer and use it in GitHub Desktop.
-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}.
@voluntas
Copy link
Author

voluntas commented Jul 3, 2021

Apple M1

  • 24.0.1
  • OpenSSL 1.1.1K
Erlang/OTP 24 [erts-12.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.0.1  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
AES-128-CTR+HMAC-SHA1: 7372333
AES-128-GCM          : 1717037
AES-256-GCM          : 1828138

@miosakuma
Copy link

miosakuma commented Jul 3, 2021

MBP 2015 2.7GHz

Eshell V12.0.3  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
AES-128-CTR+HMAC-SHA1: 29807195
AES-128-GCM          : 6372427
AES-256-GCM          : 6912702

@voluntas
Copy link
Author

voluntas commented Jul 3, 2021

AWS c6g.8xlarge

Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:32:32] [ds:32:32:10] [async-threads:1]

Eshell V12.0.3  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488319,<<"OpenSSL 1.1.1k  25 Mar 2021">>}]
AES-128-CTR+HMAC-SHA1: 12722512
AES-128-GCM          : 3921436
AES-256-GCM          : 4179435
ok

@miosakuma
Copy link

XPS 13 7390 Core i7-10710U 1.10GHz

Eshell V12.0  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488207,<<"OpenSSL 1.1.1d  10 Sep 2019">>}]
AES-128-CTR+HMAC-SHA1: 13994700
AES-128-GCM          : 3367526
AES-256-GCM          : 3461120

@voluntas
Copy link
Author

voluntas commented Jul 3, 2021

AWS c5a.8xlarge

Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:32:32] [ds:32:32:10] [async-threads:1] [jit]

Eshell V12.0.3  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488319,<<"OpenSSL 1.1.1k  25 Mar 2021">>}]
AES-128-CTR+HMAC-SHA1: 10328336
AES-128-GCM          : 3087603
AES-256-GCM          : 3181392

@torikizi
Copy link

torikizi commented Jul 3, 2021

OMEN by HP 30L ( Core™ i7-10700KF 3.80GHz)

Eshell V12.0  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488207,<<"OpenSSL 1.1.1d  10 Sep 2019">>}]
AES-128-CTR+HMAC-SHA1: 12306841
AES-128-GCM          : 2972364
AES-256-GCM          : 3106611
ok

@voluntas
Copy link
Author

voluntas commented Jul 3, 2021

AWS c5n.9xlarge

Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:36:36] [ds:36:36:10] [async-threads:1] [jit]

Eshell V12.0.3  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488319,<<"OpenSSL 1.1.1k  25 Mar 2021">>}]
AES-128-CTR+HMAC-SHA1: 11140564
AES-128-GCM          : 2831308
AES-256-GCM          : 2987265

@voluntas
Copy link
Author

voluntas commented Jul 3, 2021

MacBook Pro 2020

Erlang/OTP 24 [erts-12.0.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Eshell V12.0.2  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488319,<<"OpenSSL 1.1.1k  25 Mar 2021">>}]
AES-128-CTR+HMAC-SHA1: 14558505
AES-128-GCM          : 3375484
AES-256-GCM          : 3468492

@jj1bdx
Copy link

jj1bdx commented Jul 3, 2021

Intel(R) Client Systems NUC10i7FNH/NUC10i7FNB

Intel(R) Core(TM) i7-10710U CPU
Erlang/OTP 24.0.3 with JIT

Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit] [sharing-preserving]

Eshell V12.0.3  (abort with ^G)
1> l(benchmark).
{module,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488319,<<"OpenSSL 1.1.1k  25 Mar 2021">>}]
AES-128-CTR+HMAC-SHA1: 9296391
AES-128-GCM          : 2339944
AES-256-GCM          : 2474784

@k1complete
Copy link

k1complete commented Jul 4, 2021

MacBookAir6,2 Intel Core i5 1.3 GHz

Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Eshell V12.0  (abort with ^G)
1> c(benchmark).
{ok,benchmark}
2> benchmark:main().
[{<<"OpenSSL">>,269488175,<<"OpenSSL 1.1.1b  26 Feb 2019">>}]
AES-128-CTR+HMAC-SHA1: 32328672
AES-128-GCM          : 6354221
AES-256-GCM          : 6911667

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment