BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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