blob: 0ea4129682fc76ccd90e2d5474443eef493f57c2 [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
Jerry Yu782b9662023-08-21 11:25:01 +080034#if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker5121ce52009-01-03 21:22:43 +000036/*
37 * PadLock detection routine
38 */
Gilles Peskine449bd832023-01-11 14:50:10 +010039int mbedtls_padlock_has_support(int feature)
Paul Bakker5121ce52009-01-03 21:22:43 +000040{
41 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010042 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 if (flags == -1) {
45 asm ("movl %%ebx, %0 \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020046 "movl $0xC0000000, %%eax \n\t"
47 "cpuid \n\t"
48 "cmpl $0xC0000001, %%eax \n\t"
49 "movl $0, %%edx \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080050 "jb 1f \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020051 "movl $0xC0000001, %%eax \n\t"
52 "cpuid \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080053 "1: \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020054 "movl %%edx, %1 \n\t"
55 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000056 : "=m" (ebx), "=m" (edx)
57 : "m" (ebx)
Gilles Peskine449bd832023-01-11 14:50:10 +010058 : "eax", "ecx", "edx");
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60 flags = edx;
61 }
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 return flags & feature;
Paul Bakker5121ce52009-01-03 21:22:43 +000064}
65
66/*
67 * PadLock AES-ECB block en(de)cryption
68 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
70 int mode,
71 const unsigned char input[16],
72 unsigned char output[16])
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
Paul Bakker53e15132013-12-30 20:43:40 +010074 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000075 uint32_t *rk;
76 uint32_t *blk;
77 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000078 unsigned char buf[256];
79
Werner Lewisc1999d52022-07-05 11:55:15 +010080 rk = ctx->buf + ctx->rk_offset;
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (((long) rk & 15) != 0) {
83 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
84 }
Werner Lewisc1999d52022-07-05 11:55:15 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
87 memcpy(blk, input, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 ctrl = blk + 4;
90 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +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"
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 : "=m" (ebx)
103 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 : "memory", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 memcpy(output, blk, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000109}
110
Yanray Wang0287b9d2023-11-10 18:21:21 +0800111#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000112/*
113 * PadLock AES-CBC buffer en(de)cryption
114 */
Gilles Peskine449bd832023-01-11 14:50:10 +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
Werner Lewisc1999d52022-07-05 11:55:15 +0100129 rk = ctx->buf + ctx->rk_offset;
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (((long) input & 15) != 0 ||
132 ((long) output & 15) != 0 ||
133 ((long) rk & 15) != 0) {
134 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
135 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
138 memcpy(iw, iv, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 ctrl = iw + 4;
141 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 count = (length + 15) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200146 "popfl \n\t"
147 "movl %%ebx, %0 \n\t"
148 "movl %2, %%ecx \n\t"
149 "movl %3, %%edx \n\t"
150 "movl %4, %%ebx \n\t"
151 "movl %5, %%esi \n\t"
152 "movl %6, %%edi \n\t"
153 "movl %7, %%eax \n\t"
154 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
155 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 : "=m" (ebx)
157 : "m" (ebx), "m" (count), "m" (ctrl),
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 "m" (rk), "m" (input), "m" (output), "m" (iw)
159 : "memory", "eax", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 memcpy(iv, iw, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164}
Yanray Wang0287b9d2023-11-10 18:21:21 +0800165#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Jerry Yu782b9662023-08-21 11:25:01 +0800167#endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_PADLOCK_C */