You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
6.4 KiB

  1. // Package keccak implements the Keccak (SHA-3) hash algorithm.
  2. // http://keccak.noekeon.org / FIPS 202 draft.
  3. package keccak
  4. import (
  5. "hash"
  6. )
  7. const (
  8. domainNone = 1
  9. domainSHA3 = 0x06
  10. domainSHAKE = 0x1f
  11. )
  12. const rounds = 24
  13. var roundConstants = []uint64{
  14. 0x0000000000000001, 0x0000000000008082,
  15. 0x800000000000808A, 0x8000000080008000,
  16. 0x000000000000808B, 0x0000000080000001,
  17. 0x8000000080008081, 0x8000000000008009,
  18. 0x000000000000008A, 0x0000000000000088,
  19. 0x0000000080008009, 0x000000008000000A,
  20. 0x000000008000808B, 0x800000000000008B,
  21. 0x8000000000008089, 0x8000000000008003,
  22. 0x8000000000008002, 0x8000000000000080,
  23. 0x000000000000800A, 0x800000008000000A,
  24. 0x8000000080008081, 0x8000000000008080,
  25. 0x0000000080000001, 0x8000000080008008,
  26. }
  27. type keccak struct {
  28. S [25]uint64
  29. size int
  30. blockSize int
  31. buf []byte
  32. domain byte
  33. }
  34. func newKeccak(capacity, output int, domain byte) hash.Hash {
  35. var h keccak
  36. h.size = output / 8
  37. h.blockSize = (200 - capacity/8)
  38. h.domain = domain
  39. return &h
  40. }
  41. func New224() hash.Hash {
  42. return newKeccak(224*2, 224, domainNone)
  43. }
  44. func New256() hash.Hash {
  45. return newKeccak(256*2, 256, domainNone)
  46. }
  47. func New384() hash.Hash {
  48. return newKeccak(384*2, 384, domainNone)
  49. }
  50. func New512() hash.Hash {
  51. return newKeccak(512*2, 512, domainNone)
  52. }
  53. func (k *keccak) Write(b []byte) (int, error) {
  54. n := len(b)
  55. if len(k.buf) > 0 {
  56. x := k.blockSize - len(k.buf)
  57. if x > len(b) {
  58. x = len(b)
  59. }
  60. k.buf = append(k.buf, b[:x]...)
  61. b = b[x:]
  62. if len(k.buf) < k.blockSize {
  63. return n, nil
  64. }
  65. k.absorb(k.buf)
  66. k.buf = nil
  67. }
  68. for len(b) >= k.blockSize {
  69. k.absorb(b[:k.blockSize])
  70. b = b[k.blockSize:]
  71. }
  72. k.buf = b
  73. return n, nil
  74. }
  75. func (k0 *keccak) Sum(b []byte) []byte {
  76. k := *k0
  77. k.final()
  78. return k.squeeze(b)
  79. }
  80. func (k *keccak) Reset() {
  81. for i := range k.S {
  82. k.S[i] = 0
  83. }
  84. k.buf = nil
  85. }
  86. func (k *keccak) Size() int {
  87. return k.size
  88. }
  89. func (k *keccak) BlockSize() int {
  90. return k.blockSize
  91. }
  92. func (k *keccak) absorb(block []byte) {
  93. if len(block) != k.blockSize {
  94. panic("absorb() called with invalid block size")
  95. }
  96. for i := 0; i < k.blockSize/8; i++ {
  97. k.S[i] ^= uint64le(block[i*8:])
  98. }
  99. keccakf(&k.S)
  100. }
  101. func (k *keccak) pad(block []byte) []byte {
  102. padded := make([]byte, k.blockSize)
  103. copy(padded, k.buf)
  104. padded[len(k.buf)] = k.domain
  105. padded[len(padded)-1] |= 0x80
  106. return padded
  107. }
  108. func (k *keccak) final() {
  109. last := k.pad(k.buf)
  110. k.absorb(last)
  111. }
  112. func (k *keccak) squeeze(b []byte) []byte {
  113. buf := make([]byte, 8*len(k.S))
  114. n := k.size
  115. for {
  116. for i := range k.S {
  117. putUint64le(buf[i*8:], k.S[i])
  118. }
  119. if n <= k.blockSize {
  120. b = append(b, buf[:n]...)
  121. break
  122. }
  123. b = append(b, buf[:k.blockSize]...)
  124. n -= k.blockSize
  125. keccakf(&k.S)
  126. }
  127. return b
  128. }
  129. func keccakf(S *[25]uint64) {
  130. var bc0, bc1, bc2, bc3, bc4 uint64
  131. var S0, S1, S2, S3, S4 uint64
  132. var S5, S6, S7, S8, S9 uint64
  133. var S10, S11, S12, S13, S14 uint64
  134. var S15, S16, S17, S18, S19 uint64
  135. var S20, S21, S22, S23, S24 uint64
  136. var tmp uint64
  137. S0, S1, S2, S3, S4 = S[0], S[1], S[2], S[3], S[4]
  138. S5, S6, S7, S8, S9 = S[5], S[6], S[7], S[8], S[9]
  139. S10, S11, S12, S13, S14 = S[10], S[11], S[12], S[13], S[14]
  140. S15, S16, S17, S18, S19 = S[15], S[16], S[17], S[18], S[19]
  141. S20, S21, S22, S23, S24 = S[20], S[21], S[22], S[23], S[24]
  142. for r := 0; r < rounds; r++ {
  143. // theta
  144. bc0 = S0 ^ S5 ^ S10 ^ S15 ^ S20
  145. bc1 = S1 ^ S6 ^ S11 ^ S16 ^ S21
  146. bc2 = S2 ^ S7 ^ S12 ^ S17 ^ S22
  147. bc3 = S3 ^ S8 ^ S13 ^ S18 ^ S23
  148. bc4 = S4 ^ S9 ^ S14 ^ S19 ^ S24
  149. tmp = bc4 ^ (bc1<<1 | bc1>>(64-1))
  150. S0 ^= tmp
  151. S5 ^= tmp
  152. S10 ^= tmp
  153. S15 ^= tmp
  154. S20 ^= tmp
  155. tmp = bc0 ^ (bc2<<1 | bc2>>(64-1))
  156. S1 ^= tmp
  157. S6 ^= tmp
  158. S11 ^= tmp
  159. S16 ^= tmp
  160. S21 ^= tmp
  161. tmp = bc1 ^ (bc3<<1 | bc3>>(64-1))
  162. S2 ^= tmp
  163. S7 ^= tmp
  164. S12 ^= tmp
  165. S17 ^= tmp
  166. S22 ^= tmp
  167. tmp = bc2 ^ (bc4<<1 | bc4>>(64-1))
  168. S3 ^= tmp
  169. S8 ^= tmp
  170. S13 ^= tmp
  171. S18 ^= tmp
  172. S23 ^= tmp
  173. tmp = bc3 ^ (bc0<<1 | bc0>>(64-1))
  174. S4 ^= tmp
  175. S9 ^= tmp
  176. S14 ^= tmp
  177. S19 ^= tmp
  178. S24 ^= tmp
  179. // rho phi
  180. tmp = S1
  181. tmp, S10 = S10, tmp<<1|tmp>>(64-1)
  182. tmp, S7 = S7, tmp<<3|tmp>>(64-3)
  183. tmp, S11 = S11, tmp<<6|tmp>>(64-6)
  184. tmp, S17 = S17, tmp<<10|tmp>>(64-10)
  185. tmp, S18 = S18, tmp<<15|tmp>>(64-15)
  186. tmp, S3 = S3, tmp<<21|tmp>>(64-21)
  187. tmp, S5 = S5, tmp<<28|tmp>>(64-28)
  188. tmp, S16 = S16, tmp<<36|tmp>>(64-36)
  189. tmp, S8 = S8, tmp<<45|tmp>>(64-45)
  190. tmp, S21 = S21, tmp<<55|tmp>>(64-55)
  191. tmp, S24 = S24, tmp<<2|tmp>>(64-2)
  192. tmp, S4 = S4, tmp<<14|tmp>>(64-14)
  193. tmp, S15 = S15, tmp<<27|tmp>>(64-27)
  194. tmp, S23 = S23, tmp<<41|tmp>>(64-41)
  195. tmp, S19 = S19, tmp<<56|tmp>>(64-56)
  196. tmp, S13 = S13, tmp<<8|tmp>>(64-8)
  197. tmp, S12 = S12, tmp<<25|tmp>>(64-25)
  198. tmp, S2 = S2, tmp<<43|tmp>>(64-43)
  199. tmp, S20 = S20, tmp<<62|tmp>>(64-62)
  200. tmp, S14 = S14, tmp<<18|tmp>>(64-18)
  201. tmp, S22 = S22, tmp<<39|tmp>>(64-39)
  202. tmp, S9 = S9, tmp<<61|tmp>>(64-61)
  203. tmp, S6 = S6, tmp<<20|tmp>>(64-20)
  204. S1 = tmp<<44 | tmp>>(64-44)
  205. // chi
  206. bc0 = S0
  207. bc1 = S1
  208. bc2 = S2
  209. bc3 = S3
  210. bc4 = S4
  211. S0 ^= (^bc1) & bc2
  212. S1 ^= (^bc2) & bc3
  213. S2 ^= (^bc3) & bc4
  214. S3 ^= (^bc4) & bc0
  215. S4 ^= (^bc0) & bc1
  216. bc0 = S5
  217. bc1 = S6
  218. bc2 = S7
  219. bc3 = S8
  220. bc4 = S9
  221. S5 ^= (^bc1) & bc2
  222. S6 ^= (^bc2) & bc3
  223. S7 ^= (^bc3) & bc4
  224. S8 ^= (^bc4) & bc0
  225. S9 ^= (^bc0) & bc1
  226. bc0 = S10
  227. bc1 = S11
  228. bc2 = S12
  229. bc3 = S13
  230. bc4 = S14
  231. S10 ^= (^bc1) & bc2
  232. S11 ^= (^bc2) & bc3
  233. S12 ^= (^bc3) & bc4
  234. S13 ^= (^bc4) & bc0
  235. S14 ^= (^bc0) & bc1
  236. bc0 = S15
  237. bc1 = S16
  238. bc2 = S17
  239. bc3 = S18
  240. bc4 = S19
  241. S15 ^= (^bc1) & bc2
  242. S16 ^= (^bc2) & bc3
  243. S17 ^= (^bc3) & bc4
  244. S18 ^= (^bc4) & bc0
  245. S19 ^= (^bc0) & bc1
  246. bc0 = S20
  247. bc1 = S21
  248. bc2 = S22
  249. bc3 = S23
  250. bc4 = S24
  251. S20 ^= (^bc1) & bc2
  252. S21 ^= (^bc2) & bc3
  253. S22 ^= (^bc3) & bc4
  254. S23 ^= (^bc4) & bc0
  255. S24 ^= (^bc0) & bc1
  256. // iota
  257. S0 ^= roundConstants[r]
  258. }
  259. S[0], S[1], S[2], S[3], S[4] = S0, S1, S2, S3, S4
  260. S[5], S[6], S[7], S[8], S[9] = S5, S6, S7, S8, S9
  261. S[10], S[11], S[12], S[13], S[14] = S10, S11, S12, S13, S14
  262. S[15], S[16], S[17], S[18], S[19] = S15, S16, S17, S18, S19
  263. S[20], S[21], S[22], S[23], S[24] = S20, S21, S22, S23, S24
  264. }
  265. func uint64le(v []byte) uint64 {
  266. return uint64(v[0]) |
  267. uint64(v[1])<<8 |
  268. uint64(v[2])<<16 |
  269. uint64(v[3])<<24 |
  270. uint64(v[4])<<32 |
  271. uint64(v[5])<<40 |
  272. uint64(v[6])<<48 |
  273. uint64(v[7])<<56
  274. }
  275. func putUint64le(v []byte, x uint64) {
  276. v[0] = byte(x)
  277. v[1] = byte(x >> 8)
  278. v[2] = byte(x >> 16)
  279. v[3] = byte(x >> 24)
  280. v[4] = byte(x >> 32)
  281. v[5] = byte(x >> 40)
  282. v[6] = byte(x >> 48)
  283. v[7] = byte(x >> 56)
  284. }