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