1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* 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.
*/
#include "kerncompat.h"
#include "crypto/hash.h"
#include "crypto/crc32c.h"
#include "crypto/xxhash.h"
#include "crypto/sha.h"
#include "crypto/blake2.h"
void hash_init_crc32c(void)
{
crc32c_init_accel();
}
/*
* Default builtin implementations
*/
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;
}
int hash_xxhash(const u8 *buf, size_t length, u8 *out)
{
XXH64_hash_t hash;
hash = XXH64(buf, length, 0);
put_unaligned_le64(hash, out);
return 0;
}
/*
* Implementations of cryptographic primitives
*/
#if CRYPTOPROVIDER_BUILTIN == 1
void hash_init_accel(void)
{
crc32c_init_accel();
blake2_init_accel();
sha256_init_accel();
}
int hash_sha256(const u8 *buf, size_t len, u8 *out)
{
SHA256Context context;
SHA256Reset(&context);
SHA256Input(&context, buf, len);
SHA256Result(&context, out);
return 0;
}
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;
}
#endif
#if CRYPTOPROVIDER_LIBGCRYPT == 1
#include <gcrypt.h>
void hash_init_accel(void)
{
crc32c_init_accel();
}
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>
void hash_init_accel(void)
{
crc32c_init_accel();
}
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
#if CRYPTOPROVIDER_LIBKCAPI == 1
#include <kcapi.h>
void hash_init_accel(void)
{
crc32c_init_accel();
}
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
#if CRYPTOPROVIDER_BOTAN == 1
#include <botan/ffi.h>
void hash_init_accel(void)
{
crc32c_init_accel();
}
int hash_sha256(const u8 *buf, size_t len, u8 *out)
{
botan_hash_t hash;
static bool initialized = false;
int ret;
if (!initialized) {
ret = botan_hash_init(&hash, "SHA-256", 0);
if (ret < 0) {
fprintf(stderr, "HASH: cannot instantiate sha256, error %d\n", ret);
exit(1);
}
initialized = true;
} else {
botan_hash_clear(hash);
}
botan_hash_update(hash, buf, len);
botan_hash_final(hash, out);
/* botan_hash_destroy(hash); */
return 0;
}
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
{
botan_hash_t hash;
static bool initialized = false;
int ret;
if (!initialized) {
ret = botan_hash_init(&hash, "BLAKE2b(256)", 0);
if (ret < 0) {
fprintf(stderr, "HASH: cannot instantiate sha256, error %d\n", ret);
exit(1);
}
initialized = true;
} else {
botan_hash_clear(hash);
}
botan_hash_update(hash, buf, len);
botan_hash_final(hash, out);
/* botan_hash_destroy(hash); */
return 0;
}
#endif
#if CRYPTOPROVIDER_OPENSSL == 1
#include <openssl/params.h>
#include <openssl/evp.h>
void hash_init_accel(void)
{
crc32c_init_accel();
}
int hash_sha256(const u8 *buf, size_t len, u8 *out)
{
EVP_MD_CTX *ctx = NULL;
if (!ctx) {
ctx = EVP_MD_CTX_new();
if (!ctx) {
fprintf(stderr, "HASH: cannot instantiate sha256\n");
exit(1);
}
}
EVP_DigestInit(ctx, EVP_sha256());
EVP_DigestUpdate(ctx, buf, len);
EVP_DigestFinal(ctx, out, NULL);
/* EVP_MD_CTX_free(ctx); */
return 0;
}
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
{
EVP_MD_CTX *ctx = NULL;
size_t digest_size = 256 / 8;
const OSSL_PARAM params[] = {
OSSL_PARAM_size_t("size", &digest_size),
OSSL_PARAM_END
};
if (!ctx) {
ctx = EVP_MD_CTX_new();
if (!ctx) {
fprintf(stderr, "HASH: cannot instantiate sha256\n");
exit(1);
}
}
EVP_DigestInit_ex2(ctx, EVP_blake2b512(), params);
EVP_DigestUpdate(ctx, buf, len);
EVP_DigestFinal(ctx, out, NULL);
/* EVP_MD_CTX_free(ctx); */
return 0;
}
#endif
|