crypto: sha3-generic - simplify code
In preparation of exposing the generic SHA3 implementation to other versions as a fallback, simplify the code, and remove an inconsistency in the output handling (endian swabbing rsizw words of state before writing the output does not make sense) Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
parent
83dee2ce1a
commit
beeb504adf
|
@ -18,7 +18,6 @@
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/types.h>
|
#include <linux/types.h>
|
||||||
#include <crypto/sha3.h>
|
#include <crypto/sha3.h>
|
||||||
#include <asm/byteorder.h>
|
|
||||||
#include <asm/unaligned.h>
|
#include <asm/unaligned.h>
|
||||||
|
|
||||||
#define KECCAK_ROUNDS 24
|
#define KECCAK_ROUNDS 24
|
||||||
|
@ -146,43 +145,16 @@ static void __attribute__((__optimize__("O3"))) keccakf(u64 st[25])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sha3_init(struct sha3_state *sctx, unsigned int digest_sz)
|
static int sha3_init(struct shash_desc *desc)
|
||||||
{
|
{
|
||||||
memset(sctx, 0, sizeof(*sctx));
|
struct sha3_state *sctx = shash_desc_ctx(desc);
|
||||||
sctx->md_len = digest_sz;
|
unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
|
||||||
sctx->rsiz = 200 - 2 * digest_sz;
|
|
||||||
|
sctx->rsiz = 200 - 2 * digest_size;
|
||||||
sctx->rsizw = sctx->rsiz / 8;
|
sctx->rsizw = sctx->rsiz / 8;
|
||||||
}
|
sctx->partial = 0;
|
||||||
|
|
||||||
static int sha3_224_init(struct shash_desc *desc)
|
memset(sctx->st, 0, sizeof(sctx->st));
|
||||||
{
|
|
||||||
struct sha3_state *sctx = shash_desc_ctx(desc);
|
|
||||||
|
|
||||||
sha3_init(sctx, SHA3_224_DIGEST_SIZE);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha3_256_init(struct shash_desc *desc)
|
|
||||||
{
|
|
||||||
struct sha3_state *sctx = shash_desc_ctx(desc);
|
|
||||||
|
|
||||||
sha3_init(sctx, SHA3_256_DIGEST_SIZE);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha3_384_init(struct shash_desc *desc)
|
|
||||||
{
|
|
||||||
struct sha3_state *sctx = shash_desc_ctx(desc);
|
|
||||||
|
|
||||||
sha3_init(sctx, SHA3_384_DIGEST_SIZE);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha3_512_init(struct shash_desc *desc)
|
|
||||||
{
|
|
||||||
struct sha3_state *sctx = shash_desc_ctx(desc);
|
|
||||||
|
|
||||||
sha3_init(sctx, SHA3_512_DIGEST_SIZE);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,6 +199,8 @@ static int sha3_final(struct shash_desc *desc, u8 *out)
|
||||||
{
|
{
|
||||||
struct sha3_state *sctx = shash_desc_ctx(desc);
|
struct sha3_state *sctx = shash_desc_ctx(desc);
|
||||||
unsigned int i, inlen = sctx->partial;
|
unsigned int i, inlen = sctx->partial;
|
||||||
|
unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
|
||||||
|
__le64 *digest = (__le64 *)out;
|
||||||
|
|
||||||
sctx->buf[inlen++] = 0x06;
|
sctx->buf[inlen++] = 0x06;
|
||||||
memset(sctx->buf + inlen, 0, sctx->rsiz - inlen);
|
memset(sctx->buf + inlen, 0, sctx->rsiz - inlen);
|
||||||
|
@ -237,110 +211,70 @@ static int sha3_final(struct shash_desc *desc, u8 *out)
|
||||||
|
|
||||||
keccakf(sctx->st);
|
keccakf(sctx->st);
|
||||||
|
|
||||||
for (i = 0; i < sctx->rsizw; i++)
|
for (i = 0; i < digest_size / 8; i++)
|
||||||
sctx->st[i] = cpu_to_le64(sctx->st[i]);
|
put_unaligned_le64(sctx->st[i], digest++);
|
||||||
|
|
||||||
memcpy(out, sctx->st, sctx->md_len);
|
if (digest_size & 4)
|
||||||
|
put_unaligned_le32(sctx->st[i], (__le32 *)digest);
|
||||||
|
|
||||||
memset(sctx, 0, sizeof(*sctx));
|
memset(sctx, 0, sizeof(*sctx));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct shash_alg sha3_224 = {
|
static struct shash_alg algs[] = { {
|
||||||
.digestsize = SHA3_224_DIGEST_SIZE,
|
.digestsize = SHA3_224_DIGEST_SIZE,
|
||||||
.init = sha3_224_init,
|
.init = sha3_init,
|
||||||
.update = sha3_update,
|
.update = sha3_update,
|
||||||
.final = sha3_final,
|
.final = sha3_final,
|
||||||
.descsize = sizeof(struct sha3_state),
|
.descsize = sizeof(struct sha3_state),
|
||||||
.base = {
|
.base.cra_name = "sha3-224",
|
||||||
.cra_name = "sha3-224",
|
.base.cra_driver_name = "sha3-224-generic",
|
||||||
.cra_driver_name = "sha3-224-generic",
|
.base.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
||||||
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
.base.cra_blocksize = SHA3_224_BLOCK_SIZE,
|
||||||
.cra_blocksize = SHA3_224_BLOCK_SIZE,
|
.base.cra_module = THIS_MODULE,
|
||||||
.cra_module = THIS_MODULE,
|
}, {
|
||||||
}
|
.digestsize = SHA3_256_DIGEST_SIZE,
|
||||||
};
|
.init = sha3_init,
|
||||||
|
.update = sha3_update,
|
||||||
static struct shash_alg sha3_256 = {
|
.final = sha3_final,
|
||||||
.digestsize = SHA3_256_DIGEST_SIZE,
|
.descsize = sizeof(struct sha3_state),
|
||||||
.init = sha3_256_init,
|
.base.cra_name = "sha3-256",
|
||||||
.update = sha3_update,
|
.base.cra_driver_name = "sha3-256-generic",
|
||||||
.final = sha3_final,
|
.base.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
||||||
.descsize = sizeof(struct sha3_state),
|
.base.cra_blocksize = SHA3_256_BLOCK_SIZE,
|
||||||
.base = {
|
.base.cra_module = THIS_MODULE,
|
||||||
.cra_name = "sha3-256",
|
}, {
|
||||||
.cra_driver_name = "sha3-256-generic",
|
.digestsize = SHA3_384_DIGEST_SIZE,
|
||||||
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
.init = sha3_init,
|
||||||
.cra_blocksize = SHA3_256_BLOCK_SIZE,
|
.update = sha3_update,
|
||||||
.cra_module = THIS_MODULE,
|
.final = sha3_final,
|
||||||
}
|
.descsize = sizeof(struct sha3_state),
|
||||||
};
|
.base.cra_name = "sha3-384",
|
||||||
|
.base.cra_driver_name = "sha3-384-generic",
|
||||||
static struct shash_alg sha3_384 = {
|
.base.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
||||||
.digestsize = SHA3_384_DIGEST_SIZE,
|
.base.cra_blocksize = SHA3_384_BLOCK_SIZE,
|
||||||
.init = sha3_384_init,
|
.base.cra_module = THIS_MODULE,
|
||||||
.update = sha3_update,
|
}, {
|
||||||
.final = sha3_final,
|
.digestsize = SHA3_512_DIGEST_SIZE,
|
||||||
.descsize = sizeof(struct sha3_state),
|
.init = sha3_init,
|
||||||
.base = {
|
.update = sha3_update,
|
||||||
.cra_name = "sha3-384",
|
.final = sha3_final,
|
||||||
.cra_driver_name = "sha3-384-generic",
|
.descsize = sizeof(struct sha3_state),
|
||||||
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
.base.cra_name = "sha3-512",
|
||||||
.cra_blocksize = SHA3_384_BLOCK_SIZE,
|
.base.cra_driver_name = "sha3-512-generic",
|
||||||
.cra_module = THIS_MODULE,
|
.base.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
||||||
}
|
.base.cra_blocksize = SHA3_512_BLOCK_SIZE,
|
||||||
};
|
.base.cra_module = THIS_MODULE,
|
||||||
|
} };
|
||||||
static struct shash_alg sha3_512 = {
|
|
||||||
.digestsize = SHA3_512_DIGEST_SIZE,
|
|
||||||
.init = sha3_512_init,
|
|
||||||
.update = sha3_update,
|
|
||||||
.final = sha3_final,
|
|
||||||
.descsize = sizeof(struct sha3_state),
|
|
||||||
.base = {
|
|
||||||
.cra_name = "sha3-512",
|
|
||||||
.cra_driver_name = "sha3-512-generic",
|
|
||||||
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
|
||||||
.cra_blocksize = SHA3_512_BLOCK_SIZE,
|
|
||||||
.cra_module = THIS_MODULE,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static int __init sha3_generic_mod_init(void)
|
static int __init sha3_generic_mod_init(void)
|
||||||
{
|
{
|
||||||
int ret;
|
return crypto_register_shashes(algs, ARRAY_SIZE(algs));
|
||||||
|
|
||||||
ret = crypto_register_shash(&sha3_224);
|
|
||||||
if (ret < 0)
|
|
||||||
goto err_out;
|
|
||||||
ret = crypto_register_shash(&sha3_256);
|
|
||||||
if (ret < 0)
|
|
||||||
goto err_out_224;
|
|
||||||
ret = crypto_register_shash(&sha3_384);
|
|
||||||
if (ret < 0)
|
|
||||||
goto err_out_256;
|
|
||||||
ret = crypto_register_shash(&sha3_512);
|
|
||||||
if (ret < 0)
|
|
||||||
goto err_out_384;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
err_out_384:
|
|
||||||
crypto_unregister_shash(&sha3_384);
|
|
||||||
err_out_256:
|
|
||||||
crypto_unregister_shash(&sha3_256);
|
|
||||||
err_out_224:
|
|
||||||
crypto_unregister_shash(&sha3_224);
|
|
||||||
err_out:
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __exit sha3_generic_mod_fini(void)
|
static void __exit sha3_generic_mod_fini(void)
|
||||||
{
|
{
|
||||||
crypto_unregister_shash(&sha3_224);
|
crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
|
||||||
crypto_unregister_shash(&sha3_256);
|
|
||||||
crypto_unregister_shash(&sha3_384);
|
|
||||||
crypto_unregister_shash(&sha3_512);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(sha3_generic_mod_init);
|
module_init(sha3_generic_mod_init);
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
struct sha3_state {
|
struct sha3_state {
|
||||||
u64 st[25];
|
u64 st[25];
|
||||||
unsigned int md_len;
|
|
||||||
unsigned int rsiz;
|
unsigned int rsiz;
|
||||||
unsigned int rsizw;
|
unsigned int rsizw;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue