Skip to content

Commit b2b39c2

Browse files
committed
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>
1 parent e75445a commit b2b39c2

1 file changed

Lines changed: 94 additions & 89 deletions

File tree

‎crypto/ctr.c‎

Lines changed: 94 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ struct crypto_ctr_ctx {
2626
};
2727

2828
struct crypto_rfc3686_ctx {
29-
struct crypto_ablkcipher *child;
29+
struct crypto_skcipher *child;
3030
u8 nonce[CTR_RFC3686_NONCE_SIZE];
3131
};
3232

3333
struct crypto_rfc3686_req_ctx {
3434
u8 iv[CTR_RFC3686_BLOCK_SIZE];
35-
struct ablkcipher_request subreq CRYPTO_MINALIGN_ATTR;
35+
struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
3636
};
3737

3838
static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
@@ -249,11 +249,11 @@ static struct crypto_template crypto_ctr_tmpl = {
249249
.module = THIS_MODULE,
250250
};
251251

252-
static int crypto_rfc3686_setkey(struct crypto_ablkcipher *parent,
252+
static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
253253
const u8 *key, unsigned int keylen)
254254
{
255-
struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(parent);
256-
struct crypto_ablkcipher *child = ctx->child;
255+
struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
256+
struct crypto_skcipher *child = ctx->child;
257257
int err;
258258

259259
/* the nonce is stored in bytes at end of key */
@@ -265,173 +265,178 @@ static int crypto_rfc3686_setkey(struct crypto_ablkcipher *parent,
265265

266266
keylen -= CTR_RFC3686_NONCE_SIZE;
267267

268-
crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
269-
crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
270-
CRYPTO_TFM_REQ_MASK);
271-
err = crypto_ablkcipher_setkey(child, key, keylen);
272-
crypto_ablkcipher_set_flags(parent, crypto_ablkcipher_get_flags(child) &
273-
CRYPTO_TFM_RES_MASK);
268+
crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
269+
crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
270+
CRYPTO_TFM_REQ_MASK);
271+
err = crypto_skcipher_setkey(child, key, keylen);
272+
crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
273+
CRYPTO_TFM_RES_MASK);
274274

275275
return err;
276276
}
277277

278-
static int crypto_rfc3686_crypt(struct ablkcipher_request *req)
278+
static int crypto_rfc3686_crypt(struct skcipher_request *req)
279279
{
280-
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
281-
struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(tfm);
282-
struct crypto_ablkcipher *child = ctx->child;
283-
unsigned long align = crypto_ablkcipher_alignmask(tfm);
280+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
281+
struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
282+
struct crypto_skcipher *child = ctx->child;
283+
unsigned long align = crypto_skcipher_alignmask(tfm);
284284
struct crypto_rfc3686_req_ctx *rctx =
285-
(void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
286-
struct ablkcipher_request *subreq = &rctx->subreq;
285+
(void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
286+
struct skcipher_request *subreq = &rctx->subreq;
287287
u8 *iv = rctx->iv;
288288

289289
/* set up counter block */
290290
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);
292292

293293
/* initialize counter portion of counter block */
294294
*(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
295295
cpu_to_be32(1);
296296

297-
ablkcipher_request_set_tfm(subreq, child);
298-
ablkcipher_request_set_callback(subreq, req->base.flags,
299-
req->base.complete, req->base.data);
300-
ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->nbytes,
301-
iv);
297+
skcipher_request_set_tfm(subreq, child);
298+
skcipher_request_set_callback(subreq, req->base.flags,
299+
req->base.complete, req->base.data);
300+
skcipher_request_set_crypt(subreq, req->src, req->dst,
301+
req->cryptlen, iv);
302302

303-
return crypto_ablkcipher_encrypt(subreq);
303+
return crypto_skcipher_encrypt(subreq);
304304
}
305305

306-
static int crypto_rfc3686_init_tfm(struct crypto_tfm *tfm)
306+
static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
307307
{
308-
struct crypto_instance *inst = (void *)tfm->__crt_alg;
309-
struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst);
310-
struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm);
311-
struct crypto_ablkcipher *cipher;
308+
struct skcipher_instance *inst = skcipher_alg_instance(tfm);
309+
struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
310+
struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
311+
struct crypto_skcipher *cipher;
312312
unsigned long align;
313+
unsigned int reqsize;
313314

314-
cipher = crypto_spawn_skcipher(spawn);
315+
cipher = crypto_spawn_skcipher2(spawn);
315316
if (IS_ERR(cipher))
316317
return PTR_ERR(cipher);
317318

318319
ctx->child = cipher;
319320

320-
align = crypto_tfm_alg_alignmask(tfm);
321+
align = crypto_skcipher_alignmask(tfm);
321322
align &= ~(crypto_tfm_ctx_alignment() - 1);
322-
tfm->crt_ablkcipher.reqsize = align +
323-
sizeof(struct crypto_rfc3686_req_ctx) +
324-
crypto_ablkcipher_reqsize(cipher);
323+
reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
324+
crypto_skcipher_reqsize(cipher);
325+
crypto_skcipher_set_reqsize(tfm, reqsize);
325326

326327
return 0;
327328
}
328329

329-
static void crypto_rfc3686_exit_tfm(struct crypto_tfm *tfm)
330+
static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
330331
{
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+
}
332336

333-
crypto_free_ablkcipher(ctx->child);
337+
static 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);
334343
}
335344

336-
static struct crypto_instance *crypto_rfc3686_alloc(struct rtattr **tb)
345+
static int crypto_rfc3686_create(struct crypto_template *tmpl,
346+
struct rtattr **tb)
337347
{
338348
struct crypto_attr_type *algt;
339-
struct crypto_instance *inst;
340-
struct crypto_alg *alg;
349+
struct skcipher_instance *inst;
350+
struct skcipher_alg *alg;
341351
struct crypto_skcipher_spawn *spawn;
342352
const char *cipher_name;
343353
int err;
344354

345355
algt = crypto_get_attr_type(tb);
346356
if (IS_ERR(algt))
347-
return ERR_CAST(algt);
357+
return PTR_ERR(algt);
348358

349-
if ((algt->type ^ CRYPTO_ALG_TYPE_BLKCIPHER) & algt->mask)
350-
return ERR_PTR(-EINVAL);
359+
if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
360+
return -EINVAL;
351361

352362
cipher_name = crypto_attr_alg_name(tb[1]);
353363
if (IS_ERR(cipher_name))
354-
return ERR_CAST(cipher_name);
364+
return PTR_ERR(cipher_name);
355365

356366
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
357367
if (!inst)
358-
return ERR_PTR(-ENOMEM);
368+
return -ENOMEM;
359369

360-
spawn = crypto_instance_ctx(inst);
370+
spawn = skcipher_instance_ctx(inst);
361371

362-
crypto_set_skcipher_spawn(spawn, inst);
363-
err = crypto_grab_skcipher(spawn, cipher_name, 0,
364-
crypto_requires_sync(algt->type,
365-
algt->mask));
372+
crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
373+
err = crypto_grab_skcipher2(spawn, cipher_name, 0,
374+
crypto_requires_sync(algt->type,
375+
algt->mask));
366376
if (err)
367377
goto err_free_inst;
368378

369-
alg = crypto_skcipher_spawn_alg(spawn);
379+
alg = crypto_spawn_skcipher_alg(spawn);
370380

371381
/* We only support 16-byte blocks. */
372382
err = -EINVAL;
373-
if (alg->cra_ablkcipher.ivsize != CTR_RFC3686_BLOCK_SIZE)
383+
if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
374384
goto err_drop_spawn;
375385

376386
/* Not a stream cipher? */
377-
if (alg->cra_blocksize != 1)
387+
if (alg->base.cra_blocksize != 1)
378388
goto err_drop_spawn;
379389

380390
err = -ENAMETOOLONG;
381-
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "rfc3686(%s)",
382-
alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
391+
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
392+
"rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
383393
goto err_drop_spawn;
384-
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
385-
"rfc3686(%s)", alg->cra_driver_name) >=
386-
CRYPTO_MAX_ALG_NAME)
394+
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
395+
"rfc3686(%s)", alg->base.cra_driver_name) >=
396+
CRYPTO_MAX_ALG_NAME)
387397
goto err_drop_spawn;
388398

389-
inst->alg.cra_priority = alg->cra_priority;
390-
inst->alg.cra_blocksize = 1;
391-
inst->alg.cra_alignmask = alg->cra_alignmask;
399+
inst->alg.base.cra_priority = alg->base.cra_priority;
400+
inst->alg.base.cra_blocksize = 1;
401+
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
392402

393-
inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
394-
(alg->cra_flags & CRYPTO_ALG_ASYNC);
395-
inst->alg.cra_type = &crypto_ablkcipher_type;
403+
inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
396404

397-
inst->alg.cra_ablkcipher.ivsize = CTR_RFC3686_IV_SIZE;
398-
inst->alg.cra_ablkcipher.min_keysize =
399-
alg->cra_ablkcipher.min_keysize + CTR_RFC3686_NONCE_SIZE;
400-
inst->alg.cra_ablkcipher.max_keysize =
401-
alg->cra_ablkcipher.max_keysize + CTR_RFC3686_NONCE_SIZE;
405+
inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
406+
inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
407+
inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
408+
CTR_RFC3686_NONCE_SIZE;
409+
inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
410+
CTR_RFC3686_NONCE_SIZE;
402411

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;
404415

405-
inst->alg.cra_ablkcipher.setkey = crypto_rfc3686_setkey;
406-
inst->alg.cra_ablkcipher.encrypt = crypto_rfc3686_crypt;
407-
inst->alg.cra_ablkcipher.decrypt = crypto_rfc3686_crypt;
416+
inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
408417

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;
410420

411-
inst->alg.cra_init = crypto_rfc3686_init_tfm;
412-
inst->alg.cra_exit = crypto_rfc3686_exit_tfm;
421+
inst->free = crypto_rfc3686_free;
413422

414-
return inst;
423+
err = skcipher_register_instance(tmpl, inst);
424+
if (err)
425+
goto err_drop_spawn;
426+
427+
out:
428+
return err;
415429

416430
err_drop_spawn:
417431
crypto_drop_skcipher(spawn);
418432
err_free_inst:
419433
kfree(inst);
420-
return ERR_PTR(err);
421-
}
422-
423-
static 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);
434+
goto out;
429435
}
430436

431437
static struct crypto_template crypto_rfc3686_tmpl = {
432438
.name = "rfc3686",
433-
.alloc = crypto_rfc3686_alloc,
434-
.free = crypto_rfc3686_free,
439+
.create = crypto_rfc3686_create,
435440
.module = THIS_MODULE,
436441
};
437442

0 commit comments

Comments
 (0)