diff options
author | Raimo Niskanen <[email protected]> | 2017-10-02 16:32:14 +0200 |
---|---|---|
committer | Raimo Niskanen <[email protected]> | 2017-10-02 16:32:14 +0200 |
commit | a89405725c623d489643c2bd4a07ce626dccdcc3 (patch) | |
tree | 50008bcaa7e7ac6e70b673bd6ba9184845d2598f /lib/crypto | |
parent | 7710c0d681a4b5f17253945dde0726de0e27cdcf (diff) | |
download | otp-a89405725c623d489643c2bd4a07ce626dccdcc3.tar.gz otp-a89405725c623d489643c2bd4a07ce626dccdcc3.tar.bz2 otp-a89405725c623d489643c2bd4a07ce626dccdcc3.zip |
Future proof cache word size
Diffstat (limited to 'lib/crypto')
-rw-r--r-- | lib/crypto/src/crypto.erl | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index 69ca884471..2ca5e4db9e 100644 --- a/lib/crypto/src/crypto.erl +++ b/lib/crypto/src/crypto.erl @@ -325,12 +325,10 @@ 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, @@ -339,11 +337,11 @@ rand_seed_alg_s(?MODULE) -> uniform_n => fun ?MODULE:rand_plugin_uniform/2}, no_seed}; rand_seed_alg_s(crypto_cache) -> + CacheBits = ?CRYPTO_CACHE_BITS, EnvCacheSize = application:get_env( - crypto, rand_cache_size, - ?CRYPTO_CACHE_BITS * 16), % Cache 16 * 8 words - Bytes = (?CRYPTO_CACHE_BITS + 7) div 8, + crypto, rand_cache_size, CacheBits * 16), % Cache 16 * 8 words + Bytes = (CacheBits + 7) div 8, CacheSize = case ((EnvCacheSize + (Bytes - 1)) div Bytes) * Bytes of Sz when is_integer(Sz), Bytes =< Sz -> @@ -352,9 +350,9 @@ rand_seed_alg_s(crypto_cache) -> Bytes end, {#{ type => crypto_cache, - bits => ?CRYPTO_CACHE_BITS, + bits => CacheBits, next => fun ?MODULE:rand_cache_plugin_next/1}, - [CacheSize|<<>>]}. + {CacheBits, CacheSize, <<>>}}. rand_plugin_next(Seed) -> {bytes_to_integer(strong_rand_range(1 bsl 64)), Seed}. @@ -365,11 +363,12 @@ rand_plugin_uniform(State) -> rand_plugin_uniform(Max, State) -> {bytes_to_integer(strong_rand_range(Max)) + 1, State}. --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]}. +rand_cache_plugin_next({CacheBits, CacheSize, <<>>}) -> + rand_cache_plugin_next( + {CacheBits, CacheSize, strong_rand_bytes(CacheSize)}); +rand_cache_plugin_next({CacheBits, CacheSize, Cache}) -> + <<I:CacheBits, NewCache/binary>> = Cache, + {I, {CacheBits, CacheSize, NewCache}}. strong_rand_range(Range) when is_integer(Range), Range > 0 -> BinRange = int_to_bin(Range), |