blob: 8d761b2fd504dc57fe9edc0b23e5bc783fc81a25 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
4 * Copyright (C) 2006-2007 Christophe Devine
5 *
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.
10 *
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.
15 *
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.
19 */
20/*
21 * This implementation is based on the VIA PadLock Programming Guide:
22 *
23 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
24 * programming_guide.pdf
25 */
26
27#include "xyssl/config.h"
28
29#if defined(XYSSL_PADLOCK_C)
30
31#include "xyssl/aes.h"
32#include "xyssl/padlock.h"
33
34#if defined(XYSSL_HAVE_X86)
35
36#include <string.h>
37
38/*
39 * PadLock detection routine
40 */
41int padlock_supports( int feature )
42{
43 static int flags = -1;
44 int ebx, edx;
45
46 if( flags == -1 )
47 {
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" );
62
63 flags = edx;
64 }
65
66 return( flags & feature );
67}
68
69/*
70 * PadLock AES-ECB block en(de)cryption
71 */
72int padlock_xcryptecb( aes_context *ctx,
73 int mode,
74 unsigned char input[16],
75 unsigned char output[16] )
76{
77 int ebx;
78 unsigned long *rk;
79 unsigned long *blk;
80 unsigned long *ctrl;
81 unsigned char buf[256];
82
83 rk = ctx->rk;
84 blk = PADLOCK_ALIGN16( buf );
85 memcpy( blk, input, 16 );
86
87 ctrl = blk + 4;
88 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
89
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" );
102
103 memcpy( output, blk, 16 );
104
105 return( 0 );
106}
107
108/*
109 * PadLock AES-CBC buffer en(de)cryption
110 */
111int padlock_xcryptcbc( aes_context *ctx,
112 int mode,
113 int length,
114 unsigned char iv[16],
115 unsigned char *input,
116 unsigned char *output )
117{
118 int ebx, count;
119 unsigned long *rk;
120 unsigned long *iw;
121 unsigned long *ctrl;
122 unsigned char buf[256];
123
124 if( ( (long) input & 15 ) != 0 ||
125 ( (long) output & 15 ) != 0 )
126 return( 1 );
127
128 rk = ctx->rk;
129 iw = PADLOCK_ALIGN16( buf );
130 memcpy( iw, iv, 16 );
131
132 ctrl = iw + 4;
133 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
134
135 count = (length + 15) >> 4;
136
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" );
151
152 memcpy( iv, iw, 16 );
153
154 return( 0 );
155}
156
157#endif
158
159#endif