1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * The MORUS-1280 Authenticated-Encryption Algorithm
4 * Common x86 SIMD glue skeleton
6 * Copyright (c) 2016-2018 Ondrej Mosnacek <omosnacek@gmail.com>
7 * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/skcipher.h>
12 #include <crypto/morus1280_glue.h>
13 #include <crypto/scatterwalk.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/scatterlist.h>
19 #include <asm/fpu/api.h>
21 struct morus1280_state {
22 struct morus1280_block s[MORUS_STATE_BLOCKS];
25 struct morus1280_ops {
26 int (*skcipher_walk_init)(struct skcipher_walk *walk,
27 struct aead_request *req, bool atomic);
29 void (*crypt_blocks)(void *state, const void *src, void *dst,
31 void (*crypt_tail)(void *state, const void *src, void *dst,
35 static void crypto_morus1280_glue_process_ad(
36 struct morus1280_state *state,
37 const struct morus1280_glue_ops *ops,
38 struct scatterlist *sg_src, unsigned int assoclen)
40 struct scatter_walk walk;
41 struct morus1280_block buf;
44 scatterwalk_start(&walk, sg_src);
45 while (assoclen != 0) {
46 unsigned int size = scatterwalk_clamp(&walk, assoclen);
47 unsigned int left = size;
48 void *mapped = scatterwalk_map(&walk);
49 const u8 *src = (const u8 *)mapped;
51 if (pos + size >= MORUS1280_BLOCK_SIZE) {
53 unsigned int fill = MORUS1280_BLOCK_SIZE - pos;
54 memcpy(buf.bytes + pos, src, fill);
55 ops->ad(state, buf.bytes, MORUS1280_BLOCK_SIZE);
61 ops->ad(state, src, left);
62 src += left & ~(MORUS1280_BLOCK_SIZE - 1);
63 left &= MORUS1280_BLOCK_SIZE - 1;
66 memcpy(buf.bytes + pos, src, left);
70 scatterwalk_unmap(mapped);
71 scatterwalk_advance(&walk, size);
72 scatterwalk_done(&walk, 0, assoclen);
76 memset(buf.bytes + pos, 0, MORUS1280_BLOCK_SIZE - pos);
77 ops->ad(state, buf.bytes, MORUS1280_BLOCK_SIZE);
81 static void crypto_morus1280_glue_process_crypt(struct morus1280_state *state,
82 struct morus1280_ops ops,
83 struct skcipher_walk *walk)
85 while (walk->nbytes >= MORUS1280_BLOCK_SIZE) {
86 ops.crypt_blocks(state, walk->src.virt.addr,
88 round_down(walk->nbytes,
89 MORUS1280_BLOCK_SIZE));
90 skcipher_walk_done(walk, walk->nbytes % MORUS1280_BLOCK_SIZE);
94 ops.crypt_tail(state, walk->src.virt.addr, walk->dst.virt.addr,
96 skcipher_walk_done(walk, 0);
100 int crypto_morus1280_glue_setkey(struct crypto_aead *aead, const u8 *key,
103 struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
105 if (keylen == MORUS1280_BLOCK_SIZE) {
106 memcpy(ctx->key.bytes, key, MORUS1280_BLOCK_SIZE);
107 } else if (keylen == MORUS1280_BLOCK_SIZE / 2) {
108 memcpy(ctx->key.bytes, key, keylen);
109 memcpy(ctx->key.bytes + keylen, key, keylen);
111 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
117 EXPORT_SYMBOL_GPL(crypto_morus1280_glue_setkey);
119 int crypto_morus1280_glue_setauthsize(struct crypto_aead *tfm,
120 unsigned int authsize)
122 return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
124 EXPORT_SYMBOL_GPL(crypto_morus1280_glue_setauthsize);
126 static void crypto_morus1280_glue_crypt(struct aead_request *req,
127 struct morus1280_ops ops,
128 unsigned int cryptlen,
129 struct morus1280_block *tag_xor)
131 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
132 struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
133 struct morus1280_state state;
134 struct skcipher_walk walk;
136 ops.skcipher_walk_init(&walk, req, true);
140 ctx->ops->init(&state, &ctx->key, req->iv);
141 crypto_morus1280_glue_process_ad(&state, ctx->ops, req->src, req->assoclen);
142 crypto_morus1280_glue_process_crypt(&state, ops, &walk);
143 ctx->ops->final(&state, tag_xor, req->assoclen, cryptlen);
148 int crypto_morus1280_glue_encrypt(struct aead_request *req)
150 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
151 struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
152 struct morus1280_ops OPS = {
153 .skcipher_walk_init = skcipher_walk_aead_encrypt,
154 .crypt_blocks = ctx->ops->enc,
155 .crypt_tail = ctx->ops->enc_tail,
158 struct morus1280_block tag = {};
159 unsigned int authsize = crypto_aead_authsize(tfm);
160 unsigned int cryptlen = req->cryptlen;
162 crypto_morus1280_glue_crypt(req, OPS, cryptlen, &tag);
164 scatterwalk_map_and_copy(tag.bytes, req->dst,
165 req->assoclen + cryptlen, authsize, 1);
168 EXPORT_SYMBOL_GPL(crypto_morus1280_glue_encrypt);
170 int crypto_morus1280_glue_decrypt(struct aead_request *req)
172 static const u8 zeros[MORUS1280_BLOCK_SIZE] = {};
174 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
175 struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
176 struct morus1280_ops OPS = {
177 .skcipher_walk_init = skcipher_walk_aead_decrypt,
178 .crypt_blocks = ctx->ops->dec,
179 .crypt_tail = ctx->ops->dec_tail,
182 struct morus1280_block tag;
183 unsigned int authsize = crypto_aead_authsize(tfm);
184 unsigned int cryptlen = req->cryptlen - authsize;
186 scatterwalk_map_and_copy(tag.bytes, req->src,
187 req->assoclen + cryptlen, authsize, 0);
189 crypto_morus1280_glue_crypt(req, OPS, cryptlen, &tag);
191 return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
193 EXPORT_SYMBOL_GPL(crypto_morus1280_glue_decrypt);
195 void crypto_morus1280_glue_init_ops(struct crypto_aead *aead,
196 const struct morus1280_glue_ops *ops)
198 struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
201 EXPORT_SYMBOL_GPL(crypto_morus1280_glue_init_ops);
203 MODULE_LICENSE("GPL");
204 MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
205 MODULE_DESCRIPTION("MORUS-1280 AEAD mode -- glue for x86 optimizations");