Implement missing random functions* enacl:randombytes_int32/0* enacl:randombytes_uniform/1
This commit is contained in:
+19
-2
@@ -114,7 +114,7 @@
|
||||
hash/1,
|
||||
verify_16/2,
|
||||
verify_32/2,
|
||||
|
||||
|
||||
%% No Tests!
|
||||
unsafe_memzero/1
|
||||
]).
|
||||
@@ -122,7 +122,9 @@
|
||||
%% Randomness
|
||||
-export([
|
||||
%% EQC
|
||||
randombytes/1
|
||||
randombytes/1,
|
||||
randombytes_int32/0,
|
||||
randombytes_uniform/1
|
||||
]).
|
||||
|
||||
%%% Specific primitives
|
||||
@@ -204,6 +206,9 @@
|
||||
-define(CRYPTO_GENERICHASH_KEYBYTES_MAX, 64).
|
||||
-define(CRYPTO_GENERICHASH_KEYBYTES, 32).
|
||||
|
||||
%% Size limits
|
||||
-define(MAX_32BIT_INT, 1 bsl 32).
|
||||
|
||||
%% @doc Verify makes sure the constants defined in libsodium matches ours
|
||||
verify() ->
|
||||
true = equals(binary:copy(<<0>>, enacl_nif:crypto_box_ZEROBYTES()), ?P_ZEROBYTES),
|
||||
@@ -1119,6 +1124,18 @@ aead_chacha20poly1305_MESSAGEBYTES_MAX() ->
|
||||
randombytes(N) ->
|
||||
enacl_nif:randombytes(N).
|
||||
|
||||
%% @doc randombytes_int32/0 produces an integer in the 32bit range
|
||||
%% @end
|
||||
-spec randombytes_int32() -> integer().
|
||||
randombytes_int32() ->
|
||||
enacl_nif:randombytes_int32().
|
||||
|
||||
%% @doc randombytes_uniform/1 produces a random integer in the space [0..N)
|
||||
%% That is with the upper bound excluded. Fails for integers above 32bit size
|
||||
%% @end
|
||||
randombytes_uniform(N) when N < ?MAX_32BIT_INT ->
|
||||
enacl_nif:randombytes_uniform(N).
|
||||
|
||||
%% Helpers
|
||||
|
||||
%% @doc bump/4 bumps a reduction budget linearly before returning the result
|
||||
|
||||
+5
-1
@@ -153,7 +153,9 @@
|
||||
|
||||
%% Access to the RNG
|
||||
-export([
|
||||
randombytes/1
|
||||
randombytes/1,
|
||||
randombytes_int32/0,
|
||||
randombytes_uniform/1
|
||||
]).
|
||||
|
||||
%% Undocumented features :>
|
||||
@@ -296,5 +298,7 @@ crypto_kx_PUBLICKEYBYTES() -> erlang:nif_error(nif_not_loaded).
|
||||
crypto_kx_SECRETKEYBYTES() -> erlang:nif_error(nif_not_loaded).
|
||||
|
||||
randombytes(_RequestedSize) -> erlang:nif_error(nif_not_loaded).
|
||||
randombytes_int32() -> erlang:nif_error(nif_not_loaded).
|
||||
randombytes_uniform(_UpperBound) -> erlang:nif_error(nif_not_loaded).
|
||||
|
||||
scramble_block_16(_Block, _Key) -> erlang:nif_error(nif_not_loaded).
|
||||
|
||||
Reference in New Issue
Block a user