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