mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support
Since HMAC support is commonly needed and is fairly simple, include it as a first-class citizen of the SHA-256 library. The API supports both incremental and one-shot computation, and either preparing the key ahead of time or just using a raw key. The implementation is much more streamlined than crypto/hmac.c. I've kept it consistent with the HMAC-SHA384 and HMAC-SHA512 code as much as possible. Testing of these functions will be via sha224_kunit and sha256_kunit, added by a later commit. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250630160645.3198-9-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* SHA-256, as specified in
|
||||
* http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
|
||||
*
|
||||
* SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
|
||||
* SHA-224, SHA-256, HMAC-SHA224, and HMAC-SHA256 library functions
|
||||
*
|
||||
* Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
|
||||
* Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
|
||||
@@ -11,12 +8,14 @@
|
||||
* Copyright (c) 2014 Red Hat Inc.
|
||||
*/
|
||||
|
||||
#include <crypto/hmac.h>
|
||||
#include <crypto/internal/blockhash.h>
|
||||
#include <crypto/internal/sha2.h>
|
||||
#include <linux/export.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/string.h>
|
||||
#include <linux/wordpart.h>
|
||||
|
||||
static const struct sha256_block_state sha224_iv = {
|
||||
.h = {
|
||||
@@ -136,5 +135,143 @@ void sha256(const u8 *data, size_t len, u8 out[SHA256_DIGEST_SIZE])
|
||||
}
|
||||
EXPORT_SYMBOL(sha256);
|
||||
|
||||
MODULE_DESCRIPTION("SHA-256 Algorithm");
|
||||
/* pre-boot environment (as indicated by __DISABLE_EXPORTS) doesn't need HMAC */
|
||||
#ifndef __DISABLE_EXPORTS
|
||||
static void __hmac_sha256_preparekey(struct __hmac_sha256_key *key,
|
||||
const u8 *raw_key, size_t raw_key_len,
|
||||
const struct sha256_block_state *iv)
|
||||
{
|
||||
union {
|
||||
u8 b[SHA256_BLOCK_SIZE];
|
||||
unsigned long w[SHA256_BLOCK_SIZE / sizeof(unsigned long)];
|
||||
} derived_key = { 0 };
|
||||
|
||||
if (unlikely(raw_key_len > SHA256_BLOCK_SIZE)) {
|
||||
if (iv == &sha224_iv)
|
||||
sha224(raw_key, raw_key_len, derived_key.b);
|
||||
else
|
||||
sha256(raw_key, raw_key_len, derived_key.b);
|
||||
} else {
|
||||
memcpy(derived_key.b, raw_key, raw_key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
|
||||
derived_key.w[i] ^= REPEAT_BYTE(HMAC_IPAD_VALUE);
|
||||
key->istate = *iv;
|
||||
sha256_blocks(&key->istate, derived_key.b, 1);
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
|
||||
derived_key.w[i] ^= REPEAT_BYTE(HMAC_OPAD_VALUE ^
|
||||
HMAC_IPAD_VALUE);
|
||||
key->ostate = *iv;
|
||||
sha256_blocks(&key->ostate, derived_key.b, 1);
|
||||
|
||||
memzero_explicit(&derived_key, sizeof(derived_key));
|
||||
}
|
||||
|
||||
void hmac_sha224_preparekey(struct hmac_sha224_key *key,
|
||||
const u8 *raw_key, size_t raw_key_len)
|
||||
{
|
||||
__hmac_sha256_preparekey(&key->key, raw_key, raw_key_len, &sha224_iv);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha224_preparekey);
|
||||
|
||||
void hmac_sha256_preparekey(struct hmac_sha256_key *key,
|
||||
const u8 *raw_key, size_t raw_key_len)
|
||||
{
|
||||
__hmac_sha256_preparekey(&key->key, raw_key, raw_key_len, &sha256_iv);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha256_preparekey);
|
||||
|
||||
void __hmac_sha256_init(struct __hmac_sha256_ctx *ctx,
|
||||
const struct __hmac_sha256_key *key)
|
||||
{
|
||||
__sha256_init(&ctx->sha_ctx, &key->istate, SHA256_BLOCK_SIZE);
|
||||
ctx->ostate = key->ostate;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(__hmac_sha256_init);
|
||||
|
||||
static void __hmac_sha256_final(struct __hmac_sha256_ctx *ctx,
|
||||
u8 *out, size_t digest_size)
|
||||
{
|
||||
/* Generate the padded input for the outer hash in ctx->sha_ctx.buf. */
|
||||
__sha256_final(&ctx->sha_ctx, ctx->sha_ctx.buf, digest_size);
|
||||
memset(&ctx->sha_ctx.buf[digest_size], 0,
|
||||
SHA256_BLOCK_SIZE - digest_size);
|
||||
ctx->sha_ctx.buf[digest_size] = 0x80;
|
||||
*(__be32 *)&ctx->sha_ctx.buf[SHA256_BLOCK_SIZE - 4] =
|
||||
cpu_to_be32(8 * (SHA256_BLOCK_SIZE + digest_size));
|
||||
|
||||
/* Compute the outer hash, which gives the HMAC value. */
|
||||
sha256_blocks(&ctx->ostate, ctx->sha_ctx.buf, 1);
|
||||
for (size_t i = 0; i < digest_size; i += 4)
|
||||
put_unaligned_be32(ctx->ostate.h[i / 4], out + i);
|
||||
|
||||
memzero_explicit(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
void hmac_sha224_final(struct hmac_sha224_ctx *ctx,
|
||||
u8 out[SHA224_DIGEST_SIZE])
|
||||
{
|
||||
__hmac_sha256_final(&ctx->ctx, out, SHA224_DIGEST_SIZE);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha224_final);
|
||||
|
||||
void hmac_sha256_final(struct hmac_sha256_ctx *ctx,
|
||||
u8 out[SHA256_DIGEST_SIZE])
|
||||
{
|
||||
__hmac_sha256_final(&ctx->ctx, out, SHA256_DIGEST_SIZE);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha256_final);
|
||||
|
||||
void hmac_sha224(const struct hmac_sha224_key *key,
|
||||
const u8 *data, size_t data_len, u8 out[SHA224_DIGEST_SIZE])
|
||||
{
|
||||
struct hmac_sha224_ctx ctx;
|
||||
|
||||
hmac_sha224_init(&ctx, key);
|
||||
hmac_sha224_update(&ctx, data, data_len);
|
||||
hmac_sha224_final(&ctx, out);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha224);
|
||||
|
||||
void hmac_sha256(const struct hmac_sha256_key *key,
|
||||
const u8 *data, size_t data_len, u8 out[SHA256_DIGEST_SIZE])
|
||||
{
|
||||
struct hmac_sha256_ctx ctx;
|
||||
|
||||
hmac_sha256_init(&ctx, key);
|
||||
hmac_sha256_update(&ctx, data, data_len);
|
||||
hmac_sha256_final(&ctx, out);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha256);
|
||||
|
||||
void hmac_sha224_usingrawkey(const u8 *raw_key, size_t raw_key_len,
|
||||
const u8 *data, size_t data_len,
|
||||
u8 out[SHA224_DIGEST_SIZE])
|
||||
{
|
||||
struct hmac_sha224_key key;
|
||||
|
||||
hmac_sha224_preparekey(&key, raw_key, raw_key_len);
|
||||
hmac_sha224(&key, data, data_len, out);
|
||||
|
||||
memzero_explicit(&key, sizeof(key));
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha224_usingrawkey);
|
||||
|
||||
void hmac_sha256_usingrawkey(const u8 *raw_key, size_t raw_key_len,
|
||||
const u8 *data, size_t data_len,
|
||||
u8 out[SHA256_DIGEST_SIZE])
|
||||
{
|
||||
struct hmac_sha256_key key;
|
||||
|
||||
hmac_sha256_preparekey(&key, raw_key, raw_key_len);
|
||||
hmac_sha256(&key, data, data_len, out);
|
||||
|
||||
memzero_explicit(&key, sizeof(key));
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hmac_sha256_usingrawkey);
|
||||
#endif /* !__DISABLE_EXPORTS */
|
||||
|
||||
MODULE_DESCRIPTION("SHA-224, SHA-256, HMAC-SHA224, and HMAC-SHA256 library functions");
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
Reference in New Issue
Block a user