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 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 pub(super) fn write_multi_mode(&mut self, mode: u32) {
26 self.regs().mult_mode().write(|w| unsafe { w.bits(mode) });
27 }
28
29 pub(super) fn write_modexp_mode(&mut self, mode: u32) {
32 self.regs().modexp_mode().write(|w| unsafe { w.bits(mode) });
33 }
34
35 pub(super) fn write_modexp_start(&self) {
37 self.regs()
38 .modexp_start()
39 .write(|w| w.modexp_start().set_bit());
40 }
41
42 pub(super) fn write_multi_start(&self) {
44 self.regs().mult_start().write(|w| w.mult_start().set_bit());
45 }
46
47 pub(super) fn write_modmulti_start(&self) {
49 self.write_multi_start();
50 }
51
52 pub(super) fn clear_interrupt(&mut self) {
54 self.regs().interrupt().write(|w| w.interrupt().set_bit());
55 }
56
57 pub(super) fn is_idle(&self) -> bool {
59 self.regs().interrupt().read().interrupt().bit_is_set()
60 }
61}
62
63pub mod operand_sizes {
65 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 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 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 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}