blob: 3344a963cf24a85a390aaf1b57b506c906b68dd9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23/*
24 * This implementation is based on the VIA PadLock Programming Guide:
25 *
26 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
27 * programming_guide.pdf
28 */
29
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/aes.h"
35#include "polarssl/padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39#include <string.h>
40
41/*
42 * PadLock detection routine
43 */
44int padlock_supports( int feature )
45{
46 static int flags = -1;
47 int ebx, edx;
48
49 if( flags == -1 )
50 {
51 asm( "movl %%ebx, %0 \n" \
52 "movl $0xC0000000, %%eax \n" \
53 "cpuid \n" \
54 "cmpl $0xC0000001, %%eax \n" \
55 "movl $0, %%edx \n" \
56 "jb unsupported \n" \
57 "movl $0xC0000001, %%eax \n" \
58 "cpuid \n" \
59 "unsupported: \n" \
60 "movl %%edx, %1 \n" \
61 "movl %2, %%ebx \n"
62 : "=m" (ebx), "=m" (edx)
63 : "m" (ebx)
64 : "eax", "ecx", "edx" );
65
66 flags = edx;
67 }
68
69 return( flags & feature );
70}
71
72/*
73 * PadLock AES-ECB block en(de)cryption
74 */
75int padlock_xcryptecb( aes_context *ctx,
76 int mode,
77 unsigned char input[16],
78 unsigned char output[16] )
79{
80 int ebx;
81 unsigned long *rk;
82 unsigned long *blk;
83 unsigned long *ctrl;
84 unsigned char buf[256];
85
86 rk = ctx->rk;
87 blk = PADLOCK_ALIGN16( buf );
88 memcpy( blk, input, 16 );
89
90 ctrl = blk + 4;
91 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
92
93 asm( "pushfl; popfl \n" \
94 "movl %%ebx, %0 \n" \
95 "movl $1, %%ecx \n" \
96 "movl %2, %%edx \n" \
97 "movl %3, %%ebx \n" \
98 "movl %4, %%esi \n" \
99 "movl %4, %%edi \n" \
100 ".byte 0xf3,0x0f,0xa7,0xc8\n" \
101 "movl %1, %%ebx \n"
102 : "=m" (ebx)
103 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
104 : "ecx", "edx", "esi", "edi" );
105
106 memcpy( output, blk, 16 );
107
108 return( 0 );
109}
110
111/*
112 * PadLock AES-CBC buffer en(de)cryption
113 */
114int padlock_xcryptcbc( aes_context *ctx,
115 int mode,
116 int length,
117 unsigned char iv[16],
118 unsigned char *input,
119 unsigned char *output )
120{
121 int ebx, count;
122 unsigned long *rk;
123 unsigned long *iw;
124 unsigned long *ctrl;
125 unsigned char buf[256];
126
127 if( ( (long) input & 15 ) != 0 ||
128 ( (long) output & 15 ) != 0 )
129 return( 1 );
130
131 rk = ctx->rk;
132 iw = PADLOCK_ALIGN16( buf );
133 memcpy( iw, iv, 16 );
134
135 ctrl = iw + 4;
136 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
137
138 count = (length + 15) >> 4;
139
140 asm( "pushfl; popfl \n" \
141 "movl %%ebx, %0 \n" \
142 "movl %2, %%ecx \n" \
143 "movl %3, %%edx \n" \
144 "movl %4, %%ebx \n" \
145 "movl %5, %%esi \n" \
146 "movl %6, %%edi \n" \
147 "movl %7, %%eax \n" \
148 ".byte 0xf3,0x0f,0xa7,0xd0\n" \
149 "movl %1, %%ebx \n"
150 : "=m" (ebx)
151 : "m" (ebx), "m" (count), "m" (ctrl),
152 "m" (rk), "m" (input), "m" (output), "m" (iw)
153 : "eax", "ecx", "edx", "esi", "edi" );
154
155 memcpy( iv, iw, 16 );
156
157 return( 0 );
158}
159
160#endif
161
162#endif