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