blob: 36f0acec32e196e0d086631851b36c192963924b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joy Latten4a49b492007-12-12 20:25:13 +08002/*
3 * CCM: Counter with CBC-MAC
4 *
5 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
Joy Latten4a49b492007-12-12 20:25:13 +08006 */
7
8#include <crypto/internal/aead.h>
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +01009#include <crypto/internal/cipher.h>
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000010#include <crypto/internal/hash.h>
Joy Latten4a49b492007-12-12 20:25:13 +080011#include <crypto/internal/skcipher.h>
12#include <crypto/scatterwalk.h>
13#include <linux/err.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18
Joy Latten4a49b492007-12-12 20:25:13 +080019struct ccm_instance_ctx {
20 struct crypto_skcipher_spawn ctr;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000021 struct crypto_ahash_spawn mac;
Joy Latten4a49b492007-12-12 20:25:13 +080022};
23
24struct crypto_ccm_ctx {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000025 struct crypto_ahash *mac;
Herbert Xu464b93a2016-07-12 13:17:38 +080026 struct crypto_skcipher *ctr;
Joy Latten4a49b492007-12-12 20:25:13 +080027};
28
29struct crypto_rfc4309_ctx {
30 struct crypto_aead *child;
31 u8 nonce[3];
32};
33
Herbert Xu81c4c352015-07-14 16:53:18 +080034struct crypto_rfc4309_req_ctx {
35 struct scatterlist src[3];
36 struct scatterlist dst[3];
37 struct aead_request subreq;
38};
39
Joy Latten4a49b492007-12-12 20:25:13 +080040struct crypto_ccm_req_priv_ctx {
41 u8 odata[16];
Ard Biesheuvel3b304602017-02-27 15:30:56 +000042 u8 idata[16];
Joy Latten4a49b492007-12-12 20:25:13 +080043 u8 auth_tag[16];
Joy Latten4a49b492007-12-12 20:25:13 +080044 u32 flags;
Herbert Xu81c4c352015-07-14 16:53:18 +080045 struct scatterlist src[3];
46 struct scatterlist dst[3];
Ard Biesheuvelebf533a2018-08-07 14:18:37 -070047 union {
48 struct ahash_request ahreq;
49 struct skcipher_request skreq;
50 };
Joy Latten4a49b492007-12-12 20:25:13 +080051};
52
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000053struct cbcmac_tfm_ctx {
54 struct crypto_cipher *child;
55};
56
57struct cbcmac_desc_ctx {
58 unsigned int len;
Eric Biggers21415bf2023-10-18 22:53:32 -070059 u8 dg[];
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000060};
61
Joy Latten4a49b492007-12-12 20:25:13 +080062static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
63 struct aead_request *req)
64{
65 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
66
67 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
68}
69
70static int set_msg_len(u8 *block, unsigned int msglen, int csize)
71{
72 __be32 data;
73
74 memset(block, 0, csize);
75 block += csize;
76
77 if (csize >= 4)
78 csize = 4;
79 else if (msglen > (1 << (8 * csize)))
80 return -EOVERFLOW;
81
82 data = cpu_to_be32(msglen);
83 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
84
85 return 0;
86}
87
88static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
89 unsigned int keylen)
90{
91 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu464b93a2016-07-12 13:17:38 +080092 struct crypto_skcipher *ctr = ctx->ctr;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000093 struct crypto_ahash *mac = ctx->mac;
Eric Biggersaf5034e2019-12-30 21:19:38 -060094 int err;
Joy Latten4a49b492007-12-12 20:25:13 +080095
Herbert Xu464b93a2016-07-12 13:17:38 +080096 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
97 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
98 CRYPTO_TFM_REQ_MASK);
99 err = crypto_skcipher_setkey(ctr, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +0800100 if (err)
Eric Biggersaf5034e2019-12-30 21:19:38 -0600101 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800102
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000103 crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK);
104 crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) &
Joy Latten4a49b492007-12-12 20:25:13 +0800105 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600106 return crypto_ahash_setkey(mac, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +0800107}
108
109static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
110 unsigned int authsize)
111{
112 switch (authsize) {
113 case 4:
114 case 6:
115 case 8:
116 case 10:
117 case 12:
118 case 14:
119 case 16:
120 break;
121 default:
122 return -EINVAL;
123 }
124
125 return 0;
126}
127
128static int format_input(u8 *info, struct aead_request *req,
129 unsigned int cryptlen)
130{
131 struct crypto_aead *aead = crypto_aead_reqtfm(req);
132 unsigned int lp = req->iv[0];
133 unsigned int l = lp + 1;
134 unsigned int m;
135
136 m = crypto_aead_authsize(aead);
137
138 memcpy(info, req->iv, 16);
139
140 /* format control info per RFC 3610 and
141 * NIST Special Publication 800-38C
142 */
143 *info |= (8 * ((m - 2) / 2));
144 if (req->assoclen)
145 *info |= 64;
146
147 return set_msg_len(info + 16 - l, cryptlen, l);
148}
149
150static int format_adata(u8 *adata, unsigned int a)
151{
152 int len = 0;
153
154 /* add control info for associated data
155 * RFC 3610 and NIST Special Publication 800-38C
156 */
157 if (a < 65280) {
158 *(__be16 *)adata = cpu_to_be16(a);
159 len = 2;
160 } else {
161 *(__be16 *)adata = cpu_to_be16(0xfffe);
162 *(__be32 *)&adata[2] = cpu_to_be32(a);
163 len = 6;
164 }
165
166 return len;
167}
168
Joy Latten4a49b492007-12-12 20:25:13 +0800169static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
170 unsigned int cryptlen)
171{
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000172 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Joy Latten4a49b492007-12-12 20:25:13 +0800173 struct crypto_aead *aead = crypto_aead_reqtfm(req);
174 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
Ard Biesheuvelebf533a2018-08-07 14:18:37 -0700175 struct ahash_request *ahreq = &pctx->ahreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800176 unsigned int assoclen = req->assoclen;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000177 struct scatterlist sg[3];
Ard Biesheuvel3b304602017-02-27 15:30:56 +0000178 u8 *odata = pctx->odata;
179 u8 *idata = pctx->idata;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000180 int ilen, err;
Joy Latten4a49b492007-12-12 20:25:13 +0800181
182 /* format control data for input */
183 err = format_input(odata, req, cryptlen);
184 if (err)
185 goto out;
186
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000187 sg_init_table(sg, 3);
188 sg_set_buf(&sg[0], odata, 16);
Joy Latten4a49b492007-12-12 20:25:13 +0800189
190 /* format associated data and compute into mac */
191 if (assoclen) {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000192 ilen = format_adata(idata, assoclen);
193 sg_set_buf(&sg[1], idata, ilen);
194 sg_chain(sg, 3, req->src);
Jarod Wilson516280e2009-01-22 19:58:15 +1100195 } else {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000196 ilen = 0;
197 sg_chain(sg, 2, req->src);
Joy Latten4a49b492007-12-12 20:25:13 +0800198 }
199
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000200 ahash_request_set_tfm(ahreq, ctx->mac);
201 ahash_request_set_callback(ahreq, pctx->flags, NULL, NULL);
202 ahash_request_set_crypt(ahreq, sg, NULL, assoclen + ilen + 16);
203 err = crypto_ahash_init(ahreq);
204 if (err)
205 goto out;
206 err = crypto_ahash_update(ahreq);
207 if (err)
208 goto out;
Joy Latten4a49b492007-12-12 20:25:13 +0800209
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000210 /* we need to pad the MAC input to a round multiple of the block size */
211 ilen = 16 - (assoclen + ilen) % 16;
212 if (ilen < 16) {
213 memset(idata, 0, ilen);
214 sg_init_table(sg, 2);
215 sg_set_buf(&sg[0], idata, ilen);
216 if (plain)
217 sg_chain(sg, 2, plain);
218 plain = sg;
219 cryptlen += ilen;
220 }
221
Tianjia Zhangad981642022-09-26 17:14:21 +0800222 ahash_request_set_crypt(ahreq, plain, odata, cryptlen);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000223 err = crypto_ahash_finup(ahreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800224out:
225 return err;
226}
227
Herbert Xu255e48e2023-02-08 13:58:44 +0800228static void crypto_ccm_encrypt_done(void *data, int err)
Joy Latten4a49b492007-12-12 20:25:13 +0800229{
Herbert Xu255e48e2023-02-08 13:58:44 +0800230 struct aead_request *req = data;
Joy Latten4a49b492007-12-12 20:25:13 +0800231 struct crypto_aead *aead = crypto_aead_reqtfm(req);
232 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
233 u8 *odata = pctx->odata;
234
235 if (!err)
Herbert Xu81c4c352015-07-14 16:53:18 +0800236 scatterwalk_map_and_copy(odata, req->dst,
237 req->assoclen + req->cryptlen,
Joy Latten4a49b492007-12-12 20:25:13 +0800238 crypto_aead_authsize(aead), 1);
239 aead_request_complete(req, err);
240}
241
242static inline int crypto_ccm_check_iv(const u8 *iv)
243{
244 /* 2 <= L <= 8, so 1 <= L' <= 7. */
245 if (1 > iv[0] || iv[0] > 7)
246 return -EINVAL;
247
248 return 0;
249}
250
Herbert Xu81c4c352015-07-14 16:53:18 +0800251static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag)
252{
253 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
254 struct scatterlist *sg;
255 u8 *iv = req->iv;
256 int err;
257
258 err = crypto_ccm_check_iv(iv);
259 if (err)
260 return err;
261
262 pctx->flags = aead_request_flags(req);
263
264 /* Note: rfc 3610 and NIST 800-38C require counter of
265 * zero to encrypt auth tag.
266 */
267 memset(iv + 15 - iv[0], 0, iv[0] + 1);
268
269 sg_init_table(pctx->src, 3);
270 sg_set_buf(pctx->src, tag, 16);
271 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
272 if (sg != pctx->src + 1)
273 sg_chain(pctx->src, 2, sg);
274
275 if (req->src != req->dst) {
276 sg_init_table(pctx->dst, 3);
277 sg_set_buf(pctx->dst, tag, 16);
278 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
279 if (sg != pctx->dst + 1)
280 sg_chain(pctx->dst, 2, sg);
281 }
282
283 return 0;
284}
285
Joy Latten4a49b492007-12-12 20:25:13 +0800286static int crypto_ccm_encrypt(struct aead_request *req)
287{
288 struct crypto_aead *aead = crypto_aead_reqtfm(req);
289 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
290 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Herbert Xu464b93a2016-07-12 13:17:38 +0800291 struct skcipher_request *skreq = &pctx->skreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800292 struct scatterlist *dst;
293 unsigned int cryptlen = req->cryptlen;
294 u8 *odata = pctx->odata;
295 u8 *iv = req->iv;
296 int err;
297
Herbert Xu81c4c352015-07-14 16:53:18 +0800298 err = crypto_ccm_init_crypt(req, odata);
Joy Latten4a49b492007-12-12 20:25:13 +0800299 if (err)
300 return err;
301
Herbert Xu81c4c352015-07-14 16:53:18 +0800302 err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen);
Joy Latten4a49b492007-12-12 20:25:13 +0800303 if (err)
304 return err;
305
Joy Latten4a49b492007-12-12 20:25:13 +0800306 dst = pctx->src;
Herbert Xu81c4c352015-07-14 16:53:18 +0800307 if (req->src != req->dst)
Joy Latten4a49b492007-12-12 20:25:13 +0800308 dst = pctx->dst;
Joy Latten4a49b492007-12-12 20:25:13 +0800309
Herbert Xu464b93a2016-07-12 13:17:38 +0800310 skcipher_request_set_tfm(skreq, ctx->ctr);
311 skcipher_request_set_callback(skreq, pctx->flags,
312 crypto_ccm_encrypt_done, req);
313 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
314 err = crypto_skcipher_encrypt(skreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800315 if (err)
316 return err;
317
318 /* copy authtag to end of dst */
Herbert Xu81c4c352015-07-14 16:53:18 +0800319 scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen,
Joy Latten4a49b492007-12-12 20:25:13 +0800320 crypto_aead_authsize(aead), 1);
321 return err;
322}
323
Herbert Xu255e48e2023-02-08 13:58:44 +0800324static void crypto_ccm_decrypt_done(void *data, int err)
Joy Latten4a49b492007-12-12 20:25:13 +0800325{
Herbert Xu255e48e2023-02-08 13:58:44 +0800326 struct aead_request *req = data;
Joy Latten4a49b492007-12-12 20:25:13 +0800327 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
328 struct crypto_aead *aead = crypto_aead_reqtfm(req);
329 unsigned int authsize = crypto_aead_authsize(aead);
330 unsigned int cryptlen = req->cryptlen - authsize;
Herbert Xu81c4c352015-07-14 16:53:18 +0800331 struct scatterlist *dst;
332
333 pctx->flags = 0;
334
335 dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
Joy Latten4a49b492007-12-12 20:25:13 +0800336
337 if (!err) {
Herbert Xu81c4c352015-07-14 16:53:18 +0800338 err = crypto_ccm_auth(req, dst, cryptlen);
James Yonan6bf37e52013-09-26 02:20:39 -0600339 if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
Joy Latten4a49b492007-12-12 20:25:13 +0800340 err = -EBADMSG;
341 }
342 aead_request_complete(req, err);
343}
344
345static int crypto_ccm_decrypt(struct aead_request *req)
346{
347 struct crypto_aead *aead = crypto_aead_reqtfm(req);
348 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
349 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Herbert Xu464b93a2016-07-12 13:17:38 +0800350 struct skcipher_request *skreq = &pctx->skreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800351 struct scatterlist *dst;
352 unsigned int authsize = crypto_aead_authsize(aead);
353 unsigned int cryptlen = req->cryptlen;
354 u8 *authtag = pctx->auth_tag;
355 u8 *odata = pctx->odata;
Romain Izard441f99c2017-10-31 15:42:35 +0100356 u8 *iv = pctx->idata;
Joy Latten4a49b492007-12-12 20:25:13 +0800357 int err;
358
Joy Latten4a49b492007-12-12 20:25:13 +0800359 cryptlen -= authsize;
360
Herbert Xu81c4c352015-07-14 16:53:18 +0800361 err = crypto_ccm_init_crypt(req, authtag);
Joy Latten4a49b492007-12-12 20:25:13 +0800362 if (err)
363 return err;
364
Herbert Xu81c4c352015-07-14 16:53:18 +0800365 scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
366 authsize, 0);
Joy Latten4a49b492007-12-12 20:25:13 +0800367
368 dst = pctx->src;
Herbert Xu81c4c352015-07-14 16:53:18 +0800369 if (req->src != req->dst)
Joy Latten4a49b492007-12-12 20:25:13 +0800370 dst = pctx->dst;
Joy Latten4a49b492007-12-12 20:25:13 +0800371
Romain Izard441f99c2017-10-31 15:42:35 +0100372 memcpy(iv, req->iv, 16);
373
Herbert Xu464b93a2016-07-12 13:17:38 +0800374 skcipher_request_set_tfm(skreq, ctx->ctr);
375 skcipher_request_set_callback(skreq, pctx->flags,
376 crypto_ccm_decrypt_done, req);
377 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
378 err = crypto_skcipher_decrypt(skreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800379 if (err)
380 return err;
381
Herbert Xu81c4c352015-07-14 16:53:18 +0800382 err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
Joy Latten4a49b492007-12-12 20:25:13 +0800383 if (err)
384 return err;
385
386 /* verify */
James Yonan6bf37e52013-09-26 02:20:39 -0600387 if (crypto_memneq(authtag, odata, authsize))
Joy Latten4a49b492007-12-12 20:25:13 +0800388 return -EBADMSG;
389
390 return err;
391}
392
Herbert Xu81c4c352015-07-14 16:53:18 +0800393static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800394{
Herbert Xu81c4c352015-07-14 16:53:18 +0800395 struct aead_instance *inst = aead_alg_instance(tfm);
396 struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
397 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000398 struct crypto_ahash *mac;
Herbert Xu464b93a2016-07-12 13:17:38 +0800399 struct crypto_skcipher *ctr;
Joy Latten4a49b492007-12-12 20:25:13 +0800400 unsigned long align;
401 int err;
402
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000403 mac = crypto_spawn_ahash(&ictx->mac);
404 if (IS_ERR(mac))
405 return PTR_ERR(mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800406
Eric Biggers60425a82016-10-28 09:52:19 -0700407 ctr = crypto_spawn_skcipher(&ictx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800408 err = PTR_ERR(ctr);
409 if (IS_ERR(ctr))
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000410 goto err_free_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800411
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000412 ctx->mac = mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800413 ctx->ctr = ctr;
414
Herbert Xu81c4c352015-07-14 16:53:18 +0800415 align = crypto_aead_alignmask(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800416 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu81c4c352015-07-14 16:53:18 +0800417 crypto_aead_set_reqsize(
418 tfm,
Herbert Xu2c221ad2015-05-11 17:47:56 +0800419 align + sizeof(struct crypto_ccm_req_priv_ctx) +
Ard Biesheuvelebf533a2018-08-07 14:18:37 -0700420 max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr)));
Joy Latten4a49b492007-12-12 20:25:13 +0800421
422 return 0;
423
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000424err_free_mac:
425 crypto_free_ahash(mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800426 return err;
427}
428
Herbert Xu81c4c352015-07-14 16:53:18 +0800429static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800430{
Herbert Xu81c4c352015-07-14 16:53:18 +0800431 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800432
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000433 crypto_free_ahash(ctx->mac);
Herbert Xu464b93a2016-07-12 13:17:38 +0800434 crypto_free_skcipher(ctx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800435}
436
Herbert Xu81c4c352015-07-14 16:53:18 +0800437static void crypto_ccm_free(struct aead_instance *inst)
438{
439 struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
440
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000441 crypto_drop_ahash(&ctx->mac);
Herbert Xu81c4c352015-07-14 16:53:18 +0800442 crypto_drop_skcipher(&ctx->ctr);
443 kfree(inst);
444}
445
446static int crypto_ccm_create_common(struct crypto_template *tmpl,
447 struct rtattr **tb,
Herbert Xu81c4c352015-07-14 16:53:18 +0800448 const char *ctr_name,
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000449 const char *mac_name)
Joy Latten4a49b492007-12-12 20:25:13 +0800450{
Herbert Xu60fa9a32023-10-03 11:43:25 +0800451 struct skcipher_alg_common *ctr;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800452 u32 mask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800453 struct aead_instance *inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800454 struct ccm_instance_ctx *ictx;
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800455 struct hash_alg_common *mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800456 int err;
457
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700458 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
459 if (err)
460 return err;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800461
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800462 inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
463 if (!inst)
464 return -ENOMEM;
465 ictx = aead_instance_ctx(inst);
Joy Latten4a49b492007-12-12 20:25:13 +0800466
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800467 err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst),
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700468 mac_name, 0, mask | CRYPTO_ALG_ASYNC);
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800469 if (err)
470 goto err_free_inst;
471 mac = crypto_spawn_ahash_alg(&ictx->mac);
472
Joy Latten4a49b492007-12-12 20:25:13 +0800473 err = -EINVAL;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700474 if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
475 mac->digestsize != 16)
Joy Latten4a49b492007-12-12 20:25:13 +0800476 goto err_free_inst;
477
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800478 err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst),
479 ctr_name, 0, mask);
Joy Latten4a49b492007-12-12 20:25:13 +0800480 if (err)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800481 goto err_free_inst;
Herbert Xu60fa9a32023-10-03 11:43:25 +0800482 ctr = crypto_spawn_skcipher_alg_common(&ictx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800483
Eric Biggers6a1faa42019-04-18 14:44:27 -0700484 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Joy Latten4a49b492007-12-12 20:25:13 +0800485 err = -EINVAL;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700486 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
Herbert Xu60fa9a32023-10-03 11:43:25 +0800487 ctr->ivsize != 16 || ctr->base.cra_blocksize != 1)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800488 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800489
Eric Biggers6a1faa42019-04-18 14:44:27 -0700490 /* ctr and cbcmac must use the same underlying block cipher. */
491 if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800492 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800493
494 err = -ENAMETOOLONG;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700495 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
496 "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800497 goto err_free_inst;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700498
Herbert Xu81c4c352015-07-14 16:53:18 +0800499 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu464b93a2016-07-12 13:17:38 +0800500 "ccm_base(%s,%s)", ctr->base.cra_driver_name,
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000501 mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800502 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800503
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000504 inst->alg.base.cra_priority = (mac->base.cra_priority +
Herbert Xu464b93a2016-07-12 13:17:38 +0800505 ctr->base.cra_priority) / 2;
Herbert Xu81c4c352015-07-14 16:53:18 +0800506 inst->alg.base.cra_blocksize = 1;
Eric Biggers36cfc052023-10-22 01:10:50 -0700507 inst->alg.base.cra_alignmask = ctr->base.cra_alignmask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800508 inst->alg.ivsize = 16;
Herbert Xu60fa9a32023-10-03 11:43:25 +0800509 inst->alg.chunksize = ctr->chunksize;
Herbert Xu81c4c352015-07-14 16:53:18 +0800510 inst->alg.maxauthsize = 16;
511 inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
512 inst->alg.init = crypto_ccm_init_tfm;
513 inst->alg.exit = crypto_ccm_exit_tfm;
514 inst->alg.setkey = crypto_ccm_setkey;
515 inst->alg.setauthsize = crypto_ccm_setauthsize;
516 inst->alg.encrypt = crypto_ccm_encrypt;
517 inst->alg.decrypt = crypto_ccm_decrypt;
Joy Latten4a49b492007-12-12 20:25:13 +0800518
Herbert Xu81c4c352015-07-14 16:53:18 +0800519 inst->free = crypto_ccm_free;
520
521 err = aead_register_instance(tmpl, inst);
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800522 if (err) {
Joy Latten4a49b492007-12-12 20:25:13 +0800523err_free_inst:
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800524 crypto_ccm_free(inst);
525 }
526 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800527}
528
Herbert Xu81c4c352015-07-14 16:53:18 +0800529static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800530{
Joy Latten4a49b492007-12-12 20:25:13 +0800531 const char *cipher_name;
532 char ctr_name[CRYPTO_MAX_ALG_NAME];
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000533 char mac_name[CRYPTO_MAX_ALG_NAME];
Joy Latten4a49b492007-12-12 20:25:13 +0800534
535 cipher_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800536 if (IS_ERR(cipher_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800537 return PTR_ERR(cipher_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800538
539 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
540 cipher_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu81c4c352015-07-14 16:53:18 +0800541 return -ENAMETOOLONG;
Joy Latten4a49b492007-12-12 20:25:13 +0800542
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000543 if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)",
544 cipher_name) >= CRYPTO_MAX_ALG_NAME)
545 return -ENAMETOOLONG;
546
Eric Biggers6a1faa42019-04-18 14:44:27 -0700547 return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800548}
549
Herbert Xu81c4c352015-07-14 16:53:18 +0800550static int crypto_ccm_base_create(struct crypto_template *tmpl,
551 struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800552{
Joy Latten4a49b492007-12-12 20:25:13 +0800553 const char *ctr_name;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700554 const char *mac_name;
Joy Latten4a49b492007-12-12 20:25:13 +0800555
556 ctr_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800557 if (IS_ERR(ctr_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800558 return PTR_ERR(ctr_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800559
Eric Biggers6a1faa42019-04-18 14:44:27 -0700560 mac_name = crypto_attr_alg_name(tb[2]);
561 if (IS_ERR(mac_name))
562 return PTR_ERR(mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800563
Eric Biggers6a1faa42019-04-18 14:44:27 -0700564 return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800565}
566
Joy Latten4a49b492007-12-12 20:25:13 +0800567static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
568 unsigned int keylen)
569{
570 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
571 struct crypto_aead *child = ctx->child;
Joy Latten4a49b492007-12-12 20:25:13 +0800572
573 if (keylen < 3)
574 return -EINVAL;
575
576 keylen -= 3;
577 memcpy(ctx->nonce, key + keylen, 3);
578
579 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
580 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
581 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600582 return crypto_aead_setkey(child, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +0800583}
584
585static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
586 unsigned int authsize)
587{
588 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
589
590 switch (authsize) {
591 case 8:
592 case 12:
593 case 16:
594 break;
595 default:
596 return -EINVAL;
597 }
598
599 return crypto_aead_setauthsize(ctx->child, authsize);
600}
601
602static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
603{
Herbert Xu81c4c352015-07-14 16:53:18 +0800604 struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
605 struct aead_request *subreq = &rctx->subreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800606 struct crypto_aead *aead = crypto_aead_reqtfm(req);
607 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
608 struct crypto_aead *child = ctx->child;
Herbert Xu81c4c352015-07-14 16:53:18 +0800609 struct scatterlist *sg;
Joy Latten4a49b492007-12-12 20:25:13 +0800610 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
611 crypto_aead_alignmask(child) + 1);
612
613 /* L' */
614 iv[0] = 3;
615
616 memcpy(iv + 1, ctx->nonce, 3);
617 memcpy(iv + 4, req->iv, 8);
618
Herbert Xu81c4c352015-07-14 16:53:18 +0800619 scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
620
621 sg_init_table(rctx->src, 3);
622 sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
623 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
624 if (sg != rctx->src + 1)
625 sg_chain(rctx->src, 2, sg);
626
627 if (req->src != req->dst) {
628 sg_init_table(rctx->dst, 3);
629 sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
630 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
631 if (sg != rctx->dst + 1)
632 sg_chain(rctx->dst, 2, sg);
633 }
634
Joy Latten4a49b492007-12-12 20:25:13 +0800635 aead_request_set_tfm(subreq, child);
636 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
637 req->base.data);
Herbert Xu81c4c352015-07-14 16:53:18 +0800638 aead_request_set_crypt(subreq, rctx->src,
639 req->src == req->dst ? rctx->src : rctx->dst,
640 req->cryptlen, iv);
641 aead_request_set_ad(subreq, req->assoclen - 8);
Joy Latten4a49b492007-12-12 20:25:13 +0800642
643 return subreq;
644}
645
646static int crypto_rfc4309_encrypt(struct aead_request *req)
647{
Herbert Xu81c4c352015-07-14 16:53:18 +0800648 if (req->assoclen != 16 && req->assoclen != 20)
649 return -EINVAL;
650
Joy Latten4a49b492007-12-12 20:25:13 +0800651 req = crypto_rfc4309_crypt(req);
652
653 return crypto_aead_encrypt(req);
654}
655
656static int crypto_rfc4309_decrypt(struct aead_request *req)
657{
Herbert Xu81c4c352015-07-14 16:53:18 +0800658 if (req->assoclen != 16 && req->assoclen != 20)
659 return -EINVAL;
660
Joy Latten4a49b492007-12-12 20:25:13 +0800661 req = crypto_rfc4309_crypt(req);
662
663 return crypto_aead_decrypt(req);
664}
665
Herbert Xu81c4c352015-07-14 16:53:18 +0800666static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800667{
Herbert Xu81c4c352015-07-14 16:53:18 +0800668 struct aead_instance *inst = aead_alg_instance(tfm);
669 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
670 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800671 struct crypto_aead *aead;
672 unsigned long align;
673
674 aead = crypto_spawn_aead(spawn);
675 if (IS_ERR(aead))
676 return PTR_ERR(aead);
677
678 ctx->child = aead;
679
680 align = crypto_aead_alignmask(aead);
681 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu81c4c352015-07-14 16:53:18 +0800682 crypto_aead_set_reqsize(
683 tfm,
684 sizeof(struct crypto_rfc4309_req_ctx) +
Herbert Xu2c221ad2015-05-11 17:47:56 +0800685 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu81c4c352015-07-14 16:53:18 +0800686 align + 32);
Joy Latten4a49b492007-12-12 20:25:13 +0800687
688 return 0;
689}
690
Herbert Xu81c4c352015-07-14 16:53:18 +0800691static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800692{
Herbert Xu81c4c352015-07-14 16:53:18 +0800693 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800694
695 crypto_free_aead(ctx->child);
696}
697
Herbert Xu81c4c352015-07-14 16:53:18 +0800698static void crypto_rfc4309_free(struct aead_instance *inst)
699{
700 crypto_drop_aead(aead_instance_ctx(inst));
701 kfree(inst);
702}
703
704static int crypto_rfc4309_create(struct crypto_template *tmpl,
705 struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800706{
Eric Biggerscd900f02020-01-02 19:58:46 -0800707 u32 mask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800708 struct aead_instance *inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800709 struct crypto_aead_spawn *spawn;
Herbert Xu81c4c352015-07-14 16:53:18 +0800710 struct aead_alg *alg;
Joy Latten4a49b492007-12-12 20:25:13 +0800711 int err;
712
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700713 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
714 if (err)
715 return err;
Eric Biggerscd900f02020-01-02 19:58:46 -0800716
Joy Latten4a49b492007-12-12 20:25:13 +0800717 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
718 if (!inst)
Herbert Xu81c4c352015-07-14 16:53:18 +0800719 return -ENOMEM;
Joy Latten4a49b492007-12-12 20:25:13 +0800720
Herbert Xu81c4c352015-07-14 16:53:18 +0800721 spawn = aead_instance_ctx(inst);
Eric Biggerscd900f02020-01-02 19:58:46 -0800722 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
Eric Biggers64d66792020-02-25 20:59:14 -0800723 crypto_attr_alg_name(tb[1]), 0, mask);
Joy Latten4a49b492007-12-12 20:25:13 +0800724 if (err)
Eric Biggers64d66792020-02-25 20:59:14 -0800725 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800726
Herbert Xu81c4c352015-07-14 16:53:18 +0800727 alg = crypto_spawn_aead_alg(spawn);
Joy Latten4a49b492007-12-12 20:25:13 +0800728
729 err = -EINVAL;
730
731 /* We only support 16-byte blocks. */
Herbert Xu81c4c352015-07-14 16:53:18 +0800732 if (crypto_aead_alg_ivsize(alg) != 16)
Eric Biggers64d66792020-02-25 20:59:14 -0800733 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800734
735 /* Not a stream cipher? */
Herbert Xu81c4c352015-07-14 16:53:18 +0800736 if (alg->base.cra_blocksize != 1)
Eric Biggers64d66792020-02-25 20:59:14 -0800737 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800738
739 err = -ENAMETOOLONG;
Herbert Xu81c4c352015-07-14 16:53:18 +0800740 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
741 "rfc4309(%s)", alg->base.cra_name) >=
742 CRYPTO_MAX_ALG_NAME ||
743 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
744 "rfc4309(%s)", alg->base.cra_driver_name) >=
Joy Latten4a49b492007-12-12 20:25:13 +0800745 CRYPTO_MAX_ALG_NAME)
Eric Biggers64d66792020-02-25 20:59:14 -0800746 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800747
Herbert Xu81c4c352015-07-14 16:53:18 +0800748 inst->alg.base.cra_priority = alg->base.cra_priority;
749 inst->alg.base.cra_blocksize = 1;
750 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Joy Latten4a49b492007-12-12 20:25:13 +0800751
Herbert Xu81c4c352015-07-14 16:53:18 +0800752 inst->alg.ivsize = 8;
Herbert Xu464b93a2016-07-12 13:17:38 +0800753 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xu81c4c352015-07-14 16:53:18 +0800754 inst->alg.maxauthsize = 16;
Joy Latten4a49b492007-12-12 20:25:13 +0800755
Herbert Xu81c4c352015-07-14 16:53:18 +0800756 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
Joy Latten4a49b492007-12-12 20:25:13 +0800757
Herbert Xu81c4c352015-07-14 16:53:18 +0800758 inst->alg.init = crypto_rfc4309_init_tfm;
759 inst->alg.exit = crypto_rfc4309_exit_tfm;
Joy Latten4a49b492007-12-12 20:25:13 +0800760
Herbert Xu81c4c352015-07-14 16:53:18 +0800761 inst->alg.setkey = crypto_rfc4309_setkey;
762 inst->alg.setauthsize = crypto_rfc4309_setauthsize;
763 inst->alg.encrypt = crypto_rfc4309_encrypt;
764 inst->alg.decrypt = crypto_rfc4309_decrypt;
Joy Latten4a49b492007-12-12 20:25:13 +0800765
Herbert Xu81c4c352015-07-14 16:53:18 +0800766 inst->free = crypto_rfc4309_free;
767
768 err = aead_register_instance(tmpl, inst);
Eric Biggers64d66792020-02-25 20:59:14 -0800769 if (err) {
770err_free_inst:
771 crypto_rfc4309_free(inst);
772 }
Herbert Xu81c4c352015-07-14 16:53:18 +0800773 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800774}
775
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000776static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
777 const u8 *inkey, unsigned int keylen)
778{
779 struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
780
781 return crypto_cipher_setkey(ctx->child, inkey, keylen);
782}
783
784static int crypto_cbcmac_digest_init(struct shash_desc *pdesc)
785{
786 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
787 int bs = crypto_shash_digestsize(pdesc->tfm);
788
789 ctx->len = 0;
Eric Biggers21415bf2023-10-18 22:53:32 -0700790 memset(ctx->dg, 0, bs);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000791
792 return 0;
793}
794
795static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p,
796 unsigned int len)
797{
798 struct crypto_shash *parent = pdesc->tfm;
799 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
800 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
801 struct crypto_cipher *tfm = tctx->child;
802 int bs = crypto_shash_digestsize(parent);
803
804 while (len > 0) {
805 unsigned int l = min(len, bs - ctx->len);
806
Eric Biggers21415bf2023-10-18 22:53:32 -0700807 crypto_xor(&ctx->dg[ctx->len], p, l);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000808 ctx->len +=l;
809 len -= l;
810 p += l;
811
812 if (ctx->len == bs) {
Eric Biggers21415bf2023-10-18 22:53:32 -0700813 crypto_cipher_encrypt_one(tfm, ctx->dg, ctx->dg);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000814 ctx->len = 0;
815 }
816 }
817
818 return 0;
819}
820
821static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out)
822{
823 struct crypto_shash *parent = pdesc->tfm;
824 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
825 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
826 struct crypto_cipher *tfm = tctx->child;
827 int bs = crypto_shash_digestsize(parent);
828
829 if (ctx->len)
Eric Biggers21415bf2023-10-18 22:53:32 -0700830 crypto_cipher_encrypt_one(tfm, ctx->dg, ctx->dg);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000831
Eric Biggers21415bf2023-10-18 22:53:32 -0700832 memcpy(out, ctx->dg, bs);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000833 return 0;
834}
835
836static int cbcmac_init_tfm(struct crypto_tfm *tfm)
837{
838 struct crypto_cipher *cipher;
839 struct crypto_instance *inst = (void *)tfm->__crt_alg;
Eric Biggersd5ed3b62020-01-02 19:59:05 -0800840 struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000841 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
842
843 cipher = crypto_spawn_cipher(spawn);
844 if (IS_ERR(cipher))
845 return PTR_ERR(cipher);
846
847 ctx->child = cipher;
848
849 return 0;
850};
851
852static void cbcmac_exit_tfm(struct crypto_tfm *tfm)
853{
854 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
855 crypto_free_cipher(ctx->child);
856}
857
858static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
859{
860 struct shash_instance *inst;
Eric Biggers16672972020-01-02 19:59:01 -0800861 struct crypto_cipher_spawn *spawn;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000862 struct crypto_alg *alg;
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700863 u32 mask;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000864 int err;
865
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700866 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000867 if (err)
868 return err;
869
Eric Biggers16672972020-01-02 19:59:01 -0800870 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
871 if (!inst)
872 return -ENOMEM;
873 spawn = shash_instance_ctx(inst);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000874
Eric Biggers16672972020-01-02 19:59:01 -0800875 err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700876 crypto_attr_alg_name(tb[1]), 0, mask);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000877 if (err)
Eric Biggers16672972020-01-02 19:59:01 -0800878 goto err_free_inst;
879 alg = crypto_spawn_cipher_alg(spawn);
880
881 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
882 if (err)
883 goto err_free_inst;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000884
885 inst->alg.base.cra_priority = alg->cra_priority;
886 inst->alg.base.cra_blocksize = 1;
887
888 inst->alg.digestsize = alg->cra_blocksize;
Eric Biggers21415bf2023-10-18 22:53:32 -0700889 inst->alg.descsize = sizeof(struct cbcmac_desc_ctx) +
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000890 alg->cra_blocksize;
891
892 inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx);
893 inst->alg.base.cra_init = cbcmac_init_tfm;
894 inst->alg.base.cra_exit = cbcmac_exit_tfm;
895
896 inst->alg.init = crypto_cbcmac_digest_init;
897 inst->alg.update = crypto_cbcmac_digest_update;
898 inst->alg.final = crypto_cbcmac_digest_final;
899 inst->alg.setkey = crypto_cbcmac_digest_setkey;
900
Eric Biggersa39c66c2020-01-02 20:04:38 -0800901 inst->free = shash_free_singlespawn_instance;
902
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000903 err = shash_register_instance(tmpl, inst);
Eric Biggers16672972020-01-02 19:59:01 -0800904 if (err) {
905err_free_inst:
Eric Biggersa39c66c2020-01-02 20:04:38 -0800906 shash_free_singlespawn_instance(inst);
Eric Biggers16672972020-01-02 19:59:01 -0800907 }
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000908 return err;
909}
910
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800911static struct crypto_template crypto_ccm_tmpls[] = {
912 {
913 .name = "cbcmac",
914 .create = cbcmac_create,
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800915 .module = THIS_MODULE,
916 }, {
917 .name = "ccm_base",
918 .create = crypto_ccm_base_create,
919 .module = THIS_MODULE,
920 }, {
921 .name = "ccm",
922 .create = crypto_ccm_create,
923 .module = THIS_MODULE,
924 }, {
925 .name = "rfc4309",
926 .create = crypto_rfc4309_create,
927 .module = THIS_MODULE,
928 },
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000929};
930
Joy Latten4a49b492007-12-12 20:25:13 +0800931static int __init crypto_ccm_module_init(void)
932{
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800933 return crypto_register_templates(crypto_ccm_tmpls,
934 ARRAY_SIZE(crypto_ccm_tmpls));
Joy Latten4a49b492007-12-12 20:25:13 +0800935}
936
937static void __exit crypto_ccm_module_exit(void)
938{
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800939 crypto_unregister_templates(crypto_ccm_tmpls,
940 ARRAY_SIZE(crypto_ccm_tmpls));
Joy Latten4a49b492007-12-12 20:25:13 +0800941}
942
Eric Biggersc4741b22019-04-11 21:57:42 -0700943subsys_initcall(crypto_ccm_module_init);
Joy Latten4a49b492007-12-12 20:25:13 +0800944module_exit(crypto_ccm_module_exit);
945
946MODULE_LICENSE("GPL");
947MODULE_DESCRIPTION("Counter with CBC MAC");
Kees Cook5d26a102014-11-20 17:05:53 -0800948MODULE_ALIAS_CRYPTO("ccm_base");
949MODULE_ALIAS_CRYPTO("rfc4309");
Kees Cook4943ba12014-11-24 16:32:38 -0800950MODULE_ALIAS_CRYPTO("ccm");
Ard Biesheuvelae748b92019-06-17 10:18:48 +0200951MODULE_ALIAS_CRYPTO("cbcmac");
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +0100952MODULE_IMPORT_NS(CRYPTO_INTERNAL);