PolarSSL v1.2.11
padlock.c
Go to the documentation of this file.
1 /*
2  * VIA PadLock support functions
3  *
4  * Copyright (C) 2006-2010, Brainspark B.V.
5  *
6  * This file is part of PolarSSL (http://www.polarssl.org)
7  * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8  *
9  * All rights reserved.
10  *
11  * 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 
32 #include "polarssl/config.h"
33 
34 #if defined(POLARSSL_PADLOCK_C)
35 
36 #include "polarssl/padlock.h"
37 
38 #if defined(POLARSSL_HAVE_X86)
39 
40 /*
41  * PadLock detection routine
42  */
43 int padlock_supports( int feature )
44 {
45  static int flags = -1;
46  int ebx, edx;
47 
48  if( flags == -1 )
49  {
50  asm( "movl %%ebx, %0 \n\t"
51  "movl $0xC0000000, %%eax \n\t"
52  "cpuid \n\t"
53  "cmpl $0xC0000001, %%eax \n\t"
54  "movl $0, %%edx \n\t"
55  "jb unsupported \n\t"
56  "movl $0xC0000001, %%eax \n\t"
57  "cpuid \n\t"
58  "unsupported: \n\t"
59  "movl %%edx, %1 \n\t"
60  "movl %2, %%ebx \n\t"
61  : "=m" (ebx), "=m" (edx)
62  : "m" (ebx)
63  : "eax", "ecx", "edx" );
64 
65  flags = edx;
66  }
67 
68  return( flags & feature );
69 }
70 
71 /*
72  * PadLock AES-ECB block en(de)cryption
73  */
74 int padlock_xcryptecb( aes_context *ctx,
75  int mode,
76  const unsigned char input[16],
77  unsigned char output[16] )
78 {
79  int ebx;
80  uint32_t *rk;
81  uint32_t *blk;
82  uint32_t *ctrl;
83  unsigned char buf[256];
84 
85  rk = ctx->rk;
86  blk = PADLOCK_ALIGN16( buf );
87  memcpy( blk, input, 16 );
88 
89  ctrl = blk + 4;
90  *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
91 
92  asm( "pushfl \n\t"
93  "popfl \n\t"
94  "movl %%ebx, %0 \n\t"
95  "movl $1, %%ecx \n\t"
96  "movl %2, %%edx \n\t"
97  "movl %3, %%ebx \n\t"
98  "movl %4, %%esi \n\t"
99  "movl %4, %%edi \n\t"
100  ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
101  "movl %1, %%ebx \n\t"
102  : "=m" (ebx)
103  : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
104  : "ecx", "edx", "esi", "edi" );
105 
106  memcpy( output, blk, 16 );
107 
108  return( 0 );
109 }
110 
111 /*
112  * PadLock AES-CBC buffer en(de)cryption
113  */
114 int padlock_xcryptcbc( aes_context *ctx,
115  int mode,
116  size_t length,
117  unsigned char iv[16],
118  const unsigned char *input,
119  unsigned char *output )
120 {
121  int ebx;
122  size_t count;
123  uint32_t *rk;
124  uint32_t *iw;
125  uint32_t *ctrl;
126  unsigned char buf[256];
127 
128  if( ( (long) input & 15 ) != 0 ||
129  ( (long) output & 15 ) != 0 )
131 
132  rk = ctx->rk;
133  iw = PADLOCK_ALIGN16( buf );
134  memcpy( iw, iv, 16 );
135 
136  ctrl = iw + 4;
137  *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
138 
139  count = (length + 15) >> 4;
140 
141  asm( "pushfl \n\t"
142  "popfl \n\t"
143  "movl %%ebx, %0 \n\t"
144  "movl %2, %%ecx \n\t"
145  "movl %3, %%edx \n\t"
146  "movl %4, %%ebx \n\t"
147  "movl %5, %%esi \n\t"
148  "movl %6, %%edi \n\t"
149  "movl %7, %%eax \n\t"
150  ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
151  "movl %1, %%ebx \n\t"
152  : "=m" (ebx)
153  : "m" (ebx), "m" (count), "m" (ctrl),
154  "m" (rk), "m" (input), "m" (output), "m" (iw)
155  : "eax", "ecx", "edx", "esi", "edi" );
156 
157  memcpy( iv, iw, 16 );
158 
159  return( 0 );
160 }
161 
162 #endif
163 
164 #endif
AES context structure.
Definition: aes.h:54
Configuration options (set of defines)
#define POLARSSL_ERR_PADLOCK_DATA_MISALIGNED
Input data should be aligned.
Definition: padlock.h:32
uint32_t * rk
Definition: aes.h:57
VIA PadLock ACE for HW encryption/decryption supported by some processors.
int nr
Definition: aes.h:56