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> {
19 if self.regs().clean().read().clean().bit_is_clear() {
20 return Err(nb::Error::WouldBlock);
21 }
22 Ok(())
23 }
24
25 pub fn enable_disable_interrupt(&mut self, enable: bool) {
30 self.regs().int_ena().write(|w| w.int_ena().bit(enable));
31 }
32
33 fn write_mode(&mut self, mode: u32) {
34 self.regs().mode().write(|w| unsafe { w.bits(mode) });
35 }
36
37 pub fn enable_disable_search_acceleration(&mut self, enable: bool) {
47 self.regs()
48 .search_enable()
49 .write(|w| w.search_enable().bit(enable));
50 }
51
52 pub(super) fn is_search_enabled(&mut self) -> bool {
54 self.regs()
55 .search_enable()
56 .read()
57 .search_enable()
58 .bit_is_set()
59 }
60
61 pub(super) fn write_search_position(&mut self, search_position: u32) {
63 self.regs()
64 .search_pos()
65 .write(|w| unsafe { w.bits(search_position) });
66 }
67
68 pub fn enable_disable_constant_time_acceleration(&mut self, enable: bool) {
79 self.regs()
80 .constant_time()
81 .write(|w| w.constant_time().bit(!enable));
82 }
83
84 pub(super) fn write_modexp_start(&self) {
86 self.regs()
87 .modexp_start()
88 .write(|w| w.modexp_start().set_bit());
89 }
90
91 pub(super) fn write_multi_start(&self) {
93 self.regs().mult_start().write(|w| w.mult_start().set_bit());
94 }
95
96 pub(super) fn write_modmulti_start(&self) {
98 self.regs()
99 .modmult_start()
100 .write(|w| w.modmult_start().set_bit());
101 }
102
103 pub(super) fn clear_interrupt(&mut self) {
105 self.regs().int_clr().write(|w| w.int_clr().set_bit());
106 }
107
108 pub(super) fn is_idle(&self) -> bool {
110 self.regs().idle().read().idle().bit_is_set()
111 }
112}
113
114pub mod operand_sizes {
115 use paste::paste;
117
118 use super::{Multi, RsaMode, implement_op};
119
120 implement_op!(
121 (32, multi),
122 (64, multi),
123 (96, multi),
124 (128, multi),
125 (160, multi),
126 (192, multi),
127 (224, multi),
128 (256, multi),
129 (288, multi),
130 (320, multi),
131 (352, multi),
132 (384, multi),
133 (416, multi),
134 (448, multi),
135 (480, multi),
136 (512, multi),
137 (544, multi),
138 (576, multi),
139 (608, multi),
140 (640, multi),
141 (672, multi),
142 (704, multi),
143 (736, multi),
144 (768, multi),
145 (800, multi),
146 (832, multi),
147 (864, multi),
148 (896, multi),
149 (928, multi),
150 (960, multi),
151 (992, multi),
152 (1024, multi),
153 (1056, multi),
154 (1088, multi),
155 (1120, multi),
156 (1152, multi),
157 (1184, multi),
158 (1216, multi),
159 (1248, multi),
160 (1280, multi),
161 (1312, multi),
162 (1344, multi),
163 (1376, multi),
164 (1408, multi),
165 (1440, multi),
166 (1472, multi),
167 (1504, multi),
168 (1536, multi),
169 (1568, multi),
170 (1600, multi),
171 (1632, multi),
172 (1664, multi),
173 (1696, multi),
174 (1728, multi),
175 (1760, multi),
176 (1792, multi),
177 (1824, multi),
178 (1856, multi),
179 (1888, multi),
180 (1920, multi),
181 (1952, multi),
182 (1984, multi),
183 (2016, multi),
184 (2048, multi)
185 );
186
187 implement_op!(
188 (2080),
189 (2112),
190 (2144),
191 (2176),
192 (2208),
193 (2240),
194 (2272),
195 (2304),
196 (2336),
197 (2368),
198 (2400),
199 (2432),
200 (2464),
201 (2496),
202 (2528),
203 (2560),
204 (2592),
205 (2624),
206 (2656),
207 (2688),
208 (2720),
209 (2752),
210 (2784),
211 (2816),
212 (2848),
213 (2880),
214 (2912),
215 (2944),
216 (2976),
217 (3008),
218 (3040),
219 (3072),
220 (3104),
221 (3136),
222 (3168),
223 (3200),
224 (3232),
225 (3264),
226 (3296),
227 (3328),
228 (3360),
229 (3392),
230 (3424),
231 (3456),
232 (3488),
233 (3520),
234 (3552),
235 (3584),
236 (3616),
237 (3648),
238 (3680),
239 (3712),
240 (3744),
241 (3776),
242 (3808),
243 (3840),
244 (3872),
245 (3904),
246 (3936),
247 (3968),
248 (4000),
249 (4032),
250 (4064),
251 (4096)
252 );
253}
254
255impl<'d, T: RsaMode, Dm: crate::DriverMode, const N: usize> RsaModularExponentiation<'_, 'd, T, Dm>
256where
257 T: RsaMode<InputType = [u32; N]>,
258{
259 pub(super) fn find_search_pos(exponent: &T::InputType) -> u32 {
260 for (i, byte) in exponent.iter().rev().enumerate() {
261 if *byte == 0 {
262 continue;
263 }
264 return (exponent.len() * 32) as u32 - (byte.leading_zeros() + i as u32 * 32) - 1;
265 }
266 0
267 }
268
269 pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
271 rsa.write_mode((N - 1) as u32)
272 }
273}
274
275impl<'d, T: RsaMode, Dm: crate::DriverMode, const N: usize> RsaModularMultiplication<'_, 'd, T, Dm>
276where
277 T: RsaMode<InputType = [u32; N]>,
278{
279 pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
280 rsa.write_mode((N - 1) as u32)
281 }
282
283 pub(super) fn set_up_modular_multiplication(&mut self, operand_b: &T::InputType) {
284 self.rsa.write_operand_b(operand_b);
285 }
286}
287
288impl<'d, T: RsaMode + Multi, Dm: crate::DriverMode, const N: usize> RsaMultiplication<'_, 'd, T, Dm>
289where
290 T: RsaMode<InputType = [u32; N]>,
291{
292 pub(super) fn write_mode(rsa: &mut Rsa<'d, Dm>) {
294 rsa.write_mode((N * 2 - 1) as u32)
295 }
296
297 pub(super) fn set_up_multiplication(&mut self, operand_b: &T::InputType) {
298 self.rsa.write_multi_operand_b(operand_b);
299 }
300}