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