[go: nahoru, domu]

blob: f4a5d3bfb37624e553b47030d1cee9de4a71e811 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002/*
3 * CMAC: Cipher Block Mode for Authentication
4 *
5 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
6 *
7 * Based on work by:
8 * Copyright © 2013 Tom St Denis <tstdenis@elliptictech.com>
9 * Based on crypto/xcbc.c:
10 * Copyright © 2006 USAGI/WIDE Project,
11 * Author: Kazunori Miyazawa <miyazawa@linux-ipv6.org>
Jussi Kivilinna93b5e862013-04-08 10:48:44 +030012 */
13
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +010014#include <crypto/internal/cipher.h>
Jussi Kivilinna93b5e862013-04-08 10:48:44 +030015#include <crypto/internal/hash.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19
20/*
21 * +------------------------
22 * | <parent tfm>
23 * +------------------------
24 * | cmac_tfm_ctx
25 * +------------------------
26 * | consts (block size * 2)
27 * +------------------------
28 */
29struct cmac_tfm_ctx {
30 struct crypto_cipher *child;
31 u8 ctx[];
32};
33
34/*
35 * +------------------------
36 * | <shash desc>
37 * +------------------------
38 * | cmac_desc_ctx
39 * +------------------------
40 * | odds (block size)
41 * +------------------------
42 * | prev (block size)
43 * +------------------------
44 */
45struct cmac_desc_ctx {
46 unsigned int len;
47 u8 ctx[];
48};
49
50static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
51 const u8 *inkey, unsigned int keylen)
52{
53 unsigned long alignmask = crypto_shash_alignmask(parent);
54 struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
55 unsigned int bs = crypto_shash_blocksize(parent);
Eric Biggersf16743e2016-10-10 10:15:15 -070056 __be64 *consts = PTR_ALIGN((void *)ctx->ctx,
57 (alignmask | (__alignof__(__be64) - 1)) + 1);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +030058 u64 _const[2];
59 int i, err = 0;
60 u8 msb_mask, gfmask;
61
62 err = crypto_cipher_setkey(ctx->child, inkey, keylen);
63 if (err)
64 return err;
65
66 /* encrypt the zero block */
67 memset(consts, 0, bs);
68 crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
69
70 switch (bs) {
71 case 16:
72 gfmask = 0x87;
73 _const[0] = be64_to_cpu(consts[1]);
74 _const[1] = be64_to_cpu(consts[0]);
75
76 /* gf(2^128) multiply zero-ciphertext with u and u^2 */
77 for (i = 0; i < 4; i += 2) {
78 msb_mask = ((s64)_const[1] >> 63) & gfmask;
79 _const[1] = (_const[1] << 1) | (_const[0] >> 63);
80 _const[0] = (_const[0] << 1) ^ msb_mask;
81
82 consts[i + 0] = cpu_to_be64(_const[1]);
83 consts[i + 1] = cpu_to_be64(_const[0]);
84 }
85
86 break;
87 case 8:
88 gfmask = 0x1B;
89 _const[0] = be64_to_cpu(consts[0]);
90
91 /* gf(2^64) multiply zero-ciphertext with u and u^2 */
92 for (i = 0; i < 2; i++) {
93 msb_mask = ((s64)_const[0] >> 63) & gfmask;
94 _const[0] = (_const[0] << 1) ^ msb_mask;
95
96 consts[i] = cpu_to_be64(_const[0]);
97 }
98
99 break;
100 }
101
102 return 0;
103}
104
105static int crypto_cmac_digest_init(struct shash_desc *pdesc)
106{
107 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
108 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
109 int bs = crypto_shash_blocksize(pdesc->tfm);
110 u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
111
112 ctx->len = 0;
113 memset(prev, 0, bs);
114
115 return 0;
116}
117
118static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
119 unsigned int len)
120{
121 struct crypto_shash *parent = pdesc->tfm;
122 unsigned long alignmask = crypto_shash_alignmask(parent);
123 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
124 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
125 struct crypto_cipher *tfm = tctx->child;
126 int bs = crypto_shash_blocksize(parent);
127 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
128 u8 *prev = odds + bs;
129
130 /* checking the data can fill the block */
131 if ((ctx->len + len) <= bs) {
132 memcpy(odds + ctx->len, p, len);
133 ctx->len += len;
134 return 0;
135 }
136
137 /* filling odds with new data and encrypting it */
138 memcpy(odds + ctx->len, p, bs - ctx->len);
139 len -= bs - ctx->len;
140 p += bs - ctx->len;
141
142 crypto_xor(prev, odds, bs);
143 crypto_cipher_encrypt_one(tfm, prev, prev);
144
145 /* clearing the length */
146 ctx->len = 0;
147
148 /* encrypting the rest of data */
149 while (len > bs) {
150 crypto_xor(prev, p, bs);
151 crypto_cipher_encrypt_one(tfm, prev, prev);
152 p += bs;
153 len -= bs;
154 }
155
156 /* keeping the surplus of blocksize */
157 if (len) {
158 memcpy(odds, p, len);
159 ctx->len = len;
160 }
161
162 return 0;
163}
164
165static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
166{
167 struct crypto_shash *parent = pdesc->tfm;
168 unsigned long alignmask = crypto_shash_alignmask(parent);
169 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
170 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
171 struct crypto_cipher *tfm = tctx->child;
172 int bs = crypto_shash_blocksize(parent);
Eric Biggersf16743e2016-10-10 10:15:15 -0700173 u8 *consts = PTR_ALIGN((void *)tctx->ctx,
174 (alignmask | (__alignof__(__be64) - 1)) + 1);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300175 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
176 u8 *prev = odds + bs;
177 unsigned int offset = 0;
178
179 if (ctx->len != bs) {
180 unsigned int rlen;
181 u8 *p = odds + ctx->len;
182
183 *p = 0x80;
184 p++;
185
186 rlen = bs - ctx->len - 1;
187 if (rlen)
188 memset(p, 0, rlen);
189
190 offset += bs;
191 }
192
193 crypto_xor(prev, odds, bs);
194 crypto_xor(prev, consts + offset, bs);
195
196 crypto_cipher_encrypt_one(tfm, out, prev);
197
198 return 0;
199}
200
201static int cmac_init_tfm(struct crypto_tfm *tfm)
202{
203 struct crypto_cipher *cipher;
204 struct crypto_instance *inst = (void *)tfm->__crt_alg;
Eric Biggersd5ed3b62020-01-02 19:59:05 -0800205 struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300206 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
207
208 cipher = crypto_spawn_cipher(spawn);
209 if (IS_ERR(cipher))
210 return PTR_ERR(cipher);
211
212 ctx->child = cipher;
213
214 return 0;
215};
216
217static void cmac_exit_tfm(struct crypto_tfm *tfm)
218{
219 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
220 crypto_free_cipher(ctx->child);
221}
222
223static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
224{
225 struct shash_instance *inst;
Eric Biggers1d0459c2020-01-02 19:59:02 -0800226 struct crypto_cipher_spawn *spawn;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300227 struct crypto_alg *alg;
228 unsigned long alignmask;
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700229 u32 mask;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300230 int err;
231
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700232 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300233 if (err)
234 return err;
235
Eric Biggers1d0459c2020-01-02 19:59:02 -0800236 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
237 if (!inst)
238 return -ENOMEM;
239 spawn = shash_instance_ctx(inst);
240
241 err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700242 crypto_attr_alg_name(tb[1]), 0, mask);
Eric Biggers1d0459c2020-01-02 19:59:02 -0800243 if (err)
244 goto err_free_inst;
245 alg = crypto_spawn_cipher_alg(spawn);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300246
247 switch (alg->cra_blocksize) {
248 case 16:
249 case 8:
250 break;
251 default:
Eric Biggers48ee41bf52016-10-10 10:15:14 -0700252 err = -EINVAL;
Eric Biggers1d0459c2020-01-02 19:59:02 -0800253 goto err_free_inst;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300254 }
255
Eric Biggers1d0459c2020-01-02 19:59:02 -0800256 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300257 if (err)
Eric Biggers1d0459c2020-01-02 19:59:02 -0800258 goto err_free_inst;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300259
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000260 alignmask = alg->cra_alignmask;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300261 inst->alg.base.cra_alignmask = alignmask;
262 inst->alg.base.cra_priority = alg->cra_priority;
263 inst->alg.base.cra_blocksize = alg->cra_blocksize;
264
265 inst->alg.digestsize = alg->cra_blocksize;
266 inst->alg.descsize =
267 ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
268 + (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
269 + alg->cra_blocksize * 2;
270
271 inst->alg.base.cra_ctxsize =
Eric Biggersf16743e2016-10-10 10:15:15 -0700272 ALIGN(sizeof(struct cmac_tfm_ctx), crypto_tfm_ctx_alignment())
273 + ((alignmask | (__alignof__(__be64) - 1)) &
274 ~(crypto_tfm_ctx_alignment() - 1))
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300275 + alg->cra_blocksize * 2;
276
277 inst->alg.base.cra_init = cmac_init_tfm;
278 inst->alg.base.cra_exit = cmac_exit_tfm;
279
280 inst->alg.init = crypto_cmac_digest_init;
281 inst->alg.update = crypto_cmac_digest_update;
282 inst->alg.final = crypto_cmac_digest_final;
283 inst->alg.setkey = crypto_cmac_digest_setkey;
284
Eric Biggersa39c66c2020-01-02 20:04:38 -0800285 inst->free = shash_free_singlespawn_instance;
286
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300287 err = shash_register_instance(tmpl, inst);
288 if (err) {
Eric Biggers1d0459c2020-01-02 19:59:02 -0800289err_free_inst:
Eric Biggersa39c66c2020-01-02 20:04:38 -0800290 shash_free_singlespawn_instance(inst);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300291 }
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300292 return err;
293}
294
295static struct crypto_template crypto_cmac_tmpl = {
296 .name = "cmac",
297 .create = cmac_create,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300298 .module = THIS_MODULE,
299};
300
301static int __init crypto_cmac_module_init(void)
302{
303 return crypto_register_template(&crypto_cmac_tmpl);
304}
305
306static void __exit crypto_cmac_module_exit(void)
307{
308 crypto_unregister_template(&crypto_cmac_tmpl);
309}
310
Eric Biggersc4741b22019-04-11 21:57:42 -0700311subsys_initcall(crypto_cmac_module_init);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300312module_exit(crypto_cmac_module_exit);
313
314MODULE_LICENSE("GPL");
315MODULE_DESCRIPTION("CMAC keyed hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800316MODULE_ALIAS_CRYPTO("cmac");
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +0100317MODULE_IMPORT_NS(CRYPTO_INTERNAL);