xref: /linux/arch/x86/crypto/aegis128-aesni-glue.c (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * The AEGIS-128 Authenticated-Encryption Algorithm
4  *   Glue for AES-NI + SSE4.1 implementation
5  *
6  * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
7  * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
8  */
9 
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/skcipher.h>
12 #include <crypto/scatterwalk.h>
13 #include <linux/module.h>
14 #include <asm/fpu/api.h>
15 #include <asm/cpu_device_id.h>
16 
17 #define AEGIS128_BLOCK_ALIGN 16
18 #define AEGIS128_BLOCK_SIZE 16
19 #define AEGIS128_NONCE_SIZE 16
20 #define AEGIS128_STATE_BLOCKS 5
21 #define AEGIS128_KEY_SIZE 16
22 #define AEGIS128_MIN_AUTH_SIZE 8
23 #define AEGIS128_MAX_AUTH_SIZE 16
24 
25 struct aegis_block {
26 	u8 bytes[AEGIS128_BLOCK_SIZE] __aligned(AEGIS128_BLOCK_ALIGN);
27 };
28 
29 struct aegis_state {
30 	struct aegis_block blocks[AEGIS128_STATE_BLOCKS];
31 };
32 
33 struct aegis_ctx {
34 	struct aegis_block key;
35 };
36 
37 asmlinkage void aegis128_aesni_init(struct aegis_state *state,
38 				    const struct aegis_block *key,
39 				    const u8 iv[AEGIS128_NONCE_SIZE]);
40 
41 asmlinkage void aegis128_aesni_ad(struct aegis_state *state, const u8 *data,
42 				  unsigned int len);
43 
44 asmlinkage void aegis128_aesni_enc(struct aegis_state *state, const u8 *src,
45 				   u8 *dst, unsigned int len);
46 
47 asmlinkage void aegis128_aesni_dec(struct aegis_state *state, const u8 *src,
48 				   u8 *dst, unsigned int len);
49 
50 asmlinkage void aegis128_aesni_enc_tail(struct aegis_state *state,
51 					const u8 *src, u8 *dst,
52 					unsigned int len);
53 
54 asmlinkage void aegis128_aesni_dec_tail(struct aegis_state *state,
55 					const u8 *src, u8 *dst,
56 					unsigned int len);
57 
58 asmlinkage void aegis128_aesni_final(struct aegis_state *state,
59 				     struct aegis_block *tag_xor,
60 				     unsigned int assoclen,
61 				     unsigned int cryptlen);
62 
63 static void crypto_aegis128_aesni_process_ad(
64 		struct aegis_state *state, struct scatterlist *sg_src,
65 		unsigned int assoclen)
66 {
67 	struct scatter_walk walk;
68 	struct aegis_block buf;
69 	unsigned int pos = 0;
70 
71 	scatterwalk_start(&walk, sg_src);
72 	while (assoclen != 0) {
73 		unsigned int size = scatterwalk_next(&walk, assoclen);
74 		const u8 *src = walk.addr;
75 		unsigned int left = size;
76 
77 		if (pos + size >= AEGIS128_BLOCK_SIZE) {
78 			if (pos > 0) {
79 				unsigned int fill = AEGIS128_BLOCK_SIZE - pos;
80 				memcpy(buf.bytes + pos, src, fill);
81 				aegis128_aesni_ad(state, buf.bytes,
82 						  AEGIS128_BLOCK_SIZE);
83 				pos = 0;
84 				left -= fill;
85 				src += fill;
86 			}
87 
88 			aegis128_aesni_ad(state, src,
89 					  left & ~(AEGIS128_BLOCK_SIZE - 1));
90 			src += left & ~(AEGIS128_BLOCK_SIZE - 1);
91 			left &= AEGIS128_BLOCK_SIZE - 1;
92 		}
93 
94 		memcpy(buf.bytes + pos, src, left);
95 		pos += left;
96 		assoclen -= size;
97 
98 		scatterwalk_done_src(&walk, size);
99 	}
100 
101 	if (pos > 0) {
102 		memset(buf.bytes + pos, 0, AEGIS128_BLOCK_SIZE - pos);
103 		aegis128_aesni_ad(state, buf.bytes, AEGIS128_BLOCK_SIZE);
104 	}
105 }
106 
107 static __always_inline void
108 crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
109 				    struct skcipher_walk *walk, bool enc)
110 {
111 	while (walk->nbytes >= AEGIS128_BLOCK_SIZE) {
112 		if (enc)
113 			aegis128_aesni_enc(state, walk->src.virt.addr,
114 					   walk->dst.virt.addr,
115 					   round_down(walk->nbytes,
116 						      AEGIS128_BLOCK_SIZE));
117 		else
118 			aegis128_aesni_dec(state, walk->src.virt.addr,
119 					   walk->dst.virt.addr,
120 					   round_down(walk->nbytes,
121 						      AEGIS128_BLOCK_SIZE));
122 		skcipher_walk_done(walk, walk->nbytes % AEGIS128_BLOCK_SIZE);
123 	}
124 
125 	if (walk->nbytes) {
126 		if (enc)
127 			aegis128_aesni_enc_tail(state, walk->src.virt.addr,
128 						walk->dst.virt.addr,
129 						walk->nbytes);
130 		else
131 			aegis128_aesni_dec_tail(state, walk->src.virt.addr,
132 						walk->dst.virt.addr,
133 						walk->nbytes);
134 		skcipher_walk_done(walk, 0);
135 	}
136 }
137 
138 static struct aegis_ctx *crypto_aegis128_aesni_ctx(struct crypto_aead *aead)
139 {
140 	u8 *ctx = crypto_aead_ctx(aead);
141 	ctx = PTR_ALIGN(ctx, __alignof__(struct aegis_ctx));
142 	return (void *)ctx;
143 }
144 
145 static int crypto_aegis128_aesni_setkey(struct crypto_aead *aead, const u8 *key,
146 					unsigned int keylen)
147 {
148 	struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(aead);
149 
150 	if (keylen != AEGIS128_KEY_SIZE)
151 		return -EINVAL;
152 
153 	memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
154 
155 	return 0;
156 }
157 
158 static int crypto_aegis128_aesni_setauthsize(struct crypto_aead *tfm,
159 						unsigned int authsize)
160 {
161 	if (authsize > AEGIS128_MAX_AUTH_SIZE)
162 		return -EINVAL;
163 	if (authsize < AEGIS128_MIN_AUTH_SIZE)
164 		return -EINVAL;
165 	return 0;
166 }
167 
168 static __always_inline void
169 crypto_aegis128_aesni_crypt(struct aead_request *req,
170 			    struct aegis_block *tag_xor,
171 			    unsigned int cryptlen, bool enc)
172 {
173 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
174 	struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(tfm);
175 	struct skcipher_walk walk;
176 	struct aegis_state state;
177 
178 	if (enc)
179 		skcipher_walk_aead_encrypt(&walk, req, true);
180 	else
181 		skcipher_walk_aead_decrypt(&walk, req, true);
182 
183 	kernel_fpu_begin();
184 
185 	aegis128_aesni_init(&state, &ctx->key, req->iv);
186 	crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
187 	crypto_aegis128_aesni_process_crypt(&state, &walk, enc);
188 	aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
189 
190 	kernel_fpu_end();
191 }
192 
193 static int crypto_aegis128_aesni_encrypt(struct aead_request *req)
194 {
195 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
196 	struct aegis_block tag = {};
197 	unsigned int authsize = crypto_aead_authsize(tfm);
198 	unsigned int cryptlen = req->cryptlen;
199 
200 	crypto_aegis128_aesni_crypt(req, &tag, cryptlen, true);
201 
202 	scatterwalk_map_and_copy(tag.bytes, req->dst,
203 				 req->assoclen + cryptlen, authsize, 1);
204 	return 0;
205 }
206 
207 static int crypto_aegis128_aesni_decrypt(struct aead_request *req)
208 {
209 	static const struct aegis_block zeros = {};
210 
211 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
212 	struct aegis_block tag;
213 	unsigned int authsize = crypto_aead_authsize(tfm);
214 	unsigned int cryptlen = req->cryptlen - authsize;
215 
216 	scatterwalk_map_and_copy(tag.bytes, req->src,
217 				 req->assoclen + cryptlen, authsize, 0);
218 
219 	crypto_aegis128_aesni_crypt(req, &tag, cryptlen, false);
220 
221 	return crypto_memneq(tag.bytes, zeros.bytes, authsize) ? -EBADMSG : 0;
222 }
223 
224 static struct aead_alg crypto_aegis128_aesni_alg = {
225 	.setkey = crypto_aegis128_aesni_setkey,
226 	.setauthsize = crypto_aegis128_aesni_setauthsize,
227 	.encrypt = crypto_aegis128_aesni_encrypt,
228 	.decrypt = crypto_aegis128_aesni_decrypt,
229 
230 	.ivsize = AEGIS128_NONCE_SIZE,
231 	.maxauthsize = AEGIS128_MAX_AUTH_SIZE,
232 	.chunksize = AEGIS128_BLOCK_SIZE,
233 
234 	.base = {
235 		.cra_blocksize = 1,
236 		.cra_ctxsize = sizeof(struct aegis_ctx) +
237 			       __alignof__(struct aegis_ctx),
238 		.cra_priority = 400,
239 
240 		.cra_name = "aegis128",
241 		.cra_driver_name = "aegis128-aesni",
242 
243 		.cra_module = THIS_MODULE,
244 	}
245 };
246 
247 static int __init crypto_aegis128_aesni_module_init(void)
248 {
249 	if (!boot_cpu_has(X86_FEATURE_XMM4_1) ||
250 	    !boot_cpu_has(X86_FEATURE_AES) ||
251 	    !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL))
252 		return -ENODEV;
253 
254 	return crypto_register_aead(&crypto_aegis128_aesni_alg);
255 }
256 
257 static void __exit crypto_aegis128_aesni_module_exit(void)
258 {
259 	crypto_unregister_aead(&crypto_aegis128_aesni_alg);
260 }
261 
262 module_init(crypto_aegis128_aesni_module_init);
263 module_exit(crypto_aegis128_aesni_module_exit);
264 
265 MODULE_LICENSE("GPL");
266 MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
267 MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm -- AESNI+SSE4.1 implementation");
268 MODULE_ALIAS_CRYPTO("aegis128");
269 MODULE_ALIAS_CRYPTO("aegis128-aesni");
270