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