Skip to content

Commit

Permalink
refactor(coap): Move crypto setup into riot-rs-coap
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Jul 3, 2024
1 parent a851365 commit 3001d9f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions examples/coap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 2 additions & 15 deletions examples/coap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,27 +47,16 @@ 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"],
coap_scroll_ring_server::BufferHandler::new(&buffer),
)
.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;
Expand Down
5 changes: 4 additions & 1 deletion src/riot-rs-coap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
18 changes: 16 additions & 2 deletions src/riot-rs-coap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ mod udp_nal;

use riot_rs_embassy::embassy_net::udp::{PacketMetadata, UdpSocket};

use coapcore::seccontext;

pub async fn coap_task<const N: usize>(
handler: &mut impl coap_handler::Handler,
handler: impl coap_handler::Handler,
client_runner: impl coapcore::ClientRunner<N>,
logger: &mut impl core::fmt::Write,
) {
let stack = riot_rs_embassy::network::network_stack().await.unwrap();

Expand All @@ -41,5 +44,16 @@ pub async fn coap_task<const N: usize>(

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;
}

0 comments on commit 3001d9f

Please sign in to comment.