sha2.c 38 KB

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