sha2.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /**
  2. * Copyright (c) 2000-2001 Aaron D. Gifford
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holder nor the names of contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <string.h>
  31. #include <stdint.h>
  32. #include "sha2.h"
  33. #include "memzero.h"
  34. #include "byte_order.h"
  35. /*
  36. * ASSERT NOTE:
  37. * Some sanity checking code is included using assert(). On my FreeBSD
  38. * system, this additional code can be removed by compiling with NDEBUG
  39. * defined. Check your own systems manpage on assert() to see how to
  40. * compile WITHOUT the sanity checking code on your system.
  41. *
  42. * UNROLLED TRANSFORM LOOP NOTE:
  43. * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
  44. * loop version for the hash transform rounds (defined using macros
  45. * later in this file). Either define on the command line, for example:
  46. *
  47. * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
  48. *
  49. * or define below:
  50. *
  51. * #define SHA2_UNROLL_TRANSFORM
  52. *
  53. */
  54. /*** SHA-256/384/512 Machine Architecture Definitions *****************/
  55. /*
  56. * BYTE_ORDER NOTE:
  57. *
  58. * Please make sure that your system defines BYTE_ORDER. If your
  59. * architecture is little-endian, make sure it also defines
  60. * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
  61. * equivilent.
  62. *
  63. * If your system does not define the above, then you can do so by
  64. * hand like this:
  65. *
  66. * #define LITTLE_ENDIAN 1234
  67. * #define BIG_ENDIAN 4321
  68. *
  69. * And for little-endian machines, add:
  70. *
  71. * #define BYTE_ORDER LITTLE_ENDIAN
  72. *
  73. * Or for big-endian machines:
  74. *
  75. * #define BYTE_ORDER BIG_ENDIAN
  76. *
  77. * The FreeBSD machine this was written on defines BYTE_ORDER
  78. * appropriately by including <sys/types.h> (which in turn includes
  79. * <machine/endian.h> where the appropriate definitions are actually
  80. * made).
  81. */
  82. #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
  83. #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
  84. #endif
  85. typedef uint8_t sha2_byte; /* Exactly 1 byte */
  86. typedef uint32_t sha2_word32; /* Exactly 4 bytes */
  87. typedef uint64_t sha2_word64; /* Exactly 8 bytes */
  88. /*** SHA-256/384/512 Various Length Definitions ***********************/
  89. /* NOTE: Most of these are in sha2.h */
  90. #define SHA1_SHORT_BLOCK_LENGTH (SHA1_BLOCK_LENGTH - 8)
  91. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  92. #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
  93. /*
  94. * Macro for incrementally adding the unsigned 64-bit integer n to the
  95. * unsigned 128-bit integer (represented using a two-element array of
  96. * 64-bit words):
  97. */
  98. #define ADDINC128(w, n) \
  99. { \
  100. (w)[0] += (sha2_word64)(n); \
  101. if((w)[0] < (n)) { \
  102. (w)[1]++; \
  103. } \
  104. }
  105. #define MEMCPY_BCOPY(d, s, l) memcpy((d), (s), (l))
  106. /*** THE SIX LOGICAL FUNCTIONS ****************************************/
  107. /*
  108. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  109. *
  110. * NOTE: In the original SHA-256/384/512 document, the shift-right
  111. * function was named R and the rotate-right function was called S.
  112. * (See: http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf on the
  113. * web.)
  114. *
  115. * The newer NIST FIPS 180-2 document uses a much clearer naming
  116. * scheme, SHR for shift-right, ROTR for rotate-right, and ROTL for
  117. * rotate-left. (See:
  118. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  119. * on the web.)
  120. *
  121. * WARNING: These macros must be used cautiously, since they reference
  122. * supplied parameters sometimes more than once, and thus could have
  123. * unexpected side-effects if used without taking this into account.
  124. */
  125. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  126. #define SHR(b, x) ((x) >> (b))
  127. /* 32-bit Rotate-right (used in SHA-256): */
  128. #define ROTR32(b, x) (((x) >> (b)) | ((x) << (32 - (b))))
  129. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  130. #define ROTR64(b, x) (((x) >> (b)) | ((x) << (64 - (b))))
  131. /* 32-bit Rotate-left (used in SHA-1): */
  132. #define ROTL32(b, x) (((x) << (b)) | ((x) >> (32 - (b))))
  133. /* Two of six logical functions used in SHA-1, SHA-256, SHA-384, and SHA-512: */
  134. #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
  135. #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  136. /* Function used in SHA-1: */
  137. #define Parity(x, y, z) ((x) ^ (y) ^ (z))
  138. /* Four of six logical functions used in SHA-256: */
  139. #define Sigma0_256(x) (ROTR32(2, (x)) ^ ROTR32(13, (x)) ^ ROTR32(22, (x)))
  140. #define Sigma1_256(x) (ROTR32(6, (x)) ^ ROTR32(11, (x)) ^ ROTR32(25, (x)))
  141. #define sigma0_256(x) (ROTR32(7, (x)) ^ ROTR32(18, (x)) ^ SHR(3, (x)))
  142. #define sigma1_256(x) (ROTR32(17, (x)) ^ ROTR32(19, (x)) ^ SHR(10, (x)))
  143. /* Four of six logical functions used in SHA-384 and SHA-512: */
  144. #define Sigma0_512(x) (ROTR64(28, (x)) ^ ROTR64(34, (x)) ^ ROTR64(39, (x)))
  145. #define Sigma1_512(x) (ROTR64(14, (x)) ^ ROTR64(18, (x)) ^ ROTR64(41, (x)))
  146. #define sigma0_512(x) (ROTR64(1, (x)) ^ ROTR64(8, (x)) ^ SHR(7, (x)))
  147. #define sigma1_512(x) (ROTR64(19, (x)) ^ ROTR64(61, (x)) ^ SHR(6, (x)))
  148. /*** INTERNAL FUNCTION PROTOTYPES *************************************/
  149. /* NOTE: These should not be accessed directly from outside this
  150. * library -- they are intended for private internal visibility/use
  151. * only.
  152. */
  153. static void sha512_Last(SHA512_CTX*);
  154. /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
  155. /* Hash constant words K for SHA-1: */
  156. #define K1_0_TO_19 0x5a827999UL
  157. #define K1_20_TO_39 0x6ed9eba1UL
  158. #define K1_40_TO_59 0x8f1bbcdcUL
  159. #define K1_60_TO_79 0xca62c1d6UL
  160. /* Initial hash value H for SHA-1: */
  161. const sha2_word32 sha1_initial_hash_value[SHA1_DIGEST_LENGTH / sizeof(sha2_word32)] =
  162. {0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL};
  163. /* Hash constant words K for SHA-256: */
  164. static const sha2_word32 K256[64] = {
  165. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL,
  166. 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  167. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL,
  168. 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  169. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
  170. 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  171. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL,
  172. 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  173. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL,
  174. 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  175. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL};
  176. /* Initial hash value H for SHA-256: */
  177. const sha2_word32 sha256_initial_hash_value[8] = {
  178. 0x6a09e667UL,
  179. 0xbb67ae85UL,
  180. 0x3c6ef372UL,
  181. 0xa54ff53aUL,
  182. 0x510e527fUL,
  183. 0x9b05688cUL,
  184. 0x1f83d9abUL,
  185. 0x5be0cd19UL};
  186. /* Hash constant words K for SHA-384 and SHA-512: */
  187. static const sha2_word64 K512[80] = {
  188. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  189. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  190. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  191. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  192. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  193. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  194. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  195. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  196. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  197. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  198. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  199. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  200. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  201. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  202. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  203. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  204. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  205. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  206. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  207. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
  208. /* Initial hash value H for SHA-512 */
  209. const sha2_word64 sha512_initial_hash_value[8] = {
  210. 0x6a09e667f3bcc908ULL,
  211. 0xbb67ae8584caa73bULL,
  212. 0x3c6ef372fe94f82bULL,
  213. 0xa54ff53a5f1d36f1ULL,
  214. 0x510e527fade682d1ULL,
  215. 0x9b05688c2b3e6c1fULL,
  216. 0x1f83d9abfb41bd6bULL,
  217. 0x5be0cd19137e2179ULL};
  218. /*
  219. * Constant used by SHA256/384/512_End() functions for converting the
  220. * digest to a readable hexadecimal character string:
  221. */
  222. static const char* sha2_hex_digits = "0123456789abcdef";
  223. /*** SHA-1: ***********************************************************/
  224. void sha1_Init(SHA1_CTX* context) {
  225. MEMCPY_BCOPY(context->state, sha1_initial_hash_value, SHA1_DIGEST_LENGTH);
  226. memzero(context->buffer, SHA1_BLOCK_LENGTH);
  227. context->bitcount = 0;
  228. }
  229. #ifdef SHA2_UNROLL_TRANSFORM
  230. /* Unrolled SHA-1 round macros: */
  231. #define ROUND1_0_TO_15(a, b, c, d, e) \
  232. (e) = ROTL32(5, (a)) + Ch((b), (c), (d)) + (e) + K1_0_TO_19 + (W1[j] = *data++); \
  233. (b) = ROTL32(30, (b)); \
  234. j++;
  235. #define ROUND1_16_TO_19(a, b, c, d, e) \
  236. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f]; \
  237. (e) = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + (W1[j & 0x0f] = ROTL32(1, T1)); \
  238. (b) = ROTL32(30, b); \
  239. j++;
  240. #define ROUND1_20_TO_39(a, b, c, d, e) \
  241. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f]; \
  242. (e) = ROTL32(5, a) + Parity(b, c, d) + e + K1_20_TO_39 + (W1[j & 0x0f] = ROTL32(1, T1)); \
  243. (b) = ROTL32(30, b); \
  244. j++;
  245. #define ROUND1_40_TO_59(a, b, c, d, e) \
  246. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f]; \
  247. (e) = ROTL32(5, a) + Maj(b, c, d) + e + K1_40_TO_59 + (W1[j & 0x0f] = ROTL32(1, T1)); \
  248. (b) = ROTL32(30, b); \
  249. j++;
  250. #define ROUND1_60_TO_79(a, b, c, d, e) \
  251. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f]; \
  252. (e) = ROTL32(5, a) + Parity(b, c, d) + e + K1_60_TO_79 + (W1[j & 0x0f] = ROTL32(1, T1)); \
  253. (b) = ROTL32(30, b); \
  254. j++;
  255. void sha1_Transform(const sha2_word32* state_in, const sha2_word32* data, sha2_word32* state_out) {
  256. sha2_word32 a = 0, b = 0, c = 0, d = 0, e = 0;
  257. sha2_word32 T1 = 0;
  258. sha2_word32 W1[16] = {0};
  259. int j = 0;
  260. /* Initialize registers with the prev. intermediate value */
  261. a = state_in[0];
  262. b = state_in[1];
  263. c = state_in[2];
  264. d = state_in[3];
  265. e = state_in[4];
  266. j = 0;
  267. /* Rounds 0 to 15 unrolled: */
  268. ROUND1_0_TO_15(a, b, c, d, e);
  269. ROUND1_0_TO_15(e, a, b, c, d);
  270. ROUND1_0_TO_15(d, e, a, b, c);
  271. ROUND1_0_TO_15(c, d, e, a, b);
  272. ROUND1_0_TO_15(b, c, d, e, a);
  273. ROUND1_0_TO_15(a, b, c, d, e);
  274. ROUND1_0_TO_15(e, a, b, c, d);
  275. ROUND1_0_TO_15(d, e, a, b, c);
  276. ROUND1_0_TO_15(c, d, e, a, b);
  277. ROUND1_0_TO_15(b, c, d, e, a);
  278. ROUND1_0_TO_15(a, b, c, d, e);
  279. ROUND1_0_TO_15(e, a, b, c, d);
  280. ROUND1_0_TO_15(d, e, a, b, c);
  281. ROUND1_0_TO_15(c, d, e, a, b);
  282. ROUND1_0_TO_15(b, c, d, e, a);
  283. ROUND1_0_TO_15(a, b, c, d, e);
  284. /* Rounds 16 to 19 unrolled: */
  285. ROUND1_16_TO_19(e, a, b, c, d);
  286. ROUND1_16_TO_19(d, e, a, b, c);
  287. ROUND1_16_TO_19(c, d, e, a, b);
  288. ROUND1_16_TO_19(b, c, d, e, a);
  289. /* Rounds 20 to 39 unrolled: */
  290. ROUND1_20_TO_39(a, b, c, d, e);
  291. ROUND1_20_TO_39(e, a, b, c, d);
  292. ROUND1_20_TO_39(d, e, a, b, c);
  293. ROUND1_20_TO_39(c, d, e, a, b);
  294. ROUND1_20_TO_39(b, c, d, e, a);
  295. ROUND1_20_TO_39(a, b, c, d, e);
  296. ROUND1_20_TO_39(e, a, b, c, d);
  297. ROUND1_20_TO_39(d, e, a, b, c);
  298. ROUND1_20_TO_39(c, d, e, a, b);
  299. ROUND1_20_TO_39(b, c, d, e, a);
  300. ROUND1_20_TO_39(a, b, c, d, e);
  301. ROUND1_20_TO_39(e, a, b, c, d);
  302. ROUND1_20_TO_39(d, e, a, b, c);
  303. ROUND1_20_TO_39(c, d, e, a, b);
  304. ROUND1_20_TO_39(b, c, d, e, a);
  305. ROUND1_20_TO_39(a, b, c, d, e);
  306. ROUND1_20_TO_39(e, a, b, c, d);
  307. ROUND1_20_TO_39(d, e, a, b, c);
  308. ROUND1_20_TO_39(c, d, e, a, b);
  309. ROUND1_20_TO_39(b, c, d, e, a);
  310. /* Rounds 40 to 59 unrolled: */
  311. ROUND1_40_TO_59(a, b, c, d, e);
  312. ROUND1_40_TO_59(e, a, b, c, d);
  313. ROUND1_40_TO_59(d, e, a, b, c);
  314. ROUND1_40_TO_59(c, d, e, a, b);
  315. ROUND1_40_TO_59(b, c, d, e, a);
  316. ROUND1_40_TO_59(a, b, c, d, e);
  317. ROUND1_40_TO_59(e, a, b, c, d);
  318. ROUND1_40_TO_59(d, e, a, b, c);
  319. ROUND1_40_TO_59(c, d, e, a, b);
  320. ROUND1_40_TO_59(b, c, d, e, a);
  321. ROUND1_40_TO_59(a, b, c, d, e);
  322. ROUND1_40_TO_59(e, a, b, c, d);
  323. ROUND1_40_TO_59(d, e, a, b, c);
  324. ROUND1_40_TO_59(c, d, e, a, b);
  325. ROUND1_40_TO_59(b, c, d, e, a);
  326. ROUND1_40_TO_59(a, b, c, d, e);
  327. ROUND1_40_TO_59(e, a, b, c, d);
  328. ROUND1_40_TO_59(d, e, a, b, c);
  329. ROUND1_40_TO_59(c, d, e, a, b);
  330. ROUND1_40_TO_59(b, c, d, e, a);
  331. /* Rounds 60 to 79 unrolled: */
  332. ROUND1_60_TO_79(a, b, c, d, e);
  333. ROUND1_60_TO_79(e, a, b, c, d);
  334. ROUND1_60_TO_79(d, e, a, b, c);
  335. ROUND1_60_TO_79(c, d, e, a, b);
  336. ROUND1_60_TO_79(b, c, d, e, a);
  337. ROUND1_60_TO_79(a, b, c, d, e);
  338. ROUND1_60_TO_79(e, a, b, c, d);
  339. ROUND1_60_TO_79(d, e, a, b, c);
  340. ROUND1_60_TO_79(c, d, e, a, b);
  341. ROUND1_60_TO_79(b, c, d, e, a);
  342. ROUND1_60_TO_79(a, b, c, d, e);
  343. ROUND1_60_TO_79(e, a, b, c, d);
  344. ROUND1_60_TO_79(d, e, a, b, c);
  345. ROUND1_60_TO_79(c, d, e, a, b);
  346. ROUND1_60_TO_79(b, c, d, e, a);
  347. ROUND1_60_TO_79(a, b, c, d, e);
  348. ROUND1_60_TO_79(e, a, b, c, d);
  349. ROUND1_60_TO_79(d, e, a, b, c);
  350. ROUND1_60_TO_79(c, d, e, a, b);
  351. ROUND1_60_TO_79(b, c, d, e, a);
  352. /* Compute the current intermediate hash value */
  353. state_out[0] = state_in[0] + a;
  354. state_out[1] = state_in[1] + b;
  355. state_out[2] = state_in[2] + c;
  356. state_out[3] = state_in[3] + d;
  357. state_out[4] = state_in[4] + e;
  358. /* Clean up */
  359. a = b = c = d = e = T1 = 0;
  360. }
  361. #else /* SHA2_UNROLL_TRANSFORM */
  362. void sha1_Transform(const sha2_word32* state_in, const sha2_word32* data, sha2_word32* state_out) {
  363. sha2_word32 a = 0, b = 0, c = 0, d = 0, e = 0;
  364. sha2_word32 T1 = 0;
  365. sha2_word32 W1[16] = {0};
  366. int j = 0;
  367. /* Initialize registers with the prev. intermediate value */
  368. a = state_in[0];
  369. b = state_in[1];
  370. c = state_in[2];
  371. d = state_in[3];
  372. e = state_in[4];
  373. j = 0;
  374. do {
  375. T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + (W1[j] = *data++);
  376. e = d;
  377. d = c;
  378. c = ROTL32(30, b);
  379. b = a;
  380. a = T1;
  381. j++;
  382. } while(j < 16);
  383. do {
  384. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f];
  385. T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + (W1[j & 0x0f] = ROTL32(1, T1));
  386. e = d;
  387. d = c;
  388. c = ROTL32(30, b);
  389. b = a;
  390. a = T1;
  391. j++;
  392. } while(j < 20);
  393. do {
  394. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f];
  395. T1 = ROTL32(5, a) + Parity(b, c, d) + e + K1_20_TO_39 + (W1[j & 0x0f] = ROTL32(1, T1));
  396. e = d;
  397. d = c;
  398. c = ROTL32(30, b);
  399. b = a;
  400. a = T1;
  401. j++;
  402. } while(j < 40);
  403. do {
  404. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f];
  405. T1 = ROTL32(5, a) + Maj(b, c, d) + e + K1_40_TO_59 + (W1[j & 0x0f] = ROTL32(1, T1));
  406. e = d;
  407. d = c;
  408. c = ROTL32(30, b);
  409. b = a;
  410. a = T1;
  411. j++;
  412. } while(j < 60);
  413. do {
  414. T1 = W1[(j + 13) & 0x0f] ^ W1[(j + 8) & 0x0f] ^ W1[(j + 2) & 0x0f] ^ W1[j & 0x0f];
  415. T1 = ROTL32(5, a) + Parity(b, c, d) + e + K1_60_TO_79 + (W1[j & 0x0f] = ROTL32(1, T1));
  416. e = d;
  417. d = c;
  418. c = ROTL32(30, b);
  419. b = a;
  420. a = T1;
  421. j++;
  422. } while(j < 80);
  423. /* Compute the current intermediate hash value */
  424. state_out[0] = state_in[0] + a;
  425. state_out[1] = state_in[1] + b;
  426. state_out[2] = state_in[2] + c;
  427. state_out[3] = state_in[3] + d;
  428. state_out[4] = state_in[4] + e;
  429. /* Clean up */
  430. a = b = c = d = e = T1 = 0;
  431. }
  432. #endif /* SHA2_UNROLL_TRANSFORM */
  433. void sha1_Update(SHA1_CTX* context, const sha2_byte* data, size_t len) {
  434. unsigned int freespace = 0, usedspace = 0;
  435. if(len == 0) {
  436. /* Calling with no data is valid - we do nothing */
  437. return;
  438. }
  439. usedspace = (context->bitcount >> 3) % SHA1_BLOCK_LENGTH;
  440. if(usedspace > 0) {
  441. /* Calculate how much free space is available in the buffer */
  442. freespace = SHA1_BLOCK_LENGTH - usedspace;
  443. if(len >= freespace) {
  444. /* Fill the buffer completely and process it */
  445. MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, freespace);
  446. context->bitcount += freespace << 3;
  447. len -= freespace;
  448. data += freespace;
  449. #if BYTE_ORDER == LITTLE_ENDIAN
  450. /* Convert TO host byte order */
  451. for(int j = 0; j < 16; j++) {
  452. REVERSE32(context->buffer[j], context->buffer[j]);
  453. }
  454. #endif
  455. sha1_Transform(context->state, context->buffer, context->state);
  456. } else {
  457. /* The buffer is not yet full */
  458. MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, len);
  459. context->bitcount += len << 3;
  460. /* Clean up: */
  461. usedspace = freespace = 0;
  462. return;
  463. }
  464. }
  465. while(len >= SHA1_BLOCK_LENGTH) {
  466. /* Process as many complete blocks as we can */
  467. MEMCPY_BCOPY(context->buffer, data, SHA1_BLOCK_LENGTH);
  468. #if BYTE_ORDER == LITTLE_ENDIAN
  469. /* Convert TO host byte order */
  470. for(int j = 0; j < 16; j++) {
  471. REVERSE32(context->buffer[j], context->buffer[j]);
  472. }
  473. #endif
  474. sha1_Transform(context->state, context->buffer, context->state);
  475. context->bitcount += SHA1_BLOCK_LENGTH << 3;
  476. len -= SHA1_BLOCK_LENGTH;
  477. data += SHA1_BLOCK_LENGTH;
  478. }
  479. if(len > 0) {
  480. /* There's left-overs, so save 'em */
  481. MEMCPY_BCOPY(context->buffer, data, len);
  482. context->bitcount += len << 3;
  483. }
  484. /* Clean up: */
  485. usedspace = freespace = 0;
  486. }
  487. void sha1_Final(SHA1_CTX* context, sha2_byte digest[SHA1_DIGEST_LENGTH]) {
  488. unsigned int usedspace = 0;
  489. /* If no digest buffer is passed, we don't bother doing this: */
  490. if(digest != (sha2_byte*)0) {
  491. usedspace = (context->bitcount >> 3) % SHA1_BLOCK_LENGTH;
  492. /* Begin padding with a 1 bit: */
  493. ((uint8_t*)context->buffer)[usedspace++] = 0x80;
  494. if(usedspace > SHA1_SHORT_BLOCK_LENGTH) {
  495. memzero(((uint8_t*)context->buffer) + usedspace, SHA1_BLOCK_LENGTH - usedspace);
  496. #if BYTE_ORDER == LITTLE_ENDIAN
  497. /* Convert TO host byte order */
  498. for(int j = 0; j < 16; j++) {
  499. REVERSE32(context->buffer[j], context->buffer[j]);
  500. }
  501. #endif
  502. /* Do second-to-last transform: */
  503. sha1_Transform(context->state, context->buffer, context->state);
  504. /* And prepare the last transform: */
  505. usedspace = 0;
  506. }
  507. /* Set-up for the last transform: */
  508. memzero(((uint8_t*)context->buffer) + usedspace, SHA1_SHORT_BLOCK_LENGTH - usedspace);
  509. #if BYTE_ORDER == LITTLE_ENDIAN
  510. /* Convert TO host byte order */
  511. for(int j = 0; j < 14; j++) {
  512. REVERSE32(context->buffer[j], context->buffer[j]);
  513. }
  514. #endif
  515. /* Set the bit count: */
  516. context->buffer[14] = context->bitcount >> 32;
  517. context->buffer[15] = context->bitcount & 0xffffffff;
  518. /* Final transform: */
  519. sha1_Transform(context->state, context->buffer, context->state);
  520. #if BYTE_ORDER == LITTLE_ENDIAN
  521. /* Convert FROM host byte order */
  522. for(int j = 0; j < 5; j++) {
  523. REVERSE32(context->state[j], context->state[j]);
  524. }
  525. #endif
  526. MEMCPY_BCOPY(digest, context->state, SHA1_DIGEST_LENGTH);
  527. }
  528. /* Clean up state data: */
  529. memzero(context, sizeof(SHA1_CTX));
  530. usedspace = 0;
  531. }
  532. char* sha1_End(SHA1_CTX* context, char buffer[SHA1_DIGEST_STRING_LENGTH]) {
  533. sha2_byte digest[SHA1_DIGEST_LENGTH] = {0}, *d = digest;
  534. int i = 0;
  535. if(buffer != (char*)0) {
  536. sha1_Final(context, digest);
  537. for(i = 0; i < SHA1_DIGEST_LENGTH; i++) {
  538. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  539. *buffer++ = sha2_hex_digits[*d & 0x0f];
  540. d++;
  541. }
  542. *buffer = (char)0;
  543. } else {
  544. memzero(context, sizeof(SHA1_CTX));
  545. }
  546. memzero(digest, SHA1_DIGEST_LENGTH);
  547. return buffer;
  548. }
  549. void sha1_Raw(const sha2_byte* data, size_t len, uint8_t digest[SHA1_DIGEST_LENGTH]) {
  550. SHA1_CTX context = {0};
  551. sha1_Init(&context);
  552. sha1_Update(&context, data, len);
  553. sha1_Final(&context, digest);
  554. }
  555. char* sha1_Data(const sha2_byte* data, size_t len, char digest[SHA1_DIGEST_STRING_LENGTH]) {
  556. SHA1_CTX context = {0};
  557. sha1_Init(&context);
  558. sha1_Update(&context, data, len);
  559. return sha1_End(&context, digest);
  560. }
  561. /*** SHA-256: *********************************************************/
  562. void sha256_Init(SHA256_CTX* context) {
  563. if(context == (SHA256_CTX*)0) {
  564. return;
  565. }
  566. MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
  567. memzero(context->buffer, SHA256_BLOCK_LENGTH);
  568. context->bitcount = 0;
  569. }
  570. void sha256_Init_ex(SHA256_CTX* context, const uint32_t state[8], uint64_t bitcount) {
  571. if(context == (SHA256_CTX*)0) {
  572. return;
  573. }
  574. MEMCPY_BCOPY(context->state, state, SHA256_DIGEST_LENGTH);
  575. memzero(context->buffer, SHA256_BLOCK_LENGTH);
  576. context->bitcount = bitcount;
  577. }
  578. #ifdef SHA2_UNROLL_TRANSFORM
  579. /* Unrolled SHA-256 round macros: */
  580. #define ROUND256_0_TO_15(a, b, c, d, e, f, g, h) \
  581. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + (W256[j] = *data++); \
  582. (d) += T1; \
  583. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  584. j++
  585. #define ROUND256(a, b, c, d, e, f, g, h) \
  586. s0 = W256[(j + 1) & 0x0f]; \
  587. s0 = sigma0_256(s0); \
  588. s1 = W256[(j + 14) & 0x0f]; \
  589. s1 = sigma1_256(s1); \
  590. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
  591. (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0); \
  592. (d) += T1; \
  593. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  594. j++
  595. void sha256_Transform(const sha2_word32* state_in, const sha2_word32* data, sha2_word32* state_out) {
  596. sha2_word32 a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, s0 = 0, s1 = 0;
  597. sha2_word32 T1 = 0;
  598. sha2_word32 W256[16] = {0};
  599. int j = 0;
  600. /* Initialize registers with the prev. intermediate value */
  601. a = state_in[0];
  602. b = state_in[1];
  603. c = state_in[2];
  604. d = state_in[3];
  605. e = state_in[4];
  606. f = state_in[5];
  607. g = state_in[6];
  608. h = state_in[7];
  609. j = 0;
  610. do {
  611. /* Rounds 0 to 15 (unrolled): */
  612. ROUND256_0_TO_15(a, b, c, d, e, f, g, h);
  613. ROUND256_0_TO_15(h, a, b, c, d, e, f, g);
  614. ROUND256_0_TO_15(g, h, a, b, c, d, e, f);
  615. ROUND256_0_TO_15(f, g, h, a, b, c, d, e);
  616. ROUND256_0_TO_15(e, f, g, h, a, b, c, d);
  617. ROUND256_0_TO_15(d, e, f, g, h, a, b, c);
  618. ROUND256_0_TO_15(c, d, e, f, g, h, a, b);
  619. ROUND256_0_TO_15(b, c, d, e, f, g, h, a);
  620. } while(j < 16);
  621. /* Now for the remaining rounds to 64: */
  622. do {
  623. ROUND256(a, b, c, d, e, f, g, h);
  624. ROUND256(h, a, b, c, d, e, f, g);
  625. ROUND256(g, h, a, b, c, d, e, f);
  626. ROUND256(f, g, h, a, b, c, d, e);
  627. ROUND256(e, f, g, h, a, b, c, d);
  628. ROUND256(d, e, f, g, h, a, b, c);
  629. ROUND256(c, d, e, f, g, h, a, b);
  630. ROUND256(b, c, d, e, f, g, h, a);
  631. } while(j < 64);
  632. /* Compute the current intermediate hash value */
  633. state_out[0] = state_in[0] + a;
  634. state_out[1] = state_in[1] + b;
  635. state_out[2] = state_in[2] + c;
  636. state_out[3] = state_in[3] + d;
  637. state_out[4] = state_in[4] + e;
  638. state_out[5] = state_in[5] + f;
  639. state_out[6] = state_in[6] + g;
  640. state_out[7] = state_in[7] + h;
  641. /* Clean up */
  642. a = b = c = d = e = f = g = h = T1 = 0;
  643. }
  644. #else /* SHA2_UNROLL_TRANSFORM */
  645. void sha256_Transform(const sha2_word32* state_in, const sha2_word32* data, sha2_word32* state_out) {
  646. sha2_word32 a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, s0 = 0, s1 = 0;
  647. sha2_word32 T1 = 0, T2 = 0, W256[16] = {0};
  648. int j = 0;
  649. /* Initialize registers with the prev. intermediate value */
  650. a = state_in[0];
  651. b = state_in[1];
  652. c = state_in[2];
  653. d = state_in[3];
  654. e = state_in[4];
  655. f = state_in[5];
  656. g = state_in[6];
  657. h = state_in[7];
  658. j = 0;
  659. do {
  660. /* Apply the SHA-256 compression function to update a..h with copy */
  661. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
  662. T2 = Sigma0_256(a) + Maj(a, b, c);
  663. h = g;
  664. g = f;
  665. f = e;
  666. e = d + T1;
  667. d = c;
  668. c = b;
  669. b = a;
  670. a = T1 + T2;
  671. j++;
  672. } while(j < 16);
  673. do {
  674. /* Part of the message block expansion: */
  675. s0 = W256[(j + 1) & 0x0f];
  676. s0 = sigma0_256(s0);
  677. s1 = W256[(j + 14) & 0x0f];
  678. s1 = sigma1_256(s1);
  679. /* Apply the SHA-256 compression function to update a..h */
  680. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
  681. (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
  682. T2 = Sigma0_256(a) + Maj(a, b, c);
  683. h = g;
  684. g = f;
  685. f = e;
  686. e = d + T1;
  687. d = c;
  688. c = b;
  689. b = a;
  690. a = T1 + T2;
  691. j++;
  692. } while(j < 64);
  693. /* Compute the current intermediate hash value */
  694. state_out[0] = state_in[0] + a;
  695. state_out[1] = state_in[1] + b;
  696. state_out[2] = state_in[2] + c;
  697. state_out[3] = state_in[3] + d;
  698. state_out[4] = state_in[4] + e;
  699. state_out[5] = state_in[5] + f;
  700. state_out[6] = state_in[6] + g;
  701. state_out[7] = state_in[7] + h;
  702. /* Clean up */
  703. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  704. }
  705. #endif /* SHA2_UNROLL_TRANSFORM */
  706. void sha256_Update(SHA256_CTX* context, const sha2_byte* data, size_t len) {
  707. unsigned int freespace = 0, usedspace = 0;
  708. if(len == 0) {
  709. /* Calling with no data is valid - we do nothing */
  710. return;
  711. }
  712. usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
  713. if(usedspace > 0) {
  714. /* Calculate how much free space is available in the buffer */
  715. freespace = SHA256_BLOCK_LENGTH - usedspace;
  716. if(len >= freespace) {
  717. /* Fill the buffer completely and process it */
  718. MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, freespace);
  719. context->bitcount += freespace << 3;
  720. len -= freespace;
  721. data += freespace;
  722. #if BYTE_ORDER == LITTLE_ENDIAN
  723. /* Convert TO host byte order */
  724. for(int j = 0; j < 16; j++) {
  725. REVERSE32(context->buffer[j], context->buffer[j]);
  726. }
  727. #endif
  728. sha256_Transform(context->state, context->buffer, context->state);
  729. } else {
  730. /* The buffer is not yet full */
  731. MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, len);
  732. context->bitcount += len << 3;
  733. /* Clean up: */
  734. usedspace = freespace = 0;
  735. return;
  736. }
  737. }
  738. while(len >= SHA256_BLOCK_LENGTH) {
  739. /* Process as many complete blocks as we can */
  740. MEMCPY_BCOPY(context->buffer, data, SHA256_BLOCK_LENGTH);
  741. #if BYTE_ORDER == LITTLE_ENDIAN
  742. /* Convert TO host byte order */
  743. for(int j = 0; j < 16; j++) {
  744. REVERSE32(context->buffer[j], context->buffer[j]);
  745. }
  746. #endif
  747. sha256_Transform(context->state, context->buffer, context->state);
  748. context->bitcount += SHA256_BLOCK_LENGTH << 3;
  749. len -= SHA256_BLOCK_LENGTH;
  750. data += SHA256_BLOCK_LENGTH;
  751. }
  752. if(len > 0) {
  753. /* There's left-overs, so save 'em */
  754. MEMCPY_BCOPY(context->buffer, data, len);
  755. context->bitcount += len << 3;
  756. }
  757. /* Clean up: */
  758. usedspace = freespace = 0;
  759. }
  760. void sha256_Final(SHA256_CTX* context, sha2_byte digest[SHA256_DIGEST_LENGTH]) {
  761. unsigned int usedspace = 0;
  762. /* If no digest buffer is passed, we don't bother doing this: */
  763. if(digest != (sha2_byte*)0) {
  764. usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
  765. /* Begin padding with a 1 bit: */
  766. ((uint8_t*)context->buffer)[usedspace++] = 0x80;
  767. if(usedspace > SHA256_SHORT_BLOCK_LENGTH) {
  768. memzero(((uint8_t*)context->buffer) + usedspace, SHA256_BLOCK_LENGTH - usedspace);
  769. #if BYTE_ORDER == LITTLE_ENDIAN
  770. /* Convert TO host byte order */
  771. for(int j = 0; j < 16; j++) {
  772. REVERSE32(context->buffer[j], context->buffer[j]);
  773. }
  774. #endif
  775. /* Do second-to-last transform: */
  776. sha256_Transform(context->state, context->buffer, context->state);
  777. /* And prepare the last transform: */
  778. usedspace = 0;
  779. }
  780. /* Set-up for the last transform: */
  781. memzero(((uint8_t*)context->buffer) + usedspace, SHA256_SHORT_BLOCK_LENGTH - usedspace);
  782. #if BYTE_ORDER == LITTLE_ENDIAN
  783. /* Convert TO host byte order */
  784. for(int j = 0; j < 14; j++) {
  785. REVERSE32(context->buffer[j], context->buffer[j]);
  786. }
  787. #endif
  788. /* Set the bit count: */
  789. context->buffer[14] = context->bitcount >> 32;
  790. context->buffer[15] = context->bitcount & 0xffffffff;
  791. /* Final transform: */
  792. sha256_Transform(context->state, context->buffer, context->state);
  793. #if BYTE_ORDER == LITTLE_ENDIAN
  794. /* Convert FROM host byte order */
  795. for(int j = 0; j < 8; j++) {
  796. REVERSE32(context->state[j], context->state[j]);
  797. }
  798. #endif
  799. MEMCPY_BCOPY(digest, context->state, SHA256_DIGEST_LENGTH);
  800. }
  801. /* Clean up state data: */
  802. memzero(context, sizeof(SHA256_CTX));
  803. usedspace = 0;
  804. }
  805. char* sha256_End(SHA256_CTX* context, char buffer[SHA256_DIGEST_STRING_LENGTH]) {
  806. sha2_byte digest[SHA256_DIGEST_LENGTH] = {0}, *d = digest;
  807. int i = 0;
  808. if(buffer != (char*)0) {
  809. sha256_Final(context, digest);
  810. for(i = 0; i < SHA256_DIGEST_LENGTH; i++) {
  811. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  812. *buffer++ = sha2_hex_digits[*d & 0x0f];
  813. d++;
  814. }
  815. *buffer = (char)0;
  816. } else {
  817. memzero(context, sizeof(SHA256_CTX));
  818. }
  819. memzero(digest, SHA256_DIGEST_LENGTH);
  820. return buffer;
  821. }
  822. void sha256_Raw(const sha2_byte* data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH]) {
  823. SHA256_CTX context = {0};
  824. sha256_Init(&context);
  825. sha256_Update(&context, data, len);
  826. sha256_Final(&context, digest);
  827. }
  828. char* sha256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
  829. SHA256_CTX context = {0};
  830. sha256_Init(&context);
  831. sha256_Update(&context, data, len);
  832. return sha256_End(&context, digest);
  833. }
  834. /*** SHA-512: *********************************************************/
  835. void sha512_Init(SHA512_CTX* context) {
  836. if(context == (SHA512_CTX*)0) {
  837. return;
  838. }
  839. MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
  840. memzero(context->buffer, SHA512_BLOCK_LENGTH);
  841. context->bitcount[0] = context->bitcount[1] = 0;
  842. }
  843. #ifdef SHA2_UNROLL_TRANSFORM
  844. /* Unrolled SHA-512 round macros: */
  845. #define ROUND512_0_TO_15(a, b, c, d, e, f, g, h) \
  846. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + (W512[j] = *data++); \
  847. (d) += T1; \
  848. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  849. j++
  850. #define ROUND512(a, b, c, d, e, f, g, h) \
  851. s0 = W512[(j + 1) & 0x0f]; \
  852. s0 = sigma0_512(s0); \
  853. s1 = W512[(j + 14) & 0x0f]; \
  854. s1 = sigma1_512(s1); \
  855. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
  856. (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0); \
  857. (d) += T1; \
  858. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  859. j++
  860. void sha512_Transform(const sha2_word64* state_in, const sha2_word64* data, sha2_word64* state_out) {
  861. sha2_word64 a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, s0 = 0, s1 = 0;
  862. sha2_word64 T1 = 0, W512[16] = {0};
  863. int j = 0;
  864. /* Initialize registers with the prev. intermediate value */
  865. a = state_in[0];
  866. b = state_in[1];
  867. c = state_in[2];
  868. d = state_in[3];
  869. e = state_in[4];
  870. f = state_in[5];
  871. g = state_in[6];
  872. h = state_in[7];
  873. j = 0;
  874. do {
  875. ROUND512_0_TO_15(a, b, c, d, e, f, g, h);
  876. ROUND512_0_TO_15(h, a, b, c, d, e, f, g);
  877. ROUND512_0_TO_15(g, h, a, b, c, d, e, f);
  878. ROUND512_0_TO_15(f, g, h, a, b, c, d, e);
  879. ROUND512_0_TO_15(e, f, g, h, a, b, c, d);
  880. ROUND512_0_TO_15(d, e, f, g, h, a, b, c);
  881. ROUND512_0_TO_15(c, d, e, f, g, h, a, b);
  882. ROUND512_0_TO_15(b, c, d, e, f, g, h, a);
  883. } while(j < 16);
  884. /* Now for the remaining rounds up to 79: */
  885. do {
  886. ROUND512(a, b, c, d, e, f, g, h);
  887. ROUND512(h, a, b, c, d, e, f, g);
  888. ROUND512(g, h, a, b, c, d, e, f);
  889. ROUND512(f, g, h, a, b, c, d, e);
  890. ROUND512(e, f, g, h, a, b, c, d);
  891. ROUND512(d, e, f, g, h, a, b, c);
  892. ROUND512(c, d, e, f, g, h, a, b);
  893. ROUND512(b, c, d, e, f, g, h, a);
  894. } while(j < 80);
  895. /* Compute the current intermediate hash value */
  896. state_out[0] = state_in[0] + a;
  897. state_out[1] = state_in[1] + b;
  898. state_out[2] = state_in[2] + c;
  899. state_out[3] = state_in[3] + d;
  900. state_out[4] = state_in[4] + e;
  901. state_out[5] = state_in[5] + f;
  902. state_out[6] = state_in[6] + g;
  903. state_out[7] = state_in[7] + h;
  904. /* Clean up */
  905. a = b = c = d = e = f = g = h = T1 = 0;
  906. }
  907. #else /* SHA2_UNROLL_TRANSFORM */
  908. void sha512_Transform(const sha2_word64* state_in, const sha2_word64* data, sha2_word64* state_out) {
  909. sha2_word64 a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, s0 = 0, s1 = 0;
  910. sha2_word64 T1 = 0, T2 = 0, W512[16] = {0};
  911. int j = 0;
  912. /* Initialize registers with the prev. intermediate value */
  913. a = state_in[0];
  914. b = state_in[1];
  915. c = state_in[2];
  916. d = state_in[3];
  917. e = state_in[4];
  918. f = state_in[5];
  919. g = state_in[6];
  920. h = state_in[7];
  921. j = 0;
  922. do {
  923. /* Apply the SHA-512 compression function to update a..h with copy */
  924. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
  925. T2 = Sigma0_512(a) + Maj(a, b, c);
  926. h = g;
  927. g = f;
  928. f = e;
  929. e = d + T1;
  930. d = c;
  931. c = b;
  932. b = a;
  933. a = T1 + T2;
  934. j++;
  935. } while(j < 16);
  936. do {
  937. /* Part of the message block expansion: */
  938. s0 = W512[(j + 1) & 0x0f];
  939. s0 = sigma0_512(s0);
  940. s1 = W512[(j + 14) & 0x0f];
  941. s1 = sigma1_512(s1);
  942. /* Apply the SHA-512 compression function to update a..h */
  943. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  944. (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
  945. T2 = Sigma0_512(a) + Maj(a, b, c);
  946. h = g;
  947. g = f;
  948. f = e;
  949. e = d + T1;
  950. d = c;
  951. c = b;
  952. b = a;
  953. a = T1 + T2;
  954. j++;
  955. } while(j < 80);
  956. /* Compute the current intermediate hash value */
  957. state_out[0] = state_in[0] + a;
  958. state_out[1] = state_in[1] + b;
  959. state_out[2] = state_in[2] + c;
  960. state_out[3] = state_in[3] + d;
  961. state_out[4] = state_in[4] + e;
  962. state_out[5] = state_in[5] + f;
  963. state_out[6] = state_in[6] + g;
  964. state_out[7] = state_in[7] + h;
  965. /* Clean up */
  966. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  967. }
  968. #endif /* SHA2_UNROLL_TRANSFORM */
  969. void sha512_Update(SHA512_CTX* context, const sha2_byte* data, size_t len) {
  970. unsigned int freespace = 0, usedspace = 0;
  971. if(len == 0) {
  972. /* Calling with no data is valid - we do nothing */
  973. return;
  974. }
  975. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  976. if(usedspace > 0) {
  977. /* Calculate how much free space is available in the buffer */
  978. freespace = SHA512_BLOCK_LENGTH - usedspace;
  979. if(len >= freespace) {
  980. /* Fill the buffer completely and process it */
  981. MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, freespace);
  982. ADDINC128(context->bitcount, freespace << 3);
  983. len -= freespace;
  984. data += freespace;
  985. #if BYTE_ORDER == LITTLE_ENDIAN
  986. /* Convert TO host byte order */
  987. for(int j = 0; j < 16; j++) {
  988. REVERSE64(context->buffer[j], context->buffer[j]);
  989. }
  990. #endif
  991. sha512_Transform(context->state, context->buffer, context->state);
  992. } else {
  993. /* The buffer is not yet full */
  994. MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, len);
  995. ADDINC128(context->bitcount, len << 3);
  996. /* Clean up: */
  997. usedspace = freespace = 0;
  998. return;
  999. }
  1000. }
  1001. while(len >= SHA512_BLOCK_LENGTH) {
  1002. /* Process as many complete blocks as we can */
  1003. MEMCPY_BCOPY(context->buffer, data, SHA512_BLOCK_LENGTH);
  1004. #if BYTE_ORDER == LITTLE_ENDIAN
  1005. /* Convert TO host byte order */
  1006. for(int j = 0; j < 16; j++) {
  1007. REVERSE64(context->buffer[j], context->buffer[j]);
  1008. }
  1009. #endif
  1010. sha512_Transform(context->state, context->buffer, context->state);
  1011. ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
  1012. len -= SHA512_BLOCK_LENGTH;
  1013. data += SHA512_BLOCK_LENGTH;
  1014. }
  1015. if(len > 0) {
  1016. /* There's left-overs, so save 'em */
  1017. MEMCPY_BCOPY(context->buffer, data, len);
  1018. ADDINC128(context->bitcount, len << 3);
  1019. }
  1020. /* Clean up: */
  1021. usedspace = freespace = 0;
  1022. }
  1023. static void sha512_Last(SHA512_CTX* context) {
  1024. unsigned int usedspace = 0;
  1025. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  1026. /* Begin padding with a 1 bit: */
  1027. ((uint8_t*)context->buffer)[usedspace++] = 0x80;
  1028. if(usedspace > SHA512_SHORT_BLOCK_LENGTH) {
  1029. memzero(((uint8_t*)context->buffer) + usedspace, SHA512_BLOCK_LENGTH - usedspace);
  1030. #if BYTE_ORDER == LITTLE_ENDIAN
  1031. /* Convert TO host byte order */
  1032. for(int j = 0; j < 16; j++) {
  1033. REVERSE64(context->buffer[j], context->buffer[j]);
  1034. }
  1035. #endif
  1036. /* Do second-to-last transform: */
  1037. sha512_Transform(context->state, context->buffer, context->state);
  1038. /* And prepare the last transform: */
  1039. usedspace = 0;
  1040. }
  1041. /* Set-up for the last transform: */
  1042. memzero(((uint8_t*)context->buffer) + usedspace, SHA512_SHORT_BLOCK_LENGTH - usedspace);
  1043. #if BYTE_ORDER == LITTLE_ENDIAN
  1044. /* Convert TO host byte order */
  1045. for(int j = 0; j < 14; j++) {
  1046. REVERSE64(context->buffer[j], context->buffer[j]);
  1047. }
  1048. #endif
  1049. /* Store the length of input data (in bits): */
  1050. context->buffer[14] = context->bitcount[1];
  1051. context->buffer[15] = context->bitcount[0];
  1052. /* Final transform: */
  1053. sha512_Transform(context->state, context->buffer, context->state);
  1054. }
  1055. void sha512_Final(SHA512_CTX* context, sha2_byte digest[SHA512_DIGEST_LENGTH]) {
  1056. /* If no digest buffer is passed, we don't bother doing this: */
  1057. if(digest != (sha2_byte*)0) {
  1058. sha512_Last(context);
  1059. /* Save the hash data for output: */
  1060. #if BYTE_ORDER == LITTLE_ENDIAN
  1061. /* Convert FROM host byte order */
  1062. for(int j = 0; j < 8; j++) {
  1063. REVERSE64(context->state[j], context->state[j]);
  1064. }
  1065. #endif
  1066. MEMCPY_BCOPY(digest, context->state, SHA512_DIGEST_LENGTH);
  1067. }
  1068. /* Zero out state data */
  1069. memzero(context, sizeof(SHA512_CTX));
  1070. }
  1071. char* sha512_End(SHA512_CTX* context, char buffer[SHA512_DIGEST_STRING_LENGTH]) {
  1072. sha2_byte digest[SHA512_DIGEST_LENGTH] = {0}, *d = digest;
  1073. int i = 0;
  1074. if(buffer != (char*)0) {
  1075. sha512_Final(context, digest);
  1076. for(i = 0; i < SHA512_DIGEST_LENGTH; i++) {
  1077. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  1078. *buffer++ = sha2_hex_digits[*d & 0x0f];
  1079. d++;
  1080. }
  1081. *buffer = (char)0;
  1082. } else {
  1083. memzero(context, sizeof(SHA512_CTX));
  1084. }
  1085. memzero(digest, SHA512_DIGEST_LENGTH);
  1086. return buffer;
  1087. }
  1088. void sha512_Raw(const sha2_byte* data, size_t len, uint8_t digest[SHA512_DIGEST_LENGTH]) {
  1089. SHA512_CTX context = {0};
  1090. sha512_Init(&context);
  1091. sha512_Update(&context, data, len);
  1092. sha512_Final(&context, digest);
  1093. }
  1094. char* sha512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
  1095. SHA512_CTX context = {0};
  1096. sha512_Init(&context);
  1097. sha512_Update(&context, data, len);
  1098. return sha512_End(&context, digest);
  1099. }