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
pub use cipher::{BlockCipher, NewBlockCipher};

use cipher::{
    consts::{U11, U13, U15, U16, U24, U32, U8},
    generic_array::GenericArray,
};

use crate::{
    bitslice::{
        bit_slice_1x128_with_u32x4, bit_slice_1x16_with_u16, bit_slice_4x4_with_u16,
        bit_slice_fill_4x4_with_u32x4, decrypt_core, un_bit_slice_1x128_with_u32x4,
        un_bit_slice_1x16_with_u16, Bs8State,
    },
    consts::U32X4_0,
    expand::expand_key,
    fixslice::{self, FixsliceKeys128, FixsliceKeys192, FixsliceKeys256},
    simd::u32x4,
    Block, ParBlocks,
};

macro_rules! define_aes_impl {
    (
        $name:ident,
        $key_size:ty,
        $rounds:expr,
        $rounds2:ty,
        $fixslice_keys:ty,
        $fixslice_key_schedule:path,
        $fixslice_encrypt:path,
        $doc:expr
    ) => {
        #[doc=$doc]
        #[derive(Clone)]
        pub struct $name {
            enc_keys: $fixslice_keys,
            dec_keys: [Bs8State<u16>; $rounds],
            dec_keys8: [Bs8State<u32x4>; $rounds],
        }

        impl NewBlockCipher for $name {
            type KeySize = $key_size;

            #[inline]
            fn new(key: &GenericArray<u8, $key_size>) -> Self {
                let dk = expand_key::<$key_size, $rounds2>(key).1;
                let k8 = Bs8State(
                    U32X4_0, U32X4_0, U32X4_0, U32X4_0,
                    U32X4_0, U32X4_0, U32X4_0, U32X4_0
                );
                let mut c =  Self {
                    enc_keys: $fixslice_key_schedule(key),
                    dec_keys: [Bs8State(0, 0, 0, 0, 0, 0, 0, 0); $rounds],
                    dec_keys8: [k8; $rounds],
                };
                for i in 0..$rounds {
                    c.dec_keys[i] = bit_slice_4x4_with_u16(
                        dk[i][0], dk[i][1], dk[i][2], dk[i][3],
                    );
                    c.dec_keys8[i] = bit_slice_fill_4x4_with_u32x4(
                        dk[i][0], dk[i][1], dk[i][2], dk[i][3],
                    );
                }
                c
            }
        }

        impl BlockCipher for $name {
            type BlockSize = U16;
            type ParBlocks = U8;

            #[inline]
            fn encrypt_block(&self, block: &mut Block) {
	            let mut blocks = [Block::default(); 2];
                blocks[0].copy_from_slice(block);
                $fixslice_encrypt(&self.enc_keys, &mut blocks);
                block.copy_from_slice(&blocks[0]);
            }

            #[inline]
            fn decrypt_block(&self, block: &mut Block) {
                let mut bs = bit_slice_1x16_with_u16(block);
                bs = decrypt_core(&bs, &self.dec_keys);
                un_bit_slice_1x16_with_u16(&bs, block);
            }

            #[inline]
            fn encrypt_blocks(&self, blocks: &mut ParBlocks) {
                for chunk in blocks.chunks_mut(2) {
                    $fixslice_encrypt(&self.enc_keys, chunk);
                }
            }

            #[inline]
            fn decrypt_blocks(&self, blocks: &mut ParBlocks) {
                #[allow(unsafe_code)]
                let blocks: &mut [u8; 16*8] = unsafe {
                    &mut *(blocks as *mut _ as *mut [u8; 128])
                };
                let bs = bit_slice_1x128_with_u32x4(blocks);
                let bs2 = decrypt_core(&bs, &self.dec_keys8);
                un_bit_slice_1x128_with_u32x4(bs2, blocks);
            }
        }

        opaque_debug::implement!($name);
    }
}

define_aes_impl!(
    Aes128,
    U16,
    11,
    U11,
    FixsliceKeys128,
    fixslice::aes128_key_schedule,
    fixslice::aes128_encrypt,
    "AES-128 block cipher instance"
);

define_aes_impl!(
    Aes192,
    U24,
    13,
    U13,
    FixsliceKeys192,
    fixslice::aes192_key_schedule,
    fixslice::aes192_encrypt,
    "AES-192 block cipher instance"
);

define_aes_impl!(
    Aes256,
    U32,
    15,
    U15,
    FixsliceKeys256,
    fixslice::aes256_key_schedule,
    fixslice::aes256_encrypt,
    "AES-256 block cipher instance"
);