1
0
Fork 0

crypto: seqiv - Use common IV generation code

This patch makes use of the new common IV generation code.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
hifive-unleashed-5.1
Herbert Xu 2015-05-27 14:37:37 +08:00
parent d0ad1b246d
commit 661cfd0e5e
1 changed files with 36 additions and 56 deletions

View File

@ -13,7 +13,7 @@
* *
*/ */
#include <crypto/internal/aead.h> #include <crypto/internal/geniv.h>
#include <crypto/internal/skcipher.h> #include <crypto/internal/skcipher.h>
#include <crypto/null.h> #include <crypto/null.h>
#include <crypto/rng.h> #include <crypto/rng.h>
@ -37,30 +37,14 @@ struct seqiv_ctx {
}; };
struct seqiv_aead_ctx { struct seqiv_aead_ctx {
struct crypto_aead *child; /* aead_geniv_ctx must be first the element */
spinlock_t lock; struct aead_geniv_ctx geniv;
struct crypto_blkcipher *null; struct crypto_blkcipher *null;
u8 salt[] __attribute__ ((aligned(__alignof__(u32)))); u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
}; };
static void seqiv_free(struct crypto_instance *inst); static void seqiv_free(struct crypto_instance *inst);
static int seqiv_aead_setkey(struct crypto_aead *tfm,
const u8 *key, unsigned int keylen)
{
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm);
return crypto_aead_setkey(ctx->child, key, keylen);
}
static int seqiv_aead_setauthsize(struct crypto_aead *tfm,
unsigned int authsize)
{
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm);
return crypto_aead_setauthsize(ctx->child, authsize);
}
static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err) static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
{ {
struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
@ -289,7 +273,7 @@ static int seqiv_aead_givencrypt(struct aead_givcrypt_request *req)
return err; return err;
} }
static int seqiv_aead_encrypt_compat(struct aead_request *req) static int seqniv_aead_encrypt(struct aead_request *req)
{ {
struct crypto_aead *geniv = crypto_aead_reqtfm(req); struct crypto_aead *geniv = crypto_aead_reqtfm(req);
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
@ -309,7 +293,7 @@ static int seqiv_aead_encrypt_compat(struct aead_request *req)
if (req->assoclen > 12) if (req->assoclen > 12)
return -EINVAL; return -EINVAL;
aead_request_set_tfm(subreq, ctx->child); aead_request_set_tfm(subreq, ctx->geniv.child);
compl = seqniv_aead_encrypt_complete; compl = seqniv_aead_encrypt_complete;
data = req; data = req;
@ -359,7 +343,7 @@ static int seqiv_aead_encrypt(struct aead_request *req)
if (req->cryptlen < ivsize) if (req->cryptlen < ivsize)
return -EINVAL; return -EINVAL;
aead_request_set_tfm(subreq, ctx->child); aead_request_set_tfm(subreq, ctx->geniv.child);
compl = req->base.complete; compl = req->base.complete;
data = req->base.data; data = req->base.data;
@ -403,7 +387,7 @@ static int seqiv_aead_encrypt(struct aead_request *req)
return err; return err;
} }
static int seqiv_aead_decrypt_compat(struct aead_request *req) static int seqniv_aead_decrypt(struct aead_request *req)
{ {
struct crypto_aead *geniv = crypto_aead_reqtfm(req); struct crypto_aead *geniv = crypto_aead_reqtfm(req);
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
@ -419,7 +403,7 @@ static int seqiv_aead_decrypt_compat(struct aead_request *req)
if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
return -EINVAL; return -EINVAL;
aead_request_set_tfm(subreq, ctx->child); aead_request_set_tfm(subreq, ctx->geniv.child);
compl = req->base.complete; compl = req->base.complete;
data = req->base.data; data = req->base.data;
@ -472,7 +456,7 @@ static int seqiv_aead_decrypt(struct aead_request *req)
if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
return -EINVAL; return -EINVAL;
aead_request_set_tfm(subreq, ctx->child); aead_request_set_tfm(subreq, ctx->geniv.child);
compl = req->base.complete; compl = req->base.complete;
data = req->base.data; data = req->base.data;
@ -536,27 +520,27 @@ unlock:
return seqiv_aead_givencrypt(req); return seqiv_aead_givencrypt(req);
} }
static int seqiv_aead_encrypt_compat_first(struct aead_request *req) static int seqniv_aead_encrypt_first(struct aead_request *req)
{ {
struct crypto_aead *geniv = crypto_aead_reqtfm(req); struct crypto_aead *geniv = crypto_aead_reqtfm(req);
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
int err = 0; int err = 0;
spin_lock_bh(&ctx->lock); spin_lock_bh(&ctx->geniv.lock);
if (geniv->encrypt != seqiv_aead_encrypt_compat_first) if (geniv->encrypt != seqniv_aead_encrypt_first)
goto unlock; goto unlock;
geniv->encrypt = seqiv_aead_encrypt_compat; geniv->encrypt = seqniv_aead_encrypt;
err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
crypto_aead_ivsize(geniv)); crypto_aead_ivsize(geniv));
unlock: unlock:
spin_unlock_bh(&ctx->lock); spin_unlock_bh(&ctx->geniv.lock);
if (err) if (err)
return err; return err;
return seqiv_aead_encrypt_compat(req); return seqniv_aead_encrypt(req);
} }
static int seqiv_aead_encrypt_first(struct aead_request *req) static int seqiv_aead_encrypt_first(struct aead_request *req)
@ -565,7 +549,7 @@ static int seqiv_aead_encrypt_first(struct aead_request *req)
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
int err = 0; int err = 0;
spin_lock_bh(&ctx->lock); spin_lock_bh(&ctx->geniv.lock);
if (geniv->encrypt != seqiv_aead_encrypt_first) if (geniv->encrypt != seqiv_aead_encrypt_first)
goto unlock; goto unlock;
@ -574,7 +558,7 @@ static int seqiv_aead_encrypt_first(struct aead_request *req)
crypto_aead_ivsize(geniv)); crypto_aead_ivsize(geniv));
unlock: unlock:
spin_unlock_bh(&ctx->lock); spin_unlock_bh(&ctx->geniv.lock);
if (err) if (err)
return err; return err;
@ -613,7 +597,7 @@ static int seqiv_aead_init_common(struct crypto_tfm *tfm, unsigned int reqsize)
struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
int err; int err;
spin_lock_init(&ctx->lock); spin_lock_init(&ctx->geniv.lock);
crypto_aead_set_reqsize(geniv, sizeof(struct aead_request)); crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
@ -626,7 +610,7 @@ static int seqiv_aead_init_common(struct crypto_tfm *tfm, unsigned int reqsize)
if (err) if (err)
goto drop_null; goto drop_null;
ctx->child = geniv->child; ctx->geniv.child = geniv->child;
geniv->child = geniv; geniv->child = geniv;
out: out:
@ -651,7 +635,7 @@ static void seqiv_aead_exit(struct crypto_tfm *tfm)
{ {
struct seqiv_aead_ctx *ctx = crypto_tfm_ctx(tfm); struct seqiv_aead_ctx *ctx = crypto_tfm_ctx(tfm);
crypto_free_aead(ctx->child); crypto_free_aead(ctx->geniv.child);
crypto_put_default_null_skcipher(); crypto_put_default_null_skcipher();
} }
@ -738,15 +722,16 @@ static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
if (inst->alg.base.cra_aead.encrypt) if (inst->alg.base.cra_aead.encrypt)
return seqiv_old_aead_create(tmpl, inst); return seqiv_old_aead_create(tmpl, inst);
spawn = aead_instance_ctx(inst);
alg = crypto_spawn_aead_alg(spawn);
if (alg->base.cra_aead.encrypt)
goto done;
err = -EINVAL; err = -EINVAL;
if (inst->alg.ivsize != sizeof(u64)) if (inst->alg.ivsize != sizeof(u64))
goto free_inst; goto free_inst;
spawn = aead_instance_ctx(inst);
alg = crypto_spawn_aead_alg(spawn);
inst->alg.setkey = seqiv_aead_setkey;
inst->alg.setauthsize = seqiv_aead_setauthsize;
inst->alg.encrypt = seqiv_aead_encrypt_first; inst->alg.encrypt = seqiv_aead_encrypt_first;
inst->alg.decrypt = seqiv_aead_decrypt; inst->alg.decrypt = seqiv_aead_decrypt;
@ -756,14 +741,7 @@ static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx); inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize; inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
if (alg->base.cra_aead.encrypt) { done:
inst->alg.encrypt = seqiv_aead_encrypt_compat_first;
inst->alg.decrypt = seqiv_aead_decrypt_compat;
inst->alg.base.cra_init = seqniv_aead_init;
inst->alg.base.cra_exit = seqiv_aead_exit;
}
err = aead_register_instance(tmpl, inst); err = aead_register_instance(tmpl, inst);
if (err) if (err)
goto free_inst; goto free_inst;
@ -816,17 +794,18 @@ static int seqniv_create(struct crypto_template *tmpl, struct rtattr **tb)
if (IS_ERR(inst)) if (IS_ERR(inst))
goto put_rng; goto put_rng;
spawn = aead_instance_ctx(inst);
alg = crypto_spawn_aead_alg(spawn);
if (alg->base.cra_aead.encrypt)
goto done;
err = -EINVAL; err = -EINVAL;
if (inst->alg.ivsize != sizeof(u64)) if (inst->alg.ivsize != sizeof(u64))
goto free_inst; goto free_inst;
spawn = aead_instance_ctx(inst); inst->alg.encrypt = seqniv_aead_encrypt_first;
alg = crypto_spawn_aead_alg(spawn); inst->alg.decrypt = seqniv_aead_decrypt;
inst->alg.setkey = seqiv_aead_setkey;
inst->alg.setauthsize = seqiv_aead_setauthsize;
inst->alg.encrypt = seqiv_aead_encrypt_compat_first;
inst->alg.decrypt = seqiv_aead_decrypt_compat;
inst->alg.base.cra_init = seqniv_aead_init; inst->alg.base.cra_init = seqniv_aead_init;
inst->alg.base.cra_exit = seqiv_aead_exit; inst->alg.base.cra_exit = seqiv_aead_exit;
@ -835,6 +814,7 @@ static int seqniv_create(struct crypto_template *tmpl, struct rtattr **tb)
inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx); inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize; inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
done:
err = aead_register_instance(tmpl, inst); err = aead_register_instance(tmpl, inst);
if (err) if (err)
goto free_inst; goto free_inst;