blob: 303f82c7103853ae2ca4be68181cd124466eb4e3 [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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard45ec8da2015-02-10 13:50:47 +000032#include <string.h>
33
David Horstmannb6bf5f52023-01-03 11:07:09 +000034/* *INDENT-OFF* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020035#ifndef asm
36#define asm __asm
37#endif
David Horstmannb6bf5f52023-01-03 11:07:09 +000038/* *INDENT-ON* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker5121ce52009-01-03 21:22:43 +000042/*
43 * PadLock detection routine
44 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045int mbedtls_padlock_has_support(int feature)
Paul Bakker5121ce52009-01-03 21:22:43 +000046{
47 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010048 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 if (flags == -1) {
51 asm ("movl %%ebx, %0 \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020052 "movl $0xC0000000, %%eax \n\t"
53 "cpuid \n\t"
54 "cmpl $0xC0000001, %%eax \n\t"
55 "movl $0, %%edx \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080056 "jb 1f \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020057 "movl $0xC0000001, %%eax \n\t"
58 "cpuid \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080059 "1: \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020060 "movl %%edx, %1 \n\t"
61 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000062 : "=m" (ebx), "=m" (edx)
63 : "m" (ebx)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 : "eax", "ecx", "edx");
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66 flags = edx;
67 }
68
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 return flags & feature;
Paul Bakker5121ce52009-01-03 21:22:43 +000070}
71
72/*
73 * PadLock AES-ECB block en(de)cryption
74 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
76 int mode,
77 const unsigned char input[16],
78 unsigned char output[16])
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
Paul Bakker53e15132013-12-30 20:43:40 +010080 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000081 uint32_t *rk;
82 uint32_t *blk;
83 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000084 unsigned char buf[256];
85
86 rk = ctx->rk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
88 memcpy(blk, input, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 ctrl = blk + 4;
91 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020094 "popfl \n\t"
95 "movl %%ebx, %0 \n\t"
96 "movl $1, %%ecx \n\t"
97 "movl %2, %%edx \n\t"
98 "movl %3, %%ebx \n\t"
99 "movl %4, %%esi \n\t"
100 "movl %4, %%edi \n\t"
101 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
102 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 : "=m" (ebx)
104 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 : "memory", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 memcpy(output, blk, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * PadLock AES-CBC buffer en(de)cryption
114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
116 int mode,
117 size_t length,
118 unsigned char iv[16],
119 const unsigned char *input,
120 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121{
Paul Bakker53e15132013-12-30 20:43:40 +0100122 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000123 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000124 uint32_t *rk;
125 uint32_t *iw;
126 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 unsigned char buf[256];
128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 if (((long) input & 15) != 0 ||
130 ((long) output & 15) != 0) {
131 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
132 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134 rk = ctx->rk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
136 memcpy(iw, iv, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 ctrl = iw + 4;
139 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 count = (length + 15) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200144 "popfl \n\t"
145 "movl %%ebx, %0 \n\t"
146 "movl %2, %%ecx \n\t"
147 "movl %3, %%edx \n\t"
148 "movl %4, %%ebx \n\t"
149 "movl %5, %%esi \n\t"
150 "movl %6, %%edi \n\t"
151 "movl %7, %%eax \n\t"
152 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
153 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 : "=m" (ebx)
155 : "m" (ebx), "m" (count), "m" (ctrl),
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 "m" (rk), "m" (input), "m" (output), "m" (iw)
157 : "memory", "eax", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 memcpy(iv, iw, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_PADLOCK_C */