mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-18 22:14:16 +00:00
crypto: lib/sha256 - Use generic block helper
Use the BLOCK_HASH_UPDATE_BLOCKS helper instead of duplicating partial block handling. Also remove the unused lib/sha256 force-generic interface. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
parent
ee8a720e39
commit
3007e90572
2 changed files with 12 additions and 70 deletions
|
@ -10,13 +10,6 @@
|
|||
#include <linux/types.h>
|
||||
#include <linux/unaligned.h>
|
||||
|
||||
void sha256_update_generic(struct sha256_state *sctx,
|
||||
const u8 *data, size_t len);
|
||||
void sha256_final_generic(struct sha256_state *sctx,
|
||||
u8 out[SHA256_DIGEST_SIZE]);
|
||||
void sha224_final_generic(struct sha256_state *sctx,
|
||||
u8 out[SHA224_DIGEST_SIZE]);
|
||||
|
||||
#if IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_SHA256)
|
||||
bool sha256_is_arch_optimized(void);
|
||||
#else
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
* Copyright (c) 2014 Red Hat Inc.
|
||||
*/
|
||||
|
||||
#include <crypto/internal/blockhash.h>
|
||||
#include <crypto/internal/sha2.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
|
@ -31,71 +32,40 @@ static inline bool sha256_purgatory(void)
|
|||
}
|
||||
|
||||
static inline void sha256_blocks(u32 state[SHA256_STATE_WORDS], const u8 *data,
|
||||
size_t nblocks, bool force_generic)
|
||||
size_t nblocks)
|
||||
{
|
||||
sha256_choose_blocks(state, data, nblocks,
|
||||
force_generic || sha256_purgatory(), false);
|
||||
}
|
||||
|
||||
static inline void __sha256_update(struct sha256_state *sctx, const u8 *data,
|
||||
size_t len, bool force_generic)
|
||||
{
|
||||
size_t partial = sctx->count % SHA256_BLOCK_SIZE;
|
||||
|
||||
sctx->count += len;
|
||||
|
||||
if (partial + len >= SHA256_BLOCK_SIZE) {
|
||||
size_t nblocks;
|
||||
|
||||
if (partial) {
|
||||
size_t l = SHA256_BLOCK_SIZE - partial;
|
||||
|
||||
memcpy(&sctx->buf[partial], data, l);
|
||||
data += l;
|
||||
len -= l;
|
||||
|
||||
sha256_blocks(sctx->state, sctx->buf, 1, force_generic);
|
||||
}
|
||||
|
||||
nblocks = len / SHA256_BLOCK_SIZE;
|
||||
len %= SHA256_BLOCK_SIZE;
|
||||
|
||||
if (nblocks) {
|
||||
sha256_blocks(sctx->state, data, nblocks,
|
||||
force_generic);
|
||||
data += nblocks * SHA256_BLOCK_SIZE;
|
||||
}
|
||||
partial = 0;
|
||||
}
|
||||
if (len)
|
||||
memcpy(&sctx->buf[partial], data, len);
|
||||
sha256_choose_blocks(state, data, nblocks, sha256_purgatory(), false);
|
||||
}
|
||||
|
||||
void sha256_update(struct sha256_state *sctx, const u8 *data, size_t len)
|
||||
{
|
||||
__sha256_update(sctx, data, len, false);
|
||||
size_t partial = sctx->count % SHA256_BLOCK_SIZE;
|
||||
|
||||
sctx->count += len;
|
||||
BLOCK_HASH_UPDATE_BLOCKS(sha256_blocks, sctx->ctx.state, data, len,
|
||||
SHA256_BLOCK_SIZE, sctx->buf, partial);
|
||||
}
|
||||
EXPORT_SYMBOL(sha256_update);
|
||||
|
||||
static inline void __sha256_final(struct sha256_state *sctx, u8 *out,
|
||||
size_t digest_size, bool force_generic)
|
||||
size_t digest_size)
|
||||
{
|
||||
size_t partial = sctx->count % SHA256_BLOCK_SIZE;
|
||||
|
||||
sha256_finup(&sctx->ctx, sctx->buf, partial, out, digest_size,
|
||||
force_generic || sha256_purgatory(), false);
|
||||
sha256_purgatory(), false);
|
||||
memzero_explicit(sctx, sizeof(*sctx));
|
||||
}
|
||||
|
||||
void sha256_final(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE])
|
||||
{
|
||||
__sha256_final(sctx, out, SHA256_DIGEST_SIZE, false);
|
||||
__sha256_final(sctx, out, SHA256_DIGEST_SIZE);
|
||||
}
|
||||
EXPORT_SYMBOL(sha256_final);
|
||||
|
||||
void sha224_final(struct sha256_state *sctx, u8 out[SHA224_DIGEST_SIZE])
|
||||
{
|
||||
__sha256_final(sctx, out, SHA224_DIGEST_SIZE, false);
|
||||
__sha256_final(sctx, out, SHA224_DIGEST_SIZE);
|
||||
}
|
||||
EXPORT_SYMBOL(sha224_final);
|
||||
|
||||
|
@ -109,26 +79,5 @@ void sha256(const u8 *data, size_t len, u8 out[SHA256_DIGEST_SIZE])
|
|||
}
|
||||
EXPORT_SYMBOL(sha256);
|
||||
|
||||
#if IS_ENABLED(CONFIG_CRYPTO_SHA256) && !defined(__DISABLE_EXPORTS)
|
||||
void sha256_update_generic(struct sha256_state *sctx,
|
||||
const u8 *data, size_t len)
|
||||
{
|
||||
__sha256_update(sctx, data, len, true);
|
||||
}
|
||||
EXPORT_SYMBOL(sha256_update_generic);
|
||||
|
||||
void sha256_final_generic(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE])
|
||||
{
|
||||
__sha256_final(sctx, out, SHA256_DIGEST_SIZE, true);
|
||||
}
|
||||
EXPORT_SYMBOL(sha256_final_generic);
|
||||
|
||||
void sha224_final_generic(struct sha256_state *sctx, u8 out[SHA224_DIGEST_SIZE])
|
||||
{
|
||||
__sha256_final(sctx, out, SHA224_DIGEST_SIZE, true);
|
||||
}
|
||||
EXPORT_SYMBOL(sha224_final_generic);
|
||||
#endif
|
||||
|
||||
MODULE_DESCRIPTION("SHA-256 Algorithm");
|
||||
MODULE_LICENSE("GPL");
|
||||
|
|
Loading…
Add table
Reference in a new issue