esp_hal/rsa/
esp32.rs

1use core::convert::Infallible;
2
3use crate::rsa::{
4    Multi,
5    Rsa,
6    RsaMode,
7    RsaModularExponentiation,
8    RsaModularMultiplication,
9    RsaMultiplication,
10    implement_op,
11};
12
13impl<Dm: crate::DriverMode> Rsa<'_, Dm> {
14    /// After the RSA Accelerator is released from reset, the memory blocks
15    /// needs to be initialized, only after that peripheral should be used.
16    /// This function would return without an error if the memory is initialized
17    pub fn ready(&mut self) -> nb::Result<(), Infallible> {
18        if self.regs().clean().read().clean().bit_is_clear() {
19            return Err(nb::Error::WouldBlock);
20        }
21        Ok(())
22    }
23
24    /// Writes the multi-mode configuration to the RSA hardware.
25    pub(super) fn write_multi_mode(&mut self, mode: u32) {
26        self.regs().mult_mode().write(|w| unsafe { w.bits(mode) });
27    }
28
29    /// Writes the modular exponentiation mode configuration to the RSA
30    /// hardware.
31    pub(super) fn write_modexp_mode(&mut self, mode: u32) {
32        self.regs().modexp_mode().write(|w| unsafe { w.bits(mode) });
33    }
34
35    /// Starts the modular exponentiation operation.
36    pub(super) fn write_modexp_start(&self) {
37        self.regs()
38            .modexp_start()
39            .write(|w| w.modexp_start().set_bit());
40    }
41
42    /// Starts the multiplication operation.
43    pub(super) fn write_multi_start(&self) {
44        self.regs().mult_start().write(|w| w.mult_start().set_bit());
45    }
46
47    /// Starts the modular multiplication operation.
48    pub(super) fn write_modmulti_start(&self) {
49        self.write_multi_start();
50    }
51
52    /// Clears the RSA interrupt flag.
53    pub(super) fn clear_interrupt(&mut self) {
54        self.regs().interrupt().write(|w| w.interrupt().set_bit());
55    }
56
57    /// Checks if the RSA peripheral is idle.
58    pub(super) fn is_idle(&self) -> bool {
59        self.regs().interrupt().read().interrupt().bit_is_set()
60    }
61}
62
63/// Module defining marker types for various RSA operand sizes.
64pub mod operand_sizes {
65    //! Marker types for the operand sizes
66    use paste::paste;
67
68    use super::{Multi, RsaMode, implement_op};
69
70    implement_op!(
71        (512, multi),
72        (1024, multi),
73        (1536, multi),
74        (2048, multi),
75        (2560),
76        (3072),
77        (3584),
78        (4096)
79    );
80}
81
82impl<'d, T: RsaMode, Dm: crate::DriverMode, const N: usize> RsaModularMultiplication<'_, 'd, T, Dm>
83where
84    T: RsaMode<InputType = [u32; N]>,
85{
86    pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
87        rsa.write_multi_mode((N / 16 - 1) as u32)
88    }
89
90    /// Starts the modular multiplication operation.
91    ///
92    /// For more information refer to 24.3.2 of <https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf>.
93    pub(super) fn set_up_modular_multiplication(&mut self, operand_b: &T::InputType) {
94        self.rsa.write_multi_start();
95        self.rsa.wait_for_idle();
96
97        self.rsa.write_operand_a(operand_b);
98    }
99}
100
101impl<'d, T: RsaMode, Dm: crate::DriverMode, const N: usize> RsaModularExponentiation<'_, 'd, T, Dm>
102where
103    T: RsaMode<InputType = [u32; N]>,
104{
105    /// Sets the modular exponentiation mode for the RSA hardware.
106    pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
107        rsa.write_modexp_mode((N / 16 - 1) as u32)
108    }
109}
110
111impl<'d, T: RsaMode + Multi, Dm: crate::DriverMode, const N: usize> RsaMultiplication<'_, 'd, T, Dm>
112where
113    T: RsaMode<InputType = [u32; N]>,
114{
115    /// Sets the multiplication mode for the RSA hardware.
116    pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
117        rsa.write_multi_mode(((N * 2) / 16 + 7) as u32)
118    }
119
120    pub(super) fn set_up_multiplication(&mut self, operand_b: &T::InputType) {
121        self.rsa.write_multi_operand_b(operand_b);
122    }
123}