Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate EC point for G_X #108

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions consts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ mod structs {
MacVerificationFailed = 2,
UnsupportedMethod = 3,
UnsupportedCipherSuite = 4,
ParsingError = 5,
WrongState = 6,
EADError = 7,
UnknownError = 8,
InvalidPublicKey = 5,
ParsingError = 6,
WrongState = 7,
EADError = 8,
UnknownError = 9,
}

#[repr(C)]
Expand Down
4 changes: 4 additions & 0 deletions crypto/edhoc-crypto-cryptocell310-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,7 @@ pub fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {

(private_key, public_key)
}

pub fn p256_validate_compact_public_key(public_key: &BytesP256ElemLen) -> bool {
true
}
10 changes: 10 additions & 0 deletions crypto/edhoc-crypto-hacspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ pub fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
(private_key.to_public_array(), public_key.to_public_array())
}

pub fn p256_validate_compact_public_key(public_key: &BytesP256ElemLen) -> bool {
let public_key = BytesP256ElemLenHacspec::from_public_slice(public_key);
let point = (
P256FieldElement::from_byte_seq_be(&public_key),
p256_calculate_w(P256FieldElement::from_byte_seq_be(&public_key)),
);

p256_validate_public_key(point)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 4 additions & 0 deletions crypto/edhoc-crypto-psa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ pub fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
(private_key, public_key)
}

pub fn p256_validate_compact_public_key(public_key: &BytesP256ElemLen) -> bool {
true
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
60 changes: 32 additions & 28 deletions lib/src/edhoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,36 +120,40 @@ pub fn r_process_message_1(
if method == EDHOC_METHOD {
// Step 2: verify that the selected cipher suite is supported
if suites_i[suites_i_len - 1] == EDHOC_SUPPORTED_SUITES[0] {
// Step 3: If EAD is present make it available to the application
let ead_success = if let Some(ead_1) = ead_1 {
r_process_ead_1(ead_1).is_ok()
} else {
true
};
if ead_success {
// hash message_1 and save the hash to the state to avoid saving the whole message
let mut message_1_buf: BytesMaxBuffer = [0x00; MAX_BUFFER_LEN];
message_1_buf[..message_1.len]
.copy_from_slice(&message_1.content[..message_1.len]);
h_message_1 = sha256_digest(&message_1_buf, message_1.len);
if p256_validate_compact_public_key(&g_x) {
// Step 3: If EAD is present make it available to the application
let ead_success = if let Some(ead_1) = ead_1 {
r_process_ead_1(ead_1).is_ok()
} else {
true
};
if ead_success {
// hash message_1 and save the hash to the state to avoid saving the whole message
let mut message_1_buf: BytesMaxBuffer = [0x00; MAX_BUFFER_LEN];
message_1_buf[..message_1.len]
.copy_from_slice(&message_1.content[..message_1.len]);
h_message_1 = sha256_digest(&message_1_buf, message_1.len);

error = EDHOCError::Success;
current_state = EDHOCState::ProcessedMessage1;

state = construct_state(
current_state,
_y,
c_i,
g_x,
_prk_3e2m,
_prk_4e3m,
_prk_out,
_prk_exporter,
h_message_1,
_th_3,
);
error = EDHOCError::Success;
current_state = EDHOCState::ProcessedMessage1;

state = construct_state(
current_state,
_y,
c_i,
g_x,
_prk_3e2m,
_prk_4e3m,
_prk_out,
_prk_exporter,
h_message_1,
_th_3,
);
} else {
error = EDHOCError::EADError;
}
} else {
error = EDHOCError::EADError;
error = EDHOCError::InvalidPublicKey;
}
} else {
error = EDHOCError::UnsupportedCipherSuite;
Expand Down
21 changes: 21 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ mod test {
const MESSAGE_1_TV: &str =
"0382060258208af6f430ebe18d34184017a9a11bf511c8dff8f834730b96c1b7c8dbca2fc3b637";

// invalid test vectors, crypto-related
const MESSAGE_1_INVALID_G_X_NOT_ON_P256_CURVE_TV: &str =
"03025820a04e73601df544a70ba7ea1e57030f7d4b4eb7f673924e58d54ca77a5e7d4d4a0e";

#[test]
fn test_new_initiator() {
let state: EdhocState = Default::default();
Expand Down Expand Up @@ -403,6 +407,23 @@ mod test {
assert!(error.is_ok());
}

#[test]
fn test_process_message_1_invalid_traces_crypto() {
let message_1_tv = EdhocMessageBuffer::from_hex(MESSAGE_1_INVALID_G_X_NOT_ON_P256_CURVE_TV);
let mut responder = EdhocResponder::new(
Default::default(),
R,
G_I,
ID_CRED_I,
CRED_I,
ID_CRED_R,
CRED_R,
);
let error = responder.process_message_1(&message_1_tv);
assert!(error.is_err());
assert_eq!(error.unwrap_err(), EDHOCError::InvalidPublicKey);
}

#[test]
fn test_generate_connection_identifier() {
let conn_id = generate_connection_identifier();
Expand Down
Loading