2 * VIA PadLock support functions
4 * Copyright (C) 2006-2007 Christophe Devine
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This implementation is based on the VIA PadLock Programming Guide:
23 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
24 * programming_guide.pdf
27 #include "xyssl/config.h"
29 #if defined(XYSSL_PADLOCK_C)
31 #include "xyssl/aes.h"
32 #include "xyssl/padlock.h"
34 #if defined(XYSSL_HAVE_X86)
39 * PadLock detection routine
41 int padlock_supports( int feature
)
43 static int flags
= -1;
48 asm( "movl %%ebx, %0 \n" \
49 "movl $0xC0000000, %%eax \n" \
51 "cmpl $0xC0000001, %%eax \n" \
54 "movl $0xC0000001, %%eax \n" \
59 : "=m" (ebx
), "=m" (edx
)
61 : "eax", "ecx", "edx" );
66 return( flags
& feature
);
70 * PadLock AES-ECB block en(de)cryption
72 int padlock_xcryptecb( aes_context
*ctx
,
74 unsigned char input
[16],
75 unsigned char output
[16] )
81 unsigned char buf
[256];
84 blk
= PADLOCK_ALIGN16( buf
);
85 memcpy( blk
, input
, 16 );
88 *ctrl
= 0x80 | ctx
->nr
| ( ( ctx
->nr
+ ( mode
^1 ) - 10 ) << 9 );
90 asm( "pushfl; popfl \n" \
97 ".byte 0xf3,0x0f,0xa7,0xc8\n" \
100 : "m" (ebx
), "m" (ctrl
), "m" (rk
), "m" (blk
)
101 : "ecx", "edx", "esi", "edi" );
103 memcpy( output
, blk
, 16 );
109 * PadLock AES-CBC buffer en(de)cryption
111 int padlock_xcryptcbc( aes_context
*ctx
,
114 unsigned char iv
[16],
115 unsigned char *input
,
116 unsigned char *output
)
122 unsigned char buf
[256];
124 if( ( (long) input
& 15 ) != 0 ||
125 ( (long) output
& 15 ) != 0 )
129 iw
= PADLOCK_ALIGN16( buf
);
130 memcpy( iw
, iv
, 16 );
133 *ctrl
= 0x80 | ctx
->nr
| ( ( ctx
->nr
+ (mode
^1) - 10 ) << 9 );
135 count
= (length
+ 15) >> 4;
137 asm( "pushfl; popfl \n" \
138 "movl %%ebx, %0 \n" \
139 "movl %2, %%ecx \n" \
140 "movl %3, %%edx \n" \
141 "movl %4, %%ebx \n" \
142 "movl %5, %%esi \n" \
143 "movl %6, %%edi \n" \
144 "movl %7, %%eax \n" \
145 ".byte 0xf3,0x0f,0xa7,0xd0\n" \
148 : "m" (ebx
), "m" (count
), "m" (ctrl
),
149 "m" (rk
), "m" (input
), "m" (output
), "m" (iw
)
150 : "eax", "ecx", "edx", "esi", "edi" );
152 memcpy( iv
, iw
, 16 );