// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0 > or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT >, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[ cfg(not(feature =
"disable-encryption" ))]
use std::os::raw::c_char;
#[ cfg(not(feature =
"disable-encryption" ))]
use std::ptr::null;
use std::{
os::raw::{c_int, c_uint},
ptr::null_mut,
};
#[ cfg(feature =
"disable-encryption" )]
pub use recprot::AEAD_NULL_TAG;
pub use recprot::RecordProtection;
use crate ::{
Cipher, SECItemBorrowed, SymKey,
constants::{TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_
SHA256},
err::{Error, Res, sec::SEC_ERROR_BAD_DATA},
p11::{
self , CK_ATTRIBUTE_TYPE, CK_GENERATOR_FUNCTION, CK_MECHANISM_TYPE, CKA_DECRYPT,
CKA_ENCRYPT, CKA_NSS_MESSAGE, CKG_GENERATE_COUNTER_XOR, CKG_NO_GENERATE, CKM_AES_GCM,
CKM_CHACHA20_POLY1305, Context, PK11_AEADOp, PK11_CreateContextBySymKey,
},
secstatus_to_res,
};
#[ cfg(not(feature = "disable-encryption" ))]
use crate ::{
Version,
hp::SSL_HkdfExpandLabelWithMech,
p11::{CKM_HKDF_DATA, PK11SymKey},
};
#[ cfg(all(feature = "blapi" , feature = "disable-encryption" ))]
compile_error!("`blapi` and `disable-encryption` are mutually exclusive features" );
/// Shared API contract for all `RecordProtection` backends.
///
/// Implemented by each cfg-selected `recprot*.rs` backend so that a
/// signature change in one backend is caught at compile time across all.
/// Import this trait to call AEAD methods on `RecordProtection`.
pub trait RecordProtectionOps {
/// Get the expansion size (authentication tag length) for this AEAD.
#[ must_use]
fn expansion(&self ) -> usize;
/// Encrypt plaintext with associated data.
///
/// # Errors
///
/// Returns `Error` when encryption fails.
fn encrypt<'a>(
&self ,
count: u64,
aad: &[u8],
input: &[u8],
output: &'a mut [u8],
) -> Res<&'a [u8]>;
/// Encrypt plaintext in place with associated data.
///
/// # Errors
///
/// Returns `Error` when encryption fails.
fn encrypt_in_place(&self , count: u64, aad: &[u8], data: &e='color:red'>mut [u8]) -> Res<usize>;
/// Decrypt ciphertext with associated data.
///
/// # Errors
///
/// Returns `Error` when decryption or authentication fails.
fn decrypt<'a>(
&self ,
count: u64,
aad: &[u8],
input: &[u8],
output: &'a mut [u8],
) -> Res<&'a [u8]>;
/// Decrypt ciphertext in place with associated data.
///
/// # Errors
///
/// Returns `Error` when decryption or authentication fails.
fn decrypt_in_place(&self , count: u64, aad: &[u8], data: &e='color:red'>mut [u8]) -> Res<usize>;
}
#[ cfg_attr(feature = "disable-encryption" , path = "recprot_null.rs" )]
#[ cfg_attr(feature = "blapi" , path = "recprot_blapi.rs" )]
mod recprot;
#[ cfg(not(feature = "disable-encryption" ))]
fn expand_label(
version: Version,
cipher: Cipher,
secret: &SymKey,
label: &str,
mech: CK_MECHANISM_TYPE,
key_len: c_uint,
) -> Res<SymKey> {
let mut ptr: *mut PK11SymKey = null_mut();
unsafe {
SSL_HkdfExpandLabelWithMech(
version,
cipher,
**secret,
null(),
0 ,
label.as_ptr().cast::<c_char>(),
c_uint::try_from(label.len())?,
mech,
key_len,
&raw mut ptr,
)
}?;
SymKey::from_ptr(ptr)
}
#[ cfg(not(feature = "disable-encryption" ))]
fn expand_hkdf_label(
version: Version,
cipher: Cipher,
secret: &SymKey,
label: &str,
key_len: c_uint,
) -> Res<SymKey> {
expand_label(
version,
cipher,
secret,
label,
CK_MECHANISM_TYPE::from(CKM_HKDF_DATA),
key_len,
)
}
/// Derive a fixed-size raw key buffer using HKDF-Data. The const generic `N`
/// selects the output length, so callers get a `[u8; N]` directly with no
/// further `try_into` boilerplate.
#[ cfg(not(feature = "disable-encryption" ))]
pub (crate ) fn expand_label_buf<const N: usize>(
version: Version,
cipher: Cipher,
secret: &SymKey,
label: &str,
) -> Res<[u8; N]> {
let k = expand_hkdf_label(version, cipher, secret, label, c_uint::try_from(N)?)?;
k.key_data()?.try_into().map_err(|_| Error::Internal)
}
/// All the nonces are the same length. Exploit that.
pub const NONCE_LEN: usize = 12 ;
/// The portion of the nonce that is a counter.
const COUNTER_LEN: usize = size_of::<SequenceNumber>();
fn xor_nonce(base: &[u8; NONCE_LEN], count: SequenceNumber) -> [u8; NONCE_LEN] {
let mut nonce = *base;
for (n, &s) in nonce[NONCE_LEN - COUNTER_LEN..]
.iter_mut()
.zip(&count.to_be_bytes())
{
*n ^= s;
}
nonce
}
/// The NSS API insists on us identifying the tag separately, which is awful.
/// All of the AEAD functions here have a tag of this length, so use a fixed offset.
const TAG_LEN: usize = 16 ;
/// Split `data` into `(ct_len, tag)`, returning `SEC_ERROR_BAD_DATA` if it is
/// too short to contain a tag.
fn split_tag(data: &[u8]) -> Res<(usize, [u8; TAG_LEN])> {
let ct_len = data
.len()
.checked_sub(TAG_LEN)
.ok_or_else(|| Error::from(SEC_ERROR_BAD_DATA))?;
let mut tag = [0 u8; TAG_LEN];
tag.copy_from_slice(&data[ct_len..]);
Ok((ct_len, tag))
}
pub type SequenceNumber = u64;
/// All the lengths used by `PK11_AEADOp` are signed. This converts to that.
fn c_int_len<T>(l: T) -> Res<c_int>
where
T: TryInto<c_int>,
T::Error: std::error::Error,
{
l.try_into().map_err(|_| Error::IntegerOverflow)
}
#[ derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
Encrypt,
Decrypt,
}
impl Mode {
fn p11mode(self ) -> CK_ATTRIBUTE_TYPE {
CK_ATTRIBUTE_TYPE::from(
CKA_NSS_MESSAGE
| match self {
Self ::Encrypt => CKA_ENCRYPT,
Self ::Decrypt => CKA_DECRYPT,
},
)
}
}
#[ derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AeadAlgorithms {
Aes128Gcm,
Aes256Gcm,
ChaCha20Poly1305,
}
impl AeadAlgorithms {
#[ must_use]
pub const fn key_len(self ) -> c_uint {
match self {
Self ::Aes128Gcm => 16 ,
Self ::Aes256Gcm | Self ::ChaCha20Poly1305 => 32 ,
}
}
#[ must_use]
pub fn p11_mech(self ) -> CK_MECHANISM_TYPE {
CK_MECHANISM_TYPE::from(match self {
Self ::Aes128Gcm | Self ::Aes256Gcm => CKM_AES_GCM,
Self ::ChaCha20Poly1305 => CKM_CHACHA20_POLY1305,
})
}
}
impl TryFrom<Cipher> for AeadAlgorithms {
type Error = Error;
fn try_from(cipher: Cipher) -> Res<Self > {
match cipher {
TLS_AES_128_GCM_SHA256 => Ok(Self ::Aes128Gcm),
TLS_AES_256_GCM_SHA384 => Ok(Self ::Aes256Gcm),
TLS_CHACHA20_POLY1305_SHA256 => Ok(Self ::ChaCha20Poly1305),
_ => Err(Error::UnsupportedCipher),
}
}
}
pub struct Aead {
mode: Mode,
ctx: Context,
nonce_base: [u8; NONCE_LEN],
}
impl Aead {
pub fn import_key(algorithm: AeadAlgorithms, key: &[u8]) -> Result<SymKey, Error> {
let slot = p11::Slot::internal().map_err(|_| Error::Internal)?;
let key_item = SECItemBorrowed::wrap(key)?;
let key_item_ptr = std::ptr::from_ref(key_item.as_ref()).cast_mut();
let ptr = unsafe {
p11::PK11_ImportSymKey(
*slot,
algorithm.p11_mech(),
p11::PK11Origin::PK11_OriginUnwrap,
CK_ATTRIBUTE_TYPE::from(CKA_ENCRYPT | CKA_DECRYPT),
key_item_ptr,
null_mut(),
)
};
SymKey::from_ptr(ptr)
}
pub fn new(
mode: Mode,
algorithm: AeadAlgorithms,
key: &SymKey,
nonce_base: [u8; NONCE_LEN],
) -> Result<Self , Error> {
crate ::init()?;
let ptr = unsafe {
PK11_CreateContextBySymKey(
algorithm.p11_mech(),
mode.p11mode(),
**key,
SECItemBorrowed::wrap(&nonce_base[..])?.as_ref(),
)
};
Ok(Self {
mode,
ctx: Context::from_ptr(ptr)?,
nonce_base,
})
}
pub fn encrypt(&mut self , aad: &[u8], pt: &[u8]) -> Result<Vec<u8>, Error> {
crate ::init()?;
assert_eq!(self .mode, Mode::Encrypt);
// A copy for the nonce generator to write into. But we don't use the value.
let mut nonce = self .nonce_base;
// Ciphertext with enough space for the tag.
// Even though we give the operation a separate buffer for the tag,
// reserve the capacity on allocation.
let mut ct = vec![0 ; pt.len() + TAG_LEN];
let mut ct_len: c_int = 0 ;
let mut tag = vec![0 ; TAG_LEN];
secstatus_to_res(unsafe {
PK11_AEADOp(
*self .ctx,
CK_GENERATOR_FUNCTION::from(CKG_GENERATE_COUNTER_XOR),
c_int_len(NONCE_LEN - COUNTER_LEN)?, // Fixed portion of the nonce.
nonce.as_mut_ptr(),
c_int_len(nonce.len())?,
aad.as_ptr(),
c_int_len(aad.len())?,
ct.as_mut_ptr(),
&raw mut ct_len,
c_int_len(ct.len())?, // signed :(
tag.as_mut_ptr(),
c_int_len(tag.len())?,
pt.as_ptr(),
c_int_len(pt.len())?,
)
})?;
ct.truncate(usize::try_from(ct_len).map_err(|_| Error::IntegerOverflow)?);
debug_assert_eq!(ct.len(), pt.len());
ct.append(&mut tag);
Ok(ct)
}
/// Encrypt with an explicit sequence number. Mirrors `decrypt`'s nonce
/// construction: the final nonce is `nonce_base XOR encode_be(seq)` over
/// the trailing 8 bytes. The NSS PKCS#11 context's internal counter is
/// not used (`CKG_NO_GENERATE`). The caller must never reuse
/// `(nonce_base, seq)` with the same key.
pub fn encrypt_with_seq(
&mut self ,
aad: &[u8],
seq: SequenceNumber,
pt: &[u8],
) -> Result<Vec<u8>, Error> {
crate ::init()?;
assert_eq!(self .mode, Mode::Encrypt);
let mut nonce = xor_nonce(&self .nonce_base, seq);
let mut ct = vec![0 ; pt.len() + TAG_LEN];
let mut ct_len: c_int = 0 ;
let mut tag = vec![0 ; TAG_LEN];
secstatus_to_res(unsafe {
PK11_AEADOp(
*self .ctx,
CK_GENERATOR_FUNCTION::from(CKG_NO_GENERATE),
c_int_len(NONCE_LEN - COUNTER_LEN)?,
nonce.as_mut_ptr(),
c_int_len(nonce.len())?,
aad.as_ptr(),
c_int_len(aad.len())?,
ct.as_mut_ptr(),
&raw mut ct_len,
c_int_len(ct.len())?,
tag.as_mut_ptr(),
c_int_len(tag.len())?,
pt.as_ptr(),
c_int_len(pt.len())?,
)
})?;
ct.truncate(usize::try_from(ct_len).map_err(|_| Error::IntegerOverflow)?);
debug_assert_eq!(ct.len(), pt.len());
ct.append(&mut tag);
Ok(ct)
}
pub fn decrypt(
&mut self ,
aad: &[u8],
seq: SequenceNumber,
ct: &[u8],
) -> Result<Vec<u8>, Error> {
crate ::init()?;
assert_eq!(self .mode, Mode::Decrypt);
let mut nonce = xor_nonce(&self .nonce_base, seq);
let mut pt = vec![0 ; ct.len()]; // NSS needs more space than it uses for plaintext.
let mut pt_len: c_int = 0 ;
let pt_expected = ct.len().checked_sub(TAG_LEN).ok_or(Error::AeadTruncated)?;
secstatus_to_res(unsafe {
PK11_AEADOp(
*self .ctx,
CK_GENERATOR_FUNCTION::from(CKG_NO_GENERATE),
c_int_len(NONCE_LEN - COUNTER_LEN)?, // Fixed portion of the nonce.
nonce.as_mut_ptr(),
c_int_len(nonce.len())?,
aad.as_ptr(),
c_int_len(aad.len())?,
pt.as_mut_ptr(),
&raw mut pt_len,
c_int_len(pt.len())?,
ct.as_ptr().add(pt_expected).cast_mut(),
c_int_len(TAG_LEN)?,
ct.as_ptr(),
c_int_len(pt_expected)?,
)
})?;
let len = usize::try_from(pt_len).map_err(|_| Error::IntegerOverflow)?;
debug_assert_eq!(len, pt_expected);
pt.truncate(len);
Ok(pt)
}
}
#[ cfg(test)]
mod test {
use test_fixture::fixture_init;
use crate ::aead::{Aead, AeadAlgorithms, Mode, NONCE_LEN, SequenceNumber};
/// Check that the first invocation of encryption matches expected values.
/// Also check decryption of the same.
fn check0(
algorithm: AeadAlgorithms,
key: &[u8],
nonce: &[u8; NONCE_LEN],
aad: &[u8],
pt: &[u8],
ct: &[u8],
) {
fixture_init();
let k = Aead::import_key(algorithm, key).unwrap();
let mut enc = Aead::new(Mode::Encrypt, algorithm, &k, *nonce).unwrap();
let ciphertext = enc.encrypt(aad, pt).unwrap();
assert_eq!(&ciphertext[..], ct);
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, *nonce).unwrap();
let plaintext = dec.decrypt(aad, 0 , ct).unwrap();
assert_eq!(&plaintext[..], pt);
}
fn decrypt(
algorithm: AeadAlgorithms,
key: &[u8],
nonce: &[u8; NONCE_LEN],
seq: SequenceNumber,
aad: &[u8],
pt: &[u8],
ct: &[u8],
) {
let k = Aead::import_key(algorithm, key).unwrap();
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, *nonce).unwrap();
let plaintext = dec.decrypt(aad, seq, ct).unwrap();
assert_eq!(&plaintext[..], pt);
}
/// This tests the AEAD in QUIC in combination with the HKDF code.
/// This is an AEAD-only example.
#[ test]
fn quic_retry() {
const KEY: &[u8] = &[
0 xbe, 0 x0c, 0 x69, 0 x0b, 0 x9f, 0 x66, 0 x57, 0 x5a, 0 x1d, 0 x76, 0 x6b, 0 x54, 0 xe3, 0 x68,
0 xc8, 0 x4e,
];
const NONCE: &[u8; NONCE_LEN] = &[
0 x46, 0 x15, 0 x99, 0 xd3, 0 x5d, 0 x63, 0 x2b, 0 xf2, 0 x23, 0 x98, 0 x25, 0 xbb,
];
const AAD: &[u8] = &[
0 x08, 0 x83, 0 x94, 0 xc8, 0 xf0, 0 x3e, 0 x51, 0 x57, 0 x08, 0 xff, 0 x00, 0 x00, 0 x00, 0 x01,
0 x00, 0 x08, 0 xf0, 0 x67, 0 xa5, 0 x50, 0 x2a, 0 x42, 0 x62, 0 xb5, 0 x74, 0 x6f, 0 x6b, 0 x65,
0 x6e,
];
const CT: &[u8] = &[
0 x04, 0 xa2, 0 x65, 0 xba, 0 x2e, 0 xff, 0 x4d, 0 x82, 0 x90, 0 x58, 0 xfb, 0 x3f, 0 x0f, 0 x24,
0 x96, 0 xba,
];
check0(AeadAlgorithms::Aes128Gcm, KEY, NONCE, AAD, &[], CT);
}
#[ test]
fn quic_server_initial() {
const ALG: AeadAlgorithms = AeadAlgorithms::Aes128Gcm;
const KEY: &[u8] = &[
0 xcf, 0 x3a, 0 x53, 0 x31, 0 x65, 0 x3c, 0 x36, 0 x4c, 0 x88, 0 xf0, 0 xf3, 0 x79, 0 xb6, 0 x06,
0 x7e, 0 x37,
];
const NONCE_BASE: &[u8; NONCE_LEN] = &[
0 x0a, 0 xc1, 0 x49, 0 x3c, 0 xa1, 0 x90, 0 x58, 0 x53, 0 xb0, 0 xbb, 0 xa0, 0 x3e,
];
// Note that this integrates the sequence number of 1 from the example,
// otherwise we can't use a sequence number of 0 to encrypt.
const NONCE: &[u8; NONCE_LEN] = &[
0 x0a, 0 xc1, 0 x49, 0 x3c, 0 xa1, 0 x90, 0 x58, 0 x53, 0 xb0, 0 xbb, 0 xa0, 0 x3f,
];
const AAD: &[u8] = &[
0 xc1, 0 x00, 0 x00, 0 x00, 0 x01, 0 x00, 0 x08, 0 xf0, 0 x67, 0 xa5, 0 x50, 0 x2a, 0 x42, 0 x62,
0 xb5, 0 x00, 0 x40, 0 x75, 0 x00, 0 x01,
];
const PT: &[u8] = &[
0 x02, 0 x00, 0 x00, 0 x00, 0 x00, 0 x06, 0 x00, 0 x40, 0 x5a, 0 x02, 0 x00, 0 x00, 0 x56, 0 x03,
0 x03, 0 xee, 0 xfc, 0 xe7, 0 xf7, 0 xb3, 0 x7b, 0 xa1, 0 xd1, 0 x63, 0 x2e, 0 x96, 0 x67, 0 x78,
0 x25, 0 xdd, 0 xf7, 0 x39, 0 x88, 0 xcf, 0 xc7, 0 x98, 0 x25, 0 xdf, 0 x56, 0 x6d, 0 xc5, 0 x43,
0 x0b, 0 x9a, 0 x04, 0 x5a, 0 x12, 0 x00, 0 x13, 0 x01, 0 x00, 0 x00, 0 x2e, 0 x00, 0 x33, 0 x00,
0 x24, 0 x00, 0 x1d, 0 x00, 0 x20, 0 x9d, 0 x3c, 0 x94, 0 x0d, 0 x89, 0 x69, 0 x0b, 0 x84, 0 xd0,
0 x8a, 0 x60, 0 x99, 0 x3c, 0 x14, 0 x4e, 0 xca, 0 x68, 0 x4d, 0 x10, 0 x81, 0 x28, 0 x7c, 0 x83,
0 x4d, 0 x53, 0 x11, 0 xbc, 0 xf3, 0 x2b, 0 xb9, 0 xda, 0 x1a, 0 x00, 0 x2b, 0 x00, 0 x02, 0 x03,
0 x04,
];
const CT: &[u8] = &[
0 x5a, 0 x48, 0 x2c, 0 xd0, 0 x99, 0 x1c, 0 xd2, 0 x5b, 0 x0a, 0 xac, 0 x40, 0 x6a, 0 x58, 0 x16,
0 xb6, 0 x39, 0 x41, 0 x00, 0 xf3, 0 x7a, 0 x1c, 0 x69, 0 x79, 0 x75, 0 x54, 0 x78, 0 x0b, 0 xb3,
0 x8c, 0 xc5, 0 xa9, 0 x9f, 0 x5e, 0 xde, 0 x4c, 0 xf7, 0 x3c, 0 x3e, 0 xc2, 0 x49, 0 x3a, 0 x18,
0 x39, 0 xb3, 0 xdb, 0 xcb, 0 xa3, 0 xf6, 0 xea, 0 x46, 0 xc5, 0 xb7, 0 x68, 0 x4d, 0 xf3, 0 x54,
0 x8e, 0 x7d, 0 xde, 0 xb9, 0 xc3, 0 xbf, 0 x9c, 0 x73, 0 xcc, 0 x3f, 0 x3b, 0 xde, 0 xd7, 0 x4b,
0 x56, 0 x2b, 0 xfb, 0 x19, 0 xfb, 0 x84, 0 x02, 0 x2f, 0 x8e, 0 xf4, 0 xcd, 0 xd9, 0 x37, 0 x95,
0 xd7, 0 x7d, 0 x06, 0 xed, 0 xbb, 0 x7a, 0 xaf, 0 x2f, 0 x58, 0 x89, 0 x18, 0 x50, 0 xab, 0 xbd,
0 xca, 0 x3d, 0 x20, 0 x39, 0 x8c, 0 x27, 0 x64, 0 x56, 0 xcb, 0 xc4, 0 x21, 0 x58, 0 x40, 0 x7d,
0 xd0, 0 x74, 0 xee,
];
check0(ALG, KEY, NONCE, AAD, PT, CT);
decrypt(ALG, KEY, NONCE_BASE, 1 , AAD, PT, CT);
}
#[ test]
fn quic_chacha() {
const ALG: AeadAlgorithms = AeadAlgorithms::ChaCha20Poly1305;
const KEY: &[u8] = &[
0 xc6, 0 xd9, 0 x8f, 0 xf3, 0 x44, 0 x1c, 0 x3f, 0 xe1, 0 xb2, 0 x18, 0 x20, 0 x94, 0 xf6, 0 x9c,
0 xaa, 0 x2e, 0 xd4, 0 xb7, 0 x16, 0 xb6, 0 x54, 0 x88, 0 x96, 0 x0a, 0 x7a, 0 x98, 0 x49, 0 x79,
0 xfb, 0 x23, 0 xe1, 0 xc8,
];
const NONCE_BASE: &[u8; NONCE_LEN] = &[
0 xe0, 0 x45, 0 x9b, 0 x34, 0 x74, 0 xbd, 0 xd0, 0 xe4, 0 x4a, 0 x41, 0 xc1, 0 x44,
];
// Note that this integrates the sequence number of 654360564 from the example,
// otherwise we can't use a sequence number of 0 to encrypt.
const NONCE: &[u8; NONCE_LEN] = &[
0 xe0, 0 x45, 0 x9b, 0 x34, 0 x74, 0 xbd, 0 xd0, 0 xe4, 0 x6d, 0 x41, 0 x7e, 0 xb0,
];
const AAD: &[u8] = &[0 x42, 0 x00, 0 xbf, 0 xf4];
const PT: &[u8] = &[0 x01];
const CT: &[u8] = &[
0 x65, 0 x5e, 0 x5c, 0 xd5, 0 x5c, 0 x41, 0 xf6, 0 x90, 0 x80, 0 x57, 0 x5d, 0 x79, 0 x99, 0 xc2,
0 x5a, 0 x5b, 0 xfb,
];
check0(ALG, KEY, NONCE, AAD, PT, CT);
// Now use the real nonce and sequence number from the example.
decrypt(ALG, KEY, NONCE_BASE, 654 _360 _564 , AAD, PT, CT);
}
fn roundtrip_encrypt_with_seq(algorithm: AeadAlgorithms, key: &[u8]) {
const NONCE_BASE: [u8; NONCE_LEN] = [0 ; NONCE_LEN];
const AAD: &[u8] = b"associated" ;
const PT: &[u8] = b"hello sframe" ;
const SEQ: SequenceNumber = 0 x0123_4567_89ab;
fixture_init();
let k = Aead::import_key(algorithm, key).unwrap();
let mut enc = Aead::new(Mode::Encrypt, algorithm, &k, NONCE_BASE).unwrap();
let ct = enc.encrypt_with_seq(AAD, SEQ, PT).unwrap();
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, NONCE_BASE).unwrap();
let pt = dec.decrypt(AAD, SEQ, &ct).unwrap();
assert_eq!(&pt[..], PT);
}
#[ test]
fn encrypt_with_seq_aes128gcm() {
const KEY: &[u8] = &[0 x42; 16 ];
roundtrip_encrypt_with_seq(AeadAlgorithms::Aes128Gcm, KEY);
}
#[ test]
fn encrypt_with_seq_aes256gcm() {
const KEY: &[u8] = &[0 x42; 32 ];
roundtrip_encrypt_with_seq(AeadAlgorithms::Aes256Gcm, KEY);
}
#[ test]
fn encrypt_with_seq_chacha20poly1305() {
const KEY: &[u8] = &[0 x42; 32 ];
roundtrip_encrypt_with_seq(AeadAlgorithms::ChaCha20Poly1305, KEY);
}
}
Messung V0.5 in Prozent C=87 H=94 G=90
¤ Dauer der Verarbeitung: 0.10 Sekunden
¤
*© Formatika GbR, Deutschland