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