new polarssl
[syren.git] / src / libpolarssl / padlock.c
blob166cfc87873f7f749d40d4905190a62ab5c48a94
1 /*
2 * VIA PadLock support functions
4 * Copyright (C) 2006-2014, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
9 * All rights reserved.
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.
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.
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.
26 * This implementation is based on the VIA PadLock Programming Guide:
28 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
29 * programming_guide.pdf
32 #if !defined(POLARSSL_CONFIG_FILE)
33 #include "config.h"
34 #else
35 #include POLARSSL_CONFIG_FILE
36 #endif
38 #if defined(POLARSSL_PADLOCK_C)
40 #include "padlock.h"
42 #if defined(POLARSSL_HAVE_X86)
45 * PadLock detection routine
47 int padlock_supports( int feature )
49 static int flags = -1;
50 int ebx = 0, edx = 0;
52 if( flags == -1 )
54 asm( "movl %%ebx, %0 \n" \
55 "movl $0xC0000000, %%eax \n" \
56 "cpuid \n" \
57 "cmpl $0xC0000001, %%eax \n" \
58 "movl $0, %%edx \n" \
59 "jb unsupported \n" \
60 "movl $0xC0000001, %%eax \n" \
61 "cpuid \n" \
62 "unsupported: \n" \
63 "movl %%edx, %1 \n" \
64 "movl %2, %%ebx \n"
65 : "=m" (ebx), "=m" (edx)
66 : "m" (ebx)
67 : "eax", "ecx", "edx" );
69 flags = edx;
72 return( flags & feature );
76 * PadLock AES-ECB block en(de)cryption
78 int padlock_xcryptecb( aes_context *ctx,
79 int mode,
80 const unsigned char input[16],
81 unsigned char output[16] )
83 int ebx = 0;
84 uint32_t *rk;
85 uint32_t *blk;
86 uint32_t *ctrl;
87 unsigned char buf[256];
89 rk = ctx->rk;
90 blk = PADLOCK_ALIGN16( buf );
91 memcpy( blk, input, 16 );
93 ctrl = blk + 4;
94 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
96 asm( "pushfl; popfl \n" \
97 "movl %%ebx, %0 \n" \
98 "movl $1, %%ecx \n" \
99 "movl %2, %%edx \n" \
100 "movl %3, %%ebx \n" \
101 "movl %4, %%esi \n" \
102 "movl %4, %%edi \n" \
103 ".byte 0xf3,0x0f,0xa7,0xc8\n" \
104 "movl %1, %%ebx \n"
105 : "=m" (ebx)
106 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
107 : "ecx", "edx", "esi", "edi" );
109 memcpy( output, blk, 16 );
111 return( 0 );
115 * PadLock AES-CBC buffer en(de)cryption
117 int padlock_xcryptcbc( aes_context *ctx,
118 int mode,
119 size_t length,
120 unsigned char iv[16],
121 const unsigned char *input,
122 unsigned char *output )
124 int ebx = 0;
125 size_t count;
126 uint32_t *rk;
127 uint32_t *iw;
128 uint32_t *ctrl;
129 unsigned char buf[256];
131 if( ( (long) input & 15 ) != 0 ||
132 ( (long) output & 15 ) != 0 )
133 return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
135 rk = ctx->rk;
136 iw = PADLOCK_ALIGN16( buf );
137 memcpy( iw, iv, 16 );
139 ctrl = iw + 4;
140 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
142 count = (length + 15) >> 4;
144 asm( "pushfl; popfl \n" \
145 "movl %%ebx, %0 \n" \
146 "movl %2, %%ecx \n" \
147 "movl %3, %%edx \n" \
148 "movl %4, %%ebx \n" \
149 "movl %5, %%esi \n" \
150 "movl %6, %%edi \n" \
151 "movl %7, %%eax \n" \
152 ".byte 0xf3,0x0f,0xa7,0xd0\n" \
153 "movl %1, %%ebx \n"
154 : "=m" (ebx)
155 : "m" (ebx), "m" (count), "m" (ctrl),
156 "m" (rk), "m" (input), "m" (output), "m" (iw)
157 : "eax", "ecx", "edx", "esi", "edi" );
159 memcpy( iv, iw, 16 );
161 return( 0 );
164 #endif /* POLARSSL_HAVE_X86 */
166 #endif /* POLARSSL_PADLOCK_C */