2021-09-06 14:23:14 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2019-09-25 13:37:26 +00:00
|
|
|
#include "crypto/hash.h"
|
2019-06-10 12:56:11 +00:00
|
|
|
#include "crypto/crc32c.h"
|
2019-09-25 13:37:26 +00:00
|
|
|
#include "crypto/xxhash.h"
|
2019-10-07 11:13:26 +00:00
|
|
|
#include "crypto/sha.h"
|
2019-10-07 16:19:51 +00:00
|
|
|
#include "crypto/blake2.h"
|
2019-09-25 13:37:26 +00:00
|
|
|
|
2023-02-16 02:30:46 +00:00
|
|
|
void hash_init_accel(void)
|
|
|
|
{
|
|
|
|
crc32c_optimization_init();
|
2023-02-16 02:22:58 +00:00
|
|
|
blake2_init_accel();
|
2023-02-16 02:22:58 +00:00
|
|
|
sha256_init_accel();
|
2023-02-16 02:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hash_init_blake2(void)
|
|
|
|
{
|
|
|
|
blake2_init_accel();
|
2023-02-16 02:30:46 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 02:22:58 +00:00
|
|
|
void hash_init_sha256(void)
|
|
|
|
{
|
|
|
|
sha256_init_accel();
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:02:56 +00:00
|
|
|
/*
|
|
|
|
* Default builtin implementations
|
|
|
|
*/
|
2019-06-10 12:56:11 +00:00
|
|
|
int hash_crc32c(const u8* buf, size_t length, u8 *out)
|
|
|
|
{
|
|
|
|
u32 crc = ~0;
|
|
|
|
|
|
|
|
crc = crc32c(~0, buf, length);
|
|
|
|
put_unaligned_le32(~crc, out);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:37:26 +00:00
|
|
|
int hash_xxhash(const u8 *buf, size_t length, u8 *out)
|
|
|
|
{
|
|
|
|
XXH64_hash_t hash;
|
|
|
|
|
|
|
|
hash = XXH64(buf, length, 0);
|
2020-03-16 09:05:12 +00:00
|
|
|
put_unaligned_le64(hash, out);
|
2019-09-25 13:37:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-07 11:13:26 +00:00
|
|
|
|
2020-04-06 19:02:56 +00:00
|
|
|
/*
|
|
|
|
* Implementations of cryptographic primitives
|
|
|
|
*/
|
|
|
|
#if CRYPTOPROVIDER_BUILTIN == 1
|
|
|
|
|
2019-10-07 11:13:26 +00:00
|
|
|
int hash_sha256(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
SHA256Context context;
|
|
|
|
|
|
|
|
SHA256Reset(&context);
|
|
|
|
SHA256Input(&context, buf, len);
|
|
|
|
SHA256Result(&context, out);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-07 16:19:51 +00:00
|
|
|
|
|
|
|
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
blake2b_state S;
|
|
|
|
|
|
|
|
blake2b_init(&S, CRYPTO_HASH_SIZE_MAX);
|
|
|
|
blake2b_update(&S, buf, len);
|
|
|
|
blake2b_final(&S, out, CRYPTO_HASH_SIZE_MAX);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-06 19:02:56 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CRYPTOPROVIDER_LIBGCRYPT == 1
|
|
|
|
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
|
|
|
int hash_sha256(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
gcry_md_hash_buffer(GCRY_MD_SHA256, out, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
gcry_md_hash_buffer(GCRY_MD_BLAKE2B_256, out, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CRYPTOPROVIDER_LIBSODIUM == 1
|
|
|
|
|
|
|
|
#include <sodium/crypto_hash_sha256.h>
|
|
|
|
#include <sodium/crypto_generichash_blake2b.h>
|
|
|
|
|
|
|
|
int hash_sha256(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
return crypto_hash_sha256(out, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
return crypto_generichash_blake2b(out, CRYPTO_HASH_SIZE_MAX, buf, len,
|
|
|
|
NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2020-05-28 20:39:43 +00:00
|
|
|
|
|
|
|
#if CRYPTOPROVIDER_LIBKCAPI == 1
|
|
|
|
|
|
|
|
#include <kcapi.h>
|
|
|
|
|
|
|
|
int hash_sha256(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
static struct kcapi_handle *handle = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!handle) {
|
|
|
|
ret = kcapi_md_init(&handle, "sha256", 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"HASH: cannot instantiate sha256, error %d\n",
|
|
|
|
ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = kcapi_md_digest(handle, buf, len, out, CRYPTO_HASH_SIZE_MAX);
|
|
|
|
/* kcapi_md_destroy(handle); */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
static struct kcapi_handle *handle = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!handle) {
|
|
|
|
ret = kcapi_md_init(&handle, "blake2b-256", 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"HASH: cannot instantiate blake2b-256, error %d\n",
|
|
|
|
ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = kcapi_md_digest(handle, buf, len, out, CRYPTO_HASH_SIZE_MAX);
|
|
|
|
/* kcapi_md_destroy(handle); */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|