Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
Vendored
+284
@@ -0,0 +1,284 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{nonce::Nonce, overlapping, quic::Sample, NONCE_LEN};
|
||||
use crate::{
|
||||
bb,
|
||||
cpu::{self, GetFeature as _},
|
||||
error,
|
||||
polyfill::unwrap_const,
|
||||
};
|
||||
use cfg_if::cfg_if;
|
||||
use core::num::NonZeroU32;
|
||||
|
||||
pub(super) use ffi::Counter;
|
||||
|
||||
#[macro_use]
|
||||
mod ffi;
|
||||
|
||||
mod bs;
|
||||
pub(super) mod fallback;
|
||||
pub(super) mod hw;
|
||||
pub(super) mod vp;
|
||||
|
||||
pub type Overlapping<'o> = overlapping::Overlapping<'o, u8>;
|
||||
pub type OverlappingPartialBlock<'o> = overlapping::PartialBlock<'o, u8, BLOCK_LEN>;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(any(all(target_arch = "aarch64", target_endian = "little"), target_arch = "x86_64"))] {
|
||||
pub(super) use ffi::AES_KEY;
|
||||
} else {
|
||||
use ffi::AES_KEY;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) enum Key {
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64",
|
||||
target_arch = "x86"
|
||||
))]
|
||||
Hw(hw::Key),
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
Vp(vp::Key),
|
||||
|
||||
Fallback(fallback::Key),
|
||||
}
|
||||
|
||||
impl Key {
|
||||
#[inline]
|
||||
pub fn new(
|
||||
bytes: KeyBytes<'_>,
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
if let Some(hw_features) = cpu_features.get_feature() {
|
||||
return Ok(Self::Hw(hw::Key::new(
|
||||
bytes,
|
||||
hw_features,
|
||||
cpu_features.get_feature(),
|
||||
)?));
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86_64",
|
||||
target_arch = "x86"
|
||||
))]
|
||||
if let Some(vp_features) = cpu_features.get_feature() {
|
||||
return Ok(Self::Vp(vp::Key::new(bytes, vp_features)?));
|
||||
}
|
||||
|
||||
let _ = cpu_features;
|
||||
|
||||
Ok(Self::Fallback(fallback::Key::new(bytes)?))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn encrypt_block(&self, a: Block) -> Block {
|
||||
match self {
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64",
|
||||
target_arch = "x86"
|
||||
))]
|
||||
Key::Hw(inner) => inner.encrypt_block(a),
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
Key::Vp(inner) => inner.encrypt_block(a),
|
||||
|
||||
Key::Fallback(inner) => inner.encrypt_block(a),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_mask(&self, sample: Sample) -> [u8; 5] {
|
||||
let [b0, b1, b2, b3, b4, ..] = self.encrypt_block(sample);
|
||||
[b0, b1, b2, b3, b4]
|
||||
}
|
||||
}
|
||||
|
||||
pub const AES_128_KEY_LEN: usize = 128 / 8;
|
||||
pub const AES_256_KEY_LEN: usize = 256 / 8;
|
||||
|
||||
pub enum KeyBytes<'a> {
|
||||
AES_128(&'a [u8; AES_128_KEY_LEN]),
|
||||
AES_256(&'a [u8; AES_256_KEY_LEN]),
|
||||
}
|
||||
|
||||
// `Counter` is `ffi::Counter` as its representation is dictated by its use in
|
||||
// the FFI.
|
||||
impl Counter {
|
||||
pub fn one(nonce: Nonce) -> Self {
|
||||
let mut value = [0u8; BLOCK_LEN];
|
||||
value[..NONCE_LEN].copy_from_slice(nonce.as_ref());
|
||||
value[BLOCK_LEN - 1] = 1;
|
||||
Self(value)
|
||||
}
|
||||
|
||||
pub fn increment(&mut self) -> Iv {
|
||||
const ONE: NonZeroU32 = unwrap_const(NonZeroU32::new(1));
|
||||
|
||||
let iv = Iv(self.0);
|
||||
self.increment_by_less_safe(ONE);
|
||||
iv
|
||||
}
|
||||
|
||||
pub(super) fn increment_by_less_safe(&mut self, increment_by: NonZeroU32) {
|
||||
let [.., c0, c1, c2, c3] = &mut self.0;
|
||||
let old_value: u32 = u32::from_be_bytes([*c0, *c1, *c2, *c3]);
|
||||
let new_value = old_value.wrapping_add(increment_by.get());
|
||||
[*c0, *c1, *c2, *c3] = u32::to_be_bytes(new_value);
|
||||
}
|
||||
}
|
||||
|
||||
/// The IV for a single block encryption.
|
||||
///
|
||||
/// Intentionally not `Clone` to ensure each is used only once.
|
||||
pub struct Iv(Block);
|
||||
|
||||
impl From<Counter> for Iv {
|
||||
fn from(counter: Counter) -> Self {
|
||||
Self(counter.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) type Block = [u8; BLOCK_LEN];
|
||||
pub(super) const BLOCK_LEN: usize = 16;
|
||||
pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN];
|
||||
|
||||
pub(super) trait EncryptBlock {
|
||||
fn encrypt_block(&self, block: Block) -> Block;
|
||||
fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block;
|
||||
}
|
||||
|
||||
pub(super) trait EncryptCtr32 {
|
||||
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn encrypt_block_using_encrypt_iv_xor_block(key: &impl EncryptBlock, block: Block) -> Block {
|
||||
key.encrypt_iv_xor_block(Iv(block), ZERO_BLOCK)
|
||||
}
|
||||
|
||||
fn encrypt_iv_xor_block_using_encrypt_block(
|
||||
key: &impl EncryptBlock,
|
||||
iv: Iv,
|
||||
block: Block,
|
||||
) -> Block {
|
||||
let encrypted_iv = key.encrypt_block(iv.0);
|
||||
bb::xor_16(encrypted_iv, block)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn encrypt_iv_xor_block_using_ctr32(key: &impl EncryptCtr32, iv: Iv, mut block: Block) -> Block {
|
||||
let mut ctr = Counter(iv.0); // This is OK because we're only encrypting one block.
|
||||
key.ctr32_encrypt_within(block.as_mut().into(), &mut ctr);
|
||||
block
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::testutil as test;
|
||||
|
||||
#[test]
|
||||
pub fn test_aes() {
|
||||
test::run(test_vector_file!("aes_tests.txt"), |section, test_case| {
|
||||
assert_eq!(section, "");
|
||||
let key = consume_key(test_case, "Key");
|
||||
let input = test_case.consume_bytes("Input");
|
||||
let block: Block = input.as_slice().try_into()?;
|
||||
let expected_output = test_case.consume_bytes("Output");
|
||||
|
||||
let output = key.encrypt_block(block);
|
||||
assert_eq!(output.as_ref(), &expected_output[..]);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn consume_key(test_case: &mut test::TestCase, name: &str) -> Key {
|
||||
let key = test_case.consume_bytes(name);
|
||||
let key = &key[..];
|
||||
let key = match key.len() {
|
||||
16 => KeyBytes::AES_128(key.try_into().unwrap()),
|
||||
32 => KeyBytes::AES_256(key.try_into().unwrap()),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
Key::new(key, cpu::features()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
// These AES-GCM-specific tests are here instead of in `aes_gcm` because
|
||||
// `Counter`'s API isn't visible (enough) to aes_gcm.
|
||||
#[cfg(test)]
|
||||
mod aes_gcm_tests {
|
||||
use super::{super::aes_gcm::MAX_IN_OUT_LEN, *};
|
||||
use core::num::NonZeroU32;
|
||||
|
||||
#[test]
|
||||
fn test_aes_gcm_counter_blocks_max() {
|
||||
test_aes_gcm_counter_blocks(MAX_IN_OUT_LEN, &[0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aes_gcm_counter_blocks_max_minus_one() {
|
||||
test_aes_gcm_counter_blocks(MAX_IN_OUT_LEN - BLOCK_LEN, &[0xff, 0xff, 0xff, 0xff]);
|
||||
}
|
||||
|
||||
fn test_aes_gcm_counter_blocks(in_out_len: usize, expected_final_counter: &[u8; 4]) {
|
||||
fn ctr32(ctr: &Counter) -> &[u8; 4] {
|
||||
(&ctr.0[12..]).try_into().unwrap()
|
||||
}
|
||||
|
||||
let rounded_down = in_out_len / BLOCK_LEN;
|
||||
let blocks = rounded_down + (if in_out_len % BLOCK_LEN == 0 { 0 } else { 1 });
|
||||
let blocks = u32::try_from(blocks)
|
||||
.ok()
|
||||
.and_then(NonZeroU32::new)
|
||||
.unwrap();
|
||||
|
||||
let nonce = Nonce::assume_unique_for_key([1; 12]);
|
||||
let mut ctr = Counter::one(nonce);
|
||||
assert_eq!(ctr32(&ctr), &[0, 0, 0, 1]);
|
||||
let _tag_iv = ctr.increment();
|
||||
assert_eq!(ctr32(&ctr), &[0, 0, 0, 2]);
|
||||
ctr.increment_by_less_safe(blocks);
|
||||
|
||||
// `MAX_IN_OUT_LEN` is less on 32-bit targets, so we don't even get
|
||||
// close to wrapping, but run the tests on them anyway.
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
assert_eq!(ctr32(&ctr), expected_final_counter);
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
let _ = expected_final_counter;
|
||||
}
|
||||
}
|
||||
Vendored
+60
@@ -0,0 +1,60 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
|
||||
use super::{Counter, Overlapping, AES_KEY};
|
||||
|
||||
/// SAFETY:
|
||||
/// * The caller must ensure that if blocks > 0 then either `input` and
|
||||
/// `output` do not overlap at all, or input == output.add(n) for some
|
||||
/// (nonnegative) n.
|
||||
/// * if blocks > 0, The caller must ensure `input` points to `blocks` blocks
|
||||
/// and that `output` points to writable space for `blocks` blocks.
|
||||
/// * The caller must ensure that `vpaes_key` was initialized with
|
||||
/// `vpaes_set_encrypt_key`.
|
||||
/// * Upon returning, `blocks` blocks will have been read from `input` and
|
||||
/// written to `output`.
|
||||
pub(super) unsafe fn ctr32_encrypt_blocks_with_vpaes_key(
|
||||
in_out: Overlapping<'_>,
|
||||
vpaes_key: &AES_KEY,
|
||||
ctr: &mut Counter,
|
||||
) {
|
||||
prefixed_extern! {
|
||||
// bsaes_ctr32_encrypt_blocks requires transformation of an existing
|
||||
// VPAES key; there is no `bsaes_set_encrypt_key`.
|
||||
fn vpaes_encrypt_key_to_bsaes(bsaes_key: *mut AES_KEY, vpaes_key: &AES_KEY);
|
||||
}
|
||||
|
||||
// SAFETY:
|
||||
// * The caller ensures `vpaes_key` was initialized by
|
||||
// `vpaes_set_encrypt_key`.
|
||||
// * `bsaes_key was zeroed above, and `vpaes_encrypt_key_to_bsaes`
|
||||
// is assumed to initialize `bsaes_key`.
|
||||
let bsaes_key = unsafe { AES_KEY::derive(vpaes_encrypt_key_to_bsaes, vpaes_key) };
|
||||
|
||||
// The code for `vpaes_encrypt_key_to_bsaes` notes "vpaes stores one
|
||||
// fewer round count than bsaes, but the number of keys is the same,"
|
||||
// so use this as a sanity check.
|
||||
debug_assert_eq!(bsaes_key.rounds(), vpaes_key.rounds() + 1);
|
||||
|
||||
// SAFETY:
|
||||
// * `bsaes_key` is in bsaes format after calling
|
||||
// `vpaes_encrypt_key_to_bsaes`.
|
||||
// * `bsaes_ctr32_encrypt_blocks` satisfies the contract for
|
||||
// `ctr32_encrypt_blocks`.
|
||||
unsafe {
|
||||
ctr32_encrypt_blocks!(bsaes_ctr32_encrypt_blocks, in_out, &bsaes_key, ctr);
|
||||
}
|
||||
}
|
||||
Vendored
+44
@@ -0,0 +1,44 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
|
||||
use crate::error;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
inner: AES_KEY,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
pub(in super::super) fn new(bytes: KeyBytes<'_>) -> Result<Self, error::Unspecified> {
|
||||
let inner = unsafe { set_encrypt_key!(aes_nohw_set_encrypt_key, bytes) }?;
|
||||
Ok(Self { inner })
|
||||
}
|
||||
}
|
||||
|
||||
impl EncryptBlock for Key {
|
||||
fn encrypt_block(&self, block: Block) -> Block {
|
||||
unsafe { encrypt_block!(aes_nohw_encrypt, block, &self.inner) }
|
||||
}
|
||||
|
||||
fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block {
|
||||
super::encrypt_iv_xor_block_using_encrypt_block(self, iv, block)
|
||||
}
|
||||
}
|
||||
|
||||
impl EncryptCtr32 for Key {
|
||||
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
|
||||
unsafe { ctr32_encrypt_blocks!(aes_nohw_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
|
||||
}
|
||||
}
|
||||
Vendored
+206
@@ -0,0 +1,206 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{Block, KeyBytes, Overlapping, BLOCK_LEN};
|
||||
use crate::{bits::BitLength, c, error};
|
||||
use core::{
|
||||
ffi::{c_int, c_uint},
|
||||
num::{NonZeroU32, NonZeroUsize},
|
||||
};
|
||||
|
||||
/// nonce || big-endian counter.
|
||||
#[repr(transparent)]
|
||||
pub(in super::super) struct Counter(pub(super) [u8; BLOCK_LEN]);
|
||||
|
||||
// Keep this in sync with AES_KEY in aes.h.
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
pub(in super::super) struct AES_KEY {
|
||||
pub rd_key: [u32; 4 * (MAX_ROUNDS + 1)],
|
||||
pub rounds: c_uint,
|
||||
}
|
||||
|
||||
// Keep this in sync with `AES_MAXNR` in aes.h.
|
||||
const MAX_ROUNDS: usize = 14;
|
||||
|
||||
impl AES_KEY {
|
||||
#[inline]
|
||||
pub(super) unsafe fn new(
|
||||
f: unsafe extern "C" fn(*const u8, BitLength<c_int>, *mut AES_KEY) -> c_int,
|
||||
bytes: KeyBytes<'_>,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
let mut key = Self {
|
||||
rd_key: [0; 4 * (MAX_ROUNDS + 1)],
|
||||
rounds: 0,
|
||||
};
|
||||
|
||||
let (bytes, key_bits) = match bytes {
|
||||
KeyBytes::AES_128(bytes) => (&bytes[..], BitLength::from_bits(128)),
|
||||
KeyBytes::AES_256(bytes) => (&bytes[..], BitLength::from_bits(256)),
|
||||
};
|
||||
|
||||
// Unusually, in this case zero means success and non-zero means failure.
|
||||
if 0 == unsafe { f(bytes.as_ptr(), key_bits, &mut key) } {
|
||||
debug_assert_ne!(key.rounds, 0); // Sanity check initialization.
|
||||
Ok(key)
|
||||
} else {
|
||||
Err(error::Unspecified)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
impl AES_KEY {
|
||||
pub(super) unsafe fn derive(
|
||||
f: for<'a> unsafe extern "C" fn(*mut AES_KEY, &'a AES_KEY),
|
||||
src: &Self,
|
||||
) -> Self {
|
||||
let mut r = AES_KEY {
|
||||
rd_key: [0u32; 4 * (MAX_ROUNDS + 1)],
|
||||
rounds: 0,
|
||||
};
|
||||
unsafe { f(&mut r, src) };
|
||||
r
|
||||
}
|
||||
|
||||
pub(super) fn rounds(&self) -> u32 {
|
||||
self.rounds
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY:
|
||||
// * The function `$name` must read `bits` bits from `user_key`; `bits` will
|
||||
// always be a valid AES key length, i.e. a whole number of bytes.
|
||||
// * `$name` must set `key.rounds` to the value expected by the corresponding
|
||||
// encryption/decryption functions and return 0, or otherwise must return
|
||||
// non-zero to indicate failure.
|
||||
// * `$name` may inspect CPU features.
|
||||
//
|
||||
// In BoringSSL, the C prototypes for these are in
|
||||
// crypto/fipsmodule/aes/internal.h.
|
||||
macro_rules! set_encrypt_key {
|
||||
( $name:ident, $key_bytes:expr $(,)? ) => {{
|
||||
use crate::bits::BitLength;
|
||||
use core::ffi::c_int;
|
||||
prefixed_extern! {
|
||||
fn $name(user_key: *const u8, bits: BitLength<c_int>, key: *mut AES_KEY) -> c_int;
|
||||
}
|
||||
$crate::aead::aes::ffi::AES_KEY::new($name, $key_bytes)
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! encrypt_block {
|
||||
($name:ident, $block:expr, $key:expr) => {{
|
||||
use crate::aead::aes::{ffi::AES_KEY, Block};
|
||||
prefixed_extern! {
|
||||
fn $name(a: &Block, r: *mut Block, key: &AES_KEY);
|
||||
}
|
||||
$key.encrypt_block($name, $block)
|
||||
}};
|
||||
}
|
||||
|
||||
impl AES_KEY {
|
||||
#[inline]
|
||||
pub(super) unsafe fn encrypt_block(
|
||||
&self,
|
||||
f: unsafe extern "C" fn(&Block, *mut Block, &AES_KEY),
|
||||
a: Block,
|
||||
) -> Block {
|
||||
let mut result = core::mem::MaybeUninit::uninit();
|
||||
unsafe {
|
||||
f(&a, result.as_mut_ptr(), self);
|
||||
result.assume_init()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// SAFETY:
|
||||
/// * The caller must ensure that `$key` was initialized with the
|
||||
/// `set_encrypt_key!` invocation that `$name` requires.
|
||||
/// * The caller must ensure that fhe function `$name` satisfies the conditions
|
||||
/// for the `f` parameter to `ctr32_encrypt_blocks`.
|
||||
macro_rules! ctr32_encrypt_blocks {
|
||||
($name:ident, $in_out:expr, $key:expr, $ctr:expr $(,)? ) => {{
|
||||
use crate::{
|
||||
aead::aes::{ffi::AES_KEY, Counter, BLOCK_LEN},
|
||||
c,
|
||||
};
|
||||
prefixed_extern! {
|
||||
fn $name(
|
||||
input: *const [u8; BLOCK_LEN],
|
||||
output: *mut [u8; BLOCK_LEN],
|
||||
blocks: c::NonZero_size_t,
|
||||
key: &AES_KEY,
|
||||
ivec: &Counter,
|
||||
);
|
||||
}
|
||||
$key.ctr32_encrypt_blocks($name, $in_out, $ctr)
|
||||
}};
|
||||
}
|
||||
|
||||
impl AES_KEY {
|
||||
/// SAFETY:
|
||||
/// * `f` must not read more than `blocks` blocks from `input`.
|
||||
/// * `f` must write exactly `block` blocks to `output`.
|
||||
/// * In particular, `f` must handle blocks == 0 without reading from `input`
|
||||
/// or writing to `output`.
|
||||
/// * `f` must support the input overlapping with the output exactly or
|
||||
/// with any nonnegative offset `n` (i.e. `input == output.add(n)`);
|
||||
/// `f` does NOT need to support the cases where input < output.
|
||||
/// * `key` must have been initialized with the `set_encrypt_key!` invocation
|
||||
/// that corresponds to `f`.
|
||||
/// * `f` may inspect CPU features.
|
||||
#[inline]
|
||||
pub(super) unsafe fn ctr32_encrypt_blocks(
|
||||
&self,
|
||||
f: unsafe extern "C" fn(
|
||||
input: *const [u8; BLOCK_LEN],
|
||||
output: *mut [u8; BLOCK_LEN],
|
||||
blocks: c::NonZero_size_t,
|
||||
key: &AES_KEY,
|
||||
ivec: &Counter,
|
||||
),
|
||||
in_out: Overlapping<'_>,
|
||||
ctr: &mut Counter,
|
||||
) {
|
||||
in_out.with_input_output_len(|input, output, len| {
|
||||
debug_assert_eq!(len % BLOCK_LEN, 0);
|
||||
|
||||
let blocks = match NonZeroUsize::new(len / BLOCK_LEN) {
|
||||
Some(blocks) => blocks,
|
||||
None => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let input: *const [u8; BLOCK_LEN] = input.cast();
|
||||
let output: *mut [u8; BLOCK_LEN] = output.cast();
|
||||
let blocks_u32: NonZeroU32 = blocks.try_into().unwrap();
|
||||
|
||||
// SAFETY:
|
||||
// * `input` points to `blocks` blocks.
|
||||
// * `output` points to space for `blocks` blocks to be written.
|
||||
// * input == output.add(n), where n == src.start, and the caller is
|
||||
// responsible for ensuing this sufficient for `f` to work correctly.
|
||||
// * `blocks` is non-zero so `f` doesn't have to work for empty slices.
|
||||
// * The caller is responsible for ensuring `key` was initialized by the
|
||||
// `set_encrypt_key!` invocation required by `f`.
|
||||
unsafe {
|
||||
f(input, output, blocks, self, ctr);
|
||||
}
|
||||
|
||||
ctr.increment_by_less_safe(blocks_u32);
|
||||
});
|
||||
}
|
||||
}
|
||||
Vendored
+95
@@ -0,0 +1,95 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
|
||||
use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
|
||||
use crate::{cpu, error};
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
|
||||
pub(in super::super) type RequiredCpuFeatures = cpu::arm::Aes;
|
||||
pub(in super::super) type OptionalCpuFeatures = ();
|
||||
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
|
||||
use cpu::intel::{Aes, Avx, Ssse3};
|
||||
// Some functions seem to have been written to require only SSE/SSE2
|
||||
// but there seem to be no SSSE3-less CPUs with AES-NI, and we don't
|
||||
// have feature detection for SSE2.
|
||||
pub(in super::super) type RequiredCpuFeatures = (Aes, Ssse3);
|
||||
pub(in super::super) type OptionalCpuFeatures = Avx;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
inner: AES_KEY,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
|
||||
pub(in super::super) fn new(
|
||||
bytes: KeyBytes<'_>,
|
||||
_required_cpu_features: RequiredCpuFeatures,
|
||||
_optional_cpu_features: Option<OptionalCpuFeatures>,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
let inner = unsafe { set_encrypt_key!(aes_hw_set_encrypt_key, bytes) }?;
|
||||
Ok(Self { inner })
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
pub(in super::super) fn new(
|
||||
bytes: KeyBytes<'_>,
|
||||
(Aes { .. }, Ssse3 { .. }): RequiredCpuFeatures,
|
||||
optional_cpu_features: Option<OptionalCpuFeatures>,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
// Ssse3 is required, but upstream only uses this if there is also Avx;
|
||||
// presumably the base version is faster on pre-AVX CPUs.
|
||||
let inner = if let Some(Avx { .. }) = optional_cpu_features {
|
||||
unsafe { set_encrypt_key!(aes_hw_set_encrypt_key_alt, bytes) }?
|
||||
} else {
|
||||
unsafe { set_encrypt_key!(aes_hw_set_encrypt_key_base, bytes) }?
|
||||
};
|
||||
Ok(Self { inner })
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
#[must_use]
|
||||
pub(in super::super) fn inner_less_safe(&self) -> &AES_KEY {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl EncryptBlock for Key {
|
||||
fn encrypt_block(&self, block: Block) -> Block {
|
||||
super::encrypt_block_using_encrypt_iv_xor_block(self, block)
|
||||
}
|
||||
|
||||
fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block {
|
||||
super::encrypt_iv_xor_block_using_ctr32(self, iv, block)
|
||||
}
|
||||
}
|
||||
|
||||
impl EncryptCtr32 for Key {
|
||||
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
|
||||
unsafe { ctr32_encrypt_blocks!(aes_hw_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
|
||||
}
|
||||
}
|
||||
Vendored
+139
@@ -0,0 +1,139 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
|
||||
use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
|
||||
use crate::{cpu, error};
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
))]
|
||||
type RequiredCpuFeatures = cpu::arm::Neon;
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
pub(in super::super) type RequiredCpuFeatures = cpu::intel::Ssse3;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(in super::super) struct Key {
|
||||
inner: AES_KEY,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
pub(in super::super) fn new(
|
||||
bytes: KeyBytes<'_>,
|
||||
_cpu: RequiredCpuFeatures,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
let inner = unsafe { set_encrypt_key!(vpaes_set_encrypt_key, bytes) }?;
|
||||
Ok(Self { inner })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
impl EncryptBlock for Key {
|
||||
fn encrypt_block(&self, block: Block) -> Block {
|
||||
super::encrypt_block_using_encrypt_iv_xor_block(self, block)
|
||||
}
|
||||
|
||||
fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block {
|
||||
super::encrypt_iv_xor_block_using_ctr32(self, iv, block)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
impl EncryptCtr32 for Key {
|
||||
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
|
||||
unsafe { ctr32_encrypt_blocks!(vpaes_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
impl EncryptCtr32 for Key {
|
||||
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
|
||||
use super::{super::overlapping::IndexError, bs, BLOCK_LEN};
|
||||
|
||||
let in_out = {
|
||||
let (in_out, src) = in_out.into_slice_src_mut();
|
||||
let blocks = in_out[src.clone()].len() / BLOCK_LEN;
|
||||
|
||||
// bsaes operates in batches of 8 blocks.
|
||||
let bsaes_blocks = if blocks >= 8 && (blocks % 8) < 6 {
|
||||
// It's faster to use bsaes for all the full batches and then
|
||||
// switch to vpaes for the last partial batch (if any).
|
||||
blocks - (blocks % 8)
|
||||
} else if blocks >= 8 {
|
||||
// It's faster to let bsaes handle everything including
|
||||
// the last partial batch.
|
||||
blocks
|
||||
} else {
|
||||
// It's faster to let vpaes handle everything.
|
||||
0
|
||||
};
|
||||
let bsaes_in_out_len = bsaes_blocks * BLOCK_LEN;
|
||||
let bs_in_out =
|
||||
Overlapping::new(&mut in_out[..(src.start + bsaes_in_out_len)], src.clone())
|
||||
.unwrap_or_else(|IndexError { .. }| unreachable!());
|
||||
|
||||
// SAFETY:
|
||||
// * self.inner was initialized with `vpaes_set_encrypt_key` above,
|
||||
// as required by `bsaes_ctr32_encrypt_blocks_with_vpaes_key`.
|
||||
unsafe {
|
||||
bs::ctr32_encrypt_blocks_with_vpaes_key(bs_in_out, &self.inner, ctr);
|
||||
}
|
||||
|
||||
Overlapping::new(&mut in_out[bsaes_in_out_len..], src)
|
||||
.unwrap_or_else(|IndexError { .. }| unreachable!())
|
||||
};
|
||||
|
||||
// SAFETY:
|
||||
// * self.inner was initialized with `vpaes_set_encrypt_key` above,
|
||||
// as required by `vpaes_ctr32_encrypt_blocks`.
|
||||
// * `vpaes_ctr32_encrypt_blocks` satisfies the contract for
|
||||
// `ctr32_encrypt_blocks`.
|
||||
unsafe { ctr32_encrypt_blocks!(vpaes_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
impl EncryptBlock for Key {
|
||||
fn encrypt_block(&self, block: Block) -> Block {
|
||||
unsafe { encrypt_block!(vpaes_encrypt, block, &self.inner) }
|
||||
}
|
||||
|
||||
fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block {
|
||||
super::encrypt_iv_xor_block_using_encrypt_block(self, iv, block)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
impl EncryptCtr32 for Key {
|
||||
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
|
||||
super::super::shift::shift_full_blocks(in_out, |input| {
|
||||
self.encrypt_iv_xor_block(ctr.increment(), *input)
|
||||
});
|
||||
}
|
||||
}
|
||||
Vendored
+505
@@ -0,0 +1,505 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{
|
||||
aes::{self, Counter, Overlapping, OverlappingPartialBlock, BLOCK_LEN, ZERO_BLOCK},
|
||||
gcm,
|
||||
overlapping::IndexError,
|
||||
Aad, Nonce, Tag,
|
||||
};
|
||||
use crate::{
|
||||
cpu,
|
||||
error::{self, InputTooLongError},
|
||||
polyfill::{slice, sliceutil::overwrite_at_start, usize_from_u64_saturated},
|
||||
};
|
||||
use core::ops::RangeFrom;
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
use cpu::GetFeature as _;
|
||||
|
||||
mod aarch64;
|
||||
mod aeshwclmulmovbe;
|
||||
mod vaesclmulavx2;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct Key(DynKey);
|
||||
|
||||
impl Key {
|
||||
pub(super) fn new(
|
||||
key: aes::KeyBytes,
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
Ok(Self(DynKey::new(key, cpu_features)?))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum DynKey {
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
VAesClMulAvx2(Combo<aes::hw::Key, gcm::vclmulavx2::Key>),
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
AesHwClMulAvxMovbe(Combo<aes::hw::Key, gcm::clmulavxmovbe::Key>),
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
AesHwClMul(Combo<aes::hw::Key, gcm::clmul::Key>),
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
))]
|
||||
Simd(Combo<aes::vp::Key, gcm::neon::Key>),
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
Simd(Combo<aes::vp::Key, gcm::fallback::Key>),
|
||||
|
||||
Fallback(Combo<aes::fallback::Key, gcm::fallback::Key>),
|
||||
}
|
||||
|
||||
impl DynKey {
|
||||
fn new(key: aes::KeyBytes, cpu: cpu::Features) -> Result<Self, error::Unspecified> {
|
||||
let cpu = cpu.values();
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
if let Some((aes, gcm)) = cpu.get_feature() {
|
||||
// 14.3.1 Detection of VEX-Encoded AES and VPCLMULQDQ
|
||||
let aes_key = aes::hw::Key::new(key, aes, cpu.get_feature())?;
|
||||
let gcm_key_value = derive_gcm_key_value(&aes_key);
|
||||
let combo = if let Some(cpu) = cpu.get_feature() {
|
||||
let gcm_key = gcm::vclmulavx2::Key::new(gcm_key_value, cpu);
|
||||
Self::VAesClMulAvx2(Combo { aes_key, gcm_key })
|
||||
} else if let Some(cpu) = cpu.get_feature() {
|
||||
let gcm_key = gcm::clmulavxmovbe::Key::new(gcm_key_value, cpu);
|
||||
Self::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key })
|
||||
} else {
|
||||
let gcm_key = gcm::clmul::Key::new(gcm_key_value, gcm);
|
||||
Self::AesHwClMul(Combo { aes_key, gcm_key })
|
||||
};
|
||||
return Ok(combo);
|
||||
}
|
||||
|
||||
// x86_64 is handled above.
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86"
|
||||
))]
|
||||
if let (Some(aes), Some(gcm)) = (cpu.get_feature(), cpu.get_feature()) {
|
||||
let aes_key = aes::hw::Key::new(key, aes, cpu.get_feature())?;
|
||||
let gcm_key_value = derive_gcm_key_value(&aes_key);
|
||||
let gcm_key = gcm::clmul::Key::new(gcm_key_value, gcm);
|
||||
return Ok(Self::AesHwClMul(Combo { aes_key, gcm_key }));
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
))]
|
||||
if let Some(cpu) = cpu.get_feature() {
|
||||
return Self::new_neon(key, cpu);
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
if let Some(cpu) = cpu.get_feature() {
|
||||
return Self::new_ssse3(key, cpu);
|
||||
}
|
||||
|
||||
let _ = cpu;
|
||||
Self::new_fallback(key)
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
))]
|
||||
#[cfg_attr(target_arch = "aarch64", inline(never))]
|
||||
fn new_neon(key: aes::KeyBytes, cpu: cpu::arm::Neon) -> Result<Self, error::Unspecified> {
|
||||
let aes_key = aes::vp::Key::new(key, cpu)?;
|
||||
let gcm_key_value = derive_gcm_key_value(&aes_key);
|
||||
let gcm_key = gcm::neon::Key::new(gcm_key_value, cpu);
|
||||
Ok(Self::Simd(Combo { aes_key, gcm_key }))
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
#[inline(never)]
|
||||
fn new_ssse3(
|
||||
key: aes::KeyBytes,
|
||||
cpu: aes::vp::RequiredCpuFeatures,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
let aes_key = aes::vp::Key::new(key, cpu)?;
|
||||
let gcm_key_value = derive_gcm_key_value(&aes_key);
|
||||
let gcm_key = gcm::fallback::Key::new(gcm_key_value);
|
||||
Ok(Self::Simd(Combo { aes_key, gcm_key }))
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64",
|
||||
),
|
||||
inline(never)
|
||||
)]
|
||||
fn new_fallback(key: aes::KeyBytes) -> Result<Self, error::Unspecified> {
|
||||
let aes_key = aes::fallback::Key::new(key)?;
|
||||
let gcm_key_value = derive_gcm_key_value(&aes_key);
|
||||
let gcm_key = gcm::fallback::Key::new(gcm_key_value);
|
||||
Ok(Self::Fallback(Combo { aes_key, gcm_key }))
|
||||
}
|
||||
}
|
||||
|
||||
fn derive_gcm_key_value(aes_key: &impl aes::EncryptBlock) -> gcm::KeyValue {
|
||||
gcm::KeyValue::new(aes_key.encrypt_block(ZERO_BLOCK))
|
||||
}
|
||||
|
||||
const CHUNK_BLOCKS: usize = 3 * 1024 / 16;
|
||||
|
||||
#[inline(never)]
|
||||
pub(super) fn seal(
|
||||
Key(key): &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let mut ctr = Counter::one(nonce);
|
||||
let tag_iv = ctr.increment();
|
||||
|
||||
match key {
|
||||
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
|
||||
DynKey::AesHwClMul(c) => {
|
||||
seal_whole_partial(c, aad, in_out, ctr, tag_iv, aarch64::seal_whole)
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
DynKey::VAesClMulAvx2(c) => seal_whole_partial(
|
||||
c,
|
||||
aad,
|
||||
in_out,
|
||||
ctr,
|
||||
tag_iv,
|
||||
vaesclmulavx2::seal_whole_vaes_clmul_avx2,
|
||||
),
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
DynKey::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key }) => {
|
||||
aeshwclmulmovbe::seal(aes_key, gcm_key, ctr, tag_iv, aad, in_out)
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
|
||||
DynKey::AesHwClMul(c) => seal_strided(c, aad, in_out, ctr, tag_iv),
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86_64",
|
||||
target_arch = "x86"
|
||||
))]
|
||||
DynKey::Simd(c) => seal_strided(c, aad, in_out, ctr, tag_iv),
|
||||
|
||||
DynKey::Fallback(c) => seal_strided(c, aad, in_out, ctr, tag_iv),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
fn seal_whole_partial<A: aes::EncryptBlock, G: gcm::UpdateBlock>(
|
||||
Combo { aes_key, gcm_key }: &Combo<A, G>,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
mut ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
seal_whole: impl FnOnce(&A, &mut gcm::Context<G>, &mut Counter, slice::AsChunksMut<u8, BLOCK_LEN>),
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let mut auth = gcm::Context::new(gcm_key, aad, in_out.len())?;
|
||||
let (whole, remainder) = slice::as_chunks_mut(in_out);
|
||||
seal_whole(aes_key, &mut auth, &mut ctr, whole);
|
||||
let remainder = OverlappingPartialBlock::new(remainder.into())
|
||||
.unwrap_or_else(|InputTooLongError { .. }| unreachable!());
|
||||
seal_finish(aes_key, auth, remainder, ctr, tag_iv)
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
),
|
||||
inline(never)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
),
|
||||
cold
|
||||
)]
|
||||
fn seal_strided<
|
||||
A: aes::EncryptBlock + aes::EncryptCtr32,
|
||||
G: gcm::UpdateBlock + gcm::UpdateBlocks,
|
||||
>(
|
||||
Combo { aes_key, gcm_key }: &Combo<A, G>,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
mut ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let mut auth = gcm::Context::new(gcm_key, aad, in_out.len())?;
|
||||
|
||||
let (mut whole, remainder) = slice::as_chunks_mut(in_out);
|
||||
|
||||
for mut chunk in whole.chunks_mut::<CHUNK_BLOCKS>() {
|
||||
aes_key.ctr32_encrypt_within(chunk.as_flattened_mut().into(), &mut ctr);
|
||||
auth.update_blocks(chunk.as_ref());
|
||||
}
|
||||
|
||||
let remainder = OverlappingPartialBlock::new(remainder.into())
|
||||
.unwrap_or_else(|InputTooLongError { .. }| unreachable!());
|
||||
seal_finish(aes_key, auth, remainder, ctr, tag_iv)
|
||||
}
|
||||
|
||||
fn seal_finish<A: aes::EncryptBlock, G: gcm::UpdateBlock>(
|
||||
aes_key: &A,
|
||||
mut auth: gcm::Context<G>,
|
||||
remainder: OverlappingPartialBlock<'_>,
|
||||
ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let remainder_len = remainder.len();
|
||||
if remainder_len > 0 {
|
||||
let mut input = ZERO_BLOCK;
|
||||
overwrite_at_start(&mut input, remainder.input());
|
||||
let mut output = aes_key.encrypt_iv_xor_block(ctr.into(), input);
|
||||
output[remainder_len..].fill(0);
|
||||
auth.update_block(output);
|
||||
remainder.overwrite_at_start(output);
|
||||
}
|
||||
|
||||
Ok(finish(aes_key, auth, tag_iv))
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
pub(super) fn open(
|
||||
Key(key): &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out_slice: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let mut ctr = Counter::one(nonce);
|
||||
let tag_iv = ctr.increment();
|
||||
|
||||
match key {
|
||||
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
|
||||
DynKey::AesHwClMul(c) => {
|
||||
open_whole_partial(c, aad, in_out_slice, src, ctr, tag_iv, aarch64::open_whole)
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
DynKey::VAesClMulAvx2(c) => open_whole_partial(
|
||||
c,
|
||||
aad,
|
||||
in_out_slice,
|
||||
src,
|
||||
ctr,
|
||||
tag_iv,
|
||||
vaesclmulavx2::open_whole_vaes_clmul_avx2,
|
||||
),
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
DynKey::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key }) => {
|
||||
aeshwclmulmovbe::open(aes_key, gcm_key, ctr, tag_iv, aad, in_out_slice, src)
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
|
||||
DynKey::AesHwClMul(c) => open_strided(c, aad, in_out_slice, src, ctr, tag_iv),
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86_64",
|
||||
target_arch = "x86"
|
||||
))]
|
||||
DynKey::Simd(c) => open_strided(c, aad, in_out_slice, src, ctr, tag_iv),
|
||||
|
||||
DynKey::Fallback(c) => open_strided(c, aad, in_out_slice, src, ctr, tag_iv),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
fn open_whole_partial<A: aes::EncryptBlock, G: gcm::UpdateBlock>(
|
||||
Combo { aes_key, gcm_key }: &Combo<A, G>,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out_slice: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
mut ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
open_whole: impl FnOnce(&A, &mut gcm::Context<G>, Overlapping, &mut Counter),
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let in_out = Overlapping::new(in_out_slice, src.clone()).map_err(error::erase::<IndexError>)?;
|
||||
let mut auth = gcm::Context::new(gcm_key, aad, in_out.len())?;
|
||||
|
||||
let remainder_len = in_out.len() % BLOCK_LEN;
|
||||
|
||||
let in_out_slice_len = in_out_slice.len();
|
||||
let whole_in_out_slice = &mut in_out_slice[..(in_out_slice_len - remainder_len)];
|
||||
let whole = Overlapping::new(whole_in_out_slice, src.clone())
|
||||
.unwrap_or_else(|IndexError { .. }| unreachable!());
|
||||
let whole_len = whole.len();
|
||||
open_whole(aes_key, &mut auth, whole, &mut ctr);
|
||||
|
||||
let remainder = &mut in_out_slice[whole_len..];
|
||||
let remainder =
|
||||
Overlapping::new(remainder, src).unwrap_or_else(|IndexError { .. }| unreachable!());
|
||||
let remainder = OverlappingPartialBlock::new(remainder)
|
||||
.unwrap_or_else(|InputTooLongError { .. }| unreachable!());
|
||||
open_finish(aes_key, auth, remainder, ctr, tag_iv)
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
any(
|
||||
all(
|
||||
any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
),
|
||||
target_feature = "neon"
|
||||
),
|
||||
all(
|
||||
any(target_arch = "x86", target_arch = "x86_64"),
|
||||
target_feature = "sse"
|
||||
)
|
||||
),
|
||||
inline(never)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
),
|
||||
cold
|
||||
)]
|
||||
fn open_strided<
|
||||
A: aes::EncryptBlock + aes::EncryptCtr32,
|
||||
G: gcm::UpdateBlock + gcm::UpdateBlocks,
|
||||
>(
|
||||
Combo { aes_key, gcm_key }: &Combo<A, G>,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out_slice: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
mut ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let in_out = Overlapping::new(in_out_slice, src.clone()).map_err(error::erase::<IndexError>)?;
|
||||
let input = in_out.input();
|
||||
let input_len = input.len();
|
||||
|
||||
let mut auth = gcm::Context::new(gcm_key, aad, input_len)?;
|
||||
|
||||
let remainder_len = input_len % BLOCK_LEN;
|
||||
let whole_len = input_len - remainder_len;
|
||||
let in_prefix_len = src.start;
|
||||
|
||||
{
|
||||
let mut chunk_len = CHUNK_BLOCKS * BLOCK_LEN;
|
||||
let mut output = 0;
|
||||
let mut input = in_prefix_len;
|
||||
loop {
|
||||
if whole_len - output < chunk_len {
|
||||
chunk_len = whole_len - output;
|
||||
}
|
||||
|
||||
let ciphertext = &in_out_slice[input..][..chunk_len];
|
||||
let (ciphertext, leftover) = slice::as_chunks(ciphertext);
|
||||
debug_assert_eq!(leftover.len(), 0);
|
||||
if ciphertext.is_empty() {
|
||||
break;
|
||||
}
|
||||
auth.update_blocks(ciphertext);
|
||||
|
||||
let chunk = Overlapping::new(
|
||||
&mut in_out_slice[output..][..(chunk_len + in_prefix_len)],
|
||||
in_prefix_len..,
|
||||
)
|
||||
.map_err(error::erase::<IndexError>)?;
|
||||
aes_key.ctr32_encrypt_within(chunk, &mut ctr);
|
||||
output += chunk_len;
|
||||
input += chunk_len;
|
||||
}
|
||||
}
|
||||
|
||||
let in_out = Overlapping::new(&mut in_out_slice[whole_len..], src)
|
||||
.unwrap_or_else(|IndexError { .. }| unreachable!());
|
||||
let in_out = OverlappingPartialBlock::new(in_out)
|
||||
.unwrap_or_else(|InputTooLongError { .. }| unreachable!());
|
||||
|
||||
open_finish(aes_key, auth, in_out, ctr, tag_iv)
|
||||
}
|
||||
|
||||
fn open_finish<A: aes::EncryptBlock, G: gcm::UpdateBlock>(
|
||||
aes_key: &A,
|
||||
mut auth: gcm::Context<G>,
|
||||
remainder: OverlappingPartialBlock<'_>,
|
||||
ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
if remainder.len() > 0 {
|
||||
let mut input = ZERO_BLOCK;
|
||||
overwrite_at_start(&mut input, remainder.input());
|
||||
auth.update_block(input);
|
||||
remainder.overwrite_at_start(aes_key.encrypt_iv_xor_block(ctr.into(), input));
|
||||
}
|
||||
Ok(finish(aes_key, auth, tag_iv))
|
||||
}
|
||||
|
||||
fn finish<A: aes::EncryptBlock, G: gcm::UpdateBlock>(
|
||||
aes_key: &A,
|
||||
gcm_ctx: gcm::Context<G>,
|
||||
tag_iv: aes::Iv,
|
||||
) -> Tag {
|
||||
// Finalize the tag and return it.
|
||||
gcm_ctx.pre_finish(|pre_tag| Tag(aes_key.encrypt_iv_xor_block(tag_iv, pre_tag)))
|
||||
}
|
||||
|
||||
pub(super) const MAX_IN_OUT_LEN: usize = super::max_input_len(BLOCK_LEN, 2);
|
||||
|
||||
// [NIST SP800-38D] Section 5.2.1.1. Note that [RFC 5116 Section 5.1] and
|
||||
// [RFC 5116 Section 5.2] have an off-by-one error in `P_MAX`.
|
||||
//
|
||||
// [NIST SP800-38D]:
|
||||
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
||||
// [RFC 5116 Section 5.1]: https://tools.ietf.org/html/rfc5116#section-5.1
|
||||
// [RFC 5116 Section 5.2]: https://tools.ietf.org/html/rfc5116#section-5.2
|
||||
const _MAX_INPUT_LEN_BOUNDED_BY_NIST: () =
|
||||
assert!(MAX_IN_OUT_LEN == usize_from_u64_saturated(((1u64 << 39) - 256) / 8));
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub(super) struct Combo<Aes, Gcm> {
|
||||
pub(super) aes_key: Aes,
|
||||
pub(super) gcm_key: Gcm,
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(all(target_arch = "aarch64", target_endian = "little"))]
|
||||
|
||||
use super::{aes, gcm, Counter, BLOCK_LEN};
|
||||
use crate::{aead::aes::Overlapping, bits::BitLength, polyfill::slice::AsChunksMut};
|
||||
use core::num::NonZeroU64;
|
||||
|
||||
pub(super) fn seal_whole(
|
||||
aes_key: &aes::hw::Key,
|
||||
auth: &mut gcm::Context<gcm::clmul::Key>,
|
||||
ctr: &mut Counter,
|
||||
mut in_out: AsChunksMut<u8, BLOCK_LEN>,
|
||||
) {
|
||||
let whole_block_bits = auth.in_out_whole_block_bits();
|
||||
let whole_block_bits_u64: BitLength<u64> = whole_block_bits.into();
|
||||
if let Ok(whole_block_bits) = whole_block_bits_u64.try_into() {
|
||||
let (htable, xi) = auth.inner();
|
||||
|
||||
prefixed_extern! {
|
||||
fn aes_gcm_enc_kernel(
|
||||
input: *const [u8; BLOCK_LEN],
|
||||
in_bits: BitLength<NonZeroU64>,
|
||||
output: *mut [u8; BLOCK_LEN],
|
||||
Xi: &mut gcm::Xi,
|
||||
ivec: &mut Counter,
|
||||
key: &aes::AES_KEY,
|
||||
Htable: &gcm::HTable);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
aes_gcm_enc_kernel(
|
||||
in_out.as_ptr(),
|
||||
whole_block_bits,
|
||||
in_out.as_mut_ptr(),
|
||||
xi,
|
||||
ctr,
|
||||
aes_key.inner_less_safe(),
|
||||
htable,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn open_whole(
|
||||
aes_key: &aes::hw::Key,
|
||||
auth: &mut gcm::Context<gcm::clmul::Key>,
|
||||
in_out: Overlapping,
|
||||
ctr: &mut Counter,
|
||||
) {
|
||||
// Precondition. TODO: Create an overlapping::AsChunks for this.
|
||||
assert_eq!(in_out.len() % BLOCK_LEN, 0);
|
||||
|
||||
in_out.with_input_output_len(|input, output, _len| {
|
||||
let whole_block_bits = auth.in_out_whole_block_bits();
|
||||
let whole_block_bits_u64: BitLength<u64> = whole_block_bits.into();
|
||||
if let Ok(whole_block_bits) = whole_block_bits_u64.try_into() {
|
||||
let (htable, xi) = auth.inner();
|
||||
prefixed_extern! {
|
||||
fn aes_gcm_dec_kernel(
|
||||
input: *const u8,
|
||||
in_bits: BitLength<NonZeroU64>,
|
||||
output: *mut u8,
|
||||
Xi: &mut gcm::Xi,
|
||||
ivec: &mut Counter,
|
||||
key: &aes::AES_KEY,
|
||||
Htable: &gcm::HTable);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
aes_gcm_dec_kernel(
|
||||
input,
|
||||
whole_block_bits,
|
||||
output,
|
||||
xi,
|
||||
ctr,
|
||||
aes_key.inner_less_safe(),
|
||||
htable,
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(target_arch = "x86_64")]
|
||||
|
||||
use super::{
|
||||
super::overlapping::IndexError,
|
||||
aes::{self, Counter, EncryptCtr32, Overlapping, OverlappingPartialBlock},
|
||||
gcm, Aad, Tag,
|
||||
};
|
||||
use crate::{
|
||||
c,
|
||||
error::{self, InputTooLongError},
|
||||
polyfill::slice,
|
||||
};
|
||||
use core::ops::RangeFrom;
|
||||
|
||||
#[inline(never)]
|
||||
pub(super) fn seal(
|
||||
aes_key: &aes::hw::Key,
|
||||
gcm_key: &gcm::clmulavxmovbe::Key,
|
||||
mut ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
prefixed_extern! {
|
||||
// `HTable` and `Xi` should be 128-bit aligned. TODO: Can we shrink `HTable`? The
|
||||
// assembly says it needs just nine values in that array.
|
||||
fn aesni_gcm_encrypt(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: c::size_t,
|
||||
key: &aes::AES_KEY,
|
||||
ivec: &mut Counter,
|
||||
Htable: &gcm::HTable,
|
||||
Xi: &mut gcm::Xi) -> c::size_t;
|
||||
}
|
||||
|
||||
let mut auth = gcm::Context::new(gcm_key, aad, in_out.len())?;
|
||||
let (htable, xi) = auth.inner();
|
||||
|
||||
let processed = unsafe {
|
||||
aesni_gcm_encrypt(
|
||||
in_out.as_ptr(),
|
||||
in_out.as_mut_ptr(),
|
||||
in_out.len(),
|
||||
aes_key.inner_less_safe(),
|
||||
&mut ctr,
|
||||
htable,
|
||||
xi,
|
||||
)
|
||||
};
|
||||
|
||||
let ramaining = match in_out.get_mut(processed..) {
|
||||
Some(remaining) => remaining,
|
||||
None => {
|
||||
// This can't happen. If it did, then the assembly already
|
||||
// caused a buffer overflow.
|
||||
unreachable!()
|
||||
}
|
||||
};
|
||||
let (mut whole, remainder) = slice::as_chunks_mut(ramaining);
|
||||
aes_key.ctr32_encrypt_within(whole.as_flattened_mut().into(), &mut ctr);
|
||||
auth.update_blocks(whole.as_ref());
|
||||
let remainder = OverlappingPartialBlock::new(remainder.into())
|
||||
.unwrap_or_else(|InputTooLongError { .. }| unreachable!());
|
||||
|
||||
super::seal_finish(aes_key, auth, remainder, ctr, tag_iv)
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
pub(super) fn open(
|
||||
aes_key: &aes::hw::Key,
|
||||
gcm_key: &gcm::clmulavxmovbe::Key,
|
||||
mut ctr: Counter,
|
||||
tag_iv: aes::Iv,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out_slice: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
prefixed_extern! {
|
||||
// `HTable` and `Xi` should be 128-bit aligned. TODO: Can we shrink `HTable`? The
|
||||
// assembly says it needs just nine values in that array.
|
||||
fn aesni_gcm_decrypt(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: c::size_t,
|
||||
key: &aes::AES_KEY,
|
||||
ivec: &mut Counter,
|
||||
Htable: &gcm::HTable,
|
||||
Xi: &mut gcm::Xi) -> c::size_t;
|
||||
}
|
||||
|
||||
let in_out = Overlapping::new(in_out_slice, src.clone()).map_err(error::erase::<IndexError>)?;
|
||||
let mut auth = gcm::Context::new(gcm_key, aad, in_out.len())?;
|
||||
let processed = in_out.with_input_output_len(|input, output, len| {
|
||||
let (htable, xi) = auth.inner();
|
||||
unsafe {
|
||||
aesni_gcm_decrypt(
|
||||
input,
|
||||
output,
|
||||
len,
|
||||
aes_key.inner_less_safe(),
|
||||
&mut ctr,
|
||||
htable,
|
||||
xi,
|
||||
)
|
||||
}
|
||||
});
|
||||
let in_out_slice = in_out_slice.get_mut(processed..).unwrap_or_else(|| {
|
||||
// This can't happen. If it did, then the assembly already
|
||||
// caused a buffer overflow.
|
||||
unreachable!()
|
||||
});
|
||||
// Authenticate any remaining whole blocks.
|
||||
let in_out =
|
||||
Overlapping::new(in_out_slice, src.clone()).unwrap_or_else(|IndexError { .. }| {
|
||||
// This can't happen. If it did, then the assembly already
|
||||
// overwrote part of the remaining input.
|
||||
unreachable!()
|
||||
});
|
||||
let (whole, _) = slice::as_chunks(in_out.input());
|
||||
auth.update_blocks(whole);
|
||||
|
||||
let whole_len = whole.as_flattened().len();
|
||||
|
||||
// Decrypt any remaining whole blocks.
|
||||
let whole = Overlapping::new(&mut in_out_slice[..(src.start + whole_len)], src.clone())
|
||||
.map_err(error::erase::<IndexError>)?;
|
||||
aes_key.ctr32_encrypt_within(whole, &mut ctr);
|
||||
|
||||
let in_out_slice = match in_out_slice.get_mut(whole_len..) {
|
||||
Some(partial) => partial,
|
||||
None => unreachable!(),
|
||||
};
|
||||
let in_out =
|
||||
Overlapping::new(in_out_slice, src).unwrap_or_else(|IndexError { .. }| unreachable!());
|
||||
let in_out = OverlappingPartialBlock::new(in_out)
|
||||
.unwrap_or_else(|InputTooLongError { .. }| unreachable!());
|
||||
|
||||
super::open_finish(aes_key, auth, in_out, ctr, tag_iv)
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(target_arch = "x86_64")]
|
||||
|
||||
use super::{aes, gcm, Counter, BLOCK_LEN};
|
||||
use crate::{aead::aes::Overlapping, c, polyfill::slice::AsChunksMut};
|
||||
use core::num::NonZeroU32;
|
||||
|
||||
pub(super) fn seal_whole_vaes_clmul_avx2(
|
||||
aes_key: &aes::hw::Key,
|
||||
auth: &mut gcm::Context<gcm::vclmulavx2::Key>,
|
||||
ctr: &mut Counter,
|
||||
mut in_out: AsChunksMut<u8, BLOCK_LEN>,
|
||||
) {
|
||||
prefixed_extern! {
|
||||
fn aes_gcm_enc_update_vaes_avx2(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: c::size_t,
|
||||
key: &aes::AES_KEY,
|
||||
ivec: &Counter,
|
||||
Htable: &gcm::HTable,
|
||||
Xi: &mut gcm::Xi);
|
||||
}
|
||||
|
||||
let in_out = in_out.as_flattened_mut();
|
||||
|
||||
// Precondition: Since we have a `gcm::Context` then the number of blocks
|
||||
// must fit in `u32`.
|
||||
let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap();
|
||||
|
||||
if let Some(blocks) = NonZeroU32::new(blocks) {
|
||||
let aes_key = aes_key.inner_less_safe();
|
||||
let (htable, xi) = auth.inner();
|
||||
let input = in_out.as_ptr();
|
||||
let output = in_out.as_mut_ptr();
|
||||
let len = in_out.len();
|
||||
unsafe { aes_gcm_enc_update_vaes_avx2(input, output, len, aes_key, ctr, htable, xi) };
|
||||
ctr.increment_by_less_safe(blocks);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn open_whole_vaes_clmul_avx2(
|
||||
aes_key: &aes::hw::Key,
|
||||
auth: &mut gcm::Context<gcm::vclmulavx2::Key>,
|
||||
in_out: Overlapping,
|
||||
ctr: &mut Counter,
|
||||
) {
|
||||
prefixed_extern! {
|
||||
fn aes_gcm_dec_update_vaes_avx2(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: c::size_t,
|
||||
key: &aes::AES_KEY,
|
||||
ivec: &mut Counter,
|
||||
Htable: &gcm::HTable,
|
||||
Xi: &mut gcm::Xi);
|
||||
}
|
||||
|
||||
// Precondition. TODO: Create an overlapping::AsChunks for this.
|
||||
assert_eq!(in_out.len() % BLOCK_LEN, 0);
|
||||
// Precondition: Since we have a `gcm::Context` then the number of blocks
|
||||
// must fit in `u32`.
|
||||
let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap();
|
||||
|
||||
if let Some(blocks) = NonZeroU32::new(blocks) {
|
||||
let aes_key = aes_key.inner_less_safe();
|
||||
let (htable, xi) = auth.inner();
|
||||
in_out.with_input_output_len(|input, output, len| unsafe {
|
||||
aes_gcm_dec_update_vaes_avx2(input, output, len, aes_key, ctr, htable, xi)
|
||||
});
|
||||
ctr.increment_by_less_safe(blocks);
|
||||
}
|
||||
}
|
||||
Vendored
+269
@@ -0,0 +1,269 @@
|
||||
// Copyright 2015-2021 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::{
|
||||
bb, cpu,
|
||||
error::{self, InputTooLongError},
|
||||
hkdf,
|
||||
};
|
||||
use core::ops::RangeFrom;
|
||||
|
||||
use super::{
|
||||
aes, aes_gcm, chacha20_poly1305,
|
||||
nonce::{Nonce, NONCE_LEN},
|
||||
overlapping::{IndexError, Overlapping},
|
||||
Aad, KeyInner, Tag, TAG_LEN,
|
||||
};
|
||||
|
||||
impl hkdf::KeyType for &'static Algorithm {
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
self.key_len()
|
||||
}
|
||||
}
|
||||
|
||||
/// An AEAD Algorithm.
|
||||
pub struct Algorithm {
|
||||
init: fn(key: &[u8], cpu_features: cpu::Features) -> Result<KeyInner, error::Unspecified>,
|
||||
|
||||
seal: fn(
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified>,
|
||||
open: fn(
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified>,
|
||||
|
||||
key_len: usize,
|
||||
id: AlgorithmID,
|
||||
}
|
||||
|
||||
impl Algorithm {
|
||||
/// The length of the key.
|
||||
#[inline(always)]
|
||||
pub fn key_len(&self) -> usize {
|
||||
self.key_len
|
||||
}
|
||||
|
||||
/// The length of a tag.
|
||||
///
|
||||
/// See also `MAX_TAG_LEN`.
|
||||
#[inline(always)]
|
||||
pub fn tag_len(&self) -> usize {
|
||||
TAG_LEN
|
||||
}
|
||||
|
||||
/// The length of the nonces.
|
||||
#[inline(always)]
|
||||
pub fn nonce_len(&self) -> usize {
|
||||
NONCE_LEN
|
||||
}
|
||||
|
||||
pub(super) fn new_key(
|
||||
&self,
|
||||
key_bytes: &[u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<KeyInner, error::Unspecified> {
|
||||
(self.init)(key_bytes, cpu_features)
|
||||
}
|
||||
|
||||
pub(super) fn open_within<'io>(
|
||||
&self,
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
received_tag: Tag,
|
||||
in_out: &'io mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<&'io mut [u8], error::Unspecified> {
|
||||
let ciphertext_len = in_out.get(src.clone()).ok_or(error::Unspecified)?.len();
|
||||
|
||||
let Tag(calculated_tag) = (self.open)(key, nonce, aad, in_out, src, cpu_features)?;
|
||||
|
||||
if bb::verify_slices_are_equal(calculated_tag.as_ref(), received_tag.as_ref()).is_err() {
|
||||
// Zero out the plaintext so that it isn't accidentally leaked or used
|
||||
// after verification fails. It would be safest if we could check the
|
||||
// tag before decrypting, but some `open` implementations interleave
|
||||
// authentication with decryption for performance.
|
||||
for b in &mut in_out[..ciphertext_len] {
|
||||
*b = 0;
|
||||
}
|
||||
return Err(error::Unspecified);
|
||||
}
|
||||
|
||||
// `ciphertext_len` is also the plaintext length.
|
||||
Ok(&mut in_out[..ciphertext_len])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn seal(
|
||||
&self,
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
(self.seal)(key, nonce, aad, in_out, cpu_features)
|
||||
}
|
||||
}
|
||||
|
||||
derive_debug_via_id!(Algorithm);
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub(super) enum AlgorithmID {
|
||||
AES_128_GCM,
|
||||
AES_256_GCM,
|
||||
CHACHA20_POLY1305,
|
||||
}
|
||||
|
||||
impl PartialEq for Algorithm {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Algorithm {}
|
||||
|
||||
/// AES-128 in GCM mode with 128-bit tags and 96 bit nonces.
|
||||
pub static AES_128_GCM: Algorithm = Algorithm {
|
||||
key_len: aes::AES_128_KEY_LEN,
|
||||
init: aes_gcm_init_128,
|
||||
seal: aes_gcm_seal,
|
||||
open: aes_gcm_open,
|
||||
id: AlgorithmID::AES_128_GCM,
|
||||
};
|
||||
|
||||
/// AES-256 in GCM mode with 128-bit tags and 96 bit nonces.
|
||||
pub static AES_256_GCM: Algorithm = Algorithm {
|
||||
key_len: aes::AES_256_KEY_LEN,
|
||||
init: aes_gcm_init_256,
|
||||
seal: aes_gcm_seal,
|
||||
open: aes_gcm_open,
|
||||
id: AlgorithmID::AES_256_GCM,
|
||||
};
|
||||
|
||||
fn aes_gcm_init_128(
|
||||
key: &[u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<KeyInner, error::Unspecified> {
|
||||
let key = key.try_into().map_err(|_| error::Unspecified)?;
|
||||
Ok(KeyInner::AesGcm(aes_gcm::Key::new(
|
||||
aes::KeyBytes::AES_128(key),
|
||||
cpu_features,
|
||||
)?))
|
||||
}
|
||||
|
||||
fn aes_gcm_init_256(
|
||||
key: &[u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<KeyInner, error::Unspecified> {
|
||||
let key = key.try_into().map_err(|_| error::Unspecified)?;
|
||||
Ok(KeyInner::AesGcm(aes_gcm::Key::new(
|
||||
aes::KeyBytes::AES_256(key),
|
||||
cpu_features,
|
||||
)?))
|
||||
}
|
||||
|
||||
fn aes_gcm_seal(
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
_cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let key = match key {
|
||||
KeyInner::AesGcm(key) => key,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
aes_gcm::seal(key, nonce, aad, in_out)
|
||||
}
|
||||
|
||||
pub(super) fn aes_gcm_open(
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
_cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let key = match key {
|
||||
KeyInner::AesGcm(key) => key,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
aes_gcm::open(key, nonce, aad, in_out, src)
|
||||
}
|
||||
|
||||
/// ChaCha20-Poly1305 as described in [RFC 8439].
|
||||
///
|
||||
/// The keys are 256 bits long and the nonces are 96 bits long.
|
||||
///
|
||||
/// [RFC 8439]: https://tools.ietf.org/html/rfc8439
|
||||
pub static CHACHA20_POLY1305: Algorithm = Algorithm {
|
||||
key_len: chacha20_poly1305::KEY_LEN,
|
||||
init: chacha20_poly1305_init,
|
||||
seal: chacha20_poly1305_seal,
|
||||
open: chacha20_poly1305_open,
|
||||
id: AlgorithmID::CHACHA20_POLY1305,
|
||||
};
|
||||
|
||||
/// Copies |key| into |ctx_buf|.
|
||||
fn chacha20_poly1305_init(
|
||||
key: &[u8],
|
||||
_cpu_features: cpu::Features,
|
||||
) -> Result<KeyInner, error::Unspecified> {
|
||||
let key: [u8; chacha20_poly1305::KEY_LEN] = key.try_into()?;
|
||||
Ok(KeyInner::ChaCha20Poly1305(chacha20_poly1305::Key::new(key)))
|
||||
}
|
||||
|
||||
fn chacha20_poly1305_seal(
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let key = match key {
|
||||
KeyInner::ChaCha20Poly1305(key) => key,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
chacha20_poly1305::seal(key, nonce, aad, in_out, cpu_features)
|
||||
.map_err(error::erase::<InputTooLongError>)
|
||||
}
|
||||
|
||||
fn chacha20_poly1305_open(
|
||||
key: &KeyInner,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
src: RangeFrom<usize>,
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Tag, error::Unspecified> {
|
||||
let key = match key {
|
||||
KeyInner::ChaCha20Poly1305(key) => key,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let in_out = Overlapping::new(in_out, src).map_err(error::erase::<IndexError>)?;
|
||||
chacha20_poly1305::open(key, nonce, aad, in_out, cpu_features)
|
||||
.map_err(error::erase::<InputTooLongError>)
|
||||
}
|
||||
Vendored
+327
@@ -0,0 +1,327 @@
|
||||
// Copyright 2016 Brian Smith.
|
||||
// Portions Copyright (c) 2016, Google Inc.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{overlapping, quic::Sample, Nonce};
|
||||
use crate::cpu;
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))] {
|
||||
#[macro_use]
|
||||
mod ffi;
|
||||
#[cfg(any(target_arch = "x86", test))]
|
||||
mod fallback;
|
||||
} else {
|
||||
mod fallback;
|
||||
}
|
||||
}
|
||||
|
||||
use crate::polyfill::ArraySplitMap;
|
||||
|
||||
pub type Overlapping<'o> = overlapping::Overlapping<'o, u8>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
words: [u32; KEY_LEN / 4],
|
||||
}
|
||||
|
||||
impl Key {
|
||||
pub(super) fn new(value: [u8; KEY_LEN]) -> Self {
|
||||
Self {
|
||||
words: value.array_split_map(u32::from_le_bytes),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Key {
|
||||
// Encrypts `in_out` with the counter 0 and returns counter 1,
|
||||
// where the counter is derived from the nonce `nonce`.
|
||||
#[inline]
|
||||
pub(super) fn encrypt_single_block_with_ctr_0<const N: usize>(
|
||||
&self,
|
||||
nonce: Nonce,
|
||||
in_out: &mut [u8; N],
|
||||
cpu: cpu::Features,
|
||||
) -> Counter {
|
||||
assert!(N <= BLOCK_LEN);
|
||||
let (zero, one) = Counter::zero_one_less_safe(nonce);
|
||||
self.encrypt(zero, in_out.as_mut().into(), cpu);
|
||||
one
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_mask(&self, sample: Sample) -> [u8; 5] {
|
||||
let cpu = cpu::features(); // TODO: Remove this.
|
||||
let (ctr, nonce) = sample.split_at(4);
|
||||
let ctr = u32::from_le_bytes(ctr.try_into().unwrap());
|
||||
let nonce = Nonce::assume_unique_for_key(nonce.try_into().unwrap());
|
||||
let ctr = Counter::from_nonce_and_ctr(nonce, ctr);
|
||||
|
||||
let mut out: [u8; 5] = [0; 5];
|
||||
self.encrypt(ctr, out.as_mut().into(), cpu);
|
||||
out
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(super) fn encrypt(&self, counter: Counter, in_out: Overlapping<'_>, cpu: cpu::Features) {
|
||||
cfg_if! {
|
||||
if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
|
||||
use cpu::{GetFeature as _, arm::Neon};
|
||||
const NEON_MIN_LEN: usize = 192 + 1;
|
||||
if in_out.len() >= NEON_MIN_LEN {
|
||||
if let Some(cpu) = cpu.get_feature() {
|
||||
return chacha20_ctr32_ffi!(
|
||||
unsafe { (NEON_MIN_LEN, Neon, Overlapping<'_>) => ChaCha20_ctr32_neon },
|
||||
self, counter, in_out, cpu);
|
||||
}
|
||||
}
|
||||
if in_out.len() >= 1 {
|
||||
chacha20_ctr32_ffi!(
|
||||
unsafe { (1, (), Overlapping<'_>) => ChaCha20_ctr32_nohw },
|
||||
self, counter, in_out, ())
|
||||
}
|
||||
} else if #[cfg(all(target_arch = "arm", target_endian = "little"))] {
|
||||
use cpu::{GetFeature as _, arm::Neon};
|
||||
const NEON_MIN_LEN: usize = 192 + 1;
|
||||
if in_out.len() >= NEON_MIN_LEN {
|
||||
if let Some(cpu) = cpu.get_feature() {
|
||||
return chacha20_ctr32_ffi!(
|
||||
unsafe { (NEON_MIN_LEN, Neon, &mut [u8]) => ChaCha20_ctr32_neon },
|
||||
self, counter, in_out.copy_within(), cpu);
|
||||
}
|
||||
}
|
||||
if in_out.len() >= 1 {
|
||||
chacha20_ctr32_ffi!(
|
||||
unsafe { (1, (), &mut [u8]) => ChaCha20_ctr32_nohw },
|
||||
self, counter, in_out.copy_within(), ())
|
||||
}
|
||||
} else if #[cfg(target_arch = "x86")] {
|
||||
use cpu::{GetFeature as _, intel::Ssse3};
|
||||
if in_out.len() >= 1 {
|
||||
if let Some(cpu) = cpu.get_feature() {
|
||||
chacha20_ctr32_ffi!(
|
||||
unsafe { (1, Ssse3, &mut [u8]) => ChaCha20_ctr32_ssse3 },
|
||||
self, counter, in_out.copy_within(), cpu)
|
||||
} else {
|
||||
let _: cpu::Features = cpu;
|
||||
fallback::ChaCha20_ctr32(self, counter, in_out)
|
||||
}
|
||||
}
|
||||
} else if #[cfg(target_arch = "x86_64")] {
|
||||
use cpu::{GetFeature, intel::{Avx2, Ssse3}};
|
||||
const SSE_MIN_LEN: usize = 128 + 1; // Also AVX2, SSSE3_4X, SSSE3
|
||||
if in_out.len() >= SSE_MIN_LEN {
|
||||
let values = cpu.values();
|
||||
if let Some(cpu) = values.get_feature() {
|
||||
return chacha20_ctr32_ffi!(
|
||||
unsafe { (SSE_MIN_LEN, Avx2, Overlapping<'_>) => ChaCha20_ctr32_avx2 },
|
||||
self, counter, in_out, cpu);
|
||||
}
|
||||
if let Some(cpu) = values.get_feature() {
|
||||
return chacha20_ctr32_ffi!(
|
||||
unsafe { (SSE_MIN_LEN, Ssse3, Overlapping<'_>) =>
|
||||
ChaCha20_ctr32_ssse3_4x },
|
||||
self, counter, in_out, cpu);
|
||||
}
|
||||
}
|
||||
if in_out.len() >= 1 {
|
||||
chacha20_ctr32_ffi!(
|
||||
unsafe { (1, (), Overlapping<'_>) => ChaCha20_ctr32_nohw },
|
||||
self, counter, in_out, ())
|
||||
}
|
||||
} else {
|
||||
let _: cpu::Features = cpu;
|
||||
fallback::ChaCha20_ctr32(self, counter, in_out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn words_less_safe(&self) -> &[u32; KEY_LEN / 4] {
|
||||
&self.words
|
||||
}
|
||||
}
|
||||
|
||||
/// Counter || Nonce, all native endian.
|
||||
#[repr(transparent)]
|
||||
pub struct Counter([u32; 4]);
|
||||
|
||||
impl Counter {
|
||||
// Nonce-reuse: the caller must only use the first counter (0) for at most
|
||||
// a single block.
|
||||
fn zero_one_less_safe(nonce: Nonce) -> (Self, Self) {
|
||||
let ctr0 @ Self([_, n0, n1, n2]) = Self::from_nonce_and_ctr(nonce, 0);
|
||||
let ctr1 = Self([1, n0, n1, n2]);
|
||||
(ctr0, ctr1)
|
||||
}
|
||||
|
||||
fn from_nonce_and_ctr(nonce: Nonce, ctr: u32) -> Self {
|
||||
let [n0, n1, n2] = nonce.as_ref().array_split_map(u32::from_le_bytes);
|
||||
Self([ctr, n0, n1, n2])
|
||||
}
|
||||
|
||||
/// This is "less safe" because it hands off management of the counter to
|
||||
/// the caller.
|
||||
#[cfg(any(
|
||||
test,
|
||||
not(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))
|
||||
))]
|
||||
fn into_words_less_safe(self) -> [u32; 4] {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub const KEY_LEN: usize = 32;
|
||||
|
||||
const BLOCK_LEN: usize = 64;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate alloc;
|
||||
|
||||
use super::{super::overlapping::IndexError, *};
|
||||
use crate::error;
|
||||
use crate::testutil as test;
|
||||
use alloc::vec;
|
||||
|
||||
const MAX_ALIGNMENT_AND_OFFSET: (usize, usize) = (15, 259);
|
||||
const MAX_ALIGNMENT_AND_OFFSET_SUBSET: (usize, usize) =
|
||||
if cfg!(any(not(debug_assertions), feature = "slow_tests")) {
|
||||
MAX_ALIGNMENT_AND_OFFSET
|
||||
} else {
|
||||
(0, 0)
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn chacha20_test_default() {
|
||||
// Always use `MAX_OFFSET` if we hav assembly code.
|
||||
let max_offset = if cfg!(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
)) {
|
||||
MAX_ALIGNMENT_AND_OFFSET
|
||||
} else {
|
||||
MAX_ALIGNMENT_AND_OFFSET_SUBSET
|
||||
};
|
||||
chacha20_test(max_offset, Key::encrypt);
|
||||
}
|
||||
|
||||
// Smoketest the fallback implementation.
|
||||
#[test]
|
||||
fn chacha20_test_fallback() {
|
||||
chacha20_test(MAX_ALIGNMENT_AND_OFFSET_SUBSET, |key, ctr, in_out, _cpu| {
|
||||
fallback::ChaCha20_ctr32(key, ctr, in_out)
|
||||
});
|
||||
}
|
||||
|
||||
// Verifies the encryption is successful when done on overlapping buffers.
|
||||
//
|
||||
// On some branches of the 32-bit x86 and ARM assembly code the in-place
|
||||
// operation fails in some situations where the input/output buffers are
|
||||
// not exactly overlapping. Such failures are dependent not only on the
|
||||
// degree of overlapping but also the length of the data. `encrypt_within`
|
||||
// works around that.
|
||||
fn chacha20_test(
|
||||
max_alignment_and_offset: (usize, usize),
|
||||
f: impl for<'k, 'o> Fn(&'k Key, Counter, Overlapping<'o>, cpu::Features),
|
||||
) {
|
||||
let cpu = cpu::features();
|
||||
|
||||
// Reuse a buffer to avoid slowing down the tests with allocations.
|
||||
let mut buf = vec![0u8; 1300];
|
||||
|
||||
test::run(
|
||||
test_vector_file!("chacha_tests.txt"),
|
||||
move |section, test_case| {
|
||||
assert_eq!(section, "");
|
||||
|
||||
let key = test_case.consume_bytes("Key");
|
||||
let key: &[u8; KEY_LEN] = key.as_slice().try_into()?;
|
||||
let key = Key::new(*key);
|
||||
|
||||
let ctr = test_case.consume_usize("Ctr");
|
||||
let nonce = test_case.consume_bytes("Nonce");
|
||||
let input = test_case.consume_bytes("Input");
|
||||
let output = test_case.consume_bytes("Output");
|
||||
|
||||
// Run the test case over all prefixes of the input because the
|
||||
// behavior of ChaCha20 implementation changes dependent on the
|
||||
// length of the input.
|
||||
for len in 0..=input.len() {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
chacha20_test_case_inner(
|
||||
&key,
|
||||
&nonce,
|
||||
ctr as u32,
|
||||
&input[..len],
|
||||
&output[..len],
|
||||
&mut buf,
|
||||
max_alignment_and_offset,
|
||||
cpu,
|
||||
&f,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn chacha20_test_case_inner(
|
||||
key: &Key,
|
||||
nonce: &[u8],
|
||||
ctr: u32,
|
||||
input: &[u8],
|
||||
expected: &[u8],
|
||||
buf: &mut [u8],
|
||||
(max_alignment, max_offset): (usize, usize),
|
||||
cpu: cpu::Features,
|
||||
f: &impl for<'k, 'o> Fn(&'k Key, Counter, Overlapping<'o>, cpu::Features),
|
||||
) {
|
||||
const ARBITRARY: u8 = 123;
|
||||
|
||||
for alignment in 0..=max_alignment {
|
||||
buf[..alignment].fill(ARBITRARY);
|
||||
let buf = &mut buf[alignment..];
|
||||
for offset in 0..=max_offset {
|
||||
let buf = &mut buf[..(offset + input.len())];
|
||||
buf[..offset].fill(ARBITRARY);
|
||||
let src = offset..;
|
||||
buf[src.clone()].copy_from_slice(input);
|
||||
|
||||
let ctr = Counter::from_nonce_and_ctr(
|
||||
Nonce::try_assume_unique_for_key(nonce).unwrap(),
|
||||
ctr,
|
||||
);
|
||||
let in_out = Overlapping::new(buf, src)
|
||||
.map_err(error::erase::<IndexError>)
|
||||
.unwrap();
|
||||
f(key, ctr, in_out, cpu);
|
||||
assert_eq!(&buf[..input.len()], expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
// Copyright 2021 Brian Smith.
|
||||
// Portions Copyright (c) 2014, Google Inc.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
// Adapted from the public domain, estream code by D. Bernstein.
|
||||
// Adapted from the BoringSSL crypto/chacha/chacha.c.
|
||||
|
||||
use super::{super::overlapping::IndexError, Counter, Key, Overlapping, BLOCK_LEN};
|
||||
use crate::{bb, polyfill::sliceutil};
|
||||
use core::mem::size_of;
|
||||
|
||||
pub(super) fn ChaCha20_ctr32(key: &Key, counter: Counter, mut in_out: Overlapping<'_>) {
|
||||
const SIGMA: [u32; 4] = [
|
||||
u32::from_le_bytes(*b"expa"),
|
||||
u32::from_le_bytes(*b"nd 3"),
|
||||
u32::from_le_bytes(*b"2-by"),
|
||||
u32::from_le_bytes(*b"te k"),
|
||||
];
|
||||
|
||||
let key = key.words_less_safe();
|
||||
let counter = counter.into_words_less_safe();
|
||||
|
||||
let mut state = [
|
||||
SIGMA[0], SIGMA[1], SIGMA[2], SIGMA[3], key[0], key[1], key[2], key[3], key[4], key[5],
|
||||
key[6], key[7], counter[0], counter[1], counter[2], counter[3],
|
||||
];
|
||||
|
||||
let mut in_out_len = in_out.len();
|
||||
|
||||
let mut buf = [0u8; BLOCK_LEN];
|
||||
while in_out_len > 0 {
|
||||
chacha_core(&mut buf, &state);
|
||||
state[12] += 1;
|
||||
|
||||
debug_assert_eq!(in_out_len, in_out.len());
|
||||
|
||||
// Both branches do the same thing, but the duplication helps the
|
||||
// compiler optimize (vectorize) the `BLOCK_LEN` case.
|
||||
if in_out_len >= BLOCK_LEN {
|
||||
in_out = in_out
|
||||
.split_first_chunk::<BLOCK_LEN>(|in_out| {
|
||||
bb::xor_assign_at_start(&mut buf, in_out.input());
|
||||
sliceutil::overwrite_at_start(in_out.into_unwritten_output(), &buf);
|
||||
})
|
||||
.unwrap_or_else(|IndexError { .. }| {
|
||||
// Since `in_out_len == in_out.len() && in_out_len >= BLOCK_LEN`.
|
||||
unreachable!()
|
||||
});
|
||||
} else {
|
||||
bb::xor_assign_at_start(&mut buf, in_out.input());
|
||||
sliceutil::overwrite_at_start(in_out.into_unwritten_output(), &buf);
|
||||
break;
|
||||
}
|
||||
|
||||
in_out_len -= BLOCK_LEN;
|
||||
}
|
||||
}
|
||||
|
||||
// Performs 20 rounds of ChaCha on `input`, storing the result in `output`.
|
||||
#[inline(always)]
|
||||
fn chacha_core(output: &mut [u8; BLOCK_LEN], input: &State) {
|
||||
let mut x = *input;
|
||||
|
||||
for _ in (0..20).step_by(2) {
|
||||
quarterround(&mut x, 0, 4, 8, 12);
|
||||
quarterround(&mut x, 1, 5, 9, 13);
|
||||
quarterround(&mut x, 2, 6, 10, 14);
|
||||
quarterround(&mut x, 3, 7, 11, 15);
|
||||
quarterround(&mut x, 0, 5, 10, 15);
|
||||
quarterround(&mut x, 1, 6, 11, 12);
|
||||
quarterround(&mut x, 2, 7, 8, 13);
|
||||
quarterround(&mut x, 3, 4, 9, 14);
|
||||
}
|
||||
|
||||
for (x, input) in x.iter_mut().zip(input.iter()) {
|
||||
*x = x.wrapping_add(*input);
|
||||
}
|
||||
|
||||
output
|
||||
.chunks_exact_mut(size_of::<u32>())
|
||||
.zip(x.iter())
|
||||
.for_each(|(output, &x)| output.copy_from_slice(&x.to_le_bytes()));
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn quarterround(x: &mut State, a: usize, b: usize, c: usize, d: usize) {
|
||||
#[inline(always)]
|
||||
fn step(x: &mut State, a: usize, b: usize, c: usize, rotation: u32) {
|
||||
x[a] = x[a].wrapping_add(x[b]);
|
||||
x[c] = (x[c] ^ x[a]).rotate_left(rotation);
|
||||
}
|
||||
step(x, a, b, d, 16);
|
||||
step(x, c, d, b, 12);
|
||||
step(x, a, b, d, 8);
|
||||
step(x, c, d, b, 7);
|
||||
}
|
||||
|
||||
type State = [u32; BLOCK_LEN / 4];
|
||||
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
// Copyright 2016-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{super::overlapping::Overlapping, Counter, Key};
|
||||
|
||||
// `unsafe { (N, C, InOut) => f }` means that the function `f` is safe to call
|
||||
// iff the in/out length is at least `N`, the CPU features `C` are available,
|
||||
// and the input type is `InOut`. If `f` supports overlapping input/output then
|
||||
// `InOut` should be `Overlapping<'_, u8>`; otherwise it should be `&mut [u8]`.
|
||||
macro_rules! chacha20_ctr32_ffi {
|
||||
( unsafe { ($MIN_LEN:expr, $Cpu:ty, $InOut:ty) => $f:ident },
|
||||
$key:expr, $counter:expr, $in_out:expr, $cpu:expr ) => {{
|
||||
prefixed_extern! {
|
||||
fn $f(
|
||||
out: *mut u8,
|
||||
in_: *const u8,
|
||||
in_len: crate::c::size_t,
|
||||
key: &[u32; 8],
|
||||
counter: &crate::aead::chacha::Counter,
|
||||
);
|
||||
}
|
||||
// SAFETY: The user asserts that $f has the signature above and is safe
|
||||
// to call if additionally we have a value of type `$Cpu` and an in/out
|
||||
// value of the indicated type, which we do.
|
||||
unsafe {
|
||||
crate::aead::chacha::ffi::chacha20_ctr32_ffi::<$InOut, $Cpu, $MIN_LEN>(
|
||||
$key, $counter, $in_out, $cpu, $f,
|
||||
)
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
// Panics if `in_out.len() < MIN_LEN`. The caller should have guarded against
|
||||
// that so that the assertion gets optimized away.
|
||||
pub(super) unsafe fn chacha20_ctr32_ffi<
|
||||
'o,
|
||||
InOut: 'o + Into<Overlapping<'o, u8>>,
|
||||
Cpu,
|
||||
const MIN_LEN: usize,
|
||||
>(
|
||||
key: &Key,
|
||||
counter: Counter,
|
||||
in_out: InOut,
|
||||
cpu: Cpu,
|
||||
f: unsafe extern "C" fn(*mut u8, *const u8, crate::c::size_t, &[u32; 8], &Counter),
|
||||
) {
|
||||
assert!(MIN_LEN > 0);
|
||||
let in_out: Overlapping<'_, u8> = in_out.into();
|
||||
in_out.with_input_output_len(|input, output, len| {
|
||||
assert!(len >= MIN_LEN);
|
||||
let key = key.words_less_safe();
|
||||
let _: Cpu = cpu;
|
||||
unsafe { f(output, input, len, key, &counter) }
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{
|
||||
super::{NONCE_LEN, TAG_LEN},
|
||||
chacha::Overlapping,
|
||||
check_input_lengths, Aad, InputTooLongError, Key, Nonce, Tag, KEY_LEN,
|
||||
};
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
macro_rules! declare_open {
|
||||
( $name:ident ) => {
|
||||
prefixed_extern! {
|
||||
fn $name(
|
||||
out_plaintext: *mut u8,
|
||||
ciphertext: *const u8,
|
||||
plaintext_len: usize,
|
||||
ad: *const u8,
|
||||
ad_len: usize,
|
||||
data: &mut InOut<open_data_in>,
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! declare_seal {
|
||||
( $name:ident ) => {
|
||||
prefixed_extern! {
|
||||
fn $name(
|
||||
out_ciphertext: *mut u8,
|
||||
plaintext: *const u8,
|
||||
plaintext_len: usize,
|
||||
ad: *const u8,
|
||||
ad_len: usize,
|
||||
data: &mut InOut<seal_data_in>,
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
|
||||
use crate::cpu::arm::Neon;
|
||||
type RequiredCpuFeatures = Neon;
|
||||
type OptionalCpuFeatures = ();
|
||||
} else {
|
||||
use crate::cpu::intel::{Avx2, Bmi2, Sse41};
|
||||
type RequiredCpuFeatures = Sse41;
|
||||
type OptionalCpuFeatures = (Avx2, Bmi2);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn seal(
|
||||
Key(key): &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
required_cpu_features: RequiredCpuFeatures,
|
||||
optional_cpu_features: Option<OptionalCpuFeatures>,
|
||||
) -> Result<Tag, InputTooLongError> {
|
||||
check_input_lengths(aad, in_out)?;
|
||||
|
||||
// XXX: BoringSSL uses `alignas(16)` on `key` instead of on the
|
||||
// structure, but Rust can't do that yet; see
|
||||
// https://github.com/rust-lang/rust/issues/73557.
|
||||
//
|
||||
// Keep in sync with the anonymous struct of BoringSSL's
|
||||
// `chacha20_poly1305_seal_data`.
|
||||
#[repr(align(16), C)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct seal_data_in {
|
||||
key: [u32; KEY_LEN / 4],
|
||||
counter: u32,
|
||||
nonce: [u8; NONCE_LEN],
|
||||
extra_ciphertext: *const u8,
|
||||
extra_ciphertext_len: usize,
|
||||
}
|
||||
|
||||
let mut data = InOut {
|
||||
input: seal_data_in {
|
||||
key: *key.words_less_safe(),
|
||||
counter: 0,
|
||||
nonce: *nonce.as_ref(),
|
||||
extra_ciphertext: core::ptr::null(),
|
||||
extra_ciphertext_len: 0,
|
||||
},
|
||||
};
|
||||
|
||||
// Encrypts `plaintext_len` bytes from `plaintext` and writes them to `out_ciphertext`.
|
||||
|
||||
let output = in_out.as_mut_ptr();
|
||||
let input = in_out.as_ptr();
|
||||
let len = in_out.len();
|
||||
let ad = aad.as_ref().as_ptr();
|
||||
let ad_len = aad.as_ref().len();
|
||||
|
||||
#[allow(clippy::needless_late_init)]
|
||||
let tag;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
|
||||
declare_seal! { chacha20_poly1305_seal }
|
||||
let _: Neon = required_cpu_features;
|
||||
let _: Option<()> = optional_cpu_features;
|
||||
tag = unsafe {
|
||||
chacha20_poly1305_seal(output, input, len, ad, ad_len, &mut data);
|
||||
&data.out.tag
|
||||
};
|
||||
} else {
|
||||
let _: Sse41 = required_cpu_features;
|
||||
if matches!(optional_cpu_features, Some((Avx2 { .. }, Bmi2 { .. }))) {
|
||||
declare_seal! { chacha20_poly1305_seal_avx2 }
|
||||
tag = unsafe {
|
||||
chacha20_poly1305_seal_avx2(output, input, len, ad, ad_len, &mut data);
|
||||
&data.out.tag
|
||||
};
|
||||
} else {
|
||||
declare_seal! { chacha20_poly1305_seal_sse41 }
|
||||
tag = unsafe {
|
||||
chacha20_poly1305_seal_sse41(output, input, len, ad, ad_len, &mut data);
|
||||
&data.out.tag
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Tag(*tag))
|
||||
}
|
||||
|
||||
pub(super) fn open(
|
||||
Key(key): &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: Overlapping<'_>,
|
||||
required_cpu_features: RequiredCpuFeatures,
|
||||
optional_cpu_features: Option<OptionalCpuFeatures>,
|
||||
) -> Result<Tag, InputTooLongError> {
|
||||
check_input_lengths(aad, in_out.input())?;
|
||||
|
||||
// XXX: BoringSSL uses `alignas(16)` on `key` instead of on the
|
||||
// structure, but Rust can't do that yet; see
|
||||
// https://github.com/rust-lang/rust/issues/73557.
|
||||
//
|
||||
// Keep in sync with the anonymous struct of BoringSSL's
|
||||
// `chacha20_poly1305_open_data`.
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(align(16), C)]
|
||||
struct open_data_in {
|
||||
key: [u32; KEY_LEN / 4],
|
||||
counter: u32,
|
||||
nonce: [u8; NONCE_LEN],
|
||||
}
|
||||
|
||||
let mut data = InOut {
|
||||
input: open_data_in {
|
||||
key: *key.words_less_safe(),
|
||||
counter: 0,
|
||||
nonce: *nonce.as_ref(),
|
||||
},
|
||||
};
|
||||
|
||||
in_out.with_input_output_len(|input, output, len| {
|
||||
let ad = aad.as_ref().as_ptr();
|
||||
let ad_len = aad.as_ref().len();
|
||||
|
||||
#[allow(clippy::needless_late_init)]
|
||||
let tag;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
|
||||
declare_open! { chacha20_poly1305_open }
|
||||
let _: Neon = required_cpu_features;
|
||||
let _: Option<()> = optional_cpu_features;
|
||||
tag = unsafe {
|
||||
chacha20_poly1305_open(output, input, len, ad, ad_len, &mut data);
|
||||
&data.out.tag
|
||||
};
|
||||
} else {
|
||||
let _: Sse41 = required_cpu_features;
|
||||
if matches!(optional_cpu_features, Some((Avx2 { .. }, Bmi2 { .. }))) {
|
||||
declare_open! { chacha20_poly1305_open_avx2 }
|
||||
tag = unsafe {
|
||||
chacha20_poly1305_open_avx2(output, input, len, ad, ad_len, &mut data);
|
||||
&data.out.tag
|
||||
};
|
||||
} else {
|
||||
declare_open! { chacha20_poly1305_open_sse41 }
|
||||
tag = unsafe {
|
||||
chacha20_poly1305_open_sse41(output, input, len, ad, ad_len, &mut data);
|
||||
&data.out.tag
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Tag(*tag))
|
||||
})
|
||||
}
|
||||
|
||||
// Keep in sync with BoringSSL's `chacha20_poly1305_open_data` and
|
||||
// `chacha20_poly1305_seal_data`.
|
||||
#[repr(C)]
|
||||
pub(super) union InOut<T>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
pub(super) input: T,
|
||||
pub(super) out: Out,
|
||||
}
|
||||
|
||||
// It isn't obvious whether the assembly code works for tags that aren't
|
||||
// 16-byte aligned. In practice it will always be 16-byte aligned because it
|
||||
// is embedded in a union where the other member of the union is 16-byte
|
||||
// aligned.
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(align(16), C)]
|
||||
pub(super) struct Out {
|
||||
pub(super) tag: [u8; TAG_LEN],
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{
|
||||
chacha::{self, Counter, Overlapping},
|
||||
poly1305, Aad, Nonce, Tag,
|
||||
};
|
||||
use crate::{
|
||||
cpu,
|
||||
error::InputTooLongError,
|
||||
polyfill::{slice, sliceutil, u64_from_usize, usize_from_u64_saturated},
|
||||
};
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"))] {
|
||||
use cpu::GetFeature as _;
|
||||
mod integrated;
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) const KEY_LEN: usize = chacha::KEY_LEN;
|
||||
|
||||
const MAX_IN_OUT_LEN: usize = super::max_input_len(64, 1);
|
||||
// https://tools.ietf.org/html/rfc8439#section-2.8
|
||||
const _MAX_IN_OUT_LEN_BOUNDED_BY_RFC: () =
|
||||
assert!(MAX_IN_OUT_LEN == usize_from_u64_saturated(274_877_906_880u64));
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct Key(chacha::Key);
|
||||
|
||||
impl Key {
|
||||
pub(super) fn new(value: [u8; KEY_LEN]) -> Self {
|
||||
Self(chacha::Key::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn seal(
|
||||
key: &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
cpu: cpu::Features,
|
||||
) -> Result<Tag, InputTooLongError> {
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
if let Some(required) = cpu.get_feature() {
|
||||
return integrated::seal(key, nonce, aad, in_out, required, cpu.get_feature());
|
||||
}
|
||||
|
||||
seal_fallback(key, nonce, aad, in_out, cpu)
|
||||
}
|
||||
|
||||
pub(super) fn seal_fallback(
|
||||
Key(chacha20_key): &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: &mut [u8],
|
||||
cpu: cpu::Features,
|
||||
) -> Result<Tag, InputTooLongError> {
|
||||
let (counter, poly1305_key) = begin(chacha20_key, nonce, aad, in_out, cpu)?;
|
||||
let mut auth = poly1305::Context::from_key(poly1305_key, cpu);
|
||||
|
||||
poly1305_update_padded_16(&mut auth, aad.as_ref());
|
||||
chacha20_key.encrypt(counter, in_out.into(), cpu);
|
||||
poly1305_update_padded_16(&mut auth, in_out);
|
||||
Ok(finish(auth, aad.as_ref().len(), in_out.len()))
|
||||
}
|
||||
|
||||
pub(super) fn open(
|
||||
key: &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: Overlapping<'_>,
|
||||
cpu: cpu::Features,
|
||||
) -> Result<Tag, InputTooLongError> {
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
if let Some(required) = cpu.get_feature() {
|
||||
return integrated::open(key, nonce, aad, in_out, required, cpu.get_feature());
|
||||
}
|
||||
|
||||
open_fallback(key, nonce, aad, in_out, cpu)
|
||||
}
|
||||
|
||||
pub(super) fn open_fallback(
|
||||
Key(chacha20_key): &Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out: Overlapping<'_>,
|
||||
cpu: cpu::Features,
|
||||
) -> Result<Tag, InputTooLongError> {
|
||||
let (counter, poly1305_key) = begin(chacha20_key, nonce, aad, in_out.input(), cpu)?;
|
||||
let mut auth = poly1305::Context::from_key(poly1305_key, cpu);
|
||||
|
||||
poly1305_update_padded_16(&mut auth, aad.as_ref());
|
||||
poly1305_update_padded_16(&mut auth, in_out.input());
|
||||
let in_out_len = in_out.len();
|
||||
chacha20_key.encrypt(counter, in_out, cpu);
|
||||
Ok(finish(auth, aad.as_ref().len(), in_out_len))
|
||||
}
|
||||
|
||||
fn check_input_lengths(aad: Aad<&[u8]>, input: &[u8]) -> Result<(), InputTooLongError> {
|
||||
if input.len() > MAX_IN_OUT_LEN {
|
||||
return Err(InputTooLongError::new(input.len()));
|
||||
}
|
||||
|
||||
// RFC 8439 Section 2.8 says the maximum AAD length is 2**64 - 1, which is
|
||||
// never larger than usize::MAX, so we don't need an explicit length
|
||||
// check.
|
||||
const _USIZE_BOUNDED_BY_U64: u64 = u64_from_usize(usize::MAX);
|
||||
let _ = aad;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Also used by chacha20_poly1305_openssh.
|
||||
pub(super) fn begin(
|
||||
key: &chacha::Key,
|
||||
nonce: Nonce,
|
||||
aad: Aad<&[u8]>,
|
||||
input: &[u8],
|
||||
cpu: cpu::Features,
|
||||
) -> Result<(Counter, poly1305::Key), InputTooLongError> {
|
||||
check_input_lengths(aad, input)?;
|
||||
|
||||
let mut key_bytes = [0u8; poly1305::KEY_LEN];
|
||||
let counter = key.encrypt_single_block_with_ctr_0(nonce, &mut key_bytes, cpu);
|
||||
let poly1305_key = poly1305::Key::new(key_bytes);
|
||||
Ok((counter, poly1305_key))
|
||||
}
|
||||
|
||||
fn finish(auth: poly1305::Context, aad_len: usize, in_out_len: usize) -> Tag {
|
||||
let mut block = [0u8; poly1305::BLOCK_LEN];
|
||||
let (alen, clen) = block.split_at_mut(poly1305::BLOCK_LEN / 2);
|
||||
alen.copy_from_slice(&u64::to_le_bytes(u64_from_usize(aad_len)));
|
||||
clen.copy_from_slice(&u64::to_le_bytes(u64_from_usize(in_out_len)));
|
||||
auth.finish(&block)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poly1305_update_padded_16(ctx: &mut poly1305::Context, input: &[u8]) {
|
||||
let (whole, remainder) = slice::as_chunks(input);
|
||||
ctx.update(whole);
|
||||
if !remainder.is_empty() {
|
||||
let mut block = [0u8; poly1305::BLOCK_LEN];
|
||||
sliceutil::overwrite_at_start(&mut block, remainder);
|
||||
ctx.update_block(block);
|
||||
}
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
// Copyright 2016 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! The [chacha20-poly1305@openssh.com] AEAD-ish construct.
|
||||
//!
|
||||
//! This should only be used by SSH implementations. It has a similar, but
|
||||
//! different API from `ring::aead` because the construct cannot use the same
|
||||
//! API as `ring::aead` due to the way the construct handles the encrypted
|
||||
//! packet length.
|
||||
//!
|
||||
//! The concatenation of a and b is denoted `a||b`. `K_1` and `K_2` are defined
|
||||
//! in the [chacha20-poly1305@openssh.com] specification. `packet_length`,
|
||||
//! `padding_length`, `payload`, and `random padding` are defined in
|
||||
//! [RFC 4253]. The term `plaintext` is used as a shorthand for
|
||||
//! `padding_length||payload||random padding`.
|
||||
//!
|
||||
//! [chacha20-poly1305@openssh.com]:
|
||||
//! http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD
|
||||
//! [RFC 4253]: https://tools.ietf.org/html/rfc4253
|
||||
|
||||
use super::{
|
||||
chacha::{self, *},
|
||||
chacha20_poly1305, cpu, poly1305, Aad, Nonce, Tag,
|
||||
};
|
||||
use crate::{
|
||||
bb,
|
||||
error::{self, InputTooLongError},
|
||||
polyfill::slice,
|
||||
};
|
||||
|
||||
/// A key for sealing packets.
|
||||
pub struct SealingKey {
|
||||
key: Key,
|
||||
}
|
||||
|
||||
impl SealingKey {
|
||||
/// Constructs a new `SealingKey`.
|
||||
pub fn new(key_material: &[u8; KEY_LEN]) -> Self {
|
||||
Self {
|
||||
key: Key::new(key_material),
|
||||
}
|
||||
}
|
||||
|
||||
/// Seals (encrypts and signs) a packet.
|
||||
///
|
||||
/// On input, `plaintext_in_ciphertext_out` must contain the unencrypted
|
||||
/// `packet_length||plaintext` where `plaintext` is the
|
||||
/// `padding_length||payload||random padding`. It will be overwritten by
|
||||
/// `encrypted_packet_length||ciphertext`, where `encrypted_packet_length`
|
||||
/// is encrypted with `K_1` and `ciphertext` is encrypted by `K_2`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `plaintext_in_ciphertext_out.len() < PACKET_LENGTH_LEN`.
|
||||
///
|
||||
/// Panics if `plaintext_in_ciphertext_out` is longer than the maximum
|
||||
/// input size for ChaCha20-Poly1305. Note that this limit is much,
|
||||
/// much larger than SSH's 256KB maximum record size.
|
||||
pub fn seal_in_place(
|
||||
&self,
|
||||
sequence_number: u32,
|
||||
plaintext_in_ciphertext_out: &mut [u8],
|
||||
tag_out: &mut [u8; TAG_LEN],
|
||||
) {
|
||||
// XXX/TODO(SemVer): Refactor API to return an error.
|
||||
let (len_in_out, data_and_padding_in_out): (&mut [u8; PACKET_LENGTH_LEN], _) =
|
||||
slice::split_first_chunk_mut(plaintext_in_ciphertext_out).unwrap();
|
||||
|
||||
let cpu = cpu::features();
|
||||
// XXX/TODO(SemVer): Refactor API to return an error.
|
||||
let (counter, poly_key) = chacha20_poly1305::begin(
|
||||
&self.key.k_2,
|
||||
make_nonce(sequence_number),
|
||||
Aad::from(len_in_out),
|
||||
data_and_padding_in_out,
|
||||
cpu,
|
||||
)
|
||||
.map_err(error::erase::<InputTooLongError>)
|
||||
.unwrap();
|
||||
|
||||
let _: Counter = self.key.k_1.encrypt_single_block_with_ctr_0(
|
||||
make_nonce(sequence_number),
|
||||
len_in_out,
|
||||
cpu,
|
||||
);
|
||||
self.key
|
||||
.k_2
|
||||
.encrypt(counter, data_and_padding_in_out.into(), cpu);
|
||||
|
||||
let Tag(tag) = poly1305::sign(poly_key, plaintext_in_ciphertext_out, cpu);
|
||||
*tag_out = tag;
|
||||
}
|
||||
}
|
||||
|
||||
/// A key for opening packets.
|
||||
pub struct OpeningKey {
|
||||
key: Key,
|
||||
}
|
||||
|
||||
impl OpeningKey {
|
||||
/// Constructs a new `OpeningKey`.
|
||||
pub fn new(key_material: &[u8; KEY_LEN]) -> Self {
|
||||
Self {
|
||||
key: Key::new(key_material),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the decrypted, but unauthenticated, packet length.
|
||||
///
|
||||
/// Importantly, the result won't be authenticated until `open_in_place` is
|
||||
/// called.
|
||||
pub fn decrypt_packet_length(
|
||||
&self,
|
||||
sequence_number: u32,
|
||||
encrypted_packet_length: [u8; PACKET_LENGTH_LEN],
|
||||
) -> [u8; PACKET_LENGTH_LEN] {
|
||||
let cpu = cpu::features();
|
||||
let mut packet_length = encrypted_packet_length;
|
||||
let _: Counter = self.key.k_1.encrypt_single_block_with_ctr_0(
|
||||
make_nonce(sequence_number),
|
||||
&mut packet_length,
|
||||
cpu,
|
||||
);
|
||||
packet_length
|
||||
}
|
||||
|
||||
/// Opens (authenticates and decrypts) a packet.
|
||||
///
|
||||
/// `ciphertext_in_plaintext_out` must be of the form
|
||||
/// `encrypted_packet_length||ciphertext` where `ciphertext` is the
|
||||
/// encrypted `plaintext`. When the function succeeds the ciphertext is
|
||||
/// replaced by the plaintext and the result is `Ok(plaintext)`, where
|
||||
/// `plaintext` is `&ciphertext_in_plaintext_out[PACKET_LENGTH_LEN..]`;
|
||||
/// otherwise the contents of `ciphertext_in_plaintext_out` are unspecified
|
||||
/// and must not be used.
|
||||
pub fn open_in_place<'a>(
|
||||
&self,
|
||||
sequence_number: u32,
|
||||
ciphertext_in_plaintext_out: &'a mut [u8],
|
||||
tag: &[u8; TAG_LEN],
|
||||
) -> Result<&'a [u8], error::Unspecified> {
|
||||
let (packet_length, after_packet_length): (&mut [u8; PACKET_LENGTH_LEN], _) =
|
||||
slice::split_first_chunk_mut(ciphertext_in_plaintext_out).ok_or(error::Unspecified)?;
|
||||
|
||||
let cpu = cpu::features();
|
||||
let (counter, poly_key) = chacha20_poly1305::begin(
|
||||
&self.key.k_2,
|
||||
make_nonce(sequence_number),
|
||||
Aad::from(packet_length),
|
||||
after_packet_length,
|
||||
cpu,
|
||||
)
|
||||
.map_err(error::erase::<InputTooLongError>)?;
|
||||
|
||||
// We must verify the tag before decrypting so that
|
||||
// `ciphertext_in_plaintext_out` is unmodified if verification fails.
|
||||
// This is beyond what we guarantee.
|
||||
let calculated_tag = poly1305::sign(poly_key, ciphertext_in_plaintext_out, cpu);
|
||||
bb::verify_slices_are_equal(calculated_tag.as_ref(), tag)?;
|
||||
|
||||
// Won't panic because the length was checked above.
|
||||
let after_packet_length = &mut ciphertext_in_plaintext_out[PACKET_LENGTH_LEN..];
|
||||
|
||||
self.key
|
||||
.k_2
|
||||
.encrypt(counter, after_packet_length.into(), cpu);
|
||||
|
||||
Ok(after_packet_length)
|
||||
}
|
||||
}
|
||||
|
||||
struct Key {
|
||||
k_1: chacha::Key,
|
||||
k_2: chacha::Key,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
fn new(key_material: &[u8; KEY_LEN]) -> Self {
|
||||
// The first half becomes K_2 and the second half becomes K_1.
|
||||
let (k_2, k_1) = key_material.split_at(chacha::KEY_LEN);
|
||||
Self {
|
||||
k_1: chacha::Key::new(k_1.try_into().unwrap()),
|
||||
k_2: chacha::Key::new(k_2.try_into().unwrap()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_nonce(sequence_number: u32) -> Nonce {
|
||||
let [s0, s1, s2, s3] = sequence_number.to_be_bytes();
|
||||
let nonce = [0, 0, 0, 0, 0, 0, 0, 0, s0, s1, s2, s3];
|
||||
Nonce::assume_unique_for_key(nonce)
|
||||
}
|
||||
|
||||
/// The length of key.
|
||||
pub const KEY_LEN: usize = chacha::KEY_LEN * 2;
|
||||
|
||||
/// The length in bytes of the `packet_length` field in a SSH packet.
|
||||
pub const PACKET_LENGTH_LEN: usize = 4; // 32 bits
|
||||
|
||||
/// The length in bytes of an authentication tag.
|
||||
pub const TAG_LEN: usize = super::TAG_LEN;
|
||||
Vendored
+163
@@ -0,0 +1,163 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use self::ffi::{Block, BLOCK_LEN, ZERO_BLOCK};
|
||||
use super::{aes_gcm, Aad};
|
||||
use crate::{
|
||||
bits::{BitLength, FromByteLen as _},
|
||||
error::{self, InputTooLongError},
|
||||
polyfill::{slice::AsChunks, sliceutil::overwrite_at_start, NotSend},
|
||||
};
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
pub(super) use ffi::KeyValue;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(any(all(target_arch = "aarch64", target_endian = "little"), target_arch = "x86_64"))] {
|
||||
pub(super) use self::ffi::{HTable, Xi};
|
||||
} else {
|
||||
use self::ffi::{HTable, Xi};
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
mod ffi;
|
||||
|
||||
pub(super) mod clmul;
|
||||
pub(super) mod clmulavxmovbe;
|
||||
pub(super) mod fallback;
|
||||
pub(super) mod neon;
|
||||
pub(super) mod vclmulavx2;
|
||||
|
||||
pub(super) struct Context<'key, K> {
|
||||
Xi: Xi,
|
||||
key: &'key K,
|
||||
aad_len: BitLength<u64>,
|
||||
in_out_len: BitLength<u64>,
|
||||
_not_send: NotSend,
|
||||
}
|
||||
|
||||
impl<'key, K: UpdateBlock> Context<'key, K> {
|
||||
#[inline(always)]
|
||||
pub(crate) fn new(
|
||||
key: &'key K,
|
||||
aad: Aad<&[u8]>,
|
||||
in_out_len: usize,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
if in_out_len > aes_gcm::MAX_IN_OUT_LEN {
|
||||
return Err(error::Unspecified);
|
||||
}
|
||||
let in_out_len =
|
||||
BitLength::from_byte_len(in_out_len).map_err(error::erase::<InputTooLongError>)?;
|
||||
let aad_len = BitLength::from_byte_len(aad.as_ref().len())
|
||||
.map_err(error::erase::<InputTooLongError>)?;
|
||||
|
||||
// NIST SP800-38D Section 5.2.1.1 says that the maximum AAD length is
|
||||
// 2**64 - 1 bits, i.e. BitLength<u64>::MAX, so we don't need to do an
|
||||
// explicit check here.
|
||||
|
||||
let mut ctx = Self {
|
||||
Xi: Xi(ZERO_BLOCK),
|
||||
key,
|
||||
aad_len,
|
||||
in_out_len,
|
||||
_not_send: NotSend::VALUE,
|
||||
};
|
||||
|
||||
for ad in aad.0.chunks(BLOCK_LEN) {
|
||||
let mut block = ZERO_BLOCK;
|
||||
overwrite_at_start(&mut block, ad);
|
||||
ctx.update_block(block);
|
||||
}
|
||||
|
||||
Ok(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
target_arch = "aarch64",
|
||||
target_endian = "little",
|
||||
target_pointer_width = "64"
|
||||
))]
|
||||
impl<K> Context<'_, K> {
|
||||
pub(super) fn in_out_whole_block_bits(&self) -> BitLength<usize> {
|
||||
use crate::polyfill::usize_from_u64;
|
||||
const WHOLE_BLOCK_BITS_MASK: usize = !0b111_1111;
|
||||
#[allow(clippy::assertions_on_constants)]
|
||||
const _WHOLE_BLOCK_BITS_MASK_CORRECT: () =
|
||||
assert!(WHOLE_BLOCK_BITS_MASK == !((BLOCK_LEN * 8) - 1));
|
||||
BitLength::from_bits(usize_from_u64(self.in_out_len.as_bits()) & WHOLE_BLOCK_BITS_MASK)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
|
||||
/// Access to `inner` for the integrated AES-GCM implementations only.
|
||||
impl Context<'_, clmul::Key> {
|
||||
#[inline]
|
||||
pub(super) fn inner(&mut self) -> (&HTable, &mut Xi) {
|
||||
(&self.key.inner(), &mut self.Xi)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
impl Context<'_, clmulavxmovbe::Key> {
|
||||
/// Access to `inner` for the integrated AES-GCM implementations only.
|
||||
#[inline]
|
||||
pub(super) fn inner(&mut self) -> (&HTable, &mut Xi) {
|
||||
(self.key.inner(), &mut self.Xi)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
impl Context<'_, vclmulavx2::Key> {
|
||||
/// Access to `inner` for the integrated AES-GCM implementations only.
|
||||
#[inline]
|
||||
pub(super) fn inner(&mut self) -> (&HTable, &mut Xi) {
|
||||
(self.key.inner(), &mut self.Xi)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: UpdateBlocks> Context<'_, K> {
|
||||
#[inline(always)]
|
||||
pub fn update_blocks(&mut self, input: AsChunks<u8, BLOCK_LEN>) {
|
||||
self.key.update_blocks(&mut self.Xi, input);
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: UpdateBlock> Context<'_, K> {
|
||||
pub fn update_block(&mut self, a: Block) {
|
||||
self.key.update_block(&mut self.Xi, a);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(super) fn pre_finish<F>(mut self, f: F) -> super::Tag
|
||||
where
|
||||
F: FnOnce(Block) -> super::Tag,
|
||||
{
|
||||
let mut block = [0u8; BLOCK_LEN];
|
||||
let (alen, clen) = block.split_at_mut(BLOCK_LEN / 2);
|
||||
alen.copy_from_slice(&BitLength::<u64>::to_be_bytes(self.aad_len));
|
||||
clen.copy_from_slice(&BitLength::<u64>::to_be_bytes(self.in_out_len));
|
||||
self.update_block(block);
|
||||
f(self.Xi.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) trait UpdateBlock {
|
||||
fn update_block(&self, xi: &mut Xi, a: Block);
|
||||
}
|
||||
|
||||
pub(super) trait UpdateBlocks {
|
||||
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>);
|
||||
}
|
||||
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
|
||||
use super::{ffi::KeyValue, HTable, UpdateBlock, Xi};
|
||||
use crate::aead::gcm::ffi::BLOCK_LEN;
|
||||
use crate::cpu;
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
use {super::UpdateBlocks, crate::polyfill::slice::AsChunks};
|
||||
|
||||
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
|
||||
pub(in super::super) type RequiredCpuFeatures = cpu::arm::PMull;
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
pub(in super::super) type RequiredCpuFeatures = (cpu::intel::ClMul, cpu::intel::Ssse3);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
h_table: HTable,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
#[cfg_attr(target_arch = "x86_64", inline(never))]
|
||||
pub(in super::super) fn new(value: KeyValue, _cpu: RequiredCpuFeatures) -> Self {
|
||||
Self {
|
||||
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub(super) fn inner(&self) -> &HTable {
|
||||
&self.h_table
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlock for Key {
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
|
||||
prefixed_extern! {
|
||||
fn gcm_gmult_clmul(xi: &mut Xi, Htable: &HTable);
|
||||
}
|
||||
xi.bitxor_assign(a);
|
||||
unsafe { self.h_table.gmult(gcm_gmult_clmul, xi) };
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
|
||||
self.update_blocks(xi, (&a).into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
impl UpdateBlocks for Key {
|
||||
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, { BLOCK_LEN }>) {
|
||||
unsafe { ghash!(gcm_ghash_clmul, xi, &self.h_table, input) }
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(target_arch = "x86_64")]
|
||||
|
||||
use super::{HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
|
||||
use crate::{cpu::intel, polyfill::slice::AsChunks};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
h_table: HTable,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
#[inline(never)]
|
||||
pub(in super::super) fn new(
|
||||
value: KeyValue,
|
||||
_required_cpu_features: (intel::ClMul, intel::Avx, intel::Movbe),
|
||||
) -> Self {
|
||||
Self {
|
||||
h_table: unsafe { htable_new!(gcm_init_avx, value) },
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn inner(&self) -> &HTable {
|
||||
&self.h_table
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlock for Key {
|
||||
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
|
||||
self.update_blocks(xi, (&a).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlocks for Key {
|
||||
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) {
|
||||
unsafe { ghash!(gcm_ghash_avx, xi, self.inner(), input) }
|
||||
}
|
||||
}
|
||||
Vendored
+271
@@ -0,0 +1,271 @@
|
||||
// Copyright (c) 2019, Google Inc.
|
||||
// Portions Copyright 2020-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// This file is based on BoringSSL's gcm_nohw.c.
|
||||
|
||||
// This file contains a implementation of GHASH based on the notes
|
||||
// in https://bearssl.org/constanttime.html#ghash-for-gcm and the reduction
|
||||
// algorithm described in
|
||||
// https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf.
|
||||
//
|
||||
// Unlike the BearSSL notes, we use u128 in the 64-bit implementation.
|
||||
|
||||
use super::{ffi::U128, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
|
||||
use crate::polyfill::{slice::AsChunks, ArraySplitMap as _};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
h: U128,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
pub(in super::super) fn new(value: KeyValue) -> Self {
|
||||
Self { h: init(value) }
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlock for Key {
|
||||
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
|
||||
xi.bitxor_assign(a);
|
||||
gmult(xi, self.h);
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlocks for Key {
|
||||
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) {
|
||||
ghash(xi, self.h, input);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
fn gcm_mul64_nohw(a: u64, b: u64) -> (u64, u64) {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
#[inline(always)]
|
||||
fn lo(a: u128) -> u64 {
|
||||
a as u64
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn hi(a: u128) -> u64 {
|
||||
lo(a >> 64)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul(a: u64, b: u64) -> u128 {
|
||||
u128::from(a) * u128::from(b)
|
||||
}
|
||||
|
||||
// One term every four bits means the largest term is 64/4 = 16, which barely
|
||||
// overflows into the next term. Using one term every five bits would cost 25
|
||||
// multiplications instead of 16. It is faster to mask off the bottom four
|
||||
// bits of |a|, giving a largest term of 60/4 = 15, and apply the bottom bits
|
||||
// separately.
|
||||
let a0 = a & 0x1111111111111110;
|
||||
let a1 = a & 0x2222222222222220;
|
||||
let a2 = a & 0x4444444444444440;
|
||||
let a3 = a & 0x8888888888888880;
|
||||
|
||||
let b0 = b & 0x1111111111111111;
|
||||
let b1 = b & 0x2222222222222222;
|
||||
let b2 = b & 0x4444444444444444;
|
||||
let b3 = b & 0x8888888888888888;
|
||||
|
||||
let c0 = mul(a0, b0) ^ mul(a1, b3) ^ mul(a2, b2) ^ mul(a3, b1);
|
||||
let c1 = mul(a0, b1) ^ mul(a1, b0) ^ mul(a2, b3) ^ mul(a3, b2);
|
||||
let c2 = mul(a0, b2) ^ mul(a1, b1) ^ mul(a2, b0) ^ mul(a3, b3);
|
||||
let c3 = mul(a0, b3) ^ mul(a1, b2) ^ mul(a2, b1) ^ mul(a3, b0);
|
||||
|
||||
// Multiply the bottom four bits of |a| with |b|.
|
||||
let a0_mask = 0u64.wrapping_sub(a & 1);
|
||||
let a1_mask = 0u64.wrapping_sub((a >> 1) & 1);
|
||||
let a2_mask = 0u64.wrapping_sub((a >> 2) & 1);
|
||||
let a3_mask = 0u64.wrapping_sub((a >> 3) & 1);
|
||||
let extra = u128::from(a0_mask & b)
|
||||
^ (u128::from(a1_mask & b) << 1)
|
||||
^ (u128::from(a2_mask & b) << 2)
|
||||
^ (u128::from(a3_mask & b) << 3);
|
||||
|
||||
let lo = (lo(c0) & 0x1111111111111111)
|
||||
^ (lo(c1) & 0x2222222222222222)
|
||||
^ (lo(c2) & 0x4444444444444444)
|
||||
^ (lo(c3) & 0x8888888888888888)
|
||||
^ lo(extra);
|
||||
let hi = (hi(c0) & 0x1111111111111111)
|
||||
^ (hi(c1) & 0x2222222222222222)
|
||||
^ (hi(c2) & 0x4444444444444444)
|
||||
^ (hi(c3) & 0x8888888888888888)
|
||||
^ hi(extra);
|
||||
(lo, hi)
|
||||
}
|
||||
|
||||
#[cfg(not(target_pointer_width = "64"))]
|
||||
fn gcm_mul32_nohw(a: u32, b: u32) -> u64 {
|
||||
#[inline(always)]
|
||||
fn mul(a: u32, b: u32) -> u64 {
|
||||
u64::from(a) * u64::from(b)
|
||||
}
|
||||
|
||||
// One term every four bits means the largest term is 32/4 = 8, which does not
|
||||
// overflow into the next term.
|
||||
let a0 = a & 0x11111111;
|
||||
let a1 = a & 0x22222222;
|
||||
let a2 = a & 0x44444444;
|
||||
let a3 = a & 0x88888888;
|
||||
|
||||
let b0 = b & 0x11111111;
|
||||
let b1 = b & 0x22222222;
|
||||
let b2 = b & 0x44444444;
|
||||
let b3 = b & 0x88888888;
|
||||
|
||||
let c0 = mul(a0, b0) ^ mul(a1, b3) ^ mul(a2, b2) ^ mul(a3, b1);
|
||||
let c1 = mul(a0, b1) ^ mul(a1, b0) ^ mul(a2, b3) ^ mul(a3, b2);
|
||||
let c2 = mul(a0, b2) ^ mul(a1, b1) ^ mul(a2, b0) ^ mul(a3, b3);
|
||||
let c3 = mul(a0, b3) ^ mul(a1, b2) ^ mul(a2, b1) ^ mul(a3, b0);
|
||||
|
||||
(c0 & 0x1111111111111111)
|
||||
| (c1 & 0x2222222222222222)
|
||||
| (c2 & 0x4444444444444444)
|
||||
| (c3 & 0x8888888888888888)
|
||||
}
|
||||
|
||||
#[cfg(not(target_pointer_width = "64"))]
|
||||
fn gcm_mul64_nohw(a: u64, b: u64) -> (u64, u64) {
|
||||
#[inline(always)]
|
||||
fn lo(a: u64) -> u32 {
|
||||
a as u32
|
||||
}
|
||||
#[inline(always)]
|
||||
fn hi(a: u64) -> u32 {
|
||||
lo(a >> 32)
|
||||
}
|
||||
|
||||
let a0 = lo(a);
|
||||
let a1 = hi(a);
|
||||
let b0 = lo(b);
|
||||
let b1 = hi(b);
|
||||
// Karatsuba multiplication.
|
||||
let lo = gcm_mul32_nohw(a0, b0);
|
||||
let hi = gcm_mul32_nohw(a1, b1);
|
||||
let mid = gcm_mul32_nohw(a0 ^ a1, b0 ^ b1) ^ lo ^ hi;
|
||||
(lo ^ (mid << 32), hi ^ (mid >> 32))
|
||||
}
|
||||
|
||||
fn init(value: KeyValue) -> U128 {
|
||||
let xi = value.into_inner();
|
||||
|
||||
// We implement GHASH in terms of POLYVAL, as described in RFC 8452. This
|
||||
// avoids a shift by 1 in the multiplication, needed to account for bit
|
||||
// reversal losing a bit after multiplication, that is,
|
||||
// rev128(X) * rev128(Y) = rev255(X*Y).
|
||||
//
|
||||
// Per Appendix A, we run mulX_POLYVAL. Note this is the same transformation
|
||||
// applied by |gcm_init_clmul|, etc. Note |Xi| has already been byteswapped.
|
||||
//
|
||||
// See also slide 16 of
|
||||
// https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf
|
||||
let mut lo = xi[1];
|
||||
let mut hi = xi[0];
|
||||
|
||||
let mut carry = hi >> 63;
|
||||
carry = 0u64.wrapping_sub(carry);
|
||||
|
||||
hi <<= 1;
|
||||
hi |= lo >> 63;
|
||||
lo <<= 1;
|
||||
|
||||
// The irreducible polynomial is 1 + x^121 + x^126 + x^127 + x^128, so we
|
||||
// conditionally add 0xc200...0001.
|
||||
lo ^= carry & 1;
|
||||
hi ^= carry & 0xc200000000000000;
|
||||
|
||||
// This implementation does not use the rest of |Htable|.
|
||||
U128 { hi, lo }
|
||||
}
|
||||
|
||||
fn gcm_polyval_nohw(xi: &mut [u64; 2], h: U128) {
|
||||
// Karatsuba multiplication. The product of |Xi| and |H| is stored in |r0|
|
||||
// through |r3|. Note there is no byte or bit reversal because we are
|
||||
// evaluating POLYVAL.
|
||||
let (r0, mut r1) = gcm_mul64_nohw(xi[0], h.lo);
|
||||
let (mut r2, mut r3) = gcm_mul64_nohw(xi[1], h.hi);
|
||||
let (mut mid0, mut mid1) = gcm_mul64_nohw(xi[0] ^ xi[1], h.hi ^ h.lo);
|
||||
mid0 ^= r0 ^ r2;
|
||||
mid1 ^= r1 ^ r3;
|
||||
r2 ^= mid1;
|
||||
r1 ^= mid0;
|
||||
|
||||
// Now we multiply our 256-bit result by x^-128 and reduce. |r2| and
|
||||
// |r3| shifts into position and we must multiply |r0| and |r1| by x^-128. We
|
||||
// have:
|
||||
//
|
||||
// 1 = x^121 + x^126 + x^127 + x^128
|
||||
// x^-128 = x^-7 + x^-2 + x^-1 + 1
|
||||
//
|
||||
// This is the GHASH reduction step, but with bits flowing in reverse.
|
||||
|
||||
// The x^-7, x^-2, and x^-1 terms shift bits past x^0, which would require
|
||||
// another reduction steps. Instead, we gather the excess bits, incorporate
|
||||
// them into |r0| and |r1| and reduce once. See slides 17-19
|
||||
// of https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf.
|
||||
r1 ^= (r0 << 63) ^ (r0 << 62) ^ (r0 << 57);
|
||||
|
||||
// 1
|
||||
r2 ^= r0;
|
||||
r3 ^= r1;
|
||||
|
||||
// x^-1
|
||||
r2 ^= r0 >> 1;
|
||||
r2 ^= r1 << 63;
|
||||
r3 ^= r1 >> 1;
|
||||
|
||||
// x^-2
|
||||
r2 ^= r0 >> 2;
|
||||
r2 ^= r1 << 62;
|
||||
r3 ^= r1 >> 2;
|
||||
|
||||
// x^-7
|
||||
r2 ^= r0 >> 7;
|
||||
r2 ^= r1 << 57;
|
||||
r3 ^= r1 >> 7;
|
||||
|
||||
*xi = [r2, r3];
|
||||
}
|
||||
|
||||
fn gmult(xi: &mut Xi, h: U128) {
|
||||
with_swapped_xi(xi, |swapped| {
|
||||
gcm_polyval_nohw(swapped, h);
|
||||
})
|
||||
}
|
||||
|
||||
fn ghash(xi: &mut Xi, h: U128, input: AsChunks<u8, BLOCK_LEN>) {
|
||||
with_swapped_xi(xi, |swapped| {
|
||||
input.into_iter().for_each(|&input| {
|
||||
let input = input.array_split_map(u64::from_be_bytes);
|
||||
swapped[0] ^= input[1];
|
||||
swapped[1] ^= input[0];
|
||||
gcm_polyval_nohw(swapped, h);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_swapped_xi(Xi(xi): &mut Xi, f: impl FnOnce(&mut [u64; 2])) {
|
||||
let unswapped: [u64; 2] = xi.array_split_map(u64::from_be_bytes);
|
||||
let mut swapped: [u64; 2] = [unswapped[1], unswapped[0]];
|
||||
f(&mut swapped);
|
||||
let (xi_0, xi_1) = xi.split_at_mut(BLOCK_LEN / 2);
|
||||
xi_0.copy_from_slice(&u64::to_be_bytes(swapped[1]));
|
||||
xi_1.copy_from_slice(&u64::to_be_bytes(swapped[0]));
|
||||
}
|
||||
Vendored
+165
@@ -0,0 +1,165 @@
|
||||
// Copyright 2018 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::{
|
||||
bb,
|
||||
polyfill::{slice::AsChunks, ArraySplitMap},
|
||||
};
|
||||
|
||||
pub(in super::super) const BLOCK_LEN: usize = 16;
|
||||
pub(in super::super) type Block = [u8; BLOCK_LEN];
|
||||
pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN];
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
macro_rules! htable_new {
|
||||
( $name:ident, $value:expr $(,)? ) => {{
|
||||
use crate::aead::gcm::ffi::HTable;
|
||||
prefixed_extern! {
|
||||
fn $name(HTable: &mut HTable, h: &[u64; 2]);
|
||||
}
|
||||
HTable::new($name, $value)
|
||||
}};
|
||||
}
|
||||
|
||||
/// SAFETY:
|
||||
/// * The function `$name` must meet the contract of the `f` paramweter of
|
||||
/// `ghash()`.
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
macro_rules! ghash {
|
||||
( $name:ident, $xi:expr, $h_table:expr, $input:expr $(,)? ) => {{
|
||||
use crate::aead::gcm::ffi::{HTable, Xi};
|
||||
prefixed_extern! {
|
||||
fn $name(
|
||||
xi: &mut Xi,
|
||||
Htable: &HTable,
|
||||
inp: *const u8,
|
||||
len: crate::c::NonZero_size_t,
|
||||
);
|
||||
}
|
||||
$h_table.ghash($name, $xi, $input)
|
||||
}};
|
||||
}
|
||||
|
||||
pub(in super::super) struct KeyValue([u64; 2]);
|
||||
|
||||
impl KeyValue {
|
||||
pub(in super::super) fn new(value: Block) -> Self {
|
||||
Self(value.array_split_map(u64::from_be_bytes))
|
||||
}
|
||||
|
||||
pub(super) fn into_inner(self) -> [u64; 2] {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// SAFETY:
|
||||
/// * `f` must read `len` bytes from `inp`; it may assume
|
||||
/// that `len` is a (non-zero) multiple of `BLOCK_LEN`.
|
||||
/// * `f` may inspect CPU features.
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64"
|
||||
))]
|
||||
impl HTable {
|
||||
pub(super) unsafe fn new(
|
||||
init: unsafe extern "C" fn(HTable: &mut HTable, &[u64; 2]),
|
||||
value: KeyValue,
|
||||
) -> Self {
|
||||
let mut r = Self {
|
||||
Htable: [U128 { hi: 0, lo: 0 }; HTABLE_LEN],
|
||||
};
|
||||
unsafe { init(&mut r, &value.0) };
|
||||
r
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
))]
|
||||
pub(super) unsafe fn gmult(
|
||||
&self,
|
||||
f: unsafe extern "C" fn(xi: &mut Xi, h_table: &HTable),
|
||||
xi: &mut Xi,
|
||||
) {
|
||||
unsafe { f(xi, self) }
|
||||
}
|
||||
|
||||
pub(super) unsafe fn ghash(
|
||||
&self,
|
||||
f: unsafe extern "C" fn(
|
||||
xi: &mut Xi,
|
||||
Htable: &HTable,
|
||||
inp: *const u8,
|
||||
len: crate::c::NonZero_size_t,
|
||||
),
|
||||
xi: &mut Xi,
|
||||
input: AsChunks<u8, BLOCK_LEN>,
|
||||
) {
|
||||
use core::num::NonZeroUsize;
|
||||
|
||||
let input = input.as_flattened();
|
||||
|
||||
let input_len = match NonZeroUsize::new(input.len()) {
|
||||
Some(len) => len,
|
||||
None => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// SAFETY:
|
||||
// * There are `input_len: NonZeroUsize` bytes available at `input` for
|
||||
// `f` to read.
|
||||
unsafe {
|
||||
f(xi, self, input.as_ptr(), input_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The alignment is required by some assembly code, such as `ghash-ssse3-*`.
|
||||
#[derive(Clone)]
|
||||
#[repr(C, align(16))]
|
||||
pub(in super::super) struct HTable {
|
||||
Htable: [U128; HTABLE_LEN],
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(C)]
|
||||
pub(super) struct U128 {
|
||||
pub(super) hi: u64,
|
||||
pub(super) lo: u64,
|
||||
}
|
||||
|
||||
const HTABLE_LEN: usize = 16;
|
||||
|
||||
#[repr(transparent)]
|
||||
pub(in super::super) struct Xi(pub(super) Block);
|
||||
|
||||
impl Xi {
|
||||
#[inline]
|
||||
pub(super) fn bitxor_assign(&mut self, a: Block) {
|
||||
self.0 = bb::xor_16(self.0, a)
|
||||
}
|
||||
}
|
||||
Vendored
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright 2018-2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(any(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
all(target_arch = "arm", target_endian = "little")
|
||||
))]
|
||||
|
||||
use super::{HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
|
||||
use crate::{cpu, polyfill::slice::AsChunks};
|
||||
|
||||
pub(in super::super) type RequiredCpuFeatures = cpu::arm::Neon;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
h_table: HTable,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
pub(in super::super) fn new(value: KeyValue, _cpu: RequiredCpuFeatures) -> Self {
|
||||
Self {
|
||||
h_table: unsafe { htable_new!(gcm_init_neon, value) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlock for Key {
|
||||
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
|
||||
prefixed_extern! {
|
||||
fn gcm_gmult_neon(xi: &mut Xi, Htable: &HTable);
|
||||
}
|
||||
xi.bitxor_assign(a);
|
||||
unsafe { self.h_table.gmult(gcm_gmult_neon, xi) };
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlocks for Key {
|
||||
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) {
|
||||
unsafe { ghash!(gcm_ghash_neon, xi, &self.h_table, input) }
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
// Copyright 2018-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(target_arch = "x86_64")]
|
||||
|
||||
use super::{ffi::KeyValue, HTable, UpdateBlock, Xi};
|
||||
use crate::{
|
||||
aead::gcm::ffi::BLOCK_LEN,
|
||||
cpu::intel::{Avx2, VAesClmul},
|
||||
polyfill::slice::AsChunks,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
h_table: HTable,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
pub(in super::super) fn new(value: KeyValue, _cpu: (Avx2, VAesClmul)) -> Self {
|
||||
Self {
|
||||
h_table: unsafe { htable_new!(gcm_init_vpclmulqdq_avx2, value) },
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn inner(&self) -> &HTable {
|
||||
&self.h_table
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateBlock for Key {
|
||||
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
|
||||
let input: AsChunks<u8, BLOCK_LEN> = (&a).into();
|
||||
unsafe { ghash!(gcm_ghash_vpclmulqdq_avx2_1, xi, &self.h_table, input) }
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
// Copyright 2015-2021 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{Aad, Algorithm, KeyInner, Nonce, Tag, UnboundKey, TAG_LEN};
|
||||
use crate::{cpu, error};
|
||||
use core::ops::RangeFrom;
|
||||
|
||||
/// Immutable keys for use in situations where `OpeningKey`/`SealingKey` and
|
||||
/// `NonceSequence` cannot reasonably be used.
|
||||
///
|
||||
/// Prefer to use `OpeningKey`/`SealingKey` and `NonceSequence` when practical.
|
||||
#[derive(Clone)]
|
||||
pub struct LessSafeKey {
|
||||
inner: KeyInner,
|
||||
algorithm: &'static Algorithm,
|
||||
}
|
||||
|
||||
impl LessSafeKey {
|
||||
/// Constructs a `LessSafeKey`.
|
||||
#[inline]
|
||||
pub fn new(key: UnboundKey) -> Self {
|
||||
key.into_inner()
|
||||
}
|
||||
|
||||
pub(super) fn new_(
|
||||
algorithm: &'static Algorithm,
|
||||
key_bytes: &[u8],
|
||||
cpu_features: cpu::Features,
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
Ok(Self {
|
||||
inner: algorithm.new_key(key_bytes, cpu_features)?,
|
||||
algorithm,
|
||||
})
|
||||
}
|
||||
|
||||
/// Like [open_in_place](Self::open_in_place), except the authentication tag is
|
||||
/// passed separately.
|
||||
#[inline]
|
||||
pub fn open_in_place_separate_tag<'in_out, A>(
|
||||
&self,
|
||||
nonce: Nonce,
|
||||
aad: Aad<A>,
|
||||
tag: Tag,
|
||||
in_out: &'in_out mut [u8],
|
||||
ciphertext: RangeFrom<usize>,
|
||||
) -> Result<&'in_out mut [u8], error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
let aad = Aad::from(aad.as_ref());
|
||||
self.algorithm.open_within(
|
||||
&self.inner,
|
||||
nonce,
|
||||
aad,
|
||||
tag,
|
||||
in_out,
|
||||
ciphertext,
|
||||
cpu::features(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Like [`super::OpeningKey::open_in_place()`], except it accepts an
|
||||
/// arbitrary nonce.
|
||||
///
|
||||
/// `nonce` must be unique for every use of the key to open data.
|
||||
#[inline]
|
||||
pub fn open_in_place<'in_out, A>(
|
||||
&self,
|
||||
nonce: Nonce,
|
||||
aad: Aad<A>,
|
||||
in_out: &'in_out mut [u8],
|
||||
) -> Result<&'in_out mut [u8], error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
self.open_within(nonce, aad, in_out, 0..)
|
||||
}
|
||||
|
||||
/// Like [`super::OpeningKey::open_within()`], except it accepts an
|
||||
/// arbitrary nonce.
|
||||
///
|
||||
/// `nonce` must be unique for every use of the key to open data.
|
||||
#[inline]
|
||||
pub fn open_within<'in_out, A>(
|
||||
&self,
|
||||
nonce: Nonce,
|
||||
aad: Aad<A>,
|
||||
in_out: &'in_out mut [u8],
|
||||
ciphertext_and_tag: RangeFrom<usize>,
|
||||
) -> Result<&'in_out mut [u8], error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
let tag_offset = in_out
|
||||
.len()
|
||||
.checked_sub(TAG_LEN)
|
||||
.ok_or(error::Unspecified)?;
|
||||
|
||||
// Split the tag off the end of `in_out`.
|
||||
let (in_out, received_tag) = in_out.split_at_mut(tag_offset);
|
||||
let received_tag = (*received_tag).try_into()?;
|
||||
let ciphertext = ciphertext_and_tag;
|
||||
|
||||
self.open_in_place_separate_tag(nonce, aad, received_tag, in_out, ciphertext)
|
||||
}
|
||||
|
||||
/// Like [`super::SealingKey::seal_in_place_append_tag()`], except it
|
||||
/// accepts an arbitrary nonce.
|
||||
///
|
||||
/// `nonce` must be unique for every use of the key to seal data.
|
||||
#[inline]
|
||||
pub fn seal_in_place_append_tag<A, InOut>(
|
||||
&self,
|
||||
nonce: Nonce,
|
||||
aad: Aad<A>,
|
||||
in_out: &mut InOut,
|
||||
) -> Result<(), error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
|
||||
{
|
||||
self.seal_in_place_separate_tag(nonce, aad, in_out.as_mut())
|
||||
.map(|tag| in_out.extend(tag.as_ref()))
|
||||
}
|
||||
|
||||
/// Like `super::SealingKey::seal_in_place_separate_tag()`, except it
|
||||
/// accepts an arbitrary nonce.
|
||||
///
|
||||
/// `nonce` must be unique for every use of the key to seal data.
|
||||
#[inline]
|
||||
pub fn seal_in_place_separate_tag<A>(
|
||||
&self,
|
||||
nonce: Nonce,
|
||||
aad: Aad<A>,
|
||||
in_out: &mut [u8],
|
||||
) -> Result<Tag, error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
self.algorithm.seal(
|
||||
&self.inner,
|
||||
nonce,
|
||||
Aad::from(aad.as_ref()),
|
||||
in_out,
|
||||
cpu::features(),
|
||||
)
|
||||
}
|
||||
|
||||
/// The key's AEAD algorithm.
|
||||
#[inline]
|
||||
pub fn algorithm(&self) -> &'static Algorithm {
|
||||
self.algorithm
|
||||
}
|
||||
|
||||
pub(super) fn fmt_debug(
|
||||
&self,
|
||||
type_name: &'static str,
|
||||
f: &mut core::fmt::Formatter,
|
||||
) -> Result<(), core::fmt::Error> {
|
||||
f.debug_struct(type_name)
|
||||
.field("algorithm", &self.algorithm())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for LessSafeKey {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
self.fmt_debug("LessSafeKey", f)
|
||||
}
|
||||
}
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
// Copyright 2018 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::error;
|
||||
|
||||
/// A nonce for a single AEAD opening or sealing operation.
|
||||
///
|
||||
/// The user must ensure, for a particular key, that each nonce is unique.
|
||||
///
|
||||
/// `Nonce` intentionally doesn't implement `Clone` to ensure that each one is
|
||||
/// consumed at most once.
|
||||
pub struct Nonce([u8; NONCE_LEN]);
|
||||
|
||||
impl Nonce {
|
||||
/// Constructs a `Nonce` with the given value, assuming that the value is
|
||||
/// unique for the lifetime of the key it is being used with.
|
||||
///
|
||||
/// Fails if `value` isn't `NONCE_LEN` bytes long.
|
||||
#[inline]
|
||||
pub fn try_assume_unique_for_key(value: &[u8]) -> Result<Self, error::Unspecified> {
|
||||
let value: &[u8; NONCE_LEN] = value.try_into()?;
|
||||
Ok(Self::assume_unique_for_key(*value))
|
||||
}
|
||||
|
||||
/// Constructs a `Nonce` with the given value, assuming that the value is
|
||||
/// unique for the lifetime of the key it is being used with.
|
||||
#[inline]
|
||||
pub fn assume_unique_for_key(value: [u8; NONCE_LEN]) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8; NONCE_LEN]> for Nonce {
|
||||
fn as_ref(&self) -> &[u8; NONCE_LEN] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// All the AEADs we support use 96-bit nonces.
|
||||
pub const NONCE_LEN: usize = 96 / 8;
|
||||
Vendored
+143
@@ -0,0 +1,143 @@
|
||||
// Copyright 2015-2021 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! Authenticated Encryption with Associated Data (AEAD).
|
||||
//!
|
||||
//! See [Authenticated encryption: relations among notions and analysis of the
|
||||
//! generic composition paradigm][AEAD] for an introduction to the concept of
|
||||
//! AEADs.
|
||||
//!
|
||||
//! [AEAD]: https://eprint.iacr.org/2000/025.pdf
|
||||
//! [`crypto.cipher.AEAD`]: https://golang.org/pkg/crypto/cipher/#AEAD
|
||||
|
||||
use super::{Aad, Algorithm, BoundKey, LessSafeKey, NonceSequence, UnboundKey};
|
||||
use crate::error;
|
||||
use core::ops::RangeFrom;
|
||||
|
||||
/// An AEAD key for authenticating and decrypting ("opening"), bound to a nonce
|
||||
/// sequence.
|
||||
///
|
||||
/// Intentionally not `Clone` or `Copy` since cloning would allow duplication
|
||||
/// of the nonce sequence.
|
||||
pub struct OpeningKey<N: NonceSequence> {
|
||||
key: LessSafeKey,
|
||||
nonce_sequence: N,
|
||||
}
|
||||
|
||||
impl<N: NonceSequence> BoundKey<N> for OpeningKey<N> {
|
||||
fn new(key: UnboundKey, nonce_sequence: N) -> Self {
|
||||
Self {
|
||||
key: key.into_inner(),
|
||||
nonce_sequence,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn algorithm(&self) -> &'static Algorithm {
|
||||
self.key.algorithm()
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: NonceSequence> core::fmt::Debug for OpeningKey<N> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
self.key.fmt_debug("OpeningKey", f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: NonceSequence> OpeningKey<N> {
|
||||
/// Authenticates and decrypts (“opens”) data in place.
|
||||
///
|
||||
/// `aad` is the additional authenticated data (AAD), if any.
|
||||
///
|
||||
/// On input, `in_out` must be the ciphertext followed by the tag. When
|
||||
/// `open_in_place()` returns `Ok(plaintext)`, the input ciphertext
|
||||
/// has been overwritten by the plaintext; `plaintext` will refer to the
|
||||
/// plaintext without the tag.
|
||||
///
|
||||
/// When `open_in_place()` returns `Err(..)`, `in_out` may have been
|
||||
/// overwritten in an unspecified way.
|
||||
#[inline]
|
||||
pub fn open_in_place<'in_out, A>(
|
||||
&mut self,
|
||||
aad: Aad<A>,
|
||||
in_out: &'in_out mut [u8],
|
||||
) -> Result<&'in_out mut [u8], error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
self.key
|
||||
.open_in_place(self.nonce_sequence.advance()?, aad, in_out)
|
||||
}
|
||||
|
||||
/// Authenticates and decrypts (“opens”) data in place, with a shift.
|
||||
///
|
||||
/// `aad` is the additional authenticated data (AAD), if any.
|
||||
///
|
||||
/// On input, `in_out[ciphertext_and_tag]` must be the ciphertext followed
|
||||
/// by the tag. When `open_within()` returns `Ok(plaintext)`, the plaintext
|
||||
/// will be at `in_out[0..plaintext.len()]`. In other words, the following
|
||||
/// two code fragments are equivalent for valid values of
|
||||
/// `ciphertext_and_tag`, except `open_within` will often be more efficient:
|
||||
///
|
||||
///
|
||||
/// ```skip
|
||||
/// let plaintext = key.open_within(aad, in_out, cipertext_and_tag)?;
|
||||
/// ```
|
||||
///
|
||||
/// ```skip
|
||||
/// let ciphertext_and_tag_len = in_out[ciphertext_and_tag].len();
|
||||
/// in_out.copy_within(ciphertext_and_tag, 0);
|
||||
/// let plaintext = key.open_in_place(aad, &mut in_out[..ciphertext_and_tag_len])?;
|
||||
/// ```
|
||||
///
|
||||
/// Similarly, `key.open_within(aad, in_out, 0..)` is equivalent to
|
||||
/// `key.open_in_place(aad, in_out)`.
|
||||
///
|
||||
/// When `open_in_place()` returns `Err(..)`, `in_out` may have been
|
||||
/// overwritten in an unspecified way.
|
||||
///
|
||||
/// The shifting feature is useful in the case where multiple packets are
|
||||
/// being reassembled in place. Consider this example where the peer has
|
||||
/// sent the message “Split stream reassembled in place” split into
|
||||
/// three sealed packets:
|
||||
///
|
||||
/// ```ascii-art
|
||||
/// Packet 1 Packet 2 Packet 3
|
||||
/// Input: [Header][Ciphertext][Tag][Header][Ciphertext][Tag][Header][Ciphertext][Tag]
|
||||
/// | +--------------+ |
|
||||
/// +------+ +-----+ +----------------------------------+
|
||||
/// v v v
|
||||
/// Output: [Plaintext][Plaintext][Plaintext]
|
||||
/// “Split stream reassembled in place”
|
||||
/// ```
|
||||
///
|
||||
/// This reassembly can be accomplished with three calls to `open_within()`.
|
||||
#[inline]
|
||||
pub fn open_within<'in_out, A>(
|
||||
&mut self,
|
||||
aad: Aad<A>,
|
||||
in_out: &'in_out mut [u8],
|
||||
ciphertext_and_tag: RangeFrom<usize>,
|
||||
) -> Result<&'in_out mut [u8], error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
self.key.open_within(
|
||||
self.nonce_sequence.advance()?,
|
||||
aad,
|
||||
in_out,
|
||||
ciphertext_and_tag,
|
||||
)
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg_attr(not(test), allow(dead_code))]
|
||||
|
||||
use super::Overlapping;
|
||||
use crate::error::LenMismatchError;
|
||||
use core::array::TryFromSliceError;
|
||||
|
||||
pub struct Array<'o, T, const N: usize> {
|
||||
// Invariant: N != 0.
|
||||
// Invariant: `self.in_out.len() == N`.
|
||||
in_out: Overlapping<'o, T>,
|
||||
}
|
||||
|
||||
impl<'o, T, const N: usize> Array<'o, T, N> {
|
||||
pub(super) fn new(in_out: Overlapping<'o, T>) -> Result<Self, LenMismatchError> {
|
||||
if N == 0 || in_out.len() != N {
|
||||
return Err(LenMismatchError::new(N));
|
||||
}
|
||||
Ok(Self { in_out })
|
||||
}
|
||||
|
||||
pub fn into_unwritten_output(self) -> &'o mut [T; N]
|
||||
where
|
||||
&'o mut [T]: TryInto<&'o mut [T; N], Error = TryFromSliceError>,
|
||||
{
|
||||
self.in_out
|
||||
.into_unwritten_output()
|
||||
.try_into()
|
||||
.unwrap_or_else(|TryFromSliceError { .. }| {
|
||||
unreachable!() // Due to invariant
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const N: usize> Array<'_, T, N> {
|
||||
pub fn input<'s>(&'s self) -> &'s [T; N]
|
||||
where
|
||||
&'s [T]: TryInto<&'s [T; N], Error = TryFromSliceError>,
|
||||
{
|
||||
self.in_out
|
||||
.input()
|
||||
.try_into()
|
||||
.unwrap_or_else(|TryFromSliceError { .. }| {
|
||||
unreachable!() // Due to invariant
|
||||
})
|
||||
}
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
pub use self::index_error::IndexError;
|
||||
use super::Array;
|
||||
use crate::error::LenMismatchError;
|
||||
use core::{mem, ops::RangeFrom};
|
||||
|
||||
pub struct Overlapping<'o, T> {
|
||||
// Invariant: self.src.start <= in_out.len().
|
||||
in_out: &'o mut [T],
|
||||
src: RangeFrom<usize>,
|
||||
}
|
||||
|
||||
impl<'o, T> From<&'o mut [T]> for Overlapping<'o, T> {
|
||||
fn from(in_out: &'o mut [T]) -> Self {
|
||||
Self { in_out, src: 0.. }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'o, T> Overlapping<'o, T> {
|
||||
pub fn new(in_out: &'o mut [T], src: RangeFrom<usize>) -> Result<Self, IndexError> {
|
||||
match in_out.get(src.clone()) {
|
||||
Some(_) => Ok(Self { in_out, src }),
|
||||
None => Err(IndexError::new(src.start)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86"
|
||||
))]
|
||||
pub fn copy_within(self) -> &'o mut [T]
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
if self.src.start == 0 {
|
||||
self.in_out
|
||||
} else {
|
||||
let len = self.len();
|
||||
self.in_out.copy_within(self.src, 0);
|
||||
&mut self.in_out[..len]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
all(target_arch = "arm", target_endian = "little"),
|
||||
target_arch = "x86"
|
||||
))]
|
||||
pub fn into_slice_src_mut(self) -> (&'o mut [T], RangeFrom<usize>) {
|
||||
(self.in_out, self.src)
|
||||
}
|
||||
|
||||
pub fn into_unwritten_output(self) -> &'o mut [T] {
|
||||
let len = self.len();
|
||||
self.in_out.get_mut(..len).unwrap_or_else(|| {
|
||||
// The invariant ensures this succeeds.
|
||||
unreachable!()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Overlapping<'_, T> {
|
||||
pub fn len(&self) -> usize {
|
||||
self.input().len()
|
||||
}
|
||||
|
||||
pub fn input(&self) -> &[T] {
|
||||
self.in_out.get(self.src.clone()).unwrap_or_else(|| {
|
||||
// Ensured by invariant.
|
||||
unreachable!()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_input_output_len<R>(self, f: impl FnOnce(*const T, *mut T, usize) -> R) -> R {
|
||||
let len = self.len();
|
||||
let output = self.in_out.as_mut_ptr();
|
||||
// TODO: MSRV(1.65): use `output.cast_const()`
|
||||
let output_const: *const T = output;
|
||||
// SAFETY: The constructor ensures that `src` is a valid range.
|
||||
// Equivalent to `self.in_out[src.clone()].as_ptr()` but without
|
||||
// worries about compatibility with the stacked borrows model.
|
||||
// TODO(MSRV-1.80, probably): Avoid special casing 0; see
|
||||
// https://github.com/rust-lang/rust/pull/117329
|
||||
// https://github.com/rust-lang/rustc_codegen_gcc/issues/516
|
||||
let input = if self.src.start == 0 {
|
||||
output_const
|
||||
} else {
|
||||
unsafe { output_const.add(self.src.start) }
|
||||
};
|
||||
f(input, output, len)
|
||||
}
|
||||
|
||||
// Perhaps unlike `slice::split_first_chunk_mut`, this is biased,
|
||||
// performance-wise, against the case where `N > self.len()`, so callers
|
||||
// should be structured to avoid that.
|
||||
//
|
||||
// If the result is `Err` then nothing was written to `self`; if anything
|
||||
// was written then the result will not be `Err`.
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
pub fn split_first_chunk<const N: usize>(
|
||||
mut self,
|
||||
f: impl for<'a> FnOnce(Array<'a, T, N>),
|
||||
) -> Result<Self, IndexError> {
|
||||
let src = self.src.clone();
|
||||
let end = self
|
||||
.src
|
||||
.start
|
||||
.checked_add(N)
|
||||
.ok_or_else(|| IndexError::new(N))?;
|
||||
let first = self
|
||||
.in_out
|
||||
.get_mut(..end)
|
||||
.ok_or_else(|| IndexError::new(N))?;
|
||||
let first = Overlapping::new(first, src).unwrap_or_else(|IndexError { .. }| {
|
||||
// Since `end == src.start + N`.
|
||||
unreachable!()
|
||||
});
|
||||
let first = Array::new(first).unwrap_or_else(|LenMismatchError { .. }| {
|
||||
// Since `end == src.start + N`.
|
||||
unreachable!()
|
||||
});
|
||||
// Once we call `f`, we must return `Ok` because `f` may have written
|
||||
// over (part of) the input.
|
||||
Ok({
|
||||
f(first);
|
||||
let tail = mem::take(&mut self.in_out).get_mut(N..).unwrap_or_else(|| {
|
||||
// There are at least `N` elements since `end == src.start + N`.
|
||||
unreachable!()
|
||||
});
|
||||
Self::new(tail, self.src).unwrap_or_else(|IndexError { .. }| {
|
||||
// Follows from `end == src.start + N`.
|
||||
unreachable!()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
cold_exhaustive_error! {
|
||||
struct index_error::IndexError { index: usize }
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
pub use self::{
|
||||
array::Array,
|
||||
base::{IndexError, Overlapping},
|
||||
partial_block::PartialBlock,
|
||||
};
|
||||
|
||||
mod array;
|
||||
mod base;
|
||||
mod partial_block;
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::Overlapping;
|
||||
use crate::error::InputTooLongError;
|
||||
|
||||
pub struct PartialBlock<'i, T, const BLOCK_LEN: usize> {
|
||||
// invariant: `self.in_out.len() < BLOCK_LEN`.
|
||||
in_out: Overlapping<'i, T>,
|
||||
}
|
||||
|
||||
impl<'i, T, const BLOCK_LEN: usize> PartialBlock<'i, T, BLOCK_LEN> {
|
||||
pub fn new(in_out: Overlapping<'i, T>) -> Result<Self, InputTooLongError> {
|
||||
let len = in_out.len();
|
||||
if len >= BLOCK_LEN {
|
||||
return Err(InputTooLongError::new(len));
|
||||
}
|
||||
Ok(Self { in_out })
|
||||
}
|
||||
|
||||
pub fn overwrite_at_start(self, padded: [T; BLOCK_LEN])
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
let len = self.len();
|
||||
let output = self.in_out.into_unwritten_output();
|
||||
assert!(output.len() <= padded.len());
|
||||
output.copy_from_slice(&padded[..len]);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const BLOCK_LEN: usize> PartialBlock<'_, T, BLOCK_LEN> {
|
||||
#[inline(always)]
|
||||
pub fn input(&self) -> &[T] {
|
||||
let r = self.in_out.input();
|
||||
// Help the optimizer optimize the caller using the invariant.
|
||||
// TODO: Does this actually help?
|
||||
if r.len() >= BLOCK_LEN {
|
||||
unreachable!()
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn len(&self) -> usize {
|
||||
self.input().len()
|
||||
}
|
||||
}
|
||||
Vendored
+117
@@ -0,0 +1,117 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// TODO: enforce maximum input length.
|
||||
|
||||
use super::{Tag, TAG_LEN};
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
use crate::cpu::GetFeature as _;
|
||||
use crate::{cpu, polyfill::slice::AsChunks};
|
||||
|
||||
mod ffi_arm_neon;
|
||||
mod ffi_fallback;
|
||||
|
||||
/// A Poly1305 key.
|
||||
pub(super) struct Key {
|
||||
key_and_nonce: [u8; KEY_LEN],
|
||||
}
|
||||
|
||||
pub(super) const BLOCK_LEN: usize = 16;
|
||||
pub(super) const KEY_LEN: usize = 2 * BLOCK_LEN;
|
||||
|
||||
impl Key {
|
||||
#[inline]
|
||||
pub(super) fn new(key_and_nonce: [u8; KEY_LEN]) -> Self {
|
||||
Self { key_and_nonce }
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) enum Context {
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
ArmNeon(ffi_arm_neon::State),
|
||||
Fallback(ffi_fallback::State),
|
||||
}
|
||||
|
||||
impl Context {
|
||||
#[inline]
|
||||
pub(super) fn from_key(key: Key, cpu: cpu::Features) -> Self {
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
if let Some(cpu) = cpu.get_feature() {
|
||||
return ffi_arm_neon::State::new_context(key, cpu);
|
||||
}
|
||||
let _: cpu::Features = cpu;
|
||||
ffi_fallback::State::new_context(key)
|
||||
}
|
||||
|
||||
pub fn update_block(&mut self, input: [u8; BLOCK_LEN]) {
|
||||
self.update(AsChunks::from_ref(&input))
|
||||
}
|
||||
|
||||
pub fn update(&mut self, input: AsChunks<u8, BLOCK_LEN>) {
|
||||
self.update_internal(input.as_flattened());
|
||||
}
|
||||
|
||||
fn update_internal(&mut self, input: &[u8]) {
|
||||
match self {
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
Self::ArmNeon(state) => state.update_internal(input),
|
||||
Self::Fallback(state) => state.update_internal(input),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn finish(mut self, input: &[u8]) -> Tag {
|
||||
self.update_internal(input);
|
||||
match self {
|
||||
#[cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
Self::ArmNeon(state) => state.finish(),
|
||||
Self::Fallback(state) => state.finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Implements the original, non-IETF padding semantics.
|
||||
///
|
||||
/// This is used by chacha20_poly1305_openssh and the standalone
|
||||
/// poly1305 test vectors.
|
||||
pub(super) fn sign(key: Key, input: &[u8], cpu_features: cpu::Features) -> Tag {
|
||||
let ctx = Context::from_key(key, cpu_features);
|
||||
ctx.finish(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::testutil as test;
|
||||
|
||||
// Adapted from BoringSSL's crypto/poly1305/poly1305_test.cc.
|
||||
#[test]
|
||||
pub fn test_poly1305() {
|
||||
let cpu_features = cpu::features();
|
||||
test::run(
|
||||
test_vector_file!("poly1305_test.txt"),
|
||||
|section, test_case| {
|
||||
assert_eq!(section, "");
|
||||
let key = test_case.consume_bytes("Key");
|
||||
let key: &[u8; KEY_LEN] = key.as_slice().try_into().unwrap();
|
||||
let input = test_case.consume_bytes("Input");
|
||||
let expected_mac = test_case.consume_bytes("MAC");
|
||||
let key = Key::new(*key);
|
||||
let Tag(actual_mac) = sign(key, &input, cpu_features);
|
||||
assert_eq!(expected_mac, actual_mac.as_ref());
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
// Portions Copyright (c) 2014, 2015, Google Inc.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(all(target_arch = "arm", target_endian = "little"))]
|
||||
|
||||
use super::{Key, Tag, KEY_LEN, TAG_LEN};
|
||||
use crate::{c, cpu::arm::Neon};
|
||||
use core::num::NonZeroUsize;
|
||||
|
||||
// XXX/TODO(MSRV): change to `pub(super)`.
|
||||
pub(in super::super) struct State {
|
||||
state: poly1305_state_st,
|
||||
neon: Neon,
|
||||
}
|
||||
|
||||
// TODO: Is 16 enough?
|
||||
#[repr(C, align(16))]
|
||||
struct poly1305_state_st {
|
||||
r: fe1305x2,
|
||||
h: fe1305x2,
|
||||
c: fe1305x2,
|
||||
precomp: [fe1305x2; 2],
|
||||
|
||||
data: [u8; data_len()],
|
||||
|
||||
buf: [u8; 32],
|
||||
buf_used: c::size_t,
|
||||
key: [u8; 16],
|
||||
}
|
||||
|
||||
const fn data_len() -> usize {
|
||||
128
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(C)]
|
||||
struct fe1305x2 {
|
||||
v: [u32; 12], // for alignment; only using 10
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub(super) fn new_context(Key { key_and_nonce }: Key, neon: Neon) -> super::Context {
|
||||
prefixed_extern! {
|
||||
fn CRYPTO_poly1305_init_neon(state: &mut poly1305_state_st, key: &[u8; KEY_LEN]);
|
||||
}
|
||||
let mut r = Self {
|
||||
state: poly1305_state_st {
|
||||
r: fe1305x2 { v: [0; 12] },
|
||||
h: fe1305x2 { v: [0; 12] },
|
||||
c: fe1305x2 { v: [0; 12] },
|
||||
precomp: [fe1305x2 { v: [0; 12] }; 2],
|
||||
|
||||
data: [0u8; data_len()],
|
||||
buf: Default::default(),
|
||||
buf_used: 0,
|
||||
key: [0u8; 16],
|
||||
},
|
||||
neon,
|
||||
};
|
||||
unsafe { CRYPTO_poly1305_init_neon(&mut r.state, &key_and_nonce) }
|
||||
super::Context::ArmNeon(r)
|
||||
}
|
||||
|
||||
pub(super) fn update_internal(&mut self, input: &[u8]) {
|
||||
prefixed_extern! {
|
||||
fn CRYPTO_poly1305_update_neon(
|
||||
st: &mut poly1305_state_st,
|
||||
input: *const u8,
|
||||
in_len: c::NonZero_size_t);
|
||||
}
|
||||
if let Some(len) = NonZeroUsize::new(input.len()) {
|
||||
let _: Neon = self.neon;
|
||||
let input = input.as_ptr();
|
||||
unsafe { CRYPTO_poly1305_update_neon(&mut self.state, input, len) }
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn finish(mut self) -> Tag {
|
||||
prefixed_extern! {
|
||||
fn CRYPTO_poly1305_finish_neon(st: &mut poly1305_state_st, mac: &mut [u8; TAG_LEN]);
|
||||
}
|
||||
let mut tag = Tag([0u8; TAG_LEN]);
|
||||
unsafe { CRYPTO_poly1305_finish_neon(&mut self.state, &mut tag.0) }
|
||||
tag
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
// Copyright 2015-2025 Brian Smith.
|
||||
// Portions Copyright (c) 2014, 2015, Google Inc.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{Key, Tag, KEY_LEN, TAG_LEN};
|
||||
use crate::c;
|
||||
use core::num::NonZeroUsize;
|
||||
|
||||
// XXX/TODO(MSRV): change to `pub(super)`.
|
||||
pub(in super::super) struct State {
|
||||
state: poly1305_state_st,
|
||||
}
|
||||
|
||||
// Keep in sync with `poly1305_state_st` in poly1305.c
|
||||
#[repr(C, align(64))]
|
||||
struct poly1305_state_st {
|
||||
r0: u32,
|
||||
r1: u32,
|
||||
r2: u32,
|
||||
r3: u32,
|
||||
r4: u32,
|
||||
s1: u32,
|
||||
s2: u32,
|
||||
s3: u32,
|
||||
s4: u32,
|
||||
h0: u32,
|
||||
h1: u32,
|
||||
h2: u32,
|
||||
h3: u32,
|
||||
h4: u32,
|
||||
key: [u8; 16],
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub(super) fn new_context(Key { key_and_nonce }: Key) -> super::Context {
|
||||
prefixed_extern! {
|
||||
fn CRYPTO_poly1305_init(state: &mut poly1305_state_st, key: &[u8; KEY_LEN]);
|
||||
}
|
||||
let mut r = Self {
|
||||
state: poly1305_state_st {
|
||||
r0: 0,
|
||||
r1: 0,
|
||||
r2: 0,
|
||||
r3: 0,
|
||||
r4: 0,
|
||||
s1: 0,
|
||||
s2: 0,
|
||||
s3: 0,
|
||||
s4: 0,
|
||||
h0: 0,
|
||||
h1: 0,
|
||||
h2: 0,
|
||||
h3: 0,
|
||||
h4: 0,
|
||||
key: [0u8; 16],
|
||||
},
|
||||
};
|
||||
unsafe { CRYPTO_poly1305_init(&mut r.state, &key_and_nonce) }
|
||||
super::Context::Fallback(r)
|
||||
}
|
||||
|
||||
// `input.len % BLOCK_LEN == 0` must be true for every call except the
|
||||
// final one.
|
||||
pub(super) fn update_internal(&mut self, input: &[u8]) {
|
||||
prefixed_extern! {
|
||||
fn CRYPTO_poly1305_update(
|
||||
state: &mut poly1305_state_st,
|
||||
input: *const u8,
|
||||
in_len: c::NonZero_size_t);
|
||||
}
|
||||
if let Some(len) = NonZeroUsize::new(input.len()) {
|
||||
let input = input.as_ptr();
|
||||
unsafe { CRYPTO_poly1305_update(&mut self.state, input, len) }
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn finish(mut self) -> Tag {
|
||||
prefixed_extern! {
|
||||
fn CRYPTO_poly1305_finish(statep: &mut poly1305_state_st, mac: &mut [u8; TAG_LEN]);
|
||||
}
|
||||
let mut tag = Tag([0u8; TAG_LEN]);
|
||||
unsafe { CRYPTO_poly1305_finish(&mut self.state, &mut tag.0) }
|
||||
tag
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
# Test Vectors from OpenSSL commit bbe9769ba66ab2512678a87b0d9b266ba970db05.
|
||||
|
||||
Key = 2d773be37adb1e4d683bf0075e79c4ee037918535a7f99ccb7040fb5f5f43aea
|
||||
Input = 89dab80b7717c1db5db437860a3f70218e93e1b8f461fb677f16f35f6f87e2a91c99bc3a47ace47640cc95c345be5ecca5a3523c35cc01893af0b64a620334270372ec12482d1b1e363561698a578b359803495bb4e2ef1930b17a5190b580f141300df30adbeca28f6427a8bc1a999fd51c554a017d095d8c3e3127daf9f595
|
||||
MAC = c85d15ed44c378d6b00e23064c7bcd51
|
||||
|
||||
Key = 99e5822dd4173c995e3dae0ddefb97743fde3b080134b39f76e9bf8d0e88d546
|
||||
Input = 000000000000000b170303020000000006db1f1f368d696a810a349c0c714c9a5e7850c2407d721acded95e018d7a85266a6e1289cdb4aeb18da5ac8a2b0026d24a59ad485227f3eaedbb2e7e35e1c66cd60f9abf716dcc9ac42682dd7dab287a7024c4eefc321cc0574e16793e37cec03c5bda42b54c114a80b57af26416c7be742005e20855c73e21dc8e2edc9d435cb6f6059280011c270b71570051c1c9b3052126620bc1e2730fa066c7a509d53c60e5ae1b40aa6e39e49669228c90eecb4a50db32a50bc49e90b4f4b359a1dfd11749cd3867fcf2fb7bb6cd4738f6a4ad6f7ca5058f7618845af9f020f6c3b967b8f4cd4a91e2813b507ae66f2d35c18284f7292186062e10fd5510d18775351ef334e7634ab4743f5b68f49adcab384d3fd75f7390f4006ef2a295c8c7a076ad54546cd25d2107fbe1436c840924aaebe5b370893cd63d1325b8616fc4810886bc152c53221b6df373119393255ee72bcaa880174f1717f9184fa91646f17a24ac55d16bfddca9581a92eda479201f0edbf633600d6066d1ab36d5d2415d71351bbcd608a25108d25641992c1f26c531cf9f90203bc4cc19f5927d834b0a47116d3884bbb164b8ec883d1ac832e56b3918a98601a08d171881541d594db399c6ae6151221745aec814c45b0b05b565436fd6f137aa10a0c0b643761dbd6f9a9dcb99b1a6e690854ce0769cde39761d82fcdec15f0d92d7d8e94ade8eb83fbe0
|
||||
MAC = 2637408fe13086ea73f971e3425e2820
|
||||
|
||||
|
||||
# RFC 8439, section 2.5.2.
|
||||
|
||||
Key = 85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b
|
||||
Input = "Cryptographic Forum Research Group"
|
||||
MAC = a8061dc1305136c6c22b8baf0c0127a9
|
||||
|
||||
|
||||
# RFC 8439, section A.3.
|
||||
|
||||
Key = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
Input = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
MAC = 00000000000000000000000000000000
|
||||
|
||||
Key = 0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e
|
||||
Input = 416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f
|
||||
MAC = 36e5f6b5c5e06070f0efca96227a863e
|
||||
|
||||
Key = 36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000
|
||||
Input = 416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f
|
||||
MAC = f3477e7cd95417af89a6b8794c310cf0
|
||||
|
||||
Key = 1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0
|
||||
Input = 2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e
|
||||
MAC = 4541669a7eaaee61e708dc7cbcc5eb62
|
||||
|
||||
Key = 0200000000000000000000000000000000000000000000000000000000000000
|
||||
Input = ffffffffffffffffffffffffffffffff
|
||||
MAC = 03000000000000000000000000000000
|
||||
|
||||
Key = 02000000000000000000000000000000ffffffffffffffffffffffffffffffff
|
||||
Input = 02000000000000000000000000000000
|
||||
MAC = 03000000000000000000000000000000
|
||||
|
||||
Key = 0100000000000000000000000000000000000000000000000000000000000000
|
||||
Input = fffffffffffffffffffffffffffffffff0ffffffffffffffffffffffffffffff11000000000000000000000000000000
|
||||
MAC = 05000000000000000000000000000000
|
||||
|
||||
Key = 0100000000000000000000000000000000000000000000000000000000000000
|
||||
Input = fffffffffffffffffffffffffffffffffbfefefefefefefefefefefefefefefe01010101010101010101010101010101
|
||||
MAC = 00000000000000000000000000000000
|
||||
|
||||
Key = 0200000000000000000000000000000000000000000000000000000000000000
|
||||
Input = fdffffffffffffffffffffffffffffff
|
||||
MAC = faffffffffffffffffffffffffffffff
|
||||
|
||||
Key = 0100000000000000040000000000000000000000000000000000000000000000
|
||||
Input = e33594d7505e43b900000000000000003394d7505e4379cd01000000000000000000000000000000000000000000000001000000000000000000000000000000
|
||||
MAC = 14000000000000005500000000000000
|
||||
|
||||
Key = 0100000000000000040000000000000000000000000000000000000000000000
|
||||
Input = e33594d7505e43b900000000000000003394d7505e4379cd010000000000000000000000000000000000000000000000
|
||||
MAC = 13000000000000000000000000000000
|
||||
|
||||
|
||||
# Additional test vectors that are long enough to ensure OpenSSL's SIMD
|
||||
# assembly is fully tested.
|
||||
|
||||
# Length 2048.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfed
|
||||
MAC = 69d28f73dd09d39a92aa179da354b7ea
|
||||
|
||||
# Length 2049.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc8
|
||||
MAC = d6a26654b88572e875d9661c83471c1b
|
||||
|
||||
# Length 2050.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852
|
||||
MAC = 9fbbb7f7adcd0cd5b46a4a520b22499a
|
||||
|
||||
# Length 2051.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f5
|
||||
MAC = eb7cdceb97ade2a07622f8f5a4b1ce15
|
||||
|
||||
# Length 2052.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f590
|
||||
MAC = d41c310927cd92e14784ea78b85503db
|
||||
|
||||
# Length 2053.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073
|
||||
MAC = 16af133c423f783a14c49d9f526384cf
|
||||
|
||||
# Length 2054.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4
|
||||
MAC = 00c75db8f0636b22f195645b03091f5f
|
||||
|
||||
# Length 2055.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f434
|
||||
MAC = 4a532bc740f581555831345f3b75bf33
|
||||
|
||||
# Length 2056.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a
|
||||
MAC = 698c7d32c5923871d124a2479e521706
|
||||
|
||||
# Length 2057.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c
|
||||
MAC = a677187dbf3c927aeeafb9ebce0f61dc
|
||||
|
||||
# Length 2058.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c8a
|
||||
MAC = 201fed7eee981b31d2cc42ff6c38141a
|
||||
|
||||
# Length 2059.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c8a28
|
||||
MAC = 0c3d3d01a37f347c4f7c5826bcafb3e1
|
||||
|
||||
# Length 2060.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c8a28c9
|
||||
MAC = 33a4e0e0bed7c84c5cc5dd4784410f07
|
||||
|
||||
# Length 2061.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c8a28c99e
|
||||
MAC = 8e41c40a2f8ec58fe594f3a3a2de4ae1
|
||||
|
||||
# Length 2062.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c8a28c99e21
|
||||
MAC = c6e5d1810fd878ac6b844c66cef36a22
|
||||
|
||||
# Length 2063.
|
||||
Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
||||
Input = 248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e874cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936aff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a3709894e4eb0a4eedc4ae19468e66b81f271351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb14cf77bde35e78ae9ad7d3f57ed7e7f23926c9172f82d77684ea5ed7d74ebc6f142b997036bcb7cce8df1bbc0d5b35a46509c954fc9469d214d6238f166cbf872156b4c41d7aac5942cffb175023078252a3f36e315c5d4ce0e39928a018252862becacef96a19f03bdcf46d75584299d1f8b03c0169e9e407d937145b5e5024139e7022a1978f114f24cdfa23780a119735c41da8fb759bbb3f025c6ec30e6c6e9bce8615be68e392fce59fd26a8e6a6cc5c606e3848116e4d01d29565a1facfb524b6d29643b826eee1e42869fc76df229dd79b39a2b1df28bb335c3a5f15a855d0121e4a6da34b5e4d5b7b5d5746a03ecff70811e1516fcec1bf7462e8876a2d21710aa168c78f45a6a15015950e221da85d3ec822ad6d0a6931b25a06b7bb5f3c10bb36cd4d647f9561982fde9818de5d4bf8db7f86c53b4ff14928ac15f79023b61861e73e44216540bb302153770da2533de9795252ab5fb77ad924c9338c8144c23d4c90dab9a18feac1a1574d4545e1435eb405e6c4c439fc724fce992ae85badf345bad16d85fbd338f04433703614754d0e7e54c4ccde2670587d52ecfb5a70a14a501bacc727722649931d8515b13d020a78e511fe136d45fbf97f9c7f689fcc677cfb3683723878350ffe9d08130cc6e567b6179e01b7eb2b3bbcf0873e1308eec018edeb8cce946338e15d5bf68c71916a83a99358039ef071e009546a2df936879dffbba397a93925d229a469fd17d71b7f524e03a30da6ee927542f8b369bed4734fe25dbd63d24ffd2a222f5f84f75d858ab989be925af570ad6d45bd28ce61b5139e1dd2f0b7795fe072e6e83acbb5e7b777a70c641e4cab2af40eed69abc334cd2703c3273204fac580c6a3d6680427e5f7d051e8380a53f93a180f4556ecea4530b9a2d5948dad63d415b6874f6b90e767d6d265be86351b53ba690780bb57c21b57418c5b97559e840c68257f839e7583a4bf7c7645c5987d40cc1ba79a218c35edfacdabe581d950e4bb7a481ebe64d61d00e75b1f25f1ce5f5462334a5b9038a697aa0937a3f8017e05d2c9c05dcb05c0b02508dea619b137f5444b6f088eb3cb2c66788f88afdfbba8faa1c490485624c88ae11e57347a676902e7553f056188493209bdbb30acc63c9e41e16a9d6c009416b520a76ba38f57628170c43626b5cb46179dc5bf65de865085f84bf741c223fbe474d2d19d8f43914fbd6586351089e73babf344f988b7963fe44528457d7aad3c564f6bcbd0d772a4c9fd328e6022d1c7c9f86726f8d5a23797d309c0f653ab1ac687833eb2700f156296062a8b377078f45f6b68c3d07cae1913ba8d5a6f9bf7525a3439eb932d4cefc4bf8e1b07b48ca13ece366cbc3e0388915915d1757475103a9e9454e7e6355de2d6acbf4710f9a63e4f6d3cd70c2d6fca88dd8a14448fdb63ce9350fdaafbe0b8bd1c5d307dae76dfed799aef2d8f23d5608d37d1330dd38b94860905dbeebf78d7b7318b7d42aed40d3f9899e9f420cbd92a6eeae3026f7725694e0e4bee016ba346fed2c21172bdb4a461cebe0cfe38e76645226ac127a259c193264d735ce8c8a57e17dd3f0579e2e86dc295ad1f45ba2d85db35044da61f7d401274b31eefbeb34e8d2ae596e9b4541aae117bdac5ed0b324c20539c27c07a411d5288b0b5f6fa16e9a7df85dc319fa6b71cd08a859c06a3f7b0289e1750adbf182f9750fea96fea5ab7aa3473340607cd7ed2c626f5382491c26d5d5bea61401dee7319c94d418f297e61ceac8f258ee8c23831bda081591f5a918e96855774ddedffc51e5b180f1971806d42fc333020b734aeb45adb0bc47325d0cea5f6713a786558022afc39d573892aa3635efbfd8bcb11c57f306c72146afe8b45388125cb7bf9ecf965a7ba4f768c77be366470dcdcf214b7f6a5a9460ed4fe44ae559d85e2fdc2094de83fff12ea8804db1215c4ca865871bdd7f8ef32ab799bf923ffb02c1ded7d129beadad46c5eda31ab1a6f43da05ea08bff7ffa88d8966353d01830558c39b930b01d175e437124d8edd0d2698fd8932f2b2c9b14746e52879c57a395538150f390264f00e60d470711202f4194499ff79037ca9885dc8d695f7d917a3086ca88e8f8d0243efee09302cf39e039eb7cc8dd19d28120d5fe533b5727cd39133181c729ca6f90a015ed30be7668d5cb5ecc33a53ee69bf7d1a5ecbdb153803743c6adaaabd36bf84e5be38d3f04a5d5dbfd67bdcd3b176e65bd1391ade775cc32ce43a847fb6c672a3fe97a5d4081c4986959ec5fb898f42a9397ba2b3ec2c1018f8d76d057f2366bd0e4465514ad6560c599664fb85621fe771e00f43d39b591b2a6a321100f4d1ef23a376d5ae3eeedbfe23da73dff0ee4d16b34ebddd8f5f053db9824105fc7300dbee7ea6af56b112319e3e215a0fc79ae946f6b5227453ec7fcaf17cf7651f71499a50d81221404d5f129ac50ea7528ff0e0069ec4ab8acb7919d81749ab37a870c5ef2cc5a15cf96709d3c65b4addc77e7416847160bcabb94ea36377e0ef71be80b5cc53effd5444888044a353574c72c924bba2a8b4e8354188ebfedc852f59073f4347a8c8a28c99e21df
|
||||
MAC = f6eaae369c3cb5c05748e8d919178e00
|
||||
|
||||
# Regression test for https://rt.openssl.org/Ticket/Display.html?id=4439
|
||||
Key = 2d773be37adb1e4d683bf0075e79c4ee037918535a7f99ccb7040fb5f5f43aea
|
||||
Input = 89dab80b7717c1db5db437860a3f70218e93e1b8f461fb677f16f35f6f87e2a91c99bc3a47ace47640cc95c345be5ecca5a3523c35cc01893af0b64a620334270372ec12482d1b1e363561698a578b359803495bb4e2ef1930b17a5190b580f141300df30adbeca28f6427a8bc1a999fd51c554a017d095d8c3e3127daf9f595
|
||||
MAC = c85d15ed44c378d6b00e23064c7bcd51
|
||||
|
||||
# Regression tests for https://rt.openssl.org/Ticket/Display.html?id=4483
|
||||
|
||||
Key = 7f1b02640000000000000000000000000000000000000000cccccccccccccccc
|
||||
Input = cccccccccccccccccccccccccccccccccccccccccccccccccc80ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccceccccccccccccccccccccccccccccccccccccc5cccccccccccccccccccccccccccccccccccccccccce3ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaccccccccccccccccccccce6cccccccccc000000afccccccccccccccccccfffffff5000000000000000000000000000000000000000000000000000000ffffffe70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000719205a8521dfc
|
||||
MAC = 8559b876eceed66eb37798c0457baff9
|
||||
|
||||
Key = e00016000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
Input = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa000000000000000000800264
|
||||
MAC = 00bd1258978e205444c9aaaa82006fed
|
||||
|
||||
Key = 0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
|
||||
Input = 02fc
|
||||
MAC = 06120c0c0c0c0c0c0c0c0c0c0c0c0c0c
|
||||
|
||||
Key = 00ff000000000000000000000000000000000000001e00000000000000007b7b
|
||||
Input = 7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7a7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b5c7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b6e7b007b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7a7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b5c7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b6e7b001300000000b300000000000000000000000000000000000000000000f20000000000000000000000000000000000002000efff0009000000000000000000000000100000000009000000640000000000000000000000001300000000b300000000000000000000000000000000000000000000f20000000000000000000000000000000000002000efff00090000000000000000007a000010000000000900000064000000000000000000000000000000000000000000000000fc
|
||||
MAC = 33205bbf9e9f8f7212ab9e2ab9b7e4a5
|
||||
Vendored
+187
@@ -0,0 +1,187 @@
|
||||
// Copyright 2018 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! QUIC Header Protection.
|
||||
//!
|
||||
//! See draft-ietf-quic-tls.
|
||||
|
||||
use crate::{
|
||||
aead::{aes, chacha},
|
||||
cpu, error, hkdf,
|
||||
};
|
||||
|
||||
/// A key for generating QUIC Header Protection masks.
|
||||
pub struct HeaderProtectionKey {
|
||||
inner: KeyInner,
|
||||
algorithm: &'static Algorithm,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant, variant_size_differences)]
|
||||
enum KeyInner {
|
||||
Aes(aes::Key),
|
||||
ChaCha20(chacha::Key),
|
||||
}
|
||||
|
||||
impl From<hkdf::Okm<'_, &'static Algorithm>> for HeaderProtectionKey {
|
||||
fn from(okm: hkdf::Okm<&'static Algorithm>) -> Self {
|
||||
let mut key_bytes = [0; super::MAX_KEY_LEN];
|
||||
let algorithm = *okm.len();
|
||||
let key_bytes = &mut key_bytes[..algorithm.key_len()];
|
||||
okm.fill(key_bytes).unwrap();
|
||||
Self::new(algorithm, key_bytes).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl HeaderProtectionKey {
|
||||
/// Create a new header protection key.
|
||||
///
|
||||
/// `key_bytes` must be exactly `algorithm.key_len` bytes long.
|
||||
pub fn new(
|
||||
algorithm: &'static Algorithm,
|
||||
key_bytes: &[u8],
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
Ok(Self {
|
||||
inner: (algorithm.init)(key_bytes, cpu::features())?,
|
||||
algorithm,
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate a new QUIC Header Protection mask.
|
||||
///
|
||||
/// `sample` must be exactly `self.algorithm().sample_len()` bytes long.
|
||||
pub fn new_mask(&self, sample: &[u8]) -> Result<[u8; 5], error::Unspecified> {
|
||||
let sample = <&[u8; SAMPLE_LEN]>::try_from(sample)?;
|
||||
|
||||
let out = (self.algorithm.new_mask)(&self.inner, *sample);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The key's algorithm.
|
||||
#[inline(always)]
|
||||
pub fn algorithm(&self) -> &'static Algorithm {
|
||||
self.algorithm
|
||||
}
|
||||
}
|
||||
|
||||
const SAMPLE_LEN: usize = super::TAG_LEN;
|
||||
|
||||
/// QUIC sample for new key masks
|
||||
pub type Sample = [u8; SAMPLE_LEN];
|
||||
|
||||
/// A QUIC Header Protection Algorithm.
|
||||
pub struct Algorithm {
|
||||
init: fn(key: &[u8], cpu_features: cpu::Features) -> Result<KeyInner, error::Unspecified>,
|
||||
|
||||
new_mask: fn(key: &KeyInner, sample: Sample) -> [u8; 5],
|
||||
|
||||
key_len: usize,
|
||||
id: AlgorithmID,
|
||||
}
|
||||
|
||||
impl hkdf::KeyType for &'static Algorithm {
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
self.key_len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Algorithm {
|
||||
/// The length of the key.
|
||||
#[inline(always)]
|
||||
pub fn key_len(&self) -> usize {
|
||||
self.key_len
|
||||
}
|
||||
|
||||
/// The required sample length.
|
||||
#[inline(always)]
|
||||
pub fn sample_len(&self) -> usize {
|
||||
SAMPLE_LEN
|
||||
}
|
||||
}
|
||||
|
||||
derive_debug_via_id!(Algorithm);
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum AlgorithmID {
|
||||
AES_128,
|
||||
AES_256,
|
||||
CHACHA20,
|
||||
}
|
||||
|
||||
impl PartialEq for Algorithm {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Algorithm {}
|
||||
|
||||
/// AES-128.
|
||||
pub static AES_128: Algorithm = Algorithm {
|
||||
key_len: 16,
|
||||
init: aes_init_128,
|
||||
new_mask: aes_new_mask,
|
||||
id: AlgorithmID::AES_128,
|
||||
};
|
||||
|
||||
/// AES-256.
|
||||
pub static AES_256: Algorithm = Algorithm {
|
||||
key_len: 32,
|
||||
init: aes_init_256,
|
||||
new_mask: aes_new_mask,
|
||||
id: AlgorithmID::AES_256,
|
||||
};
|
||||
|
||||
fn aes_init_128(key: &[u8], cpu_features: cpu::Features) -> Result<KeyInner, error::Unspecified> {
|
||||
let key = key.try_into().map_err(|_| error::Unspecified)?;
|
||||
let aes_key = aes::Key::new(aes::KeyBytes::AES_128(key), cpu_features)?;
|
||||
Ok(KeyInner::Aes(aes_key))
|
||||
}
|
||||
|
||||
fn aes_init_256(key: &[u8], cpu_features: cpu::Features) -> Result<KeyInner, error::Unspecified> {
|
||||
let key = key.try_into().map_err(|_| error::Unspecified)?;
|
||||
let aes_key = aes::Key::new(aes::KeyBytes::AES_256(key), cpu_features)?;
|
||||
Ok(KeyInner::Aes(aes_key))
|
||||
}
|
||||
|
||||
fn aes_new_mask(key: &KeyInner, sample: Sample) -> [u8; 5] {
|
||||
let aes_key = match key {
|
||||
KeyInner::Aes(key) => key,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
aes_key.new_mask(sample)
|
||||
}
|
||||
|
||||
/// ChaCha20.
|
||||
pub static CHACHA20: Algorithm = Algorithm {
|
||||
key_len: chacha::KEY_LEN,
|
||||
init: chacha20_init,
|
||||
new_mask: chacha20_new_mask,
|
||||
id: AlgorithmID::CHACHA20,
|
||||
};
|
||||
|
||||
fn chacha20_init(key: &[u8], _cpu_features: cpu::Features) -> Result<KeyInner, error::Unspecified> {
|
||||
let chacha20_key: [u8; chacha::KEY_LEN] = key.try_into()?;
|
||||
Ok(KeyInner::ChaCha20(chacha::Key::new(chacha20_key)))
|
||||
}
|
||||
|
||||
fn chacha20_new_mask(key: &KeyInner, sample: Sample) -> [u8; 5] {
|
||||
let chacha20_key = match key {
|
||||
KeyInner::ChaCha20(key) => key,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
chacha20_key.new_mask(sample)
|
||||
}
|
||||
Vendored
+104
@@ -0,0 +1,104 @@
|
||||
// Copyright 2015-2021 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! Authenticated Encryption with Associated Data (AEAD).
|
||||
//!
|
||||
//! See [Authenticated encryption: relations among notions and analysis of the
|
||||
//! generic composition paradigm][AEAD] for an introduction to the concept of
|
||||
//! AEADs.
|
||||
//!
|
||||
//! [AEAD]: https://eprint.iacr.org/2000/025.pdf
|
||||
//! [`crypto.cipher.AEAD`]: https://golang.org/pkg/crypto/cipher/#AEAD
|
||||
|
||||
use super::{Aad, Algorithm, BoundKey, LessSafeKey, NonceSequence, Tag, UnboundKey};
|
||||
use crate::error;
|
||||
|
||||
/// An AEAD key for encrypting and signing ("sealing"), bound to a nonce
|
||||
/// sequence.
|
||||
///
|
||||
/// Intentionally not `Clone` or `Copy` since cloning would allow duplication
|
||||
/// of the nonce sequence.
|
||||
pub struct SealingKey<N: NonceSequence> {
|
||||
key: LessSafeKey,
|
||||
nonce_sequence: N,
|
||||
}
|
||||
|
||||
impl<N: NonceSequence> BoundKey<N> for SealingKey<N> {
|
||||
fn new(key: UnboundKey, nonce_sequence: N) -> Self {
|
||||
Self {
|
||||
key: key.into_inner(),
|
||||
nonce_sequence,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn algorithm(&self) -> &'static Algorithm {
|
||||
self.key.algorithm()
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: NonceSequence> core::fmt::Debug for SealingKey<N> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
self.key.fmt_debug("SealingKey", f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: NonceSequence> SealingKey<N> {
|
||||
/// Encrypts and signs (“seals”) data in place, appending the tag to the
|
||||
/// resulting ciphertext.
|
||||
///
|
||||
/// `key.seal_in_place_append_tag(aad, in_out)` is equivalent to:
|
||||
///
|
||||
/// ```skip
|
||||
/// key.seal_in_place_separate_tag(aad, in_out.as_mut())
|
||||
/// .map(|tag| in_out.extend(tag.as_ref()))
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn seal_in_place_append_tag<A, InOut>(
|
||||
&mut self,
|
||||
aad: Aad<A>,
|
||||
in_out: &mut InOut,
|
||||
) -> Result<(), error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
|
||||
{
|
||||
self.key
|
||||
.seal_in_place_append_tag(self.nonce_sequence.advance()?, aad, in_out)
|
||||
}
|
||||
|
||||
/// Encrypts and signs (“seals”) data in place.
|
||||
///
|
||||
/// `aad` is the additional authenticated data (AAD), if any. This is
|
||||
/// authenticated but not encrypted. The type `A` could be a byte slice
|
||||
/// `&[u8]`, a byte array `[u8; N]` for some constant `N`, `Vec<u8>`, etc.
|
||||
/// If there is no AAD then use `Aad::empty()`.
|
||||
///
|
||||
/// The plaintext is given as the input value of `in_out`. `seal_in_place()`
|
||||
/// will overwrite the plaintext with the ciphertext and return the tag.
|
||||
/// For most protocols, the caller must append the tag to the ciphertext.
|
||||
/// The tag will be `self.algorithm.tag_len()` bytes long.
|
||||
#[inline]
|
||||
pub fn seal_in_place_separate_tag<A>(
|
||||
&mut self,
|
||||
aad: Aad<A>,
|
||||
in_out: &mut [u8],
|
||||
) -> Result<Tag, error::Unspecified>
|
||||
where
|
||||
A: AsRef<[u8]>,
|
||||
{
|
||||
self.key
|
||||
.seal_in_place_separate_tag(self.nonce_sequence.advance()?, aad, in_out)
|
||||
}
|
||||
}
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
// Copyright 2018 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
pub fn shift_full_blocks<const BLOCK_LEN: usize>(
|
||||
in_out: super::overlapping::Overlapping<'_, u8>,
|
||||
mut transform: impl FnMut(&[u8; BLOCK_LEN]) -> [u8; BLOCK_LEN],
|
||||
) {
|
||||
let (in_out, src) = in_out.into_slice_src_mut();
|
||||
let in_out_len = in_out[src.clone()].len();
|
||||
|
||||
for i in (0..in_out_len).step_by(BLOCK_LEN) {
|
||||
let block = {
|
||||
let input =
|
||||
<&[u8; BLOCK_LEN]>::try_from(&in_out[(src.start + i)..][..BLOCK_LEN]).unwrap();
|
||||
transform(input)
|
||||
};
|
||||
let output = <&mut [u8; BLOCK_LEN]>::try_from(&mut in_out[i..][..BLOCK_LEN]).unwrap();
|
||||
*output = block;
|
||||
}
|
||||
}
|
||||
Vendored
+74
@@ -0,0 +1,74 @@
|
||||
// Copyright 2015-2021 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! Authenticated Encryption with Associated Data (AEAD).
|
||||
//!
|
||||
//! See [Authenticated encryption: relations among notions and analysis of the
|
||||
//! generic composition paradigm][AEAD] for an introduction to the concept of
|
||||
//! AEADs.
|
||||
//!
|
||||
//! [AEAD]: https://eprint.iacr.org/2000/025.pdf
|
||||
//! [`crypto.cipher.AEAD`]: https://golang.org/pkg/crypto/cipher/#AEAD
|
||||
|
||||
use super::{Algorithm, LessSafeKey, MAX_KEY_LEN};
|
||||
use crate::{cpu, error, hkdf};
|
||||
|
||||
/// An AEAD key without a designated role or nonce sequence.
|
||||
pub struct UnboundKey {
|
||||
inner: LessSafeKey,
|
||||
}
|
||||
|
||||
impl UnboundKey {
|
||||
/// Constructs a `UnboundKey`.
|
||||
///
|
||||
/// Fails if `key_bytes.len() != algorithm.key_len()`.
|
||||
#[inline]
|
||||
pub fn new(
|
||||
algorithm: &'static Algorithm,
|
||||
key_bytes: &[u8],
|
||||
) -> Result<Self, error::Unspecified> {
|
||||
Ok(Self {
|
||||
inner: LessSafeKey::new_(algorithm, key_bytes, cpu::features())?,
|
||||
})
|
||||
}
|
||||
|
||||
/// The key's AEAD algorithm.
|
||||
#[inline]
|
||||
pub fn algorithm(&self) -> &'static Algorithm {
|
||||
self.inner.algorithm()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn into_inner(self) -> LessSafeKey {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for UnboundKey {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
self.inner.fmt_debug("UnboundKey", f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<hkdf::Okm<'_, &'static Algorithm>> for UnboundKey {
|
||||
fn from(okm: hkdf::Okm<&'static Algorithm>) -> Self {
|
||||
let mut key_bytes = [0; MAX_KEY_LEN];
|
||||
let key_bytes = &mut key_bytes[..okm.len().key_len()];
|
||||
let algorithm = *okm.len();
|
||||
okm.fill(key_bytes).unwrap();
|
||||
Self {
|
||||
inner: LessSafeKey::new_(algorithm, key_bytes, cpu::features()).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user