pub struct SealedMessage { /* private fields */ }Expand description
A sealed message that can only be decrypted by the intended recipient.
SealedMessage provides a public key encryption mechanism where a message
is encrypted with a symmetric key, and that key is then encapsulated using
the recipient’s public key. This ensures that only the recipient can decrypt
the message by first decapsulating the shared secret using their private
key.
Features:
- Anonymous sender: The sender’s identity is not revealed in the sealed message
- Authenticated encryption: Message integrity and authenticity are guaranteed
- Forward secrecy: Each message uses a different ephemeral key
- Post-quantum security options: Can use ML-KEM for quantum-resistant encryption
The structure internally contains:
- An
EncryptedMessagecontaining the actual encrypted data - An
EncapsulationCiphertextcontaining the encapsulated shared secret
Implementations§
Source§impl SealedMessage
impl SealedMessage
Sourcepub fn new(plaintext: impl AsRef<[u8]>, recipient: &dyn Encrypter) -> Self
pub fn new(plaintext: impl AsRef<[u8]>, recipient: &dyn Encrypter) -> Self
Creates a new SealedMessage from the given plaintext and recipient.
This method performs the following steps:
- Generates a new shared secret key and encapsulates it for the recipient
- Encrypts the plaintext using that shared secret
§Parameters
plaintext- The message data to encryptrecipient- The recipient who will be able to decrypt the message
§Returns
A new SealedMessage containing the encrypted message and encapsulated
key
§Example
use bc_components::{EncapsulationScheme, SealedMessage};
// Generate a keypair for the recipient
let (recipient_private_key, recipient_public_key) =
EncapsulationScheme::default().keypair();
// Create a sealed message for the recipient
let plaintext = b"For your eyes only";
let sealed_message = SealedMessage::new(plaintext, &recipient_public_key);
// The recipient can decrypt the message
let decrypted = sealed_message.decrypt(&recipient_private_key).unwrap();
assert_eq!(decrypted, plaintext);Sourcepub fn new_with_aad(
plaintext: impl AsRef<[u8]>,
recipient: &dyn Encrypter,
aad: Option<impl AsRef<[u8]>>,
) -> Self
pub fn new_with_aad( plaintext: impl AsRef<[u8]>, recipient: &dyn Encrypter, aad: Option<impl AsRef<[u8]>>, ) -> Self
Creates a new SealedMessage with additional authenticated data (AAD).
AAD is data that is authenticated but not encrypted. It can be used to bind the encrypted message to some context.
§Parameters
plaintext- The message data to encryptrecipient- The recipient who will be able to decrypt the messageaad- Additional authenticated data that will be bound to the encryption
§Returns
A new SealedMessage containing the encrypted message and encapsulated
key
§Example
use bc_components::{EncapsulationScheme, SealedMessage};
// Generate a keypair for the recipient
let (recipient_private_key, recipient_public_key) =
EncapsulationScheme::default().keypair();
// Create a sealed message with additional authenticated data
let plaintext = b"For your eyes only";
let aad = b"Message ID: 12345";
let sealed_message = SealedMessage::new_with_aad(
plaintext,
&recipient_public_key,
Some(aad),
);
// The recipient can decrypt the message
let decrypted = sealed_message.decrypt(&recipient_private_key).unwrap();
assert_eq!(decrypted, plaintext);Sourcepub fn new_opt(
plaintext: impl AsRef<[u8]>,
recipient: &dyn Encrypter,
aad: Option<impl AsRef<[u8]>>,
test_nonce: Option<impl AsRef<Nonce>>,
) -> Self
pub fn new_opt( plaintext: impl AsRef<[u8]>, recipient: &dyn Encrypter, aad: Option<impl AsRef<[u8]>>, test_nonce: Option<impl AsRef<Nonce>>, ) -> Self
Creates a new SealedMessage with options for testing.
This method is similar to new_with_aad but allows specifying a test
nonce, which is useful for deterministic testing.
§Parameters
plaintext- The message data to encryptrecipient- The recipient who will be able to decrypt the messageaad- Additional authenticated data that will be bound to the encryptiontest_nonce- Optional nonce for deterministic encryption (testing only)
§Returns
A new SealedMessage containing the encrypted message and encapsulated
key
Sourcepub fn decrypt(&self, private_key: &dyn Decrypter) -> Result<Vec<u8>>
pub fn decrypt(&self, private_key: &dyn Decrypter) -> Result<Vec<u8>>
Decrypts the message using the recipient’s private key.
This method performs the following steps:
- Decapsulates the shared secret using the recipient’s private key
- Uses the shared secret to decrypt the message
§Parameters
private_key- The private key of the intended recipient
§Returns
A Result containing the decrypted message data if successful,
or an error if decryption fails
§Errors
Returns an error if:
- The private key doesn’t match the one used for encapsulation
- The decapsulation process fails
- The decryption process fails (e.g., message tampering)
§Example
use bc_components::{EncapsulationScheme, SealedMessage};
// Generate keypairs for different users
let (alice_private_key, _) = EncapsulationScheme::default().keypair();
let (bob_private_key, bob_public_key) =
EncapsulationScheme::default().keypair();
// Alice sends a message to Bob
let plaintext = b"Secret message for Bob";
let sealed_message = SealedMessage::new(plaintext, &bob_public_key);
// Bob can decrypt the message
let decrypted = sealed_message.decrypt(&bob_private_key).unwrap();
assert_eq!(decrypted, plaintext);
// Alice cannot decrypt the message she sent
assert!(sealed_message.decrypt(&alice_private_key).is_err());Sourcepub fn encapsulation_scheme(&self) -> EncapsulationScheme
pub fn encapsulation_scheme(&self) -> EncapsulationScheme
Returns the encapsulation scheme used for this sealed message.
§Returns
The encapsulation scheme (X25519, MLKEM512, MLKEM768, or MLKEM1024) that was used to create this sealed message.
§Example
use bc_components::{EncapsulationScheme, SealedMessage};
// Generate a keypair using ML-KEM768
let (_, public_key) = EncapsulationScheme::MLKEM768.keypair();
// Create a sealed message
let sealed_message =
SealedMessage::new(b"Quantum-resistant message", &public_key);
// Check the encapsulation scheme
assert_eq!(
sealed_message.encapsulation_scheme(),
EncapsulationScheme::MLKEM768
);Trait Implementations§
Source§impl AsRef<SealedMessage> for SealedMessage
Implementation of AsRef trait for SealedMessage.
impl AsRef<SealedMessage> for SealedMessage
Implementation of AsRef trait for SealedMessage.
Source§fn as_ref(&self) -> &SealedMessage
fn as_ref(&self) -> &SealedMessage
Source§impl CBORTagged for SealedMessage
Implementation of CBOR tagging for SealedMessage.
impl CBORTagged for SealedMessage
Implementation of CBOR tagging for SealedMessage.
Source§impl CBORTaggedDecodable for SealedMessage
Implementation of CBOR decoding for SealedMessage.
impl CBORTaggedDecodable for SealedMessage
Implementation of CBOR decoding for SealedMessage.
Source§fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for SealedMessage
Implementation of CBOR encoding for SealedMessage.
impl CBORTaggedEncodable for SealedMessage
Implementation of CBOR encoding for SealedMessage.
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl Clone for SealedMessage
impl Clone for SealedMessage
Source§fn clone(&self) -> SealedMessage
fn clone(&self) -> SealedMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SealedMessage
impl Debug for SealedMessage
Source§impl From<SealedMessage> for CBOR
Conversion from SealedMessage to CBOR for serialization.
impl From<SealedMessage> for CBOR
Conversion from SealedMessage to CBOR for serialization.
Source§fn from(value: SealedMessage) -> Self
fn from(value: SealedMessage) -> Self
Source§impl PartialEq for SealedMessage
impl PartialEq for SealedMessage
Source§impl TryFrom<CBOR> for SealedMessage
Conversion from CBOR to SealedMessage for deserialization.
impl TryFrom<CBOR> for SealedMessage
Conversion from CBOR to SealedMessage for deserialization.