Skip to content

Commit 7fd4edb

Browse files
committed
nucleo_u545 hash driver
1 parent 7e67154 commit 7fd4edb

37 files changed

Lines changed: 4317 additions & 73 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ flash-debug: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/debug/$(PLATFORM).elf
2222
flash: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).elf
2323
$(OPENOCD) $(OPENOCD_OPTIONS) -c "init; halt; stm32l4x mass_erase 0; program $< verify reset exit"
2424

25+
.PHONY: flash-probe-rs
26+
flash-probe-rs: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).elf
27+
$(Q)probe-rs download --chip STM32U545RETx --allow-erase-all $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).elf
28+
$(Q)probe-rs reset --chip STM32U545RETx
29+
2530
.PHONY: program
2631
program:
2732
tockloader install --openocd --board nucleo_u545re_q $(APP)

boards/nucleo_u545re_q/src/main.rs

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

9+
use capsules_extra::test::hmac_sha256::TestHmacSha256;
910
use kernel::capabilities;
1011
use kernel::component::Component;
1112
use kernel::debug::PanicResources;
@@ -51,6 +52,10 @@ struct NucleoU545RE {
5152
stm32u545::tim::Tim2<'static>,
5253
>,
5354
>,
55+
test_hmac_sha256: &'static capsules_extra::test::hmac_sha256::TestHmacSha256<
56+
'static,
57+
stm32u545::hash::sha256::Sha256Adapter<'static>,
58+
>,
5459
}
5560

5661
impl SyscallDriverLookup for NucleoU545RE {
@@ -151,10 +156,17 @@ unsafe fn start() -> (
151156
);
152157
usart1.register();
153158

159+
let hash = static_init!(
160+
stm32u545::hash::core_unit::Hash<'static>,
161+
stm32u545::hash::core_unit::Hash::new(stm32u545::hash::regs::HASH_BASE)
162+
);
163+
164+
hash.register();
165+
154166
// Load Peripherals Bundle
155167
let periphs = static_init!(
156168
stm32u545::chip::Stm32u5xxDefaultPeripherals<'static>,
157-
stm32u545::chip::Stm32u5xxDefaultPeripherals::new(usart1, exti, dma1)
169+
stm32u545::chip::Stm32u5xxDefaultPeripherals::new(usart1, exti, dma1, hash)
158170
);
159171

160172
// Initialize wiring (DMA, clocks)
@@ -164,6 +176,16 @@ unsafe fn start() -> (
164176
periphs.tim2.start();
165177
set_pin_primary_functions(periphs);
166178

179+
// Create an adapter for the HASH peripheral.
180+
// In this way it is ensured that only one mode is used by the peripheral.
181+
let sha256 = static_init!(
182+
stm32u545::hash::sha256::Sha256Adapter<'static>,
183+
stm32u545::hash::sha256::Sha256Adapter::new(hash)
184+
);
185+
186+
// Adapter receives callbacks from the peripheral
187+
let _ = hash.set_sha256_adapter(sha256);
188+
167189
// Kernel and Muxes
168190
let processes = components::process_array::ProcessArrayComponent::new()
169191
.finalize(components::process_array_component_static!(NUM_PROCS));
@@ -232,6 +254,50 @@ unsafe fn start() -> (
232254
)
233255
.finalize(components::button_component_static!(stm32u545::gpio::Pin));
234256

257+
let hmac_key = static_init!(
258+
[u8; 64],
259+
[
260+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
261+
0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
262+
0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
263+
0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
264+
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
265+
]
266+
);
267+
268+
// An example from STM32 RM0456 Reference manual
269+
//
270+
// “Sample message for keylen = blocklen”
271+
let hash_data_buffer = static_init!(
272+
[u8; 34],
273+
[
274+
0x53, 0x61, 0x6d, 0x70, 0x6C, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
275+
0x20, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x65, 0x79, 0x6C, 0x65, 0x6e, 0x3d, 0x62, 0x6c,
276+
0x6f, 0x63, 0x6b, 0x6c, 0x65, 0x6e
277+
]
278+
);
279+
280+
let hash_digest_buffer = static_init!([u8; 32], [0u8; 32]);
281+
let correct = static_init!(
282+
[u8; 32],
283+
[
284+
0x8b, 0xb9, 0xa1, 0xdb, 0x98, 0x06, 0xf2, 0x0d, 0xf7, 0xf7, 0x7b, 0x82, 0x13, 0x8c,
285+
0x79, 0x14, 0xd1, 0x74, 0xd5, 0x9e, 0x13, 0xdc, 0x4d, 0x01, 0x69, 0xc9, 0x05, 0x7b,
286+
0x13, 0x3e, 0x1d, 0x62
287+
]
288+
);
289+
290+
let test_hmac_sha256 = static_init!(
291+
TestHmacSha256<'static, stm32u545::hash::sha256::Sha256Adapter<'static>>,
292+
TestHmacSha256::new(
293+
sha256,
294+
hmac_key,
295+
hash_data_buffer,
296+
hash_digest_buffer,
297+
correct
298+
)
299+
);
300+
235301
// Platform and Interrupts
236302
let platform = static_init!(
237303
NucleoU545RE,
@@ -243,6 +309,7 @@ unsafe fn start() -> (
243309
led,
244310
button,
245311
alarm,
312+
test_hmac_sha256
246313
}
247314
);
248315

@@ -291,6 +358,7 @@ pub unsafe fn main() {
291358
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
292359

293360
let (board_kernel, platform, chip) = start();
361+
platform.test_hmac_sha256.run();
294362
// Hand over control to the Tock Kernel Loop
295363
board_kernel.kernel_loop::<NucleoU545RE, ChipHw, { NUM_PROCS as u8 }>(
296364
platform,

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)