blob: 44104524e95a8816d044a4d3c9d91550a2acacda [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
18#include "internal.h"
19
20struct ccm_instance_ctx {
21 struct crypto_skcipher_spawn ctr;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000022 struct crypto_ahash_spawn mac;
Joy Latten4a49b492007-12-12 20:25:13 +080023};
24
25struct crypto_ccm_ctx {
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000026 struct crypto_ahash *mac;
Herbert Xu464b93a2016-07-12 13:17:38 +080027 struct crypto_skcipher *ctr;
Joy Latten4a49b492007-12-12 20:25:13 +080028};
29
30struct crypto_rfc4309_ctx {
31 struct crypto_aead *child;
32 u8 nonce[3];
33};
34
Herbert Xu81c4c352015-07-14 16:53:18 +080035struct crypto_rfc4309_req_ctx {
36 struct scatterlist src[3];
37 struct scatterlist dst[3];
38 struct aead_request subreq;
39};
40
Joy Latten4a49b492007-12-12 20:25:13 +080041struct crypto_ccm_req_priv_ctx {
42 u8 odata[16];
Ard Biesheuvel3b304602017-02-27 15:30:56 +000043 u8 idata[16];
Joy Latten4a49b492007-12-12 20:25:13 +080044 u8 auth_tag[16];
Joy Latten4a49b492007-12-12 20:25:13 +080045 u32 flags;
Herbert Xu81c4c352015-07-14 16:53:18 +080046 struct scatterlist src[3];
47 struct scatterlist dst[3];
Ard Biesheuvelebf533a2018-08-07 14:18:37 -070048 union {
49 struct ahash_request ahreq;
50 struct skcipher_request skreq;
51 };
Joy Latten4a49b492007-12-12 20:25:13 +080052};
53
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +000054struct cbcmac_tfm_ctx {
55 struct crypto_cipher *child;
56};
57
58struct cbcmac_desc_ctx {
59 unsigned int len;
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
222 ahash_request_set_crypt(ahreq, plain, pctx->odata, cryptlen);
223 err = crypto_ahash_finup(ahreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800224out:
225 return err;
226}
227
228static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
229{
230 struct aead_request *req = areq->data;
231 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
324static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
325 int err)
326{
327 struct aead_request *req = areq->data;
328 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
329 struct crypto_aead *aead = crypto_aead_reqtfm(req);
330 unsigned int authsize = crypto_aead_authsize(aead);
331 unsigned int cryptlen = req->cryptlen - authsize;
Herbert Xu81c4c352015-07-14 16:53:18 +0800332 struct scatterlist *dst;
333
334 pctx->flags = 0;
335
336 dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
Joy Latten4a49b492007-12-12 20:25:13 +0800337
338 if (!err) {
Herbert Xu81c4c352015-07-14 16:53:18 +0800339 err = crypto_ccm_auth(req, dst, cryptlen);
James Yonan6bf37e52013-09-26 02:20:39 -0600340 if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
Joy Latten4a49b492007-12-12 20:25:13 +0800341 err = -EBADMSG;
342 }
343 aead_request_complete(req, err);
344}
345
346static int crypto_ccm_decrypt(struct aead_request *req)
347{
348 struct crypto_aead *aead = crypto_aead_reqtfm(req);
349 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
350 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
Herbert Xu464b93a2016-07-12 13:17:38 +0800351 struct skcipher_request *skreq = &pctx->skreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800352 struct scatterlist *dst;
353 unsigned int authsize = crypto_aead_authsize(aead);
354 unsigned int cryptlen = req->cryptlen;
355 u8 *authtag = pctx->auth_tag;
356 u8 *odata = pctx->odata;
Romain Izard441f99c2017-10-31 15:42:35 +0100357 u8 *iv = pctx->idata;
Joy Latten4a49b492007-12-12 20:25:13 +0800358 int err;
359
Joy Latten4a49b492007-12-12 20:25:13 +0800360 cryptlen -= authsize;
361
Herbert Xu81c4c352015-07-14 16:53:18 +0800362 err = crypto_ccm_init_crypt(req, authtag);
Joy Latten4a49b492007-12-12 20:25:13 +0800363 if (err)
364 return err;
365
Herbert Xu81c4c352015-07-14 16:53:18 +0800366 scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
367 authsize, 0);
Joy Latten4a49b492007-12-12 20:25:13 +0800368
369 dst = pctx->src;
Herbert Xu81c4c352015-07-14 16:53:18 +0800370 if (req->src != req->dst)
Joy Latten4a49b492007-12-12 20:25:13 +0800371 dst = pctx->dst;
Joy Latten4a49b492007-12-12 20:25:13 +0800372
Romain Izard441f99c2017-10-31 15:42:35 +0100373 memcpy(iv, req->iv, 16);
374
Herbert Xu464b93a2016-07-12 13:17:38 +0800375 skcipher_request_set_tfm(skreq, ctx->ctr);
376 skcipher_request_set_callback(skreq, pctx->flags,
377 crypto_ccm_decrypt_done, req);
378 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
379 err = crypto_skcipher_decrypt(skreq);
Joy Latten4a49b492007-12-12 20:25:13 +0800380 if (err)
381 return err;
382
Herbert Xu81c4c352015-07-14 16:53:18 +0800383 err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
Joy Latten4a49b492007-12-12 20:25:13 +0800384 if (err)
385 return err;
386
387 /* verify */
James Yonan6bf37e52013-09-26 02:20:39 -0600388 if (crypto_memneq(authtag, odata, authsize))
Joy Latten4a49b492007-12-12 20:25:13 +0800389 return -EBADMSG;
390
391 return err;
392}
393
Herbert Xu81c4c352015-07-14 16:53:18 +0800394static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800395{
Herbert Xu81c4c352015-07-14 16:53:18 +0800396 struct aead_instance *inst = aead_alg_instance(tfm);
397 struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
398 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000399 struct crypto_ahash *mac;
Herbert Xu464b93a2016-07-12 13:17:38 +0800400 struct crypto_skcipher *ctr;
Joy Latten4a49b492007-12-12 20:25:13 +0800401 unsigned long align;
402 int err;
403
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000404 mac = crypto_spawn_ahash(&ictx->mac);
405 if (IS_ERR(mac))
406 return PTR_ERR(mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800407
Eric Biggers60425a82016-10-28 09:52:19 -0700408 ctr = crypto_spawn_skcipher(&ictx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800409 err = PTR_ERR(ctr);
410 if (IS_ERR(ctr))
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000411 goto err_free_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800412
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000413 ctx->mac = mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800414 ctx->ctr = ctr;
415
Herbert Xu81c4c352015-07-14 16:53:18 +0800416 align = crypto_aead_alignmask(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800417 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu81c4c352015-07-14 16:53:18 +0800418 crypto_aead_set_reqsize(
419 tfm,
Herbert Xu2c221ad2015-05-11 17:47:56 +0800420 align + sizeof(struct crypto_ccm_req_priv_ctx) +
Ard Biesheuvelebf533a2018-08-07 14:18:37 -0700421 max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr)));
Joy Latten4a49b492007-12-12 20:25:13 +0800422
423 return 0;
424
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000425err_free_mac:
426 crypto_free_ahash(mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800427 return err;
428}
429
Herbert Xu81c4c352015-07-14 16:53:18 +0800430static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800431{
Herbert Xu81c4c352015-07-14 16:53:18 +0800432 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800433
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000434 crypto_free_ahash(ctx->mac);
Herbert Xu464b93a2016-07-12 13:17:38 +0800435 crypto_free_skcipher(ctx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800436}
437
Herbert Xu81c4c352015-07-14 16:53:18 +0800438static void crypto_ccm_free(struct aead_instance *inst)
439{
440 struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
441
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000442 crypto_drop_ahash(&ctx->mac);
Herbert Xu81c4c352015-07-14 16:53:18 +0800443 crypto_drop_skcipher(&ctx->ctr);
444 kfree(inst);
445}
446
447static int crypto_ccm_create_common(struct crypto_template *tmpl,
448 struct rtattr **tb,
Herbert Xu81c4c352015-07-14 16:53:18 +0800449 const char *ctr_name,
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000450 const char *mac_name)
Joy Latten4a49b492007-12-12 20:25:13 +0800451{
452 struct crypto_attr_type *algt;
Herbert Xu81c4c352015-07-14 16:53:18 +0800453 struct aead_instance *inst;
Herbert Xu464b93a2016-07-12 13:17:38 +0800454 struct skcipher_alg *ctr;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000455 struct crypto_alg *mac_alg;
456 struct hash_alg_common *mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800457 struct ccm_instance_ctx *ictx;
458 int err;
459
460 algt = crypto_get_attr_type(tb);
Joy Latten4a49b492007-12-12 20:25:13 +0800461 if (IS_ERR(algt))
Herbert Xu81c4c352015-07-14 16:53:18 +0800462 return PTR_ERR(algt);
Joy Latten4a49b492007-12-12 20:25:13 +0800463
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800464 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu81c4c352015-07-14 16:53:18 +0800465 return -EINVAL;
Joy Latten4a49b492007-12-12 20:25:13 +0800466
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000467 mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type,
468 CRYPTO_ALG_TYPE_HASH,
469 CRYPTO_ALG_TYPE_AHASH_MASK |
470 CRYPTO_ALG_ASYNC);
471 if (IS_ERR(mac_alg))
472 return PTR_ERR(mac_alg);
Joy Latten4a49b492007-12-12 20:25:13 +0800473
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000474 mac = __crypto_hash_alg_common(mac_alg);
Joy Latten4a49b492007-12-12 20:25:13 +0800475 err = -EINVAL;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700476 if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
477 mac->digestsize != 16)
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000478 goto out_put_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800479
480 inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
481 err = -ENOMEM;
482 if (!inst)
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000483 goto out_put_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800484
Herbert Xu81c4c352015-07-14 16:53:18 +0800485 ictx = aead_instance_ctx(inst);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000486 err = crypto_init_ahash_spawn(&ictx->mac, mac,
487 aead_crypto_instance(inst));
Joy Latten4a49b492007-12-12 20:25:13 +0800488 if (err)
489 goto err_free_inst;
490
Herbert Xu81c4c352015-07-14 16:53:18 +0800491 crypto_set_skcipher_spawn(&ictx->ctr, aead_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700492 err = crypto_grab_skcipher(&ictx->ctr, ctr_name, 0,
493 crypto_requires_sync(algt->type,
494 algt->mask));
Joy Latten4a49b492007-12-12 20:25:13 +0800495 if (err)
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000496 goto err_drop_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800497
Herbert Xu464b93a2016-07-12 13:17:38 +0800498 ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
Joy Latten4a49b492007-12-12 20:25:13 +0800499
Eric Biggers6a1faa42019-04-18 14:44:27 -0700500 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Joy Latten4a49b492007-12-12 20:25:13 +0800501 err = -EINVAL;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700502 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
503 crypto_skcipher_alg_ivsize(ctr) != 16 ||
504 ctr->base.cra_blocksize != 1)
Joy Latten4a49b492007-12-12 20:25:13 +0800505 goto err_drop_ctr;
506
Eric Biggers6a1faa42019-04-18 14:44:27 -0700507 /* ctr and cbcmac must use the same underlying block cipher. */
508 if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0)
Joy Latten4a49b492007-12-12 20:25:13 +0800509 goto err_drop_ctr;
510
511 err = -ENAMETOOLONG;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700512 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
513 "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
514 goto err_drop_ctr;
515
Herbert Xu81c4c352015-07-14 16:53:18 +0800516 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu464b93a2016-07-12 13:17:38 +0800517 "ccm_base(%s,%s)", ctr->base.cra_driver_name,
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000518 mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Joy Latten4a49b492007-12-12 20:25:13 +0800519 goto err_drop_ctr;
520
Herbert Xu464b93a2016-07-12 13:17:38 +0800521 inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000522 inst->alg.base.cra_priority = (mac->base.cra_priority +
Herbert Xu464b93a2016-07-12 13:17:38 +0800523 ctr->base.cra_priority) / 2;
Herbert Xu81c4c352015-07-14 16:53:18 +0800524 inst->alg.base.cra_blocksize = 1;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000525 inst->alg.base.cra_alignmask = mac->base.cra_alignmask |
Ard Biesheuvel5ba8e2a2017-02-11 19:25:22 +0000526 ctr->base.cra_alignmask;
Herbert Xu81c4c352015-07-14 16:53:18 +0800527 inst->alg.ivsize = 16;
Herbert Xu464b93a2016-07-12 13:17:38 +0800528 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xu81c4c352015-07-14 16:53:18 +0800529 inst->alg.maxauthsize = 16;
530 inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
531 inst->alg.init = crypto_ccm_init_tfm;
532 inst->alg.exit = crypto_ccm_exit_tfm;
533 inst->alg.setkey = crypto_ccm_setkey;
534 inst->alg.setauthsize = crypto_ccm_setauthsize;
535 inst->alg.encrypt = crypto_ccm_encrypt;
536 inst->alg.decrypt = crypto_ccm_decrypt;
Joy Latten4a49b492007-12-12 20:25:13 +0800537
Herbert Xu81c4c352015-07-14 16:53:18 +0800538 inst->free = crypto_ccm_free;
539
540 err = aead_register_instance(tmpl, inst);
541 if (err)
542 goto err_drop_ctr;
543
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000544out_put_mac:
545 crypto_mod_put(mac_alg);
Herbert Xu81c4c352015-07-14 16:53:18 +0800546 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800547
548err_drop_ctr:
549 crypto_drop_skcipher(&ictx->ctr);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000550err_drop_mac:
551 crypto_drop_ahash(&ictx->mac);
Joy Latten4a49b492007-12-12 20:25:13 +0800552err_free_inst:
553 kfree(inst);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000554 goto out_put_mac;
Joy Latten4a49b492007-12-12 20:25:13 +0800555}
556
Herbert Xu81c4c352015-07-14 16:53:18 +0800557static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800558{
Joy Latten4a49b492007-12-12 20:25:13 +0800559 const char *cipher_name;
560 char ctr_name[CRYPTO_MAX_ALG_NAME];
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000561 char mac_name[CRYPTO_MAX_ALG_NAME];
Joy Latten4a49b492007-12-12 20:25:13 +0800562
563 cipher_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800564 if (IS_ERR(cipher_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800565 return PTR_ERR(cipher_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800566
567 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
568 cipher_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu81c4c352015-07-14 16:53:18 +0800569 return -ENAMETOOLONG;
Joy Latten4a49b492007-12-12 20:25:13 +0800570
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000571 if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)",
572 cipher_name) >= CRYPTO_MAX_ALG_NAME)
573 return -ENAMETOOLONG;
574
Eric Biggers6a1faa42019-04-18 14:44:27 -0700575 return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800576}
577
Herbert Xu81c4c352015-07-14 16:53:18 +0800578static int crypto_ccm_base_create(struct crypto_template *tmpl,
579 struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800580{
Joy Latten4a49b492007-12-12 20:25:13 +0800581 const char *ctr_name;
Eric Biggers6a1faa42019-04-18 14:44:27 -0700582 const char *mac_name;
Joy Latten4a49b492007-12-12 20:25:13 +0800583
584 ctr_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800585 if (IS_ERR(ctr_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800586 return PTR_ERR(ctr_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800587
Eric Biggers6a1faa42019-04-18 14:44:27 -0700588 mac_name = crypto_attr_alg_name(tb[2]);
589 if (IS_ERR(mac_name))
590 return PTR_ERR(mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800591
Eric Biggers6a1faa42019-04-18 14:44:27 -0700592 return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800593}
594
Joy Latten4a49b492007-12-12 20:25:13 +0800595static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
596 unsigned int keylen)
597{
598 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
599 struct crypto_aead *child = ctx->child;
Joy Latten4a49b492007-12-12 20:25:13 +0800600
601 if (keylen < 3)
602 return -EINVAL;
603
604 keylen -= 3;
605 memcpy(ctx->nonce, key + keylen, 3);
606
607 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
608 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
609 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600610 return crypto_aead_setkey(child, key, keylen);
Joy Latten4a49b492007-12-12 20:25:13 +0800611}
612
613static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
614 unsigned int authsize)
615{
616 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
617
618 switch (authsize) {
619 case 8:
620 case 12:
621 case 16:
622 break;
623 default:
624 return -EINVAL;
625 }
626
627 return crypto_aead_setauthsize(ctx->child, authsize);
628}
629
630static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
631{
Herbert Xu81c4c352015-07-14 16:53:18 +0800632 struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
633 struct aead_request *subreq = &rctx->subreq;
Joy Latten4a49b492007-12-12 20:25:13 +0800634 struct crypto_aead *aead = crypto_aead_reqtfm(req);
635 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
636 struct crypto_aead *child = ctx->child;
Herbert Xu81c4c352015-07-14 16:53:18 +0800637 struct scatterlist *sg;
Joy Latten4a49b492007-12-12 20:25:13 +0800638 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
639 crypto_aead_alignmask(child) + 1);
640
641 /* L' */
642 iv[0] = 3;
643
644 memcpy(iv + 1, ctx->nonce, 3);
645 memcpy(iv + 4, req->iv, 8);
646
Herbert Xu81c4c352015-07-14 16:53:18 +0800647 scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
648
649 sg_init_table(rctx->src, 3);
650 sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
651 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
652 if (sg != rctx->src + 1)
653 sg_chain(rctx->src, 2, sg);
654
655 if (req->src != req->dst) {
656 sg_init_table(rctx->dst, 3);
657 sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
658 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
659 if (sg != rctx->dst + 1)
660 sg_chain(rctx->dst, 2, sg);
661 }
662
Joy Latten4a49b492007-12-12 20:25:13 +0800663 aead_request_set_tfm(subreq, child);
664 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
665 req->base.data);
Herbert Xu81c4c352015-07-14 16:53:18 +0800666 aead_request_set_crypt(subreq, rctx->src,
667 req->src == req->dst ? rctx->src : rctx->dst,
668 req->cryptlen, iv);
669 aead_request_set_ad(subreq, req->assoclen - 8);
Joy Latten4a49b492007-12-12 20:25:13 +0800670
671 return subreq;
672}
673
674static int crypto_rfc4309_encrypt(struct aead_request *req)
675{
Herbert Xu81c4c352015-07-14 16:53:18 +0800676 if (req->assoclen != 16 && req->assoclen != 20)
677 return -EINVAL;
678
Joy Latten4a49b492007-12-12 20:25:13 +0800679 req = crypto_rfc4309_crypt(req);
680
681 return crypto_aead_encrypt(req);
682}
683
684static int crypto_rfc4309_decrypt(struct aead_request *req)
685{
Herbert Xu81c4c352015-07-14 16:53:18 +0800686 if (req->assoclen != 16 && req->assoclen != 20)
687 return -EINVAL;
688
Joy Latten4a49b492007-12-12 20:25:13 +0800689 req = crypto_rfc4309_crypt(req);
690
691 return crypto_aead_decrypt(req);
692}
693
Herbert Xu81c4c352015-07-14 16:53:18 +0800694static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800695{
Herbert Xu81c4c352015-07-14 16:53:18 +0800696 struct aead_instance *inst = aead_alg_instance(tfm);
697 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
698 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800699 struct crypto_aead *aead;
700 unsigned long align;
701
702 aead = crypto_spawn_aead(spawn);
703 if (IS_ERR(aead))
704 return PTR_ERR(aead);
705
706 ctx->child = aead;
707
708 align = crypto_aead_alignmask(aead);
709 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu81c4c352015-07-14 16:53:18 +0800710 crypto_aead_set_reqsize(
711 tfm,
712 sizeof(struct crypto_rfc4309_req_ctx) +
Herbert Xu2c221ad2015-05-11 17:47:56 +0800713 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu81c4c352015-07-14 16:53:18 +0800714 align + 32);
Joy Latten4a49b492007-12-12 20:25:13 +0800715
716 return 0;
717}
718
Herbert Xu81c4c352015-07-14 16:53:18 +0800719static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
Joy Latten4a49b492007-12-12 20:25:13 +0800720{
Herbert Xu81c4c352015-07-14 16:53:18 +0800721 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
Joy Latten4a49b492007-12-12 20:25:13 +0800722
723 crypto_free_aead(ctx->child);
724}
725
Herbert Xu81c4c352015-07-14 16:53:18 +0800726static void crypto_rfc4309_free(struct aead_instance *inst)
727{
728 crypto_drop_aead(aead_instance_ctx(inst));
729 kfree(inst);
730}
731
732static int crypto_rfc4309_create(struct crypto_template *tmpl,
733 struct rtattr **tb)
Joy Latten4a49b492007-12-12 20:25:13 +0800734{
735 struct crypto_attr_type *algt;
Herbert Xu81c4c352015-07-14 16:53:18 +0800736 struct aead_instance *inst;
Joy Latten4a49b492007-12-12 20:25:13 +0800737 struct crypto_aead_spawn *spawn;
Herbert Xu81c4c352015-07-14 16:53:18 +0800738 struct aead_alg *alg;
Joy Latten4a49b492007-12-12 20:25:13 +0800739 const char *ccm_name;
740 int err;
741
742 algt = crypto_get_attr_type(tb);
Joy Latten4a49b492007-12-12 20:25:13 +0800743 if (IS_ERR(algt))
Herbert Xu81c4c352015-07-14 16:53:18 +0800744 return PTR_ERR(algt);
Joy Latten4a49b492007-12-12 20:25:13 +0800745
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800746 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu81c4c352015-07-14 16:53:18 +0800747 return -EINVAL;
Joy Latten4a49b492007-12-12 20:25:13 +0800748
749 ccm_name = crypto_attr_alg_name(tb[1]);
Joy Latten4a49b492007-12-12 20:25:13 +0800750 if (IS_ERR(ccm_name))
Herbert Xu81c4c352015-07-14 16:53:18 +0800751 return PTR_ERR(ccm_name);
Joy Latten4a49b492007-12-12 20:25:13 +0800752
753 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
754 if (!inst)
Herbert Xu81c4c352015-07-14 16:53:18 +0800755 return -ENOMEM;
Joy Latten4a49b492007-12-12 20:25:13 +0800756
Herbert Xu81c4c352015-07-14 16:53:18 +0800757 spawn = aead_instance_ctx(inst);
758 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Joy Latten4a49b492007-12-12 20:25:13 +0800759 err = crypto_grab_aead(spawn, ccm_name, 0,
760 crypto_requires_sync(algt->type, algt->mask));
761 if (err)
762 goto out_free_inst;
763
Herbert Xu81c4c352015-07-14 16:53:18 +0800764 alg = crypto_spawn_aead_alg(spawn);
Joy Latten4a49b492007-12-12 20:25:13 +0800765
766 err = -EINVAL;
767
768 /* We only support 16-byte blocks. */
Herbert Xu81c4c352015-07-14 16:53:18 +0800769 if (crypto_aead_alg_ivsize(alg) != 16)
Joy Latten4a49b492007-12-12 20:25:13 +0800770 goto out_drop_alg;
771
772 /* Not a stream cipher? */
Herbert Xu81c4c352015-07-14 16:53:18 +0800773 if (alg->base.cra_blocksize != 1)
Joy Latten4a49b492007-12-12 20:25:13 +0800774 goto out_drop_alg;
775
776 err = -ENAMETOOLONG;
Herbert Xu81c4c352015-07-14 16:53:18 +0800777 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
778 "rfc4309(%s)", alg->base.cra_name) >=
779 CRYPTO_MAX_ALG_NAME ||
780 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
781 "rfc4309(%s)", alg->base.cra_driver_name) >=
Joy Latten4a49b492007-12-12 20:25:13 +0800782 CRYPTO_MAX_ALG_NAME)
783 goto out_drop_alg;
784
Herbert Xu81c4c352015-07-14 16:53:18 +0800785 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu81c4c352015-07-14 16:53:18 +0800786 inst->alg.base.cra_priority = alg->base.cra_priority;
787 inst->alg.base.cra_blocksize = 1;
788 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Joy Latten4a49b492007-12-12 20:25:13 +0800789
Herbert Xu81c4c352015-07-14 16:53:18 +0800790 inst->alg.ivsize = 8;
Herbert Xu464b93a2016-07-12 13:17:38 +0800791 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xu81c4c352015-07-14 16:53:18 +0800792 inst->alg.maxauthsize = 16;
Joy Latten4a49b492007-12-12 20:25:13 +0800793
Herbert Xu81c4c352015-07-14 16:53:18 +0800794 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
Joy Latten4a49b492007-12-12 20:25:13 +0800795
Herbert Xu81c4c352015-07-14 16:53:18 +0800796 inst->alg.init = crypto_rfc4309_init_tfm;
797 inst->alg.exit = crypto_rfc4309_exit_tfm;
Joy Latten4a49b492007-12-12 20:25:13 +0800798
Herbert Xu81c4c352015-07-14 16:53:18 +0800799 inst->alg.setkey = crypto_rfc4309_setkey;
800 inst->alg.setauthsize = crypto_rfc4309_setauthsize;
801 inst->alg.encrypt = crypto_rfc4309_encrypt;
802 inst->alg.decrypt = crypto_rfc4309_decrypt;
Joy Latten4a49b492007-12-12 20:25:13 +0800803
Herbert Xu81c4c352015-07-14 16:53:18 +0800804 inst->free = crypto_rfc4309_free;
805
806 err = aead_register_instance(tmpl, inst);
807 if (err)
808 goto out_drop_alg;
Joy Latten4a49b492007-12-12 20:25:13 +0800809
810out:
Herbert Xu81c4c352015-07-14 16:53:18 +0800811 return err;
Joy Latten4a49b492007-12-12 20:25:13 +0800812
813out_drop_alg:
814 crypto_drop_aead(spawn);
815out_free_inst:
816 kfree(inst);
Joy Latten4a49b492007-12-12 20:25:13 +0800817 goto out;
818}
819
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000820static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
821 const u8 *inkey, unsigned int keylen)
822{
823 struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
824
825 return crypto_cipher_setkey(ctx->child, inkey, keylen);
826}
827
828static int crypto_cbcmac_digest_init(struct shash_desc *pdesc)
829{
830 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
831 int bs = crypto_shash_digestsize(pdesc->tfm);
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000832 u8 *dg = (u8 *)ctx + crypto_shash_descsize(pdesc->tfm) - bs;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000833
834 ctx->len = 0;
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000835 memset(dg, 0, bs);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000836
837 return 0;
838}
839
840static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p,
841 unsigned int len)
842{
843 struct crypto_shash *parent = pdesc->tfm;
844 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
845 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
846 struct crypto_cipher *tfm = tctx->child;
847 int bs = crypto_shash_digestsize(parent);
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000848 u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000849
850 while (len > 0) {
851 unsigned int l = min(len, bs - ctx->len);
852
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000853 crypto_xor(dg + ctx->len, p, l);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000854 ctx->len +=l;
855 len -= l;
856 p += l;
857
858 if (ctx->len == bs) {
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000859 crypto_cipher_encrypt_one(tfm, dg, dg);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000860 ctx->len = 0;
861 }
862 }
863
864 return 0;
865}
866
867static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out)
868{
869 struct crypto_shash *parent = pdesc->tfm;
870 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
871 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
872 struct crypto_cipher *tfm = tctx->child;
873 int bs = crypto_shash_digestsize(parent);
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000874 u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000875
876 if (ctx->len)
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000877 crypto_cipher_encrypt_one(tfm, dg, dg);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000878
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000879 memcpy(out, dg, bs);
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000880 return 0;
881}
882
883static int cbcmac_init_tfm(struct crypto_tfm *tfm)
884{
885 struct crypto_cipher *cipher;
886 struct crypto_instance *inst = (void *)tfm->__crt_alg;
887 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
888 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
889
890 cipher = crypto_spawn_cipher(spawn);
891 if (IS_ERR(cipher))
892 return PTR_ERR(cipher);
893
894 ctx->child = cipher;
895
896 return 0;
897};
898
899static void cbcmac_exit_tfm(struct crypto_tfm *tfm)
900{
901 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
902 crypto_free_cipher(ctx->child);
903}
904
905static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
906{
907 struct shash_instance *inst;
908 struct crypto_alg *alg;
909 int err;
910
911 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
912 if (err)
913 return err;
914
915 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
916 CRYPTO_ALG_TYPE_MASK);
917 if (IS_ERR(alg))
918 return PTR_ERR(alg);
919
920 inst = shash_alloc_instance("cbcmac", alg);
921 err = PTR_ERR(inst);
922 if (IS_ERR(inst))
923 goto out_put_alg;
924
925 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
926 shash_crypto_instance(inst),
927 CRYPTO_ALG_TYPE_MASK);
928 if (err)
929 goto out_free_inst;
930
931 inst->alg.base.cra_priority = alg->cra_priority;
932 inst->alg.base.cra_blocksize = 1;
933
934 inst->alg.digestsize = alg->cra_blocksize;
Ard Biesheuvel5338ad72017-02-11 19:25:21 +0000935 inst->alg.descsize = ALIGN(sizeof(struct cbcmac_desc_ctx),
936 alg->cra_alignmask + 1) +
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000937 alg->cra_blocksize;
938
939 inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx);
940 inst->alg.base.cra_init = cbcmac_init_tfm;
941 inst->alg.base.cra_exit = cbcmac_exit_tfm;
942
943 inst->alg.init = crypto_cbcmac_digest_init;
944 inst->alg.update = crypto_cbcmac_digest_update;
945 inst->alg.final = crypto_cbcmac_digest_final;
946 inst->alg.setkey = crypto_cbcmac_digest_setkey;
947
948 err = shash_register_instance(tmpl, inst);
949
950out_free_inst:
951 if (err)
952 shash_free_instance(shash_crypto_instance(inst));
953
954out_put_alg:
955 crypto_mod_put(alg);
956 return err;
957}
958
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800959static struct crypto_template crypto_ccm_tmpls[] = {
960 {
961 .name = "cbcmac",
962 .create = cbcmac_create,
963 .free = shash_free_instance,
964 .module = THIS_MODULE,
965 }, {
966 .name = "ccm_base",
967 .create = crypto_ccm_base_create,
968 .module = THIS_MODULE,
969 }, {
970 .name = "ccm",
971 .create = crypto_ccm_create,
972 .module = THIS_MODULE,
973 }, {
974 .name = "rfc4309",
975 .create = crypto_rfc4309_create,
976 .module = THIS_MODULE,
977 },
Ard Biesheuvelf15f05b2017-02-03 14:49:36 +0000978};
979
Joy Latten4a49b492007-12-12 20:25:13 +0800980static int __init crypto_ccm_module_init(void)
981{
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800982 return crypto_register_templates(crypto_ccm_tmpls,
983 ARRAY_SIZE(crypto_ccm_tmpls));
Joy Latten4a49b492007-12-12 20:25:13 +0800984}
985
986static void __exit crypto_ccm_module_exit(void)
987{
Xiongfeng Wang0db19032019-01-18 13:58:12 +0800988 crypto_unregister_templates(crypto_ccm_tmpls,
989 ARRAY_SIZE(crypto_ccm_tmpls));
Joy Latten4a49b492007-12-12 20:25:13 +0800990}
991
Eric Biggersc4741b22019-04-11 21:57:42 -0700992subsys_initcall(crypto_ccm_module_init);
Joy Latten4a49b492007-12-12 20:25:13 +0800993module_exit(crypto_ccm_module_exit);
994
995MODULE_LICENSE("GPL");
996MODULE_DESCRIPTION("Counter with CBC MAC");
Kees Cook5d26a102014-11-20 17:05:53 -0800997MODULE_ALIAS_CRYPTO("ccm_base");
998MODULE_ALIAS_CRYPTO("rfc4309");
Kees Cook4943ba12014-11-24 16:32:38 -0800999MODULE_ALIAS_CRYPTO("ccm");
Ard Biesheuvelae748b92019-06-17 10:18:48 +02001000MODULE_ALIAS_CRYPTO("cbcmac");