Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Authenc: Simple AEAD wrapper for IPsec |
| 3 | * |
| 4 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
| 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 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 13 | #include <crypto/aead.h> |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 14 | #include <crypto/internal/hash.h> |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 15 | #include <crypto/internal/skcipher.h> |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 16 | #include <crypto/authenc.h> |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 17 | #include <crypto/scatterwalk.h> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 22 | #include <linux/rtnetlink.h> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 26 | typedef u8 *(*authenc_ahash_t)(struct aead_request *req, unsigned int flags); |
| 27 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 28 | struct authenc_instance_ctx { |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 29 | struct crypto_ahash_spawn auth; |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 30 | struct crypto_skcipher_spawn enc; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct crypto_authenc_ctx { |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 34 | unsigned int reqoff; |
| 35 | struct crypto_ahash *auth; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 36 | struct crypto_ablkcipher *enc; |
| 37 | }; |
| 38 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 39 | struct authenc_request_ctx { |
| 40 | unsigned int cryptlen; |
| 41 | struct scatterlist *sg; |
| 42 | struct scatterlist asg[2]; |
| 43 | struct scatterlist cipher[2]; |
| 44 | crypto_completion_t complete; |
| 45 | crypto_completion_t update_complete; |
| 46 | char tail[]; |
| 47 | }; |
| 48 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 49 | static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key, |
| 50 | unsigned int keylen) |
| 51 | { |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 52 | unsigned int authkeylen; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 53 | unsigned int enckeylen; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 54 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 55 | struct crypto_ahash *auth = ctx->auth; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 56 | struct crypto_ablkcipher *enc = ctx->enc; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 57 | struct rtattr *rta = (void *)key; |
| 58 | struct crypto_authenc_key_param *param; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 59 | int err = -EINVAL; |
| 60 | |
Herbert Xu | 12dc5e6 | 2007-12-10 10:55:21 +0800 | [diff] [blame] | 61 | if (!RTA_OK(rta, keylen)) |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 62 | goto badkey; |
| 63 | if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) |
| 64 | goto badkey; |
| 65 | if (RTA_PAYLOAD(rta) < sizeof(*param)) |
| 66 | goto badkey; |
| 67 | |
| 68 | param = RTA_DATA(rta); |
| 69 | enckeylen = be32_to_cpu(param->enckeylen); |
| 70 | |
| 71 | key += RTA_ALIGN(rta->rta_len); |
| 72 | keylen -= RTA_ALIGN(rta->rta_len); |
| 73 | |
| 74 | if (keylen < enckeylen) |
| 75 | goto badkey; |
| 76 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 77 | authkeylen = keylen - enckeylen; |
| 78 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 79 | crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); |
| 80 | crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) & |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 81 | CRYPTO_TFM_REQ_MASK); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 82 | err = crypto_ahash_setkey(auth, key, authkeylen); |
| 83 | crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) & |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 84 | CRYPTO_TFM_RES_MASK); |
| 85 | |
| 86 | if (err) |
| 87 | goto out; |
| 88 | |
| 89 | crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK); |
| 90 | crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) & |
| 91 | CRYPTO_TFM_REQ_MASK); |
| 92 | err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen); |
| 93 | crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) & |
| 94 | CRYPTO_TFM_RES_MASK); |
| 95 | |
| 96 | out: |
| 97 | return err; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 98 | |
| 99 | badkey: |
| 100 | crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 101 | goto out; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 102 | } |
| 103 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 104 | static void authenc_chain(struct scatterlist *head, struct scatterlist *sg, |
| 105 | int chain) |
| 106 | { |
| 107 | if (chain) { |
| 108 | head->length += sg->length; |
| 109 | sg = scatterwalk_sg_next(sg); |
| 110 | } |
| 111 | |
| 112 | if (sg) |
| 113 | scatterwalk_sg_chain(head, 2, sg); |
| 114 | else |
| 115 | sg_mark_end(head); |
| 116 | } |
| 117 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 118 | static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq, |
| 119 | int err) |
| 120 | { |
| 121 | struct aead_request *req = areq->data; |
| 122 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 123 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 124 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 125 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff); |
| 126 | |
| 127 | if (err) |
| 128 | goto out; |
| 129 | |
| 130 | ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result, |
| 131 | areq_ctx->cryptlen); |
| 132 | ahash_request_set_callback(ahreq, aead_request_flags(req) & |
| 133 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 134 | areq_ctx->complete, req); |
| 135 | |
| 136 | err = crypto_ahash_finup(ahreq); |
| 137 | if (err) |
| 138 | goto out; |
| 139 | |
| 140 | scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg, |
| 141 | areq_ctx->cryptlen, |
| 142 | crypto_aead_authsize(authenc), 1); |
| 143 | |
| 144 | out: |
| 145 | aead_request_complete(req, err); |
| 146 | } |
| 147 | |
| 148 | static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err) |
| 149 | { |
| 150 | struct aead_request *req = areq->data; |
| 151 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 152 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 153 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 154 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff); |
| 155 | |
| 156 | if (err) |
| 157 | goto out; |
| 158 | |
| 159 | scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg, |
| 160 | areq_ctx->cryptlen, |
| 161 | crypto_aead_authsize(authenc), 1); |
| 162 | |
| 163 | out: |
| 164 | aead_request_complete(req, err); |
| 165 | } |
| 166 | |
| 167 | static void authenc_verify_ahash_update_done(struct crypto_async_request *areq, |
| 168 | int err) |
| 169 | { |
| 170 | u8 *ihash; |
| 171 | unsigned int authsize; |
| 172 | struct ablkcipher_request *abreq; |
| 173 | struct aead_request *req = areq->data; |
| 174 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 175 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 176 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 177 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff); |
| 178 | |
| 179 | if (err) |
| 180 | goto out; |
| 181 | |
| 182 | ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result, |
| 183 | areq_ctx->cryptlen); |
| 184 | ahash_request_set_callback(ahreq, aead_request_flags(req) & |
| 185 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 186 | areq_ctx->complete, req); |
| 187 | |
| 188 | err = crypto_ahash_finup(ahreq); |
| 189 | if (err) |
| 190 | goto out; |
| 191 | |
| 192 | authsize = crypto_aead_authsize(authenc); |
| 193 | ihash = ahreq->result + authsize; |
| 194 | scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen, |
| 195 | authsize, 0); |
| 196 | |
Richard Hartmann | f3542e6 | 2010-02-16 20:27:20 +0800 | [diff] [blame] | 197 | err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 198 | if (err) |
| 199 | goto out; |
| 200 | |
| 201 | abreq = aead_request_ctx(req); |
| 202 | ablkcipher_request_set_tfm(abreq, ctx->enc); |
| 203 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 204 | req->base.complete, req->base.data); |
| 205 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, |
| 206 | req->cryptlen, req->iv); |
| 207 | |
| 208 | err = crypto_ablkcipher_decrypt(abreq); |
| 209 | |
| 210 | out: |
| 211 | aead_request_complete(req, err); |
| 212 | } |
| 213 | |
| 214 | static void authenc_verify_ahash_done(struct crypto_async_request *areq, |
| 215 | int err) |
| 216 | { |
| 217 | u8 *ihash; |
| 218 | unsigned int authsize; |
| 219 | struct ablkcipher_request *abreq; |
| 220 | struct aead_request *req = areq->data; |
| 221 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 222 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 223 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 224 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff); |
| 225 | |
| 226 | if (err) |
| 227 | goto out; |
| 228 | |
| 229 | authsize = crypto_aead_authsize(authenc); |
| 230 | ihash = ahreq->result + authsize; |
| 231 | scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen, |
| 232 | authsize, 0); |
| 233 | |
Richard Hartmann | f3542e6 | 2010-02-16 20:27:20 +0800 | [diff] [blame] | 234 | err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 235 | if (err) |
| 236 | goto out; |
| 237 | |
| 238 | abreq = aead_request_ctx(req); |
| 239 | ablkcipher_request_set_tfm(abreq, ctx->enc); |
| 240 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 241 | req->base.complete, req->base.data); |
| 242 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, |
| 243 | req->cryptlen, req->iv); |
| 244 | |
| 245 | err = crypto_ablkcipher_decrypt(abreq); |
| 246 | |
| 247 | out: |
| 248 | aead_request_complete(req, err); |
| 249 | } |
| 250 | |
| 251 | static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 252 | { |
| 253 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 254 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 255 | struct crypto_ahash *auth = ctx->auth; |
| 256 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 257 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff); |
| 258 | u8 *hash = areq_ctx->tail; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 259 | int err; |
| 260 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 261 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth), |
| 262 | crypto_ahash_alignmask(auth) + 1); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 263 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 264 | ahash_request_set_tfm(ahreq, auth); |
| 265 | |
| 266 | err = crypto_ahash_init(ahreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 267 | if (err) |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 268 | return ERR_PTR(err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 269 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 270 | ahash_request_set_crypt(ahreq, req->assoc, hash, req->assoclen); |
| 271 | ahash_request_set_callback(ahreq, aead_request_flags(req) & flags, |
| 272 | areq_ctx->update_complete, req); |
| 273 | |
| 274 | err = crypto_ahash_update(ahreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 275 | if (err) |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 276 | return ERR_PTR(err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 277 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 278 | ahash_request_set_crypt(ahreq, areq_ctx->sg, hash, |
| 279 | areq_ctx->cryptlen); |
| 280 | ahash_request_set_callback(ahreq, aead_request_flags(req) & flags, |
| 281 | areq_ctx->complete, req); |
| 282 | |
| 283 | err = crypto_ahash_finup(ahreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 284 | if (err) |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 285 | return ERR_PTR(err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 286 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 287 | return hash; |
| 288 | } |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 289 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 290 | static u8 *crypto_authenc_ahash(struct aead_request *req, unsigned int flags) |
| 291 | { |
| 292 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 293 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 294 | struct crypto_ahash *auth = ctx->auth; |
| 295 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 296 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff); |
| 297 | u8 *hash = areq_ctx->tail; |
| 298 | int err; |
| 299 | |
| 300 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth), |
| 301 | crypto_ahash_alignmask(auth) + 1); |
| 302 | |
| 303 | ahash_request_set_tfm(ahreq, auth); |
| 304 | ahash_request_set_crypt(ahreq, areq_ctx->sg, hash, |
| 305 | areq_ctx->cryptlen); |
| 306 | ahash_request_set_callback(ahreq, aead_request_flags(req) & flags, |
| 307 | areq_ctx->complete, req); |
| 308 | |
| 309 | err = crypto_ahash_digest(ahreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 310 | if (err) |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 311 | return ERR_PTR(err); |
| 312 | |
| 313 | return hash; |
| 314 | } |
| 315 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 316 | static int crypto_authenc_genicv(struct aead_request *req, u8 *iv, |
| 317 | unsigned int flags) |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 318 | { |
| 319 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 320 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 321 | struct scatterlist *dst = req->dst; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 322 | struct scatterlist *assoc = req->assoc; |
| 323 | struct scatterlist *cipher = areq_ctx->cipher; |
| 324 | struct scatterlist *asg = areq_ctx->asg; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 325 | unsigned int ivsize = crypto_aead_ivsize(authenc); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 326 | unsigned int cryptlen = req->cryptlen; |
| 327 | authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb; |
| 328 | struct page *dstp; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 329 | u8 *vdst; |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 330 | u8 *hash; |
| 331 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 332 | dstp = sg_page(dst); |
| 333 | vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset; |
| 334 | |
Herbert Xu | 29b37f4 | 2009-01-13 11:26:18 +1100 | [diff] [blame] | 335 | if (ivsize) { |
| 336 | sg_init_table(cipher, 2); |
| 337 | sg_set_buf(cipher, iv, ivsize); |
| 338 | authenc_chain(cipher, dst, vdst == iv + ivsize); |
| 339 | dst = cipher; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 340 | cryptlen += ivsize; |
Herbert Xu | 29b37f4 | 2009-01-13 11:26:18 +1100 | [diff] [blame] | 341 | } |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 342 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 343 | if (sg_is_last(assoc)) { |
| 344 | authenc_ahash_fn = crypto_authenc_ahash; |
| 345 | sg_init_table(asg, 2); |
| 346 | sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset); |
| 347 | authenc_chain(asg, dst, 0); |
| 348 | dst = asg; |
| 349 | cryptlen += req->assoclen; |
| 350 | } |
| 351 | |
| 352 | areq_ctx->cryptlen = cryptlen; |
| 353 | areq_ctx->sg = dst; |
| 354 | |
| 355 | areq_ctx->complete = authenc_geniv_ahash_done; |
| 356 | areq_ctx->update_complete = authenc_geniv_ahash_update_done; |
| 357 | |
| 358 | hash = authenc_ahash_fn(req, flags); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 359 | if (IS_ERR(hash)) |
| 360 | return PTR_ERR(hash); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 361 | |
Herbert Xu | 29b37f4 | 2009-01-13 11:26:18 +1100 | [diff] [blame] | 362 | scatterwalk_map_and_copy(hash, dst, cryptlen, |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 363 | crypto_aead_authsize(authenc), 1); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static void crypto_authenc_encrypt_done(struct crypto_async_request *req, |
| 368 | int err) |
| 369 | { |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 370 | struct aead_request *areq = req->data; |
| 371 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 372 | if (!err) { |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 373 | struct crypto_aead *authenc = crypto_aead_reqtfm(areq); |
| 374 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 375 | struct ablkcipher_request *abreq = aead_request_ctx(areq); |
| 376 | u8 *iv = (u8 *)(abreq + 1) + |
| 377 | crypto_ablkcipher_reqsize(ctx->enc); |
| 378 | |
| 379 | err = crypto_authenc_genicv(areq, iv, 0); |
| 380 | } |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 381 | |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 382 | aead_request_complete(areq, err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static int crypto_authenc_encrypt(struct aead_request *req) |
| 386 | { |
| 387 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 388 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 389 | struct ablkcipher_request *abreq = aead_request_ctx(req); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 390 | struct crypto_ablkcipher *enc = ctx->enc; |
| 391 | struct scatterlist *dst = req->dst; |
| 392 | unsigned int cryptlen = req->cryptlen; |
| 393 | u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 394 | int err; |
| 395 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 396 | ablkcipher_request_set_tfm(abreq, enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 397 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 398 | crypto_authenc_encrypt_done, req); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 399 | ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv); |
| 400 | |
| 401 | memcpy(iv, req->iv, crypto_aead_ivsize(authenc)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 402 | |
| 403 | err = crypto_ablkcipher_encrypt(abreq); |
| 404 | if (err) |
| 405 | return err; |
| 406 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 407 | return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP); |
| 408 | } |
| 409 | |
| 410 | static void crypto_authenc_givencrypt_done(struct crypto_async_request *req, |
| 411 | int err) |
| 412 | { |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 413 | struct aead_request *areq = req->data; |
| 414 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 415 | if (!err) { |
Patrick McHardy | 1616132 | 2008-04-29 21:44:28 +0800 | [diff] [blame] | 416 | struct skcipher_givcrypt_request *greq = aead_request_ctx(areq); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 417 | |
Patrick McHardy | 1616132 | 2008-04-29 21:44:28 +0800 | [diff] [blame] | 418 | err = crypto_authenc_genicv(areq, greq->giv, 0); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 419 | } |
| 420 | |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 421 | aead_request_complete(areq, err); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req) |
| 425 | { |
| 426 | struct crypto_aead *authenc = aead_givcrypt_reqtfm(req); |
| 427 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 428 | struct aead_request *areq = &req->areq; |
| 429 | struct skcipher_givcrypt_request *greq = aead_request_ctx(areq); |
| 430 | u8 *iv = req->giv; |
| 431 | int err; |
| 432 | |
| 433 | skcipher_givcrypt_set_tfm(greq, ctx->enc); |
| 434 | skcipher_givcrypt_set_callback(greq, aead_request_flags(areq), |
| 435 | crypto_authenc_givencrypt_done, areq); |
| 436 | skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen, |
| 437 | areq->iv); |
| 438 | skcipher_givcrypt_set_giv(greq, iv, req->seq); |
| 439 | |
| 440 | err = crypto_skcipher_givencrypt(greq); |
| 441 | if (err) |
| 442 | return err; |
| 443 | |
| 444 | return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 445 | } |
| 446 | |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 447 | static int crypto_authenc_verify(struct aead_request *req, |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 448 | authenc_ahash_t authenc_ahash_fn) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 449 | { |
| 450 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 451 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 452 | u8 *ohash; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 453 | u8 *ihash; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 454 | unsigned int authsize; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 455 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 456 | areq_ctx->complete = authenc_verify_ahash_done; |
| 457 | areq_ctx->complete = authenc_verify_ahash_update_done; |
| 458 | |
| 459 | ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 460 | if (IS_ERR(ohash)) |
| 461 | return PTR_ERR(ohash); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 462 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 463 | authsize = crypto_aead_authsize(authenc); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 464 | ihash = ohash + authsize; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 465 | scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen, |
| 466 | authsize, 0); |
Richard Hartmann | f3542e6 | 2010-02-16 20:27:20 +0800 | [diff] [blame] | 467 | return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 468 | } |
| 469 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 470 | static int crypto_authenc_iverify(struct aead_request *req, u8 *iv, |
| 471 | unsigned int cryptlen) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 472 | { |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 473 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 474 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 475 | struct scatterlist *src = req->src; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 476 | struct scatterlist *assoc = req->assoc; |
| 477 | struct scatterlist *cipher = areq_ctx->cipher; |
| 478 | struct scatterlist *asg = areq_ctx->asg; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 479 | unsigned int ivsize = crypto_aead_ivsize(authenc); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 480 | authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb; |
| 481 | struct page *srcp; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 482 | u8 *vsrc; |
| 483 | |
| 484 | srcp = sg_page(src); |
| 485 | vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset; |
| 486 | |
Herbert Xu | 29b37f4 | 2009-01-13 11:26:18 +1100 | [diff] [blame] | 487 | if (ivsize) { |
| 488 | sg_init_table(cipher, 2); |
| 489 | sg_set_buf(cipher, iv, ivsize); |
| 490 | authenc_chain(cipher, src, vsrc == iv + ivsize); |
| 491 | src = cipher; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 492 | cryptlen += ivsize; |
Herbert Xu | 29b37f4 | 2009-01-13 11:26:18 +1100 | [diff] [blame] | 493 | } |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 494 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 495 | if (sg_is_last(assoc)) { |
| 496 | authenc_ahash_fn = crypto_authenc_ahash; |
| 497 | sg_init_table(asg, 2); |
| 498 | sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset); |
| 499 | authenc_chain(asg, src, 0); |
| 500 | src = asg; |
| 501 | cryptlen += req->assoclen; |
| 502 | } |
| 503 | |
| 504 | areq_ctx->cryptlen = cryptlen; |
| 505 | areq_ctx->sg = src; |
| 506 | |
| 507 | return crypto_authenc_verify(req, authenc_ahash_fn); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static int crypto_authenc_decrypt(struct aead_request *req) |
| 511 | { |
| 512 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 513 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 514 | struct ablkcipher_request *abreq = aead_request_ctx(req); |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 515 | unsigned int cryptlen = req->cryptlen; |
| 516 | unsigned int authsize = crypto_aead_authsize(authenc); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 517 | u8 *iv = req->iv; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 518 | int err; |
| 519 | |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 520 | if (cryptlen < authsize) |
| 521 | return -EINVAL; |
| 522 | cryptlen -= authsize; |
| 523 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 524 | err = crypto_authenc_iverify(req, iv, cryptlen); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 525 | if (err) |
| 526 | return err; |
| 527 | |
| 528 | ablkcipher_request_set_tfm(abreq, ctx->enc); |
| 529 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 530 | req->base.complete, req->base.data); |
| 531 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 532 | |
| 533 | return crypto_ablkcipher_decrypt(abreq); |
| 534 | } |
| 535 | |
| 536 | static int crypto_authenc_init_tfm(struct crypto_tfm *tfm) |
| 537 | { |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 538 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 539 | struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 540 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 541 | struct crypto_ahash *auth; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 542 | struct crypto_ablkcipher *enc; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 543 | int err; |
| 544 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 545 | auth = crypto_spawn_ahash(&ictx->auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 546 | if (IS_ERR(auth)) |
| 547 | return PTR_ERR(auth); |
| 548 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 549 | ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) + |
| 550 | crypto_ahash_alignmask(auth), |
| 551 | crypto_ahash_alignmask(auth) + 1); |
| 552 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 553 | enc = crypto_spawn_skcipher(&ictx->enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 554 | err = PTR_ERR(enc); |
| 555 | if (IS_ERR(enc)) |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 556 | goto err_free_ahash; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 557 | |
| 558 | ctx->auth = auth; |
| 559 | ctx->enc = enc; |
Richard Hartmann | f3542e6 | 2010-02-16 20:27:20 +0800 | [diff] [blame] | 560 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 561 | tfm->crt_aead.reqsize = max_t(unsigned int, |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 562 | crypto_ahash_reqsize(auth) + ctx->reqoff + |
| 563 | sizeof(struct authenc_request_ctx) + |
Richard Hartmann | f3542e6 | 2010-02-16 20:27:20 +0800 | [diff] [blame] | 564 | sizeof(struct ahash_request), |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 565 | sizeof(struct skcipher_givcrypt_request) + |
| 566 | crypto_ablkcipher_reqsize(enc) + |
| 567 | crypto_ablkcipher_ivsize(enc)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 571 | err_free_ahash: |
| 572 | crypto_free_ahash(auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 573 | return err; |
| 574 | } |
| 575 | |
| 576 | static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm) |
| 577 | { |
| 578 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 579 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 580 | crypto_free_ahash(ctx->auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 581 | crypto_free_ablkcipher(ctx->enc); |
| 582 | } |
| 583 | |
| 584 | static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb) |
| 585 | { |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 586 | struct crypto_attr_type *algt; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 587 | struct crypto_instance *inst; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 588 | struct hash_alg_common *auth; |
| 589 | struct crypto_alg *auth_base; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 590 | struct crypto_alg *enc; |
| 591 | struct authenc_instance_ctx *ctx; |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 592 | const char *enc_name; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 593 | int err; |
| 594 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 595 | algt = crypto_get_attr_type(tb); |
| 596 | err = PTR_ERR(algt); |
| 597 | if (IS_ERR(algt)) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 598 | return ERR_PTR(err); |
| 599 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 600 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
| 601 | return ERR_PTR(-EINVAL); |
| 602 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 603 | auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, |
| 604 | CRYPTO_ALG_TYPE_AHASH_MASK); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 605 | if (IS_ERR(auth)) |
| 606 | return ERR_PTR(PTR_ERR(auth)); |
| 607 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 608 | auth_base = &auth->base; |
| 609 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 610 | enc_name = crypto_attr_alg_name(tb[2]); |
| 611 | err = PTR_ERR(enc_name); |
| 612 | if (IS_ERR(enc_name)) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 613 | goto out_put_auth; |
| 614 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 615 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 616 | err = -ENOMEM; |
| 617 | if (!inst) |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 618 | goto out_put_auth; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 619 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 620 | ctx = crypto_instance_ctx(inst); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 621 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 622 | err = crypto_init_ahash_spawn(&ctx->auth, auth, inst); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 623 | if (err) |
| 624 | goto err_free_inst; |
| 625 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 626 | crypto_set_skcipher_spawn(&ctx->enc, inst); |
| 627 | err = crypto_grab_skcipher(&ctx->enc, enc_name, 0, |
| 628 | crypto_requires_sync(algt->type, |
| 629 | algt->mask)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 630 | if (err) |
| 631 | goto err_drop_auth; |
| 632 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 633 | enc = crypto_skcipher_spawn_alg(&ctx->enc); |
| 634 | |
| 635 | err = -ENAMETOOLONG; |
| 636 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 637 | "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >= |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 638 | CRYPTO_MAX_ALG_NAME) |
| 639 | goto err_drop_enc; |
| 640 | |
| 641 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 642 | "authenc(%s,%s)", auth_base->cra_driver_name, |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 643 | enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 644 | goto err_drop_enc; |
| 645 | |
| 646 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
| 647 | inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 648 | inst->alg.cra_priority = enc->cra_priority * |
| 649 | 10 + auth_base->cra_priority; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 650 | inst->alg.cra_blocksize = enc->cra_blocksize; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 651 | inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 652 | inst->alg.cra_type = &crypto_aead_type; |
| 653 | |
Herbert Xu | c2c61f51 | 2007-12-10 10:54:44 +0800 | [diff] [blame] | 654 | inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 655 | inst->alg.cra_aead.maxauthsize = auth->digestsize; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 656 | |
| 657 | inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx); |
| 658 | |
| 659 | inst->alg.cra_init = crypto_authenc_init_tfm; |
| 660 | inst->alg.cra_exit = crypto_authenc_exit_tfm; |
| 661 | |
| 662 | inst->alg.cra_aead.setkey = crypto_authenc_setkey; |
| 663 | inst->alg.cra_aead.encrypt = crypto_authenc_encrypt; |
| 664 | inst->alg.cra_aead.decrypt = crypto_authenc_decrypt; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 665 | inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 666 | |
| 667 | out: |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 668 | crypto_mod_put(auth_base); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 669 | return inst; |
| 670 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 671 | err_drop_enc: |
| 672 | crypto_drop_skcipher(&ctx->enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 673 | err_drop_auth: |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 674 | crypto_drop_ahash(&ctx->auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 675 | err_free_inst: |
| 676 | kfree(inst); |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 677 | out_put_auth: |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 678 | inst = ERR_PTR(err); |
| 679 | goto out; |
| 680 | } |
| 681 | |
| 682 | static void crypto_authenc_free(struct crypto_instance *inst) |
| 683 | { |
| 684 | struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 685 | |
Herbert Xu | 9ffde35 | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 686 | crypto_drop_skcipher(&ctx->enc); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 687 | crypto_drop_ahash(&ctx->auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 688 | kfree(inst); |
| 689 | } |
| 690 | |
| 691 | static struct crypto_template crypto_authenc_tmpl = { |
| 692 | .name = "authenc", |
| 693 | .alloc = crypto_authenc_alloc, |
| 694 | .free = crypto_authenc_free, |
| 695 | .module = THIS_MODULE, |
| 696 | }; |
| 697 | |
| 698 | static int __init crypto_authenc_module_init(void) |
| 699 | { |
| 700 | return crypto_register_template(&crypto_authenc_tmpl); |
| 701 | } |
| 702 | |
| 703 | static void __exit crypto_authenc_module_exit(void) |
| 704 | { |
| 705 | crypto_unregister_template(&crypto_authenc_tmpl); |
| 706 | } |
| 707 | |
| 708 | module_init(crypto_authenc_module_init); |
| 709 | module_exit(crypto_authenc_module_exit); |
| 710 | |
| 711 | MODULE_LICENSE("GPL"); |
| 712 | MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec"); |