Skip to content

Commit 0b6bf1b

Browse files
committed
stm32u5xx: hash driver
1 parent 884171d commit 0b6bf1b

36 files changed

Lines changed: 4214 additions & 68 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: 35 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;
@@ -56,6 +57,11 @@ struct NucleoU545RE {
5657
>,
5758
>,
5859
gpio: &'static GpioDriver,
60+
hmac: &'static capsules_extra::hmac::HmacDriver<
61+
'static,
62+
stm32u545::hash::sha256::Sha256Adapter<'static>,
63+
32,
64+
>,
5965
}
6066

6167
impl SyscallDriverLookup for NucleoU545RE {
@@ -69,6 +75,7 @@ impl SyscallDriverLookup for NucleoU545RE {
6975
capsules_core::button::DRIVER_NUM => f(Some(self.button)),
7076
capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
7177
capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
78+
capsules_extra::hmac::DRIVER_NUM => f(Some(self.hmac)),
7279
_ => f(None),
7380
}
7481
}
@@ -157,10 +164,17 @@ unsafe fn start() -> (
157164
);
158165
usart1.register();
159166

167+
let hash = static_init!(
168+
stm32u545::hash::hash::Hash<'static>,
169+
stm32u545::hash::hash::Hash::new(stm32u545::hash::regs::HASH_BASE)
170+
);
171+
172+
hash.register();
173+
160174
// Load Peripherals Bundle
161175
let periphs = static_init!(
162176
stm32u545::chip::Stm32u5xxDefaultPeripherals<'static>,
163-
stm32u545::chip::Stm32u5xxDefaultPeripherals::new(usart1, exti, dma1)
177+
stm32u545::chip::Stm32u5xxDefaultPeripherals::new(usart1, exti, dma1, hash)
164178
);
165179

166180
// Initialize wiring (DMA, clocks)
@@ -170,6 +184,16 @@ unsafe fn start() -> (
170184
periphs.tim2.start();
171185
set_pin_primary_functions(periphs);
172186

187+
// Create an adapter for the HASH peripheral.
188+
// In this way it is ensured that only one mode is used by the peripheral.
189+
let sha256 = static_init!(
190+
stm32u545::hash::sha256::Sha256Adapter<'static>,
191+
stm32u545::hash::sha256::Sha256Adapter::new(hash)
192+
);
193+
194+
// Adapter receives callbacks from the peripheral
195+
let _ = hash.set_sha256_adapter(sha256);
196+
173197
// Kernel and Muxes
174198
let processes = components::process_array::ProcessArrayComponent::new()
175199
.finalize(components::process_array_component_static!(NUM_PROCS));
@@ -274,6 +298,15 @@ unsafe fn start() -> (
274298
),
275299
)
276300
.finalize(components::gpio_component_static!(GpioHw));
301+
let hmac = components::hmac::HmacComponent::new(
302+
board_kernel,
303+
capsules_extra::hmac::DRIVER_NUM,
304+
sha256,
305+
)
306+
.finalize(hmac_component_static!(
307+
stm32u545::hash::sha256::Sha256Adapter<'static>,
308+
32
309+
));
277310

278311
// Platform and Interrupts
279312
let platform = static_init!(
@@ -287,6 +320,7 @@ unsafe fn start() -> (
287320
button,
288321
alarm,
289322
gpio,
323+
hmac
290324
}
291325
);
292326

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ fn sha256software_verify() {
3636
)
3737
};
3838

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

4247
run_kernel_op(1000);

capsules/extra/src/hmac.rs

Lines changed: 63 additions & 5 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,
@@ -90,7 +93,13 @@ pub struct HmacDriver<'a, H: digest::Digest<'a, DIGEST_LEN>, const DIGEST_LEN: u
9093

9194
impl<
9295
'a,
93-
H: digest::Digest<'a, DIGEST_LEN> + digest::HmacSha256 + digest::HmacSha384 + digest::HmacSha512,
96+
H: digest::Digest<'a, DIGEST_LEN>
97+
+ digest::HmacMd5
98+
+ digest::HmacSha1
99+
+ digest::HmacSha224
100+
+ digest::HmacSha256
101+
+ digest::HmacSha384
102+
+ digest::HmacSha512,
94103
const DIGEST_LEN: usize,
95104
> HmacDriver<'a, H, DIGEST_LEN>
96105
{
@@ -128,9 +137,19 @@ impl<
128137
let mut tmp_key_buffer: [u8; TMP_KEY_BUFFER_SIZE] =
129138
[0; TMP_KEY_BUFFER_SIZE];
130139
let key_len = core::cmp::min(k.len(), TMP_KEY_BUFFER_SIZE);
140+
131141
k[..key_len].copy_to_slice(&mut tmp_key_buffer[..key_len]);
132142

133143
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]),
134153
ShaOperation::Sha256 => self
135154
.hmac
136155
.set_mode_hmacsha256(&tmp_key_buffer[..key_len]),
@@ -247,7 +266,13 @@ impl<
247266

248267
impl<
249268
'a,
250-
H: digest::Digest<'a, DIGEST_LEN> + digest::HmacSha256 + digest::HmacSha384 + digest::HmacSha512,
269+
H: digest::Digest<'a, DIGEST_LEN>
270+
+ digest::HmacMd5
271+
+ digest::HmacSha1
272+
+ digest::HmacSha224
273+
+ digest::HmacSha256
274+
+ digest::HmacSha384
275+
+ digest::HmacSha512,
251276
const DIGEST_LEN: usize,
252277
> digest::ClientData<DIGEST_LEN> for HmacDriver<'a, H, DIGEST_LEN>
253278
{
@@ -382,7 +407,13 @@ impl<
382407

383408
impl<
384409
'a,
385-
H: digest::Digest<'a, DIGEST_LEN> + digest::HmacSha256 + digest::HmacSha384 + digest::HmacSha512,
410+
H: digest::Digest<'a, DIGEST_LEN>
411+
+ digest::HmacMd5
412+
+ digest::HmacSha1
413+
+ digest::HmacSha224
414+
+ digest::HmacSha256
415+
+ digest::HmacSha384
416+
+ digest::HmacSha512,
386417
const DIGEST_LEN: usize,
387418
> digest::ClientHash<DIGEST_LEN> for HmacDriver<'a, H, DIGEST_LEN>
388419
{
@@ -433,7 +464,13 @@ impl<
433464

434465
impl<
435466
'a,
436-
H: digest::Digest<'a, DIGEST_LEN> + digest::HmacSha256 + digest::HmacSha384 + digest::HmacSha512,
467+
H: digest::Digest<'a, DIGEST_LEN>
468+
+ digest::HmacMd5
469+
+ digest::HmacSha1
470+
+ digest::HmacSha224
471+
+ digest::HmacSha256
472+
+ digest::HmacSha384
473+
+ digest::HmacSha512,
437474
const DIGEST_LEN: usize,
438475
> digest::ClientVerify<DIGEST_LEN> for HmacDriver<'a, H, DIGEST_LEN>
439476
{
@@ -483,7 +520,13 @@ impl<
483520
/// the HMAC digest before calling the `hash_done` callback.
484521
impl<
485522
'a,
486-
H: digest::Digest<'a, DIGEST_LEN> + digest::HmacSha256 + digest::HmacSha384 + digest::HmacSha512,
523+
H: digest::Digest<'a, DIGEST_LEN>
524+
+ digest::HmacMd5
525+
+ digest::HmacSha1
526+
+ digest::HmacSha224
527+
+ digest::HmacSha256
528+
+ digest::HmacSha384
529+
+ digest::HmacSha512,
487530
const DIGEST_LEN: usize,
488531
> SyscallDriver for HmacDriver<'a, H, DIGEST_LEN>
489532
{
@@ -627,6 +670,21 @@ impl<
627670
app.sha_operation = Some(ShaOperation::Sha512);
628671
CommandReturn::success()
629672
}
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+
}
630688
_ => CommandReturn::failure(ErrorCode::NOSUPPORT),
631689
}
632690
}

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)