From 3001d9f2dfe45a59ba66e252b102f1578e1c0958 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 3 Jul 2024 10:56:59 +0200 Subject: [PATCH] refactor(coap): Move crypto setup into riot-rs-coap --- Cargo.lock | 6 +++--- examples/coap/Cargo.toml | 3 --- examples/coap/src/main.rs | 17 ++--------------- src/riot-rs-coap/Cargo.toml | 5 ++++- src/riot-rs-coap/src/lib.rs | 18 ++++++++++++++++-- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e9112692c..9adcbaefd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -453,9 +453,6 @@ dependencies = [ "embassy-time", "embedded-nal-coap", "heapless 0.8.0", - "hexlit", - "lakers", - "lakers-crypto-rustcrypto", "riot-rs", "riot-rs-boards", "scroll-ring", @@ -3416,6 +3413,9 @@ dependencies = [ "embedded-io-async", "embedded-nal-async", "embedded-nal-coap", + "hexlit", + "lakers", + "lakers-crypto-rustcrypto", "riot-rs-embassy", "riot-rs-random", "smoltcp", diff --git a/examples/coap/Cargo.toml b/examples/coap/Cargo.toml index 5bcfa7d15..5cb825b89 100644 --- a/examples/coap/Cargo.toml +++ b/examples/coap/Cargo.toml @@ -26,11 +26,8 @@ coap-request = "0.2.0-alpha.2" coap-message = "0.3.2" coap-message-demos = { version = "0.4.0", default-features = false } coap-request-implementations = "0.1.0-alpha.4" -lakers = { version = "0.6.0", default-features = false } -lakers-crypto-rustcrypto = "0.6.0" coap-handler = "0.2.0" coap-handler-implementations = "0.5.0" -hexlit = "0.5.5" static-alloc = "0.2.5" coap-scroll-ring-server = "0.2.0" diff --git a/examples/coap/src/main.rs b/examples/coap/src/main.rs index bb2d89c56..0bb8b83a4 100644 --- a/examples/coap/src/main.rs +++ b/examples/coap/src/main.rs @@ -3,12 +3,10 @@ #![feature(type_alias_impl_trait)] #![feature(used_with_arg)] -use riot_rs::{debug::println, embassy::network}; +use riot_rs::debug::println; use riot_rs::embassy::embassy_net; -use coapcore::seccontext; - // because coapcore depends on it temporarily extern crate alloc; use static_alloc::Bump; @@ -49,13 +47,6 @@ async fn run() { writeln!(stdout, "We have our own stdout now.").unwrap(); writeln!(stdout, "With rings and atomics.").unwrap(); - use hexlit::hex; - const R: &[u8] = &hex!("72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3e6aa4aac"); - let own_identity = ( - &lakers::CredentialRPK::new(lakers::EdhocMessageBuffer::new_from_slice(&hex!("A2026008A101A5010202410A2001215820BBC34960526EA4D32E940CAD2A234148DDC21791A12AFBCBAC93622046DD44F02258204519E257236B2A0CE2023F0931F1F386CA7AFDA64FCDE0108C224C51EABF6072")).expect("Credential should be small enough")).expect("Credential should be processable"), - R, - ); - let handler = coap_message_demos::full_application_tree(log) .at( &["stdout"], @@ -63,13 +54,9 @@ async fn run() { ) .with_wkc(); - let mut handler = seccontext::OscoreEdhocHandler::new(own_identity, handler, stdout, || { - lakers_crypto_rustcrypto::Crypto::new(riot_rs::random::crypto_rng()) - }); - println!("Server is ready."); - riot_rs::coap::coap_task(&mut handler, Client).await; + riot_rs::coap::coap_task(handler, Client, &mut stdout).await; } struct Client; diff --git a/src/riot-rs-coap/Cargo.toml b/src/riot-rs-coap/Cargo.toml index f22a64aca..4e30174c0 100644 --- a/src/riot-rs-coap/Cargo.toml +++ b/src/riot-rs-coap/Cargo.toml @@ -7,7 +7,7 @@ repository.workspace = true [dependencies] coapcore.path = "../lib/coapcore" -riot-rs-random.path = "../riot-rs-random" +riot-rs-random = { path = "../riot-rs-random", features = ["csprng"] } riot-rs-embassy.path = "../riot-rs-embassy" # actually patched with https://github.com/smoltcp-rs/smoltcp/pull/904 but @@ -16,6 +16,9 @@ smoltcp = { version = "0.11", default-features = false } embedded-nal-coap = "0.1.0-alpha.2" coap-handler = "0.2.0" +hexlit = "0.5.5" +lakers = { version = "0.6.0", default-features = false } +lakers-crypto-rustcrypto = "0.6.0" # for the udp_nal mod embedded-nal-async = "0.7" diff --git a/src/riot-rs-coap/src/lib.rs b/src/riot-rs-coap/src/lib.rs index 2bb6c3f82..1bb26a679 100644 --- a/src/riot-rs-coap/src/lib.rs +++ b/src/riot-rs-coap/src/lib.rs @@ -12,9 +12,12 @@ mod udp_nal; use riot_rs_embassy::embassy_net::udp::{PacketMetadata, UdpSocket}; +use coapcore::seccontext; + pub async fn coap_task( - handler: &mut impl coap_handler::Handler, + handler: impl coap_handler::Handler, client_runner: impl coapcore::ClientRunner, + logger: &mut impl core::fmt::Write, ) { let stack = riot_rs_embassy::network::network_stack().await.unwrap(); @@ -41,5 +44,16 @@ pub async fn coap_task( let mut rng = riot_rs_random::fast_rng(); - coapcore::coap_task(&mut sock, handler, &mut rng, client_runner).await; + use hexlit::hex; + const R: &[u8] = &hex!("72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3e6aa4aac"); + let own_identity = ( + &lakers::CredentialRPK::new(lakers::EdhocMessageBuffer::new_from_slice(&hex!("A2026008A101A5010202410A2001215820BBC34960526EA4D32E940CAD2A234148DDC21791A12AFBCBAC93622046DD44F02258204519E257236B2A0CE2023F0931F1F386CA7AFDA64FCDE0108C224C51EABF6072")).expect("Credential should be small enough")).expect("Credential should be processable"), + R, + ); + + let mut handler = seccontext::OscoreEdhocHandler::new(own_identity, handler, logger, || { + lakers_crypto_rustcrypto::Crypto::new(riot_rs_random::crypto_rng()) + }); + + coapcore::coap_task(&mut sock, &mut handler, &mut rng, client_runner).await; }