Skip to content

Instantly share code, notes, and snippets.

@mururu
Created November 10, 2015 04:45
Show Gist options
  • Save mururu/d665a27ca8c0c984adaa to your computer and use it in GitHub Desktop.
Save mururu/d665a27ca8c0c984adaa to your computer and use it in GitHub Desktop.
Micro benchmark of EVP for AES-CTR in Erlang/OTP
-module(bench3).
-export([run/0, encrypt/0, decrypt/0]).
-define(KEY, binary:copy(<<0>>, 16)).
-define(IV, binary:copy(<<0>>, 16)).
-define(PLAIN_TEXT, binary:copy(<<"1234567890">>, 140)).
-define(TIME, 1.0e6).
run() ->
C0 = encrypt(),
C1 = decrypt(),
io:format("encrypt: ~p times/sec~ndecrypt: ~p times/sec~n", [C0, C1]).
encrypt() ->
encrypt0(?KEY, ?IV, ?PLAIN_TEXT).
encrypt0(Key, Ivec, PlainText) ->
encrypt1(0, erlang:timestamp(), Key, Ivec, PlainText).
encrypt1(C, TS, Key, Ivec, PlainText) ->
case timer:now_diff(erlang:timestamp(), TS) of
N when (N > ?TIME) ->
C;
_ ->
crypto:stream_encrypt(crypto:stream_init(aes_ctr, Key, Ivec), PlainText),
encrypt1(C+1, TS, Key, Ivec, PlainText)
end.
decrypt() ->
{_State, CipherText} = crypto:stream_encrypt(crypto:stream_init(aes_ctr, ?KEY, ?IV), ?PLAIN_TEXT),
% same with encryption
encrypt0(?KEY, ?IV, CipherText).

no-EVP

1> c(bench3).
{ok,bench3}
2> bench3:run().
encrypt: 65230 times/sec
decrypt: 67715 times/sec
ok

EVP

1> c(bench3).
{ok,bench3}
2> bench3:run().
encrypt: 273026 times/sec
decrypt: 272186 times/sec
ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment