blob: 5d06390cada142f1567f6f3ee55578fcfd632fab [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, 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 {
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020054 asm( "movl %%ebx, %0 \n\t"
55 "movl $0xC0000000, %%eax \n\t"
56 "cpuid \n\t"
57 "cmpl $0xC0000001, %%eax \n\t"
58 "movl $0, %%edx \n\t"
59 "jb unsupported \n\t"
60 "movl $0xC0000001, %%eax \n\t"
61 "cpuid \n\t"
62 "unsupported: \n\t"
63 "movl %%edx, %1 \n\t"
64 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000065 : "=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
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020096 asm( "pushfl \n\t"
97 "popfl \n\t"
98 "movl %%ebx, %0 \n\t"
99 "movl $1, %%ecx \n\t"
100 "movl %2, %%edx \n\t"
101 "movl %3, %%ebx \n\t"
102 "movl %4, %%esi \n\t"
103 "movl %4, %%edi \n\t"
104 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
105 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 : "=m" (ebx)
107 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
108 : "ecx", "edx", "esi", "edi" );
109
110 memcpy( output, blk, 16 );
111
112 return( 0 );
113}
114
115/*
116 * PadLock AES-CBC buffer en(de)cryption
117 */
118int padlock_xcryptcbc( aes_context *ctx,
119 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000120 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000122 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 unsigned char *output )
124{
Paul Bakker53e15132013-12-30 20:43:40 +0100125 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000126 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000127 uint32_t *rk;
128 uint32_t *iw;
129 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 unsigned char buf[256];
131
132 if( ( (long) input & 15 ) != 0 ||
133 ( (long) output & 15 ) != 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000134 return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136 rk = ctx->rk;
137 iw = PADLOCK_ALIGN16( buf );
138 memcpy( iw, iv, 16 );
139
140 ctrl = iw + 4;
Paul Bakker66d5d072014-06-17 16:39:18 +0200141 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Paul Bakker66d5d072014-06-17 16:39:18 +0200143 count = ( length + 15 ) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200145 asm( "pushfl \n\t"
146 "popfl \n\t"
147 "movl %%ebx, %0 \n\t"
148 "movl %2, %%ecx \n\t"
149 "movl %3, %%edx \n\t"
150 "movl %4, %%ebx \n\t"
151 "movl %5, %%esi \n\t"
152 "movl %6, %%edi \n\t"
153 "movl %7, %%eax \n\t"
154 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
155 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 : "=m" (ebx)
157 : "m" (ebx), "m" (count), "m" (ctrl),
158 "m" (rk), "m" (input), "m" (output), "m" (iw)
159 : "eax", "ecx", "edx", "esi", "edi" );
160
161 memcpy( iv, iw, 16 );
162
163 return( 0 );
164}
165
Paul Bakker9af723c2014-05-01 13:03:14 +0200166#endif /* POLARSSL_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakker9af723c2014-05-01 13:03:14 +0200168#endif /* POLARSSL_PADLOCK_C */