Skip to content

Commit

Permalink
[CRYPTO] aead: Make authsize a run-time parameter
Browse files Browse the repository at this point in the history
As it is authsize is an algorithm paramter which cannot be changed at
run-time.  This is inconvenient because hardware that implements such
algorithms would have to register each authsize that they support
separately.

Since authsize is a property common to all AEAD algorithms, we can add
a function setauthsize that sets it at run-time, just like setkey.

This patch does exactly that and also changes authenc so that authsize
is no longer a parameter of its template.

Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
herbertx committed Jan 10, 2008
1 parent e29bc6a commit 7ba683a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
24 changes: 21 additions & 3 deletions crypto/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
return aead->setkey(tfm, key, keylen);
}

int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
{
int err;

if (authsize > crypto_aead_alg(tfm)->maxauthsize)
return -EINVAL;

if (crypto_aead_alg(tfm)->setauthsize) {
err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
if (err)
return err;
}

crypto_aead_crt(tfm)->authsize = authsize;
return 0;
}
EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);

static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
u32 mask)
{
Expand All @@ -64,14 +82,14 @@ static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
struct aead_tfm *crt = &tfm->crt_aead;

if (max(alg->authsize, alg->ivsize) > PAGE_SIZE / 8)
if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
return -EINVAL;

crt->setkey = setkey;
crt->encrypt = alg->encrypt;
crt->decrypt = alg->decrypt;
crt->ivsize = alg->ivsize;
crt->authsize = alg->authsize;
crt->authsize = alg->maxauthsize;

return 0;
}
Expand All @@ -85,7 +103,7 @@ static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
seq_printf(m, "type : aead\n");
seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
seq_printf(m, "ivsize : %u\n", aead->ivsize);
seq_printf(m, "authsize : %u\n", aead->authsize);
seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
}

const struct crypto_type crypto_aead_type = {
Expand Down
39 changes: 12 additions & 27 deletions crypto/authenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct authenc_instance_ctx {
struct crypto_spawn auth;
struct crypto_spawn enc;

unsigned int authsize;
unsigned int enckeylen;
};

Expand Down Expand Up @@ -76,8 +75,6 @@ static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
static int crypto_authenc_hash(struct aead_request *req)
{
struct crypto_aead *authenc = crypto_aead_reqtfm(req);
struct authenc_instance_ctx *ictx =
crypto_instance_ctx(crypto_aead_alg_instance(authenc));
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
struct crypto_hash *auth = ctx->auth;
struct hash_desc desc = {
Expand Down Expand Up @@ -111,7 +108,8 @@ static int crypto_authenc_hash(struct aead_request *req)
if (err)
return err;

scatterwalk_map_and_copy(hash, dst, cryptlen, ictx->authsize, 1);
scatterwalk_map_and_copy(hash, dst, cryptlen,
crypto_aead_authsize(authenc), 1);
return 0;
}

Expand Down Expand Up @@ -147,8 +145,6 @@ static int crypto_authenc_encrypt(struct aead_request *req)
static int crypto_authenc_verify(struct aead_request *req)
{
struct crypto_aead *authenc = crypto_aead_reqtfm(req);
struct authenc_instance_ctx *ictx =
crypto_instance_ctx(crypto_aead_alg_instance(authenc));
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
struct crypto_hash *auth = ctx->auth;
struct hash_desc desc = {
Expand Down Expand Up @@ -186,7 +182,7 @@ static int crypto_authenc_verify(struct aead_request *req)
if (err)
return err;

authsize = ictx->authsize;
authsize = crypto_aead_authsize(authenc);
scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
return memcmp(ihash, ohash, authsize) ? -EINVAL : 0;
}
Expand Down Expand Up @@ -224,18 +220,12 @@ static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
struct crypto_hash *auth;
struct crypto_ablkcipher *enc;
unsigned int digestsize;
int err;

auth = crypto_spawn_hash(&ictx->auth);
if (IS_ERR(auth))
return PTR_ERR(auth);

err = -EINVAL;
digestsize = crypto_hash_digestsize(auth);
if (ictx->authsize > digestsize)
goto err_free_hash;

enc = crypto_spawn_ablkcipher(&ictx->enc);
err = PTR_ERR(enc);
if (IS_ERR(enc))
Expand All @@ -246,7 +236,7 @@ static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
tfm->crt_aead.reqsize = max_t(unsigned int,
(crypto_hash_alignmask(auth) &
~(crypto_tfm_ctx_alignment() - 1)) +
digestsize * 2,
crypto_hash_digestsize(auth) * 2,
sizeof(struct ablkcipher_request) +
crypto_ablkcipher_reqsize(enc));

Expand All @@ -273,7 +263,6 @@ static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
struct crypto_alg *auth;
struct crypto_alg *enc;
struct authenc_instance_ctx *ctx;
unsigned int authsize;
unsigned int enckeylen;
int err;

Expand All @@ -286,18 +275,13 @@ static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
if (IS_ERR(auth))
return ERR_PTR(PTR_ERR(auth));

err = crypto_attr_u32(tb[2], &authsize);
inst = ERR_PTR(err);
if (err)
goto out_put_auth;

enc = crypto_attr_alg(tb[3], CRYPTO_ALG_TYPE_BLKCIPHER,
enc = crypto_attr_alg(tb[2], CRYPTO_ALG_TYPE_BLKCIPHER,
CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
inst = ERR_PTR(PTR_ERR(enc));
if (IS_ERR(enc))
goto out_put_auth;

err = crypto_attr_u32(tb[4], &enckeylen);
err = crypto_attr_u32(tb[3], &enckeylen);
if (err)
goto out_put_enc;

Expand All @@ -308,18 +292,17 @@ static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)

err = -ENAMETOOLONG;
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
"authenc(%s,%u,%s,%u)", auth->cra_name, authsize,
"authenc(%s,%s,%u)", auth->cra_name,
enc->cra_name, enckeylen) >= CRYPTO_MAX_ALG_NAME)
goto err_free_inst;

if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
"authenc(%s,%u,%s,%u)", auth->cra_driver_name,
authsize, enc->cra_driver_name, enckeylen) >=
"authenc(%s,%s,%u)", auth->cra_driver_name,
enc->cra_driver_name, enckeylen) >=
CRYPTO_MAX_ALG_NAME)
goto err_free_inst;

ctx = crypto_instance_ctx(inst);
ctx->authsize = authsize;
ctx->enckeylen = enckeylen;

err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
Expand All @@ -337,7 +320,9 @@ static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
inst->alg.cra_type = &crypto_aead_type;

inst->alg.cra_aead.ivsize = enc->cra_blkcipher.ivsize;
inst->alg.cra_aead.authsize = authsize;
inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
auth->cra_hash.digestsize :
auth->cra_digest.dia_digestsize;

inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);

Expand Down
2 changes: 1 addition & 1 deletion crypto/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
inst->alg.cra_alignmask = __alignof__(u32) - 1;
inst->alg.cra_type = &crypto_aead_type;
inst->alg.cra_aead.ivsize = 12;
inst->alg.cra_aead.authsize = 16;
inst->alg.cra_aead.maxauthsize = 16;
inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
inst->alg.cra_init = crypto_gcm_init_tfm;
inst->alg.cra_exit = crypto_gcm_exit_tfm;
Expand Down
5 changes: 4 additions & 1 deletion include/linux/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@ struct ablkcipher_alg {
struct aead_alg {
int (*setkey)(struct crypto_aead *tfm, const u8 *key,
unsigned int keylen);
int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
int (*encrypt)(struct aead_request *req);
int (*decrypt)(struct aead_request *req);

unsigned int ivsize;
unsigned int authsize;
unsigned int maxauthsize;
};

struct blkcipher_alg {
Expand Down Expand Up @@ -754,6 +755,8 @@ static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
}

int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);

static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
{
return __crypto_aead_cast(req->base.tfm);
Expand Down

0 comments on commit 7ba683a

Please sign in to comment.