sha256.h
Go to the documentation of this file.
1 /*-
2  * SPDX-FileCopyrightText: 2005 Colin Percival
3  * SPDX-FileCopyrightText: 2013 Christian Mehlis & RenĂ© Kijewski
4  * SPDX-FileCopyrightText: 2016 Martin Landsmann <martin.landsmann@haw-hamburg.de>
5  * SPDX-FileCopyrightText: 2016 OTA keys S.A.
6  * SPDX-License-Identifier: BSD-2-Clause
7  */
8 
9 #pragma once
10 
26 #include <inttypes.h>
27 #include <stddef.h>
28 
29 #include "hashes/sha2xx_common.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
38 #define SHA256_DIGEST_LENGTH 32
39 
43 #define SHA256_INTERNAL_BLOCK_SIZE (64)
44 
49 
53 typedef struct {
59 
63 typedef struct {
65  size_t index;
67  unsigned char element[SHA256_DIGEST_LENGTH];
69 
76 
84 static inline void sha256_update(sha256_context_t *ctx, const void *data, size_t len)
85 {
86  sha2xx_update(ctx, data, len);
87 }
88 
96 static inline void sha256_final(sha256_context_t *ctx, void *digest)
97 {
98  sha2xx_final(ctx, digest, SHA256_DIGEST_LENGTH);
99 }
100 
110 void sha256(const void *data, size_t len, void *digest);
111 
118 void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length);
119 
126 void hmac_sha256_update(hmac_context_t *ctx, const void *data, size_t len);
127 
133 void hmac_sha256_final(hmac_context_t *ctx, void *digest);
134 
145 void hmac_sha256(const void *key, size_t key_length,
146  const void *data, size_t len, void *digest);
147 
162 void *sha256_chain(const void *seed, size_t seed_length,
163  size_t elements, void *tail_element);
164 
193 void *sha256_chain_with_waypoints(const void *seed, size_t seed_length,
194  size_t elements, void *tail_element,
195  sha256_chain_idx_elm_t *waypoints,
196  size_t *waypoints_length);
197 
209 int sha256_chain_verify_element(void *element,
210  size_t element_index,
211  void *tail_element,
212  size_t chain_length);
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
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.
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
static void sha256_update(sha256_context_t *ctx, const void *data, size_t len)
Add bytes into the hash.
Definition: sha256.h:84
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.
static void sha256_final(sha256_context_t *ctx, void *digest)
SHA-256 finalization.
Definition: sha256.h:96
void hmac_sha256_final(hmac_context_t *ctx, void *digest)
hmac_sha256_final HMAC SHA-256 finalization.
void hmac_sha256_update(hmac_context_t *ctx, const void *data, size_t len)
hmac_sha256_update Add data bytes for HMAC calculation
void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length)
hmac_sha256_init HMAC SHA-256 calculation.
void sha256_init(sha256_context_t *ctx)
SHA-256 initialization.
#define SHA256_DIGEST_LENGTH
Length of SHA256 digests in bytes.
Definition: sha256.h:38
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...
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 sha256_context_t
Context for cipher operations based on sha256.
Definition: sha256.h:48
void sha2xx_final(sha2xx_context_t *ctx, void *digest, size_t dig_len)
SHA-2XX finalization.
void sha2xx_update(sha2xx_context_t *ctx, const void *data, size_t len)
Add bytes into the hash.
Adds include for missing inttype definitions.
Common definitions for the SHA-224/256 hash functions.
Context for HMAC operations based on sha256.
Definition: sha256.h:53
sha256_context_t c_out
Context for outer hash calculation.
Definition: sha256.h:57
sha256_context_t c_in
Context for inner hash calculation.
Definition: sha256.h:55
sha256-chain indexed element
Definition: sha256.h:63
size_t index
the position of this element in its chain
Definition: sha256.h:65
Structure to hold the SHA-2XX context.
Definition: sha2xx_common.h:38