blob: 35b3d00c4a0102236d48278ddfbd4ab5b3d085fc [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/*
2 * AES-NI support functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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é-Gonnard92ac76f2013-12-16 17:12:53 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010020 */
21
22/*
23 * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010024 * [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é-Gonnard92ac76f2013-12-16 17:12:53 +010025 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010034
Gilles Peskine5053efd2018-04-05 15:25:50 +020035#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é-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/aesni.h"
Teppo Järvelin91d79382019-10-02 09:09:31 +030042#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
44#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020046#ifndef asm
47#define asm __asm
48#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010051
52/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010053 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010054 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010055int mbedtls_aesni_has_support( unsigned int what )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010056{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010057 static int done = 0;
58 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010059
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010060 if( ! done )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010061 {
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020062 asm( "movl $1, %%eax \n\t"
63 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010064 : "=c" (c)
65 :
66 : "eax", "ebx", "edx" );
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010067 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010068 }
69
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010070 return( ( c & what ) != 0 );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010071}
72
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010073/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020074 * Binutils needs to be at least 2.19 to support AES-NI instructions.
75 * Unfortunately, a lot of users have a lower version now (2014-04).
76 * Emit bytecode directly in order to support "old" version of gas.
77 *
78 * Opcodes from the Intel architecture reference manual, vol. 3.
79 * We always use registers, so we don't need prefixes for memory operands.
80 * Operand macros are in gas order (src, dst) as opposed to Intel order
81 * (dst, src) in order to blend better into the surrounding assembly code.
82 */
83#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
84#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
85#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
86#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
87#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
88#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
89#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
90
91#define xmm0_xmm0 "0xC0"
92#define xmm0_xmm1 "0xC8"
93#define xmm0_xmm2 "0xD0"
94#define xmm0_xmm3 "0xD8"
95#define xmm0_xmm4 "0xE0"
96#define xmm1_xmm0 "0xC1"
97#define xmm1_xmm2 "0xD1"
98
99/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100100 * AES-NI AES-ECB block en(de)cryption
101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100103 int mode,
104 const unsigned char input[16],
105 unsigned char output[16] )
106{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200107 asm( "movdqu (%3), %%xmm0 \n\t" // load input
108 "movdqu (%1), %%xmm1 \n\t" // load round key 0
109 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000110 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200111 "subl $1, %0 \n\t" // normal rounds = nr - 1
112 "test %2, %2 \n\t" // mode?
113 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100114
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200115 "1: \n\t" // encryption loop
116 "movdqu (%1), %%xmm1 \n\t" // load round key
117 AESENC xmm1_xmm0 "\n\t" // do round
James Cowgill6c8edca2015-12-17 01:40:26 +0000118 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200119 "subl $1, %0 \n\t" // loop
120 "jnz 1b \n\t"
121 "movdqu (%1), %%xmm1 \n\t" // load round key
122 AESENCLAST xmm1_xmm0 "\n\t" // last round
123 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100124
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200125 "2: \n\t" // decryption loop
126 "movdqu (%1), %%xmm1 \n\t"
127 AESDEC xmm1_xmm0 "\n\t" // do round
James Cowgill6c8edca2015-12-17 01:40:26 +0000128 "add $16, %1 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200129 "subl $1, %0 \n\t"
130 "jnz 2b \n\t"
131 "movdqu (%1), %%xmm1 \n\t" // load round key
132 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100133
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200134 "3: \n\t"
135 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100136 :
137 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
138 : "memory", "cc", "xmm0", "xmm1" );
139
140
141 return( 0 );
142}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100143
144/*
145 * GCM multiplication: c = a times b in GF(2^128)
146 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100149 const unsigned char a[16],
150 const unsigned char b[16] )
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100151{
152 unsigned char aa[16], bb[16], cc[16];
153 size_t i;
154
155 /* The inputs are in big-endian order, so byte-reverse them */
156 for( i = 0; i < 16; i++ )
157 {
158 aa[i] = a[15 - i];
159 bb[i] = b[15 - i];
160 }
161
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200162 asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
163 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100164
165 /*
166 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
167 * using [CLMUL-WP] algorithm 1 (p. 13).
168 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200169 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
170 "movdqa %%xmm1, %%xmm3 \n\t" // same
171 "movdqa %%xmm1, %%xmm4 \n\t" // same
172 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
173 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
174 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
175 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
176 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
177 "movdqa %%xmm4, %%xmm3 \n\t" // same
178 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
179 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
180 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
181 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100182
183 /*
184 * Now shift the result one bit to the left,
185 * taking advantage of [CLMUL-WP] eq 27 (p. 20)
186 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200187 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
188 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
189 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
190 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
191 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
192 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
193 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
194 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
195 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
196 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
197 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
198 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
199 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100200
201 /*
202 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
203 * using [CLMUL-WP] algorithm 5 (p. 20).
204 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
205 */
206 /* Step 2 (1) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200207 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
208 "movdqa %%xmm1, %%xmm4 \n\t" // same
209 "movdqa %%xmm1, %%xmm5 \n\t" // same
210 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
211 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
212 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100213
214 /* Step 2 (2) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200215 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
216 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
217 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
218 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100219
220 /* Steps 3 and 4 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200221 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
222 "movdqa %%xmm1,%%xmm4 \n\t" // same
223 "movdqa %%xmm1,%%xmm5 \n\t" // same
224 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
225 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
226 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
227 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
228 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
229 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
230 // bits carried from d. Now get those\t bits back in.
231 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
232 "movdqa %%xmm1,%%xmm4 \n\t" // same
233 "movdqa %%xmm1,%%xmm5 \n\t" // same
234 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
235 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
236 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
237 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
238 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
239 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
240 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
241 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
242 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100243
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200244 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100245 :
246 : "r" (aa), "r" (bb), "r" (cc)
247 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
248
249 /* Now byte-reverse the outputs */
250 for( i = 0; i < 16; i++ )
251 c[i] = cc[15 - i];
252
Paul Bakkere7f51332013-12-30 15:32:02 +0100253 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100254}
255
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100256/*
257 * Compute decryption round keys from encryption round keys
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100260 const unsigned char *fwdkey, int nr )
261{
262 unsigned char *ik = invkey;
263 const unsigned char *fk = fwdkey + 16 * nr;
264
Teppo Järvelin91d79382019-10-02 09:09:31 +0300265 mbedtls_platform_memcpy( ik, fk, 16 );
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100266
267 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200268 asm( "movdqu (%0), %%xmm0 \n\t"
269 AESIMC xmm0_xmm0 "\n\t"
270 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100271 :
272 : "r" (fk), "r" (ik)
273 : "memory", "xmm0" );
274
Teppo Järvelin91d79382019-10-02 09:09:31 +0300275 mbedtls_platform_memcpy( ik, fk, 16 );
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100276}
277
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100278/*
279 * Key expansion, 128-bit case
280 */
Paul Bakker93759b02013-12-30 19:20:06 +0100281static void aesni_setkey_enc_128( unsigned char *rk,
282 const unsigned char *key )
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100283{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200284 asm( "movdqu (%1), %%xmm0 \n\t" // copy the original key
285 "movdqu %%xmm0, (%0) \n\t" // as round key 0
286 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100287
288 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100289 * Finish generating the next round key.
290 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100291 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
292 * with X = rot( sub( r3 ) ) ^ RCON.
293 *
294 * On exit, xmm0 is r7:r6:r5:r4
295 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
296 * and those are written to the round key buffer.
297 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200298 "1: \n\t"
299 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
300 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
301 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
302 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
303 "pslldq $4, %%xmm0 \n\t" // etc
304 "pxor %%xmm0, %%xmm1 \n\t"
305 "pslldq $4, %%xmm0 \n\t"
306 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
307 "add $16, %0 \n\t" // point to next round key
308 "movdqu %%xmm0, (%0) \n\t" // write it
309 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100310
311 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200312 "2: \n\t"
313 AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
314 AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
315 AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
316 AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
317 AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
318 AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
319 AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
320 AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
321 AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
322 AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100323 :
324 : "r" (rk), "r" (key)
325 : "memory", "cc", "0" );
326}
327
328/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100329 * Key expansion, 192-bit case
330 */
Arto Kinnunen77b9cfc2019-08-30 11:43:21 +0300331#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Paul Bakker93759b02013-12-30 19:20:06 +0100332static void aesni_setkey_enc_192( unsigned char *rk,
333 const unsigned char *key )
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100334{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200335 asm( "movdqu (%1), %%xmm0 \n\t" // copy original round key
336 "movdqu %%xmm0, (%0) \n\t"
337 "add $16, %0 \n\t"
338 "movq 16(%1), %%xmm1 \n\t"
339 "movq %%xmm1, (%0) \n\t"
340 "add $8, %0 \n\t"
341 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100342
343 /*
344 * Finish generating the next 6 quarter-keys.
345 *
346 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
347 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
348 *
349 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
350 * and those are written to the round key buffer.
351 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200352 "1: \n\t"
353 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
354 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
355 "pslldq $4, %%xmm0 \n\t" // etc
356 "pxor %%xmm0, %%xmm2 \n\t"
357 "pslldq $4, %%xmm0 \n\t"
358 "pxor %%xmm0, %%xmm2 \n\t"
359 "pslldq $4, %%xmm0 \n\t"
360 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
361 "movdqu %%xmm0, (%0) \n\t"
362 "add $16, %0 \n\t"
363 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
364 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
365 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
366 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
367 "movq %%xmm1, (%0) \n\t"
368 "add $8, %0 \n\t"
369 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100370
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200371 "2: \n\t"
372 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
373 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
374 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
375 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
376 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
377 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
378 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
379 AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100380
381 :
382 : "r" (rk), "r" (key)
383 : "memory", "cc", "0" );
384}
Arto Kinnunen77b9cfc2019-08-30 11:43:21 +0300385#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100386
387/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100388 * Key expansion, 256-bit case
389 */
Arto Kinnunen77b9cfc2019-08-30 11:43:21 +0300390#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Paul Bakker93759b02013-12-30 19:20:06 +0100391static void aesni_setkey_enc_256( unsigned char *rk,
392 const unsigned char *key )
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100393{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200394 asm( "movdqu (%1), %%xmm0 \n\t"
395 "movdqu %%xmm0, (%0) \n\t"
396 "add $16, %0 \n\t"
397 "movdqu 16(%1), %%xmm1 \n\t"
398 "movdqu %%xmm1, (%0) \n\t"
399 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100400
401 /*
402 * Finish generating the next two round keys.
403 *
404 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
405 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
406 *
407 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
408 * and those have been written to the output buffer.
409 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200410 "1: \n\t"
411 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
412 "pxor %%xmm0, %%xmm2 \n\t"
413 "pslldq $4, %%xmm0 \n\t"
414 "pxor %%xmm0, %%xmm2 \n\t"
415 "pslldq $4, %%xmm0 \n\t"
416 "pxor %%xmm0, %%xmm2 \n\t"
417 "pslldq $4, %%xmm0 \n\t"
418 "pxor %%xmm2, %%xmm0 \n\t"
419 "add $16, %0 \n\t"
420 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100421
422 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
423 * and proceed to generate next round key from there */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200424 AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
425 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
426 "pxor %%xmm1, %%xmm2 \n\t"
427 "pslldq $4, %%xmm1 \n\t"
428 "pxor %%xmm1, %%xmm2 \n\t"
429 "pslldq $4, %%xmm1 \n\t"
430 "pxor %%xmm1, %%xmm2 \n\t"
431 "pslldq $4, %%xmm1 \n\t"
432 "pxor %%xmm2, %%xmm1 \n\t"
433 "add $16, %0 \n\t"
434 "movdqu %%xmm1, (%0) \n\t"
435 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100436
437 /*
438 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100440 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200441 "2: \n\t"
442 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
443 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
444 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
445 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
446 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
447 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
448 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100449 :
450 : "r" (rk), "r" (key)
451 : "memory", "cc", "0" );
452}
Arto Kinnunen77b9cfc2019-08-30 11:43:21 +0300453#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100454
455/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100456 * Key expansion, wrapper
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100459 const unsigned char *key,
460 size_t bits )
461{
462 switch( bits )
463 {
464 case 128: aesni_setkey_enc_128( rk, key ); break;
Arto Kinnunen77b9cfc2019-08-30 11:43:21 +0300465#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100466 case 192: aesni_setkey_enc_192( rk, key ); break;
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100467 case 256: aesni_setkey_enc_256( rk, key ); break;
Arto Kinnunen77b9cfc2019-08-30 11:43:21 +0300468#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100470 }
471
472 return( 0 );
473}
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_AESNI_C */