]>
Commit | Line | Data |
---|---|---|
56e8e57f OM |
1 | /* |
2 | * The MORUS-640 Authenticated-Encryption Algorithm | |
2808f173 | 3 | * Common x86 SIMD glue skeleton |
56e8e57f OM |
4 | * |
5 | * Copyright (c) 2016-2018 Ondrej Mosnacek <omosnacek@gmail.com> | |
6 | * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the Free | |
10 | * Software Foundation; either version 2 of the License, or (at your option) | |
11 | * any later version. | |
12 | */ | |
13 | ||
56e8e57f OM |
14 | #include <crypto/internal/aead.h> |
15 | #include <crypto/internal/skcipher.h> | |
16 | #include <crypto/morus640_glue.h> | |
17 | #include <crypto/scatterwalk.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/scatterlist.h> | |
23 | #include <asm/fpu/api.h> | |
24 | ||
25 | struct morus640_state { | |
26 | struct morus640_block s[MORUS_STATE_BLOCKS]; | |
27 | }; | |
28 | ||
29 | struct morus640_ops { | |
30 | int (*skcipher_walk_init)(struct skcipher_walk *walk, | |
31 | struct aead_request *req, bool atomic); | |
32 | ||
33 | void (*crypt_blocks)(void *state, const void *src, void *dst, | |
34 | unsigned int length); | |
35 | void (*crypt_tail)(void *state, const void *src, void *dst, | |
36 | unsigned int length); | |
37 | }; | |
38 | ||
39 | static void crypto_morus640_glue_process_ad( | |
40 | struct morus640_state *state, | |
41 | const struct morus640_glue_ops *ops, | |
42 | struct scatterlist *sg_src, unsigned int assoclen) | |
43 | { | |
44 | struct scatter_walk walk; | |
45 | struct morus640_block buf; | |
46 | unsigned int pos = 0; | |
47 | ||
48 | scatterwalk_start(&walk, sg_src); | |
49 | while (assoclen != 0) { | |
50 | unsigned int size = scatterwalk_clamp(&walk, assoclen); | |
51 | unsigned int left = size; | |
52 | void *mapped = scatterwalk_map(&walk); | |
53 | const u8 *src = (const u8 *)mapped; | |
54 | ||
55 | if (pos + size >= MORUS640_BLOCK_SIZE) { | |
56 | if (pos > 0) { | |
57 | unsigned int fill = MORUS640_BLOCK_SIZE - pos; | |
58 | memcpy(buf.bytes + pos, src, fill); | |
59 | ops->ad(state, buf.bytes, MORUS640_BLOCK_SIZE); | |
60 | pos = 0; | |
61 | left -= fill; | |
62 | src += fill; | |
63 | } | |
64 | ||
65 | ops->ad(state, src, left); | |
66 | src += left & ~(MORUS640_BLOCK_SIZE - 1); | |
67 | left &= MORUS640_BLOCK_SIZE - 1; | |
68 | } | |
69 | ||
70 | memcpy(buf.bytes + pos, src, left); | |
71 | ||
72 | pos += left; | |
73 | assoclen -= size; | |
74 | scatterwalk_unmap(mapped); | |
75 | scatterwalk_advance(&walk, size); | |
76 | scatterwalk_done(&walk, 0, assoclen); | |
77 | } | |
78 | ||
79 | if (pos > 0) { | |
80 | memset(buf.bytes + pos, 0, MORUS640_BLOCK_SIZE - pos); | |
81 | ops->ad(state, buf.bytes, MORUS640_BLOCK_SIZE); | |
82 | } | |
83 | } | |
84 | ||
85 | static void crypto_morus640_glue_process_crypt(struct morus640_state *state, | |
86 | struct morus640_ops ops, | |
2060e284 | 87 | struct skcipher_walk *walk) |
56e8e57f | 88 | { |
2060e284 EB |
89 | while (walk->nbytes >= MORUS640_BLOCK_SIZE) { |
90 | ops.crypt_blocks(state, walk->src.virt.addr, | |
91 | walk->dst.virt.addr, | |
92 | round_down(walk->nbytes, MORUS640_BLOCK_SIZE)); | |
93 | skcipher_walk_done(walk, walk->nbytes % MORUS640_BLOCK_SIZE); | |
94 | } | |
56e8e57f | 95 | |
2060e284 EB |
96 | if (walk->nbytes) { |
97 | ops.crypt_tail(state, walk->src.virt.addr, walk->dst.virt.addr, | |
98 | walk->nbytes); | |
99 | skcipher_walk_done(walk, 0); | |
56e8e57f OM |
100 | } |
101 | } | |
102 | ||
103 | int crypto_morus640_glue_setkey(struct crypto_aead *aead, const u8 *key, | |
104 | unsigned int keylen) | |
105 | { | |
106 | struct morus640_ctx *ctx = crypto_aead_ctx(aead); | |
107 | ||
108 | if (keylen != MORUS640_BLOCK_SIZE) { | |
109 | crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
110 | return -EINVAL; | |
111 | } | |
112 | ||
113 | memcpy(ctx->key.bytes, key, MORUS640_BLOCK_SIZE); | |
114 | return 0; | |
115 | } | |
116 | EXPORT_SYMBOL_GPL(crypto_morus640_glue_setkey); | |
117 | ||
118 | int crypto_morus640_glue_setauthsize(struct crypto_aead *tfm, | |
119 | unsigned int authsize) | |
120 | { | |
121 | return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL; | |
122 | } | |
123 | EXPORT_SYMBOL_GPL(crypto_morus640_glue_setauthsize); | |
124 | ||
125 | static void crypto_morus640_glue_crypt(struct aead_request *req, | |
126 | struct morus640_ops ops, | |
127 | unsigned int cryptlen, | |
128 | struct morus640_block *tag_xor) | |
129 | { | |
130 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); | |
131 | struct morus640_ctx *ctx = crypto_aead_ctx(tfm); | |
132 | struct morus640_state state; | |
2060e284 EB |
133 | struct skcipher_walk walk; |
134 | ||
135 | ops.skcipher_walk_init(&walk, req, true); | |
56e8e57f OM |
136 | |
137 | kernel_fpu_begin(); | |
138 | ||
139 | ctx->ops->init(&state, &ctx->key, req->iv); | |
140 | crypto_morus640_glue_process_ad(&state, ctx->ops, req->src, req->assoclen); | |
2060e284 | 141 | crypto_morus640_glue_process_crypt(&state, ops, &walk); |
56e8e57f OM |
142 | ctx->ops->final(&state, tag_xor, req->assoclen, cryptlen); |
143 | ||
144 | kernel_fpu_end(); | |
145 | } | |
146 | ||
147 | int crypto_morus640_glue_encrypt(struct aead_request *req) | |
148 | { | |
149 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); | |
150 | struct morus640_ctx *ctx = crypto_aead_ctx(tfm); | |
151 | struct morus640_ops OPS = { | |
152 | .skcipher_walk_init = skcipher_walk_aead_encrypt, | |
153 | .crypt_blocks = ctx->ops->enc, | |
154 | .crypt_tail = ctx->ops->enc_tail, | |
155 | }; | |
156 | ||
157 | struct morus640_block tag = {}; | |
158 | unsigned int authsize = crypto_aead_authsize(tfm); | |
159 | unsigned int cryptlen = req->cryptlen; | |
160 | ||
161 | crypto_morus640_glue_crypt(req, OPS, cryptlen, &tag); | |
162 | ||
163 | scatterwalk_map_and_copy(tag.bytes, req->dst, | |
164 | req->assoclen + cryptlen, authsize, 1); | |
165 | return 0; | |
166 | } | |
167 | EXPORT_SYMBOL_GPL(crypto_morus640_glue_encrypt); | |
168 | ||
169 | int crypto_morus640_glue_decrypt(struct aead_request *req) | |
170 | { | |
171 | static const u8 zeros[MORUS640_BLOCK_SIZE] = {}; | |
172 | ||
173 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); | |
174 | struct morus640_ctx *ctx = crypto_aead_ctx(tfm); | |
175 | struct morus640_ops OPS = { | |
176 | .skcipher_walk_init = skcipher_walk_aead_decrypt, | |
177 | .crypt_blocks = ctx->ops->dec, | |
178 | .crypt_tail = ctx->ops->dec_tail, | |
179 | }; | |
180 | ||
181 | struct morus640_block tag; | |
182 | unsigned int authsize = crypto_aead_authsize(tfm); | |
183 | unsigned int cryptlen = req->cryptlen - authsize; | |
184 | ||
185 | scatterwalk_map_and_copy(tag.bytes, req->src, | |
186 | req->assoclen + cryptlen, authsize, 0); | |
187 | ||
188 | crypto_morus640_glue_crypt(req, OPS, cryptlen, &tag); | |
189 | ||
190 | return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0; | |
191 | } | |
192 | EXPORT_SYMBOL_GPL(crypto_morus640_glue_decrypt); | |
193 | ||
194 | void crypto_morus640_glue_init_ops(struct crypto_aead *aead, | |
195 | const struct morus640_glue_ops *ops) | |
196 | { | |
197 | struct morus640_ctx *ctx = crypto_aead_ctx(aead); | |
198 | ctx->ops = ops; | |
199 | } | |
200 | EXPORT_SYMBOL_GPL(crypto_morus640_glue_init_ops); | |
201 | ||
56e8e57f OM |
202 | MODULE_LICENSE("GPL"); |
203 | MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>"); | |
2808f173 | 204 | MODULE_DESCRIPTION("MORUS-640 AEAD mode -- glue for x86 optimizations"); |