aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/src
diff options
context:
space:
mode:
authorRaimo Niskanen <[email protected]>2017-09-28 14:48:44 +0200
committerRaimo Niskanen <[email protected]>2017-09-28 17:01:14 +0200
commit7710c0d681a4b5f17253945dde0726de0e27cdcf (patch)
tree9904a7c8fd58dd961a352cc3240f48e0ec4a0650 /lib/crypto/src
parent29222f06f38e321e5a2ac8dae67ced92b6544bde (diff)
downloadotp-7710c0d681a4b5f17253945dde0726de0e27cdcf.tar.gz
otp-7710c0d681a4b5f17253945dde0726de0e27cdcf.tar.bz2
otp-7710c0d681a4b5f17253945dde0726de0e27cdcf.zip
Make cache size configurable
Diffstat (limited to 'lib/crypto/src')
-rw-r--r--lib/crypto/src/crypto.app.src2
-rw-r--r--lib/crypto/src/crypto.erl38
2 files changed, 30 insertions, 10 deletions
diff --git a/lib/crypto/src/crypto.app.src b/lib/crypto/src/crypto.app.src
index 1d3f35e465..492aa10e51 100644
--- a/lib/crypto/src/crypto.app.src
+++ b/lib/crypto/src/crypto.app.src
@@ -24,7 +24,7 @@
crypto_ec_curves]},
{registered, []},
{applications, [kernel, stdlib]},
- {env, [{fips_mode, false}]},
+ {env, [{fips_mode, false}, {rand_cache_size, 896}]},
{runtime_dependencies, ["erts-9.0","stdlib-3.4","kernel-5.3"]}]}.
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index 69c98c651f..69ca884471 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -297,11 +297,17 @@ stream_decrypt(State, Data0) ->
%%
%% RAND - pseudo random numbers using RN_ and BN_ functions in crypto lib
%%
+-type rand_cache_seed() ::
+ nonempty_improper_list(non_neg_integer(), binary()).
-spec strong_rand_bytes(non_neg_integer()) -> binary().
-spec rand_seed() -> rand:state().
-spec rand_seed_s() -> rand:state().
--spec rand_seed_alg(Alg :: atom()) -> rand:state().
--spec rand_seed_alg_s(Alg :: atom()) -> rand:state().
+-spec rand_seed_alg(Alg :: atom()) ->
+ {rand:alg_handler(),
+ atom() | rand_cache_seed()}.
+-spec rand_seed_alg_s(Alg :: atom()) ->
+ {rand:alg_handler(),
+ atom() | rand_cache_seed()}.
-spec rand_uniform(crypto_integer(), crypto_integer()) ->
crypto_integer().
@@ -319,10 +325,12 @@ rand_seed() ->
rand_seed_s() ->
rand_seed_alg_s(?MODULE).
+-dialyzer({no_improper_lists, rand_seed_alg/1}).
rand_seed_alg(Alg) ->
rand:seed(rand_seed_alg_s(Alg)).
-
+
-define(CRYPTO_CACHE_BITS, 56).
+-dialyzer({no_improper_lists, rand_seed_alg_s/1}).
rand_seed_alg_s(?MODULE) ->
{#{ type => ?MODULE,
bits => 64,
@@ -331,10 +339,22 @@ rand_seed_alg_s(?MODULE) ->
uniform_n => fun ?MODULE:rand_plugin_uniform/2},
no_seed};
rand_seed_alg_s(crypto_cache) ->
+ EnvCacheSize =
+ application:get_env(
+ crypto, rand_cache_size,
+ ?CRYPTO_CACHE_BITS * 16), % Cache 16 * 8 words
+ Bytes = (?CRYPTO_CACHE_BITS + 7) div 8,
+ CacheSize =
+ case ((EnvCacheSize + (Bytes - 1)) div Bytes) * Bytes of
+ Sz when is_integer(Sz), Bytes =< Sz ->
+ Sz;
+ _ ->
+ Bytes
+ end,
{#{ type => crypto_cache,
bits => ?CRYPTO_CACHE_BITS,
next => fun ?MODULE:rand_cache_plugin_next/1},
- <<>>}.
+ [CacheSize|<<>>]}.
rand_plugin_next(Seed) ->
{bytes_to_integer(strong_rand_range(1 bsl 64)), Seed}.
@@ -345,11 +365,11 @@ rand_plugin_uniform(State) ->
rand_plugin_uniform(Max, State) ->
{bytes_to_integer(strong_rand_range(Max)) + 1, State}.
-rand_cache_plugin_next(<<>>) ->
- rand_cache_plugin_next(
- strong_rand_bytes(?CRYPTO_CACHE_BITS * 16)); % Cache 16 * 8 words
-rand_cache_plugin_next(<<I:?CRYPTO_CACHE_BITS,Cache/binary>>) ->
- {I, Cache}.
+-dialyzer({no_improper_lists, rand_cache_plugin_next/1}).
+rand_cache_plugin_next([CacheSize|<<>>]) ->
+ rand_cache_plugin_next([CacheSize|strong_rand_bytes(CacheSize)]);
+rand_cache_plugin_next([CacheSize|<<I:?CRYPTO_CACHE_BITS,Cache/binary>>]) ->
+ {I, [CacheSize|Cache]}.
strong_rand_range(Range) when is_integer(Range), Range > 0 ->
BinRange = int_to_bin(Range),