wip: shar

This commit is contained in:
Matthew LeVan 2023-12-18 12:55:13 -05:00
parent eb77dad57f
commit 561701aaf1
3 changed files with 20 additions and 49 deletions

View File

@ -55,6 +55,7 @@ version = "0.1.0"
dependencies = [
"aes-siv",
"assert_no_alloc",
"curve25519-dalek",
"ed25519",
"ed25519-dalek",
"ibig",
@ -652,26 +653,6 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]]
name = "serde"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sha2"
version = "0.10.8"
@ -888,8 +869,6 @@ checksum = "fb66477291e7e8d2b0ff1bcb900bf29489a9692816d79874bea351e7a8b6de96"
dependencies = [
"curve25519-dalek",
"rand_core",
"serde",
"zeroize",
]
[[package]]
@ -897,17 +876,3 @@ name = "zeroize"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d"
dependencies = [
"zeroize_derive",
]
[[package]]
name = "zeroize_derive"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@ -16,7 +16,8 @@ rand = { version = "0.8.5", features = ["std_rng"], default-features = false }
urcrypt-sys = { version = "0.1.1", optional = true }
sha2 = "0.10.8"
ibig = "0.3.6"
x25519-dalek = "2.0.0"
x25519-dalek = { version = "2.0.0", features = ["static_secrets"], default-features = false }
curve25519-dalek = "4.1.1"
[features]
# XX turn off test_vs_urcrypt after development

View File

@ -1,6 +1,6 @@
use rand::{rngs::StdRng, SeedableRng};
use curve25519_dalek::{edwards::CompressedEdwardsY, EdwardsPoint, MontgomeryPoint};
use ed25519_dalek::{SigningKey, VerifyingKey};
use x25519_dalek::{EphemeralSecret, PublicKey, SharedSecret};
use x25519_dalek::{PublicKey, StaticSecret};
/// Generate a public key from the given seed and write it to the given output slice.
pub fn ac_ed_puck(seed: &mut [u8; 32], out: &mut [u8; 32]) {
@ -13,11 +13,16 @@ pub fn ac_ed_puck(seed: &mut [u8; 32], out: &mut [u8; 32]) {
/// the keypair generate from the given seed, writing the resulting shared key
/// to the given output slice.
pub fn ac_ed_shar(public: &[u8; 32], seed: &[u8; 32], out: &mut [u8; 32]) {
// let self_key = SigningKey::from_bytes(seed);
let self_key = SigningKey::from_bytes(seed);
let self_secret = StaticSecret::from(self_key.to_bytes());
// let other_key = PublicKey::from(*public);
// let shared_secret = self_key.diffie_hellman(&other_key);
// *out = shared_secret.to_bytes();
if let Ok(compressed_ed_pt) = CompressedEdwardsY::from_slice(public) {
if let Some(ed_pt) = compressed_ed_pt.decompress() {
let public_key = PublicKey::from(ed_pt.to_montgomery().to_bytes());
let shared_secret = self_secret.diffie_hellman(&public_key);
*out = shared_secret.to_bytes();
}
}
}
#[cfg(test)]
@ -61,18 +66,18 @@ mod ucrypt_tests {
#[test]
fn test_ed_shar() {
let seed_src =
&ubig!(_0x4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb)
.to_le_bytes();
let mut seed: [u8; 32] = [0; 32];
seed.copy_from_slice(seed_src);
let public_key_src =
&ubig!(_0xd75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a)
.to_le_bytes();
let mut public_key: [u8; 32] = [0; 32];
public_key.copy_from_slice(public_key_src);
let seed_src =
&ubig!(_0x4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb)
.to_le_bytes();
let mut seed: [u8; 32] = [0; 32];
seed.copy_from_slice(seed_src);
let mut uc_out: [u8; 32] = [0; 32];
unsafe { urcrypt_ed_shar(public_key.as_ptr(), seed.as_ptr(), uc_out.as_mut_ptr()) };