sha2.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // /*
  2. // * FIPS 180-2 SHA-224/256/384/512 implementation
  3. // * Last update: 02/02/2007
  4. // * Issue date: 04/30/2005
  5. // *
  6. // * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch>
  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 project nor the names of its 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 PROJECT AND CONTRIBUTORS ``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 PROJECT OR CONTRIBUTORS 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. // #if 0
  34. // #define UNROLL_LOOPS /* Enable loops unrolling */
  35. // #endif
  36. // #include <string.h>
  37. // #include "sha2.h"
  38. // #define SHFR(x, n) (x >> n)
  39. // #define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
  40. // #define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
  41. // #define CH(x, y, z) ((x & y) ^ (~x & z))
  42. // #define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  43. // #define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  44. // #define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  45. // #define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3))
  46. // #define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10))
  47. // #define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
  48. // #define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
  49. // #define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7))
  50. // #define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6))
  51. // #define UNPACK32(x, str) \
  52. // { \
  53. // *((str) + 3) = (uint8) ((x) ); \
  54. // *((str) + 2) = (uint8) ((x) >> 8); \
  55. // *((str) + 1) = (uint8) ((x) >> 16); \
  56. // *((str) + 0) = (uint8) ((x) >> 24); \
  57. // }
  58. // #define PACK32(str, x) \
  59. // { \
  60. // *(x) = ((uint32) *((str) + 3) ) \
  61. // | ((uint32) *((str) + 2) << 8) \
  62. // | ((uint32) *((str) + 1) << 16) \
  63. // | ((uint32) *((str) + 0) << 24); \
  64. // }
  65. // #define UNPACK64(x, str) \
  66. // { \
  67. // *((str) + 7) = (uint8) ((x) ); \
  68. // *((str) + 6) = (uint8) ((x) >> 8); \
  69. // *((str) + 5) = (uint8) ((x) >> 16); \
  70. // *((str) + 4) = (uint8) ((x) >> 24); \
  71. // *((str) + 3) = (uint8) ((x) >> 32); \
  72. // *((str) + 2) = (uint8) ((x) >> 40); \
  73. // *((str) + 1) = (uint8) ((x) >> 48); \
  74. // *((str) + 0) = (uint8) ((x) >> 56); \
  75. // }
  76. // #define PACK64(str, x) \
  77. // { \
  78. // *(x) = ((uint64) *((str) + 7) ) \
  79. // | ((uint64) *((str) + 6) << 8) \
  80. // | ((uint64) *((str) + 5) << 16) \
  81. // | ((uint64) *((str) + 4) << 24) \
  82. // | ((uint64) *((str) + 3) << 32) \
  83. // | ((uint64) *((str) + 2) << 40) \
  84. // | ((uint64) *((str) + 1) << 48) \
  85. // | ((uint64) *((str) + 0) << 56); \
  86. // }
  87. // /* Macros used for loops unrolling */
  88. // #define SHA256_SCR(i) \
  89. // { \
  90. // w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \
  91. // + SHA256_F3(w[i - 15]) + w[i - 16]; \
  92. // }
  93. // #define SHA512_SCR(i) \
  94. // { \
  95. // w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \
  96. // + SHA512_F3(w[i - 15]) + w[i - 16]; \
  97. // }
  98. // #define SHA256_EXP(a, b, c, d, e, f, g, h, j) \
  99. // { \
  100. // t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
  101. // + sha256_k[j] + w[j]; \
  102. // t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
  103. // wv[d] += t1; \
  104. // wv[h] = t1 + t2; \
  105. // }
  106. // #define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \
  107. // { \
  108. // t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
  109. // + sha512_k[j] + w[j]; \
  110. // t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
  111. // wv[d] += t1; \
  112. // wv[h] = t1 + t2; \
  113. // }
  114. // uint32 sha224_h0[8] =
  115. // {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
  116. // 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4};
  117. // uint32 sha256_h0[8] =
  118. // {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  119. // 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
  120. // uint64 sha384_h0[8] =
  121. // {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
  122. // 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
  123. // 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
  124. // 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
  125. // uint64 sha512_h0[8] =
  126. // {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
  127. // 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
  128. // 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
  129. // 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
  130. // uint32 sha256_k[64] =
  131. // {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  132. // 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  133. // 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  134. // 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  135. // 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  136. // 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  137. // 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  138. // 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  139. // 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  140. // 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  141. // 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  142. // 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  143. // 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  144. // 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  145. // 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  146. // 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
  147. // uint64 sha512_k[80] =
  148. // {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  149. // 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  150. // 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  151. // 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  152. // 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  153. // 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  154. // 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  155. // 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  156. // 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  157. // 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  158. // 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  159. // 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  160. // 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  161. // 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  162. // 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  163. // 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  164. // 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  165. // 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  166. // 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  167. // 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  168. // 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  169. // 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  170. // 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  171. // 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  172. // 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  173. // 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  174. // 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  175. // 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  176. // 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  177. // 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  178. // 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  179. // 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  180. // 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  181. // 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  182. // 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  183. // 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  184. // 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  185. // 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  186. // 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  187. // 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
  188. // /* SHA-256 functions */
  189. // void sha256_transf(sha256_ctx *ctx, const unsigned char *message,
  190. // unsigned int block_nb)
  191. // {
  192. // uint32 w[64];
  193. // uint32 wv[8];
  194. // uint32 t1, t2;
  195. // const unsigned char *sub_block;
  196. // int i;
  197. // #ifndef UNROLL_LOOPS
  198. // int j;
  199. // #endif
  200. // for (i = 0; i < (int) block_nb; i++) {
  201. // sub_block = message + (i << 6);
  202. // #ifndef UNROLL_LOOPS
  203. // for (j = 0; j < 16; j++) {
  204. // PACK32(&sub_block[j << 2], &w[j]);
  205. // }
  206. // for (j = 16; j < 64; j++) {
  207. // SHA256_SCR(j);
  208. // }
  209. // for (j = 0; j < 8; j++) {
  210. // wv[j] = ctx->h[j];
  211. // }
  212. // for (j = 0; j < 64; j++) {
  213. // t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
  214. // + sha256_k[j] + w[j];
  215. // t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
  216. // wv[7] = wv[6];
  217. // wv[6] = wv[5];
  218. // wv[5] = wv[4];
  219. // wv[4] = wv[3] + t1;
  220. // wv[3] = wv[2];
  221. // wv[2] = wv[1];
  222. // wv[1] = wv[0];
  223. // wv[0] = t1 + t2;
  224. // }
  225. // for (j = 0; j < 8; j++) {
  226. // ctx->h[j] += wv[j];
  227. // }
  228. // #else
  229. // PACK32(&sub_block[ 0], &w[ 0]); PACK32(&sub_block[ 4], &w[ 1]);
  230. // PACK32(&sub_block[ 8], &w[ 2]); PACK32(&sub_block[12], &w[ 3]);
  231. // PACK32(&sub_block[16], &w[ 4]); PACK32(&sub_block[20], &w[ 5]);
  232. // PACK32(&sub_block[24], &w[ 6]); PACK32(&sub_block[28], &w[ 7]);
  233. // PACK32(&sub_block[32], &w[ 8]); PACK32(&sub_block[36], &w[ 9]);
  234. // PACK32(&sub_block[40], &w[10]); PACK32(&sub_block[44], &w[11]);
  235. // PACK32(&sub_block[48], &w[12]); PACK32(&sub_block[52], &w[13]);
  236. // PACK32(&sub_block[56], &w[14]); PACK32(&sub_block[60], &w[15]);
  237. // SHA256_SCR(16); SHA256_SCR(17); SHA256_SCR(18); SHA256_SCR(19);
  238. // SHA256_SCR(20); SHA256_SCR(21); SHA256_SCR(22); SHA256_SCR(23);
  239. // SHA256_SCR(24); SHA256_SCR(25); SHA256_SCR(26); SHA256_SCR(27);
  240. // SHA256_SCR(28); SHA256_SCR(29); SHA256_SCR(30); SHA256_SCR(31);
  241. // SHA256_SCR(32); SHA256_SCR(33); SHA256_SCR(34); SHA256_SCR(35);
  242. // SHA256_SCR(36); SHA256_SCR(37); SHA256_SCR(38); SHA256_SCR(39);
  243. // SHA256_SCR(40); SHA256_SCR(41); SHA256_SCR(42); SHA256_SCR(43);
  244. // SHA256_SCR(44); SHA256_SCR(45); SHA256_SCR(46); SHA256_SCR(47);
  245. // SHA256_SCR(48); SHA256_SCR(49); SHA256_SCR(50); SHA256_SCR(51);
  246. // SHA256_SCR(52); SHA256_SCR(53); SHA256_SCR(54); SHA256_SCR(55);
  247. // SHA256_SCR(56); SHA256_SCR(57); SHA256_SCR(58); SHA256_SCR(59);
  248. // SHA256_SCR(60); SHA256_SCR(61); SHA256_SCR(62); SHA256_SCR(63);
  249. // wv[0] = ctx->h[0]; wv[1] = ctx->h[1];
  250. // wv[2] = ctx->h[2]; wv[3] = ctx->h[3];
  251. // wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
  252. // wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
  253. // SHA256_EXP(0,1,2,3,4,5,6,7, 0); SHA256_EXP(7,0,1,2,3,4,5,6, 1);
  254. // SHA256_EXP(6,7,0,1,2,3,4,5, 2); SHA256_EXP(5,6,7,0,1,2,3,4, 3);
  255. // SHA256_EXP(4,5,6,7,0,1,2,3, 4); SHA256_EXP(3,4,5,6,7,0,1,2, 5);
  256. // SHA256_EXP(2,3,4,5,6,7,0,1, 6); SHA256_EXP(1,2,3,4,5,6,7,0, 7);
  257. // SHA256_EXP(0,1,2,3,4,5,6,7, 8); SHA256_EXP(7,0,1,2,3,4,5,6, 9);
  258. // SHA256_EXP(6,7,0,1,2,3,4,5,10); SHA256_EXP(5,6,7,0,1,2,3,4,11);
  259. // SHA256_EXP(4,5,6,7,0,1,2,3,12); SHA256_EXP(3,4,5,6,7,0,1,2,13);
  260. // SHA256_EXP(2,3,4,5,6,7,0,1,14); SHA256_EXP(1,2,3,4,5,6,7,0,15);
  261. // SHA256_EXP(0,1,2,3,4,5,6,7,16); SHA256_EXP(7,0,1,2,3,4,5,6,17);
  262. // SHA256_EXP(6,7,0,1,2,3,4,5,18); SHA256_EXP(5,6,7,0,1,2,3,4,19);
  263. // SHA256_EXP(4,5,6,7,0,1,2,3,20); SHA256_EXP(3,4,5,6,7,0,1,2,21);
  264. // SHA256_EXP(2,3,4,5,6,7,0,1,22); SHA256_EXP(1,2,3,4,5,6,7,0,23);
  265. // SHA256_EXP(0,1,2,3,4,5,6,7,24); SHA256_EXP(7,0,1,2,3,4,5,6,25);
  266. // SHA256_EXP(6,7,0,1,2,3,4,5,26); SHA256_EXP(5,6,7,0,1,2,3,4,27);
  267. // SHA256_EXP(4,5,6,7,0,1,2,3,28); SHA256_EXP(3,4,5,6,7,0,1,2,29);
  268. // SHA256_EXP(2,3,4,5,6,7,0,1,30); SHA256_EXP(1,2,3,4,5,6,7,0,31);
  269. // SHA256_EXP(0,1,2,3,4,5,6,7,32); SHA256_EXP(7,0,1,2,3,4,5,6,33);
  270. // SHA256_EXP(6,7,0,1,2,3,4,5,34); SHA256_EXP(5,6,7,0,1,2,3,4,35);
  271. // SHA256_EXP(4,5,6,7,0,1,2,3,36); SHA256_EXP(3,4,5,6,7,0,1,2,37);
  272. // SHA256_EXP(2,3,4,5,6,7,0,1,38); SHA256_EXP(1,2,3,4,5,6,7,0,39);
  273. // SHA256_EXP(0,1,2,3,4,5,6,7,40); SHA256_EXP(7,0,1,2,3,4,5,6,41);
  274. // SHA256_EXP(6,7,0,1,2,3,4,5,42); SHA256_EXP(5,6,7,0,1,2,3,4,43);
  275. // SHA256_EXP(4,5,6,7,0,1,2,3,44); SHA256_EXP(3,4,5,6,7,0,1,2,45);
  276. // SHA256_EXP(2,3,4,5,6,7,0,1,46); SHA256_EXP(1,2,3,4,5,6,7,0,47);
  277. // SHA256_EXP(0,1,2,3,4,5,6,7,48); SHA256_EXP(7,0,1,2,3,4,5,6,49);
  278. // SHA256_EXP(6,7,0,1,2,3,4,5,50); SHA256_EXP(5,6,7,0,1,2,3,4,51);
  279. // SHA256_EXP(4,5,6,7,0,1,2,3,52); SHA256_EXP(3,4,5,6,7,0,1,2,53);
  280. // SHA256_EXP(2,3,4,5,6,7,0,1,54); SHA256_EXP(1,2,3,4,5,6,7,0,55);
  281. // SHA256_EXP(0,1,2,3,4,5,6,7,56); SHA256_EXP(7,0,1,2,3,4,5,6,57);
  282. // SHA256_EXP(6,7,0,1,2,3,4,5,58); SHA256_EXP(5,6,7,0,1,2,3,4,59);
  283. // SHA256_EXP(4,5,6,7,0,1,2,3,60); SHA256_EXP(3,4,5,6,7,0,1,2,61);
  284. // SHA256_EXP(2,3,4,5,6,7,0,1,62); SHA256_EXP(1,2,3,4,5,6,7,0,63);
  285. // ctx->h[0] += wv[0]; ctx->h[1] += wv[1];
  286. // ctx->h[2] += wv[2]; ctx->h[3] += wv[3];
  287. // ctx->h[4] += wv[4]; ctx->h[5] += wv[5];
  288. // ctx->h[6] += wv[6]; ctx->h[7] += wv[7];
  289. // #endif /* !UNROLL_LOOPS */
  290. // }
  291. // }
  292. // void sha256(const unsigned char *message, unsigned int len, unsigned char *digest)
  293. // {
  294. // sha256_ctx ctx;
  295. // sha256_init(&ctx);
  296. // sha256_update(&ctx, message, len);
  297. // sha256_final(&ctx, digest);
  298. // }
  299. // void sha256_init(sha256_ctx *ctx)
  300. // {
  301. // #ifndef UNROLL_LOOPS
  302. // int i;
  303. // for (i = 0; i < 8; i++) {
  304. // ctx->h[i] = sha256_h0[i];
  305. // }
  306. // #else
  307. // ctx->h[0] = sha256_h0[0]; ctx->h[1] = sha256_h0[1];
  308. // ctx->h[2] = sha256_h0[2]; ctx->h[3] = sha256_h0[3];
  309. // ctx->h[4] = sha256_h0[4]; ctx->h[5] = sha256_h0[5];
  310. // ctx->h[6] = sha256_h0[6]; ctx->h[7] = sha256_h0[7];
  311. // #endif /* !UNROLL_LOOPS */
  312. // ctx->len = 0;
  313. // ctx->tot_len = 0;
  314. // }
  315. // void sha256_update(sha256_ctx *ctx, const unsigned char *message,
  316. // unsigned int len)
  317. // {
  318. // unsigned int block_nb;
  319. // unsigned int new_len, rem_len, tmp_len;
  320. // const unsigned char *shifted_message;
  321. // tmp_len = SHA256_BLOCK_SIZE - ctx->len;
  322. // rem_len = len < tmp_len ? len : tmp_len;
  323. // memcpy(&ctx->block[ctx->len], message, rem_len);
  324. // if (ctx->len + len < SHA256_BLOCK_SIZE) {
  325. // ctx->len += len;
  326. // return;
  327. // }
  328. // new_len = len - rem_len;
  329. // block_nb = new_len / SHA256_BLOCK_SIZE;
  330. // shifted_message = message + rem_len;
  331. // sha256_transf(ctx, ctx->block, 1);
  332. // sha256_transf(ctx, shifted_message, block_nb);
  333. // rem_len = new_len % SHA256_BLOCK_SIZE;
  334. // memcpy(ctx->block, &shifted_message[block_nb << 6],
  335. // rem_len);
  336. // ctx->len = rem_len;
  337. // ctx->tot_len += (block_nb + 1) << 6;
  338. // }
  339. // void sha256_final(sha256_ctx *ctx, unsigned char *digest)
  340. // {
  341. // unsigned int block_nb;
  342. // unsigned int pm_len;
  343. // unsigned int len_b;
  344. // #ifndef UNROLL_LOOPS
  345. // int i;
  346. // #endif
  347. // block_nb = (1 + ((SHA256_BLOCK_SIZE - 9)
  348. // < (ctx->len % SHA256_BLOCK_SIZE)));
  349. // len_b = (ctx->tot_len + ctx->len) << 3;
  350. // pm_len = block_nb << 6;
  351. // memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
  352. // ctx->block[ctx->len] = 0x80;
  353. // UNPACK32(len_b, ctx->block + pm_len - 4);
  354. // sha256_transf(ctx, ctx->block, block_nb);
  355. // #ifndef UNROLL_LOOPS
  356. // for (i = 0 ; i < 8; i++) {
  357. // UNPACK32(ctx->h[i], &digest[i << 2]);
  358. // }
  359. // #else
  360. // UNPACK32(ctx->h[0], &digest[ 0]);
  361. // UNPACK32(ctx->h[1], &digest[ 4]);
  362. // UNPACK32(ctx->h[2], &digest[ 8]);
  363. // UNPACK32(ctx->h[3], &digest[12]);
  364. // UNPACK32(ctx->h[4], &digest[16]);
  365. // UNPACK32(ctx->h[5], &digest[20]);
  366. // UNPACK32(ctx->h[6], &digest[24]);
  367. // UNPACK32(ctx->h[7], &digest[28]);
  368. // #endif /* !UNROLL_LOOPS */
  369. // }
  370. // /* SHA-512 functions */
  371. // void sha512_transf(sha512_ctx *ctx, const unsigned char *message,
  372. // unsigned int block_nb)
  373. // {
  374. // uint64 w[80];
  375. // uint64 wv[8];
  376. // uint64 t1, t2;
  377. // const unsigned char *sub_block;
  378. // int i, j;
  379. // for (i = 0; i < (int) block_nb; i++) {
  380. // sub_block = message + (i << 7);
  381. // #ifndef UNROLL_LOOPS
  382. // for (j = 0; j < 16; j++) {
  383. // PACK64(&sub_block[j << 3], &w[j]);
  384. // }
  385. // for (j = 16; j < 80; j++) {
  386. // SHA512_SCR(j);
  387. // }
  388. // for (j = 0; j < 8; j++) {
  389. // wv[j] = ctx->h[j];
  390. // }
  391. // for (j = 0; j < 80; j++) {
  392. // t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
  393. // + sha512_k[j] + w[j];
  394. // t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
  395. // wv[7] = wv[6];
  396. // wv[6] = wv[5];
  397. // wv[5] = wv[4];
  398. // wv[4] = wv[3] + t1;
  399. // wv[3] = wv[2];
  400. // wv[2] = wv[1];
  401. // wv[1] = wv[0];
  402. // wv[0] = t1 + t2;
  403. // }
  404. // for (j = 0; j < 8; j++) {
  405. // ctx->h[j] += wv[j];
  406. // }
  407. // #else
  408. // PACK64(&sub_block[ 0], &w[ 0]); PACK64(&sub_block[ 8], &w[ 1]);
  409. // PACK64(&sub_block[ 16], &w[ 2]); PACK64(&sub_block[ 24], &w[ 3]);
  410. // PACK64(&sub_block[ 32], &w[ 4]); PACK64(&sub_block[ 40], &w[ 5]);
  411. // PACK64(&sub_block[ 48], &w[ 6]); PACK64(&sub_block[ 56], &w[ 7]);
  412. // PACK64(&sub_block[ 64], &w[ 8]); PACK64(&sub_block[ 72], &w[ 9]);
  413. // PACK64(&sub_block[ 80], &w[10]); PACK64(&sub_block[ 88], &w[11]);
  414. // PACK64(&sub_block[ 96], &w[12]); PACK64(&sub_block[104], &w[13]);
  415. // PACK64(&sub_block[112], &w[14]); PACK64(&sub_block[120], &w[15]);
  416. // SHA512_SCR(16); SHA512_SCR(17); SHA512_SCR(18); SHA512_SCR(19);
  417. // SHA512_SCR(20); SHA512_SCR(21); SHA512_SCR(22); SHA512_SCR(23);
  418. // SHA512_SCR(24); SHA512_SCR(25); SHA512_SCR(26); SHA512_SCR(27);
  419. // SHA512_SCR(28); SHA512_SCR(29); SHA512_SCR(30); SHA512_SCR(31);
  420. // SHA512_SCR(32); SHA512_SCR(33); SHA512_SCR(34); SHA512_SCR(35);
  421. // SHA512_SCR(36); SHA512_SCR(37); SHA512_SCR(38); SHA512_SCR(39);
  422. // SHA512_SCR(40); SHA512_SCR(41); SHA512_SCR(42); SHA512_SCR(43);
  423. // SHA512_SCR(44); SHA512_SCR(45); SHA512_SCR(46); SHA512_SCR(47);
  424. // SHA512_SCR(48); SHA512_SCR(49); SHA512_SCR(50); SHA512_SCR(51);
  425. // SHA512_SCR(52); SHA512_SCR(53); SHA512_SCR(54); SHA512_SCR(55);
  426. // SHA512_SCR(56); SHA512_SCR(57); SHA512_SCR(58); SHA512_SCR(59);
  427. // SHA512_SCR(60); SHA512_SCR(61); SHA512_SCR(62); SHA512_SCR(63);
  428. // SHA512_SCR(64); SHA512_SCR(65); SHA512_SCR(66); SHA512_SCR(67);
  429. // SHA512_SCR(68); SHA512_SCR(69); SHA512_SCR(70); SHA512_SCR(71);
  430. // SHA512_SCR(72); SHA512_SCR(73); SHA512_SCR(74); SHA512_SCR(75);
  431. // SHA512_SCR(76); SHA512_SCR(77); SHA512_SCR(78); SHA512_SCR(79);
  432. // wv[0] = ctx->h[0]; wv[1] = ctx->h[1];
  433. // wv[2] = ctx->h[2]; wv[3] = ctx->h[3];
  434. // wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
  435. // wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
  436. // j = 0;
  437. // do {
  438. // SHA512_EXP(0,1,2,3,4,5,6,7,j); j++;
  439. // SHA512_EXP(7,0,1,2,3,4,5,6,j); j++;
  440. // SHA512_EXP(6,7,0,1,2,3,4,5,j); j++;
  441. // SHA512_EXP(5,6,7,0,1,2,3,4,j); j++;
  442. // SHA512_EXP(4,5,6,7,0,1,2,3,j); j++;
  443. // SHA512_EXP(3,4,5,6,7,0,1,2,j); j++;
  444. // SHA512_EXP(2,3,4,5,6,7,0,1,j); j++;
  445. // SHA512_EXP(1,2,3,4,5,6,7,0,j); j++;
  446. // } while (j < 80);
  447. // ctx->h[0] += wv[0]; ctx->h[1] += wv[1];
  448. // ctx->h[2] += wv[2]; ctx->h[3] += wv[3];
  449. // ctx->h[4] += wv[4]; ctx->h[5] += wv[5];
  450. // ctx->h[6] += wv[6]; ctx->h[7] += wv[7];
  451. // #endif /* !UNROLL_LOOPS */
  452. // }
  453. // }
  454. // void sha512(const unsigned char *message, unsigned int len,
  455. // unsigned char *digest)
  456. // {
  457. // sha512_ctx ctx;
  458. // sha512_init(&ctx);
  459. // sha512_update(&ctx, message, len);
  460. // sha512_final(&ctx, digest);
  461. // }
  462. // void sha512_init(sha512_ctx *ctx)
  463. // {
  464. // #ifndef UNROLL_LOOPS
  465. // int i;
  466. // for (i = 0; i < 8; i++) {
  467. // ctx->h[i] = sha512_h0[i];
  468. // }
  469. // #else
  470. // ctx->h[0] = sha512_h0[0]; ctx->h[1] = sha512_h0[1];
  471. // ctx->h[2] = sha512_h0[2]; ctx->h[3] = sha512_h0[3];
  472. // ctx->h[4] = sha512_h0[4]; ctx->h[5] = sha512_h0[5];
  473. // ctx->h[6] = sha512_h0[6]; ctx->h[7] = sha512_h0[7];
  474. // #endif /* !UNROLL_LOOPS */
  475. // ctx->len = 0;
  476. // ctx->tot_len = 0;
  477. // }
  478. // void sha512_update(sha512_ctx *ctx, const unsigned char *message,
  479. // unsigned int len)
  480. // {
  481. // unsigned int block_nb;
  482. // unsigned int new_len, rem_len, tmp_len;
  483. // const unsigned char *shifted_message;
  484. // tmp_len = SHA512_BLOCK_SIZE - ctx->len;
  485. // rem_len = len < tmp_len ? len : tmp_len;
  486. // memcpy(&ctx->block[ctx->len], message, rem_len);
  487. // if (ctx->len + len < SHA512_BLOCK_SIZE) {
  488. // ctx->len += len;
  489. // return;
  490. // }
  491. // new_len = len - rem_len;
  492. // block_nb = new_len / SHA512_BLOCK_SIZE;
  493. // shifted_message = message + rem_len;
  494. // sha512_transf(ctx, ctx->block, 1);
  495. // sha512_transf(ctx, shifted_message, block_nb);
  496. // rem_len = new_len % SHA512_BLOCK_SIZE;
  497. // memcpy(ctx->block, &shifted_message[block_nb << 7],
  498. // rem_len);
  499. // ctx->len = rem_len;
  500. // ctx->tot_len += (block_nb + 1) << 7;
  501. // }
  502. // void sha512_final(sha512_ctx *ctx, unsigned char *digest)
  503. // {
  504. // unsigned int block_nb;
  505. // unsigned int pm_len;
  506. // unsigned int len_b;
  507. // #ifndef UNROLL_LOOPS
  508. // int i;
  509. // #endif
  510. // block_nb = 1 + ((SHA512_BLOCK_SIZE - 17)
  511. // < (ctx->len % SHA512_BLOCK_SIZE));
  512. // len_b = (ctx->tot_len + ctx->len) << 3;
  513. // pm_len = block_nb << 7;
  514. // memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
  515. // ctx->block[ctx->len] = 0x80;
  516. // UNPACK32(len_b, ctx->block + pm_len - 4);
  517. // sha512_transf(ctx, ctx->block, block_nb);
  518. // #ifndef UNROLL_LOOPS
  519. // for (i = 0 ; i < 8; i++) {
  520. // UNPACK64(ctx->h[i], &digest[i << 3]);
  521. // }
  522. // #else
  523. // UNPACK64(ctx->h[0], &digest[ 0]);
  524. // UNPACK64(ctx->h[1], &digest[ 8]);
  525. // UNPACK64(ctx->h[2], &digest[16]);
  526. // UNPACK64(ctx->h[3], &digest[24]);
  527. // UNPACK64(ctx->h[4], &digest[32]);
  528. // UNPACK64(ctx->h[5], &digest[40]);
  529. // UNPACK64(ctx->h[6], &digest[48]);
  530. // UNPACK64(ctx->h[7], &digest[56]);
  531. // #endif /* !UNROLL_LOOPS */
  532. // }
  533. // /* SHA-384 functions */
  534. // void sha384(const unsigned char *message, unsigned int len,
  535. // unsigned char *digest)
  536. // {
  537. // sha384_ctx ctx;
  538. // sha384_init(&ctx);
  539. // sha384_update(&ctx, message, len);
  540. // sha384_final(&ctx, digest);
  541. // }
  542. // void sha384_init(sha384_ctx *ctx)
  543. // {
  544. // #ifndef UNROLL_LOOPS
  545. // int i;
  546. // for (i = 0; i < 8; i++) {
  547. // ctx->h[i] = sha384_h0[i];
  548. // }
  549. // #else
  550. // ctx->h[0] = sha384_h0[0]; ctx->h[1] = sha384_h0[1];
  551. // ctx->h[2] = sha384_h0[2]; ctx->h[3] = sha384_h0[3];
  552. // ctx->h[4] = sha384_h0[4]; ctx->h[5] = sha384_h0[5];
  553. // ctx->h[6] = sha384_h0[6]; ctx->h[7] = sha384_h0[7];
  554. // #endif /* !UNROLL_LOOPS */
  555. // ctx->len = 0;
  556. // ctx->tot_len = 0;
  557. // }
  558. // void sha384_update(sha384_ctx *ctx, const unsigned char *message,
  559. // unsigned int len)
  560. // {
  561. // unsigned int block_nb;
  562. // unsigned int new_len, rem_len, tmp_len;
  563. // const unsigned char *shifted_message;
  564. // tmp_len = SHA384_BLOCK_SIZE - ctx->len;
  565. // rem_len = len < tmp_len ? len : tmp_len;
  566. // memcpy(&ctx->block[ctx->len], message, rem_len);
  567. // if (ctx->len + len < SHA384_BLOCK_SIZE) {
  568. // ctx->len += len;
  569. // return;
  570. // }
  571. // new_len = len - rem_len;
  572. // block_nb = new_len / SHA384_BLOCK_SIZE;
  573. // shifted_message = message + rem_len;
  574. // sha512_transf(ctx, ctx->block, 1);
  575. // sha512_transf(ctx, shifted_message, block_nb);
  576. // rem_len = new_len % SHA384_BLOCK_SIZE;
  577. // memcpy(ctx->block, &shifted_message[block_nb << 7],
  578. // rem_len);
  579. // ctx->len = rem_len;
  580. // ctx->tot_len += (block_nb + 1) << 7;
  581. // }
  582. // void sha384_final(sha384_ctx *ctx, unsigned char *digest)
  583. // {
  584. // unsigned int block_nb;
  585. // unsigned int pm_len;
  586. // unsigned int len_b;
  587. // #ifndef UNROLL_LOOPS
  588. // int i;
  589. // #endif
  590. // block_nb = (1 + ((SHA384_BLOCK_SIZE - 17)
  591. // < (ctx->len % SHA384_BLOCK_SIZE)));
  592. // len_b = (ctx->tot_len + ctx->len) << 3;
  593. // pm_len = block_nb << 7;
  594. // memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
  595. // ctx->block[ctx->len] = 0x80;
  596. // UNPACK32(len_b, ctx->block + pm_len - 4);
  597. // sha512_transf(ctx, ctx->block, block_nb);
  598. // #ifndef UNROLL_LOOPS
  599. // for (i = 0 ; i < 6; i++) {
  600. // UNPACK64(ctx->h[i], &digest[i << 3]);
  601. // }
  602. // #else
  603. // UNPACK64(ctx->h[0], &digest[ 0]);
  604. // UNPACK64(ctx->h[1], &digest[ 8]);
  605. // UNPACK64(ctx->h[2], &digest[16]);
  606. // UNPACK64(ctx->h[3], &digest[24]);
  607. // UNPACK64(ctx->h[4], &digest[32]);
  608. // UNPACK64(ctx->h[5], &digest[40]);
  609. // #endif /* !UNROLL_LOOPS */
  610. // }
  611. // /* SHA-224 functions */
  612. // void sha224(const unsigned char *message, unsigned int len,
  613. // unsigned char *digest)
  614. // {
  615. // sha224_ctx ctx;
  616. // sha224_init(&ctx);
  617. // sha224_update(&ctx, message, len);
  618. // sha224_final(&ctx, digest);
  619. // }
  620. // void sha224_init(sha224_ctx *ctx)
  621. // {
  622. // #ifndef UNROLL_LOOPS
  623. // int i;
  624. // for (i = 0; i < 8; i++) {
  625. // ctx->h[i] = sha224_h0[i];
  626. // }
  627. // #else
  628. // ctx->h[0] = sha224_h0[0]; ctx->h[1] = sha224_h0[1];
  629. // ctx->h[2] = sha224_h0[2]; ctx->h[3] = sha224_h0[3];
  630. // ctx->h[4] = sha224_h0[4]; ctx->h[5] = sha224_h0[5];
  631. // ctx->h[6] = sha224_h0[6]; ctx->h[7] = sha224_h0[7];
  632. // #endif /* !UNROLL_LOOPS */
  633. // ctx->len = 0;
  634. // ctx->tot_len = 0;
  635. // }
  636. // void sha224_update(sha224_ctx *ctx, const unsigned char *message,
  637. // unsigned int len)
  638. // {
  639. // unsigned int block_nb;
  640. // unsigned int new_len, rem_len, tmp_len;
  641. // const unsigned char *shifted_message;
  642. // tmp_len = SHA224_BLOCK_SIZE - ctx->len;
  643. // rem_len = len < tmp_len ? len : tmp_len;
  644. // memcpy(&ctx->block[ctx->len], message, rem_len);
  645. // if (ctx->len + len < SHA224_BLOCK_SIZE) {
  646. // ctx->len += len;
  647. // return;
  648. // }
  649. // new_len = len - rem_len;
  650. // block_nb = new_len / SHA224_BLOCK_SIZE;
  651. // shifted_message = message + rem_len;
  652. // sha256_transf(ctx, ctx->block, 1);
  653. // sha256_transf(ctx, shifted_message, block_nb);
  654. // rem_len = new_len % SHA224_BLOCK_SIZE;
  655. // memcpy(ctx->block, &shifted_message[block_nb << 6],
  656. // rem_len);
  657. // ctx->len = rem_len;
  658. // ctx->tot_len += (block_nb + 1) << 6;
  659. // }
  660. // void sha224_final(sha224_ctx *ctx, unsigned char *digest)
  661. // {
  662. // unsigned int block_nb;
  663. // unsigned int pm_len;
  664. // unsigned int len_b;
  665. // #ifndef UNROLL_LOOPS
  666. // int i;
  667. // #endif
  668. // block_nb = (1 + ((SHA224_BLOCK_SIZE - 9)
  669. // < (ctx->len % SHA224_BLOCK_SIZE)));
  670. // len_b = (ctx->tot_len + ctx->len) << 3;
  671. // pm_len = block_nb << 6;
  672. // memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
  673. // ctx->block[ctx->len] = 0x80;
  674. // UNPACK32(len_b, ctx->block + pm_len - 4);
  675. // sha256_transf(ctx, ctx->block, block_nb);
  676. // #ifndef UNROLL_LOOPS
  677. // for (i = 0 ; i < 7; i++) {
  678. // UNPACK32(ctx->h[i], &digest[i << 2]);
  679. // }
  680. // #else
  681. // UNPACK32(ctx->h[0], &digest[ 0]);
  682. // UNPACK32(ctx->h[1], &digest[ 4]);
  683. // UNPACK32(ctx->h[2], &digest[ 8]);
  684. // UNPACK32(ctx->h[3], &digest[12]);
  685. // UNPACK32(ctx->h[4], &digest[16]);
  686. // UNPACK32(ctx->h[5], &digest[20]);
  687. // UNPACK32(ctx->h[6], &digest[24]);
  688. // #endif /* !UNROLL_LOOPS */
  689. // }