diff options
| author | Will Dillon <william@housedillon.com> | 2025-11-08 06:54:10 +0000 |
|---|---|---|
| committer | Will Dillon <william@housedillon.com> | 2025-11-08 06:54:10 +0000 |
| commit | 6ce2b201ae021f055df181fac19ea53b41d77895 (patch) | |
| tree | 7cc778bef6969270ee571d7afd633412bcfddec1 /src/crypto.rs | |
| parent | Back over to the laptop (diff) | |
| download | meshcore-rs-6ce2b201ae021f055df181fac19ea53b41d77895.tar.gz meshcore-rs-6ce2b201ae021f055df181fac19ea53b41d77895.zip | |
Getting pretty close to finished
Diffstat (limited to 'src/crypto.rs')
| -rw-r--r-- | src/crypto.rs | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/crypto.rs b/src/crypto.rs index 8e3afa7..3175501 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -81,6 +81,12 @@ impl std::fmt::Debug for PublicKey { } } +impl PublicKey { + pub fn hash_prefix(&self) -> u8 { + self.0.as_bytes()[0] + } +} + #[derive(Clone)] pub struct SharedSecret(MontgomeryPoint); @@ -104,11 +110,15 @@ impl FromStr for SharedSecret { type Err = MeshcoreCryptoError; fn from_str(s: &str) -> Result<Self, Self::Err> { - let mut slice = [0_u8; 32]; - if decode_to_slice(s, &mut slice).is_err() { + let mut array = [0_u8; 32]; + + // The provided group secrets are only 16 bytes, + // but they're zero-paded to be 32. So, we're + // going to get the hex from the string and copy it in. + if decode_to_slice(s, &mut array[0..16]).is_err() { return Err(MeshcoreCryptoError::TryFromSliceError) } else { - Ok(SharedSecret(MontgomeryPoint(slice))) + Ok(SharedSecret(MontgomeryPoint(array))) } } } @@ -218,6 +228,10 @@ impl SharedSecret { Err(MeshcoreCryptoError::KeyLengthError) } } + + pub fn hash_prefix(&self) -> u8 { + self.0.0[0] + } } // This is just for creating placeholders @@ -297,8 +311,6 @@ impl FromStr for PrivateKey { impl From<&PrivateKey> for PublicKey { fn from(key: &PrivateKey) -> Self { - println!("Scalar: {}", encode(key.0.scalar.to_bytes())); - // let key = key.0.verifying_key(); let key = (key.0.scalar * curve25519_dalek::constants::ED25519_BASEPOINT_POINT).compress(); PublicKey(VerifyingKey::from_bytes(key.as_bytes()).unwrap()) } @@ -311,7 +323,7 @@ impl PrivateKey { } impl SharedSecret { - pub fn mac_then_decrypt(&self, mac: u16, data: Bytes) -> Option<Bytes> { + pub fn mac_then_decrypt(&self, mac: u16, data: &Bytes) -> Option<Bytes> { // Get the MAC of the message and key to check vailidity let our_mac = self.get_hmac(&data); if our_mac != mac { return None } @@ -383,7 +395,7 @@ mod tests { fn hmac() { // Test using a group secret let group_secret = SharedSecret::new_from_group_secret(Bytes::copy_from_slice(&decode("8b3387e9c5cdea6ac9e5edbaa115cd72").unwrap())); - let test_group_secret =SharedSecret::from_str("8b3387e9c5cdea6ac9e5edbaa115cd7200000000000000000000000000000000").unwrap(); + let test_group_secret =SharedSecret::from_str("8b3387e9c5cdea6ac9e5edbaa115cd72").unwrap(); assert!(group_secret == test_group_secret); // Test using the secret and ciphertext to make a MAC and ensure it matches an example for a group secret @@ -501,7 +513,7 @@ mod tests { let sample_data = Bytes::copy_from_slice(&decode("354D619BAE9590E4D177DB7EEAF982F5BDCF78005D75157D9535FA90178F785D").unwrap()); let mac = 0xC3C1; - let cleartext = group_secret.mac_then_decrypt(mac, sample_data).unwrap(); + let cleartext = group_secret.mac_then_decrypt(mac, &sample_data).unwrap(); assert!(cleartext == decode("3757d06800f09f8cb220547265653a20e29881efb88f00000000000000000000").unwrap()); } }
\ No newline at end of file |
