blob: ba9adc95cb65b5642aa00b049f4cb83df4cbccdd [file] [log] [blame]
Jerry Yu49231312023-01-10 16:57:21 +08001/*
2 * Arm64 crypto engine support functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * 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.
18 */
19
20#include <string.h>
21#include "common.h"
22
23#if defined(MBEDTLS_AESCE_C)
24
25#include "aesce.h"
26
27#if defined(MBEDTLS_HAVE_ARM64)
28
29#if defined(__clang__)
30# if __clang_major__ < 4
31# error "A more recent Clang is required for MBEDTLS_AES_C"
32# endif
33#elif defined(__GNUC__)
34# if __GNUC__ < 6
35# error "A more recent GCC is required for MBEDTLS_AES_C"
36# endif
37#else
38# error "Only GCC and Clang supported for MBEDTLS_AES_C"
39#endif
40
41#if !defined(__ARM_FEATURE_CRYPTO)
42# error "`crypto` feature moddifier MUST be enabled for MBEDTLS_AESCE_C."
43# error "Typical option for GCC and Clang is `-march=armv8-a+crypto`."
44#endif /* !__ARM_FEATURE_CRYPTO */
45
46#include <arm_neon.h>
47
Jerry Yub95c7762023-01-10 16:59:51 +080048#if defined(__linux__)
49#include <asm/hwcap.h>
50#include <sys/auxv.h>
51#endif
52
53/*
54 * AES instruction support detection routine
55 */
56int mbedtls_aesce_has_support(void)
57{
58#if defined(__linux__)
59 unsigned long auxval = getauxval(AT_HWCAP);
60 return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
61 (HWCAP_ASIMD | HWCAP_AES);
62#else
63 /* Suppose aes instructions are supported. */
64 return 1;
65#endif
66}
67
Jerry Yu3f2fb712023-01-10 17:05:42 +080068
Jerry Yue096da12023-01-10 17:07:01 +080069/*
70 * Compute decryption round keys from encryption round keys
71 */
72void mbedtls_aesce_inverse_key(unsigned char *invkey,
73 const unsigned char *fwdkey,
74 int nr)
75{
76 int i, j;
77 j = nr;
78 vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
79 for (i = 1, j--; j > 0; i++, j--) {
80 vst1q_u8(invkey + i * 16,
81 vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
82 }
83 vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
84
85}
86
Jerry Yu3f2fb712023-01-10 17:05:42 +080087static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
88 0x20, 0x40, 0x80, 0x1b, 0x36 };
89
90static inline uint32_t ror32_8(uint32_t word)
91{
92 return (word << (32 - 8)) | (word >> 8);
93}
94
95static inline uint32_t aes_sub(uint32_t in)
96{
97 uint32x4_t _in = vdupq_n_u32(in);
98 uint32x4_t v;
99 uint8x16_t zero = vdupq_n_u8(0);
100 v = vreinterpretq_u32_u8(vaeseq_u8(zero, vreinterpretq_u8_u32(_in)));
101 return vgetq_lane_u32(v, 0);
102}
103
104/*
105 * Key expansion, 128-bit case
106 */
107static void aesce_setkey_enc_128(unsigned char *rk,
108 const unsigned char *key)
109{
110 uint32_t *rki;
111 uint32_t *rko;
112 uint32_t *rk_u32 = (uint32_t *) rk;
113 memcpy(rk, key, (128 / 8));
114
115 for (size_t i = 0; i < sizeof(rcon); i++) {
116 rki = rk_u32 + i * (128 / 32);
117 rko = rki + (128 / 32);
118 rko[0] = ror32_8(aes_sub(rki[(128 / 32) - 1])) ^ rcon[i] ^ rki[0];
119 rko[1] = rko[0] ^ rki[1];
120 rko[2] = rko[1] ^ rki[2];
121 rko[3] = rko[2] ^ rki[3];
122 }
123}
124
125/*
126 * Key expansion, 192-bit case
127 */
128static void aesce_setkey_enc_192(unsigned char *rk,
129 const unsigned char *key)
130{
131 uint32_t *rki;
132 uint32_t *rko;
133 uint32_t *rk_u32 = (uint32_t *) rk;
134 memcpy(rk, key, (192 / 8));
135
136 for (size_t i = 0; i < 8; i++) {
137 rki = rk_u32 + i * (192 / 32);
138 rko = rki + (192 / 32);
139 rko[0] = ror32_8(aes_sub(rki[(192 / 32) - 1])) ^ rcon[i] ^ rki[0];
140 rko[1] = rko[0] ^ rki[1];
141 rko[2] = rko[1] ^ rki[2];
142 rko[3] = rko[2] ^ rki[3];
143 if (i < 7) {
144 rko[4] = rko[3] ^ rki[4];
145 rko[5] = rko[4] ^ rki[5];
146 }
147 }
148}
149
150/*
151 * Key expansion, 256-bit case
152 */
153static void aesce_setkey_enc_256(unsigned char *rk,
154 const unsigned char *key)
155{
156 uint32_t *rki;
157 uint32_t *rko;
158 uint32_t *rk_u32 = (uint32_t *) rk;
159 memcpy(rk, key, (256 / 8));
160
161 for (size_t i = 0; i < 7; i++) {
162 rki = rk_u32 + i * (256 / 32);
163 rko = rki + (256 / 32);
164 rko[0] = ror32_8(aes_sub(rki[(256 / 32) - 1])) ^ rcon[i] ^ rki[0];
165 rko[1] = rko[0] ^ rki[1];
166 rko[2] = rko[1] ^ rki[2];
167 rko[3] = rko[2] ^ rki[3];
168 if (i < 6) {
169 rko[4] = aes_sub(rko[3]) ^ rki[4];
170 rko[5] = rko[4] ^ rki[5];
171 rko[6] = rko[5] ^ rki[6];
172 rko[7] = rko[6] ^ rki[7];
173 }
174 }
175}
176
177/*
178 * Key expansion, wrapper
179 */
180int mbedtls_aesce_setkey_enc(unsigned char *rk,
181 const unsigned char *key,
182 size_t bits)
183{
184 switch (bits) {
185 case 128: aesce_setkey_enc_128(rk, key); break;
186 case 192: aesce_setkey_enc_192(rk, key); break;
187 case 256: aesce_setkey_enc_256(rk, key); break;
188 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
189 }
190
191 return 0;
192}
193
Jerry Yu49231312023-01-10 16:57:21 +0800194#endif /* MBEDTLS_HAVE_ARM64 */
195
196#endif /* MBEDTLS_AESCE_C */