Skip to content

Commit

Permalink
refactor!: Pull Crypto trait to the high-level implementation
Browse files Browse the repository at this point in the history
This allows pushing back the edhoc-crypto ("the default implementation
that is selected statically, making all implementations possible
dependencies") into the dev-dependencies.

The crypto-* features are removed from edhoc-rs; testing depends on
edhoc-crypto being pulled in in parallel to the test, and a feature
selected on that.

Follow-up-for: openwsn-berkeley#127
  • Loading branch information
chrysn committed Nov 15, 2023
1 parent da2be2e commit 66976da
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 99 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ jobs:
strategy:
fail-fast: false
matrix:
crypto_backend: [crypto-hacspec, crypto-psa]
crypto_backend: [edhoc-crypto/hacspec, edhoc-crypto/psa]
ead: [ead-none, ead-zeroconf]

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Run unit tests # note that we only add `--package edhoc-hacspec` when testing the hacspec version of the lib
run: RUST_BACKTRACE=1 cargo test -p edhoc-rs -p edhoc-consts -p edhoc-ead-zeroconf --no-default-features --features="${{ matrix.crypto_backend }}, ${{ matrix.ead }}" --no-fail-fast -- --test-threads 1
run: RUST_BACKTRACE=1 cargo test -p edhoc-rs -p edhoc-crypto -p edhoc-consts -p edhoc-ead-zeroconf --no-default-features --features="${{ matrix.crypto_backend }}, ${{ matrix.ead }}" --no-fail-fast -- --test-threads 1


build-edhoc-package:
Expand All @@ -46,7 +46,7 @@ jobs:
strategy:
fail-fast: false
matrix:
crypto_backend: [crypto-hacspec, crypto-psa, crypto-psa-baremetal, crypto-cryptocell310]
crypto_backend: [edhoc-crypto/hacspec, edhoc-crypto/psa, edhoc-crypto/psa-baremetal, edhoc-crypto/cryptocell310]
ead: [ead-none, ead-zeroconf]

steps:
Expand Down
3 changes: 2 additions & 1 deletion examples/coap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
edhoc-rs = { path = "../../lib", features = [ "crypto-hacspec" ] }
edhoc-rs = { path = "../../lib" }
edhoc-crypto = { path = "../../crypto/", features = [ "hacspec" ] }
hexlit = "0.5.3"
coap = { version = "0.13" }
coap-lite = { version = "0.11.3" }
Expand Down
10 changes: 8 additions & 2 deletions examples/coap/src/bin/coapclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ fn main() {
println!("Client request: {}", url);

let state = Default::default();
let initiator = EdhocInitiator::new(state, &I, &CRED_I, Some(&CRED_R));
let initiator = EdhocInitiator::new(
state,
edhoc_crypto::default_crypto(),
&I,
&CRED_I,
Some(&CRED_R),
);

// Send Message 1 over CoAP and convert the response to byte
let mut msg_1_buf = Vec::from([0xf5u8]); // EDHOC message_1 when transported over CoAP is prepended with CBOR true
let c_i = generate_connection_identifier_cbor();
let c_i = generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto());
let (initiator, message_1) = initiator.prepare_message_1(c_i).unwrap();
msg_1_buf.extend_from_slice(&message_1.content[..message_1.len]);
println!("message_1 len = {}", msg_1_buf.len());
Expand Down
16 changes: 12 additions & 4 deletions examples/coap/src/bin/coapserver-coaphandler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use edhoc_crypto::Crypto;
use edhoc_rs::*;
use hexlit::hex;

Expand All @@ -14,11 +15,11 @@ const R: &[u8] = &hex!("72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3

#[derive(Default, Debug)]
struct EdhocHandler {
connections: Vec<(u8, EdhocResponderWaitM3<'static>)>,
connections: Vec<(u8, EdhocResponderWaitM3<'static, Crypto>)>,
}

impl EdhocHandler {
fn take_connection_by_c_r(&mut self, c_r: u8) -> Option<EdhocResponderWaitM3<'static>> {
fn take_connection_by_c_r(&mut self, c_r: u8) -> Option<EdhocResponderWaitM3<'static, Crypto>> {
let index = self
.connections
.iter()
Expand All @@ -45,7 +46,7 @@ enum EdhocResponse {
// take up a slot there anyway) if we make it an enum.
OkSend2 {
c_r: u8,
responder: EdhocResponderBuildM2<'static>,
responder: EdhocResponderBuildM2<'static, Crypto>,
},
Message3Processed,
}
Expand All @@ -60,7 +61,14 @@ impl coap_handler::Handler for EdhocHandler {

if starts_with_true {
let state = EdhocState::default();
let responder = EdhocResponder::new(state, &R, &CRED_R, Some(&CRED_I));

let responder = EdhocResponder::new(
state,
edhoc_crypto::default_crypto(),
&R,
&CRED_R,
Some(&CRED_I),
);

let response = responder
.process_message_1(&request.payload()[1..].try_into().expect("wrong length"));
Expand Down
11 changes: 9 additions & 2 deletions examples/coap/src/bin/coapserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ fn main() {
// This is an EDHOC message
if request.message.payload[0] == 0xf5 {
let state = EdhocState::default();
let responder = EdhocResponder::new(state, &R, &CRED_R, Some(&CRED_I));
let responder = EdhocResponder::new(
state,
edhoc_crypto::default_crypto(),
&R,
&CRED_R,
Some(&CRED_I),
);

let result = responder.process_message_1(
&request.message.payload[1..]
Expand All @@ -41,7 +47,8 @@ fn main() {
);

if let Ok(responder) = result {
let c_r = generate_connection_identifier_cbor();
let c_r =
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto());
let (responder, message_2) = responder.prepare_message_2(c_r).unwrap();
response.message.payload = Vec::from(&message_2.content[..message_2.len]);
// save edhoc connection
Expand Down
3 changes: 2 additions & 1 deletion examples/edhoc-rs-cc2538/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "edhoc-rs example on CC2538 SoC"

[dependencies]
edhoc-rs = { path = "../../lib", default-features = false }
edhoc-crypto = { path = "../../crypto", default-features = false }
# depend on an allocator
embedded-alloc = "0.5.0"
hexlit = "0.5.3"
Expand All @@ -20,5 +21,5 @@ rtt-target = { version = "0.3.1", features = ["cortex-m"] }

[features]
default = [ "psa" ]
psa = [ "edhoc-rs/crypto-psa-baremetal" ]
psa = [ "edhoc-crypto/psa-baremetal" ]

4 changes: 2 additions & 2 deletions examples/edhoc-rs-no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rtt-target = { version = "0.3.1", features = ["cortex-m"] }
[features]
default = [ "rtt", "crypto-cryptocell310", "ead-none" ]
rtt = [ ]
crypto-psa = [ "edhoc-rs/crypto-psa-baremetal" ]
crypto-cryptocell310 = [ "edhoc-rs/crypto-cryptocell310" ]
crypto-psa = [ "edhoc-crypto/psa-baremetal" ]
crypto-cryptocell310 = [ "edhoc-crypto/cryptocell310" ]
ead-none = [ "edhoc-rs/ead-none" ]
ead-zeroconf = [ "edhoc-rs/ead-zeroconf" ]
45 changes: 36 additions & 9 deletions examples/edhoc-rs-no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ fn main() -> ! {

fn test_new_initiator() {
let state = Default::default();
let _initiator = EdhocInitiator::new(state, I, CRED_I, Some(CRED_R));
let _initiator = EdhocInitiator::new(
state,
edhoc_crypto::default_crypto(),
I,
CRED_I,
Some(CRED_R),
);
}

test_new_initiator();
Expand All @@ -94,9 +100,16 @@ fn main() -> ! {

fn test_prepare_message_1() {
let state = Default::default();
let mut initiator = EdhocInitiator::new(state, I, CRED_I, Some(CRED_R));

let c_i: u8 = generate_connection_identifier_cbor().into();
let mut initiator = EdhocInitiator::new(
state,
edhoc_crypto::default_crypto(),
I,
CRED_I,
Some(CRED_R),
);

let c_i: u8 =
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto()).into();
let message_1 = initiator.prepare_message_1(c_i);
assert!(message_1.is_ok());
}
Expand All @@ -106,16 +119,30 @@ fn main() -> ! {

fn test_handshake() {
let state_initiator = Default::default();
let mut initiator = EdhocInitiator::new(state_initiator, I, CRED_I, Some(CRED_R));
let mut initiator = EdhocInitiator::new(
state_initiator,
edhoc_crypto::default_crypto(),
I,
CRED_I,
Some(CRED_R),
);
let state_responder = Default::default();
let responder = EdhocResponder::new(state_responder, R, CRED_R, Some(CRED_I));

let c_i: u8 = generate_connection_identifier_cbor().into();
let responder = EdhocResponder::new(
state_responder,
edhoc_crypto::default_crypto(),
R,
CRED_R,
Some(CRED_I),
);

let c_i: u8 =
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto()).into();
let (initiator, message_1) = initiator.prepare_message_1(c_i).unwrap(); // to update the state

let responder = responder.process_message_1(&message_1).unwrap();

let c_r: u8 = generate_connection_identifier_cbor().into();
let c_r: u8 =
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto()).into();
let (responder, message_2) = responder.prepare_message_2(c_r).unwrap();
assert!(c_r != 0xff);

Expand Down
8 changes: 3 additions & 5 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ hexlit = "0.5.3"
hex = { version = "0.4.3", default-features = false }

hacspec-lib = { version = "0.1.0-beta.1", default-features = false, optional = true }
edhoc-crypto = { path = "../crypto", default-features = false }
edhoc-crypto-trait = { path = "../crypto/edhoc-crypto-trait" }
edhoc-consts = { path = "../consts" }
edhoc-ead = { path = "../ead", default-features = false }
Expand All @@ -20,12 +19,11 @@ panic-semihosting = { version = "0.6.0", features = ["exit"], optional = true }
[build-dependencies]
cbindgen = "0.24.5"

[dev-dependencies]
edhoc-crypto = { path = "../crypto", default-features = false }

[features]
default = [ "edhoc-ead/ead-none" ]
crypto-hacspec = ["hacspec-lib/std", "edhoc-crypto/hacspec" ]
crypto-psa = [ "edhoc-crypto/psa" ]
crypto-psa-baremetal = [ "edhoc-crypto/psa-baremetal", "panic-semihosting" ]
crypto-cryptocell310 = [ "edhoc-crypto/cryptocell310", "panic-semihosting" ]
ead-none = [ "edhoc-ead/ead-none" ]
ead-zeroconf = [ "edhoc-ead/ead-zeroconf" ]

Expand Down
Loading

0 comments on commit 66976da

Please sign in to comment.