Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
electronichamsters
Date:
Mon Jul 10 04:00:25 2017 +0000
Revision:
5:f09f5ed830ca
Parent:
1:24750b9ad5ef
working gateway

Who changed what in which revision?

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