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