new, smaller, faster and untested version of KLISP
[syren.git] / src / xyssl / padlock.c
blob8d761b2fd504dc57fe9edc0b23e5bc783fc81a25
1 /*
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)
36 #include <string.h>
39 * PadLock detection routine
41 int padlock_supports( int feature )
43 static int flags = -1;
44 int ebx, edx;
46 if( flags == -1 )
48 asm( "movl %%ebx, %0 \n" \
49 "movl $0xC0000000, %%eax \n" \
50 "cpuid \n" \
51 "cmpl $0xC0000001, %%eax \n" \
52 "movl $0, %%edx \n" \
53 "jb unsupported \n" \
54 "movl $0xC0000001, %%eax \n" \
55 "cpuid \n" \
56 "unsupported: \n" \
57 "movl %%edx, %1 \n" \
58 "movl %2, %%ebx \n"
59 : "=m" (ebx), "=m" (edx)
60 : "m" (ebx)
61 : "eax", "ecx", "edx" );
63 flags = edx;
66 return( flags & feature );
70 * PadLock AES-ECB block en(de)cryption
72 int padlock_xcryptecb( aes_context *ctx,
73 int mode,
74 unsigned char input[16],
75 unsigned char output[16] )
77 int ebx;
78 unsigned long *rk;
79 unsigned long *blk;
80 unsigned long *ctrl;
81 unsigned char buf[256];
83 rk = ctx->rk;
84 blk = PADLOCK_ALIGN16( buf );
85 memcpy( blk, input, 16 );
87 ctrl = blk + 4;
88 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
90 asm( "pushfl; popfl \n" \
91 "movl %%ebx, %0 \n" \
92 "movl $1, %%ecx \n" \
93 "movl %2, %%edx \n" \
94 "movl %3, %%ebx \n" \
95 "movl %4, %%esi \n" \
96 "movl %4, %%edi \n" \
97 ".byte 0xf3,0x0f,0xa7,0xc8\n" \
98 "movl %1, %%ebx \n"
99 : "=m" (ebx)
100 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
101 : "ecx", "edx", "esi", "edi" );
103 memcpy( output, blk, 16 );
105 return( 0 );
109 * PadLock AES-CBC buffer en(de)cryption
111 int padlock_xcryptcbc( aes_context *ctx,
112 int mode,
113 int length,
114 unsigned char iv[16],
115 unsigned char *input,
116 unsigned char *output )
118 int ebx, count;
119 unsigned long *rk;
120 unsigned long *iw;
121 unsigned long *ctrl;
122 unsigned char buf[256];
124 if( ( (long) input & 15 ) != 0 ||
125 ( (long) output & 15 ) != 0 )
126 return( 1 );
128 rk = ctx->rk;
129 iw = PADLOCK_ALIGN16( buf );
130 memcpy( iw, iv, 16 );
132 ctrl = iw + 4;
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" \
146 "movl %1, %%ebx \n"
147 : "=m" (ebx)
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 );
154 return( 0 );
157 #endif
159 #endif