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 | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 14 | #include <crypto/internal/skcipher.h> |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 15 | #include <crypto/authenc.h> |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 16 | #include <crypto/scatterwalk.h> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 17 | #include <linux/err.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 21 | #include <linux/rtnetlink.h> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 25 | struct authenc_instance_ctx { |
| 26 | struct crypto_spawn auth; |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 27 | struct crypto_skcipher_spawn enc; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct crypto_authenc_ctx { |
| 31 | spinlock_t auth_lock; |
| 32 | struct crypto_hash *auth; |
| 33 | struct crypto_ablkcipher *enc; |
| 34 | }; |
| 35 | |
| 36 | static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key, |
| 37 | unsigned int keylen) |
| 38 | { |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 39 | unsigned int authkeylen; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 40 | unsigned int enckeylen; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 41 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 42 | struct crypto_hash *auth = ctx->auth; |
| 43 | struct crypto_ablkcipher *enc = ctx->enc; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 44 | struct rtattr *rta = (void *)key; |
| 45 | struct crypto_authenc_key_param *param; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 46 | int err = -EINVAL; |
| 47 | |
Herbert Xu | 12dc5e6 | 2007-12-10 10:55:21 +0800 | [diff] [blame] | 48 | if (!RTA_OK(rta, keylen)) |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 49 | goto badkey; |
| 50 | if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) |
| 51 | goto badkey; |
| 52 | if (RTA_PAYLOAD(rta) < sizeof(*param)) |
| 53 | goto badkey; |
| 54 | |
| 55 | param = RTA_DATA(rta); |
| 56 | enckeylen = be32_to_cpu(param->enckeylen); |
| 57 | |
| 58 | key += RTA_ALIGN(rta->rta_len); |
| 59 | keylen -= RTA_ALIGN(rta->rta_len); |
| 60 | |
| 61 | if (keylen < enckeylen) |
| 62 | goto badkey; |
| 63 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 64 | authkeylen = keylen - enckeylen; |
| 65 | |
| 66 | crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); |
| 67 | crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) & |
| 68 | CRYPTO_TFM_REQ_MASK); |
| 69 | err = crypto_hash_setkey(auth, key, authkeylen); |
| 70 | crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) & |
| 71 | CRYPTO_TFM_RES_MASK); |
| 72 | |
| 73 | if (err) |
| 74 | goto out; |
| 75 | |
| 76 | crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK); |
| 77 | crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) & |
| 78 | CRYPTO_TFM_REQ_MASK); |
| 79 | err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen); |
| 80 | crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) & |
| 81 | CRYPTO_TFM_RES_MASK); |
| 82 | |
| 83 | out: |
| 84 | return err; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 85 | |
| 86 | badkey: |
| 87 | crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 88 | goto out; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 91 | static void authenc_chain(struct scatterlist *head, struct scatterlist *sg, |
| 92 | int chain) |
| 93 | { |
| 94 | if (chain) { |
| 95 | head->length += sg->length; |
| 96 | sg = scatterwalk_sg_next(sg); |
| 97 | } |
| 98 | |
| 99 | if (sg) |
| 100 | scatterwalk_sg_chain(head, 2, sg); |
| 101 | else |
| 102 | sg_mark_end(head); |
| 103 | } |
| 104 | |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 105 | static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags, |
| 106 | struct scatterlist *cipher, |
| 107 | unsigned int cryptlen) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 108 | { |
| 109 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 110 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 111 | struct crypto_hash *auth = ctx->auth; |
| 112 | struct hash_desc desc = { |
| 113 | .tfm = auth, |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 114 | .flags = aead_request_flags(req) & flags, |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 115 | }; |
| 116 | u8 *hash = aead_request_ctx(req); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 117 | int err; |
| 118 | |
| 119 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth), |
| 120 | crypto_hash_alignmask(auth) + 1); |
| 121 | |
| 122 | spin_lock_bh(&ctx->auth_lock); |
| 123 | err = crypto_hash_init(&desc); |
| 124 | if (err) |
| 125 | goto auth_unlock; |
| 126 | |
| 127 | err = crypto_hash_update(&desc, req->assoc, req->assoclen); |
| 128 | if (err) |
| 129 | goto auth_unlock; |
| 130 | |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 131 | err = crypto_hash_update(&desc, cipher, cryptlen); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 132 | if (err) |
| 133 | goto auth_unlock; |
| 134 | |
| 135 | err = crypto_hash_final(&desc, hash); |
| 136 | auth_unlock: |
| 137 | spin_unlock_bh(&ctx->auth_lock); |
| 138 | |
| 139 | if (err) |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 140 | return ERR_PTR(err); |
| 141 | |
| 142 | return hash; |
| 143 | } |
| 144 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 145 | static int crypto_authenc_genicv(struct aead_request *req, u8 *iv, |
| 146 | unsigned int flags) |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 147 | { |
| 148 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 149 | struct scatterlist *dst = req->dst; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 150 | struct scatterlist cipher[2]; |
| 151 | struct page *dstp; |
| 152 | unsigned int ivsize = crypto_aead_ivsize(authenc); |
| 153 | unsigned int cryptlen; |
| 154 | u8 *vdst; |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 155 | u8 *hash; |
| 156 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 157 | dstp = sg_page(dst); |
| 158 | vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset; |
| 159 | |
| 160 | sg_init_table(cipher, 2); |
| 161 | sg_set_buf(cipher, iv, ivsize); |
| 162 | authenc_chain(cipher, dst, vdst == iv + ivsize); |
| 163 | |
| 164 | cryptlen = req->cryptlen + ivsize; |
| 165 | hash = crypto_authenc_hash(req, flags, cipher, cryptlen); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 166 | if (IS_ERR(hash)) |
| 167 | return PTR_ERR(hash); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 168 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 169 | scatterwalk_map_and_copy(hash, cipher, cryptlen, |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 170 | crypto_aead_authsize(authenc), 1); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void crypto_authenc_encrypt_done(struct crypto_async_request *req, |
| 175 | int err) |
| 176 | { |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 177 | struct aead_request *areq = req->data; |
| 178 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 179 | if (!err) { |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 180 | struct crypto_aead *authenc = crypto_aead_reqtfm(areq); |
| 181 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 182 | struct ablkcipher_request *abreq = aead_request_ctx(areq); |
| 183 | u8 *iv = (u8 *)(abreq + 1) + |
| 184 | crypto_ablkcipher_reqsize(ctx->enc); |
| 185 | |
| 186 | err = crypto_authenc_genicv(areq, iv, 0); |
| 187 | } |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 188 | |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 189 | aead_request_complete(areq, err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int crypto_authenc_encrypt(struct aead_request *req) |
| 193 | { |
| 194 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 195 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 196 | struct ablkcipher_request *abreq = aead_request_ctx(req); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 197 | struct crypto_ablkcipher *enc = ctx->enc; |
| 198 | struct scatterlist *dst = req->dst; |
| 199 | unsigned int cryptlen = req->cryptlen; |
| 200 | u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 201 | int err; |
| 202 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 203 | ablkcipher_request_set_tfm(abreq, enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 204 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 205 | crypto_authenc_encrypt_done, req); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 206 | ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv); |
| 207 | |
| 208 | memcpy(iv, req->iv, crypto_aead_ivsize(authenc)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 209 | |
| 210 | err = crypto_ablkcipher_encrypt(abreq); |
| 211 | if (err) |
| 212 | return err; |
| 213 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 214 | return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP); |
| 215 | } |
| 216 | |
| 217 | static void crypto_authenc_givencrypt_done(struct crypto_async_request *req, |
| 218 | int err) |
| 219 | { |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 220 | struct aead_request *areq = req->data; |
| 221 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 222 | if (!err) { |
Patrick McHardy | 1616132 | 2008-04-29 21:44:28 +0800 | [diff] [blame] | 223 | struct skcipher_givcrypt_request *greq = aead_request_ctx(areq); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 224 | |
Patrick McHardy | 1616132 | 2008-04-29 21:44:28 +0800 | [diff] [blame] | 225 | err = crypto_authenc_genicv(areq, greq->giv, 0); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 228 | aead_request_complete(areq, err); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req) |
| 232 | { |
| 233 | struct crypto_aead *authenc = aead_givcrypt_reqtfm(req); |
| 234 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 235 | struct aead_request *areq = &req->areq; |
| 236 | struct skcipher_givcrypt_request *greq = aead_request_ctx(areq); |
| 237 | u8 *iv = req->giv; |
| 238 | int err; |
| 239 | |
| 240 | skcipher_givcrypt_set_tfm(greq, ctx->enc); |
| 241 | skcipher_givcrypt_set_callback(greq, aead_request_flags(areq), |
| 242 | crypto_authenc_givencrypt_done, areq); |
| 243 | skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen, |
| 244 | areq->iv); |
| 245 | skcipher_givcrypt_set_giv(greq, iv, req->seq); |
| 246 | |
| 247 | err = crypto_skcipher_givencrypt(greq); |
| 248 | if (err) |
| 249 | return err; |
| 250 | |
| 251 | return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 254 | static int crypto_authenc_verify(struct aead_request *req, |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 255 | struct scatterlist *cipher, |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 256 | unsigned int cryptlen) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 257 | { |
| 258 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 259 | u8 *ohash; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 260 | u8 *ihash; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 261 | unsigned int authsize; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 262 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 263 | ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, cipher, |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 264 | cryptlen); |
| 265 | if (IS_ERR(ohash)) |
| 266 | return PTR_ERR(ohash); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 267 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 268 | authsize = crypto_aead_authsize(authenc); |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 269 | ihash = ohash + authsize; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 270 | scatterwalk_map_and_copy(ihash, cipher, cryptlen, authsize, 0); |
Herbert Xu | fe70f5d | 2007-12-04 20:07:27 +1100 | [diff] [blame] | 271 | return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 272 | } |
| 273 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 274 | static int crypto_authenc_iverify(struct aead_request *req, u8 *iv, |
| 275 | unsigned int cryptlen) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 276 | { |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 277 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 278 | struct scatterlist *src = req->src; |
| 279 | struct scatterlist cipher[2]; |
| 280 | struct page *srcp; |
| 281 | unsigned int ivsize = crypto_aead_ivsize(authenc); |
| 282 | u8 *vsrc; |
| 283 | |
| 284 | srcp = sg_page(src); |
| 285 | vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset; |
| 286 | |
| 287 | sg_init_table(cipher, 2); |
| 288 | sg_set_buf(cipher, iv, ivsize); |
| 289 | authenc_chain(cipher, src, vsrc == iv + ivsize); |
| 290 | |
| 291 | return crypto_authenc_verify(req, cipher, cryptlen + ivsize); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static int crypto_authenc_decrypt(struct aead_request *req) |
| 295 | { |
| 296 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 297 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 298 | struct ablkcipher_request *abreq = aead_request_ctx(req); |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 299 | unsigned int cryptlen = req->cryptlen; |
| 300 | unsigned int authsize = crypto_aead_authsize(authenc); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 301 | u8 *iv = req->iv; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 302 | int err; |
| 303 | |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 304 | if (cryptlen < authsize) |
| 305 | return -EINVAL; |
| 306 | cryptlen -= authsize; |
| 307 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 308 | err = crypto_authenc_iverify(req, iv, cryptlen); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 309 | if (err) |
| 310 | return err; |
| 311 | |
| 312 | ablkcipher_request_set_tfm(abreq, ctx->enc); |
| 313 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 314 | req->base.complete, req->base.data); |
| 315 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 316 | |
| 317 | return crypto_ablkcipher_decrypt(abreq); |
| 318 | } |
| 319 | |
| 320 | static int crypto_authenc_init_tfm(struct crypto_tfm *tfm) |
| 321 | { |
| 322 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 323 | struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 324 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 325 | struct crypto_hash *auth; |
| 326 | struct crypto_ablkcipher *enc; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 327 | int err; |
| 328 | |
| 329 | auth = crypto_spawn_hash(&ictx->auth); |
| 330 | if (IS_ERR(auth)) |
| 331 | return PTR_ERR(auth); |
| 332 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 333 | enc = crypto_spawn_skcipher(&ictx->enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 334 | err = PTR_ERR(enc); |
| 335 | if (IS_ERR(enc)) |
| 336 | goto err_free_hash; |
| 337 | |
| 338 | ctx->auth = auth; |
| 339 | ctx->enc = enc; |
| 340 | tfm->crt_aead.reqsize = max_t(unsigned int, |
| 341 | (crypto_hash_alignmask(auth) & |
| 342 | ~(crypto_tfm_ctx_alignment() - 1)) + |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 343 | crypto_hash_digestsize(auth) * 2, |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 344 | sizeof(struct skcipher_givcrypt_request) + |
| 345 | crypto_ablkcipher_reqsize(enc) + |
| 346 | crypto_ablkcipher_ivsize(enc)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 347 | |
| 348 | spin_lock_init(&ctx->auth_lock); |
| 349 | |
| 350 | return 0; |
| 351 | |
| 352 | err_free_hash: |
| 353 | crypto_free_hash(auth); |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm) |
| 358 | { |
| 359 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 360 | |
| 361 | crypto_free_hash(ctx->auth); |
| 362 | crypto_free_ablkcipher(ctx->enc); |
| 363 | } |
| 364 | |
| 365 | static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb) |
| 366 | { |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 367 | struct crypto_attr_type *algt; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 368 | struct crypto_instance *inst; |
| 369 | struct crypto_alg *auth; |
| 370 | struct crypto_alg *enc; |
| 371 | struct authenc_instance_ctx *ctx; |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 372 | const char *enc_name; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 373 | int err; |
| 374 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 375 | algt = crypto_get_attr_type(tb); |
| 376 | err = PTR_ERR(algt); |
| 377 | if (IS_ERR(algt)) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 378 | return ERR_PTR(err); |
| 379 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 380 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
| 381 | return ERR_PTR(-EINVAL); |
| 382 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 383 | auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, |
| 384 | CRYPTO_ALG_TYPE_HASH_MASK); |
| 385 | if (IS_ERR(auth)) |
| 386 | return ERR_PTR(PTR_ERR(auth)); |
| 387 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 388 | enc_name = crypto_attr_alg_name(tb[2]); |
| 389 | err = PTR_ERR(enc_name); |
| 390 | if (IS_ERR(enc_name)) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 391 | goto out_put_auth; |
| 392 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 393 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 394 | err = -ENOMEM; |
| 395 | if (!inst) |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 396 | goto out_put_auth; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 397 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 398 | ctx = crypto_instance_ctx(inst); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 399 | |
| 400 | err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK); |
| 401 | if (err) |
| 402 | goto err_free_inst; |
| 403 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 404 | crypto_set_skcipher_spawn(&ctx->enc, inst); |
| 405 | err = crypto_grab_skcipher(&ctx->enc, enc_name, 0, |
| 406 | crypto_requires_sync(algt->type, |
| 407 | algt->mask)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 408 | if (err) |
| 409 | goto err_drop_auth; |
| 410 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 411 | enc = crypto_skcipher_spawn_alg(&ctx->enc); |
| 412 | |
| 413 | err = -ENAMETOOLONG; |
| 414 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, |
| 415 | "authenc(%s,%s)", auth->cra_name, enc->cra_name) >= |
| 416 | CRYPTO_MAX_ALG_NAME) |
| 417 | goto err_drop_enc; |
| 418 | |
| 419 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 420 | "authenc(%s,%s)", auth->cra_driver_name, |
| 421 | enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 422 | goto err_drop_enc; |
| 423 | |
| 424 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
| 425 | inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 426 | inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority; |
| 427 | inst->alg.cra_blocksize = enc->cra_blocksize; |
Herbert Xu | e29bc6a | 2007-11-22 22:46:40 +0800 | [diff] [blame] | 428 | inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 429 | inst->alg.cra_type = &crypto_aead_type; |
| 430 | |
Herbert Xu | c2c61f51 | 2007-12-10 10:54:44 +0800 | [diff] [blame] | 431 | inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 432 | inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ? |
| 433 | auth->cra_hash.digestsize : |
| 434 | auth->cra_digest.dia_digestsize; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 435 | |
| 436 | inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx); |
| 437 | |
| 438 | inst->alg.cra_init = crypto_authenc_init_tfm; |
| 439 | inst->alg.cra_exit = crypto_authenc_exit_tfm; |
| 440 | |
| 441 | inst->alg.cra_aead.setkey = crypto_authenc_setkey; |
| 442 | inst->alg.cra_aead.encrypt = crypto_authenc_encrypt; |
| 443 | inst->alg.cra_aead.decrypt = crypto_authenc_decrypt; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 444 | inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 445 | |
| 446 | out: |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 447 | crypto_mod_put(auth); |
| 448 | return inst; |
| 449 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 450 | err_drop_enc: |
| 451 | crypto_drop_skcipher(&ctx->enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 452 | err_drop_auth: |
| 453 | crypto_drop_spawn(&ctx->auth); |
| 454 | err_free_inst: |
| 455 | kfree(inst); |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 456 | out_put_auth: |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 457 | inst = ERR_PTR(err); |
| 458 | goto out; |
| 459 | } |
| 460 | |
| 461 | static void crypto_authenc_free(struct crypto_instance *inst) |
| 462 | { |
| 463 | struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 464 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 465 | crypto_drop_skcipher(&ctx->enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 466 | crypto_drop_spawn(&ctx->auth); |
| 467 | kfree(inst); |
| 468 | } |
| 469 | |
| 470 | static struct crypto_template crypto_authenc_tmpl = { |
| 471 | .name = "authenc", |
| 472 | .alloc = crypto_authenc_alloc, |
| 473 | .free = crypto_authenc_free, |
| 474 | .module = THIS_MODULE, |
| 475 | }; |
| 476 | |
| 477 | static int __init crypto_authenc_module_init(void) |
| 478 | { |
| 479 | return crypto_register_template(&crypto_authenc_tmpl); |
| 480 | } |
| 481 | |
| 482 | static void __exit crypto_authenc_module_exit(void) |
| 483 | { |
| 484 | crypto_unregister_template(&crypto_authenc_tmpl); |
| 485 | } |
| 486 | |
| 487 | module_init(crypto_authenc_module_init); |
| 488 | module_exit(crypto_authenc_module_exit); |
| 489 | |
| 490 | MODULE_LICENSE("GPL"); |
| 491 | MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec"); |