blob: 1f006910c2e90d810a16b48807295817fab2597b [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
Tom Van Eyckc1633172024-04-09 18:44:13 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +02006 */
7/*
8 * This implementation is based on the VIA PadLock Programming Guide:
9 *
10 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
11 * programming_guide.pdf
12 */
13
Jerome Forissier79013242021-07-28 10:24:04 +020014#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020015
16#if defined(MBEDTLS_PADLOCK_C)
17
Jens Wiklander32b31802023-10-06 16:59:46 +020018#include "padlock.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020019
20#include <string.h>
21
Tom Van Eyckc1633172024-04-09 18:44:13 +020022#if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
Jens Wiklander817466c2018-05-22 13:49:31 +020023
24/*
25 * PadLock detection routine
26 */
Jens Wiklander32b31802023-10-06 16:59:46 +020027int mbedtls_padlock_has_support(int feature)
Jens Wiklander817466c2018-05-22 13:49:31 +020028{
29 static int flags = -1;
30 int ebx = 0, edx = 0;
31
Jens Wiklander32b31802023-10-06 16:59:46 +020032 if (flags == -1) {
33 asm ("movl %%ebx, %0 \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020034 "movl $0xC0000000, %%eax \n\t"
35 "cpuid \n\t"
36 "cmpl $0xC0000001, %%eax \n\t"
37 "movl $0, %%edx \n\t"
Jerome Forissier79013242021-07-28 10:24:04 +020038 "jb 1f \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020039 "movl $0xC0000001, %%eax \n\t"
40 "cpuid \n\t"
Jerome Forissier79013242021-07-28 10:24:04 +020041 "1: \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020042 "movl %%edx, %1 \n\t"
43 "movl %2, %%ebx \n\t"
44 : "=m" (ebx), "=m" (edx)
45 : "m" (ebx)
Jens Wiklander32b31802023-10-06 16:59:46 +020046 : "eax", "ecx", "edx");
Jens Wiklander817466c2018-05-22 13:49:31 +020047
48 flags = edx;
49 }
50
Jens Wiklander32b31802023-10-06 16:59:46 +020051 return flags & feature;
Jens Wiklander817466c2018-05-22 13:49:31 +020052}
53
54/*
55 * PadLock AES-ECB block en(de)cryption
56 */
Jens Wiklander32b31802023-10-06 16:59:46 +020057int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
58 int mode,
59 const unsigned char input[16],
60 unsigned char output[16])
Jens Wiklander817466c2018-05-22 13:49:31 +020061{
62 int ebx = 0;
63 uint32_t *rk;
64 uint32_t *blk;
65 uint32_t *ctrl;
66 unsigned char buf[256];
67
Jens Wiklander32b31802023-10-06 16:59:46 +020068 rk = ctx->buf + ctx->rk_offset;
Jens Wiklander817466c2018-05-22 13:49:31 +020069
Jens Wiklander32b31802023-10-06 16:59:46 +020070 if (((long) rk & 15) != 0) {
71 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
72 }
Jens Wiklander817466c2018-05-22 13:49:31 +020073
Jens Wiklander32b31802023-10-06 16:59:46 +020074 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
75 memcpy(blk, input, 16);
76
77 ctrl = blk + 4;
78 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
79
80 asm ("pushfl \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +020081 "popfl \n\t"
82 "movl %%ebx, %0 \n\t"
83 "movl $1, %%ecx \n\t"
84 "movl %2, %%edx \n\t"
85 "movl %3, %%ebx \n\t"
86 "movl %4, %%esi \n\t"
87 "movl %4, %%edi \n\t"
88 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
89 "movl %1, %%ebx \n\t"
90 : "=m" (ebx)
91 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Jens Wiklander32b31802023-10-06 16:59:46 +020092 : "memory", "ecx", "edx", "esi", "edi");
Jens Wiklander817466c2018-05-22 13:49:31 +020093
Jens Wiklander32b31802023-10-06 16:59:46 +020094 memcpy(output, blk, 16);
Jens Wiklander817466c2018-05-22 13:49:31 +020095
Jens Wiklander32b31802023-10-06 16:59:46 +020096 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +020097}
98
Tom Van Eyckc1633172024-04-09 18:44:13 +020099#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander817466c2018-05-22 13:49:31 +0200100/*
101 * PadLock AES-CBC buffer en(de)cryption
102 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200103int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
104 int mode,
105 size_t length,
106 unsigned char iv[16],
107 const unsigned char *input,
108 unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200109{
110 int ebx = 0;
111 size_t count;
112 uint32_t *rk;
113 uint32_t *iw;
114 uint32_t *ctrl;
115 unsigned char buf[256];
116
Jens Wiklander32b31802023-10-06 16:59:46 +0200117 rk = ctx->buf + ctx->rk_offset;
Jens Wiklander817466c2018-05-22 13:49:31 +0200118
Jens Wiklander32b31802023-10-06 16:59:46 +0200119 if (((long) input & 15) != 0 ||
120 ((long) output & 15) != 0 ||
121 ((long) rk & 15) != 0) {
122 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
123 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200124
Jens Wiklander32b31802023-10-06 16:59:46 +0200125 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
126 memcpy(iw, iv, 16);
Jens Wiklander817466c2018-05-22 13:49:31 +0200127
Jens Wiklander32b31802023-10-06 16:59:46 +0200128 ctrl = iw + 4;
129 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
Jens Wiklander817466c2018-05-22 13:49:31 +0200130
Jens Wiklander32b31802023-10-06 16:59:46 +0200131 count = (length + 15) >> 4;
132
133 asm ("pushfl \n\t"
Jens Wiklander817466c2018-05-22 13:49:31 +0200134 "popfl \n\t"
135 "movl %%ebx, %0 \n\t"
136 "movl %2, %%ecx \n\t"
137 "movl %3, %%edx \n\t"
138 "movl %4, %%ebx \n\t"
139 "movl %5, %%esi \n\t"
140 "movl %6, %%edi \n\t"
141 "movl %7, %%eax \n\t"
142 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
143 "movl %1, %%ebx \n\t"
144 : "=m" (ebx)
145 : "m" (ebx), "m" (count), "m" (ctrl),
Jens Wiklander32b31802023-10-06 16:59:46 +0200146 "m" (rk), "m" (input), "m" (output), "m" (iw)
147 : "memory", "eax", "ecx", "edx", "esi", "edi");
Jens Wiklander817466c2018-05-22 13:49:31 +0200148
Jens Wiklander32b31802023-10-06 16:59:46 +0200149 memcpy(iv, iw, 16);
Jens Wiklander817466c2018-05-22 13:49:31 +0200150
Jens Wiklander32b31802023-10-06 16:59:46 +0200151 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200152}
Tom Van Eyckc1633172024-04-09 18:44:13 +0200153#endif /* MBEDTLS_CIPHER_MODE_CBC */
Jens Wiklander817466c2018-05-22 13:49:31 +0200154
Tom Van Eyckc1633172024-04-09 18:44:13 +0200155#endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
Jens Wiklander817466c2018-05-22 13:49:31 +0200156
157#endif /* MBEDTLS_PADLOCK_C */