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