Debian Codegraph @

cipher/aria.c

raw
1 /* aria.c - ARIA Cipher Algorithm
2 *
3 * Copyright (C) 2022-2023 Taehee Yoo <[email protected]>
4 * Copyright (C) 2023 Jussi Kivilinna <[email protected]>
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23
24 #include "types.h"
25 #include "g10lib.h"
26 #include "cipher.h"
27 #include "bufhelp.h"
28 #include "cipher-internal.h"
29 #include "bulkhelp.h"
30
31 /* Attribute macro to force alignment to 64 bytes. */
32 #ifdef HAVE_GCC_ATTRIBUTE_ALIGNED
33 # define ATTR_ALIGNED_64 __attribute__ ((aligned (64)))
34 #else
35 # define ATTR_ALIGNED_64
36 #endif
37
38 /* Attribute macro to force inlining of function. */
39 #if __GNUC__ >= 4
40 # define ALWAYS_INLINE inline __attribute__ ((always_inline))
41 #else
42 # define ALWAYS_INLINE inline
43 #endif
44
45 /* Attribute macro to prevent inlining of function. */
46 #if __GNUC__ >= 4
47 # define NO_INLINE __attribute__ ((noinline))
48 #else
49 # define NO_INLINE
50 #endif
51
52
53 /* USE_AESNI_AVX inidicates whether to compile with Intel AES-NI/AVX code. */
54 #undef USE_AESNI_AVX
55 #if defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX_SUPPORT)
56 # if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
57 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
58 # define USE_AESNI_AVX 1
59 # endif
60 #endif
61
62 /* USE_GFNI_AVX inidicates whether to compile with Intel GFNI/AVX code. */
63 #undef USE_GFNI_AVX
64 #if defined(USE_AESNI_AVX) && defined(ENABLE_GFNI_SUPPORT)
65 # define USE_GFNI_AVX 1
66 #endif
67
68 /* USE_AESNI_AVX2 inidicates whether to compile with Intel AES-NI/AVX2 code. */
69 #undef USE_AESNI_AVX2
70 #if defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX2_SUPPORT)
71 # if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
72 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
73 # define USE_AESNI_AVX2 1
74 # endif
75 #endif
76
77 /* USE_VAES_AVX2 inidicates whether to compile with Intel VAES/AVX2 code. */
78 #undef USE_VAES_AVX2
79 #if defined(USE_AESNI_AVX2) && defined(HAVE_GCC_INLINE_ASM_VAES_VPCLMUL)
80 # define USE_VAES_AVX2 1
81 #endif
82
83 /* USE_GFNI_AVX2 inidicates whether to compile with Intel GFNI/AVX2 code. */
84 #undef USE_GFNI_AVX2
85 #if defined(USE_AESNI_AVX2) && defined(ENABLE_GFNI_SUPPORT)
86 # define USE_GFNI_AVX2 1
87 #endif
88
89 /* USE_GFNI_AVX512 inidicates whether to compile with Intel GFNI/AVX512 code. */
90 #undef USE_GFNI_AVX512
91 #if defined(ENABLE_GFNI_SUPPORT) && defined(ENABLE_AVX512_SUPPORT)
92 # if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
93 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
94 # define USE_GFNI_AVX512 1
95 # endif
96 #endif
97
98 /* How many parallel blocks to handle in bulk processing functions. */
99 #if defined(USE_GFNI_AVX512)
100 # define MAX_PARALLEL_BLKS 64
101 #elif defined(USE_AESNI_AVX2)
102 # define MAX_PARALLEL_BLKS 32
103 #elif defined(USE_AESNI_AVX)
104 # define MAX_PARALLEL_BLKS 16
105 #else
106 # define MAX_PARALLEL_BLKS 8
107 #endif
108
109 /* Assembly implementations use SystemV ABI, ABI conversion and additional
110 * stack to store XMM6-XMM15 needed on Win64. */
111 #undef ASM_FUNC_ABI
112 #undef ASM_EXTRA_STACK
113 #if defined(USE_AESNI_AVX) || defined(USE_AESNI_AVX2) || \
114 defined(USE_GFNI_AVX512)
115 # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
116 # define ASM_FUNC_ABI __attribute__((sysv_abi))
117 # define ASM_EXTRA_STACK (10 * 16)
118 # else
119 # define ASM_FUNC_ABI
120 # define ASM_EXTRA_STACK 0
121 # endif
122 #endif
123
124
125 static const char *aria_selftest (void);
126
127
128 #define ARIA_MIN_KEY_SIZE 16
129 #define ARIA_MAX_KEY_SIZE 32
130 #define ARIA_BLOCK_SIZE 16
131 #define ARIA_MAX_RD_KEYS 17
132 #define ARIA_RD_KEY_WORDS (ARIA_BLOCK_SIZE / sizeof(u32))
133
134
135 typedef struct
136 {
137 u32 enc_key[ARIA_MAX_RD_KEYS][ARIA_RD_KEY_WORDS];
138 u32 dec_key[ARIA_MAX_RD_KEYS][ARIA_RD_KEY_WORDS];
139 int rounds;
140
141 unsigned int decryption_prepared:1; /* The decryption key is set up. */
142 unsigned int bulk_prefetch_ready:1; /* Look-up table prefetch ready for
143 * current bulk operation. */
144
145 #ifdef USE_AESNI_AVX
146 unsigned int use_aesni_avx:1;
147 unsigned int use_gfni_avx:1;
148 #endif
149 #ifdef USE_AESNI_AVX2
150 unsigned int use_aesni_avx2:1;
151 unsigned int use_vaes_avx2:1;
152 unsigned int use_gfni_avx2:1;
153 #endif
154 #ifdef USE_GFNI_AVX512
155 unsigned int use_gfni_avx512:1;
156 #endif
157 } ARIA_context;
158
159
160 static const u32 key_rc[20] =
161 {
162 0x517cc1b7, 0x27220a94, 0xfe13abe8, 0xfa9a6ee0,
163 0x6db14acc, 0x9e21c820, 0xff28b1d5, 0xef5de2b0,
164 0xdb92371d, 0x2126e970, 0x03249775, 0x04e8c90e,
165 0x517cc1b7, 0x27220a94, 0xfe13abe8, 0xfa9a6ee0,
166 0x6db14acc, 0x9e21c820, 0xff28b1d5, 0xef5de2b0
167 };
168
169
170 static struct
171 {
172 volatile u32 counter_head;
173 u32 cacheline_align[64 / 4 - 1];
174 u32 s1[256];
175 u32 s2[256];
176 u32 x1[256];
177 u32 x2[256];
178 volatile u32 counter_tail;
179 } sboxes ATTR_ALIGNED_64 =
180 {
181 0,
182 { 0, },
183
184 { /* s1 */
185 0x00636363, 0x007c7c7c, 0x00777777, 0x007b7b7b,
186 0x00f2f2f2, 0x006b6b6b, 0x006f6f6f, 0x00c5c5c5,
187 0x00303030, 0x00010101, 0x00676767, 0x002b2b2b,
188 0x00fefefe, 0x00d7d7d7, 0x00ababab, 0x00767676,
189 0x00cacaca, 0x00828282, 0x00c9c9c9, 0x007d7d7d,
190 0x00fafafa, 0x00595959, 0x00474747, 0x00f0f0f0,
191 0x00adadad, 0x00d4d4d4, 0x00a2a2a2, 0x00afafaf,
192 0x009c9c9c, 0x00a4a4a4, 0x00727272, 0x00c0c0c0,
193 0x00b7b7b7, 0x00fdfdfd, 0x00939393, 0x00262626,
194 0x00363636, 0x003f3f3f, 0x00f7f7f7, 0x00cccccc,
195 0x00343434, 0x00a5a5a5, 0x00e5e5e5, 0x00f1f1f1,
196 0x00717171, 0x00d8d8d8, 0x00313131, 0x00151515,
197 0x00040404, 0x00c7c7c7, 0x00232323, 0x00c3c3c3,
198 0x00181818, 0x00969696, 0x00050505, 0x009a9a9a,
199 0x00070707, 0x00121212, 0x00808080, 0x00e2e2e2,
200 0x00ebebeb, 0x00272727, 0x00b2b2b2, 0x00757575,
201 0x00090909, 0x00838383, 0x002c2c2c, 0x001a1a1a,
202 0x001b1b1b, 0x006e6e6e, 0x005a5a5a, 0x00a0a0a0,
203 0x00525252, 0x003b3b3b, 0x00d6d6d6, 0x00b3b3b3,
204 0x00292929, 0x00e3e3e3, 0x002f2f2f, 0x00848484,
205 0x00535353, 0x00d1d1d1, 0x00000000, 0x00ededed,
206 0x00202020, 0x00fcfcfc, 0x00b1b1b1, 0x005b5b5b,
207 0x006a6a6a, 0x00cbcbcb, 0x00bebebe, 0x00393939,
208 0x004a4a4a, 0x004c4c4c, 0x00585858, 0x00cfcfcf,
209 0x00d0d0d0, 0x00efefef, 0x00aaaaaa, 0x00fbfbfb,
210 0x00434343, 0x004d4d4d, 0x00333333, 0x00858585,
211 0x00454545, 0x00f9f9f9, 0x00020202, 0x007f7f7f,
212 0x00505050, 0x003c3c3c, 0x009f9f9f, 0x00a8a8a8,
213 0x00515151, 0x00a3a3a3, 0x00404040, 0x008f8f8f,
214 0x00929292, 0x009d9d9d, 0x00383838, 0x00f5f5f5,
215 0x00bcbcbc, 0x00b6b6b6, 0x00dadada, 0x00212121,
216 0x00101010, 0x00ffffff, 0x00f3f3f3, 0x00d2d2d2,
217 0x00cdcdcd, 0x000c0c0c, 0x00131313, 0x00ececec,
218 0x005f5f5f, 0x00979797, 0x00444444, 0x00171717,
219 0x00c4c4c4, 0x00a7a7a7, 0x007e7e7e, 0x003d3d3d,
220 0x00646464, 0x005d5d5d, 0x00191919, 0x00737373,
221 0x00606060, 0x00818181, 0x004f4f4f, 0x00dcdcdc,
222 0x00222222, 0x002a2a2a, 0x00909090, 0x00888888,
223 0x00464646, 0x00eeeeee, 0x00b8b8b8, 0x00141414,
224 0x00dedede, 0x005e5e5e, 0x000b0b0b, 0x00dbdbdb,
225 0x00e0e0e0, 0x00323232, 0x003a3a3a, 0x000a0a0a,
226 0x00494949, 0x00060606, 0x00242424, 0x005c5c5c,
227 0x00c2c2c2, 0x00d3d3d3, 0x00acacac, 0x00626262,
228 0x00919191, 0x00959595, 0x00e4e4e4, 0x00797979,
229 0x00e7e7e7, 0x00c8c8c8, 0x00373737, 0x006d6d6d,
230 0x008d8d8d, 0x00d5d5d5, 0x004e4e4e, 0x00a9a9a9,
231 0x006c6c6c, 0x00565656, 0x00f4f4f4, 0x00eaeaea,
232 0x00656565, 0x007a7a7a, 0x00aeaeae, 0x00080808,
233 0x00bababa, 0x00787878, 0x00252525, 0x002e2e2e,
234 0x001c1c1c, 0x00a6a6a6, 0x00b4b4b4, 0x00c6c6c6,
235 0x00e8e8e8, 0x00dddddd, 0x00747474, 0x001f1f1f,
236 0x004b4b4b, 0x00bdbdbd, 0x008b8b8b, 0x008a8a8a,
237 0x00707070, 0x003e3e3e, 0x00b5b5b5, 0x00666666,
238 0x00484848, 0x00030303, 0x00f6f6f6, 0x000e0e0e,
239 0x00616161, 0x00353535, 0x00575757, 0x00b9b9b9,
240 0x00868686, 0x00c1c1c1, 0x001d1d1d, 0x009e9e9e,
241 0x00e1e1e1, 0x00f8f8f8, 0x00989898, 0x00111111,
242 0x00696969, 0x00d9d9d9, 0x008e8e8e, 0x00949494,
243 0x009b9b9b, 0x001e1e1e, 0x00878787, 0x00e9e9e9,
244 0x00cecece, 0x00555555, 0x00282828, 0x00dfdfdf,
245 0x008c8c8c, 0x00a1a1a1, 0x00898989, 0x000d0d0d,
246 0x00bfbfbf, 0x00e6e6e6, 0x00424242, 0x00686868,
247 0x00414141, 0x00999999, 0x002d2d2d, 0x000f0f0f,
248 0x00b0b0b0, 0x00545454, 0x00bbbbbb, 0x00161616
249 },
250 { /* s2 */
251 0xe200e2e2, 0x4e004e4e, 0x54005454, 0xfc00fcfc,
252 0x94009494, 0xc200c2c2, 0x4a004a4a, 0xcc00cccc,
253 0x62006262, 0x0d000d0d, 0x6a006a6a, 0x46004646,
254 0x3c003c3c, 0x4d004d4d, 0x8b008b8b, 0xd100d1d1,
255 0x5e005e5e, 0xfa00fafa, 0x64006464, 0xcb00cbcb,
256 0xb400b4b4, 0x97009797, 0xbe00bebe, 0x2b002b2b,
257 0xbc00bcbc, 0x77007777, 0x2e002e2e, 0x03000303,
258 0xd300d3d3, 0x19001919, 0x59005959, 0xc100c1c1,
259 0x1d001d1d, 0x06000606, 0x41004141, 0x6b006b6b,
260 0x55005555, 0xf000f0f0, 0x99009999, 0x69006969,
261 0xea00eaea, 0x9c009c9c, 0x18001818, 0xae00aeae,
262 0x63006363, 0xdf00dfdf, 0xe700e7e7, 0xbb00bbbb,
263 0x00000000, 0x73007373, 0x66006666, 0xfb00fbfb,
264 0x96009696, 0x4c004c4c, 0x85008585, 0xe400e4e4,
265 0x3a003a3a, 0x09000909, 0x45004545, 0xaa00aaaa,
266 0x0f000f0f, 0xee00eeee, 0x10001010, 0xeb00ebeb,
267 0x2d002d2d, 0x7f007f7f, 0xf400f4f4, 0x29002929,
268 0xac00acac, 0xcf00cfcf, 0xad00adad, 0x91009191,
269 0x8d008d8d, 0x78007878, 0xc800c8c8, 0x95009595,
270 0xf900f9f9, 0x2f002f2f, 0xce00cece, 0xcd00cdcd,
271 0x08000808, 0x7a007a7a, 0x88008888, 0x38003838,
272 0x5c005c5c, 0x83008383, 0x2a002a2a, 0x28002828,
273 0x47004747, 0xdb00dbdb, 0xb800b8b8, 0xc700c7c7,
274 0x93009393, 0xa400a4a4, 0x12001212, 0x53005353,
275 0xff00ffff, 0x87008787, 0x0e000e0e, 0x31003131,
276 0x36003636, 0x21002121, 0x58005858, 0x48004848,
277 0x01000101, 0x8e008e8e, 0x37003737, 0x74007474,
278 0x32003232, 0xca00caca, 0xe900e9e9, 0xb100b1b1,
279 0xb700b7b7, 0xab00abab, 0x0c000c0c, 0xd700d7d7,
280 0xc400c4c4, 0x56005656, 0x42004242, 0x26002626,
281 0x07000707, 0x98009898, 0x60006060, 0xd900d9d9,
282 0xb600b6b6, 0xb900b9b9, 0x11001111, 0x40004040,
283 0xec00ecec, 0x20002020, 0x8c008c8c, 0xbd00bdbd,
284 0xa000a0a0, 0xc900c9c9, 0x84008484, 0x04000404,
285 0x49004949, 0x23002323, 0xf100f1f1, 0x4f004f4f,
286 0x50005050, 0x1f001f1f, 0x13001313, 0xdc00dcdc,
287 0xd800d8d8, 0xc000c0c0, 0x9e009e9e, 0x57005757,
288 0xe300e3e3, 0xc300c3c3, 0x7b007b7b, 0x65006565,
289 0x3b003b3b, 0x02000202, 0x8f008f8f, 0x3e003e3e,
290 0xe800e8e8, 0x25002525, 0x92009292, 0xe500e5e5,
291 0x15001515, 0xdd00dddd, 0xfd00fdfd, 0x17001717,
292 0xa900a9a9, 0xbf00bfbf, 0xd400d4d4, 0x9a009a9a,
293 0x7e007e7e, 0xc500c5c5, 0x39003939, 0x67006767,
294 0xfe00fefe, 0x76007676, 0x9d009d9d, 0x43004343,
295 0xa700a7a7, 0xe100e1e1, 0xd000d0d0, 0xf500f5f5,
296 0x68006868, 0xf200f2f2, 0x1b001b1b, 0x34003434,
297 0x70007070, 0x05000505, 0xa300a3a3, 0x8a008a8a,
298 0xd500d5d5, 0x79007979, 0x86008686, 0xa800a8a8,
299 0x30003030, 0xc600c6c6, 0x51005151, 0x4b004b4b,
300 0x1e001e1e, 0xa600a6a6, 0x27002727, 0xf600f6f6,
301 0x35003535, 0xd200d2d2, 0x6e006e6e, 0x24002424,
302 0x16001616, 0x82008282, 0x5f005f5f, 0xda00dada,
303 0xe600e6e6, 0x75007575, 0xa200a2a2, 0xef00efef,
304 0x2c002c2c, 0xb200b2b2, 0x1c001c1c, 0x9f009f9f,
305 0x5d005d5d, 0x6f006f6f, 0x80008080, 0x0a000a0a,
306 0x72007272, 0x44004444, 0x9b009b9b, 0x6c006c6c,
307 0x90009090, 0x0b000b0b, 0x5b005b5b, 0x33003333,
308 0x7d007d7d, 0x5a005a5a, 0x52005252, 0xf300f3f3,
309 0x61006161, 0xa100a1a1, 0xf700f7f7, 0xb000b0b0,
310 0xd600d6d6, 0x3f003f3f, 0x7c007c7c, 0x6d006d6d,
311 0xed00eded, 0x14001414, 0xe000e0e0, 0xa500a5a5,
312 0x3d003d3d, 0x22002222, 0xb300b3b3, 0xf800f8f8,
313 0x89008989, 0xde00dede, 0x71007171, 0x1a001a1a,
314 0xaf00afaf, 0xba00baba, 0xb500b5b5, 0x81008181
315 },
316 { /* x1 */
317 0x52520052, 0x09090009, 0x6a6a006a, 0xd5d500d5,
318 0x30300030, 0x36360036, 0xa5a500a5, 0x38380038,
319 0xbfbf00bf, 0x40400040, 0xa3a300a3, 0x9e9e009e,
320 0x81810081, 0xf3f300f3, 0xd7d700d7, 0xfbfb00fb,
321 0x7c7c007c, 0xe3e300e3, 0x39390039, 0x82820082,
322 0x9b9b009b, 0x2f2f002f, 0xffff00ff, 0x87870087,
323 0x34340034, 0x8e8e008e, 0x43430043, 0x44440044,
324 0xc4c400c4, 0xdede00de, 0xe9e900e9, 0xcbcb00cb,
325 0x54540054, 0x7b7b007b, 0x94940094, 0x32320032,
326 0xa6a600a6, 0xc2c200c2, 0x23230023, 0x3d3d003d,
327 0xeeee00ee, 0x4c4c004c, 0x95950095, 0x0b0b000b,
328 0x42420042, 0xfafa00fa, 0xc3c300c3, 0x4e4e004e,
329 0x08080008, 0x2e2e002e, 0xa1a100a1, 0x66660066,
330 0x28280028, 0xd9d900d9, 0x24240024, 0xb2b200b2,
331 0x76760076, 0x5b5b005b, 0xa2a200a2, 0x49490049,
332 0x6d6d006d, 0x8b8b008b, 0xd1d100d1, 0x25250025,
333 0x72720072, 0xf8f800f8, 0xf6f600f6, 0x64640064,
334 0x86860086, 0x68680068, 0x98980098, 0x16160016,
335 0xd4d400d4, 0xa4a400a4, 0x5c5c005c, 0xcccc00cc,
336 0x5d5d005d, 0x65650065, 0xb6b600b6, 0x92920092,
337 0x6c6c006c, 0x70700070, 0x48480048, 0x50500050,
338 0xfdfd00fd, 0xeded00ed, 0xb9b900b9, 0xdada00da,
339 0x5e5e005e, 0x15150015, 0x46460046, 0x57570057,
340 0xa7a700a7, 0x8d8d008d, 0x9d9d009d, 0x84840084,
341 0x90900090, 0xd8d800d8, 0xabab00ab, 0x00000000,
342 0x8c8c008c, 0xbcbc00bc, 0xd3d300d3, 0x0a0a000a,
343 0xf7f700f7, 0xe4e400e4, 0x58580058, 0x05050005,
344 0xb8b800b8, 0xb3b300b3, 0x45450045, 0x06060006,
345 0xd0d000d0, 0x2c2c002c, 0x1e1e001e, 0x8f8f008f,
346 0xcaca00ca, 0x3f3f003f, 0x0f0f000f, 0x02020002,
347 0xc1c100c1, 0xafaf00af, 0xbdbd00bd, 0x03030003,
348 0x01010001, 0x13130013, 0x8a8a008a, 0x6b6b006b,
349 0x3a3a003a, 0x91910091, 0x11110011, 0x41410041,
350 0x4f4f004f, 0x67670067, 0xdcdc00dc, 0xeaea00ea,
351 0x97970097, 0xf2f200f2, 0xcfcf00cf, 0xcece00ce,
352 0xf0f000f0, 0xb4b400b4, 0xe6e600e6, 0x73730073,
353 0x96960096, 0xacac00ac, 0x74740074, 0x22220022,
354 0xe7e700e7, 0xadad00ad, 0x35350035, 0x85850085,
355 0xe2e200e2, 0xf9f900f9, 0x37370037, 0xe8e800e8,
356 0x1c1c001c, 0x75750075, 0xdfdf00df, 0x6e6e006e,
357 0x47470047, 0xf1f100f1, 0x1a1a001a, 0x71710071,
358 0x1d1d001d, 0x29290029, 0xc5c500c5, 0x89890089,
359 0x6f6f006f, 0xb7b700b7, 0x62620062, 0x0e0e000e,
360 0xaaaa00aa, 0x18180018, 0xbebe00be, 0x1b1b001b,
361 0xfcfc00fc, 0x56560056, 0x3e3e003e, 0x4b4b004b,
362 0xc6c600c6, 0xd2d200d2, 0x79790079, 0x20200020,
363 0x9a9a009a, 0xdbdb00db, 0xc0c000c0, 0xfefe00fe,
364 0x78780078, 0xcdcd00cd, 0x5a5a005a, 0xf4f400f4,
365 0x1f1f001f, 0xdddd00dd, 0xa8a800a8, 0x33330033,
366 0x88880088, 0x07070007, 0xc7c700c7, 0x31310031,
367 0xb1b100b1, 0x12120012, 0x10100010, 0x59590059,
368 0x27270027, 0x80800080, 0xecec00ec, 0x5f5f005f,
369 0x60600060, 0x51510051, 0x7f7f007f, 0xa9a900a9,
370 0x19190019, 0xb5b500b5, 0x4a4a004a, 0x0d0d000d,
371 0x2d2d002d, 0xe5e500e5, 0x7a7a007a, 0x9f9f009f,
372 0x93930093, 0xc9c900c9, 0x9c9c009c, 0xefef00ef,
373 0xa0a000a0, 0xe0e000e0, 0x3b3b003b, 0x4d4d004d,
374 0xaeae00ae, 0x2a2a002a, 0xf5f500f5, 0xb0b000b0,
375 0xc8c800c8, 0xebeb00eb, 0xbbbb00bb, 0x3c3c003c,
376 0x83830083, 0x53530053, 0x99990099, 0x61610061,
377 0x17170017, 0x2b2b002b, 0x04040004, 0x7e7e007e,
378 0xbaba00ba, 0x77770077, 0xd6d600d6, 0x26260026,
379 0xe1e100e1, 0x69690069, 0x14140014, 0x63630063,
380 0x55550055, 0x21210021, 0x0c0c000c, 0x7d7d007d
381 },
382 { /* x2 */
383 0x30303000, 0x68686800, 0x99999900, 0x1b1b1b00,
384 0x87878700, 0xb9b9b900, 0x21212100, 0x78787800,
385 0x50505000, 0x39393900, 0xdbdbdb00, 0xe1e1e100,
386 0x72727200, 0x09090900, 0x62626200, 0x3c3c3c00,
387 0x3e3e3e00, 0x7e7e7e00, 0x5e5e5e00, 0x8e8e8e00,
388 0xf1f1f100, 0xa0a0a000, 0xcccccc00, 0xa3a3a300,
389 0x2a2a2a00, 0x1d1d1d00, 0xfbfbfb00, 0xb6b6b600,
390 0xd6d6d600, 0x20202000, 0xc4c4c400, 0x8d8d8d00,
391 0x81818100, 0x65656500, 0xf5f5f500, 0x89898900,
392 0xcbcbcb00, 0x9d9d9d00, 0x77777700, 0xc6c6c600,
393 0x57575700, 0x43434300, 0x56565600, 0x17171700,
394 0xd4d4d400, 0x40404000, 0x1a1a1a00, 0x4d4d4d00,
395 0xc0c0c000, 0x63636300, 0x6c6c6c00, 0xe3e3e300,
396 0xb7b7b700, 0xc8c8c800, 0x64646400, 0x6a6a6a00,
397 0x53535300, 0xaaaaaa00, 0x38383800, 0x98989800,
398 0x0c0c0c00, 0xf4f4f400, 0x9b9b9b00, 0xededed00,
399 0x7f7f7f00, 0x22222200, 0x76767600, 0xafafaf00,
400 0xdddddd00, 0x3a3a3a00, 0x0b0b0b00, 0x58585800,
401 0x67676700, 0x88888800, 0x06060600, 0xc3c3c300,
402 0x35353500, 0x0d0d0d00, 0x01010100, 0x8b8b8b00,
403 0x8c8c8c00, 0xc2c2c200, 0xe6e6e600, 0x5f5f5f00,
404 0x02020200, 0x24242400, 0x75757500, 0x93939300,
405 0x66666600, 0x1e1e1e00, 0xe5e5e500, 0xe2e2e200,
406 0x54545400, 0xd8d8d800, 0x10101000, 0xcecece00,
407 0x7a7a7a00, 0xe8e8e800, 0x08080800, 0x2c2c2c00,
408 0x12121200, 0x97979700, 0x32323200, 0xababab00,
409 0xb4b4b400, 0x27272700, 0x0a0a0a00, 0x23232300,
410 0xdfdfdf00, 0xefefef00, 0xcacaca00, 0xd9d9d900,
411 0xb8b8b800, 0xfafafa00, 0xdcdcdc00, 0x31313100,
412 0x6b6b6b00, 0xd1d1d100, 0xadadad00, 0x19191900,
413 0x49494900, 0xbdbdbd00, 0x51515100, 0x96969600,
414 0xeeeeee00, 0xe4e4e400, 0xa8a8a800, 0x41414100,
415 0xdadada00, 0xffffff00, 0xcdcdcd00, 0x55555500,
416 0x86868600, 0x36363600, 0xbebebe00, 0x61616100,
417 0x52525200, 0xf8f8f800, 0xbbbbbb00, 0x0e0e0e00,
418 0x82828200, 0x48484800, 0x69696900, 0x9a9a9a00,
419 0xe0e0e000, 0x47474700, 0x9e9e9e00, 0x5c5c5c00,
420 0x04040400, 0x4b4b4b00, 0x34343400, 0x15151500,
421 0x79797900, 0x26262600, 0xa7a7a700, 0xdedede00,
422 0x29292900, 0xaeaeae00, 0x92929200, 0xd7d7d700,
423 0x84848400, 0xe9e9e900, 0xd2d2d200, 0xbababa00,
424 0x5d5d5d00, 0xf3f3f300, 0xc5c5c500, 0xb0b0b000,
425 0xbfbfbf00, 0xa4a4a400, 0x3b3b3b00, 0x71717100,
426 0x44444400, 0x46464600, 0x2b2b2b00, 0xfcfcfc00,
427 0xebebeb00, 0x6f6f6f00, 0xd5d5d500, 0xf6f6f600,
428 0x14141400, 0xfefefe00, 0x7c7c7c00, 0x70707000,
429 0x5a5a5a00, 0x7d7d7d00, 0xfdfdfd00, 0x2f2f2f00,
430 0x18181800, 0x83838300, 0x16161600, 0xa5a5a500,
431 0x91919100, 0x1f1f1f00, 0x05050500, 0x95959500,
432 0x74747400, 0xa9a9a900, 0xc1c1c100, 0x5b5b5b00,
433 0x4a4a4a00, 0x85858500, 0x6d6d6d00, 0x13131300,
434 0x07070700, 0x4f4f4f00, 0x4e4e4e00, 0x45454500,
435 0xb2b2b200, 0x0f0f0f00, 0xc9c9c900, 0x1c1c1c00,
436 0xa6a6a600, 0xbcbcbc00, 0xececec00, 0x73737300,
437 0x90909000, 0x7b7b7b00, 0xcfcfcf00, 0x59595900,
438 0x8f8f8f00, 0xa1a1a100, 0xf9f9f900, 0x2d2d2d00,
439 0xf2f2f200, 0xb1b1b100, 0x00000000, 0x94949400,
440 0x37373700, 0x9f9f9f00, 0xd0d0d000, 0x2e2e2e00,
441 0x9c9c9c00, 0x6e6e6e00, 0x28282800, 0x3f3f3f00,
442 0x80808000, 0xf0f0f000, 0x3d3d3d00, 0xd3d3d300,
443 0x25252500, 0x8a8a8a00, 0xb5b5b500, 0xe7e7e700,
444 0x42424200, 0xb3b3b300, 0xc7c7c700, 0xeaeaea00,
445 0xf7f7f700, 0x4c4c4c00, 0x11111100, 0x33333300,
446 0x03030300, 0xa2a2a200, 0xacacac00, 0x60606000
447 },
448 0
449 };
450
451 #ifdef USE_AESNI_AVX
452 extern unsigned int
453 _gcry_aria_aesni_avx_ecb_crypt_blk1_16(const void *ctx, byte *out,
454 const byte *in, const void *key,
455 u64 nblks) ASM_FUNC_ABI;
456 extern unsigned int
457 _gcry_aria_aesni_avx_ctr_crypt_blk16(const void *ctx, byte *out,
458 const byte *in, byte *iv) ASM_FUNC_ABI;
459
460 #ifdef USE_GFNI_AVX
461 extern unsigned int
462 _gcry_aria_gfni_avx_ecb_crypt_blk1_16(const void *ctx, byte *out,
463 const byte *in, const void *key,
464 u64 nblks) ASM_FUNC_ABI;
465 extern unsigned int
466 _gcry_aria_gfni_avx_ctr_crypt_blk16(const void *ctx, byte *out,
467 const byte *in, byte *iv) ASM_FUNC_ABI;
468 #endif /* USE_GFNI_AVX */
469
470 static inline unsigned int
471 aria_avx_ecb_crypt_blk1_16(const ARIA_context *ctx, byte *out, const byte *in,
472 const u32 key[][ARIA_RD_KEY_WORDS], size_t nblks)
473 {
474 if (0) { }
475 #ifdef USE_GFNI_AVX
476 else if (ctx->use_gfni_avx)
477 return _gcry_aria_gfni_avx_ecb_crypt_blk1_16(ctx, out, in, key, nblks)
478 + ASM_EXTRA_STACK;
479 #endif /* USE_GFNI_AVX */
480 else
481 return _gcry_aria_aesni_avx_ecb_crypt_blk1_16(ctx, out, in, key, nblks)
482 + ASM_EXTRA_STACK;
483 }
484
485 static inline unsigned int
486 aria_avx_ctr_crypt_blk16(const ARIA_context *ctx, byte *out, const byte *in,
487 byte *iv)
488 {
489 if (0) { }
490 #ifdef USE_GFNI_AVX
491 else if (ctx->use_gfni_avx)
492 return _gcry_aria_gfni_avx_ctr_crypt_blk16(ctx, out, in, iv)
493 + ASM_EXTRA_STACK;
494 #endif /* USE_GFNI_AVX */
495 else
496 return _gcry_aria_aesni_avx_ctr_crypt_blk16(ctx, out, in, iv)
497 + ASM_EXTRA_STACK;
498 }
499 #endif /* USE_AESNI_AVX */
500
501 #ifdef USE_AESNI_AVX2
502 extern unsigned int
503 _gcry_aria_aesni_avx2_ecb_crypt_blk32(const void *ctx, byte *out,
504 const byte *in,
505 const void *key) ASM_FUNC_ABI;
506 extern unsigned int
507 _gcry_aria_aesni_avx2_ctr_crypt_blk32(const void *ctx, byte *out,
508 const byte *in, byte *iv) ASM_FUNC_ABI;
509
510 #ifdef USE_VAES_AVX2
511 extern unsigned int
512 _gcry_aria_vaes_avx2_ecb_crypt_blk32(const void *ctx, byte *out,
513 const byte *in,
514 const void *key) ASM_FUNC_ABI;
515 extern unsigned int
516 _gcry_aria_vaes_avx2_ctr_crypt_blk32(const void *ctx, byte *out,
517 const byte *in, byte *iv) ASM_FUNC_ABI;
518 #endif /* USE_VAES_AVX2 */
519
520 #ifdef USE_GFNI_AVX2
521 extern unsigned int
522 _gcry_aria_gfni_avx2_ecb_crypt_blk32(const void *ctx, byte *out,
523 const byte *in,
524 const void *key) ASM_FUNC_ABI;
525 extern unsigned int
526 _gcry_aria_gfni_avx2_ctr_crypt_blk32(const void *ctx, byte *out,
527 const byte *in, byte *iv) ASM_FUNC_ABI;
528 #endif /* USE_GFNI_AVX2 */
529
530 static inline unsigned int
531 aria_avx2_ecb_crypt_blk32(const ARIA_context *ctx, byte *out, const byte *in,
532 const u32 key[][ARIA_RD_KEY_WORDS])
533 {
534 if (0) { }
535 #ifdef USE_GFNI_AVX2
536 else if (ctx->use_gfni_avx2)
537 return _gcry_aria_gfni_avx2_ecb_crypt_blk32(ctx, out, in, key)
538 + ASM_EXTRA_STACK;
539 #endif /* USE_GFNI_AVX2 */
540 #ifdef USE_VAES_AVX2
541 else if (ctx->use_vaes_avx2)
542 return _gcry_aria_vaes_avx2_ecb_crypt_blk32(ctx, out, in, key)
543 + ASM_EXTRA_STACK;
544 #endif /* USE_VAES_AVX2 */
545 else
546 return _gcry_aria_aesni_avx2_ecb_crypt_blk32(ctx, out, in, key)
547 + ASM_EXTRA_STACK;
548 }
549
550 static inline unsigned int
551 aria_avx2_ctr_crypt_blk32(const ARIA_context *ctx, byte *out, const byte *in,
552 byte *iv)
553 {
554 if (0) { }
555 #ifdef USE_GFNI_AVX2
556 else if (ctx->use_gfni_avx2)
557 return _gcry_aria_gfni_avx2_ctr_crypt_blk32(ctx, out, in, iv)
558 + ASM_EXTRA_STACK;
559 #endif /* USE_GFNI_AVX2 */
560 #ifdef USE_VAES_AVX2
561 else if (ctx->use_vaes_avx2)
562 return _gcry_aria_vaes_avx2_ctr_crypt_blk32(ctx, out, in, iv)
563 + ASM_EXTRA_STACK;
564 #endif /* USE_VAES_AVX2 */
565 else
566 return _gcry_aria_aesni_avx2_ctr_crypt_blk32(ctx, out, in, iv)
567 + ASM_EXTRA_STACK;
568 }
569 #endif /* USE_AESNI_AVX2 */
570
571 #ifdef USE_GFNI_AVX512
572 extern unsigned int
573 _gcry_aria_gfni_avx512_ecb_crypt_blk64(const void *ctx, byte *out,
574 const byte *in,
575 const void *key) ASM_FUNC_ABI;
576 extern unsigned int
577 _gcry_aria_gfni_avx512_ctr_crypt_blk64(const void *ctx, byte *out,
578 const byte *in, byte *iv) ASM_FUNC_ABI;
579
580 static inline unsigned int
581 aria_gfni_avx512_ecb_crypt_blk64(const ARIA_context *ctx, byte *out,
582 const byte *in,
583 const u32 key[][ARIA_RD_KEY_WORDS])
584 {
585 return _gcry_aria_gfni_avx512_ecb_crypt_blk64(ctx, out, in, key)
586 + ASM_EXTRA_STACK;
587 }
588
589 static inline unsigned int
590 aria_gfni_avx512_ctr_crypt_blk64(const ARIA_context *ctx, byte *out,
591 const byte *in, byte *iv)
592 {
593 return _gcry_aria_gfni_avx512_ctr_crypt_blk64(ctx, out, in, iv)
594 + ASM_EXTRA_STACK;
595 }
596 #endif /* USE_AESNI_AVX2 */
597
598 /* Prefetching for sbox tables. */
599 static inline void
600 prefetch_table(const volatile byte *tab, size_t len)
601 {
602 size_t i;
603
604 for (i = 0; len - i >= 8 * 32; i += 8 * 32)
605 {
606 (void)tab[i + 0 * 32];
607 (void)tab[i + 1 * 32];
608 (void)tab[i + 2 * 32];
609 (void)tab[i + 3 * 32];
610 (void)tab[i + 4 * 32];
611 (void)tab[i + 5 * 32];
612 (void)tab[i + 6 * 32];
613 (void)tab[i + 7 * 32];
614 }
615 for (; i < len; i += 32)
616 {
617 (void)tab[i];
618 }
619
620 (void)tab[len - 1];
621 }
622
623 static inline void
624 prefetch_sboxes(void)
625 {
626 /* Modify counters to trigger copy-on-write and unsharing if physical pages
627 * of look-up table are shared between processes. Modifying counters also
628 * causes checksums for pages to change and hint same-page merging algorithm
629 * that these pages are frequently changing. */
630 sboxes.counter_head++;
631 sboxes.counter_tail++;
632
633 /* Prefetch look-up tables to cache. */
634 prefetch_table((const void *)&sboxes, sizeof(sboxes));
635 }
636
637
638 static ALWAYS_INLINE
639 u32 rotr32(u32 v, u32 r)
640 {
641 return ror(v, r);
642 }
643
644 static ALWAYS_INLINE u32
645 get_u8(u32 x, u32 y)
646 {
647 return (x >> ((3 - y) * 8)) & 0xFF;
648 }
649
650 static ALWAYS_INLINE u32
651 make_u32(byte v0, byte v1, byte v2, byte v3)
652 {
653 return ((u32)v0 << 24) | ((u32)v1 << 16) | ((u32)v2 << 8) | ((u32)v3);
654 }
655
656 static ALWAYS_INLINE u32
657 aria_m(u32 t0)
658 {
659 return rotr32(t0, 8) ^ rotr32(t0 ^ rotr32(t0, 8), 16);
660 }
661
662 /* S-Box Layer 1 + M */
663 static ALWAYS_INLINE void
664 aria_sbox_layer1_with_pre_diff(u32 *t0, u32 *t1, u32 *t2, u32 *t3)
665 {
666 *t0 = sboxes.s1[get_u8(*t0, 0)] ^
667 sboxes.s2[get_u8(*t0, 1)] ^
668 sboxes.x1[get_u8(*t0, 2)] ^
669 sboxes.x2[get_u8(*t0, 3)];
670 *t1 = sboxes.s1[get_u8(*t1, 0)] ^
671 sboxes.s2[get_u8(*t1, 1)] ^
672 sboxes.x1[get_u8(*t1, 2)] ^
673 sboxes.x2[get_u8(*t1, 3)];
674 *t2 = sboxes.s1[get_u8(*t2, 0)] ^
675 sboxes.s2[get_u8(*t2, 1)] ^
676 sboxes.x1[get_u8(*t2, 2)] ^
677 sboxes.x2[get_u8(*t2, 3)];
678 *t3 = sboxes.s1[get_u8(*t3, 0)] ^
679 sboxes.s2[get_u8(*t3, 1)] ^
680 sboxes.x1[get_u8(*t3, 2)] ^
681 sboxes.x2[get_u8(*t3, 3)];
682 }
683
684 /* S-Box Layer 2 + M */
685 static ALWAYS_INLINE void
686 aria_sbox_layer2_with_pre_diff(u32 *t0, u32 *t1, u32 *t2, u32 *t3)
687 {
688 *t0 = sboxes.x1[get_u8(*t0, 0)] ^
689 sboxes.x2[get_u8(*t0, 1)] ^
690 sboxes.s1[get_u8(*t0, 2)] ^
691 sboxes.s2[get_u8(*t0, 3)];
692 *t1 = sboxes.x1[get_u8(*t1, 0)] ^
693 sboxes.x2[get_u8(*t1, 1)] ^
694 sboxes.s1[get_u8(*t1, 2)] ^
695 sboxes.s2[get_u8(*t1, 3)];
696 *t2 = sboxes.x1[get_u8(*t2, 0)] ^
697 sboxes.x2[get_u8(*t2, 1)] ^
698 sboxes.s1[get_u8(*t2, 2)] ^
699 sboxes.s2[get_u8(*t2, 3)];
700 *t3 = sboxes.x1[get_u8(*t3, 0)] ^
701 sboxes.x2[get_u8(*t3, 1)] ^
702 sboxes.s1[get_u8(*t3, 2)] ^
703 sboxes.s2[get_u8(*t3, 3)];
704 }
705
706 /* Word-level diffusion */
707 static ALWAYS_INLINE void
708 aria_diff_word(u32 *t0, u32 *t1, u32 *t2, u32 *t3)
709 {
710 *t1 ^= *t2;
711 *t2 ^= *t3;
712 *t0 ^= *t1;
713
714 *t3 ^= *t1;
715 *t2 ^= *t0;
716 *t1 ^= *t2;
717 }
718
719 /* Byte-level diffusion */
720 static inline void aria_diff_byte(u32 *t1, u32 *t2, u32 *t3)
721 {
722 *t1 = ((*t1 << 8) & 0xff00ff00) ^ ((*t1 >> 8) & 0x00ff00ff);
723 *t2 = rotr32(*t2, 16);
724 *t3 = _gcry_bswap32(*t3);
725 }
726
727 /* Key XOR Layer */
728 static ALWAYS_INLINE void
729 aria_add_round_key(u32 *rk, u32 *t0, u32 *t1, u32 *t2, u32 *t3)
730 {
731 *t0 ^= rk[0];
732 *t1 ^= rk[1];
733 *t2 ^= rk[2];
734 *t3 ^= rk[3];
735 }
736
737 /* Odd round Substitution & Diffusion */
738 static ALWAYS_INLINE void
739 aria_subst_diff_odd(u32 *t0, u32 *t1, u32 *t2, u32 *t3)
740 {
741 aria_sbox_layer1_with_pre_diff(t0, t1, t2, t3);
742 aria_diff_word(t0, t1, t2, t3);
743 aria_diff_byte(t1, t2, t3);
744 aria_diff_word(t0, t1, t2, t3);
745 }
746
747 /* Even round Substitution & Diffusion */
748 static ALWAYS_INLINE void
749 aria_subst_diff_even(u32 *t0, u32 *t1, u32 *t2, u32 *t3)
750 {
751 aria_sbox_layer2_with_pre_diff(t0, t1, t2, t3);
752 aria_diff_word(t0, t1, t2, t3);
753 aria_diff_byte(t3, t0, t1);
754 aria_diff_word(t0, t1, t2, t3);
755 }
756
757 /* Last round */
758 static ALWAYS_INLINE void
759 aria_last_round(u32 *t0, u32 *t1, u32 *t2, u32 *t3)
760 {
761 *t0 = make_u32((byte)(sboxes.x1[get_u8(*t0, 0)]),
762 (byte)(sboxes.x2[get_u8(*t0, 1)] >> 24),
763 (byte)(sboxes.s1[get_u8(*t0, 2)]),
764 (byte)(sboxes.s2[get_u8(*t0, 3)]));
765 *t1 = make_u32((byte)(sboxes.x1[get_u8(*t1, 0)]),
766 (byte)(sboxes.x2[get_u8(*t1, 1)] >> 24),
767 (byte)(sboxes.s1[get_u8(*t1, 2)]),
768 (byte)(sboxes.s2[get_u8(*t1, 3)]));
769 *t2 = make_u32((byte)(sboxes.x1[get_u8(*t2, 0)]),
770 (byte)(sboxes.x2[get_u8(*t2, 1)] >> 24),
771 (byte)(sboxes.s1[get_u8(*t2, 2)]),
772 (byte)(sboxes.s2[get_u8(*t2, 3)]));
773 *t3 = make_u32((byte)(sboxes.x1[get_u8(*t3, 0)]),
774 (byte)(sboxes.x2[get_u8(*t3, 1)] >> 24),
775 (byte)(sboxes.s1[get_u8(*t3, 2)]),
776 (byte)(sboxes.s2[get_u8(*t3, 3)]));
777 }
778
779 /* Q, R Macro expanded ARIA GSRK */
780 static ALWAYS_INLINE void
781 aria_gsrk(u32 *rk, u32 *x, u32 *y, u32 n)
782 {
783 int q = 4 - (n / 32);
784 int r = n % 32;
785
786 rk[0] = (x[0]) ^
787 ((y[q % 4]) >> r) ^
788 ((y[(q + 3) % 4]) << (32 - r));
789 rk[1] = (x[1]) ^
790 ((y[(q + 1) % 4]) >> r) ^
791 ((y[q % 4]) << (32 - r));
792 rk[2] = (x[2]) ^
793 ((y[(q + 2) % 4]) >> r) ^
794 ((y[(q + 1) % 4]) << (32 - r));
795 rk[3] = (x[3]) ^
796 ((y[(q + 3) % 4]) >> r) ^
797 ((y[(q + 2) % 4]) << (32 - r));
798 }
799
800
801 static NO_INLINE void
802 aria_set_encrypt_key(ARIA_context *ctx, const byte *in_key, u32 key_len)
803 {
804 u32 w0[4], w1[4], w2[4], w3[4];
805 u32 reg0, reg1, reg2, reg3;
806 const u32 *ck;
807 int rkidx = 0;
808
809 ctx->rounds = (key_len + 32) / 4;
810 prefetch_sboxes();
811
812 ck = &key_rc[(key_len - 16) / 2];
813
814 w0[0] = buf_get_be32(in_key + 0);
815 w0[1] = buf_get_be32(in_key + 4);
816 w0[2] = buf_get_be32(in_key + 8);
817 w0[3] = buf_get_be32(in_key + 12);
818
819 reg0 = w0[0] ^ ck[0];
820 reg1 = w0[1] ^ ck[1];
821 reg2 = w0[2] ^ ck[2];
822 reg3 = w0[3] ^ ck[3];
823
824 aria_subst_diff_odd(&reg0, &reg1, &reg2, &reg3);
825
826 if (key_len > 16)
827 {
828 w1[0] = buf_get_be32(in_key + 16);
829 w1[1] = buf_get_be32(in_key + 20);
830 if (key_len > 24)
831 {
832 w1[2] = buf_get_be32(in_key + 24);
833 w1[3] = buf_get_be32(in_key + 28);
834 }
835 else
836 {
837 w1[2] = 0;
838 w1[3] = 0;
839 }
840 }
841 else
842 {
843 w1[0] = 0;
844 w1[1] = 0;
845 w1[2] = 0;
846 w1[3] = 0;
847 }
848
849 w1[0] ^= reg0;
850 w1[1] ^= reg1;
851 w1[2] ^= reg2;
852 w1[3] ^= reg3;
853
854 reg0 = w1[0];
855 reg1 = w1[1];
856 reg2 = w1[2];
857 reg3 = w1[3];
858
859 reg0 ^= ck[4];
860 reg1 ^= ck[5];
861 reg2 ^= ck[6];
862 reg3 ^= ck[7];
863
864 aria_subst_diff_even(&reg0, &reg1, &reg2, &reg3);
865
866 reg0 ^= w0[0];
867 reg1 ^= w0[1];
868 reg2 ^= w0[2];
869 reg3 ^= w0[3];
870
871 w2[0] = reg0;
872 w2[1] = reg1;
873 w2[2] = reg2;
874 w2[3] = reg3;
875
876 reg0 ^= ck[8];
877 reg1 ^= ck[9];
878 reg2 ^= ck[10];
879 reg3 ^= ck[11];
880
881 aria_subst_diff_odd(&reg0, &reg1, &reg2, &reg3);
882
883 w3[0] = reg0 ^ w1[0];
884 w3[1] = reg1 ^ w1[1];
885 w3[2] = reg2 ^ w1[2];
886 w3[3] = reg3 ^ w1[3];
887
888 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 19);
889 rkidx++;
890 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 19);
891 rkidx++;
892 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 19);
893 rkidx++;
894 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 19);
895
896 rkidx++;
897 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 31);
898 rkidx++;
899 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 31);
900 rkidx++;
901 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 31);
902 rkidx++;
903 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 31);
904
905 rkidx++;
906 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 67);
907 rkidx++;
908 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 67);
909 rkidx++;
910 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 67);
911 rkidx++;
912 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 67);
913
914 rkidx++;
915 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 97);
916 if (key_len > 16)
917 {
918 rkidx++;
919 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 97);
920 rkidx++;
921 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 97);
922
923 if (key_len > 24)
924 {
925 rkidx++;
926 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 97);
927
928 rkidx++;
929 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 109);
930 }
931 }
932
933 wipememory(w0, sizeof(w0));
934 wipememory(w1, sizeof(w1));
935 wipememory(w2, sizeof(w2));
936 wipememory(w3, sizeof(w3));
937 }
938
939 static void
940 aria_set_decrypt_key(ARIA_context *ctx)
941 {
942 int i;
943
944 for (i = 0; i < 4; i++)
945 {
946 ctx->dec_key[0][i] = ctx->enc_key[ctx->rounds][i];
947 ctx->dec_key[ctx->rounds][i] = ctx->enc_key[0][i];
948 }
949
950 for (i = 1; i < ctx->rounds; i++)
951 {
952 ctx->dec_key[i][0] = aria_m(ctx->enc_key[ctx->rounds - i][0]);
953 ctx->dec_key[i][1] = aria_m(ctx->enc_key[ctx->rounds - i][1]);
954 ctx->dec_key[i][2] = aria_m(ctx->enc_key[ctx->rounds - i][2]);
955 ctx->dec_key[i][3] = aria_m(ctx->enc_key[ctx->rounds - i][3]);
956
957 aria_diff_word(&ctx->dec_key[i][0], &ctx->dec_key[i][1],
958 &ctx->dec_key[i][2], &ctx->dec_key[i][3]);
959 aria_diff_byte(&ctx->dec_key[i][1],
960 &ctx->dec_key[i][2], &ctx->dec_key[i][3]);
961 aria_diff_word(&ctx->dec_key[i][0], &ctx->dec_key[i][1],
962 &ctx->dec_key[i][2], &ctx->dec_key[i][3]);
963 }
964 }
965
966 static NO_INLINE unsigned int
967 aria_crypt(ARIA_context *ctx, byte *out, const byte *in,
968 u32 key[][ARIA_RD_KEY_WORDS])
969 {
970 u32 reg0, reg1, reg2, reg3;
971 int rounds = ctx->rounds;
972 int rkidx = 0;
973
974 reg0 = buf_get_be32(in + 0);
975 reg1 = buf_get_be32(in + 4);
976 reg2 = buf_get_be32(in + 8);
977 reg3 = buf_get_be32(in + 12);
978
979 aria_add_round_key(key[rkidx], &reg0, &reg1, &reg2, &reg3);
980 rkidx++;
981
982 while (1)
983 {
984 aria_subst_diff_odd(&reg0, &reg1, &reg2, &reg3);
985 aria_add_round_key(key[rkidx], &reg0, &reg1, &reg2, &reg3);
986 rkidx++;
987
988 if (rkidx >= rounds)
989 break;
990
991 aria_subst_diff_even(&reg0, &reg1, &reg2, &reg3);
992 aria_add_round_key(key[rkidx], &reg0, &reg1, &reg2, &reg3);
993 rkidx++;
994 }
995
996 aria_last_round(&reg0, &reg1, &reg2, &reg3);
997 aria_add_round_key(key[rkidx], &reg0, &reg1, &reg2, &reg3);
998
999 buf_put_be32(out + 0, reg0);
1000 buf_put_be32(out + 4, reg1);
1001 buf_put_be32(out + 8, reg2);
1002 buf_put_be32(out + 12, reg3);
1003
1004 return 4 * sizeof(void *) + 4 * sizeof(u32); /* stack burn depth */
1005 }
1006
1007 unsigned int
1008 aria_encrypt(void *c, byte *outbuf, const byte *inbuf)
1009 {
1010 ARIA_context *ctx = (ARIA_context *)c;
1011
1012 prefetch_sboxes ();
1013
1014 return aria_crypt (ctx, outbuf, inbuf, ctx->enc_key);
1015 }
1016
1017 unsigned int
1018 aria_decrypt(void *c, byte *outbuf, const byte *inbuf)
1019 {
1020 ARIA_context *ctx = (ARIA_context *)c;
1021
1022 if (!ctx->decryption_prepared)
1023 {
1024 aria_set_decrypt_key (ctx);
1025 ctx->decryption_prepared = 1;
1026 }
1027
1028 prefetch_sboxes ();
1029
1030 return aria_crypt (ctx, outbuf, inbuf, ctx->dec_key);
1031 }
1032
1033
1034 static unsigned int
1035 aria_crypt_2blks(ARIA_context *ctx, byte *out, const byte *in,
1036 u32 key[][ARIA_RD_KEY_WORDS])
1037 {
1038 u32 ra0, ra1, ra2, ra3;
1039 u32 rb0, rb1, rb2, rb3;
1040 int rounds = ctx->rounds;
1041 int rkidx = 0;
1042
1043 ra0 = buf_get_be32(in + 0);
1044 ra1 = buf_get_be32(in + 4);
1045 ra2 = buf_get_be32(in + 8);
1046 ra3 = buf_get_be32(in + 12);
1047 rb0 = buf_get_be32(in + 16);
1048 rb1 = buf_get_be32(in + 20);
1049 rb2 = buf_get_be32(in + 24);
1050 rb3 = buf_get_be32(in + 28);
1051
1052 while (1)
1053 {
1054 aria_add_round_key(key[rkidx], &ra0, &ra1, &ra2, &ra3);
1055 aria_add_round_key(key[rkidx], &rb0, &rb1, &rb2, &rb3);
1056 rkidx++;
1057
1058 aria_subst_diff_odd(&ra0, &ra1, &ra2, &ra3);
1059 aria_subst_diff_odd(&rb0, &rb1, &rb2, &rb3);
1060 aria_add_round_key(key[rkidx], &ra0, &ra1, &ra2, &ra3);
1061 aria_add_round_key(key[rkidx], &rb0, &rb1, &rb2, &rb3);
1062 rkidx++;
1063
1064 if (rkidx >= rounds)
1065 break;
1066
1067 aria_subst_diff_even(&ra0, &ra1, &ra2, &ra3);
1068 aria_subst_diff_even(&rb0, &rb1, &rb2, &rb3);
1069 }
1070
1071 aria_last_round(&ra0, &ra1, &ra2, &ra3);
1072 aria_last_round(&rb0, &rb1, &rb2, &rb3);
1073 aria_add_round_key(key[rkidx], &ra0, &ra1, &ra2, &ra3);
1074 aria_add_round_key(key[rkidx], &rb0, &rb1, &rb2, &rb3);
1075
1076 buf_put_be32(out + 0, ra0);
1077 buf_put_be32(out + 4, ra1);
1078 buf_put_be32(out + 8, ra2);
1079 buf_put_be32(out + 12, ra3);
1080 buf_put_be32(out + 16, rb0);
1081 buf_put_be32(out + 20, rb1);
1082 buf_put_be32(out + 24, rb2);
1083 buf_put_be32(out + 28, rb3);
1084
1085 return 4 * sizeof(void *) + 8 * sizeof(u32); /* stack burn depth */
1086 }
1087
1088 static unsigned int
1089 aria_crypt_blocks (ARIA_context *ctx, byte *out, const byte *in,
1090 size_t num_blks, u32 key[][ARIA_RD_KEY_WORDS])
1091 {
1092 unsigned int burn_depth = 0;
1093
1094 #ifdef USE_GFNI_AVX512
1095 if (ctx->use_gfni_avx512)
1096 {
1097 unsigned int nburn = 0;
1098
1099 while (num_blks >= 64)
1100 {
1101 nburn = aria_gfni_avx512_ecb_crypt_blk64 (ctx, out, in, key);
1102 in += 64 * ARIA_BLOCK_SIZE;
1103 out += 64 * ARIA_BLOCK_SIZE;
1104 num_blks -= 64;
1105 }
1106
1107 burn_depth = nburn > burn_depth ? nburn : burn_depth;
1108
1109 if (num_blks == 0)
1110 return burn_depth;
1111 }
1112 #endif /* USE_AESNI_AVX2 */
1113
1114 #ifdef USE_AESNI_AVX2
1115 if (ctx->use_aesni_avx2 || ctx->use_gfni_avx2)
1116 {
1117 unsigned int nburn = 0;
1118
1119 while (num_blks >= 32)
1120 {
1121 nburn = aria_avx2_ecb_crypt_blk32 (ctx, out, in, key);
1122 in += 32 * ARIA_BLOCK_SIZE;
1123 out += 32 * ARIA_BLOCK_SIZE;
1124 num_blks -= 32;
1125 }
1126
1127 burn_depth = nburn > burn_depth ? nburn : burn_depth;
1128
1129 if (num_blks == 0)
1130 return burn_depth;
1131 }
1132 #endif /* USE_AESNI_AVX2 */
1133
1134 #ifdef USE_AESNI_AVX
1135 if (ctx->use_aesni_avx || ctx->use_gfni_avx)
1136 {
1137 unsigned int nburn = 0;
1138
1139 while (num_blks >= 3)
1140 {
1141 size_t curr_blks = num_blks < 16 ? num_blks : 16;
1142 nburn = aria_avx_ecb_crypt_blk1_16 (ctx, out, in, key, curr_blks);
1143 in += curr_blks * ARIA_BLOCK_SIZE;
1144 out += curr_blks * ARIA_BLOCK_SIZE;
1145 num_blks -= curr_blks;
1146 }
1147
1148 burn_depth = nburn > burn_depth ? nburn : burn_depth;
1149
1150 if (num_blks == 0)
1151 return burn_depth;
1152 }
1153 #endif /* USE_AESNI_AVX */
1154
1155 if (!ctx->bulk_prefetch_ready)
1156 {
1157 prefetch_sboxes();
1158 ctx->bulk_prefetch_ready = 1;
1159 }
1160
1161 while (num_blks >= 2)
1162 {
1163 unsigned int nburn = aria_crypt_2blks (ctx, out, in, key);
1164 burn_depth = nburn > burn_depth ? nburn : burn_depth;
1165 out += 2 * ARIA_BLOCK_SIZE;
1166 in += 2 * ARIA_BLOCK_SIZE;
1167 num_blks -= 2;
1168 }
1169
1170 while (num_blks)
1171 {
1172 unsigned int nburn = aria_crypt (ctx, out, in, key);
1173 burn_depth = nburn > burn_depth ? nburn : burn_depth;
1174 out += ARIA_BLOCK_SIZE;
1175 in += ARIA_BLOCK_SIZE;
1176 num_blks--;
1177 }
1178
1179 if (burn_depth)
1180 burn_depth += sizeof(void *) * 5;
1181 return burn_depth;
1182 }
1183
1184 static unsigned int
1185 aria_enc_blocks (void *c, byte *out, const byte *in, size_t num_blks)
1186 {
1187 ARIA_context *ctx = (ARIA_context *)c;
1188
1189 return aria_crypt_blocks (ctx, out, in, num_blks, ctx->enc_key);
1190 }
1191
1192 static unsigned int
1193 aria_dec_blocks (void *c, byte *out, const byte *in, size_t num_blks)
1194 {
1195 ARIA_context *ctx = (ARIA_context *)c;
1196
1197 return aria_crypt_blocks (ctx, out, in, num_blks, ctx->dec_key);
1198 }
1199
1200
1201 /* Bulk encryption of complete blocks in CTR mode. This function is only
1202 intended for the bulk encryption feature of cipher.c. CTR is expected to be
1203 of size 16. */
1204 static void
1205 _gcry_aria_ctr_enc(void *context, unsigned char *ctr,
1206 void *outbuf_arg, const void *inbuf_arg,
1207 size_t nblocks)
1208 {
1209 ARIA_context *ctx = context;
1210 byte *outbuf = outbuf_arg;
1211 const byte *inbuf = inbuf_arg;
1212 int burn_stack_depth = 0;
1213
1214 #ifdef USE_GFNI_AVX512
1215 if (ctx->use_gfni_avx512)
1216 {
1217 size_t nburn = 0;
1218
1219 while (nblocks >= 64)
1220 {
1221 nburn = aria_gfni_avx512_ctr_crypt_blk64 (ctx, outbuf, inbuf, ctr);
1222 inbuf += 64 * ARIA_BLOCK_SIZE;
1223 outbuf += 64 * ARIA_BLOCK_SIZE;
1224 nblocks -= 64;
1225 }
1226
1227 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1228 }
1229 #endif /* USE_AESNI_AVX */
1230
1231 #ifdef USE_AESNI_AVX2
1232 if (ctx->use_aesni_avx2 || ctx->use_gfni_avx2)
1233 {
1234 size_t nburn = 0;
1235
1236 while (nblocks >= 32)
1237 {
1238 nburn = aria_avx2_ctr_crypt_blk32 (ctx, outbuf, inbuf, ctr);
1239 inbuf += 32 * ARIA_BLOCK_SIZE;
1240 outbuf += 32 * ARIA_BLOCK_SIZE;
1241 nblocks -= 32;
1242 }
1243
1244 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1245 }
1246 #endif /* USE_AESNI_AVX */
1247
1248 #ifdef USE_AESNI_AVX
1249 if (ctx->use_aesni_avx || ctx->use_gfni_avx)
1250 {
1251 size_t nburn = 0;
1252
1253 while (nblocks >= 16)
1254 {
1255 nburn = aria_avx_ctr_crypt_blk16 (ctx, outbuf, inbuf, ctr);
1256 inbuf += 16 * ARIA_BLOCK_SIZE;
1257 outbuf += 16 * ARIA_BLOCK_SIZE;
1258 nblocks -= 16;
1259 }
1260
1261 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1262 }
1263 #endif /* USE_AESNI_AVX */
1264
1265 /* Process remaining blocks. */
1266 if (nblocks)
1267 {
1268 byte tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1269 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1270 size_t nburn = 0;
1271
1272 ctx->bulk_prefetch_ready = 0;
1273
1274 nburn = bulk_ctr_enc_128(ctx, aria_enc_blocks, outbuf, inbuf,
1275 nblocks, ctr, tmpbuf,
1276 sizeof(tmpbuf) / ARIA_BLOCK_SIZE, &tmp_used);
1277 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1278
1279 wipememory (tmpbuf, tmp_used);
1280 }
1281
1282 if (burn_stack_depth)
1283 _gcry_burn_stack (burn_stack_depth);
1284 }
1285
1286 /* Bulk encryption of complete blocks in CBC mode. */
1287 static void
1288 _gcry_aria_cbc_enc (void *context, unsigned char *iv,
1289 void *outbuf_arg, const void *inbuf_arg,
1290 size_t nblocks, int cbc_mac)
1291 {
1292 ARIA_context *ctx = context;
1293 unsigned char *outbuf = outbuf_arg;
1294 const unsigned char *inbuf = inbuf_arg;
1295 unsigned char *last_iv;
1296 unsigned int burn_depth = 0;
1297
1298 prefetch_sboxes();
1299
1300 last_iv = iv;
1301
1302 for (; nblocks; nblocks--)
1303 {
1304 cipher_block_xor (outbuf, inbuf, last_iv, ARIA_BLOCK_SIZE);
1305
1306 burn_depth = aria_crypt (ctx, outbuf, outbuf, ctx->enc_key);
1307
1308 last_iv = outbuf;
1309 inbuf += ARIA_BLOCK_SIZE;
1310 if (!cbc_mac)
1311 outbuf += ARIA_BLOCK_SIZE;
1312 }
1313
1314 if (last_iv != iv)
1315 cipher_block_cpy (iv, last_iv, ARIA_BLOCK_SIZE);
1316
1317 if (burn_depth)
1318 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1319 }
1320
1321 /* Bulk decryption of complete blocks in CBC mode. This function is only
1322 intended for the bulk encryption feature of cipher.c. */
1323 static void
1324 _gcry_aria_cbc_dec(void *context, unsigned char *iv,
1325 void *outbuf_arg, const void *inbuf_arg,
1326 size_t nblocks)
1327 {
1328 ARIA_context *ctx = context;
1329 unsigned char *outbuf = outbuf_arg;
1330 const unsigned char *inbuf = inbuf_arg;
1331 int burn_stack_depth = 0;
1332
1333 if (!ctx->decryption_prepared)
1334 {
1335 aria_set_decrypt_key (ctx);
1336 ctx->decryption_prepared = 1;
1337 }
1338
1339 /* Process remaining blocks. */
1340 if (nblocks)
1341 {
1342 unsigned char tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1343 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1344 size_t nburn;
1345
1346 ctx->bulk_prefetch_ready = 0;
1347
1348 nburn = bulk_cbc_dec_128(ctx, aria_dec_blocks, outbuf, inbuf,
1349 nblocks, iv, tmpbuf,
1350 sizeof(tmpbuf) / ARIA_BLOCK_SIZE, &tmp_used);
1351 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1352
1353 wipememory (tmpbuf, tmp_used);
1354 }
1355
1356 if (burn_stack_depth)
1357 _gcry_burn_stack (burn_stack_depth);
1358 }
1359
1360 /* Bulk encryption of complete blocks in CFB mode. */
1361 static void
1362 _gcry_aria_cfb_enc (void *context, unsigned char *iv,
1363 void *outbuf_arg, const void *inbuf_arg,
1364 size_t nblocks)
1365 {
1366 ARIA_context *ctx = context;
1367 unsigned char *outbuf = outbuf_arg;
1368 const unsigned char *inbuf = inbuf_arg;
1369 unsigned int burn_depth = 0;
1370
1371 prefetch_sboxes();
1372
1373 for (; nblocks; nblocks--)
1374 {
1375 /* Encrypt the IV. */
1376 burn_depth = aria_crypt (ctx, iv, iv, ctx->enc_key);
1377 /* XOR the input with the IV and store input into IV. */
1378 cipher_block_xor_2dst(outbuf, iv, inbuf, ARIA_BLOCK_SIZE);
1379 outbuf += ARIA_BLOCK_SIZE;
1380 inbuf += ARIA_BLOCK_SIZE;
1381 }
1382
1383 if (burn_depth)
1384 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1385 }
1386
1387 /* Bulk decryption of complete blocks in CFB mode. This function is only
1388 intended for the bulk encryption feature of cipher.c. */
1389 static void
1390 _gcry_aria_cfb_dec(void *context, unsigned char *iv,
1391 void *outbuf_arg, const void *inbuf_arg,
1392 size_t nblocks)
1393 {
1394 ARIA_context *ctx = context;
1395 unsigned char *outbuf = outbuf_arg;
1396 const unsigned char *inbuf = inbuf_arg;
1397 int burn_stack_depth = 0;
1398
1399 /* Process remaining blocks. */
1400 if (nblocks)
1401 {
1402 unsigned char tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1403 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1404 size_t nburn;
1405
1406 ctx->bulk_prefetch_ready = 0;
1407
1408 nburn = bulk_cfb_dec_128(ctx, aria_enc_blocks, outbuf, inbuf,
1409 nblocks, iv, tmpbuf,
1410 sizeof(tmpbuf) / ARIA_BLOCK_SIZE, &tmp_used);
1411 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1412
1413 wipememory (tmpbuf, tmp_used);
1414 }
1415
1416 if (burn_stack_depth)
1417 _gcry_burn_stack (burn_stack_depth);
1418 }
1419
1420 /* Bulk encryption/decryption in ECB mode. */
1421 static void
1422 _gcry_aria_ecb_crypt (void *context, void *outbuf_arg,
1423 const void *inbuf_arg, size_t nblocks, int encrypt)
1424 {
1425 ARIA_context *ctx = context;
1426 unsigned char *outbuf = outbuf_arg;
1427 const unsigned char *inbuf = inbuf_arg;
1428 int burn_stack_depth = 0;
1429
1430 if (!encrypt && !ctx->decryption_prepared)
1431 {
1432 aria_set_decrypt_key (ctx);
1433 ctx->decryption_prepared = 1;
1434 }
1435
1436 /* Process remaining blocks. */
1437 if (nblocks)
1438 {
1439 bulk_crypt_fn_t crypt_blk1_n;
1440 size_t nburn;
1441
1442 ctx->bulk_prefetch_ready = 0;
1443 crypt_blk1_n = encrypt ? aria_enc_blocks : aria_dec_blocks;
1444
1445 nburn = bulk_ecb_crypt_128(ctx, crypt_blk1_n,
1446 outbuf, inbuf, nblocks, MAX_PARALLEL_BLKS);
1447 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1448 }
1449
1450 if (burn_stack_depth)
1451 _gcry_burn_stack (burn_stack_depth);
1452 }
1453
1454 /* Bulk encryption/decryption of complete blocks in XTS mode. */
1455 static void
1456 _gcry_aria_xts_crypt (void *context, unsigned char *tweak, void *outbuf_arg,
1457 const void *inbuf_arg, size_t nblocks, int encrypt)
1458 {
1459 ARIA_context *ctx = context;
1460 unsigned char *outbuf = outbuf_arg;
1461 const unsigned char *inbuf = inbuf_arg;
1462 int burn_stack_depth = 0;
1463
1464 if (!encrypt && !ctx->decryption_prepared)
1465 {
1466 aria_set_decrypt_key (ctx);
1467 ctx->decryption_prepared = 1;
1468 }
1469
1470 /* Process remaining blocks. */
1471 if (nblocks)
1472 {
1473 unsigned char tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1474 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1475 bulk_crypt_fn_t crypt_blk1_n;
1476 size_t nburn;
1477
1478 ctx->bulk_prefetch_ready = 0;
1479 crypt_blk1_n = encrypt ? aria_enc_blocks : aria_dec_blocks;
1480
1481 nburn = bulk_xts_crypt_128(ctx, crypt_blk1_n,
1482 outbuf, inbuf, nblocks,
1483 tweak, tmpbuf,
1484 sizeof(tmpbuf) / ARIA_BLOCK_SIZE,
1485 &tmp_used);
1486 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1487
1488 wipememory (tmpbuf, tmp_used);
1489 }
1490
1491 if (burn_stack_depth)
1492 _gcry_burn_stack (burn_stack_depth);
1493 }
1494
1495 /* Bulk encryption of complete blocks in CTR32LE mode (for GCM-SIV). */
1496 static void
1497 _gcry_aria_ctr32le_enc(void *context, unsigned char *ctr,
1498 void *outbuf_arg, const void *inbuf_arg,
1499 size_t nblocks)
1500 {
1501 ARIA_context *ctx = context;
1502 byte *outbuf = outbuf_arg;
1503 const byte *inbuf = inbuf_arg;
1504 int burn_stack_depth = 0;
1505
1506 /* Process remaining blocks. */
1507 if (nblocks)
1508 {
1509 unsigned char tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1510 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1511 size_t nburn;
1512
1513 ctx->bulk_prefetch_ready = 0;
1514
1515 nburn = bulk_ctr32le_enc_128 (ctx, aria_enc_blocks, outbuf, inbuf,
1516 nblocks, ctr, tmpbuf,
1517 sizeof(tmpbuf) / ARIA_BLOCK_SIZE,
1518 &tmp_used);
1519 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1520
1521 wipememory (tmpbuf, tmp_used);
1522 }
1523
1524 if (burn_stack_depth)
1525 _gcry_burn_stack (burn_stack_depth);
1526 }
1527
1528 /* Bulk encryption/decryption of complete blocks in OCB mode. */
1529 static size_t
1530 _gcry_aria_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
1531 const void *inbuf_arg, size_t nblocks, int encrypt)
1532 {
1533 ARIA_context *ctx = (void *)&c->context.c;
1534 unsigned char *outbuf = outbuf_arg;
1535 const unsigned char *inbuf = inbuf_arg;
1536 u64 blkn = c->u_mode.ocb.data_nblocks;
1537 int burn_stack_depth = 0;
1538
1539 if (!encrypt && !ctx->decryption_prepared)
1540 {
1541 aria_set_decrypt_key (ctx);
1542 ctx->decryption_prepared = 1;
1543 }
1544
1545 /* Process remaining blocks. */
1546 if (nblocks)
1547 {
1548 unsigned char tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1549 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1550 bulk_crypt_fn_t crypt_blk1_n;
1551 size_t nburn;
1552
1553 ctx->bulk_prefetch_ready = 0;
1554 crypt_blk1_n = encrypt ? aria_enc_blocks : aria_dec_blocks;
1555
1556 nburn = bulk_ocb_crypt_128 (c, ctx, crypt_blk1_n, outbuf, inbuf, nblocks,
1557 &blkn, encrypt, tmpbuf,
1558 sizeof(tmpbuf) / ARIA_BLOCK_SIZE,
1559 &tmp_used);
1560 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1561
1562 wipememory (tmpbuf, tmp_used);
1563 }
1564
1565 c->u_mode.ocb.data_nblocks = blkn;
1566
1567 if (burn_stack_depth)
1568 _gcry_burn_stack (burn_stack_depth);
1569
1570 return 0;
1571 }
1572
1573 /* Bulk authentication of complete blocks in OCB mode. */
1574 static size_t
1575 _gcry_aria_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg, size_t nblocks)
1576 {
1577 ARIA_context *ctx = (void *)&c->context.c;
1578 const unsigned char *abuf = abuf_arg;
1579 u64 blkn = c->u_mode.ocb.aad_nblocks;
1580 int burn_stack_depth = 0;
1581
1582 /* Process remaining blocks. */
1583 if (nblocks)
1584 {
1585 unsigned char tmpbuf[MAX_PARALLEL_BLKS * ARIA_BLOCK_SIZE];
1586 unsigned int tmp_used = ARIA_BLOCK_SIZE;
1587 size_t nburn;
1588
1589 ctx->bulk_prefetch_ready = 0;
1590
1591 nburn = bulk_ocb_auth_128 (c, ctx, aria_enc_blocks, abuf, nblocks,
1592 &blkn, tmpbuf,
1593 sizeof(tmpbuf) / ARIA_BLOCK_SIZE, &tmp_used);
1594 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1595
1596 wipememory (tmpbuf, tmp_used);
1597 }
1598
1599 c->u_mode.ocb.aad_nblocks = blkn;
1600
1601 if (burn_stack_depth)
1602 _gcry_burn_stack (burn_stack_depth);
1603
1604 return 0;
1605 }
1606
1607
1608 static gcry_err_code_t
1609 aria_setkey(void *c, const byte *key, unsigned keylen,
1610 cipher_bulk_ops_t *bulk_ops)
1611 {
1612 ARIA_context *ctx = c;
1613 static int initialized = 0;
1614 static const char *selftest_failed = NULL;
1615 unsigned int hwf = _gcry_get_hw_features ();
1616
1617 (void)hwf;
1618
1619 if (keylen != 16 && keylen != 24 && keylen != 32)
1620 return GPG_ERR_INV_KEYLEN;
1621
1622 if (!initialized)
1623 {
1624 initialized = 1;
1625 selftest_failed = aria_selftest ();
1626 if (selftest_failed)
1627 log_error("%s\n", selftest_failed);
1628 }
1629
1630 if (selftest_failed)
1631 return GPG_ERR_SELFTEST_FAILED;
1632
1633 #ifdef USE_GFNI_AVX512
1634 ctx->use_gfni_avx512 = (hwf & HWF_INTEL_GFNI) && (hwf & HWF_INTEL_AVX512);
1635 #endif
1636 #ifdef USE_AESNI_AVX2
1637 ctx->use_aesni_avx2 = (hwf & HWF_INTEL_AESNI) && (hwf & HWF_INTEL_AVX2);
1638 #endif
1639 #ifdef USE_GFNI_AVX2
1640 ctx->use_gfni_avx2 = (hwf & HWF_INTEL_GFNI) && (hwf & HWF_INTEL_AVX2);
1641 #endif
1642 #ifdef USE_VAES_AVX2
1643 ctx->use_vaes_avx2 = (hwf & HWF_INTEL_VAES_VPCLMUL) && (hwf & HWF_INTEL_AVX2);
1644 #endif
1645 #ifdef USE_AESNI_AVX
1646 ctx->use_aesni_avx = (hwf & HWF_INTEL_AESNI) && (hwf & HWF_INTEL_AVX);
1647 #endif
1648 #ifdef USE_GFNI_AVX
1649 ctx->use_gfni_avx = (hwf & HWF_INTEL_GFNI) && (hwf & HWF_INTEL_AVX);
1650 #endif
1651
1652 /* Setup bulk encryption routines. */
1653 memset (bulk_ops, 0, sizeof(*bulk_ops));
1654 bulk_ops->cbc_enc = _gcry_aria_cbc_enc;
1655 bulk_ops->cbc_dec = _gcry_aria_cbc_dec;
1656 bulk_ops->cfb_enc = _gcry_aria_cfb_enc;
1657 bulk_ops->cfb_dec = _gcry_aria_cfb_dec;
1658 bulk_ops->ctr_enc = _gcry_aria_ctr_enc;
1659 bulk_ops->ctr32le_enc = _gcry_aria_ctr32le_enc;
1660 bulk_ops->ecb_crypt = _gcry_aria_ecb_crypt;
1661 bulk_ops->xts_crypt = _gcry_aria_xts_crypt;
1662 bulk_ops->ocb_crypt = _gcry_aria_ocb_crypt;
1663 bulk_ops->ocb_auth = _gcry_aria_ocb_auth;
1664
1665 /* Setup context and encryption key. */
1666 ctx->decryption_prepared = 0;
1667 aria_set_encrypt_key (ctx, key, keylen);
1668
1669 _gcry_burn_stack (3 * sizeof(void *) + 5 * 4 * sizeof(u32));
1670 return 0;
1671 }
1672
1673
1674 static const char *
1675 aria_selftest (void)
1676 {
1677 ARIA_context ctx;
1678 byte scratch[16];
1679
1680 static const byte key[16] = {
1681 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1682 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
1683 };
1684 static const byte plaintext[16] = {
1685 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
1686 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
1687 };
1688 static const byte ciphertext[16] = {
1689 0xd7, 0x18, 0xfb, 0xd6, 0xab, 0x64, 0x4c, 0x73,
1690 0x9d, 0xa9, 0x5f, 0x3b, 0xe6, 0x45, 0x17, 0x78
1691 };
1692
1693 memset (&ctx, 0, sizeof(ctx));
1694
1695 aria_set_encrypt_key (&ctx, key, 16);
1696 aria_encrypt (&ctx, scratch, plaintext);
1697 if (memcmp (scratch, ciphertext, sizeof (ciphertext)))
1698 return "ARIA test encryption failed.";
1699 aria_decrypt (&ctx, scratch, scratch);
1700 if (memcmp (scratch, plaintext, sizeof (plaintext)))
1701 return "ARIA test decryption failed.";
1702
1703 return NULL;
1704 }
1705
1706
1707 static const gcry_cipher_oid_spec_t aria128_oids[] =
1708 {
1709 { "1.2.410.200046.1.1.1", GCRY_CIPHER_MODE_ECB },
1710 { "1.2.410.200046.1.1.2", GCRY_CIPHER_MODE_CBC },
1711 { "1.2.410.200046.1.1.3", GCRY_CIPHER_MODE_CFB },
1712 { "1.2.410.200046.1.1.4", GCRY_CIPHER_MODE_OFB },
1713 { "1.2.410.200046.1.1.5", GCRY_CIPHER_MODE_CTR },
1714 { "1.2.410.200046.1.1.34", GCRY_CIPHER_MODE_GCM },
1715 { "1.2.410.200046.1.1.37", GCRY_CIPHER_MODE_CCM },
1716 { NULL }
1717 };
1718
1719 static const gcry_cipher_oid_spec_t aria192_oids[] =
1720 {
1721 { "1.2.410.200046.1.1.6", GCRY_CIPHER_MODE_ECB },
1722 { "1.2.410.200046.1.1.7", GCRY_CIPHER_MODE_CBC },
1723 { "1.2.410.200046.1.1.8", GCRY_CIPHER_MODE_CFB },
1724 { "1.2.410.200046.1.1.9", GCRY_CIPHER_MODE_OFB },
1725 { "1.2.410.200046.1.1.10", GCRY_CIPHER_MODE_CTR },
1726 { "1.2.410.200046.1.1.35", GCRY_CIPHER_MODE_GCM },
1727 { "1.2.410.200046.1.1.38", GCRY_CIPHER_MODE_CCM },
1728 { NULL }
1729 };
1730
1731 static const gcry_cipher_oid_spec_t aria256_oids[] =
1732 {
1733 { "1.2.410.200046.1.1.11", GCRY_CIPHER_MODE_ECB },
1734 { "1.2.410.200046.1.1.12", GCRY_CIPHER_MODE_CBC },
1735 { "1.2.410.200046.1.1.13", GCRY_CIPHER_MODE_CFB },
1736 { "1.2.410.200046.1.1.14", GCRY_CIPHER_MODE_OFB },
1737 { "1.2.410.200046.1.1.15", GCRY_CIPHER_MODE_CTR },
1738 { "1.2.410.200046.1.1.36", GCRY_CIPHER_MODE_GCM },
1739 { "1.2.410.200046.1.1.39", GCRY_CIPHER_MODE_CCM },
1740 { NULL }
1741 };
1742
1743 gcry_cipher_spec_t _gcry_cipher_spec_aria128 =
1744 {
1745 GCRY_CIPHER_ARIA128, { 0, 0 },
1746 "ARIA128", NULL, aria128_oids, ARIA_BLOCK_SIZE, 128,
1747 sizeof(ARIA_context), aria_setkey, aria_encrypt, aria_decrypt
1748 };
1749
1750 gcry_cipher_spec_t _gcry_cipher_spec_aria192 =
1751 {
1752 GCRY_CIPHER_ARIA192, { 0, 0 },
1753 "ARIA192",NULL,aria192_oids, ARIA_BLOCK_SIZE, 192,
1754 sizeof(ARIA_context), aria_setkey, aria_encrypt, aria_decrypt
1755 };
1756
1757 gcry_cipher_spec_t _gcry_cipher_spec_aria256 =
1758 {
1759 GCRY_CIPHER_ARIA256, { 0, 0 },
1760 "ARIA256", NULL, aria256_oids, ARIA_BLOCK_SIZE, 256,
1761 sizeof(ARIA_context), aria_setkey, aria_encrypt, aria_decrypt
1762 };