Introduce enacl:randombytes/1.

This commit is contained in:
Jesper Louis Andersen
2014-12-09 00:33:55 +01:00
parent 6d37abd76a
commit fd9f421621
5 changed files with 69 additions and 2 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{application, enacl,
[
{description, "Erlang NaCl bindings"},
{vsn, "0.9.0"},
{vsn, "0.10.0"},
{registered, []},
{applications, [kernel, stdlib]},
{env, []}
+25
View File
@@ -62,6 +62,11 @@
verify_32/2
]).
%% Libsodium specific functions (which are also part of the "undocumented" interface to NaCl
-export([
randombytes/1
]).
%% Other helper functions
-export([
reds/1
@@ -85,6 +90,8 @@
-define(AUTH_REDUCTIONS, 102 * 2).
-define(ONETIME_AUTH_SIZE, 128 * 1024).
-define(ONETIME_AUTH_REDUCTIONS, 105 * 2).
-define(RANDOMBYTES_SIZE, 1024).
-define(RANDOMBYTES_REDUCTIONS, 200).
%% Count reductions and number of scheduler yields for Fun. Fun is assumed
%% to be one of the above exor variants.
@@ -481,6 +488,24 @@ onetime_auth_size() -> enacl_nif:crypto_onetimeauth_BYTES().
-spec onetime_auth_key_size() -> pos_integer().
onetime_auth_key_size() -> enacl_nif:crypto_onetimeauth_KEYBYTES().
%% Obtaining random bytes
%% @doc randombytes/1 produces a stream of random bytes of the given size
%% The security properties of the random stream are that of the libsodium library. Specifically,
%% we use:
%%
%% * RtlGenRandom() on Windows systems
%% * arc4random() on OpenBSD and Bitrig
%% * /dev/urandom on other Unix environments
%%
%% It is up to you to pick a system with a appropriately strong (P)RNG for your purpose. We refer
%% you to the underlying system implementations for random data.
%% @end
randombytes(N) when N =< ?RANDOMBYTES_SIZE ->
bump(enacl_nif:randombytes_b(N), ?RANDOMBYTES_REDUCTIONS, ?RANDOMBYTES_SIZE, N);
randombytes(N) ->
enacl_nif:randombytes(N).
%% Helpers
p_zerobytes() ->
binary:copy(<<0>>, enacl_nif:crypto_box_ZEROBYTES()).
+10
View File
@@ -72,6 +72,12 @@
crypto_verify_32/2
]).
%% Access to the RNG
-export([
randombytes/1,
randombytes_b/1
]).
-on_load(init/0).
init() ->
@@ -143,3 +149,7 @@ crypto_hash(Input) when is_binary(Input) -> not_loaded().
crypto_hash_b(Input) when is_binary(Input) -> not_loaded().
crypto_verify_16(_X, _Y) -> not_loaded().
crypto_verify_32(_X, _Y) -> not_loaded().
randombytes(_RequestedSize) -> not_loaded().
randombytes_b(_RequestedSize) -> not_loaded().