Skip to content

Commit 7a0c2d0

Browse files
authored
Merge pull request #123 from nyonson/add-back-std-helpers
Add back allocation wrapper methods
2 parents 7b6bdf9 + 64fd811 commit 7a0c2d0

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

protocol/src/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,41 @@ impl InboundCipher {
419419

420420
Ok(PacketType::from_byte(&plaintext_buffer[0]))
421421
}
422+
423+
/// Decrypt an inbound packet with automatic allocation.
424+
///
425+
/// This is a convenience method that handles buffer allocation automatically.
426+
/// For zero-allocation scenarios, use [`decrypt`] instead.
427+
///
428+
/// # Arguments
429+
///
430+
/// * `ciphertext` - The packet from the peer excluding the first 3 length bytes. It should contain
431+
/// the header, contents, and authentication tag.
432+
/// * `aad` - Optional associated authenticated data.
433+
///
434+
/// # Returns
435+
///
436+
/// A `Result` containing:
437+
/// * `Ok((PacketType, Vec<u8>))`: The packet type and decrypted plaintext including header byte.
438+
/// * `Err(Error)`: An error that occurred during decryption.
439+
///
440+
/// # Errors
441+
///
442+
/// * `CiphertextTooSmall` - Ciphertext argument does not contain a whole packet.
443+
/// * Decryption errors for any failures such as a tag mismatch.
444+
#[cfg(feature = "std")]
445+
pub fn decrypt_to_vec(
446+
&mut self,
447+
ciphertext: &[u8],
448+
aad: Option<&[u8]>,
449+
) -> Result<(PacketType, std::vec::Vec<u8>), Error> {
450+
let plaintext_len = Self::decryption_buffer_len(ciphertext.len());
451+
let mut plaintext_buffer = std::vec![0u8; plaintext_len];
452+
453+
let packet_type = self.decrypt(ciphertext, &mut plaintext_buffer, aad)?;
454+
455+
Ok((packet_type, plaintext_buffer))
456+
}
422457
}
423458

424459
/// Encrypts packets to send to the remote peer.
@@ -490,6 +525,37 @@ impl OutboundCipher {
490525

491526
Ok(())
492527
}
528+
529+
/// Encrypt plaintext into a packet with automatic allocation.
530+
///
531+
/// This is a convenience method that handles buffer allocation automatically.
532+
/// For zero-allocation scenarios, use [`encrypt`] instead.
533+
///
534+
/// # Arguments
535+
///
536+
/// * `plaintext` - Plaintext contents to be encrypted.
537+
/// * `packet_type` - Is this a genuine packet or a decoy.
538+
/// * `aad` - Optional associated authenticated data.
539+
///
540+
/// # Returns
541+
///
542+
/// The complete encrypted packet ready for transmission.
543+
#[cfg(feature = "std")]
544+
pub fn encrypt_to_vec(
545+
&mut self,
546+
plaintext: &[u8],
547+
packet_type: PacketType,
548+
aad: Option<&[u8]>,
549+
) -> std::vec::Vec<u8> {
550+
let packet_len = Self::encryption_buffer_len(plaintext.len());
551+
let mut ciphertext_buffer = std::vec![0u8; packet_len];
552+
553+
// This will never fail since we allocate the exact required size
554+
self.encrypt(plaintext, &mut ciphertext_buffer, packet_type, aad)
555+
.expect("encrypt should never fail with correctly sized buffer");
556+
557+
ciphertext_buffer
558+
}
493559
}
494560

495561
/// Manages cipher state for a BIP324 encrypted connection.

0 commit comments

Comments
 (0)