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 | |
| 13 | #include <crypto/algapi.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | |
| 21 | #include "scatterwalk.h" |
| 22 | |
| 23 | struct authenc_instance_ctx { |
| 24 | struct crypto_spawn auth; |
| 25 | struct crypto_spawn enc; |
| 26 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 27 | unsigned int enckeylen; |
| 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 | { |
| 39 | struct authenc_instance_ctx *ictx = |
| 40 | crypto_instance_ctx(crypto_aead_alg_instance(authenc)); |
| 41 | unsigned int enckeylen = ictx->enckeylen; |
| 42 | unsigned int authkeylen; |
| 43 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 44 | struct crypto_hash *auth = ctx->auth; |
| 45 | struct crypto_ablkcipher *enc = ctx->enc; |
| 46 | int err = -EINVAL; |
| 47 | |
| 48 | if (keylen < enckeylen) { |
| 49 | crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 50 | goto out; |
| 51 | } |
| 52 | authkeylen = keylen - enckeylen; |
| 53 | |
| 54 | crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); |
| 55 | crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) & |
| 56 | CRYPTO_TFM_REQ_MASK); |
| 57 | err = crypto_hash_setkey(auth, key, authkeylen); |
| 58 | crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) & |
| 59 | CRYPTO_TFM_RES_MASK); |
| 60 | |
| 61 | if (err) |
| 62 | goto out; |
| 63 | |
| 64 | crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK); |
| 65 | crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) & |
| 66 | CRYPTO_TFM_REQ_MASK); |
| 67 | err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen); |
| 68 | crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) & |
| 69 | CRYPTO_TFM_RES_MASK); |
| 70 | |
| 71 | out: |
| 72 | return err; |
| 73 | } |
| 74 | |
| 75 | static int crypto_authenc_hash(struct aead_request *req) |
| 76 | { |
| 77 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 78 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 79 | struct crypto_hash *auth = ctx->auth; |
| 80 | struct hash_desc desc = { |
| 81 | .tfm = auth, |
| 82 | }; |
| 83 | u8 *hash = aead_request_ctx(req); |
Herbert Xu | f347c4f | 2007-10-11 16:45:17 +0800 | [diff] [blame] | 84 | struct scatterlist *dst = req->dst; |
| 85 | unsigned int cryptlen = req->cryptlen; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 86 | int err; |
| 87 | |
| 88 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth), |
| 89 | crypto_hash_alignmask(auth) + 1); |
| 90 | |
| 91 | spin_lock_bh(&ctx->auth_lock); |
| 92 | err = crypto_hash_init(&desc); |
| 93 | if (err) |
| 94 | goto auth_unlock; |
| 95 | |
| 96 | err = crypto_hash_update(&desc, req->assoc, req->assoclen); |
| 97 | if (err) |
| 98 | goto auth_unlock; |
| 99 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 100 | err = crypto_hash_update(&desc, dst, cryptlen); |
| 101 | if (err) |
| 102 | goto auth_unlock; |
| 103 | |
| 104 | err = crypto_hash_final(&desc, hash); |
| 105 | auth_unlock: |
| 106 | spin_unlock_bh(&ctx->auth_lock); |
| 107 | |
| 108 | if (err) |
| 109 | return err; |
| 110 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 111 | scatterwalk_map_and_copy(hash, dst, cryptlen, |
| 112 | crypto_aead_authsize(authenc), 1); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static void crypto_authenc_encrypt_done(struct crypto_async_request *req, |
| 117 | int err) |
| 118 | { |
| 119 | if (!err) |
| 120 | err = crypto_authenc_hash(req->data); |
| 121 | |
| 122 | aead_request_complete(req->data, err); |
| 123 | } |
| 124 | |
| 125 | static int crypto_authenc_encrypt(struct aead_request *req) |
| 126 | { |
| 127 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 128 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 129 | struct ablkcipher_request *abreq = aead_request_ctx(req); |
| 130 | int err; |
| 131 | |
| 132 | ablkcipher_request_set_tfm(abreq, ctx->enc); |
| 133 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 134 | crypto_authenc_encrypt_done, req); |
| 135 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen, |
| 136 | req->iv); |
| 137 | |
| 138 | err = crypto_ablkcipher_encrypt(abreq); |
| 139 | if (err) |
| 140 | return err; |
| 141 | |
| 142 | return crypto_authenc_hash(req); |
| 143 | } |
| 144 | |
| 145 | static int crypto_authenc_verify(struct aead_request *req) |
| 146 | { |
| 147 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 148 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 149 | struct crypto_hash *auth = ctx->auth; |
| 150 | struct hash_desc desc = { |
| 151 | .tfm = auth, |
| 152 | .flags = aead_request_flags(req), |
| 153 | }; |
| 154 | u8 *ohash = aead_request_ctx(req); |
| 155 | u8 *ihash; |
Herbert Xu | f347c4f | 2007-10-11 16:45:17 +0800 | [diff] [blame] | 156 | struct scatterlist *src = req->src; |
| 157 | unsigned int cryptlen = req->cryptlen; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 158 | unsigned int authsize; |
| 159 | int err; |
| 160 | |
| 161 | ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth), |
| 162 | crypto_hash_alignmask(auth) + 1); |
| 163 | ihash = ohash + crypto_hash_digestsize(auth); |
| 164 | |
| 165 | spin_lock_bh(&ctx->auth_lock); |
| 166 | err = crypto_hash_init(&desc); |
| 167 | if (err) |
| 168 | goto auth_unlock; |
| 169 | |
| 170 | err = crypto_hash_update(&desc, req->assoc, req->assoclen); |
| 171 | if (err) |
| 172 | goto auth_unlock; |
| 173 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 174 | err = crypto_hash_update(&desc, src, cryptlen); |
| 175 | if (err) |
| 176 | goto auth_unlock; |
| 177 | |
| 178 | err = crypto_hash_final(&desc, ohash); |
| 179 | auth_unlock: |
| 180 | spin_unlock_bh(&ctx->auth_lock); |
| 181 | |
| 182 | if (err) |
| 183 | return err; |
| 184 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 185 | authsize = crypto_aead_authsize(authenc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 186 | scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0); |
| 187 | return memcmp(ihash, ohash, authsize) ? -EINVAL : 0; |
| 188 | } |
| 189 | |
| 190 | static void crypto_authenc_decrypt_done(struct crypto_async_request *req, |
| 191 | int err) |
| 192 | { |
| 193 | aead_request_complete(req->data, err); |
| 194 | } |
| 195 | |
| 196 | static int crypto_authenc_decrypt(struct aead_request *req) |
| 197 | { |
| 198 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 199 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 200 | struct ablkcipher_request *abreq = aead_request_ctx(req); |
| 201 | int err; |
| 202 | |
| 203 | err = crypto_authenc_verify(req); |
| 204 | if (err) |
| 205 | return err; |
| 206 | |
| 207 | ablkcipher_request_set_tfm(abreq, ctx->enc); |
| 208 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 209 | crypto_authenc_decrypt_done, req); |
| 210 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen, |
| 211 | req->iv); |
| 212 | |
| 213 | return crypto_ablkcipher_decrypt(abreq); |
| 214 | } |
| 215 | |
| 216 | static int crypto_authenc_init_tfm(struct crypto_tfm *tfm) |
| 217 | { |
| 218 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 219 | struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 220 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 221 | struct crypto_hash *auth; |
| 222 | struct crypto_ablkcipher *enc; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 223 | int err; |
| 224 | |
| 225 | auth = crypto_spawn_hash(&ictx->auth); |
| 226 | if (IS_ERR(auth)) |
| 227 | return PTR_ERR(auth); |
| 228 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 229 | enc = crypto_spawn_ablkcipher(&ictx->enc); |
| 230 | err = PTR_ERR(enc); |
| 231 | if (IS_ERR(enc)) |
| 232 | goto err_free_hash; |
| 233 | |
| 234 | ctx->auth = auth; |
| 235 | ctx->enc = enc; |
| 236 | tfm->crt_aead.reqsize = max_t(unsigned int, |
| 237 | (crypto_hash_alignmask(auth) & |
| 238 | ~(crypto_tfm_ctx_alignment() - 1)) + |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 239 | crypto_hash_digestsize(auth) * 2, |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 240 | sizeof(struct ablkcipher_request) + |
| 241 | crypto_ablkcipher_reqsize(enc)); |
| 242 | |
| 243 | spin_lock_init(&ctx->auth_lock); |
| 244 | |
| 245 | return 0; |
| 246 | |
| 247 | err_free_hash: |
| 248 | crypto_free_hash(auth); |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm) |
| 253 | { |
| 254 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 255 | |
| 256 | crypto_free_hash(ctx->auth); |
| 257 | crypto_free_ablkcipher(ctx->enc); |
| 258 | } |
| 259 | |
| 260 | static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb) |
| 261 | { |
| 262 | struct crypto_instance *inst; |
| 263 | struct crypto_alg *auth; |
| 264 | struct crypto_alg *enc; |
| 265 | struct authenc_instance_ctx *ctx; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 266 | unsigned int enckeylen; |
| 267 | int err; |
| 268 | |
| 269 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD); |
| 270 | if (err) |
| 271 | return ERR_PTR(err); |
| 272 | |
| 273 | auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, |
| 274 | CRYPTO_ALG_TYPE_HASH_MASK); |
| 275 | if (IS_ERR(auth)) |
| 276 | return ERR_PTR(PTR_ERR(auth)); |
| 277 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 278 | enc = crypto_attr_alg(tb[2], CRYPTO_ALG_TYPE_BLKCIPHER, |
Herbert Xu | 332f884 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 279 | CRYPTO_ALG_TYPE_BLKCIPHER_MASK); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 280 | inst = ERR_PTR(PTR_ERR(enc)); |
| 281 | if (IS_ERR(enc)) |
| 282 | goto out_put_auth; |
| 283 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 284 | err = crypto_attr_u32(tb[3], &enckeylen); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 285 | if (err) |
| 286 | goto out_put_enc; |
| 287 | |
| 288 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 289 | err = -ENOMEM; |
| 290 | if (!inst) |
| 291 | goto out_put_enc; |
| 292 | |
| 293 | err = -ENAMETOOLONG; |
| 294 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 295 | "authenc(%s,%s,%u)", auth->cra_name, |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 296 | enc->cra_name, enckeylen) >= CRYPTO_MAX_ALG_NAME) |
| 297 | goto err_free_inst; |
| 298 | |
| 299 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 300 | "authenc(%s,%s,%u)", auth->cra_driver_name, |
| 301 | enc->cra_driver_name, enckeylen) >= |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 302 | CRYPTO_MAX_ALG_NAME) |
| 303 | goto err_free_inst; |
| 304 | |
| 305 | ctx = crypto_instance_ctx(inst); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 306 | ctx->enckeylen = enckeylen; |
| 307 | |
| 308 | err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK); |
| 309 | if (err) |
| 310 | goto err_free_inst; |
| 311 | |
| 312 | err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK); |
| 313 | if (err) |
| 314 | goto err_drop_auth; |
| 315 | |
| 316 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC; |
| 317 | inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority; |
| 318 | inst->alg.cra_blocksize = enc->cra_blocksize; |
Herbert Xu | e29bc6a | 2007-11-22 22:46:40 +0800 | [diff] [blame] | 319 | inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 320 | inst->alg.cra_type = &crypto_aead_type; |
| 321 | |
| 322 | inst->alg.cra_aead.ivsize = enc->cra_blkcipher.ivsize; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame^] | 323 | inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ? |
| 324 | auth->cra_hash.digestsize : |
| 325 | auth->cra_digest.dia_digestsize; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 326 | |
| 327 | inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx); |
| 328 | |
| 329 | inst->alg.cra_init = crypto_authenc_init_tfm; |
| 330 | inst->alg.cra_exit = crypto_authenc_exit_tfm; |
| 331 | |
| 332 | inst->alg.cra_aead.setkey = crypto_authenc_setkey; |
| 333 | inst->alg.cra_aead.encrypt = crypto_authenc_encrypt; |
| 334 | inst->alg.cra_aead.decrypt = crypto_authenc_decrypt; |
| 335 | |
| 336 | out: |
| 337 | crypto_mod_put(enc); |
| 338 | out_put_auth: |
| 339 | crypto_mod_put(auth); |
| 340 | return inst; |
| 341 | |
| 342 | err_drop_auth: |
| 343 | crypto_drop_spawn(&ctx->auth); |
| 344 | err_free_inst: |
| 345 | kfree(inst); |
| 346 | out_put_enc: |
| 347 | inst = ERR_PTR(err); |
| 348 | goto out; |
| 349 | } |
| 350 | |
| 351 | static void crypto_authenc_free(struct crypto_instance *inst) |
| 352 | { |
| 353 | struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 354 | |
| 355 | crypto_drop_spawn(&ctx->enc); |
| 356 | crypto_drop_spawn(&ctx->auth); |
| 357 | kfree(inst); |
| 358 | } |
| 359 | |
| 360 | static struct crypto_template crypto_authenc_tmpl = { |
| 361 | .name = "authenc", |
| 362 | .alloc = crypto_authenc_alloc, |
| 363 | .free = crypto_authenc_free, |
| 364 | .module = THIS_MODULE, |
| 365 | }; |
| 366 | |
| 367 | static int __init crypto_authenc_module_init(void) |
| 368 | { |
| 369 | return crypto_register_template(&crypto_authenc_tmpl); |
| 370 | } |
| 371 | |
| 372 | static void __exit crypto_authenc_module_exit(void) |
| 373 | { |
| 374 | crypto_unregister_template(&crypto_authenc_tmpl); |
| 375 | } |
| 376 | |
| 377 | module_init(crypto_authenc_module_init); |
| 378 | module_exit(crypto_authenc_module_exit); |
| 379 | |
| 380 | MODULE_LICENSE("GPL"); |
| 381 | MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec"); |