Skip to content

Commit 66976da

Browse files
committed
refactor!: Pull Crypto trait to the high-level implementation
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: #127
1 parent da2be2e commit 66976da

File tree

10 files changed

+190
-99
lines changed

10 files changed

+190
-99
lines changed

.github/workflows/build-and-test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ jobs:
2828
strategy:
2929
fail-fast: false
3030
matrix:
31-
crypto_backend: [crypto-hacspec, crypto-psa]
31+
crypto_backend: [edhoc-crypto/hacspec, edhoc-crypto/psa]
3232
ead: [ead-none, ead-zeroconf]
3333

3434
steps:
3535
- name: Checkout repo
3636
uses: actions/checkout@v3
3737

3838
- name: Run unit tests # note that we only add `--package edhoc-hacspec` when testing the hacspec version of the lib
39-
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
39+
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
4040

4141

4242
build-edhoc-package:
@@ -46,7 +46,7 @@ jobs:
4646
strategy:
4747
fail-fast: false
4848
matrix:
49-
crypto_backend: [crypto-hacspec, crypto-psa, crypto-psa-baremetal, crypto-cryptocell310]
49+
crypto_backend: [edhoc-crypto/hacspec, edhoc-crypto/psa, edhoc-crypto/psa-baremetal, edhoc-crypto/cryptocell310]
5050
ead: [ead-none, ead-zeroconf]
5151

5252
steps:

examples/coap/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
edhoc-rs = { path = "../../lib", features = [ "crypto-hacspec" ] }
7+
edhoc-rs = { path = "../../lib" }
8+
edhoc-crypto = { path = "../../crypto/", features = [ "hacspec" ] }
89
hexlit = "0.5.3"
910
coap = { version = "0.13" }
1011
coap-lite = { version = "0.11.3" }

examples/coap/src/bin/coapclient.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ fn main() {
2121
println!("Client request: {}", url);
2222

2323
let state = Default::default();
24-
let initiator = EdhocInitiator::new(state, &I, &CRED_I, Some(&CRED_R));
24+
let initiator = EdhocInitiator::new(
25+
state,
26+
edhoc_crypto::default_crypto(),
27+
&I,
28+
&CRED_I,
29+
Some(&CRED_R),
30+
);
2531

2632
// Send Message 1 over CoAP and convert the response to byte
2733
let mut msg_1_buf = Vec::from([0xf5u8]); // EDHOC message_1 when transported over CoAP is prepended with CBOR true
28-
let c_i = generate_connection_identifier_cbor();
34+
let c_i = generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto());
2935
let (initiator, message_1) = initiator.prepare_message_1(c_i).unwrap();
3036
msg_1_buf.extend_from_slice(&message_1.content[..message_1.len]);
3137
println!("message_1 len = {}", msg_1_buf.len());

examples/coap/src/bin/coapserver-coaphandler.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use edhoc_crypto::Crypto;
12
use edhoc_rs::*;
23
use hexlit::hex;
34

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

1516
#[derive(Default, Debug)]
1617
struct EdhocHandler {
17-
connections: Vec<(u8, EdhocResponderWaitM3<'static>)>,
18+
connections: Vec<(u8, EdhocResponderWaitM3<'static, Crypto>)>,
1819
}
1920

2021
impl EdhocHandler {
21-
fn take_connection_by_c_r(&mut self, c_r: u8) -> Option<EdhocResponderWaitM3<'static>> {
22+
fn take_connection_by_c_r(&mut self, c_r: u8) -> Option<EdhocResponderWaitM3<'static, Crypto>> {
2223
let index = self
2324
.connections
2425
.iter()
@@ -45,7 +46,7 @@ enum EdhocResponse {
4546
// take up a slot there anyway) if we make it an enum.
4647
OkSend2 {
4748
c_r: u8,
48-
responder: EdhocResponderBuildM2<'static>,
49+
responder: EdhocResponderBuildM2<'static, Crypto>,
4950
},
5051
Message3Processed,
5152
}
@@ -60,7 +61,14 @@ impl coap_handler::Handler for EdhocHandler {
6061

6162
if starts_with_true {
6263
let state = EdhocState::default();
63-
let responder = EdhocResponder::new(state, &R, &CRED_R, Some(&CRED_I));
64+
65+
let responder = EdhocResponder::new(
66+
state,
67+
edhoc_crypto::default_crypto(),
68+
&R,
69+
&CRED_R,
70+
Some(&CRED_I),
71+
);
6472

6573
let response = responder
6674
.process_message_1(&request.payload()[1..].try_into().expect("wrong length"));

examples/coap/src/bin/coapserver.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ fn main() {
3232
// This is an EDHOC message
3333
if request.message.payload[0] == 0xf5 {
3434
let state = EdhocState::default();
35-
let responder = EdhocResponder::new(state, &R, &CRED_R, Some(&CRED_I));
35+
let responder = EdhocResponder::new(
36+
state,
37+
edhoc_crypto::default_crypto(),
38+
&R,
39+
&CRED_R,
40+
Some(&CRED_I),
41+
);
3642

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

4349
if let Ok(responder) = result {
44-
let c_r = generate_connection_identifier_cbor();
50+
let c_r =
51+
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto());
4552
let (responder, message_2) = responder.prepare_message_2(c_r).unwrap();
4653
response.message.payload = Vec::from(&message_2.content[..message_2.len]);
4754
// save edhoc connection

examples/edhoc-rs-cc2538/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ description = "edhoc-rs example on CC2538 SoC"
88

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

2122
[features]
2223
default = [ "psa" ]
23-
psa = [ "edhoc-rs/crypto-psa-baremetal" ]
24+
psa = [ "edhoc-crypto/psa-baremetal" ]
2425

examples/edhoc-rs-no_std/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ rtt-target = { version = "0.3.1", features = ["cortex-m"] }
2323
[features]
2424
default = [ "rtt", "crypto-cryptocell310", "ead-none" ]
2525
rtt = [ ]
26-
crypto-psa = [ "edhoc-rs/crypto-psa-baremetal" ]
27-
crypto-cryptocell310 = [ "edhoc-rs/crypto-cryptocell310" ]
26+
crypto-psa = [ "edhoc-crypto/psa-baremetal" ]
27+
crypto-cryptocell310 = [ "edhoc-crypto/cryptocell310" ]
2828
ead-none = [ "edhoc-rs/ead-none" ]
2929
ead-zeroconf = [ "edhoc-rs/ead-zeroconf" ]

examples/edhoc-rs-no_std/src/main.rs

+36-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ fn main() -> ! {
7474

7575
fn test_new_initiator() {
7676
let state = Default::default();
77-
let _initiator = EdhocInitiator::new(state, I, CRED_I, Some(CRED_R));
77+
let _initiator = EdhocInitiator::new(
78+
state,
79+
edhoc_crypto::default_crypto(),
80+
I,
81+
CRED_I,
82+
Some(CRED_R),
83+
);
7884
}
7985

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

95101
fn test_prepare_message_1() {
96102
let state = Default::default();
97-
let mut initiator = EdhocInitiator::new(state, I, CRED_I, Some(CRED_R));
98-
99-
let c_i: u8 = generate_connection_identifier_cbor().into();
103+
let mut initiator = EdhocInitiator::new(
104+
state,
105+
edhoc_crypto::default_crypto(),
106+
I,
107+
CRED_I,
108+
Some(CRED_R),
109+
);
110+
111+
let c_i: u8 =
112+
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto()).into();
100113
let message_1 = initiator.prepare_message_1(c_i);
101114
assert!(message_1.is_ok());
102115
}
@@ -106,16 +119,30 @@ fn main() -> ! {
106119

107120
fn test_handshake() {
108121
let state_initiator = Default::default();
109-
let mut initiator = EdhocInitiator::new(state_initiator, I, CRED_I, Some(CRED_R));
122+
let mut initiator = EdhocInitiator::new(
123+
state_initiator,
124+
edhoc_crypto::default_crypto(),
125+
I,
126+
CRED_I,
127+
Some(CRED_R),
128+
);
110129
let state_responder = Default::default();
111-
let responder = EdhocResponder::new(state_responder, R, CRED_R, Some(CRED_I));
112-
113-
let c_i: u8 = generate_connection_identifier_cbor().into();
130+
let responder = EdhocResponder::new(
131+
state_responder,
132+
edhoc_crypto::default_crypto(),
133+
R,
134+
CRED_R,
135+
Some(CRED_I),
136+
);
137+
138+
let c_i: u8 =
139+
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto()).into();
114140
let (initiator, message_1) = initiator.prepare_message_1(c_i).unwrap(); // to update the state
115141

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

118-
let c_r: u8 = generate_connection_identifier_cbor().into();
144+
let c_r: u8 =
145+
generate_connection_identifier_cbor(&mut edhoc_crypto::default_crypto()).into();
119146
let (responder, message_2) = responder.prepare_message_2(c_r).unwrap();
120147
assert!(c_r != 0xff);
121148

lib/Cargo.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ hexlit = "0.5.3"
1111
hex = { version = "0.4.3", default-features = false }
1212

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

22+
[dev-dependencies]
23+
edhoc-crypto = { path = "../crypto", default-features = false }
24+
2325
[features]
2426
default = [ "edhoc-ead/ead-none" ]
25-
crypto-hacspec = ["hacspec-lib/std", "edhoc-crypto/hacspec" ]
26-
crypto-psa = [ "edhoc-crypto/psa" ]
27-
crypto-psa-baremetal = [ "edhoc-crypto/psa-baremetal", "panic-semihosting" ]
28-
crypto-cryptocell310 = [ "edhoc-crypto/cryptocell310", "panic-semihosting" ]
2927
ead-none = [ "edhoc-ead/ead-none" ]
3028
ead-zeroconf = [ "edhoc-ead/ead-zeroconf" ]
3129

0 commit comments

Comments
 (0)