Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and fix async RSA #2002

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove `fn free(self)` in HMAC which goes against esp-hal API guidelines (#1972)
- PARL_IO use ReadBuffer and WriteBuffer for Async DMA (#1996)
- `AnyPin`, `AnyInputOnyPin` and `DummyPin` are now accessible from `gpio` module (#1918)
- Changed the RSA modular multiplication API to be consistent across devices (#2002)

### Fixed

Expand All @@ -45,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Reset peripherals in driver constructors where missing (#1893, #1961)
- Fixed ESP32-S2 systimer interrupts (#1979)
- Software interrupt 3 is no longer available when it is required by `esp-hal-embassy`. (#2011)
- ESP32: Fixed async RSA (#2002)

### Removed

Expand Down
138 changes: 20 additions & 118 deletions esp-hal/src/rsa/esp32.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use core::{
convert::Infallible,
marker::PhantomData,
ptr::{copy_nonoverlapping, write_bytes},
};
use core::convert::Infallible;

use crate::rsa::{
implement_op,
Expand Down Expand Up @@ -37,35 +33,30 @@ impl<'d, DM: crate::Mode> Rsa<'d, DM> {
}

/// Starts the modular exponentiation operation.
pub(super) fn write_modexp_start(&mut self) {
pub(super) fn write_modexp_start(&self) {
self.rsa
.modexp_start()
.write(|w| w.modexp_start().set_bit());
}

/// Starts the multiplication operation.
pub(super) fn write_multi_start(&mut self) {
pub(super) fn write_multi_start(&self) {
self.rsa.mult_start().write(|w| w.mult_start().set_bit());
}

/// Starts the modular multiplication operation.
pub(super) fn write_modmulti_start(&self) {
self.write_multi_start();
}

/// Clears the RSA interrupt flag.
pub(super) fn clear_interrupt(&mut self) {
self.rsa.interrupt().write(|w| w.interrupt().set_bit());
}

/// Checks if the RSA peripheral is idle.
pub(super) fn is_idle(&mut self) -> bool {
self.rsa.interrupt().read().bits() == 1
}

unsafe fn write_multi_operand_a<const N: usize>(&mut self, operand_a: &[u32; N]) {
copy_nonoverlapping(operand_a.as_ptr(), self.rsa.x_mem(0).as_ptr(), N);
write_bytes(self.rsa.x_mem(0).as_ptr().add(N), 0, N);
}

unsafe fn write_multi_operand_b<const N: usize>(&mut self, operand_b: &[u32; N]) {
write_bytes(self.rsa.z_mem(0).as_ptr(), 0, N);
copy_nonoverlapping(operand_b.as_ptr(), self.rsa.z_mem(0).as_ptr().add(N), N);
pub(super) fn is_idle(&self) -> bool {
self.rsa.interrupt().read().interrupt().bit_is_set()
}
}

Expand All @@ -92,130 +83,41 @@ impl<'a, 'd, T: RsaMode, DM: crate::Mode, const N: usize> RsaModularMultiplicati
where
T: RsaMode<InputType = [u32; N]>,
{
/// Creates an instance of `RsaMultiplication`.
///
/// `m_prime` can be calculated using `-(modular multiplicative inverse of
/// modulus) mod 2^32`.
///
/// For more information refer to 24.3.2 of <https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf>.
pub fn new(rsa: &'a mut Rsa<'d, DM>, modulus: &T::InputType, m_prime: u32) -> Self {
Self::set_mode(rsa);
unsafe {
rsa.write_modulus(modulus);
}
rsa.write_mprime(m_prime);

Self {
rsa,
phantom: PhantomData,
}
}

fn set_mode(rsa: &mut Rsa<'d, DM>) {
pub(super) fn write_mode(rsa: &mut Rsa<'d, DM>) {
rsa.write_multi_mode((N / 16 - 1) as u32)
}

/// Starts the first step of modular multiplication operation.
///
/// `r` can be calculated using `2 ^ ( bitlength * 2 ) mod modulus`.
/// Starts the modular multiplication operation.
///
/// For more information refer to 24.3.2 of <https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf>.
pub fn start_step1(&mut self, operand_a: &T::InputType, r: &T::InputType) {
unsafe {
self.rsa.write_operand_a(operand_a);
self.rsa.write_r(r);
}
self.start();
}

/// Starts the second step of modular multiplication operation.
///
/// This is a non blocking function that returns without an error if
/// operation is completed successfully. `start_step1` must be called
/// before calling this function.
pub fn start_step2(&mut self, operand_b: &T::InputType) {
while !self.rsa.is_idle() {}

self.rsa.clear_interrupt();
unsafe {
self.rsa.write_operand_a(operand_b);
}
self.start();
}

fn start(&mut self) {
pub(super) fn set_up_modular_multiplication(&mut self, operand_b: &T::InputType) {
self.rsa.write_multi_start();
self.rsa.wait_for_idle();

self.rsa.write_operand_a(operand_b);
}
}

impl<'a, 'd, T: RsaMode, DM: crate::Mode, const N: usize> RsaModularExponentiation<'a, 'd, T, DM>
where
T: RsaMode<InputType = [u32; N]>,
{
/// Creates an instance of `RsaModularExponentiation`.
///
/// `m_prime` can be calculated using `-(modular multiplicative inverse of
/// modulus) mod 2^32`.
///
/// For more information refer to 24.3.2 of <https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf>.
pub fn new(
rsa: &'a mut Rsa<'d, DM>,
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);
Self {
rsa,
phantom: PhantomData,
}
}

/// Sets the modular exponentiation mode for the RSA hardware.
pub(super) fn set_mode(rsa: &mut Rsa<'d, DM>) {
pub(super) fn write_mode(rsa: &mut Rsa<'d, DM>) {
rsa.write_modexp_mode((N / 16 - 1) as u32)
}

/// Starts the modular exponentiation operation on the RSA hardware.
pub(super) fn start(&mut self) {
self.rsa.write_modexp_start();
}
}

impl<'a, 'd, T: RsaMode + Multi, DM: crate::Mode, const N: usize> RsaMultiplication<'a, 'd, T, DM>
where
T: RsaMode<InputType = [u32; N]>,
{
/// Creates an instance of `RsaMultiplication`.
pub fn new(rsa: &'a mut Rsa<'d, DM>) -> Self {
Self::set_mode(rsa);
Self {
rsa,
phantom: PhantomData,
}
}

/// Starts the multiplication operation.
pub fn start_multiplication(&mut self, operand_a: &T::InputType, operand_b: &T::InputType) {
unsafe {
self.rsa.write_multi_operand_a(operand_a);
self.rsa.write_multi_operand_b(operand_b);
}
self.start();
}

/// Sets the multiplication mode for the RSA hardware.
pub(super) fn set_mode(rsa: &mut Rsa<'d, DM>) {
pub(super) fn write_mode(rsa: &mut Rsa<'d, DM>) {
rsa.write_multi_mode(((N * 2) / 16 + 7) as u32)
}

/// Starts the multiplication operation on the RSA hardware.
pub(super) fn start(&mut self) {
self.rsa.write_multi_start();
pub(super) fn set_up_multiplication(&mut self, operand_b: &T::InputType) {
self.rsa.write_multi_operand_b(operand_b);
}
}
121 changes: 13 additions & 108 deletions esp-hal/src/rsa/esp32cX.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{convert::Infallible, marker::PhantomData, ptr::copy_nonoverlapping};
use core::convert::Infallible;

use crate::rsa::{
implement_op,
Expand Down Expand Up @@ -94,21 +94,21 @@ impl<'d, DM: crate::Mode> Rsa<'d, DM> {
}

/// Starts the modular exponentiation operation.
pub(super) fn write_modexp_start(&mut self) {
pub(super) fn write_modexp_start(&self) {
self.rsa
.set_start_modexp()
.write(|w| w.set_start_modexp().set_bit());
}

/// Starts the multiplication operation.
pub(super) fn write_multi_start(&mut self) {
pub(super) fn write_multi_start(&self) {
self.rsa
.set_start_mult()
.write(|w| w.set_start_mult().set_bit());
}

/// Starts the modular multiplication operation.
fn write_modmulti_start(&mut self) {
pub(super) fn write_modmulti_start(&self) {
self.rsa
.set_start_modmult()
.write(|w| w.set_start_modmult().set_bit());
Expand All @@ -120,13 +120,9 @@ impl<'d, DM: crate::Mode> Rsa<'d, DM> {
}

/// Checks if the RSA peripheral is idle.
pub(super) fn is_idle(&mut self) -> bool {
pub(super) fn is_idle(&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);
}
}

/// Module defining marker types for various RSA operand sizes.
Expand Down Expand Up @@ -240,34 +236,7 @@ impl<'a, 'd, T: RsaMode, DM: crate::Mode, const N: usize> RsaModularExponentiati
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 refer to 19.3.1 of <https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf>.
pub fn new(
rsa: &'a mut Rsa<'d, DM>,
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 {
pub(super) fn find_search_pos(exponent: &T::InputType) -> u32 {
for (i, byte) in exponent.iter().rev().enumerate() {
if *byte == 0 {
continue;
Expand All @@ -278,98 +247,34 @@ where
}

/// Sets the modular exponentiation mode for the RSA hardware.
pub(super) fn set_mode(rsa: &mut Rsa<'d, DM>) {
pub(super) fn write_mode(rsa: &mut Rsa<'d, DM>) {
rsa.write_mode((N - 1) as u32)
}

/// Starts the modular exponentiation operation on the RSA hardware.
pub(super) fn start(&mut self) {
self.rsa.write_modexp_start();
}
}

impl<'a, 'd, T: RsaMode, DM: crate::Mode, const N: usize> RsaModularMultiplication<'a, 'd, T, DM>
where
T: RsaMode<InputType = [u32; N]>,
{
fn write_mode(rsa: &mut Rsa<'d, DM>) {
pub(super) fn write_mode(rsa: &mut Rsa<'d, DM>) {
rsa.write_mode((N - 1) as u32)
}

/// Creates an instance of `RsaModularMultiplication`.
///
/// `m_prime` can be calculated using `-(modular multiplicative inverse of
/// modulus) mod 2^32`.
///
/// For more information refer to 19.3.1 of <https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf>.
pub fn new(
rsa: &'a mut Rsa<'d, DM>,
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 refer to 19.3.1 of <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.start();
}

fn start(&mut self) {
self.rsa.write_modmulti_start();
pub(super) fn set_up_modular_multiplication(&mut self, operand_b: &T::InputType) {
self.rsa.write_operand_b(operand_b);
}
}

impl<'a, 'd, T: RsaMode + Multi, DM: crate::Mode, const N: usize> RsaMultiplication<'a, 'd, T, DM>
where
T: RsaMode<InputType = [u32; N]>,
{
/// Creates an instance of `RsaMultiplication`.
pub fn new(rsa: &'a mut Rsa<'d, DM>, 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.start();
pub(super) fn set_up_multiplication(&mut self, operand_b: &T::InputType) {
self.rsa.write_multi_operand_b(operand_b);
}

/// Sets the multiplication mode for the RSA hardware.
pub(super) fn set_mode(rsa: &mut Rsa<'d, DM>) {
pub(super) fn write_mode(rsa: &mut Rsa<'d, DM>) {
rsa.write_mode((N * 2 - 1) as u32)
}

/// Starts the multiplication operation on the RSA hardware.
pub(super) fn start(&mut self) {
self.rsa.write_multi_start();
}
}
Loading