From 8cc7057d8164e6677409145e4bfa0b017ac35a09 Mon Sep 17 00:00:00 2001 From: Geovane Fedrecheski Date: Tue, 28 Nov 2023 16:35:59 +0100 Subject: [PATCH 1/2] feat: use crate p256 to decompress the public key Doing this since psa does not support compressed form. This has an overhead of around 14KB due to pulling the p256 crate. --- crypto/edhoc-crypto-psa/Cargo.toml | 1 + crypto/edhoc-crypto-psa/src/lib.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crypto/edhoc-crypto-psa/Cargo.toml b/crypto/edhoc-crypto-psa/Cargo.toml index f6c5b5f1..b12f406d 100644 --- a/crypto/edhoc-crypto-psa/Cargo.toml +++ b/crypto/edhoc-crypto-psa/Cargo.toml @@ -10,6 +10,7 @@ repository.workspace = true [dependencies] edhoc-consts.workspace = true psa-crypto = { version = "0.9.2" } +p256 = { version = "0.13.2", default-features = false, features = [ "ecdh" ] } [features] baremetal = [ "psa-crypto/baremetal" ] diff --git a/crypto/edhoc-crypto-psa/src/lib.rs b/crypto/edhoc-crypto-psa/src/lib.rs index eefc8acc..aad33987 100644 --- a/crypto/edhoc-crypto-psa/src/lib.rs +++ b/crypto/edhoc-crypto-psa/src/lib.rs @@ -1,6 +1,8 @@ #![no_std] use edhoc_consts::{Crypto as CryptoTrait, *}; +use p256::elliptic_curve::point::DecompressPoint; +use p256::elliptic_curve::sec1::ToEncodedPoint; use psa_crypto::operations::hash::hash_compute; use psa_crypto::operations::{aead, key_agreement, key_management, other::generate_random}; use psa_crypto::types::algorithm::Hash; @@ -174,9 +176,13 @@ impl CryptoTrait for Crypto { private_key: &BytesP256ElemLen, public_key: &BytesP256ElemLen, ) -> BytesP256ElemLen { - let mut peer_public_key: [u8; 33] = [0; 33]; - peer_public_key[0] = 0x02; // sign does not matter for ECDH operation - peer_public_key[1..33].copy_from_slice(&public_key[..]); + let peer_public_key = p256::AffinePoint::decompress( + public_key.into(), + 1.into(), /* Y coordinate choice does not matter for ECDH operation */ + ) + .unwrap() + .to_encoded_point(false); + let peer_public_key = peer_public_key.as_bytes(); let alg = RawKeyAgreement::Ecdh; let mut usage_flags: UsageFlags = Default::default(); From 54ea9dbb1fc7235e7fcb3759c6a36b6c0d3569e6 Mon Sep 17 00:00:00 2001 From: Geovane Fedrecheski Date: Tue, 28 Nov 2023 17:09:09 +0100 Subject: [PATCH 2/2] chore: update rust-psa (patched only for baremetal) --- Cargo.toml | 2 +- crypto/edhoc-crypto-psa/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 61c86bb6..eb435faa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,4 +61,4 @@ hacspec-hkdf = { git = "https://github.com/malishav/hacspec", branch = "aesccm" hacspec-sha256 = { git = "https://github.com/malishav/hacspec", branch = "aesccm" } hacspec-aes = { git = "https://github.com/malishav/hacspec", branch = "aesccm" } hacspec-aes-ccm = { git = "https://github.com/malishav/hacspec", branch = "aesccm" } -psa-crypto = { git = "https://github.com/malishav/rust-psa-crypto", branch = "baremetal" } +psa-crypto = { git = "https://github.com/geonnave/rust-psa-crypto", branch = "have-no-std-and-baremetal-features" } diff --git a/crypto/edhoc-crypto-psa/Cargo.toml b/crypto/edhoc-crypto-psa/Cargo.toml index b12f406d..a3acb08b 100644 --- a/crypto/edhoc-crypto-psa/Cargo.toml +++ b/crypto/edhoc-crypto-psa/Cargo.toml @@ -9,7 +9,7 @@ repository.workspace = true [dependencies] edhoc-consts.workspace = true -psa-crypto = { version = "0.9.2" } +psa-crypto = { version = "0.12.0" } p256 = { version = "0.13.2", default-features = false, features = [ "ecdh" ] } [features]