fixslice32.rs

cdk-rsx/crates/crypto/aes/vendor/soft/fixslice32.rs

fixslice32.rs1,479 lines← SDK documentation
1//! Fixsliced implementations of AES-128, AES-192 and AES-256 (32-bit)2//! adapted from the C implementation3//!4//! All implementations are fully bitsliced and do not rely on any5//! Look-Up Table (LUT).6//!7//! See the paper at <https://eprint.iacr.org/2020/1123.pdf> for more details.8//!9//! # Author (original C code)10//!11//! Alexandre Adomnicai, Nanyang Technological University, Singapore12//! <alexandre.adomnicai@ntu.edu.sg>13//!14//! Originally licensed MIT. Relicensed as Apache 2.0+MIT with permission.1516#![allow(clippy::unreadable_literal)]1718use crate::Block;19use cipher::{consts::U2, generic_array::GenericArray};2021/// AES block batch size for this implementation22pub(crate) type FixsliceBlocks = U2;2324pub(crate) type BatchBlocks = GenericArray<Block, FixsliceBlocks>;2526/// AES-128 round keys27pub(crate) type FixsliceKeys128 = [u32; 88];2829/// AES-192 round keys30pub(crate) type FixsliceKeys192 = [u32; 104];3132/// AES-256 round keys33pub(crate) type FixsliceKeys256 = [u32; 120];3435/// 256-bit internal state36pub(crate) type State = [u32; 8];3738/// Fully bitsliced AES-128 key schedule to match the fully-fixsliced representation.39pub(crate) fn aes128_key_schedule(key: &[u8; 16]) -> FixsliceKeys128 {40    let mut rkeys = [0u32; 88];4142    bitslice(&mut rkeys[..8], key, key);4344    let mut rk_off = 0;45    for rcon in 0..10 {46        memshift32(&mut rkeys, rk_off);47        rk_off += 8;4849        sub_bytes(&mut rkeys[rk_off..(rk_off + 8)]);50        sub_bytes_nots(&mut rkeys[rk_off..(rk_off + 8)]);5152        if rcon < 8 {53            add_round_constant_bit(&mut rkeys[rk_off..(rk_off + 8)], rcon);54        } else {55            add_round_constant_bit(&mut rkeys[rk_off..(rk_off + 8)], rcon - 8);56            add_round_constant_bit(&mut rkeys[rk_off..(rk_off + 8)], rcon - 7);57            add_round_constant_bit(&mut rkeys[rk_off..(rk_off + 8)], rcon - 5);58            add_round_constant_bit(&mut rkeys[rk_off..(rk_off + 8)], rcon - 4);59        }6061        xor_columns(&mut rkeys, rk_off, 8, ror_distance(1, 3));62    }6364    // Adjust to match fixslicing format65    #[cfg(aes_compact)]66    {67        for i in (8..88).step_by(16) {68            inv_shift_rows_1(&mut rkeys[i..(i + 8)]);69        }70    }71    #[cfg(not(aes_compact))]72    {73        for i in (8..72).step_by(32) {74            inv_shift_rows_1(&mut rkeys[i..(i + 8)]);75            inv_shift_rows_2(&mut rkeys[(i + 8)..(i + 16)]);76            inv_shift_rows_3(&mut rkeys[(i + 16)..(i + 24)]);77        }78        inv_shift_rows_1(&mut rkeys[72..80]);79    }8081    // Account for NOTs removed from sub_bytes82    for i in 1..11 {83        sub_bytes_nots(&mut rkeys[(i * 8)..(i * 8 + 8)]);84    }8586    rkeys87}8889/// Fully bitsliced AES-192 key schedule to match the fully-fixsliced representation.90pub(crate) fn aes192_key_schedule(key: &[u8; 24]) -> FixsliceKeys192 {91    let mut rkeys = [0u32; 104];92    let mut tmp = [0u32; 8];9394    bitslice(&mut rkeys[..8], &key[..16], &key[..16]);95    bitslice(&mut tmp, &key[8..], &key[8..]);9697    let mut rcon = 0;98    let mut rk_off = 8;99100    loop {101        for i in 0..8 {102            rkeys[rk_off + i] =103                (0x0f0f0f0f & (tmp[i] >> 4)) | (0xf0f0f0f0 & (rkeys[(rk_off - 8) + i] << 4));104        }105106        sub_bytes(&mut tmp);107        sub_bytes_nots(&mut tmp);108109        add_round_constant_bit(&mut tmp, rcon);110        rcon += 1;111112        for i in 0..8 {113            let mut ti = rkeys[rk_off + i];114            ti ^= 0x30303030 & ror(tmp[i], ror_distance(1, 1));115            ti ^= 0xc0c0c0c0 & (ti << 2);116            tmp[i] = ti;117        }118        rkeys[rk_off..(rk_off + 8)].copy_from_slice(&tmp);119        rk_off += 8;120121        for i in 0..8 {122            let ui = tmp[i];123            let mut ti = (0x0f0f0f0f & (rkeys[(rk_off - 16) + i] >> 4)) | (0xf0f0f0f0 & (ui << 4));124            ti ^= 0x03030303 & (ui >> 6);125            tmp[i] =126                ti ^ (0xfcfcfcfc & (ti << 2)) ^ (0xf0f0f0f0 & (ti << 4)) ^ (0xc0c0c0c0 & (ti << 6));127        }128        rkeys[rk_off..(rk_off + 8)].copy_from_slice(&tmp);129        rk_off += 8;130131        sub_bytes(&mut tmp);132        sub_bytes_nots(&mut tmp);133134        add_round_constant_bit(&mut tmp, rcon);135        rcon += 1;136137        for i in 0..8 {138            let mut ti = (0x0f0f0f0f & (rkeys[(rk_off - 16) + i] >> 4))139                | (0xf0f0f0f0 & (rkeys[(rk_off - 8) + i] << 4));140            ti ^= 0x03030303 & ror(tmp[i], ror_distance(1, 3));141            rkeys[rk_off + i] =142                ti ^ (0xfcfcfcfc & (ti << 2)) ^ (0xf0f0f0f0 & (ti << 4)) ^ (0xc0c0c0c0 & (ti << 6));143        }144        rk_off += 8;145146        if rcon >= 8 {147            break;148        }149150        for i in 0..8 {151            let ui = rkeys[(rk_off - 8) + i];152            let mut ti = rkeys[(rk_off - 16) + i];153            ti ^= 0x30303030 & (ui >> 2);154            ti ^= 0xc0c0c0c0 & (ti << 2);155            tmp[i] = ti;156        }157    }158159    // Adjust to match fixslicing format160    #[cfg(aes_compact)]161    {162        for i in (8..104).step_by(16) {163            inv_shift_rows_1(&mut rkeys[i..(i + 8)]);164        }165    }166    #[cfg(not(aes_compact))]167    {168        for i in (0..96).step_by(32) {169            inv_shift_rows_1(&mut rkeys[(i + 8)..(i + 16)]);170            inv_shift_rows_2(&mut rkeys[(i + 16)..(i + 24)]);171            inv_shift_rows_3(&mut rkeys[(i + 24)..(i + 32)]);172        }173    }174175    // Account for NOTs removed from sub_bytes176    for i in 1..13 {177        sub_bytes_nots(&mut rkeys[(i * 8)..(i * 8 + 8)]);178    }179180    rkeys181}182183/// Fully bitsliced AES-256 key schedule to match the fully-fixsliced representation.184pub(crate) fn aes256_key_schedule(key: &[u8; 32]) -> FixsliceKeys256 {185    let mut rkeys = [0u32; 120];186187    bitslice(&mut rkeys[..8], &key[..16], &key[..16]);188    bitslice(&mut rkeys[8..16], &key[16..], &key[16..]);189190    let mut rk_off = 8;191192    let mut rcon = 0;193    loop {194        memshift32(&mut rkeys, rk_off);195        rk_off += 8;196197        sub_bytes(&mut rkeys[rk_off..(rk_off + 8)]);198        sub_bytes_nots(&mut rkeys[rk_off..(rk_off + 8)]);199200        add_round_constant_bit(&mut rkeys[rk_off..(rk_off + 8)], rcon);201        xor_columns(&mut rkeys, rk_off, 16, ror_distance(1, 3));202        rcon += 1;203204        if rcon == 7 {205            break;206        }207208        memshift32(&mut rkeys, rk_off);209        rk_off += 8;210211        sub_bytes(&mut rkeys[rk_off..(rk_off + 8)]);212        sub_bytes_nots(&mut rkeys[rk_off..(rk_off + 8)]);213214        xor_columns(&mut rkeys, rk_off, 16, ror_distance(0, 3));215    }216217    // Adjust to match fixslicing format218    #[cfg(aes_compact)]219    {220        for i in (8..120).step_by(16) {221            inv_shift_rows_1(&mut rkeys[i..(i + 8)]);222        }223    }224    #[cfg(not(aes_compact))]225    {226        for i in (8..104).step_by(32) {227            inv_shift_rows_1(&mut rkeys[i..(i + 8)]);228            inv_shift_rows_2(&mut rkeys[(i + 8)..(i + 16)]);229            inv_shift_rows_3(&mut rkeys[(i + 16)..(i + 24)]);230        }231        inv_shift_rows_1(&mut rkeys[104..112]);232    }233234    // Account for NOTs removed from sub_bytes235    for i in 1..15 {236        sub_bytes_nots(&mut rkeys[(i * 8)..(i * 8 + 8)]);237    }238239    rkeys240}241242/// Fully-fixsliced AES-128 decryption (the InvShiftRows is completely omitted).243///244/// Decrypts four blocks in-place and in parallel.245pub(crate) fn aes128_decrypt(rkeys: &FixsliceKeys128, blocks: &BatchBlocks) -> BatchBlocks {246    let mut state = State::default();247248    bitslice(&mut state, &blocks[0], &blocks[1]);249250    add_round_key(&mut state, &rkeys[80..]);251    inv_sub_bytes(&mut state);252253    #[cfg(not(aes_compact))]254    {255        inv_shift_rows_2(&mut state);256    }257258    let mut rk_off = 72;259    loop {260        #[cfg(aes_compact)]261        {262            inv_shift_rows_2(&mut state);263        }264265        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);266        inv_mix_columns_1(&mut state);267        inv_sub_bytes(&mut state);268        rk_off -= 8;269270        if rk_off == 0 {271            break;272        }273274        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);275        inv_mix_columns_0(&mut state);276        inv_sub_bytes(&mut state);277        rk_off -= 8;278279        #[cfg(not(aes_compact))]280        {281            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);282            inv_mix_columns_3(&mut state);283            inv_sub_bytes(&mut state);284            rk_off -= 8;285286            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);287            inv_mix_columns_2(&mut state);288            inv_sub_bytes(&mut state);289            rk_off -= 8;290        }291    }292293    add_round_key(&mut state, &rkeys[..8]);294295    inv_bitslice(&state)296}297298/// Fully-fixsliced AES-128 encryption (the ShiftRows is completely omitted).299///300/// Encrypts four blocks in-place and in parallel.301pub(crate) fn aes128_encrypt(rkeys: &FixsliceKeys128, blocks: &BatchBlocks) -> BatchBlocks {302    let mut state = State::default();303304    bitslice(&mut state, &blocks[0], &blocks[1]);305306    add_round_key(&mut state, &rkeys[..8]);307308    let mut rk_off = 8;309    loop {310        sub_bytes(&mut state);311        mix_columns_1(&mut state);312        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);313        rk_off += 8;314315        #[cfg(aes_compact)]316        {317            shift_rows_2(&mut state);318        }319320        if rk_off == 80 {321            break;322        }323324        #[cfg(not(aes_compact))]325        {326            sub_bytes(&mut state);327            mix_columns_2(&mut state);328            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);329            rk_off += 8;330331            sub_bytes(&mut state);332            mix_columns_3(&mut state);333            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);334            rk_off += 8;335        }336337        sub_bytes(&mut state);338        mix_columns_0(&mut state);339        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);340        rk_off += 8;341    }342343    #[cfg(not(aes_compact))]344    {345        shift_rows_2(&mut state);346    }347348    sub_bytes(&mut state);349    add_round_key(&mut state, &rkeys[80..]);350351    inv_bitslice(&state)352}353354/// Fully-fixsliced AES-192 decryption (the InvShiftRows is completely omitted).355///356/// Decrypts four blocks in-place and in parallel.357pub(crate) fn aes192_decrypt(rkeys: &FixsliceKeys192, blocks: &BatchBlocks) -> BatchBlocks {358    let mut state = State::default();359360    bitslice(&mut state, &blocks[0], &blocks[1]);361362    add_round_key(&mut state, &rkeys[96..]);363    inv_sub_bytes(&mut state);364365    let mut rk_off = 88;366    loop {367        #[cfg(aes_compact)]368        {369            inv_shift_rows_2(&mut state);370        }371        #[cfg(not(aes_compact))]372        {373            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);374            inv_mix_columns_3(&mut state);375            inv_sub_bytes(&mut state);376            rk_off -= 8;377378            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);379            inv_mix_columns_2(&mut state);380            inv_sub_bytes(&mut state);381            rk_off -= 8;382        }383384        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);385        inv_mix_columns_1(&mut state);386        inv_sub_bytes(&mut state);387        rk_off -= 8;388389        if rk_off == 0 {390            break;391        }392393        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);394        inv_mix_columns_0(&mut state);395        inv_sub_bytes(&mut state);396        rk_off -= 8;397    }398399    add_round_key(&mut state, &rkeys[..8]);400401    inv_bitslice(&state)402}403404/// Fully-fixsliced AES-192 encryption (the ShiftRows is completely omitted).405///406/// Encrypts four blocks in-place and in parallel.407pub(crate) fn aes192_encrypt(rkeys: &FixsliceKeys192, blocks: &BatchBlocks) -> BatchBlocks {408    let mut state = State::default();409410    bitslice(&mut state, &blocks[0], &blocks[1]);411412    add_round_key(&mut state, &rkeys[..8]);413414    let mut rk_off = 8;415    loop {416        sub_bytes(&mut state);417        mix_columns_1(&mut state);418        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);419        rk_off += 8;420421        #[cfg(aes_compact)]422        {423            shift_rows_2(&mut state);424        }425        #[cfg(not(aes_compact))]426        {427            sub_bytes(&mut state);428            mix_columns_2(&mut state);429            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);430            rk_off += 8;431432            sub_bytes(&mut state);433            mix_columns_3(&mut state);434            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);435            rk_off += 8;436        }437438        if rk_off == 96 {439            break;440        }441442        sub_bytes(&mut state);443        mix_columns_0(&mut state);444        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);445        rk_off += 8;446    }447448    sub_bytes(&mut state);449    add_round_key(&mut state, &rkeys[96..]);450451    inv_bitslice(&state)452}453454/// Fully-fixsliced AES-256 decryption (the InvShiftRows is completely omitted).455///456/// Decrypts four blocks in-place and in parallel.457pub(crate) fn aes256_decrypt(rkeys: &FixsliceKeys256, blocks: &BatchBlocks) -> BatchBlocks {458    let mut state = State::default();459460    bitslice(&mut state, &blocks[0], &blocks[1]);461462    add_round_key(&mut state, &rkeys[112..]);463    inv_sub_bytes(&mut state);464465    #[cfg(not(aes_compact))]466    {467        inv_shift_rows_2(&mut state);468    }469470    let mut rk_off = 104;471    loop {472        #[cfg(aes_compact)]473        {474            inv_shift_rows_2(&mut state);475        }476477        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);478        inv_mix_columns_1(&mut state);479        inv_sub_bytes(&mut state);480        rk_off -= 8;481482        if rk_off == 0 {483            break;484        }485486        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);487        inv_mix_columns_0(&mut state);488        inv_sub_bytes(&mut state);489        rk_off -= 8;490491        #[cfg(not(aes_compact))]492        {493            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);494            inv_mix_columns_3(&mut state);495            inv_sub_bytes(&mut state);496            rk_off -= 8;497498            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);499            inv_mix_columns_2(&mut state);500            inv_sub_bytes(&mut state);501            rk_off -= 8;502        }503    }504505    add_round_key(&mut state, &rkeys[..8]);506507    inv_bitslice(&state)508}509510/// Fully-fixsliced AES-256 encryption (the ShiftRows is completely omitted).511///512/// Encrypts four blocks in-place and in parallel.513pub(crate) fn aes256_encrypt(rkeys: &FixsliceKeys256, blocks: &BatchBlocks) -> BatchBlocks {514    let mut state = State::default();515516    bitslice(&mut state, &blocks[0], &blocks[1]);517518    add_round_key(&mut state, &rkeys[..8]);519520    let mut rk_off = 8;521    loop {522        sub_bytes(&mut state);523        mix_columns_1(&mut state);524        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);525        rk_off += 8;526527        #[cfg(aes_compact)]528        {529            shift_rows_2(&mut state);530        }531532        if rk_off == 112 {533            break;534        }535536        #[cfg(not(aes_compact))]537        {538            sub_bytes(&mut state);539            mix_columns_2(&mut state);540            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);541            rk_off += 8;542543            sub_bytes(&mut state);544            mix_columns_3(&mut state);545            add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);546            rk_off += 8;547        }548549        sub_bytes(&mut state);550        mix_columns_0(&mut state);551        add_round_key(&mut state, &rkeys[rk_off..(rk_off + 8)]);552        rk_off += 8;553    }554555    #[cfg(not(aes_compact))]556    {557        shift_rows_2(&mut state);558    }559560    sub_bytes(&mut state);561    add_round_key(&mut state, &rkeys[112..]);562563    inv_bitslice(&state)564}565566/// Note that the 4 bitwise NOT (^= 0xffffffff) are accounted for here so that it is a true567/// inverse of 'sub_bytes'.568fn inv_sub_bytes(state: &mut [u32]) {569    debug_assert_eq!(state.len(), 8);570571    // Scheduled using https://github.com/Ko-/aes-armcortexm/tree/public/scheduler572    // Inline "stack" comments reflect suggested stores and loads (ARM Cortex-M3 and M4)573574    let u7 = state[0];575    let u6 = state[1];576    let u5 = state[2];577    let u4 = state[3];578    let u3 = state[4];579    let u2 = state[5];580    let u1 = state[6];581    let u0 = state[7];582583    let t23 = u0 ^ u3;584    let t8 = u1 ^ t23;585    let m2 = t23 & t8;586    let t4 = u4 ^ t8;587    let t22 = u1 ^ u3;588    let t2 = u0 ^ u1;589    let t1 = u3 ^ u4;590    // t23 -> stack591    let t9 = u7 ^ t1;592    // t8 -> stack593    let m7 = t22 & t9;594    // t9 -> stack595    let t24 = u4 ^ u7;596    // m7 -> stack597    let t10 = t2 ^ t24;598    // u4 -> stack599    let m14 = t2 & t10;600    let r5 = u6 ^ u7;601    // m2 -> stack602    let t3 = t1 ^ r5;603    // t2 -> stack604    let t13 = t2 ^ r5;605    let t19 = t22 ^ r5;606    // t3 -> stack607    let t17 = u2 ^ t19;608    // t4 -> stack609    let t25 = u2 ^ t1;610    let r13 = u1 ^ u6;611    // t25 -> stack612    let t20 = t24 ^ r13;613    // t17 -> stack614    let m9 = t20 & t17;615    // t20 -> stack616    let r17 = u2 ^ u5;617    // t22 -> stack618    let t6 = t22 ^ r17;619    // t13 -> stack620    let m1 = t13 & t6;621    let y5 = u0 ^ r17;622    let m4 = t19 & y5;623    let m5 = m4 ^ m1;624    let m17 = m5 ^ t24;625    let r18 = u5 ^ u6;626    let t27 = t1 ^ r18;627    let t15 = t10 ^ t27;628    // t6 -> stack629    let m11 = t1 & t15;630    let m15 = m14 ^ m11;631    let m21 = m17 ^ m15;632    // t1 -> stack633    // t4 <- stack634    let m12 = t4 & t27;635    let m13 = m12 ^ m11;636    let t14 = t10 ^ r18;637    let m3 = t14 ^ m1;638    // m2 <- stack639    let m16 = m3 ^ m2;640    let m20 = m16 ^ m13;641    // u4 <- stack642    let r19 = u2 ^ u4;643    let t16 = r13 ^ r19;644    // t3 <- stack645    let t26 = t3 ^ t16;646    let m6 = t3 & t16;647    let m8 = t26 ^ m6;648    // t10 -> stack649    // m7 <- stack650    let m18 = m8 ^ m7;651    let m22 = m18 ^ m13;652    let m25 = m22 & m20;653    let m26 = m21 ^ m25;654    let m10 = m9 ^ m6;655    let m19 = m10 ^ m15;656    // t25 <- stack657    let m23 = m19 ^ t25;658    let m28 = m23 ^ m25;659    let m24 = m22 ^ m23;660    let m30 = m26 & m24;661    let m39 = m23 ^ m30;662    let m48 = m39 & y5;663    let m57 = m39 & t19;664    // m48 -> stack665    let m36 = m24 ^ m25;666    let m31 = m20 & m23;667    let m27 = m20 ^ m21;668    let m32 = m27 & m31;669    let m29 = m28 & m27;670    let m37 = m21 ^ m29;671    // m39 -> stack672    let m42 = m37 ^ m39;673    let m52 = m42 & t15;674    // t27 -> stack675    // t1 <- stack676    let m61 = m42 & t1;677    let p0 = m52 ^ m61;678    let p16 = m57 ^ m61;679    // m57 -> stack680    // t20 <- stack681    let m60 = m37 & t20;682    // p16 -> stack683    // t17 <- stack684    let m51 = m37 & t17;685    let m33 = m27 ^ m25;686    let m38 = m32 ^ m33;687    let m43 = m37 ^ m38;688    let m49 = m43 & t16;689    let p6 = m49 ^ m60;690    let p13 = m49 ^ m51;691    let m58 = m43 & t3;692    // t9 <- stack693    let m50 = m38 & t9;694    // t22 <- stack695    let m59 = m38 & t22;696    // p6 -> stack697    let p1 = m58 ^ m59;698    let p7 = p0 ^ p1;699    let m34 = m21 & m22;700    let m35 = m24 & m34;701    let m40 = m35 ^ m36;702    let m41 = m38 ^ m40;703    let m45 = m42 ^ m41;704    // t27 <- stack705    let m53 = m45 & t27;706    let p8 = m50 ^ m53;707    let p23 = p7 ^ p8;708    // t4 <- stack709    let m62 = m45 & t4;710    let p14 = m49 ^ m62;711    let s6 = p14 ^ p23;712    // t10 <- stack713    let m54 = m41 & t10;714    let p2 = m54 ^ m62;715    let p22 = p2 ^ p7;716    let s0 = p13 ^ p22;717    let p17 = m58 ^ p2;718    let p15 = m54 ^ m59;719    // t2 <- stack720    let m63 = m41 & t2;721    // m39 <- stack722    let m44 = m39 ^ m40;723    // p17 -> stack724    // t6 <- stack725    let m46 = m44 & t6;726    let p5 = m46 ^ m51;727    // p23 -> stack728    let p18 = m63 ^ p5;729    let p24 = p5 ^ p7;730    // m48 <- stack731    let p12 = m46 ^ m48;732    let s3 = p12 ^ p22;733    // t13 <- stack734    let m55 = m44 & t13;735    let p9 = m55 ^ m63;736    // p16 <- stack737    let s7 = p9 ^ p16;738    // t8 <- stack739    let m47 = m40 & t8;740    let p3 = m47 ^ m50;741    let p19 = p2 ^ p3;742    let s5 = p19 ^ p24;743    let p11 = p0 ^ p3;744    let p26 = p9 ^ p11;745    // t23 <- stack746    let m56 = m40 & t23;747    let p4 = m48 ^ m56;748    // p6 <- stack749    let p20 = p4 ^ p6;750    let p29 = p15 ^ p20;751    let s1 = p26 ^ p29;752    // m57 <- stack753    let p10 = m57 ^ p4;754    let p27 = p10 ^ p18;755    // p23 <- stack756    let s4 = p23 ^ p27;757    let p25 = p6 ^ p10;758    let p28 = p11 ^ p25;759    // p17 <- stack760    let s2 = p17 ^ p28;761762    state[0] = s7;763    state[1] = s6;764    state[2] = s5;765    state[3] = s4;766    state[4] = s3;767    state[5] = s2;768    state[6] = s1;769    state[7] = s0;770}771772/// Bitsliced implementation of the AES Sbox based on Boyar, Peralta and Calik.773///774/// See: <http://www.cs.yale.edu/homes/peralta/CircuitStuff/SLP_AES_113.txt>775///776/// Note that the 4 bitwise NOT (^= 0xffffffff) are moved to the key schedule.777fn sub_bytes(state: &mut [u32]) {778    debug_assert_eq!(state.len(), 8);779780    // Scheduled using https://github.com/Ko-/aes-armcortexm/tree/public/scheduler781    // Inline "stack" comments reflect suggested stores and loads (ARM Cortex-M3 and M4)782783    let u7 = state[0];784    let u6 = state[1];785    let u5 = state[2];786    let u4 = state[3];787    let u3 = state[4];788    let u2 = state[5];789    let u1 = state[6];790    let u0 = state[7];791792    let y14 = u3 ^ u5;793    let y13 = u0 ^ u6;794    let y12 = y13 ^ y14;795    let t1 = u4 ^ y12;796    let y15 = t1 ^ u5;797    let t2 = y12 & y15;798    let y6 = y15 ^ u7;799    let y20 = t1 ^ u1;800    // y12 -> stack801    let y9 = u0 ^ u3;802    // y20 -> stack803    let y11 = y20 ^ y9;804    // y9 -> stack805    let t12 = y9 & y11;806    // y6 -> stack807    let y7 = u7 ^ y11;808    let y8 = u0 ^ u5;809    let t0 = u1 ^ u2;810    let y10 = y15 ^ t0;811    // y15 -> stack812    let y17 = y10 ^ y11;813    // y14 -> stack814    let t13 = y14 & y17;815    let t14 = t13 ^ t12;816    // y17 -> stack817    let y19 = y10 ^ y8;818    // y10 -> stack819    let t15 = y8 & y10;820    let t16 = t15 ^ t12;821    let y16 = t0 ^ y11;822    // y11 -> stack823    let y21 = y13 ^ y16;824    // y13 -> stack825    let t7 = y13 & y16;826    // y16 -> stack827    let y18 = u0 ^ y16;828    let y1 = t0 ^ u7;829    let y4 = y1 ^ u3;830    // u7 -> stack831    let t5 = y4 & u7;832    let t6 = t5 ^ t2;833    let t18 = t6 ^ t16;834    let t22 = t18 ^ y19;835    let y2 = y1 ^ u0;836    let t10 = y2 & y7;837    let t11 = t10 ^ t7;838    let t20 = t11 ^ t16;839    let t24 = t20 ^ y18;840    let y5 = y1 ^ u6;841    let t8 = y5 & y1;842    let t9 = t8 ^ t7;843    let t19 = t9 ^ t14;844    let t23 = t19 ^ y21;845    let y3 = y5 ^ y8;846    // y6 <- stack847    let t3 = y3 & y6;848    let t4 = t3 ^ t2;849    // y20 <- stack850    let t17 = t4 ^ y20;851    let t21 = t17 ^ t14;852    let t26 = t21 & t23;853    let t27 = t24 ^ t26;854    let t31 = t22 ^ t26;855    let t25 = t21 ^ t22;856    // y4 -> stack857    let t28 = t25 & t27;858    let t29 = t28 ^ t22;859    let z14 = t29 & y2;860    let z5 = t29 & y7;861    let t30 = t23 ^ t24;862    let t32 = t31 & t30;863    let t33 = t32 ^ t24;864    let t35 = t27 ^ t33;865    let t36 = t24 & t35;866    let t38 = t27 ^ t36;867    let t39 = t29 & t38;868    let t40 = t25 ^ t39;869    let t43 = t29 ^ t40;870    // y16 <- stack871    let z3 = t43 & y16;872    let tc12 = z3 ^ z5;873    // tc12 -> stack874    // y13 <- stack875    let z12 = t43 & y13;876    let z13 = t40 & y5;877    let z4 = t40 & y1;878    let tc6 = z3 ^ z4;879    let t34 = t23 ^ t33;880    let t37 = t36 ^ t34;881    let t41 = t40 ^ t37;882    // y10 <- stack883    let z8 = t41 & y10;884    let z17 = t41 & y8;885    let t44 = t33 ^ t37;886    // y15 <- stack887    let z0 = t44 & y15;888    // z17 -> stack889    // y12 <- stack890    let z9 = t44 & y12;891    let z10 = t37 & y3;892    let z1 = t37 & y6;893    let tc5 = z1 ^ z0;894    let tc11 = tc6 ^ tc5;895    // y4 <- stack896    let z11 = t33 & y4;897    let t42 = t29 ^ t33;898    let t45 = t42 ^ t41;899    // y17 <- stack900    let z7 = t45 & y17;901    let tc8 = z7 ^ tc6;902    // y14 <- stack903    let z16 = t45 & y14;904    // y11 <- stack905    let z6 = t42 & y11;906    let tc16 = z6 ^ tc8;907    // z14 -> stack908    // y9 <- stack909    let z15 = t42 & y9;910    let tc20 = z15 ^ tc16;911    let tc1 = z15 ^ z16;912    let tc2 = z10 ^ tc1;913    let tc21 = tc2 ^ z11;914    let tc3 = z9 ^ tc2;915    let s0 = tc3 ^ tc16;916    let s3 = tc3 ^ tc11;917    let s1 = s3 ^ tc16;918    let tc13 = z13 ^ tc1;919    // u7 <- stack920    let z2 = t33 & u7;921    let tc4 = z0 ^ z2;922    let tc7 = z12 ^ tc4;923    let tc9 = z8 ^ tc7;924    let tc10 = tc8 ^ tc9;925    // z14 <- stack926    let tc17 = z14 ^ tc10;927    let s5 = tc21 ^ tc17;928    let tc26 = tc17 ^ tc20;929    // z17 <- stack930    let s2 = tc26 ^ z17;931    // tc12 <- stack932    let tc14 = tc4 ^ tc12;933    let tc18 = tc13 ^ tc14;934    let s6 = tc10 ^ tc18;935    let s7 = z12 ^ tc18;936    let s4 = tc14 ^ s3;937938    state[0] = s7;939    state[1] = s6;940    state[2] = s5;941    state[3] = s4;942    state[4] = s3;943    state[5] = s2;944    state[6] = s1;945    state[7] = s0;946}947948/// NOT operations that are omitted in S-box949#[inline]950fn sub_bytes_nots(state: &mut [u32]) {951    debug_assert_eq!(state.len(), 8);952    state[0] ^= 0xffffffff;953    state[1] ^= 0xffffffff;954    state[5] ^= 0xffffffff;955    state[6] ^= 0xffffffff;956}957958/// Computation of the MixColumns transformation in the fixsliced representation, with different959/// rotations used according to the round number mod 4.960///961/// Based on Käsper-Schwabe, similar to https://github.com/Ko-/aes-armcortexm.962macro_rules! define_mix_columns {963    (964        $name:ident,965        $name_inv:ident,966        $first_rotate:path,967        $second_rotate:path968    ) => {969        #[rustfmt::skip]970        fn $name(state: &mut State) {971            let (a0, a1, a2, a3, a4, a5, a6, a7) = (972                state[0], state[1], state[2], state[3], state[4], state[5], state[6], state[7]973            );974            let (b0, b1, b2, b3, b4, b5, b6, b7) = (975                $first_rotate(a0),976                $first_rotate(a1),977                $first_rotate(a2),978                $first_rotate(a3),979                $first_rotate(a4),980                $first_rotate(a5),981                $first_rotate(a6),982                $first_rotate(a7),983            );984            let (c0, c1, c2, c3, c4, c5, c6, c7) = (985                a0 ^ b0,986                a1 ^ b1,987                a2 ^ b2,988                a3 ^ b3,989                a4 ^ b4,990                a5 ^ b5,991                a6 ^ b6,992                a7 ^ b7,993            );994            state[0] = b0      ^ c7 ^ $second_rotate(c0);995            state[1] = b1 ^ c0 ^ c7 ^ $second_rotate(c1);996            state[2] = b2 ^ c1      ^ $second_rotate(c2);997            state[3] = b3 ^ c2 ^ c7 ^ $second_rotate(c3);998            state[4] = b4 ^ c3 ^ c7 ^ $second_rotate(c4);999            state[5] = b5 ^ c4      ^ $second_rotate(c5);1000            state[6] = b6 ^ c5      ^ $second_rotate(c6);1001            state[7] = b7 ^ c6      ^ $second_rotate(c7);1002        }10031004        #[rustfmt::skip]1005        fn $name_inv(state: &mut State) {1006            let (a0, a1, a2, a3, a4, a5, a6, a7) = (1007                state[0], state[1], state[2], state[3], state[4], state[5], state[6], state[7]1008            );1009            let (b0, b1, b2, b3, b4, b5, b6, b7) = (1010                $first_rotate(a0),1011                $first_rotate(a1),1012                $first_rotate(a2),1013                $first_rotate(a3),1014                $first_rotate(a4),1015                $first_rotate(a5),1016                $first_rotate(a6),1017                $first_rotate(a7),1018            );1019            let (c0, c1, c2, c3, c4, c5, c6, c7) = (1020                a0 ^ b0,1021                a1 ^ b1,1022                a2 ^ b2,1023                a3 ^ b3,1024                a4 ^ b4,1025                a5 ^ b5,1026                a6 ^ b6,1027                a7 ^ b7,1028            );1029            let (d0, d1, d2, d3, d4, d5, d6, d7) = (1030                a0      ^ c7,1031                a1 ^ c0 ^ c7,1032                a2 ^ c1,1033                a3 ^ c2 ^ c7,1034                a4 ^ c3 ^ c7,1035                a5 ^ c4,1036                a6 ^ c5,1037                a7 ^ c6,1038            );1039            let (e0, e1, e2, e3, e4, e5, e6, e7) = (1040                c0      ^ d6,1041                c1      ^ d6 ^ d7,1042                c2 ^ d0      ^ d7,1043                c3 ^ d1 ^ d6,1044                c4 ^ d2 ^ d6 ^ d7,1045                c5 ^ d3      ^ d7,1046                c6 ^ d4,1047                c7 ^ d5,1048            );1049            state[0] = d0 ^ e0 ^ $second_rotate(e0);1050            state[1] = d1 ^ e1 ^ $second_rotate(e1);1051            state[2] = d2 ^ e2 ^ $second_rotate(e2);1052            state[3] = d3 ^ e3 ^ $second_rotate(e3);1053            state[4] = d4 ^ e4 ^ $second_rotate(e4);1054            state[5] = d5 ^ e5 ^ $second_rotate(e5);1055            state[6] = d6 ^ e6 ^ $second_rotate(e6);1056            state[7] = d7 ^ e7 ^ $second_rotate(e7);1057        }1058    }1059}10601061define_mix_columns!(1062    mix_columns_0,1063    inv_mix_columns_0,1064    rotate_rows_1,1065    rotate_rows_21066);10671068define_mix_columns!(1069    mix_columns_1,1070    inv_mix_columns_1,1071    rotate_rows_and_columns_1_1,1072    rotate_rows_and_columns_2_21073);10741075#[cfg(not(aes_compact))]1076define_mix_columns!(1077    mix_columns_2,1078    inv_mix_columns_2,1079    rotate_rows_and_columns_1_2,1080    rotate_rows_21081);10821083#[cfg(not(aes_compact))]1084define_mix_columns!(1085    mix_columns_3,1086    inv_mix_columns_3,1087    rotate_rows_and_columns_1_3,1088    rotate_rows_and_columns_2_21089);10901091#[inline]1092fn delta_swap_1(a: &mut u32, shift: u32, mask: u32) {1093    let t = (*a ^ ((*a) >> shift)) & mask;1094    *a ^= t ^ (t << shift);1095}10961097#[inline]1098fn delta_swap_2(a: &mut u32, b: &mut u32, shift: u32, mask: u32) {1099    let t = (*a ^ ((*b) >> shift)) & mask;1100    *a ^= t;1101    *b ^= t << shift;1102}11031104/// Applies ShiftRows once on an AES state (or key).1105#[cfg(any(not(aes_compact), feature = "hazmat"))]1106#[inline]1107fn shift_rows_1(state: &mut [u32]) {1108    debug_assert_eq!(state.len(), 8);1109    for x in state.iter_mut() {1110        delta_swap_1(x, 4, 0x0c0f0300);1111        delta_swap_1(x, 2, 0x33003300);1112    }1113}11141115/// Applies ShiftRows twice on an AES state (or key).1116#[inline]1117fn shift_rows_2(state: &mut [u32]) {1118    debug_assert_eq!(state.len(), 8);1119    for x in state.iter_mut() {1120        delta_swap_1(x, 4, 0x0f000f00);1121    }1122}11231124/// Applies ShiftRows three times on an AES state (or key).1125#[inline]1126fn shift_rows_3(state: &mut [u32]) {1127    debug_assert_eq!(state.len(), 8);1128    for x in state.iter_mut() {1129        delta_swap_1(x, 4, 0x030f0c00);1130        delta_swap_1(x, 2, 0x33003300);1131    }1132}11331134#[inline(always)]1135fn inv_shift_rows_1(state: &mut [u32]) {1136    shift_rows_3(state);1137}11381139#[inline(always)]1140fn inv_shift_rows_2(state: &mut [u32]) {1141    shift_rows_2(state);1142}11431144#[cfg(not(aes_compact))]1145#[inline(always)]1146fn inv_shift_rows_3(state: &mut [u32]) {1147    shift_rows_1(state);1148}11491150/// XOR the columns after the S-box during the key schedule round function.1151///1152/// The `idx_xor` parameter refers to the index of the previous round key that is1153/// involved in the XOR computation (should be 8 and 16 for AES-128 and AES-256,1154/// respectively).1155///1156/// The `idx_ror` parameter refers to the rotation value, which varies between the1157/// different key schedules.1158fn xor_columns(rkeys: &mut [u32], offset: usize, idx_xor: usize, idx_ror: u32) {1159    for i in 0..8 {1160        let off_i = offset + i;1161        let rk = rkeys[off_i - idx_xor] ^ (0x03030303 & ror(rkeys[off_i], idx_ror));1162        rkeys[off_i] =1163            rk ^ (0xfcfcfcfc & (rk << 2)) ^ (0xf0f0f0f0 & (rk << 4)) ^ (0xc0c0c0c0 & (rk << 6));1164    }1165}11661167/// Bitslice two 128-bit input blocks input0, input1 into a 256-bit internal state.1168fn bitslice(output: &mut [u32], input0: &[u8], input1: &[u8]) {1169    debug_assert_eq!(output.len(), 8);1170    debug_assert_eq!(input0.len(), 16);1171    debug_assert_eq!(input1.len(), 16);11721173    // Bitslicing is a bit index manipulation. 256 bits of data means each bit is positioned at an1174    // 8-bit index. AES data is 2 blocks, each one a 4x4 column-major matrix of bytes, so the1175    // index is initially ([b]lock, [c]olumn, [r]ow, [p]osition):1176    //     b0 c1 c0 r1 r0 p2 p1 p01177    //1178    // The desired bitsliced data groups first by bit position, then row, column, block:1179    //     p2 p1 p0 r1 r0 c1 c0 b011801181    // Interleave the columns on input (note the order of input)1182    //     b0 c1 c0 __ __ __ __ __ => c1 c0 b0 __ __ __ __ __1183    let mut t0 = u32::from_le_bytes(input0[0x00..0x04].try_into().unwrap());1184    let mut t2 = u32::from_le_bytes(input0[0x04..0x08].try_into().unwrap());1185    let mut t4 = u32::from_le_bytes(input0[0x08..0x0c].try_into().unwrap());1186    let mut t6 = u32::from_le_bytes(input0[0x0c..0x10].try_into().unwrap());1187    let mut t1 = u32::from_le_bytes(input1[0x00..0x04].try_into().unwrap());1188    let mut t3 = u32::from_le_bytes(input1[0x04..0x08].try_into().unwrap());1189    let mut t5 = u32::from_le_bytes(input1[0x08..0x0c].try_into().unwrap());1190    let mut t7 = u32::from_le_bytes(input1[0x0c..0x10].try_into().unwrap());11911192    // Bit Index Swap 5 <-> 0:1193    //     __ __ b0 __ __ __ __ p0 => __ __ p0 __ __ __ __ b01194    let m0 = 0x55555555;1195    delta_swap_2(&mut t1, &mut t0, 1, m0);1196    delta_swap_2(&mut t3, &mut t2, 1, m0);1197    delta_swap_2(&mut t5, &mut t4, 1, m0);1198    delta_swap_2(&mut t7, &mut t6, 1, m0);11991200    // Bit Index Swap 6 <-> 1:1201    //     __ c0 __ __ __ __ p1 __ => __ p1 __ __ __ __ c0 __1202    let m1 = 0x33333333;1203    delta_swap_2(&mut t2, &mut t0, 2, m1);1204    delta_swap_2(&mut t3, &mut t1, 2, m1);1205    delta_swap_2(&mut t6, &mut t4, 2, m1);1206    delta_swap_2(&mut t7, &mut t5, 2, m1);12071208    // Bit Index Swap 7 <-> 2:1209    //     c1 __ __ __ __ p2 __ __ => p2 __ __ __ __ c1 __ __1210    let m2 = 0x0f0f0f0f;1211    delta_swap_2(&mut t4, &mut t0, 4, m2);1212    delta_swap_2(&mut t5, &mut t1, 4, m2);1213    delta_swap_2(&mut t6, &mut t2, 4, m2);1214    delta_swap_2(&mut t7, &mut t3, 4, m2);12151216    // Final bitsliced bit index, as desired:1217    //     p2 p1 p0 r1 r0 c1 c0 b01218    output[0] = t0;1219    output[1] = t1;1220    output[2] = t2;1221    output[3] = t3;1222    output[4] = t4;1223    output[5] = t5;1224    output[6] = t6;1225    output[7] = t7;1226}12271228/// Un-bitslice a 256-bit internal state into two 128-bit blocks of output.1229fn inv_bitslice(input: &[u32]) -> BatchBlocks {1230    debug_assert_eq!(input.len(), 8);12311232    // Unbitslicing is a bit index manipulation. 256 bits of data means each bit is positioned at1233    // an 8-bit index. AES data is 2 blocks, each one a 4x4 column-major matrix of bytes, so the1234    // desired index for the output is ([b]lock, [c]olumn, [r]ow, [p]osition):1235    //     b0 c1 c0 r1 r0 p2 p1 p01236    //1237    // The initially bitsliced data groups first by bit position, then row, column, block:1238    //     p2 p1 p0 r1 r0 c1 c0 b012391240    let mut t0 = input[0];1241    let mut t1 = input[1];1242    let mut t2 = input[2];1243    let mut t3 = input[3];1244    let mut t4 = input[4];1245    let mut t5 = input[5];1246    let mut t6 = input[6];1247    let mut t7 = input[7];12481249    // TODO: these bit index swaps are identical to those in 'packing'12501251    // Bit Index Swap 5 <-> 0:1252    //     __ __ p0 __ __ __ __ b0 => __ __ b0 __ __ __ __ p01253    let m0 = 0x55555555;1254    delta_swap_2(&mut t1, &mut t0, 1, m0);1255    delta_swap_2(&mut t3, &mut t2, 1, m0);1256    delta_swap_2(&mut t5, &mut t4, 1, m0);1257    delta_swap_2(&mut t7, &mut t6, 1, m0);12581259    // Bit Index Swap 6 <-> 1:1260    //     __ p1 __ __ __ __ c0 __ => __ c0 __ __ __ __ p1 __1261    let m1 = 0x33333333;1262    delta_swap_2(&mut t2, &mut t0, 2, m1);1263    delta_swap_2(&mut t3, &mut t1, 2, m1);1264    delta_swap_2(&mut t6, &mut t4, 2, m1);1265    delta_swap_2(&mut t7, &mut t5, 2, m1);12661267    // Bit Index Swap 7 <-> 2:1268    //     p2 __ __ __ __ c1 __ __ => c1 __ __ __ __ p2 __ __1269    let m2 = 0x0f0f0f0f;1270    delta_swap_2(&mut t4, &mut t0, 4, m2);1271    delta_swap_2(&mut t5, &mut t1, 4, m2);1272    delta_swap_2(&mut t6, &mut t2, 4, m2);1273    delta_swap_2(&mut t7, &mut t3, 4, m2);12741275    let mut output = BatchBlocks::default();1276    // De-interleave the columns on output (note the order of output)1277    //     c1 c0 b0 __ __ __ __ __ => b0 c1 c0 __ __ __ __ __1278    output[0][0x00..0x04].copy_from_slice(&t0.to_le_bytes());1279    output[0][0x04..0x08].copy_from_slice(&t2.to_le_bytes());1280    output[0][0x08..0x0c].copy_from_slice(&t4.to_le_bytes());1281    output[0][0x0c..0x10].copy_from_slice(&t6.to_le_bytes());1282    output[1][0x00..0x04].copy_from_slice(&t1.to_le_bytes());1283    output[1][0x04..0x08].copy_from_slice(&t3.to_le_bytes());1284    output[1][0x08..0x0c].copy_from_slice(&t5.to_le_bytes());1285    output[1][0x0c..0x10].copy_from_slice(&t7.to_le_bytes());12861287    // Final AES bit index, as desired:1288    //     b0 c1 c0 r1 r0 p2 p1 p01289    output1290}12911292/// Copy 32-bytes within the provided slice to an 8-byte offset1293fn memshift32(buffer: &mut [u32], src_offset: usize) {1294    debug_assert_eq!(src_offset % 8, 0);12951296    let dst_offset = src_offset + 8;1297    debug_assert!(dst_offset + 8 <= buffer.len());12981299    for i in (0..8).rev() {1300        buffer[dst_offset + i] = buffer[src_offset + i];1301    }1302}13031304/// XOR the round key to the internal state. The round keys are expected to be1305/// pre-computed and to be packed in the fixsliced representation.1306#[inline]1307fn add_round_key(state: &mut State, rkey: &[u32]) {1308    debug_assert_eq!(rkey.len(), 8);1309    for (a, b) in state.iter_mut().zip(rkey) {1310        *a ^= b;1311    }1312}13131314#[inline(always)]1315fn add_round_constant_bit(state: &mut [u32], bit: usize) {1316    state[bit] ^= 0x0000c000;1317}13181319#[inline(always)]1320fn ror(x: u32, y: u32) -> u32 {1321    x.rotate_right(y)1322}13231324#[inline(always)]1325fn ror_distance(rows: u32, cols: u32) -> u32 {1326    (rows << 3) + (cols << 1)1327}13281329#[inline(always)]1330fn rotate_rows_1(x: u32) -> u32 {1331    ror(x, ror_distance(1, 0))1332}13331334#[inline(always)]1335fn rotate_rows_2(x: u32) -> u32 {1336    ror(x, ror_distance(2, 0))1337}13381339#[inline(always)]1340#[rustfmt::skip]1341fn rotate_rows_and_columns_1_1(x: u32) -> u32 {1342    (ror(x, ror_distance(1, 1)) & 0x3f3f3f3f) |1343    (ror(x, ror_distance(0, 1)) & 0xc0c0c0c0)1344}13451346#[cfg(not(aes_compact))]1347#[inline(always)]1348#[rustfmt::skip]1349fn rotate_rows_and_columns_1_2(x: u32) -> u32 {1350    (ror(x, ror_distance(1, 2)) & 0x0f0f0f0f) |1351    (ror(x, ror_distance(0, 2)) & 0xf0f0f0f0)1352}13531354#[cfg(not(aes_compact))]1355#[inline(always)]1356#[rustfmt::skip]1357fn rotate_rows_and_columns_1_3(x: u32) -> u32 {1358    (ror(x, ror_distance(1, 3)) & 0x03030303) |1359    (ror(x, ror_distance(0, 3)) & 0xfcfcfcfc)1360}13611362#[inline(always)]1363#[rustfmt::skip]1364fn rotate_rows_and_columns_2_2(x: u32) -> u32 {1365    (ror(x, ror_distance(2, 2)) & 0x0f0f0f0f) |1366    (ror(x, ror_distance(1, 2)) & 0xf0f0f0f0)1367}13681369/// Low-level "hazmat" AES functions.1370///1371/// Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`1372/// implementations in this crate, but instead provides raw access to1373/// the AES round function gated under the `hazmat` crate feature.1374#[cfg(feature = "hazmat")]1375pub(crate) mod hazmat {1376    use super::{1377        bitslice, inv_bitslice, inv_mix_columns_0, inv_shift_rows_1, inv_sub_bytes, mix_columns_0,1378        shift_rows_1, sub_bytes, sub_bytes_nots, State,1379    };1380    use crate::{Block, Block8};13811382    /// XOR the `src` block into the `dst` block in-place.1383    fn xor_in_place(dst: &mut Block, src: &Block) {1384        for (a, b) in dst.iter_mut().zip(src.as_slice()) {1385            *a ^= *b;1386        }1387    }13881389    /// Perform a bitslice operation, loading a single block.1390    fn bitslice_block(block: &Block) -> State {1391        let mut state = State::default();1392        bitslice(&mut state, block, block);1393        state1394    }13951396    /// Perform an inverse bitslice operation, extracting a single block.1397    fn inv_bitslice_block(block: &mut Block, state: &State) {1398        let out = inv_bitslice(state);1399        block.copy_from_slice(&out[0]);1400    }14011402    /// AES cipher (encrypt) round function.1403    #[inline]1404    pub(crate) fn cipher_round(block: &mut Block, round_key: &Block) {1405        let mut state = bitslice_block(block);1406        sub_bytes(&mut state);1407        sub_bytes_nots(&mut state);1408        shift_rows_1(&mut state);1409        mix_columns_0(&mut state);1410        inv_bitslice_block(block, &state);1411        xor_in_place(block, round_key);1412    }14131414    /// AES cipher (encrypt) round function: parallel version.1415    #[inline]1416    pub(crate) fn cipher_round_par(blocks: &mut Block8, round_keys: &Block8) {1417        for (chunk, keys) in blocks.chunks_exact_mut(2).zip(round_keys.chunks_exact(2)) {1418            let mut state = State::default();1419            bitslice(&mut state, &chunk[0], &chunk[1]);1420            sub_bytes(&mut state);1421            sub_bytes_nots(&mut state);1422            shift_rows_1(&mut state);1423            mix_columns_0(&mut state);1424            let res = inv_bitslice(&state);14251426            for i in 0..2 {1427                chunk[i] = res[i];1428                xor_in_place(&mut chunk[i], &keys[i]);1429            }1430        }1431    }14321433    /// AES cipher (encrypt) round function.1434    #[inline]1435    pub(crate) fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {1436        let mut state = bitslice_block(block);1437        sub_bytes_nots(&mut state);1438        inv_sub_bytes(&mut state);1439        inv_shift_rows_1(&mut state);1440        inv_mix_columns_0(&mut state);1441        inv_bitslice_block(block, &state);1442        xor_in_place(block, round_key);1443    }14441445    /// AES cipher (encrypt) round function: parallel version.1446    #[inline]1447    pub(crate) fn equiv_inv_cipher_round_par(blocks: &mut Block8, round_keys: &Block8) {1448        for (chunk, keys) in blocks.chunks_exact_mut(2).zip(round_keys.chunks_exact(2)) {1449            let mut state = State::default();1450            bitslice(&mut state, &chunk[0], &chunk[1]);1451            sub_bytes_nots(&mut state);1452            inv_sub_bytes(&mut state);1453            inv_shift_rows_1(&mut state);1454            inv_mix_columns_0(&mut state);1455            let res = inv_bitslice(&state);14561457            for i in 0..2 {1458                chunk[i] = res[i];1459                xor_in_place(&mut chunk[i], &keys[i]);1460            }1461        }1462    }14631464    /// AES mix columns function.1465    #[inline]1466    pub(crate) fn mix_columns(block: &mut Block) {1467        let mut state = bitslice_block(block);1468        mix_columns_0(&mut state);1469        inv_bitslice_block(block, &state);1470    }14711472    /// AES inverse mix columns function.1473    #[inline]1474    pub(crate) fn inv_mix_columns(block: &mut Block) {1475        let mut state = bitslice_block(block);1476        inv_mix_columns_0(&mut state);1477        inv_bitslice_block(block, &state);1478    }1479}