Skip to content

Commit

Permalink
derive shared secrets using ANSI-x963 (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoloki authored Nov 21, 2024
1 parent ebd7d77 commit 810394f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@ pub fn make_shared_secret(private_key: &Scalar, public_key: &Point) -> [u8; 32]

/// Create a shared secret from the passed Diffie-Hellman shared key
pub fn make_shared_secret_from_key(shared_key: &Point) -> [u8; 32] {
ansi_x963_derive_key(
shared_key.compress().as_bytes(),
"DH_SHARED_SECRET_KEY/".as_bytes(),
)
}

/// Derive a shared key using the ANSI-x963 standard
/// https://www.secg.org/sec1-v2.pdf (section 3.6.1)
pub fn ansi_x963_derive_key(shared_key: &[u8], shared_info: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
let counter = 1u32;

hasher.update("DH_SHARED_SECRET_KEY/".as_bytes());
hasher.update(shared_key.compress().as_bytes());
hasher.update(shared_key);
hasher.update(counter.to_be_bytes());
hasher.update(shared_info);

let hash = hasher.finalize();
let mut bytes = [0u8; 32];
Expand Down

0 comments on commit 810394f

Please sign in to comment.