[][src]Function devolutions_crypto::key::mix_key_exchange

pub fn mix_key_exchange(
    private_key: &PrivateKey,
    public_key: &PublicKey
) -> Result<Vec<u8>, Error>

Mix a PrivateKey with another client PublicKey to get a secret shared between the two parties.

Arguments

  • private_key - The user's PrivateKey obtained through generate_keypair().
  • public_key - The peer's PublicKey.

Returns

Returns a shared secret in the form of a Vec<u8>, which can then be used as an encryption key between the two parties.

Example

use std::convert::TryFrom as _;
use devolutions_crypto::key::{PublicKey, PrivateKey, generate_keypair, mix_key_exchange, KeyVersion};

// This happens on Bob's side.
let bob_keypair = generate_keypair(KeyVersion::Latest);
let bob_serialized_pub: Vec<u8> = bob_keypair.public_key.into();

send_key_to_alice(&bob_serialized_pub);

// This happens on Alice's side.
let alice_keypair = generate_keypair(KeyVersion::Latest);
let alice_serialized_pub: Vec<u8> = alice_keypair.public_key.into();

send_key_to_bob(&alice_serialized_pub);

// Bob can now generate the shared secret.
let alice_received_serialized_pub = receive_key_from_alice();
let alice_received_pub = PublicKey::try_from(alice_received_serialized_pub.as_slice()).unwrap();

let bob_shared = mix_key_exchange(&bob_keypair.private_key, &alice_received_pub).unwrap();

// Alice can now generate the shared secret
let bob_received_serialized_pub = receive_key_from_bob();
let bob_received_pub = PublicKey::try_from(bob_received_serialized_pub.as_slice()).unwrap();

let alice_shared = mix_key_exchange(&alice_keypair.private_key, &bob_received_pub).unwrap();

// They now have a shared secret!
assert_eq!(bob_shared, alice_shared);