Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 3 | * Copyright (c) 2020 Cisco and/or its affiliates. |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 18 | #ifndef __aes_h__ |
| 19 | #define __aes_h__ |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 20 | |
| 21 | typedef enum |
| 22 | { |
Damjan Marion | 7d08e39 | 2020-01-28 09:55:25 +0100 | [diff] [blame] | 23 | AES_KEY_128 = 0, |
| 24 | AES_KEY_192 = 1, |
| 25 | AES_KEY_256 = 2, |
| 26 | } aes_key_size_t; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 27 | |
Damjan Marion | 7d08e39 | 2020-01-28 09:55:25 +0100 | [diff] [blame] | 28 | #define AES_KEY_ROUNDS(x) (10 + x * 2) |
| 29 | #define AES_KEY_BYTES(x) (16 + x * 8) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 30 | |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 31 | static_always_inline u8x16 |
| 32 | aes_block_load (u8 * p) |
| 33 | { |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 34 | return *(u8x16u *) p; |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static_always_inline u8x16 |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 38 | aes_enc_round_x1 (u8x16 a, u8x16 k) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 39 | { |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 40 | #if defined (__AES__) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 41 | return (u8x16) _mm_aesenc_si128 ((__m128i) a, (__m128i) k); |
Damjan Marion | 4ba16a4 | 2020-04-28 13:29:37 +0200 | [diff] [blame] | 42 | #elif defined (__ARM_FEATURE_CRYPTO) |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 43 | return vaesmcq_u8 (vaeseq_u8 (a, u8x16_splat (0))) ^ k; |
| 44 | #endif |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Damjan Marion | adeaf16 | 2023-03-14 18:04:45 +0000 | [diff] [blame] | 47 | #if defined(__VAES__) && defined(__AVX512F__) |
Damjan Marion | e84e9d7 | 2020-02-13 13:11:02 +0100 | [diff] [blame] | 48 | static_always_inline u8x64 |
| 49 | aes_enc_round_x4 (u8x64 a, u8x64 k) |
| 50 | { |
| 51 | return (u8x64) _mm512_aesenc_epi128 ((__m512i) a, (__m512i) k); |
| 52 | } |
| 53 | |
| 54 | static_always_inline u8x64 |
| 55 | aes_enc_last_round_x4 (u8x64 a, u8x64 k) |
| 56 | { |
| 57 | return (u8x64) _mm512_aesenclast_epi128 ((__m512i) a, (__m512i) k); |
| 58 | } |
| 59 | |
| 60 | static_always_inline u8x64 |
| 61 | aes_dec_round_x4 (u8x64 a, u8x64 k) |
| 62 | { |
| 63 | return (u8x64) _mm512_aesdec_epi128 ((__m512i) a, (__m512i) k); |
| 64 | } |
| 65 | |
| 66 | static_always_inline u8x64 |
| 67 | aes_dec_last_round_x4 (u8x64 a, u8x64 k) |
| 68 | { |
| 69 | return (u8x64) _mm512_aesdeclast_epi128 ((__m512i) a, (__m512i) k); |
| 70 | } |
| 71 | #endif |
| 72 | |
Damjan Marion | adeaf16 | 2023-03-14 18:04:45 +0000 | [diff] [blame] | 73 | #ifdef __VAES__ |
| 74 | static_always_inline u8x32 |
| 75 | aes_enc_round_x2 (u8x32 a, u8x32 k) |
| 76 | { |
| 77 | return (u8x32) _mm256_aesenc_epi128 ((__m256i) a, (__m256i) k); |
| 78 | } |
| 79 | |
| 80 | static_always_inline u8x32 |
| 81 | aes_enc_last_round_x2 (u8x32 a, u8x32 k) |
| 82 | { |
| 83 | return (u8x32) _mm256_aesenclast_epi128 ((__m256i) a, (__m256i) k); |
| 84 | } |
| 85 | |
| 86 | static_always_inline u8x32 |
| 87 | aes_dec_round_x2 (u8x32 a, u8x32 k) |
| 88 | { |
| 89 | return (u8x32) _mm256_aesdec_epi128 ((__m256i) a, (__m256i) k); |
| 90 | } |
| 91 | |
| 92 | static_always_inline u8x32 |
| 93 | aes_dec_last_round_x2 (u8x32 a, u8x32 k) |
| 94 | { |
| 95 | return (u8x32) _mm256_aesdeclast_epi128 ((__m256i) a, (__m256i) k); |
| 96 | } |
| 97 | #endif |
| 98 | |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 99 | static_always_inline u8x16 |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 100 | aes_enc_last_round_x1 (u8x16 a, u8x16 k) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 101 | { |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 102 | #if defined (__AES__) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 103 | return (u8x16) _mm_aesenclast_si128 ((__m128i) a, (__m128i) k); |
Damjan Marion | 4ba16a4 | 2020-04-28 13:29:37 +0200 | [diff] [blame] | 104 | #elif defined (__ARM_FEATURE_CRYPTO) |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 105 | return vaeseq_u8 (a, u8x16_splat (0)) ^ k; |
| 106 | #endif |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 109 | #ifdef __x86_64__ |
| 110 | |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 111 | static_always_inline u8x16 |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 112 | aes_dec_round_x1 (u8x16 a, u8x16 k) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 113 | { |
| 114 | return (u8x16) _mm_aesdec_si128 ((__m128i) a, (__m128i) k); |
| 115 | } |
| 116 | |
| 117 | static_always_inline u8x16 |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 118 | aes_dec_last_round_x1 (u8x16 a, u8x16 k) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 119 | { |
| 120 | return (u8x16) _mm_aesdeclast_si128 ((__m128i) a, (__m128i) k); |
| 121 | } |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 122 | #endif |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 123 | |
| 124 | static_always_inline void |
| 125 | aes_block_store (u8 * p, u8x16 r) |
| 126 | { |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 127 | *(u8x16u *) p = r; |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static_always_inline u8x16 |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 131 | aes_encrypt_block (u8x16 block, const u8x16 * round_keys, aes_key_size_t ks) |
| 132 | { |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 133 | int rounds = AES_KEY_ROUNDS (ks); |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 134 | block ^= round_keys[0]; |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 135 | for (int i = 1; i < rounds; i += 1) |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 136 | block = aes_enc_round_x1 (block, round_keys[i]); |
| 137 | return aes_enc_last_round_x1 (block, round_keys[rounds]); |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static_always_inline u8x16 |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 141 | aes_inv_mix_column (u8x16 a) |
| 142 | { |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 143 | #if defined (__AES__) |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 144 | return (u8x16) _mm_aesimc_si128 ((__m128i) a); |
Damjan Marion | 4ba16a4 | 2020-04-28 13:29:37 +0200 | [diff] [blame] | 145 | #elif defined (__ARM_FEATURE_CRYPTO) |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 146 | return vaesimcq_u8 (a); |
| 147 | #endif |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 148 | } |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 149 | |
Damjan Marion | 622b5ce | 2020-02-12 10:59:14 +0100 | [diff] [blame] | 150 | #ifdef __x86_64__ |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 151 | #define aes_keygen_assist(a, b) \ |
| 152 | (u8x16) _mm_aeskeygenassist_si128((__m128i) a, b) |
| 153 | |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 154 | /* AES-NI based AES key expansion based on code samples from |
| 155 | Intel(r) Advanced Encryption Standard (AES) New Instructions White Paper |
| 156 | (323641-001) */ |
| 157 | |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 158 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 159 | aes128_key_assist (u8x16 * rk, u8x16 r) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 160 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 161 | u8x16 t = rk[-1]; |
| 162 | t ^= u8x16_word_shift_left (t, 4); |
| 163 | t ^= u8x16_word_shift_left (t, 4); |
| 164 | t ^= u8x16_word_shift_left (t, 4); |
| 165 | rk[0] = t ^ (u8x16) u32x4_shuffle ((u32x4) r, 3, 3, 3, 3); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static_always_inline void |
Damjan Marion | 21feecf | 2021-10-29 22:08:39 +0200 | [diff] [blame] | 169 | aes128_key_expand (u8x16 *rk, u8x16u const *k) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 170 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 171 | rk[0] = k[0]; |
| 172 | aes128_key_assist (rk + 1, aes_keygen_assist (rk[0], 0x01)); |
| 173 | aes128_key_assist (rk + 2, aes_keygen_assist (rk[1], 0x02)); |
| 174 | aes128_key_assist (rk + 3, aes_keygen_assist (rk[2], 0x04)); |
| 175 | aes128_key_assist (rk + 4, aes_keygen_assist (rk[3], 0x08)); |
| 176 | aes128_key_assist (rk + 5, aes_keygen_assist (rk[4], 0x10)); |
| 177 | aes128_key_assist (rk + 6, aes_keygen_assist (rk[5], 0x20)); |
| 178 | aes128_key_assist (rk + 7, aes_keygen_assist (rk[6], 0x40)); |
| 179 | aes128_key_assist (rk + 8, aes_keygen_assist (rk[7], 0x80)); |
| 180 | aes128_key_assist (rk + 9, aes_keygen_assist (rk[8], 0x1b)); |
| 181 | aes128_key_assist (rk + 10, aes_keygen_assist (rk[9], 0x36)); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 185 | aes192_key_assist (u8x16 * r1, u8x16 * r2, u8x16 key_assist) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 186 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 187 | u8x16 t; |
| 188 | r1[0] ^= t = u8x16_word_shift_left (r1[0], 4); |
| 189 | r1[0] ^= t = u8x16_word_shift_left (t, 4); |
| 190 | r1[0] ^= u8x16_word_shift_left (t, 4); |
| 191 | r1[0] ^= (u8x16) _mm_shuffle_epi32 ((__m128i) key_assist, 0x55); |
| 192 | r2[0] ^= u8x16_word_shift_left (r2[0], 4); |
| 193 | r2[0] ^= (u8x16) _mm_shuffle_epi32 ((__m128i) r1[0], 0xff); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 197 | aes192_key_expand (u8x16 * rk, u8x16u const *k) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 198 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 199 | u8x16 r1, r2; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 200 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 201 | rk[0] = r1 = k[0]; |
| 202 | /* *INDENT-OFF* */ |
| 203 | rk[1] = r2 = (u8x16) (u64x2) { *(u64 *) (k + 1), 0 }; |
| 204 | /* *INDENT-ON* */ |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 205 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 206 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x1)); |
| 207 | rk[1] = (u8x16) _mm_shuffle_pd ((__m128d) rk[1], (__m128d) r1, 0); |
| 208 | rk[2] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1); |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 209 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 210 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x2)); |
| 211 | rk[3] = r1; |
| 212 | rk[4] = r2; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 213 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 214 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x4)); |
| 215 | rk[4] = (u8x16) _mm_shuffle_pd ((__m128d) rk[4], (__m128d) r1, 0); |
| 216 | rk[5] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 217 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 218 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x8)); |
| 219 | rk[6] = r1; |
| 220 | rk[7] = r2; |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 221 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 222 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x10)); |
| 223 | rk[7] = (u8x16) _mm_shuffle_pd ((__m128d) rk[7], (__m128d) r1, 0); |
| 224 | rk[8] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1); |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 225 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 226 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x20)); |
| 227 | rk[9] = r1; |
| 228 | rk[10] = r2; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 229 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 230 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x40)); |
| 231 | rk[10] = (u8x16) _mm_shuffle_pd ((__m128d) rk[10], (__m128d) r1, 0); |
| 232 | rk[11] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1); |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 233 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 234 | aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x80)); |
| 235 | rk[12] = r1; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 239 | aes256_key_assist (u8x16 * rk, int i, u8x16 key_assist) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 240 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 241 | u8x16 r, t; |
| 242 | rk += i; |
| 243 | r = rk[-2]; |
| 244 | r ^= t = u8x16_word_shift_left (r, 4); |
| 245 | r ^= t = u8x16_word_shift_left (t, 4); |
| 246 | r ^= u8x16_word_shift_left (t, 4); |
| 247 | r ^= (u8x16) u32x4_shuffle ((u32x4) key_assist, 3, 3, 3, 3); |
| 248 | rk[0] = r; |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 249 | |
| 250 | if (i >= 14) |
| 251 | return; |
| 252 | |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 253 | key_assist = aes_keygen_assist (rk[0], 0x0); |
| 254 | r = rk[-1]; |
| 255 | r ^= t = u8x16_word_shift_left (r, 4); |
| 256 | r ^= t = u8x16_word_shift_left (t, 4); |
| 257 | r ^= u8x16_word_shift_left (t, 4); |
| 258 | r ^= (u8x16) u32x4_shuffle ((u32x4) key_assist, 2, 2, 2, 2); |
| 259 | rk[1] = r; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 263 | aes256_key_expand (u8x16 * rk, u8x16u const *k) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 264 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 265 | rk[0] = k[0]; |
| 266 | rk[1] = k[1]; |
| 267 | aes256_key_assist (rk, 2, aes_keygen_assist (rk[1], 0x01)); |
| 268 | aes256_key_assist (rk, 4, aes_keygen_assist (rk[3], 0x02)); |
| 269 | aes256_key_assist (rk, 6, aes_keygen_assist (rk[5], 0x04)); |
| 270 | aes256_key_assist (rk, 8, aes_keygen_assist (rk[7], 0x08)); |
| 271 | aes256_key_assist (rk, 10, aes_keygen_assist (rk[9], 0x10)); |
| 272 | aes256_key_assist (rk, 12, aes_keygen_assist (rk[11], 0x20)); |
| 273 | aes256_key_assist (rk, 14, aes_keygen_assist (rk[13], 0x40)); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 274 | } |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 275 | #endif |
| 276 | |
| 277 | #ifdef __aarch64__ |
| 278 | |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 279 | static const u8x16 aese_prep_mask1 = |
| 280 | { 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12 }; |
| 281 | static const u8x16 aese_prep_mask2 = |
| 282 | { 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15 }; |
| 283 | |
Damjan Marion | 4fe44af | 2020-02-12 18:24:24 +0100 | [diff] [blame] | 284 | static_always_inline void |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 285 | aes128_key_expand_round_neon (u8x16 * rk, u32 rcon) |
| 286 | { |
| 287 | u8x16 r, t, last_round = rk[-1], z = { }; |
| 288 | r = vqtbl1q_u8 (last_round, aese_prep_mask1); |
| 289 | r = vaeseq_u8 (r, z); |
| 290 | r ^= (u8x16) vdupq_n_u32 (rcon); |
| 291 | r ^= last_round; |
| 292 | r ^= t = vextq_u8 (z, last_round, 12); |
| 293 | r ^= t = vextq_u8 (z, t, 12); |
| 294 | r ^= vextq_u8 (z, t, 12); |
| 295 | rk[0] = r; |
| 296 | } |
| 297 | |
Damjan Marion | 4fe44af | 2020-02-12 18:24:24 +0100 | [diff] [blame] | 298 | static_always_inline void |
Jieqiang Wang | 83b982b | 2021-11-29 14:25:03 +0000 | [diff] [blame] | 299 | aes128_key_expand (u8x16 *rk, u8x16u const *k) |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 300 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 301 | rk[0] = k[0]; |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 302 | aes128_key_expand_round_neon (rk + 1, 0x01); |
| 303 | aes128_key_expand_round_neon (rk + 2, 0x02); |
| 304 | aes128_key_expand_round_neon (rk + 3, 0x04); |
| 305 | aes128_key_expand_round_neon (rk + 4, 0x08); |
| 306 | aes128_key_expand_round_neon (rk + 5, 0x10); |
| 307 | aes128_key_expand_round_neon (rk + 6, 0x20); |
| 308 | aes128_key_expand_round_neon (rk + 7, 0x40); |
| 309 | aes128_key_expand_round_neon (rk + 8, 0x80); |
| 310 | aes128_key_expand_round_neon (rk + 9, 0x1b); |
| 311 | aes128_key_expand_round_neon (rk + 10, 0x36); |
| 312 | } |
| 313 | |
Damjan Marion | 4fe44af | 2020-02-12 18:24:24 +0100 | [diff] [blame] | 314 | static_always_inline void |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 315 | aes192_key_expand_round_neon (u8x8 * rk, u32 rcon) |
| 316 | { |
| 317 | u8x8 r, last_round = rk[-1], z = { }; |
| 318 | u8x16 r2, z2 = { }; |
| 319 | |
| 320 | r2 = (u8x16) vdupq_lane_u64 ((uint64x1_t) last_round, 0); |
| 321 | r2 = vqtbl1q_u8 (r2, aese_prep_mask1); |
| 322 | r2 = vaeseq_u8 (r2, z2); |
| 323 | r2 ^= (u8x16) vdupq_n_u32 (rcon); |
| 324 | |
| 325 | r = (u8x8) vdup_laneq_u64 ((u64x2) r2, 0); |
| 326 | r ^= rk[-3]; |
| 327 | r ^= vext_u8 (z, rk[-3], 4); |
| 328 | rk[0] = r; |
| 329 | |
| 330 | r = rk[-2] ^ vext_u8 (r, z, 4); |
| 331 | r ^= vext_u8 (z, r, 4); |
| 332 | rk[1] = r; |
| 333 | |
| 334 | if (rcon == 0x80) |
| 335 | return; |
| 336 | |
| 337 | r = rk[-1] ^ vext_u8 (r, z, 4); |
| 338 | r ^= vext_u8 (z, r, 4); |
| 339 | rk[2] = r; |
| 340 | } |
| 341 | |
Damjan Marion | 4fe44af | 2020-02-12 18:24:24 +0100 | [diff] [blame] | 342 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 343 | aes192_key_expand (u8x16 * ek, const u8x16u * k) |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 344 | { |
| 345 | u8x8 *rk = (u8x8 *) ek; |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 346 | ek[0] = k[0]; |
| 347 | rk[2] = *(u8x8u *) (k + 1); |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 348 | aes192_key_expand_round_neon (rk + 3, 0x01); |
| 349 | aes192_key_expand_round_neon (rk + 6, 0x02); |
| 350 | aes192_key_expand_round_neon (rk + 9, 0x04); |
| 351 | aes192_key_expand_round_neon (rk + 12, 0x08); |
| 352 | aes192_key_expand_round_neon (rk + 15, 0x10); |
| 353 | aes192_key_expand_round_neon (rk + 18, 0x20); |
| 354 | aes192_key_expand_round_neon (rk + 21, 0x40); |
| 355 | aes192_key_expand_round_neon (rk + 24, 0x80); |
| 356 | } |
| 357 | |
| 358 | |
Damjan Marion | 4fe44af | 2020-02-12 18:24:24 +0100 | [diff] [blame] | 359 | static_always_inline void |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 360 | aes256_key_expand_round_neon (u8x16 * rk, u32 rcon) |
| 361 | { |
| 362 | u8x16 r, t, z = { }; |
| 363 | |
| 364 | r = vqtbl1q_u8 (rk[-1], rcon ? aese_prep_mask1 : aese_prep_mask2); |
| 365 | r = vaeseq_u8 (r, z); |
| 366 | if (rcon) |
| 367 | r ^= (u8x16) vdupq_n_u32 (rcon); |
| 368 | r ^= rk[-2]; |
| 369 | r ^= t = vextq_u8 (z, rk[-2], 12); |
| 370 | r ^= t = vextq_u8 (z, t, 12); |
| 371 | r ^= vextq_u8 (z, t, 12); |
| 372 | rk[0] = r; |
| 373 | } |
| 374 | |
Damjan Marion | 4fe44af | 2020-02-12 18:24:24 +0100 | [diff] [blame] | 375 | static_always_inline void |
Jieqiang Wang | 83b982b | 2021-11-29 14:25:03 +0000 | [diff] [blame] | 376 | aes256_key_expand (u8x16 *rk, u8x16u const *k) |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 377 | { |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 378 | rk[0] = k[0]; |
| 379 | rk[1] = k[1]; |
Damjan Marion | 776644e | 2020-01-31 10:24:07 +0100 | [diff] [blame] | 380 | aes256_key_expand_round_neon (rk + 2, 0x01); |
| 381 | aes256_key_expand_round_neon (rk + 3, 0); |
| 382 | aes256_key_expand_round_neon (rk + 4, 0x02); |
| 383 | aes256_key_expand_round_neon (rk + 5, 0); |
| 384 | aes256_key_expand_round_neon (rk + 6, 0x04); |
| 385 | aes256_key_expand_round_neon (rk + 7, 0); |
| 386 | aes256_key_expand_round_neon (rk + 8, 0x08); |
| 387 | aes256_key_expand_round_neon (rk + 9, 0); |
| 388 | aes256_key_expand_round_neon (rk + 10, 0x10); |
| 389 | aes256_key_expand_round_neon (rk + 11, 0); |
| 390 | aes256_key_expand_round_neon (rk + 12, 0x20); |
| 391 | aes256_key_expand_round_neon (rk + 13, 0); |
| 392 | aes256_key_expand_round_neon (rk + 14, 0x40); |
| 393 | } |
| 394 | |
| 395 | #endif |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 396 | |
| 397 | static_always_inline void |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 398 | aes_key_expand (u8x16 * key_schedule, u8 const *key, aes_key_size_t ks) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 399 | { |
| 400 | switch (ks) |
| 401 | { |
Damjan Marion | 7d08e39 | 2020-01-28 09:55:25 +0100 | [diff] [blame] | 402 | case AES_KEY_128: |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 403 | aes128_key_expand (key_schedule, (u8x16u const *) key); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 404 | break; |
Damjan Marion | 7d08e39 | 2020-01-28 09:55:25 +0100 | [diff] [blame] | 405 | case AES_KEY_192: |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 406 | aes192_key_expand (key_schedule, (u8x16u const *) key); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 407 | break; |
Damjan Marion | 7d08e39 | 2020-01-28 09:55:25 +0100 | [diff] [blame] | 408 | case AES_KEY_256: |
Damjan Marion | 415b4b0 | 2020-02-11 17:04:38 +0100 | [diff] [blame] | 409 | aes256_key_expand (key_schedule, (u8x16u const *) key); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | } |
| 413 | |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 414 | static_always_inline void |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 415 | aes_key_enc_to_dec (u8x16 * ke, u8x16 * kd, aes_key_size_t ks) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 416 | { |
Damjan Marion | 7d08e39 | 2020-01-28 09:55:25 +0100 | [diff] [blame] | 417 | int rounds = AES_KEY_ROUNDS (ks); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 418 | |
Damjan Marion | 78b58f6 | 2020-01-29 10:31:26 +0100 | [diff] [blame] | 419 | kd[rounds] = ke[0]; |
| 420 | kd[0] = ke[rounds]; |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 421 | |
| 422 | for (int i = 1; i < (rounds / 2); i++) |
| 423 | { |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 424 | kd[rounds - i] = aes_inv_mix_column (ke[i]); |
| 425 | kd[i] = aes_inv_mix_column (ke[rounds - i]); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Damjan Marion | 93975e6 | 2020-01-30 15:46:23 +0100 | [diff] [blame] | 428 | kd[rounds / 2] = aes_inv_mix_column (ke[rounds / 2]); |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 429 | } |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 430 | #if defined(__VAES__) && defined(__AVX512F__) |
| 431 | #define N_AES_LANES 4 |
| 432 | #define aes_load_partial(p, n) u8x64_load_partial ((u8 *) (p), n) |
| 433 | #define aes_store_partial(v, p, n) u8x64_store_partial (v, (u8 *) (p), n) |
| 434 | #define aes_reflect(r) u8x64_reflect_u8x16 (r) |
| 435 | typedef u8x64 aes_data_t; |
| 436 | typedef u8x64u aes_mem_t; |
| 437 | typedef u32x16 aes_counter_t; |
| 438 | #elif defined(__VAES__) |
| 439 | #define N_AES_LANES 2 |
| 440 | #define aes_load_partial(p, n) u8x32_load_partial ((u8 *) (p), n) |
| 441 | #define aes_store_partial(v, p, n) u8x32_store_partial (v, (u8 *) (p), n) |
| 442 | #define aes_reflect(r) u8x32_reflect_u8x16 (r) |
| 443 | typedef u8x32 aes_data_t; |
| 444 | typedef u8x32u aes_mem_t; |
| 445 | typedef u32x8 aes_counter_t; |
| 446 | #else |
| 447 | #define N_AES_LANES 1 |
| 448 | #define aes_load_partial(p, n) u8x16_load_partial ((u8 *) (p), n) |
| 449 | #define aes_store_partial(v, p, n) u8x16_store_partial (v, (u8 *) (p), n) |
| 450 | #define aes_reflect(r) u8x16_reflect (r) |
| 451 | typedef u8x16 aes_data_t; |
| 452 | typedef u8x16u aes_mem_t; |
| 453 | typedef u32x4 aes_counter_t; |
| 454 | #endif |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 455 | |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 456 | #define N_AES_BYTES (N_AES_LANES * 16) |
Damjan Marion | deb8af6 | 2019-04-02 19:06:50 +0200 | [diff] [blame] | 457 | |
Damjan Marion | 9caef2a | 2024-01-08 19:05:40 +0000 | [diff] [blame] | 458 | typedef union |
| 459 | { |
| 460 | u8x16 x1; |
| 461 | u8x32 x2; |
| 462 | u8x64 x4; |
| 463 | u8x16 lanes[4]; |
| 464 | } aes_expaned_key_t; |
| 465 | |
| 466 | static_always_inline void |
| 467 | aes_enc_round (aes_data_t *r, const aes_expaned_key_t *ek, uword n_blocks) |
| 468 | { |
| 469 | for (int i = 0; i < n_blocks; i++) |
| 470 | #if N_AES_LANES == 4 |
| 471 | r[i] = aes_enc_round_x4 (r[i], ek->x4); |
| 472 | #elif N_AES_LANES == 2 |
| 473 | r[i] = aes_enc_round_x2 (r[i], ek->x2); |
| 474 | #else |
| 475 | r[i] = aes_enc_round_x1 (r[i], ek->x1); |
| 476 | #endif |
| 477 | } |
| 478 | |
| 479 | static_always_inline void |
| 480 | aes_enc_last_round (aes_data_t *r, aes_data_t *d, const aes_expaned_key_t *ek, |
| 481 | uword n_blocks) |
| 482 | { |
| 483 | for (int i = 0; i < n_blocks; i++) |
| 484 | #if N_AES_LANES == 4 |
| 485 | d[i] ^= r[i] = aes_enc_last_round_x4 (r[i], ek->x4); |
| 486 | #elif N_AES_LANES == 2 |
| 487 | d[i] ^= r[i] = aes_enc_last_round_x2 (r[i], ek->x2); |
| 488 | #else |
| 489 | d[i] ^= r[i] = aes_enc_last_round_x1 (r[i], ek->x1); |
| 490 | #endif |
| 491 | } |
| 492 | |
| 493 | #endif /* __aes_h__ */ |