xref: /linux/crypto/aegis128-core.c (revision 09149997)
1cf3d41adSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0-or-later
2cf3d41adSArd Biesheuvel /*
3cf3d41adSArd Biesheuvel  * The AEGIS-128 Authenticated-Encryption Algorithm
4cf3d41adSArd Biesheuvel  *
5cf3d41adSArd Biesheuvel  * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
6cf3d41adSArd Biesheuvel  * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
7cf3d41adSArd Biesheuvel  */
8cf3d41adSArd Biesheuvel 
9cf3d41adSArd Biesheuvel #include <crypto/algapi.h>
10cf3d41adSArd Biesheuvel #include <crypto/internal/aead.h>
11cf3d41adSArd Biesheuvel #include <crypto/internal/simd.h>
12cf3d41adSArd Biesheuvel #include <crypto/internal/skcipher.h>
13cf3d41adSArd Biesheuvel #include <crypto/scatterwalk.h>
14cf3d41adSArd Biesheuvel #include <linux/err.h>
15cf3d41adSArd Biesheuvel #include <linux/init.h>
162698bce1SArd Biesheuvel #include <linux/jump_label.h>
17cf3d41adSArd Biesheuvel #include <linux/kernel.h>
18cf3d41adSArd Biesheuvel #include <linux/module.h>
19cf3d41adSArd Biesheuvel #include <linux/scatterlist.h>
20cf3d41adSArd Biesheuvel 
21cf3d41adSArd Biesheuvel #include <asm/simd.h>
22cf3d41adSArd Biesheuvel 
23cf3d41adSArd Biesheuvel #include "aegis.h"
24cf3d41adSArd Biesheuvel 
25cf3d41adSArd Biesheuvel #define AEGIS128_NONCE_SIZE 16
26cf3d41adSArd Biesheuvel #define AEGIS128_STATE_BLOCKS 5
27cf3d41adSArd Biesheuvel #define AEGIS128_KEY_SIZE 16
28cf3d41adSArd Biesheuvel #define AEGIS128_MIN_AUTH_SIZE 8
29cf3d41adSArd Biesheuvel #define AEGIS128_MAX_AUTH_SIZE 16
30cf3d41adSArd Biesheuvel 
31cf3d41adSArd Biesheuvel struct aegis_state {
32cf3d41adSArd Biesheuvel 	union aegis_block blocks[AEGIS128_STATE_BLOCKS];
33cf3d41adSArd Biesheuvel };
34cf3d41adSArd Biesheuvel 
35cf3d41adSArd Biesheuvel struct aegis_ctx {
36cf3d41adSArd Biesheuvel 	union aegis_block key;
37cf3d41adSArd Biesheuvel };
38cf3d41adSArd Biesheuvel 
392698bce1SArd Biesheuvel static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_simd);
40cf3d41adSArd Biesheuvel 
41f1d087b9SYueHaibing static const union aegis_block crypto_aegis_const[2] = {
42f1d087b9SYueHaibing 	{ .words64 = {
43f1d087b9SYueHaibing 		cpu_to_le64(U64_C(0x0d08050302010100)),
44f1d087b9SYueHaibing 		cpu_to_le64(U64_C(0x6279e99059372215)),
45f1d087b9SYueHaibing 	} },
46f1d087b9SYueHaibing 	{ .words64 = {
47f1d087b9SYueHaibing 		cpu_to_le64(U64_C(0xf12fc26d55183ddb)),
48f1d087b9SYueHaibing 		cpu_to_le64(U64_C(0xdd28b57342311120)),
49f1d087b9SYueHaibing 	} },
50f1d087b9SYueHaibing };
51f1d087b9SYueHaibing 
aegis128_do_simd(void)52cf3d41adSArd Biesheuvel static bool aegis128_do_simd(void)
53cf3d41adSArd Biesheuvel {
54cf3d41adSArd Biesheuvel #ifdef CONFIG_CRYPTO_AEGIS128_SIMD
552698bce1SArd Biesheuvel 	if (static_branch_likely(&have_simd))
56cf3d41adSArd Biesheuvel 		return crypto_simd_usable();
57cf3d41adSArd Biesheuvel #endif
58cf3d41adSArd Biesheuvel 	return false;
59cf3d41adSArd Biesheuvel }
60cf3d41adSArd Biesheuvel 
crypto_aegis128_update(struct aegis_state * state)61cf3d41adSArd Biesheuvel static void crypto_aegis128_update(struct aegis_state *state)
62cf3d41adSArd Biesheuvel {
63cf3d41adSArd Biesheuvel 	union aegis_block tmp;
64cf3d41adSArd Biesheuvel 	unsigned int i;
65cf3d41adSArd Biesheuvel 
66cf3d41adSArd Biesheuvel 	tmp = state->blocks[AEGIS128_STATE_BLOCKS - 1];
67cf3d41adSArd Biesheuvel 	for (i = AEGIS128_STATE_BLOCKS - 1; i > 0; i--)
68cf3d41adSArd Biesheuvel 		crypto_aegis_aesenc(&state->blocks[i], &state->blocks[i - 1],
69cf3d41adSArd Biesheuvel 				    &state->blocks[i]);
70cf3d41adSArd Biesheuvel 	crypto_aegis_aesenc(&state->blocks[0], &tmp, &state->blocks[0]);
71cf3d41adSArd Biesheuvel }
72cf3d41adSArd Biesheuvel 
crypto_aegis128_update_a(struct aegis_state * state,const union aegis_block * msg,bool do_simd)73cf3d41adSArd Biesheuvel static void crypto_aegis128_update_a(struct aegis_state *state,
74ac50aec4SArd Biesheuvel 				     const union aegis_block *msg,
75ac50aec4SArd Biesheuvel 				     bool do_simd)
76cf3d41adSArd Biesheuvel {
77*0464e0efSArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) && do_simd) {
78cf3d41adSArd Biesheuvel 		crypto_aegis128_update_simd(state, msg);
79cf3d41adSArd Biesheuvel 		return;
80cf3d41adSArd Biesheuvel 	}
81cf3d41adSArd Biesheuvel 
82cf3d41adSArd Biesheuvel 	crypto_aegis128_update(state);
83cf3d41adSArd Biesheuvel 	crypto_aegis_block_xor(&state->blocks[0], msg);
84cf3d41adSArd Biesheuvel }
85cf3d41adSArd Biesheuvel 
crypto_aegis128_update_u(struct aegis_state * state,const void * msg,bool do_simd)86ac50aec4SArd Biesheuvel static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg,
87ac50aec4SArd Biesheuvel 				     bool do_simd)
88cf3d41adSArd Biesheuvel {
89*0464e0efSArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) && do_simd) {
90cf3d41adSArd Biesheuvel 		crypto_aegis128_update_simd(state, msg);
91cf3d41adSArd Biesheuvel 		return;
92cf3d41adSArd Biesheuvel 	}
93cf3d41adSArd Biesheuvel 
94cf3d41adSArd Biesheuvel 	crypto_aegis128_update(state);
95cf3d41adSArd Biesheuvel 	crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
96cf3d41adSArd Biesheuvel }
97cf3d41adSArd Biesheuvel 
crypto_aegis128_init(struct aegis_state * state,const union aegis_block * key,const u8 * iv)98cf3d41adSArd Biesheuvel static void crypto_aegis128_init(struct aegis_state *state,
99cf3d41adSArd Biesheuvel 				 const union aegis_block *key,
100cf3d41adSArd Biesheuvel 				 const u8 *iv)
101cf3d41adSArd Biesheuvel {
102cf3d41adSArd Biesheuvel 	union aegis_block key_iv;
103cf3d41adSArd Biesheuvel 	unsigned int i;
104cf3d41adSArd Biesheuvel 
105cf3d41adSArd Biesheuvel 	key_iv = *key;
106cf3d41adSArd Biesheuvel 	crypto_xor(key_iv.bytes, iv, AEGIS_BLOCK_SIZE);
107cf3d41adSArd Biesheuvel 
108cf3d41adSArd Biesheuvel 	state->blocks[0] = key_iv;
109cf3d41adSArd Biesheuvel 	state->blocks[1] = crypto_aegis_const[1];
110cf3d41adSArd Biesheuvel 	state->blocks[2] = crypto_aegis_const[0];
111cf3d41adSArd Biesheuvel 	state->blocks[3] = *key;
112cf3d41adSArd Biesheuvel 	state->blocks[4] = *key;
113cf3d41adSArd Biesheuvel 
114cf3d41adSArd Biesheuvel 	crypto_aegis_block_xor(&state->blocks[3], &crypto_aegis_const[0]);
115cf3d41adSArd Biesheuvel 	crypto_aegis_block_xor(&state->blocks[4], &crypto_aegis_const[1]);
116cf3d41adSArd Biesheuvel 
117cf3d41adSArd Biesheuvel 	for (i = 0; i < 5; i++) {
118ac50aec4SArd Biesheuvel 		crypto_aegis128_update_a(state, key, false);
119ac50aec4SArd Biesheuvel 		crypto_aegis128_update_a(state, &key_iv, false);
120cf3d41adSArd Biesheuvel 	}
121cf3d41adSArd Biesheuvel }
122cf3d41adSArd Biesheuvel 
crypto_aegis128_ad(struct aegis_state * state,const u8 * src,unsigned int size,bool do_simd)123cf3d41adSArd Biesheuvel static void crypto_aegis128_ad(struct aegis_state *state,
124ac50aec4SArd Biesheuvel 			       const u8 *src, unsigned int size,
125ac50aec4SArd Biesheuvel 			       bool do_simd)
126cf3d41adSArd Biesheuvel {
127cf3d41adSArd Biesheuvel 	if (AEGIS_ALIGNED(src)) {
128cf3d41adSArd Biesheuvel 		const union aegis_block *src_blk =
129cf3d41adSArd Biesheuvel 				(const union aegis_block *)src;
130cf3d41adSArd Biesheuvel 
131cf3d41adSArd Biesheuvel 		while (size >= AEGIS_BLOCK_SIZE) {
132ac50aec4SArd Biesheuvel 			crypto_aegis128_update_a(state, src_blk, do_simd);
133cf3d41adSArd Biesheuvel 
134cf3d41adSArd Biesheuvel 			size -= AEGIS_BLOCK_SIZE;
135cf3d41adSArd Biesheuvel 			src_blk++;
136cf3d41adSArd Biesheuvel 		}
137cf3d41adSArd Biesheuvel 	} else {
138cf3d41adSArd Biesheuvel 		while (size >= AEGIS_BLOCK_SIZE) {
139ac50aec4SArd Biesheuvel 			crypto_aegis128_update_u(state, src, do_simd);
140cf3d41adSArd Biesheuvel 
141cf3d41adSArd Biesheuvel 			size -= AEGIS_BLOCK_SIZE;
142cf3d41adSArd Biesheuvel 			src += AEGIS_BLOCK_SIZE;
143cf3d41adSArd Biesheuvel 		}
144cf3d41adSArd Biesheuvel 	}
145cf3d41adSArd Biesheuvel }
146cf3d41adSArd Biesheuvel 
crypto_aegis128_wipe_chunk(struct aegis_state * state,u8 * dst,const u8 * src,unsigned int size)14702685906SArd Biesheuvel static void crypto_aegis128_wipe_chunk(struct aegis_state *state, u8 *dst,
14802685906SArd Biesheuvel 				       const u8 *src, unsigned int size)
14902685906SArd Biesheuvel {
15002685906SArd Biesheuvel 	memzero_explicit(dst, size);
15102685906SArd Biesheuvel }
15202685906SArd Biesheuvel 
crypto_aegis128_encrypt_chunk(struct aegis_state * state,u8 * dst,const u8 * src,unsigned int size)153cf3d41adSArd Biesheuvel static void crypto_aegis128_encrypt_chunk(struct aegis_state *state, u8 *dst,
154cf3d41adSArd Biesheuvel 					  const u8 *src, unsigned int size)
155cf3d41adSArd Biesheuvel {
156cf3d41adSArd Biesheuvel 	union aegis_block tmp;
157cf3d41adSArd Biesheuvel 
158cf3d41adSArd Biesheuvel 	if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
159cf3d41adSArd Biesheuvel 		while (size >= AEGIS_BLOCK_SIZE) {
160cf3d41adSArd Biesheuvel 			union aegis_block *dst_blk =
161cf3d41adSArd Biesheuvel 					(union aegis_block *)dst;
162cf3d41adSArd Biesheuvel 			const union aegis_block *src_blk =
163cf3d41adSArd Biesheuvel 					(const union aegis_block *)src;
164cf3d41adSArd Biesheuvel 
165cf3d41adSArd Biesheuvel 			tmp = state->blocks[2];
166cf3d41adSArd Biesheuvel 			crypto_aegis_block_and(&tmp, &state->blocks[3]);
167cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[4]);
168cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[1]);
169cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, src_blk);
170cf3d41adSArd Biesheuvel 
171ac50aec4SArd Biesheuvel 			crypto_aegis128_update_a(state, src_blk, false);
172cf3d41adSArd Biesheuvel 
173cf3d41adSArd Biesheuvel 			*dst_blk = tmp;
174cf3d41adSArd Biesheuvel 
175cf3d41adSArd Biesheuvel 			size -= AEGIS_BLOCK_SIZE;
176cf3d41adSArd Biesheuvel 			src += AEGIS_BLOCK_SIZE;
177cf3d41adSArd Biesheuvel 			dst += AEGIS_BLOCK_SIZE;
178cf3d41adSArd Biesheuvel 		}
179cf3d41adSArd Biesheuvel 	} else {
180cf3d41adSArd Biesheuvel 		while (size >= AEGIS_BLOCK_SIZE) {
181cf3d41adSArd Biesheuvel 			tmp = state->blocks[2];
182cf3d41adSArd Biesheuvel 			crypto_aegis_block_and(&tmp, &state->blocks[3]);
183cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[4]);
184cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[1]);
185cf3d41adSArd Biesheuvel 			crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
186cf3d41adSArd Biesheuvel 
187ac50aec4SArd Biesheuvel 			crypto_aegis128_update_u(state, src, false);
188cf3d41adSArd Biesheuvel 
189cf3d41adSArd Biesheuvel 			memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
190cf3d41adSArd Biesheuvel 
191cf3d41adSArd Biesheuvel 			size -= AEGIS_BLOCK_SIZE;
192cf3d41adSArd Biesheuvel 			src += AEGIS_BLOCK_SIZE;
193cf3d41adSArd Biesheuvel 			dst += AEGIS_BLOCK_SIZE;
194cf3d41adSArd Biesheuvel 		}
195cf3d41adSArd Biesheuvel 	}
196cf3d41adSArd Biesheuvel 
197cf3d41adSArd Biesheuvel 	if (size > 0) {
198cf3d41adSArd Biesheuvel 		union aegis_block msg = {};
199cf3d41adSArd Biesheuvel 		memcpy(msg.bytes, src, size);
200cf3d41adSArd Biesheuvel 
201cf3d41adSArd Biesheuvel 		tmp = state->blocks[2];
202cf3d41adSArd Biesheuvel 		crypto_aegis_block_and(&tmp, &state->blocks[3]);
203cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(&tmp, &state->blocks[4]);
204cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(&tmp, &state->blocks[1]);
205cf3d41adSArd Biesheuvel 
206ac50aec4SArd Biesheuvel 		crypto_aegis128_update_a(state, &msg, false);
207cf3d41adSArd Biesheuvel 
208cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(&msg, &tmp);
209cf3d41adSArd Biesheuvel 
210cf3d41adSArd Biesheuvel 		memcpy(dst, msg.bytes, size);
211cf3d41adSArd Biesheuvel 	}
212cf3d41adSArd Biesheuvel }
213cf3d41adSArd Biesheuvel 
crypto_aegis128_decrypt_chunk(struct aegis_state * state,u8 * dst,const u8 * src,unsigned int size)214cf3d41adSArd Biesheuvel static void crypto_aegis128_decrypt_chunk(struct aegis_state *state, u8 *dst,
215cf3d41adSArd Biesheuvel 					  const u8 *src, unsigned int size)
216cf3d41adSArd Biesheuvel {
217cf3d41adSArd Biesheuvel 	union aegis_block tmp;
218cf3d41adSArd Biesheuvel 
219cf3d41adSArd Biesheuvel 	if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
220cf3d41adSArd Biesheuvel 		while (size >= AEGIS_BLOCK_SIZE) {
221cf3d41adSArd Biesheuvel 			union aegis_block *dst_blk =
222cf3d41adSArd Biesheuvel 					(union aegis_block *)dst;
223cf3d41adSArd Biesheuvel 			const union aegis_block *src_blk =
224cf3d41adSArd Biesheuvel 					(const union aegis_block *)src;
225cf3d41adSArd Biesheuvel 
226cf3d41adSArd Biesheuvel 			tmp = state->blocks[2];
227cf3d41adSArd Biesheuvel 			crypto_aegis_block_and(&tmp, &state->blocks[3]);
228cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[4]);
229cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[1]);
230cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, src_blk);
231cf3d41adSArd Biesheuvel 
232ac50aec4SArd Biesheuvel 			crypto_aegis128_update_a(state, &tmp, false);
233cf3d41adSArd Biesheuvel 
234cf3d41adSArd Biesheuvel 			*dst_blk = tmp;
235cf3d41adSArd Biesheuvel 
236cf3d41adSArd Biesheuvel 			size -= AEGIS_BLOCK_SIZE;
237cf3d41adSArd Biesheuvel 			src += AEGIS_BLOCK_SIZE;
238cf3d41adSArd Biesheuvel 			dst += AEGIS_BLOCK_SIZE;
239cf3d41adSArd Biesheuvel 		}
240cf3d41adSArd Biesheuvel 	} else {
241cf3d41adSArd Biesheuvel 		while (size >= AEGIS_BLOCK_SIZE) {
242cf3d41adSArd Biesheuvel 			tmp = state->blocks[2];
243cf3d41adSArd Biesheuvel 			crypto_aegis_block_and(&tmp, &state->blocks[3]);
244cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[4]);
245cf3d41adSArd Biesheuvel 			crypto_aegis_block_xor(&tmp, &state->blocks[1]);
246cf3d41adSArd Biesheuvel 			crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
247cf3d41adSArd Biesheuvel 
248ac50aec4SArd Biesheuvel 			crypto_aegis128_update_a(state, &tmp, false);
249cf3d41adSArd Biesheuvel 
250cf3d41adSArd Biesheuvel 			memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
251cf3d41adSArd Biesheuvel 
252cf3d41adSArd Biesheuvel 			size -= AEGIS_BLOCK_SIZE;
253cf3d41adSArd Biesheuvel 			src += AEGIS_BLOCK_SIZE;
254cf3d41adSArd Biesheuvel 			dst += AEGIS_BLOCK_SIZE;
255cf3d41adSArd Biesheuvel 		}
256cf3d41adSArd Biesheuvel 	}
257cf3d41adSArd Biesheuvel 
258cf3d41adSArd Biesheuvel 	if (size > 0) {
259cf3d41adSArd Biesheuvel 		union aegis_block msg = {};
260cf3d41adSArd Biesheuvel 		memcpy(msg.bytes, src, size);
261cf3d41adSArd Biesheuvel 
262cf3d41adSArd Biesheuvel 		tmp = state->blocks[2];
263cf3d41adSArd Biesheuvel 		crypto_aegis_block_and(&tmp, &state->blocks[3]);
264cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(&tmp, &state->blocks[4]);
265cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(&tmp, &state->blocks[1]);
266cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(&msg, &tmp);
267cf3d41adSArd Biesheuvel 
268cf3d41adSArd Biesheuvel 		memset(msg.bytes + size, 0, AEGIS_BLOCK_SIZE - size);
269cf3d41adSArd Biesheuvel 
270ac50aec4SArd Biesheuvel 		crypto_aegis128_update_a(state, &msg, false);
271cf3d41adSArd Biesheuvel 
272cf3d41adSArd Biesheuvel 		memcpy(dst, msg.bytes, size);
273cf3d41adSArd Biesheuvel 	}
274cf3d41adSArd Biesheuvel }
275cf3d41adSArd Biesheuvel 
crypto_aegis128_process_ad(struct aegis_state * state,struct scatterlist * sg_src,unsigned int assoclen,bool do_simd)276cf3d41adSArd Biesheuvel static void crypto_aegis128_process_ad(struct aegis_state *state,
277cf3d41adSArd Biesheuvel 				       struct scatterlist *sg_src,
278ac50aec4SArd Biesheuvel 				       unsigned int assoclen,
279ac50aec4SArd Biesheuvel 				       bool do_simd)
280cf3d41adSArd Biesheuvel {
281cf3d41adSArd Biesheuvel 	struct scatter_walk walk;
282cf3d41adSArd Biesheuvel 	union aegis_block buf;
283cf3d41adSArd Biesheuvel 	unsigned int pos = 0;
284cf3d41adSArd Biesheuvel 
285cf3d41adSArd Biesheuvel 	scatterwalk_start(&walk, sg_src);
286cf3d41adSArd Biesheuvel 	while (assoclen != 0) {
287cf3d41adSArd Biesheuvel 		unsigned int size = scatterwalk_clamp(&walk, assoclen);
288cf3d41adSArd Biesheuvel 		unsigned int left = size;
289cf3d41adSArd Biesheuvel 		void *mapped = scatterwalk_map(&walk);
290cf3d41adSArd Biesheuvel 		const u8 *src = (const u8 *)mapped;
291cf3d41adSArd Biesheuvel 
292cf3d41adSArd Biesheuvel 		if (pos + size >= AEGIS_BLOCK_SIZE) {
293cf3d41adSArd Biesheuvel 			if (pos > 0) {
294cf3d41adSArd Biesheuvel 				unsigned int fill = AEGIS_BLOCK_SIZE - pos;
295cf3d41adSArd Biesheuvel 				memcpy(buf.bytes + pos, src, fill);
296ac50aec4SArd Biesheuvel 				crypto_aegis128_update_a(state, &buf, do_simd);
297cf3d41adSArd Biesheuvel 				pos = 0;
298cf3d41adSArd Biesheuvel 				left -= fill;
299cf3d41adSArd Biesheuvel 				src += fill;
300cf3d41adSArd Biesheuvel 			}
301cf3d41adSArd Biesheuvel 
302ac50aec4SArd Biesheuvel 			crypto_aegis128_ad(state, src, left, do_simd);
303cf3d41adSArd Biesheuvel 			src += left & ~(AEGIS_BLOCK_SIZE - 1);
304cf3d41adSArd Biesheuvel 			left &= AEGIS_BLOCK_SIZE - 1;
305cf3d41adSArd Biesheuvel 		}
306cf3d41adSArd Biesheuvel 
307cf3d41adSArd Biesheuvel 		memcpy(buf.bytes + pos, src, left);
308cf3d41adSArd Biesheuvel 
309cf3d41adSArd Biesheuvel 		pos += left;
310cf3d41adSArd Biesheuvel 		assoclen -= size;
311cf3d41adSArd Biesheuvel 		scatterwalk_unmap(mapped);
312cf3d41adSArd Biesheuvel 		scatterwalk_advance(&walk, size);
313cf3d41adSArd Biesheuvel 		scatterwalk_done(&walk, 0, assoclen);
314cf3d41adSArd Biesheuvel 	}
315cf3d41adSArd Biesheuvel 
316cf3d41adSArd Biesheuvel 	if (pos > 0) {
317cf3d41adSArd Biesheuvel 		memset(buf.bytes + pos, 0, AEGIS_BLOCK_SIZE - pos);
318ac50aec4SArd Biesheuvel 		crypto_aegis128_update_a(state, &buf, do_simd);
319cf3d41adSArd Biesheuvel 	}
320cf3d41adSArd Biesheuvel }
321cf3d41adSArd Biesheuvel 
3222698bce1SArd Biesheuvel static __always_inline
crypto_aegis128_process_crypt(struct aegis_state * state,struct skcipher_walk * walk,void (* crypt)(struct aegis_state * state,u8 * dst,const u8 * src,unsigned int size))3232698bce1SArd Biesheuvel int crypto_aegis128_process_crypt(struct aegis_state *state,
3242698bce1SArd Biesheuvel 				  struct skcipher_walk *walk,
3252698bce1SArd Biesheuvel 				  void (*crypt)(struct aegis_state *state,
3262698bce1SArd Biesheuvel 					        u8 *dst, const u8 *src,
3272698bce1SArd Biesheuvel 					        unsigned int size))
328cf3d41adSArd Biesheuvel {
3292698bce1SArd Biesheuvel 	int err = 0;
330cf3d41adSArd Biesheuvel 
3312698bce1SArd Biesheuvel 	while (walk->nbytes) {
3322698bce1SArd Biesheuvel 		unsigned int nbytes = walk->nbytes;
333cf3d41adSArd Biesheuvel 
3342698bce1SArd Biesheuvel 		if (nbytes < walk->total)
3352698bce1SArd Biesheuvel 			nbytes = round_down(nbytes, walk->stride);
336cf3d41adSArd Biesheuvel 
3372698bce1SArd Biesheuvel 		crypt(state, walk->dst.virt.addr, walk->src.virt.addr, nbytes);
338cf3d41adSArd Biesheuvel 
3392698bce1SArd Biesheuvel 		err = skcipher_walk_done(walk, walk->nbytes - nbytes);
340cf3d41adSArd Biesheuvel 	}
3412698bce1SArd Biesheuvel 	return err;
342cf3d41adSArd Biesheuvel }
343cf3d41adSArd Biesheuvel 
crypto_aegis128_final(struct aegis_state * state,union aegis_block * tag_xor,u64 assoclen,u64 cryptlen)344cf3d41adSArd Biesheuvel static void crypto_aegis128_final(struct aegis_state *state,
345cf3d41adSArd Biesheuvel 				  union aegis_block *tag_xor,
346cf3d41adSArd Biesheuvel 				  u64 assoclen, u64 cryptlen)
347cf3d41adSArd Biesheuvel {
348cf3d41adSArd Biesheuvel 	u64 assocbits = assoclen * 8;
349cf3d41adSArd Biesheuvel 	u64 cryptbits = cryptlen * 8;
350cf3d41adSArd Biesheuvel 
351cf3d41adSArd Biesheuvel 	union aegis_block tmp;
352cf3d41adSArd Biesheuvel 	unsigned int i;
353cf3d41adSArd Biesheuvel 
354cf3d41adSArd Biesheuvel 	tmp.words64[0] = cpu_to_le64(assocbits);
355cf3d41adSArd Biesheuvel 	tmp.words64[1] = cpu_to_le64(cryptbits);
356cf3d41adSArd Biesheuvel 
357cf3d41adSArd Biesheuvel 	crypto_aegis_block_xor(&tmp, &state->blocks[3]);
358cf3d41adSArd Biesheuvel 
359cf3d41adSArd Biesheuvel 	for (i = 0; i < 7; i++)
360ac50aec4SArd Biesheuvel 		crypto_aegis128_update_a(state, &tmp, false);
361cf3d41adSArd Biesheuvel 
362cf3d41adSArd Biesheuvel 	for (i = 0; i < AEGIS128_STATE_BLOCKS; i++)
363cf3d41adSArd Biesheuvel 		crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
364cf3d41adSArd Biesheuvel }
365cf3d41adSArd Biesheuvel 
crypto_aegis128_setkey(struct crypto_aead * aead,const u8 * key,unsigned int keylen)366cf3d41adSArd Biesheuvel static int crypto_aegis128_setkey(struct crypto_aead *aead, const u8 *key,
367cf3d41adSArd Biesheuvel 				  unsigned int keylen)
368cf3d41adSArd Biesheuvel {
369cf3d41adSArd Biesheuvel 	struct aegis_ctx *ctx = crypto_aead_ctx(aead);
370cf3d41adSArd Biesheuvel 
371674f368aSEric Biggers 	if (keylen != AEGIS128_KEY_SIZE)
372cf3d41adSArd Biesheuvel 		return -EINVAL;
373cf3d41adSArd Biesheuvel 
374cf3d41adSArd Biesheuvel 	memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
375cf3d41adSArd Biesheuvel 	return 0;
376cf3d41adSArd Biesheuvel }
377cf3d41adSArd Biesheuvel 
crypto_aegis128_setauthsize(struct crypto_aead * tfm,unsigned int authsize)378cf3d41adSArd Biesheuvel static int crypto_aegis128_setauthsize(struct crypto_aead *tfm,
379cf3d41adSArd Biesheuvel 				       unsigned int authsize)
380cf3d41adSArd Biesheuvel {
381cf3d41adSArd Biesheuvel 	if (authsize > AEGIS128_MAX_AUTH_SIZE)
382cf3d41adSArd Biesheuvel 		return -EINVAL;
383cf3d41adSArd Biesheuvel 	if (authsize < AEGIS128_MIN_AUTH_SIZE)
384cf3d41adSArd Biesheuvel 		return -EINVAL;
385cf3d41adSArd Biesheuvel 	return 0;
386cf3d41adSArd Biesheuvel }
387cf3d41adSArd Biesheuvel 
crypto_aegis128_encrypt_generic(struct aead_request * req)388ac50aec4SArd Biesheuvel static int crypto_aegis128_encrypt_generic(struct aead_request *req)
389cf3d41adSArd Biesheuvel {
390cf3d41adSArd Biesheuvel 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3912698bce1SArd Biesheuvel 	union aegis_block tag = {};
3922698bce1SArd Biesheuvel 	unsigned int authsize = crypto_aead_authsize(tfm);
393cf3d41adSArd Biesheuvel 	struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
3942698bce1SArd Biesheuvel 	unsigned int cryptlen = req->cryptlen;
3952698bce1SArd Biesheuvel 	struct skcipher_walk walk;
396cf3d41adSArd Biesheuvel 	struct aegis_state state;
397cf3d41adSArd Biesheuvel 
3982698bce1SArd Biesheuvel 	skcipher_walk_aead_encrypt(&walk, req, false);
39952828263SArd Biesheuvel 	crypto_aegis128_init(&state, &ctx->key, req->iv);
400ac50aec4SArd Biesheuvel 	crypto_aegis128_process_ad(&state, req->src, req->assoclen, false);
40102685906SArd Biesheuvel 	crypto_aegis128_process_crypt(&state, &walk,
4022698bce1SArd Biesheuvel 				      crypto_aegis128_encrypt_chunk);
4032698bce1SArd Biesheuvel 	crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
404cf3d41adSArd Biesheuvel 
405cf3d41adSArd Biesheuvel 	scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
406cf3d41adSArd Biesheuvel 				 authsize, 1);
407cf3d41adSArd Biesheuvel 	return 0;
408cf3d41adSArd Biesheuvel }
409cf3d41adSArd Biesheuvel 
crypto_aegis128_decrypt_generic(struct aead_request * req)410ac50aec4SArd Biesheuvel static int crypto_aegis128_decrypt_generic(struct aead_request *req)
411cf3d41adSArd Biesheuvel {
412cf3d41adSArd Biesheuvel 	static const u8 zeros[AEGIS128_MAX_AUTH_SIZE] = {};
413cf3d41adSArd Biesheuvel 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
414cf3d41adSArd Biesheuvel 	union aegis_block tag;
415cf3d41adSArd Biesheuvel 	unsigned int authsize = crypto_aead_authsize(tfm);
416cf3d41adSArd Biesheuvel 	unsigned int cryptlen = req->cryptlen - authsize;
4172698bce1SArd Biesheuvel 	struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
4182698bce1SArd Biesheuvel 	struct skcipher_walk walk;
4192698bce1SArd Biesheuvel 	struct aegis_state state;
420cf3d41adSArd Biesheuvel 
421cf3d41adSArd Biesheuvel 	scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
422cf3d41adSArd Biesheuvel 				 authsize, 0);
423cf3d41adSArd Biesheuvel 
4242698bce1SArd Biesheuvel 	skcipher_walk_aead_decrypt(&walk, req, false);
42552828263SArd Biesheuvel 	crypto_aegis128_init(&state, &ctx->key, req->iv);
426ac50aec4SArd Biesheuvel 	crypto_aegis128_process_ad(&state, req->src, req->assoclen, false);
42702685906SArd Biesheuvel 	crypto_aegis128_process_crypt(&state, &walk,
4282698bce1SArd Biesheuvel 				      crypto_aegis128_decrypt_chunk);
4292698bce1SArd Biesheuvel 	crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
430cf3d41adSArd Biesheuvel 
43102685906SArd Biesheuvel 	if (unlikely(crypto_memneq(tag.bytes, zeros, authsize))) {
43202685906SArd Biesheuvel 		/*
43302685906SArd Biesheuvel 		 * From Chapter 4. 'Security Analysis' of the AEGIS spec [0]
43402685906SArd Biesheuvel 		 *
43502685906SArd Biesheuvel 		 * "3. If verification fails, the decrypted plaintext and the
43602685906SArd Biesheuvel 		 *     wrong authentication tag should not be given as output."
43702685906SArd Biesheuvel 		 *
43802685906SArd Biesheuvel 		 * [0] https://competitions.cr.yp.to/round3/aegisv11.pdf
43902685906SArd Biesheuvel 		 */
44002685906SArd Biesheuvel 		skcipher_walk_aead_decrypt(&walk, req, false);
44102685906SArd Biesheuvel 		crypto_aegis128_process_crypt(NULL, &walk,
44202685906SArd Biesheuvel 					      crypto_aegis128_wipe_chunk);
44302685906SArd Biesheuvel 		memzero_explicit(&tag, sizeof(tag));
44402685906SArd Biesheuvel 		return -EBADMSG;
44502685906SArd Biesheuvel 	}
44602685906SArd Biesheuvel 	return 0;
447cf3d41adSArd Biesheuvel }
448cf3d41adSArd Biesheuvel 
crypto_aegis128_encrypt_simd(struct aead_request * req)449ac50aec4SArd Biesheuvel static int crypto_aegis128_encrypt_simd(struct aead_request *req)
450ac50aec4SArd Biesheuvel {
451ac50aec4SArd Biesheuvel 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
452ac50aec4SArd Biesheuvel 	union aegis_block tag = {};
453ac50aec4SArd Biesheuvel 	unsigned int authsize = crypto_aead_authsize(tfm);
454ac50aec4SArd Biesheuvel 	struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
455ac50aec4SArd Biesheuvel 	unsigned int cryptlen = req->cryptlen;
456ac50aec4SArd Biesheuvel 	struct skcipher_walk walk;
457ac50aec4SArd Biesheuvel 	struct aegis_state state;
458ac50aec4SArd Biesheuvel 
459ac50aec4SArd Biesheuvel 	if (!aegis128_do_simd())
460ac50aec4SArd Biesheuvel 		return crypto_aegis128_encrypt_generic(req);
461ac50aec4SArd Biesheuvel 
462ac50aec4SArd Biesheuvel 	skcipher_walk_aead_encrypt(&walk, req, false);
463ac50aec4SArd Biesheuvel 	crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
464ac50aec4SArd Biesheuvel 	crypto_aegis128_process_ad(&state, req->src, req->assoclen, true);
465ac50aec4SArd Biesheuvel 	crypto_aegis128_process_crypt(&state, &walk,
466ac50aec4SArd Biesheuvel 				      crypto_aegis128_encrypt_chunk_simd);
467ac50aec4SArd Biesheuvel 	crypto_aegis128_final_simd(&state, &tag, req->assoclen, cryptlen, 0);
468ac50aec4SArd Biesheuvel 
469ac50aec4SArd Biesheuvel 	scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
470ac50aec4SArd Biesheuvel 				 authsize, 1);
471ac50aec4SArd Biesheuvel 	return 0;
472ac50aec4SArd Biesheuvel }
473ac50aec4SArd Biesheuvel 
crypto_aegis128_decrypt_simd(struct aead_request * req)474ac50aec4SArd Biesheuvel static int crypto_aegis128_decrypt_simd(struct aead_request *req)
475ac50aec4SArd Biesheuvel {
476ac50aec4SArd Biesheuvel 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
477ac50aec4SArd Biesheuvel 	union aegis_block tag;
478ac50aec4SArd Biesheuvel 	unsigned int authsize = crypto_aead_authsize(tfm);
479ac50aec4SArd Biesheuvel 	unsigned int cryptlen = req->cryptlen - authsize;
480ac50aec4SArd Biesheuvel 	struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
481ac50aec4SArd Biesheuvel 	struct skcipher_walk walk;
482ac50aec4SArd Biesheuvel 	struct aegis_state state;
483ac50aec4SArd Biesheuvel 
484ac50aec4SArd Biesheuvel 	if (!aegis128_do_simd())
485ac50aec4SArd Biesheuvel 		return crypto_aegis128_decrypt_generic(req);
486ac50aec4SArd Biesheuvel 
487ac50aec4SArd Biesheuvel 	scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
488ac50aec4SArd Biesheuvel 				 authsize, 0);
489ac50aec4SArd Biesheuvel 
490ac50aec4SArd Biesheuvel 	skcipher_walk_aead_decrypt(&walk, req, false);
491ac50aec4SArd Biesheuvel 	crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
492ac50aec4SArd Biesheuvel 	crypto_aegis128_process_ad(&state, req->src, req->assoclen, true);
493ac50aec4SArd Biesheuvel 	crypto_aegis128_process_crypt(&state, &walk,
494ac50aec4SArd Biesheuvel 				      crypto_aegis128_decrypt_chunk_simd);
495ac50aec4SArd Biesheuvel 
496ac50aec4SArd Biesheuvel 	if (unlikely(crypto_aegis128_final_simd(&state, &tag, req->assoclen,
497ac50aec4SArd Biesheuvel 						cryptlen, authsize))) {
498ac50aec4SArd Biesheuvel 		skcipher_walk_aead_decrypt(&walk, req, false);
499ac50aec4SArd Biesheuvel 		crypto_aegis128_process_crypt(NULL, &walk,
500ac50aec4SArd Biesheuvel 					      crypto_aegis128_wipe_chunk);
501ac50aec4SArd Biesheuvel 		return -EBADMSG;
502ac50aec4SArd Biesheuvel 	}
503ac50aec4SArd Biesheuvel 	return 0;
504ac50aec4SArd Biesheuvel }
505ac50aec4SArd Biesheuvel 
506ac50aec4SArd Biesheuvel static struct aead_alg crypto_aegis128_alg_generic = {
507cf3d41adSArd Biesheuvel 	.setkey			= crypto_aegis128_setkey,
508cf3d41adSArd Biesheuvel 	.setauthsize		= crypto_aegis128_setauthsize,
509ac50aec4SArd Biesheuvel 	.encrypt		= crypto_aegis128_encrypt_generic,
510ac50aec4SArd Biesheuvel 	.decrypt		= crypto_aegis128_decrypt_generic,
511cf3d41adSArd Biesheuvel 
512cf3d41adSArd Biesheuvel 	.ivsize			= AEGIS128_NONCE_SIZE,
513cf3d41adSArd Biesheuvel 	.maxauthsize		= AEGIS128_MAX_AUTH_SIZE,
514cf3d41adSArd Biesheuvel 	.chunksize		= AEGIS_BLOCK_SIZE,
515cf3d41adSArd Biesheuvel 
516ac50aec4SArd Biesheuvel 	.base.cra_blocksize	= 1,
517ac50aec4SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct aegis_ctx),
518ac50aec4SArd Biesheuvel 	.base.cra_alignmask	= 0,
519ac50aec4SArd Biesheuvel 	.base.cra_priority	= 100,
520ac50aec4SArd Biesheuvel 	.base.cra_name		= "aegis128",
521ac50aec4SArd Biesheuvel 	.base.cra_driver_name	= "aegis128-generic",
522ac50aec4SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
523ac50aec4SArd Biesheuvel };
524cf3d41adSArd Biesheuvel 
525ac50aec4SArd Biesheuvel static struct aead_alg crypto_aegis128_alg_simd = {
526ac50aec4SArd Biesheuvel 	.setkey			= crypto_aegis128_setkey,
527ac50aec4SArd Biesheuvel 	.setauthsize		= crypto_aegis128_setauthsize,
528ac50aec4SArd Biesheuvel 	.encrypt		= crypto_aegis128_encrypt_simd,
529ac50aec4SArd Biesheuvel 	.decrypt		= crypto_aegis128_decrypt_simd,
530cf3d41adSArd Biesheuvel 
531ac50aec4SArd Biesheuvel 	.ivsize			= AEGIS128_NONCE_SIZE,
532ac50aec4SArd Biesheuvel 	.maxauthsize		= AEGIS128_MAX_AUTH_SIZE,
533ac50aec4SArd Biesheuvel 	.chunksize		= AEGIS_BLOCK_SIZE,
534cf3d41adSArd Biesheuvel 
535ac50aec4SArd Biesheuvel 	.base.cra_blocksize	= 1,
536ac50aec4SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct aegis_ctx),
537ac50aec4SArd Biesheuvel 	.base.cra_alignmask	= 0,
538ac50aec4SArd Biesheuvel 	.base.cra_priority	= 200,
539ac50aec4SArd Biesheuvel 	.base.cra_name		= "aegis128",
540ac50aec4SArd Biesheuvel 	.base.cra_driver_name	= "aegis128-simd",
541ac50aec4SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
542cf3d41adSArd Biesheuvel };
543cf3d41adSArd Biesheuvel 
crypto_aegis128_module_init(void)544cf3d41adSArd Biesheuvel static int __init crypto_aegis128_module_init(void)
545cf3d41adSArd Biesheuvel {
546ac50aec4SArd Biesheuvel 	int ret;
547cf3d41adSArd Biesheuvel 
548ac50aec4SArd Biesheuvel 	ret = crypto_register_aead(&crypto_aegis128_alg_generic);
549ac50aec4SArd Biesheuvel 	if (ret)
550ac50aec4SArd Biesheuvel 		return ret;
551ac50aec4SArd Biesheuvel 
552ac50aec4SArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) &&
553ac50aec4SArd Biesheuvel 	    crypto_aegis128_have_simd()) {
554ac50aec4SArd Biesheuvel 		ret = crypto_register_aead(&crypto_aegis128_alg_simd);
555ac50aec4SArd Biesheuvel 		if (ret) {
556ac50aec4SArd Biesheuvel 			crypto_unregister_aead(&crypto_aegis128_alg_generic);
557ac50aec4SArd Biesheuvel 			return ret;
558ac50aec4SArd Biesheuvel 		}
559ac50aec4SArd Biesheuvel 		static_branch_enable(&have_simd);
560ac50aec4SArd Biesheuvel 	}
561ac50aec4SArd Biesheuvel 	return 0;
562cf3d41adSArd Biesheuvel }
563cf3d41adSArd Biesheuvel 
crypto_aegis128_module_exit(void)564cf3d41adSArd Biesheuvel static void __exit crypto_aegis128_module_exit(void)
565cf3d41adSArd Biesheuvel {
566ac50aec4SArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) &&
567ac50aec4SArd Biesheuvel 	    crypto_aegis128_have_simd())
568ac50aec4SArd Biesheuvel 		crypto_unregister_aead(&crypto_aegis128_alg_simd);
569ac50aec4SArd Biesheuvel 
570ac50aec4SArd Biesheuvel 	crypto_unregister_aead(&crypto_aegis128_alg_generic);
571cf3d41adSArd Biesheuvel }
572cf3d41adSArd Biesheuvel 
573cf3d41adSArd Biesheuvel subsys_initcall(crypto_aegis128_module_init);
574cf3d41adSArd Biesheuvel module_exit(crypto_aegis128_module_exit);
575cf3d41adSArd Biesheuvel 
576cf3d41adSArd Biesheuvel MODULE_LICENSE("GPL");
577cf3d41adSArd Biesheuvel MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
578cf3d41adSArd Biesheuvel MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm");
579cf3d41adSArd Biesheuvel MODULE_ALIAS_CRYPTO("aegis128");
580cf3d41adSArd Biesheuvel MODULE_ALIAS_CRYPTO("aegis128-generic");
581ac50aec4SArd Biesheuvel MODULE_ALIAS_CRYPTO("aegis128-simd");
582