sha256.h
Go to the documentation of this file.
1 /*-
2  * Copyright 2005 Colin Percival
3  * Copyright 2013 Christian Mehlis & RenĂ© Kijewski
4  * Copyright 2016 Martin Landsmann <martin.landsmann@haw-hamburg.de>
5  * Copyright 2016 OTA keys S.A.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/lib/libmd/sha256.h,v 1.1.2.1 2005/06/24 13:32:25 cperciva Exp $
30  */
31 
32 
48 #ifndef HASHES_SHA256_H
49 #define HASHES_SHA256_H
50 
51 #include <inttypes.h>
52 #include <stddef.h>
53 
54 #include "hashes/sha2xx_common.h"
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
63 #define SHA256_DIGEST_LENGTH 32
64 
68 #define SHA256_INTERNAL_BLOCK_SIZE (64)
69 
74 
78 typedef struct {
84 
88 typedef struct {
90  size_t index;
92  unsigned char element[SHA256_DIGEST_LENGTH];
94 
100 void sha256_init(sha256_context_t *ctx);
101 
109 static inline void sha256_update(sha256_context_t *ctx, const void *data, size_t len)
110 {
111  sha2xx_update(ctx, data, len);
112 }
113 
121 static inline void sha256_final(sha256_context_t *ctx, void *digest)
122 {
123  sha2xx_final(ctx, digest, SHA256_DIGEST_LENGTH);
124 }
125 
136 void *sha256(const void *data, size_t len, void *digest);
137 
144 void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length);
145 
152 void hmac_sha256_update(hmac_context_t *ctx, const void *data, size_t len);
153 
161 void hmac_sha256_final(hmac_context_t *ctx, void *digest);
162 
176 const void *hmac_sha256(const void *key, size_t key_length,
177  const void *data, size_t len, void *digest);
178 
193 void *sha256_chain(const void *seed, size_t seed_length,
194  size_t elements, void *tail_element);
195 
224 void *sha256_chain_with_waypoints(const void *seed, size_t seed_length,
225  size_t elements, void *tail_element,
226  sha256_chain_idx_elm_t *waypoints,
227  size_t *waypoints_length);
228 
240 int sha256_chain_verify_element(void *element,
241  size_t element_index,
242  void *tail_element,
243  size_t chain_length);
244 
245 #ifdef __cplusplus
246 }
247 #endif
248 
250 #endif /* HASHES_SHA256_H */
sha256_chain_verify_element
int sha256_chain_verify_element(void *element, size_t element_index, void *tail_element, size_t chain_length)
function to verify if a given chain element is part of the chain.
sha2xx_common.h
Common definitions for the SHA-224/256 hash functions.
sha256_init
void sha256_init(sha256_context_t *ctx)
SHA-256 initialization.
sha256
void * sha256(const void *data, size_t len, void *digest)
A wrapper function to simplify the generation of a hash, this is useful for generating sha256 for one...
sha2xx_update
void sha2xx_update(sha2xx_context_t *ctx, const void *data, size_t len)
Add bytes into the hash.
sha2xx_final
void sha2xx_final(sha2xx_context_t *ctx, void *digest, size_t dig_len)
SHA-2XX finalization.
sha256_chain_idx_elm_t
sha256-chain indexed element
Definition: sha256.h:88
hmac_sha256_update
void hmac_sha256_update(hmac_context_t *ctx, const void *data, size_t len)
hmac_sha256_update Add data bytes for HMAC calculation
sha256_chain_with_waypoints
void * sha256_chain_with_waypoints(const void *seed, size_t seed_length, size_t elements, void *tail_element, sha256_chain_idx_elm_t *waypoints, size_t *waypoints_length)
function to produce a hash chain starting with a given seed element.
hmac_context_t::c_in
sha256_context_t c_in
Context for inner hash calculation.
Definition: sha256.h:80
sha256_update
static void sha256_update(sha256_context_t *ctx, const void *data, size_t len)
Add bytes into the hash.
Definition: sha256.h:109
hmac_sha256
const void * hmac_sha256(const void *key, size_t key_length, const void *data, size_t len, void *digest)
function to compute a hmac-sha256 from a given message
hmac_context_t
Context for HMAC operations based on sha256.
Definition: sha256.h:78
hmac_sha256_final
void hmac_sha256_final(hmac_context_t *ctx, void *digest)
hmac_sha256_final HMAC SHA-256 finalization.
sha256_final
static void sha256_final(sha256_context_t *ctx, void *digest)
SHA-256 finalization.
Definition: sha256.h:121
hmac_sha256_init
void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length)
hmac_sha256_init HMAC SHA-256 calculation.
sha256_chain_idx_elm_t::index
size_t index
the position of this element in its chain
Definition: sha256.h:90
SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH
Length of SHA256 digests in bytes.
Definition: sha256.h:63
hmac_context_t::c_out
sha256_context_t c_out
Context for outer hash calculation.
Definition: sha256.h:82
sha256_context_t
sha2xx_context_t sha256_context_t
Context for cipher operations based on sha256.
Definition: sha256.h:73
inttypes.h
Adds include for missing inttype definitions.
sha256_chain
void * sha256_chain(const void *seed, size_t seed_length, size_t elements, void *tail_element)
function to produce a hash chain starting with a given seed element.
sha2xx_context_t
Structure to hold the SHA-2XX context.
Definition: sha2xx_common.h:63