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 #pragma once
33 
49 #include <inttypes.h>
50 #include <stddef.h>
51 
52 #include "hashes/sha2xx_common.h"
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
61 #define SHA256_DIGEST_LENGTH 32
62 
66 #define SHA256_INTERNAL_BLOCK_SIZE (64)
67 
72 
76 typedef struct {
82 
86 typedef struct {
88  size_t index;
90  unsigned char element[SHA256_DIGEST_LENGTH];
92 
99 
107 static inline void sha256_update(sha256_context_t *ctx, const void *data, size_t len)
108 {
109  sha2xx_update(ctx, data, len);
110 }
111 
119 static inline void sha256_final(sha256_context_t *ctx, void *digest)
120 {
121  sha2xx_final(ctx, digest, SHA256_DIGEST_LENGTH);
122 }
123 
133 void sha256(const void *data, size_t len, void *digest);
134 
141 void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length);
142 
149 void hmac_sha256_update(hmac_context_t *ctx, const void *data, size_t len);
150 
156 void hmac_sha256_final(hmac_context_t *ctx, void *digest);
157 
168 void hmac_sha256(const void *key, size_t key_length,
169  const void *data, size_t len, void *digest);
170 
185 void *sha256_chain(const void *seed, size_t seed_length,
186  size_t elements, void *tail_element);
187 
216 void *sha256_chain_with_waypoints(const void *seed, size_t seed_length,
217  size_t elements, void *tail_element,
218  sha256_chain_idx_elm_t *waypoints,
219  size_t *waypoints_length);
220 
232 int sha256_chain_verify_element(void *element,
233  size_t element_index,
234  void *tail_element,
235  size_t chain_length);
236 
237 #ifdef __cplusplus
238 }
239 #endif
240 
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:107
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:119
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:61
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:71
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:76
sha256_context_t c_out
Context for outer hash calculation.
Definition: sha256.h:80
sha256_context_t c_in
Context for inner hash calculation.
Definition: sha256.h:78
sha256-chain indexed element
Definition: sha256.h:86
size_t index
the position of this element in its chain
Definition: sha256.h:88
Structure to hold the SHA-2XX context.
Definition: sha2xx_common.h:61