1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! FFI bridge for the `randombytes` symbol expected by `mldsa-native`.
//!
//! Provides a configurable Rust-backed implementation and a convenience
//! macro to export a C-compatible `randombytes` function with the
//! correct symbol name for linking with the backend C code.
//!
//! **All notes [about `randombytes`](crate#about-randombytes)
//! documented at the top of the crate apply to this module.**
//!
//! # Feature flags
//!
//! - `rand`: Enables the default Rust-backed CSPRNG implementation.
//! - `extern-C-randombytes`: Automatically exports the C symbol.
/// The internal name for the `randombytes` function symbol expected
/// by the mldsa-native C code, to bridge the C and Rust
/// implementations.
///
/// NOTE: We use a custom symbol name when compiling both the C code
/// and this crate to avoid global symbol collisions at link time.
// NOTE: This is set via env variable in our build.rs file.
pub const RANDOMBYTES_INTERNAL_NAME: &str = env!;
/// Generates random bytes and writes them into the provided buffer.
///
/// # Parameters
///
/// - `out`: A mutable slice where the random bytes will be written.
///
/// # Returns
///
/// Returns `Ok(())` on success. This function cannot fail as it uses
/// `rand::fill`, which is infallible.
// NOTE: See
// <https://internals.rust-lang.org/t/pub-on-macro-rules/19358/16>
// on why we need this dance to make the macro available to
// external crates, without polluting the crate's top namespace.
/// Convenience macro to generate the extern "C" "randombytes"
/// function expected by the underlying C code of
/// [mldsa-native](https://github.com/pq-code-package/mldsa-native/blob/0b1c5364dc468a726aab4adc12a9385ba55f0306/mldsa/src/randombytes.h).
///
/// The exported name of the function symbol will be set to
/// [crate::randombytes::RANDOMBYTES_INTERNAL_NAME], as required to
/// bridge the C and Rust implementations.
///
/// # Invocation Forms
///
/// ## Without arguments
///
/// ```rust, ignore
/// make_randombytes_fn!()
/// ```
///
/// Use this crate's default implementation:
///
/// - If the `rand` feature is enabled, it forwards to
/// [`crate::randombytes::default_randombytes`].
/// - Otherwise, it uses a stub implementation that always fails.
///
/// ## With a custom implementation
///
/// ```rust, ignore
/// make_randombytes_fn!(EXPR)
/// ```
///
/// `EXPR` must evaluate to either:
///
/// - A function with the following signature:
///
/// ```rust, ignore
/// fn f(out: &mut [u8]) -> Result<(), E>
/// ```
///
/// - A closure of the form:
///
/// ```rust, ignore
/// |out: &mut [u8]| -> Result<(), E>
/// ```
///
/// ### Requirements
///
/// The provided function or closure must:
///
/// - Fill `out` completely with fresh, unique, unpredictable bytes
/// - Use a cryptographically secure pseudorandom number generator (CSPRNG)
/// - Return `Ok(())` on success
/// - Return `Err(_)` if randomness generation fails
///
/// ### Security
///
/// This function is a critical cryptographic dependency. Any weakness
/// in the provided randomness source directly compromises the security
/// of all non-deterministic operations performed by `mldsa-native`.
///
/// # Examples
///
/// Provide a custom closure:
///
/// ```rust
/// use mldsa_native_rs::randombytes::make_randombytes_fn;
///
/// make_randombytes_fn!(|_out| {
/// Err("this stub always fails")
/// });
/// ```
///
/// Provide a function:
///
/// ```rust
/// use mldsa_native_rs::randombytes::make_randombytes_fn;
///
/// make_randombytes_fn!(
/// mldsa_native_rs::randombytes::default_randombytes
/// );
/// ```
///
/// The zero-argument form:
///
/// ```rust
/// use mldsa_native_rs::randombytes::make_randombytes_fn;
///
/// make_randombytes_fn!();
/// ```
///
/// is equivalent to either of the previous 2 examples, depending on
/// the `rand` feature being enabled.
pub use __khbfVNyeQk_make_randombytes_fn as make_randombytes_fn;
make_randombytes_fn!;