Use const fn for aead::max_input_len.

This commit is contained in:
Brian Smith 2018-12-06 12:29:23 -10:00
parent 66a2711601
commit 11d12d037a
4 changed files with 8 additions and 16 deletions

View File

@ -12,7 +12,7 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use super::{Block, Tag};
use super::{Block, BLOCK_LEN, Tag};
use crate::{aead, bssl, c, error};
#[repr(align(16))]
@ -108,9 +108,7 @@ const AES_KEY_CTX_BUF_LEN: usize = AES_KEY_BUF_LEN + GCM128_SERIALIZED_LEN;
// Keep this in sync with `AES_KEY` in aes.h.
const AES_KEY_BUF_LEN: usize = (4 * 4 * (AES_MAX_ROUNDS + 1)) + 8;
const AES_BLOCK_LEN: u64 = 16;
const AES_GCM_OVERHEAD_BLOCKS_PER_NONCE: u64 = 2;
const AES_GCM_MAX_INPUT_LEN: u64 = max_input_len!(AES_BLOCK_LEN, AES_GCM_OVERHEAD_BLOCKS_PER_NONCE);
const AES_GCM_MAX_INPUT_LEN: u64 = super::max_input_len(BLOCK_LEN, 2);
// Keep this in sync with `AES_MAXNR` in aes.h.
const AES_MAX_ROUNDS: usize = 14;

View File

@ -29,12 +29,9 @@ pub static CHACHA20_POLY1305: aead::Algorithm = aead::Algorithm {
seal: chacha20_poly1305_seal,
open: chacha20_poly1305_open,
id: aead::AlgorithmID::CHACHA20_POLY1305,
max_input_len: max_input_len!(CHACHA20_BLOCK_LEN, CHACHA20_OVERHEAD_BLOCKS_PER_NONCE),
max_input_len: super::max_input_len(64, 1),
};
const CHACHA20_BLOCK_LEN: u64 = 64;
const CHACHA20_OVERHEAD_BLOCKS_PER_NONCE: u64 = 1;
/// Copies |key| into |ctx_buf|.
fn chacha20_poly1305_init(key: &[u8]) -> Result<aead::KeyInner, error::Unspecified> {
Ok(aead::KeyInner::ChaCha20Poly1305(chacha::Key::from(

View File

@ -286,13 +286,10 @@ pub struct Algorithm {
max_input_len: u64,
}
/// TODO: Make this a `const fn` when those become stable.
macro_rules! max_input_len {
($block_len:expr, $overhead_blocks_per_nonce:expr) => {
// Each of our AEADs use a 32-bit block counter so the maximum is the
// largest input that will not overflow the counter.
(((1u64 << 32) - $overhead_blocks_per_nonce) * $block_len)
};
const fn max_input_len(block_len: usize, overhead_blocks_per_nonce: usize) -> u64 {
// Each of our AEADs use a 32-bit block counter so the maximum is the
// largest input that will not overflow the counter.
((1u64 << 32) - polyfill::u64_from_usize(overhead_blocks_per_nonce)) * polyfill::u64_from_usize(block_len)
}
impl Algorithm {

View File

@ -18,7 +18,7 @@
use core;
#[inline(always)]
pub fn u64_from_usize(x: usize) -> u64 { x as u64 }
pub const fn u64_from_usize(x: usize) -> u64 { x as u64 }
/// `core::num::Wrapping` doesn't support `rotate_left`.
/// There is no usable trait for `rotate_left`, so this polyfill just