diff --git a/crypto/lakers-crypto-cryptocell310-sys/src/lib.rs b/crypto/lakers-crypto-cryptocell310-sys/src/lib.rs index d14fe624..90e5a9c0 100644 --- a/crypto/lakers-crypto-cryptocell310-sys/src/lib.rs +++ b/crypto/lakers-crypto-cryptocell310-sys/src/lib.rs @@ -22,6 +22,13 @@ fn convert_array(input: &[u32]) -> [u8; SHA256_DIGEST_LEN] { pub struct Crypto; impl CryptoTrait for Crypto { + fn supported_suites(&self) -> &EdhocBuffer { + &EdhocBuffer:: { + content: [EDHOCSuite::CipherSuite2 as u8, 0, 0, 0, 0, 0, 0, 0, 0], + len: 1, + } + } + fn sha256_digest(&mut self, message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen { let mut buffer: [u32; 64 / 4] = [0x00; 64 / 4]; diff --git a/crypto/lakers-crypto-rustcrypto/src/lib.rs b/crypto/lakers-crypto-rustcrypto/src/lib.rs index 6808527c..3497ef2e 100644 --- a/crypto/lakers-crypto-rustcrypto/src/lib.rs +++ b/crypto/lakers-crypto-rustcrypto/src/lib.rs @@ -20,20 +20,11 @@ type AesCcm16_64_128 = ccm::Ccm; /// Its size depends on the implementation of Rng passed in at creation. pub struct Crypto { rng: Rng, - supported_suites: EdhocBuffer, } impl Crypto { pub const fn new(rng: Rng) -> Self { - // avoid calling `new*` to keep this function constant - let supported_suites = EdhocBuffer:: { - content: [EDHOCSuite::CipherSuite2 as u8, 0, 0, 0, 0, 0, 0, 0, 0], - len: 1, - }; - Self { - rng, - supported_suites, - } + Self { rng } } } @@ -47,7 +38,10 @@ impl core::fmt::Debug for Crypto impl CryptoTrait for Crypto { fn supported_suites(&self) -> &EdhocBuffer { - &self.supported_suites + &EdhocBuffer:: { + content: [EDHOCSuite::CipherSuite2 as u8, 0, 0, 0, 0, 0, 0, 0, 0], + len: 1, + } } fn sha256_digest(&mut self, message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen { diff --git a/examples/coap/src/bin/coapclient.rs b/examples/coap/src/bin/coapclient.rs index a4b92204..b4c2e771 100644 --- a/examples/coap/src/bin/coapclient.rs +++ b/examples/coap/src/bin/coapclient.rs @@ -33,7 +33,11 @@ fn client_handshake() -> Result<(), EDHOCError> { let cred_i = CredentialRPK::new(CRED_I.try_into().unwrap()).unwrap(); let cred_r = CredentialRPK::new(CRED_R.try_into().unwrap()).unwrap(); - let initiator = EdhocInitiator::new(lakers_crypto::default_crypto()); + let initiator = EdhocInitiator::new( + lakers_crypto::default_crypto(), + EDHOCMethod::StatStat, + EDHOCSuite::CipherSuite2, + ); // 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 diff --git a/examples/lakers-no_std/src/main.rs b/examples/lakers-no_std/src/main.rs index bf859276..18e8dbe7 100644 --- a/examples/lakers-no_std/src/main.rs +++ b/examples/lakers-no_std/src/main.rs @@ -62,7 +62,11 @@ fn main() -> ! { const _C_R_TV: [u8; 1] = hex!("27"); fn test_new_initiator() { - let _initiator = EdhocInitiator::new(lakers_crypto::default_crypto()); + let _initiator = EdhocInitiator::new( + lakers_crypto::default_crypto(), + EDHOCMethod::StatStat, + EDHOCSuite::CipherSuite2, + ); } test_new_initiator(); @@ -81,7 +85,11 @@ fn main() -> ! { println!("Test test_p256_keys passed."); fn test_prepare_message_1() { - let mut initiator = EdhocInitiator::new(lakers_crypto::default_crypto()); + let mut initiator = EdhocInitiator::new( + lakers_crypto::default_crypto(), + EDHOCMethod::StatStat, + EDHOCSuite::CipherSuite2, + ); let c_i = generate_connection_identifier_cbor(&mut lakers_crypto::default_crypto()).as_slice(); @@ -96,7 +104,11 @@ fn main() -> ! { let cred_i = CredentialRPK::new(CRED_I.try_into().unwrap()).unwrap(); let cred_r = CredentialRPK::new(CRED_R.try_into().unwrap()).unwrap(); - let mut initiator = EdhocInitiator::new(lakers_crypto::default_crypto()); + let mut initiator = EdhocInitiator::new( + lakers_crypto::default_crypto(), + EDHOCMethod::StatStat, + EDHOCSuite::CipherSuite2, + ); let responder = EdhocResponder::new(lakers_crypto::default_crypto(), R, cred_r.clone()); let (initiator, message_1) = initiator.prepare_message_1(None, &None).unwrap(); diff --git a/lakers-c/src/initiator.rs b/lakers-c/src/initiator.rs index 4f07cc09..60b44530 100644 --- a/lakers-c/src/initiator.rs +++ b/lakers-c/src/initiator.rs @@ -21,17 +21,16 @@ pub struct EdhocInitiator { #[no_mangle] pub unsafe extern "C" fn initiator_new(initiator: *mut EdhocInitiator) -> i8 { - // we only support a single cipher suite which is already CBOR-encoded - let mut suites_i: BytesSuites = [0x0; SUITES_LEN]; - let suites_i_len = EDHOC_SUPPORTED_SUITES.len(); - suites_i[0..suites_i_len].copy_from_slice(&EDHOC_SUPPORTED_SUITES[..]); - let (x, g_x) = default_crypto().p256_generate_key_pair(); + let mut crypto = default_crypto(); + let suites_i = + prepare_suites_i(crypto.supported_suites(), EDHOCSuite::CipherSuite2.into()).unwrap(); + let (x, g_x) = crypto.p256_generate_key_pair(); let start = InitiatorStart { x, g_x, suites_i, - suites_i_len, + method: EDHOCMethod::StatStat.into(), }; core::ptr::write(&mut (*initiator).start, start); diff --git a/lakers-python/src/initiator.rs b/lakers-python/src/initiator.rs index c0688086..0acd1253 100644 --- a/lakers-python/src/initiator.rs +++ b/lakers-python/src/initiator.rs @@ -16,19 +16,18 @@ pub struct PyEdhocInitiator { impl PyEdhocInitiator { #[new] fn new() -> Self { - // we only support a single cipher suite which is already CBOR-encoded - let mut suites_i: BytesSuites = [0x0; SUITES_LEN]; - let suites_i_len = EDHOC_SUPPORTED_SUITES.len(); - suites_i[0..suites_i_len].copy_from_slice(&EDHOC_SUPPORTED_SUITES[..]); - let (x, g_x) = default_crypto().p256_generate_key_pair(); + let mut crypto = default_crypto(); + let suites_i = + prepare_suites_i(crypto.supported_suites(), EDHOCSuite::CipherSuite2.into()).unwrap(); + let (x, g_x) = crypto.p256_generate_key_pair(); Self { cred_i: None, start: InitiatorStart { x, g_x, + method: EDHOCMethod::StatStat.into(), suites_i, - suites_i_len, }, wait_m2: WaitM2::default(), processing_m2: ProcessingM2::default(), @@ -185,6 +184,6 @@ impl PyEdhocInitiator { } pub fn selected_cipher_suite(&self) -> PyResult { - Ok(self.start.suites_i[self.start.suites_i_len - 1]) + Ok(self.start.suites_i[self.start.suites_i.len() - 1]) } } diff --git a/lakers-python/test_requirements.txt b/lakers-python/test_requirements.txt index 55b033e9..330a2ab7 100644 --- a/lakers-python/test_requirements.txt +++ b/lakers-python/test_requirements.txt @@ -1 +1,2 @@ -pytest \ No newline at end of file +pytest +cbor2 \ No newline at end of file diff --git a/lib/src/lib.rs b/lib/src/lib.rs index c2a350d3..26977763 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -251,13 +251,16 @@ impl EdhocResponderDone { impl<'a, Crypto: CryptoTrait> EdhocInitiator { pub fn new(mut crypto: Crypto, method: EDHOCMethod, selected_suite: EDHOCSuite) -> Self { trace!("Initializing EdhocInitiator"); - let suites_i = prepare_suites_i(crypto.supported_suites(), selected_suite.into()).unwrap(); - let (x, g_x) = crypto.p256_generate_key_pair(); EdhocInitiator { - state: InitiatorStart { x, g_x, suites_i }, + state: InitiatorStart { + x, + g_x, + method: method.into(), + suites_i, + }, crypto, } } diff --git a/shared/src/buffer.rs b/shared/src/buffer.rs index 35a6f515..1537ef54 100644 --- a/shared/src/buffer.rs +++ b/shared/src/buffer.rs @@ -1,4 +1,4 @@ -use core::ops::{Index, IndexMut}; +use core::ops::Index; // NOTE: This constant is only here for now because it is only ever used in instances of EdhocBuffer. // TODO: move to lib.rs, once EdhocMessageBuffer is replaced by EdhocBuffer. diff --git a/shared/src/crypto.rs b/shared/src/crypto.rs index 8f60f9f2..a9f63d04 100644 --- a/shared/src/crypto.rs +++ b/shared/src/crypto.rs @@ -37,7 +37,7 @@ pub fn prepare_suites_i( /// platform's mutex, or to refactor the main initiator and responder objects into a form where the /// cryptography implementation can be taken out and stored separately. pub trait Crypto: core::fmt::Debug { - /// Returns the list of cryptographic suites by backend implementation. + /// Returns the list of cryptographic suites supported by the backend implementation. fn supported_suites(&self) -> &EdhocBuffer; fn sha256_digest(&mut self, message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen; fn hkdf_expand( diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 8af46dea..f60f2cd6 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -304,6 +304,7 @@ impl ErrCode { #[repr(C)] pub struct InitiatorStart { pub suites_i: EdhocBuffer, + pub method: u8, pub x: BytesP256ElemLen, // ephemeral private key of myself pub g_x: BytesP256ElemLen, // ephemeral public key of myself }