text
[RRG-proxmark3.git] / common / mbedtls / padlock.c
blobf45f36cb5ca274281a19ca1a13efbdc071d9a1df
1 /*
2 * VIA PadLock support functions
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 * This implementation is based on the VIA PadLock Programming Guide:
22 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
23 * programming_guide.pdf
26 #include "common.h"
28 #if defined(MBEDTLS_PADLOCK_C)
30 #include "mbedtls/padlock.h"
32 #include <string.h>
34 #ifndef asm
35 #define asm __asm
36 #endif
38 #if defined(MBEDTLS_HAVE_X86)
41 * PadLock detection routine
43 int mbedtls_padlock_has_support(int feature) {
44 static int flags = -1;
45 int ebx = 0, edx = 0;
47 if (flags == -1) {
48 asm("movl %%ebx, %0 \n\t"
49 "movl $0xC0000000, %%eax \n\t"
50 "cpuid \n\t"
51 "cmpl $0xC0000001, %%eax \n\t"
52 "movl $0, %%edx \n\t"
53 "jb 1f \n\t"
54 "movl $0xC0000001, %%eax \n\t"
55 "cpuid \n\t"
56 "1: \n\t"
57 "movl %%edx, %1 \n\t"
58 "movl %2, %%ebx \n\t"
59 : "=m"(ebx), "=m"(edx)
60 : "m"(ebx)
61 : "eax", "ecx", "edx");
63 flags = edx;
66 return (flags & feature);
70 * PadLock AES-ECB block en(de)cryption
72 int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
73 int mode,
74 const unsigned char input[16],
75 unsigned char output[16]) {
76 int ebx = 0;
77 uint32_t *rk;
78 uint32_t *blk;
79 uint32_t *ctrl;
80 unsigned char buf[256];
82 rk = ctx->rk;
83 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
84 memcpy(blk, input, 16);
86 ctrl = blk + 4;
87 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
89 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"
99 : "=m"(ebx)
100 : "m"(ebx), "m"(ctrl), "m"(rk), "m"(blk)
101 : "memory", "ecx", "edx", "esi", "edi");
103 memcpy(output, blk, 16);
105 return (0);
109 * PadLock AES-CBC buffer en(de)cryption
111 int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
112 int mode,
113 size_t length,
114 unsigned char iv[16],
115 const unsigned char *input,
116 unsigned char *output) {
117 int ebx = 0;
118 size_t count;
119 uint32_t *rk;
120 uint32_t *iw;
121 uint32_t *ctrl;
122 unsigned char buf[256];
124 if (((long) input & 15) != 0 ||
125 ((long) output & 15) != 0)
126 return (MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED);
128 rk = ctx->rk;
129 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
130 memcpy(iw, iv, 16);
132 ctrl = iw + 4;
133 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
135 count = (length + 15) >> 4;
137 asm("pushfl \n\t"
138 "popfl \n\t"
139 "movl %%ebx, %0 \n\t"
140 "movl %2, %%ecx \n\t"
141 "movl %3, %%edx \n\t"
142 "movl %4, %%ebx \n\t"
143 "movl %5, %%esi \n\t"
144 "movl %6, %%edi \n\t"
145 "movl %7, %%eax \n\t"
146 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
147 "movl %1, %%ebx \n\t"
148 : "=m"(ebx)
149 : "m"(ebx), "m"(count), "m"(ctrl),
150 "m"(rk), "m"(input), "m"(output), "m"(iw)
151 : "memory", "eax", "ecx", "edx", "esi", "edi");
153 memcpy(iv, iw, 16);
155 return (0);
158 #endif /* MBEDTLS_HAVE_X86 */
160 #endif /* MBEDTLS_PADLOCK_C */