Skip to content

Commit 69d3150

Browse files
jkivilinklassert
authored andcommitted
crypto: ctr - make rfc3686 asynchronous block cipher
Some hardware crypto drivers register asynchronous ctr(aes), which is left unused in IPSEC because rfc3686 template only supports synchronous block ciphers. Some other drivers register rfc3686(ctr(aes)) to workaround this limitation but not all. This patch changes rfc3686 to use asynchronous block ciphers, to allow async ctr(aes) algorithms to be utilized automatically by IPSEC. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi> Acked-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent 71331da commit 69d3150

3 files changed

Lines changed: 115 additions & 63 deletions

File tree

‎crypto/ctr.c‎

Lines changed: 110 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <crypto/algapi.h>
1414
#include <crypto/ctr.h>
15+
#include <crypto/internal/skcipher.h>
1516
#include <linux/err.h>
1617
#include <linux/init.h>
1718
#include <linux/kernel.h>
@@ -25,10 +26,15 @@ struct crypto_ctr_ctx {
2526
};
2627

2728
struct crypto_rfc3686_ctx {
28-
struct crypto_blkcipher *child;
29+
struct crypto_ablkcipher *child;
2930
u8 nonce[CTR_RFC3686_NONCE_SIZE];
3031
};
3132

33+
struct crypto_rfc3686_req_ctx {
34+
u8 iv[CTR_RFC3686_BLOCK_SIZE];
35+
struct ablkcipher_request subreq CRYPTO_MINALIGN_ATTR;
36+
};
37+
3238
static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
3339
unsigned int keylen)
3440
{
@@ -243,11 +249,11 @@ static struct crypto_template crypto_ctr_tmpl = {
243249
.module = THIS_MODULE,
244250
};
245251

246-
static int crypto_rfc3686_setkey(struct crypto_tfm *parent, const u8 *key,
247-
unsigned int keylen)
252+
static int crypto_rfc3686_setkey(struct crypto_ablkcipher *parent,
253+
const u8 *key, unsigned int keylen)
248254
{
249-
struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(parent);
250-
struct crypto_blkcipher *child = ctx->child;
255+
struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(parent);
256+
struct crypto_ablkcipher *child = ctx->child;
251257
int err;
252258

253259
/* the nonce is stored in bytes at end of key */
@@ -259,134 +265,175 @@ static int crypto_rfc3686_setkey(struct crypto_tfm *parent, const u8 *key,
259265

260266
keylen -= CTR_RFC3686_NONCE_SIZE;
261267

262-
crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
263-
crypto_blkcipher_set_flags(child, crypto_tfm_get_flags(parent) &
264-
CRYPTO_TFM_REQ_MASK);
265-
err = crypto_blkcipher_setkey(child, key, keylen);
266-
crypto_tfm_set_flags(parent, crypto_blkcipher_get_flags(child) &
267-
CRYPTO_TFM_RES_MASK);
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);
268274

269275
return err;
270276
}
271277

272-
static int crypto_rfc3686_crypt(struct blkcipher_desc *desc,
273-
struct scatterlist *dst,
274-
struct scatterlist *src, unsigned int nbytes)
278+
static int crypto_rfc3686_crypt(struct ablkcipher_request *req)
275279
{
276-
struct crypto_blkcipher *tfm = desc->tfm;
277-
struct crypto_rfc3686_ctx *ctx = crypto_blkcipher_ctx(tfm);
278-
struct crypto_blkcipher *child = ctx->child;
279-
unsigned long alignmask = crypto_blkcipher_alignmask(tfm);
280-
u8 ivblk[CTR_RFC3686_BLOCK_SIZE + alignmask];
281-
u8 *iv = PTR_ALIGN(ivblk + 0, alignmask + 1);
282-
u8 *info = desc->info;
283-
int err;
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);
284+
struct crypto_rfc3686_req_ctx *rctx =
285+
(void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
286+
struct ablkcipher_request *subreq = &rctx->subreq;
287+
u8 *iv = rctx->iv;
284288

285289
/* set up counter block */
286290
memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
287-
memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
291+
memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->info, CTR_RFC3686_IV_SIZE);
288292

289293
/* initialize counter portion of counter block */
290294
*(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
291295
cpu_to_be32(1);
292296

293-
desc->tfm = child;
294-
desc->info = iv;
295-
err = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
296-
desc->tfm = tfm;
297-
desc->info = info;
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);
298302

299-
return err;
303+
return crypto_ablkcipher_encrypt(subreq);
300304
}
301305

302306
static int crypto_rfc3686_init_tfm(struct crypto_tfm *tfm)
303307
{
304308
struct crypto_instance *inst = (void *)tfm->__crt_alg;
305-
struct crypto_spawn *spawn = crypto_instance_ctx(inst);
309+
struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst);
306310
struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm);
307-
struct crypto_blkcipher *cipher;
311+
struct crypto_ablkcipher *cipher;
312+
unsigned long align;
308313

309-
cipher = crypto_spawn_blkcipher(spawn);
314+
cipher = crypto_spawn_skcipher(spawn);
310315
if (IS_ERR(cipher))
311316
return PTR_ERR(cipher);
312317

313318
ctx->child = cipher;
314319

320+
align = crypto_tfm_alg_alignmask(tfm);
321+
align &= ~(crypto_tfm_ctx_alignment() - 1);
322+
tfm->crt_ablkcipher.reqsize = align +
323+
sizeof(struct crypto_rfc3686_req_ctx) +
324+
crypto_ablkcipher_reqsize(cipher);
325+
315326
return 0;
316327
}
317328

318329
static void crypto_rfc3686_exit_tfm(struct crypto_tfm *tfm)
319330
{
320331
struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm);
321332

322-
crypto_free_blkcipher(ctx->child);
333+
crypto_free_ablkcipher(ctx->child);
323334
}
324335

325336
static struct crypto_instance *crypto_rfc3686_alloc(struct rtattr **tb)
326337
{
338+
struct crypto_attr_type *algt;
327339
struct crypto_instance *inst;
328340
struct crypto_alg *alg;
341+
struct crypto_skcipher_spawn *spawn;
342+
const char *cipher_name;
329343
int err;
330344

331-
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
332-
if (err)
345+
algt = crypto_get_attr_type(tb);
346+
err = PTR_ERR(algt);
347+
if (IS_ERR(algt))
333348
return ERR_PTR(err);
334349

335-
alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_BLKCIPHER,
336-
CRYPTO_ALG_TYPE_MASK);
337-
err = PTR_ERR(alg);
338-
if (IS_ERR(alg))
350+
if ((algt->type ^ CRYPTO_ALG_TYPE_BLKCIPHER) & algt->mask)
351+
return ERR_PTR(-EINVAL);
352+
353+
cipher_name = crypto_attr_alg_name(tb[1]);
354+
err = PTR_ERR(cipher_name);
355+
if (IS_ERR(cipher_name))
339356
return ERR_PTR(err);
340357

358+
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
359+
if (!inst)
360+
return ERR_PTR(-ENOMEM);
361+
362+
spawn = crypto_instance_ctx(inst);
363+
364+
crypto_set_skcipher_spawn(spawn, inst);
365+
err = crypto_grab_skcipher(spawn, cipher_name, 0,
366+
crypto_requires_sync(algt->type,
367+
algt->mask));
368+
if (err)
369+
goto err_free_inst;
370+
371+
alg = crypto_skcipher_spawn_alg(spawn);
372+
341373
/* We only support 16-byte blocks. */
342374
err = -EINVAL;
343-
if (alg->cra_blkcipher.ivsize != CTR_RFC3686_BLOCK_SIZE)
344-
goto out_put_alg;
375+
if (alg->cra_ablkcipher.ivsize != CTR_RFC3686_BLOCK_SIZE)
376+
goto err_drop_spawn;
345377

346378
/* Not a stream cipher? */
347379
if (alg->cra_blocksize != 1)
348-
goto out_put_alg;
380+
goto err_drop_spawn;
349381

350-
inst = crypto_alloc_instance("rfc3686", alg);
351-
if (IS_ERR(inst))
352-
goto out;
382+
err = -ENAMETOOLONG;
383+
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "rfc3686(%s)",
384+
alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
385+
goto err_drop_spawn;
386+
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
387+
"rfc3686(%s)", alg->cra_driver_name) >=
388+
CRYPTO_MAX_ALG_NAME)
389+
goto err_drop_spawn;
353390

354-
inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
355391
inst->alg.cra_priority = alg->cra_priority;
356392
inst->alg.cra_blocksize = 1;
357393
inst->alg.cra_alignmask = alg->cra_alignmask;
358-
inst->alg.cra_type = &crypto_blkcipher_type;
359394

360-
inst->alg.cra_blkcipher.ivsize = CTR_RFC3686_IV_SIZE;
361-
inst->alg.cra_blkcipher.min_keysize = alg->cra_blkcipher.min_keysize
362-
+ CTR_RFC3686_NONCE_SIZE;
363-
inst->alg.cra_blkcipher.max_keysize = alg->cra_blkcipher.max_keysize
364-
+ CTR_RFC3686_NONCE_SIZE;
395+
inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
396+
(alg->cra_flags & CRYPTO_ALG_ASYNC);
397+
inst->alg.cra_type = &crypto_ablkcipher_type;
398+
399+
inst->alg.cra_ablkcipher.ivsize = CTR_RFC3686_IV_SIZE;
400+
inst->alg.cra_ablkcipher.min_keysize =
401+
alg->cra_ablkcipher.min_keysize + CTR_RFC3686_NONCE_SIZE;
402+
inst->alg.cra_ablkcipher.max_keysize =
403+
alg->cra_ablkcipher.max_keysize + CTR_RFC3686_NONCE_SIZE;
365404

366-
inst->alg.cra_blkcipher.geniv = "seqiv";
405+
inst->alg.cra_ablkcipher.geniv = "seqiv";
406+
407+
inst->alg.cra_ablkcipher.setkey = crypto_rfc3686_setkey;
408+
inst->alg.cra_ablkcipher.encrypt = crypto_rfc3686_crypt;
409+
inst->alg.cra_ablkcipher.decrypt = crypto_rfc3686_crypt;
367410

368411
inst->alg.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
369412

370413
inst->alg.cra_init = crypto_rfc3686_init_tfm;
371414
inst->alg.cra_exit = crypto_rfc3686_exit_tfm;
372415

373-
inst->alg.cra_blkcipher.setkey = crypto_rfc3686_setkey;
374-
inst->alg.cra_blkcipher.encrypt = crypto_rfc3686_crypt;
375-
inst->alg.cra_blkcipher.decrypt = crypto_rfc3686_crypt;
376-
377-
out:
378-
crypto_mod_put(alg);
379416
return inst;
380417

381-
out_put_alg:
382-
inst = ERR_PTR(err);
383-
goto out;
418+
err_drop_spawn:
419+
crypto_drop_skcipher(spawn);
420+
err_free_inst:
421+
kfree(inst);
422+
return ERR_PTR(err);
423+
}
424+
425+
static void crypto_rfc3686_free(struct crypto_instance *inst)
426+
{
427+
struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst);
428+
429+
crypto_drop_skcipher(spawn);
430+
kfree(inst);
384431
}
385432

386433
static struct crypto_template crypto_rfc3686_tmpl = {
387434
.name = "rfc3686",
388435
.alloc = crypto_rfc3686_alloc,
389-
.free = crypto_ctr_free,
436+
.free = crypto_rfc3686_free,
390437
.module = THIS_MODULE,
391438
};
392439

‎crypto/tcrypt.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,10 @@ static int do_test(int m)
15911591
speed_template_16_24_32);
15921592
test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
15931593
speed_template_16_24_32);
1594+
test_acipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL, 0,
1595+
speed_template_20_28_36);
1596+
test_acipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL, 0,
1597+
speed_template_20_28_36);
15941598
break;
15951599

15961600
case 501:

‎crypto/tcrypt.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static u8 speed_template_8_16[] = {8, 16, 0};
5151
static u8 speed_template_8_32[] = {8, 32, 0};
5252
static u8 speed_template_16_32[] = {16, 32, 0};
5353
static u8 speed_template_16_24_32[] = {16, 24, 32, 0};
54+
static u8 speed_template_20_28_36[] = {20, 28, 36, 0};
5455
static u8 speed_template_32_40_48[] = {32, 40, 48, 0};
5556
static u8 speed_template_32_48[] = {32, 48, 0};
5657
static u8 speed_template_32_48_64[] = {32, 48, 64, 0};

0 commit comments

Comments
 (0)