/thirdparty/liblastfm2/src/fingerprint/Sha256.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 480 lines · 367 code · 76 blank · 37 comment · 41 complexity · db89fdfafce54100e04eefc9e69d9804 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * $Id: sha256.c 680 2003-07-25 21:57:49Z asaddi $
  27. */
  28. /*
  29. * Define WORDS_BIGENDIAN if compiling on a big-endian architecture.
  30. *
  31. * Define SHA256_TEST to test the implementation using the NIST's
  32. * sample messages. The output should be:
  33. *
  34. * ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad
  35. * 248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1
  36. * cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0
  37. */
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. #endif /* HAVE_CONFIG_H */
  41. #if HAVE_INTTYPES_H
  42. # include <inttypes.h>
  43. #else
  44. # if HAVE_STDINT_H
  45. # include <stdint.h>
  46. # endif
  47. #endif
  48. #include <string.h>
  49. #include "Sha256.h"
  50. #ifndef lint
  51. static const char rcsid[] =
  52. "$Id: sha256.c 680 2003-07-25 21:57:49Z asaddi $";
  53. #endif /* !lint */
  54. #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
  55. #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
  56. #define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  57. #define Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
  58. #define SIGMA0(x) (ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
  59. #define SIGMA1(x) (ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
  60. #define sigma0(x) (ROTR((x), 7) ^ ROTR((x), 18) ^ ((x) >> 3))
  61. #define sigma1(x) (ROTR((x), 17) ^ ROTR((x), 19) ^ ((x) >> 10))
  62. #define DO_ROUND() { \
  63. t1 = h + SIGMA1(e) + Ch(e, f, g) + *(Kp++) + *(W++); \
  64. t2 = SIGMA0(a) + Maj(a, b, c); \
  65. h = g; \
  66. g = f; \
  67. f = e; \
  68. e = d + t1; \
  69. d = c; \
  70. c = b; \
  71. b = a; \
  72. a = t1 + t2; \
  73. }
  74. static const uint32_t K[64] = {
  75. 0x428a2f98L, 0x71374491L, 0xb5c0fbcfL, 0xe9b5dba5L,
  76. 0x3956c25bL, 0x59f111f1L, 0x923f82a4L, 0xab1c5ed5L,
  77. 0xd807aa98L, 0x12835b01L, 0x243185beL, 0x550c7dc3L,
  78. 0x72be5d74L, 0x80deb1feL, 0x9bdc06a7L, 0xc19bf174L,
  79. 0xe49b69c1L, 0xefbe4786L, 0x0fc19dc6L, 0x240ca1ccL,
  80. 0x2de92c6fL, 0x4a7484aaL, 0x5cb0a9dcL, 0x76f988daL,
  81. 0x983e5152L, 0xa831c66dL, 0xb00327c8L, 0xbf597fc7L,
  82. 0xc6e00bf3L, 0xd5a79147L, 0x06ca6351L, 0x14292967L,
  83. 0x27b70a85L, 0x2e1b2138L, 0x4d2c6dfcL, 0x53380d13L,
  84. 0x650a7354L, 0x766a0abbL, 0x81c2c92eL, 0x92722c85L,
  85. 0xa2bfe8a1L, 0xa81a664bL, 0xc24b8b70L, 0xc76c51a3L,
  86. 0xd192e819L, 0xd6990624L, 0xf40e3585L, 0x106aa070L,
  87. 0x19a4c116L, 0x1e376c08L, 0x2748774cL, 0x34b0bcb5L,
  88. 0x391c0cb3L, 0x4ed8aa4aL, 0x5b9cca4fL, 0x682e6ff3L,
  89. 0x748f82eeL, 0x78a5636fL, 0x84c87814L, 0x8cc70208L,
  90. 0x90befffaL, 0xa4506cebL, 0xbef9a3f7L, 0xc67178f2L
  91. };
  92. #ifndef RUNTIME_ENDIAN
  93. #ifdef WORDS_BIGENDIAN
  94. #define BYTESWAP(x) (x)
  95. #define BYTESWAP64(x) (x)
  96. #else /* WORDS_BIGENDIAN */
  97. #define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \
  98. (ROTL((x), 8) & 0x00ff00ffL))
  99. #define BYTESWAP64(x) _byteswap64(x)
  100. static inline uint64_t _byteswap64(uint64_t x)
  101. {
  102. uint32_t a = x >> 32;
  103. uint32_t b = (uint32_t) x;
  104. return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a);
  105. }
  106. #endif /* WORDS_BIGENDIAN */
  107. #else /* !RUNTIME_ENDIAN */
  108. #define BYTESWAP(x) _byteswap(sc->littleEndian, x)
  109. #define BYTESWAP64(x) _byteswap64(sc->littleEndian, x)
  110. #define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \
  111. (ROTL((x), 8) & 0x00ff00ffL))
  112. #define _BYTESWAP64(x) __byteswap64(x)
  113. static inline uint64_t __byteswap64(uint64_t x)
  114. {
  115. uint32_t a = x >> 32;
  116. uint32_t b = (uint32_t) x;
  117. return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a);
  118. }
  119. static inline uint32_t _byteswap(int littleEndian, uint32_t x)
  120. {
  121. if (!littleEndian)
  122. return x;
  123. else
  124. return _BYTESWAP(x);
  125. }
  126. static inline uint64_t _byteswap64(int littleEndian, uint64_t x)
  127. {
  128. if (!littleEndian)
  129. return x;
  130. else
  131. return _BYTESWAP64(x);
  132. }
  133. static inline void setEndian(int *littleEndianp)
  134. {
  135. union {
  136. uint32_t w;
  137. uint8_t b[4];
  138. } endian;
  139. endian.w = 1L;
  140. *littleEndianp = endian.b[0] != 0;
  141. }
  142. #endif /* !RUNTIME_ENDIAN */
  143. static const uint8_t padding[64] = {
  144. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  150. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  152. };
  153. void
  154. SHA256Init (SHA256Context *sc)
  155. {
  156. #ifdef RUNTIME_ENDIAN
  157. setEndian (&sc->littleEndian);
  158. #endif /* RUNTIME_ENDIAN */
  159. sc->totalLength = 0LL;
  160. sc->hash[0] = 0x6a09e667L;
  161. sc->hash[1] = 0xbb67ae85L;
  162. sc->hash[2] = 0x3c6ef372L;
  163. sc->hash[3] = 0xa54ff53aL;
  164. sc->hash[4] = 0x510e527fL;
  165. sc->hash[5] = 0x9b05688cL;
  166. sc->hash[6] = 0x1f83d9abL;
  167. sc->hash[7] = 0x5be0cd19L;
  168. sc->bufferLength = 0L;
  169. }
  170. static void
  171. burnStack (int size)
  172. {
  173. char buf[128];
  174. memset (buf, 0, sizeof (buf));
  175. size -= sizeof (buf);
  176. if (size > 0)
  177. burnStack (size);
  178. }
  179. static void
  180. SHA256Guts (SHA256Context *sc, const uint32_t *cbuf)
  181. {
  182. uint32_t buf[64];
  183. uint32_t *W, *W2, *W7, *W15, *W16;
  184. uint32_t a, b, c, d, e, f, g, h;
  185. uint32_t t1, t2;
  186. const uint32_t *Kp;
  187. int i;
  188. W = buf;
  189. for (i = 15; i >= 0; i--) {
  190. *(W++) = BYTESWAP(*cbuf);
  191. cbuf++;
  192. }
  193. W16 = &buf[0];
  194. W15 = &buf[1];
  195. W7 = &buf[9];
  196. W2 = &buf[14];
  197. for (i = 47; i >= 0; i--) {
  198. *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++);
  199. W2++;
  200. W15++;
  201. }
  202. a = sc->hash[0];
  203. b = sc->hash[1];
  204. c = sc->hash[2];
  205. d = sc->hash[3];
  206. e = sc->hash[4];
  207. f = sc->hash[5];
  208. g = sc->hash[6];
  209. h = sc->hash[7];
  210. Kp = K;
  211. W = buf;
  212. #ifndef SHA256_UNROLL
  213. #define SHA256_UNROLL 1
  214. #endif /* !SHA256_UNROLL */
  215. #if SHA256_UNROLL == 1
  216. for (i = 63; i >= 0; i--)
  217. DO_ROUND();
  218. #elif SHA256_UNROLL == 2
  219. for (i = 31; i >= 0; i--) {
  220. DO_ROUND(); DO_ROUND();
  221. }
  222. #elif SHA256_UNROLL == 4
  223. for (i = 15; i >= 0; i--) {
  224. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  225. }
  226. #elif SHA256_UNROLL == 8
  227. for (i = 7; i >= 0; i--) {
  228. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  229. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  230. }
  231. #elif SHA256_UNROLL == 16
  232. for (i = 3; i >= 0; i--) {
  233. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  234. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  235. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  236. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  237. }
  238. #elif SHA256_UNROLL == 32
  239. for (i = 1; i >= 0; i--) {
  240. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  241. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  242. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  243. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  244. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  245. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  246. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  247. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  248. }
  249. #elif SHA256_UNROLL == 64
  250. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  251. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  252. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  253. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  254. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  255. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  256. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  257. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  258. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  259. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  260. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  261. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  262. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  263. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  264. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  265. DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND();
  266. #else
  267. #error "SHA256_UNROLL must be 1, 2, 4, 8, 16, 32, or 64!"
  268. #endif
  269. sc->hash[0] += a;
  270. sc->hash[1] += b;
  271. sc->hash[2] += c;
  272. sc->hash[3] += d;
  273. sc->hash[4] += e;
  274. sc->hash[5] += f;
  275. sc->hash[6] += g;
  276. sc->hash[7] += h;
  277. }
  278. void
  279. SHA256Update (SHA256Context *sc, const void *vdata, uint32_t len)
  280. {
  281. const uint8_t *data = (const uint8_t*)vdata;
  282. uint32_t bufferBytesLeft;
  283. uint32_t bytesToCopy;
  284. int needBurn = 0;
  285. #ifdef SHA256_FAST_COPY
  286. if (sc->bufferLength) {
  287. bufferBytesLeft = 64L - sc->bufferLength;
  288. bytesToCopy = bufferBytesLeft;
  289. if (bytesToCopy > len)
  290. bytesToCopy = len;
  291. memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
  292. sc->totalLength += bytesToCopy * 8L;
  293. sc->bufferLength += bytesToCopy;
  294. data += bytesToCopy;
  295. len -= bytesToCopy;
  296. if (sc->bufferLength == 64L) {
  297. SHA256Guts (sc, sc->buffer.words);
  298. needBurn = 1;
  299. sc->bufferLength = 0L;
  300. }
  301. }
  302. while (len > 63L) {
  303. sc->totalLength += 512L;
  304. SHA256Guts (sc, data);
  305. needBurn = 1;
  306. data += 64L;
  307. len -= 64L;
  308. }
  309. if (len) {
  310. memcpy (&sc->buffer.bytes[sc->bufferLength], data, len);
  311. sc->totalLength += len * 8L;
  312. sc->bufferLength += len;
  313. }
  314. #else /* SHA256_FAST_COPY */
  315. while (len) {
  316. bufferBytesLeft = 64L - sc->bufferLength;
  317. bytesToCopy = bufferBytesLeft;
  318. if (bytesToCopy > len)
  319. bytesToCopy = len;
  320. memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
  321. sc->totalLength += bytesToCopy * 8L;
  322. sc->bufferLength += bytesToCopy;
  323. data += bytesToCopy;
  324. len -= bytesToCopy;
  325. if (sc->bufferLength == 64L) {
  326. SHA256Guts (sc, sc->buffer.words);
  327. needBurn = 1;
  328. sc->bufferLength = 0L;
  329. }
  330. }
  331. #endif /* SHA256_FAST_COPY */
  332. if (needBurn)
  333. burnStack (sizeof (uint32_t[74]) + sizeof (uint32_t *[6]) + sizeof (int));
  334. }
  335. void
  336. SHA256Final (SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE])
  337. {
  338. uint32_t bytesToPad;
  339. uint64_t lengthPad;
  340. int i;
  341. bytesToPad = 120L - sc->bufferLength;
  342. if (bytesToPad > 64L)
  343. bytesToPad -= 64L;
  344. lengthPad = BYTESWAP64(sc->totalLength);
  345. SHA256Update (sc, padding, bytesToPad);
  346. SHA256Update (sc, &lengthPad, 8L);
  347. if (hash) {
  348. for (i = 0; i < SHA256_HASH_WORDS; i++) {
  349. #ifdef SHA256_FAST_COPY
  350. *((uint32_t *) hash) = BYTESWAP(sc->hash[i]);
  351. #else /* SHA256_FAST_COPY */
  352. hash[0] = (uint8_t) (sc->hash[i] >> 24);
  353. hash[1] = (uint8_t) (sc->hash[i] >> 16);
  354. hash[2] = (uint8_t) (sc->hash[i] >> 8);
  355. hash[3] = (uint8_t) sc->hash[i];
  356. #endif /* SHA256_FAST_COPY */
  357. hash += 4;
  358. }
  359. }
  360. }
  361. #ifdef SHA256_TEST
  362. #include <stdio.h>
  363. #include <stdlib.h>
  364. int
  365. main (int argc, char *argv[])
  366. {
  367. SHA256Context foo;
  368. uint8_t hash[SHA256_HASH_SIZE];
  369. char buf[1000];
  370. int i;
  371. SHA256Init (&foo);
  372. SHA256Update (&foo, "abc", 3);
  373. SHA256Final (&foo, hash);
  374. for (i = 0; i < SHA256_HASH_SIZE;) {
  375. printf ("%02x", hash[i++]);
  376. if (!(i % 4))
  377. printf (" ");
  378. }
  379. printf ("\n");
  380. SHA256Init (&foo);
  381. SHA256Update (&foo,
  382. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  383. 56);
  384. SHA256Final (&foo, hash);
  385. for (i = 0; i < SHA256_HASH_SIZE;) {
  386. printf ("%02x", hash[i++]);
  387. if (!(i % 4))
  388. printf (" ");
  389. }
  390. printf ("\n");
  391. SHA256Init (&foo);
  392. memset (buf, 'a', sizeof (buf));
  393. for (i = 0; i < 1000; i++)
  394. SHA256Update (&foo, buf, sizeof (buf));
  395. SHA256Final (&foo, hash);
  396. for (i = 0; i < SHA256_HASH_SIZE;) {
  397. printf ("%02x", hash[i++]);
  398. if (!(i % 4))
  399. printf (" ");
  400. }
  401. printf ("\n");
  402. exit (0);
  403. }
  404. #endif /* SHA256_TEST */