Skip to content

Commit f8f36b8

Browse files
continued saes development, finished key wrapping. tbd: RNG Busy flag
1 parent f020c7f commit f8f36b8

3 files changed

Lines changed: 59 additions & 57 deletions

File tree

boards/nucleo_u545re_q/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,12 @@ unsafe fn start() -> (
273273
let aes_cbc_src = static_init!([u8; 64], [0; 64]);
274274
let aes_cbc_dst = static_init!([u8; 96], [0; 96]);
275275
let aes_cbc_test = static_init!(
276-
capsules_extra::test::aes256::TestAES256Cbc<'static, stm32u545::aes::Aes<'static, AES256>>,
276+
capsules_extra::test::aes256::TestAES256Cbc<
277+
'static,
278+
stm32u545::saes::Saes<'static, AES256>,
279+
>,
277280
capsules_extra::test::aes256::TestAES256Cbc::new(
278-
aes,
281+
saes,
279282
aes_cbc_key,
280283
aes_cbc_iv,
281284
aes_cbc_src,

chips/stm32u5xx/src/rcc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Rcc {
106106
}
107107

108108
pub fn enable_saes(&self) {
109-
self.registers.ahb2enr1.modify(AHB2ENR1::AESEN::SET);
109+
self.registers.ahb2enr1.modify(AHB2ENR1::SAESEN::SET);
110110
}
111111

112112
pub fn enable_syscfg(&self) {

chips/stm32u5xx/src/saes.rs

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::cell::Cell;
22
use core::marker::PhantomData;
3+
use kernel::debug;
34
use kernel::errorcode::ErrorCode;
45
use kernel::hil::symmetric_encryption::{
56
AESKey, AESKeySize, AES, AES128_KEY_SIZE, AES_BLOCK_SIZE, AES_IV_SIZE,
@@ -179,6 +180,26 @@ enum KeyID {
179180
XOR,
180181
}
181182

183+
impl KeyID {
184+
fn to_bits(&self) -> registers::FieldValue<u32, CR::Register> {
185+
match self {
186+
KeyID::DHUK => CR::KEYSEL::DHUK,
187+
KeyID::BHK => CR::KEYSEL::BHK,
188+
KeyID::XOR => CR::KEYSEL::XOR_DHUK_BHK,
189+
}
190+
}
191+
}
192+
193+
impl From<usize> for KeyID {
194+
fn from(value: usize) -> Self {
195+
match value {
196+
0 => KeyID::DHUK,
197+
1 => KeyID::BHK,
198+
2.. => KeyID::XOR,
199+
}
200+
}
201+
}
202+
182203
#[derive(Copy, Clone, PartialEq, Debug)]
183204
enum DeferredOp {
184205
None,
@@ -197,9 +218,9 @@ struct CryptoContext {
197218
#[derive(Copy, Clone, PartialEq, Debug)]
198219
enum State {
199220
Idle,
200-
KeyPreparation(DeferredOp),
221+
KeyPrep(DeferredOp),
201222
Crypt(CryptoContext),
202-
KeyWrapping(KeyID),
223+
KeyWrap(KeyID),
203224
}
204225

205226
pub struct Saes<'a, K: AESKeySize> {
@@ -282,7 +303,7 @@ impl<'a, K: AESKeySize> Saes<'a, K> {
282303

283304
/// Function for ECB and CBC decryption modes which goes though Key derivation operation
284305
fn prepare_decryption_key(&self, key: &[u8]) {
285-
self.state.set(State::KeyPreparation(DeferredOp::None));
306+
self.state.set(State::KeyPrep(DeferredOp::None));
286307
self.registers.cr.modify(CR::EN::CLEAR);
287308
self.registers.cr.modify(CR::MODE::KeyDerivation);
288309

@@ -346,13 +367,7 @@ impl<'a, K: AESKeySize> Saes<'a, K> {
346367
self.write_input(ctx);
347368
}
348369

349-
fn start_key_wrapping(
350-
&self,
351-
source: Option<&'static mut [u8]>,
352-
dest: &'static mut [u8],
353-
ctx: CryptoContext,
354-
key_id: KeyID,
355-
) {
370+
fn start_key_wrapping(&self, ctx: CryptoContext, key_id: KeyID) {
356371
let regs = self.registers;
357372

358373
match K::LENGTH {
@@ -366,26 +381,16 @@ impl<'a, K: AESKeySize> Saes<'a, K> {
366381
}
367382

368383
regs.cr.modify(CR::KMOD::WRAPPED);
369-
370-
match key_id {
371-
KeyID::DHUK => regs.cr.modify(CR::KEYSEL::DHUK),
372-
KeyID::BHK => regs.cr.modify(CR::KEYSEL::BHK),
373-
KeyID::XOR => regs.cr.modify(CR::KEYSEL::XOR_DHUK_BHK),
374-
}
375-
384+
regs.cr.modify(key_id.to_bits());
376385
regs.cr.modify(CR::EN::SET);
377386

378-
if let Some(src) = source {
379-
self.input.replace(src);
380-
}
381-
self.output.replace(dest);
382387
self.write_input(ctx);
383388
self.state.set(State::Crypt(ctx));
384389
}
385390

386391
fn computation_complete(&self) {
387392
match self.state.get() {
388-
State::KeyPreparation(deferred_op) => {
393+
State::KeyPrep(deferred_op) => {
389394
self.registers.cr.modify(CR::EN::CLEAR);
390395
self.registers.cr.modify(CR::MODE::Decrypt);
391396
match deferred_op {
@@ -437,19 +442,23 @@ impl<'a, K: AESKeySize> Saes<'a, K> {
437442

438443
pub fn handle_interrupt(&self) {
439444
if self.registers.isr.is_set(ISR::CCF) {
445+
debug!("CCF");
440446
self.registers.icr.write(ICR::CCF::SET);
441447
self.computation_complete();
442448
}
443449

444450
if self.registers.isr.is_set(ISR::RWEIF) {
451+
debug!("RWEIF");
445452
self.registers.icr.write(ICR::RWEIF::SET);
446453
}
447454

448455
if self.registers.isr.is_set(ISR::KEIF) {
456+
debug!("KEIF");
449457
self.registers.icr.write(ICR::KEIF::SET);
450458
}
451459

452460
if self.registers.isr.is_set(ISR::RNGEIF) {
461+
debug!("RNGEIF");
453462
self.registers.icr.write(ICR::RNGEIF::SET);
454463
}
455464
}
@@ -462,6 +471,8 @@ impl<'a, K: AESKeySize> kernel::hil::symmetric_encryption::AES<'a, K> for Saes<'
462471
self.registers.cr.modify(CR::DATATYPE::Byte);
463472
self.state.set(State::Idle);
464473
self.enable_interrupts();
474+
debug!("CR: {:02x?}", self.registers.cr.get());
475+
debug!("SR: {:02x?}", self.registers.sr.get());
465476
}
466477

467478
fn disable(&self) {
@@ -484,14 +495,13 @@ impl<'a, K: AESKeySize> kernel::hil::symmetric_encryption::AES<'a, K> for Saes<'
484495
let key = match key {
485496
AESKey::PlainText(key) => key,
486497
AESKey::Id(key_id) => {
487-
// 0, 1 and 2 are valid key ids
488-
match key_id {
489-
0 => self.state.set(State::KeyWrapping(KeyID::DHUK)),
490-
1 => self.state.set(State::KeyWrapping(KeyID::BHK)),
491-
2 => self.state.set(State::KeyWrapping(KeyID::XOR)),
492-
_ => {
493-
return Err(ErrorCode::INVAL);
494-
}
498+
// 0, 1 and 2 are valid key ids. Also, to unwrap a key, AESKey::Wrapped should be used
499+
if key_id > 2 || !self.encrypting.get() {
500+
return Err(ErrorCode::INVAL);
501+
}
502+
let id = KeyID::from(key_id);
503+
if self.encrypting.get() {
504+
self.state.set(State::KeyWrap(id));
495505
}
496506
return Ok(());
497507
}
@@ -512,7 +522,7 @@ impl<'a, K: AESKeySize> kernel::hil::symmetric_encryption::AES<'a, K> for Saes<'
512522
self.registers.cr.modify(CR::KEYPROT::ALLOW_TRASNFER);
513523
self.registers.cr.modify(CR::KMOD::NORMAL);
514524

515-
if self.encrypting.get() {
525+
if !self.encrypting.get() {
516526
self.prepare_decryption_key(key);
517527
} else {
518528
self.write_key_registers(key);
@@ -532,9 +542,9 @@ impl<'a, K: AESKeySize> kernel::hil::symmetric_encryption::AES<'a, K> for Saes<'
532542

533543
match self.state.get() {
534544
State::Idle => self.write_iv_registers(iv.try_into().unwrap()),
535-
State::KeyPreparation(_) => {
545+
State::KeyPrep(_) => {
536546
self.iv.set(iv.try_into().unwrap());
537-
self.state.set(State::KeyPreparation(DeferredOp::WriteIvx));
547+
self.state.set(State::KeyPrep(DeferredOp::WriteIvx));
538548
}
539549
_ => return Err(ErrorCode::BUSY),
540550
}
@@ -558,12 +568,7 @@ impl<'a, K: AESKeySize> kernel::hil::symmetric_encryption::AES<'a, K> for Saes<'
558568
let state = self.state.get();
559569

560570
// Hardware busy check
561-
if self.output.is_some()
562-
|| !matches!(
563-
state,
564-
State::Idle | State::KeyPreparation(_) | State::KeyWrapping(_)
565-
)
566-
{
571+
if self.output.is_some() {
567572
return Some((Err(ErrorCode::BUSY), source, dest));
568573
}
569574

@@ -581,34 +586,28 @@ impl<'a, K: AESKeySize> kernel::hil::symmetric_encryption::AES<'a, K> for Saes<'
581586
return Some((Err(ErrorCode::INVAL), source, dest));
582587
}
583588

589+
// if we are in key wrapping mode, the input must be a key of size K::LENGTH
590+
if matches!(state, State::KeyWrap(_)) && stop_index - start_index != K::LENGTH {
591+
return Some((Err(ErrorCode::INVAL), source, dest));
592+
}
593+
584594
let ctx = CryptoContext {
585595
start_index,
586596
stop_index,
587597
curr_index: 0,
588598
using_dma: false,
589599
};
590600

591-
// if we are in key wrapping mode, the input must be a key of size K::LENGTH
592-
if let State::KeyWrapping(key_id) = state {
593-
if stop_index - start_index != K::LENGTH {
594-
return Some((Err(ErrorCode::INVAL), source, dest));
595-
}
596-
self.start_key_wrapping(source, dest, ctx, key_id);
597-
return None;
598-
}
599601
if let Some(src) = source {
600602
self.input.replace(src);
601603
}
602604
self.output.replace(dest);
603605

604-
if let State::KeyPreparation(_) = state {
605-
self.state
606-
.set(State::KeyPreparation(DeferredOp::Crypt(ctx)));
607-
return None;
606+
match state {
607+
State::KeyWrap(key_id) => self.start_key_wrapping(ctx, key_id),
608+
State::KeyPrep(_) => self.state.set(State::KeyPrep(DeferredOp::Crypt(ctx))),
609+
_ => self.start_crypt(ctx),
608610
}
609-
610-
self.start_crypt(ctx);
611-
612611
None
613612
}
614613
}

0 commit comments

Comments
 (0)