Rizin
unix-like reverse engineering framework and cli tools
sha2.c File Reference
#include <string.h>
#include "sha2.h"
#include <rz_util/rz_mem.h>

Go to the source code of this file.

Macros

#define WEAK_ALIASING   0
 
#define LITTLE_ENDIAN   1234
 
#define BIG_ENDIAN   4321
 
#define BYTE_ORDER   LITTLE_ENDIAN
 
#define SHA256_SHORT_BLOCK_LENGTH   (SHA256_BLOCK_LENGTH - 8)
 
#define SHA384_SHORT_BLOCK_LENGTH   (SHA384_BLOCK_LENGTH - 16)
 
#define SHA512_SHORT_BLOCK_LENGTH   (SHA512_BLOCK_LENGTH - 16)
 
#define REVERSE32(w, x)
 
#define REVERSE64(w, x)
 
#define ADDINC128(w, n)
 
#define R(b, x)   ((x) >> (b))
 
#define S32(b, x)   (((x) >> (b)) | ((x) << (32 - (b))))
 
#define S64(b, x)   (((x) >> (b)) | ((x) << (64 - (b))))
 
#define Ch(x, y, z)   (((x) & (y)) ^ ((~(x)) & (z)))
 
#define Maj(x, y, z)   (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
 
#define Sigma0_256(x)   (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
 
#define Sigma1_256(x)   (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
 
#define sigma0_256(x)   (S32(7, (x)) ^ S32(18, (x)) ^ R(3, (x)))
 
#define sigma1_256(x)   (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
 
#define Sigma0_512(x)   (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
 
#define Sigma1_512(x)   (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
 
#define sigma0_512(x)   (S64(1, (x)) ^ S64(8, (x)) ^ R(7, (x)))
 
#define sigma1_512(x)   (S64(19, (x)) ^ S64(61, (x)) ^ R(6, (x)))
 

Functions

void SHA512_Last (RZ_SHA512_CTX *)
 
void SHA256_Transform (RZ_SHA256_CTX *, const ut32 *)
 
void SHA512_Transform (RZ_SHA512_CTX *, const ut64 *)
 
void SHA256_Init (RZ_SHA256_CTX *context)
 
void SHA256_Update (RZ_SHA256_CTX *context, const ut8 *data, size_t len)
 
void SHA256_Final (ut8 *digest, RZ_SHA256_CTX *context)
 
char * SHA256_End (RZ_SHA256_CTX *context, char buffer[])
 
char * SHA256_Data (const ut8 *data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH])
 
void SHA512_Init (RZ_SHA512_CTX *context)
 
void SHA512_Update (RZ_SHA512_CTX *context, const ut8 *data, size_t len)
 
void SHA512_Final (ut8 digest[], RZ_SHA512_CTX *context)
 
char * SHA512_End (RZ_SHA512_CTX *context, char buffer[])
 
char * SHA512_Data (const ut8 *data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH])
 
void SHA384_Init (RZ_SHA384_CTX *context)
 
void SHA384_Update (RZ_SHA384_CTX *context, const ut8 *data, size_t len)
 
void SHA384_Final (ut8 digest[], RZ_SHA384_CTX *context)
 
char * SHA384_End (RZ_SHA384_CTX *context, char buffer[])
 
char * SHA384_Data (const ut8 *data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH])
 

Variables

static const ut32 K256 [64]
 
static const ut32 sha256_initial_hash_value [8]
 
static const ut64 K512 [80]
 
static const ut64 sha384_initial_hash_value [8]
 
static const ut64 sha512_initial_hash_value [8]
 
static const char * sha2_hex_digits = "0123456789abcdef"
 

Macro Definition Documentation

◆ ADDINC128

#define ADDINC128 (   w,
  n 
)
Value:
{ \
(w)[0] += (ut64)(n); \
if ((w)[0] < (n)) { \
(w)[1]++; \
} \
}
#define w
Definition: crypto_rc6.c:13
int n
Definition: mipsasm.c:19
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

Definition at line 128 of file sha2.c.

◆ BIG_ENDIAN

#define BIG_ENDIAN   4321

Definition at line 89 of file sha2.c.

◆ BYTE_ORDER

#define BYTE_ORDER   LITTLE_ENDIAN

Definition at line 90 of file sha2.c.

◆ Ch

#define Ch (   x,
  y,
 
)    (((x) & (y)) ^ ((~(x)) & (z)))

Definition at line 153 of file sha2.c.

◆ LITTLE_ENDIAN

#define LITTLE_ENDIAN   1234

Definition at line 88 of file sha2.c.

◆ Maj

#define Maj (   x,
  y,
 
)    (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))

Definition at line 154 of file sha2.c.

◆ R

#define R (   b,
  x 
)    ((x) >> (b))

Definition at line 146 of file sha2.c.

◆ REVERSE32

#define REVERSE32 (   w,
  x 
)
Value:
{ \
ut32 tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
}
int x
Definition: mipsasm.c:20

Definition at line 106 of file sha2.c.

◆ REVERSE64

#define REVERSE64 (   w,
  x 
)
Value:
{ \
ut64 tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
((tmp & 0x00ff00ff00ff00ffULL) << 8); \
(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
((tmp & 0x0000ffff0000ffffULL) << 16); \
}

Definition at line 112 of file sha2.c.

◆ S32

#define S32 (   b,
  x 
)    (((x) >> (b)) | ((x) << (32 - (b))))

Definition at line 148 of file sha2.c.

◆ S64

#define S64 (   b,
  x 
)    (((x) >> (b)) | ((x) << (64 - (b))))

Definition at line 150 of file sha2.c.

◆ SHA256_SHORT_BLOCK_LENGTH

#define SHA256_SHORT_BLOCK_LENGTH   (SHA256_BLOCK_LENGTH - 8)

Definition at line 100 of file sha2.c.

◆ SHA384_SHORT_BLOCK_LENGTH

#define SHA384_SHORT_BLOCK_LENGTH   (SHA384_BLOCK_LENGTH - 16)

Definition at line 101 of file sha2.c.

◆ SHA512_SHORT_BLOCK_LENGTH

#define SHA512_SHORT_BLOCK_LENGTH   (SHA512_BLOCK_LENGTH - 16)

Definition at line 102 of file sha2.c.

◆ Sigma0_256

#define Sigma0_256 (   x)    (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))

Definition at line 157 of file sha2.c.

◆ sigma0_256

#define sigma0_256 (   x)    (S32(7, (x)) ^ S32(18, (x)) ^ R(3, (x)))

Definition at line 159 of file sha2.c.

◆ Sigma0_512

#define Sigma0_512 (   x)    (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))

Definition at line 163 of file sha2.c.

◆ sigma0_512

#define sigma0_512 (   x)    (S64(1, (x)) ^ S64(8, (x)) ^ R(7, (x)))

Definition at line 165 of file sha2.c.

◆ Sigma1_256

#define Sigma1_256 (   x)    (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))

Definition at line 158 of file sha2.c.

◆ sigma1_256

#define sigma1_256 (   x)    (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))

Definition at line 160 of file sha2.c.

◆ Sigma1_512

#define Sigma1_512 (   x)    (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))

Definition at line 164 of file sha2.c.

◆ sigma1_512

#define sigma1_512 (   x)    (S64(19, (x)) ^ S64(61, (x)) ^ R(6, (x)))

Definition at line 166 of file sha2.c.

◆ WEAK_ALIASING

#define WEAK_ALIASING   0

Definition at line 42 of file sha2.c.

Function Documentation

◆ SHA256_Data()

char* SHA256_Data ( const ut8 data,
size_t  len,
char  digest[SHA256_DIGEST_STRING_LENGTH] 
)

Definition at line 597 of file sha2.c.

597  {
599 
601  SHA256_Update(&context, data, len);
602  return SHA256_End(&context, digest);
603 }
size_t len
Definition: 6502dis.c:15
void SHA256_Update(RZ_SHA256_CTX *context, const ut8 *data, size_t len)
Definition: sha2.c:462
void SHA256_Init(RZ_SHA256_CTX *context)
Definition: sha2.c:285
char * SHA256_End(RZ_SHA256_CTX *context, char buffer[])
Definition: sha2.c:574

References len, SHA256_End(), SHA256_Init(), and SHA256_Update().

◆ SHA256_End()

char* SHA256_End ( RZ_SHA256_CTX context,
char  buffer[] 
)

Definition at line 574 of file sha2.c.

574  {
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 }
lzma_index ** i
Definition: index.h:629
struct buffer buffer
#define NULL
Definition: cris-opc.c:27
uint8_t ut8
Definition: lh5801.h:11
RZ_API void rz_mem_memzero(void *, size_t)
Definition: mem.c:365
#define d(i)
Definition: sha256.c:44
static const char * sha2_hex_digits
Definition: sha2.c:282
void SHA256_Final(ut8 *digest, RZ_SHA256_CTX *context)
Definition: sha2.c:503
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:60
Definition: buffer.h:15

References d, i, NULL, rz_mem_memzero(), SHA256_DIGEST_LENGTH, SHA256_Final(), and sha2_hex_digits.

Referenced by SHA256_Data().

◆ SHA256_Final()

void SHA256_Final ( ut8 digest,
RZ_SHA256_CTX context 
)

Definition at line 503 of file sha2.c.

503  {
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 }
uint32_t ut32
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define REVERSE64(w, x)
Definition: sha2.c:112
#define REVERSE32(w, x)
Definition: sha2.c:106
#define SHA256_SHORT_BLOCK_LENGTH
Definition: sha2.c:100
void SHA256_Transform(RZ_SHA256_CTX *, const ut32 *)
Definition: sha2.c:385
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:96
zip_uint8_t buffer[BUFSIZE]

References context::buffer, d, memcpy(), memset(), p, REVERSE32, REVERSE64, SHA256_BLOCK_LENGTH, SHA256_DIGEST_LENGTH, SHA256_SHORT_BLOCK_LENGTH, SHA256_Transform(), and ut64().

Referenced by plugin_sha256_final(), plugin_sha256_small_block(), and SHA256_End().

◆ SHA256_Init()

void SHA256_Init ( RZ_SHA256_CTX context)

Definition at line 285 of file sha2.c.

285  {
286  if (context == (RZ_SHA256_CTX *)0) {
287  return;
288  }
291  context->bitcount = 0;
292 }
static const ut32 sha256_initial_hash_value[8]
Definition: sha2.c:199

References context::buffer, memcpy(), rz_mem_memzero(), SHA256_BLOCK_LENGTH, SHA256_DIGEST_LENGTH, and sha256_initial_hash_value.

Referenced by plugin_sha256_init(), plugin_sha256_small_block(), and SHA256_Data().

◆ SHA256_Transform()

void SHA256_Transform ( RZ_SHA256_CTX context,
const ut32 data 
)

Definition at line 385 of file sha2.c.

385  {
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 }
#define e(frag)
struct @667 g
#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 BYTE_ORDER
Definition: sha2.c:90
#define Sigma0_256(x)
Definition: sha2.c:157
#define Maj(x, y, z)
Definition: sha2.c:154
static const ut32 K256[64]
Definition: sha2.c:179
#define sigma1_256(x)
Definition: sha2.c:160
#define LITTLE_ENDIAN
Definition: sha2.c:88
#define Sigma1_256(x)
Definition: sha2.c:158
#define Ch(x, y, z)
Definition: sha2.c:153
#define sigma0_256(x)
Definition: sha2.c:159

References a, b, context::buffer, BYTE_ORDER, c, Ch, d, e, f, g, h, K256, LITTLE_ENDIAN, Maj, REVERSE32, s0, s1, Sigma0_256, and Sigma1_256.

Referenced by SHA256_Final(), and SHA256_Update().

◆ SHA256_Update()

void SHA256_Update ( RZ_SHA256_CTX context,
const ut8 data,
size_t  len 
)

Definition at line 462 of file sha2.c.

462  {
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 }

References context::buffer, len, memcpy(), SHA256_BLOCK_LENGTH, and SHA256_Transform().

Referenced by plugin_sha256_small_block(), plugin_sha256_update(), and SHA256_Data().

◆ SHA384_Data()

char* SHA384_Data ( const ut8 data,
size_t  len,
char  digest[SHA384_DIGEST_STRING_LENGTH] 
)

Definition at line 1007 of file sha2.c.

1007  {
1009  SHA384_Init(&context);
1010  SHA384_Update(&context, data, len);
1011  return SHA384_End(&context, digest);
1012 }
void SHA384_Init(RZ_SHA384_CTX *context)
Definition: sha2.c:938
void SHA384_Update(RZ_SHA384_CTX *context, const ut8 *data, size_t len)
Definition: sha2.c:947
char * SHA384_End(RZ_SHA384_CTX *context, char buffer[])
Definition: sha2.c:982

References len, SHA384_End(), SHA384_Init(), and SHA384_Update().

◆ SHA384_End()

char* SHA384_End ( RZ_SHA384_CTX context,
char  buffer[] 
)

Definition at line 982 of file sha2.c.

982  {
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 }
void SHA384_Final(ut8 digest[], RZ_SHA384_CTX *context)
Definition: sha2.c:951
#define SHA384_DIGEST_LENGTH
Definition: sha2.h:63

References d, i, memset(), NULL, sha2_hex_digits, SHA384_DIGEST_LENGTH, and SHA384_Final().

Referenced by SHA384_Data().

◆ SHA384_Final()

void SHA384_Final ( ut8  digest[],
RZ_SHA384_CTX context 
)

Definition at line 951 of file sha2.c.

951  {
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 }
void SHA512_Last(RZ_SHA512_CTX *)
Definition: sha2.c:823

References d, memcpy(), memset(), REVERSE64, SHA384_DIGEST_LENGTH, SHA512_Last(), and ut64().

Referenced by plugin_sha384_final(), plugin_sha384_small_block(), and SHA384_End().

◆ SHA384_Init()

void SHA384_Init ( RZ_SHA384_CTX context)

Definition at line 938 of file sha2.c.

938  {
939  if (context == (RZ_SHA384_CTX *)0) {
940  return;
941  }
944  context->bitcount[0] = context->bitcount[1] = 0;
945 }
static const ut64 sha384_initial_hash_value[8]
Definition: sha2.c:255
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:66
#define SHA384_BLOCK_LENGTH
Definition: sha2.h:103

References context::buffer, memcpy(), memset(), SHA384_BLOCK_LENGTH, sha384_initial_hash_value, and SHA512_DIGEST_LENGTH.

Referenced by plugin_sha384_init(), plugin_sha384_small_block(), and SHA384_Data().

◆ SHA384_Update()

void SHA384_Update ( RZ_SHA384_CTX context,
const ut8 data,
size_t  len 
)

Definition at line 947 of file sha2.c.

947  {
949 }
void SHA512_Update(RZ_SHA512_CTX *context, const ut8 *data, size_t len)
Definition: sha2.c:777

References len, and SHA512_Update().

Referenced by plugin_sha384_small_block(), plugin_sha384_update(), and SHA384_Data().

◆ SHA512_Data()

char* SHA512_Data ( const ut8 data,
size_t  len,
char  digest[SHA512_DIGEST_STRING_LENGTH] 
)

Definition at line 929 of file sha2.c.

929  {
931 
933  SHA512_Update(&context, data, len);
934  return SHA512_End(&context, digest);
935 }
char * SHA512_End(RZ_SHA512_CTX *context, char buffer[])
Definition: sha2.c:904
void SHA512_Init(RZ_SHA512_CTX *context)
Definition: sha2.c:606

References len, SHA512_End(), SHA512_Init(), and SHA512_Update().

◆ SHA512_End()

char* SHA512_End ( RZ_SHA512_CTX context,
char  buffer[] 
)

Definition at line 904 of file sha2.c.

904  {
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 }
void SHA512_Final(ut8 digest[], RZ_SHA512_CTX *context)
Definition: sha2.c:873

References d, i, NULL, rz_mem_memzero(), sha2_hex_digits, SHA512_DIGEST_LENGTH, and SHA512_Final().

Referenced by SHA512_Data().

◆ SHA512_Final()

void SHA512_Final ( ut8  digest[],
RZ_SHA512_CTX context 
)

Definition at line 873 of file sha2.c.

873  {
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 }

References d, memcpy(), REVERSE64, rz_mem_memzero(), SHA512_DIGEST_LENGTH, SHA512_Last(), and ut64().

Referenced by plugin_sha512_final(), plugin_sha512_small_block(), and SHA512_End().

◆ SHA512_Init()

void SHA512_Init ( RZ_SHA512_CTX context)

Definition at line 606 of file sha2.c.

606  {
607  if (context == (RZ_SHA512_CTX *)0) {
608  return;
609  }
612  context->bitcount[0] = context->bitcount[1] = 0;
613 }
static const ut64 sha512_initial_hash_value[8]
Definition: sha2.c:267
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:104

References context::buffer, memcpy(), rz_mem_memzero(), SHA512_BLOCK_LENGTH, SHA512_DIGEST_LENGTH, and sha512_initial_hash_value.

Referenced by plugin_sha512_init(), plugin_sha512_small_block(), and SHA512_Data().

◆ SHA512_Last()

void SHA512_Last ( RZ_SHA512_CTX context)

Definition at line 823 of file sha2.c.

823  {
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 }
void SHA512_Transform(RZ_SHA512_CTX *, const ut64 *)
Definition: sha2.c:702
#define SHA512_SHORT_BLOCK_LENGTH
Definition: sha2.c:102

References context::buffer, memset(), p, REVERSE64, SHA512_BLOCK_LENGTH, SHA512_SHORT_BLOCK_LENGTH, SHA512_Transform(), and ut64().

Referenced by SHA384_Final(), and SHA512_Final().

◆ SHA512_Transform()

void SHA512_Transform ( RZ_SHA512_CTX context,
const ut64 data 
)

Definition at line 702 of file sha2.c.

702  {
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 }
#define Sigma0_512(x)
Definition: sha2.c:163
#define sigma0_512(x)
Definition: sha2.c:165
#define Sigma1_512(x)
Definition: sha2.c:164
#define sigma1_512(x)
Definition: sha2.c:166
static const ut64 K512[80]
Definition: sha2.c:211

References a, b, context::buffer, BYTE_ORDER, c, Ch, d, e, f, g, h, K512, LITTLE_ENDIAN, Maj, REVERSE64, s0, s1, Sigma0_512, Sigma1_512, and ut64().

Referenced by SHA512_Last(), and SHA512_Update().

◆ SHA512_Update()

void SHA512_Update ( RZ_SHA512_CTX context,
const ut8 data,
size_t  len 
)

Definition at line 777 of file sha2.c.

777  {
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 }
#define ADDINC128(w, n)
Definition: sha2.c:128

References ADDINC128, context::buffer, len, memcpy(), SHA512_BLOCK_LENGTH, SHA512_Transform(), and ut64().

Referenced by plugin_sha512_small_block(), plugin_sha512_update(), SHA384_Update(), and SHA512_Data().

Variable Documentation

◆ K256

const ut32 K256[64]
static
Initial value:
= {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
}

Definition at line 179 of file sha2.c.

Referenced by SHA256_Transform().

◆ K512

const ut64 K512[80]
static

Definition at line 211 of file sha2.c.

Referenced by SHA512_Transform().

◆ sha256_initial_hash_value

const ut32 sha256_initial_hash_value[8]
static
Initial value:
= {
0x6a09e667UL,
0xbb67ae85UL,
0x3c6ef372UL,
0xa54ff53aUL,
0x510e527fUL,
0x9b05688cUL,
0x1f83d9abUL,
0x5be0cd19UL
}

Definition at line 199 of file sha2.c.

Referenced by SHA256_Init().

◆ sha2_hex_digits

const char* sha2_hex_digits = "0123456789abcdef"
static

Definition at line 282 of file sha2.c.

Referenced by SHA256_End(), SHA384_End(), and SHA512_End().

◆ sha384_initial_hash_value

const ut64 sha384_initial_hash_value[8]
static
Initial value:
= {
0xcbbb9d5dc1059ed8ULL,
0x629a292a367cd507ULL,
0x9159015a3070dd17ULL,
0x152fecd8f70e5939ULL,
0x67332667ffc00b31ULL,
0x8eb44a8768581511ULL,
0xdb0c2e0d64f98fa7ULL,
0x47b5481dbefa4fa4ULL
}

Definition at line 255 of file sha2.c.

Referenced by SHA384_Init().

◆ sha512_initial_hash_value

const ut64 sha512_initial_hash_value[8]
static
Initial value:
= {
0x6a09e667f3bcc908ULL,
0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL,
0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL,
0x5be0cd19137e2179ULL
}

Definition at line 267 of file sha2.c.

Referenced by SHA512_Init().