.. | .. |
---|
78 | 78 | { |
---|
79 | 79 | struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent); |
---|
80 | 80 | struct crypto_skcipher *child = ctx->child; |
---|
81 | | - int err; |
---|
82 | 81 | |
---|
83 | 82 | crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
---|
84 | 83 | crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
---|
85 | 84 | CRYPTO_TFM_REQ_MASK); |
---|
86 | | - err = crypto_skcipher_setkey(child, key, keylen); |
---|
87 | | - crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) & |
---|
88 | | - CRYPTO_TFM_RES_MASK); |
---|
89 | | - return err; |
---|
| 85 | + return crypto_skcipher_setkey(child, key, keylen); |
---|
90 | 86 | } |
---|
91 | 87 | |
---|
92 | 88 | static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err) |
---|
.. | .. |
---|
152 | 148 | struct skcipher_request *subreq = &rctx->subreq; |
---|
153 | 149 | int bsize = crypto_skcipher_blocksize(tfm); |
---|
154 | 150 | unsigned int nbytes = req->cryptlen; |
---|
155 | | - int cbc_blocks = (nbytes + bsize - 1) / bsize - 1; |
---|
156 | 151 | unsigned int offset; |
---|
157 | 152 | |
---|
158 | 153 | skcipher_request_set_tfm(subreq, ctx->child); |
---|
159 | 154 | |
---|
160 | | - if (cbc_blocks <= 0) { |
---|
| 155 | + if (nbytes < bsize) |
---|
| 156 | + return -EINVAL; |
---|
| 157 | + |
---|
| 158 | + if (nbytes == bsize) { |
---|
161 | 159 | skcipher_request_set_callback(subreq, req->base.flags, |
---|
162 | 160 | req->base.complete, |
---|
163 | 161 | req->base.data); |
---|
.. | .. |
---|
166 | 164 | return crypto_skcipher_encrypt(subreq); |
---|
167 | 165 | } |
---|
168 | 166 | |
---|
169 | | - offset = cbc_blocks * bsize; |
---|
| 167 | + offset = rounddown(nbytes - 1, bsize); |
---|
170 | 168 | rctx->offset = offset; |
---|
171 | 169 | |
---|
172 | 170 | skcipher_request_set_callback(subreq, req->base.flags, |
---|
.. | .. |
---|
244 | 242 | struct skcipher_request *subreq = &rctx->subreq; |
---|
245 | 243 | int bsize = crypto_skcipher_blocksize(tfm); |
---|
246 | 244 | unsigned int nbytes = req->cryptlen; |
---|
247 | | - int cbc_blocks = (nbytes + bsize - 1) / bsize - 1; |
---|
248 | 245 | unsigned int offset; |
---|
249 | 246 | u8 *space; |
---|
250 | 247 | |
---|
251 | 248 | skcipher_request_set_tfm(subreq, ctx->child); |
---|
252 | 249 | |
---|
253 | | - if (cbc_blocks <= 0) { |
---|
| 250 | + if (nbytes < bsize) |
---|
| 251 | + return -EINVAL; |
---|
| 252 | + |
---|
| 253 | + if (nbytes == bsize) { |
---|
254 | 254 | skcipher_request_set_callback(subreq, req->base.flags, |
---|
255 | 255 | req->base.complete, |
---|
256 | 256 | req->base.data); |
---|
.. | .. |
---|
264 | 264 | |
---|
265 | 265 | space = crypto_cts_reqctx_space(req); |
---|
266 | 266 | |
---|
267 | | - offset = cbc_blocks * bsize; |
---|
| 267 | + offset = rounddown(nbytes - 1, bsize); |
---|
268 | 268 | rctx->offset = offset; |
---|
269 | 269 | |
---|
270 | | - if (cbc_blocks <= 1) |
---|
| 270 | + if (offset <= bsize) |
---|
271 | 271 | memcpy(space, req->iv, bsize); |
---|
272 | 272 | else |
---|
273 | 273 | scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize, |
---|
.. | .. |
---|
325 | 325 | { |
---|
326 | 326 | struct crypto_skcipher_spawn *spawn; |
---|
327 | 327 | struct skcipher_instance *inst; |
---|
328 | | - struct crypto_attr_type *algt; |
---|
329 | 328 | struct skcipher_alg *alg; |
---|
330 | | - const char *cipher_name; |
---|
| 329 | + u32 mask; |
---|
331 | 330 | int err; |
---|
332 | 331 | |
---|
333 | | - algt = crypto_get_attr_type(tb); |
---|
334 | | - if (IS_ERR(algt)) |
---|
335 | | - return PTR_ERR(algt); |
---|
336 | | - |
---|
337 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) |
---|
338 | | - return -EINVAL; |
---|
339 | | - |
---|
340 | | - cipher_name = crypto_attr_alg_name(tb[1]); |
---|
341 | | - if (IS_ERR(cipher_name)) |
---|
342 | | - return PTR_ERR(cipher_name); |
---|
| 332 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask); |
---|
| 333 | + if (err) |
---|
| 334 | + return err; |
---|
343 | 335 | |
---|
344 | 336 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
---|
345 | 337 | if (!inst) |
---|
.. | .. |
---|
347 | 339 | |
---|
348 | 340 | spawn = skcipher_instance_ctx(inst); |
---|
349 | 341 | |
---|
350 | | - crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); |
---|
351 | | - err = crypto_grab_skcipher(spawn, cipher_name, 0, |
---|
352 | | - crypto_requires_sync(algt->type, |
---|
353 | | - algt->mask)); |
---|
| 342 | + err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst), |
---|
| 343 | + crypto_attr_alg_name(tb[1]), 0, mask); |
---|
354 | 344 | if (err) |
---|
355 | 345 | goto err_free_inst; |
---|
356 | 346 | |
---|
.. | .. |
---|
358 | 348 | |
---|
359 | 349 | err = -EINVAL; |
---|
360 | 350 | if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize) |
---|
361 | | - goto err_drop_spawn; |
---|
| 351 | + goto err_free_inst; |
---|
362 | 352 | |
---|
363 | 353 | if (strncmp(alg->base.cra_name, "cbc(", 4)) |
---|
364 | | - goto err_drop_spawn; |
---|
| 354 | + goto err_free_inst; |
---|
365 | 355 | |
---|
366 | 356 | err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts", |
---|
367 | 357 | &alg->base); |
---|
368 | 358 | if (err) |
---|
369 | | - goto err_drop_spawn; |
---|
| 359 | + goto err_free_inst; |
---|
370 | 360 | |
---|
371 | | - inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
---|
372 | 361 | inst->alg.base.cra_priority = alg->base.cra_priority; |
---|
373 | 362 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
---|
374 | 363 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
---|
.. | .. |
---|
390 | 379 | inst->free = crypto_cts_free; |
---|
391 | 380 | |
---|
392 | 381 | err = skcipher_register_instance(tmpl, inst); |
---|
393 | | - if (err) |
---|
394 | | - goto err_drop_spawn; |
---|
395 | | - |
---|
396 | | -out: |
---|
397 | | - return err; |
---|
398 | | - |
---|
399 | | -err_drop_spawn: |
---|
400 | | - crypto_drop_skcipher(spawn); |
---|
| 382 | + if (err) { |
---|
401 | 383 | err_free_inst: |
---|
402 | | - kfree(inst); |
---|
403 | | - goto out; |
---|
| 384 | + crypto_cts_free(inst); |
---|
| 385 | + } |
---|
| 386 | + return err; |
---|
404 | 387 | } |
---|
405 | 388 | |
---|
406 | 389 | static struct crypto_template crypto_cts_tmpl = { |
---|
.. | .. |
---|
419 | 402 | crypto_unregister_template(&crypto_cts_tmpl); |
---|
420 | 403 | } |
---|
421 | 404 | |
---|
422 | | -module_init(crypto_cts_module_init); |
---|
| 405 | +subsys_initcall(crypto_cts_module_init); |
---|
423 | 406 | module_exit(crypto_cts_module_exit); |
---|
424 | 407 | |
---|
425 | 408 | MODULE_LICENSE("Dual BSD/GPL"); |
---|