Skip to content

Commit

Permalink
refactor: move cbor helpers to consts crate
Browse files Browse the repository at this point in the history
  • Loading branch information
geonnave committed Oct 13, 2023
1 parent 7d33db3 commit 9930ec6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
41 changes: 41 additions & 0 deletions consts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]

pub use cbor::*;
pub use consts::*;
pub use structs::*;

Expand Down Expand Up @@ -192,3 +193,43 @@ mod structs {
}
}
}

mod cbor {
use super::consts::*;

/// Check for: an unsigned integer encoded as a single byte
#[inline(always)]
pub fn is_cbor_uint_1byte(byte: u8) -> bool {
return byte >= CBOR_UINT_1BYTE_START && byte <= CBOR_UINT_1BYTE_END;
}

/// Check for: an unsigned integer encoded as two bytes
#[inline(always)]
pub fn is_cbor_uint_2bytes(byte: u8) -> bool {
return byte == CBOR_UINT_1BYTE;
}

/// Check for: a negative integer encoded as a single byte
#[inline(always)]
pub fn is_cbor_neg_int_1byte(byte: u8) -> bool {
return byte >= CBOR_NEG_INT_1BYTE_START && byte <= CBOR_NEG_INT_1BYTE_END;
}

/// Check for: a bstr denoted by a single byte which encodes both type and content length
#[inline(always)]
pub fn is_cbor_bstr_1byte_prefix(byte: u8) -> bool {
return byte >= CBOR_MAJOR_BYTE_STRING && byte <= CBOR_MAJOR_BYTE_STRING_MAX;
}

/// Check for: a bstr denoted by two bytes, one for type the other for content length
#[inline(always)]
pub fn is_cbor_bstr_2bytes_prefix(byte: u8) -> bool {
return byte == CBOR_BYTE_STRING;
}

/// Check for: an array denoted by a single byte which encodes both type and content length
#[inline(always)]
pub fn is_cbor_array_1byte_prefix(byte: u8) -> bool {
return byte >= CBOR_MAJOR_ARRAY && byte <= CBOR_MAJOR_ARRAY_MAX;
}
}
10 changes: 6 additions & 4 deletions ead/edhoc-ead-zeroconf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ fn parse_voucher_response(

let array_size = voucher_response.content[0] - CBOR_MAJOR_ARRAY;

if !(array_size == 2 || array_size == 3) || voucher_response.content[1] != CBOR_BYTE_STRING {
if !(array_size == 2 || array_size == 3)
|| !is_cbor_bstr_2bytes_prefix(voucher_response.content[1])
{
return Err(());
}

Expand All @@ -381,7 +383,7 @@ fn parse_voucher_response(
);

if array_size == 3 {
if voucher_response.content[5 + message_1.len + voucher.len] != CBOR_BYTE_STRING {
if !is_cbor_bstr_2bytes_prefix(voucher_response.content[5 + message_1.len + voucher.len]) {
return Err(());
}
let mut opaque_state = EdhocMessageBuffer::new();
Expand Down Expand Up @@ -475,15 +477,15 @@ fn parse_voucher_request(

let array_size = vreq.content[0] - CBOR_MAJOR_ARRAY;

if (array_size != 1 && array_size != 2) || vreq.content[1] != CBOR_BYTE_STRING {
if (array_size != 1 && array_size != 2) || !is_cbor_bstr_2bytes_prefix(vreq.content[1]) {
return Err(());
}

message_1.len = vreq.content[2] as usize;
message_1.content[..message_1.len].copy_from_slice(&vreq.content[3..3 + message_1.len]);

if array_size == 2 {
if vreq.content[3 + message_1.len] != CBOR_BYTE_STRING {
if !is_cbor_bstr_2bytes_prefix(vreq.content[3 + message_1.len]) {
return Err(());
}
let mut opaque_state: EdhocMessageBuffer = EdhocMessageBuffer::new();
Expand Down
36 changes: 0 additions & 36 deletions lib/src/edhoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,42 +657,6 @@ pub fn construct_state(
)
}

/// Check for: an unsigned integer encoded as a single byte
#[inline(always)]
fn is_cbor_uint_1byte(byte: u8) -> bool {
return byte >= CBOR_UINT_1BYTE_START && byte <= CBOR_UINT_1BYTE_END;
}

/// Check for: an unsigned integer encoded as two bytes
#[inline(always)]
fn is_cbor_uint_2bytes(byte: u8) -> bool {
return byte == CBOR_UINT_1BYTE;
}

/// Check for: a negative integer encoded as a single byte
#[inline(always)]
fn is_cbor_neg_int_1byte(byte: u8) -> bool {
return byte >= CBOR_NEG_INT_1BYTE_START && byte <= CBOR_NEG_INT_1BYTE_END;
}

/// Check for: a bstr denoted by a single byte which encodes both type and content length
#[inline(always)]
fn is_cbor_bstr_1byte_prefix(byte: u8) -> bool {
return byte >= CBOR_MAJOR_BYTE_STRING && byte <= CBOR_MAJOR_BYTE_STRING_MAX;
}

/// Check for: a bstr denoted by two bytes, one for type the other for content length
#[inline(always)]
fn is_cbor_bstr_2bytes_prefix(byte: u8) -> bool {
return byte == CBOR_BYTE_STRING;
}

/// Check for: an array denoted by a single byte which encodes both type and content length
#[inline(always)]
fn is_cbor_array_1byte_prefix(byte: u8) -> bool {
return byte >= CBOR_MAJOR_ARRAY && byte <= CBOR_MAJOR_ARRAY_MAX;
}

fn parse_suites_i(
rcvd_message_1: &BufferMessage1,
) -> Result<(BytesSuites, usize, usize), EDHOCError> {
Expand Down

0 comments on commit 9930ec6

Please sign in to comment.