summaryrefslogtreecommitdiffstats
path: root/crypto/ctr.c
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2016-07-12 01:17:37 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-07-18 05:35:39 -0400
commitb2b39c2f976bc7b69e26dbe4530593d2867544eb (patch)
tree91eb1a6f7af861097d92304f8c06460e67009c30 /crypto/ctr.c
parente75445a8445215ab4623f180cbc930ecf271181f (diff)
crypto: ctr - Use skcipher in rfc3686
This patch converts rfc3686 to use the new skcipher interface as opposed to ablkcipher. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/ctr.c')
-rw-r--r--crypto/ctr.c183
1 files changed, 94 insertions, 89 deletions
diff --git a/crypto/ctr.c b/crypto/ctr.c
index 2386f7313952..ff4d21eddb83 100644
--- a/crypto/ctr.c
+++ b/crypto/ctr.c
@@ -26,13 +26,13 @@ struct crypto_ctr_ctx {
26}; 26};
27 27
28struct crypto_rfc3686_ctx { 28struct crypto_rfc3686_ctx {
29 struct crypto_ablkcipher *child; 29 struct crypto_skcipher *child;
30 u8 nonce[CTR_RFC3686_NONCE_SIZE]; 30 u8 nonce[CTR_RFC3686_NONCE_SIZE];
31}; 31};
32 32
33struct crypto_rfc3686_req_ctx { 33struct crypto_rfc3686_req_ctx {
34 u8 iv[CTR_RFC3686_BLOCK_SIZE]; 34 u8 iv[CTR_RFC3686_BLOCK_SIZE];
35 struct ablkcipher_request subreq CRYPTO_MINALIGN_ATTR; 35 struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
36}; 36};
37 37
38static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key, 38static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
@@ -249,11 +249,11 @@ static struct crypto_template crypto_ctr_tmpl = {
249 .module = THIS_MODULE, 249 .module = THIS_MODULE,
250}; 250};
251 251
252static int crypto_rfc3686_setkey(struct crypto_ablkcipher *parent, 252static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
253 const u8 *key, unsigned int keylen) 253 const u8 *key, unsigned int keylen)
254{ 254{
255 struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(parent); 255 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
256 struct crypto_ablkcipher *child = ctx->child; 256 struct crypto_skcipher *child = ctx->child;
257 int err; 257 int err;
258 258
259 /* the nonce is stored in bytes at end of key */ 259 /* the nonce is stored in bytes at end of key */
@@ -265,173 +265,178 @@ static int crypto_rfc3686_setkey(struct crypto_ablkcipher *parent,
265 265
266 keylen -= CTR_RFC3686_NONCE_SIZE; 266 keylen -= CTR_RFC3686_NONCE_SIZE;
267 267
268 crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 268 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
269 crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) & 269 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
270 CRYPTO_TFM_REQ_MASK); 270 CRYPTO_TFM_REQ_MASK);
271 err = crypto_ablkcipher_setkey(child, key, keylen); 271 err = crypto_skcipher_setkey(child, key, keylen);
272 crypto_ablkcipher_set_flags(parent, crypto_ablkcipher_get_flags(child) & 272 crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
273 CRYPTO_TFM_RES_MASK); 273 CRYPTO_TFM_RES_MASK);
274 274
275 return err; 275 return err;
276} 276}
277 277
278static int crypto_rfc3686_crypt(struct ablkcipher_request *req) 278static int crypto_rfc3686_crypt(struct skcipher_request *req)
279{ 279{
280 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); 280 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
281 struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(tfm); 281 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
282 struct crypto_ablkcipher *child = ctx->child; 282 struct crypto_skcipher *child = ctx->child;
283 unsigned long align = crypto_ablkcipher_alignmask(tfm); 283 unsigned long align = crypto_skcipher_alignmask(tfm);
284 struct crypto_rfc3686_req_ctx *rctx = 284 struct crypto_rfc3686_req_ctx *rctx =
285 (void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1); 285 (void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
286 struct ablkcipher_request *subreq = &rctx->subreq; 286 struct skcipher_request *subreq = &rctx->subreq;
287 u8 *iv = rctx->iv; 287 u8 *iv = rctx->iv;
288 288
289 /* set up counter block */ 289 /* set up counter block */
290 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE); 290 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
291 memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->info, CTR_RFC3686_IV_SIZE); 291 memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
292 292
293 /* initialize counter portion of counter block */ 293 /* initialize counter portion of counter block */
294 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) = 294 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
295 cpu_to_be32(1); 295 cpu_to_be32(1);
296 296
297 ablkcipher_request_set_tfm(subreq, child); 297 skcipher_request_set_tfm(subreq, child);
298 ablkcipher_request_set_callback(subreq, req->base.flags, 298 skcipher_request_set_callback(subreq, req->base.flags,
299 req->base.complete, req->base.data); 299 req->base.complete, req->base.data);
300 ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->nbytes, 300 skcipher_request_set_crypt(subreq, req->src, req->dst,
301 iv); 301 req->cryptlen, iv);
302 302
303 return crypto_ablkcipher_encrypt(subreq); 303 return crypto_skcipher_encrypt(subreq);
304} 304}
305 305
306static int crypto_rfc3686_init_tfm(struct crypto_tfm *tfm) 306static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
307{ 307{
308 struct crypto_instance *inst = (void *)tfm->__crt_alg; 308 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
309 struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst); 309 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
310 struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm); 310 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
311 struct crypto_ablkcipher *cipher; 311 struct crypto_skcipher *cipher;
312 unsigned long align; 312 unsigned long align;
313 unsigned int reqsize;
313 314
314 cipher = crypto_spawn_skcipher(spawn); 315 cipher = crypto_spawn_skcipher2(spawn);
315 if (IS_ERR(cipher)) 316 if (IS_ERR(cipher))
316 return PTR_ERR(cipher); 317 return PTR_ERR(cipher);
317 318
318 ctx->child = cipher; 319 ctx->child = cipher;
319 320
320 align = crypto_tfm_alg_alignmask(tfm); 321 align = crypto_skcipher_alignmask(tfm);
321 align &= ~(crypto_tfm_ctx_alignment() - 1); 322 align &= ~(crypto_tfm_ctx_alignment() - 1);
322 tfm->crt_ablkcipher.reqsize = align + 323 reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
323 sizeof(struct crypto_rfc3686_req_ctx) + 324 crypto_skcipher_reqsize(cipher);
324 crypto_ablkcipher_reqsize(cipher); 325 crypto_skcipher_set_reqsize(tfm, reqsize);
325 326
326 return 0; 327 return 0;
327} 328}
328 329
329static void crypto_rfc3686_exit_tfm(struct crypto_tfm *tfm) 330static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
330{ 331{
331 struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm); 332 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
333
334 crypto_free_skcipher(ctx->child);
335}
332 336
333 crypto_free_ablkcipher(ctx->child); 337static void crypto_rfc3686_free(struct skcipher_instance *inst)
338{
339 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
340
341 crypto_drop_skcipher(spawn);
342 kfree(inst);
334} 343}
335 344
336static struct crypto_instance *crypto_rfc3686_alloc(struct rtattr **tb) 345static int crypto_rfc3686_create(struct crypto_template *tmpl,
346 struct rtattr **tb)
337{ 347{
338 struct crypto_attr_type *algt; 348 struct crypto_attr_type *algt;
339 struct crypto_instance *inst; 349 struct skcipher_instance *inst;
340 struct crypto_alg *alg; 350 struct skcipher_alg *alg;
341 struct crypto_skcipher_spawn *spawn; 351 struct crypto_skcipher_spawn *spawn;
342 const char *cipher_name; 352 const char *cipher_name;
343 int err; 353 int err;
344 354
345 algt = crypto_get_attr_type(tb); 355 algt = crypto_get_attr_type(tb);
346 if (IS_ERR(algt)) 356 if (IS_ERR(algt))
347 return ERR_CAST(algt); 357 return PTR_ERR(algt);
348 358
349 if ((algt->type ^ CRYPTO_ALG_TYPE_BLKCIPHER) & algt->mask) 359 if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
350 return ERR_PTR(-EINVAL); 360 return -EINVAL;
351 361
352 cipher_name = crypto_attr_alg_name(tb[1]); 362 cipher_name = crypto_attr_alg_name(tb[1]);
353 if (IS_ERR(cipher_name)) 363 if (IS_ERR(cipher_name))
354 return ERR_CAST(cipher_name); 364 return PTR_ERR(cipher_name);
355 365
356 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); 366 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
357 if (!inst) 367 if (!inst)
358 return ERR_PTR(-ENOMEM); 368 return -ENOMEM;
359 369
360 spawn = crypto_instance_ctx(inst); 370 spawn = skcipher_instance_ctx(inst);
361 371
362 crypto_set_skcipher_spawn(spawn, inst); 372 crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
363 err = crypto_grab_skcipher(spawn, cipher_name, 0, 373 err = crypto_grab_skcipher2(spawn, cipher_name, 0,
364 crypto_requires_sync(algt->type, 374 crypto_requires_sync(algt->type,
365 algt->mask)); 375 algt->mask));
366 if (err) 376 if (err)
367 goto err_free_inst; 377 goto err_free_inst;
368 378
369 alg = crypto_skcipher_spawn_alg(spawn); 379 alg = crypto_spawn_skcipher_alg(spawn);
370 380
371 /* We only support 16-byte blocks. */ 381 /* We only support 16-byte blocks. */
372 err = -EINVAL; 382 err = -EINVAL;
373 if (alg->cra_ablkcipher.ivsize != CTR_RFC3686_BLOCK_SIZE) 383 if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
374 goto err_drop_spawn; 384 goto err_drop_spawn;
375 385
376 /* Not a stream cipher? */ 386 /* Not a stream cipher? */
377 if (alg->cra_blocksize != 1) 387 if (alg->base.cra_blocksize != 1)
378 goto err_drop_spawn; 388 goto err_drop_spawn;
379 389
380 err = -ENAMETOOLONG; 390 err = -ENAMETOOLONG;
381 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "rfc3686(%s)", 391 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
382 alg->cra_name) >= CRYPTO_MAX_ALG_NAME) 392 "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
383 goto err_drop_spawn; 393 goto err_drop_spawn;
384 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, 394 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
385 "rfc3686(%s)", alg->cra_driver_name) >= 395 "rfc3686(%s)", alg->base.cra_driver_name) >=
386 CRYPTO_MAX_ALG_NAME) 396 CRYPTO_MAX_ALG_NAME)
387 goto err_drop_spawn; 397 goto err_drop_spawn;
388 398
389 inst->alg.cra_priority = alg->cra_priority; 399 inst->alg.base.cra_priority = alg->base.cra_priority;
390 inst->alg.cra_blocksize = 1; 400 inst->alg.base.cra_blocksize = 1;
391 inst->alg.cra_alignmask = alg->cra_alignmask; 401 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
392 402
393 inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 403 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
394 (alg->cra_flags & CRYPTO_ALG_ASYNC);
395 inst->alg.cra_type = &crypto_ablkcipher_type;
396 404
397 inst->alg.cra_ablkcipher.ivsize = CTR_RFC3686_IV_SIZE; 405 inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
398 inst->alg.cra_ablkcipher.min_keysize = 406 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
399 alg->cra_ablkcipher.min_keysize + CTR_RFC3686_NONCE_SIZE; 407 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
400 inst->alg.cra_ablkcipher.max_keysize = 408 CTR_RFC3686_NONCE_SIZE;
401 alg->cra_ablkcipher.max_keysize + CTR_RFC3686_NONCE_SIZE; 409 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
410 CTR_RFC3686_NONCE_SIZE;
402 411
403 inst->alg.cra_ablkcipher.geniv = "seqiv"; 412 inst->alg.setkey = crypto_rfc3686_setkey;
413 inst->alg.encrypt = crypto_rfc3686_crypt;
414 inst->alg.decrypt = crypto_rfc3686_crypt;
404 415
405 inst->alg.cra_ablkcipher.setkey = crypto_rfc3686_setkey; 416 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
406 inst->alg.cra_ablkcipher.encrypt = crypto_rfc3686_crypt;
407 inst->alg.cra_ablkcipher.decrypt = crypto_rfc3686_crypt;
408 417
409 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx); 418 inst->alg.init = crypto_rfc3686_init_tfm;
419 inst->alg.exit = crypto_rfc3686_exit_tfm;
410 420
411 inst->alg.cra_init = crypto_rfc3686_init_tfm; 421 inst->free = crypto_rfc3686_free;
412 inst->alg.cra_exit = crypto_rfc3686_exit_tfm;
413 422
414 return inst; 423 err = skcipher_register_instance(tmpl, inst);
424 if (err)
425 goto err_drop_spawn;
426
427out:
428 return err;
415 429
416err_drop_spawn: 430err_drop_spawn:
417 crypto_drop_skcipher(spawn); 431 crypto_drop_skcipher(spawn);
418err_free_inst: 432err_free_inst:
419 kfree(inst); 433 kfree(inst);
420 return ERR_PTR(err); 434 goto out;
421}
422
423static void crypto_rfc3686_free(struct crypto_instance *inst)
424{
425 struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst);
426
427 crypto_drop_skcipher(spawn);
428 kfree(inst);
429} 435}
430 436
431static struct crypto_template crypto_rfc3686_tmpl = { 437static struct crypto_template crypto_rfc3686_tmpl = {
432 .name = "rfc3686", 438 .name = "rfc3686",
433 .alloc = crypto_rfc3686_alloc, 439 .create = crypto_rfc3686_create,
434 .free = crypto_rfc3686_free,
435 .module = THIS_MODULE, 440 .module = THIS_MODULE,
436}; 441};
437 442