blob: 38d110e0b351e9f22e27ed9906d8b461a4fa8316 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Chris Jones16dbaeb2021-03-09 17:47:55 +000030#include "padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard45ec8da2015-02-10 13:50:47 +000032#include <string.h>
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Jerry Yub241db32023-08-04 16:28:22 +080036#if defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
37#error "MBEDTLS_PADLOCK_C defined, but not all prerequisites"
38#endif
39
Paul Bakker5121ce52009-01-03 21:22:43 +000040/*
41 * PadLock detection routine
42 */
Gilles Peskine449bd832023-01-11 14:50:10 +010043int mbedtls_padlock_has_support(int feature)
Paul Bakker5121ce52009-01-03 21:22:43 +000044{
45 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010046 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (flags == -1) {
49 asm ("movl %%ebx, %0 \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020050 "movl $0xC0000000, %%eax \n\t"
51 "cpuid \n\t"
52 "cmpl $0xC0000001, %%eax \n\t"
53 "movl $0, %%edx \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080054 "jb 1f \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020055 "movl $0xC0000001, %%eax \n\t"
56 "cpuid \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080057 "1: \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020058 "movl %%edx, %1 \n\t"
59 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000060 : "=m" (ebx), "=m" (edx)
61 : "m" (ebx)
Gilles Peskine449bd832023-01-11 14:50:10 +010062 : "eax", "ecx", "edx");
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64 flags = edx;
65 }
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 return flags & feature;
Paul Bakker5121ce52009-01-03 21:22:43 +000068}
69
70/*
71 * PadLock AES-ECB block en(de)cryption
72 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
74 int mode,
75 const unsigned char input[16],
76 unsigned char output[16])
Paul Bakker5121ce52009-01-03 21:22:43 +000077{
Paul Bakker53e15132013-12-30 20:43:40 +010078 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000079 uint32_t *rk;
80 uint32_t *blk;
81 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000082 unsigned char buf[256];
83
Werner Lewisc1999d52022-07-05 11:55:15 +010084 rk = ctx->buf + ctx->rk_offset;
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (((long) rk & 15) != 0) {
87 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
88 }
Werner Lewisc1999d52022-07-05 11:55:15 +010089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
91 memcpy(blk, input, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 ctrl = blk + 4;
94 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020097 "popfl \n\t"
98 "movl %%ebx, %0 \n\t"
99 "movl $1, %%ecx \n\t"
100 "movl %2, %%edx \n\t"
101 "movl %3, %%ebx \n\t"
102 "movl %4, %%esi \n\t"
103 "movl %4, %%edi \n\t"
104 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
105 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 : "=m" (ebx)
107 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 : "memory", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 memcpy(output, blk, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000113}
114
115/*
116 * PadLock AES-CBC buffer en(de)cryption
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
119 int mode,
120 size_t length,
121 unsigned char iv[16],
122 const unsigned char *input,
123 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000124{
Paul Bakker53e15132013-12-30 20:43:40 +0100125 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000126 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000127 uint32_t *rk;
128 uint32_t *iw;
129 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 unsigned char buf[256];
131
Werner Lewisc1999d52022-07-05 11:55:15 +0100132 rk = ctx->buf + ctx->rk_offset;
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (((long) input & 15) != 0 ||
135 ((long) output & 15) != 0 ||
136 ((long) rk & 15) != 0) {
137 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
138 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
141 memcpy(iw, iv, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 ctrl = iw + 4;
144 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 count = (length + 15) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200149 "popfl \n\t"
150 "movl %%ebx, %0 \n\t"
151 "movl %2, %%ecx \n\t"
152 "movl %3, %%edx \n\t"
153 "movl %4, %%ebx \n\t"
154 "movl %5, %%esi \n\t"
155 "movl %6, %%edi \n\t"
156 "movl %7, %%eax \n\t"
157 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
158 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 : "=m" (ebx)
160 : "m" (ebx), "m" (count), "m" (ctrl),
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 "m" (rk), "m" (input), "m" (output), "m" (iw)
162 : "memory", "eax", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 memcpy(iv, iw, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_PADLOCK_C */