1use core::convert::Infallible;
2
3use crate::rsa::{
4 implement_op,
5 Multi,
6 Rsa,
7 RsaMode,
8 RsaModularExponentiation,
9 RsaModularMultiplication,
10 RsaMultiplication,
11};
12
13impl<Dm: crate::DriverMode> Rsa<'_, Dm> {
14 pub fn ready(&mut self) -> nb::Result<(), Infallible> {
18 if self
19 .regs()
20 .query_clean()
21 .read()
22 .query_clean()
23 .bit_is_clear()
24 {
25 return Err(nb::Error::WouldBlock);
26 }
27 Ok(())
28 }
29
30 pub fn enable_disable_interrupt(&mut self, enable: bool) {
35 self.regs().int_ena().write(|w| w.int_ena().bit(enable));
36 }
37
38 fn write_mode(&mut self, mode: u32) {
39 self.regs().mode().write(|w| unsafe { w.bits(mode) });
40 }
41
42 pub fn enable_disable_search_acceleration(&mut self, enable: bool) {
52 self.regs()
53 .search_enable()
54 .write(|w| w.search_enable().bit(enable));
55 }
56
57 pub(super) fn is_search_enabled(&mut self) -> bool {
59 self.regs()
60 .search_enable()
61 .read()
62 .search_enable()
63 .bit_is_set()
64 }
65
66 pub(super) fn write_search_position(&mut self, search_position: u32) {
68 self.regs()
69 .search_pos()
70 .write(|w| unsafe { w.bits(search_position) });
71 }
72
73 pub fn enable_disable_constant_time_acceleration(&mut self, enable: bool) {
84 self.regs()
85 .constant_time()
86 .write(|w| w.constant_time().bit(enable));
87 }
88
89 pub(super) fn write_modexp_start(&self) {
91 self.regs()
92 .set_start_modexp()
93 .write(|w| w.set_start_modexp().set_bit());
94 }
95
96 pub(super) fn write_multi_start(&self) {
98 self.regs()
99 .set_start_mult()
100 .write(|w| w.set_start_mult().set_bit());
101 }
102
103 pub(super) fn write_modmulti_start(&self) {
105 self.regs()
106 .set_start_modmult()
107 .write(|w| w.set_start_modmult().set_bit());
108 }
109
110 pub(super) fn clear_interrupt(&mut self) {
112 self.regs().int_clr().write(|w| w.int_clr().set_bit());
113 }
114
115 pub(super) fn is_idle(&self) -> bool {
117 self.regs().query_idle().read().query_idle().bit_is_set()
118 }
119}
120
121pub mod operand_sizes {
123 use paste::paste;
125
126 use super::{implement_op, Multi, RsaMode};
127
128 implement_op!(
129 (32, multi),
130 (64, multi),
131 (96, multi),
132 (128, multi),
133 (160, multi),
134 (192, multi),
135 (224, multi),
136 (256, multi),
137 (288, multi),
138 (320, multi),
139 (352, multi),
140 (384, multi),
141 (416, multi),
142 (448, multi),
143 (480, multi),
144 (512, multi),
145 (544, multi),
146 (576, multi),
147 (608, multi),
148 (640, multi),
149 (672, multi),
150 (704, multi),
151 (736, multi),
152 (768, multi),
153 (800, multi),
154 (832, multi),
155 (864, multi),
156 (896, multi),
157 (928, multi),
158 (960, multi),
159 (992, multi),
160 (1024, multi),
161 (1056, multi),
162 (1088, multi),
163 (1120, multi),
164 (1152, multi),
165 (1184, multi),
166 (1216, multi),
167 (1248, multi),
168 (1280, multi),
169 (1312, multi),
170 (1344, multi),
171 (1376, multi),
172 (1408, multi),
173 (1440, multi),
174 (1472, multi),
175 (1504, multi),
176 (1536, multi),
177 (1568),
178 (1600),
179 (1632),
180 (1664),
181 (1696),
182 (1728),
183 (1760),
184 (1792),
185 (1824),
186 (1856),
187 (1888),
188 (1920),
189 (1952),
190 (1984),
191 (2016),
192 (2048),
193 (2080),
194 (2112),
195 (2144),
196 (2176),
197 (2208),
198 (2240),
199 (2272),
200 (2304),
201 (2336),
202 (2368),
203 (2400),
204 (2432),
205 (2464),
206 (2496),
207 (2528),
208 (2560),
209 (2592),
210 (2624),
211 (2656),
212 (2688),
213 (2720),
214 (2752),
215 (2784),
216 (2816),
217 (2848),
218 (2880),
219 (2912),
220 (2944),
221 (2976),
222 (3008),
223 (3040),
224 (3072)
225 );
226}
227
228impl<'d, T: RsaMode, Dm: crate::DriverMode, const N: usize> RsaModularExponentiation<'_, 'd, T, Dm>
229where
230 T: RsaMode<InputType = [u32; N]>,
231{
232 pub(super) fn find_search_pos(exponent: &T::InputType) -> u32 {
233 for (i, byte) in exponent.iter().rev().enumerate() {
234 if *byte == 0 {
235 continue;
236 }
237 return (exponent.len() * 32) as u32 - (byte.leading_zeros() + i as u32 * 32) - 1;
238 }
239 0
240 }
241
242 pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
244 rsa.write_mode((N - 1) as u32)
245 }
246}
247
248impl<'d, T: RsaMode, Dm: crate::DriverMode, const N: usize> RsaModularMultiplication<'_, 'd, T, Dm>
249where
250 T: RsaMode<InputType = [u32; N]>,
251{
252 pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
253 rsa.write_mode((N - 1) as u32)
254 }
255
256 pub(super) fn set_up_modular_multiplication(&mut self, operand_b: &T::InputType) {
257 self.rsa.write_operand_b(operand_b);
258 }
259}
260
261impl<'d, T: RsaMode + Multi, Dm: crate::DriverMode, const N: usize> RsaMultiplication<'_, 'd, T, Dm>
262where
263 T: RsaMode<InputType = [u32; N]>,
264{
265 pub(super) fn set_up_multiplication(&mut self, operand_b: &T::InputType) {
266 self.rsa.write_multi_operand_b(operand_b);
267 }
268
269 pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
271 rsa.write_mode((N * 2 - 1) as u32)
272 }
273}