6048fdcc5f
In preparation for using blake2s in the RNG, we change the way that it is wired-in to the build system. Instead of using ifdefs to select the right symbol, we use weak symbols. And because ARM doesn't need the generic implementation, we make the generic one default only if an arch library doesn't need it already, and then have arch libraries that do need it opt-in. So that the arch libraries can remain tristate rather than bool, we then split the shash part from the glue code. Acked-by: Herbert Xu <herbert@gondor.apana.org.au> Acked-by: Ard Biesheuvel <ardb@kernel.org> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Masahiro Yamada <masahiroy@kernel.org> Cc: linux-kbuild@vger.kernel.org Cc: linux-crypto@vger.kernel.org Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
87 lines
2.3 KiB
C
87 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0 OR MIT
|
|
/*
|
|
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
*
|
|
* This is an implementation of the BLAKE2s hash and PRF functions.
|
|
*
|
|
* Information: https://blake2.net/
|
|
*
|
|
*/
|
|
|
|
#include <crypto/internal/blake2s.h>
|
|
#include <linux/types.h>
|
|
#include <linux/string.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/bug.h>
|
|
|
|
void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen)
|
|
{
|
|
__blake2s_update(state, in, inlen, blake2s_compress);
|
|
}
|
|
EXPORT_SYMBOL(blake2s_update);
|
|
|
|
void blake2s_final(struct blake2s_state *state, u8 *out)
|
|
{
|
|
WARN_ON(IS_ENABLED(DEBUG) && !out);
|
|
__blake2s_final(state, out, blake2s_compress);
|
|
memzero_explicit(state, sizeof(*state));
|
|
}
|
|
EXPORT_SYMBOL(blake2s_final);
|
|
|
|
void blake2s256_hmac(u8 *out, const u8 *in, const u8 *key, const size_t inlen,
|
|
const size_t keylen)
|
|
{
|
|
struct blake2s_state state;
|
|
u8 x_key[BLAKE2S_BLOCK_SIZE] __aligned(__alignof__(u32)) = { 0 };
|
|
u8 i_hash[BLAKE2S_HASH_SIZE] __aligned(__alignof__(u32));
|
|
int i;
|
|
|
|
if (keylen > BLAKE2S_BLOCK_SIZE) {
|
|
blake2s_init(&state, BLAKE2S_HASH_SIZE);
|
|
blake2s_update(&state, key, keylen);
|
|
blake2s_final(&state, x_key);
|
|
} else
|
|
memcpy(x_key, key, keylen);
|
|
|
|
for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
|
|
x_key[i] ^= 0x36;
|
|
|
|
blake2s_init(&state, BLAKE2S_HASH_SIZE);
|
|
blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
|
|
blake2s_update(&state, in, inlen);
|
|
blake2s_final(&state, i_hash);
|
|
|
|
for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
|
|
x_key[i] ^= 0x5c ^ 0x36;
|
|
|
|
blake2s_init(&state, BLAKE2S_HASH_SIZE);
|
|
blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
|
|
blake2s_update(&state, i_hash, BLAKE2S_HASH_SIZE);
|
|
blake2s_final(&state, i_hash);
|
|
|
|
memcpy(out, i_hash, BLAKE2S_HASH_SIZE);
|
|
memzero_explicit(x_key, BLAKE2S_BLOCK_SIZE);
|
|
memzero_explicit(i_hash, BLAKE2S_HASH_SIZE);
|
|
}
|
|
EXPORT_SYMBOL(blake2s256_hmac);
|
|
|
|
static int __init blake2s_mod_init(void)
|
|
{
|
|
if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) &&
|
|
WARN_ON(!blake2s_selftest()))
|
|
return -ENODEV;
|
|
return 0;
|
|
}
|
|
|
|
static void __exit blake2s_mod_exit(void)
|
|
{
|
|
}
|
|
|
|
module_init(blake2s_mod_init);
|
|
module_exit(blake2s_mod_exit);
|
|
MODULE_LICENSE("GPL v2");
|
|
MODULE_DESCRIPTION("BLAKE2s hash function");
|
|
MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
|