/src/kamailio/src/core/crypto/sha256.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * FILE: sha256.c |
3 | | * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ |
4 | | * |
5 | | * Copyright (c) 2000-2001, Aaron D. Gifford |
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 w627ith the distribution. |
16 | | * 3. Neither the name of the copyright holder nor the names of contributors |
17 | | * may be used to endorse or promote products derived from this software |
18 | | * without specific prior written permission. |
19 | | * |
20 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND |
21 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE |
24 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | | * SUCH DAMAGE. |
31 | | * |
32 | | */ |
33 | | |
34 | | #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */ |
35 | | #include <assert.h> /* assert() */ |
36 | | #include "sha256.h" |
37 | | |
38 | | /* discover byte order on solaris */ |
39 | | #if defined(__SVR4) || defined(__sun) |
40 | | #include <sys/isa_defs.h> |
41 | | #define BYTE_ORDER _BYTE_ORDER |
42 | | #endif |
43 | | |
44 | | /* |
45 | | * ASSERT NOTE: |
46 | | * Some sanity checking code is included using assert(). On my FreeBSD |
47 | | * system, this additional code can be removed by compiling with NDEBUG |
48 | | * defined. Check your own systems manpage on assert() to see how to |
49 | | * compile WITHOUT the sanity checking code on your system. |
50 | | * |
51 | | * UNROLLED TRANSFORM LOOP NOTE: |
52 | | * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform |
53 | | * loop version for the hash transform rounds (defined using macros |
54 | | * later in this file). Either define on the command line, for example: |
55 | | * |
56 | | * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c |
57 | | * |
58 | | * or define below: |
59 | | * |
60 | | * #define SHA2_UNROLL_TRANSFORM |
61 | | * |
62 | | */ |
63 | | |
64 | | |
65 | | /*** SHA-256/384/512 Machine Architecture Definitions *****************/ |
66 | | /* |
67 | | * BYTE_ORDER NOTE: |
68 | | * |
69 | | * Please make sure that your system defines BYTE_ORDER. If your |
70 | | * architecture is little-endian, make sure it also defines |
71 | | * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are |
72 | | * equivilent. |
73 | | * |
74 | | * If your system does not define the above, then you can do so by |
75 | | * hand like this: |
76 | | * |
77 | | * #define LITTLE_ENDIAN 1234 |
78 | | * #define BIG_ENDIAN 4321 |
79 | | * |
80 | | * And for little-endian machines, add: |
81 | | * |
82 | | * #define BYTE_ORDER LITTLE_ENDIAN |
83 | | * |
84 | | * Or for big-endian machines: |
85 | | * |
86 | | * #define BYTE_ORDER BIG_ENDIAN |
87 | | * |
88 | | * The FreeBSD machine this was written on defines BYTE_ORDER |
89 | | * appropriately by including <sys/types.h> (which in turn includes |
90 | | * <machine/endian.h> where the appropriate definitions are actually |
91 | | * made). |
92 | | */ |
93 | | #if !defined(BYTE_ORDER) \ |
94 | | || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) |
95 | | #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN |
96 | | #endif |
97 | | |
98 | | /*** SHA-256/384/512 Various Length Definitions ***********************/ |
99 | | /* NOTE: Most of these are in sha2.h */ |
100 | 0 | #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) |
101 | | #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16) |
102 | 0 | #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) |
103 | | |
104 | | |
105 | | /*** ENDIAN REVERSAL MACROS *******************************************/ |
106 | | #if BYTE_ORDER == LITTLE_ENDIAN |
107 | | #define REVERSE32(w, x) \ |
108 | 0 | { \ |
109 | 0 | sha2_word32 tmp = (w); \ |
110 | 0 | tmp = (tmp >> 16) | (tmp << 16); \ |
111 | 0 | (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ |
112 | 0 | } |
113 | | #define REVERSE64(w, x) \ |
114 | 0 | { \ |
115 | 0 | sha2_word64 tmp = (w); \ |
116 | 0 | tmp = (tmp >> 32) | (tmp << 32); \ |
117 | 0 | tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) \ |
118 | 0 | | ((tmp & 0x00ff00ff00ff00ffULL) << 8); \ |
119 | 0 | (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) \ |
120 | 0 | | ((tmp & 0x0000ffff0000ffffULL) << 16); \ |
121 | 0 | } |
122 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
123 | | |
124 | | /* |
125 | | * Macro for incrementally adding the unsigned 64-bit integer n to the |
126 | | * unsigned 128-bit integer (represented using a two-element array of |
127 | | * 64-bit words): |
128 | | */ |
129 | | #define ADDINC128(w, n) \ |
130 | 0 | { \ |
131 | 0 | (w)[0] += (sha2_word64)(n); \ |
132 | 0 | if((w)[0] < (n)) { \ |
133 | 0 | (w)[1]++; \ |
134 | 0 | } \ |
135 | 0 | } |
136 | | |
137 | | /* |
138 | | * Macros for copying blocks of memory and for zeroing out ranges |
139 | | * of memory. Using these macros makes it easy to switch from |
140 | | * using memset()/memcpy() and using bzero()/bcopy(). |
141 | | * |
142 | | * Please define either SHA2_USE_MEMSET_MEMCPY or define |
143 | | * SHA2_USE_BZERO_BCOPY depending on which function set you |
144 | | * choose to use: |
145 | | */ |
146 | | #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY) |
147 | | /* Default to memset()/memcpy() if no option is specified */ |
148 | | #define SHA2_USE_MEMSET_MEMCPY 1 |
149 | | #endif |
150 | | #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY) |
151 | | /* Abort with an error if BOTH options are defined */ |
152 | | #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both! |
153 | | #endif |
154 | | |
155 | | #ifdef SHA2_USE_MEMSET_MEMCPY |
156 | 0 | #define MEMSET_BZERO(p, l) memset((p), 0, (l)) |
157 | 0 | #define MEMCPY_BCOPY(d, s, l) memcpy((d), (s), (l)) |
158 | | #endif |
159 | | #ifdef SHA2_USE_BZERO_BCOPY |
160 | | #define MEMSET_BZERO(p, l) bzero((p), (l)) |
161 | | #define MEMCPY_BCOPY(d, s, l) bcopy((s), (d), (l)) |
162 | | #endif |
163 | | |
164 | | |
165 | | /*** THE SIX LOGICAL FUNCTIONS ****************************************/ |
166 | | /* |
167 | | * Bit shifting and rotation (used by the six SHA-XYZ logical functions: |
168 | | * |
169 | | * NOTE: The naming of R and S appears backwards here (R is a SHIFT and |
170 | | * S is a ROTATION) because the SHA-256/384/512 description document |
171 | | * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this |
172 | | * same "backwards" definition. |
173 | | */ |
174 | | /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ |
175 | 0 | #define R(b, x) ((x) >> (b)) |
176 | | /* 32-bit Rotate-right (used in SHA-256): */ |
177 | 0 | #define S32(b, x) (((x) >> (b)) | ((x) << (32 - (b)))) |
178 | | /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ |
179 | 0 | #define S64(b, x) (((x) >> (b)) | ((x) << (64 - (b)))) |
180 | | |
181 | | /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ |
182 | 0 | #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) |
183 | 0 | #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
184 | | |
185 | | /* Four of six logical functions used in SHA-256: */ |
186 | 0 | #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) |
187 | 0 | #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) |
188 | 0 | #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3, (x))) |
189 | 0 | #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) |
190 | | |
191 | | /* Four of six logical functions used in SHA-384 and SHA-512: */ |
192 | 0 | #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) |
193 | 0 | #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) |
194 | 0 | #define sigma0_512(x) (S64(1, (x)) ^ S64(8, (x)) ^ R(7, (x))) |
195 | 0 | #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R(6, (x))) |
196 | | |
197 | | /*** INTERNAL FUNCTION PROTOTYPES *************************************/ |
198 | | /* NOTE: These should not be accessed directly from outside this |
199 | | * library -- they are intended for private internal visibility/use |
200 | | * only. |
201 | | */ |
202 | | void SHA512_Last(SHA512_CTX *); |
203 | | void SHA256_Transform(SHA256_CTX *, const sha2_word32 *); |
204 | | void SHA512_Transform(SHA512_CTX *, const sha2_word64 *); |
205 | | |
206 | | |
207 | | /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ |
208 | | /* Hash constant words K for SHA-256: */ |
209 | | const static sha2_word32 K256[64] = {0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, |
210 | | 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, |
211 | | 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, |
212 | | 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, |
213 | | 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, |
214 | | 0x76f988daUL, 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, |
215 | | 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, |
216 | | 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, |
217 | | 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, |
218 | | 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, |
219 | | 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, |
220 | | 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, |
221 | | 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, |
222 | | 0xc67178f2UL}; |
223 | | |
224 | | /* Initial hash value H for SHA-256: */ |
225 | | const static sha2_word32 sha256_initial_hash_value[8] = {0x6a09e667UL, |
226 | | 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL, 0x510e527fUL, 0x9b05688cUL, |
227 | | 0x1f83d9abUL, 0x5be0cd19UL}; |
228 | | |
229 | | /* Hash constant words K for SHA-384 and SHA-512: */ |
230 | | const static sha2_word64 K512[80] = {0x428a2f98d728ae22ULL, |
231 | | 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, |
232 | | 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, |
233 | | 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, |
234 | | 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, |
235 | | 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, |
236 | | 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, |
237 | | 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, |
238 | | 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 0x983e5152ee66dfabULL, |
239 | | 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, |
240 | | 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, |
241 | | 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, |
242 | | 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, |
243 | | 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, |
244 | | 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, |
245 | | 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, |
246 | | 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 0x19a4c116b8d2d0c8ULL, |
247 | | 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, |
248 | | 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, |
249 | | 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, |
250 | | 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 0x90befffa23631e28ULL, |
251 | | 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, |
252 | | 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, |
253 | | 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, |
254 | | 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 0x28db77f523047d84ULL, |
255 | | 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, |
256 | | 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, |
257 | | 0x6c44198c4a475817ULL}; |
258 | | |
259 | | /* Initial hash value H for SHA-384 */ |
260 | | const static sha2_word64 sha384_initial_hash_value[8] = {0xcbbb9d5dc1059ed8ULL, |
261 | | 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, |
262 | | 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, |
263 | | 0x47b5481dbefa4fa4ULL}; |
264 | | |
265 | | /* Initial hash value H for SHA-512 */ |
266 | | const static sha2_word64 sha512_initial_hash_value[8] = {0x6a09e667f3bcc908ULL, |
267 | | 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, |
268 | | 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, |
269 | | 0x5be0cd19137e2179ULL}; |
270 | | |
271 | | /* Initial hash value H for SHA-512/256 */ |
272 | | const static sha2_word64 sha512_256_initial_hash_value[8] = { |
273 | | 0x22312194FC2BF72CULL, 0x9F555FA3C84C64C2ULL, 0x2393B86B6F53B151ULL, |
274 | | 0x963877195940EABDULL, 0x96283EE2A88EFFE3ULL, 0xBE5E1E2553863992ULL, |
275 | | 0x2B0199FC2C85B8AAULL, 0x0EB72DDC81C52CA2ULL}; |
276 | | |
277 | | /* |
278 | | * Constant used by SHA256/384/512_End() functions for converting the |
279 | | * digest to a readable hexadecimal character string: |
280 | | */ |
281 | | static const char *sha2_hex_digits = "0123456789abcdef"; |
282 | | |
283 | | |
284 | | /*** SHA-256: *********************************************************/ |
285 | | void sr_SHA256_Init(SHA256_CTX *context) |
286 | 0 | { |
287 | 0 | if(context == (SHA256_CTX *)0) { |
288 | 0 | return; |
289 | 0 | } |
290 | 0 | MEMCPY_BCOPY( |
291 | 0 | context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH); |
292 | 0 | MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH); |
293 | 0 | context->bitcount = 0; |
294 | 0 | } |
295 | | |
296 | | #ifdef SHA2_UNROLL_TRANSFORM |
297 | | |
298 | | /* Unrolled SHA-256 round macros: */ |
299 | | |
300 | | #if BYTE_ORDER == LITTLE_ENDIAN |
301 | | |
302 | | #define ROUND256_0_TO_15(a, b, c, d, e, f, g, h) \ |
303 | | REVERSE32(*data++, W256[j]); \ |
304 | | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + W256[j]; \ |
305 | | (d) += T1; \ |
306 | | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ |
307 | | j++ |
308 | | |
309 | | |
310 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
311 | | |
312 | | #define ROUND256_0_TO_15(a, b, c, d, e, f, g, h) \ |
313 | | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] \ |
314 | | + (W256[j] = *data++); \ |
315 | | (d) += T1; \ |
316 | | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ |
317 | | j++ |
318 | | |
319 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
320 | | |
321 | | #define ROUND256(a, b, c, d, e, f, g, h) \ |
322 | | s0 = W256[(j + 1) & 0x0f]; \ |
323 | | s0 = sigma0_256(s0); \ |
324 | | s1 = W256[(j + 14) & 0x0f]; \ |
325 | | s1 = sigma1_256(s1); \ |
326 | | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] \ |
327 | | + (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0); \ |
328 | | (d) += T1; \ |
329 | | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ |
330 | | j++ |
331 | | |
332 | | void SHA256_Transform(SHA256_CTX *context, const sha2_word32 *data) |
333 | | { |
334 | | sha2_word32 a, b, c, d, e, f, g, h, s0, s1; |
335 | | sha2_word32 T1, *W256; |
336 | | int j; |
337 | | |
338 | | W256 = (sha2_word32 *)context->buffer; |
339 | | |
340 | | /* Initialize registers with the prev. intermediate value */ |
341 | | a = context->state[0]; |
342 | | b = context->state[1]; |
343 | | c = context->state[2]; |
344 | | d = context->state[3]; |
345 | | e = context->state[4]; |
346 | | f = context->state[5]; |
347 | | g = context->state[6]; |
348 | | h = context->state[7]; |
349 | | |
350 | | j = 0; |
351 | | do { |
352 | | /* Rounds 0 to 15 (unrolled): */ |
353 | | ROUND256_0_TO_15(a, b, c, d, e, f, g, h); |
354 | | ROUND256_0_TO_15(h, a, b, c, d, e, f, g); |
355 | | ROUND256_0_TO_15(g, h, a, b, c, d, e, f); |
356 | | ROUND256_0_TO_15(f, g, h, a, b, c, d, e); |
357 | | ROUND256_0_TO_15(e, f, g, h, a, b, c, d); |
358 | | ROUND256_0_TO_15(d, e, f, g, h, a, b, c); |
359 | | ROUND256_0_TO_15(c, d, e, f, g, h, a, b); |
360 | | ROUND256_0_TO_15(b, c, d, e, f, g, h, a); |
361 | | } while(j < 16); |
362 | | |
363 | | /* Now for the remaining rounds to 64: */ |
364 | | do { |
365 | | ROUND256(a, b, c, d, e, f, g, h); |
366 | | ROUND256(h, a, b, c, d, e, f, g); |
367 | | ROUND256(g, h, a, b, c, d, e, f); |
368 | | ROUND256(f, g, h, a, b, c, d, e); |
369 | | ROUND256(e, f, g, h, a, b, c, d); |
370 | | ROUND256(d, e, f, g, h, a, b, c); |
371 | | ROUND256(c, d, e, f, g, h, a, b); |
372 | | ROUND256(b, c, d, e, f, g, h, a); |
373 | | } while(j < 64); |
374 | | |
375 | | /* Compute the current intermediate hash value */ |
376 | | context->state[0] += a; |
377 | | context->state[1] += b; |
378 | | context->state[2] += c; |
379 | | context->state[3] += d; |
380 | | context->state[4] += e; |
381 | | context->state[5] += f; |
382 | | context->state[6] += g; |
383 | | context->state[7] += h; |
384 | | |
385 | | /* Clean up */ |
386 | | a = b = c = d = e = f = g = h = T1 = 0; |
387 | | } |
388 | | |
389 | | #else /* SHA2_UNROLL_TRANSFORM */ |
390 | | |
391 | | void SHA256_Transform(SHA256_CTX *context, const sha2_word32 *data) |
392 | 0 | { |
393 | 0 | sha2_word32 a, b, c, d, e, f, g, h, s0, s1; |
394 | 0 | sha2_word32 T1, T2, *W256; |
395 | 0 | int j; |
396 | |
|
397 | 0 | W256 = (sha2_word32 *)context->buffer; |
398 | | |
399 | | /* Initialize registers with the prev. intermediate value */ |
400 | 0 | a = context->state[0]; |
401 | 0 | b = context->state[1]; |
402 | 0 | c = context->state[2]; |
403 | 0 | d = context->state[3]; |
404 | 0 | e = context->state[4]; |
405 | 0 | f = context->state[5]; |
406 | 0 | g = context->state[6]; |
407 | 0 | h = context->state[7]; |
408 | |
|
409 | 0 | j = 0; |
410 | 0 | do { |
411 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
412 | | /* Copy data while converting to host byte order */ |
413 | 0 | REVERSE32(*data++, W256[j]); |
414 | | /* Apply the SHA-256 compression function to update a..h */ |
415 | 0 | T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; |
416 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
417 | | /* Apply the SHA-256 compression function to update a..h with copy */ |
418 | | T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++); |
419 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
420 | 0 | T2 = Sigma0_256(a) + Maj(a, b, c); |
421 | 0 | h = g; |
422 | 0 | g = f; |
423 | 0 | f = e; |
424 | 0 | e = d + T1; |
425 | 0 | d = c; |
426 | 0 | c = b; |
427 | 0 | b = a; |
428 | 0 | a = T1 + T2; |
429 | |
|
430 | 0 | j++; |
431 | 0 | } while(j < 16); |
432 | |
|
433 | 0 | do { |
434 | | /* Part of the message block expansion: */ |
435 | 0 | s0 = W256[(j + 1) & 0x0f]; |
436 | 0 | s0 = sigma0_256(s0); |
437 | 0 | s1 = W256[(j + 14) & 0x0f]; |
438 | 0 | s1 = sigma1_256(s1); |
439 | | |
440 | | /* Apply the SHA-256 compression function to update a..h */ |
441 | 0 | T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] |
442 | 0 | + (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0); |
443 | 0 | T2 = Sigma0_256(a) + Maj(a, b, c); |
444 | 0 | h = g; |
445 | 0 | g = f; |
446 | 0 | f = e; |
447 | 0 | e = d + T1; |
448 | 0 | d = c; |
449 | 0 | c = b; |
450 | 0 | b = a; |
451 | 0 | a = T1 + T2; |
452 | |
|
453 | 0 | j++; |
454 | 0 | } while(j < 64); |
455 | | |
456 | | /* Compute the current intermediate hash value */ |
457 | 0 | context->state[0] += a; |
458 | 0 | context->state[1] += b; |
459 | 0 | context->state[2] += c; |
460 | 0 | context->state[3] += d; |
461 | 0 | context->state[4] += e; |
462 | 0 | context->state[5] += f; |
463 | 0 | context->state[6] += g; |
464 | 0 | context->state[7] += h; |
465 | | |
466 | | /* Clean up */ |
467 | 0 | a = b = c = d = e = f = g = h = T1 = T2 = 0; |
468 | 0 | } |
469 | | |
470 | | #endif /* SHA2_UNROLL_TRANSFORM */ |
471 | | |
472 | | void sr_SHA256_Update(SHA256_CTX *context, const sha2_byte *data, size_t len) |
473 | 0 | { |
474 | 0 | unsigned int freespace, usedspace; |
475 | |
|
476 | 0 | if(len == 0) { |
477 | | /* Calling with no data is valid - we do nothing */ |
478 | 0 | return; |
479 | 0 | } |
480 | | |
481 | | /* Sanity check: */ |
482 | 0 | assert(context != (SHA256_CTX *)0 && data != (sha2_byte *)0); |
483 | | |
484 | 0 | usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
485 | 0 | if(usedspace > 0) { |
486 | | /* Calculate how much free space is available in the buffer */ |
487 | 0 | freespace = SHA256_BLOCK_LENGTH - usedspace; |
488 | |
|
489 | 0 | if(len >= freespace) { |
490 | | /* Fill the buffer completely and process it */ |
491 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); |
492 | 0 | context->bitcount += freespace << 3; |
493 | 0 | len -= freespace; |
494 | 0 | data += freespace; |
495 | 0 | SHA256_Transform(context, (sha2_word32 *)context->buffer); |
496 | 0 | } else { |
497 | | /* The buffer is not yet full */ |
498 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, len); |
499 | 0 | context->bitcount += len << 3; |
500 | | /* Clean up: */ |
501 | 0 | usedspace = freespace = 0; |
502 | 0 | return; |
503 | 0 | } |
504 | 0 | } |
505 | 0 | while(len >= SHA256_BLOCK_LENGTH) { |
506 | | /* Process as many complete blocks as we can */ |
507 | 0 | SHA256_Transform(context, (sha2_word32 *)data); |
508 | 0 | context->bitcount += SHA256_BLOCK_LENGTH << 3; |
509 | 0 | len -= SHA256_BLOCK_LENGTH; |
510 | 0 | data += SHA256_BLOCK_LENGTH; |
511 | 0 | } |
512 | 0 | if(len > 0) { |
513 | | /* There's left-overs, so save 'em */ |
514 | 0 | MEMCPY_BCOPY(context->buffer, data, len); |
515 | 0 | context->bitcount += len << 3; |
516 | 0 | } |
517 | | /* Clean up: */ |
518 | 0 | usedspace = freespace = 0; |
519 | 0 | } |
520 | | |
521 | | void sr_SHA256_Final( |
522 | | sha2_byte digest[SHA256_DIGEST_LENGTH], SHA256_CTX *context) |
523 | 0 | { |
524 | 0 | sha2_word32 *d = (sha2_word32 *)digest; |
525 | 0 | unsigned int usedspace; |
526 | | |
527 | | /* Sanity check: */ |
528 | 0 | assert(context != (SHA256_CTX *)0); |
529 | | |
530 | | /* If no digest buffer is passed, we don't bother doing this: */ |
531 | 0 | if(digest != (sha2_byte *)0) { |
532 | 0 | usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
533 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
534 | | /* Convert FROM host byte order */ |
535 | 0 | REVERSE64(context->bitcount, context->bitcount); |
536 | 0 | #endif |
537 | 0 | if(usedspace > 0) { |
538 | | /* Begin padding with a 1 bit: */ |
539 | 0 | context->buffer[usedspace++] = 0x80; |
540 | |
|
541 | 0 | if(usedspace <= SHA256_SHORT_BLOCK_LENGTH) { |
542 | | /* Set-up for the last transform: */ |
543 | 0 | MEMSET_BZERO(&context->buffer[usedspace], |
544 | 0 | SHA256_SHORT_BLOCK_LENGTH - usedspace); |
545 | 0 | } else { |
546 | 0 | if(usedspace < SHA256_BLOCK_LENGTH) { |
547 | 0 | MEMSET_BZERO(&context->buffer[usedspace], |
548 | 0 | SHA256_BLOCK_LENGTH - usedspace); |
549 | 0 | } |
550 | | /* Do second-to-last transform: */ |
551 | 0 | SHA256_Transform(context, (sha2_word32 *)context->buffer); |
552 | | |
553 | | /* And set-up for the last transform: */ |
554 | 0 | MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH); |
555 | 0 | } |
556 | 0 | } else { |
557 | | /* Set-up for the last transform: */ |
558 | 0 | MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH); |
559 | | |
560 | | /* Begin padding with a 1 bit: */ |
561 | 0 | *context->buffer = 0x80; |
562 | 0 | } |
563 | | /* Set the bit count: */ |
564 | 0 | MEMCPY_BCOPY(&(context->buffer[SHA256_SHORT_BLOCK_LENGTH]), |
565 | 0 | &(context->bitcount), sizeof(sha2_word64)); |
566 | | |
567 | | /* Final transform: */ |
568 | 0 | SHA256_Transform(context, (sha2_word32 *)context->buffer); |
569 | |
|
570 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
571 | 0 | { |
572 | | /* Convert TO host byte order */ |
573 | 0 | int j; |
574 | 0 | for(j = 0; j < 8; j++) { |
575 | 0 | REVERSE32(context->state[j], context->state[j]); |
576 | 0 | *d++ = context->state[j]; |
577 | 0 | } |
578 | 0 | } |
579 | | #else |
580 | | MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH); |
581 | | #endif |
582 | 0 | } |
583 | | |
584 | | /* Clean up state data: */ |
585 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
586 | 0 | usedspace = 0; |
587 | 0 | } |
588 | | |
589 | | char *sr_SHA256_End( |
590 | | SHA256_CTX *context, char buffer[SHA256_DIGEST_STRING_LENGTH]) |
591 | 0 | { |
592 | 0 | sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest; |
593 | 0 | int i; |
594 | | |
595 | | /* Sanity check: */ |
596 | 0 | assert(context != (SHA256_CTX *)0); |
597 | | |
598 | 0 | if(buffer != (char *)0) { |
599 | 0 | sr_SHA256_Final(digest, context); |
600 | |
|
601 | 0 | for(i = 0; i < SHA256_DIGEST_LENGTH; i++) { |
602 | 0 | *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; |
603 | 0 | *buffer++ = sha2_hex_digits[*d & 0x0f]; |
604 | 0 | d++; |
605 | 0 | } |
606 | 0 | *buffer = (char)0; |
607 | 0 | } else { |
608 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
609 | 0 | } |
610 | 0 | MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH); |
611 | 0 | return buffer; |
612 | 0 | } |
613 | | |
614 | | char *sr_SHA256_Data(const sha2_byte *data, size_t len, |
615 | | char digest[SHA256_DIGEST_STRING_LENGTH]) |
616 | 0 | { |
617 | 0 | SHA256_CTX context; |
618 | |
|
619 | 0 | sr_SHA256_Init(&context); |
620 | 0 | sr_SHA256_Update(&context, data, len); |
621 | 0 | return sr_SHA256_End(&context, digest); |
622 | 0 | } |
623 | | |
624 | | |
625 | | /*** SHA-512 SHA-512/256: *********************************************************/ |
626 | | void sr_SHA512_Init(SHA512_CTX *context) |
627 | 0 | { |
628 | 0 | if(context == (SHA512_CTX *)0) { |
629 | 0 | return; |
630 | 0 | } |
631 | 0 | MEMCPY_BCOPY( |
632 | 0 | context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); |
633 | 0 | MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH); |
634 | 0 | context->bitcount[0] = context->bitcount[1] = 0; |
635 | 0 | } |
636 | | |
637 | | void sr_SHA512_256_Init(SHA512_CTX *context) |
638 | 0 | { |
639 | 0 | if(context == (SHA512_CTX *)0) { |
640 | 0 | return; |
641 | 0 | } |
642 | 0 | MEMCPY_BCOPY(context->state, sha512_256_initial_hash_value, |
643 | 0 | SHA512_DIGEST_LENGTH); |
644 | 0 | MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH); |
645 | 0 | context->bitcount[0] = context->bitcount[1] = 0; |
646 | 0 | } |
647 | | |
648 | | #ifdef SHA2_UNROLL_TRANSFORM |
649 | | |
650 | | /* Unrolled SHA-512 round macros: */ |
651 | | #if BYTE_ORDER == LITTLE_ENDIAN |
652 | | |
653 | | #define ROUND512_0_TO_15(a, b, c, d, e, f, g, h) \ |
654 | | REVERSE64(*data++, W512[j]); \ |
655 | | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + W512[j]; \ |
656 | | (d) += T1, (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), j++ |
657 | | |
658 | | |
659 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
660 | | |
661 | | #define ROUND512_0_TO_15(a, b, c, d, e, f, g, h) \ |
662 | | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] \ |
663 | | + (W512[j] = *data++); \ |
664 | | (d) += T1; \ |
665 | | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ |
666 | | j++ |
667 | | |
668 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
669 | | |
670 | | #define ROUND512(a, b, c, d, e, f, g, h) \ |
671 | | s0 = W512[(j + 1) & 0x0f]; \ |
672 | | s0 = sigma0_512(s0); \ |
673 | | s1 = W512[(j + 14) & 0x0f]; \ |
674 | | s1 = sigma1_512(s1); \ |
675 | | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] \ |
676 | | + (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0); \ |
677 | | (d) += T1; \ |
678 | | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ |
679 | | j++ |
680 | | |
681 | | void SHA512_Transform(SHA512_CTX *context, const sha2_word64 *data) |
682 | | { |
683 | | sha2_word64 a, b, c, d, e, f, g, h, s0, s1; |
684 | | sha2_word64 T1, *W512 = (sha2_word64 *)context->buffer; |
685 | | int j; |
686 | | |
687 | | /* Initialize registers with the prev. intermediate value */ |
688 | | a = context->state[0]; |
689 | | b = context->state[1]; |
690 | | c = context->state[2]; |
691 | | d = context->state[3]; |
692 | | e = context->state[4]; |
693 | | f = context->state[5]; |
694 | | g = context->state[6]; |
695 | | h = context->state[7]; |
696 | | |
697 | | j = 0; |
698 | | do { |
699 | | ROUND512_0_TO_15(a, b, c, d, e, f, g, h); |
700 | | ROUND512_0_TO_15(h, a, b, c, d, e, f, g); |
701 | | ROUND512_0_TO_15(g, h, a, b, c, d, e, f); |
702 | | ROUND512_0_TO_15(f, g, h, a, b, c, d, e); |
703 | | ROUND512_0_TO_15(e, f, g, h, a, b, c, d); |
704 | | ROUND512_0_TO_15(d, e, f, g, h, a, b, c); |
705 | | ROUND512_0_TO_15(c, d, e, f, g, h, a, b); |
706 | | ROUND512_0_TO_15(b, c, d, e, f, g, h, a); |
707 | | } while(j < 16); |
708 | | |
709 | | /* Now for the remaining rounds up to 79: */ |
710 | | do { |
711 | | ROUND512(a, b, c, d, e, f, g, h); |
712 | | ROUND512(h, a, b, c, d, e, f, g); |
713 | | ROUND512(g, h, a, b, c, d, e, f); |
714 | | ROUND512(f, g, h, a, b, c, d, e); |
715 | | ROUND512(e, f, g, h, a, b, c, d); |
716 | | ROUND512(d, e, f, g, h, a, b, c); |
717 | | ROUND512(c, d, e, f, g, h, a, b); |
718 | | ROUND512(b, c, d, e, f, g, h, a); |
719 | | } while(j < 80); |
720 | | |
721 | | /* Compute the current intermediate hash value */ |
722 | | context->state[0] += a; |
723 | | context->state[1] += b; |
724 | | context->state[2] += c; |
725 | | context->state[3] += d; |
726 | | context->state[4] += e; |
727 | | context->state[5] += f; |
728 | | context->state[6] += g; |
729 | | context->state[7] += h; |
730 | | |
731 | | /* Clean up */ |
732 | | a = b = c = d = e = f = g = h = T1 = 0; |
733 | | } |
734 | | |
735 | | #else /* SHA2_UNROLL_TRANSFORM */ |
736 | | |
737 | | void SHA512_Transform(SHA512_CTX *context, const sha2_word64 *data) |
738 | 0 | { |
739 | 0 | sha2_word64 a, b, c, d, e, f, g, h, s0, s1; |
740 | 0 | sha2_word64 T1, T2, *W512 = (sha2_word64 *)context->buffer; |
741 | 0 | int j; |
742 | | |
743 | | /* Initialize registers with the prev. intermediate value */ |
744 | 0 | a = context->state[0]; |
745 | 0 | b = context->state[1]; |
746 | 0 | c = context->state[2]; |
747 | 0 | d = context->state[3]; |
748 | 0 | e = context->state[4]; |
749 | 0 | f = context->state[5]; |
750 | 0 | g = context->state[6]; |
751 | 0 | h = context->state[7]; |
752 | |
|
753 | 0 | j = 0; |
754 | 0 | do { |
755 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
756 | | /* Convert TO host byte order */ |
757 | 0 | REVERSE64(*data++, W512[j]); |
758 | | /* Apply the SHA-512 compression function to update a..h */ |
759 | 0 | T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j]; |
760 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
761 | | /* Apply the SHA-512 compression function to update a..h with copy */ |
762 | | T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++); |
763 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
764 | 0 | T2 = Sigma0_512(a) + Maj(a, b, c); |
765 | 0 | h = g; |
766 | 0 | g = f; |
767 | 0 | f = e; |
768 | 0 | e = d + T1; |
769 | 0 | d = c; |
770 | 0 | c = b; |
771 | 0 | b = a; |
772 | 0 | a = T1 + T2; |
773 | |
|
774 | 0 | j++; |
775 | 0 | } while(j < 16); |
776 | |
|
777 | 0 | do { |
778 | | /* Part of the message block expansion: */ |
779 | 0 | s0 = W512[(j + 1) & 0x0f]; |
780 | 0 | s0 = sigma0_512(s0); |
781 | 0 | s1 = W512[(j + 14) & 0x0f]; |
782 | 0 | s1 = sigma1_512(s1); |
783 | | |
784 | | /* Apply the SHA-512 compression function to update a..h */ |
785 | 0 | T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] |
786 | 0 | + (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0); |
787 | 0 | T2 = Sigma0_512(a) + Maj(a, b, c); |
788 | 0 | h = g; |
789 | 0 | g = f; |
790 | 0 | f = e; |
791 | 0 | e = d + T1; |
792 | 0 | d = c; |
793 | 0 | c = b; |
794 | 0 | b = a; |
795 | 0 | a = T1 + T2; |
796 | |
|
797 | 0 | j++; |
798 | 0 | } while(j < 80); |
799 | | |
800 | | /* Compute the current intermediate hash value */ |
801 | 0 | context->state[0] += a; |
802 | 0 | context->state[1] += b; |
803 | 0 | context->state[2] += c; |
804 | 0 | context->state[3] += d; |
805 | 0 | context->state[4] += e; |
806 | 0 | context->state[5] += f; |
807 | 0 | context->state[6] += g; |
808 | 0 | context->state[7] += h; |
809 | | |
810 | | /* Clean up */ |
811 | 0 | a = b = c = d = e = f = g = h = T1 = T2 = 0; |
812 | 0 | } |
813 | | |
814 | | #endif /* SHA2_UNROLL_TRANSFORM */ |
815 | | |
816 | | void sr_SHA512_Update(SHA512_CTX *context, const sha2_byte *data, size_t len) |
817 | 0 | { |
818 | 0 | unsigned int freespace, usedspace; |
819 | |
|
820 | 0 | if(len == 0) { |
821 | | /* Calling with no data is valid - we do nothing */ |
822 | 0 | return; |
823 | 0 | } |
824 | | |
825 | | /* Sanity check: */ |
826 | 0 | assert(context != (SHA512_CTX *)0 && data != (sha2_byte *)0); |
827 | | |
828 | 0 | usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
829 | 0 | if(usedspace > 0) { |
830 | | /* Calculate how much free space is available in the buffer */ |
831 | 0 | freespace = SHA512_BLOCK_LENGTH - usedspace; |
832 | |
|
833 | 0 | if(len >= freespace) { |
834 | | /* Fill the buffer completely and process it */ |
835 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); |
836 | 0 | ADDINC128(context->bitcount, freespace << 3); |
837 | 0 | len -= freespace; |
838 | 0 | data += freespace; |
839 | 0 | SHA512_Transform(context, (sha2_word64 *)context->buffer); |
840 | 0 | } else { |
841 | | /* The buffer is not yet full */ |
842 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, len); |
843 | 0 | ADDINC128(context->bitcount, len << 3); |
844 | | /* Clean up: */ |
845 | 0 | usedspace = freespace = 0; |
846 | 0 | return; |
847 | 0 | } |
848 | 0 | } |
849 | 0 | while(len >= SHA512_BLOCK_LENGTH) { |
850 | | /* Process as many complete blocks as we can */ |
851 | 0 | SHA512_Transform(context, (sha2_word64 *)data); |
852 | 0 | ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); |
853 | 0 | len -= SHA512_BLOCK_LENGTH; |
854 | 0 | data += SHA512_BLOCK_LENGTH; |
855 | 0 | } |
856 | 0 | if(len > 0) { |
857 | | /* There's left-overs, so save 'em */ |
858 | 0 | MEMCPY_BCOPY(context->buffer, data, len); |
859 | 0 | ADDINC128(context->bitcount, len << 3); |
860 | 0 | } |
861 | | /* Clean up: */ |
862 | 0 | usedspace = freespace = 0; |
863 | 0 | } |
864 | | |
865 | | void SHA512_Last(SHA512_CTX *context) |
866 | 0 | { |
867 | 0 | unsigned int usedspace; |
868 | |
|
869 | 0 | usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
870 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
871 | | /* Convert FROM host byte order */ |
872 | 0 | REVERSE64(context->bitcount[0], context->bitcount[0]); |
873 | 0 | REVERSE64(context->bitcount[1], context->bitcount[1]); |
874 | 0 | #endif |
875 | 0 | if(usedspace > 0) { |
876 | | /* Begin padding with a 1 bit: */ |
877 | 0 | context->buffer[usedspace++] = 0x80; |
878 | |
|
879 | 0 | if(usedspace <= SHA512_SHORT_BLOCK_LENGTH) { |
880 | | /* Set-up for the last transform: */ |
881 | 0 | MEMSET_BZERO(&context->buffer[usedspace], |
882 | 0 | SHA512_SHORT_BLOCK_LENGTH - usedspace); |
883 | 0 | } else { |
884 | 0 | if(usedspace < SHA512_BLOCK_LENGTH) { |
885 | 0 | MEMSET_BZERO(&context->buffer[usedspace], |
886 | 0 | SHA512_BLOCK_LENGTH - usedspace); |
887 | 0 | } |
888 | | /* Do second-to-last transform: */ |
889 | 0 | SHA512_Transform(context, (sha2_word64 *)context->buffer); |
890 | | |
891 | | /* And set-up for the last transform: */ |
892 | 0 | MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2); |
893 | 0 | } |
894 | 0 | } else { |
895 | | /* Prepare for final transform: */ |
896 | 0 | MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH); |
897 | | |
898 | | /* Begin padding with a 1 bit: */ |
899 | 0 | *context->buffer = 0x80; |
900 | 0 | } |
901 | | /* Store the length of input data (in bits): */ |
902 | 0 | MEMCPY_BCOPY(&(context->buffer[SHA512_SHORT_BLOCK_LENGTH + 0]), |
903 | 0 | &(context->bitcount[1]), sizeof(sha2_word64)); |
904 | 0 | MEMCPY_BCOPY(&(context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8]), |
905 | 0 | &(context->bitcount[0]), sizeof(sha2_word64)); |
906 | | |
907 | | /* Final transform: */ |
908 | 0 | SHA512_Transform(context, (sha2_word64 *)context->buffer); |
909 | 0 | } |
910 | | |
911 | | void sr_SHA512_Final( |
912 | | sha2_byte digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context) |
913 | 0 | { |
914 | 0 | sha2_word64 *d = (sha2_word64 *)digest; |
915 | | |
916 | | /* Sanity check: */ |
917 | 0 | assert(context != (SHA512_CTX *)0); |
918 | | |
919 | | /* If no digest buffer is passed, we don't bother doing this: */ |
920 | 0 | if(digest != (sha2_byte *)0) { |
921 | 0 | SHA512_Last(context); |
922 | | |
923 | | /* Save the hash data for output: */ |
924 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
925 | 0 | { |
926 | | /* Convert TO host byte order */ |
927 | 0 | int j; |
928 | 0 | for(j = 0; j < 8; j++) { |
929 | 0 | REVERSE64(context->state[j], context->state[j]); |
930 | 0 | *d++ = context->state[j]; |
931 | 0 | } |
932 | 0 | } |
933 | | #else |
934 | | MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH); |
935 | | #endif |
936 | 0 | } |
937 | | |
938 | | /* Zero out state data */ |
939 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
940 | 0 | } |
941 | | |
942 | | char *sr_SHA512_End( |
943 | | SHA512_CTX *context, char buffer[SHA512_DIGEST_STRING_LENGTH]) |
944 | 0 | { |
945 | 0 | sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest; |
946 | 0 | int i; |
947 | | |
948 | | /* Sanity check: */ |
949 | 0 | assert(context != (SHA512_CTX *)0); |
950 | | |
951 | 0 | if(buffer != (char *)0) { |
952 | 0 | sr_SHA512_Final(digest, context); |
953 | |
|
954 | 0 | for(i = 0; i < SHA512_DIGEST_LENGTH; i++) { |
955 | 0 | *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; |
956 | 0 | *buffer++ = sha2_hex_digits[*d & 0x0f]; |
957 | 0 | d++; |
958 | 0 | } |
959 | 0 | *buffer = (char)0; |
960 | 0 | } else { |
961 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
962 | 0 | } |
963 | 0 | MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH); |
964 | 0 | return buffer; |
965 | 0 | } |
966 | | |
967 | | char *sr_SHA512_Data(const sha2_byte *data, size_t len, |
968 | | char digest[SHA512_DIGEST_STRING_LENGTH]) |
969 | 0 | { |
970 | 0 | SHA512_CTX context; |
971 | |
|
972 | 0 | sr_SHA512_Init(&context); |
973 | 0 | sr_SHA512_Update(&context, data, len); |
974 | 0 | return sr_SHA512_End(&context, digest); |
975 | 0 | } |
976 | | |
977 | | |
978 | | /*** SHA-384: *********************************************************/ |
979 | | void sr_SHA384_Init(SHA384_CTX *context) |
980 | 0 | { |
981 | 0 | if(context == (SHA384_CTX *)0) { |
982 | 0 | return; |
983 | 0 | } |
984 | 0 | MEMCPY_BCOPY( |
985 | 0 | context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH); |
986 | 0 | MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH); |
987 | 0 | context->bitcount[0] = context->bitcount[1] = 0; |
988 | 0 | } |
989 | | |
990 | | void sr_SHA384_Update(SHA384_CTX *context, const sha2_byte *data, size_t len) |
991 | 0 | { |
992 | 0 | sr_SHA512_Update((SHA512_CTX *)context, data, len); |
993 | 0 | } |
994 | | |
995 | | void sr_SHA384_Final( |
996 | | sha2_byte digest[SHA384_DIGEST_LENGTH], SHA384_CTX *context) |
997 | 0 | { |
998 | 0 | sha2_word64 *d = (sha2_word64 *)digest; |
999 | | |
1000 | | /* Sanity check: */ |
1001 | 0 | assert(context != (SHA384_CTX *)0); |
1002 | | |
1003 | | /* If no digest buffer is passed, we don't bother doing this: */ |
1004 | 0 | if(digest != (sha2_byte *)0) { |
1005 | 0 | SHA512_Last((SHA512_CTX *)context); |
1006 | | |
1007 | | /* Save the hash data for output: */ |
1008 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
1009 | 0 | { |
1010 | | /* Convert TO host byte order */ |
1011 | 0 | int j; |
1012 | 0 | for(j = 0; j < 6; j++) { |
1013 | 0 | REVERSE64(context->state[j], context->state[j]); |
1014 | 0 | *d++ = context->state[j]; |
1015 | 0 | } |
1016 | 0 | } |
1017 | | #else |
1018 | | MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH); |
1019 | | #endif |
1020 | 0 | } |
1021 | | |
1022 | | /* Zero out state data */ |
1023 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
1024 | 0 | } |
1025 | | |
1026 | | char *sr_SHA384_End( |
1027 | | SHA384_CTX *context, char buffer[SHA384_DIGEST_STRING_LENGTH]) |
1028 | 0 | { |
1029 | 0 | sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest; |
1030 | 0 | int i; |
1031 | | |
1032 | | /* Sanity check: */ |
1033 | 0 | assert(context != (SHA384_CTX *)0); |
1034 | | |
1035 | 0 | if(buffer != (char *)0) { |
1036 | 0 | sr_SHA384_Final(digest, context); |
1037 | |
|
1038 | 0 | for(i = 0; i < SHA384_DIGEST_LENGTH; i++) { |
1039 | 0 | *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; |
1040 | 0 | *buffer++ = sha2_hex_digits[*d & 0x0f]; |
1041 | 0 | d++; |
1042 | 0 | } |
1043 | 0 | *buffer = (char)0; |
1044 | 0 | } else { |
1045 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
1046 | 0 | } |
1047 | 0 | MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH); |
1048 | 0 | return buffer; |
1049 | 0 | } |
1050 | | |
1051 | | char *sr_SHA384_Data(const sha2_byte *data, size_t len, |
1052 | | char digest[SHA384_DIGEST_STRING_LENGTH]) |
1053 | 0 | { |
1054 | 0 | SHA384_CTX context; |
1055 | |
|
1056 | 0 | sr_SHA384_Init(&context); |
1057 | 0 | sr_SHA384_Update(&context, data, len); |
1058 | 0 | return sr_SHA384_End(&context, digest); |
1059 | 0 | } |