summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/picotls/deps/cifra/src/sha2.h
blob: c4ed24b578e8a043839a516335a537d6e802b33e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * cifra - embedded cryptography library
 * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#ifndef SHA2_H
#define SHA2_H

#include <stddef.h>
#include <stdint.h>

#include "chash.h"

/**
 * SHA224/SHA256
 * =============
 */

/* .. c:macro:: CF_SHA224_HASHSZ
 * The output size of SHA224: 28 bytes. */
#define CF_SHA224_HASHSZ 28

/* .. c:macro:: CF_SHA224_BLOCKSZ
 * The block size of SHA224: 64 bytes. */
#define CF_SHA224_BLOCKSZ 64

/* .. c:macro:: CF_SHA256_HASHSZ
 * The output size of SHA256: 32 bytes. */
#define CF_SHA256_HASHSZ 32

/* .. c:macro:: CF_SHA256_BLOCKSZ
 * The block size of SHA256: 64 bytes. */
#define CF_SHA256_BLOCKSZ 64

/* .. c:type:: cf_sha256_context
 * Incremental SHA256 hashing context.
 *
 * .. c:member:: cf_sha256_context.H
 * Intermediate values.
 *
 * .. c:member:: cf_sha256_context.partial
 * Unprocessed input.
 *
 * .. c:member:: cf_sha256_context.npartial
 * Number of bytes of unprocessed input.
 *
 * .. c:member:: cf_sha256_context.blocks
 * Number of full blocks processed.
 */
typedef struct
{
  uint32_t H[8];                      /* State. */
  uint8_t partial[CF_SHA256_BLOCKSZ]; /* Partial block of input. */
  uint32_t blocks;                    /* Number of full blocks processed into H. */
  size_t npartial;                    /* Number of bytes in prefix of partial. */
} cf_sha256_context;

/* .. c:function:: $DECL
 * Sets up `ctx` ready to hash a new message.
 */
extern void cf_sha256_init(cf_sha256_context *ctx);

/* .. c:function:: $DECL
 * Hashes `nbytes` at `data`.  Copies the data if there isn't enough to make
 * a full block.
 */
extern void cf_sha256_update(cf_sha256_context *ctx, const void *data, size_t nbytes);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA256_HASHSZ` bytes to `hash`.
 *
 * This leaves `ctx` unchanged.
 */
extern void cf_sha256_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA256_HASHSZ` bytes to `hash`.
 *
 * This destroys `ctx`, but uses less stack than :c:func:`cf_sha256_digest`.
 */
extern void cf_sha256_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]);

/* .. c:function:: $DECL
 * Sets up `ctx` ready to hash a new message.
 *
 * nb. SHA224 uses SHA256's underlying types.
 */
extern void cf_sha224_init(cf_sha256_context *ctx);

/* .. c:function:: $DECL
 * Hashes `nbytes` at `data`.  Copies the data if there isn't enough to make
 * a full block.
 */
extern void cf_sha224_update(cf_sha256_context *ctx, const void *data, size_t nbytes);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA224_HASHSZ` bytes to `hash`.
 *
 * This leaves `ctx` unchanged.
 */
extern void cf_sha224_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ]);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA224_HASHSZ` bytes to `hash`.
 *
 * This destroys `ctx`, but uses less stack than :c:func:`cf_sha224_digest`.
 */
extern void cf_sha224_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ]);

/* .. c:var:: cf_sha224
 * Abstract interface to SHA224.  See :c:type:`cf_chash` for more information.
 */
extern const cf_chash cf_sha224;

/* .. c:var:: cf_sha256
 * Abstract interface to SHA256.  See :c:type:`cf_chash` for more information.
 */
extern const cf_chash cf_sha256;

/**
 * SHA384/SHA512
 * =============
 */

/* .. c:macro:: CF_SHA384_HASHSZ
 * The output size of SHA384: 48 bytes. */
#define CF_SHA384_HASHSZ 48

/* .. c:macro:: CF_SHA384_BLOCKSZ
 * The block size of SHA384: 128 bytes. */
#define CF_SHA384_BLOCKSZ 128

/* .. c:macro:: CF_SHA512_HASHSZ
 * The output size of SHA512: 64 bytes. */
#define CF_SHA512_HASHSZ 64

/* .. c:macro:: CF_SHA512_BLOCKSZ
 * The block size of SHA512: 128 bytes. */
#define CF_SHA512_BLOCKSZ 128

/* .. c:type:: cf_sha512_context
 * Incremental SHA512 hashing context.
 *
 * .. c:member:: cf_sha512_context.H
 * Intermediate values.
 *
 * .. c:member:: cf_sha512_context.partial
 * Unprocessed input.
 *
 * .. c:member:: cf_sha512_context.npartial
 * Number of bytes of unprocessed input.
 *
 * .. c:member:: cf_sha512_context.blocks
 * Number of full blocks processed.
 */
typedef struct
{
  uint64_t H[8];
  uint8_t partial[CF_SHA512_BLOCKSZ];
  uint32_t blocks;
  size_t npartial;
} cf_sha512_context;

/* .. c:function:: $DECL
 * Sets up `ctx` ready to hash a new message.
 */
extern void cf_sha512_init(cf_sha512_context *ctx);

/* .. c:function:: $DECL
 * Hashes `nbytes` at `data`.  Copies the data if there isn't enough to make
 * a full block.
 */
extern void cf_sha512_update(cf_sha512_context *ctx, const void *data, size_t nbytes);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA512_HASHSZ` bytes to `hash`.
 *
 * This leaves `ctx` unchanged.
 */
extern void cf_sha512_digest(const cf_sha512_context *ctx, uint8_t hash[CF_SHA512_HASHSZ]);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA512_HASHSZ` bytes to `hash`.
 *
 * This destroys `ctx`, but uses less stack than :c:func:`cf_sha512_digest`.
 */
extern void cf_sha512_digest_final(cf_sha512_context *ctx, uint8_t hash[CF_SHA512_HASHSZ]);

/* .. c:function:: $DECL
 * Sets up `ctx` ready to hash a new message.
 *
 * nb. SHA384 uses SHA512's underlying types.
 */
extern void cf_sha384_init(cf_sha512_context *ctx);

/* .. c:function:: $DECL
 * Hashes `nbytes` at `data`.  Copies the data if there isn't enough to make
 * a full block.
 */
extern void cf_sha384_update(cf_sha512_context *ctx, const void *data, size_t nbytes);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA384_HASHSZ` bytes to `hash`.
 *
 * This leaves `ctx` unchanged.
 */
extern void cf_sha384_digest(const cf_sha512_context *ctx, uint8_t hash[CF_SHA384_HASHSZ]);

/* .. c:function:: $DECL
 * Finishes the hash operation, writing `CF_SHA384_HASHSZ` bytes to `hash`.
 *
 * This destroys `ctx`, but uses less stack than :c:func:`cf_sha384_digest`.
 */
extern void cf_sha384_digest_final(cf_sha512_context *ctx, uint8_t hash[CF_SHA384_HASHSZ]);

/* .. c:var:: cf_sha384
 * Abstract interface to SHA384.  See :c:type:`cf_chash` for more information.
 */
extern const cf_chash cf_sha384;

/* .. c:var:: cf_sha512
 * Abstract interface to SHA512.  See :c:type:`cf_chash` for more information.
 */
extern const cf_chash cf_sha512;

#endif