bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / library / padlock.c
blob8536c8c90403ccbeee19b14f022afd8613256e28
1 /*
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>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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)
51 #include <string.h>
54 * PadLock detection routine
56 int padlock_supports(int feature)
58 static int flags = -1;
59 int ebx, edx;
61 if (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),
63 "=m"
64 (edx)
65 : "m"(ebx)
66 : "eax", "ecx", "edx");
68 flags = edx;
71 return (flags & feature);
75 * PadLock AES-ECB block en(de)cryption
77 int padlock_xcryptecb(aes_context * ctx,
78 int mode,
79 const unsigned char input[16],
80 unsigned char output[16])
82 int ebx;
83 unsigned long *rk;
84 unsigned long *blk;
85 unsigned long *ctrl;
86 unsigned char buf[256];
88 rk = ctx->rk;
89 blk = PADLOCK_ALIGN16(buf);
90 memcpy(blk, input, 16);
92 ctrl = blk + 4;
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);
101 return (0);
105 * PadLock AES-CBC buffer en(de)cryption
107 int padlock_xcryptcbc(aes_context * ctx,
108 int mode,
109 int length,
110 unsigned char iv[16],
111 const unsigned char *input,
112 unsigned char *output)
114 int ebx, count;
115 unsigned long *rk;
116 unsigned long *iw;
117 unsigned long *ctrl;
118 unsigned char buf[256];
120 if (((long)input & 15) != 0 || ((long)output & 15) != 0)
121 return (1);
123 rk = ctx->rk;
124 iw = PADLOCK_ALIGN16(buf);
125 memcpy(iw, iv, 16);
127 ctrl = iw + 4;
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");
137 memcpy(iv, iw, 16);
139 return (0);
142 #endif
144 #endif