blob: 1f006910c2e90d810a16b48807295817fab2597b [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020014#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000017
Chris Jones16dbaeb2021-03-09 17:47:55 +000018#include "padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000019
Manuel Pégourié-Gonnard45ec8da2015-02-10 13:50:47 +000020#include <string.h>
21
Jerry Yu782b9662023-08-21 11:25:01 +080022#if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Paul Bakker5121ce52009-01-03 21:22:43 +000024/*
25 * PadLock detection routine
26 */
Gilles Peskine449bd832023-01-11 14:50:10 +010027int mbedtls_padlock_has_support(int feature)
Paul Bakker5121ce52009-01-03 21:22:43 +000028{
29 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010030 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 if (flags == -1) {
33 asm ("movl %%ebx, %0 \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020034 "movl $0xC0000000, %%eax \n\t"
35 "cpuid \n\t"
36 "cmpl $0xC0000001, %%eax \n\t"
37 "movl $0, %%edx \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080038 "jb 1f \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020039 "movl $0xC0000001, %%eax \n\t"
40 "cpuid \n\t"
okhowang(王沛文)0c4bbda2020-06-24 16:02:10 +080041 "1: \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020042 "movl %%edx, %1 \n\t"
43 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000044 : "=m" (ebx), "=m" (edx)
45 : "m" (ebx)
Gilles Peskine449bd832023-01-11 14:50:10 +010046 : "eax", "ecx", "edx");
Paul Bakker5121ce52009-01-03 21:22:43 +000047
48 flags = edx;
49 }
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051 return flags & feature;
Paul Bakker5121ce52009-01-03 21:22:43 +000052}
53
54/*
55 * PadLock AES-ECB block en(de)cryption
56 */
Gilles Peskine449bd832023-01-11 14:50:10 +010057int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
58 int mode,
59 const unsigned char input[16],
60 unsigned char output[16])
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
Paul Bakker53e15132013-12-30 20:43:40 +010062 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000063 uint32_t *rk;
64 uint32_t *blk;
65 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000066 unsigned char buf[256];
67
Werner Lewisc1999d52022-07-05 11:55:15 +010068 rk = ctx->buf + ctx->rk_offset;
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (((long) rk & 15) != 0) {
71 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
72 }
Werner Lewisc1999d52022-07-05 11:55:15 +010073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
75 memcpy(blk, input, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 ctrl = blk + 4;
78 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +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"
Paul Bakker5121ce52009-01-03 21:22:43 +000090 : "=m" (ebx)
91 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Gilles Peskine449bd832023-01-11 14:50:10 +010092 : "memory", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 memcpy(output, blk, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000097}
98
Yanray Wang0287b9d2023-11-10 18:21:21 +080099#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100/*
101 * PadLock AES-CBC buffer en(de)cryption
102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int 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)
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
Paul Bakker53e15132013-12-30 20:43:40 +0100110 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000111 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000112 uint32_t *rk;
113 uint32_t *iw;
114 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 unsigned char buf[256];
116
Werner Lewisc1999d52022-07-05 11:55:15 +0100117 rk = ctx->buf + ctx->rk_offset;
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (((long) input & 15) != 0 ||
120 ((long) output & 15) != 0 ||
121 ((long) rk & 15) != 0) {
122 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
123 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
126 memcpy(iw, iv, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 ctrl = iw + 4;
129 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 count = (length + 15) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 asm ("pushfl \n\t"
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +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"
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 : "=m" (ebx)
145 : "m" (ebx), "m" (count), "m" (ctrl),
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 "m" (rk), "m" (input), "m" (output), "m" (iw)
147 : "memory", "eax", "ecx", "edx", "esi", "edi");
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 memcpy(iv, iw, 16);
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000152}
Yanray Wang0287b9d2023-11-10 18:21:21 +0800153#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Jerry Yu782b9662023-08-21 11:25:01 +0800155#endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_PADLOCK_C */