Commit 6160b289 authored by Herbert Xu's avatar Herbert Xu

[CRYPTO] gcm: Fix ICV handling

The crypto_aead convention for ICVs is to include it directly in the
output.  If we decided to change this in future then we would make
the ICV (if the algorithm has an explicit one) available in the
request itself.

For now no algorithm needs this so this patch changes gcm to conform
to this convention.  It also adjusts the tcrypt aead tests to take
this into account.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 8df213d9
...@@ -36,6 +36,7 @@ struct crypto_gcm_ghash_ctx { ...@@ -36,6 +36,7 @@ struct crypto_gcm_ghash_ctx {
struct crypto_gcm_req_priv_ctx { struct crypto_gcm_req_priv_ctx {
u8 auth_tag[16]; u8 auth_tag[16];
u8 iauth_tag[16];
u8 counter[16]; u8 counter[16];
struct crypto_gcm_ghash_ctx ghash; struct crypto_gcm_ghash_ctx ghash;
}; };
...@@ -89,6 +90,9 @@ static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx *ctx, ...@@ -89,6 +90,9 @@ static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx *ctx,
u8 *src; u8 *src;
int n; int n;
if (!len)
return;
scatterwalk_start(&walk, sg); scatterwalk_start(&walk, sg);
while (len) { while (len) {
...@@ -211,9 +215,10 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key, ...@@ -211,9 +215,10 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
} }
static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req, static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
struct aead_request *req, struct aead_request *req,
void (*done)(struct crypto_async_request *, unsigned int cryptlen,
int)) void (*done)(struct crypto_async_request *,
int))
{ {
struct crypto_aead *aead = crypto_aead_reqtfm(req); struct crypto_aead *aead = crypto_aead_reqtfm(req);
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
...@@ -228,7 +233,7 @@ static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req, ...@@ -228,7 +233,7 @@ static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
ablkcipher_request_set_callback(ablk_req, aead_request_flags(req), ablkcipher_request_set_callback(ablk_req, aead_request_flags(req),
done, req); done, req);
ablkcipher_request_set_crypt(ablk_req, req->src, req->dst, ablkcipher_request_set_crypt(ablk_req, req->src, req->dst,
req->cryptlen, counter); cryptlen, counter);
err = crypto_gcm_encrypt_counter(aead, auth_tag, 0, req->iv); err = crypto_gcm_encrypt_counter(aead, auth_tag, 0, req->iv);
if (err) if (err)
...@@ -239,18 +244,16 @@ static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req, ...@@ -239,18 +244,16 @@ static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
crypto_gcm_ghash_init(ghash, flags, ctx->gf128); crypto_gcm_ghash_init(ghash, flags, ctx->gf128);
if (req->assoclen) { crypto_gcm_ghash_update_sg(ghash, req->assoc, req->assoclen);
crypto_gcm_ghash_update_sg(ghash, req->assoc, req->assoclen); crypto_gcm_ghash_flush(ghash);
crypto_gcm_ghash_flush(ghash);
}
out: out:
return err; return err;
} }
static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err) static int crypto_gcm_hash(struct aead_request *req)
{ {
struct aead_request *req = areq->data; struct crypto_aead *aead = crypto_aead_reqtfm(req);
struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req); struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
u8 *auth_tag = pctx->auth_tag; u8 *auth_tag = pctx->auth_tag;
struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash; struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
...@@ -259,18 +262,28 @@ static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err) ...@@ -259,18 +262,28 @@ static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err)
crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen, crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen,
auth_tag); auth_tag);
scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
crypto_aead_authsize(aead), 1);
return 0;
}
static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err)
{
struct aead_request *req = areq->data;
if (!err)
err = crypto_gcm_hash(req);
aead_request_complete(req, err); aead_request_complete(req, err);
} }
static int crypto_gcm_encrypt(struct aead_request *req) static int crypto_gcm_encrypt(struct aead_request *req)
{ {
struct ablkcipher_request abreq; struct ablkcipher_request abreq;
struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
u8 *auth_tag = pctx->auth_tag;
struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
int err = 0; int err = 0;
err = crypto_gcm_init_crypt(&abreq, req, crypto_gcm_encrypt_done); err = crypto_gcm_init_crypt(&abreq, req, req->cryptlen,
crypto_gcm_encrypt_done);
if (err) if (err)
return err; return err;
...@@ -278,14 +291,9 @@ static int crypto_gcm_encrypt(struct aead_request *req) ...@@ -278,14 +291,9 @@ static int crypto_gcm_encrypt(struct aead_request *req)
err = crypto_ablkcipher_encrypt(&abreq); err = crypto_ablkcipher_encrypt(&abreq);
if (err) if (err)
return err; return err;
crypto_gcm_ghash_update_sg(ghash, req->dst, req->cryptlen);
} }
crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen, return crypto_gcm_hash(req);
auth_tag);
return err;
} }
static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err) static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err)
...@@ -296,25 +304,29 @@ static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err) ...@@ -296,25 +304,29 @@ static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err)
static int crypto_gcm_decrypt(struct aead_request *req) static int crypto_gcm_decrypt(struct aead_request *req)
{ {
struct ablkcipher_request abreq; struct ablkcipher_request abreq;
struct crypto_aead *aead = crypto_aead_reqtfm(req);
struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req); struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
u8 *auth_tag = pctx->auth_tag; u8 *auth_tag = pctx->auth_tag;
u8 *iauth_tag = pctx->iauth_tag;
struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash; struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
u8 tag[16]; unsigned int cryptlen = req->cryptlen;
unsigned int authsize = crypto_aead_authsize(aead);
int err; int err;
if (!req->cryptlen) if (cryptlen < authsize)
return -EINVAL; return -EINVAL;
cryptlen -= authsize;
memcpy(tag, auth_tag, 16); err = crypto_gcm_init_crypt(&abreq, req, cryptlen,
err = crypto_gcm_init_crypt(&abreq, req, crypto_gcm_decrypt_done); crypto_gcm_decrypt_done);
if (err) if (err)
return err; return err;
crypto_gcm_ghash_update_sg(ghash, req->src, req->cryptlen); crypto_gcm_ghash_update_sg(ghash, req->src, cryptlen);
crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen, crypto_gcm_ghash_final_xor(ghash, req->assoclen, cryptlen, auth_tag);
auth_tag);
if (memcmp(tag, auth_tag, 16)) scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
if (memcmp(iauth_tag, auth_tag, authsize))
return -EINVAL; return -EINVAL;
return crypto_ablkcipher_decrypt(&abreq); return crypto_ablkcipher_decrypt(&abreq);
......
...@@ -235,6 +235,7 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -235,6 +235,7 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
struct scatterlist asg[8]; struct scatterlist asg[8];
const char *e; const char *e;
struct tcrypt_result result; struct tcrypt_result result;
unsigned int authsize;
if (enc == ENCRYPT) if (enc == ENCRYPT)
e = "encryption"; e = "encryption";
...@@ -265,6 +266,8 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -265,6 +266,8 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
return; return;
} }
authsize = crypto_aead_authsize(tfm);
req = aead_request_alloc(tfm, GFP_KERNEL); req = aead_request_alloc(tfm, GFP_KERNEL);
if (!req) { if (!req) {
printk(KERN_INFO "failed to allocate request for %s\n", algo); printk(KERN_INFO "failed to allocate request for %s\n", algo);
...@@ -296,7 +299,7 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -296,7 +299,7 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
} }
sg_init_one(&sg[0], aead_tv[i].input, sg_init_one(&sg[0], aead_tv[i].input,
aead_tv[i].ilen); aead_tv[i].ilen + (enc ? authsize : 0));
sg_init_one(&asg[0], aead_tv[i].assoc, sg_init_one(&asg[0], aead_tv[i].assoc,
aead_tv[i].alen); aead_tv[i].alen);
...@@ -307,13 +310,9 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -307,13 +310,9 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
aead_request_set_assoc(req, asg, aead_tv[i].alen); aead_request_set_assoc(req, asg, aead_tv[i].alen);
if (enc) { ret = enc ?
ret = crypto_aead_encrypt(req); crypto_aead_encrypt(req) :
} else { crypto_aead_decrypt(req);
memcpy(req->__ctx, aead_tv[i].tag,
aead_tv[i].tlen);
ret = crypto_aead_decrypt(req);
}
switch (ret) { switch (ret) {
case 0: case 0:
...@@ -335,16 +334,10 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -335,16 +334,10 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
q = kmap(sg_page(&sg[0])) + sg[0].offset; q = kmap(sg_page(&sg[0])) + sg[0].offset;
hexdump(q, aead_tv[i].rlen); hexdump(q, aead_tv[i].rlen);
printk(KERN_INFO "auth tag: ");
hexdump((unsigned char *)req->__ctx, aead_tv[i].tlen);
printk(KERN_INFO "enc/dec: %s\n", printk(KERN_INFO "enc/dec: %s\n",
memcmp(q, aead_tv[i].result, memcmp(q, aead_tv[i].result,
aead_tv[i].rlen) ? "fail" : "pass"); aead_tv[i].rlen) ? "fail" : "pass");
printk(KERN_INFO "auth tag: %s\n",
memcmp(req->__ctx, aead_tv[i].tag,
aead_tv[i].tlen) ? "fail" : "pass");
} }
} }
...@@ -381,6 +374,9 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -381,6 +374,9 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
aead_tv[i].tap[k]); aead_tv[i].tap[k]);
} }
if (enc)
sg[k - 1].length += authsize;
sg_init_table(asg, aead_tv[i].anp); sg_init_table(asg, aead_tv[i].anp);
for (k = 0, temp = 0; k < aead_tv[i].anp; k++) { for (k = 0, temp = 0; k < aead_tv[i].anp; k++) {
memcpy(&axbuf[IDX[k]], memcpy(&axbuf[IDX[k]],
...@@ -397,13 +393,9 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -397,13 +393,9 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
aead_request_set_assoc(req, asg, aead_tv[i].alen); aead_request_set_assoc(req, asg, aead_tv[i].alen);
if (enc) { ret = enc ?
ret = crypto_aead_encrypt(req); crypto_aead_encrypt(req) :
} else { crypto_aead_decrypt(req);
memcpy(req->__ctx, aead_tv[i].tag,
aead_tv[i].tlen);
ret = crypto_aead_decrypt(req);
}
switch (ret) { switch (ret) {
case 0: case 0:
...@@ -429,17 +421,13 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template, ...@@ -429,17 +421,13 @@ static void test_aead(char *algo, int enc, struct aead_testvec *template,
hexdump(q, aead_tv[i].tap[k]); hexdump(q, aead_tv[i].tap[k]);
printk(KERN_INFO "%s\n", printk(KERN_INFO "%s\n",
memcmp(q, aead_tv[i].result + temp, memcmp(q, aead_tv[i].result + temp,
aead_tv[i].tap[k]) ? aead_tv[i].tap[k] -
(k < aead_tv[i].np - 1 || enc ?
0 : authsize)) ?
"fail" : "pass"); "fail" : "pass");
temp += aead_tv[i].tap[k]; temp += aead_tv[i].tap[k];
} }
printk(KERN_INFO "auth tag: ");
hexdump((unsigned char *)req->__ctx, aead_tv[i].tlen);
printk(KERN_INFO "auth tag: %s\n",
memcmp(req->__ctx, aead_tv[i].tag,
aead_tv[i].tlen) ? "fail" : "pass");
} }
} }
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment