blob: 0928aebc6205278c7aaffa3bcc62a6edf4ff6add [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
14#include <crypto/internal/hash.h>
15#include <linux/err.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18
19/*
20 * +------------------------
21 * | <parent tfm>
22 * +------------------------
23 * | cmac_tfm_ctx
24 * +------------------------
25 * | consts (block size * 2)
26 * +------------------------
27 */
28struct cmac_tfm_ctx {
29 struct crypto_cipher *child;
30 u8 ctx[];
31};
32
33/*
34 * +------------------------
35 * | <shash desc>
36 * +------------------------
37 * | cmac_desc_ctx
38 * +------------------------
39 * | odds (block size)
40 * +------------------------
41 * | prev (block size)
42 * +------------------------
43 */
44struct cmac_desc_ctx {
45 unsigned int len;
46 u8 ctx[];
47};
48
49static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
50 const u8 *inkey, unsigned int keylen)
51{
52 unsigned long alignmask = crypto_shash_alignmask(parent);
53 struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
54 unsigned int bs = crypto_shash_blocksize(parent);
Eric Biggersf16743e2016-10-10 10:15:15 -070055 __be64 *consts = PTR_ALIGN((void *)ctx->ctx,
56 (alignmask | (__alignof__(__be64) - 1)) + 1);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +030057 u64 _const[2];
58 int i, err = 0;
59 u8 msb_mask, gfmask;
60
61 err = crypto_cipher_setkey(ctx->child, inkey, keylen);
62 if (err)
63 return err;
64
65 /* encrypt the zero block */
66 memset(consts, 0, bs);
67 crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
68
69 switch (bs) {
70 case 16:
71 gfmask = 0x87;
72 _const[0] = be64_to_cpu(consts[1]);
73 _const[1] = be64_to_cpu(consts[0]);
74
75 /* gf(2^128) multiply zero-ciphertext with u and u^2 */
76 for (i = 0; i < 4; i += 2) {
77 msb_mask = ((s64)_const[1] >> 63) & gfmask;
78 _const[1] = (_const[1] << 1) | (_const[0] >> 63);
79 _const[0] = (_const[0] << 1) ^ msb_mask;
80
81 consts[i + 0] = cpu_to_be64(_const[1]);
82 consts[i + 1] = cpu_to_be64(_const[0]);
83 }
84
85 break;
86 case 8:
87 gfmask = 0x1B;
88 _const[0] = be64_to_cpu(consts[0]);
89
90 /* gf(2^64) multiply zero-ciphertext with u and u^2 */
91 for (i = 0; i < 2; i++) {
92 msb_mask = ((s64)_const[0] >> 63) & gfmask;
93 _const[0] = (_const[0] << 1) ^ msb_mask;
94
95 consts[i] = cpu_to_be64(_const[0]);
96 }
97
98 break;
99 }
100
101 return 0;
102}
103
104static int crypto_cmac_digest_init(struct shash_desc *pdesc)
105{
106 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
107 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
108 int bs = crypto_shash_blocksize(pdesc->tfm);
109 u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
110
111 ctx->len = 0;
112 memset(prev, 0, bs);
113
114 return 0;
115}
116
117static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
118 unsigned int len)
119{
120 struct crypto_shash *parent = pdesc->tfm;
121 unsigned long alignmask = crypto_shash_alignmask(parent);
122 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
123 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
124 struct crypto_cipher *tfm = tctx->child;
125 int bs = crypto_shash_blocksize(parent);
126 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
127 u8 *prev = odds + bs;
128
129 /* checking the data can fill the block */
130 if ((ctx->len + len) <= bs) {
131 memcpy(odds + ctx->len, p, len);
132 ctx->len += len;
133 return 0;
134 }
135
136 /* filling odds with new data and encrypting it */
137 memcpy(odds + ctx->len, p, bs - ctx->len);
138 len -= bs - ctx->len;
139 p += bs - ctx->len;
140
141 crypto_xor(prev, odds, bs);
142 crypto_cipher_encrypt_one(tfm, prev, prev);
143
144 /* clearing the length */
145 ctx->len = 0;
146
147 /* encrypting the rest of data */
148 while (len > bs) {
149 crypto_xor(prev, p, bs);
150 crypto_cipher_encrypt_one(tfm, prev, prev);
151 p += bs;
152 len -= bs;
153 }
154
155 /* keeping the surplus of blocksize */
156 if (len) {
157 memcpy(odds, p, len);
158 ctx->len = len;
159 }
160
161 return 0;
162}
163
164static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
165{
166 struct crypto_shash *parent = pdesc->tfm;
167 unsigned long alignmask = crypto_shash_alignmask(parent);
168 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
169 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
170 struct crypto_cipher *tfm = tctx->child;
171 int bs = crypto_shash_blocksize(parent);
Eric Biggersf16743e2016-10-10 10:15:15 -0700172 u8 *consts = PTR_ALIGN((void *)tctx->ctx,
173 (alignmask | (__alignof__(__be64) - 1)) + 1);
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300174 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
175 u8 *prev = odds + bs;
176 unsigned int offset = 0;
177
178 if (ctx->len != bs) {
179 unsigned int rlen;
180 u8 *p = odds + ctx->len;
181
182 *p = 0x80;
183 p++;
184
185 rlen = bs - ctx->len - 1;
186 if (rlen)
187 memset(p, 0, rlen);
188
189 offset += bs;
190 }
191
192 crypto_xor(prev, odds, bs);
193 crypto_xor(prev, consts + offset, bs);
194
195 crypto_cipher_encrypt_one(tfm, out, prev);
196
197 return 0;
198}
199
200static int cmac_init_tfm(struct crypto_tfm *tfm)
201{
202 struct crypto_cipher *cipher;
203 struct crypto_instance *inst = (void *)tfm->__crt_alg;
204 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
205 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
206
207 cipher = crypto_spawn_cipher(spawn);
208 if (IS_ERR(cipher))
209 return PTR_ERR(cipher);
210
211 ctx->child = cipher;
212
213 return 0;
214};
215
216static void cmac_exit_tfm(struct crypto_tfm *tfm)
217{
218 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
219 crypto_free_cipher(ctx->child);
220}
221
222static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
223{
224 struct shash_instance *inst;
225 struct crypto_alg *alg;
226 unsigned long alignmask;
227 int err;
228
229 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
230 if (err)
231 return err;
232
233 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
234 CRYPTO_ALG_TYPE_MASK);
235 if (IS_ERR(alg))
236 return PTR_ERR(alg);
237
238 switch (alg->cra_blocksize) {
239 case 16:
240 case 8:
241 break;
242 default:
Eric Biggers48ee41b2016-10-10 10:15:14 -0700243 err = -EINVAL;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300244 goto out_put_alg;
245 }
246
247 inst = shash_alloc_instance("cmac", alg);
248 err = PTR_ERR(inst);
249 if (IS_ERR(inst))
250 goto out_put_alg;
251
252 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
253 shash_crypto_instance(inst),
254 CRYPTO_ALG_TYPE_MASK);
255 if (err)
256 goto out_free_inst;
257
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000258 alignmask = alg->cra_alignmask;
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300259 inst->alg.base.cra_alignmask = alignmask;
260 inst->alg.base.cra_priority = alg->cra_priority;
261 inst->alg.base.cra_blocksize = alg->cra_blocksize;
262
263 inst->alg.digestsize = alg->cra_blocksize;
264 inst->alg.descsize =
265 ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
266 + (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
267 + alg->cra_blocksize * 2;
268
269 inst->alg.base.cra_ctxsize =
Eric Biggersf16743e2016-10-10 10:15:15 -0700270 ALIGN(sizeof(struct cmac_tfm_ctx), crypto_tfm_ctx_alignment())
271 + ((alignmask | (__alignof__(__be64) - 1)) &
272 ~(crypto_tfm_ctx_alignment() - 1))
Jussi Kivilinna93b5e862013-04-08 10:48:44 +0300273 + alg->cra_blocksize * 2;
274
275 inst->alg.base.cra_init = cmac_init_tfm;
276 inst->alg.base.cra_exit = cmac_exit_tfm;
277
278 inst->alg.init = crypto_cmac_digest_init;
279 inst->alg.update = crypto_cmac_digest_update;
280 inst->alg.final = crypto_cmac_digest_final;
281 inst->alg.setkey = crypto_cmac_digest_setkey;
282
283 err = shash_register_instance(tmpl, inst);
284 if (err) {
285out_free_inst:
286 shash_free_instance(shash_crypto_instance(inst));
287 }
288
289out_put_alg:
290 crypto_mod_put(alg);
291 return err;
292}
293
294static struct crypto_template crypto_cmac_tmpl = {
295 .name = "cmac",
296 .create = cmac_create,
297 .free = shash_free_instance,
298 .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");