2 * VIA PadLock support functions
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * This implementation is based on the VIA PadLock Programming Guide:
38 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
39 * programming_guide.pdf
42 #include "tropicssl/config.h"
44 #if defined(TROPICSSL_PADLOCK_C)
46 #include "tropicssl/aes.h"
47 #include "tropicssl/padlock.h"
49 #if defined(TROPICSSL_HAVE_X86)
54 * PadLock detection routine
56 int padlock_supports(int feature
)
58 static int flags
= -1;
62 asm("movl %%ebx, %0 \n" "movl $0xC0000000, %%eax \n" "cpuid \n" "cmpl $0xC0000001, %%eax \n" "movl $0, %%edx \n" "jb unsupported \n" "movl $0xC0000001, %%eax \n" "cpuid \n" "unsupported: \n" "movl %%edx, %1 \n" "movl %2, %%ebx \n":"=m"(ebx
),
66 : "eax", "ecx", "edx");
71 return (flags
& feature
);
75 * PadLock AES-ECB block en(de)cryption
77 int padlock_xcryptecb(aes_context
* ctx
,
79 const unsigned char input
[16],
80 unsigned char output
[16])
86 unsigned char buf
[256];
89 blk
= PADLOCK_ALIGN16(buf
);
90 memcpy(blk
, input
, 16);
93 *ctrl
= 0x80 | ctx
->nr
| ((ctx
->nr
+ (mode
^ 1) - 10) << 9);
95 asm("pushfl; popfl \n" "movl %%ebx, %0 \n" "movl $1, %%ecx \n" "movl %2, %%edx \n" "movl %3, %%ebx \n" "movl %4, %%esi \n" "movl %4, %%edi \n" ".byte 0xf3,0x0f,0xa7,0xc8\n" "movl %1, %%ebx \n":"=m"(ebx
)
96 : "m"(ebx
), "m"(ctrl
), "m"(rk
), "m"(blk
)
97 : "ecx", "edx", "esi", "edi");
99 memcpy(output
, blk
, 16);
105 * PadLock AES-CBC buffer en(de)cryption
107 int padlock_xcryptcbc(aes_context
* ctx
,
110 unsigned char iv
[16],
111 const unsigned char *input
,
112 unsigned char *output
)
118 unsigned char buf
[256];
120 if (((long)input
& 15) != 0 || ((long)output
& 15) != 0)
124 iw
= PADLOCK_ALIGN16(buf
);
128 *ctrl
= 0x80 | ctx
->nr
| ((ctx
->nr
+ (mode
^ 1) - 10) << 9);
130 count
= (length
+ 15) >> 4;
132 asm("pushfl; popfl \n" "movl %%ebx, %0 \n" "movl %2, %%ecx \n" "movl %3, %%edx \n" "movl %4, %%ebx \n" "movl %5, %%esi \n" "movl %6, %%edi \n" "movl %7, %%eax \n" ".byte 0xf3,0x0f,0xa7,0xd0\n" "movl %1, %%ebx \n":"=m"(ebx
)
133 : "m"(ebx
), "m"(count
), "m"(ctrl
),
134 "m"(rk
), "m"(input
), "m"(output
), "m"(iw
)
135 : "eax", "ecx", "edx", "esi", "edi");