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 "padlock.h"
19
20 #include <string.h>
21
22 #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
23
24 /*
25 * PadLock detection routine
26 */
mbedtls_padlock_has_support(int feature)27 int mbedtls_padlock_has_support(int feature)
28 {
29 static int flags = -1;
30 int ebx = 0, edx = 0;
31
32 if (flags == -1) {
33 asm ("movl %%ebx, %0 \n\t"
34 "movl $0xC0000000, %%eax \n\t"
35 "cpuid \n\t"
36 "cmpl $0xC0000001, %%eax \n\t"
37 "movl $0, %%edx \n\t"
38 "jb 1f \n\t"
39 "movl $0xC0000001, %%eax \n\t"
40 "cpuid \n\t"
41 "1: \n\t"
42 "movl %%edx, %1 \n\t"
43 "movl %2, %%ebx \n\t"
44 : "=m" (ebx), "=m" (edx)
45 : "m" (ebx)
46 : "eax", "ecx", "edx");
47
48 flags = edx;
49 }
50
51 return flags & feature;
52 }
53
54 /*
55 * PadLock AES-ECB block en(de)cryption
56 */
mbedtls_padlock_xcryptecb(mbedtls_aes_context * ctx,int mode,const unsigned char input[16],unsigned char output[16])57 int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
58 int mode,
59 const unsigned char input[16],
60 unsigned char output[16])
61 {
62 int ebx = 0;
63 uint32_t *rk;
64 uint32_t *blk;
65 uint32_t *ctrl;
66 unsigned char buf[256];
67
68 rk = ctx->buf + ctx->rk_offset;
69
70 if (((long) rk & 15) != 0) {
71 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
72 }
73
74 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"
81 "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)
92 : "memory", "ecx", "edx", "esi", "edi");
93
94 memcpy(output, blk, 16);
95
96 return 0;
97 }
98
99 /*
100 * PadLock AES-CBC buffer en(de)cryption
101 */
mbedtls_padlock_xcryptcbc(mbedtls_aes_context * ctx,int mode,size_t length,unsigned char iv[16],const unsigned char * input,unsigned char * output)102 int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
103 int mode,
104 size_t length,
105 unsigned char iv[16],
106 const unsigned char *input,
107 unsigned char *output)
108 {
109 int ebx = 0;
110 size_t count;
111 uint32_t *rk;
112 uint32_t *iw;
113 uint32_t *ctrl;
114 unsigned char buf[256];
115
116 rk = ctx->buf + ctx->rk_offset;
117
118 if (((long) input & 15) != 0 ||
119 ((long) output & 15) != 0 ||
120 ((long) rk & 15) != 0) {
121 return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
122 }
123
124 iw = MBEDTLS_PADLOCK_ALIGN16(buf);
125 memcpy(iw, iv, 16);
126
127 ctrl = iw + 4;
128 *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
129
130 count = (length + 15) >> 4;
131
132 asm ("pushfl \n\t"
133 "popfl \n\t"
134 "movl %%ebx, %0 \n\t"
135 "movl %2, %%ecx \n\t"
136 "movl %3, %%edx \n\t"
137 "movl %4, %%ebx \n\t"
138 "movl %5, %%esi \n\t"
139 "movl %6, %%edi \n\t"
140 "movl %7, %%eax \n\t"
141 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
142 "movl %1, %%ebx \n\t"
143 : "=m" (ebx)
144 : "m" (ebx), "m" (count), "m" (ctrl),
145 "m" (rk), "m" (input), "m" (output), "m" (iw)
146 : "memory", "eax", "ecx", "edx", "esi", "edi");
147
148 memcpy(iv, iw, 16);
149
150 return 0;
151 }
152
153 #endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
154
155 #endif /* MBEDTLS_PADLOCK_C */
156