1 /*
2 * VIA PadLock support functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
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
14 #include "common.h"
15
16 #if defined(MBEDTLS_PADLOCK_C)
17
18 #include "mbedtls/padlock.h"
19
20 #include <string.h>
21
22 /* *INDENT-OFF* */
23 #ifndef asm
24 #define asm __asm
25 #endif
26 /* *INDENT-ON* */
27
28 #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
29
30 /*
31 * PadLock detection routine
32 */
mbedtls_padlock_has_support(int feature)33 int mbedtls_padlock_has_support(int feature)
34 {
35 static int flags = -1;
36 int ebx = 0, edx = 0;
37
38 if (flags == -1) {
39 asm ("movl %%ebx, %0 \n\t"
40 "movl $0xC0000000, %%eax \n\t"
41 "cpuid \n\t"
42 "cmpl $0xC0000001, %%eax \n\t"
43 "movl $0, %%edx \n\t"
44 "jb 1f \n\t"
45 "movl $0xC0000001, %%eax \n\t"
46 "cpuid \n\t"
47 "1: \n\t"
48 "movl %%edx, %1 \n\t"
49 "movl %2, %%ebx \n\t"
50 : "=m" (ebx), "=m" (edx)
51 : "m" (ebx)
52 : "eax", "ecx", "edx");
53
54 flags = edx;
55 }
56
57 return flags & feature;
58 }
59
60 /*
61 * PadLock AES-ECB block en(de)cryption
62 */
mbedtls_padlock_xcryptecb(mbedtls_aes_context * ctx,int mode,const unsigned char input[16],unsigned char output[16])63 int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
64 int mode,
65 const unsigned char input[16],
66 unsigned char output[16])
67 {
68 int ebx = 0;
69 uint32_t *rk;
70 uint32_t *blk;
71 uint32_t *ctrl;
72 unsigned char buf[256];
73
74 rk = ctx->rk;
75 blk = MBEDTLS_PADLOCK_ALIGN16(buf);
76 memcpy(blk, input, 16);
77
78 ctrl = blk + 4;
79 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
80
81 asm ("pushfl \n\t"
82 "popfl \n\t"
83 "movl %%ebx, %0 \n\t"
84 "movl $1, %%ecx \n\t"
85 "movl %2, %%edx \n\t"
86 "movl %3, %%ebx \n\t"
87 "movl %4, %%esi \n\t"
88 "movl %4, %%edi \n\t"
89 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
90 "movl %1, %%ebx \n\t"
91 : "=m" (ebx)
92 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
93 : "memory", "ecx", "edx", "esi", "edi");
94
95 memcpy(output, blk, 16);
96
97 return 0;
98 }
99
100 /*
101 * PadLock AES-CBC buffer en(de)cryption
102 */
mbedtls_padlock_xcryptcbc(mbedtls_aes_context * ctx,int mode,size_t length,unsigned char iv[16],const unsigned char * input,unsigned char * output)103 int 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)
109 {
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
117 if (((long) input & 15) != 0 ||
118 ((long) output & 15) != 0) {
119 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
120 }
121
122 rk = ctx->rk;
123 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
124 memcpy(iw, iv, 16);
125
126 ctrl = iw + 4;
127 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
128
129 count = (length + 15) >> 4;
130
131 asm ("pushfl \n\t"
132 "popfl \n\t"
133 "movl %%ebx, %0 \n\t"
134 "movl %2, %%ecx \n\t"
135 "movl %3, %%edx \n\t"
136 "movl %4, %%ebx \n\t"
137 "movl %5, %%esi \n\t"
138 "movl %6, %%edi \n\t"
139 "movl %7, %%eax \n\t"
140 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
141 "movl %1, %%ebx \n\t"
142 : "=m" (ebx)
143 : "m" (ebx), "m" (count), "m" (ctrl),
144 "m" (rk), "m" (input), "m" (output), "m" (iw)
145 : "memory", "eax", "ecx", "edx", "esi", "edi");
146
147 memcpy(iv, iw, 16);
148
149 return 0;
150 }
151
152 #endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
153
154 #endif /* MBEDTLS_PADLOCK_C */
155