use cfg_if::cfg_if;
use ockam_core::compat::{collections::BTreeMap, vec::Vec};
/// A handle to X25519 secret key inside a vault.
///
/// - X25519 as defined [here][1].
/// - Curve25519 as defined [here][2].
///
/// [1]: https://datatracker.ietf.org/doc/html/rfc7748
/// [2]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf
pub struct X25519SecretKeyHandle(pub HandleToSecret);
pub struct SecretBufferHandle {
pub handle: HandleToSecret,
pub length: usize,
}
/// The number of hkdf outputs to produce from the hkdf function.
pub enum HKDFNumberOfOutputs {
Two,
Three,
}
cfg_if! {
if #[cfg(feature = "OCKAM_XX_25519_ChaChaPolyBLAKE2s")] {
pub struct Blake2sOutput([u8; 32]);
pub struct HashOutput(pub Blake2sOutput);
pub struct Blake2sHkdfOutput(Vec<SecretBufferHandle>);
pub struct HkdfOutput(pub Blake2sHkdfOutput);
pub struct Chacha20Poly1305SecretKeyHandle(pub HandleToSecret);
pub struct AeadSecretKeyHandle(pub Chacha20Poly1305SecretKeyHandle);
} else if #[cfg(feature = "OCKAM_XX_25519_AES128_GCM_SHA256")] {
pub struct HashOutput(pub Sha256Output);
pub struct Sha256HkdfOutput(Vec<SecretBufferHandle>);
pub struct HkdfOutput(pub Sha256HkdfOutput);
pub struct Aes128GcmSecretKeyHandle(pub HandleToSecret);
pub struct AeadSecretKeyHandle(pub Aes128GcmSecretKeyHandle);
} else {
// OCKAM_XX_25519_AES256_GCM_SHA256
pub struct HashOutput(pub Sha256Output);
pub struct Sha256HkdfOutput(Vec<SecretBufferHandle>);
pub struct HkdfOutput(pub Sha256HkdfOutput);
pub struct Aes256GcmSecretKeyHandle(pub HandleToSecret);
pub struct AeadSecretKeyHandle(pub Aes256GcmSecretKeyHandle);
}
}
#[async_trait]
pub trait VaultForSecureChannels: Send + Sync + 'static {
/// [1]: http://www.noiseprotocol.org/noise.html#dh-functions
async fn dh(
&self,
secret_key_handle: &X25519SecretKeyHandle,
peer_public_key: &X25519PublicKey,
) -> Result<SecretBufferHandle>;
/// [1]: http://www.noiseprotocol.org/noise.html#hash-functions
async fn hash(&self, data: &[u8]) -> Result<HashOutput>;
/// [1]: http://www.noiseprotocol.org/noise.html#hash-functions
async fn hkdf(
&self,
salt: &SecretBufferHandle,
input_key_material: Option<&SecretBufferHandle>,
number_of_outputs: HKDFNumberOfOutputs,
) -> Result<HkdfOutput>;
/// AEAD Encrypt
/// [1]: http://www.noiseprotocol.org/noise.html#cipher-functions
async fn encrypt(
&self,
secret_key_handle: &AeadSecretKeyHandle,
plain_text: &[u8],
nonce: &[u8],
aad: &[u8],
) -> Result<Vec<u8>>;
/// AEAD Decrypt
/// [1]: http://www.noiseprotocol.org/noise.html#cipher-functions
async fn decrypt(
&self,
secret_key_handle: &AeadSecretKeyHandle,
cipher_text: &[u8],
nonce: &[u8],
aad: &[u8],
) -> Result<Vec<u8>>;
async fn generate_ephemeral_x25519_secret_key(&self) -> Result<X25519SecretKeyHandle>;
async fn delete_ephemeral_x25519_secret_key(
&self,
secret_key_handle: X25519SecretKeyHandle,
) -> Result<bool>;
async fn get_x25519_public_key(
&self,
secret_key_handle: &X25519SecretKeyHandle,
) -> Result<X25519PublicKey>;
async fn get_x25519_secret_key_handle(
&self,
public_key: &X25519PublicKey,
) -> Result<X25519SecretKeyHandle>;
async fn import_secret_buffer(&self, buffer: Vec<u8>) -> Result<SecretBufferHandle>;
async fn delete_secret_buffer(&self, secret_buffer_handle: SecretBufferHandle) -> Result<bool>;
async fn convert_secret_buffer_to_aead_key(
&self,
secret_buffer_handle: SecretBufferHandle,
) -> Result<AeadSecretKeyHandle>;
async fn delete_aead_secret_key(&self, secret_key_handle: AeadSecretKeyHandle) -> Result<bool>;
}