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 * \file
35 * Platform-independent SHA-256 API.
36 * \author
37 * Konrad Krentz <konrad.krentz@gmail.com>
38 */
39
40#ifndef SHA_256_H_
41#define SHA_256_H_
42
43#include "contiki.h"
44#include <stddef.h>
45#include <stdint.h>
46
47#define SHA_256_DIGEST_LENGTH 32
48#define SHA_256_BLOCK_SIZE 64
49
50#ifdef SHA_256_CONF
51#define SHA_256 SHA_256_CONF
52#else /* SHA_256_CONF */
53#define SHA_256 sha_256_driver
54#endif /* SHA_256_CONF */
55
56typedef struct {
57 uint64_t bit_count;
58 uint32_t state[SHA_256_DIGEST_LENGTH / sizeof(uint32_t)];
59 uint8_t buf[SHA_256_BLOCK_SIZE];
60 size_t buf_len;
61} sha_256_checkpoint_t;
62
63/**
64 * Structure of SHA-256 drivers.
65 */
67
68 /**
69 * \brief Starts a hash session.
70 */
71 void (* init)(void);
72
73 /**
74 * \brief Processes a chunk of data.
75 * \param data pointer to the data to hash
76 * \param len length of the data to hash in bytes
77 */
78 void (* update)(const uint8_t *data, size_t len);
79
80 /**
81 * \brief Terminates the hash session and produces the digest.
82 * \param digest pointer to the hash value
83 */
84 void (* finalize)(uint8_t digest[static SHA_256_DIGEST_LENGTH]);
85
86 /**
87 * \brief Saves the hash session, e.g., before pausing a protothread.
88 */
89 void (* create_checkpoint)(sha_256_checkpoint_t *checkpoint);
90
91 /**
92 * \brief Restores a hash session, e.g., after resuming a protothread.
93 */
94 void (* restore_checkpoint)(const sha_256_checkpoint_t *checkpoint);
95
96 /**
97 * \brief Does init, update, and finalize at once.
98 * \param data pointer to the data to hash
99 * \param len length of the data to hash in bytes
100 * \param digest pointer to the hash value
101 */
102 void (* hash)(const uint8_t *data, size_t len,
103 uint8_t digest[static SHA_256_DIGEST_LENGTH]);
104};
105
106extern const struct sha_256_driver SHA_256;
107
108/**
109 * \brief Generic implementation of sha_256_driver#hash.
110 */
111void sha_256_hash(const uint8_t *data, size_t len,
112 uint8_t digest[static SHA_256_DIGEST_LENGTH]);
113
114/**
115 * \brief Computes HMAC-SHA-256 as per RFC 2104.
116 * \param key the key to authenticate with
117 * \param key_len length of key in bytes
118 * \param data the data to authenticate
119 * \param data_len length of data in bytes
120 * \param hmac pointer to where the resulting HMAC shall be stored
121 */
122void sha_256_hmac(const uint8_t *key, size_t key_len,
123 const uint8_t *data, size_t data_len,
124 uint8_t hmac[static SHA_256_DIGEST_LENGTH]);
125
126/**
127 * \brief Extracts a key as per RFC 5869.
128 * \param salt optional salt value
129 * \param salt_len length of salt in bytes
130 * \param ikm input keying material
131 * \param ikm_len length of ikm in bytes
132 * \param prk pointer to where the extracted key shall be stored
133 */
134void sha_256_hkdf_extract(const uint8_t *salt, size_t salt_len,
135 const uint8_t *ikm, size_t ikm_len,
136 uint8_t prk[static SHA_256_DIGEST_LENGTH]);
137
138/**
139 * \brief Expands a key as per RFC 5869.
140 * \param prk a pseudorandom key of at least SHA_256_DIGEST_LENGTH bytes
141 * \param prk_len length of prk in bytes
142 * \param info optional context and application specific information
143 * \param info_len length of info in bytes
144 * \param okm output keying material
145 * \param okm_len length of okm in bytes (<= 255 * SHA_256_DIGEST_LENGTH)
146 */
147void sha_256_hkdf_expand(const uint8_t *prk, size_t prk_len,
148 const uint8_t *info, size_t info_len,
149 uint8_t *okm, uint_fast16_t okm_len);
150
151/**
152 * \brief Performs both extraction and expansion as per RFC 5869.
153 * \param salt optional salt value
154 * \param salt_len length of salt in bytes
155 * \param ikm input keying material
156 * \param ikm_len length of ikm in bytes
157 * \param info optional context and application specific information
158 * \param info_len length of info in bytes
159 * \param okm output keying material
160 * \param okm_len length of okm in bytes (<= 255 * SHA_256_DIGEST_LENGTH)
161 */
162void sha_256_hkdf(const uint8_t *salt, size_t salt_len,
163 const uint8_t *ikm, size_t ikm_len,
164 const uint8_t *info, size_t info_len,
165 uint8_t *okm, uint_fast16_t okm_len);
166
167#endif /* SHA_256_H_ */
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:442
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:410
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:66
void(* create_checkpoint)(sha_256_checkpoint_t *checkpoint)
Saves the hash session, e.g., before pausing a protothread.
Definition sha-256.h:89
void(* finalize)(uint8_t digest[static 32])
Terminates the hash session and produces the digest.
Definition sha-256.h:84
void(* init)(void)
Starts a hash session.
Definition sha-256.h:71
void(* restore_checkpoint)(const sha_256_checkpoint_t *checkpoint)
Restores a hash session, e.g., after resuming a protothread.
Definition sha-256.h:94
void(* update)(const uint8_t *data, size_t len)
Processes a chunk of data.
Definition sha-256.h:78
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:102