1 /*
2 Written in the D programming language.
3 For git maintenance (ensure at least one congruent line with originating C header):
4 #define randombytes_H
5 */
6 
7 /**
8  * Functions related to randomness.
9  * See_Also: https://download.libsodium.org/doc/generating_random_data#usage
10  */
11 
12 module deimos.sodium.randombytes;
13 
14 import deimos.sodium.export_;
15 
16 
17 extern(C) nothrow :
18 
19 /// See_Also:
20 struct randombytes_implementation {
21     const(char)* function()                             implementation_name; /* required */
22     uint         function()                             random;              /* required */
23     void         function()                             stir;                /* optional */
24     uint         function(const uint upper_bound)       uniform;             /* optional, a default implementation will be used if null */
25     void         function(void* buf, const size_t size) buf;                 /* required */
26     int          function()                             close;               /* optional */
27 }
28 
29 @nogc :
30 
31 ///
32 enum randombytes_BYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX, uint.max); // 0xffff_ffffU
33 
34 /* my understanding of the pure attribute in D is, that the following functions (except randombytes_seedbytes and randombytes_buf_deterministic)
35    are impure: they depend on hidden global mutable state */
36 
37 
38 ///
39 enum ubyte randombytes_SEEDBYTES = 32U;
40 
41 /// Returns: 32
42 size_t randombytes_seedbytes() pure @trusted;
43 
44 /// fills `buf` with `size` unpredictable bytes.
45 void randombytes_buf(scope void* buf, const size_t size); // __attribute__ ((nonnull));
46 
47 /// same as randombytes_buf, but based on seed delivers reproducible 'unpredictable' bytes
48 void randombytes_buf_deterministic(void* buf, const size_t size,
49                                    ref const(ubyte)[randombytes_SEEDBYTES] seed) pure; // __attribute__ ((nonnull));
50 
51 /// Returns: an unpredictable value between 0 and uint.max (included)
52 uint randombytes_random() @trusted;
53 
54 /// Returns: an unpredictable value between 0 and upper_bound (excluded)
55 uint randombytes_uniform(const uint upper_bound) @trusted;
56 
57 /// reseeds the pseudo-random number generator
58 void randombytes_stir() @trusted;
59 
60 /// closes the pseudo-random number generator
61 /// Returns:
62 int randombytes_close() @trusted;
63 
64 ///
65 int randombytes_set_implementation(scope randombytes_implementation* impl); // __attribute__ ((nonnull));
66 
67 /// Returns: name of current active randombytes_implementation
68 const(char)* randombytes_implementation_name();
69 
70 /* -- NaCl compatibility interface -- */
71 
72 /// same as randombytes_buf, fills `buf` with `buf_len` unpredictable bytes.
73 void randombytes(scope ubyte* buf, const ulong buf_len); // __attribute__ ((nonnull));