blob: f42c40ff93e5246e913d9704c1629c7baea4f030 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * VIA PadLock support functions
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
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
Jerome Forissier79013242021-07-28 10:24:04 +020026#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020027
28#if defined(MBEDTLS_PADLOCK_C)
29
Jens Wiklander32b31802023-10-06 16:59:46 +020030#include "padlock.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020031
32#include <string.h>
33
Jens Wiklander817466c2018-05-22 13:49:31 +020034#if defined(MBEDTLS_HAVE_X86)
35
36/*
37 * PadLock detection routine
38 */
Jens Wiklander32b31802023-10-06 16:59:46 +020039int mbedtls_padlock_has_support(int feature)
Jens Wiklander817466c2018-05-22 13:49:31 +020040{
41 static int flags = -1;
42 int ebx = 0, edx = 0;
43
Jens Wiklander32b31802023-10-06 16:59:46 +020044 if (flags == -1) {
45 asm ("movl %%ebx, %0 \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020046 "movl $0xC0000000, %%eax \n\t"
47 "cpuid \n\t"
48 "cmpl $0xC0000001, %%eax \n\t"
49 "movl $0, %%edx \n\t"
Jerome Forissier79013242021-07-28 10:24:04 +020050 "jb 1f \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020051 "movl $0xC0000001, %%eax \n\t"
52 "cpuid \n\t"
Jerome Forissier79013242021-07-28 10:24:04 +020053 "1: \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020054 "movl %%edx, %1 \n\t"
55 "movl %2, %%ebx \n\t"
56 : "=m" (ebx), "=m" (edx)
57 : "m" (ebx)
Jens Wiklander32b31802023-10-06 16:59:46 +020058 : "eax", "ecx", "edx");
Jens Wiklander817466c2018-05-22 13:49:31 +020059
60 flags = edx;
61 }
62
Jens Wiklander32b31802023-10-06 16:59:46 +020063 return flags & feature;
Jens Wiklander817466c2018-05-22 13:49:31 +020064}
65
66/*
67 * PadLock AES-ECB block en(de)cryption
68 */
Jens Wiklander32b31802023-10-06 16:59:46 +020069int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
70 int mode,
71 const unsigned char input[16],
72 unsigned char output[16])
Jens Wiklander817466c2018-05-22 13:49:31 +020073{
74 int ebx = 0;
75 uint32_t *rk;
76 uint32_t *blk;
77 uint32_t *ctrl;
78 unsigned char buf[256];
79
Jens Wiklander32b31802023-10-06 16:59:46 +020080 rk = ctx->buf + ctx->rk_offset;
Jens Wiklander817466c2018-05-22 13:49:31 +020081
Jens Wiklander32b31802023-10-06 16:59:46 +020082 if (((long) rk & 15) != 0) {
83 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
84 }
Jens Wiklander817466c2018-05-22 13:49:31 +020085
Jens Wiklander32b31802023-10-06 16:59:46 +020086 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"
Jens Wiklander817466c2018-05-22 13:49:31 +020093 "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)
Jens Wiklander32b31802023-10-06 16:59:46 +0200104 : "memory", "ecx", "edx", "esi", "edi");
Jens Wiklander817466c2018-05-22 13:49:31 +0200105
Jens Wiklander32b31802023-10-06 16:59:46 +0200106 memcpy(output, blk, 16);
Jens Wiklander817466c2018-05-22 13:49:31 +0200107
Jens Wiklander32b31802023-10-06 16:59:46 +0200108 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200109}
110
111/*
112 * PadLock AES-CBC buffer en(de)cryption
113 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200114int 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)
Jens Wiklander817466c2018-05-22 13:49:31 +0200120{
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
Jens Wiklander32b31802023-10-06 16:59:46 +0200128 rk = ctx->buf + ctx->rk_offset;
Jens Wiklander817466c2018-05-22 13:49:31 +0200129
Jens Wiklander32b31802023-10-06 16:59:46 +0200130 if (((long) input & 15) != 0 ||
131 ((long) output & 15) != 0 ||
132 ((long) rk & 15) != 0) {
133 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
134 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200135
Jens Wiklander32b31802023-10-06 16:59:46 +0200136 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
137 memcpy(iw, iv, 16);
Jens Wiklander817466c2018-05-22 13:49:31 +0200138
Jens Wiklander32b31802023-10-06 16:59:46 +0200139 ctrl = iw + 4;
140 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
Jens Wiklander817466c2018-05-22 13:49:31 +0200141
Jens Wiklander32b31802023-10-06 16:59:46 +0200142 count = (length + 15) >> 4;
143
144 asm ("pushfl \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +0200145 "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),
Jens Wiklander32b31802023-10-06 16:59:46 +0200157 "m" (rk), "m" (input), "m" (output), "m" (iw)
158 : "memory", "eax", "ecx", "edx", "esi", "edi");
Jens Wiklander817466c2018-05-22 13:49:31 +0200159
Jens Wiklander32b31802023-10-06 16:59:46 +0200160 memcpy(iv, iw, 16);
Jens Wiklander817466c2018-05-22 13:49:31 +0200161
Jens Wiklander32b31802023-10-06 16:59:46 +0200162 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200163}
164
165#endif /* MBEDTLS_HAVE_X86 */
166
167#endif /* MBEDTLS_PADLOCK_C */