blob: 3a59a22de516d2ff01e257ebef2ec7de1bd2acc1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
32#include POLARSSL_CONFIG_FILE
33#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker40e46942009-01-03 21:51:57 +000035#if defined(POLARSSL_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#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
Paul Bakker5121ce52009-01-03 21:22:43 +000041/*
42 * PadLock detection routine
43 */
44int padlock_supports( int feature )
45{
46 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010047 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49 if( flags == -1 )
50 {
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020051 asm( "movl %%ebx, %0 \n\t"
52 "movl $0xC0000000, %%eax \n\t"
53 "cpuid \n\t"
54 "cmpl $0xC0000001, %%eax \n\t"
55 "movl $0, %%edx \n\t"
56 "jb unsupported \n\t"
57 "movl $0xC0000001, %%eax \n\t"
58 "cpuid \n\t"
59 "unsupported: \n\t"
60 "movl %%edx, %1 \n\t"
61 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000062 : "=m" (ebx), "=m" (edx)
63 : "m" (ebx)
64 : "eax", "ecx", "edx" );
65
66 flags = edx;
67 }
68
69 return( flags & feature );
70}
71
72/*
73 * PadLock AES-ECB block en(de)cryption
74 */
75int padlock_xcryptecb( aes_context *ctx,
76 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000077 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000078 unsigned char output[16] )
79{
Paul Bakker53e15132013-12-30 20:43:40 +010080 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000081 uint32_t *rk;
82 uint32_t *blk;
83 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000084 unsigned char buf[256];
85
86 rk = ctx->rk;
87 blk = PADLOCK_ALIGN16( buf );
88 memcpy( blk, input, 16 );
89
90 ctrl = blk + 4;
91 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
92
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020093 asm( "pushfl \n\t"
94 "popfl \n\t"
95 "movl %%ebx, %0 \n\t"
96 "movl $1, %%ecx \n\t"
97 "movl %2, %%edx \n\t"
98 "movl %3, %%ebx \n\t"
99 "movl %4, %%esi \n\t"
100 "movl %4, %%edi \n\t"
101 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
102 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 : "=m" (ebx)
104 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
105 : "ecx", "edx", "esi", "edi" );
106
107 memcpy( output, blk, 16 );
108
109 return( 0 );
110}
111
112/*
113 * PadLock AES-CBC buffer en(de)cryption
114 */
115int padlock_xcryptcbc( aes_context *ctx,
116 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000117 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000119 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 unsigned char *output )
121{
Paul Bakker53e15132013-12-30 20:43:40 +0100122 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000123 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000124 uint32_t *rk;
125 uint32_t *iw;
126 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 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;
Paul Bakker66d5d072014-06-17 16:39:18 +0200138 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Paul Bakker66d5d072014-06-17 16:39:18 +0200140 count = ( length + 15 ) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200142 asm( "pushfl \n\t"
143 "popfl \n\t"
144 "movl %%ebx, %0 \n\t"
145 "movl %2, %%ecx \n\t"
146 "movl %3, %%edx \n\t"
147 "movl %4, %%ebx \n\t"
148 "movl %5, %%esi \n\t"
149 "movl %6, %%edi \n\t"
150 "movl %7, %%eax \n\t"
151 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
152 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 : "=m" (ebx)
154 : "m" (ebx), "m" (count), "m" (ctrl),
155 "m" (rk), "m" (input), "m" (output), "m" (iw)
156 : "eax", "ecx", "edx", "esi", "edi" );
157
158 memcpy( iv, iw, 16 );
159
160 return( 0 );
161}
162
Paul Bakker9af723c2014-05-01 13:03:14 +0200163#endif /* POLARSSL_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakker9af723c2014-05-01 13:03:14 +0200165#endif /* POLARSSL_PADLOCK_C */