blob: 7628a0362c895834a0dd4eb2525f55738a021b36 [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/*
2 * AES-NI support functions
3 *
4 * Copyright (C) 2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
28 */
29
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_AESNI_C)
33
34#include "polarssl/aesni.h"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010035#include <stdio.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010036
37#if defined(POLARSSL_HAVE_X86_64)
38
39/*
40 * AES-NI support detection routine, [AES-WP] figure 23
41 */
42int aesni_supported( void )
43{
44 static int supported = -1;
45 unsigned int c;
46
47 if( supported == -1 )
48 {
49 asm( "movl $1, %%eax \n"
50 "cpuid \n"
51 : "=c" (c)
52 :
53 : "eax", "ebx", "edx" );
54 supported = ( ( c & 0x02000000 ) != 0 );
55 }
56
57 return( supported );
58}
59
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010060/*
61 * AES-NI AES-ECB block en(de)cryption
62 */
63int aesni_crypt_ecb( aes_context *ctx,
64 int mode,
65 const unsigned char input[16],
66 unsigned char output[16] )
67{
68 asm( "movdqu (%3), %%xmm0 \n" // load input
69 "movdqu (%1), %%xmm1 \n" // load round key 0
70 "pxor %%xmm1, %%xmm0 \n" // round 0
71 "addq $16, %1 \n" // point to next round key
72 "subl $1, %0 \n" // normal rounds = nr - 1
73 "test %2, %2 \n" // mode?
74 "jz 2f \n" // 0 = decrypt
75
76 "1: \n" // encryption loop
77 "movdqu (%1), %%xmm1 \n" // load round key
78 "aesenc %%xmm1, %%xmm0 \n" // do round
79 "addq $16, %1 \n" // point to next round key
80 "subl $1, %0 \n" // loop
81 "jnz 1b \n"
82 "movdqu (%1), %%xmm1 \n" // load round key
83 "aesenclast %%xmm1, %%xmm0 \n" // last round
84 "jmp 3f \n"
85
86 "2: \n" // decryption loop
87 "movdqu (%1), %%xmm1 \n"
88 "aesdec %%xmm1, %%xmm0 \n"
89 "addq $16, %1 \n"
90 "subl $1, %0 \n"
91 "jnz 2b \n"
92 "movdqu (%1), %%xmm1 \n" // load round key
93 "aesdeclast %%xmm1, %%xmm0 \n" // last round
94
95 "3: \n"
96 "movdqu %%xmm0, (%4) \n" // export output
97 :
98 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
99 : "memory", "cc", "xmm0", "xmm1" );
100
101
102 return( 0 );
103}
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100104#endif /* POLARSSL_HAVE_X86_64 */
105
106#endif /* POLARSSL_AESNI_C */