Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker-volodya committed Feb 15, 2024
1 parent dc13883 commit cfd8abe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait AdnlPrivateKey {
pub struct AdnlSecret([u8; 32]);

/// Wrapper struct to hold ADNL address, which is a hash of public key
#[derive(PartialEq)]
#[derive(PartialEq, Debug)]
pub struct AdnlAddress([u8; 32]);

impl From<[u8; 32]> for AdnlAddress {
Expand Down Expand Up @@ -169,7 +169,7 @@ pub enum AdnlError {
#[error("Incorrect ip address")]
IncorrectAddr(AddrParseError),
#[error("Receiver ADNL address mismatch")]
AddrMismatch,
UnknownAddr(AdnlAddress),
#[error(transparent)]
OtherError(#[from] Error),
}
14 changes: 12 additions & 2 deletions src/primitives/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ impl<P: AdnlPublicKey> AdnlHandshake<P> {
&self.aes_params
}

/// Get initiator public key of this handshake
pub fn sender(&self) -> &P {
&self.sender
}

/// Get destination ADNL address of this handshake
pub fn receiver(&self) -> &AdnlAddress {
&self.receiver
}

/// Serialize handshake to send it over the transport
pub fn to_bytes(&self) -> [u8; 256] {
let mut raw_params = self.aes_params.to_bytes();
Expand Down Expand Up @@ -79,15 +89,15 @@ impl<P: AdnlPublicKey> AdnlHandshake<P> {

impl AdnlHandshake<[u8; 32]> {
/// Deserialize and decrypt handshake
pub fn decrypt_from_raw<S: AdnlPrivateKey>(packet: [u8; 256], key: S) -> Result<Self, AdnlError> {
pub fn decrypt_from_raw<S: AdnlPrivateKey>(packet: &[u8], key: &S) -> Result<Self, AdnlError> {
let receiver: [u8; 32] = packet[..32].try_into().unwrap();
let receiver = AdnlAddress::from(receiver);
let sender = packet[32..64].try_into().unwrap();
let hash: [u8; 32] = packet[64..96].try_into().unwrap();
let mut raw_params: [u8; 160] = packet[96..256].try_into().unwrap();

if key.public().address() != receiver {
return Err(AdnlError::AddrMismatch)
return Err(AdnlError::UnknownAddr(receiver))
}

let secret = key.key_agreement(sender);
Expand Down
34 changes: 30 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,44 @@ fn test_handshake(
aes_params: Vec<u8>,
expected_handshake: Vec<u8>,
) {
let aes_params: [u8; 160] = aes_params.try_into().unwrap();
let aes_params = AdnlAesParams::from(aes_params);
// test serializing
let aes_params_raw: [u8; 160] = aes_params.try_into().unwrap();
let aes_params = AdnlAesParams::from(aes_params_raw);
let remote_public: [u8; 32] = remote_public.try_into().unwrap();
let local_public: [u8; 32] = local_public.try_into().unwrap();
let ecdh: [u8; 32] = ecdh.try_into().unwrap();
let ecdh = AdnlSecret::from(ecdh);
let ecdh_raw: [u8; 32] = ecdh.try_into().unwrap();
let ecdh = AdnlSecret::from(ecdh_raw);
let handshake = AdnlHandshake::new(remote_public.address(), local_public, ecdh, aes_params);
assert_eq!(
handshake.to_bytes(),
expected_handshake.as_slice(),
"handshake is not the same!"
);

// test deserializing
struct DummyKey {
ecdh: [u8; 32],
public: [u8; 32]
}

impl AdnlPrivateKey for DummyKey {
type PublicKey = [u8; 32];

fn key_agreement<P: AdnlPublicKey>(&self, _their_public: P) -> AdnlSecret {
AdnlSecret::from(self.ecdh)
}

fn public(&self) -> Self::PublicKey {
self.public
}
}

let key = DummyKey { ecdh: ecdh_raw, public: remote_public };
let handshake2 = AdnlHandshake::decrypt_from_raw(&expected_handshake, &key).expect("valid handshake");
assert_eq!(handshake2.aes_params().to_bytes(), aes_params_raw, "aes_params mismatch");
assert_eq!(handshake2.receiver(), &remote_public.address(), "receiver mismatch");
assert_eq!(handshake2.sender(), &local_public, "sender mismatch");
assert_eq!(&handshake2.to_bytes(), expected_handshake.as_slice(), "reencryption failed");
}

#[tokio::test]
Expand Down

0 comments on commit cfd8abe

Please sign in to comment.