blob: 52b04f0f3f8dda65d439a0408c21d1c91fefa6da [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker40e46942009-01-03 21:51:57 +000038#if defined(POLARSSL_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker40e46942009-01-03 21:51:57 +000042#if defined(POLARSSL_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Paul Bakker5121ce52009-01-03 21:22:43 +000044/*
45 * PadLock detection routine
46 */
47int padlock_supports( int feature )
48{
49 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010050 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52 if( flags == -1 )
53 {
54 asm( "movl %%ebx, %0 \n" \
55 "movl $0xC0000000, %%eax \n" \
56 "cpuid \n" \
57 "cmpl $0xC0000001, %%eax \n" \
58 "movl $0, %%edx \n" \
59 "jb unsupported \n" \
60 "movl $0xC0000001, %%eax \n" \
61 "cpuid \n" \
62 "unsupported: \n" \
63 "movl %%edx, %1 \n" \
64 "movl %2, %%ebx \n"
65 : "=m" (ebx), "=m" (edx)
66 : "m" (ebx)
67 : "eax", "ecx", "edx" );
68
69 flags = edx;
70 }
71
72 return( flags & feature );
73}
74
75/*
76 * PadLock AES-ECB block en(de)cryption
77 */
78int padlock_xcryptecb( aes_context *ctx,
79 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000080 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000081 unsigned char output[16] )
82{
Paul Bakker53e15132013-12-30 20:43:40 +010083 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000084 uint32_t *rk;
85 uint32_t *blk;
86 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000087 unsigned char buf[256];
88
89 rk = ctx->rk;
90 blk = PADLOCK_ALIGN16( buf );
91 memcpy( blk, input, 16 );
92
93 ctrl = blk + 4;
94 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
95
96 asm( "pushfl; popfl \n" \
97 "movl %%ebx, %0 \n" \
98 "movl $1, %%ecx \n" \
99 "movl %2, %%edx \n" \
100 "movl %3, %%ebx \n" \
101 "movl %4, %%esi \n" \
102 "movl %4, %%edi \n" \
103 ".byte 0xf3,0x0f,0xa7,0xc8\n" \
104 "movl %1, %%ebx \n"
105 : "=m" (ebx)
106 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
107 : "ecx", "edx", "esi", "edi" );
108
109 memcpy( output, blk, 16 );
110
111 return( 0 );
112}
113
114/*
115 * PadLock AES-CBC buffer en(de)cryption
116 */
117int padlock_xcryptcbc( aes_context *ctx,
118 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 unsigned char *output )
123{
Paul Bakker53e15132013-12-30 20:43:40 +0100124 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000125 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000126 uint32_t *rk;
127 uint32_t *iw;
128 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 unsigned char buf[256];
130
131 if( ( (long) input & 15 ) != 0 ||
132 ( (long) output & 15 ) != 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000133 return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 rk = ctx->rk;
136 iw = PADLOCK_ALIGN16( buf );
137 memcpy( iw, iv, 16 );
138
139 ctrl = iw + 4;
140 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
141
142 count = (length + 15) >> 4;
143
144 asm( "pushfl; popfl \n" \
145 "movl %%ebx, %0 \n" \
146 "movl %2, %%ecx \n" \
147 "movl %3, %%edx \n" \
148 "movl %4, %%ebx \n" \
149 "movl %5, %%esi \n" \
150 "movl %6, %%edi \n" \
151 "movl %7, %%eax \n" \
152 ".byte 0xf3,0x0f,0xa7,0xd0\n" \
153 "movl %1, %%ebx \n"
154 : "=m" (ebx)
155 : "m" (ebx), "m" (count), "m" (ctrl),
156 "m" (rk), "m" (input), "m" (output), "m" (iw)
157 : "eax", "ecx", "edx", "esi", "edi" );
158
159 memcpy( iv, iw, 16 );
160
161 return( 0 );
162}
163
164#endif
165
166#endif