#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Curve25519Xsalsa20Hmacsha256;
type S = Curve25519Xsalsa20Hmacsha256;
impl super::Suite for S {
type E = generic_ec::curves::Ed25519;
type Mac = hmac::Hmac<sha2::Sha256>;
type Enc = salsa20::XSalsa20;
type Dec = salsa20::XSalsa20;
}
pub type PrivateKey = crate::PrivateKey<S>;
pub type PublicKey = crate::PublicKey<S>;
pub type EncryptedMessage<'m> = crate::EncryptedMessage<'m, S>;
impl PublicKey {
pub fn encrypt_in_place<'m>(
&self,
message: &'m mut [u8],
rng: &mut (impl rand_core::RngCore + rand_core::CryptoRng),
) -> Result<EncryptedMessage<'m>, crate::EncError> {
self.stream_encrypt_in_place(message, rng)
}
pub fn encrypt(
&self,
message: &[u8],
rng: &mut (impl rand_core::RngCore + rand_core::CryptoRng),
) -> Result<Vec<u8>, crate::EncError> {
self.stream_encrypt(message, rng)
}
}
impl PrivateKey {
pub fn decrypt_in_place<'m>(
&self,
message: EncryptedMessage<'m>,
) -> Result<&'m mut [u8], crate::DecError> {
self.stream_decrypt_in_place(message)
}
pub fn decrypt(&self, message: &EncryptedMessage<'_>) -> Result<Vec<u8>, crate::DecError> {
self.stream_decrypt(message)
}
}
#[cfg(test)]
crate::common::make_tests!("stream");
#[cfg(test)]
mod test {
#[test]
fn openssl_key() {
let key_bytes =
hex::decode("eaec3fecf6d988cd8a51bbfba5d5a310d1887459f8433fa0a17fc09f34ee77a4")
.unwrap();
let pubkey_bytes =
hex::decode("d24f3652e2100524d31ae794e781c4cd0b4f53e2bb02665b85f9c71d5e80ab69")
.unwrap();
let pubkey = super::PublicKey::from_bytes(pubkey_bytes).unwrap();
let key =
super::PrivateKey::from_eddsa_pkey_bytes(key_bytes[0..32].try_into().unwrap()).unwrap();
assert_eq!(key.public_key(), pubkey);
}
#[test]
fn compat() {
let key_bytes =
hex::decode("eaec3fecf6d988cd8a51bbfba5d5a310d1887459f8433fa0a17fc09f34ee77a4")
.unwrap();
let key =
super::PrivateKey::from_eddsa_pkey_bytes(key_bytes[0..32].try_into().unwrap()).unwrap();
let plaintext = b"Look at this very important data, I hope nothing happens to it.";
let mut ciphertext = [
92, 192, 226, 249, 116, 180, 153, 152, 175, 76, 239, 137, 146, 108, 44, 37, 235, 153,
93, 214, 162, 184, 139, 81, 116, 9, 222, 126, 28, 56, 67, 115, 240, 125, 224, 21, 125,
74, 130, 14, 122, 74, 148, 102, 17, 64, 196, 16, 37, 118, 97, 118, 106, 77, 244, 187,
167, 116, 82, 230, 180, 88, 165, 33, 235, 106, 231, 254, 195, 15, 223, 88, 182, 9, 47,
197, 182, 25, 47, 55, 238, 79, 132, 141, 236, 27, 95, 59, 91, 226, 208, 222, 159, 8,
53, 105, 82, 6, 253, 206, 83, 2, 191, 72, 8, 194, 112, 92, 18, 54, 131, 20, 34, 228,
195, 79, 169, 25, 246, 176, 181, 143, 5, 172, 230, 96, 53,
];
let message = super::EncryptedMessage::from_bytes(&mut ciphertext).unwrap();
let decrypted = key.decrypt_in_place(message).unwrap();
assert_eq!(plaintext, decrypted);
}
}