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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use core::{convert::Infallible, marker::PhantomData, ptr::copy_nonoverlapping};
use crate::rsa::{
implement_op,
Multi,
Rsa,
RsaMode,
RsaModularExponentiation,
RsaModularMultiplication,
RsaMultiplication,
};
impl<'d> Rsa<'d> {
/// After the RSA Accelerator is released from reset, the memory blocks
/// needs to be initialized, only after that peripheral should be used.
/// This function would return without an error if the memory is initialized
pub fn ready(&mut self) -> nb::Result<(), Infallible> {
if self.rsa.query_clean().read().query_clean().bit_is_clear() {
return Err(nb::Error::WouldBlock);
}
Ok(())
}
/// Enables/disables rsa interrupt, when enabled rsa perpheral would
/// generate an interrupt when a operation is finished.
pub fn enable_disable_interrupt(&mut self, enable: bool) {
match enable {
true => self.rsa.int_ena().write(|w| w.int_ena().set_bit()),
false => self.rsa.int_ena().write(|w| w.int_ena().clear_bit()),
}
}
fn write_mode(&mut self, mode: u32) {
self.rsa.mode().write(|w| unsafe { w.bits(mode) });
}
/// Enables/disables search acceleration, when enabled it would increases
/// the performance of modular exponentiation by discarding the
/// exponent's bits before the most significant set bit. Note: this might
/// affect the security, for more info refer 18.3.4 of <https://www.espressif.com/sites/default/files/documentation/esp32-c6_technical_reference_manual_en.pdf>
pub fn enable_disable_search_acceleration(&mut self, enable: bool) {
match enable {
true => self
.rsa
.search_enable()
.write(|w| w.search_enable().set_bit()),
false => self
.rsa
.search_enable()
.write(|w| w.search_enable().clear_bit()),
}
}
pub(super) fn is_search_enabled(&mut self) -> bool {
self.rsa.search_enable().read().search_enable().bit_is_set()
}
pub(super) fn write_search_position(&mut self, search_position: u32) {
self.rsa
.search_pos()
.write(|w| unsafe { w.bits(search_position) });
}
/// Enables/disables constant time acceleration, when enabled it would
/// increases the performance of modular exponentiation by simplifying
/// the calculation concerning the 0 bits of the exponent i.e. lesser the
/// hamming weight, greater the performance. Note : this might affect
/// the security, for more info refer 18.3.4 of <https://www.espressif.com/sites/default/files/documentation/esp32-c6_technical_reference_manual_en.pdf>
pub fn enable_disable_constant_time_acceleration(&mut self, enable: bool) {
match enable {
true => self
.rsa
.constant_time()
.write(|w| w.constant_time().clear_bit()),
false => self
.rsa
.constant_time()
.write(|w| w.constant_time().set_bit()),
}
}
pub(super) fn write_modexp_start(&mut self) {
self.rsa
.set_start_modexp()
.write(|w| w.set_start_modexp().set_bit());
}
pub(super) fn write_multi_start(&mut self) {
self.rsa
.set_start_mult()
.write(|w| w.set_start_mult().set_bit());
}
fn write_modmulti_start(&mut self) {
self.rsa
.set_start_modmult()
.write(|w| w.set_start_modmult().set_bit());
}
pub(super) fn clear_interrupt(&mut self) {
self.rsa.int_clr().write(|w| w.clear_interrupt().set_bit());
}
pub(super) fn is_idle(&mut self) -> bool {
self.rsa.query_idle().read().query_idle().bit_is_set()
}
unsafe fn write_multi_operand_b<const N: usize>(&mut self, operand_b: &[u32; N]) {
copy_nonoverlapping(operand_b.as_ptr(), self.rsa.z_mem(0).as_ptr().add(N), N);
}
}
pub mod operand_sizes {
//! Marker types for the operand sizes
use paste::paste;
use super::{implement_op, Multi, RsaMode};
implement_op!(
(32, multi),
(64, multi),
(96, multi),
(128, multi),
(160, multi),
(192, multi),
(224, multi),
(256, multi),
(288, multi),
(320, multi),
(352, multi),
(384, multi),
(416, multi),
(448, multi),
(480, multi),
(512, multi),
(544, multi),
(576, multi),
(608, multi),
(640, multi),
(672, multi),
(704, multi),
(736, multi),
(768, multi),
(800, multi),
(832, multi),
(864, multi),
(896, multi),
(928, multi),
(960, multi),
(992, multi),
(1024, multi),
(1056, multi),
(1088, multi),
(1120, multi),
(1152, multi),
(1184, multi),
(1216, multi),
(1248, multi),
(1280, multi),
(1312, multi),
(1344, multi),
(1376, multi),
(1408, multi),
(1440, multi),
(1472, multi),
(1504, multi),
(1536, multi),
(1568),
(1600),
(1632),
(1664),
(1696),
(1728),
(1760),
(1792),
(1824),
(1856),
(1888),
(1920),
(1952),
(1984),
(2016),
(2048),
(2080),
(2112),
(2144),
(2176),
(2208),
(2240),
(2272),
(2304),
(2336),
(2368),
(2400),
(2432),
(2464),
(2496),
(2528),
(2560),
(2592),
(2624),
(2656),
(2688),
(2720),
(2752),
(2784),
(2816),
(2848),
(2880),
(2912),
(2944),
(2976),
(3008),
(3040),
(3072)
);
}
impl<'a, 'd, T: RsaMode, const N: usize> RsaModularExponentiation<'a, 'd, T>
where
T: RsaMode<InputType = [u32; N]>,
{
/// Creates an Instance of `RsaModularExponentiation`.
/// `m_prime` could be calculated using `-(modular multiplicative inverse of
/// modulus) mod 2^32`, for more information check 19.3.1 in the
/// <https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf>
pub fn new(
rsa: &'a mut Rsa<'d>,
exponent: &T::InputType,
modulus: &T::InputType,
m_prime: u32,
) -> Self {
Self::set_mode(rsa);
unsafe {
rsa.write_operand_b(exponent);
rsa.write_modulus(modulus);
}
rsa.write_mprime(m_prime);
if rsa.is_search_enabled() {
rsa.write_search_position(Self::find_search_pos(exponent));
}
Self {
rsa,
phantom: PhantomData,
}
}
fn find_search_pos(exponent: &T::InputType) -> u32 {
for (i, byte) in exponent.iter().rev().enumerate() {
if *byte == 0 {
continue;
}
return (exponent.len() * 32) as u32 - (byte.leading_zeros() + i as u32 * 32) - 1;
}
0
}
pub(super) fn set_mode(rsa: &mut Rsa) {
rsa.write_mode((N - 1) as u32)
}
pub(super) fn set_start(&mut self) {
self.rsa.write_modexp_start();
}
}
impl<'a, 'd, T: RsaMode, const N: usize> RsaModularMultiplication<'a, 'd, T>
where
T: RsaMode<InputType = [u32; N]>,
{
fn write_mode(rsa: &mut Rsa) {
rsa.write_mode((N - 1) as u32)
}
/// Creates an Instance of `RsaModularMultiplication`.
/// `m_prime` could be calculated using `-(modular multiplicative inverse of
/// modulus) mod 2^32`, for more information check 19.3.1 in the
/// <https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf>
pub fn new(
rsa: &'a mut Rsa<'d>,
operand_a: &T::InputType,
operand_b: &T::InputType,
modulus: &T::InputType,
m_prime: u32,
) -> Self {
Self::write_mode(rsa);
rsa.write_mprime(m_prime);
unsafe {
rsa.write_modulus(modulus);
rsa.write_operand_a(operand_a);
rsa.write_operand_b(operand_b);
}
Self {
rsa,
phantom: PhantomData,
}
}
/// Starts the modular multiplication operation. `r` could be calculated
/// using `2 ^ ( bitlength * 2 ) mod modulus`, for more information
/// check 19.3.1 in the <https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf>
pub fn start_modular_multiplication(&mut self, r: &T::InputType) {
unsafe {
self.rsa.write_r(r);
}
self.set_start();
}
fn set_start(&mut self) {
self.rsa.write_modmulti_start();
}
}
impl<'a, 'd, T: RsaMode + Multi, const N: usize> RsaMultiplication<'a, 'd, T>
where
T: RsaMode<InputType = [u32; N]>,
{
/// Creates an Instance of `RsaMultiplication`.
pub fn new(rsa: &'a mut Rsa<'d>, operand_a: &T::InputType) -> Self {
Self::set_mode(rsa);
unsafe {
rsa.write_operand_a(operand_a);
}
Self {
rsa,
phantom: PhantomData,
}
}
/// Starts the multiplication operation.
pub fn start_multiplication(&mut self, operand_b: &T::InputType) {
unsafe {
self.rsa.write_multi_operand_b(operand_b);
}
self.set_start();
}
pub(super) fn set_mode(rsa: &mut Rsa) {
rsa.write_mode((N * 2 - 1) as u32)
}
pub(super) fn set_start(&mut self) {
self.rsa.write_multi_start();
}
}