Skip to content

Commit

Permalink
refactor: adjust namespaces after removing hacspec
Browse files Browse the repository at this point in the history
  • Loading branch information
geonnave committed Oct 2, 2023
1 parent dcb71b9 commit 780c01a
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 417 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:


hacspec-to-fstar:
# TODO: rename to `generate-fstar` and adjust to use the main library
needs: unit-tests
runs-on: ubuntu-latest
steps:
Expand Down
181 changes: 88 additions & 93 deletions consts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,81 @@
#![no_std]

pub use common::*;
pub use consts::*;
pub use structs::*;

#[cfg(feature = "hacspec")]
pub use hacspec::*;
mod consts {
use super::structs::*;

#[cfg(feature = "rust")]
pub use rust::*;
pub const MAX_MESSAGE_SIZE_LEN: usize = 64;
pub const MAX_EAD_SIZE_LEN: usize = 64;
pub type EADMessageBuffer = EdhocMessageBuffer; // TODO: make it of size MAX_EAD_SIZE_LEN
pub const EAD_ZEROCONF_LABEL: u8 = 0x1; // NOTE: in lake-authz-draft-02 it is still TBD1

pub const ID_CRED_LEN: usize = 4;
pub const SUITES_LEN: usize = 9;
pub const SUPPORTED_SUITES_LEN: usize = 1;
pub const EDHOC_METHOD: u8 = 3u8; // stat-stat is the only supported method
pub const P256_ELEM_LEN: usize = 32;
pub const SHA256_DIGEST_LEN: usize = 32;
pub const AES_CCM_KEY_LEN: usize = 16;
pub const AES_CCM_IV_LEN: usize = 13;
pub const AES_CCM_TAG_LEN: usize = 8;
pub const MAC_LENGTH_2: usize = 8;
pub const MAC_LENGTH_3: usize = MAC_LENGTH_2;

// maximum supported length of connection identifier for R
pub const MAX_KDF_CONTEXT_LEN: usize = 150;
pub const MAX_KDF_LABEL_LEN: usize = 15; // for "KEYSTREAM_2"
pub const MAX_BUFFER_LEN: usize = 220;
pub const CBOR_BYTE_STRING: u8 = 0x58u8;
pub const CBOR_UINT_1BYTE: u8 = 0x18u8;
pub const CBOR_NEG_INT_1BYTE_START: u8 = 0x20u8;
pub const CBOR_NEG_INT_1BYTE_END: u8 = 0x37u8;
pub const CBOR_UINT_1BYTE_START: u8 = 0x0u8;
pub const CBOR_UINT_1BYTE_END: u8 = 0x17u8;
pub const CBOR_MAJOR_TEXT_STRING: u8 = 0x60u8;
pub const CBOR_MAJOR_BYTE_STRING: u8 = 0x40u8;
pub const CBOR_MAJOR_BYTE_STRING_MAX: u8 = 0x57u8;
pub const CBOR_MAJOR_ARRAY: u8 = 0x80u8;
pub const CBOR_MAJOR_ARRAY_MAX: u8 = 0x97u8;
pub const MAX_INFO_LEN: usize = 2 + SHA256_DIGEST_LEN + // 32-byte digest as bstr
1 + MAX_KDF_LABEL_LEN + // label <24 bytes as tstr
1 + MAX_KDF_CONTEXT_LEN + // context <24 bytes as bstr
1; // length as u8

pub const ENC_STRUCTURE_LEN: usize = 8 + 5 + SHA256_DIGEST_LEN; // 8 for ENCRYPT0

pub const EDHOC_SUITES: BytesSuites = [0, 1, 2, 3, 4, 5, 6, 24, 25]; // all but private cipher suites
pub const EDHOC_SUPPORTED_SUITES: BytesSupportedSuites = [0x2u8];
}

mod common {
mod structs {
use super::consts::*;

pub type U8 = u8; // TODO: remove
pub type BytesEad2 = [u8; 0];
pub type BytesIdCred = [u8; ID_CRED_LEN];
pub type BytesSuites = [u8; SUITES_LEN];
pub type BytesSupportedSuites = [u8; SUPPORTED_SUITES_LEN];
pub type Bytes8 = [u8; 8];
pub type BytesCcmKeyLen = [u8; AES_CCM_KEY_LEN];
pub type BytesCcmIvLen = [u8; AES_CCM_IV_LEN];
pub type BufferPlaintext2 = EdhocMessageBuffer;
pub type BufferPlaintext3 = EdhocMessageBuffer;
pub type BytesMac2 = [u8; MAC_LENGTH_2];
pub type BytesMac3 = [u8; MAC_LENGTH_3];
pub type BufferMessage1 = EdhocMessageBuffer;
pub type BufferMessage3 = EdhocMessageBuffer;
pub type BufferCiphertext2 = EdhocMessageBuffer;
pub type BufferCiphertext3 = EdhocMessageBuffer;
pub type BytesHashLen = [u8; SHA256_DIGEST_LEN];
pub type BytesP256ElemLen = [u8; P256_ELEM_LEN];
pub type BufferMessage2 = EdhocMessageBuffer;
pub type BytesMaxBuffer = [u8; MAX_BUFFER_LEN];
pub type BytesMaxContextBuffer = [u8; MAX_KDF_CONTEXT_LEN];
pub type BytesMaxInfoBuffer = [u8; MAX_INFO_LEN];
pub type BytesMaxLabelBuffeer = [u8; MAX_KDF_LABEL_LEN];
pub type BytesEncStructureLen = [u8; ENC_STRUCTURE_LEN];

#[repr(C)]
#[derive(Default, PartialEq, Copy, Clone, Debug)]
Expand Down Expand Up @@ -36,6 +103,21 @@ mod common {
UnknownError = 8,
}

#[repr(C)]
#[derive(Default, Copy, Clone, Debug)]
pub struct State(
pub EDHOCState,
pub BytesP256ElemLen, // x or y, ephemeral private key of myself
pub u8, // c_i, connection identifier chosen by the initiator
pub BytesP256ElemLen, // g_y or g_x, ephemeral public key of the peer
pub BytesHashLen, // prk_3e2m
pub BytesHashLen, // prk_4e3m
pub BytesHashLen, // prk_out
pub BytesHashLen, // prk_exporter
pub BytesHashLen, // h_message_1
pub BytesHashLen, // th_3
);

#[repr(C)]
#[derive(PartialEq, Debug)]
pub struct EdhocMessageBuffer {
Expand Down Expand Up @@ -107,91 +189,4 @@ mod common {
}
}
}

pub const MAX_MESSAGE_SIZE_LEN: usize = 64;
pub const MAX_EAD_SIZE_LEN: usize = 64;
pub type EADMessageBuffer = EdhocMessageBuffer; // TODO: make it of size MAX_EAD_SIZE_LEN
pub const EAD_ZEROCONF_LABEL: u8 = 0x1; // NOTE: in lake-authz-draft-02 it is still TBD1

pub const ID_CRED_LEN: usize = 4;
pub const SUITES_LEN: usize = 9;
pub const SUPPORTED_SUITES_LEN: usize = 1;
pub const EDHOC_METHOD: u8 = 3u8; // stat-stat is the only supported method
pub const P256_ELEM_LEN: usize = 32;
pub const SHA256_DIGEST_LEN: usize = 32;
pub const AES_CCM_KEY_LEN: usize = 16;
pub const AES_CCM_IV_LEN: usize = 13;
pub const AES_CCM_TAG_LEN: usize = 8;
pub const MAC_LENGTH_2: usize = 8;
pub const MAC_LENGTH_3: usize = MAC_LENGTH_2;

// maximum supported length of connection identifier for R
pub const MAX_KDF_CONTEXT_LEN: usize = 150;
pub const MAX_KDF_LABEL_LEN: usize = 15; // for "KEYSTREAM_2"
pub const MAX_BUFFER_LEN: usize = 220;
pub const CBOR_BYTE_STRING: u8 = 0x58u8;
pub const CBOR_UINT_1BYTE: u8 = 0x18u8;
pub const CBOR_NEG_INT_1BYTE_START: u8 = 0x20u8;
pub const CBOR_NEG_INT_1BYTE_END: u8 = 0x37u8;
pub const CBOR_UINT_1BYTE_START: u8 = 0x0u8;
pub const CBOR_UINT_1BYTE_END: u8 = 0x17u8;
pub const CBOR_MAJOR_TEXT_STRING: u8 = 0x60u8;
pub const CBOR_MAJOR_BYTE_STRING: u8 = 0x40u8;
pub const CBOR_MAJOR_BYTE_STRING_MAX: u8 = 0x57u8;
pub const CBOR_MAJOR_ARRAY: u8 = 0x80u8;
pub const CBOR_MAJOR_ARRAY_MAX: u8 = 0x97u8;
pub const MAX_INFO_LEN: usize = 2 + SHA256_DIGEST_LEN + // 32-byte digest as bstr
1 + MAX_KDF_LABEL_LEN + // label <24 bytes as tstr
1 + MAX_KDF_CONTEXT_LEN + // context <24 bytes as bstr
1; // length as u8

pub const ENC_STRUCTURE_LEN: usize = 8 + 5 + SHA256_DIGEST_LEN; // 8 for ENCRYPT0
}

#[cfg(feature = "rust")]
mod rust {
use super::common::*;

pub type U8 = u8;
pub type BytesEad2 = [u8; 0];
pub type BytesIdCred = [u8; ID_CRED_LEN];
pub type BytesSuites = [u8; SUITES_LEN];
pub type BytesSupportedSuites = [u8; SUPPORTED_SUITES_LEN];
pub type Bytes8 = [u8; 8];
pub type BytesCcmKeyLen = [u8; AES_CCM_KEY_LEN];
pub type BytesCcmIvLen = [u8; AES_CCM_IV_LEN];
pub type BufferPlaintext2 = EdhocMessageBuffer;
pub type BufferPlaintext3 = EdhocMessageBuffer;
pub type BytesMac2 = [u8; MAC_LENGTH_2];
pub type BytesMac3 = [u8; MAC_LENGTH_3];
pub type BufferMessage1 = EdhocMessageBuffer;
pub type BufferMessage3 = EdhocMessageBuffer;
pub type BufferCiphertext2 = EdhocMessageBuffer;
pub type BufferCiphertext3 = EdhocMessageBuffer;
pub type BytesHashLen = [u8; SHA256_DIGEST_LEN];
pub type BytesP256ElemLen = [u8; P256_ELEM_LEN];
pub type BufferMessage2 = EdhocMessageBuffer;
pub type BytesMaxBuffer = [u8; MAX_BUFFER_LEN];
pub type BytesMaxContextBuffer = [u8; MAX_KDF_CONTEXT_LEN];
pub type BytesMaxInfoBuffer = [u8; MAX_INFO_LEN];
pub type BytesMaxLabelBuffeer = [u8; MAX_KDF_LABEL_LEN];
pub type BytesEncStructureLen = [u8; ENC_STRUCTURE_LEN];

pub const EDHOC_SUITES: BytesSuites = [0, 1, 2, 3, 4, 5, 6, 24, 25]; // all but private cipher suites
pub const EDHOC_SUPPORTED_SUITES: BytesSupportedSuites = [0x2u8];

#[repr(C)]
#[derive(Default, Copy, Clone, Debug)]
pub struct State(
pub EDHOCState,
pub BytesP256ElemLen, // x or y, ephemeral private key of myself
pub u8, // c_i, connection identifier chosen by the initiator
pub BytesP256ElemLen, // g_y or g_x, ephemeral public key of the peer
pub BytesHashLen, // prk_3e2m
pub BytesHashLen, // prk_4e3m
pub BytesHashLen, // prk_out
pub BytesHashLen, // prk_exporter
pub BytesHashLen, // h_message_1
pub BytesHashLen, // th_3
);
}
14 changes: 7 additions & 7 deletions lib/src/c_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rust::*;
use crate::*;
use core::{slice, str};
use edhoc_consts::*;

Expand Down Expand Up @@ -37,8 +37,8 @@ pub struct EdhocInitiatorC {
}

impl EdhocInitiatorC {
pub fn to_rust(&self) -> RustEdhocInitiator {
RustEdhocInitiator::new(
pub fn to_rust(&self) -> EdhocInitiator {
EdhocInitiator::new(
self.state,
unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.i, self.i_len)) },
unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.g_r, self.g_r_len)) },
Expand Down Expand Up @@ -76,8 +76,8 @@ pub struct EdhocResponderC {
}

impl EdhocResponderC {
pub fn to_rust(&self) -> RustEdhocResponder {
RustEdhocResponder::new(
pub fn to_rust(&self) -> EdhocResponder {
EdhocResponder::new(
self.state,
unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.r, self.r_len)) },
unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.g_i, self.g_i_len)) },
Expand Down Expand Up @@ -112,7 +112,7 @@ pub unsafe extern "C" fn responder_new(
cred_r: *const u8,
cred_r_len: usize,
) -> EdhocResponderC {
RustEdhocResponder::new(
EdhocResponder::new(
State::default(),
str::from_utf8_unchecked(slice::from_raw_parts(r, r_len)),
str::from_utf8_unchecked(slice::from_raw_parts(g_i, g_i_len)),
Expand All @@ -139,7 +139,7 @@ pub unsafe extern "C" fn initiator_new(
cred_r: *const u8,
cred_r_len: usize,
) -> EdhocInitiatorC {
RustEdhocInitiator::new(
EdhocInitiator::new(
State::default(),
str::from_utf8_unchecked(slice::from_raw_parts(i, i_len)),
str::from_utf8_unchecked(slice::from_raw_parts(g_r, g_r_len)),
Expand Down
Loading

0 comments on commit 780c01a

Please sign in to comment.