crypto: gcm - Use GHASH library instead of crypto_ahash

Make the "gcm" template access GHASH using the library API instead of
crypto_ahash.  This is much simpler and more efficient, especially given
that all GHASH implementations are synchronous and CPU-based anyway.

Note that this allows "ghash" to be removed from the crypto_ahash (and
crypto_shash) API, which a later commit will do.

This mirrors the similar cleanup that was done with POLYVAL.

Acked-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20260319061723.1140720-16-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
This commit is contained in:
Eric Biggers
2026-03-18 23:17:16 -07:00
parent 3e79c8ec49
commit 9f4e9553a1
4 changed files with 92 additions and 349 deletions

View File

@@ -794,7 +794,7 @@ config CRYPTO_GCM
tristate "GCM (Galois/Counter Mode) and GMAC (GCM MAC)" tristate "GCM (Galois/Counter Mode) and GMAC (GCM MAC)"
select CRYPTO_CTR select CRYPTO_CTR
select CRYPTO_AEAD select CRYPTO_AEAD
select CRYPTO_GHASH select CRYPTO_LIB_GF128HASH
select CRYPTO_MANAGER select CRYPTO_MANAGER
help help
GCM (Galois/Counter Mode) authenticated encryption mode and GMAC GCM (Galois/Counter Mode) authenticated encryption mode and GMAC

View File

@@ -5,13 +5,11 @@
* Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi> * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
*/ */
#include <crypto/gf128mul.h>
#include <crypto/internal/aead.h> #include <crypto/internal/aead.h>
#include <crypto/internal/skcipher.h> #include <crypto/internal/skcipher.h>
#include <crypto/internal/hash.h>
#include <crypto/scatterwalk.h> #include <crypto/scatterwalk.h>
#include <crypto/gcm.h> #include <crypto/gcm.h>
#include <crypto/hash.h> #include <crypto/gf128hash.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/kernel.h> #include <linux/kernel.h>
@@ -20,12 +18,11 @@
struct gcm_instance_ctx { struct gcm_instance_ctx {
struct crypto_skcipher_spawn ctr; struct crypto_skcipher_spawn ctr;
struct crypto_ahash_spawn ghash;
}; };
struct crypto_gcm_ctx { struct crypto_gcm_ctx {
struct crypto_skcipher *ctr; struct crypto_skcipher *ctr;
struct crypto_ahash *ghash; struct ghash_key ghash;
}; };
struct crypto_rfc4106_ctx { struct crypto_rfc4106_ctx {
@@ -52,31 +49,15 @@ struct crypto_rfc4543_req_ctx {
struct aead_request subreq; struct aead_request subreq;
}; };
struct crypto_gcm_ghash_ctx {
unsigned int cryptlen;
struct scatterlist *src;
int (*complete)(struct aead_request *req, u32 flags);
};
struct crypto_gcm_req_priv_ctx { struct crypto_gcm_req_priv_ctx {
u8 iv[16]; u8 iv[16];
u8 auth_tag[16]; u8 auth_tag[16];
u8 iauth_tag[16]; u8 iauth_tag[16];
struct scatterlist src[3]; struct scatterlist src[3];
struct scatterlist dst[3]; struct scatterlist dst[3];
struct scatterlist sg; struct skcipher_request skreq; /* Must be last */
struct crypto_gcm_ghash_ctx ghash_ctx;
union {
struct ahash_request ahreq;
struct skcipher_request skreq;
} u;
}; };
static struct {
u8 buf[16];
struct scatterlist sg;
} *gcm_zeroes;
static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx( static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
struct aead_request *req) struct aead_request *req)
{ {
@@ -89,10 +70,9 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
unsigned int keylen) unsigned int keylen)
{ {
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
struct crypto_ahash *ghash = ctx->ghash;
struct crypto_skcipher *ctr = ctx->ctr; struct crypto_skcipher *ctr = ctx->ctr;
struct { struct {
be128 hash; u8 h[GHASH_BLOCK_SIZE];
u8 iv[16]; u8 iv[16];
struct crypto_wait wait; struct crypto_wait wait;
@@ -115,14 +95,14 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
return -ENOMEM; return -ENOMEM;
crypto_init_wait(&data->wait); crypto_init_wait(&data->wait);
sg_init_one(data->sg, &data->hash, sizeof(data->hash)); sg_init_one(data->sg, data->h, sizeof(data->h));
skcipher_request_set_tfm(&data->req, ctr); skcipher_request_set_tfm(&data->req, ctr);
skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
CRYPTO_TFM_REQ_MAY_BACKLOG, CRYPTO_TFM_REQ_MAY_BACKLOG,
crypto_req_done, crypto_req_done,
&data->wait); &data->wait);
skcipher_request_set_crypt(&data->req, data->sg, data->sg, skcipher_request_set_crypt(&data->req, data->sg, data->sg,
sizeof(data->hash), data->iv); sizeof(data->h), data->iv);
err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
&data->wait); &data->wait);
@@ -130,10 +110,7 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
if (err) if (err)
goto out; goto out;
crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK); ghash_preparekey(&ctx->ghash, data->h);
crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
CRYPTO_TFM_REQ_MASK);
err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
out: out:
kfree_sensitive(data); kfree_sensitive(data);
return err; return err;
@@ -176,7 +153,7 @@ static void crypto_gcm_init_crypt(struct aead_request *req,
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);
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct skcipher_request *skreq = &pctx->u.skreq; struct skcipher_request *skreq = &pctx->skreq;
struct scatterlist *dst; struct scatterlist *dst;
dst = req->src == req->dst ? pctx->src : pctx->dst; dst = req->src == req->dst ? pctx->src : pctx->dst;
@@ -187,244 +164,65 @@ static void crypto_gcm_init_crypt(struct aead_request *req,
pctx->iv); pctx->iv);
} }
static inline unsigned int gcm_remain(unsigned int len) static void ghash_update_sg_and_pad(struct ghash_ctx *ghash,
struct scatterlist *sg, unsigned int len)
{ {
len &= 0xfU; static const u8 zeroes[GHASH_BLOCK_SIZE];
return len ? 16 - len : 0;
if (len) {
unsigned int pad_len = -len % GHASH_BLOCK_SIZE;
struct scatter_walk walk;
scatterwalk_start(&walk, sg);
do {
unsigned int n = scatterwalk_next(&walk, len);
ghash_update(ghash, walk.addr, n);
scatterwalk_done_src(&walk, n);
len -= n;
} while (len);
if (pad_len)
ghash_update(ghash, zeroes, pad_len);
}
} }
static void gcm_hash_len_done(void *data, int err); static void gcm_hash(struct aead_request *req, struct scatterlist *ctext,
unsigned int datalen, u8 out[GHASH_BLOCK_SIZE])
static int gcm_hash_update(struct aead_request *req,
crypto_completion_t compl,
struct scatterlist *src,
unsigned int len, u32 flags)
{ {
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); const struct crypto_gcm_ctx *ctx =
struct ahash_request *ahreq = &pctx->u.ahreq; crypto_aead_ctx(crypto_aead_reqtfm(req));
__be64 lengths[2] = {
cpu_to_be64(8 * (u64)req->assoclen),
cpu_to_be64(8 * (u64)datalen),
};
struct ghash_ctx ghash;
ahash_request_set_callback(ahreq, flags, compl, req); ghash_init(&ghash, &ctx->ghash);
ahash_request_set_crypt(ahreq, src, NULL, len);
return crypto_ahash_update(ahreq); /* Associated data, then zero-padding to the next 16-byte boundary */
ghash_update_sg_and_pad(&ghash, req->src, req->assoclen);
/* Ciphertext, then zero-padding to the next 16-byte boundary */
ghash_update_sg_and_pad(&ghash, ctext, datalen);
/* Lengths block */
ghash_update(&ghash, (const u8 *)lengths, sizeof(lengths));
ghash_final(&ghash, out);
} }
static int gcm_hash_remain(struct aead_request *req, static int gcm_add_auth_tag(struct aead_request *req)
unsigned int remain,
crypto_completion_t compl, u32 flags)
{ {
return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
}
static int gcm_hash_len(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct ahash_request *ahreq = &pctx->u.ahreq;
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
be128 lengths;
lengths.a = cpu_to_be64(req->assoclen * 8);
lengths.b = cpu_to_be64(gctx->cryptlen * 8);
memcpy(pctx->iauth_tag, &lengths, 16);
sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
ahash_request_set_crypt(ahreq, &pctx->sg,
pctx->iauth_tag, sizeof(lengths));
return crypto_ahash_finup(ahreq);
}
static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
return gctx->complete(req, flags);
}
static void gcm_hash_len_done(void *data, int err)
{
struct aead_request *req = data;
if (err)
goto out;
err = gcm_hash_len_continue(req, 0);
if (err == -EINPROGRESS)
return;
out:
aead_request_complete(req, err);
}
static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
{
return gcm_hash_len(req, flags) ?:
gcm_hash_len_continue(req, flags);
}
static void gcm_hash_crypt_remain_done(void *data, int err)
{
struct aead_request *req = data;
if (err)
goto out;
err = gcm_hash_crypt_remain_continue(req, 0);
if (err == -EINPROGRESS)
return;
out:
aead_request_complete(req, err);
}
static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
unsigned int remain;
remain = gcm_remain(gctx->cryptlen);
if (remain)
return gcm_hash_remain(req, remain,
gcm_hash_crypt_remain_done, flags) ?:
gcm_hash_crypt_remain_continue(req, flags);
return gcm_hash_crypt_remain_continue(req, flags);
}
static void gcm_hash_crypt_done(void *data, int err)
{
struct aead_request *req = data;
if (err)
goto out;
err = gcm_hash_crypt_continue(req, 0);
if (err == -EINPROGRESS)
return;
out:
aead_request_complete(req, err);
}
static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
if (gctx->cryptlen)
return gcm_hash_update(req, gcm_hash_crypt_done,
gctx->src, gctx->cryptlen, flags) ?:
gcm_hash_crypt_continue(req, flags);
return gcm_hash_crypt_remain_continue(req, flags);
}
static void gcm_hash_assoc_remain_done(void *data, int err)
{
struct aead_request *req = data;
if (err)
goto out;
err = gcm_hash_assoc_remain_continue(req, 0);
if (err == -EINPROGRESS)
return;
out:
aead_request_complete(req, err);
}
static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
{
unsigned int remain;
remain = gcm_remain(req->assoclen);
if (remain)
return gcm_hash_remain(req, remain,
gcm_hash_assoc_remain_done, flags) ?:
gcm_hash_assoc_remain_continue(req, flags);
return gcm_hash_assoc_remain_continue(req, flags);
}
static void gcm_hash_assoc_done(void *data, int err)
{
struct aead_request *req = data;
if (err)
goto out;
err = gcm_hash_assoc_continue(req, 0);
if (err == -EINPROGRESS)
return;
out:
aead_request_complete(req, err);
}
static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
{
if (req->assoclen)
return gcm_hash_update(req, gcm_hash_assoc_done,
req->src, req->assoclen, flags) ?:
gcm_hash_assoc_continue(req, flags);
return gcm_hash_assoc_remain_continue(req, flags);
}
static void gcm_hash_init_done(void *data, int err)
{
struct aead_request *req = data;
if (err)
goto out;
err = gcm_hash_init_continue(req, 0);
if (err == -EINPROGRESS)
return;
out:
aead_request_complete(req, err);
}
static int gcm_hash(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct ahash_request *ahreq = &pctx->u.ahreq;
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
ahash_request_set_tfm(ahreq, ctx->ghash);
ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
return crypto_ahash_init(ahreq) ?:
gcm_hash_init_continue(req, flags);
}
static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct crypto_aead *aead = crypto_aead_reqtfm(req); struct crypto_aead *aead = crypto_aead_reqtfm(req);
u8 *auth_tag = pctx->auth_tag;
crypto_xor(auth_tag, pctx->iauth_tag, 16);
scatterwalk_map_and_copy(auth_tag, req->dst,
req->assoclen + req->cryptlen,
crypto_aead_authsize(aead), 1);
return 0;
}
static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst); gcm_hash(req, sg_next(req->src == req->dst ? pctx->src : pctx->dst),
gctx->cryptlen = req->cryptlen; req->cryptlen, pctx->iauth_tag);
gctx->complete = gcm_enc_copy_hash; crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
memcpy_to_sglist(req->dst, req->assoclen + req->cryptlen,
return gcm_hash(req, flags); pctx->auth_tag, crypto_aead_authsize(aead));
return 0;
} }
static void gcm_encrypt_done(void *data, int err) static void gcm_encrypt_done(void *data, int err)
@@ -434,9 +232,7 @@ static void gcm_encrypt_done(void *data, int err)
if (err) if (err)
goto out; goto out;
err = gcm_encrypt_continue(req, 0); err = gcm_add_auth_tag(req);
if (err == -EINPROGRESS)
return;
out: out:
aead_request_complete(req, err); aead_request_complete(req, err);
@@ -445,15 +241,14 @@ out:
static int crypto_gcm_encrypt(struct aead_request *req) static int crypto_gcm_encrypt(struct aead_request *req)
{ {
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct skcipher_request *skreq = &pctx->u.skreq; struct skcipher_request *skreq = &pctx->skreq;
u32 flags = aead_request_flags(req); u32 flags = aead_request_flags(req);
crypto_gcm_init_common(req); crypto_gcm_init_common(req);
crypto_gcm_init_crypt(req, req->cryptlen); crypto_gcm_init_crypt(req, req->cryptlen);
skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req); skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
return crypto_skcipher_encrypt(skreq) ?: return crypto_skcipher_encrypt(skreq) ?: gcm_add_auth_tag(req);
gcm_encrypt_continue(req, flags);
} }
static int crypto_gcm_verify(struct aead_request *req) static int crypto_gcm_verify(struct aead_request *req)
@@ -481,35 +276,21 @@ static void gcm_decrypt_done(void *data, int err)
aead_request_complete(req, err); aead_request_complete(req, err);
} }
static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
{
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct skcipher_request *skreq = &pctx->u.skreq;
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
crypto_gcm_init_crypt(req, gctx->cryptlen);
skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
}
static int crypto_gcm_decrypt(struct aead_request *req) static int crypto_gcm_decrypt(struct aead_request *req)
{ {
struct crypto_aead *aead = crypto_aead_reqtfm(req); struct crypto_aead *aead = crypto_aead_reqtfm(req);
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; struct skcipher_request *skreq = &pctx->skreq;
unsigned int authsize = crypto_aead_authsize(aead); unsigned int datalen = req->cryptlen - crypto_aead_authsize(aead);
unsigned int cryptlen = req->cryptlen;
u32 flags = aead_request_flags(req);
cryptlen -= authsize;
crypto_gcm_init_common(req); crypto_gcm_init_common(req);
gctx->src = sg_next(pctx->src); gcm_hash(req, sg_next(pctx->src), datalen, pctx->iauth_tag);
gctx->cryptlen = cryptlen;
gctx->complete = gcm_dec_hash_continue;
return gcm_hash(req, flags); crypto_gcm_init_crypt(req, datalen);
skcipher_request_set_callback(skreq, aead_request_flags(req),
gcm_decrypt_done, req);
return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
} }
static int crypto_gcm_init_tfm(struct crypto_aead *tfm) static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
@@ -518,43 +299,26 @@ static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
struct gcm_instance_ctx *ictx = aead_instance_ctx(inst); struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
struct crypto_skcipher *ctr; struct crypto_skcipher *ctr;
struct crypto_ahash *ghash;
unsigned long align; unsigned long align;
int err;
ghash = crypto_spawn_ahash(&ictx->ghash);
if (IS_ERR(ghash))
return PTR_ERR(ghash);
ctr = crypto_spawn_skcipher(&ictx->ctr); ctr = crypto_spawn_skcipher(&ictx->ctr);
err = PTR_ERR(ctr);
if (IS_ERR(ctr)) if (IS_ERR(ctr))
goto err_free_hash; return PTR_ERR(ctr);
ctx->ctr = ctr; ctx->ctr = ctr;
ctx->ghash = ghash;
align = crypto_aead_alignmask(tfm); align = crypto_aead_alignmask(tfm);
align &= ~(crypto_tfm_ctx_alignment() - 1); align &= ~(crypto_tfm_ctx_alignment() - 1);
crypto_aead_set_reqsize(tfm, crypto_aead_set_reqsize(tfm,
align + offsetof(struct crypto_gcm_req_priv_ctx, u) + align + sizeof(struct crypto_gcm_req_priv_ctx) +
max(sizeof(struct skcipher_request) + crypto_skcipher_reqsize(ctr));
crypto_skcipher_reqsize(ctr),
sizeof(struct ahash_request) +
crypto_ahash_reqsize(ghash)));
return 0; return 0;
err_free_hash:
crypto_free_ahash(ghash);
return err;
} }
static void crypto_gcm_exit_tfm(struct crypto_aead *tfm) static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
{ {
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
crypto_free_ahash(ctx->ghash);
crypto_free_skcipher(ctx->ctr); crypto_free_skcipher(ctx->ctr);
} }
@@ -563,20 +327,16 @@ static void crypto_gcm_free(struct aead_instance *inst)
struct gcm_instance_ctx *ctx = aead_instance_ctx(inst); struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
crypto_drop_skcipher(&ctx->ctr); crypto_drop_skcipher(&ctx->ctr);
crypto_drop_ahash(&ctx->ghash);
kfree(inst); kfree(inst);
} }
static int crypto_gcm_create_common(struct crypto_template *tmpl, static int crypto_gcm_create_common(struct crypto_template *tmpl,
struct rtattr **tb, struct rtattr **tb, const char *ctr_name)
const char *ctr_name,
const char *ghash_name)
{ {
struct skcipher_alg_common *ctr; struct skcipher_alg_common *ctr;
u32 mask; u32 mask;
struct aead_instance *inst; struct aead_instance *inst;
struct gcm_instance_ctx *ctx; struct gcm_instance_ctx *ctx;
struct hash_alg_common *ghash;
int err; int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
@@ -588,17 +348,6 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl,
return -ENOMEM; return -ENOMEM;
ctx = aead_instance_ctx(inst); ctx = aead_instance_ctx(inst);
err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst),
ghash_name, 0, mask);
if (err)
goto err_free_inst;
ghash = crypto_spawn_ahash_alg(&ctx->ghash);
err = -EINVAL;
if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
ghash->digestsize != 16)
goto err_free_inst;
err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst), err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
ctr_name, 0, mask); ctr_name, 0, mask);
if (err) if (err)
@@ -617,13 +366,11 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl,
goto err_free_inst; goto err_free_inst;
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
"gcm_base(%s,%s)", ctr->base.cra_driver_name, "gcm_base(%s,ghash-lib)",
ghash->base.cra_driver_name) >= ctr->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
CRYPTO_MAX_ALG_NAME)
goto err_free_inst; goto err_free_inst;
inst->alg.base.cra_priority = (ghash->base.cra_priority + inst->alg.base.cra_priority = ctr->base.cra_priority;
ctr->base.cra_priority) / 2;
inst->alg.base.cra_blocksize = 1; inst->alg.base.cra_blocksize = 1;
inst->alg.base.cra_alignmask = ctr->base.cra_alignmask; inst->alg.base.cra_alignmask = ctr->base.cra_alignmask;
inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx); inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
@@ -660,7 +407,7 @@ static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
CRYPTO_MAX_ALG_NAME) CRYPTO_MAX_ALG_NAME)
return -ENAMETOOLONG; return -ENAMETOOLONG;
return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash"); return crypto_gcm_create_common(tmpl, tb, ctr_name);
} }
static int crypto_gcm_base_create(struct crypto_template *tmpl, static int crypto_gcm_base_create(struct crypto_template *tmpl,
@@ -677,7 +424,16 @@ static int crypto_gcm_base_create(struct crypto_template *tmpl,
if (IS_ERR(ghash_name)) if (IS_ERR(ghash_name))
return PTR_ERR(ghash_name); return PTR_ERR(ghash_name);
return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name); /*
* Originally this parameter allowed requesting a specific
* implementation of GHASH. This is no longer supported. Now the best
* implementation of GHASH is just always used.
*/
if (strcmp(ghash_name, "ghash") != 0 &&
strcmp(ghash_name, "ghash-lib") != 0)
return -EINVAL;
return crypto_gcm_create_common(tmpl, tb, ctr_name);
} }
static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key, static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
@@ -1096,25 +852,12 @@ static struct crypto_template crypto_gcm_tmpls[] = {
static int __init crypto_gcm_module_init(void) static int __init crypto_gcm_module_init(void)
{ {
int err; return crypto_register_templates(crypto_gcm_tmpls,
ARRAY_SIZE(crypto_gcm_tmpls));
gcm_zeroes = kzalloc_obj(*gcm_zeroes);
if (!gcm_zeroes)
return -ENOMEM;
sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
err = crypto_register_templates(crypto_gcm_tmpls,
ARRAY_SIZE(crypto_gcm_tmpls));
if (err)
kfree(gcm_zeroes);
return err;
} }
static void __exit crypto_gcm_module_exit(void) static void __exit crypto_gcm_module_exit(void)
{ {
kfree(gcm_zeroes);
crypto_unregister_templates(crypto_gcm_tmpls, crypto_unregister_templates(crypto_gcm_tmpls,
ARRAY_SIZE(crypto_gcm_tmpls)); ARRAY_SIZE(crypto_gcm_tmpls));
} }

View File

@@ -4965,7 +4965,7 @@ static const struct alg_test_desc alg_test_descs[] = {
}, { }, {
#endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */ #endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */
.alg = "gcm(aes)", .alg = "gcm(aes)",
.generic_driver = "gcm_base(ctr(aes-lib),ghash-generic)", .generic_driver = "gcm_base(ctr(aes-lib),ghash-lib)",
.test = alg_test_aead, .test = alg_test_aead,
.fips_allowed = 1, .fips_allowed = 1,
.suite = { .suite = {
@@ -4973,14 +4973,14 @@ static const struct alg_test_desc alg_test_descs[] = {
} }
}, { }, {
.alg = "gcm(aria)", .alg = "gcm(aria)",
.generic_driver = "gcm_base(ctr(aria-generic),ghash-generic)", .generic_driver = "gcm_base(ctr(aria-generic),ghash-lib)",
.test = alg_test_aead, .test = alg_test_aead,
.suite = { .suite = {
.aead = __VECS(aria_gcm_tv_template) .aead = __VECS(aria_gcm_tv_template)
} }
}, { }, {
.alg = "gcm(sm4)", .alg = "gcm(sm4)",
.generic_driver = "gcm_base(ctr(sm4-generic),ghash-generic)", .generic_driver = "gcm_base(ctr(sm4-generic),ghash-lib)",
.test = alg_test_aead, .test = alg_test_aead,
.suite = { .suite = {
.aead = __VECS(sm4_gcm_tv_template) .aead = __VECS(sm4_gcm_tv_template)
@@ -5314,7 +5314,7 @@ static const struct alg_test_desc alg_test_descs[] = {
} }
}, { }, {
.alg = "rfc4106(gcm(aes))", .alg = "rfc4106(gcm(aes))",
.generic_driver = "rfc4106(gcm_base(ctr(aes-lib),ghash-generic))", .generic_driver = "rfc4106(gcm_base(ctr(aes-lib),ghash-lib))",
.test = alg_test_aead, .test = alg_test_aead,
.fips_allowed = 1, .fips_allowed = 1,
.suite = { .suite = {
@@ -5338,7 +5338,7 @@ static const struct alg_test_desc alg_test_descs[] = {
} }
}, { }, {
.alg = "rfc4543(gcm(aes))", .alg = "rfc4543(gcm(aes))",
.generic_driver = "rfc4543(gcm_base(ctr(aes-lib),ghash-generic))", .generic_driver = "rfc4543(gcm_base(ctr(aes-lib),ghash-lib))",
.test = alg_test_aead, .test = alg_test_aead,
.suite = { .suite = {
.aead = { .aead = {

View File

@@ -1008,7 +1008,7 @@ static int starfive_aes_ccm_init_tfm(struct crypto_aead *tfm)
static int starfive_aes_gcm_init_tfm(struct crypto_aead *tfm) static int starfive_aes_gcm_init_tfm(struct crypto_aead *tfm)
{ {
return starfive_aes_aead_init_tfm(tfm, "gcm_base(ctr(aes-lib),ghash-generic)"); return starfive_aes_aead_init_tfm(tfm, "gcm_base(ctr(aes-lib),ghash-lib)");
} }
static struct skcipher_engine_alg skcipher_algs[] = { static struct skcipher_engine_alg skcipher_algs[] = {