Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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