1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * CMAC: Cipher Block Mode for Authentication
5 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
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>
14 #include <crypto/internal/cipher.h>
15 #include <crypto/internal/hash.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
21 * +------------------------
23 * +------------------------
25 * +------------------------
26 * | consts (block size * 2)
27 * +------------------------
30 struct crypto_cipher
*child
;
35 * +------------------------
37 * +------------------------
39 * +------------------------
41 * +------------------------
43 * +------------------------
45 struct cmac_desc_ctx
{
50 static int crypto_cmac_digest_setkey(struct crypto_shash
*parent
,
51 const u8
*inkey
, unsigned int keylen
)
53 struct cmac_tfm_ctx
*ctx
= crypto_shash_ctx(parent
);
54 unsigned int bs
= crypto_shash_blocksize(parent
);
55 __be64
*consts
= ctx
->consts
;
60 err
= crypto_cipher_setkey(ctx
->child
, inkey
, keylen
);
64 /* encrypt the zero block */
65 memset(consts
, 0, bs
);
66 crypto_cipher_encrypt_one(ctx
->child
, (u8
*)consts
, (u8
*)consts
);
71 _const
[0] = be64_to_cpu(consts
[1]);
72 _const
[1] = be64_to_cpu(consts
[0]);
74 /* gf(2^128) multiply zero-ciphertext with u and u^2 */
75 for (i
= 0; i
< 4; i
+= 2) {
76 msb_mask
= ((s64
)_const
[1] >> 63) & gfmask
;
77 _const
[1] = (_const
[1] << 1) | (_const
[0] >> 63);
78 _const
[0] = (_const
[0] << 1) ^ msb_mask
;
80 consts
[i
+ 0] = cpu_to_be64(_const
[1]);
81 consts
[i
+ 1] = cpu_to_be64(_const
[0]);
87 _const
[0] = be64_to_cpu(consts
[0]);
89 /* gf(2^64) multiply zero-ciphertext with u and u^2 */
90 for (i
= 0; i
< 2; i
++) {
91 msb_mask
= ((s64
)_const
[0] >> 63) & gfmask
;
92 _const
[0] = (_const
[0] << 1) ^ msb_mask
;
94 consts
[i
] = cpu_to_be64(_const
[0]);
103 static int crypto_cmac_digest_init(struct shash_desc
*pdesc
)
105 struct cmac_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
106 int bs
= crypto_shash_blocksize(pdesc
->tfm
);
107 u8
*prev
= &ctx
->odds
[bs
];
115 static int crypto_cmac_digest_update(struct shash_desc
*pdesc
, const u8
*p
,
118 struct crypto_shash
*parent
= pdesc
->tfm
;
119 struct cmac_tfm_ctx
*tctx
= crypto_shash_ctx(parent
);
120 struct cmac_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
121 struct crypto_cipher
*tfm
= tctx
->child
;
122 int bs
= crypto_shash_blocksize(parent
);
123 u8
*odds
= ctx
->odds
;
124 u8
*prev
= odds
+ bs
;
126 /* checking the data can fill the block */
127 if ((ctx
->len
+ len
) <= bs
) {
128 memcpy(odds
+ ctx
->len
, p
, len
);
133 /* filling odds with new data and encrypting it */
134 memcpy(odds
+ ctx
->len
, p
, bs
- ctx
->len
);
135 len
-= bs
- ctx
->len
;
138 crypto_xor(prev
, odds
, bs
);
139 crypto_cipher_encrypt_one(tfm
, prev
, prev
);
141 /* clearing the length */
144 /* encrypting the rest of data */
146 crypto_xor(prev
, p
, bs
);
147 crypto_cipher_encrypt_one(tfm
, prev
, prev
);
152 /* keeping the surplus of blocksize */
154 memcpy(odds
, p
, len
);
161 static int crypto_cmac_digest_final(struct shash_desc
*pdesc
, u8
*out
)
163 struct crypto_shash
*parent
= pdesc
->tfm
;
164 struct cmac_tfm_ctx
*tctx
= crypto_shash_ctx(parent
);
165 struct cmac_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
166 struct crypto_cipher
*tfm
= tctx
->child
;
167 int bs
= crypto_shash_blocksize(parent
);
168 u8
*odds
= ctx
->odds
;
169 u8
*prev
= odds
+ bs
;
170 unsigned int offset
= 0;
172 if (ctx
->len
!= bs
) {
174 u8
*p
= odds
+ ctx
->len
;
179 rlen
= bs
- ctx
->len
- 1;
186 crypto_xor(prev
, odds
, bs
);
187 crypto_xor(prev
, (const u8
*)tctx
->consts
+ offset
, bs
);
189 crypto_cipher_encrypt_one(tfm
, out
, prev
);
194 static int cmac_init_tfm(struct crypto_shash
*tfm
)
196 struct shash_instance
*inst
= shash_alg_instance(tfm
);
197 struct cmac_tfm_ctx
*ctx
= crypto_shash_ctx(tfm
);
198 struct crypto_cipher_spawn
*spawn
;
199 struct crypto_cipher
*cipher
;
201 spawn
= shash_instance_ctx(inst
);
202 cipher
= crypto_spawn_cipher(spawn
);
204 return PTR_ERR(cipher
);
211 static int cmac_clone_tfm(struct crypto_shash
*tfm
, struct crypto_shash
*otfm
)
213 struct cmac_tfm_ctx
*octx
= crypto_shash_ctx(otfm
);
214 struct cmac_tfm_ctx
*ctx
= crypto_shash_ctx(tfm
);
215 struct crypto_cipher
*cipher
;
217 cipher
= crypto_clone_cipher(octx
->child
);
219 return PTR_ERR(cipher
);
226 static void cmac_exit_tfm(struct crypto_shash
*tfm
)
228 struct cmac_tfm_ctx
*ctx
= crypto_shash_ctx(tfm
);
229 crypto_free_cipher(ctx
->child
);
232 static int cmac_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
234 struct shash_instance
*inst
;
235 struct crypto_cipher_spawn
*spawn
;
236 struct crypto_alg
*alg
;
240 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SHASH
, &mask
);
244 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
247 spawn
= shash_instance_ctx(inst
);
249 err
= crypto_grab_cipher(spawn
, shash_crypto_instance(inst
),
250 crypto_attr_alg_name(tb
[1]), 0, mask
);
253 alg
= crypto_spawn_cipher_alg(spawn
);
255 switch (alg
->cra_blocksize
) {
264 err
= crypto_inst_setname(shash_crypto_instance(inst
), tmpl
->name
, alg
);
268 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
269 inst
->alg
.base
.cra_blocksize
= alg
->cra_blocksize
;
270 inst
->alg
.base
.cra_ctxsize
= sizeof(struct cmac_tfm_ctx
) +
271 alg
->cra_blocksize
* 2;
273 inst
->alg
.digestsize
= alg
->cra_blocksize
;
274 inst
->alg
.descsize
= sizeof(struct cmac_desc_ctx
) +
275 alg
->cra_blocksize
* 2;
276 inst
->alg
.init
= crypto_cmac_digest_init
;
277 inst
->alg
.update
= crypto_cmac_digest_update
;
278 inst
->alg
.final
= crypto_cmac_digest_final
;
279 inst
->alg
.setkey
= crypto_cmac_digest_setkey
;
280 inst
->alg
.init_tfm
= cmac_init_tfm
;
281 inst
->alg
.clone_tfm
= cmac_clone_tfm
;
282 inst
->alg
.exit_tfm
= cmac_exit_tfm
;
284 inst
->free
= shash_free_singlespawn_instance
;
286 err
= shash_register_instance(tmpl
, inst
);
289 shash_free_singlespawn_instance(inst
);
294 static struct crypto_template crypto_cmac_tmpl
= {
296 .create
= cmac_create
,
297 .module
= THIS_MODULE
,
300 static int __init
crypto_cmac_module_init(void)
302 return crypto_register_template(&crypto_cmac_tmpl
);
305 static void __exit
crypto_cmac_module_exit(void)
307 crypto_unregister_template(&crypto_cmac_tmpl
);
310 subsys_initcall(crypto_cmac_module_init
);
311 module_exit(crypto_cmac_module_exit
);
313 MODULE_LICENSE("GPL");
314 MODULE_DESCRIPTION("CMAC keyed hash algorithm");
315 MODULE_ALIAS_CRYPTO("cmac");
316 MODULE_IMPORT_NS(CRYPTO_INTERNAL
);