blob: d576544a8ee02656a84e1f4bdbf95cba5ee64197 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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.
15 *
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.
20 *
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.
24 */
25/*
26 * This implementation is based on the VIA PadLock Programming Guide:
27 *
28 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
29 * programming_guide.pdf
30 */
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#if defined(POLARSSL_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/aes.h"
37#include "polarssl/padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#include <string.h>
42
43/*
44 * PadLock detection routine
45 */
46int padlock_supports( int feature )
47{
48 static int flags = -1;
49 int ebx, edx;
50
51 if( flags == -1 )
52 {
53 asm( "movl %%ebx, %0 \n" \
54 "movl $0xC0000000, %%eax \n" \
55 "cpuid \n" \
56 "cmpl $0xC0000001, %%eax \n" \
57 "movl $0, %%edx \n" \
58 "jb unsupported \n" \
59 "movl $0xC0000001, %%eax \n" \
60 "cpuid \n" \
61 "unsupported: \n" \
62 "movl %%edx, %1 \n" \
63 "movl %2, %%ebx \n"
64 : "=m" (ebx), "=m" (edx)
65 : "m" (ebx)
66 : "eax", "ecx", "edx" );
67
68 flags = edx;
69 }
70
71 return( flags & feature );
72}
73
74/*
75 * PadLock AES-ECB block en(de)cryption
76 */
77int padlock_xcryptecb( aes_context *ctx,
78 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000079 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000080 unsigned char output[16] )
81{
82 int ebx;
83 unsigned long *rk;
84 unsigned long *blk;
85 unsigned long *ctrl;
86 unsigned char buf[256];
87
88 rk = ctx->rk;
89 blk = PADLOCK_ALIGN16( buf );
90 memcpy( blk, input, 16 );
91
92 ctrl = blk + 4;
93 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
94
95 asm( "pushfl; popfl \n" \
96 "movl %%ebx, %0 \n" \
97 "movl $1, %%ecx \n" \
98 "movl %2, %%edx \n" \
99 "movl %3, %%ebx \n" \
100 "movl %4, %%esi \n" \
101 "movl %4, %%edi \n" \
102 ".byte 0xf3,0x0f,0xa7,0xc8\n" \
103 "movl %1, %%ebx \n"
104 : "=m" (ebx)
105 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
106 : "ecx", "edx", "esi", "edi" );
107
108 memcpy( output, blk, 16 );
109
110 return( 0 );
111}
112
113/*
114 * PadLock AES-CBC buffer en(de)cryption
115 */
116int padlock_xcryptcbc( aes_context *ctx,
117 int mode,
118 int length,
119 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 unsigned char *output )
122{
123 int ebx, count;
124 unsigned long *rk;
125 unsigned long *iw;
126 unsigned long *ctrl;
127 unsigned char buf[256];
128
129 if( ( (long) input & 15 ) != 0 ||
130 ( (long) output & 15 ) != 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000131 return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133 rk = ctx->rk;
134 iw = PADLOCK_ALIGN16( buf );
135 memcpy( iw, iv, 16 );
136
137 ctrl = iw + 4;
138 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
139
140 count = (length + 15) >> 4;
141
142 asm( "pushfl; popfl \n" \
143 "movl %%ebx, %0 \n" \
144 "movl %2, %%ecx \n" \
145 "movl %3, %%edx \n" \
146 "movl %4, %%ebx \n" \
147 "movl %5, %%esi \n" \
148 "movl %6, %%edi \n" \
149 "movl %7, %%eax \n" \
150 ".byte 0xf3,0x0f,0xa7,0xd0\n" \
151 "movl %1, %%ebx \n"
152 : "=m" (ebx)
153 : "m" (ebx), "m" (count), "m" (ctrl),
154 "m" (rk), "m" (input), "m" (output), "m" (iw)
155 : "eax", "ecx", "edx", "esi", "edi" );
156
157 memcpy( iv, iw, 16 );
158
159 return( 0 );
160}
161
162#endif
163
164#endif