Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AES-NI support functions |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Gilles Peskine | d4f31c8 | 2023-03-10 22:21:47 +0100 | [diff] [blame] | 21 | * [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html |
| 22 | * [CLMUL-WP] https://www.intel.com/content/www/us/en/develop/download/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode.html |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 23 | */ |
| 24 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 25 | #include "common.h" |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_AESNI_C) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 28 | |
Chris Jones | 187782f | 2021-03-09 17:28:35 +0000 | [diff] [blame] | 29 | #include "aesni.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | |
| 31 | #include <string.h> |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 32 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 33 | #if defined(MBEDTLS_AESNI_HAVE_CODE) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 34 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 35 | #if MBEDTLS_AESNI_HAVE_CODE == 2 |
Pengyu Lv | 0ecb635 | 2023-10-11 10:36:55 +0800 | [diff] [blame] | 36 | #if defined(__GNUC__) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 37 | #include <cpuid.h> |
Pengyu Lv | 0ecb635 | 2023-10-11 10:36:55 +0800 | [diff] [blame] | 38 | #elif defined(_MSC_VER) |
SlugFiller | 5ca3f0b | 2023-05-22 06:31:45 +0300 | [diff] [blame] | 39 | #include <intrin.h> |
Pengyu Lv | 0ecb635 | 2023-10-11 10:36:55 +0800 | [diff] [blame] | 40 | #else |
| 41 | #error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler" |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 42 | #endif |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 43 | #include <immintrin.h> |
| 44 | #endif |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 45 | |
Beniamin Sandu | 800f2b7 | 2023-10-27 16:58:00 +0100 | [diff] [blame^] | 46 | #if defined(MBEDTLS_ARCH_IS_X86) |
| 47 | #if defined(MBEDTLS_COMPILER_IS_GCC) |
| 48 | #pragma GCC push_options |
| 49 | #pragma GCC target ("pclmul,sse2,aes") |
| 50 | #define MBEDTLS_POP_TARGET_PRAGMA |
| 51 | #elif defined(__clang__) |
| 52 | #pragma clang attribute push (__attribute__((target("pclmul,sse2,aes"))), apply_to=function) |
| 53 | #define MBEDTLS_POP_TARGET_PRAGMA |
| 54 | #endif |
| 55 | #endif |
| 56 | |
Jerry Yu | 0a6272d | 2023-08-18 17:35:59 +0800 | [diff] [blame] | 57 | #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 58 | /* |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 59 | * AES-NI support detection routine |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 60 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | int mbedtls_aesni_has_support(unsigned int what) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 62 | { |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 63 | static int done = 0; |
| 64 | static unsigned int c = 0; |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | if (!done) { |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 67 | #if MBEDTLS_AESNI_HAVE_CODE == 2 |
Pengyu Lv | e8c4bf1 | 2023-10-10 18:12:43 +0800 | [diff] [blame] | 68 | static int info[4] = { 0, 0, 0, 0 }; |
Pengyu Lv | 0ecb635 | 2023-10-11 10:36:55 +0800 | [diff] [blame] | 69 | #if defined(_MSC_VER) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 70 | __cpuid(info, 1); |
| 71 | #else |
| 72 | __cpuid(1, info[0], info[1], info[2], info[3]); |
| 73 | #endif |
| 74 | c = info[2]; |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 75 | #else /* AESNI using asm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | asm ("movl $1, %%eax \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 77 | "cpuid \n\t" |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 78 | : "=c" (c) |
| 79 | : |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | : "eax", "ebx", "edx"); |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 81 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 82 | done = 1; |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | return (c & what) != 0; |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 86 | } |
Jerry Yu | 3660623 | 2023-04-19 10:44:29 +0800 | [diff] [blame] | 87 | #endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 88 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 89 | #if MBEDTLS_AESNI_HAVE_CODE == 2 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * AES-NI AES-ECB block en(de)cryption |
| 93 | */ |
| 94 | int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, |
| 95 | int mode, |
| 96 | const unsigned char input[16], |
| 97 | unsigned char output[16]) |
| 98 | { |
| 99 | const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset); |
| 100 | unsigned nr = ctx->nr; // Number of remaining rounds |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 101 | |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 102 | // Load round key 0 |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 103 | __m128i state; |
| 104 | memcpy(&state, input, 16); |
| 105 | state = _mm_xor_si128(state, rk[0]); // state ^= *rk; |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 106 | ++rk; |
| 107 | --nr; |
| 108 | |
| 109 | if (mode == 0) { |
| 110 | while (nr != 0) { |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 111 | state = _mm_aesdec_si128(state, *rk); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 112 | ++rk; |
| 113 | --nr; |
| 114 | } |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 115 | state = _mm_aesdeclast_si128(state, *rk); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 116 | } else { |
| 117 | while (nr != 0) { |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 118 | state = _mm_aesenc_si128(state, *rk); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 119 | ++rk; |
| 120 | --nr; |
| 121 | } |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 122 | state = _mm_aesenclast_si128(state, *rk); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 125 | memcpy(output, &state, 16); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * GCM multiplication: c = a times b in GF(2^128) |
| 131 | * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5. |
| 132 | */ |
| 133 | |
| 134 | static void gcm_clmul(const __m128i aa, const __m128i bb, |
| 135 | __m128i *cc, __m128i *dd) |
| 136 | { |
| 137 | /* |
| 138 | * Caryless multiplication dd:cc = aa * bb |
| 139 | * using [CLMUL-WP] algorithm 1 (p. 12). |
| 140 | */ |
| 141 | *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0 |
| 142 | *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0 |
| 143 | __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0 |
| 144 | __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0 |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 145 | ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 146 | ee = ff; // e1+f1:e0+f0 |
| 147 | ff = _mm_srli_si128(ff, 8); // 0:e1+f1 |
| 148 | ee = _mm_slli_si128(ee, 8); // e0+f0:0 |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 149 | *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1 |
Gilles Peskine | d0185f7 | 2023-03-16 13:08:18 +0100 | [diff] [blame] | 150 | *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void gcm_shift(__m128i *cc, __m128i *dd) |
| 154 | { |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 155 | /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left, |
| 156 | * taking advantage of [CLMUL-WP] eq 27 (p. 18). */ |
| 157 | // // *cc = r1:r0 |
| 158 | // // *dd = r3:r2 |
| 159 | __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1 |
| 160 | __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1 |
| 161 | __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63 |
| 162 | __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63 |
| 163 | __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63 |
| 164 | cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0 |
| 165 | dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 166 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 167 | *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1 |
| 168 | *dd = _mm_or_si128(_mm_or_si128(dd_lo, dd_hi), xmm5); // r3<<1|r2>>62:r2<<1|r1>>63 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 171 | static __m128i gcm_reduce(__m128i xx) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 172 | { |
| 173 | // // xx = x1:x0 |
| 174 | /* [CLMUL-WP] Algorithm 5 Step 2 */ |
| 175 | __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a |
| 176 | __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b |
| 177 | __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 178 | __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0 |
| 179 | return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 182 | static __m128i gcm_mix(__m128i dx) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 183 | { |
| 184 | /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */ |
| 185 | __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0' |
| 186 | __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0' |
| 187 | __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0' |
| 188 | |
| 189 | // e0'+f0'+g0' is almost e0+f0+g0, except for some missing |
| 190 | // bits carried from d. Now get those bits back in. |
| 191 | __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff |
| 192 | __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff |
| 193 | __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 194 | __m128i hh = _mm_srli_si128(_mm_xor_si128(_mm_xor_si128(eh, fh), gh), 8); // 0:missing bits of d |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 195 | |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 196 | return _mm_xor_si128(_mm_xor_si128(_mm_xor_si128(_mm_xor_si128(ee, ff), gg), hh), dx); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 200 | const unsigned char a[16], |
| 201 | const unsigned char b[16]) |
| 202 | { |
Sergey Markelov | 3898f10 | 2023-10-16 12:54:48 -0700 | [diff] [blame] | 203 | __m128i aa = { 0 }, bb = { 0 }, cc, dd; |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 204 | |
| 205 | /* The inputs are in big-endian order, so byte-reverse them */ |
| 206 | for (size_t i = 0; i < 16; i++) { |
| 207 | ((uint8_t *) &aa)[i] = a[15 - i]; |
| 208 | ((uint8_t *) &bb)[i] = b[15 - i]; |
| 209 | } |
| 210 | |
| 211 | gcm_clmul(aa, bb, &cc, &dd); |
| 212 | gcm_shift(&cc, &dd); |
| 213 | /* |
| 214 | * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1 |
| 215 | * using [CLMUL-WP] algorithm 5 (p. 18). |
| 216 | * Currently dd:cc holds x3:x2:x1:x0 (already shifted). |
| 217 | */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 218 | __m128i dx = gcm_reduce(cc); |
| 219 | __m128i xh = gcm_mix(dx); |
Tom Cosgrove | 02edb75 | 2023-03-13 15:32:52 +0000 | [diff] [blame] | 220 | cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 221 | |
| 222 | /* Now byte-reverse the outputs */ |
| 223 | for (size_t i = 0; i < 16; i++) { |
| 224 | c[i] = ((uint8_t *) &cc)[15 - i]; |
| 225 | } |
| 226 | |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Compute decryption round keys from encryption round keys |
| 232 | */ |
| 233 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 234 | const unsigned char *fwdkey, int nr) |
| 235 | { |
| 236 | __m128i *ik = (__m128i *) invkey; |
| 237 | const __m128i *fk = (const __m128i *) fwdkey + nr; |
| 238 | |
| 239 | *ik = *fk; |
| 240 | for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) { |
| 241 | *ik = _mm_aesimc_si128(*fk); |
| 242 | } |
| 243 | *ik = *fk; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Key expansion, 128-bit case |
| 248 | */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 249 | static __m128i aesni_set_rk_128(__m128i state, __m128i xword) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 250 | { |
| 251 | /* |
| 252 | * Finish generating the next round key. |
| 253 | * |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 254 | * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff |
| 255 | * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST). |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 256 | * |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 257 | * On exit, xword is r7:r6:r5:r4 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 258 | * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3 |
| 259 | * and this is returned, to be written to the round key buffer. |
| 260 | */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 261 | xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X |
| 262 | xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4 |
| 263 | state = _mm_slli_si128(state, 4); // r2:r1:r0:0 |
| 264 | xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4 |
| 265 | state = _mm_slli_si128(state, 4); // r1:r0:0:0 |
| 266 | xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4 |
| 267 | state = _mm_slli_si128(state, 4); // r0:0:0:0 |
| 268 | state = _mm_xor_si128(xword, state); // r7:r6:r5:r4 |
| 269 | return state; |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void aesni_setkey_enc_128(unsigned char *rk_bytes, |
| 273 | const unsigned char *key) |
| 274 | { |
| 275 | __m128i *rk = (__m128i *) rk_bytes; |
| 276 | |
| 277 | memcpy(&rk[0], key, 16); |
| 278 | rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01)); |
| 279 | rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02)); |
| 280 | rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04)); |
| 281 | rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08)); |
| 282 | rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10)); |
| 283 | rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20)); |
| 284 | rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40)); |
| 285 | rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80)); |
| 286 | rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B)); |
| 287 | rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36)); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Key expansion, 192-bit case |
| 292 | */ |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 293 | #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 294 | static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword, |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 295 | unsigned char *rk) |
| 296 | { |
| 297 | /* |
| 298 | * Finish generating the next 6 quarter-keys. |
| 299 | * |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 300 | * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4 |
| 301 | * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON |
| 302 | * (obtained with AESKEYGENASSIST). |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 303 | * |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 304 | * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 305 | * and those are written to the round key buffer. |
| 306 | */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 307 | xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X |
| 308 | xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0 |
| 309 | *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0 |
| 310 | xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0 |
| 311 | *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0 |
| 312 | xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0 |
| 313 | *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0 |
| 314 | xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0 |
| 315 | *state0 = xword; // = r9:r8:r7:r6 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 316 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 317 | xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9 |
| 318 | xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4 |
| 319 | *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0 |
| 320 | xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4 |
| 321 | *state1 = xword; // = stuff:stuff:r11:r10 |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 322 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 323 | /* Store state0 and the low half of state1 into rk, which is conceptually |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 324 | * an array of 24-byte elements. Since 24 is not a multiple of 16, |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 325 | * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */ |
| 326 | memcpy(rk, state0, 16); |
Gilles Peskine | dde3c65 | 2023-03-15 23:16:27 +0100 | [diff] [blame] | 327 | memcpy(rk + 16, state1, 8); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void aesni_setkey_enc_192(unsigned char *rk, |
| 331 | const unsigned char *key) |
| 332 | { |
| 333 | /* First round: use original key */ |
| 334 | memcpy(rk, key, 24); |
| 335 | /* aes.c guarantees that rk is aligned on a 16-byte boundary. */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 336 | __m128i state0 = ((__m128i *) rk)[0]; |
| 337 | __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 338 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 339 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1); |
| 340 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2); |
| 341 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3); |
| 342 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4); |
| 343 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5); |
| 344 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6); |
| 345 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7); |
| 346 | aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8); |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 347 | } |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 348 | #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */ |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * Key expansion, 256-bit case |
| 352 | */ |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 353 | #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 354 | static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword, |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 355 | __m128i *rk0, __m128i *rk1) |
| 356 | { |
| 357 | /* |
| 358 | * Finish generating the next two round keys. |
| 359 | * |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 360 | * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and |
| 361 | * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON |
| 362 | * (obtained with AESKEYGENASSIST). |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 363 | * |
| 364 | * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12 |
| 365 | */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 366 | xword = _mm_shuffle_epi32(xword, 0xff); |
| 367 | xword = _mm_xor_si128(xword, state0); |
| 368 | state0 = _mm_slli_si128(state0, 4); |
| 369 | xword = _mm_xor_si128(xword, state0); |
| 370 | state0 = _mm_slli_si128(state0, 4); |
| 371 | xword = _mm_xor_si128(xword, state0); |
| 372 | state0 = _mm_slli_si128(state0, 4); |
| 373 | state0 = _mm_xor_si128(state0, xword); |
| 374 | *rk0 = state0; |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 375 | |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 376 | /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 ) |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 377 | * and proceed to generate next round key from there */ |
Gilles Peskine | dafeee4 | 2023-03-15 20:37:57 +0100 | [diff] [blame] | 378 | xword = _mm_aeskeygenassist_si128(state0, 0x00); |
| 379 | xword = _mm_shuffle_epi32(xword, 0xaa); |
| 380 | xword = _mm_xor_si128(xword, state1); |
| 381 | state1 = _mm_slli_si128(state1, 4); |
| 382 | xword = _mm_xor_si128(xword, state1); |
| 383 | state1 = _mm_slli_si128(state1, 4); |
| 384 | xword = _mm_xor_si128(xword, state1); |
| 385 | state1 = _mm_slli_si128(state1, 4); |
| 386 | state1 = _mm_xor_si128(state1, xword); |
| 387 | *rk1 = state1; |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void aesni_setkey_enc_256(unsigned char *rk_bytes, |
| 391 | const unsigned char *key) |
| 392 | { |
| 393 | __m128i *rk = (__m128i *) rk_bytes; |
| 394 | |
| 395 | memcpy(&rk[0], key, 16); |
| 396 | memcpy(&rk[1], key + 16, 16); |
| 397 | |
| 398 | /* |
| 399 | * Main "loop" - Generating one more key than necessary, |
| 400 | * see definition of mbedtls_aes_context.buf |
| 401 | */ |
| 402 | aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]); |
| 403 | aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]); |
| 404 | aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]); |
| 405 | aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]); |
| 406 | aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]); |
| 407 | aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]); |
| 408 | aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]); |
| 409 | } |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 410 | #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */ |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 411 | |
Beniamin Sandu | 800f2b7 | 2023-10-27 16:58:00 +0100 | [diff] [blame^] | 412 | #if defined(MBEDTLS_POP_TARGET_PRAGMA) |
| 413 | #if defined(__clang__) |
| 414 | #pragma clang attribute pop |
| 415 | #elif defined(__GNUC__) |
| 416 | #pragma GCC pop_options |
| 417 | #endif |
| 418 | #undef MBEDTLS_POP_TARGET_PRAGMA |
| 419 | #endif |
| 420 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 421 | #else /* MBEDTLS_AESNI_HAVE_CODE == 1 */ |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 422 | |
Gilles Peskine | d0f9b0b | 2023-03-10 22:25:13 +0100 | [diff] [blame] | 423 | #if defined(__has_feature) |
| 424 | #if __has_feature(memory_sanitizer) |
| 425 | #warning \ |
| 426 | "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code." |
| 427 | #endif |
| 428 | #endif |
| 429 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 430 | /* |
Manuel Pégourié-Gonnard | b1fd397 | 2014-04-26 17:17:31 +0200 | [diff] [blame] | 431 | * Binutils needs to be at least 2.19 to support AES-NI instructions. |
| 432 | * Unfortunately, a lot of users have a lower version now (2014-04). |
| 433 | * Emit bytecode directly in order to support "old" version of gas. |
| 434 | * |
| 435 | * Opcodes from the Intel architecture reference manual, vol. 3. |
| 436 | * We always use registers, so we don't need prefixes for memory operands. |
| 437 | * Operand macros are in gas order (src, dst) as opposed to Intel order |
| 438 | * (dst, src) in order to blend better into the surrounding assembly code. |
| 439 | */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 440 | #define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t" |
| 441 | #define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t" |
| 442 | #define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t" |
| 443 | #define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t" |
| 444 | #define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t" |
| 445 | #define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t" |
| 446 | #define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t" |
Manuel Pégourié-Gonnard | b1fd397 | 2014-04-26 17:17:31 +0200 | [diff] [blame] | 447 | |
| 448 | #define xmm0_xmm0 "0xC0" |
| 449 | #define xmm0_xmm1 "0xC8" |
| 450 | #define xmm0_xmm2 "0xD0" |
| 451 | #define xmm0_xmm3 "0xD8" |
| 452 | #define xmm0_xmm4 "0xE0" |
| 453 | #define xmm1_xmm0 "0xC1" |
| 454 | #define xmm1_xmm2 "0xD1" |
| 455 | |
| 456 | /* |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 457 | * AES-NI AES-ECB block en(de)cryption |
| 458 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, |
| 460 | int mode, |
| 461 | const unsigned char input[16], |
| 462 | unsigned char output[16]) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 463 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | asm ("movdqu (%3), %%xmm0 \n\t" // load input |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 465 | "movdqu (%1), %%xmm1 \n\t" // load round key 0 |
| 466 | "pxor %%xmm1, %%xmm0 \n\t" // round 0 |
James Cowgill | 6c8edca | 2015-12-17 01:40:26 +0000 | [diff] [blame] | 467 | "add $16, %1 \n\t" // point to next round key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 468 | "subl $1, %0 \n\t" // normal rounds = nr - 1 |
| 469 | "test %2, %2 \n\t" // mode? |
| 470 | "jz 2f \n\t" // 0 = decrypt |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 471 | |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 472 | "1: \n\t" // encryption loop |
| 473 | "movdqu (%1), %%xmm1 \n\t" // load round key |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 474 | AESENC(xmm1_xmm0) // do round |
| 475 | "add $16, %1 \n\t" // point to next round key |
| 476 | "subl $1, %0 \n\t" // loop |
| 477 | "jnz 1b \n\t" |
| 478 | "movdqu (%1), %%xmm1 \n\t" // load round key |
| 479 | AESENCLAST(xmm1_xmm0) // last round |
| 480 | "jmp 3f \n\t" |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 481 | |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 482 | "2: \n\t" // decryption loop |
| 483 | "movdqu (%1), %%xmm1 \n\t" |
| 484 | AESDEC(xmm1_xmm0) // do round |
| 485 | "add $16, %1 \n\t" |
| 486 | "subl $1, %0 \n\t" |
| 487 | "jnz 2b \n\t" |
| 488 | "movdqu (%1), %%xmm1 \n\t" // load round key |
| 489 | AESDECLAST(xmm1_xmm0) // last round |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 490 | |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 491 | "3: \n\t" |
| 492 | "movdqu %%xmm0, (%4) \n\t" // export output |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 493 | : |
Werner Lewis | dd76ef3 | 2022-05-30 12:00:21 +0100 | [diff] [blame] | 494 | : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | : "memory", "cc", "xmm0", "xmm1"); |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 496 | |
| 497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | return 0; |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 499 | } |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * GCM multiplication: c = a times b in GF(2^128) |
| 503 | * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5. |
| 504 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 506 | const unsigned char a[16], |
| 507 | const unsigned char b[16]) |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 508 | { |
| 509 | unsigned char aa[16], bb[16], cc[16]; |
| 510 | size_t i; |
| 511 | |
| 512 | /* The inputs are in big-endian order, so byte-reverse them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | for (i = 0; i < 16; i++) { |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 514 | aa[i] = a[15 - i]; |
| 515 | bb[i] = b[15 - i]; |
| 516 | } |
| 517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0 |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 519 | "movdqu (%1), %%xmm1 \n\t" // b1:b0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1 |
Gilles Peskine | d4f31c8 | 2023-03-10 22:21:47 +0100 | [diff] [blame] | 523 | * using [CLMUL-WP] algorithm 1 (p. 12). |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 524 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 525 | "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0 |
| 526 | "movdqa %%xmm1, %%xmm3 \n\t" // same |
| 527 | "movdqa %%xmm1, %%xmm4 \n\t" // same |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 528 | PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0 |
| 529 | PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0 |
| 530 | PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0 |
| 531 | PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0 |
| 532 | "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0 |
| 533 | "movdqa %%xmm4, %%xmm3 \n\t" // same |
| 534 | "psrldq $8, %%xmm4 \n\t" // 0:e1+f1 |
| 535 | "pslldq $8, %%xmm3 \n\t" // e0+f0:0 |
| 536 | "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1 |
| 537 | "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * Now shift the result one bit to the left, |
Gilles Peskine | d4f31c8 | 2023-03-10 22:21:47 +0100 | [diff] [blame] | 541 | * taking advantage of [CLMUL-WP] eq 27 (p. 18) |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 542 | */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 543 | "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0 |
| 544 | "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2 |
| 545 | "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1 |
| 546 | "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1 |
| 547 | "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63 |
| 548 | "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63 |
| 549 | "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63 |
| 550 | "pslldq $8, %%xmm3 \n\t" // r0>>63:0 |
| 551 | "pslldq $8, %%xmm4 \n\t" // r2>>63:0 |
| 552 | "psrldq $8, %%xmm5 \n\t" // 0:r1>>63 |
| 553 | "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1 |
| 554 | "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1 |
| 555 | "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1 |
Gilles Peskine | d4f31c8 | 2023-03-10 22:21:47 +0100 | [diff] [blame] | 559 | * using [CLMUL-WP] algorithm 5 (p. 18). |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 560 | * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted). |
| 561 | */ |
| 562 | /* Step 2 (1) */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 563 | "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0 |
| 564 | "movdqa %%xmm1, %%xmm4 \n\t" // same |
| 565 | "movdqa %%xmm1, %%xmm5 \n\t" // same |
| 566 | "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a |
| 567 | "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b |
| 568 | "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 569 | |
| 570 | /* Step 2 (2) */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 571 | "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b |
| 572 | "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c |
| 573 | "pslldq $8, %%xmm3 \n\t" // a+b+c:0 |
| 574 | "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 575 | |
| 576 | /* Steps 3 and 4 */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 577 | "movdqa %%xmm1,%%xmm0 \n\t" // d:x0 |
| 578 | "movdqa %%xmm1,%%xmm4 \n\t" // same |
| 579 | "movdqa %%xmm1,%%xmm5 \n\t" // same |
| 580 | "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0' |
| 581 | "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0' |
| 582 | "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0' |
| 583 | "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0' |
| 584 | "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0' |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 585 | // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing |
| 586 | // bits carried from d. Now get those\t bits back in. |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 587 | "movdqa %%xmm1,%%xmm3 \n\t" // d:x0 |
| 588 | "movdqa %%xmm1,%%xmm4 \n\t" // same |
| 589 | "movdqa %%xmm1,%%xmm5 \n\t" // same |
| 590 | "psllq $63, %%xmm3 \n\t" // d<<63:stuff |
| 591 | "psllq $62, %%xmm4 \n\t" // d<<62:stuff |
| 592 | "psllq $57, %%xmm5 \n\t" // d<<57:stuff |
| 593 | "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff |
| 594 | "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff |
| 595 | "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d |
| 596 | "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0 |
| 597 | "pxor %%xmm1, %%xmm0 \n\t" // h1:h0 |
| 598 | "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 599 | |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 600 | "movdqu %%xmm0, (%2) \n\t" // done |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 601 | : |
| 602 | : "r" (aa), "r" (bb), "r" (cc) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 603 | : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"); |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 604 | |
| 605 | /* Now byte-reverse the outputs */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | for (i = 0; i < 16; i++) { |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 607 | c[i] = cc[15 - i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | } |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 609 | |
Paul Bakker | e7f5133 | 2013-12-30 15:32:02 +0100 | [diff] [blame] | 610 | return; |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 613 | /* |
| 614 | * Compute decryption round keys from encryption round keys |
| 615 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 617 | const unsigned char *fwdkey, int nr) |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 618 | { |
| 619 | unsigned char *ik = invkey; |
| 620 | const unsigned char *fk = fwdkey + 16 * nr; |
| 621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | memcpy(ik, fk, 16); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) { |
| 625 | asm ("movdqu (%0), %%xmm0 \n\t" |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 626 | AESIMC(xmm0_xmm0) |
| 627 | "movdqu %%xmm0, (%1) \n\t" |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 628 | : |
| 629 | : "r" (fk), "r" (ik) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 630 | : "memory", "xmm0"); |
| 631 | } |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | memcpy(ik, fk, 16); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 634 | } |
| 635 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 636 | /* |
| 637 | * Key expansion, 128-bit case |
| 638 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | static void aesni_setkey_enc_128(unsigned char *rk, |
| 640 | const unsigned char *key) |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 641 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 643 | "movdqu %%xmm0, (%0) \n\t" // as round key 0 |
| 644 | "jmp 2f \n\t" // skip auxiliary routine |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 645 | |
| 646 | /* |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 647 | * Finish generating the next round key. |
| 648 | * |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 649 | * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff |
| 650 | * with X = rot( sub( r3 ) ) ^ RCON. |
| 651 | * |
| 652 | * On exit, xmm0 is r7:r6:r5:r4 |
| 653 | * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3 |
| 654 | * and those are written to the round key buffer. |
| 655 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 656 | "1: \n\t" |
| 657 | "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X |
| 658 | "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4 |
| 659 | "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0 |
| 660 | "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4 |
| 661 | "pslldq $4, %%xmm0 \n\t" // etc |
| 662 | "pxor %%xmm0, %%xmm1 \n\t" |
| 663 | "pslldq $4, %%xmm0 \n\t" |
| 664 | "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time! |
| 665 | "add $16, %0 \n\t" // point to next round key |
| 666 | "movdqu %%xmm0, (%0) \n\t" // write it |
| 667 | "ret \n\t" |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 668 | |
| 669 | /* Main "loop" */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 670 | "2: \n\t" |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 671 | AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t" |
| 672 | AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t" |
| 673 | AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t" |
| 674 | AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t" |
| 675 | AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t" |
| 676 | AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t" |
| 677 | AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t" |
| 678 | AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t" |
| 679 | AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t" |
| 680 | AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t" |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 681 | : |
| 682 | : "r" (rk), "r" (key) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | : "memory", "cc", "0"); |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 687 | * Key expansion, 192-bit case |
| 688 | */ |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 689 | #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | static void aesni_setkey_enc_192(unsigned char *rk, |
| 691 | const unsigned char *key) |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 692 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 694 | "movdqu %%xmm0, (%0) \n\t" |
| 695 | "add $16, %0 \n\t" |
| 696 | "movq 16(%1), %%xmm1 \n\t" |
| 697 | "movq %%xmm1, (%0) \n\t" |
| 698 | "add $8, %0 \n\t" |
| 699 | "jmp 2f \n\t" // skip auxiliary routine |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * Finish generating the next 6 quarter-keys. |
| 703 | * |
| 704 | * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4 |
| 705 | * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON. |
| 706 | * |
| 707 | * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10 |
| 708 | * and those are written to the round key buffer. |
| 709 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 710 | "1: \n\t" |
| 711 | "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X |
| 712 | "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4 |
| 713 | "pslldq $4, %%xmm0 \n\t" // etc |
| 714 | "pxor %%xmm0, %%xmm2 \n\t" |
| 715 | "pslldq $4, %%xmm0 \n\t" |
| 716 | "pxor %%xmm0, %%xmm2 \n\t" |
| 717 | "pslldq $4, %%xmm0 \n\t" |
| 718 | "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6 |
| 719 | "movdqu %%xmm0, (%0) \n\t" |
| 720 | "add $16, %0 \n\t" |
| 721 | "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9 |
| 722 | "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10 |
| 723 | "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0 |
| 724 | "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10 |
| 725 | "movq %%xmm1, (%0) \n\t" |
| 726 | "add $8, %0 \n\t" |
| 727 | "ret \n\t" |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 728 | |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 729 | "2: \n\t" |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 730 | AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t" |
| 731 | AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t" |
| 732 | AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t" |
| 733 | AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t" |
| 734 | AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t" |
| 735 | AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t" |
| 736 | AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t" |
| 737 | AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t" |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 738 | |
| 739 | : |
| 740 | : "r" (rk), "r" (key) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 741 | : "memory", "cc", "0"); |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 742 | } |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 743 | #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */ |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 744 | |
| 745 | /* |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 746 | * Key expansion, 256-bit case |
| 747 | */ |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 748 | #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | static void aesni_setkey_enc_256(unsigned char *rk, |
| 750 | const unsigned char *key) |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 751 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 752 | asm ("movdqu (%1), %%xmm0 \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 753 | "movdqu %%xmm0, (%0) \n\t" |
| 754 | "add $16, %0 \n\t" |
| 755 | "movdqu 16(%1), %%xmm1 \n\t" |
| 756 | "movdqu %%xmm1, (%0) \n\t" |
| 757 | "jmp 2f \n\t" // skip auxiliary routine |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * Finish generating the next two round keys. |
| 761 | * |
| 762 | * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and |
| 763 | * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON |
| 764 | * |
| 765 | * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12 |
| 766 | * and those have been written to the output buffer. |
| 767 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 768 | "1: \n\t" |
| 769 | "pshufd $0xff, %%xmm2, %%xmm2 \n\t" |
| 770 | "pxor %%xmm0, %%xmm2 \n\t" |
| 771 | "pslldq $4, %%xmm0 \n\t" |
| 772 | "pxor %%xmm0, %%xmm2 \n\t" |
| 773 | "pslldq $4, %%xmm0 \n\t" |
| 774 | "pxor %%xmm0, %%xmm2 \n\t" |
| 775 | "pslldq $4, %%xmm0 \n\t" |
| 776 | "pxor %%xmm2, %%xmm0 \n\t" |
| 777 | "add $16, %0 \n\t" |
| 778 | "movdqu %%xmm0, (%0) \n\t" |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 779 | |
| 780 | /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 ) |
| 781 | * and proceed to generate next round key from there */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 782 | AESKEYGENA(xmm0_xmm2, "0x00") |
| 783 | "pshufd $0xaa, %%xmm2, %%xmm2 \n\t" |
| 784 | "pxor %%xmm1, %%xmm2 \n\t" |
| 785 | "pslldq $4, %%xmm1 \n\t" |
| 786 | "pxor %%xmm1, %%xmm2 \n\t" |
| 787 | "pslldq $4, %%xmm1 \n\t" |
| 788 | "pxor %%xmm1, %%xmm2 \n\t" |
| 789 | "pslldq $4, %%xmm1 \n\t" |
| 790 | "pxor %%xmm2, %%xmm1 \n\t" |
| 791 | "add $16, %0 \n\t" |
| 792 | "movdqu %%xmm1, (%0) \n\t" |
| 793 | "ret \n\t" |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | * Main "loop" - Generating one more key than necessary, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | * see definition of mbedtls_aes_context.buf |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 798 | */ |
Gilles Peskine | 4e20144 | 2023-03-15 19:36:03 +0100 | [diff] [blame] | 799 | "2: \n\t" |
| 800 | AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t" |
| 801 | AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t" |
| 802 | AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t" |
| 803 | AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t" |
| 804 | AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t" |
| 805 | AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t" |
| 806 | AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t" |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 807 | : |
| 808 | : "r" (rk), "r" (key) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | : "memory", "cc", "0"); |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 810 | } |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 811 | #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */ |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 812 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 813 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
Gilles Peskine | d671917 | 2023-03-10 22:37:11 +0100 | [diff] [blame] | 814 | |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 815 | /* |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 816 | * Key expansion, wrapper |
| 817 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 818 | int mbedtls_aesni_setkey_enc(unsigned char *rk, |
| 819 | const unsigned char *key, |
| 820 | size_t bits) |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 821 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 822 | switch (bits) { |
| 823 | case 128: aesni_setkey_enc_128(rk, key); break; |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 824 | #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | case 192: aesni_setkey_enc_192(rk, key); break; |
| 826 | case 256: aesni_setkey_enc_256(rk, key); break; |
Arto Kinnunen | 732ca32 | 2023-04-14 14:26:10 +0800 | [diff] [blame] | 827 | #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | return 0; |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 834 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | #endif /* MBEDTLS_AESNI_C */ |