// 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.
use nss_rs::{
Mode, RecordProtection, RecordProtectionOps
as _, SymKey,
constants::{Cipher, TLS_VERSION_1_3},
hkdf::import_key,
};
/// The HKDF key material shared across all AEAD integration tests.
pub const SECRET_BYTES: &[u8] = &[
0 x47,
0 xb2,
0 xea,
0 xea,
0 x6c,
0 x26,
0 x6e,
0 x32,
0 xc0,
0 x69,
0 x7a,
0 x9e,
0 x2a,
0 x89,
0 x8b,
0 xdf,
0 x5c,
0 x4f,
0 xb3,
0 xe5,
0 xac,
0 x34,
0 xf0,
0 xe5,
0 x49,
0 xbf,
0 x2c,
0 x58,
0 x58,
0 x1a,
0 x38,
0 x11,
];
pub fn import_secret() -> SymKey {
import_key(TLS_VERSION_1_3, SECRET_BYTES).expect(
"make a secret" )
}
/// Encrypt/decrypt roundtrip for a single cipher suite, including in-place.
pub fn roundtrip(secret: &SymKey, cipher: Cipher) {
let enc = RecordProtection::new(TLS_VERSION_1_3, cipher, secret,
"quic " , Mode::Encrypt)
.expect(
"encrypt context" );
let dec = RecordProtection::new(TLS_VERSION_1_3, cipher, secret,
"quic " , Mode::Decrypt)
.expect(
"decrypt context" );
let aad = b
"associated data" ;
let plaintext = b
"hello roundtrip" ;
let ct_buf = &
mut [
0 u8;
1024 ][..];
let ct = enc.encrypt(
0 , aad, plaintext, ct_buf).expect(
"encrypt" );
let pt_buf = &
mut [
0 u8;
1024 ][..];
let pt = dec.decrypt(
0 , aad, ct, pt_buf).expect(
"decrypt" );
assert_eq!(pt, plaintext);
let mut ip = Vec::from(plaintext
as &[u8]);
ip.resize(plaintext.len() + enc.expansion(),
0 );
enc.encrypt_in_place(
1 , aad, &
mut ip)
.expect(
"encrypt_in_place" );
let dec_len = dec
.decrypt_in_place(
1 , aad, &
mut ip)
.expect(
"decrypt_in_place" );
assert_eq!(&ip[..dec_len], plaintext);
}
Messung V0.5 in Prozent C=90 H=98 G=94
¤ Dauer der Verarbeitung: 0.4 Sekunden
¤
*© Formatika GbR, Deutschland