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/hash.h>
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
20 * +------------------------
22 * +------------------------
24 * +------------------------
25 * | consts (block size * 2)
26 * +------------------------
29 struct crypto_cipher
*child
;
34 * +------------------------
36 * +------------------------
38 * +------------------------
40 * +------------------------
42 * +------------------------
44 struct cmac_desc_ctx
{
49 static int crypto_cmac_digest_setkey(struct crypto_shash
*parent
,
50 const u8
*inkey
, unsigned int keylen
)
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
);
55 __be64
*consts
= PTR_ALIGN((void *)ctx
->ctx
,
56 (alignmask
| (__alignof__(__be64
) - 1)) + 1);
61 err
= crypto_cipher_setkey(ctx
->child
, inkey
, keylen
);
65 /* encrypt the zero block */
66 memset(consts
, 0, bs
);
67 crypto_cipher_encrypt_one(ctx
->child
, (u8
*)consts
, (u8
*)consts
);
72 _const
[0] = be64_to_cpu(consts
[1]);
73 _const
[1] = be64_to_cpu(consts
[0]);
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
;
81 consts
[i
+ 0] = cpu_to_be64(_const
[1]);
82 consts
[i
+ 1] = cpu_to_be64(_const
[0]);
88 _const
[0] = be64_to_cpu(consts
[0]);
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
;
95 consts
[i
] = cpu_to_be64(_const
[0]);
104 static int crypto_cmac_digest_init(struct shash_desc
*pdesc
)
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
;
117 static int crypto_cmac_digest_update(struct shash_desc
*pdesc
, const u8
*p
,
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
;
129 /* checking the data can fill the block */
130 if ((ctx
->len
+ len
) <= bs
) {
131 memcpy(odds
+ ctx
->len
, p
, len
);
136 /* filling odds with new data and encrypting it */
137 memcpy(odds
+ ctx
->len
, p
, bs
- ctx
->len
);
138 len
-= bs
- ctx
->len
;
141 crypto_xor(prev
, odds
, bs
);
142 crypto_cipher_encrypt_one(tfm
, prev
, prev
);
144 /* clearing the length */
147 /* encrypting the rest of data */
149 crypto_xor(prev
, p
, bs
);
150 crypto_cipher_encrypt_one(tfm
, prev
, prev
);
155 /* keeping the surplus of blocksize */
157 memcpy(odds
, p
, len
);
164 static int crypto_cmac_digest_final(struct shash_desc
*pdesc
, u8
*out
)
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
);
172 u8
*consts
= PTR_ALIGN((void *)tctx
->ctx
,
173 (alignmask
| (__alignof__(__be64
) - 1)) + 1);
174 u8
*odds
= PTR_ALIGN((void *)ctx
->ctx
, alignmask
+ 1);
175 u8
*prev
= odds
+ bs
;
176 unsigned int offset
= 0;
178 if (ctx
->len
!= bs
) {
180 u8
*p
= odds
+ ctx
->len
;
185 rlen
= bs
- ctx
->len
- 1;
192 crypto_xor(prev
, odds
, bs
);
193 crypto_xor(prev
, consts
+ offset
, bs
);
195 crypto_cipher_encrypt_one(tfm
, out
, prev
);
200 static int cmac_init_tfm(struct crypto_tfm
*tfm
)
202 struct crypto_cipher
*cipher
;
203 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
204 struct crypto_cipher_spawn
*spawn
= crypto_instance_ctx(inst
);
205 struct cmac_tfm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
207 cipher
= crypto_spawn_cipher(spawn
);
209 return PTR_ERR(cipher
);
216 static void cmac_exit_tfm(struct crypto_tfm
*tfm
)
218 struct cmac_tfm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
219 crypto_free_cipher(ctx
->child
);
222 static int cmac_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
224 struct shash_instance
*inst
;
225 struct crypto_cipher_spawn
*spawn
;
226 struct crypto_alg
*alg
;
227 unsigned long alignmask
;
231 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SHASH
, &mask
);
235 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
238 spawn
= shash_instance_ctx(inst
);
240 err
= crypto_grab_cipher(spawn
, shash_crypto_instance(inst
),
241 crypto_attr_alg_name(tb
[1]), 0, mask
);
244 alg
= crypto_spawn_cipher_alg(spawn
);
246 switch (alg
->cra_blocksize
) {
255 err
= crypto_inst_setname(shash_crypto_instance(inst
), tmpl
->name
, alg
);
259 alignmask
= alg
->cra_alignmask
;
260 inst
->alg
.base
.cra_alignmask
= alignmask
;
261 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
262 inst
->alg
.base
.cra_blocksize
= alg
->cra_blocksize
;
264 inst
->alg
.digestsize
= alg
->cra_blocksize
;
266 ALIGN(sizeof(struct cmac_desc_ctx
), crypto_tfm_ctx_alignment())
267 + (alignmask
& ~(crypto_tfm_ctx_alignment() - 1))
268 + alg
->cra_blocksize
* 2;
270 inst
->alg
.base
.cra_ctxsize
=
271 ALIGN(sizeof(struct cmac_tfm_ctx
), crypto_tfm_ctx_alignment())
272 + ((alignmask
| (__alignof__(__be64
) - 1)) &
273 ~(crypto_tfm_ctx_alignment() - 1))
274 + alg
->cra_blocksize
* 2;
276 inst
->alg
.base
.cra_init
= cmac_init_tfm
;
277 inst
->alg
.base
.cra_exit
= cmac_exit_tfm
;
279 inst
->alg
.init
= crypto_cmac_digest_init
;
280 inst
->alg
.update
= crypto_cmac_digest_update
;
281 inst
->alg
.final
= crypto_cmac_digest_final
;
282 inst
->alg
.setkey
= crypto_cmac_digest_setkey
;
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");