aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/cracen.rs
blob: 6381701c023cc85b367279e41bc5531bef95d933 (plain)
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
//! CRACEN - Cryptographic Accelerator Engine driver.

#![macro_use]

use core::marker::PhantomData;

use crate::mode::{Blocking, Mode};
use crate::{Peri, interrupt, pac, peripherals};

/// A wrapper around an nRF54 CRACEN peripheral.
///
/// It has a blocking api through `rand`.
pub struct Cracen<'d, M: Mode> {
    _peri: Peri<'d, peripherals::CRACEN>,
    _p: PhantomData<M>,
}

impl<'d> Cracen<'d, Blocking> {
    /// Create a new CRACEN driver.
    pub fn new_blocking(_peri: Peri<'d, peripherals::CRACEN>) -> Self {
        Self { _peri, _p: PhantomData }
    }
}

impl<'d, M: Mode> Cracen<'d, M> {
    fn regs() -> pac::cracen::Cracen {
        pac::CRACEN
    }

    fn core() -> pac::cracencore::Cracencore {
        pac::CRACENCORE
    }

    fn start_rng(&self) {
        let r = Self::regs();
        r.enable().write(|w| {
            w.set_rng(true);
        });

        let r = Self::core();
        r.rngcontrol().control().write(|w| {
            w.set_enable(true);
        });

        while r.rngcontrol().status().read().state() == pac::cracencore::vals::State::STARTUP {}
    }

    fn stop_rng(&self) {
        let r = Self::core();
        r.rngcontrol().control().write(|w| {
            w.set_enable(false);
        });

        while r.rngcontrol().status().read().state() != pac::cracencore::vals::State::RESET {}

        let r = Self::regs();
        r.enable().write(|w| {
            w.set_cryptomaster(false);
            w.set_rng(false);
            w.set_pkeikg(false);
        });
    }

    /// Fill the buffer with random bytes, blocking version.
    pub fn blocking_fill_bytes(&mut self, dest: &mut [u8]) {
        self.start_rng();

        let r = Self::core();
        for chunk in dest.chunks_mut(4) {
            while r.rngcontrol().fifolevel().read() == 0 {}
            let word = r.rngcontrol().fifo(0).read().to_ne_bytes();
            let to_copy = word.len().min(chunk.len());
            chunk[..to_copy].copy_from_slice(&word[..to_copy]);
        }

        self.stop_rng();
    }

    /// Generate a random u32
    pub fn blocking_next_u32(&mut self) -> u32 {
        let mut bytes = [0; 4];
        self.blocking_fill_bytes(&mut bytes);
        // We don't care about the endianness, so just use the native one.
        u32::from_ne_bytes(bytes)
    }

    /// Generate a random u64
    pub fn blocking_next_u64(&mut self) -> u64 {
        let mut bytes = [0; 8];
        self.blocking_fill_bytes(&mut bytes);
        u64::from_ne_bytes(bytes)
    }
}

impl<'d, M: Mode> Drop for Cracen<'d, M> {
    fn drop(&mut self) {
        // nothing to do here, since we stop+disable rng for each operation.
    }
}

impl<'d, M: Mode> rand_core_06::RngCore for Cracen<'d, M> {
    fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.blocking_fill_bytes(dest);
    }
    fn next_u32(&mut self) -> u32 {
        self.blocking_next_u32()
    }
    fn next_u64(&mut self) -> u64 {
        self.blocking_next_u64()
    }
    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
        self.blocking_fill_bytes(dest);
        Ok(())
    }
}

impl<'d, M: Mode> rand_core_06::CryptoRng for Cracen<'d, M> {}

impl<'d, M: Mode> rand_core_09::RngCore for Cracen<'d, M> {
    fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.blocking_fill_bytes(dest);
    }
    fn next_u32(&mut self) -> u32 {
        self.blocking_next_u32()
    }
    fn next_u64(&mut self) -> u64 {
        self.blocking_next_u64()
    }
}

impl<'d, M: Mode> rand_core_09::CryptoRng for Cracen<'d, M> {}

pub(crate) trait SealedInstance {}

/// CRACEN peripheral instance.
#[allow(private_bounds)]
pub trait Instance: SealedInstance + 'static + Send {
    /// Interrupt for this peripheral.
    type Interrupt: interrupt::typelevel::Interrupt;
}

macro_rules! impl_cracen {
    ($type:ident, $pac_type:ident, $irq:ident) => {
        impl crate::cracen::SealedInstance for peripherals::$type {}
        impl crate::cracen::Instance for peripherals::$type {
            type Interrupt = crate::interrupt::typelevel::$irq;
        }
    };
}