This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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