Contiki-NG
Loading...
Searching...
No Matches
sha-256.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021, Uppsala universitet.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \addtogroup crypto
35 * @{
36 * \file
37 * Platform-independent SHA-256 API.
38 * \author
39 * Konrad Krentz <konrad.krentz@gmail.com>
40 */
41
42#ifndef SHA_256_H_
43#define SHA_256_H_
44
45#include "contiki.h"
46#include <stddef.h>
47#include <stdint.h>
48
49#define SHA_256_DIGEST_LENGTH 32
50#define SHA_256_BLOCK_SIZE 64
51
52#ifdef SHA_256_CONF
53#define SHA_256 SHA_256_CONF
54#else /* SHA_256_CONF */
55#define SHA_256 sha_256_driver
56#endif /* SHA_256_CONF */
57
58typedef struct {
59 uint64_t bit_count;
60 uint32_t state[SHA_256_DIGEST_LENGTH / sizeof(uint32_t)];
61 uint8_t buf[SHA_256_BLOCK_SIZE];
62 size_t buf_len;
63 uint8_t opad[SHA_256_BLOCK_SIZE]; /* HMAC's outer padding */
64} sha_256_checkpoint_t;
65
66/**
67 * Structure of SHA-256 drivers.
68 */
70
71 /**
72 * \brief Starts a hash session.
73 */
74 void (* init)(void);
75
76 /**
77 * \brief Processes a chunk of data.
78 * \param data pointer to the data to hash
79 * \param len length of the data to hash in bytes
80 */
81 void (* update)(const uint8_t *data, size_t len);
82
83 /**
84 * \brief Terminates the hash session and produces the digest.
85 * \param digest pointer to the hash value
86 */
87 void (* finalize)(uint8_t digest[static SHA_256_DIGEST_LENGTH]);
88
89 /**
90 * \brief Saves the hash session, e.g., before pausing a protothread.
91 */
92 void (* create_checkpoint)(sha_256_checkpoint_t *checkpoint);
93
94 /**
95 * \brief Restores a hash session, e.g., after resuming a protothread.
96 */
97 void (* restore_checkpoint)(const sha_256_checkpoint_t *checkpoint);
98
99 /**
100 * \brief Does init, update, and finalize at once.
101 * \param data pointer to the data to hash
102 * \param len length of the data to hash in bytes
103 * \param digest pointer to the hash value
104 */
105 void (* hash)(const uint8_t *data, size_t len,
106 uint8_t digest[static SHA_256_DIGEST_LENGTH]);
107};
108
109extern const struct sha_256_driver SHA_256;
110
111/**
112 * \brief Generic implementation of sha_256_driver#hash.
113 */
114void sha_256_hash(const uint8_t *data, size_t len,
115 uint8_t digest[static SHA_256_DIGEST_LENGTH]);
116
117/**
118 * \brief Initiates a stepwise HMAC-SHA-256 computation.
119 * \param key the key to authenticate with
120 * \param key_len length of key in bytes
121 */
122void sha_256_hmac_init(const uint8_t *key, size_t key_len);
123
124/**
125 * \brief Proceeds with the computation of an HMAC-SHA-256.
126 * \param data further data to authenticate
127 * \param data_len length of data in bytes
128 */
129void sha_256_hmac_update(const uint8_t *data, size_t data_len);
130
131/**
132 * \brief Finishes the computation of an HMAC-SHA-256.
133 * \param hmac pointer to where the resulting HMAC shall be stored
134 */
135void sha_256_hmac_finish(uint8_t hmac[static SHA_256_DIGEST_LENGTH]);
136
137/**
138 * \brief Computes HMAC-SHA-256 as per RFC 2104.
139 * \param key the key to authenticate with
140 * \param key_len length of key in bytes
141 * \param data the data to authenticate
142 * \param data_len length of data in bytes
143 * \param hmac pointer to where the resulting HMAC shall be stored
144 */
145void sha_256_hmac(const uint8_t *key, size_t key_len,
146 const uint8_t *data, size_t data_len,
147 uint8_t hmac[static SHA_256_DIGEST_LENGTH]);
148
149/**
150 * \brief Extracts a key as per RFC 5869.
151 * \param salt optional salt value
152 * \param salt_len length of salt in bytes
153 * \param ikm input keying material
154 * \param ikm_len length of ikm in bytes
155 * \param prk pointer to where the extracted key shall be stored
156 */
157void sha_256_hkdf_extract(const uint8_t *salt, size_t salt_len,
158 const uint8_t *ikm, size_t ikm_len,
159 uint8_t prk[static SHA_256_DIGEST_LENGTH]);
160
161/**
162 * \brief Expands a key as per RFC 5869.
163 * \param prk a pseudorandom key of at least SHA_256_DIGEST_LENGTH bytes
164 * \param prk_len length of prk in bytes
165 * \param info optional context and application specific information
166 * \param info_len length of info in bytes
167 * \param okm output keying material
168 * \param okm_len length of okm in bytes (<= 255 * SHA_256_DIGEST_LENGTH)
169 */
170void sha_256_hkdf_expand(const uint8_t *prk, size_t prk_len,
171 const uint8_t *info, size_t info_len,
172 uint8_t *okm, uint_fast16_t okm_len);
173
174/**
175 * \brief Performs both extraction and expansion as per RFC 5869.
176 * \param salt optional salt value
177 * \param salt_len length of salt in bytes
178 * \param ikm input keying material
179 * \param ikm_len length of ikm in bytes
180 * \param info optional context and application specific information
181 * \param info_len length of info in bytes
182 * \param okm output keying material
183 * \param okm_len length of okm in bytes (<= 255 * SHA_256_DIGEST_LENGTH)
184 */
185void sha_256_hkdf(const uint8_t *salt, size_t salt_len,
186 const uint8_t *ikm, size_t ikm_len,
187 const uint8_t *info, size_t info_len,
188 uint8_t *okm, uint_fast16_t okm_len);
189
190#endif /* SHA_256_H_ */
191
192/** @} */
void sha_256_hmac_update(const uint8_t *data, size_t data_len)
Proceeds with the computation of an HMAC-SHA-256.
Definition sha-256.c:380
void sha_256_hkdf(const uint8_t *salt, size_t salt_len, const uint8_t *ikm, size_t ikm_len, const uint8_t *info, size_t info_len, uint8_t *okm, uint_fast16_t okm_len)
Performs both extraction and expansion as per RFC 5869.
Definition sha-256.c:443
void sha_256_hkdf_expand(const uint8_t *prk, size_t prk_len, const uint8_t *info, size_t info_len, uint8_t *okm, uint_fast16_t okm_len)
Expands a key as per RFC 5869.
Definition sha-256.c:415
void sha_256_hmac_finish(uint8_t hmac[static 32])
Finishes the computation of an HMAC-SHA-256.
void sha_256_hmac_init(const uint8_t *key, size_t key_len)
Initiates a stepwise HMAC-SHA-256 computation.
Definition sha-256.c:355
void sha_256_hash(const uint8_t *data, size_t len, uint8_t digest[static 32])
Generic implementation of sha_256_driver::hash.
void sha_256_hkdf_extract(const uint8_t *salt, size_t salt_len, const uint8_t *ikm, size_t ikm_len, uint8_t prk[static 32])
Extracts a key as per RFC 5869.
void sha_256_hmac(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t hmac[static 32])
Computes HMAC-SHA-256 as per RFC 2104.
Structure of SHA-256 drivers.
Definition sha-256.h:69
void(* create_checkpoint)(sha_256_checkpoint_t *checkpoint)
Saves the hash session, e.g., before pausing a protothread.
Definition sha-256.h:92
void(* finalize)(uint8_t digest[static 32])
Terminates the hash session and produces the digest.
Definition sha-256.h:87
void(* init)(void)
Starts a hash session.
Definition sha-256.h:74
void(* restore_checkpoint)(const sha_256_checkpoint_t *checkpoint)
Restores a hash session, e.g., after resuming a protothread.
Definition sha-256.h:97
void(* update)(const uint8_t *data, size_t len)
Processes a chunk of data.
Definition sha-256.h:81
void(* hash)(const uint8_t *data, size_t len, uint8_t digest[static 32])
Does init, update, and finalize at once.
Definition sha-256.h:105