Coverage Report

Created: 2025-06-10 06:56

/src/ghostpdl/base/sha2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * FILE:  sha2.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 with 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
 * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
33
 */
34
35
#include "std.h"
36
#include "string_.h"
37
#include "sha2.h"
38
39
/*
40
 * Disable asserts for now -- they're all just null pointer checks
41
 * anyway, and this way we don't require <assert.h>.
42
 */
43
5.48k
#define assert(x) (void)0
44
45
/*
46
 * UNROLLED TRANSFORM LOOP NOTE:
47
 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
48
 * loop version for the hash transform rounds (defined using macros
49
 * later in this file).  Either define on the command line, for example:
50
 *
51
 *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
52
 *
53
 * or define below:
54
 *
55
 *   #define SHA2_UNROLL_TRANSFORM
56
 *
57
 */
58
59
/*** SHA-256/384/512 Machine Architecture Definitions *****************/
60
/*
61
 * BYTE_ORDER NOTE:
62
 *
63
 * Please make sure that your system defines BYTE_ORDER.  If your
64
 * architecture is little-endian, make sure it also defines
65
 * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
66
 * equivilent.
67
 *
68
 * If your system does not define the above, then you can do so by
69
 * hand like this:
70
 *
71
 *   #define LITTLE_ENDIAN 1234
72
 *   #define BIG_ENDIAN    4321
73
 *
74
 * And for little-endian machines, add:
75
 *
76
 *   #define BYTE_ORDER LITTLE_ENDIAN
77
 *
78
 * Or for big-endian machines:
79
 *
80
 *   #define BYTE_ORDER BIG_ENDIAN
81
 *
82
 * The FreeBSD machine this was written on defines BYTE_ORDER
83
 * appropriately by including <sys/types.h> (which in turn includes
84
 * <machine/endian.h> where the appropriate definitions are actually
85
 * made).
86
 */
87
88
/*
89
 * Use ghostscript's ARCH_IS_BIG_ENDIAN macro to define BYTE_ORDER
90
 * the way this code expects it.
91
 */
92
#undef BYTE_ORDER
93
#undef LITTLE_ENDIAN
94
#undef BIG_ENDIAN
95
96
#define LITTLE_ENDIAN 1234
97
#define BIG_ENDIAN 4321
98
99
#if ARCH_IS_BIG_ENDIAN
100
#  define BYTE_ORDER BIG_ENDIAN
101
#else
102
#  define BYTE_ORDER LITTLE_ENDIAN
103
#endif
104
105
#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
106
#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
107
#endif
108
109
/*
110
 * Define the followingsha2_* types to types of the correct length on
111
 * the native archtecture.   Most BSD systems and Linux define u_intXX_t
112
 * types.  Machines with very recent ANSI C headers, can use the
113
 * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
114
 * during compile or in the sha.h header file.
115
 *
116
 * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
117
 * will need to define these three typedefs below (and the appropriate
118
 * ones in sha.h too) by hand according to their system architecture.
119
 *
120
 * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
121
 * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
122
 */
123
#ifdef SHA2_USE_INTTYPES_H
124
125
typedef uint8_t  sha2_byte; /* Exactly 1 byte */
126
typedef uint32_t sha2_word32; /* Exactly 4 bytes */
127
typedef uint64_t sha2_word64; /* Exactly 8 bytes */
128
129
#else /* SHA2_USE_INTTYPES_H */
130
131
typedef u_int8_t  sha2_byte;  /* Exactly 1 byte */
132
typedef u_int32_t sha2_word32;  /* Exactly 4 bytes */
133
typedef u_int64_t sha2_word64;  /* Exactly 8 bytes */
134
135
#endif /* SHA2_USE_INTTYPES_H */
136
137
/* Microsoft/Borland/Intel have their own 64-bit siffix */
138
#if defined(_MSC_VER) || defined( __BORLANDC__)
139
#  define ULL(x) x##ui64
140
#else
141
3.55M
#  define ULL(x) x##ULL
142
#endif
143
144
/*** SHA-256/384/512 Various Length Definitions ***********************/
145
/* NOTE: Most of these are in sha2.h */
146
1.06k
#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
147
#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
148
3.42k
#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
149
150
/*** ENDIAN REVERSAL MACROS *******************************************/
151
#if BYTE_ORDER == LITTLE_ENDIAN
152
971k
#define REVERSE32(w,x)  { \
153
971k
        sha2_word32 tmp = (w); \
154
971k
        tmp = (tmp >> 16) | (tmp << 16); \
155
971k
        (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
156
971k
}
157
889k
#define REVERSE64(w,x)  { \
158
889k
        sha2_word64 tmp = (w); \
159
889k
        tmp = (tmp >> 32) | (tmp << 32); \
160
889k
        tmp = ((tmp & ULL(0xff00ff00ff00ff00)) >> 8) | \
161
889k
              ((tmp & ULL(0x00ff00ff00ff00ff)) << 8); \
162
889k
        (x) = ((tmp & ULL(0xffff0000ffff0000)) >> 16) | \
163
889k
              ((tmp & ULL(0x0000ffff0000ffff)) << 16); \
164
889k
}
165
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
166
167
/*
168
 * Macro for incrementally adding the unsigned 64-bit integer n to the
169
 * unsigned 128-bit integer (represented using a two-element array of
170
 * 64-bit words):
171
 */
172
52.8k
#define ADDINC128(w,n)  { \
173
52.8k
        (w)[0] += (sha2_word64)(n); \
174
52.8k
        if ((w)[0] < (n)) { \
175
0
                (w)[1]++; \
176
0
        } \
177
52.8k
}
178
179
/*
180
 * Macros for copying blocks of memory and for zeroing out ranges
181
 * of memory.  Using these macros makes it easy to switch from
182
 * using memset()/memcpy() and using bzero()/bcopy().
183
 *
184
 * Please define either SHA2_USE_MEMSET_MEMCPY or define
185
 * SHA2_USE_BZERO_BCOPY depending on which function set you
186
 * choose to use:
187
 */
188
#if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
189
/* Default to memset()/memcpy() if no option is specified */
190
#define SHA2_USE_MEMSET_MEMCPY  1
191
#endif
192
#if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
193
/* Abort with an error if BOTH options are defined */
194
#error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
195
#endif
196
197
#ifdef SHA2_USE_MEMSET_MEMCPY
198
8.21k
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
199
2.78k
#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
200
#endif
201
#ifdef SHA2_USE_BZERO_BCOPY
202
#define MEMSET_BZERO(p,l) bzero((p), (l))
203
#define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
204
#endif
205
206
/*** THE SIX LOGICAL FUNCTIONS ****************************************/
207
/*
208
 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
209
 *
210
 *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
211
 *   S is a ROTATION) because the SHA-256/384/512 description document
212
 *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
213
 *   same "backwards" definition.
214
 */
215
/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
216
12.7M
#define R(b,x)    ((x) >> (b))
217
/* 32-bit Rotate-right (used in SHA-256): */
218
34.6M
#define S32(b,x)  (((x) >> (b)) | ((x) << (32 - (b))))
219
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
220
40.1M
#define S64(b,x)  (((x) >> (b)) | ((x) << (64 - (b))))
221
222
/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
223
8.22M
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
224
8.22M
#define Maj(x,y,z)  (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
225
226
/* Four of six logical functions used in SHA-256: */
227
3.85M
#define Sigma0_256(x) (S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
228
3.85M
#define Sigma1_256(x) (S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
229
2.89M
#define sigma0_256(x) (S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
230
2.89M
#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
231
232
/* Four of six logical functions used in SHA-384 and SHA-512: */
233
4.36M
#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
234
4.36M
#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
235
3.49M
#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
236
3.49M
#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
237
238
/*** INTERNAL FUNCTION PROTOTYPES *************************************/
239
/* NOTE: These should not be accessed directly from outside this
240
 * library -- they are intended for private internal visibility/use
241
 * only.
242
 */
243
void pSHA512_Last(SHA512_CTX*);
244
void pSHA256_Transform(SHA256_CTX*, const sha2_word32*);
245
void pSHA512_Transform(SHA512_CTX*, const sha2_word64*);
246
247
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
248
/* Hash constant words K for SHA-256: */
249
const static sha2_word32 K256[64] = {
250
        0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
251
        0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
252
        0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
253
        0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
254
        0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
255
        0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
256
        0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
257
        0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
258
        0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
259
        0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
260
        0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
261
        0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
262
        0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
263
        0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
264
        0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
265
        0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
266
};
267
268
/* Initial hash value H for SHA-256: */
269
const static sha2_word32 sha256_initial_hash_value[8] = {
270
        0x6a09e667UL,
271
        0xbb67ae85UL,
272
        0x3c6ef372UL,
273
        0xa54ff53aUL,
274
        0x510e527fUL,
275
        0x9b05688cUL,
276
        0x1f83d9abUL,
277
        0x5be0cd19UL
278
};
279
280
/* Hash constant words K for SHA-384 and SHA-512: */
281
const static sha2_word64 K512[80] = {
282
        ULL(0x428a2f98d728ae22), ULL(0x7137449123ef65cd),
283
        ULL(0xb5c0fbcfec4d3b2f), ULL(0xe9b5dba58189dbbc),
284
        ULL(0x3956c25bf348b538), ULL(0x59f111f1b605d019),
285
        ULL(0x923f82a4af194f9b), ULL(0xab1c5ed5da6d8118),
286
        ULL(0xd807aa98a3030242), ULL(0x12835b0145706fbe),
287
        ULL(0x243185be4ee4b28c), ULL(0x550c7dc3d5ffb4e2),
288
        ULL(0x72be5d74f27b896f), ULL(0x80deb1fe3b1696b1),
289
        ULL(0x9bdc06a725c71235), ULL(0xc19bf174cf692694),
290
        ULL(0xe49b69c19ef14ad2), ULL(0xefbe4786384f25e3),
291
        ULL(0x0fc19dc68b8cd5b5), ULL(0x240ca1cc77ac9c65),
292
        ULL(0x2de92c6f592b0275), ULL(0x4a7484aa6ea6e483),
293
        ULL(0x5cb0a9dcbd41fbd4), ULL(0x76f988da831153b5),
294
        ULL(0x983e5152ee66dfab), ULL(0xa831c66d2db43210),
295
        ULL(0xb00327c898fb213f), ULL(0xbf597fc7beef0ee4),
296
        ULL(0xc6e00bf33da88fc2), ULL(0xd5a79147930aa725),
297
        ULL(0x06ca6351e003826f), ULL(0x142929670a0e6e70),
298
        ULL(0x27b70a8546d22ffc), ULL(0x2e1b21385c26c926),
299
        ULL(0x4d2c6dfc5ac42aed), ULL(0x53380d139d95b3df),
300
        ULL(0x650a73548baf63de), ULL(0x766a0abb3c77b2a8),
301
        ULL(0x81c2c92e47edaee6), ULL(0x92722c851482353b),
302
        ULL(0xa2bfe8a14cf10364), ULL(0xa81a664bbc423001),
303
        ULL(0xc24b8b70d0f89791), ULL(0xc76c51a30654be30),
304
        ULL(0xd192e819d6ef5218), ULL(0xd69906245565a910),
305
        ULL(0xf40e35855771202a), ULL(0x106aa07032bbd1b8),
306
        ULL(0x19a4c116b8d2d0c8), ULL(0x1e376c085141ab53),
307
        ULL(0x2748774cdf8eeb99), ULL(0x34b0bcb5e19b48a8),
308
        ULL(0x391c0cb3c5c95a63), ULL(0x4ed8aa4ae3418acb),
309
        ULL(0x5b9cca4f7763e373), ULL(0x682e6ff3d6b2b8a3),
310
        ULL(0x748f82ee5defb2fc), ULL(0x78a5636f43172f60),
311
        ULL(0x84c87814a1f0ab72), ULL(0x8cc702081a6439ec),
312
        ULL(0x90befffa23631e28), ULL(0xa4506cebde82bde9),
313
        ULL(0xbef9a3f7b2c67915), ULL(0xc67178f2e372532b),
314
        ULL(0xca273eceea26619c), ULL(0xd186b8c721c0c207),
315
        ULL(0xeada7dd6cde0eb1e), ULL(0xf57d4f7fee6ed178),
316
        ULL(0x06f067aa72176fba), ULL(0x0a637dc5a2c898a6),
317
        ULL(0x113f9804bef90dae), ULL(0x1b710b35131c471b),
318
        ULL(0x28db77f523047d84), ULL(0x32caab7b40c72493),
319
        ULL(0x3c9ebe0a15c9bebc), ULL(0x431d67c49c100d4c),
320
        ULL(0x4cc5d4becb3e42b6), ULL(0x597f299cfc657e2a),
321
        ULL(0x5fcb6fab3ad6faec), ULL(0x6c44198c4a475817)
322
};
323
324
/* Initial hash value H for SHA-384 */
325
const static sha2_word64 sha384_initial_hash_value[8] = {
326
        ULL(0xcbbb9d5dc1059ed8),
327
        ULL(0x629a292a367cd507),
328
        ULL(0x9159015a3070dd17),
329
        ULL(0x152fecd8f70e5939),
330
        ULL(0x67332667ffc00b31),
331
        ULL(0x8eb44a8768581511),
332
        ULL(0xdb0c2e0d64f98fa7),
333
        ULL(0x47b5481dbefa4fa4)
334
};
335
336
/* Initial hash value H for SHA-512 */
337
const static sha2_word64 sha512_initial_hash_value[8] = {
338
        ULL(0x6a09e667f3bcc908),
339
        ULL(0xbb67ae8584caa73b),
340
        ULL(0x3c6ef372fe94f82b),
341
        ULL(0xa54ff53a5f1d36f1),
342
        ULL(0x510e527fade682d1),
343
        ULL(0x9b05688c2b3e6c1f),
344
        ULL(0x1f83d9abfb41bd6b),
345
        ULL(0x5be0cd19137e2179)
346
};
347
348
/*
349
 * Constant used by SHA256/384/512_End() functions for converting the
350
 * digest to a readable hexadecimal character string:
351
 */
352
static const char *sha2_hex_digits = "0123456789abcdef";
353
354
/*** SHA-256: *********************************************************/
355
1.02k
void pSHA256_Init(SHA256_CTX* context) {
356
1.02k
        if (context == (SHA256_CTX*)0) {
357
0
                return;
358
0
        }
359
1.02k
        MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
360
1.02k
        MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
361
1.02k
        context->bitcount = 0;
362
1.02k
}
363
364
#ifdef SHA2_UNROLL_TRANSFORM
365
366
/* Unrolled SHA-256 round macros: */
367
368
#if BYTE_ORDER == LITTLE_ENDIAN
369
370
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
371
        REVERSE32(*data++, W256[j]); \
372
        T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
373
             K256[j] + W256[j]; \
374
        (d) += T1; \
375
        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
376
        j++
377
378
#else /* BYTE_ORDER == LITTLE_ENDIAN */
379
380
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
381
        T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
382
             K256[j] + (W256[j] = *data++); \
383
        (d) += T1; \
384
        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
385
        j++
386
387
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
388
389
#define ROUND256(a,b,c,d,e,f,g,h) \
390
        s0 = W256[(j+1)&0x0f]; \
391
        s0 = sigma0_256(s0); \
392
        s1 = W256[(j+14)&0x0f]; \
393
        s1 = sigma1_256(s1); \
394
        T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
395
             (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
396
        (d) += T1; \
397
        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
398
        j++
399
400
void pSHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
401
        sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
402
        sha2_word32 T1, *W256;
403
        int   j;
404
405
        W256 = (sha2_word32*)context->buffer;
406
407
        /* Initialize registers with the prev. intermediate value */
408
        a = context->state[0];
409
        b = context->state[1];
410
        c = context->state[2];
411
        d = context->state[3];
412
        e = context->state[4];
413
        f = context->state[5];
414
        g = context->state[6];
415
        h = context->state[7];
416
417
        j = 0;
418
        do {
419
                /* Rounds 0 to 15 (unrolled): */
420
                ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
421
                ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
422
                ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
423
                ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
424
                ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
425
                ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
426
                ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
427
                ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
428
        } while (j < 16);
429
430
        /* Now for the remaining rounds to 64: */
431
        do {
432
                ROUND256(a,b,c,d,e,f,g,h);
433
                ROUND256(h,a,b,c,d,e,f,g);
434
                ROUND256(g,h,a,b,c,d,e,f);
435
                ROUND256(f,g,h,a,b,c,d,e);
436
                ROUND256(e,f,g,h,a,b,c,d);
437
                ROUND256(d,e,f,g,h,a,b,c);
438
                ROUND256(c,d,e,f,g,h,a,b);
439
                ROUND256(b,c,d,e,f,g,h,a);
440
        } while (j < 64);
441
442
        /* Compute the current intermediate hash value */
443
        context->state[0] += a;
444
        context->state[1] += b;
445
        context->state[2] += c;
446
        context->state[3] += d;
447
        context->state[4] += e;
448
        context->state[5] += f;
449
        context->state[6] += g;
450
        context->state[7] += h;
451
452
        /* Clean up */
453
        a = b = c = d = e = f = g = h = T1 = 0;
454
}
455
456
#else /* SHA2_UNROLL_TRANSFORM */
457
458
60.2k
void pSHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
459
60.2k
        sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
460
60.2k
        sha2_word32 T1, T2, *W256;
461
60.2k
        int   j;
462
463
60.2k
        W256 = (sha2_word32*)context->buffer;
464
465
        /* Initialize registers with the prev. intermediate value */
466
60.2k
        a = context->state[0];
467
60.2k
        b = context->state[1];
468
60.2k
        c = context->state[2];
469
60.2k
        d = context->state[3];
470
60.2k
        e = context->state[4];
471
60.2k
        f = context->state[5];
472
60.2k
        g = context->state[6];
473
60.2k
        h = context->state[7];
474
475
60.2k
        j = 0;
476
963k
        do {
477
963k
#if BYTE_ORDER == LITTLE_ENDIAN
478
                /* Copy data while converting to host byte order */
479
963k
                REVERSE32(*data++,W256[j]);
480
                /* Apply the SHA-256 compression function to update a..h */
481
963k
                T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
482
#else /* BYTE_ORDER == LITTLE_ENDIAN */
483
                /* Apply the SHA-256 compression function to update a..h with copy */
484
                T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
485
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
486
963k
                T2 = Sigma0_256(a) + Maj(a, b, c);
487
963k
                h = g;
488
963k
                g = f;
489
963k
                f = e;
490
963k
                e = d + T1;
491
963k
                d = c;
492
963k
                c = b;
493
963k
                b = a;
494
963k
                a = T1 + T2;
495
496
963k
                j++;
497
963k
        } while (j < 16);
498
499
2.89M
        do {
500
                /* Part of the message block expansion: */
501
2.89M
                s0 = W256[(j+1)&0x0f];
502
2.89M
                s0 = sigma0_256(s0);
503
2.89M
                s1 = W256[(j+14)&0x0f];
504
2.89M
                s1 = sigma1_256(s1);
505
506
                /* Apply the SHA-256 compression function to update a..h */
507
2.89M
                T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
508
2.89M
                     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
509
2.89M
                T2 = Sigma0_256(a) + Maj(a, b, c);
510
2.89M
                h = g;
511
2.89M
                g = f;
512
2.89M
                f = e;
513
2.89M
                e = d + T1;
514
2.89M
                d = c;
515
2.89M
                c = b;
516
2.89M
                b = a;
517
2.89M
                a = T1 + T2;
518
519
2.89M
                j++;
520
2.89M
        } while (j < 64);
521
522
        /* Compute the current intermediate hash value */
523
60.2k
        context->state[0] += a;
524
60.2k
        context->state[1] += b;
525
60.2k
        context->state[2] += c;
526
60.2k
        context->state[3] += d;
527
60.2k
        context->state[4] += e;
528
60.2k
        context->state[5] += f;
529
60.2k
        context->state[6] += g;
530
60.2k
        context->state[7] += h;
531
532
        /* Clean up */
533
60.2k
        a = b = c = d = e = f = g = h = T1 = T2 = 0;
534
60.2k
}
535
536
#endif /* SHA2_UNROLL_TRANSFORM */
537
538
1.07k
void pSHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
539
1.07k
        unsigned int  freespace, usedspace;
540
541
1.07k
        if (len == 0) {
542
                /* Calling with no data is valid - we do nothing */
543
40
                return;
544
40
        }
545
546
        /* Sanity check: */
547
1.03k
        assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
548
549
1.03k
        usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
550
1.03k
        if (usedspace > 0) {
551
                /* Calculate how much free space is available in the buffer */
552
12
                freespace = SHA256_BLOCK_LENGTH - usedspace;
553
554
12
                if (len >= freespace) {
555
                        /* Fill the buffer completely and process it */
556
0
                        MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
557
0
                        context->bitcount += freespace << 3;
558
0
                        len -= freespace;
559
0
                        data += freespace;
560
0
                        pSHA256_Transform(context, (sha2_word32*)context->buffer);
561
12
                } else {
562
                        /* The buffer is not yet full */
563
12
                        MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
564
12
                        context->bitcount += len << 3;
565
                        /* Clean up: */
566
12
                        usedspace = freespace = 0;
567
12
                        return;
568
12
                }
569
12
        }
570
60.2k
        while (len >= SHA256_BLOCK_LENGTH) {
571
                /* Process as many complete blocks as we can */
572
59.2k
                pSHA256_Transform(context, (sha2_word32*)data);
573
59.2k
                context->bitcount += SHA256_BLOCK_LENGTH << 3;
574
59.2k
                len -= SHA256_BLOCK_LENGTH;
575
59.2k
                data += SHA256_BLOCK_LENGTH;
576
59.2k
        }
577
1.02k
        if (len > 0) {
578
                /* There's left-overs, so save 'em */
579
40
                MEMCPY_BCOPY(context->buffer, data, len);
580
40
                context->bitcount += len << 3;
581
40
        }
582
        /* Clean up: */
583
1.02k
        usedspace = freespace = 0;
584
1.02k
}
585
586
1.02k
void pSHA256_Final(sha2_byte digest[SHA256_DIGEST_LENGTH], SHA256_CTX* context) {
587
1.02k
        sha2_word32 *d = (sha2_word32*)digest;
588
1.02k
        unsigned int  usedspace;
589
590
        /* Sanity check: */
591
1.02k
        assert(context != (SHA256_CTX*)0);
592
593
        /* If no digest buffer is passed, we don't bother doing this: */
594
1.02k
        if (digest != (sha2_byte*)0) {
595
1.02k
                usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
596
1.02k
#if BYTE_ORDER == LITTLE_ENDIAN
597
                /* Convert FROM host byte order */
598
1.02k
                REVERSE64(context->bitcount,context->bitcount);
599
1.02k
#endif
600
1.02k
                if (usedspace > 0) {
601
                        /* Begin padding with a 1 bit: */
602
40
                        context->buffer[usedspace++] = 0x80;
603
604
40
                        if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
605
                                /* Set-up for the last transform: */
606
28
                                MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
607
28
                        } else {
608
12
                                if (usedspace < SHA256_BLOCK_LENGTH) {
609
12
                                        MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
610
12
                                }
611
                                /* Do second-to-last transform: */
612
12
                                pSHA256_Transform(context, (sha2_word32*)context->buffer);
613
614
                                /* And set-up for the last transform: */
615
12
                                MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
616
12
                        }
617
983
                } else {
618
                        /* Set-up for the last transform: */
619
983
                        MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
620
621
                        /* Begin padding with a 1 bit: */
622
983
                        *context->buffer = 0x80;
623
983
                }
624
                /* Set the bit count: */
625
1.02k
                *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
626
627
                /* Final transform: */
628
1.02k
                pSHA256_Transform(context, (sha2_word32*)context->buffer);
629
630
1.02k
#if BYTE_ORDER == LITTLE_ENDIAN
631
1.02k
                {
632
                        /* Convert TO host byte order */
633
1.02k
                        int j;
634
9.20k
                        for (j = 0; j < 8; j++) {
635
8.18k
                                REVERSE32(context->state[j],context->state[j]);
636
8.18k
                                *d++ = context->state[j];
637
8.18k
                        }
638
1.02k
                }
639
#else
640
                MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
641
#endif
642
1.02k
        }
643
644
        /* Clean up state data: */
645
1.02k
        MEMSET_BZERO(context, sizeof(*context));
646
1.02k
        usedspace = 0;
647
1.02k
}
648
649
0
char *pSHA256_End(SHA256_CTX* context, char buffer[SHA256_DIGEST_STRING_LENGTH]) {
650
0
        sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
651
0
        int   i;
652
653
        /* Sanity check: */
654
0
        assert(context != (SHA256_CTX*)0);
655
656
0
        if (buffer != (char*)0) {
657
0
                pSHA256_Final(digest, context);
658
659
0
                for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
660
0
                        *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
661
0
                        *buffer++ = sha2_hex_digits[*d & 0x0f];
662
0
                        d++;
663
0
                }
664
0
                *buffer = (char)0;
665
0
        } else {
666
0
                MEMSET_BZERO(context, sizeof(*context));
667
0
        }
668
0
        MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
669
0
        return buffer;
670
0
}
671
672
0
char* pSHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
673
0
        SHA256_CTX  context;
674
675
0
        pSHA256_Init(&context);
676
0
        pSHA256_Update(&context, data, len);
677
0
        return pSHA256_End(&context, digest);
678
0
}
679
680
/*** SHA-512: *********************************************************/
681
873
void pSHA512_Init(SHA512_CTX* context) {
682
873
        if (context == (SHA512_CTX*)0) {
683
0
                return;
684
0
        }
685
873
        MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
686
873
        MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
687
873
        context->bitcount[0] = context->bitcount[1] =  0;
688
873
}
689
690
#ifdef SHA2_UNROLL_TRANSFORM
691
692
/* Unrolled SHA-512 round macros: */
693
#if BYTE_ORDER == LITTLE_ENDIAN
694
695
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
696
        REVERSE64(*data++, W512[j]); \
697
        T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
698
             K512[j] + W512[j]; \
699
        (d) += T1, \
700
        (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
701
        j++
702
703
#else /* BYTE_ORDER == LITTLE_ENDIAN */
704
705
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
706
        T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
707
             K512[j] + (W512[j] = *data++); \
708
        (d) += T1; \
709
        (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
710
        j++
711
712
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
713
714
#define ROUND512(a,b,c,d,e,f,g,h) \
715
        s0 = W512[(j+1)&0x0f]; \
716
        s0 = sigma0_512(s0); \
717
        s1 = W512[(j+14)&0x0f]; \
718
        s1 = sigma1_512(s1); \
719
        T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
720
             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
721
        (d) += T1; \
722
        (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
723
        j++
724
725
void pSHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
726
        sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
727
        sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
728
        int   j;
729
730
        /* Initialize registers with the prev. intermediate value */
731
        a = context->state[0];
732
        b = context->state[1];
733
        c = context->state[2];
734
        d = context->state[3];
735
        e = context->state[4];
736
        f = context->state[5];
737
        g = context->state[6];
738
        h = context->state[7];
739
740
        j = 0;
741
        do {
742
                ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
743
                ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
744
                ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
745
                ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
746
                ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
747
                ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
748
                ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
749
                ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
750
        } while (j < 16);
751
752
        /* Now for the remaining rounds up to 79: */
753
        do {
754
                ROUND512(a,b,c,d,e,f,g,h);
755
                ROUND512(h,a,b,c,d,e,f,g);
756
                ROUND512(g,h,a,b,c,d,e,f);
757
                ROUND512(f,g,h,a,b,c,d,e);
758
                ROUND512(e,f,g,h,a,b,c,d);
759
                ROUND512(d,e,f,g,h,a,b,c);
760
                ROUND512(c,d,e,f,g,h,a,b);
761
                ROUND512(b,c,d,e,f,g,h,a);
762
        } while (j < 80);
763
764
        /* Compute the current intermediate hash value */
765
        context->state[0] += a;
766
        context->state[1] += b;
767
        context->state[2] += c;
768
        context->state[3] += d;
769
        context->state[4] += e;
770
        context->state[5] += f;
771
        context->state[6] += g;
772
        context->state[7] += h;
773
774
        /* Clean up */
775
        a = b = c = d = e = f = g = h = T1 = 0;
776
}
777
778
#else /* SHA2_UNROLL_TRANSFORM */
779
780
54.5k
void pSHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
781
54.5k
        sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
782
54.5k
        sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
783
54.5k
        int   j;
784
785
        /* Initialize registers with the prev. intermediate value */
786
54.5k
        a = context->state[0];
787
54.5k
        b = context->state[1];
788
54.5k
        c = context->state[2];
789
54.5k
        d = context->state[3];
790
54.5k
        e = context->state[4];
791
54.5k
        f = context->state[5];
792
54.5k
        g = context->state[6];
793
54.5k
        h = context->state[7];
794
795
54.5k
        j = 0;
796
873k
        do {
797
873k
#if BYTE_ORDER == LITTLE_ENDIAN
798
                /* Convert TO host byte order */
799
873k
                REVERSE64(*data++, W512[j]);
800
                /* Apply the SHA-512 compression function to update a..h */
801
873k
                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
802
#else /* BYTE_ORDER == LITTLE_ENDIAN */
803
                /* Apply the SHA-512 compression function to update a..h with copy */
804
                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
805
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
806
873k
                T2 = Sigma0_512(a) + Maj(a, b, c);
807
873k
                h = g;
808
873k
                g = f;
809
873k
                f = e;
810
873k
                e = d + T1;
811
873k
                d = c;
812
873k
                c = b;
813
873k
                b = a;
814
873k
                a = T1 + T2;
815
816
873k
                j++;
817
873k
        } while (j < 16);
818
819
3.49M
        do {
820
                /* Part of the message block expansion: */
821
3.49M
                s0 = W512[(j+1)&0x0f];
822
3.49M
                s0 = sigma0_512(s0);
823
3.49M
                s1 = W512[(j+14)&0x0f];
824
3.49M
                s1 =  sigma1_512(s1);
825
826
                /* Apply the SHA-512 compression function to update a..h */
827
3.49M
                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
828
3.49M
                     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
829
3.49M
                T2 = Sigma0_512(a) + Maj(a, b, c);
830
3.49M
                h = g;
831
3.49M
                g = f;
832
3.49M
                f = e;
833
3.49M
                e = d + T1;
834
3.49M
                d = c;
835
3.49M
                c = b;
836
3.49M
                b = a;
837
3.49M
                a = T1 + T2;
838
839
3.49M
                j++;
840
3.49M
        } while (j < 80);
841
842
        /* Compute the current intermediate hash value */
843
54.5k
        context->state[0] += a;
844
54.5k
        context->state[1] += b;
845
54.5k
        context->state[2] += c;
846
54.5k
        context->state[3] += d;
847
54.5k
        context->state[4] += e;
848
54.5k
        context->state[5] += f;
849
54.5k
        context->state[6] += g;
850
54.5k
        context->state[7] += h;
851
852
        /* Clean up */
853
54.5k
        a = b = c = d = e = f = g = h = T1 = T2 = 0;
854
54.5k
}
855
856
#endif /* SHA2_UNROLL_TRANSFORM */
857
858
1.71k
void pSHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
859
1.71k
        unsigned int  freespace, usedspace;
860
861
1.71k
        if (len == 0) {
862
                /* Calling with no data is valid - we do nothing */
863
0
                return;
864
0
        }
865
866
        /* Sanity check: */
867
1.71k
        assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
868
869
1.71k
        usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
870
1.71k
        if (usedspace > 0) {
871
                /* Calculate how much free space is available in the buffer */
872
0
                freespace = SHA512_BLOCK_LENGTH - usedspace;
873
874
0
                if (len >= freespace) {
875
                        /* Fill the buffer completely and process it */
876
0
                        MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
877
0
                        ADDINC128(context->bitcount, freespace << 3);
878
0
                        len -= freespace;
879
0
                        data += freespace;
880
0
                        pSHA512_Transform(context, (sha2_word64*)context->buffer);
881
0
                } else {
882
                        /* The buffer is not yet full */
883
0
                        MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
884
0
                        ADDINC128(context->bitcount, len << 3);
885
                        /* Clean up: */
886
0
                        usedspace = freespace = 0;
887
0
                        return;
888
0
                }
889
0
        }
890
54.5k
        while (len >= SHA512_BLOCK_LENGTH) {
891
                /* Process as many complete blocks as we can */
892
52.8k
                pSHA512_Transform(context, (sha2_word64*)data);
893
52.8k
                ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
894
52.8k
                len -= SHA512_BLOCK_LENGTH;
895
52.8k
                data += SHA512_BLOCK_LENGTH;
896
52.8k
        }
897
1.71k
        if (len > 0) {
898
                /* There's left-overs, so save 'em */
899
0
                MEMCPY_BCOPY(context->buffer, data, len);
900
0
                ADDINC128(context->bitcount, len << 3);
901
0
        }
902
        /* Clean up: */
903
1.71k
        usedspace = freespace = 0;
904
1.71k
}
905
906
1.71k
void pSHA512_Last(SHA512_CTX* context) {
907
1.71k
        unsigned int  usedspace;
908
909
1.71k
        usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
910
1.71k
#if BYTE_ORDER == LITTLE_ENDIAN
911
        /* Convert FROM host byte order */
912
1.71k
        REVERSE64(context->bitcount[0],context->bitcount[0]);
913
1.71k
        REVERSE64(context->bitcount[1],context->bitcount[1]);
914
1.71k
#endif
915
1.71k
        if (usedspace > 0) {
916
                /* Begin padding with a 1 bit: */
917
0
                context->buffer[usedspace++] = 0x80;
918
919
0
                if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
920
                        /* Set-up for the last transform: */
921
0
                        MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
922
0
                } else {
923
0
                        if (usedspace < SHA512_BLOCK_LENGTH) {
924
0
                                MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
925
0
                        }
926
                        /* Do second-to-last transform: */
927
0
                        pSHA512_Transform(context, (sha2_word64*)context->buffer);
928
929
                        /* And set-up for the last transform: */
930
0
                        MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
931
0
                }
932
1.71k
        } else {
933
                /* Prepare for final transform: */
934
1.71k
                MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
935
936
                /* Begin padding with a 1 bit: */
937
1.71k
                *context->buffer = 0x80;
938
1.71k
        }
939
        /* Store the length of input data (in bits): */
940
1.71k
        *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
941
1.71k
        *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
942
943
        /* Final transform: */
944
1.71k
        pSHA512_Transform(context, (sha2_word64*)context->buffer);
945
1.71k
}
946
947
873
void pSHA512_Final(sha2_byte digest[SHA512_DIGEST_LENGTH], SHA512_CTX* context) {
948
873
        sha2_word64 *d = (sha2_word64*)digest;
949
950
        /* Sanity check: */
951
873
        assert(context != (SHA512_CTX*)0);
952
953
        /* If no digest buffer is passed, we don't bother doing this: */
954
873
        if (digest != (sha2_byte*)0) {
955
873
                pSHA512_Last(context);
956
957
                /* Save the hash data for output: */
958
873
#if BYTE_ORDER == LITTLE_ENDIAN
959
873
                {
960
                        /* Convert TO host byte order */
961
873
                        int j;
962
7.85k
                        for (j = 0; j < 8; j++) {
963
6.98k
                                REVERSE64(context->state[j],context->state[j]);
964
6.98k
                                *d++ = context->state[j];
965
6.98k
                        }
966
873
                }
967
#else
968
                MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
969
#endif
970
873
        }
971
972
        /* Zero out state data */
973
873
        MEMSET_BZERO(context, sizeof(*context));
974
873
}
975
976
0
char *pSHA512_End(SHA512_CTX* context, char buffer[SHA512_DIGEST_STRING_LENGTH]) {
977
0
        sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
978
0
        int   i;
979
980
        /* Sanity check: */
981
0
        assert(context != (SHA512_CTX*)0);
982
983
0
        if (buffer != (char*)0) {
984
0
                pSHA512_Final(digest, context);
985
986
0
                for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
987
0
                        *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
988
0
                        *buffer++ = sha2_hex_digits[*d & 0x0f];
989
0
                        d++;
990
0
                }
991
0
                *buffer = (char)0;
992
0
        } else {
993
0
                MEMSET_BZERO(context, sizeof(*context));
994
0
        }
995
0
        MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
996
0
        return buffer;
997
0
}
998
999
0
char* pSHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
1000
0
        SHA512_CTX  context;
1001
1002
0
        pSHA512_Init(&context);
1003
0
        pSHA512_Update(&context, data, len);
1004
0
        return pSHA512_End(&context, digest);
1005
0
}
1006
1007
/*** SHA-384: *********************************************************/
1008
839
void pSHA384_Init(SHA384_CTX* context) {
1009
839
        if (context == (SHA384_CTX*)0) {
1010
0
                return;
1011
0
        }
1012
839
        MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
1013
839
        MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
1014
839
        context->bitcount[0] = context->bitcount[1] = 0;
1015
839
}
1016
1017
839
void pSHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
1018
839
        pSHA512_Update((SHA512_CTX*)context, data, len);
1019
839
}
1020
1021
839
void pSHA384_Final(sha2_byte digest[SHA384_DIGEST_LENGTH], SHA384_CTX* context) {
1022
839
        sha2_word64 *d = (sha2_word64*)digest;
1023
1024
        /* Sanity check: */
1025
839
        assert(context != (SHA384_CTX*)0);
1026
1027
        /* If no digest buffer is passed, we don't bother doing this: */
1028
839
        if (digest != (sha2_byte*)0) {
1029
839
                pSHA512_Last((SHA512_CTX*)context);
1030
1031
                /* Save the hash data for output: */
1032
839
#if BYTE_ORDER == LITTLE_ENDIAN
1033
839
                {
1034
                        /* Convert TO host byte order */
1035
839
                        int j;
1036
5.87k
                        for (j = 0; j < 6; j++) {
1037
5.03k
                                REVERSE64(context->state[j],context->state[j]);
1038
5.03k
                                *d++ = context->state[j];
1039
5.03k
                        }
1040
839
                }
1041
#else
1042
                MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
1043
#endif
1044
839
        }
1045
1046
        /* Zero out state data */
1047
839
        MEMSET_BZERO(context, sizeof(*context));
1048
839
}
1049
1050
0
char *pSHA384_End(SHA384_CTX* context, char buffer[SHA384_DIGEST_STRING_LENGTH]) {
1051
0
        sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
1052
0
        int   i;
1053
1054
        /* Sanity check: */
1055
0
        assert(context != (SHA384_CTX*)0);
1056
1057
0
        if (buffer != (char*)0) {
1058
0
                pSHA384_Final(digest, context);
1059
1060
0
                for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
1061
0
                        *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
1062
0
                        *buffer++ = sha2_hex_digits[*d & 0x0f];
1063
0
                        d++;
1064
0
                }
1065
0
                *buffer = (char)0;
1066
0
        } else {
1067
0
                MEMSET_BZERO(context, sizeof(*context));
1068
0
        }
1069
0
        MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1070
0
        return buffer;
1071
0
}
1072
1073
0
char* pSHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
1074
0
        SHA384_CTX  context;
1075
1076
0
        pSHA384_Init(&context);
1077
0
        pSHA384_Update(&context, data, len);
1078
0
        return pSHA384_End(&context, digest);
1079
0
}