ring/aead/aes/ffi.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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);
});
}
}