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> |
Huang Ying | 0a2e821 | 2009-02-19 14:44:02 +0800 | [diff] [blame] | 18 | #include <crypto/crypto_wq.h> |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 22 | #include <linux/module.h> |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/string.h> |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
| 26 | |
| 27 | enum { |
| 28 | CHAINIV_STATE_INUSE = 0, |
| 29 | }; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 30 | |
| 31 | struct chainiv_ctx { |
| 32 | spinlock_t lock; |
| 33 | char iv[]; |
| 34 | }; |
| 35 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 36 | struct async_chainiv_ctx { |
| 37 | unsigned long state; |
| 38 | |
| 39 | spinlock_t lock; |
| 40 | int err; |
| 41 | |
| 42 | struct crypto_queue queue; |
| 43 | struct work_struct postponed; |
| 44 | |
| 45 | char iv[]; |
| 46 | }; |
| 47 | |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 48 | static int chainiv_givencrypt(struct skcipher_givcrypt_request *req) |
| 49 | { |
| 50 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 51 | struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 52 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 53 | unsigned int ivsize; |
| 54 | int err; |
| 55 | |
| 56 | ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv)); |
| 57 | ablkcipher_request_set_callback(subreq, req->creq.base.flags & |
| 58 | ~CRYPTO_TFM_REQ_MAY_SLEEP, |
| 59 | req->creq.base.complete, |
| 60 | req->creq.base.data); |
| 61 | ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst, |
| 62 | req->creq.nbytes, req->creq.info); |
| 63 | |
| 64 | spin_lock_bh(&ctx->lock); |
| 65 | |
| 66 | ivsize = crypto_ablkcipher_ivsize(geniv); |
| 67 | |
| 68 | memcpy(req->giv, ctx->iv, ivsize); |
| 69 | memcpy(subreq->info, ctx->iv, ivsize); |
| 70 | |
| 71 | err = crypto_ablkcipher_encrypt(subreq); |
| 72 | if (err) |
| 73 | goto unlock; |
| 74 | |
| 75 | memcpy(ctx->iv, subreq->info, ivsize); |
| 76 | |
| 77 | unlock: |
| 78 | spin_unlock_bh(&ctx->lock); |
| 79 | |
| 80 | return err; |
| 81 | } |
| 82 | |
| 83 | static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) |
| 84 | { |
| 85 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 86 | struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 87 | int err = 0; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 88 | |
| 89 | spin_lock_bh(&ctx->lock); |
| 90 | if (crypto_ablkcipher_crt(geniv)->givencrypt != |
| 91 | chainiv_givencrypt_first) |
| 92 | goto unlock; |
| 93 | |
| 94 | crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 95 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv, |
| 96 | crypto_ablkcipher_ivsize(geniv)); |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 97 | |
| 98 | unlock: |
| 99 | spin_unlock_bh(&ctx->lock); |
| 100 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 101 | if (err) |
| 102 | return err; |
| 103 | |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 104 | return chainiv_givencrypt(req); |
| 105 | } |
| 106 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 107 | static int chainiv_init_common(struct crypto_tfm *tfm) |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 108 | { |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 109 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request); |
| 110 | |
| 111 | return skcipher_geniv_init(tfm); |
| 112 | } |
| 113 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 114 | static int chainiv_init(struct crypto_tfm *tfm) |
| 115 | { |
| 116 | struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm); |
| 117 | |
| 118 | spin_lock_init(&ctx->lock); |
| 119 | |
| 120 | return chainiv_init_common(tfm); |
| 121 | } |
| 122 | |
| 123 | static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx) |
| 124 | { |
| 125 | int queued; |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 126 | int err = ctx->err; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 127 | |
| 128 | if (!ctx->queue.qlen) { |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 129 | smp_mb__before_atomic(); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 130 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
| 131 | |
| 132 | if (!ctx->queue.qlen || |
| 133 | test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 134 | goto out; |
| 135 | } |
| 136 | |
Huang Ying | 0a2e821 | 2009-02-19 14:44:02 +0800 | [diff] [blame] | 137 | queued = queue_work(kcrypto_wq, &ctx->postponed); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 138 | BUG_ON(!queued); |
| 139 | |
| 140 | out: |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 141 | return err; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req) |
| 145 | { |
| 146 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 147 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 148 | int err; |
| 149 | |
| 150 | spin_lock_bh(&ctx->lock); |
| 151 | err = skcipher_enqueue_givcrypt(&ctx->queue, req); |
| 152 | spin_unlock_bh(&ctx->lock); |
| 153 | |
| 154 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 155 | return err; |
| 156 | |
| 157 | ctx->err = err; |
| 158 | return async_chainiv_schedule_work(ctx); |
| 159 | } |
| 160 | |
| 161 | static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req) |
| 162 | { |
| 163 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 164 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 165 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 166 | unsigned int ivsize = crypto_ablkcipher_ivsize(geniv); |
| 167 | |
| 168 | memcpy(req->giv, ctx->iv, ivsize); |
| 169 | memcpy(subreq->info, ctx->iv, ivsize); |
| 170 | |
| 171 | ctx->err = crypto_ablkcipher_encrypt(subreq); |
| 172 | if (ctx->err) |
| 173 | goto out; |
| 174 | |
| 175 | memcpy(ctx->iv, subreq->info, ivsize); |
| 176 | |
| 177 | out: |
| 178 | return async_chainiv_schedule_work(ctx); |
| 179 | } |
| 180 | |
| 181 | static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req) |
| 182 | { |
| 183 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 184 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 185 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 186 | |
| 187 | ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv)); |
| 188 | ablkcipher_request_set_callback(subreq, req->creq.base.flags, |
| 189 | req->creq.base.complete, |
| 190 | req->creq.base.data); |
| 191 | ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst, |
| 192 | req->creq.nbytes, req->creq.info); |
| 193 | |
| 194 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 195 | goto postpone; |
| 196 | |
| 197 | if (ctx->queue.qlen) { |
| 198 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
| 199 | goto postpone; |
| 200 | } |
| 201 | |
| 202 | return async_chainiv_givencrypt_tail(req); |
| 203 | |
| 204 | postpone: |
| 205 | return async_chainiv_postpone_request(req); |
| 206 | } |
| 207 | |
| 208 | static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) |
| 209 | { |
| 210 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 211 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 212 | int err = 0; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 213 | |
| 214 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
| 215 | goto out; |
| 216 | |
| 217 | if (crypto_ablkcipher_crt(geniv)->givencrypt != |
| 218 | async_chainiv_givencrypt_first) |
| 219 | goto unlock; |
| 220 | |
| 221 | crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 222 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv, |
| 223 | crypto_ablkcipher_ivsize(geniv)); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 224 | |
| 225 | unlock: |
| 226 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
| 227 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 228 | if (err) |
| 229 | return err; |
| 230 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 231 | out: |
| 232 | return async_chainiv_givencrypt(req); |
| 233 | } |
| 234 | |
| 235 | static void async_chainiv_do_postponed(struct work_struct *work) |
| 236 | { |
| 237 | struct async_chainiv_ctx *ctx = container_of(work, |
| 238 | struct async_chainiv_ctx, |
| 239 | postponed); |
| 240 | struct skcipher_givcrypt_request *req; |
| 241 | struct ablkcipher_request *subreq; |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 242 | int err; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 243 | |
| 244 | /* Only handle one request at a time to avoid hogging keventd. */ |
| 245 | spin_lock_bh(&ctx->lock); |
| 246 | req = skcipher_dequeue_givcrypt(&ctx->queue); |
| 247 | spin_unlock_bh(&ctx->lock); |
| 248 | |
| 249 | if (!req) { |
| 250 | async_chainiv_schedule_work(ctx); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | subreq = skcipher_givcrypt_reqctx(req); |
| 255 | subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP; |
| 256 | |
Herbert Xu | 872ac87 | 2008-07-10 17:42:36 +0800 | [diff] [blame] | 257 | err = async_chainiv_givencrypt_tail(req); |
| 258 | |
| 259 | local_bh_disable(); |
| 260 | skcipher_givcrypt_complete(req, err); |
| 261 | local_bh_enable(); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int async_chainiv_init(struct crypto_tfm *tfm) |
| 265 | { |
| 266 | struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm); |
| 267 | |
| 268 | spin_lock_init(&ctx->lock); |
| 269 | |
| 270 | crypto_init_queue(&ctx->queue, 100); |
| 271 | INIT_WORK(&ctx->postponed, async_chainiv_do_postponed); |
| 272 | |
| 273 | return chainiv_init_common(tfm); |
| 274 | } |
| 275 | |
| 276 | static void async_chainiv_exit(struct crypto_tfm *tfm) |
| 277 | { |
| 278 | struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm); |
| 279 | |
| 280 | BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen); |
| 281 | |
| 282 | skcipher_geniv_exit(tfm); |
| 283 | } |
| 284 | |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 285 | static struct crypto_template chainiv_tmpl; |
| 286 | |
| 287 | static struct crypto_instance *chainiv_alloc(struct rtattr **tb) |
| 288 | { |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 289 | struct crypto_attr_type *algt; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 290 | struct crypto_instance *inst; |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 291 | int err; |
Herbert Xu | 7f47073 | 2007-11-27 23:17:23 +0800 | [diff] [blame] | 292 | |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 293 | algt = crypto_get_attr_type(tb); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 294 | if (IS_ERR(algt)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 295 | return ERR_CAST(algt); |
Herbert Xu | e7cd251 | 2007-12-14 22:28:14 +0800 | [diff] [blame] | 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"); |