2 * CMAC: Cipher Block Mode for Authentication
4 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
7 * Copyright © 2013 Tom St Denis <tstdenis@elliptictech.com>
8 * Based on crypto/xcbc.c:
9 * Copyright © 2006 USAGI/WIDE Project,
10 * Author: Kazunori Miyazawa <miyazawa@linux-ipv6.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
19 #include <crypto/internal/hash.h>
20 #include <linux/err.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
25 * +------------------------
27 * +------------------------
29 * +------------------------
30 * | consts (block size * 2)
31 * +------------------------
34 struct crypto_cipher *child;
39 * +------------------------
41 * +------------------------
43 * +------------------------
45 * +------------------------
47 * +------------------------
49 struct cmac_desc_ctx {
54 static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
55 const u8 *inkey, unsigned int keylen)
57 unsigned long alignmask = crypto_shash_alignmask(parent);
58 struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
59 unsigned int bs = crypto_shash_blocksize(parent);
60 __be64 *consts = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
65 err = crypto_cipher_setkey(ctx->child, inkey, keylen);
69 /* encrypt the zero block */
70 memset(consts, 0, bs);
71 crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
76 _const[0] = be64_to_cpu(consts[1]);
77 _const[1] = be64_to_cpu(consts[0]);
79 /* gf(2^128) multiply zero-ciphertext with u and u^2 */
80 for (i = 0; i < 4; i += 2) {
81 msb_mask = ((s64)_const[1] >> 63) & gfmask;
82 _const[1] = (_const[1] << 1) | (_const[0] >> 63);
83 _const[0] = (_const[0] << 1) ^ msb_mask;
85 consts[i + 0] = cpu_to_be64(_const[1]);
86 consts[i + 1] = cpu_to_be64(_const[0]);
92 _const[0] = be64_to_cpu(consts[0]);
94 /* gf(2^64) multiply zero-ciphertext with u and u^2 */
95 for (i = 0; i < 2; i++) {
96 msb_mask = ((s64)_const[0] >> 63) & gfmask;
97 _const[0] = (_const[0] << 1) ^ msb_mask;
99 consts[i] = cpu_to_be64(_const[0]);
108 static int crypto_cmac_digest_init(struct shash_desc *pdesc)
110 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
111 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
112 int bs = crypto_shash_blocksize(pdesc->tfm);
113 u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
121 static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
124 struct crypto_shash *parent = pdesc->tfm;
125 unsigned long alignmask = crypto_shash_alignmask(parent);
126 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
127 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
128 struct crypto_cipher *tfm = tctx->child;
129 int bs = crypto_shash_blocksize(parent);
130 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
131 u8 *prev = odds + bs;
133 /* checking the data can fill the block */
134 if ((ctx->len + len) <= bs) {
135 memcpy(odds + ctx->len, p, len);
140 /* filling odds with new data and encrypting it */
141 memcpy(odds + ctx->len, p, bs - ctx->len);
142 len -= bs - ctx->len;
145 crypto_xor(prev, odds, bs);
146 crypto_cipher_encrypt_one(tfm, prev, prev);
148 /* clearing the length */
151 /* encrypting the rest of data */
153 crypto_xor(prev, p, bs);
154 crypto_cipher_encrypt_one(tfm, prev, prev);
159 /* keeping the surplus of blocksize */
161 memcpy(odds, p, len);
168 static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
170 struct crypto_shash *parent = pdesc->tfm;
171 unsigned long alignmask = crypto_shash_alignmask(parent);
172 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
173 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
174 struct crypto_cipher *tfm = tctx->child;
175 int bs = crypto_shash_blocksize(parent);
176 u8 *consts = PTR_ALIGN((void *)tctx->ctx, alignmask + 1);
177 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
178 u8 *prev = odds + bs;
179 unsigned int offset = 0;
181 if (ctx->len != bs) {
183 u8 *p = odds + ctx->len;
188 rlen = bs - ctx->len - 1;
195 crypto_xor(prev, odds, bs);
196 crypto_xor(prev, consts + offset, bs);
198 crypto_cipher_encrypt_one(tfm, out, prev);
203 static int cmac_init_tfm(struct crypto_tfm *tfm)
205 struct crypto_cipher *cipher;
206 struct crypto_instance *inst = (void *)tfm->__crt_alg;
207 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
208 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
210 cipher = crypto_spawn_cipher(spawn);
212 return PTR_ERR(cipher);
219 static void cmac_exit_tfm(struct crypto_tfm *tfm)
221 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
222 crypto_free_cipher(ctx->child);
225 static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
227 struct shash_instance *inst;
228 struct crypto_alg *alg;
229 unsigned long alignmask;
232 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
236 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
237 CRYPTO_ALG_TYPE_MASK);
241 switch (alg->cra_blocksize) {
249 inst = shash_alloc_instance("cmac", alg);
254 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
255 shash_crypto_instance(inst),
256 CRYPTO_ALG_TYPE_MASK);
260 alignmask = alg->cra_alignmask | (sizeof(long) - 1);
261 inst->alg.base.cra_alignmask = alignmask;
262 inst->alg.base.cra_priority = alg->cra_priority;
263 inst->alg.base.cra_blocksize = alg->cra_blocksize;
265 inst->alg.digestsize = alg->cra_blocksize;
267 ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
268 + (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
269 + alg->cra_blocksize * 2;
271 inst->alg.base.cra_ctxsize =
272 ALIGN(sizeof(struct cmac_tfm_ctx), alignmask + 1)
273 + alg->cra_blocksize * 2;
275 inst->alg.base.cra_init = cmac_init_tfm;
276 inst->alg.base.cra_exit = cmac_exit_tfm;
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;
283 err = shash_register_instance(tmpl, inst);
286 shash_free_instance(shash_crypto_instance(inst));
294 static struct crypto_template crypto_cmac_tmpl = {
296 .create = cmac_create,
297 .free = shash_free_instance,
298 .module = THIS_MODULE,
301 static int __init crypto_cmac_module_init(void)
303 return crypto_register_template(&crypto_cmac_tmpl);
306 static void __exit crypto_cmac_module_exit(void)
308 crypto_unregister_template(&crypto_cmac_tmpl);
311 module_init(crypto_cmac_module_init);
312 module_exit(crypto_cmac_module_exit);
314 MODULE_LICENSE("GPL");
315 MODULE_DESCRIPTION("CMAC keyed hash algorithm");