SHA256Block.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*====================================================================*
  2. *
  3. * void SHA256Block (struct sha256 * sha256, void const * memory);
  4. *
  5. * SHA256.h
  6. *
  7. * encode a fixed-length block of memory into the hash buffer and
  8. * update the hash state; the length is implicit in the algorithm
  9. * and need not be specified as an argument;
  10. *
  11. * Read standard FIPS180-2 sec 5.3.2 for an explanation;
  12. *
  13. * Motley Tools by Charles Maier;
  14. * Copyright (c) 2001-2006 by Charles Maier Associates;
  15. * Licensed under the Internet Software Consortium License;
  16. *
  17. *--------------------------------------------------------------------*/
  18. #ifndef SHA256MERGE_SOURCE
  19. #define SHA256MERGE_SOURCE
  20. #include <stdio.h>
  21. #include "../key/SHA256.h"
  22. #define SHR(word,bits) ((word & 0xFFFFFFFF) >> bits)
  23. #define ROTR(word,bits) (SHR(word,bits) | (word << (32 - bits)))
  24. void SHA256Block (struct sha256 * sha256, void const * memory)
  25. {
  26. static const uint32_t K [sizeof (sha256->block)] =
  27. {
  28. 0x428A2F98,
  29. 0x71374491,
  30. 0xB5C0FBCF,
  31. 0xE9B5DBA5,
  32. 0x3956C25B,
  33. 0x59F111F1,
  34. 0x923F82A4,
  35. 0xAB1C5ED5,
  36. 0xD807AA98,
  37. 0x12835B01,
  38. 0x243185BE,
  39. 0x550C7DC3,
  40. 0x72BE5D74,
  41. 0x80DEB1FE,
  42. 0x9BDC06A7,
  43. 0xC19BF174,
  44. 0xE49B69C1,
  45. 0xEFBE4786,
  46. 0x0FC19DC6,
  47. 0x240CA1CC,
  48. 0x2DE92C6F,
  49. 0x4A7484AA,
  50. 0x5CB0A9DC,
  51. 0x76F988DA,
  52. 0x983E5152,
  53. 0xA831C66D,
  54. 0xB00327C8,
  55. 0xBF597FC7,
  56. 0xC6E00BF3,
  57. 0xD5A79147,
  58. 0x06CA6351,
  59. 0x14292967,
  60. 0x27B70A85,
  61. 0x2E1B2138,
  62. 0x4D2C6DFC,
  63. 0x53380D13,
  64. 0x650A7354,
  65. 0x766A0ABB,
  66. 0x81C2C92E,
  67. 0x92722C85,
  68. 0xA2BFE8A1,
  69. 0xA81A664B,
  70. 0xC24B8B70,
  71. 0xC76C51A3,
  72. 0xD192E819,
  73. 0xD6990624,
  74. 0xF40E3585,
  75. 0x106AA070,
  76. 0x19A4C116,
  77. 0x1E376C08,
  78. 0x2748774C,
  79. 0x34B0BCB5,
  80. 0x391C0CB3,
  81. 0x4ED8AA4A,
  82. 0x5B9CCA4F,
  83. 0x682E6FF3,
  84. 0x748F82EE,
  85. 0x78A5636F,
  86. 0x84C87814,
  87. 0x8CC70208,
  88. 0x90BEFFFA,
  89. 0xA4506CEB,
  90. 0xBEF9A3F7,
  91. 0xC67178F2
  92. };
  93. unsigned pass;
  94. unsigned word;
  95. uint32_t H [sizeof (sha256->state)/sizeof (uint32_t)];
  96. uint32_t W [sizeof (sha256->block)];
  97. uint8_t * buffer = (uint8_t *)(memory);
  98. for (word = 0; word < 16; word++)
  99. {
  100. W [word] = 0;
  101. W [word] |= (uint32_t)(*buffer++) << 24;
  102. W [word] |= (uint32_t)(*buffer++) << 16;
  103. W [word] |= (uint32_t)(*buffer++) << 8;
  104. W [word] |= (uint32_t)(*buffer++) << 0;;
  105. }
  106. for ( ; word < sizeof (sha256->block); word++)
  107. {
  108. uint32_t s0 = ROTR (W [word-15], 7) ^ ROTR (W [word-15], 18) ^ SHR (W [word-15], 3);
  109. uint32_t s1 = ROTR (W [word- 2], 17) ^ ROTR (W [word- 2], 19) ^ SHR (W [word- 2], 10);
  110. W [word] = W [word - 16] + s0 + W [word - 7] + s1;
  111. }
  112. for (word = 0; word < (sizeof (sha256->state) / sizeof (uint32_t)); word++)
  113. {
  114. H [word] = sha256->state [word];
  115. }
  116. for (pass = 0; pass < sizeof (sha256->block); pass++)
  117. {
  118. uint32_t s2 = ROTR (H [0], 2) ^ ROTR (H [0], 13) ^ ROTR (H [0], 22);
  119. uint32_t maj = (H [0] & H [1]) ^ (H [0] & H [2]) ^ (H [1] & H [2]);
  120. uint32_t t2 = s2 + maj;
  121. uint32_t s3 = ROTR (H [4], 6) ^ ROTR (H [4], 11) ^ ROTR (H [4], 25);
  122. uint32_t ch = (H [4] & H [5]) ^ ((~ H [4]) & H [6]);
  123. uint32_t t1 = H [7] + s3 + ch + K [pass] + W [pass];
  124. for (word = (sizeof (sha256->state) / sizeof (uint32_t)) - 1; word > 0; word--)
  125. {
  126. H [word] = H [word-1];
  127. }
  128. H [0] = t1 + t2;
  129. H [4] += t1;
  130. }
  131. for (word = 0; word < (sizeof (sha256->state) / sizeof (uint32_t)); word++)
  132. {
  133. sha256->state [word] += H [word];
  134. }
  135. return;
  136. }
  137. #endif