Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * chainiv: Chain IV Generator |
| 3 | * |
| 4 | * Generate IVs simply be using the last block of the previous encryption. |
| 5 | * This is mainly useful for CBC with a synchronous algorithm. |
| 6 | * |
| 7 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <crypto/internal/skcipher.h> |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 17 | #include <crypto/rng.h> |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 21 | #include <linux/module.h> |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/string.h> |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
| 25 | |
| 26 | enum { |
| 27 | CHAINIV_STATE_INUSE = 0, |
| 28 | }; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 29 | |
| 30 | struct chainiv_ctx { |
| 31 | spinlock_t lock; |
| 32 | char iv[]; |
| 33 | }; |
| 34 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 35 | struct async_chainiv_ctx { |
| 36 | unsigned long state; |
| 37 | |
| 38 | spinlock_t lock; |
| 39 | int err; |
| 40 | |
| 41 | struct crypto_queue queue; |
| 42 | struct work_struct postponed; |
| 43 | |
| 44 | char iv[]; |
| 45 | }; |
| 46 | |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 47 | static int chainiv_givencrypt(struct skcipher_givcrypt_request *req) |
| 48 | { |
| 49 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 50 | struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 51 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 52 | unsigned int ivsize; |
| 53 | int err; |
| 54 | |
| 55 | ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv)); |
| 56 | ablkcipher_request_set_callback(subreq, req->creq.base.flags & |
| 57 | ~CRYPTO_TFM_REQ_MAY_SLEEP, |
| 58 | req->creq.base.complete, |
| 59 | req->creq.base.data); |
| 60 | ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst, |
| 61 | req->creq.nbytes, req->creq.info); |
| 62 | |
| 63 | spin_lock_bh(&ctx->lock); |
| 64 | |
| 65 | ivsize = crypto_ablkcipher_ivsize(geniv); |
| 66 | |
| 67 | memcpy(req->giv, ctx->iv, ivsize); |
| 68 | memcpy(subreq->info, ctx->iv, ivsize); |
| 69 | |
| 70 | err = crypto_ablkcipher_encrypt(subreq); |
| 71 | if (err) |
| 72 | goto unlock; |
| 73 | |
| 74 | memcpy(ctx->iv, subreq->info, ivsize); |
| 75 | |
| 76 | unlock: |
| 77 | spin_unlock_bh(&ctx->lock); |
| 78 | |
| 79 | return err; |
| 80 | } |
| 81 | |
| 82 | static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) |
| 83 | { |
| 84 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 85 | struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 86 | int err = 0; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 87 | |
| 88 | spin_lock_bh(&ctx->lock); |
| 89 | if (crypto_ablkcipher_crt(geniv)->givencrypt != |
| 90 | chainiv_givencrypt_first) |
| 91 | goto unlock; |
| 92 | |
| 93 | crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 94 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv, |
| 95 | crypto_ablkcipher_ivsize(geniv)); |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 96 | |
| 97 | unlock: |
| 98 | spin_unlock_bh(&ctx->lock); |
| 99 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 100 | if (err) |
| 101 | return err; |
| 102 | |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 103 | return chainiv_givencrypt(req); |
| 104 | } |
| 105 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 106 | static int chainiv_init_common(struct crypto_tfm *tfm) |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 107 | { |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 108 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request); |
| 109 | |
| 110 | return skcipher_geniv_init(tfm); |
| 111 | } |
| 112 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 113 | static int chainiv_init(struct crypto_tfm *tfm) |
| 114 | { |
| 115 | struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm); |
| 116 | |
| 117 | spin_lock_init(&ctx->lock); |
| 118 | |
| 119 | return chainiv_init_common(tfm); |
| 120 | } |
| 121 | |
| 122 | static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx) |
| 123 | { |
| 124 | int queued; |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 125 | int err = ctx->err; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 126 | |
| 127 | if (!ctx->queue.qlen) { |
| 128 | smp_mb__before_clear_bit(); |
| 129 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
| 130 | |
| 131 | if (!ctx->queue.qlen || |
| 132 | test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 133 | goto out; |
| 134 | } |
| 135 | |
| 136 | queued = schedule_work(&ctx->postponed); |
| 137 | BUG_ON(!queued); |
| 138 | |
| 139 | out: |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 140 | return err; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req) |
| 144 | { |
| 145 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 146 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 147 | int err; |
| 148 | |
| 149 | spin_lock_bh(&ctx->lock); |
| 150 | err = skcipher_enqueue_givcrypt(&ctx->queue, req); |
| 151 | spin_unlock_bh(&ctx->lock); |
| 152 | |
| 153 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 154 | return err; |
| 155 | |
| 156 | ctx->err = err; |
| 157 | return async_chainiv_schedule_work(ctx); |
| 158 | } |
| 159 | |
| 160 | static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req) |
| 161 | { |
| 162 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 163 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 164 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 165 | unsigned int ivsize = crypto_ablkcipher_ivsize(geniv); |
| 166 | |
| 167 | memcpy(req->giv, ctx->iv, ivsize); |
| 168 | memcpy(subreq->info, ctx->iv, ivsize); |
| 169 | |
| 170 | ctx->err = crypto_ablkcipher_encrypt(subreq); |
| 171 | if (ctx->err) |
| 172 | goto out; |
| 173 | |
| 174 | memcpy(ctx->iv, subreq->info, ivsize); |
| 175 | |
| 176 | out: |
| 177 | return async_chainiv_schedule_work(ctx); |
| 178 | } |
| 179 | |
| 180 | static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req) |
| 181 | { |
| 182 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 183 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 184 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 185 | |
| 186 | ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv)); |
| 187 | ablkcipher_request_set_callback(subreq, req->creq.base.flags, |
| 188 | req->creq.base.complete, |
| 189 | req->creq.base.data); |
| 190 | ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst, |
| 191 | req->creq.nbytes, req->creq.info); |
| 192 | |
| 193 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 194 | goto postpone; |
| 195 | |
| 196 | if (ctx->queue.qlen) { |
| 197 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
| 198 | goto postpone; |
| 199 | } |
| 200 | |
| 201 | return async_chainiv_givencrypt_tail(req); |
| 202 | |
| 203 | postpone: |
| 204 | return async_chainiv_postpone_request(req); |
| 205 | } |
| 206 | |
| 207 | static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) |
| 208 | { |
| 209 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 210 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 211 | int err = 0; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 212 | |
| 213 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 214 | goto out; |
| 215 | |
| 216 | if (crypto_ablkcipher_crt(geniv)->givencrypt != |
| 217 | async_chainiv_givencrypt_first) |
| 218 | goto unlock; |
| 219 | |
| 220 | crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 221 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv, |
| 222 | crypto_ablkcipher_ivsize(geniv)); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 223 | |
| 224 | unlock: |
| 225 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
| 226 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 227 | if (err) |
| 228 | return err; |
| 229 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 230 | out: |
| 231 | return async_chainiv_givencrypt(req); |
| 232 | } |
| 233 | |
| 234 | static void async_chainiv_do_postponed(struct work_struct *work) |
| 235 | { |
| 236 | struct async_chainiv_ctx *ctx = container_of(work, |
| 237 | struct async_chainiv_ctx, |
| 238 | postponed); |
| 239 | struct skcipher_givcrypt_request *req; |
| 240 | struct ablkcipher_request *subreq; |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 241 | int err; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 242 | |
| 243 | /* Only handle one request at a time to avoid hogging keventd. */ |
| 244 | spin_lock_bh(&ctx->lock); |
| 245 | req = skcipher_dequeue_givcrypt(&ctx->queue); |
| 246 | spin_unlock_bh(&ctx->lock); |
| 247 | |
| 248 | if (!req) { |
| 249 | async_chainiv_schedule_work(ctx); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | subreq = skcipher_givcrypt_reqctx(req); |
| 254 | subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP; |
| 255 | |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 256 | err = async_chainiv_givencrypt_tail(req); |
| 257 | |
| 258 | local_bh_disable(); |
| 259 | skcipher_givcrypt_complete(req, err); |
| 260 | local_bh_enable(); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static int async_chainiv_init(struct crypto_tfm *tfm) |
| 264 | { |
| 265 | struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm); |
| 266 | |
| 267 | spin_lock_init(&ctx->lock); |
| 268 | |
| 269 | crypto_init_queue(&ctx->queue, 100); |
| 270 | INIT_WORK(&ctx->postponed, async_chainiv_do_postponed); |
| 271 | |
| 272 | return chainiv_init_common(tfm); |
| 273 | } |
| 274 | |
| 275 | static void async_chainiv_exit(struct crypto_tfm *tfm) |
| 276 | { |
| 277 | struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm); |
| 278 | |
| 279 | BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen); |
| 280 | |
| 281 | skcipher_geniv_exit(tfm); |
| 282 | } |
| 283 | |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 284 | static struct crypto_template chainiv_tmpl; |
| 285 | |
| 286 | static struct crypto_instance *chainiv_alloc(struct rtattr **tb) |
| 287 | { |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 288 | struct crypto_attr_type *algt; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 289 | struct crypto_instance *inst; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 290 | int err; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 291 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 292 | algt = crypto_get_attr_type(tb); |
| 293 | err = PTR_ERR(algt); |
| 294 | if (IS_ERR(algt)) |
| 295 | return ERR_PTR(err); |
| 296 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 297 | err = crypto_get_default_rng(); |
| 298 | if (err) |
| 299 | return ERR_PTR(err); |
| 300 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 301 | inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0); |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 302 | if (IS_ERR(inst)) |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 303 | goto put_rng; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 304 | |
| 305 | inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first; |
| 306 | |
| 307 | inst->alg.cra_init = chainiv_init; |
| 308 | inst->alg.cra_exit = skcipher_geniv_exit; |
| 309 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 310 | inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx); |
| 311 | |
| 312 | if (!crypto_requires_sync(algt->type, algt->mask)) { |
| 313 | inst->alg.cra_flags |= CRYPTO_ALG_ASYNC; |
| 314 | |
| 315 | inst->alg.cra_ablkcipher.givencrypt = |
| 316 | async_chainiv_givencrypt_first; |
| 317 | |
| 318 | inst->alg.cra_init = async_chainiv_init; |
| 319 | inst->alg.cra_exit = async_chainiv_exit; |
| 320 | |
| 321 | inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx); |
| 322 | } |
| 323 | |
| 324 | inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 325 | |
| 326 | out: |
| 327 | return inst; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 328 | |
| 329 | put_rng: |
| 330 | crypto_put_default_rng(); |
| 331 | goto out; |
| 332 | } |
| 333 | |
| 334 | static void chainiv_free(struct crypto_instance *inst) |
| 335 | { |
| 336 | skcipher_geniv_free(inst); |
| 337 | crypto_put_default_rng(); |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static struct crypto_template chainiv_tmpl = { |
| 341 | .name = "chainiv", |
| 342 | .alloc = chainiv_alloc, |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame^] | 343 | .free = chainiv_free, |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 344 | .module = THIS_MODULE, |
| 345 | }; |
| 346 | |
Herbert Xu | 5be5e66 | 2008-08-17 18:04:30 +1000 | [diff] [blame] | 347 | static int __init chainiv_module_init(void) |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 348 | { |
| 349 | return crypto_register_template(&chainiv_tmpl); |
| 350 | } |
| 351 | |
Herbert Xu | 5be5e66 | 2008-08-17 18:04:30 +1000 | [diff] [blame] | 352 | static void chainiv_module_exit(void) |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 353 | { |
| 354 | crypto_unregister_template(&chainiv_tmpl); |
| 355 | } |
Herbert Xu | 5be5e66 | 2008-08-17 18:04:30 +1000 | [diff] [blame] | 356 | |
| 357 | module_init(chainiv_module_init); |
| 358 | module_exit(chainiv_module_exit); |
| 359 | |
| 360 | MODULE_LICENSE("GPL"); |
| 361 | MODULE_DESCRIPTION("Chain IV Generator"); |