5dfaff6296
* Update hmac and blake3 * Remain paranoid for `0.*` crates * Most paranoid versions :) * Updated aes and using published version of blake3 Co-authored-by: Jędrzej Stuczyński <jedrzej.stuczynski@gmail.com>
71 lines
2.2 KiB
Rust
71 lines
2.2 KiB
Rust
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
pub use crypto::generic_array::typenum::Unsigned;
|
|
use crypto::{
|
|
crypto_hash,
|
|
generic_array::GenericArray,
|
|
symmetric::stream_cipher::{generate_key, CipherKey, NewCipher},
|
|
Digest,
|
|
};
|
|
use nymsphinx_params::{ReplySurbEncryptionAlgorithm, ReplySurbKeyDigestAlgorithm};
|
|
use rand::{CryptoRng, RngCore};
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
pub type EncryptionKeyDigest =
|
|
GenericArray<u8, <ReplySurbKeyDigestAlgorithm as Digest>::OutputSize>;
|
|
|
|
pub type SurbEncryptionKeySize = <ReplySurbEncryptionAlgorithm as NewCipher>::KeySize;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct SurbEncryptionKey(CipherKey<ReplySurbEncryptionAlgorithm>);
|
|
|
|
#[derive(Debug)]
|
|
pub enum SurbEncryptionKeyError {
|
|
BytesOfInvalidLengthError,
|
|
}
|
|
|
|
impl Display for SurbEncryptionKeyError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
SurbEncryptionKeyError::BytesOfInvalidLengthError => {
|
|
write!(f, "provided bytes have invalid length")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for SurbEncryptionKeyError {}
|
|
|
|
impl SurbEncryptionKey {
|
|
/// Generates fresh pseudorandom key that is going to be used by the recipient of the message
|
|
/// to encrypt payload of the reply. It is only generated when reply-SURB is attached.
|
|
pub fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
|
|
SurbEncryptionKey(generate_key::<ReplySurbEncryptionAlgorithm, _>(rng))
|
|
}
|
|
|
|
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, SurbEncryptionKeyError> {
|
|
if bytes.len() != SurbEncryptionKeySize::to_usize() {
|
|
return Err(SurbEncryptionKeyError::BytesOfInvalidLengthError);
|
|
}
|
|
|
|
Ok(SurbEncryptionKey(GenericArray::clone_from_slice(bytes)))
|
|
}
|
|
|
|
pub fn compute_digest(&self) -> EncryptionKeyDigest {
|
|
crypto_hash::compute_digest::<ReplySurbKeyDigestAlgorithm>(&self.0)
|
|
}
|
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
|
self.0.to_vec()
|
|
}
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
self.0.as_ref()
|
|
}
|
|
|
|
pub fn inner(&self) -> &CipherKey<ReplySurbEncryptionAlgorithm> {
|
|
&self.0
|
|
}
|
|
}
|