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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_HAVE_X86_64) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 34 | |
| 35 | /* |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 36 | * AES-NI support detection routine |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 37 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int mbedtls_aesni_has_support(unsigned int what) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 39 | { |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 40 | static int done = 0; |
| 41 | static unsigned int c = 0; |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 42 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | if (!done) { |
| 44 | asm ("movl $1, %%eax \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 45 | "cpuid \n\t" |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 46 | : "=c" (c) |
| 47 | : |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | : "eax", "ebx", "edx"); |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 49 | done = 1; |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | return (c & what) != 0; |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Gilles Peskine | d0f9b0b | 2023-03-10 22:25:13 +0100 | [diff] [blame^] | 55 | #if defined(__has_feature) |
| 56 | #if __has_feature(memory_sanitizer) |
| 57 | #warning \ |
| 58 | "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code." |
| 59 | #endif |
| 60 | #endif |
| 61 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 62 | /* |
Manuel Pégourié-Gonnard | b1fd397 | 2014-04-26 17:17:31 +0200 | [diff] [blame] | 63 | * Binutils needs to be at least 2.19 to support AES-NI instructions. |
| 64 | * Unfortunately, a lot of users have a lower version now (2014-04). |
| 65 | * Emit bytecode directly in order to support "old" version of gas. |
| 66 | * |
| 67 | * Opcodes from the Intel architecture reference manual, vol. 3. |
| 68 | * We always use registers, so we don't need prefixes for memory operands. |
| 69 | * Operand macros are in gas order (src, dst) as opposed to Intel order |
| 70 | * (dst, src) in order to blend better into the surrounding assembly code. |
| 71 | */ |
| 72 | #define AESDEC ".byte 0x66,0x0F,0x38,0xDE," |
| 73 | #define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF," |
| 74 | #define AESENC ".byte 0x66,0x0F,0x38,0xDC," |
| 75 | #define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD," |
| 76 | #define AESIMC ".byte 0x66,0x0F,0x38,0xDB," |
| 77 | #define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF," |
| 78 | #define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44," |
| 79 | |
| 80 | #define xmm0_xmm0 "0xC0" |
| 81 | #define xmm0_xmm1 "0xC8" |
| 82 | #define xmm0_xmm2 "0xD0" |
| 83 | #define xmm0_xmm3 "0xD8" |
| 84 | #define xmm0_xmm4 "0xE0" |
| 85 | #define xmm1_xmm0 "0xC1" |
| 86 | #define xmm1_xmm2 "0xD1" |
| 87 | |
| 88 | /* |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 89 | * AES-NI AES-ECB block en(de)cryption |
| 90 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, |
| 92 | int mode, |
| 93 | const unsigned char input[16], |
| 94 | unsigned char output[16]) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 95 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | asm ("movdqu (%3), %%xmm0 \n\t" // load input |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 97 | "movdqu (%1), %%xmm1 \n\t" // load round key 0 |
| 98 | "pxor %%xmm1, %%xmm0 \n\t" // round 0 |
James Cowgill | 6c8edca | 2015-12-17 01:40:26 +0000 | [diff] [blame] | 99 | "add $16, %1 \n\t" // point to next round key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 100 | "subl $1, %0 \n\t" // normal rounds = nr - 1 |
| 101 | "test %2, %2 \n\t" // mode? |
| 102 | "jz 2f \n\t" // 0 = decrypt |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 104 | "1: \n\t" // encryption loop |
| 105 | "movdqu (%1), %%xmm1 \n\t" // load round key |
| 106 | AESENC xmm1_xmm0 "\n\t" // do round |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | "add $16, %1 \n\t" // point to next round key |
| 108 | "subl $1, %0 \n\t" // loop |
| 109 | "jnz 1b \n\t" |
| 110 | "movdqu (%1), %%xmm1 \n\t" // load round key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 111 | AESENCLAST xmm1_xmm0 "\n\t" // last round |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | "jmp 3f \n\t" |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | "2: \n\t" // decryption loop |
| 115 | "movdqu (%1), %%xmm1 \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 116 | AESDEC xmm1_xmm0 "\n\t" // do round |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | "add $16, %1 \n\t" |
| 118 | "subl $1, %0 \n\t" |
| 119 | "jnz 2b \n\t" |
| 120 | "movdqu (%1), %%xmm1 \n\t" // load round key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 121 | AESDECLAST xmm1_xmm0 "\n\t" // last round |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | "3: \n\t" |
| 124 | "movdqu %%xmm0, (%4) \n\t" // export output |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 125 | : |
Werner Lewis | dd76ef3 | 2022-05-30 12:00:21 +0100 | [diff] [blame] | 126 | : "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] | 127 | : "memory", "cc", "xmm0", "xmm1"); |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 128 | |
| 129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | return 0; |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 131 | } |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * GCM multiplication: c = a times b in GF(2^128) |
| 135 | * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5. |
| 136 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 138 | const unsigned char a[16], |
| 139 | const unsigned char b[16]) |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 140 | { |
| 141 | unsigned char aa[16], bb[16], cc[16]; |
| 142 | size_t i; |
| 143 | |
| 144 | /* The inputs are in big-endian order, so byte-reverse them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | for (i = 0; i < 16; i++) { |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 146 | aa[i] = a[15 - i]; |
| 147 | bb[i] = b[15 - i]; |
| 148 | } |
| 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0 |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 151 | "movdqu (%1), %%xmm1 \n\t" // b1:b0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1 |
Gilles Peskine | d4f31c8 | 2023-03-10 22:21:47 +0100 | [diff] [blame] | 155 | * using [CLMUL-WP] algorithm 1 (p. 12). |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 156 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 157 | "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0 |
| 158 | "movdqa %%xmm1, %%xmm3 \n\t" // same |
| 159 | "movdqa %%xmm1, %%xmm4 \n\t" // same |
| 160 | PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0 |
| 161 | PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0 |
| 162 | PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0 |
| 163 | PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0 |
| 165 | "movdqa %%xmm4, %%xmm3 \n\t" // same |
| 166 | "psrldq $8, %%xmm4 \n\t" // 0:e1+f1 |
| 167 | "pslldq $8, %%xmm3 \n\t" // e0+f0:0 |
| 168 | "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1 |
| 169 | "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Now shift the result one bit to the left, |
Gilles Peskine | d4f31c8 | 2023-03-10 22:21:47 +0100 | [diff] [blame] | 173 | * taking advantage of [CLMUL-WP] eq 27 (p. 18) |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 174 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0 |
| 176 | "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2 |
| 177 | "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1 |
| 178 | "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1 |
| 179 | "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63 |
| 180 | "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63 |
| 181 | "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63 |
| 182 | "pslldq $8, %%xmm3 \n\t" // r0>>63:0 |
| 183 | "pslldq $8, %%xmm4 \n\t" // r2>>63:0 |
| 184 | "psrldq $8, %%xmm5 \n\t" // 0:r1>>63 |
| 185 | "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1 |
| 186 | "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1 |
| 187 | "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] | 188 | |
| 189 | /* |
| 190 | * 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] | 191 | * using [CLMUL-WP] algorithm 5 (p. 18). |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 192 | * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted). |
| 193 | */ |
| 194 | /* Step 2 (1) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0 |
| 196 | "movdqa %%xmm1, %%xmm4 \n\t" // same |
| 197 | "movdqa %%xmm1, %%xmm5 \n\t" // same |
| 198 | "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a |
| 199 | "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b |
| 200 | "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] | 201 | |
| 202 | /* Step 2 (2) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b |
| 204 | "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c |
| 205 | "pslldq $8, %%xmm3 \n\t" // a+b+c:0 |
| 206 | "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] | 207 | |
| 208 | /* Steps 3 and 4 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | "movdqa %%xmm1,%%xmm0 \n\t" // d:x0 |
| 210 | "movdqa %%xmm1,%%xmm4 \n\t" // same |
| 211 | "movdqa %%xmm1,%%xmm5 \n\t" // same |
| 212 | "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0' |
| 213 | "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0' |
| 214 | "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0' |
| 215 | "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0' |
| 216 | "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] | 217 | // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing |
| 218 | // bits carried from d. Now get those\t bits back in. |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | "movdqa %%xmm1,%%xmm3 \n\t" // d:x0 |
| 220 | "movdqa %%xmm1,%%xmm4 \n\t" // same |
| 221 | "movdqa %%xmm1,%%xmm5 \n\t" // same |
| 222 | "psllq $63, %%xmm3 \n\t" // d<<63:stuff |
| 223 | "psllq $62, %%xmm4 \n\t" // d<<62:stuff |
| 224 | "psllq $57, %%xmm5 \n\t" // d<<57:stuff |
| 225 | "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff |
| 226 | "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff |
| 227 | "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d |
| 228 | "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0 |
| 229 | "pxor %%xmm1, %%xmm0 \n\t" // h1:h0 |
| 230 | "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0 |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | "movdqu %%xmm0, (%2) \n\t" // done |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 233 | : |
| 234 | : "r" (aa), "r" (bb), "r" (cc) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"); |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 236 | |
| 237 | /* Now byte-reverse the outputs */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | for (i = 0; i < 16; i++) { |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 239 | c[i] = cc[15 - i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | } |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 241 | |
Paul Bakker | e7f5133 | 2013-12-30 15:32:02 +0100 | [diff] [blame] | 242 | return; |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Compute decryption round keys from encryption round keys |
| 247 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 249 | const unsigned char *fwdkey, int nr) |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 250 | { |
| 251 | unsigned char *ik = invkey; |
| 252 | const unsigned char *fk = fwdkey + 16 * nr; |
| 253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | memcpy(ik, fk, 16); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) { |
| 257 | asm ("movdqu (%0), %%xmm0 \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 258 | AESIMC xmm0_xmm0 "\n\t" |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | "movdqu %%xmm0, (%1) \n\t" |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 260 | : |
| 261 | : "r" (fk), "r" (ik) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | : "memory", "xmm0"); |
| 263 | } |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | memcpy(ik, fk, 16); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 268 | /* |
| 269 | * Key expansion, 128-bit case |
| 270 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | static void aesni_setkey_enc_128(unsigned char *rk, |
| 272 | const unsigned char *key) |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 273 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 275 | "movdqu %%xmm0, (%0) \n\t" // as round key 0 |
| 276 | "jmp 2f \n\t" // skip auxiliary routine |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 277 | |
| 278 | /* |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 279 | * Finish generating the next round key. |
| 280 | * |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 281 | * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff |
| 282 | * with X = rot( sub( r3 ) ) ^ RCON. |
| 283 | * |
| 284 | * On exit, xmm0 is r7:r6:r5:r4 |
| 285 | * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3 |
| 286 | * and those are written to the round key buffer. |
| 287 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 288 | "1: \n\t" |
| 289 | "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X |
| 290 | "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4 |
| 291 | "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0 |
| 292 | "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4 |
| 293 | "pslldq $4, %%xmm0 \n\t" // etc |
| 294 | "pxor %%xmm0, %%xmm1 \n\t" |
| 295 | "pslldq $4, %%xmm0 \n\t" |
| 296 | "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time! |
| 297 | "add $16, %0 \n\t" // point to next round key |
| 298 | "movdqu %%xmm0, (%0) \n\t" // write it |
| 299 | "ret \n\t" |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 300 | |
| 301 | /* Main "loop" */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 302 | "2: \n\t" |
| 303 | AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t" |
| 304 | AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t" |
| 305 | AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t" |
| 306 | AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t" |
| 307 | AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t" |
| 308 | AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t" |
| 309 | AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t" |
| 310 | AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t" |
| 311 | AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t" |
| 312 | AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t" |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 313 | : |
| 314 | : "r" (rk), "r" (key) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | : "memory", "cc", "0"); |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 319 | * Key expansion, 192-bit case |
| 320 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | static void aesni_setkey_enc_192(unsigned char *rk, |
| 322 | const unsigned char *key) |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 323 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 325 | "movdqu %%xmm0, (%0) \n\t" |
| 326 | "add $16, %0 \n\t" |
| 327 | "movq 16(%1), %%xmm1 \n\t" |
| 328 | "movq %%xmm1, (%0) \n\t" |
| 329 | "add $8, %0 \n\t" |
| 330 | "jmp 2f \n\t" // skip auxiliary routine |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * Finish generating the next 6 quarter-keys. |
| 334 | * |
| 335 | * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4 |
| 336 | * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON. |
| 337 | * |
| 338 | * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10 |
| 339 | * and those are written to the round key buffer. |
| 340 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 341 | "1: \n\t" |
| 342 | "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X |
| 343 | "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4 |
| 344 | "pslldq $4, %%xmm0 \n\t" // etc |
| 345 | "pxor %%xmm0, %%xmm2 \n\t" |
| 346 | "pslldq $4, %%xmm0 \n\t" |
| 347 | "pxor %%xmm0, %%xmm2 \n\t" |
| 348 | "pslldq $4, %%xmm0 \n\t" |
| 349 | "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6 |
| 350 | "movdqu %%xmm0, (%0) \n\t" |
| 351 | "add $16, %0 \n\t" |
| 352 | "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9 |
| 353 | "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10 |
| 354 | "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0 |
| 355 | "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10 |
| 356 | "movq %%xmm1, (%0) \n\t" |
| 357 | "add $8, %0 \n\t" |
| 358 | "ret \n\t" |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 359 | |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 360 | "2: \n\t" |
| 361 | AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t" |
| 362 | AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t" |
| 363 | AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t" |
| 364 | AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t" |
| 365 | AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t" |
| 366 | AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t" |
| 367 | AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t" |
| 368 | AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t" |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 369 | |
| 370 | : |
| 371 | : "r" (rk), "r" (key) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | : "memory", "cc", "0"); |
Manuel Pégourié-Gonnard | 23c2f6f | 2013-12-29 16:05:22 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 376 | * Key expansion, 256-bit case |
| 377 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | static void aesni_setkey_enc_256(unsigned char *rk, |
| 379 | const unsigned char *key) |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 380 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | asm ("movdqu (%1), %%xmm0 \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 382 | "movdqu %%xmm0, (%0) \n\t" |
| 383 | "add $16, %0 \n\t" |
| 384 | "movdqu 16(%1), %%xmm1 \n\t" |
| 385 | "movdqu %%xmm1, (%0) \n\t" |
| 386 | "jmp 2f \n\t" // skip auxiliary routine |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Finish generating the next two round keys. |
| 390 | * |
| 391 | * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and |
| 392 | * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON |
| 393 | * |
| 394 | * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12 |
| 395 | * and those have been written to the output buffer. |
| 396 | */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 397 | "1: \n\t" |
| 398 | "pshufd $0xff, %%xmm2, %%xmm2 \n\t" |
| 399 | "pxor %%xmm0, %%xmm2 \n\t" |
| 400 | "pslldq $4, %%xmm0 \n\t" |
| 401 | "pxor %%xmm0, %%xmm2 \n\t" |
| 402 | "pslldq $4, %%xmm0 \n\t" |
| 403 | "pxor %%xmm0, %%xmm2 \n\t" |
| 404 | "pslldq $4, %%xmm0 \n\t" |
| 405 | "pxor %%xmm2, %%xmm0 \n\t" |
| 406 | "add $16, %0 \n\t" |
| 407 | "movdqu %%xmm0, (%0) \n\t" |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 408 | |
| 409 | /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 ) |
| 410 | * and proceed to generate next round key from there */ |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 411 | AESKEYGENA xmm0_xmm2 ",0x00 \n\t" |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | "pshufd $0xaa, %%xmm2, %%xmm2 \n\t" |
| 413 | "pxor %%xmm1, %%xmm2 \n\t" |
| 414 | "pslldq $4, %%xmm1 \n\t" |
| 415 | "pxor %%xmm1, %%xmm2 \n\t" |
| 416 | "pslldq $4, %%xmm1 \n\t" |
| 417 | "pxor %%xmm1, %%xmm2 \n\t" |
| 418 | "pslldq $4, %%xmm1 \n\t" |
| 419 | "pxor %%xmm2, %%xmm1 \n\t" |
| 420 | "add $16, %0 \n\t" |
| 421 | "movdqu %%xmm1, (%0) \n\t" |
| 422 | "ret \n\t" |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * Main "loop" - Generating one more key than necessary, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | * see definition of mbedtls_aes_context.buf |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 427 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | "2: \n\t" |
Manuel Pégourié-Gonnard | 0534fd4 | 2014-06-23 12:35:42 +0200 | [diff] [blame] | 429 | AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t" |
| 430 | AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t" |
| 431 | AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t" |
| 432 | AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t" |
| 433 | AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t" |
| 434 | AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t" |
| 435 | AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t" |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 436 | : |
| 437 | : "r" (rk), "r" (key) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | : "memory", "cc", "0"); |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 442 | * Key expansion, wrapper |
| 443 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | int mbedtls_aesni_setkey_enc(unsigned char *rk, |
| 445 | const unsigned char *key, |
| 446 | size_t bits) |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 447 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | switch (bits) { |
| 449 | case 128: aesni_setkey_enc_128(rk, key); break; |
| 450 | case 192: aesni_setkey_enc_192(rk, key); break; |
| 451 | case 256: aesni_setkey_enc_256(rk, key); break; |
| 452 | default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | return 0; |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | #endif /* MBEDTLS_HAVE_X86_64 */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 459 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 460 | #endif /* MBEDTLS_AESNI_C */ |