Skip to content

Commit b8e13ec

Browse files
committed
stm32u5xx: hash driver
1 parent 6f4bb8c commit b8e13ec

36 files changed

Lines changed: 4196 additions & 63 deletions

File tree

boards/components/src/hmac.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl<
6262
A: kernel::hil::digest::HmacSha256
6363
+ digest::HmacSha384
6464
+ digest::HmacSha512
65+
+ digest::HmacMd5
66+
+ digest::HmacSha224
67+
+ digest::HmacSha1
6568
+ 'static
6669
+ digest::Digest<'static, L>,
6770
const L: usize,

boards/components/src/sha.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ impl<
6767
A: kernel::hil::digest::Sha256
6868
+ digest::Sha384
6969
+ digest::Sha512
70+
+ digest::Md5
71+
+ digest::Sha1
72+
+ digest::Sha224
7073
+ 'static
7174
+ digest::Digest<'static, L>,
7275
const L: usize,

boards/configurations/nrf52840dk/nrf52840dk-test-kernel/src/test/hmac_sha256_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub static mut WIKI_HMAC: [u8; 32] = [
3030

3131
unsafe fn static_init_test_hmacsha256(
3232
client: &'static dyn CapsuleTestClient,
33-
) -> &'static TestHmacSha256 {
33+
) -> &'static TestHmacSha256<'static, HmacSha256Software<'static, Sha256Software<'static>>> {
3434
let sha256_hash_buf = static_init!([u8; 64], [0; 64]);
3535

3636
let sha256 = static_init!(Sha256Software<'static>, Sha256Software::new());
@@ -45,7 +45,7 @@ unsafe fn static_init_test_hmacsha256(
4545
kernel::hil::digest::Digest::set_client(sha256, hmacsha256);
4646

4747
let test = static_init!(
48-
TestHmacSha256,
48+
TestHmacSha256<'static, HmacSha256Software<'static, Sha256Software<'static>>>,
4949
TestHmacSha256::new(
5050
hmacsha256,
5151
&mut *addr_of_mut!(WIKI_KEY),

boards/configurations/nrf52840dk/nrf52840dk-test-kernel/src/test/sha256_test.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ pub static mut LHASH: [u8; 32] = [
4545
0x45, 0x94, 0xd5, 0xee, 0x15, 0xcb, 0x8a, 0x1e, 0x28, 0x7c, 0x20, 0x12, 0xc2, 0xce, 0xb5, 0xa9,
4646
];
4747

48-
unsafe fn static_init_test_sha256(client: &'static dyn CapsuleTestClient) -> &'static TestSha256 {
48+
unsafe fn static_init_test_sha256(
49+
client: &'static dyn CapsuleTestClient,
50+
) -> &'static TestSha256<'static, Sha256Software<'static>> {
4951
let sha = static_init!(Sha256Software<'static>, Sha256Software::new());
5052
kernel::deferred_call::DeferredCallClient::register(sha);
5153
let bytes = b"hello ";
@@ -56,7 +58,7 @@ unsafe fn static_init_test_sha256(client: &'static dyn CapsuleTestClient) -> &'s
5658
}
5759
// We expect LSTRING to hash to LHASH, so final argument is true
5860
let test = static_init!(
59-
TestSha256,
61+
TestSha256<Sha256Software>,
6062
TestSha256::new(
6163
sha,
6264
&mut *addr_of_mut!(LSTRING),

boards/imix/src/test/sha256_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub static mut LHASH: [u8; 32] = [
4747
0x45, 0x94, 0xd5, 0xee, 0x15, 0xcb, 0x8a, 0x1e, 0x28, 0x7c, 0x20, 0x12, 0xc2, 0xce, 0xb5, 0xa9,
4848
];
4949

50-
unsafe fn static_init_test_sha256() -> &'static TestSha256 {
50+
unsafe fn static_init_test_sha256() -> &'static TestSha256<'static, Sha256Software<'static>> {
5151
let sha = static_init!(Sha256Software<'static>, Sha256Software::new());
5252
kernel::deferred_call::DeferredCallClient::register(sha);
5353
let bytes = b"hello ";
@@ -58,7 +58,7 @@ unsafe fn static_init_test_sha256() -> &'static TestSha256 {
5858
}
5959
// We expect LSTRING to hash to LHASH, so final argument is true
6060
let test = static_init!(
61-
TestSha256,
61+
TestSha256<'static, Sha256Software<'static>>,
6262
TestSha256::new(
6363
sha,
6464
&mut *addr_of_mut!(LSTRING),

boards/nucleo_u545re_q/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ flash: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).elf
4545

4646
.PHONY: program
4747
program:
48-
# Note: tockloader does not natively wrap probe-rs yet.
48+
# Note: tockloader does not natively wrap probe-rs yet.
4949
# The --openocd flag was removed; tockloader will use its default method (usually serial/jlink).
5050
tockloader install --board nucleo_u545re_q $(APP)

boards/nucleo_u545re_q/src/main.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![no_std]
77
#![no_main]
88

9+
use components::hmac_component_static;
910
use kernel::capabilities;
1011
use kernel::component::Component;
1112
use kernel::debug::PanicResources;
@@ -52,6 +53,11 @@ struct NucleoU545RE {
5253
stm32u545::tim::Tim2<'static>,
5354
>,
5455
>,
56+
hmac: &'static capsules_extra::hmac::HmacDriver<
57+
'static,
58+
stm32u545::hash::sha256::Sha256Adapter<'static>,
59+
32,
60+
>,
5561
}
5662

5763
impl SyscallDriverLookup for NucleoU545RE {
@@ -64,6 +70,7 @@ impl SyscallDriverLookup for NucleoU545RE {
6470
capsules_core::led::DRIVER_NUM => f(Some(self.led)),
6571
capsules_core::button::DRIVER_NUM => f(Some(self.button)),
6672
capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
73+
capsules_extra::hmac::DRIVER_NUM => f(Some(self.hmac)),
6774
_ => f(None),
6875
}
6976
}
@@ -152,10 +159,17 @@ unsafe fn start() -> (
152159
);
153160
usart1.register();
154161

162+
let hash = static_init!(
163+
stm32u545::hash::hash::Hash<'static>,
164+
stm32u545::hash::hash::Hash::new(stm32u545::hash::regs::HASH_BASE)
165+
);
166+
167+
hash.register();
168+
155169
// Load Peripherals Bundle
156170
let periphs = static_init!(
157171
stm32u545::chip::Stm32u5xxDefaultPeripherals<'static>,
158-
stm32u545::chip::Stm32u5xxDefaultPeripherals::new(usart1, exti, dma1)
172+
stm32u545::chip::Stm32u5xxDefaultPeripherals::new(usart1, exti, dma1, hash)
159173
);
160174

161175
// Initialize wiring (DMA, clocks)
@@ -165,6 +179,16 @@ unsafe fn start() -> (
165179
periphs.tim2.start();
166180
set_pin_primary_functions(periphs);
167181

182+
// Create an adapter for the HASH peripheral.
183+
// In this way it is ensured that only one mode is used by the peripheral.
184+
let sha256 = static_init!(
185+
stm32u545::hash::sha256::Sha256Adapter<'static>,
186+
stm32u545::hash::sha256::Sha256Adapter::new(hash)
187+
);
188+
189+
// Adapter receives callbacks from the peripheral
190+
let _ = hash.set_sha256_adapter(sha256);
191+
168192
// Kernel and Muxes
169193
let processes = components::process_array::ProcessArrayComponent::new()
170194
.finalize(components::process_array_component_static!(NUM_PROCS));
@@ -233,6 +257,16 @@ unsafe fn start() -> (
233257
)
234258
.finalize(components::button_component_static!(stm32u545::gpio::Pin));
235259

260+
let hmac = components::hmac::HmacComponent::new(
261+
board_kernel,
262+
capsules_extra::hmac::DRIVER_NUM,
263+
sha256,
264+
)
265+
.finalize(hmac_component_static!(
266+
stm32u545::hash::sha256::Sha256Adapter<'static>,
267+
32
268+
));
269+
236270
// Platform and Interrupts
237271
let platform = static_init!(
238272
NucleoU545RE,
@@ -244,6 +278,7 @@ unsafe fn start() -> (
244278
led,
245279
button,
246280
alarm,
281+
hmac
247282
}
248283
);
249284

boards/opentitan/earlgrey-cw310/src/tests/sha256soft_test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use crate::tests::run_kernel_op;
88
use crate::SHA256SOFT;
9+
use capsules_extra::sha256::Sha256Software;
910
use capsules_extra::test::sha256::TestSha256;
1011
use kernel::debug;
1112
use kernel::static_init;
@@ -36,7 +37,12 @@ fn sha256software_verify() {
3637
)
3738
};
3839

39-
let test = unsafe { static_init!(TestSha256, TestSha256::new(sha, lstring, lhash, true)) };
40+
let test = unsafe {
41+
static_init!(
42+
TestSha256<'static, Sha256Software<'static>>,
43+
TestSha256::new(sha, lstring, lhash, true)
44+
)
45+
};
4046
test.run();
4147

4248
run_kernel_op(1000);

capsules/extra/src/hmac.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ use kernel::utilities::leasable_buffer::SubSliceMut;
6060
use kernel::{ErrorCode, ProcessId};
6161

6262
enum ShaOperation {
63+
Md5,
64+
Sha1,
65+
Sha224,
6366
Sha256,
6467
Sha384,
6568
Sha512,
@@ -91,6 +94,9 @@ pub struct HmacDriver<'a, H: digest::Digest<'a, DIGEST_LEN>, const DIGEST_LEN: u
9194
impl<
9295
'a,
9396
H: digest::Digest<'a, DIGEST_LEN>
97+
+ digest::HmacMd5
98+
+ digest::HmacSha1
99+
+ digest::HmacSha224
94100
+ digest::HmacSha256
95101
+ digest::HmacSha384
96102
+ digest::HmacSha512,
@@ -131,9 +137,19 @@ impl<
131137
let mut tmp_key_buffer: [u8; TMP_KEY_BUFFER_SIZE] =
132138
[0; TMP_KEY_BUFFER_SIZE];
133139
let key_len = core::cmp::min(k.len(), TMP_KEY_BUFFER_SIZE);
140+
134141
k[..key_len].copy_to_slice(&mut tmp_key_buffer[..key_len]);
135142

136143
match op {
144+
ShaOperation::Md5 => {
145+
self.hmac.set_mode_hmacmd5(&tmp_key_buffer[..key_len])
146+
}
147+
ShaOperation::Sha1 => {
148+
self.hmac.set_mode_hmacsha1(&tmp_key_buffer[..key_len])
149+
}
150+
ShaOperation::Sha224 => self
151+
.hmac
152+
.set_mode_hmacsha224(&tmp_key_buffer[..key_len]),
137153
ShaOperation::Sha256 => self
138154
.hmac
139155
.set_mode_hmacsha256(&tmp_key_buffer[..key_len]),
@@ -251,6 +267,9 @@ impl<
251267
impl<
252268
'a,
253269
H: digest::Digest<'a, DIGEST_LEN>
270+
+ digest::HmacMd5
271+
+ digest::HmacSha1
272+
+ digest::HmacSha224
254273
+ digest::HmacSha256
255274
+ digest::HmacSha384
256275
+ digest::HmacSha512,
@@ -389,6 +408,9 @@ impl<
389408
impl<
390409
'a,
391410
H: digest::Digest<'a, DIGEST_LEN>
411+
+ digest::HmacMd5
412+
+ digest::HmacSha1
413+
+ digest::HmacSha224
392414
+ digest::HmacSha256
393415
+ digest::HmacSha384
394416
+ digest::HmacSha512,
@@ -443,6 +465,9 @@ impl<
443465
impl<
444466
'a,
445467
H: digest::Digest<'a, DIGEST_LEN>
468+
+ digest::HmacMd5
469+
+ digest::HmacSha1
470+
+ digest::HmacSha224
446471
+ digest::HmacSha256
447472
+ digest::HmacSha384
448473
+ digest::HmacSha512,
@@ -496,6 +521,9 @@ impl<
496521
impl<
497522
'a,
498523
H: digest::Digest<'a, DIGEST_LEN>
524+
+ digest::HmacMd5
525+
+ digest::HmacSha1
526+
+ digest::HmacSha224
499527
+ digest::HmacSha256
500528
+ digest::HmacSha384
501529
+ digest::HmacSha512,
@@ -642,6 +670,21 @@ impl<
642670
app.sha_operation = Some(ShaOperation::Sha512);
643671
CommandReturn::success()
644672
}
673+
// SHA224
674+
3 => {
675+
app.sha_operation = Some(ShaOperation::Sha224);
676+
CommandReturn::success()
677+
}
678+
// SHA1
679+
4 => {
680+
app.sha_operation = Some(ShaOperation::Sha1);
681+
CommandReturn::success()
682+
}
683+
// Md5
684+
5 => {
685+
app.sha_operation = Some(ShaOperation::Md5);
686+
CommandReturn::success()
687+
}
645688
_ => CommandReturn::failure(ErrorCode::NOSUPPORT),
646689
}
647690
}

capsules/extra/src/hmac_sha256.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,27 @@ impl<'a, S: hil::digest::Sha256 + hil::digest::DigestDataHash<'a, 32>> hil::dige
548548
Err(ErrorCode::NOSUPPORT)
549549
}
550550
}
551+
552+
impl<'a, S: hil::digest::Sha256 + hil::digest::DigestDataHash<'a, 32>> hil::digest::HmacSha224
553+
for HmacSha256Software<'a, S>
554+
{
555+
fn set_mode_hmacsha224(&self, _key: &[u8]) -> Result<(), ErrorCode> {
556+
Err(ErrorCode::NOSUPPORT)
557+
}
558+
}
559+
560+
impl<'a, S: hil::digest::Sha256 + hil::digest::DigestDataHash<'a, 32>> hil::digest::HmacSha1
561+
for HmacSha256Software<'a, S>
562+
{
563+
fn set_mode_hmacsha1(&self, _key: &[u8]) -> Result<(), ErrorCode> {
564+
Err(ErrorCode::NOSUPPORT)
565+
}
566+
}
567+
568+
impl<'a, S: hil::digest::Sha256 + hil::digest::DigestDataHash<'a, 32>> hil::digest::HmacMd5
569+
for HmacSha256Software<'a, S>
570+
{
571+
fn set_mode_hmacmd5(&self, _key: &[u8]) -> Result<(), ErrorCode> {
572+
Err(ErrorCode::NOSUPPORT)
573+
}
574+
}

0 commit comments

Comments
 (0)