blob: a9fb46f22eaaff10a1c95ea4b636daef2b089843 [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 Biesheuvelf15f05b2017-02-03 14:49:36 +00009#include <crypto/internal/hash.h>
Joy Latten4a49b492007-12-12 20:25:13 +080010#include <crypto/internal/skcipher.h>
11#include <crypto/scatterwalk.h>
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17
Joy Latten4a49b492007-12-12 20:25:13 +080018struct ccm_instance_ctx {
19 struct crypto_skcipher_spawn ctr;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000020 struct crypto_ahash_spawn mac;
Joy Latten4a49b492007-12-12 20:25:13 +080021};
22
23struct crypto_ccm_ctx {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000024 struct crypto_ahash *mac;
Herbert Xu464b93a2016-07-12 13:17:38 +080025 struct crypto_skcipher *ctr;
Joy Latten4a49b492007-12-12 20:25:13 +080026};
27
28struct crypto_rfc4309_ctx {
29 struct crypto_aead *child;
30 u8 nonce[3];
31};
32
Herbert Xu81c4c352015-07-14 16:53:18 +080033struct crypto_rfc4309_req_ctx {
34 struct scatterlist src[3];
35 struct scatterlist dst[3];
36 struct aead_request subreq;
37};
38
Joy Latten4a49b492007-12-12 20:25:13 +080039struct crypto_ccm_req_priv_ctx {
40 u8 odata[16];
Ard Biesheuvel3b304602017-02-27 15:30:56 +000041 u8 idata[16];
Joy Latten4a49b492007-12-12 20:25:13 +080042 u8 auth_tag[16];
Joy Latten4a49b492007-12-12 20:25:13 +080043 u32 flags;
Herbert Xu81c4c352015-07-14 16:53:18 +080044 struct scatterlist src[3];
45 struct scatterlist dst[3];
Ard Biesheuvelebf533a2018-08-07 14:18:37 -070046 union {
47 struct ahash_request ahreq;
48 struct skcipher_request skreq;
49 };
Joy Latten4a49b492007-12-12 20:25:13 +080050};
51
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000052struct cbcmac_tfm_ctx {
53 struct crypto_cipher *child;
54};
55
56struct cbcmac_desc_ctx {
57 unsigned int len;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000058};
59
Joy Latten4a49b492007-12-12 20:25:13 +080060static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
61 struct aead_request *req)
62{
63 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
64
65 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
66}
67
68static int set_msg_len(u8 *block, unsigned int msglen, int csize)
69{
70 __be32 data;
71
72 memset(block, 0, csize);
73 block += csize;
74
75 if (csize >= 4)
76 csize = 4;
77 else if (msglen > (1 << (8 * csize)))
78 return -EOVERFLOW;
79
80 data = cpu_to_be32(msglen);
81 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
82
83 return 0;
84}
85
86static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
87 unsigned int keylen)
88{
89 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu464b93a2016-07-12 13:17:38 +080090 struct crypto_skcipher *ctr = ctx->ctr;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000091 struct crypto_ahash *mac = ctx->mac;
Eric Biggersaf5034e2019-12-30 21:19:38 -060092 int err;
Joy Latten4a49b492007-12-12 20:25:13 +080093
Herbert Xu464b93a2016-07-12 13:17:38 +080094 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
95 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
96 CRYPTO_TFM_REQ_MASK);
97 err = crypto_skcipher_setkey(ctr, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +080098 if (err)
Eric Biggersaf5034e2019-12-30 21:19:38 -060099 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800100
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000101 crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK);
102 crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) &
Joy Latten4a49b492007-12-12 20:25:13 +0800103 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600104 return crypto_ahash_setkey(mac, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +0800105}
106
107static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
108 unsigned int authsize)
109{
110 switch (authsize) {
111 case 4:
112 case 6:
113 case 8:
114 case 10:
115 case 12:
116 case 14:
117 case 16:
118 break;
119 default:
120 return -EINVAL;
121 }
122
123 return 0;
124}
125
126static int format_input(u8 *info, struct aead_request *req,
127 unsigned int cryptlen)
128{
129 struct crypto_aead *aead = crypto_aead_reqtfm(req);
130 unsigned int lp = req->iv[0];
131 unsigned int l = lp + 1;
132 unsigned int m;
133
134 m = crypto_aead_authsize(aead);
135
136 memcpy(info, req->iv, 16);
137
138 /* format control info per RFC 3610 and
139 * NIST Special Publication 800-38C
140 */
141 *info |= (8 * ((m - 2) / 2));
142 if (req->assoclen)
143 *info |= 64;
144
145 return set_msg_len(info + 16 - l, cryptlen, l);
146}
147
148static int format_adata(u8 *adata, unsigned int a)
149{
150 int len = 0;
151
152 /* add control info for associated data
153 * RFC 3610 and NIST Special Publication 800-38C
154 */
155 if (a < 65280) {
156 *(__be16 *)adata = cpu_to_be16(a);
157 len = 2;
158 } else {
159 *(__be16 *)adata = cpu_to_be16(0xfffe);
160 *(__be32 *)&adata[2] = cpu_to_be32(a);
161 len = 6;
162 }
163
164 return len;
165}
166
Joy Latten4a49b492007-12-12 20:25:13 +0800167static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
168 unsigned int cryptlen)
169{
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000170 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Joy Latten4a49b492007-12-12 20:25:13 +0800171 struct crypto_aead *aead = crypto_aead_reqtfm(req);
172 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
Ard Biesheuvelebf533a2018-08-07 14:18:37 -0700173 struct ahash_request *ahreq = &pctx->ahreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800174 unsigned int assoclen = req->assoclen;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000175 struct scatterlist sg[3];
Ard Biesheuvel3b304602017-02-27 15:30:56 +0000176 u8 *odata = pctx->odata;
177 u8 *idata = pctx->idata;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000178 int ilen, err;
Joy Latten4a49b492007-12-12 20:25:13 +0800179
180 /* format control data for input */
181 err = format_input(odata, req, cryptlen);
182 if (err)
183 goto out;
184
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000185 sg_init_table(sg, 3);
186 sg_set_buf(&sg[0], odata, 16);
Joy Latten4a49b492007-12-12 20:25:13 +0800187
188 /* format associated data and compute into mac */
189 if (assoclen) {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000190 ilen = format_adata(idata, assoclen);
191 sg_set_buf(&sg[1], idata, ilen);
192 sg_chain(sg, 3, req->src);
Jarod Wilson516280e2009-01-22 19:58:15 +1100193 } else {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000194 ilen = 0;
195 sg_chain(sg, 2, req->src);
Joy Latten4a49b492007-12-12 20:25:13 +0800196 }
197
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000198 ahash_request_set_tfm(ahreq, ctx->mac);
199 ahash_request_set_callback(ahreq, pctx->flags, NULL, NULL);
200 ahash_request_set_crypt(ahreq, sg, NULL, assoclen + ilen + 16);
201 err = crypto_ahash_init(ahreq);
202 if (err)
203 goto out;
204 err = crypto_ahash_update(ahreq);
205 if (err)
206 goto out;
Joy Latten4a49b492007-12-12 20:25:13 +0800207
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000208 /* we need to pad the MAC input to a round multiple of the block size */
209 ilen = 16 - (assoclen + ilen) % 16;
210 if (ilen < 16) {
211 memset(idata, 0, ilen);
212 sg_init_table(sg, 2);
213 sg_set_buf(&sg[0], idata, ilen);
214 if (plain)
215 sg_chain(sg, 2, plain);
216 plain = sg;
217 cryptlen += ilen;
218 }
219
220 ahash_request_set_crypt(ahreq, plain, pctx->odata, cryptlen);
221 err = crypto_ahash_finup(ahreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800222out:
223 return err;
224}
225
226static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
227{
228 struct aead_request *req = areq->data;
229 struct crypto_aead *aead = crypto_aead_reqtfm(req);
230 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
231 u8 *odata = pctx->odata;
232
233 if (!err)
Herbert Xu81c4c352015-07-14 16:53:18 +0800234 scatterwalk_map_and_copy(odata, req->dst,
235 req->assoclen + req->cryptlen,
Joy Latten4a49b492007-12-12 20:25:13 +0800236 crypto_aead_authsize(aead), 1);
237 aead_request_complete(req, err);
238}
239
240static inline int crypto_ccm_check_iv(const u8 *iv)
241{
242 /* 2 <= L <= 8, so 1 <= L' <= 7. */
243 if (1 > iv[0] || iv[0] > 7)
244 return -EINVAL;
245
246 return 0;
247}
248
Herbert Xu81c4c352015-07-14 16:53:18 +0800249static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag)
250{
251 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
252 struct scatterlist *sg;
253 u8 *iv = req->iv;
254 int err;
255
256 err = crypto_ccm_check_iv(iv);
257 if (err)
258 return err;
259
260 pctx->flags = aead_request_flags(req);
261
262 /* Note: rfc 3610 and NIST 800-38C require counter of
263 * zero to encrypt auth tag.
264 */
265 memset(iv + 15 - iv[0], 0, iv[0] + 1);
266
267 sg_init_table(pctx->src, 3);
268 sg_set_buf(pctx->src, tag, 16);
269 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
270 if (sg != pctx->src + 1)
271 sg_chain(pctx->src, 2, sg);
272
273 if (req->src != req->dst) {
274 sg_init_table(pctx->dst, 3);
275 sg_set_buf(pctx->dst, tag, 16);
276 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
277 if (sg != pctx->dst + 1)
278 sg_chain(pctx->dst, 2, sg);
279 }
280
281 return 0;
282}
283
Joy Latten4a49b492007-12-12 20:25:13 +0800284static int crypto_ccm_encrypt(struct aead_request *req)
285{
286 struct crypto_aead *aead = crypto_aead_reqtfm(req);
287 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
288 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Herbert Xu464b93a2016-07-12 13:17:38 +0800289 struct skcipher_request *skreq = &pctx->skreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800290 struct scatterlist *dst;
291 unsigned int cryptlen = req->cryptlen;
292 u8 *odata = pctx->odata;
293 u8 *iv = req->iv;
294 int err;
295
Herbert Xu81c4c352015-07-14 16:53:18 +0800296 err = crypto_ccm_init_crypt(req, odata);
Joy Latten4a49b492007-12-12 20:25:13 +0800297 if (err)
298 return err;
299
Herbert Xu81c4c352015-07-14 16:53:18 +0800300 err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen);
Joy Latten4a49b492007-12-12 20:25:13 +0800301 if (err)
302 return err;
303
Joy Latten4a49b492007-12-12 20:25:13 +0800304 dst = pctx->src;
Herbert Xu81c4c352015-07-14 16:53:18 +0800305 if (req->src != req->dst)
Joy Latten4a49b492007-12-12 20:25:13 +0800306 dst = pctx->dst;
Joy Latten4a49b492007-12-12 20:25:13 +0800307
Herbert Xu464b93a2016-07-12 13:17:38 +0800308 skcipher_request_set_tfm(skreq, ctx->ctr);
309 skcipher_request_set_callback(skreq, pctx->flags,
310 crypto_ccm_encrypt_done, req);
311 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
312 err = crypto_skcipher_encrypt(skreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800313 if (err)
314 return err;
315
316 /* copy authtag to end of dst */
Herbert Xu81c4c352015-07-14 16:53:18 +0800317 scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen,
Joy Latten4a49b492007-12-12 20:25:13 +0800318 crypto_aead_authsize(aead), 1);
319 return err;
320}
321
322static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
323 int err)
324{
325 struct aead_request *req = areq->data;
326 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
327 struct crypto_aead *aead = crypto_aead_reqtfm(req);
328 unsigned int authsize = crypto_aead_authsize(aead);
329 unsigned int cryptlen = req->cryptlen - authsize;
Herbert Xu81c4c352015-07-14 16:53:18 +0800330 struct scatterlist *dst;
331
332 pctx->flags = 0;
333
334 dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
Joy Latten4a49b492007-12-12 20:25:13 +0800335
336 if (!err) {
Herbert Xu81c4c352015-07-14 16:53:18 +0800337 err = crypto_ccm_auth(req, dst, cryptlen);
James Yonan6bf37e52013-09-26 02:20:39 -0600338 if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
Joy Latten4a49b492007-12-12 20:25:13 +0800339 err = -EBADMSG;
340 }
341 aead_request_complete(req, err);
342}
343
344static int crypto_ccm_decrypt(struct aead_request *req)
345{
346 struct crypto_aead *aead = crypto_aead_reqtfm(req);
347 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
348 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Herbert Xu464b93a2016-07-12 13:17:38 +0800349 struct skcipher_request *skreq = &pctx->skreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800350 struct scatterlist *dst;
351 unsigned int authsize = crypto_aead_authsize(aead);
352 unsigned int cryptlen = req->cryptlen;
353 u8 *authtag = pctx->auth_tag;
354 u8 *odata = pctx->odata;
Romain Izard441f99c2017-10-31 15:42:35 +0100355 u8 *iv = pctx->idata;
Joy Latten4a49b492007-12-12 20:25:13 +0800356 int err;
357
Joy Latten4a49b492007-12-12 20:25:13 +0800358 cryptlen -= authsize;
359
Herbert Xu81c4c352015-07-14 16:53:18 +0800360 err = crypto_ccm_init_crypt(req, authtag);
Joy Latten4a49b492007-12-12 20:25:13 +0800361 if (err)
362 return err;
363
Herbert Xu81c4c352015-07-14 16:53:18 +0800364 scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
365 authsize, 0);
Joy Latten4a49b492007-12-12 20:25:13 +0800366
367 dst = pctx->src;
Herbert Xu81c4c352015-07-14 16:53:18 +0800368 if (req->src != req->dst)
Joy Latten4a49b492007-12-12 20:25:13 +0800369 dst = pctx->dst;
Joy Latten4a49b492007-12-12 20:25:13 +0800370
Romain Izard441f99c2017-10-31 15:42:35 +0100371 memcpy(iv, req->iv, 16);
372
Herbert Xu464b93a2016-07-12 13:17:38 +0800373 skcipher_request_set_tfm(skreq, ctx->ctr);
374 skcipher_request_set_callback(skreq, pctx->flags,
375 crypto_ccm_decrypt_done, req);
376 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
377 err = crypto_skcipher_decrypt(skreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800378 if (err)
379 return err;
380
Herbert Xu81c4c352015-07-14 16:53:18 +0800381 err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
Joy Latten4a49b492007-12-12 20:25:13 +0800382 if (err)
383 return err;
384
385 /* verify */
James Yonan6bf37e52013-09-26 02:20:39 -0600386 if (crypto_memneq(authtag, odata, authsize))
Joy Latten4a49b492007-12-12 20:25:13 +0800387 return -EBADMSG;
388
389 return err;
390}
391
Herbert Xu81c4c352015-07-14 16:53:18 +0800392static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800393{
Herbert Xu81c4c352015-07-14 16:53:18 +0800394 struct aead_instance *inst = aead_alg_instance(tfm);
395 struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
396 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000397 struct crypto_ahash *mac;
Herbert Xu464b93a2016-07-12 13:17:38 +0800398 struct crypto_skcipher *ctr;
Joy Latten4a49b492007-12-12 20:25:13 +0800399 unsigned long align;
400 int err;
401
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000402 mac = crypto_spawn_ahash(&ictx->mac);
403 if (IS_ERR(mac))
404 return PTR_ERR(mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800405
Eric Biggers60425a82016-10-28 09:52:19 -0700406 ctr = crypto_spawn_skcipher(&ictx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800407 err = PTR_ERR(ctr);
408 if (IS_ERR(ctr))
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000409 goto err_free_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800410
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000411 ctx->mac = mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800412 ctx->ctr = ctr;
413
Herbert Xu81c4c352015-07-14 16:53:18 +0800414 align = crypto_aead_alignmask(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800415 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu81c4c352015-07-14 16:53:18 +0800416 crypto_aead_set_reqsize(
417 tfm,
Herbert Xu2c221ad2015-05-11 17:47:56 +0800418 align + sizeof(struct crypto_ccm_req_priv_ctx) +
Ard Biesheuvelebf533a2018-08-07 14:18:37 -0700419 max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr)));
Joy Latten4a49b492007-12-12 20:25:13 +0800420
421 return 0;
422
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000423err_free_mac:
424 crypto_free_ahash(mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800425 return err;
426}
427
Herbert Xu81c4c352015-07-14 16:53:18 +0800428static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800429{
Herbert Xu81c4c352015-07-14 16:53:18 +0800430 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800431
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000432 crypto_free_ahash(ctx->mac);
Herbert Xu464b93a2016-07-12 13:17:38 +0800433 crypto_free_skcipher(ctx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800434}
435
Herbert Xu81c4c352015-07-14 16:53:18 +0800436static void crypto_ccm_free(struct aead_instance *inst)
437{
438 struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
439
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000440 crypto_drop_ahash(&ctx->mac);
Herbert Xu81c4c352015-07-14 16:53:18 +0800441 crypto_drop_skcipher(&ctx->ctr);
442 kfree(inst);
443}
444
445static int crypto_ccm_create_common(struct crypto_template *tmpl,
446 struct rtattr **tb,
Herbert Xu81c4c352015-07-14 16:53:18 +0800447 const char *ctr_name,
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000448 const char *mac_name)
Joy Latten4a49b492007-12-12 20:25:13 +0800449{
450 struct crypto_attr_type *algt;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800451 u32 mask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800452 struct aead_instance *inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800453 struct ccm_instance_ctx *ictx;
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800454 struct skcipher_alg *ctr;
455 struct hash_alg_common *mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800456 int err;
457
458 algt = crypto_get_attr_type(tb);
Joy Latten4a49b492007-12-12 20:25:13 +0800459 if (IS_ERR(algt))
Herbert Xu81c4c352015-07-14 16:53:18 +0800460 return PTR_ERR(algt);
Joy Latten4a49b492007-12-12 20:25:13 +0800461
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800462 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu81c4c352015-07-14 16:53:18 +0800463 return -EINVAL;
Joy Latten4a49b492007-12-12 20:25:13 +0800464
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800465 mask = crypto_requires_sync(algt->type, algt->mask);
466
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800467 inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
468 if (!inst)
469 return -ENOMEM;
470 ictx = aead_instance_ctx(inst);
Joy Latten4a49b492007-12-12 20:25:13 +0800471
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800472 err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst),
473 mac_name, 0, CRYPTO_ALG_ASYNC);
474 if (err)
475 goto err_free_inst;
476 mac = crypto_spawn_ahash_alg(&ictx->mac);
477
Joy Latten4a49b492007-12-12 20:25:13 +0800478 err = -EINVAL;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700479 if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
480 mac->digestsize != 16)
Joy Latten4a49b492007-12-12 20:25:13 +0800481 goto err_free_inst;
482
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800483 err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst),
484 ctr_name, 0, mask);
Joy Latten4a49b492007-12-12 20:25:13 +0800485 if (err)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800486 goto err_free_inst;
Herbert Xu464b93a2016-07-12 13:17:38 +0800487 ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800488
Eric Biggers6a1faa42019-04-18 14:44:27 -0700489 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Joy Latten4a49b492007-12-12 20:25:13 +0800490 err = -EINVAL;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700491 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
492 crypto_skcipher_alg_ivsize(ctr) != 16 ||
493 ctr->base.cra_blocksize != 1)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800494 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800495
Eric Biggers6a1faa42019-04-18 14:44:27 -0700496 /* ctr and cbcmac must use the same underlying block cipher. */
497 if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800498 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800499
500 err = -ENAMETOOLONG;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700501 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
502 "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800503 goto err_free_inst;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700504
Herbert Xu81c4c352015-07-14 16:53:18 +0800505 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu464b93a2016-07-12 13:17:38 +0800506 "ccm_base(%s,%s)", ctr->base.cra_driver_name,
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000507 mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800508 goto err_free_inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800509
Herbert Xu464b93a2016-07-12 13:17:38 +0800510 inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000511 inst->alg.base.cra_priority = (mac->base.cra_priority +
Herbert Xu464b93a2016-07-12 13:17:38 +0800512 ctr->base.cra_priority) / 2;
Herbert Xu81c4c352015-07-14 16:53:18 +0800513 inst->alg.base.cra_blocksize = 1;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000514 inst->alg.base.cra_alignmask = mac->base.cra_alignmask |
Ard Biesheuvel5ba8e2a2017-02-11 19:25:22 +0000515 ctr->base.cra_alignmask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800516 inst->alg.ivsize = 16;
Herbert Xu464b93a2016-07-12 13:17:38 +0800517 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xu81c4c352015-07-14 16:53:18 +0800518 inst->alg.maxauthsize = 16;
519 inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
520 inst->alg.init = crypto_ccm_init_tfm;
521 inst->alg.exit = crypto_ccm_exit_tfm;
522 inst->alg.setkey = crypto_ccm_setkey;
523 inst->alg.setauthsize = crypto_ccm_setauthsize;
524 inst->alg.encrypt = crypto_ccm_encrypt;
525 inst->alg.decrypt = crypto_ccm_decrypt;
Joy Latten4a49b492007-12-12 20:25:13 +0800526
Herbert Xu81c4c352015-07-14 16:53:18 +0800527 inst->free = crypto_ccm_free;
528
529 err = aead_register_instance(tmpl, inst);
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800530 if (err) {
Joy Latten4a49b492007-12-12 20:25:13 +0800531err_free_inst:
Eric Biggers05b3bbb2020-01-02 19:58:58 -0800532 crypto_ccm_free(inst);
533 }
534 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800535}
536
Herbert Xu81c4c352015-07-14 16:53:18 +0800537static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800538{
Joy Latten4a49b492007-12-12 20:25:13 +0800539 const char *cipher_name;
540 char ctr_name[CRYPTO_MAX_ALG_NAME];
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000541 char mac_name[CRYPTO_MAX_ALG_NAME];
Joy Latten4a49b492007-12-12 20:25:13 +0800542
543 cipher_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800544 if (IS_ERR(cipher_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800545 return PTR_ERR(cipher_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800546
547 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
548 cipher_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu81c4c352015-07-14 16:53:18 +0800549 return -ENAMETOOLONG;
Joy Latten4a49b492007-12-12 20:25:13 +0800550
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000551 if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)",
552 cipher_name) >= CRYPTO_MAX_ALG_NAME)
553 return -ENAMETOOLONG;
554
Eric Biggers6a1faa42019-04-18 14:44:27 -0700555 return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800556}
557
Herbert Xu81c4c352015-07-14 16:53:18 +0800558static int crypto_ccm_base_create(struct crypto_template *tmpl,
559 struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800560{
Joy Latten4a49b492007-12-12 20:25:13 +0800561 const char *ctr_name;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700562 const char *mac_name;
Joy Latten4a49b492007-12-12 20:25:13 +0800563
564 ctr_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800565 if (IS_ERR(ctr_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800566 return PTR_ERR(ctr_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800567
Eric Biggers6a1faa42019-04-18 14:44:27 -0700568 mac_name = crypto_attr_alg_name(tb[2]);
569 if (IS_ERR(mac_name))
570 return PTR_ERR(mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800571
Eric Biggers6a1faa42019-04-18 14:44:27 -0700572 return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800573}
574
Joy Latten4a49b492007-12-12 20:25:13 +0800575static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
576 unsigned int keylen)
577{
578 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
579 struct crypto_aead *child = ctx->child;
Joy Latten4a49b492007-12-12 20:25:13 +0800580
581 if (keylen < 3)
582 return -EINVAL;
583
584 keylen -= 3;
585 memcpy(ctx->nonce, key + keylen, 3);
586
587 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
588 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
589 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600590 return crypto_aead_setkey(child, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +0800591}
592
593static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
594 unsigned int authsize)
595{
596 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
597
598 switch (authsize) {
599 case 8:
600 case 12:
601 case 16:
602 break;
603 default:
604 return -EINVAL;
605 }
606
607 return crypto_aead_setauthsize(ctx->child, authsize);
608}
609
610static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
611{
Herbert Xu81c4c352015-07-14 16:53:18 +0800612 struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
613 struct aead_request *subreq = &rctx->subreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800614 struct crypto_aead *aead = crypto_aead_reqtfm(req);
615 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
616 struct crypto_aead *child = ctx->child;
Herbert Xu81c4c352015-07-14 16:53:18 +0800617 struct scatterlist *sg;
Joy Latten4a49b492007-12-12 20:25:13 +0800618 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
619 crypto_aead_alignmask(child) + 1);
620
621 /* L' */
622 iv[0] = 3;
623
624 memcpy(iv + 1, ctx->nonce, 3);
625 memcpy(iv + 4, req->iv, 8);
626
Herbert Xu81c4c352015-07-14 16:53:18 +0800627 scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
628
629 sg_init_table(rctx->src, 3);
630 sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
631 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
632 if (sg != rctx->src + 1)
633 sg_chain(rctx->src, 2, sg);
634
635 if (req->src != req->dst) {
636 sg_init_table(rctx->dst, 3);
637 sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
638 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
639 if (sg != rctx->dst + 1)
640 sg_chain(rctx->dst, 2, sg);
641 }
642
Joy Latten4a49b492007-12-12 20:25:13 +0800643 aead_request_set_tfm(subreq, child);
644 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
645 req->base.data);
Herbert Xu81c4c352015-07-14 16:53:18 +0800646 aead_request_set_crypt(subreq, rctx->src,
647 req->src == req->dst ? rctx->src : rctx->dst,
648 req->cryptlen, iv);
649 aead_request_set_ad(subreq, req->assoclen - 8);
Joy Latten4a49b492007-12-12 20:25:13 +0800650
651 return subreq;
652}
653
654static int crypto_rfc4309_encrypt(struct aead_request *req)
655{
Herbert Xu81c4c352015-07-14 16:53:18 +0800656 if (req->assoclen != 16 && req->assoclen != 20)
657 return -EINVAL;
658
Joy Latten4a49b492007-12-12 20:25:13 +0800659 req = crypto_rfc4309_crypt(req);
660
661 return crypto_aead_encrypt(req);
662}
663
664static int crypto_rfc4309_decrypt(struct aead_request *req)
665{
Herbert Xu81c4c352015-07-14 16:53:18 +0800666 if (req->assoclen != 16 && req->assoclen != 20)
667 return -EINVAL;
668
Joy Latten4a49b492007-12-12 20:25:13 +0800669 req = crypto_rfc4309_crypt(req);
670
671 return crypto_aead_decrypt(req);
672}
673
Herbert Xu81c4c352015-07-14 16:53:18 +0800674static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800675{
Herbert Xu81c4c352015-07-14 16:53:18 +0800676 struct aead_instance *inst = aead_alg_instance(tfm);
677 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
678 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800679 struct crypto_aead *aead;
680 unsigned long align;
681
682 aead = crypto_spawn_aead(spawn);
683 if (IS_ERR(aead))
684 return PTR_ERR(aead);
685
686 ctx->child = aead;
687
688 align = crypto_aead_alignmask(aead);
689 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu81c4c352015-07-14 16:53:18 +0800690 crypto_aead_set_reqsize(
691 tfm,
692 sizeof(struct crypto_rfc4309_req_ctx) +
Herbert Xu2c221ad2015-05-11 17:47:56 +0800693 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu81c4c352015-07-14 16:53:18 +0800694 align + 32);
Joy Latten4a49b492007-12-12 20:25:13 +0800695
696 return 0;
697}
698
Herbert Xu81c4c352015-07-14 16:53:18 +0800699static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800700{
Herbert Xu81c4c352015-07-14 16:53:18 +0800701 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800702
703 crypto_free_aead(ctx->child);
704}
705
Herbert Xu81c4c352015-07-14 16:53:18 +0800706static void crypto_rfc4309_free(struct aead_instance *inst)
707{
708 crypto_drop_aead(aead_instance_ctx(inst));
709 kfree(inst);
710}
711
712static int crypto_rfc4309_create(struct crypto_template *tmpl,
713 struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800714{
715 struct crypto_attr_type *algt;
Eric Biggerscd900f02020-01-02 19:58:46 -0800716 u32 mask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800717 struct aead_instance *inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800718 struct crypto_aead_spawn *spawn;
Herbert Xu81c4c352015-07-14 16:53:18 +0800719 struct aead_alg *alg;
Joy Latten4a49b492007-12-12 20:25:13 +0800720 const char *ccm_name;
721 int err;
722
723 algt = crypto_get_attr_type(tb);
Joy Latten4a49b492007-12-12 20:25:13 +0800724 if (IS_ERR(algt))
Herbert Xu81c4c352015-07-14 16:53:18 +0800725 return PTR_ERR(algt);
Joy Latten4a49b492007-12-12 20:25:13 +0800726
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800727 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu81c4c352015-07-14 16:53:18 +0800728 return -EINVAL;
Joy Latten4a49b492007-12-12 20:25:13 +0800729
Eric Biggerscd900f02020-01-02 19:58:46 -0800730 mask = crypto_requires_sync(algt->type, algt->mask);
731
Joy Latten4a49b492007-12-12 20:25:13 +0800732 ccm_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800733 if (IS_ERR(ccm_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800734 return PTR_ERR(ccm_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800735
736 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
737 if (!inst)
Herbert Xu81c4c352015-07-14 16:53:18 +0800738 return -ENOMEM;
Joy Latten4a49b492007-12-12 20:25:13 +0800739
Herbert Xu81c4c352015-07-14 16:53:18 +0800740 spawn = aead_instance_ctx(inst);
Eric Biggerscd900f02020-01-02 19:58:46 -0800741 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
742 ccm_name, 0, mask);
Joy Latten4a49b492007-12-12 20:25:13 +0800743 if (err)
744 goto out_free_inst;
745
Herbert Xu81c4c352015-07-14 16:53:18 +0800746 alg = crypto_spawn_aead_alg(spawn);
Joy Latten4a49b492007-12-12 20:25:13 +0800747
748 err = -EINVAL;
749
750 /* We only support 16-byte blocks. */
Herbert Xu81c4c352015-07-14 16:53:18 +0800751 if (crypto_aead_alg_ivsize(alg) != 16)
Joy Latten4a49b492007-12-12 20:25:13 +0800752 goto out_drop_alg;
753
754 /* Not a stream cipher? */
Herbert Xu81c4c352015-07-14 16:53:18 +0800755 if (alg->base.cra_blocksize != 1)
Joy Latten4a49b492007-12-12 20:25:13 +0800756 goto out_drop_alg;
757
758 err = -ENAMETOOLONG;
Herbert Xu81c4c352015-07-14 16:53:18 +0800759 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
760 "rfc4309(%s)", alg->base.cra_name) >=
761 CRYPTO_MAX_ALG_NAME ||
762 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
763 "rfc4309(%s)", alg->base.cra_driver_name) >=
Joy Latten4a49b492007-12-12 20:25:13 +0800764 CRYPTO_MAX_ALG_NAME)
765 goto out_drop_alg;
766
Herbert Xu81c4c352015-07-14 16:53:18 +0800767 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu81c4c352015-07-14 16:53:18 +0800768 inst->alg.base.cra_priority = alg->base.cra_priority;
769 inst->alg.base.cra_blocksize = 1;
770 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Joy Latten4a49b492007-12-12 20:25:13 +0800771
Herbert Xu81c4c352015-07-14 16:53:18 +0800772 inst->alg.ivsize = 8;
Herbert Xu464b93a2016-07-12 13:17:38 +0800773 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xu81c4c352015-07-14 16:53:18 +0800774 inst->alg.maxauthsize = 16;
Joy Latten4a49b492007-12-12 20:25:13 +0800775
Herbert Xu81c4c352015-07-14 16:53:18 +0800776 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
Joy Latten4a49b492007-12-12 20:25:13 +0800777
Herbert Xu81c4c352015-07-14 16:53:18 +0800778 inst->alg.init = crypto_rfc4309_init_tfm;
779 inst->alg.exit = crypto_rfc4309_exit_tfm;
Joy Latten4a49b492007-12-12 20:25:13 +0800780
Herbert Xu81c4c352015-07-14 16:53:18 +0800781 inst->alg.setkey = crypto_rfc4309_setkey;
782 inst->alg.setauthsize = crypto_rfc4309_setauthsize;
783 inst->alg.encrypt = crypto_rfc4309_encrypt;
784 inst->alg.decrypt = crypto_rfc4309_decrypt;
Joy Latten4a49b492007-12-12 20:25:13 +0800785
Herbert Xu81c4c352015-07-14 16:53:18 +0800786 inst->free = crypto_rfc4309_free;
787
788 err = aead_register_instance(tmpl, inst);
789 if (err)
790 goto out_drop_alg;
Joy Latten4a49b492007-12-12 20:25:13 +0800791
792out:
Herbert Xu81c4c352015-07-14 16:53:18 +0800793 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800794
795out_drop_alg:
796 crypto_drop_aead(spawn);
797out_free_inst:
798 kfree(inst);
Joy Latten4a49b492007-12-12 20:25:13 +0800799 goto out;
800}
801
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000802static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
803 const u8 *inkey, unsigned int keylen)
804{
805 struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
806
807 return crypto_cipher_setkey(ctx->child, inkey, keylen);
808}
809
810static int crypto_cbcmac_digest_init(struct shash_desc *pdesc)
811{
812 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
813 int bs = crypto_shash_digestsize(pdesc->tfm);
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000814 u8 *dg = (u8 *)ctx + crypto_shash_descsize(pdesc->tfm) - bs;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000815
816 ctx->len = 0;
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000817 memset(dg, 0, bs);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000818
819 return 0;
820}
821
822static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p,
823 unsigned int len)
824{
825 struct crypto_shash *parent = pdesc->tfm;
826 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
827 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
828 struct crypto_cipher *tfm = tctx->child;
829 int bs = crypto_shash_digestsize(parent);
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000830 u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000831
832 while (len > 0) {
833 unsigned int l = min(len, bs - ctx->len);
834
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000835 crypto_xor(dg + ctx->len, p, l);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000836 ctx->len +=l;
837 len -= l;
838 p += l;
839
840 if (ctx->len == bs) {
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000841 crypto_cipher_encrypt_one(tfm, dg, dg);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000842 ctx->len = 0;
843 }
844 }
845
846 return 0;
847}
848
849static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out)
850{
851 struct crypto_shash *parent = pdesc->tfm;
852 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
853 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
854 struct crypto_cipher *tfm = tctx->child;
855 int bs = crypto_shash_digestsize(parent);
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000856 u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000857
858 if (ctx->len)
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000859 crypto_cipher_encrypt_one(tfm, dg, dg);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000860
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000861 memcpy(out, dg, bs);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000862 return 0;
863}
864
865static int cbcmac_init_tfm(struct crypto_tfm *tfm)
866{
867 struct crypto_cipher *cipher;
868 struct crypto_instance *inst = (void *)tfm->__crt_alg;
869 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
870 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
871
872 cipher = crypto_spawn_cipher(spawn);
873 if (IS_ERR(cipher))
874 return PTR_ERR(cipher);
875
876 ctx->child = cipher;
877
878 return 0;
879};
880
881static void cbcmac_exit_tfm(struct crypto_tfm *tfm)
882{
883 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
884 crypto_free_cipher(ctx->child);
885}
886
887static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
888{
889 struct shash_instance *inst;
890 struct crypto_alg *alg;
891 int err;
892
893 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
894 if (err)
895 return err;
896
897 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
898 CRYPTO_ALG_TYPE_MASK);
899 if (IS_ERR(alg))
900 return PTR_ERR(alg);
901
902 inst = shash_alloc_instance("cbcmac", alg);
903 err = PTR_ERR(inst);
904 if (IS_ERR(inst))
905 goto out_put_alg;
906
907 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
908 shash_crypto_instance(inst),
909 CRYPTO_ALG_TYPE_MASK);
910 if (err)
911 goto out_free_inst;
912
913 inst->alg.base.cra_priority = alg->cra_priority;
914 inst->alg.base.cra_blocksize = 1;
915
916 inst->alg.digestsize = alg->cra_blocksize;
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000917 inst->alg.descsize = ALIGN(sizeof(struct cbcmac_desc_ctx),
918 alg->cra_alignmask + 1) +
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000919 alg->cra_blocksize;
920
921 inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx);
922 inst->alg.base.cra_init = cbcmac_init_tfm;
923 inst->alg.base.cra_exit = cbcmac_exit_tfm;
924
925 inst->alg.init = crypto_cbcmac_digest_init;
926 inst->alg.update = crypto_cbcmac_digest_update;
927 inst->alg.final = crypto_cbcmac_digest_final;
928 inst->alg.setkey = crypto_cbcmac_digest_setkey;
929
930 err = shash_register_instance(tmpl, inst);
931
932out_free_inst:
933 if (err)
934 shash_free_instance(shash_crypto_instance(inst));
935
936out_put_alg:
937 crypto_mod_put(alg);
938 return err;
939}
940
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800941static struct crypto_template crypto_ccm_tmpls[] = {
942 {
943 .name = "cbcmac",
944 .create = cbcmac_create,
945 .free = shash_free_instance,
946 .module = THIS_MODULE,
947 }, {
948 .name = "ccm_base",
949 .create = crypto_ccm_base_create,
950 .module = THIS_MODULE,
951 }, {
952 .name = "ccm",
953 .create = crypto_ccm_create,
954 .module = THIS_MODULE,
955 }, {
956 .name = "rfc4309",
957 .create = crypto_rfc4309_create,
958 .module = THIS_MODULE,
959 },
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000960};
961
Joy Latten4a49b492007-12-12 20:25:13 +0800962static int __init crypto_ccm_module_init(void)
963{
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800964 return crypto_register_templates(crypto_ccm_tmpls,
965 ARRAY_SIZE(crypto_ccm_tmpls));
Joy Latten4a49b492007-12-12 20:25:13 +0800966}
967
968static void __exit crypto_ccm_module_exit(void)
969{
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800970 crypto_unregister_templates(crypto_ccm_tmpls,
971 ARRAY_SIZE(crypto_ccm_tmpls));
Joy Latten4a49b492007-12-12 20:25:13 +0800972}
973
Eric Biggersc4741b22019-04-11 21:57:42 -0700974subsys_initcall(crypto_ccm_module_init);
Joy Latten4a49b492007-12-12 20:25:13 +0800975module_exit(crypto_ccm_module_exit);
976
977MODULE_LICENSE("GPL");
978MODULE_DESCRIPTION("Counter with CBC MAC");
Kees Cook5d26a102014-11-20 17:05:53 -0800979MODULE_ALIAS_CRYPTO("ccm_base");
980MODULE_ALIAS_CRYPTO("rfc4309");
Kees Cook4943ba12014-11-24 16:32:38 -0800981MODULE_ALIAS_CRYPTO("ccm");
Ard Biesheuvelae748b92019-06-17 10:18:48 +0200982MODULE_ALIAS_CRYPTO("cbcmac");