SHA256Block.c.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. SHA256Block.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='SetProperty.c.html' title=' SetProperty.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='SHA256.c.html' title=' SHA256.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * void SHA256Block (struct sha256 * sha256, void const * memory);
  24. *
  25. * SHA256.h
  26. *
  27. * encode a fixed-length block of memory into the hash buffer and
  28. * update the hash state; the length is implicit in the algorithm
  29. * and need not be specified as an argument;
  30. *
  31. * Read standard FIPS180-2 sec 5.3.2 for an explanation;
  32. *
  33. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  34. * Copyright (c) 2001-2006 by Charles Maier Associates;
  35. * Licensed under the Internet Software Consortium License;
  36. *
  37. *--------------------------------------------------------------------*/
  38. #ifndef SHA256MERGE_SOURCE
  39. #define SHA256MERGE_SOURCE
  40. #include &lt;stdio.h&gt;
  41. #include &quot;../key/SHA256.h&quot;
  42. #define SHR(word,bits) ((word &amp; 0xFFFFFFFF) &gt;&gt; bits)
  43. #define ROTR(word,bits) (SHR(word,bits) | (word &lt;&lt; (32 - bits)))
  44. void SHA256Block (struct sha256 * sha256, void const * memory)
  45. {
  46. static const uint32_t K [sizeof (sha256-&gt;block)] =
  47. {
  48. 0x428A2F98,
  49. 0x71374491,
  50. 0xB5C0FBCF,
  51. 0xE9B5DBA5,
  52. 0x3956C25B,
  53. 0x59F111F1,
  54. 0x923F82A4,
  55. 0xAB1C5ED5,
  56. 0xD807AA98,
  57. 0x12835B01,
  58. 0x243185BE,
  59. 0x550C7DC3,
  60. 0x72BE5D74,
  61. 0x80DEB1FE,
  62. 0x9BDC06A7,
  63. 0xC19BF174,
  64. 0xE49B69C1,
  65. 0xEFBE4786,
  66. 0x0FC19DC6,
  67. 0x240CA1CC,
  68. 0x2DE92C6F,
  69. 0x4A7484AA,
  70. 0x5CB0A9DC,
  71. 0x76F988DA,
  72. 0x983E5152,
  73. 0xA831C66D,
  74. 0xB00327C8,
  75. 0xBF597FC7,
  76. 0xC6E00BF3,
  77. 0xD5A79147,
  78. 0x06CA6351,
  79. 0x14292967,
  80. 0x27B70A85,
  81. 0x2E1B2138,
  82. 0x4D2C6DFC,
  83. 0x53380D13,
  84. 0x650A7354,
  85. 0x766A0ABB,
  86. 0x81C2C92E,
  87. 0x92722C85,
  88. 0xA2BFE8A1,
  89. 0xA81A664B,
  90. 0xC24B8B70,
  91. 0xC76C51A3,
  92. 0xD192E819,
  93. 0xD6990624,
  94. 0xF40E3585,
  95. 0x106AA070,
  96. 0x19A4C116,
  97. 0x1E376C08,
  98. 0x2748774C,
  99. 0x34B0BCB5,
  100. 0x391C0CB3,
  101. 0x4ED8AA4A,
  102. 0x5B9CCA4F,
  103. 0x682E6FF3,
  104. 0x748F82EE,
  105. 0x78A5636F,
  106. 0x84C87814,
  107. 0x8CC70208,
  108. 0x90BEFFFA,
  109. 0xA4506CEB,
  110. 0xBEF9A3F7,
  111. 0xC67178F2
  112. };
  113. unsigned pass;
  114. unsigned word;
  115. uint32_t H [sizeof (sha256-&gt;state)/sizeof (uint32_t)];
  116. uint32_t W [sizeof (sha256-&gt;block)];
  117. uint8_t * buffer = (uint8_t *)(memory);
  118. for (word = 0; word &lt; 16; word++)
  119. {
  120. W [word] = 0;
  121. W [word] |= (uint32_t)(*buffer++) &lt;&lt; 24;
  122. W [word] |= (uint32_t)(*buffer++) &lt;&lt; 16;
  123. W [word] |= (uint32_t)(*buffer++) &lt;&lt; 8;
  124. W [word] |= (uint32_t)(*buffer++) &lt;&lt; 0;;
  125. }
  126. for ( ; word &lt; sizeof (sha256-&gt;block); word++)
  127. {
  128. uint32_t s0 = ROTR (W [word-15], 7) ^ ROTR (W [word-15], 18) ^ SHR (W [word-15], 3);
  129. uint32_t s1 = ROTR (W [word- 2], 17) ^ ROTR (W [word- 2], 19) ^ SHR (W [word- 2], 10);
  130. W [word] = W [word - 16] + s0 + W [word - 7] + s1;
  131. }
  132. for (word = 0; word &lt; (sizeof (sha256-&gt;state) / sizeof (uint32_t)); word++)
  133. {
  134. H [word] = sha256-&gt;state [word];
  135. }
  136. for (pass = 0; pass &lt; sizeof (sha256-&gt;block); pass++)
  137. {
  138. uint32_t s2 = ROTR (H [0], 2) ^ ROTR (H [0], 13) ^ ROTR (H [0], 22);
  139. uint32_t maj = (H [0] &amp; H [1]) ^ (H [0] &amp; H [2]) ^ (H [1] &amp; H [2]);
  140. uint32_t t2 = s2 + maj;
  141. uint32_t s3 = ROTR (H [4], 6) ^ ROTR (H [4], 11) ^ ROTR (H [4], 25);
  142. uint32_t ch = (H [4] &amp; H [5]) ^ ((~ H [4]) &amp; H [6]);
  143. uint32_t t1 = H [7] + s3 + ch + K [pass] + W [pass];
  144. for (word = (sizeof (sha256-&gt;state) / sizeof (uint32_t)) - 1; word &gt; 0; word--)
  145. {
  146. H [word] = H [word-1];
  147. }
  148. H [0] = t1 + t2;
  149. H [4] += t1;
  150. }
  151. for (word = 0; word &lt; (sizeof (sha256-&gt;state) / sizeof (uint32_t)); word++)
  152. {
  153. sha256-&gt;state [word] += H [word];
  154. }
  155. return;
  156. }
  157. #endif
  158. </pre>
  159. <div class='footerlink'>
  160. [<a href='SetProperty.c.html' title=' SetProperty.c '>PREV</a>]
  161. [<a href='toolkit.html' title=' Index '>HOME</a>]
  162. [<a href='SHA256.c.html' title=' SHA256.c '>NEXT</a>]
  163. </div>
  164. </body>
  165. </html>