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 | 0eb76ba | 2020-12-11 13:27:15 +0100 | [diff] [blame] | 9 | #include <crypto/internal/cipher.h> |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 10 | #include <crypto/internal/hash.h> |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 11 | #include <crypto/internal/skcipher.h> |
| 12 | #include <crypto/scatterwalk.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 19 | struct ccm_instance_ctx { |
| 20 | struct crypto_skcipher_spawn ctr; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 21 | struct crypto_ahash_spawn mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | struct crypto_ccm_ctx { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 25 | struct crypto_ahash *mac; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 26 | struct crypto_skcipher *ctr; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct crypto_rfc4309_ctx { |
| 30 | struct crypto_aead *child; |
| 31 | u8 nonce[3]; |
| 32 | }; |
| 33 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 34 | struct crypto_rfc4309_req_ctx { |
| 35 | struct scatterlist src[3]; |
| 36 | struct scatterlist dst[3]; |
| 37 | struct aead_request subreq; |
| 38 | }; |
| 39 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 40 | struct crypto_ccm_req_priv_ctx { |
| 41 | u8 odata[16]; |
Ard Biesheuvel | 3b30460 | 2017-02-27 15:30:56 +0000 | [diff] [blame] | 42 | u8 idata[16]; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 43 | u8 auth_tag[16]; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 44 | u32 flags; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 45 | struct scatterlist src[3]; |
| 46 | struct scatterlist dst[3]; |
Ard Biesheuvel | ebf533a | 2018-08-07 14:18:37 -0700 | [diff] [blame] | 47 | union { |
| 48 | struct ahash_request ahreq; |
| 49 | struct skcipher_request skreq; |
| 50 | }; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 53 | struct cbcmac_tfm_ctx { |
| 54 | struct crypto_cipher *child; |
| 55 | }; |
| 56 | |
| 57 | struct cbcmac_desc_ctx { |
| 58 | unsigned int len; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 61 | static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx( |
| 62 | struct aead_request *req) |
| 63 | { |
| 64 | unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req)); |
| 65 | |
| 66 | return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1); |
| 67 | } |
| 68 | |
| 69 | static int set_msg_len(u8 *block, unsigned int msglen, int csize) |
| 70 | { |
| 71 | __be32 data; |
| 72 | |
| 73 | memset(block, 0, csize); |
| 74 | block += csize; |
| 75 | |
| 76 | if (csize >= 4) |
| 77 | csize = 4; |
| 78 | else if (msglen > (1 << (8 * csize))) |
| 79 | return -EOVERFLOW; |
| 80 | |
| 81 | data = cpu_to_be32(msglen); |
| 82 | memcpy(block - csize, (u8 *)&data + 4 - csize, csize); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key, |
| 88 | unsigned int keylen) |
| 89 | { |
| 90 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 91 | struct crypto_skcipher *ctr = ctx->ctr; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 92 | struct crypto_ahash *mac = ctx->mac; |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 93 | int err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 94 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 95 | crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); |
| 96 | crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
| 97 | CRYPTO_TFM_REQ_MASK); |
| 98 | err = crypto_skcipher_setkey(ctr, key, keylen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 99 | if (err) |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 100 | return err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 101 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 102 | crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK); |
| 103 | crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) & |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 104 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 105 | return crypto_ahash_setkey(mac, key, keylen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int crypto_ccm_setauthsize(struct crypto_aead *tfm, |
| 109 | unsigned int authsize) |
| 110 | { |
| 111 | switch (authsize) { |
| 112 | case 4: |
| 113 | case 6: |
| 114 | case 8: |
| 115 | case 10: |
| 116 | case 12: |
| 117 | case 14: |
| 118 | case 16: |
| 119 | break; |
| 120 | default: |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int format_input(u8 *info, struct aead_request *req, |
| 128 | unsigned int cryptlen) |
| 129 | { |
| 130 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 131 | unsigned int lp = req->iv[0]; |
| 132 | unsigned int l = lp + 1; |
| 133 | unsigned int m; |
| 134 | |
| 135 | m = crypto_aead_authsize(aead); |
| 136 | |
| 137 | memcpy(info, req->iv, 16); |
| 138 | |
| 139 | /* format control info per RFC 3610 and |
| 140 | * NIST Special Publication 800-38C |
| 141 | */ |
| 142 | *info |= (8 * ((m - 2) / 2)); |
| 143 | if (req->assoclen) |
| 144 | *info |= 64; |
| 145 | |
| 146 | return set_msg_len(info + 16 - l, cryptlen, l); |
| 147 | } |
| 148 | |
| 149 | static int format_adata(u8 *adata, unsigned int a) |
| 150 | { |
| 151 | int len = 0; |
| 152 | |
| 153 | /* add control info for associated data |
| 154 | * RFC 3610 and NIST Special Publication 800-38C |
| 155 | */ |
| 156 | if (a < 65280) { |
| 157 | *(__be16 *)adata = cpu_to_be16(a); |
| 158 | len = 2; |
| 159 | } else { |
| 160 | *(__be16 *)adata = cpu_to_be16(0xfffe); |
| 161 | *(__be32 *)&adata[2] = cpu_to_be32(a); |
| 162 | len = 6; |
| 163 | } |
| 164 | |
| 165 | return len; |
| 166 | } |
| 167 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 168 | static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain, |
| 169 | unsigned int cryptlen) |
| 170 | { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 171 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 172 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 173 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead); |
Ard Biesheuvel | ebf533a | 2018-08-07 14:18:37 -0700 | [diff] [blame] | 174 | struct ahash_request *ahreq = &pctx->ahreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 175 | unsigned int assoclen = req->assoclen; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 176 | struct scatterlist sg[3]; |
Ard Biesheuvel | 3b30460 | 2017-02-27 15:30:56 +0000 | [diff] [blame] | 177 | u8 *odata = pctx->odata; |
| 178 | u8 *idata = pctx->idata; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 179 | int ilen, err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 180 | |
| 181 | /* format control data for input */ |
| 182 | err = format_input(odata, req, cryptlen); |
| 183 | if (err) |
| 184 | goto out; |
| 185 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 186 | sg_init_table(sg, 3); |
| 187 | sg_set_buf(&sg[0], odata, 16); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 188 | |
| 189 | /* format associated data and compute into mac */ |
| 190 | if (assoclen) { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 191 | ilen = format_adata(idata, assoclen); |
| 192 | sg_set_buf(&sg[1], idata, ilen); |
| 193 | sg_chain(sg, 3, req->src); |
Jarod Wilson | 516280e | 2009-01-22 19:58:15 +1100 | [diff] [blame] | 194 | } else { |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 195 | ilen = 0; |
| 196 | sg_chain(sg, 2, req->src); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 197 | } |
| 198 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 199 | ahash_request_set_tfm(ahreq, ctx->mac); |
| 200 | ahash_request_set_callback(ahreq, pctx->flags, NULL, NULL); |
| 201 | ahash_request_set_crypt(ahreq, sg, NULL, assoclen + ilen + 16); |
| 202 | err = crypto_ahash_init(ahreq); |
| 203 | if (err) |
| 204 | goto out; |
| 205 | err = crypto_ahash_update(ahreq); |
| 206 | if (err) |
| 207 | goto out; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 208 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 209 | /* we need to pad the MAC input to a round multiple of the block size */ |
| 210 | ilen = 16 - (assoclen + ilen) % 16; |
| 211 | if (ilen < 16) { |
| 212 | memset(idata, 0, ilen); |
| 213 | sg_init_table(sg, 2); |
| 214 | sg_set_buf(&sg[0], idata, ilen); |
| 215 | if (plain) |
| 216 | sg_chain(sg, 2, plain); |
| 217 | plain = sg; |
| 218 | cryptlen += ilen; |
| 219 | } |
| 220 | |
| 221 | ahash_request_set_crypt(ahreq, plain, pctx->odata, cryptlen); |
| 222 | err = crypto_ahash_finup(ahreq); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 223 | out: |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err) |
| 228 | { |
| 229 | struct aead_request *req = areq->data; |
| 230 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 231 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
| 232 | u8 *odata = pctx->odata; |
| 233 | |
| 234 | if (!err) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 235 | scatterwalk_map_and_copy(odata, req->dst, |
| 236 | req->assoclen + req->cryptlen, |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 237 | crypto_aead_authsize(aead), 1); |
| 238 | aead_request_complete(req, err); |
| 239 | } |
| 240 | |
| 241 | static inline int crypto_ccm_check_iv(const u8 *iv) |
| 242 | { |
| 243 | /* 2 <= L <= 8, so 1 <= L' <= 7. */ |
| 244 | if (1 > iv[0] || iv[0] > 7) |
| 245 | return -EINVAL; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 250 | static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag) |
| 251 | { |
| 252 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
| 253 | struct scatterlist *sg; |
| 254 | u8 *iv = req->iv; |
| 255 | int err; |
| 256 | |
| 257 | err = crypto_ccm_check_iv(iv); |
| 258 | if (err) |
| 259 | return err; |
| 260 | |
| 261 | pctx->flags = aead_request_flags(req); |
| 262 | |
| 263 | /* Note: rfc 3610 and NIST 800-38C require counter of |
| 264 | * zero to encrypt auth tag. |
| 265 | */ |
| 266 | memset(iv + 15 - iv[0], 0, iv[0] + 1); |
| 267 | |
| 268 | sg_init_table(pctx->src, 3); |
| 269 | sg_set_buf(pctx->src, tag, 16); |
| 270 | sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen); |
| 271 | if (sg != pctx->src + 1) |
| 272 | sg_chain(pctx->src, 2, sg); |
| 273 | |
| 274 | if (req->src != req->dst) { |
| 275 | sg_init_table(pctx->dst, 3); |
| 276 | sg_set_buf(pctx->dst, tag, 16); |
| 277 | sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen); |
| 278 | if (sg != pctx->dst + 1) |
| 279 | sg_chain(pctx->dst, 2, sg); |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 285 | static int crypto_ccm_encrypt(struct aead_request *req) |
| 286 | { |
| 287 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 288 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead); |
| 289 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 290 | struct skcipher_request *skreq = &pctx->skreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 291 | struct scatterlist *dst; |
| 292 | unsigned int cryptlen = req->cryptlen; |
| 293 | u8 *odata = pctx->odata; |
| 294 | u8 *iv = req->iv; |
| 295 | int err; |
| 296 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 297 | err = crypto_ccm_init_crypt(req, odata); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 298 | if (err) |
| 299 | return err; |
| 300 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 301 | err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 302 | if (err) |
| 303 | return err; |
| 304 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 305 | dst = pctx->src; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 306 | if (req->src != req->dst) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 307 | dst = pctx->dst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 308 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 309 | skcipher_request_set_tfm(skreq, ctx->ctr); |
| 310 | skcipher_request_set_callback(skreq, pctx->flags, |
| 311 | crypto_ccm_encrypt_done, req); |
| 312 | skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv); |
| 313 | err = crypto_skcipher_encrypt(skreq); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 314 | if (err) |
| 315 | return err; |
| 316 | |
| 317 | /* copy authtag to end of dst */ |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 318 | scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen, |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 319 | crypto_aead_authsize(aead), 1); |
| 320 | return err; |
| 321 | } |
| 322 | |
| 323 | static void crypto_ccm_decrypt_done(struct crypto_async_request *areq, |
| 324 | int err) |
| 325 | { |
| 326 | struct aead_request *req = areq->data; |
| 327 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
| 328 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 329 | unsigned int authsize = crypto_aead_authsize(aead); |
| 330 | unsigned int cryptlen = req->cryptlen - authsize; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 331 | struct scatterlist *dst; |
| 332 | |
| 333 | pctx->flags = 0; |
| 334 | |
| 335 | dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 336 | |
| 337 | if (!err) { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 338 | err = crypto_ccm_auth(req, dst, cryptlen); |
James Yonan | 6bf37e5 | 2013-09-26 02:20:39 -0600 | [diff] [blame] | 339 | if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize)) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 340 | err = -EBADMSG; |
| 341 | } |
| 342 | aead_request_complete(req, err); |
| 343 | } |
| 344 | |
| 345 | static int crypto_ccm_decrypt(struct aead_request *req) |
| 346 | { |
| 347 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 348 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead); |
| 349 | struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 350 | struct skcipher_request *skreq = &pctx->skreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 351 | struct scatterlist *dst; |
| 352 | unsigned int authsize = crypto_aead_authsize(aead); |
| 353 | unsigned int cryptlen = req->cryptlen; |
| 354 | u8 *authtag = pctx->auth_tag; |
| 355 | u8 *odata = pctx->odata; |
Romain Izard | 441f99c | 2017-10-31 15:42:35 +0100 | [diff] [blame] | 356 | u8 *iv = pctx->idata; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 357 | int err; |
| 358 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 359 | cryptlen -= authsize; |
| 360 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 361 | err = crypto_ccm_init_crypt(req, authtag); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 362 | if (err) |
| 363 | return err; |
| 364 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 365 | scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen, |
| 366 | authsize, 0); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 367 | |
| 368 | dst = pctx->src; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 369 | if (req->src != req->dst) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 370 | dst = pctx->dst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 371 | |
Romain Izard | 441f99c | 2017-10-31 15:42:35 +0100 | [diff] [blame] | 372 | memcpy(iv, req->iv, 16); |
| 373 | |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 374 | skcipher_request_set_tfm(skreq, ctx->ctr); |
| 375 | skcipher_request_set_callback(skreq, pctx->flags, |
| 376 | crypto_ccm_decrypt_done, req); |
| 377 | skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv); |
| 378 | err = crypto_skcipher_decrypt(skreq); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 379 | if (err) |
| 380 | return err; |
| 381 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 382 | err = crypto_ccm_auth(req, sg_next(dst), cryptlen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 383 | if (err) |
| 384 | return err; |
| 385 | |
| 386 | /* verify */ |
James Yonan | 6bf37e5 | 2013-09-26 02:20:39 -0600 | [diff] [blame] | 387 | if (crypto_memneq(authtag, odata, authsize)) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 388 | return -EBADMSG; |
| 389 | |
| 390 | return err; |
| 391 | } |
| 392 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 393 | static int crypto_ccm_init_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 394 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 395 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 396 | struct ccm_instance_ctx *ictx = aead_instance_ctx(inst); |
| 397 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 398 | struct crypto_ahash *mac; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 399 | struct crypto_skcipher *ctr; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 400 | unsigned long align; |
| 401 | int err; |
| 402 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 403 | mac = crypto_spawn_ahash(&ictx->mac); |
| 404 | if (IS_ERR(mac)) |
| 405 | return PTR_ERR(mac); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 406 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 407 | ctr = crypto_spawn_skcipher(&ictx->ctr); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 408 | err = PTR_ERR(ctr); |
| 409 | if (IS_ERR(ctr)) |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 410 | goto err_free_mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 411 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 412 | ctx->mac = mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 413 | ctx->ctr = ctr; |
| 414 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 415 | align = crypto_aead_alignmask(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 416 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 417 | crypto_aead_set_reqsize( |
| 418 | tfm, |
Herbert Xu | 2c221ad | 2015-05-11 17:47:56 +0800 | [diff] [blame] | 419 | align + sizeof(struct crypto_ccm_req_priv_ctx) + |
Ard Biesheuvel | ebf533a | 2018-08-07 14:18:37 -0700 | [diff] [blame] | 420 | max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr))); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 421 | |
| 422 | return 0; |
| 423 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 424 | err_free_mac: |
| 425 | crypto_free_ahash(mac); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 426 | return err; |
| 427 | } |
| 428 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 429 | static void crypto_ccm_exit_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 430 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 431 | struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 432 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 433 | crypto_free_ahash(ctx->mac); |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 434 | crypto_free_skcipher(ctx->ctr); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 435 | } |
| 436 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 437 | static void crypto_ccm_free(struct aead_instance *inst) |
| 438 | { |
| 439 | struct ccm_instance_ctx *ctx = aead_instance_ctx(inst); |
| 440 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 441 | crypto_drop_ahash(&ctx->mac); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 442 | crypto_drop_skcipher(&ctx->ctr); |
| 443 | kfree(inst); |
| 444 | } |
| 445 | |
| 446 | static int crypto_ccm_create_common(struct crypto_template *tmpl, |
| 447 | struct rtattr **tb, |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 448 | const char *ctr_name, |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 449 | const char *mac_name) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 450 | { |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 451 | u32 mask; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 452 | struct aead_instance *inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 453 | struct ccm_instance_ctx *ictx; |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 454 | struct skcipher_alg *ctr; |
| 455 | struct hash_alg_common *mac; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 456 | int err; |
| 457 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 458 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
| 459 | if (err) |
| 460 | return err; |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 461 | |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 462 | inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); |
| 463 | if (!inst) |
| 464 | return -ENOMEM; |
| 465 | ictx = aead_instance_ctx(inst); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 466 | |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 467 | err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst), |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 468 | mac_name, 0, mask | CRYPTO_ALG_ASYNC); |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 469 | if (err) |
| 470 | goto err_free_inst; |
| 471 | mac = crypto_spawn_ahash_alg(&ictx->mac); |
| 472 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 473 | err = -EINVAL; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 474 | if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 || |
| 475 | mac->digestsize != 16) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 476 | goto err_free_inst; |
| 477 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 478 | err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst), |
| 479 | ctr_name, 0, mask); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 480 | if (err) |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 481 | goto err_free_inst; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 482 | ctr = crypto_spawn_skcipher_alg(&ictx->ctr); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 483 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 484 | /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 485 | err = -EINVAL; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 486 | if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || |
| 487 | crypto_skcipher_alg_ivsize(ctr) != 16 || |
| 488 | ctr->base.cra_blocksize != 1) |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 489 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 490 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 491 | /* ctr and cbcmac must use the same underlying block cipher. */ |
| 492 | if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0) |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 493 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 494 | |
| 495 | err = -ENAMETOOLONG; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 496 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 497 | "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 498 | goto err_free_inst; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 499 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 500 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 501 | "ccm_base(%s,%s)", ctr->base.cra_driver_name, |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 502 | mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 503 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 504 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 505 | inst->alg.base.cra_priority = (mac->base.cra_priority + |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 506 | ctr->base.cra_priority) / 2; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 507 | inst->alg.base.cra_blocksize = 1; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 508 | inst->alg.base.cra_alignmask = mac->base.cra_alignmask | |
Ard Biesheuvel | 5ba8e2a | 2017-02-11 19:25:22 +0000 | [diff] [blame] | 509 | ctr->base.cra_alignmask; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 510 | inst->alg.ivsize = 16; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 511 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 512 | inst->alg.maxauthsize = 16; |
| 513 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx); |
| 514 | inst->alg.init = crypto_ccm_init_tfm; |
| 515 | inst->alg.exit = crypto_ccm_exit_tfm; |
| 516 | inst->alg.setkey = crypto_ccm_setkey; |
| 517 | inst->alg.setauthsize = crypto_ccm_setauthsize; |
| 518 | inst->alg.encrypt = crypto_ccm_encrypt; |
| 519 | inst->alg.decrypt = crypto_ccm_decrypt; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 520 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 521 | inst->free = crypto_ccm_free; |
| 522 | |
| 523 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 524 | if (err) { |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 525 | err_free_inst: |
Eric Biggers | 05b3bbb | 2020-01-02 19:58:58 -0800 | [diff] [blame] | 526 | crypto_ccm_free(inst); |
| 527 | } |
| 528 | return err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 529 | } |
| 530 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 531 | static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 532 | { |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 533 | const char *cipher_name; |
| 534 | char ctr_name[CRYPTO_MAX_ALG_NAME]; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 535 | char mac_name[CRYPTO_MAX_ALG_NAME]; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 536 | |
| 537 | cipher_name = crypto_attr_alg_name(tb[1]); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 538 | if (IS_ERR(cipher_name)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 539 | return PTR_ERR(cipher_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 540 | |
| 541 | if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", |
| 542 | cipher_name) >= CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 543 | return -ENAMETOOLONG; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 544 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 545 | if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)", |
| 546 | cipher_name) >= CRYPTO_MAX_ALG_NAME) |
| 547 | return -ENAMETOOLONG; |
| 548 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 549 | return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 550 | } |
| 551 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 552 | static int crypto_ccm_base_create(struct crypto_template *tmpl, |
| 553 | struct rtattr **tb) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 554 | { |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 555 | const char *ctr_name; |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 556 | const char *mac_name; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 557 | |
| 558 | ctr_name = crypto_attr_alg_name(tb[1]); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 559 | if (IS_ERR(ctr_name)) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 560 | return PTR_ERR(ctr_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 561 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 562 | mac_name = crypto_attr_alg_name(tb[2]); |
| 563 | if (IS_ERR(mac_name)) |
| 564 | return PTR_ERR(mac_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 565 | |
Eric Biggers | 6a1faa4 | 2019-04-18 14:44:27 -0700 | [diff] [blame] | 566 | return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 567 | } |
| 568 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 569 | static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key, |
| 570 | unsigned int keylen) |
| 571 | { |
| 572 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent); |
| 573 | struct crypto_aead *child = ctx->child; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 574 | |
| 575 | if (keylen < 3) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | keylen -= 3; |
| 579 | memcpy(ctx->nonce, key + keylen, 3); |
| 580 | |
| 581 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 582 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 583 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 584 | return crypto_aead_setkey(child, key, keylen); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int crypto_rfc4309_setauthsize(struct crypto_aead *parent, |
| 588 | unsigned int authsize) |
| 589 | { |
| 590 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent); |
| 591 | |
| 592 | switch (authsize) { |
| 593 | case 8: |
| 594 | case 12: |
| 595 | case 16: |
| 596 | break; |
| 597 | default: |
| 598 | return -EINVAL; |
| 599 | } |
| 600 | |
| 601 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 602 | } |
| 603 | |
| 604 | static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req) |
| 605 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 606 | struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req); |
| 607 | struct aead_request *subreq = &rctx->subreq; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 608 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 609 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead); |
| 610 | struct crypto_aead *child = ctx->child; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 611 | struct scatterlist *sg; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 612 | u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child), |
| 613 | crypto_aead_alignmask(child) + 1); |
| 614 | |
| 615 | /* L' */ |
| 616 | iv[0] = 3; |
| 617 | |
| 618 | memcpy(iv + 1, ctx->nonce, 3); |
| 619 | memcpy(iv + 4, req->iv, 8); |
| 620 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 621 | scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0); |
| 622 | |
| 623 | sg_init_table(rctx->src, 3); |
| 624 | sg_set_buf(rctx->src, iv + 16, req->assoclen - 8); |
| 625 | sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen); |
| 626 | if (sg != rctx->src + 1) |
| 627 | sg_chain(rctx->src, 2, sg); |
| 628 | |
| 629 | if (req->src != req->dst) { |
| 630 | sg_init_table(rctx->dst, 3); |
| 631 | sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8); |
| 632 | sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen); |
| 633 | if (sg != rctx->dst + 1) |
| 634 | sg_chain(rctx->dst, 2, sg); |
| 635 | } |
| 636 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 637 | aead_request_set_tfm(subreq, child); |
| 638 | aead_request_set_callback(subreq, req->base.flags, req->base.complete, |
| 639 | req->base.data); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 640 | aead_request_set_crypt(subreq, rctx->src, |
| 641 | req->src == req->dst ? rctx->src : rctx->dst, |
| 642 | req->cryptlen, iv); |
| 643 | aead_request_set_ad(subreq, req->assoclen - 8); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 644 | |
| 645 | return subreq; |
| 646 | } |
| 647 | |
| 648 | static int crypto_rfc4309_encrypt(struct aead_request *req) |
| 649 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 650 | if (req->assoclen != 16 && req->assoclen != 20) |
| 651 | return -EINVAL; |
| 652 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 653 | req = crypto_rfc4309_crypt(req); |
| 654 | |
| 655 | return crypto_aead_encrypt(req); |
| 656 | } |
| 657 | |
| 658 | static int crypto_rfc4309_decrypt(struct aead_request *req) |
| 659 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 660 | if (req->assoclen != 16 && req->assoclen != 20) |
| 661 | return -EINVAL; |
| 662 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 663 | req = crypto_rfc4309_crypt(req); |
| 664 | |
| 665 | return crypto_aead_decrypt(req); |
| 666 | } |
| 667 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 668 | static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 669 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 670 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 671 | struct crypto_aead_spawn *spawn = aead_instance_ctx(inst); |
| 672 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 673 | struct crypto_aead *aead; |
| 674 | unsigned long align; |
| 675 | |
| 676 | aead = crypto_spawn_aead(spawn); |
| 677 | if (IS_ERR(aead)) |
| 678 | return PTR_ERR(aead); |
| 679 | |
| 680 | ctx->child = aead; |
| 681 | |
| 682 | align = crypto_aead_alignmask(aead); |
| 683 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 684 | crypto_aead_set_reqsize( |
| 685 | tfm, |
| 686 | sizeof(struct crypto_rfc4309_req_ctx) + |
Herbert Xu | 2c221ad | 2015-05-11 17:47:56 +0800 | [diff] [blame] | 687 | ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) + |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 688 | align + 32); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 693 | static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 694 | { |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 695 | struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 696 | |
| 697 | crypto_free_aead(ctx->child); |
| 698 | } |
| 699 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 700 | static void crypto_rfc4309_free(struct aead_instance *inst) |
| 701 | { |
| 702 | crypto_drop_aead(aead_instance_ctx(inst)); |
| 703 | kfree(inst); |
| 704 | } |
| 705 | |
| 706 | static int crypto_rfc4309_create(struct crypto_template *tmpl, |
| 707 | struct rtattr **tb) |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 708 | { |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 709 | u32 mask; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 710 | struct aead_instance *inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 711 | struct crypto_aead_spawn *spawn; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 712 | struct aead_alg *alg; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 713 | int err; |
| 714 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 715 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
| 716 | if (err) |
| 717 | return err; |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 718 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 719 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 720 | if (!inst) |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 721 | return -ENOMEM; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 722 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 723 | spawn = aead_instance_ctx(inst); |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 724 | err = crypto_grab_aead(spawn, aead_crypto_instance(inst), |
Eric Biggers | 64d6679 | 2020-02-25 20:59:14 -0800 | [diff] [blame] | 725 | crypto_attr_alg_name(tb[1]), 0, mask); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 726 | if (err) |
Eric Biggers | 64d6679 | 2020-02-25 20:59:14 -0800 | [diff] [blame] | 727 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 728 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 729 | alg = crypto_spawn_aead_alg(spawn); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 730 | |
| 731 | err = -EINVAL; |
| 732 | |
| 733 | /* We only support 16-byte blocks. */ |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 734 | if (crypto_aead_alg_ivsize(alg) != 16) |
Eric Biggers | 64d6679 | 2020-02-25 20:59:14 -0800 | [diff] [blame] | 735 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 736 | |
| 737 | /* Not a stream cipher? */ |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 738 | if (alg->base.cra_blocksize != 1) |
Eric Biggers | 64d6679 | 2020-02-25 20:59:14 -0800 | [diff] [blame] | 739 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 740 | |
| 741 | err = -ENAMETOOLONG; |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 742 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 743 | "rfc4309(%s)", alg->base.cra_name) >= |
| 744 | CRYPTO_MAX_ALG_NAME || |
| 745 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 746 | "rfc4309(%s)", alg->base.cra_driver_name) >= |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 747 | CRYPTO_MAX_ALG_NAME) |
Eric Biggers | 64d6679 | 2020-02-25 20:59:14 -0800 | [diff] [blame] | 748 | goto err_free_inst; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 749 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 750 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 751 | inst->alg.base.cra_blocksize = 1; |
| 752 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 753 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 754 | inst->alg.ivsize = 8; |
Herbert Xu | 464b93a | 2016-07-12 13:17:38 +0800 | [diff] [blame] | 755 | inst->alg.chunksize = crypto_aead_alg_chunksize(alg); |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 756 | inst->alg.maxauthsize = 16; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 757 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 758 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 759 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 760 | inst->alg.init = crypto_rfc4309_init_tfm; |
| 761 | inst->alg.exit = crypto_rfc4309_exit_tfm; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 762 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 763 | inst->alg.setkey = crypto_rfc4309_setkey; |
| 764 | inst->alg.setauthsize = crypto_rfc4309_setauthsize; |
| 765 | inst->alg.encrypt = crypto_rfc4309_encrypt; |
| 766 | inst->alg.decrypt = crypto_rfc4309_decrypt; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 767 | |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 768 | inst->free = crypto_rfc4309_free; |
| 769 | |
| 770 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | 64d6679 | 2020-02-25 20:59:14 -0800 | [diff] [blame] | 771 | if (err) { |
| 772 | err_free_inst: |
| 773 | crypto_rfc4309_free(inst); |
| 774 | } |
Herbert Xu | 81c4c35 | 2015-07-14 16:53:18 +0800 | [diff] [blame] | 775 | return err; |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 776 | } |
| 777 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 778 | static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent, |
| 779 | const u8 *inkey, unsigned int keylen) |
| 780 | { |
| 781 | struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent); |
| 782 | |
| 783 | return crypto_cipher_setkey(ctx->child, inkey, keylen); |
| 784 | } |
| 785 | |
| 786 | static int crypto_cbcmac_digest_init(struct shash_desc *pdesc) |
| 787 | { |
| 788 | struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc); |
| 789 | int bs = crypto_shash_digestsize(pdesc->tfm); |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 790 | u8 *dg = (u8 *)ctx + crypto_shash_descsize(pdesc->tfm) - bs; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 791 | |
| 792 | ctx->len = 0; |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 793 | memset(dg, 0, bs); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p, |
| 799 | unsigned int len) |
| 800 | { |
| 801 | struct crypto_shash *parent = pdesc->tfm; |
| 802 | struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent); |
| 803 | struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc); |
| 804 | struct crypto_cipher *tfm = tctx->child; |
| 805 | int bs = crypto_shash_digestsize(parent); |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 806 | u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 807 | |
| 808 | while (len > 0) { |
| 809 | unsigned int l = min(len, bs - ctx->len); |
| 810 | |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 811 | crypto_xor(dg + ctx->len, p, l); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 812 | ctx->len +=l; |
| 813 | len -= l; |
| 814 | p += l; |
| 815 | |
| 816 | if (ctx->len == bs) { |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 817 | crypto_cipher_encrypt_one(tfm, dg, dg); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 818 | ctx->len = 0; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out) |
| 826 | { |
| 827 | struct crypto_shash *parent = pdesc->tfm; |
| 828 | struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent); |
| 829 | struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc); |
| 830 | struct crypto_cipher *tfm = tctx->child; |
| 831 | int bs = crypto_shash_digestsize(parent); |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 832 | u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 833 | |
| 834 | if (ctx->len) |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 835 | crypto_cipher_encrypt_one(tfm, dg, dg); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 836 | |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 837 | memcpy(out, dg, bs); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static int cbcmac_init_tfm(struct crypto_tfm *tfm) |
| 842 | { |
| 843 | struct crypto_cipher *cipher; |
| 844 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
Eric Biggers | d5ed3b6 | 2020-01-02 19:59:05 -0800 | [diff] [blame] | 845 | struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 846 | struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 847 | |
| 848 | cipher = crypto_spawn_cipher(spawn); |
| 849 | if (IS_ERR(cipher)) |
| 850 | return PTR_ERR(cipher); |
| 851 | |
| 852 | ctx->child = cipher; |
| 853 | |
| 854 | return 0; |
| 855 | }; |
| 856 | |
| 857 | static void cbcmac_exit_tfm(struct crypto_tfm *tfm) |
| 858 | { |
| 859 | struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 860 | crypto_free_cipher(ctx->child); |
| 861 | } |
| 862 | |
| 863 | static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 864 | { |
| 865 | struct shash_instance *inst; |
Eric Biggers | 1667297 | 2020-01-02 19:59:01 -0800 | [diff] [blame] | 866 | struct crypto_cipher_spawn *spawn; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 867 | struct crypto_alg *alg; |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 868 | u32 mask; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 869 | int err; |
| 870 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 871 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 872 | if (err) |
| 873 | return err; |
| 874 | |
Eric Biggers | 1667297 | 2020-01-02 19:59:01 -0800 | [diff] [blame] | 875 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 876 | if (!inst) |
| 877 | return -ENOMEM; |
| 878 | spawn = shash_instance_ctx(inst); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 879 | |
Eric Biggers | 1667297 | 2020-01-02 19:59:01 -0800 | [diff] [blame] | 880 | err = crypto_grab_cipher(spawn, shash_crypto_instance(inst), |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 881 | crypto_attr_alg_name(tb[1]), 0, mask); |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 882 | if (err) |
Eric Biggers | 1667297 | 2020-01-02 19:59:01 -0800 | [diff] [blame] | 883 | goto err_free_inst; |
| 884 | alg = crypto_spawn_cipher_alg(spawn); |
| 885 | |
| 886 | err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); |
| 887 | if (err) |
| 888 | goto err_free_inst; |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 889 | |
| 890 | inst->alg.base.cra_priority = alg->cra_priority; |
| 891 | inst->alg.base.cra_blocksize = 1; |
| 892 | |
| 893 | inst->alg.digestsize = alg->cra_blocksize; |
Ard Biesheuvel | 5338ad7 | 2017-02-11 19:25:21 +0000 | [diff] [blame] | 894 | inst->alg.descsize = ALIGN(sizeof(struct cbcmac_desc_ctx), |
| 895 | alg->cra_alignmask + 1) + |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 896 | alg->cra_blocksize; |
| 897 | |
| 898 | inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx); |
| 899 | inst->alg.base.cra_init = cbcmac_init_tfm; |
| 900 | inst->alg.base.cra_exit = cbcmac_exit_tfm; |
| 901 | |
| 902 | inst->alg.init = crypto_cbcmac_digest_init; |
| 903 | inst->alg.update = crypto_cbcmac_digest_update; |
| 904 | inst->alg.final = crypto_cbcmac_digest_final; |
| 905 | inst->alg.setkey = crypto_cbcmac_digest_setkey; |
| 906 | |
Eric Biggers | a39c66c | 2020-01-02 20:04:38 -0800 | [diff] [blame] | 907 | inst->free = shash_free_singlespawn_instance; |
| 908 | |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 909 | err = shash_register_instance(tmpl, inst); |
Eric Biggers | 1667297 | 2020-01-02 19:59:01 -0800 | [diff] [blame] | 910 | if (err) { |
| 911 | err_free_inst: |
Eric Biggers | a39c66c | 2020-01-02 20:04:38 -0800 | [diff] [blame] | 912 | shash_free_singlespawn_instance(inst); |
Eric Biggers | 1667297 | 2020-01-02 19:59:01 -0800 | [diff] [blame] | 913 | } |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 914 | return err; |
| 915 | } |
| 916 | |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 917 | static struct crypto_template crypto_ccm_tmpls[] = { |
| 918 | { |
| 919 | .name = "cbcmac", |
| 920 | .create = cbcmac_create, |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 921 | .module = THIS_MODULE, |
| 922 | }, { |
| 923 | .name = "ccm_base", |
| 924 | .create = crypto_ccm_base_create, |
| 925 | .module = THIS_MODULE, |
| 926 | }, { |
| 927 | .name = "ccm", |
| 928 | .create = crypto_ccm_create, |
| 929 | .module = THIS_MODULE, |
| 930 | }, { |
| 931 | .name = "rfc4309", |
| 932 | .create = crypto_rfc4309_create, |
| 933 | .module = THIS_MODULE, |
| 934 | }, |
Ard Biesheuvel | f15f05b | 2017-02-03 14:49:36 +0000 | [diff] [blame] | 935 | }; |
| 936 | |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 937 | static int __init crypto_ccm_module_init(void) |
| 938 | { |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 939 | return crypto_register_templates(crypto_ccm_tmpls, |
| 940 | ARRAY_SIZE(crypto_ccm_tmpls)); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static void __exit crypto_ccm_module_exit(void) |
| 944 | { |
Xiongfeng Wang | 0db1903 | 2019-01-18 13:58:12 +0800 | [diff] [blame] | 945 | crypto_unregister_templates(crypto_ccm_tmpls, |
| 946 | ARRAY_SIZE(crypto_ccm_tmpls)); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 947 | } |
| 948 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 949 | subsys_initcall(crypto_ccm_module_init); |
Joy Latten | 4a49b49 | 2007-12-12 20:25:13 +0800 | [diff] [blame] | 950 | module_exit(crypto_ccm_module_exit); |
| 951 | |
| 952 | MODULE_LICENSE("GPL"); |
| 953 | MODULE_DESCRIPTION("Counter with CBC MAC"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 954 | MODULE_ALIAS_CRYPTO("ccm_base"); |
| 955 | MODULE_ALIAS_CRYPTO("rfc4309"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 956 | MODULE_ALIAS_CRYPTO("ccm"); |
Ard Biesheuvel | ae748b9 | 2019-06-17 10:18:48 +0200 | [diff] [blame] | 957 | MODULE_ALIAS_CRYPTO("cbcmac"); |
Ard Biesheuvel | 0eb76ba | 2020-12-11 13:27:15 +0100 | [diff] [blame] | 958 | MODULE_IMPORT_NS(CRYPTO_INTERNAL); |