SHA3.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file SHA3.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "SHA3.h"
  19. #include <cstdint>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include "RLP.h"
  24. #include "picosha2.h"
  25. using namespace std;
  26. using namespace dev;
  27. namespace dev
  28. {
  29. h256 EmptySHA3 = sha3(bytesConstRef());
  30. h256 EmptyListSHA3 = sha3(rlpList());
  31. namespace keccak
  32. {
  33. /** libkeccak-tiny
  34. *
  35. * A single-file implementation of SHA-3 and SHAKE.
  36. *
  37. * Implementor: David Leon Gil
  38. * License: CC0, attribution kindly requested. Blame taken too,
  39. * but not liability.
  40. */
  41. #define decshake(bits) \
  42. int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);
  43. #define decsha3(bits) \
  44. int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
  45. decshake(128)
  46. decshake(256)
  47. decsha3(224)
  48. decsha3(256)
  49. decsha3(384)
  50. decsha3(512)
  51. /******** The Keccak-f[1600] permutation ********/
  52. /*** Constants. ***/
  53. static const uint8_t rho[24] = \
  54. { 1, 3, 6, 10, 15, 21,
  55. 28, 36, 45, 55, 2, 14,
  56. 27, 41, 56, 8, 25, 43,
  57. 62, 18, 39, 61, 20, 44};
  58. static const uint8_t pi[24] = \
  59. {10, 7, 11, 17, 18, 3,
  60. 5, 16, 8, 21, 24, 4,
  61. 15, 23, 19, 13, 12, 2,
  62. 20, 14, 22, 9, 6, 1};
  63. static const uint64_t RC[24] = \
  64. {1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
  65. 0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
  66. 0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
  67. 0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
  68. 0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
  69. 0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
  70. /*** Helper macros to unroll the permutation. ***/
  71. #define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
  72. #define REPEAT6(e) e e e e e e
  73. #define REPEAT24(e) REPEAT6(e e e e)
  74. #define REPEAT5(e) e e e e e
  75. #define FOR5(v, s, e) \
  76. v = 0; \
  77. REPEAT5(e; v += s;)
  78. /*** Keccak-f[1600] ***/
  79. static inline void keccakf(void* state) {
  80. uint64_t* a = (uint64_t*)state;
  81. uint64_t b[5] = {0};
  82. uint64_t t = 0;
  83. uint8_t x, y;
  84. for (int i = 0; i < 24; i++) {
  85. // Theta
  86. FOR5(x, 1,
  87. b[x] = 0;
  88. FOR5(y, 5,
  89. b[x] ^= a[x + y]; ))
  90. FOR5(x, 1,
  91. FOR5(y, 5,
  92. a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
  93. // Rho and pi
  94. t = a[1];
  95. x = 0;
  96. REPEAT24(b[0] = a[pi[x]];
  97. a[pi[x]] = rol(t, rho[x]);
  98. t = b[0];
  99. x++; )
  100. // Chi
  101. FOR5(y,
  102. 5,
  103. FOR5(x, 1,
  104. b[x] = a[y + x];)
  105. FOR5(x, 1,
  106. a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
  107. // Iota
  108. a[0] ^= RC[i];
  109. }
  110. }
  111. /******** The FIPS202-defined functions. ********/
  112. /*** Some helper macros. ***/
  113. #define _(S) do { S } while (0)
  114. #define FOR(i, ST, L, S) \
  115. _(for (size_t i = 0; i < L; i += ST) { S; })
  116. #define mkapply_ds(NAME, S) \
  117. static inline void NAME(uint8_t* dst, \
  118. const uint8_t* src, \
  119. size_t len) { \
  120. FOR(i, 1, len, S); \
  121. }
  122. #define mkapply_sd(NAME, S) \
  123. static inline void NAME(const uint8_t* src, \
  124. uint8_t* dst, \
  125. size_t len) { \
  126. FOR(i, 1, len, S); \
  127. }
  128. mkapply_ds(xorin, dst[i] ^= src[i]) // xorin
  129. mkapply_sd(setout, dst[i] = src[i]) // setout
  130. #define P keccakf
  131. #define Plen 200
  132. // Fold P*F over the full blocks of an input.
  133. #define foldP(I, L, F) \
  134. while (L >= rate) { \
  135. F(a, I, rate); \
  136. P(a); \
  137. I += rate; \
  138. L -= rate; \
  139. }
  140. /** The sponge-based hash construction. **/
  141. static inline int hash(uint8_t* out, size_t outlen,
  142. const uint8_t* in, size_t inlen,
  143. size_t rate, uint8_t delim) {
  144. if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
  145. return -1;
  146. }
  147. uint8_t a[Plen] = {0};
  148. // Absorb input.
  149. foldP(in, inlen, xorin);
  150. // Xor in the DS and pad frame.
  151. a[inlen] ^= delim;
  152. a[rate - 1] ^= 0x80;
  153. // Xor in the last block.
  154. xorin(a, in, inlen);
  155. // Apply P
  156. P(a);
  157. // Squeeze output.
  158. foldP(out, outlen, setout);
  159. setout(a, out, outlen);
  160. memset(a, 0, 200);
  161. return 0;
  162. }
  163. /*** Helper macros to define SHA3 and SHAKE instances. ***/
  164. #define defshake(bits) \
  165. int shake##bits(uint8_t* out, size_t outlen, \
  166. const uint8_t* in, size_t inlen) { \
  167. return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
  168. }
  169. #define defsha3(bits) \
  170. int sha3_##bits(uint8_t* out, size_t outlen, \
  171. const uint8_t* in, size_t inlen) { \
  172. if (outlen > (bits/8)) { \
  173. return -1; \
  174. } \
  175. return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \
  176. }
  177. /*** FIPS202 SHAKE VOFs ***/
  178. defshake(128)
  179. defshake(256)
  180. /*** FIPS202 SHA3 FOFs ***/
  181. defsha3(224)
  182. defsha3(256)
  183. defsha3(384)
  184. defsha3(512)
  185. }
  186. bool sha3(bytesConstRef _input, bytesRef o_output)
  187. {
  188. // FIXME: What with unaligned memory?
  189. if (o_output.size() != 32)
  190. return false;
  191. keccak::sha3_256(o_output.data(), 32, _input.data(), _input.size());
  192. // keccak::keccak(ret.data(), 32, (uint64_t const*)_input.data(), _input.size());
  193. return true;
  194. }
  195. }