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