Skip to main content

haste_encryption/
traits.rs

1use haste_fhir_operation_error::OperationOutcomeError;
2use std::{future::Future, pin::Pin};
3
4/// A secret's raw byte value. `Debug` is redacted so the value never
5/// ends up in logs or error messages by accident.
6pub struct Secret(Vec<u8>);
7
8impl Secret {
9    #[must_use]
10    pub fn new(bytes: Vec<u8>) -> Self {
11        Self(bytes)
12    }
13
14    #[must_use]
15    pub fn expose_bytes(&self) -> &[u8] {
16        &self.0
17    }
18}
19
20impl std::fmt::Debug for Secret {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        f.write_str("Secret(REDACTED)")
23    }
24}
25
26/// Retrieves secret material (encryption keys, credentials, etc.) by name
27/// from a backing store, e.g. AWS Secrets Manager, GCP Secret Manager, or
28/// environment variables.
29pub trait SecretsProvider: Sync + Send {
30    fn get_secret<'a>(
31        &'a self,
32        name: &'a str,
33    ) -> Pin<Box<dyn Future<Output = Result<Secret, OperationOutcomeError>> + Send + 'a>>;
34}
35
36pub struct EncryptionResult {
37    pub nonce: Vec<u8>,
38    pub ciphertext: Vec<u8>,
39}
40
41/// Provides symmetric authenticated encryption and decryption of arbitrary
42/// byte payloads.
43///
44/// Implementations are expected to encrypt data in a way that ensures both
45/// confidentiality and integrity. A value returned by [`Self::encrypt`]
46/// should be decryptable only by the same implementation initialized with the
47/// same keying material.
48pub trait Encryptor: Sync + Send {
49    /// Encrypts the given plaintext: [`Self::encrypt_with_aad`] with empty
50    /// associated data.
51    ///
52    /// # Errors
53    ///
54    /// Returns an [`OperationOutcomeError`] if the plaintext cannot be
55    /// encrypted.
56    fn encrypt(&self, plaintext: &[u8]) -> Result<EncryptionResult, OperationOutcomeError> {
57        self.encrypt_with_aad(plaintext, &[])
58    }
59    /// Decrypts a payload encrypted with empty associated data.
60    ///
61    /// # Errors
62    ///
63    /// Returns an [`OperationOutcomeError`] if the ciphertext is invalid,
64    /// has been tampered with, or cannot be decrypted.
65    fn decrypt(&self, ciphertext: &EncryptionResult) -> Result<Vec<u8>, OperationOutcomeError> {
66        self.decrypt_with_aad(ciphertext, &[])
67    }
68    /// Encrypts the given plaintext and binds the result to `aad`, its
69    /// associated data: what the value belongs to, such as the key of the row
70    /// that stores it. The associated data is neither secret nor part of the
71    /// result. Decryption needs the same bytes again, so a result copied to
72    /// another owner does not decrypt there.
73    ///
74    /// # Errors
75    ///
76    /// Returns an [`OperationOutcomeError`] if the plaintext cannot be
77    /// encrypted.
78    fn encrypt_with_aad(
79        &self,
80        plaintext: &[u8],
81        aad: &[u8],
82    ) -> Result<EncryptionResult, OperationOutcomeError>;
83    /// Decrypts a payload encrypted with the associated data `aad`.
84    ///
85    /// # Errors
86    ///
87    /// Returns an [`OperationOutcomeError`] if the ciphertext is invalid,
88    /// has been tampered with, was encrypted with other associated data, or
89    /// cannot be decrypted.
90    fn decrypt_with_aad(
91        &self,
92        ciphertext: &EncryptionResult,
93        aad: &[u8],
94    ) -> Result<Vec<u8>, OperationOutcomeError>;
95}