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