Linux Audio

Check our new training course

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