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
28 #if defined(MBEDTLS_PADLOCK_C)
30 #include "mbedtls/padlock.h"
38 #if defined(MBEDTLS_HAVE_X86)
41 * PadLock detection routine
43 int mbedtls_padlock_has_support(int feature
) {
44 static int flags
= -1;
48 asm("movl %%ebx, %0 \n\t"
49 "movl $0xC0000000, %%eax \n\t"
51 "cmpl $0xC0000001, %%eax \n\t"
54 "movl $0xC0000001, %%eax \n\t"
59 : "=m"(ebx
), "=m"(edx
)
61 : "eax", "ecx", "edx");
66 return (flags
& feature
);
70 * PadLock AES-ECB block en(de)cryption
72 int mbedtls_padlock_xcryptecb(mbedtls_aes_context
*ctx
,
74 const unsigned char input
[16],
75 unsigned char output
[16]) {
80 unsigned char buf
[256];
83 blk
= MBEDTLS_PADLOCK_ALIGN16(buf
);
84 memcpy(blk
, input
, 16);
87 *ctrl
= 0x80 | ctx
->nr
| ((ctx
->nr
+ (mode
^ 1) - 10) << 9);
97 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
100 : "m"(ebx
), "m"(ctrl
), "m"(rk
), "m"(blk
)
101 : "memory", "ecx", "edx", "esi", "edi");
103 memcpy(output
, blk
, 16);
109 * PadLock AES-CBC buffer en(de)cryption
111 int mbedtls_padlock_xcryptcbc(mbedtls_aes_context
*ctx
,
114 unsigned char iv
[16],
115 const unsigned char *input
,
116 unsigned char *output
) {
122 unsigned char buf
[256];
124 if (((long) input
& 15) != 0 ||
125 ((long) output
& 15) != 0)
126 return (MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED
);
129 iw
= MBEDTLS_PADLOCK_ALIGN16(buf
);
133 *ctrl
= 0x80 | ctx
->nr
| ((ctx
->nr
+ (mode
^ 1) - 10) << 9);
135 count
= (length
+ 15) >> 4;
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"
149 : "m"(ebx
), "m"(count
), "m"(ctrl
),
150 "m"(rk
), "m"(input
), "m"(output
), "m"(iw
)
151 : "memory", "eax", "ecx", "edx", "esi", "edi");
158 #endif /* MBEDTLS_HAVE_X86 */
160 #endif /* MBEDTLS_PADLOCK_C */