Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/sha3/sph_sha2.h
1201 views
1
/* $Id: sph_sha2.h 216 2010-06-08 09:46:57Z tp $ */
2
/**
3
* SHA-224, SHA-256, SHA-384 and SHA-512 interface.
4
*
5
* SHA-256 has been published in FIPS 180-2, now amended with a change
6
* notice to include SHA-224 as well (which is a simple variation on
7
* SHA-256). SHA-384 and SHA-512 are also defined in FIPS 180-2. FIPS
8
* standards can be found at:
9
* http://csrc.nist.gov/publications/fips/
10
*
11
* ==========================(LICENSE BEGIN)============================
12
*
13
* Copyright (c) 2007-2010 Projet RNRT SAPHIR
14
*
15
* Permission is hereby granted, free of charge, to any person obtaining
16
* a copy of this software and associated documentation files (the
17
* "Software"), to deal in the Software without restriction, including
18
* without limitation the rights to use, copy, modify, merge, publish,
19
* distribute, sublicense, and/or sell copies of the Software, and to
20
* permit persons to whom the Software is furnished to do so, subject to
21
* the following conditions:
22
*
23
* The above copyright notice and this permission notice shall be
24
* included in all copies or substantial portions of the Software.
25
*
26
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
30
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
*
34
* ===========================(LICENSE END)=============================
35
*
36
* @file sph_sha2.h
37
* @author Thomas Pornin <[email protected]>
38
*/
39
40
#ifndef SPH_SHA2_H__
41
#define SPH_SHA2_H__
42
43
#include <stddef.h>
44
#include "sph_types.h"
45
46
/**
47
* Output size (in bits) for SHA-224.
48
*/
49
#define SPH_SIZE_sha224 224
50
51
/**
52
* Output size (in bits) for SHA-256.
53
*/
54
#define SPH_SIZE_sha256 256
55
56
/**
57
* This structure is a context for SHA-224 computations: it contains the
58
* intermediate values and some data from the last entered block. Once
59
* a SHA-224 computation has been performed, the context can be reused for
60
* another computation.
61
*
62
* The contents of this structure are private. A running SHA-224 computation
63
* can be cloned by copying the context (e.g. with a simple
64
* <code>memcpy()</code>).
65
*/
66
typedef struct {
67
#ifndef DOXYGEN_IGNORE
68
unsigned char buf[64]; /* first field, for alignment */
69
sph_u32 val[8];
70
#if SPH_64
71
sph_u64 count;
72
#else
73
sph_u32 count_high, count_low;
74
#endif
75
#endif
76
} sph_sha224_context;
77
78
/**
79
* This structure is a context for SHA-256 computations. It is identical
80
* to the SHA-224 context. However, a context is initialized for SHA-224
81
* <strong>or</strong> SHA-256, but not both (the internal IV is not the
82
* same).
83
*/
84
typedef sph_sha224_context sph_sha256_context;
85
86
/**
87
* Initialize a SHA-224 context. This process performs no memory allocation.
88
*
89
* @param cc the SHA-224 context (pointer to
90
* a <code>sph_sha224_context</code>)
91
*/
92
void sph_sha224_init(void *cc);
93
94
/**
95
* Process some data bytes. It is acceptable that <code>len</code> is zero
96
* (in which case this function does nothing).
97
*
98
* @param cc the SHA-224 context
99
* @param data the input data
100
* @param len the input data length (in bytes)
101
*/
102
void sph_sha224(void *cc, const void *data, size_t len);
103
104
/**
105
* Terminate the current SHA-224 computation and output the result into the
106
* provided buffer. The destination buffer must be wide enough to
107
* accomodate the result (28 bytes). The context is automatically
108
* reinitialized.
109
*
110
* @param cc the SHA-224 context
111
* @param dst the destination buffer
112
*/
113
void sph_sha224_close(void *cc, void *dst);
114
115
/**
116
* Add a few additional bits (0 to 7) to the current computation, then
117
* terminate it and output the result in the provided buffer, which must
118
* be wide enough to accomodate the result (28 bytes). If bit number i
119
* in <code>ub</code> has value 2^i, then the extra bits are those
120
* numbered 7 downto 8-n (this is the big-endian convention at the byte
121
* level). The context is automatically reinitialized.
122
*
123
* @param cc the SHA-224 context
124
* @param ub the extra bits
125
* @param n the number of extra bits (0 to 7)
126
* @param dst the destination buffer
127
*/
128
void sph_sha224_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);
129
130
/**
131
* Apply the SHA-224 compression function on the provided data. The
132
* <code>msg</code> parameter contains the 16 32-bit input blocks,
133
* as numerical values (hence after the big-endian decoding). The
134
* <code>val</code> parameter contains the 8 32-bit input blocks for
135
* the compression function; the output is written in place in this
136
* array.
137
*
138
* @param msg the message block (16 values)
139
* @param val the function 256-bit input and output
140
*/
141
void sph_sha224_comp(const sph_u32 msg[16], sph_u32 val[8]);
142
143
/**
144
* Initialize a SHA-256 context. This process performs no memory allocation.
145
*
146
* @param cc the SHA-256 context (pointer to
147
* a <code>sph_sha256_context</code>)
148
*/
149
void sph_sha256_init(void *cc);
150
151
#ifdef DOXYGEN_IGNORE
152
/**
153
* Process some data bytes, for SHA-256. This function is identical to
154
* <code>sha_224()</code>
155
*
156
* @param cc the SHA-224 context
157
* @param data the input data
158
* @param len the input data length (in bytes)
159
*/
160
void sph_sha256(void *cc, const void *data, size_t len);
161
#endif
162
163
#ifndef DOXYGEN_IGNORE
164
#define sph_sha256 sph_sha224
165
#endif
166
167
/**
168
* Terminate the current SHA-256 computation and output the result into the
169
* provided buffer. The destination buffer must be wide enough to
170
* accomodate the result (32 bytes). The context is automatically
171
* reinitialized.
172
*
173
* @param cc the SHA-256 context
174
* @param dst the destination buffer
175
*/
176
void sph_sha256_close(void *cc, void *dst);
177
178
/**
179
* Add a few additional bits (0 to 7) to the current computation, then
180
* terminate it and output the result in the provided buffer, which must
181
* be wide enough to accomodate the result (32 bytes). If bit number i
182
* in <code>ub</code> has value 2^i, then the extra bits are those
183
* numbered 7 downto 8-n (this is the big-endian convention at the byte
184
* level). The context is automatically reinitialized.
185
*
186
* @param cc the SHA-256 context
187
* @param ub the extra bits
188
* @param n the number of extra bits (0 to 7)
189
* @param dst the destination buffer
190
*/
191
void sph_sha256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);
192
193
#ifdef DOXYGEN_IGNORE
194
/**
195
* Apply the SHA-256 compression function on the provided data. This
196
* function is identical to <code>sha224_comp()</code>.
197
*
198
* @param msg the message block (16 values)
199
* @param val the function 256-bit input and output
200
*/
201
void sph_sha256_comp(const sph_u32 msg[16], sph_u32 val[8]);
202
#endif
203
204
#ifndef DOXYGEN_IGNORE
205
#define sph_sha256_comp sph_sha224_comp
206
#endif
207
208
#if SPH_64
209
210
/**
211
* Output size (in bits) for SHA-384.
212
*/
213
#define SPH_SIZE_sha384 384
214
215
/**
216
* Output size (in bits) for SHA-512.
217
*/
218
#define SPH_SIZE_sha512 512
219
220
/**
221
* This structure is a context for SHA-384 computations: it contains the
222
* intermediate values and some data from the last entered block. Once
223
* a SHA-384 computation has been performed, the context can be reused for
224
* another computation.
225
*
226
* The contents of this structure are private. A running SHA-384 computation
227
* can be cloned by copying the context (e.g. with a simple
228
* <code>memcpy()</code>).
229
*/
230
typedef struct {
231
#ifndef DOXYGEN_IGNORE
232
unsigned char buf[128]; /* first field, for alignment */
233
sph_u64 val[8];
234
sph_u64 count;
235
#endif
236
} sph_sha384_context;
237
238
/**
239
* Initialize a SHA-384 context. This process performs no memory allocation.
240
*
241
* @param cc the SHA-384 context (pointer to
242
* a <code>sph_sha384_context</code>)
243
*/
244
void sph_sha384_init(void *cc);
245
246
/**
247
* Process some data bytes. It is acceptable that <code>len</code> is zero
248
* (in which case this function does nothing).
249
*
250
* @param cc the SHA-384 context
251
* @param data the input data
252
* @param len the input data length (in bytes)
253
*/
254
void sph_sha384(void *cc, const void *data, size_t len);
255
256
/**
257
* Terminate the current SHA-384 computation and output the result into the
258
* provided buffer. The destination buffer must be wide enough to
259
* accomodate the result (48 bytes). The context is automatically
260
* reinitialized.
261
*
262
* @param cc the SHA-384 context
263
* @param dst the destination buffer
264
*/
265
void sph_sha384_close(void *cc, void *dst);
266
267
/**
268
* Add a few additional bits (0 to 7) to the current computation, then
269
* terminate it and output the result in the provided buffer, which must
270
* be wide enough to accomodate the result (48 bytes). If bit number i
271
* in <code>ub</code> has value 2^i, then the extra bits are those
272
* numbered 7 downto 8-n (this is the big-endian convention at the byte
273
* level). The context is automatically reinitialized.
274
*
275
* @param cc the SHA-384 context
276
* @param ub the extra bits
277
* @param n the number of extra bits (0 to 7)
278
* @param dst the destination buffer
279
*/
280
void sph_sha384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);
281
282
/**
283
* Apply the SHA-384 compression function on the provided data. The
284
* <code>msg</code> parameter contains the 16 64-bit input blocks,
285
* as numerical values (hence after the big-endian decoding). The
286
* <code>val</code> parameter contains the 8 64-bit input blocks for
287
* the compression function; the output is written in place in this
288
* array.
289
*
290
* @param msg the message block (16 values)
291
* @param val the function 512-bit input and output
292
*/
293
void sph_sha384_comp(const sph_u64 msg[16], sph_u64 val[8]);
294
295
/**
296
* This structure is a context for SHA-512 computations. It is identical
297
* to the SHA-384 context. However, a context is initialized for SHA-384
298
* <strong>or</strong> SHA-512, but not both (the internal IV is not the
299
* same).
300
*/
301
typedef sph_sha384_context sph_sha512_context;
302
303
/**
304
* Initialize a SHA-512 context. This process performs no memory allocation.
305
*
306
* @param cc the SHA-512 context (pointer to
307
* a <code>sph_sha512_context</code>)
308
*/
309
void sph_sha512_init(void *cc);
310
311
#ifdef DOXYGEN_IGNORE
312
/**
313
* Process some data bytes, for SHA-512. This function is identical to
314
* <code>sph_sha384()</code>.
315
*
316
* @param cc the SHA-384 context
317
* @param data the input data
318
* @param len the input data length (in bytes)
319
*/
320
void sph_sha512(void *cc, const void *data, size_t len);
321
#endif
322
323
#ifndef DOXYGEN_IGNORE
324
#define sph_sha512 sph_sha384
325
#endif
326
327
/**
328
* Terminate the current SHA-512 computation and output the result into the
329
* provided buffer. The destination buffer must be wide enough to
330
* accomodate the result (64 bytes). The context is automatically
331
* reinitialized.
332
*
333
* @param cc the SHA-512 context
334
* @param dst the destination buffer
335
*/
336
void sph_sha512_close(void *cc, void *dst);
337
338
/**
339
* Add a few additional bits (0 to 7) to the current computation, then
340
* terminate it and output the result in the provided buffer, which must
341
* be wide enough to accomodate the result (64 bytes). If bit number i
342
* in <code>ub</code> has value 2^i, then the extra bits are those
343
* numbered 7 downto 8-n (this is the big-endian convention at the byte
344
* level). The context is automatically reinitialized.
345
*
346
* @param cc the SHA-512 context
347
* @param ub the extra bits
348
* @param n the number of extra bits (0 to 7)
349
* @param dst the destination buffer
350
*/
351
void sph_sha512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);
352
353
#ifdef DOXYGEN_IGNORE
354
/**
355
* Apply the SHA-512 compression function. This function is identical to
356
* <code>sph_sha384_comp()</code>.
357
*
358
* @param msg the message block (16 values)
359
* @param val the function 512-bit input and output
360
*/
361
void sph_sha512_comp(const sph_u64 msg[16], sph_u64 val[8]);
362
#endif
363
364
#ifndef DOXYGEN_IGNORE
365
#define sph_sha512_comp sph_sha384_comp
366
#endif
367
368
#endif
369
370
#endif
371
372