blob: 15343ab10c67c0b8a7a1e96a4238c2342b529f62 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * This implementation is based on the VIA PadLock Programming Guide:
24 *
25 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
26 * programming_guide.pdf
27 */
28
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#if defined(POLARSSL_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#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
Paul Bakker5121ce52009-01-03 21:22:43 +000037/*
38 * PadLock detection routine
39 */
40int padlock_supports( int feature )
41{
42 static int flags = -1;
43 int ebx, edx;
44
45 if( flags == -1 )
46 {
Manuel Pégourié-Gonnard877a0942014-06-23 12:40:01 +020047 asm( "movl %%ebx, %0 \n\t"
48 "movl $0xC0000000, %%eax \n\t"
49 "cpuid \n\t"
50 "cmpl $0xC0000001, %%eax \n\t"
51 "movl $0, %%edx \n\t"
52 "jb unsupported \n\t"
53 "movl $0xC0000001, %%eax \n\t"
54 "cpuid \n\t"
55 "unsupported: \n\t"
56 "movl %%edx, %1 \n\t"
57 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000058 : "=m" (ebx), "=m" (edx)
59 : "m" (ebx)
60 : "eax", "ecx", "edx" );
61
62 flags = edx;
63 }
64
65 return( flags & feature );
66}
67
68/*
69 * PadLock AES-ECB block en(de)cryption
70 */
71int padlock_xcryptecb( aes_context *ctx,
72 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000073 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000074 unsigned char output[16] )
75{
76 int ebx;
Paul Bakker5c2364c2012-10-01 14:41:15 +000077 uint32_t *rk;
78 uint32_t *blk;
79 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000080 unsigned char buf[256];
81
82 rk = ctx->rk;
83 blk = PADLOCK_ALIGN16( buf );
84 memcpy( blk, input, 16 );
85
86 ctrl = blk + 4;
87 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
88
Manuel Pégourié-Gonnard877a0942014-06-23 12:40:01 +020089 asm( "pushfl \n\t"
90 "popfl \n\t"
91 "movl %%ebx, %0 \n\t"
92 "movl $1, %%ecx \n\t"
93 "movl %2, %%edx \n\t"
94 "movl %3, %%ebx \n\t"
95 "movl %4, %%esi \n\t"
96 "movl %4, %%edi \n\t"
97 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
98 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000099 : "=m" (ebx)
100 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Manuel Pégourié-Gonnardcd7d24d2015-04-10 17:38:38 +0200101 : "memory", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103 memcpy( output, blk, 16 );
104
105 return( 0 );
106}
107
108/*
109 * PadLock AES-CBC buffer en(de)cryption
110 */
111int padlock_xcryptcbc( aes_context *ctx,
112 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000113 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 unsigned char *output )
117{
Paul Bakker23986e52011-04-24 08:57:21 +0000118 int ebx;
119 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000120 uint32_t *rk;
121 uint32_t *iw;
122 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 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
Manuel Pégourié-Gonnard877a0942014-06-23 12:40:01 +0200138 asm( "pushfl \n\t"
139 "popfl \n\t"
140 "movl %%ebx, %0 \n\t"
141 "movl %2, %%ecx \n\t"
142 "movl %3, %%edx \n\t"
143 "movl %4, %%ebx \n\t"
144 "movl %5, %%esi \n\t"
145 "movl %6, %%edi \n\t"
146 "movl %7, %%eax \n\t"
147 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
148 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 : "=m" (ebx)
150 : "m" (ebx), "m" (count), "m" (ctrl),
151 "m" (rk), "m" (input), "m" (output), "m" (iw)
Manuel Pégourié-Gonnardcd7d24d2015-04-10 17:38:38 +0200152 : "memory", "eax", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154 memcpy( iv, iw, 16 );
155
156 return( 0 );
157}
158
159#endif
160
161#endif