1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * The MORUS-640 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/morus640_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 morus640_state {
22 struct morus640_block s[MORUS_STATE_BLOCKS];
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_morus640_glue_process_ad(
36 struct morus640_state *state,
37 const struct morus640_glue_ops *ops,
38 struct scatterlist *sg_src, unsigned int assoclen)
40 struct scatter_walk walk;
41 struct morus640_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 >= MORUS640_BLOCK_SIZE) {
53 unsigned int fill = MORUS640_BLOCK_SIZE - pos;
54 memcpy(buf.bytes + pos, src, fill);
55 ops->ad(state, buf.bytes, MORUS640_BLOCK_SIZE);
61 ops->ad(state, src, left);
62 src += left & ~(MORUS640_BLOCK_SIZE - 1);
63 left &= MORUS640_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, MORUS640_BLOCK_SIZE - pos);
77 ops->ad(state, buf.bytes, MORUS640_BLOCK_SIZE);
81 static void crypto_morus640_glue_process_crypt(struct morus640_state *state,
82 struct morus640_ops ops,
83 struct skcipher_walk *walk)
85 while (walk->nbytes >= MORUS640_BLOCK_SIZE) {
86 ops.crypt_blocks(state, walk->src.virt.addr,
88 round_down(walk->nbytes, MORUS640_BLOCK_SIZE));
89 skcipher_walk_done(walk, walk->nbytes % MORUS640_BLOCK_SIZE);
93 ops.crypt_tail(state, walk->src.virt.addr, walk->dst.virt.addr,
95 skcipher_walk_done(walk, 0);
99 int crypto_morus640_glue_setkey(struct crypto_aead *aead, const u8 *key,
102 struct morus640_ctx *ctx = crypto_aead_ctx(aead);
104 if (keylen != MORUS640_BLOCK_SIZE) {
105 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
109 memcpy(ctx->key.bytes, key, MORUS640_BLOCK_SIZE);
112 EXPORT_SYMBOL_GPL(crypto_morus640_glue_setkey);
114 int crypto_morus640_glue_setauthsize(struct crypto_aead *tfm,
115 unsigned int authsize)
117 return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
119 EXPORT_SYMBOL_GPL(crypto_morus640_glue_setauthsize);
121 static void crypto_morus640_glue_crypt(struct aead_request *req,
122 struct morus640_ops ops,
123 unsigned int cryptlen,
124 struct morus640_block *tag_xor)
126 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
127 struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
128 struct morus640_state state;
129 struct skcipher_walk walk;
131 ops.skcipher_walk_init(&walk, req, true);
135 ctx->ops->init(&state, &ctx->key, req->iv);
136 crypto_morus640_glue_process_ad(&state, ctx->ops, req->src, req->assoclen);
137 crypto_morus640_glue_process_crypt(&state, ops, &walk);
138 ctx->ops->final(&state, tag_xor, req->assoclen, cryptlen);
143 int crypto_morus640_glue_encrypt(struct aead_request *req)
145 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
146 struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
147 struct morus640_ops OPS = {
148 .skcipher_walk_init = skcipher_walk_aead_encrypt,
149 .crypt_blocks = ctx->ops->enc,
150 .crypt_tail = ctx->ops->enc_tail,
153 struct morus640_block tag = {};
154 unsigned int authsize = crypto_aead_authsize(tfm);
155 unsigned int cryptlen = req->cryptlen;
157 crypto_morus640_glue_crypt(req, OPS, cryptlen, &tag);
159 scatterwalk_map_and_copy(tag.bytes, req->dst,
160 req->assoclen + cryptlen, authsize, 1);
163 EXPORT_SYMBOL_GPL(crypto_morus640_glue_encrypt);
165 int crypto_morus640_glue_decrypt(struct aead_request *req)
167 static const u8 zeros[MORUS640_BLOCK_SIZE] = {};
169 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
170 struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
171 struct morus640_ops OPS = {
172 .skcipher_walk_init = skcipher_walk_aead_decrypt,
173 .crypt_blocks = ctx->ops->dec,
174 .crypt_tail = ctx->ops->dec_tail,
177 struct morus640_block tag;
178 unsigned int authsize = crypto_aead_authsize(tfm);
179 unsigned int cryptlen = req->cryptlen - authsize;
181 scatterwalk_map_and_copy(tag.bytes, req->src,
182 req->assoclen + cryptlen, authsize, 0);
184 crypto_morus640_glue_crypt(req, OPS, cryptlen, &tag);
186 return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
188 EXPORT_SYMBOL_GPL(crypto_morus640_glue_decrypt);
190 void crypto_morus640_glue_init_ops(struct crypto_aead *aead,
191 const struct morus640_glue_ops *ops)
193 struct morus640_ctx *ctx = crypto_aead_ctx(aead);
196 EXPORT_SYMBOL_GPL(crypto_morus640_glue_init_ops);
198 MODULE_LICENSE("GPL");
199 MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
200 MODULE_DESCRIPTION("MORUS-640 AEAD mode -- glue for x86 optimizations");