1 /*
2 * VIA PadLock support functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 */
19 /*
20 * This implementation is based on the VIA PadLock Programming Guide:
21 *
22 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
23 * programming_guide.pdf
24 */
25
26 #include "common.h"
27
28 #if defined(MBEDTLS_PADLOCK_C)
29
30 #include "padlock.h"
31
32 #include <string.h>
33
34 #if defined(MBEDTLS_HAVE_X86)
35
36 /*
37 * PadLock detection routine
38 */
mbedtls_padlock_has_support(int feature)39 int mbedtls_padlock_has_support(int feature)
40 {
41 static int flags = -1;
42 int ebx = 0, edx = 0;
43
44 if (flags == -1) {
45 asm ("movl %%ebx, %0 \n\t"
46 "movl $0xC0000000, %%eax \n\t"
47 "cpuid \n\t"
48 "cmpl $0xC0000001, %%eax \n\t"
49 "movl $0, %%edx \n\t"
50 "jb 1f \n\t"
51 "movl $0xC0000001, %%eax \n\t"
52 "cpuid \n\t"
53 "1: \n\t"
54 "movl %%edx, %1 \n\t"
55 "movl %2, %%ebx \n\t"
56 : "=m" (ebx), "=m" (edx)
57 : "m" (ebx)
58 : "eax", "ecx", "edx");
59
60 flags = edx;
61 }
62
63 return flags & feature;
64 }
65
66 /*
67 * PadLock AES-ECB block en(de)cryption
68 */
mbedtls_padlock_xcryptecb(mbedtls_aes_context * ctx,int mode,const unsigned char input[16],unsigned char output[16])69 int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
70 int mode,
71 const unsigned char input[16],
72 unsigned char output[16])
73 {
74 int ebx = 0;
75 uint32_t *rk;
76 uint32_t *blk;
77 uint32_t *ctrl;
78 unsigned char buf[256];
79
80 rk = ctx->buf + ctx->rk_offset;
81
82 if (((long) rk & 15) != 0) {
83 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
84 }
85
86 blk = MBEDTLS_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 : "memory", "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 */
mbedtls_padlock_xcryptcbc(mbedtls_aes_context * ctx,int mode,size_t length,unsigned char iv[16],const unsigned char * input,unsigned char * output)114 int mbedtls_padlock_xcryptcbc(mbedtls_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 = 0;
122 size_t count;
123 uint32_t *rk;
124 uint32_t *iw;
125 uint32_t *ctrl;
126 unsigned char buf[256];
127
128 rk = ctx->buf + ctx->rk_offset;
129
130 if (((long) input & 15) != 0 ||
131 ((long) output & 15) != 0 ||
132 ((long) rk & 15) != 0) {
133 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
134 }
135
136 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
137 memcpy(iw, iv, 16);
138
139 ctrl = iw + 4;
140 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
141
142 count = (length + 15) >> 4;
143
144 asm ("pushfl \n\t"
145 "popfl \n\t"
146 "movl %%ebx, %0 \n\t"
147 "movl %2, %%ecx \n\t"
148 "movl %3, %%edx \n\t"
149 "movl %4, %%ebx \n\t"
150 "movl %5, %%esi \n\t"
151 "movl %6, %%edi \n\t"
152 "movl %7, %%eax \n\t"
153 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
154 "movl %1, %%ebx \n\t"
155 : "=m" (ebx)
156 : "m" (ebx), "m" (count), "m" (ctrl),
157 "m" (rk), "m" (input), "m" (output), "m" (iw)
158 : "memory", "eax", "ecx", "edx", "esi", "edi");
159
160 memcpy(iv, iw, 16);
161
162 return 0;
163 }
164
165 #endif /* MBEDTLS_HAVE_X86 */
166
167 #endif /* MBEDTLS_PADLOCK_C */
168