cryprot_core/
lib.rs

1#![cfg_attr(feature = "nightly", feature(test))]
2//! Core utilites for cryptographic protocols.
3//!
4//! This crate implements several core utilities for cryptographic protocols.
5//! The most important type is the 128-bit [`Block`]. As we generally use a
6//! security parameter of 128 bits, this type is a convenient way of storing
7//! security parameter many bits.
8
9pub mod aes_hash;
10pub mod aes_rng;
11pub mod alloc;
12pub mod block;
13pub mod buf;
14pub mod rand_compat;
15pub mod random_oracle;
16#[cfg(feature = "tokio-rayon")]
17pub mod tokio_rayon;
18pub mod transpose;
19pub mod utils;
20
21pub use block::Block;
22
23/// Number of Blocks for which hardware accelerated AES can make use of ILP.
24///
25/// This corresponds to `ParBlocksSize` in [`aes::cipher::ParBlocksSizeUser`]
26/// for the SIMD backend on the target architecture. This means, that this
27/// constant depends on the target architecture and is different on `x86_64` and
28/// `aarch64`.
29/// Do not depend on the value of the constant.
30// https://github.com/RustCrypto/block-ciphers/blob/4da9b802de52a3326fdc74d559caddd57042fed2/aes/src/ni.rs#L43
31#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
32pub const AES_PAR_BLOCKS: usize = 9;
33#[cfg(target_arch = "aarch64")]
34// https://github.com/RustCrypto/block-ciphers/blob/4da9b802de52a3326fdc74d559caddd57042fed2/aes/src/armv8.rs#L32
35pub const AES_PAR_BLOCKS: usize = 21;
36#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
37// TODO what should the fallback be?
38pub const AES_PAR_BLOCKS: usize = 4;
39
40#[cfg(all(test, not(miri), target_feature = "aes"))]
41mod tests {
42    use aes::{
43        Aes128,
44        cipher::{
45            BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, KeyInit, ParBlocksSizeUser,
46        },
47    };
48
49    use crate::AES_PAR_BLOCKS;
50
51    #[test]
52    fn aes_par_block_size() {
53        use hybrid_array::typenum::Unsigned;
54
55        struct GetParBlockSize;
56        impl BlockSizeUser for GetParBlockSize {
57            type BlockSize = aes::cipher::array::sizes::U16;
58        }
59        impl BlockCipherEncClosure for GetParBlockSize {
60            fn call<B: aes::cipher::BlockCipherEncBackend<BlockSize = Self::BlockSize>>(
61                self,
62                _backend: &B,
63            ) {
64                assert_eq!(
65                    AES_PAR_BLOCKS,
66                    // size_of ArrayType<u8> is equal to its length
67                    <<B as ParBlocksSizeUser>::ParBlocksSize as Unsigned>::USIZE,
68                );
69            }
70        }
71        let aes = Aes128::new(&Default::default());
72        aes.encrypt_with_backend(GetParBlockSize);
73    }
74}