Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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