Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 2 | /* |
| 3 | * CCM: Counter with CBC-MAC |
| 4 | * |
| 5 | * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com> |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <crypto/internal/aead.h> |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 9 | #include <crypto/internal/hash.h> |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 10 | #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 | |
| 20 | struct ccm_instance_ctx { |
| 21 | struct crypto_skcipher_spawn ctr; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 22 | struct crypto_ahash_spawn mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | struct crypto_ccm_ctx { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 26 | struct crypto_ahash *mac; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 27 | struct crypto_skcipher *ctr; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct crypto_rfc4309_ctx { |
| 31 | struct crypto_aead *child; |
| 32 | u8 nonce[3]; |
| 33 | }; |
| 34 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 35 | struct crypto_rfc4309_req_ctx { |
| 36 | struct scatterlist src[3]; |
| 37 | struct scatterlist dst[3]; |
| 38 | struct aead_request subreq; |
| 39 | }; |
| 40 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 41 | struct crypto_ccm_req_priv_ctx { |
| 42 | u8 odata[16]; |
Ard Biesheuvel | 3b30460 | 2017-02-27 15:30:56 +0000 | [diff] [blame] | 43 | u8 idata[16]; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 44 | u8 auth_tag[16]; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 45 | u32 flags; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 46 | struct scatterlist src[3]; |
| 47 | struct scatterlist dst[3]; |
Ard Biesheuvel | ebf533a | 2018-08-07 14:18:37 -0700 | [diff] [blame] | 48 | union { |
| 49 | struct ahash_request ahreq; |
| 50 | struct skcipher_request skreq; |
| 51 | }; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 52 | }; |
| 53 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 54 | struct cbcmac_tfm_ctx { |
| 55 | struct crypto_cipher *child; |
| 56 | }; |
| 57 | |
| 58 | struct cbcmac_desc_ctx { |
| 59 | unsigned int len; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 62 | static 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 | |
| 70 | static 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 | |
| 88 | static 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 Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 92 | struct crypto_skcipher *ctr = ctx->ctr; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 93 | struct crypto_ahash *mac = ctx->mac; |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 94 | int err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 95 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 96 | 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 100 | if (err) |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 101 | return err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 102 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 103 | crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK); |
| 104 | crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) & |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 105 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 106 | return crypto_ahash_setkey(mac, key, keylen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static 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 | |
| 128 | static 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 | |
| 150 | static 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 169 | static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain, |
| 170 | unsigned int cryptlen) |
| 171 | { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 172 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 173 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 174 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead); |
Ard Biesheuvel | ebf533a | 2018-08-07 14:18:37 -0700 | [diff] [blame] | 175 | struct ahash_request *ahreq = &pctx->ahreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 176 | unsigned int assoclen = req->assoclen; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 177 | struct scatterlist sg[3]; |
Ard Biesheuvel | 3b30460 | 2017-02-27 15:30:56 +0000 | [diff] [blame] | 178 | u8 *odata = pctx->odata; |
| 179 | u8 *idata = pctx->idata; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 180 | int ilen, err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 181 | |
| 182 | /* format control data for input */ |
| 183 | err = format_input(odata, req, cryptlen); |
| 184 | if (err) |
| 185 | goto out; |
| 186 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 187 | sg_init_table(sg, 3); |
| 188 | sg_set_buf(&sg[0], odata, 16); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 189 | |
| 190 | /* format associated data and compute into mac */ |
| 191 | if (assoclen) { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 192 | ilen = format_adata(idata, assoclen); |
| 193 | sg_set_buf(&sg[1], idata, ilen); |
| 194 | sg_chain(sg, 3, req->src); |
Jarod Wilson | 516280e | 2009-01-22 19:58:15 +1100 | [diff] [blame] | 195 | } else { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 196 | ilen = 0; |
| 197 | sg_chain(sg, 2, req->src); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 200 | 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 209 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 210 | /* 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 224 | out: |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | static 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 Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 236 | scatterwalk_map_and_copy(odata, req->dst, |
| 237 | req->assoclen + req->cryptlen, |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 238 | crypto_aead_authsize(aead), 1); |
| 239 | aead_request_complete(req, err); |
| 240 | } |
| 241 | |
| 242 | static 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 Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 251 | static 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 286 | static 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 Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 291 | struct skcipher_request *skreq = &pctx->skreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 292 | struct scatterlist *dst; |
| 293 | unsigned int cryptlen = req->cryptlen; |
| 294 | u8 *odata = pctx->odata; |
| 295 | u8 *iv = req->iv; |
| 296 | int err; |
| 297 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 298 | err = crypto_ccm_init_crypt(req, odata); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 299 | if (err) |
| 300 | return err; |
| 301 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 302 | err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 303 | if (err) |
| 304 | return err; |
| 305 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 306 | dst = pctx->src; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 307 | if (req->src != req->dst) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 308 | dst = pctx->dst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 309 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 310 | 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 315 | if (err) |
| 316 | return err; |
| 317 | |
| 318 | /* copy authtag to end of dst */ |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 319 | scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen, |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 320 | crypto_aead_authsize(aead), 1); |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | static 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 Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 332 | struct scatterlist *dst; |
| 333 | |
| 334 | pctx->flags = 0; |
| 335 | |
| 336 | dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 337 | |
| 338 | if (!err) { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 339 | err = crypto_ccm_auth(req, dst, cryptlen); |
James Yonan | 6bf37e5 | 2013-09-26 02:20:39 -0600 | [diff] [blame] | 340 | if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize)) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 341 | err = -EBADMSG; |
| 342 | } |
| 343 | aead_request_complete(req, err); |
| 344 | } |
| 345 | |
| 346 | static 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 Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 351 | struct skcipher_request *skreq = &pctx->skreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 352 | 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 Izard | 441f99c | 2017-10-31 15:42:35 +0100 | [diff] [blame] | 357 | u8 *iv = pctx->idata; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 358 | int err; |
| 359 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 360 | cryptlen -= authsize; |
| 361 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 362 | err = crypto_ccm_init_crypt(req, authtag); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 363 | if (err) |
| 364 | return err; |
| 365 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 366 | scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen, |
| 367 | authsize, 0); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 368 | |
| 369 | dst = pctx->src; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 370 | if (req->src != req->dst) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 371 | dst = pctx->dst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 372 | |
Romain Izard | 441f99c | 2017-10-31 15:42:35 +0100 | [diff] [blame] | 373 | memcpy(iv, req->iv, 16); |
| 374 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 375 | 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 Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 380 | if (err) |
| 381 | return err; |
| 382 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 383 | err = crypto_ccm_auth(req, sg_next(dst), cryptlen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 384 | if (err) |
| 385 | return err; |
| 386 | |
| 387 | /* verify */ |
James Yonan | 6bf37e5 | 2013-09-26 02:20:39 -0600 | [diff] [blame] | 388 | if (crypto_memneq(authtag, odata, authsize)) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 389 | return -EBADMSG; |
| 390 | |
| 391 | return err; |
| 392 | } |
| 393 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 394 | static int crypto_ccm_init_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 395 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 396 | 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 Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 399 | struct crypto_ahash *mac; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 400 | struct crypto_skcipher *ctr; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 401 | unsigned long align; |
| 402 | int err; |
| 403 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 404 | mac = crypto_spawn_ahash(&ictx->mac); |
| 405 | if (IS_ERR(mac)) |
| 406 | return PTR_ERR(mac); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 407 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 408 | ctr = crypto_spawn_skcipher(&ictx->ctr); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 409 | err = PTR_ERR(ctr); |
| 410 | if (IS_ERR(ctr)) |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 411 | goto err_free_mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 412 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 413 | ctx->mac = mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 414 | ctx->ctr = ctr; |
| 415 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 416 | align = crypto_aead_alignmask(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 417 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 418 | crypto_aead_set_reqsize( |
| 419 | tfm, |
Herbert Xu | 2c221ad | 2015-05-11 17:47:56 +0800 | [diff] [blame] | 420 | align + sizeof(struct crypto_ccm_req_priv_ctx) + |
Ard Biesheuvel | ebf533a | 2018-08-07 14:18:37 -0700 | [diff] [blame] | 421 | max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr))); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 425 | err_free_mac: |
| 426 | crypto_free_ahash(mac); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 427 | return err; |
| 428 | } |
| 429 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 430 | static void crypto_ccm_exit_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 431 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 432 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 433 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 434 | crypto_free_ahash(ctx->mac); |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 435 | crypto_free_skcipher(ctx->ctr); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 436 | } |
| 437 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 438 | static void crypto_ccm_free(struct aead_instance *inst) |
| 439 | { |
| 440 | struct ccm_instance_ctx *ctx = aead_instance_ctx(inst); |
| 441 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 442 | crypto_drop_ahash(&ctx->mac); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 443 | crypto_drop_skcipher(&ctx->ctr); |
| 444 | kfree(inst); |
| 445 | } |
| 446 | |
| 447 | static int crypto_ccm_create_common(struct crypto_template *tmpl, |
| 448 | struct rtattr **tb, |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 449 | const char *ctr_name, |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 450 | const char *mac_name) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 451 | { |
| 452 | struct crypto_attr_type *algt; |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 453 | u32 mask; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 454 | struct aead_instance *inst; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 455 | struct skcipher_alg *ctr; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 456 | struct crypto_alg *mac_alg; |
| 457 | struct hash_alg_common *mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 458 | struct ccm_instance_ctx *ictx; |
| 459 | int err; |
| 460 | |
| 461 | algt = crypto_get_attr_type(tb); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 462 | if (IS_ERR(algt)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 463 | return PTR_ERR(algt); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 464 | |
Herbert Xu | 5e4b8c1 | 2015-08-13 17:29:06 +0800 | [diff] [blame] | 465 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 466 | return -EINVAL; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 467 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 468 | mask = crypto_requires_sync(algt->type, algt->mask); |
| 469 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 470 | mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type, |
| 471 | CRYPTO_ALG_TYPE_HASH, |
| 472 | CRYPTO_ALG_TYPE_AHASH_MASK | |
| 473 | CRYPTO_ALG_ASYNC); |
| 474 | if (IS_ERR(mac_alg)) |
| 475 | return PTR_ERR(mac_alg); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 476 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 477 | mac = __crypto_hash_alg_common(mac_alg); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 478 | err = -EINVAL; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 479 | if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 || |
| 480 | mac->digestsize != 16) |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 481 | goto out_put_mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 482 | |
| 483 | inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); |
| 484 | err = -ENOMEM; |
| 485 | if (!inst) |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 486 | goto out_put_mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 487 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 488 | ictx = aead_instance_ctx(inst); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 489 | err = crypto_init_ahash_spawn(&ictx->mac, mac, |
| 490 | aead_crypto_instance(inst)); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 491 | if (err) |
| 492 | goto err_free_inst; |
| 493 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 494 | err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst), |
| 495 | ctr_name, 0, mask); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 496 | if (err) |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 497 | goto err_drop_mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 498 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 499 | ctr = crypto_spawn_skcipher_alg(&ictx->ctr); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 500 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 501 | /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 502 | err = -EINVAL; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 503 | if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || |
| 504 | crypto_skcipher_alg_ivsize(ctr) != 16 || |
| 505 | ctr->base.cra_blocksize != 1) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 506 | goto err_drop_ctr; |
| 507 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 508 | /* ctr and cbcmac must use the same underlying block cipher. */ |
| 509 | if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 510 | goto err_drop_ctr; |
| 511 | |
| 512 | err = -ENAMETOOLONG; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 513 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 514 | "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) |
| 515 | goto err_drop_ctr; |
| 516 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 517 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 518 | "ccm_base(%s,%s)", ctr->base.cra_driver_name, |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 519 | mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 520 | goto err_drop_ctr; |
| 521 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 522 | inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 523 | inst->alg.base.cra_priority = (mac->base.cra_priority + |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 524 | ctr->base.cra_priority) / 2; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 525 | inst->alg.base.cra_blocksize = 1; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 526 | inst->alg.base.cra_alignmask = mac->base.cra_alignmask | |
Ard Biesheuvel | 5ba8e2a | 2017-02-11 19:25:22 +0000 | [diff] [blame] | 527 | ctr->base.cra_alignmask; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 528 | inst->alg.ivsize = 16; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 529 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 530 | inst->alg.maxauthsize = 16; |
| 531 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx); |
| 532 | inst->alg.init = crypto_ccm_init_tfm; |
| 533 | inst->alg.exit = crypto_ccm_exit_tfm; |
| 534 | inst->alg.setkey = crypto_ccm_setkey; |
| 535 | inst->alg.setauthsize = crypto_ccm_setauthsize; |
| 536 | inst->alg.encrypt = crypto_ccm_encrypt; |
| 537 | inst->alg.decrypt = crypto_ccm_decrypt; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 538 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 539 | inst->free = crypto_ccm_free; |
| 540 | |
| 541 | err = aead_register_instance(tmpl, inst); |
| 542 | if (err) |
| 543 | goto err_drop_ctr; |
| 544 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 545 | out_put_mac: |
| 546 | crypto_mod_put(mac_alg); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 547 | return err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 548 | |
| 549 | err_drop_ctr: |
| 550 | crypto_drop_skcipher(&ictx->ctr); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 551 | err_drop_mac: |
| 552 | crypto_drop_ahash(&ictx->mac); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 553 | err_free_inst: |
| 554 | kfree(inst); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 555 | goto out_put_mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 556 | } |
| 557 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 558 | static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 559 | { |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 560 | const char *cipher_name; |
| 561 | char ctr_name[CRYPTO_MAX_ALG_NAME]; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 562 | char mac_name[CRYPTO_MAX_ALG_NAME]; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 563 | |
| 564 | cipher_name = crypto_attr_alg_name(tb[1]); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 565 | if (IS_ERR(cipher_name)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 566 | return PTR_ERR(cipher_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 567 | |
| 568 | if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", |
| 569 | cipher_name) >= CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 570 | return -ENAMETOOLONG; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 571 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 572 | if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)", |
| 573 | cipher_name) >= CRYPTO_MAX_ALG_NAME) |
| 574 | return -ENAMETOOLONG; |
| 575 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 576 | return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 577 | } |
| 578 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 579 | static int crypto_ccm_base_create(struct crypto_template *tmpl, |
| 580 | struct rtattr **tb) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 581 | { |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 582 | const char *ctr_name; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 583 | const char *mac_name; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 584 | |
| 585 | ctr_name = crypto_attr_alg_name(tb[1]); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 586 | if (IS_ERR(ctr_name)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 587 | return PTR_ERR(ctr_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 588 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 589 | mac_name = crypto_attr_alg_name(tb[2]); |
| 590 | if (IS_ERR(mac_name)) |
| 591 | return PTR_ERR(mac_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 592 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 593 | return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 596 | static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key, |
| 597 | unsigned int keylen) |
| 598 | { |
| 599 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent); |
| 600 | struct crypto_aead *child = ctx->child; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 601 | |
| 602 | if (keylen < 3) |
| 603 | return -EINVAL; |
| 604 | |
| 605 | keylen -= 3; |
| 606 | memcpy(ctx->nonce, key + keylen, 3); |
| 607 | |
| 608 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 609 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 610 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 611 | return crypto_aead_setkey(child, key, keylen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static int crypto_rfc4309_setauthsize(struct crypto_aead *parent, |
| 615 | unsigned int authsize) |
| 616 | { |
| 617 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent); |
| 618 | |
| 619 | switch (authsize) { |
| 620 | case 8: |
| 621 | case 12: |
| 622 | case 16: |
| 623 | break; |
| 624 | default: |
| 625 | return -EINVAL; |
| 626 | } |
| 627 | |
| 628 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 629 | } |
| 630 | |
| 631 | static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req) |
| 632 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 633 | struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req); |
| 634 | struct aead_request *subreq = &rctx->subreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 635 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 636 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead); |
| 637 | struct crypto_aead *child = ctx->child; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 638 | struct scatterlist *sg; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 639 | u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child), |
| 640 | crypto_aead_alignmask(child) + 1); |
| 641 | |
| 642 | /* L' */ |
| 643 | iv[0] = 3; |
| 644 | |
| 645 | memcpy(iv + 1, ctx->nonce, 3); |
| 646 | memcpy(iv + 4, req->iv, 8); |
| 647 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 648 | scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0); |
| 649 | |
| 650 | sg_init_table(rctx->src, 3); |
| 651 | sg_set_buf(rctx->src, iv + 16, req->assoclen - 8); |
| 652 | sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen); |
| 653 | if (sg != rctx->src + 1) |
| 654 | sg_chain(rctx->src, 2, sg); |
| 655 | |
| 656 | if (req->src != req->dst) { |
| 657 | sg_init_table(rctx->dst, 3); |
| 658 | sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8); |
| 659 | sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen); |
| 660 | if (sg != rctx->dst + 1) |
| 661 | sg_chain(rctx->dst, 2, sg); |
| 662 | } |
| 663 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 664 | aead_request_set_tfm(subreq, child); |
| 665 | aead_request_set_callback(subreq, req->base.flags, req->base.complete, |
| 666 | req->base.data); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 667 | aead_request_set_crypt(subreq, rctx->src, |
| 668 | req->src == req->dst ? rctx->src : rctx->dst, |
| 669 | req->cryptlen, iv); |
| 670 | aead_request_set_ad(subreq, req->assoclen - 8); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 671 | |
| 672 | return subreq; |
| 673 | } |
| 674 | |
| 675 | static int crypto_rfc4309_encrypt(struct aead_request *req) |
| 676 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 677 | if (req->assoclen != 16 && req->assoclen != 20) |
| 678 | return -EINVAL; |
| 679 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 680 | req = crypto_rfc4309_crypt(req); |
| 681 | |
| 682 | return crypto_aead_encrypt(req); |
| 683 | } |
| 684 | |
| 685 | static int crypto_rfc4309_decrypt(struct aead_request *req) |
| 686 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 687 | if (req->assoclen != 16 && req->assoclen != 20) |
| 688 | return -EINVAL; |
| 689 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 690 | req = crypto_rfc4309_crypt(req); |
| 691 | |
| 692 | return crypto_aead_decrypt(req); |
| 693 | } |
| 694 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 695 | static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 696 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 697 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 698 | struct crypto_aead_spawn *spawn = aead_instance_ctx(inst); |
| 699 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 700 | struct crypto_aead *aead; |
| 701 | unsigned long align; |
| 702 | |
| 703 | aead = crypto_spawn_aead(spawn); |
| 704 | if (IS_ERR(aead)) |
| 705 | return PTR_ERR(aead); |
| 706 | |
| 707 | ctx->child = aead; |
| 708 | |
| 709 | align = crypto_aead_alignmask(aead); |
| 710 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 711 | crypto_aead_set_reqsize( |
| 712 | tfm, |
| 713 | sizeof(struct crypto_rfc4309_req_ctx) + |
Herbert Xu | 2c221ad | 2015-05-11 17:47:56 +0800 | [diff] [blame] | 714 | ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) + |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 715 | align + 32); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 720 | static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 721 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 722 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 723 | |
| 724 | crypto_free_aead(ctx->child); |
| 725 | } |
| 726 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 727 | static void crypto_rfc4309_free(struct aead_instance *inst) |
| 728 | { |
| 729 | crypto_drop_aead(aead_instance_ctx(inst)); |
| 730 | kfree(inst); |
| 731 | } |
| 732 | |
| 733 | static int crypto_rfc4309_create(struct crypto_template *tmpl, |
| 734 | struct rtattr **tb) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 735 | { |
| 736 | struct crypto_attr_type *algt; |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 737 | u32 mask; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 738 | struct aead_instance *inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 739 | struct crypto_aead_spawn *spawn; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 740 | struct aead_alg *alg; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 741 | const char *ccm_name; |
| 742 | int err; |
| 743 | |
| 744 | algt = crypto_get_attr_type(tb); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 745 | if (IS_ERR(algt)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 746 | return PTR_ERR(algt); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 747 | |
Herbert Xu | 5e4b8c1 | 2015-08-13 17:29:06 +0800 | [diff] [blame] | 748 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 749 | return -EINVAL; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 750 | |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 751 | mask = crypto_requires_sync(algt->type, algt->mask); |
| 752 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 753 | ccm_name = crypto_attr_alg_name(tb[1]); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 754 | if (IS_ERR(ccm_name)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 755 | return PTR_ERR(ccm_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 756 | |
| 757 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 758 | if (!inst) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 759 | return -ENOMEM; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 760 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 761 | spawn = aead_instance_ctx(inst); |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 762 | err = crypto_grab_aead(spawn, aead_crypto_instance(inst), |
| 763 | ccm_name, 0, mask); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 764 | if (err) |
| 765 | goto out_free_inst; |
| 766 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 767 | alg = crypto_spawn_aead_alg(spawn); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 768 | |
| 769 | err = -EINVAL; |
| 770 | |
| 771 | /* We only support 16-byte blocks. */ |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 772 | if (crypto_aead_alg_ivsize(alg) != 16) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 773 | goto out_drop_alg; |
| 774 | |
| 775 | /* Not a stream cipher? */ |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 776 | if (alg->base.cra_blocksize != 1) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 777 | goto out_drop_alg; |
| 778 | |
| 779 | err = -ENAMETOOLONG; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 780 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 781 | "rfc4309(%s)", alg->base.cra_name) >= |
| 782 | CRYPTO_MAX_ALG_NAME || |
| 783 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 784 | "rfc4309(%s)", alg->base.cra_driver_name) >= |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 785 | CRYPTO_MAX_ALG_NAME) |
| 786 | goto out_drop_alg; |
| 787 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 788 | inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 789 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 790 | inst->alg.base.cra_blocksize = 1; |
| 791 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 792 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 793 | inst->alg.ivsize = 8; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 794 | inst->alg.chunksize = crypto_aead_alg_chunksize(alg); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 795 | inst->alg.maxauthsize = 16; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 796 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 797 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 798 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 799 | inst->alg.init = crypto_rfc4309_init_tfm; |
| 800 | inst->alg.exit = crypto_rfc4309_exit_tfm; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 801 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 802 | inst->alg.setkey = crypto_rfc4309_setkey; |
| 803 | inst->alg.setauthsize = crypto_rfc4309_setauthsize; |
| 804 | inst->alg.encrypt = crypto_rfc4309_encrypt; |
| 805 | inst->alg.decrypt = crypto_rfc4309_decrypt; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 806 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 807 | inst->free = crypto_rfc4309_free; |
| 808 | |
| 809 | err = aead_register_instance(tmpl, inst); |
| 810 | if (err) |
| 811 | goto out_drop_alg; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 812 | |
| 813 | out: |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 814 | return err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 815 | |
| 816 | out_drop_alg: |
| 817 | crypto_drop_aead(spawn); |
| 818 | out_free_inst: |
| 819 | kfree(inst); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 820 | goto out; |
| 821 | } |
| 822 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 823 | static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent, |
| 824 | const u8 *inkey, unsigned int keylen) |
| 825 | { |
| 826 | struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent); |
| 827 | |
| 828 | return crypto_cipher_setkey(ctx->child, inkey, keylen); |
| 829 | } |
| 830 | |
| 831 | static int crypto_cbcmac_digest_init(struct shash_desc *pdesc) |
| 832 | { |
| 833 | struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc); |
| 834 | int bs = crypto_shash_digestsize(pdesc->tfm); |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 835 | u8 *dg = (u8 *)ctx + crypto_shash_descsize(pdesc->tfm) - bs; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 836 | |
| 837 | ctx->len = 0; |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 838 | memset(dg, 0, bs); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p, |
| 844 | unsigned int len) |
| 845 | { |
| 846 | struct crypto_shash *parent = pdesc->tfm; |
| 847 | struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent); |
| 848 | struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc); |
| 849 | struct crypto_cipher *tfm = tctx->child; |
| 850 | int bs = crypto_shash_digestsize(parent); |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 851 | u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 852 | |
| 853 | while (len > 0) { |
| 854 | unsigned int l = min(len, bs - ctx->len); |
| 855 | |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 856 | crypto_xor(dg + ctx->len, p, l); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 857 | ctx->len +=l; |
| 858 | len -= l; |
| 859 | p += l; |
| 860 | |
| 861 | if (ctx->len == bs) { |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 862 | crypto_cipher_encrypt_one(tfm, dg, dg); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 863 | ctx->len = 0; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out) |
| 871 | { |
| 872 | struct crypto_shash *parent = pdesc->tfm; |
| 873 | struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent); |
| 874 | struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc); |
| 875 | struct crypto_cipher *tfm = tctx->child; |
| 876 | int bs = crypto_shash_digestsize(parent); |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 877 | u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 878 | |
| 879 | if (ctx->len) |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 880 | crypto_cipher_encrypt_one(tfm, dg, dg); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 881 | |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 882 | memcpy(out, dg, bs); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static int cbcmac_init_tfm(struct crypto_tfm *tfm) |
| 887 | { |
| 888 | struct crypto_cipher *cipher; |
| 889 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 890 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); |
| 891 | struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 892 | |
| 893 | cipher = crypto_spawn_cipher(spawn); |
| 894 | if (IS_ERR(cipher)) |
| 895 | return PTR_ERR(cipher); |
| 896 | |
| 897 | ctx->child = cipher; |
| 898 | |
| 899 | return 0; |
| 900 | }; |
| 901 | |
| 902 | static void cbcmac_exit_tfm(struct crypto_tfm *tfm) |
| 903 | { |
| 904 | struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 905 | crypto_free_cipher(ctx->child); |
| 906 | } |
| 907 | |
| 908 | static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 909 | { |
| 910 | struct shash_instance *inst; |
| 911 | struct crypto_alg *alg; |
| 912 | int err; |
| 913 | |
| 914 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
| 915 | if (err) |
| 916 | return err; |
| 917 | |
| 918 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, |
| 919 | CRYPTO_ALG_TYPE_MASK); |
| 920 | if (IS_ERR(alg)) |
| 921 | return PTR_ERR(alg); |
| 922 | |
| 923 | inst = shash_alloc_instance("cbcmac", alg); |
| 924 | err = PTR_ERR(inst); |
| 925 | if (IS_ERR(inst)) |
| 926 | goto out_put_alg; |
| 927 | |
| 928 | err = crypto_init_spawn(shash_instance_ctx(inst), alg, |
| 929 | shash_crypto_instance(inst), |
| 930 | CRYPTO_ALG_TYPE_MASK); |
| 931 | if (err) |
| 932 | goto out_free_inst; |
| 933 | |
| 934 | inst->alg.base.cra_priority = alg->cra_priority; |
| 935 | inst->alg.base.cra_blocksize = 1; |
| 936 | |
| 937 | inst->alg.digestsize = alg->cra_blocksize; |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 938 | inst->alg.descsize = ALIGN(sizeof(struct cbcmac_desc_ctx), |
| 939 | alg->cra_alignmask + 1) + |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 940 | alg->cra_blocksize; |
| 941 | |
| 942 | inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx); |
| 943 | inst->alg.base.cra_init = cbcmac_init_tfm; |
| 944 | inst->alg.base.cra_exit = cbcmac_exit_tfm; |
| 945 | |
| 946 | inst->alg.init = crypto_cbcmac_digest_init; |
| 947 | inst->alg.update = crypto_cbcmac_digest_update; |
| 948 | inst->alg.final = crypto_cbcmac_digest_final; |
| 949 | inst->alg.setkey = crypto_cbcmac_digest_setkey; |
| 950 | |
| 951 | err = shash_register_instance(tmpl, inst); |
| 952 | |
| 953 | out_free_inst: |
| 954 | if (err) |
| 955 | shash_free_instance(shash_crypto_instance(inst)); |
| 956 | |
| 957 | out_put_alg: |
| 958 | crypto_mod_put(alg); |
| 959 | return err; |
| 960 | } |
| 961 | |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 962 | static struct crypto_template crypto_ccm_tmpls[] = { |
| 963 | { |
| 964 | .name = "cbcmac", |
| 965 | .create = cbcmac_create, |
| 966 | .free = shash_free_instance, |
| 967 | .module = THIS_MODULE, |
| 968 | }, { |
| 969 | .name = "ccm_base", |
| 970 | .create = crypto_ccm_base_create, |
| 971 | .module = THIS_MODULE, |
| 972 | }, { |
| 973 | .name = "ccm", |
| 974 | .create = crypto_ccm_create, |
| 975 | .module = THIS_MODULE, |
| 976 | }, { |
| 977 | .name = "rfc4309", |
| 978 | .create = crypto_rfc4309_create, |
| 979 | .module = THIS_MODULE, |
| 980 | }, |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 981 | }; |
| 982 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 983 | static int __init crypto_ccm_module_init(void) |
| 984 | { |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 985 | return crypto_register_templates(crypto_ccm_tmpls, |
| 986 | ARRAY_SIZE(crypto_ccm_tmpls)); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | static void __exit crypto_ccm_module_exit(void) |
| 990 | { |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 991 | crypto_unregister_templates(crypto_ccm_tmpls, |
| 992 | ARRAY_SIZE(crypto_ccm_tmpls)); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 993 | } |
| 994 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 995 | subsys_initcall(crypto_ccm_module_init); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 996 | module_exit(crypto_ccm_module_exit); |
| 997 | |
| 998 | MODULE_LICENSE("GPL"); |
| 999 | MODULE_DESCRIPTION("Counter with CBC MAC"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 1000 | MODULE_ALIAS_CRYPTO("ccm_base"); |
| 1001 | MODULE_ALIAS_CRYPTO("rfc4309"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 1002 | MODULE_ALIAS_CRYPTO("ccm"); |
Ard Biesheuvel | ae748b9 | 2019-06-17 10:18:48 +0200 | [diff] [blame] | 1003 | MODULE_ALIAS_CRYPTO("cbcmac"); |