sha2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * FILE: sha2.c
  3. * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
  4. *
  5. * Copyright (c) 2000-2001, Aaron D. Gifford
  6. * Copyright (c) 2015, DiUS Computing Pty Ltd (jmattsson@dius.com.au)
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holder nor the names of contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. */
  34. /* ESP8266-specific tweaks by Johny Mattsson <jmattsson@dius.com.au> */
  35. #include "user_config.h"
  36. #ifdef SHA2_ENABLE
  37. #include "sha2.h"
  38. #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
  39. #define assert(x) do {} while (0)
  40. /*
  41. * ASSERT NOTE:
  42. * Some sanity checking code is included using assert(). On my FreeBSD
  43. * system, this additional code can be removed by compiling with NDEBUG
  44. * defined. Check your own systems manpage on assert() to see how to
  45. * compile WITHOUT the sanity checking code on your system.
  46. *
  47. * UNROLLED TRANSFORM LOOP NOTE:
  48. * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
  49. * loop version for the hash transform rounds (defined using macros
  50. * later in this file). Either define on the command line, for example:
  51. *
  52. * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
  53. *
  54. * or define below:
  55. *
  56. * #define SHA2_UNROLL_TRANSFORM
  57. *
  58. */
  59. typedef uint8_t sha2_byte; /* Exactly 1 byte */
  60. typedef uint32_t sha2_word32; /* Exactly 4 bytes */
  61. typedef uint64_t sha2_word64; /* Exactly 8 bytes */
  62. /*** SHA-256/384/512 Various Length Definitions ***********************/
  63. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  64. #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
  65. #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
  66. /*** ENDIAN REVERSAL MACROS *******************************************/
  67. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  68. #define REVERSE32(w,x) { \
  69. sha2_word32 tmp = (w); \
  70. tmp = (tmp >> 16) | (tmp << 16); \
  71. (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
  72. }
  73. #define REVERSE64(w,x) { \
  74. sha2_word64 tmp = (w); \
  75. tmp = (tmp >> 32) | (tmp << 32); \
  76. tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
  77. ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
  78. (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
  79. ((tmp & 0x0000ffff0000ffffULL) << 16); \
  80. }
  81. #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
  82. /*
  83. * Macro for incrementally adding the unsigned 64-bit integer n to the
  84. * unsigned 128-bit integer (represented using a two-element array of
  85. * 64-bit words):
  86. */
  87. #define ADDINC128(w,n) { \
  88. (w)[0] += (sha2_word64)(n); \
  89. if ((w)[0] < (n)) { \
  90. (w)[1]++; \
  91. } \
  92. }
  93. /*
  94. * Macros for copying blocks of memory and for zeroing out ranges
  95. * of memory. Using these macros makes it easy to switch from
  96. * using memset()/memcpy() and using bzero()/bcopy().
  97. *
  98. * Please define either SHA2_USE_MEMSET_MEMCPY or define
  99. * SHA2_USE_BZERO_BCOPY depending on which function set you
  100. * choose to use:
  101. */
  102. #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
  103. /* Default to memset()/memcpy() if no option is specified */
  104. #define SHA2_USE_MEMSET_MEMCPY 1
  105. #endif
  106. #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
  107. /* Abort with an error if BOTH options are defined */
  108. #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
  109. #endif
  110. #ifdef SHA2_USE_MEMSET_MEMCPY
  111. #define MEMSET_BZERO(p,l) memset((p), 0, (l))
  112. #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
  113. #endif
  114. #ifdef SHA2_USE_BZERO_BCOPY
  115. #define MEMSET_BZERO(p,l) bzero((p), (l))
  116. #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
  117. #endif
  118. /*** THE SIX LOGICAL FUNCTIONS ****************************************/
  119. /*
  120. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  121. *
  122. * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
  123. * S is a ROTATION) because the SHA-256/384/512 description document
  124. * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
  125. * same "backwards" definition.
  126. */
  127. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  128. #define R(b,x) ((x) >> (b))
  129. /* 32-bit Rotate-right (used in SHA-256): */
  130. #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
  131. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  132. #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
  133. /* Two of six logical functions used in 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. /* Four of six logical functions used in SHA-256: */
  137. #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
  138. #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
  139. #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
  140. #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
  141. /* Four of six logical functions used in SHA-384 and SHA-512: */
  142. #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
  143. #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
  144. #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
  145. #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
  146. /*** INTERNAL FUNCTION PROTOTYPES *************************************/
  147. /* NOTE: These should not be accessed directly from outside this
  148. * library -- they are intended for private internal visibility/use
  149. * only.
  150. */
  151. void SHA512_Last(SHA512_CTX*);
  152. void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
  153. void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
  154. /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
  155. /* Hash constant words K for SHA-256: */
  156. const static sha2_word32 K256[64] ICACHE_RODATA_ATTR = {
  157. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  158. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  159. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  160. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  161. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  162. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  163. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  164. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  165. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  166. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  167. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  168. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  169. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  170. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  171. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  172. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  173. };
  174. /* Initial hash value H for SHA-256: */
  175. const static sha2_word32 sha256_initial_hash_value[8] ICACHE_RODATA_ATTR = {
  176. 0x6a09e667UL,
  177. 0xbb67ae85UL,
  178. 0x3c6ef372UL,
  179. 0xa54ff53aUL,
  180. 0x510e527fUL,
  181. 0x9b05688cUL,
  182. 0x1f83d9abUL,
  183. 0x5be0cd19UL
  184. };
  185. /* Hash constant words K for SHA-384 and SHA-512: */
  186. const static sha2_word64 K512[80] ICACHE_RODATA_ATTR = {
  187. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  188. 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  189. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  190. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  191. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  192. 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  193. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  194. 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  195. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  196. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  197. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  198. 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  199. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  200. 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  201. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  202. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  203. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  204. 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  205. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  206. 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  207. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  208. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  209. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  210. 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  211. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  212. 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  213. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  214. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  215. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  216. 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  217. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  218. 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  219. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  220. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  221. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  222. 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  223. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  224. 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  225. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  226. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  227. };
  228. /* Initial hash value H for SHA-384 */
  229. const static sha2_word64 sha384_initial_hash_value[8] ICACHE_RODATA_ATTR = {
  230. 0xcbbb9d5dc1059ed8ULL,
  231. 0x629a292a367cd507ULL,
  232. 0x9159015a3070dd17ULL,
  233. 0x152fecd8f70e5939ULL,
  234. 0x67332667ffc00b31ULL,
  235. 0x8eb44a8768581511ULL,
  236. 0xdb0c2e0d64f98fa7ULL,
  237. 0x47b5481dbefa4fa4ULL
  238. };
  239. /* Initial hash value H for SHA-512 */
  240. const static sha2_word64 sha512_initial_hash_value[8] ICACHE_RODATA_ATTR = {
  241. 0x6a09e667f3bcc908ULL,
  242. 0xbb67ae8584caa73bULL,
  243. 0x3c6ef372fe94f82bULL,
  244. 0xa54ff53a5f1d36f1ULL,
  245. 0x510e527fade682d1ULL,
  246. 0x9b05688c2b3e6c1fULL,
  247. 0x1f83d9abfb41bd6bULL,
  248. 0x5be0cd19137e2179ULL
  249. };
  250. /*** SHA-256: *********************************************************/
  251. void ICACHE_FLASH_ATTR SHA256_Init(SHA256_CTX* context) {
  252. if (context == (SHA256_CTX*)0) {
  253. return;
  254. }
  255. MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
  256. MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
  257. context->bitcount = 0;
  258. }
  259. #ifdef SHA2_UNROLL_TRANSFORM
  260. /* Unrolled SHA-256 round macros: */
  261. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_
  262. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  263. REVERSE32(*data++, W256[j]); \
  264. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  265. K256[j] + W256[j]; \
  266. (d) += T1; \
  267. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  268. j++
  269. #else /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  270. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  271. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  272. K256[j] + (W256[j] = *data++); \
  273. (d) += T1; \
  274. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  275. j++
  276. #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  277. #define ROUND256(a,b,c,d,e,f,g,h) \
  278. s0 = W256[(j+1)&0x0f]; \
  279. s0 = sigma0_256(s0); \
  280. s1 = W256[(j+14)&0x0f]; \
  281. s1 = sigma1_256(s1); \
  282. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
  283. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
  284. (d) += T1; \
  285. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  286. j++
  287. void ICACHE_FLASH_ATTR SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
  288. sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
  289. sha2_word32 T1, *W256;
  290. int j;
  291. W256 = (sha2_word32*)context->buffer;
  292. /* Initialize registers with the prev. intermediate value */
  293. a = context->state[0];
  294. b = context->state[1];
  295. c = context->state[2];
  296. d = context->state[3];
  297. e = context->state[4];
  298. f = context->state[5];
  299. g = context->state[6];
  300. h = context->state[7];
  301. j = 0;
  302. do {
  303. /* Rounds 0 to 15 (unrolled): */
  304. ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
  305. ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
  306. ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
  307. ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
  308. ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
  309. ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
  310. ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
  311. ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
  312. } while (j < 16);
  313. /* Now for the remaining rounds to 64: */
  314. do {
  315. ROUND256(a,b,c,d,e,f,g,h);
  316. ROUND256(h,a,b,c,d,e,f,g);
  317. ROUND256(g,h,a,b,c,d,e,f);
  318. ROUND256(f,g,h,a,b,c,d,e);
  319. ROUND256(e,f,g,h,a,b,c,d);
  320. ROUND256(d,e,f,g,h,a,b,c);
  321. ROUND256(c,d,e,f,g,h,a,b);
  322. ROUND256(b,c,d,e,f,g,h,a);
  323. } while (j < 64);
  324. /* Compute the current intermediate hash value */
  325. context->state[0] += a;
  326. context->state[1] += b;
  327. context->state[2] += c;
  328. context->state[3] += d;
  329. context->state[4] += e;
  330. context->state[5] += f;
  331. context->state[6] += g;
  332. context->state[7] += h;
  333. /* Clean up */
  334. a = b = c = d = e = f = g = h = T1 = 0;
  335. }
  336. #else /* SHA2_UNROLL_TRANSFORM */
  337. void ICACHE_FLASH_ATTR SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
  338. sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
  339. sha2_word32 T1, T2, *W256;
  340. int j;
  341. W256 = (sha2_word32*)context->buffer;
  342. /* Initialize registers with the prev. intermediate value */
  343. a = context->state[0];
  344. b = context->state[1];
  345. c = context->state[2];
  346. d = context->state[3];
  347. e = context->state[4];
  348. f = context->state[5];
  349. g = context->state[6];
  350. h = context->state[7];
  351. j = 0;
  352. do {
  353. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  354. /* Copy data while converting to host byte order */
  355. REVERSE32(*data++,W256[j]);
  356. /* Apply the SHA-256 compression function to update a..h */
  357. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
  358. #else /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  359. /* Apply the SHA-256 compression function to update a..h with copy */
  360. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
  361. #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  362. T2 = Sigma0_256(a) + Maj(a, b, c);
  363. h = g;
  364. g = f;
  365. f = e;
  366. e = d + T1;
  367. d = c;
  368. c = b;
  369. b = a;
  370. a = T1 + T2;
  371. j++;
  372. } while (j < 16);
  373. do {
  374. /* Part of the message block expansion: */
  375. s0 = W256[(j+1)&0x0f];
  376. s0 = sigma0_256(s0);
  377. s1 = W256[(j+14)&0x0f];
  378. s1 = sigma1_256(s1);
  379. /* Apply the SHA-256 compression function to update a..h */
  380. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
  381. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
  382. T2 = Sigma0_256(a) + Maj(a, b, c);
  383. h = g;
  384. g = f;
  385. f = e;
  386. e = d + T1;
  387. d = c;
  388. c = b;
  389. b = a;
  390. a = T1 + T2;
  391. j++;
  392. } while (j < 64);
  393. /* Compute the current intermediate hash value */
  394. context->state[0] += a;
  395. context->state[1] += b;
  396. context->state[2] += c;
  397. context->state[3] += d;
  398. context->state[4] += e;
  399. context->state[5] += f;
  400. context->state[6] += g;
  401. context->state[7] += h;
  402. /* Clean up */
  403. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  404. }
  405. #endif /* SHA2_UNROLL_TRANSFORM */
  406. void ICACHE_FLASH_ATTR SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
  407. unsigned int freespace, usedspace;
  408. if (len == 0) {
  409. /* Calling with no data is valid - we do nothing */
  410. return;
  411. }
  412. /* Sanity check: */
  413. assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
  414. usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
  415. if (usedspace > 0) {
  416. /* Calculate how much free space is available in the buffer */
  417. freespace = SHA256_BLOCK_LENGTH - usedspace;
  418. if (len >= freespace) {
  419. /* Fill the buffer completely and process it */
  420. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  421. context->bitcount += freespace << 3;
  422. len -= freespace;
  423. data += freespace;
  424. SHA256_Transform(context, (sha2_word32*)context->buffer);
  425. } else {
  426. /* The buffer is not yet full */
  427. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  428. context->bitcount += len << 3;
  429. /* Clean up: */
  430. usedspace = freespace = 0;
  431. return;
  432. }
  433. }
  434. while (len >= SHA256_BLOCK_LENGTH) {
  435. /* Process as many complete blocks as we can */
  436. if ((int)data & (sizeof(sha2_word32)-1))
  437. {
  438. // have to bounce via buffer, otherwise we'll hit unaligned load exception
  439. MEMCPY_BCOPY(context->buffer, data, SHA256_BLOCK_LENGTH);
  440. SHA256_Transform(context, (sha2_word32*)context->buffer);
  441. }
  442. else
  443. SHA256_Transform(context, (sha2_word32*)data);
  444. context->bitcount += SHA256_BLOCK_LENGTH << 3;
  445. len -= SHA256_BLOCK_LENGTH;
  446. data += SHA256_BLOCK_LENGTH;
  447. }
  448. if (len > 0) {
  449. /* There's left-overs, so save 'em */
  450. MEMCPY_BCOPY(context->buffer, data, len);
  451. context->bitcount += len << 3;
  452. }
  453. /* Clean up: */
  454. usedspace = freespace = 0;
  455. }
  456. void ICACHE_FLASH_ATTR SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
  457. sha2_word32 *d = (sha2_word32*)digest;
  458. unsigned int usedspace;
  459. /* Sanity check: */
  460. assert(context != (SHA256_CTX*)0);
  461. /* If no digest buffer is passed, we don't bother doing this: */
  462. if (digest != (sha2_byte*)0) {
  463. usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
  464. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  465. /* Convert FROM host byte order */
  466. REVERSE64(context->bitcount,context->bitcount);
  467. #endif
  468. if (usedspace > 0) {
  469. /* Begin padding with a 1 bit: */
  470. context->buffer[usedspace++] = 0x80;
  471. if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
  472. /* Set-up for the last transform: */
  473. MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
  474. } else {
  475. if (usedspace < SHA256_BLOCK_LENGTH) {
  476. MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
  477. }
  478. /* Do second-to-last transform: */
  479. SHA256_Transform(context, (sha2_word32*)context->buffer);
  480. /* And set-up for the last transform: */
  481. MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
  482. }
  483. } else {
  484. /* Set-up for the last transform: */
  485. MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
  486. /* Begin padding with a 1 bit: */
  487. *context->buffer = 0x80;
  488. }
  489. /* Set the bit count: */
  490. *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
  491. /* Final transform: */
  492. SHA256_Transform(context, (sha2_word32*)context->buffer);
  493. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  494. {
  495. /* Convert TO host byte order */
  496. int j;
  497. for (j = 0; j < 8; j++) {
  498. REVERSE32(context->state[j],context->state[j]);
  499. *d++ = context->state[j];
  500. }
  501. }
  502. #else
  503. MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
  504. #endif
  505. }
  506. /* Clean up state data: */
  507. MEMSET_BZERO(context, sizeof(SHA256_CTX));
  508. usedspace = 0;
  509. }
  510. /*** SHA-512: *********************************************************/
  511. void ICACHE_FLASH_ATTR SHA512_Init(SHA512_CTX* context) {
  512. if (context == (SHA512_CTX*)0) {
  513. return;
  514. }
  515. MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
  516. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
  517. context->bitcount[0] = context->bitcount[1] = 0;
  518. }
  519. #ifdef SHA2_UNROLL_TRANSFORM
  520. /* Unrolled SHA-512 round macros: */
  521. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_
  522. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  523. REVERSE64(*data++, W512[j]); \
  524. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  525. K512[j] + W512[j]; \
  526. (d) += T1, \
  527. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
  528. j++
  529. #else /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  530. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  531. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  532. K512[j] + (W512[j] = *data++); \
  533. (d) += T1; \
  534. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  535. j++
  536. #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  537. #define ROUND512(a,b,c,d,e,f,g,h) \
  538. s0 = W512[(j+1)&0x0f]; \
  539. s0 = sigma0_512(s0); \
  540. s1 = W512[(j+14)&0x0f]; \
  541. s1 = sigma1_512(s1); \
  542. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
  543. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
  544. (d) += T1; \
  545. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  546. j++
  547. void ICACHE_FLASH_ATTR SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
  548. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  549. sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
  550. int j;
  551. /* Initialize registers with the prev. intermediate value */
  552. a = context->state[0];
  553. b = context->state[1];
  554. c = context->state[2];
  555. d = context->state[3];
  556. e = context->state[4];
  557. f = context->state[5];
  558. g = context->state[6];
  559. h = context->state[7];
  560. j = 0;
  561. do {
  562. ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
  563. ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
  564. ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
  565. ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
  566. ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
  567. ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
  568. ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
  569. ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
  570. } while (j < 16);
  571. /* Now for the remaining rounds up to 79: */
  572. do {
  573. ROUND512(a,b,c,d,e,f,g,h);
  574. ROUND512(h,a,b,c,d,e,f,g);
  575. ROUND512(g,h,a,b,c,d,e,f);
  576. ROUND512(f,g,h,a,b,c,d,e);
  577. ROUND512(e,f,g,h,a,b,c,d);
  578. ROUND512(d,e,f,g,h,a,b,c);
  579. ROUND512(c,d,e,f,g,h,a,b);
  580. ROUND512(b,c,d,e,f,g,h,a);
  581. } while (j < 80);
  582. /* Compute the current intermediate hash value */
  583. context->state[0] += a;
  584. context->state[1] += b;
  585. context->state[2] += c;
  586. context->state[3] += d;
  587. context->state[4] += e;
  588. context->state[5] += f;
  589. context->state[6] += g;
  590. context->state[7] += h;
  591. /* Clean up */
  592. a = b = c = d = e = f = g = h = T1 = 0;
  593. }
  594. #else /* SHA2_UNROLL_TRANSFORM */
  595. void ICACHE_FLASH_ATTR SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
  596. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  597. sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
  598. int j;
  599. /* Initialize registers with the prev. intermediate value */
  600. a = context->state[0];
  601. b = context->state[1];
  602. c = context->state[2];
  603. d = context->state[3];
  604. e = context->state[4];
  605. f = context->state[5];
  606. g = context->state[6];
  607. h = context->state[7];
  608. j = 0;
  609. do {
  610. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  611. /* Convert TO host byte order */
  612. REVERSE64(*data++, W512[j]);
  613. /* Apply the SHA-512 compression function to update a..h */
  614. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
  615. #else /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  616. /* Apply the SHA-512 compression function to update a..h with copy */
  617. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
  618. #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN_ */
  619. T2 = Sigma0_512(a) + Maj(a, b, c);
  620. h = g;
  621. g = f;
  622. f = e;
  623. e = d + T1;
  624. d = c;
  625. c = b;
  626. b = a;
  627. a = T1 + T2;
  628. j++;
  629. } while (j < 16);
  630. do {
  631. /* Part of the message block expansion: */
  632. s0 = W512[(j+1)&0x0f];
  633. s0 = sigma0_512(s0);
  634. s1 = W512[(j+14)&0x0f];
  635. s1 = sigma1_512(s1);
  636. /* Apply the SHA-512 compression function to update a..h */
  637. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  638. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
  639. T2 = Sigma0_512(a) + Maj(a, b, c);
  640. h = g;
  641. g = f;
  642. f = e;
  643. e = d + T1;
  644. d = c;
  645. c = b;
  646. b = a;
  647. a = T1 + T2;
  648. j++;
  649. } while (j < 80);
  650. /* Compute the current intermediate hash value */
  651. context->state[0] += a;
  652. context->state[1] += b;
  653. context->state[2] += c;
  654. context->state[3] += d;
  655. context->state[4] += e;
  656. context->state[5] += f;
  657. context->state[6] += g;
  658. context->state[7] += h;
  659. /* Clean up */
  660. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  661. }
  662. #endif /* SHA2_UNROLL_TRANSFORM */
  663. void ICACHE_FLASH_ATTR SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
  664. unsigned int freespace, usedspace;
  665. if (len == 0) {
  666. /* Calling with no data is valid - we do nothing */
  667. return;
  668. }
  669. /* Sanity check: */
  670. assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
  671. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  672. if (usedspace > 0) {
  673. /* Calculate how much free space is available in the buffer */
  674. freespace = SHA512_BLOCK_LENGTH - usedspace;
  675. if (len >= freespace) {
  676. /* Fill the buffer completely and process it */
  677. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  678. ADDINC128(context->bitcount, freespace << 3);
  679. len -= freespace;
  680. data += freespace;
  681. SHA512_Transform(context, (sha2_word64*)context->buffer);
  682. } else {
  683. /* The buffer is not yet full */
  684. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  685. ADDINC128(context->bitcount, len << 3);
  686. /* Clean up: */
  687. usedspace = freespace = 0;
  688. return;
  689. }
  690. }
  691. while (len >= SHA512_BLOCK_LENGTH) {
  692. /* Process as many complete blocks as we can */
  693. if ((int)data & (sizeof(sha2_word64)-1))
  694. {
  695. // have to bounce via buffer, otherwise we'll hit unaligned load exception
  696. MEMCPY_BCOPY(context->buffer, data, SHA512_BLOCK_LENGTH);
  697. SHA512_Transform(context, (sha2_word64*)context->buffer);
  698. }
  699. else
  700. SHA512_Transform(context, (sha2_word64*)data);
  701. ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
  702. len -= SHA512_BLOCK_LENGTH;
  703. data += SHA512_BLOCK_LENGTH;
  704. }
  705. if (len > 0) {
  706. /* There's left-overs, so save 'em */
  707. MEMCPY_BCOPY(context->buffer, data, len);
  708. ADDINC128(context->bitcount, len << 3);
  709. }
  710. /* Clean up: */
  711. usedspace = freespace = 0;
  712. }
  713. void ICACHE_FLASH_ATTR SHA512_Last(SHA512_CTX* context) {
  714. unsigned int usedspace;
  715. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  716. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  717. /* Convert FROM host byte order */
  718. REVERSE64(context->bitcount[0],context->bitcount[0]);
  719. REVERSE64(context->bitcount[1],context->bitcount[1]);
  720. #endif
  721. if (usedspace > 0) {
  722. /* Begin padding with a 1 bit: */
  723. context->buffer[usedspace++] = 0x80;
  724. if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
  725. /* Set-up for the last transform: */
  726. MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
  727. } else {
  728. if (usedspace < SHA512_BLOCK_LENGTH) {
  729. MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
  730. }
  731. /* Do second-to-last transform: */
  732. SHA512_Transform(context, (sha2_word64*)context->buffer);
  733. /* And set-up for the last transform: */
  734. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
  735. }
  736. } else {
  737. /* Prepare for final transform: */
  738. MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
  739. /* Begin padding with a 1 bit: */
  740. *context->buffer = 0x80;
  741. }
  742. /* Store the length of input data (in bits): */
  743. *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
  744. *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
  745. /* Final transform: */
  746. SHA512_Transform(context, (sha2_word64*)context->buffer);
  747. }
  748. void ICACHE_FLASH_ATTR SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
  749. sha2_word64 *d = (sha2_word64*)digest;
  750. /* Sanity check: */
  751. assert(context != (SHA512_CTX*)0);
  752. /* If no digest buffer is passed, we don't bother doing this: */
  753. if (digest != (sha2_byte*)0) {
  754. SHA512_Last(context);
  755. /* Save the hash data for output: */
  756. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  757. {
  758. /* Convert TO host byte order */
  759. int j;
  760. for (j = 0; j < 8; j++) {
  761. REVERSE64(context->state[j],context->state[j]);
  762. *d++ = context->state[j];
  763. }
  764. }
  765. #else
  766. MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
  767. #endif
  768. }
  769. /* Zero out state data */
  770. MEMSET_BZERO(context, sizeof(SHA512_CTX));
  771. }
  772. /*** SHA-384: *********************************************************/
  773. void ICACHE_FLASH_ATTR SHA384_Init(SHA384_CTX* context) {
  774. if (context == (SHA384_CTX*)0) {
  775. return;
  776. }
  777. MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
  778. MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
  779. context->bitcount[0] = context->bitcount[1] = 0;
  780. }
  781. void ICACHE_FLASH_ATTR SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
  782. SHA512_Update((SHA512_CTX*)context, data, len);
  783. }
  784. void ICACHE_FLASH_ATTR SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
  785. sha2_word64 *d = (sha2_word64*)digest;
  786. /* Sanity check: */
  787. assert(context != (SHA384_CTX*)0);
  788. /* If no digest buffer is passed, we don't bother doing this: */
  789. if (digest != (sha2_byte*)0) {
  790. SHA512_Last((SHA512_CTX*)context);
  791. /* Save the hash data for output: */
  792. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  793. {
  794. /* Convert TO host byte order */
  795. int j;
  796. for (j = 0; j < 6; j++) {
  797. REVERSE64(context->state[j],context->state[j]);
  798. *d++ = context->state[j];
  799. }
  800. }
  801. #else
  802. MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
  803. #endif
  804. }
  805. /* Zero out state data */
  806. MEMSET_BZERO(context, sizeof(SHA384_CTX));
  807. }
  808. #endif // SHA2_ENABLE