hash.h
1 /*********************************************************************** 2 * Copyright (c) 2014 Pieter Wuille * 3 * Distributed under the MIT software license, see the accompanying * 4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.* 5 ***********************************************************************/ 6 7 #ifndef SECP256K1_HASH_H 8 #define SECP256K1_HASH_H 9 10 #include <stdlib.h> 11 #include <stdint.h> 12 13 typedef struct { 14 secp256k1_sha256_compression_function fn_sha256_compression; 15 } secp256k1_hash_ctx; 16 17 static void secp256k1_hash_ctx_init(secp256k1_hash_ctx *hash_ctx); 18 19 typedef struct { 20 uint32_t s[8]; 21 unsigned char buf[64]; 22 uint64_t bytes; 23 } secp256k1_sha256; 24 25 static void secp256k1_sha256_initialize(secp256k1_sha256 *hash); 26 /* Initialize a SHA256 hash state with a precomputed midstate. 27 * The byte counter must be a multiple of 64, i.e., there must be no unwritten 28 * bytes in the buffer. */ 29 static void secp256k1_sha256_initialize_midstate(secp256k1_sha256 *hash, uint64_t bytes, const uint32_t state[8]); 30 static void secp256k1_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *data, size_t size); 31 static void secp256k1_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, unsigned char *out32); 32 static void secp256k1_sha256_clear(secp256k1_sha256 *hash); 33 34 typedef struct { 35 secp256k1_sha256 inner, outer; 36 } secp256k1_hmac_sha256; 37 38 static void secp256k1_hmac_sha256_initialize(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t size); 39 static void secp256k1_hmac_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size); 40 static void secp256k1_hmac_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, unsigned char *out32); 41 static void secp256k1_hmac_sha256_clear(secp256k1_hmac_sha256 *hash); 42 43 typedef struct { 44 unsigned char v[32]; 45 unsigned char k[32]; 46 int retry; 47 } secp256k1_rfc6979_hmac_sha256; 48 49 static void secp256k1_rfc6979_hmac_sha256_initialize(const secp256k1_hash_ctx *hash_ctx, secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen); 50 static void secp256k1_rfc6979_hmac_sha256_generate(const secp256k1_hash_ctx *hash_ctx, secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen); 51 static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng); 52 static void secp256k1_rfc6979_hmac_sha256_clear(secp256k1_rfc6979_hmac_sha256 *rng); 53 54 #endif /* SECP256K1_HASH_H */