Skip to content

Commit

Permalink
agility: update c and python (no api change)
Browse files Browse the repository at this point in the history
  • Loading branch information
geonnave committed Jun 4, 2024
1 parent c464077 commit 1794423
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 34 deletions.
7 changes: 7 additions & 0 deletions crypto/lakers-crypto-cryptocell310-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MAX_SUITES_LEN> {
&EdhocBuffer::<MAX_SUITES_LEN> {
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];

Expand Down
16 changes: 5 additions & 11 deletions crypto/lakers-crypto-rustcrypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,11 @@ type AesCcm16_64_128 = ccm::Ccm<aes::Aes128, ccm::consts::U8, ccm::consts::U13>;
/// Its size depends on the implementation of Rng passed in at creation.
pub struct Crypto<Rng: rand_core::RngCore + rand_core::CryptoRng> {
rng: Rng,
supported_suites: EdhocBuffer<MAX_SUITES_LEN>,
}

impl<Rng: rand_core::RngCore + rand_core::CryptoRng> Crypto<Rng> {
pub const fn new(rng: Rng) -> Self {
// avoid calling `new*` to keep this function constant
let supported_suites = EdhocBuffer::<MAX_SUITES_LEN> {
content: [EDHOCSuite::CipherSuite2 as u8, 0, 0, 0, 0, 0, 0, 0, 0],
len: 1,
};
Self {
rng,
supported_suites,
}
Self { rng }
}
}

Expand All @@ -47,7 +38,10 @@ impl<Rng: rand_core::RngCore + rand_core::CryptoRng> core::fmt::Debug for Crypto

impl<Rng: rand_core::RngCore + rand_core::CryptoRng> CryptoTrait for Crypto<Rng> {
fn supported_suites(&self) -> &EdhocBuffer<MAX_SUITES_LEN> {
&self.supported_suites
&EdhocBuffer::<MAX_SUITES_LEN> {
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 {
Expand Down
6 changes: 5 additions & 1 deletion examples/coap/src/bin/coapclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions examples/lakers-no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions lakers-c/src/initiator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions lakers-python/src/initiator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -185,6 +184,6 @@ impl PyEdhocInitiator {
}

pub fn selected_cipher_suite(&self) -> PyResult<u8> {
Ok(self.start.suites_i[self.start.suites_i_len - 1])
Ok(self.start.suites_i[self.start.suites_i.len() - 1])
}
}
3 changes: 2 additions & 1 deletion lakers-python/test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest
pytest
cbor2
9 changes: 6 additions & 3 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,16 @@ impl<Crypto: CryptoTrait> EdhocResponderDone<Crypto> {
impl<'a, Crypto: CryptoTrait> EdhocInitiator<Crypto> {
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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion shared/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion shared/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MAX_SUITES_LEN>;
fn sha256_digest(&mut self, message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen;
fn hkdf_expand(
Expand Down
1 change: 1 addition & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ impl ErrCode {
#[repr(C)]
pub struct InitiatorStart {
pub suites_i: EdhocBuffer<MAX_SUITES_LEN>,
pub method: u8,
pub x: BytesP256ElemLen, // ephemeral private key of myself
pub g_x: BytesP256ElemLen, // ephemeral public key of myself
}
Expand Down

0 comments on commit 1794423

Please sign in to comment.