2017-11-24 22:00:34 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2011-04-20 03:29:18 +08:00
|
|
|
/*
|
|
|
|
* Cryptographic API.
|
|
|
|
*
|
|
|
|
* s390 implementation of the GHASH algorithm for GCM (Galois/Counter Mode).
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2011
|
|
|
|
* Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/internal/hash.h>
|
|
|
|
#include <linux/module.h>
|
2015-02-20 00:34:07 +08:00
|
|
|
#include <linux/cpufeature.h>
|
2016-03-17 22:22:12 +08:00
|
|
|
#include <asm/cpacf.h>
|
2011-04-20 03:29:18 +08:00
|
|
|
|
|
|
|
#define GHASH_BLOCK_SIZE 16
|
|
|
|
#define GHASH_DIGEST_SIZE 16
|
|
|
|
|
|
|
|
struct ghash_ctx {
|
2015-05-21 16:01:11 +08:00
|
|
|
u8 key[GHASH_BLOCK_SIZE];
|
2011-04-20 03:29:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ghash_desc_ctx {
|
2015-05-21 16:01:11 +08:00
|
|
|
u8 icv[GHASH_BLOCK_SIZE];
|
|
|
|
u8 key[GHASH_BLOCK_SIZE];
|
2011-04-20 03:29:18 +08:00
|
|
|
u8 buffer[GHASH_BLOCK_SIZE];
|
|
|
|
u32 bytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ghash_init(struct shash_desc *desc)
|
|
|
|
{
|
|
|
|
struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
|
2015-05-21 16:01:11 +08:00
|
|
|
struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
|
2011-04-20 03:29:18 +08:00
|
|
|
|
|
|
|
memset(dctx, 0, sizeof(*dctx));
|
2015-05-21 16:01:11 +08:00
|
|
|
memcpy(dctx->key, ctx->key, GHASH_BLOCK_SIZE);
|
2011-04-20 03:29:18 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ghash_setkey(struct crypto_shash *tfm,
|
|
|
|
const u8 *key, unsigned int keylen)
|
|
|
|
{
|
|
|
|
struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
|
|
|
|
|
crypto: remove CRYPTO_TFM_RES_BAD_KEY_LEN
The CRYPTO_TFM_RES_BAD_KEY_LEN flag was apparently meant as a way to
make the ->setkey() functions provide more information about errors.
However, no one actually checks for this flag, which makes it pointless.
Also, many algorithms fail to set this flag when given a bad length key.
Reviewing just the generic implementations, this is the case for
aes-fixed-time, cbcmac, echainiv, nhpoly1305, pcrypt, rfc3686, rfc4309,
rfc7539, rfc7539esp, salsa20, seqiv, and xcbc. But there are probably
many more in arch/*/crypto/ and drivers/crypto/.
Some algorithms can even set this flag when the key is the correct
length. For example, authenc and authencesn set it when the key payload
is malformed in any way (not just a bad length), the atmel-sha and ccree
drivers can set it if a memory allocation fails, and the chelsio driver
sets it for bad auth tag lengths, not just bad key lengths.
So even if someone actually wanted to start checking this flag (which
seems unlikely, since it's been unused for a long time), there would be
a lot of work needed to get it working correctly. But it would probably
be much better to go back to the drawing board and just define different
return values, like -EINVAL if the key is invalid for the algorithm vs.
-EKEYREJECTED if the key was rejected by a policy like "no weak keys".
That would be much simpler, less error-prone, and easier to test.
So just remove this flag.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-12-31 11:19:36 +08:00
|
|
|
if (keylen != GHASH_BLOCK_SIZE)
|
2011-04-20 03:29:18 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memcpy(ctx->key, key, GHASH_BLOCK_SIZE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ghash_update(struct shash_desc *desc,
|
|
|
|
const u8 *src, unsigned int srclen)
|
|
|
|
{
|
|
|
|
struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
|
|
|
|
unsigned int n;
|
|
|
|
u8 *buf = dctx->buffer;
|
|
|
|
|
|
|
|
if (dctx->bytes) {
|
|
|
|
u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
|
|
|
|
|
|
|
|
n = min(srclen, dctx->bytes);
|
|
|
|
dctx->bytes -= n;
|
|
|
|
srclen -= n;
|
|
|
|
|
|
|
|
memcpy(pos, src, n);
|
|
|
|
src += n;
|
|
|
|
|
|
|
|
if (!dctx->bytes) {
|
2016-08-15 16:41:52 +08:00
|
|
|
cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf,
|
|
|
|
GHASH_BLOCK_SIZE);
|
2011-04-20 03:29:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = srclen & ~(GHASH_BLOCK_SIZE - 1);
|
|
|
|
if (n) {
|
2016-08-15 16:41:52 +08:00
|
|
|
cpacf_kimd(CPACF_KIMD_GHASH, dctx, src, n);
|
2011-04-20 03:29:18 +08:00
|
|
|
src += n;
|
|
|
|
srclen -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srclen) {
|
|
|
|
dctx->bytes = GHASH_BLOCK_SIZE - srclen;
|
|
|
|
memcpy(buf, src, srclen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:01:11 +08:00
|
|
|
static int ghash_flush(struct ghash_desc_ctx *dctx)
|
2011-04-20 03:29:18 +08:00
|
|
|
{
|
|
|
|
u8 *buf = dctx->buffer;
|
|
|
|
|
|
|
|
if (dctx->bytes) {
|
|
|
|
u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
|
|
|
|
|
|
|
|
memset(pos, 0, dctx->bytes);
|
2016-08-15 16:41:52 +08:00
|
|
|
cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE);
|
2015-05-21 16:01:11 +08:00
|
|
|
dctx->bytes = 0;
|
2011-04-20 03:29:18 +08:00
|
|
|
}
|
|
|
|
|
2012-10-26 21:06:12 +08:00
|
|
|
return 0;
|
2011-04-20 03:29:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ghash_final(struct shash_desc *desc, u8 *dst)
|
|
|
|
{
|
|
|
|
struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
|
2012-10-26 21:06:12 +08:00
|
|
|
int ret;
|
2011-04-20 03:29:18 +08:00
|
|
|
|
2015-05-21 16:01:11 +08:00
|
|
|
ret = ghash_flush(dctx);
|
2012-10-26 21:06:12 +08:00
|
|
|
if (!ret)
|
2015-05-21 16:01:11 +08:00
|
|
|
memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE);
|
2012-10-26 21:06:12 +08:00
|
|
|
return ret;
|
2011-04-20 03:29:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct shash_alg ghash_alg = {
|
|
|
|
.digestsize = GHASH_DIGEST_SIZE,
|
|
|
|
.init = ghash_init,
|
|
|
|
.update = ghash_update,
|
|
|
|
.final = ghash_final,
|
|
|
|
.setkey = ghash_setkey,
|
|
|
|
.descsize = sizeof(struct ghash_desc_ctx),
|
|
|
|
.base = {
|
|
|
|
.cra_name = "ghash",
|
|
|
|
.cra_driver_name = "ghash-s390",
|
2016-03-17 22:22:12 +08:00
|
|
|
.cra_priority = 300,
|
2011-04-20 03:29:18 +08:00
|
|
|
.cra_blocksize = GHASH_BLOCK_SIZE,
|
|
|
|
.cra_ctxsize = sizeof(struct ghash_ctx),
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init ghash_mod_init(void)
|
|
|
|
{
|
2016-08-18 18:59:46 +08:00
|
|
|
if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_GHASH))
|
2019-06-12 21:33:04 +08:00
|
|
|
return -ENODEV;
|
2011-04-20 03:29:18 +08:00
|
|
|
|
|
|
|
return crypto_register_shash(&ghash_alg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit ghash_mod_exit(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_shash(&ghash_alg);
|
|
|
|
}
|
|
|
|
|
2015-02-20 00:34:07 +08:00
|
|
|
module_cpu_feature_match(MSA, ghash_mod_init);
|
2011-04-20 03:29:18 +08:00
|
|
|
module_exit(ghash_mod_exit);
|
|
|
|
|
2014-11-21 09:05:53 +08:00
|
|
|
MODULE_ALIAS_CRYPTO("ghash");
|
2011-04-20 03:29:18 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2019-07-20 14:09:18 +08:00
|
|
|
MODULE_DESCRIPTION("GHASH hash function, s390 implementation");
|