Skip to content

Commit 51d0252

Browse files
authored
Merge pull request #4217 from embassy-rs/rand09
Add rand-core v0.9 support.
2 parents e8b1ea1 + e4fc487 commit 51d0252

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+218
-122
lines changed

embassy-imxrt/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ cortex-m = "0.7.6"
8080
critical-section = "1.1"
8181
embedded-io = { version = "0.6.1" }
8282
embedded-io-async = { version = "0.6.1" }
83-
rand_core = "0.6.4"
8483
fixed = "1.23.1"
8584

85+
rand-core-06 = { package = "rand_core", version = "0.6" }
86+
rand-core-09 = { package = "rand_core", version = "0.9" }
87+
8688
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
8789
"unproven",
8890
] }

embassy-imxrt/src/rng.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::task::Poll;
66

77
use embassy_futures::block_on;
88
use embassy_sync::waitqueue::AtomicWaker;
9-
use rand_core::{CryptoRng, RngCore};
109

1110
use crate::clocks::{enable_and_reset, SysconPeripheral};
1211
use crate::interrupt::typelevel::Interrupt;
@@ -201,32 +200,63 @@ impl<'d> Rng<'d> {
201200
.mctl()
202201
.modify(|_, w| w.trng_acc().set_bit().prgm().clear_bit());
203202
}
204-
}
205203

206-
impl RngCore for Rng<'_> {
207-
fn next_u32(&mut self) -> u32 {
204+
/// Generate a random u32
205+
pub fn blocking_next_u32(&mut self) -> u32 {
208206
let mut bytes = [0u8; 4];
209207
block_on(self.async_fill_bytes(&mut bytes)).unwrap();
210208
u32::from_ne_bytes(bytes)
211209
}
212210

213-
fn next_u64(&mut self) -> u64 {
211+
/// Generate a random u64
212+
pub fn blocking_next_u64(&mut self) -> u64 {
214213
let mut bytes = [0u8; 8];
215214
block_on(self.async_fill_bytes(&mut bytes)).unwrap();
216215
u64::from_ne_bytes(bytes)
217216
}
218217

219-
fn fill_bytes(&mut self, dest: &mut [u8]) {
218+
/// Fill a slice with random bytes.
219+
pub fn blocking_fill_bytes(&mut self, dest: &mut [u8]) {
220220
block_on(self.async_fill_bytes(dest)).unwrap();
221221
}
222+
}
222223

223-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
224-
self.fill_bytes(dest);
224+
impl<'d> rand_core_06::RngCore for Rng<'d> {
225+
fn next_u32(&mut self) -> u32 {
226+
self.blocking_next_u32()
227+
}
228+
229+
fn next_u64(&mut self) -> u64 {
230+
self.blocking_next_u64()
231+
}
232+
233+
fn fill_bytes(&mut self, dest: &mut [u8]) {
234+
self.blocking_fill_bytes(dest);
235+
}
236+
237+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
238+
self.blocking_fill_bytes(dest);
225239
Ok(())
226240
}
227241
}
228242

229-
impl CryptoRng for Rng<'_> {}
243+
impl<'d> rand_core_06::CryptoRng for Rng<'d> {}
244+
245+
impl<'d> rand_core_09::RngCore for Rng<'d> {
246+
fn next_u32(&mut self) -> u32 {
247+
self.blocking_next_u32()
248+
}
249+
250+
fn next_u64(&mut self) -> u64 {
251+
self.blocking_next_u64()
252+
}
253+
254+
fn fill_bytes(&mut self, dest: &mut [u8]) {
255+
self.blocking_fill_bytes(dest);
256+
}
257+
}
258+
259+
impl<'d> rand_core_09::CryptoRng for Rng<'d> {}
230260

231261
struct Info {
232262
regs: crate::pac::Trng,

embassy-nrf/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ embedded-hal-async = { version = "1.0" }
154154
embedded-io = { version = "0.6.0" }
155155
embedded-io-async = { version = "0.6.1" }
156156

157+
rand-core-06 = { package = "rand_core", version = "0.6" }
158+
rand-core-09 = { package = "rand_core", version = "0.9" }
159+
157160
nrf-pac = "0.1.0"
158161

159162
defmt = { version = "0.3", optional = true }
@@ -162,7 +165,6 @@ log = { version = "0.4.14", optional = true }
162165
cortex-m-rt = ">=0.6.15,<0.8"
163166
cortex-m = "0.7.6"
164167
critical-section = "1.1"
165-
rand_core = "0.6.3"
166168
fixed = "1.10.0"
167169
embedded-storage = "0.3.1"
168170
embedded-storage-async = "0.4.1"

embassy-nrf/src/rng.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ impl<'d, T: Instance> Rng<'d, T> {
167167

168168
self.stop();
169169
}
170+
171+
/// Generate a random u32
172+
pub fn blocking_next_u32(&mut self) -> u32 {
173+
let mut bytes = [0; 4];
174+
self.blocking_fill_bytes(&mut bytes);
175+
// We don't care about the endianness, so just use the native one.
176+
u32::from_ne_bytes(bytes)
177+
}
178+
179+
/// Generate a random u64
180+
pub fn blocking_next_u64(&mut self) -> u64 {
181+
let mut bytes = [0; 8];
182+
self.blocking_fill_bytes(&mut bytes);
183+
u64::from_ne_bytes(bytes)
184+
}
170185
}
171186

172187
impl<'d, T: Instance> Drop for Rng<'d, T> {
@@ -180,31 +195,37 @@ impl<'d, T: Instance> Drop for Rng<'d, T> {
180195
}
181196
}
182197

183-
impl<'d, T: Instance> rand_core::RngCore for Rng<'d, T> {
198+
impl<'d, T: Instance> rand_core_06::RngCore for Rng<'d, T> {
184199
fn fill_bytes(&mut self, dest: &mut [u8]) {
185200
self.blocking_fill_bytes(dest);
186201
}
187-
188202
fn next_u32(&mut self) -> u32 {
189-
let mut bytes = [0; 4];
190-
self.blocking_fill_bytes(&mut bytes);
191-
// We don't care about the endianness, so just use the native one.
192-
u32::from_ne_bytes(bytes)
203+
self.blocking_next_u32()
193204
}
194-
195205
fn next_u64(&mut self) -> u64 {
196-
let mut bytes = [0; 8];
197-
self.blocking_fill_bytes(&mut bytes);
198-
u64::from_ne_bytes(bytes)
206+
self.blocking_next_u64()
199207
}
200-
201-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
208+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
202209
self.blocking_fill_bytes(dest);
203210
Ok(())
204211
}
205212
}
206213

207-
impl<'d, T: Instance> rand_core::CryptoRng for Rng<'d, T> {}
214+
impl<'d, T: Instance> rand_core_06::CryptoRng for Rng<'d, T> {}
215+
216+
impl<'d, T: Instance> rand_core_09::RngCore for Rng<'d, T> {
217+
fn fill_bytes(&mut self, dest: &mut [u8]) {
218+
self.blocking_fill_bytes(dest);
219+
}
220+
fn next_u32(&mut self) -> u32 {
221+
self.blocking_next_u32()
222+
}
223+
fn next_u64(&mut self) -> u64 {
224+
self.blocking_next_u64()
225+
}
226+
}
227+
228+
impl<'d, T: Instance> rand_core_09::CryptoRng for Rng<'d, T> {}
208229

209230
/// Peripheral static state
210231
pub(crate) struct State {

embassy-rp/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ embedded-io = { version = "0.6.1" }
157157
embedded-io-async = { version = "0.6.1" }
158158
embedded-storage = { version = "0.3" }
159159
embedded-storage-async = { version = "0.4.1" }
160-
rand_core = "0.6.4"
161160
fixed = "1.28.0"
162161

163162
rp-pac = { version = "7.0.0" }
@@ -167,6 +166,9 @@ embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
167166
embedded-hal-async = { version = "1.0" }
168167
embedded-hal-nb = { version = "1.0" }
169168

169+
rand-core-06 = { package = "rand_core", version = "0.6" }
170+
rand-core-09 = { package = "rand_core", version = "0.9" }
171+
170172
pio = { version = "0.3" }
171173
rp2040-boot2 = "0.3"
172174
document-features = "0.2.10"

embassy-rp/src/clocks.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,8 @@ impl<'d, T: GpoutPin> Drop for Gpout<'d, T> {
17761776
pub struct RoscRng;
17771777

17781778
impl RoscRng {
1779-
fn next_u8() -> u8 {
1779+
/// Get a random u8
1780+
pub fn next_u8() -> u8 {
17801781
let random_reg = pac::ROSC.randombit();
17811782
let mut acc = 0;
17821783
for _ in 0..u8::BITS {
@@ -1785,26 +1786,60 @@ impl RoscRng {
17851786
}
17861787
acc
17871788
}
1789+
1790+
/// Get a random u32
1791+
pub fn next_u32(&mut self) -> u32 {
1792+
rand_core_09::impls::next_u32_via_fill(self)
1793+
}
1794+
1795+
/// Get a random u64
1796+
pub fn next_u64(&mut self) -> u64 {
1797+
rand_core_09::impls::next_u64_via_fill(self)
1798+
}
1799+
1800+
/// Fill a slice with random bytes
1801+
pub fn fill_bytes(&mut self, dest: &mut [u8]) {
1802+
dest.fill_with(Self::next_u8)
1803+
}
17881804
}
17891805

1790-
impl rand_core::RngCore for RoscRng {
1791-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
1792-
Ok(self.fill_bytes(dest))
1806+
impl rand_core_06::RngCore for RoscRng {
1807+
fn next_u32(&mut self) -> u32 {
1808+
self.next_u32()
1809+
}
1810+
1811+
fn next_u64(&mut self) -> u64 {
1812+
self.next_u64()
1813+
}
1814+
1815+
fn fill_bytes(&mut self, dest: &mut [u8]) {
1816+
self.fill_bytes(dest);
1817+
}
1818+
1819+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
1820+
self.fill_bytes(dest);
1821+
Ok(())
17931822
}
1823+
}
1824+
1825+
impl rand_core_06::CryptoRng for RoscRng {}
17941826

1827+
impl rand_core_09::RngCore for RoscRng {
17951828
fn next_u32(&mut self) -> u32 {
1796-
rand_core::impls::next_u32_via_fill(self)
1829+
self.next_u32()
17971830
}
17981831

17991832
fn next_u64(&mut self) -> u64 {
1800-
rand_core::impls::next_u64_via_fill(self)
1833+
self.next_u64()
18011834
}
18021835

18031836
fn fill_bytes(&mut self, dest: &mut [u8]) {
1804-
dest.fill_with(Self::next_u8)
1837+
self.fill_bytes(dest);
18051838
}
18061839
}
18071840

1841+
impl rand_core_09::CryptoRng for RoscRng {}
1842+
18081843
/// Enter the `DORMANT` sleep state. This will stop *all* internal clocks
18091844
/// and can only be exited through resets, dormant-wake GPIO interrupts,
18101845
/// and RTC interrupts. If RTC is clocked from an internal clock source

embassy-rp/src/trng.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use core::task::Poll;
77

88
use embassy_hal_internal::{Peri, PeripheralType};
99
use embassy_sync::waitqueue::AtomicWaker;
10-
use rand_core::Error;
1110

1211
use crate::interrupt::typelevel::{Binding, Interrupt};
1312
use crate::peripherals::TRNG;
@@ -369,7 +368,7 @@ impl<'d, T: Instance> Trng<'d, T> {
369368
}
370369
}
371370

372-
impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> {
371+
impl<'d, T: Instance> rand_core_06::RngCore for Trng<'d, T> {
373372
fn next_u32(&mut self) -> u32 {
374373
self.blocking_next_u32()
375374
}
@@ -379,16 +378,32 @@ impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> {
379378
}
380379

381380
fn fill_bytes(&mut self, dest: &mut [u8]) {
382-
self.blocking_fill_bytes(dest)
381+
self.blocking_fill_bytes(dest);
383382
}
384383

385-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
384+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
386385
self.blocking_fill_bytes(dest);
387386
Ok(())
388387
}
389388
}
390389

391-
impl<'d, T: Instance> rand_core::CryptoRng for Trng<'d, T> {}
390+
impl<'d, T: Instance> rand_core_06::CryptoRng for Trng<'d, T> {}
391+
392+
impl<'d, T: Instance> rand_core_09::RngCore for Trng<'d, T> {
393+
fn next_u32(&mut self) -> u32 {
394+
self.blocking_next_u32()
395+
}
396+
397+
fn next_u64(&mut self) -> u64 {
398+
self.blocking_next_u64()
399+
}
400+
401+
fn fill_bytes(&mut self, dest: &mut [u8]) {
402+
self.blocking_fill_bytes(dest);
403+
}
404+
}
405+
406+
impl<'d, T: Instance> rand_core_09::CryptoRng for Trng<'d, T> {}
392407

393408
/// TRNG interrupt handler.
394409
pub struct InterruptHandler<T: Instance> {

embassy-stm32/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ embedded-can = "0.4"
6363
embedded-storage = "0.3.1"
6464
embedded-storage-async = { version = "0.4.1" }
6565

66+
rand-core-06 = { package = "rand_core", version = "0.6" }
67+
rand-core-09 = { package = "rand_core", version = "0.9" }
68+
6669

6770
defmt = { version = "0.3", optional = true }
6871
log = { version = "0.4.14", optional = true }
6972
cortex-m-rt = ">=0.6.15,<0.8"
7073
cortex-m = "0.7.6"
7174
futures-util = { version = "0.3.30", default-features = false }
72-
rand_core = "0.6.3"
7375
sdio-host = "0.9.0"
7476
critical-section = "1.1"
7577
#stm32-metapac = { version = "16" }

0 commit comments

Comments
 (0)