sha3.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package sha3 implements the SHA3 hash algorithm (formerly called Keccak) chosen by NIST in 2012.
  5. // This file provides a SHA3 implementation which implements the standard hash.Hash interface.
  6. // Writing input data, including padding, and reading output data are computed in this file.
  7. // Note that the current implementation can compute the hash of an integral number of bytes only.
  8. // This is a consequence of the hash interface in which a buffer of bytes is passed in.
  9. // The internals of the Keccak-f function are computed in keccakf.go.
  10. // For the detailed specification, refer to the Keccak web site (http://keccak.noekeon.org/).
  11. package sha3
  12. import (
  13. "encoding/binary"
  14. "hash"
  15. )
  16. // laneSize is the size in bytes of each "lane" of the internal state of SHA3 (5 * 5 * 8).
  17. // Note that changing this size would requires using a type other than uint64 to store each lane.
  18. const laneSize = 8
  19. // sliceSize represents the dimensions of the internal state, a square matrix of
  20. // sliceSize ** 2 lanes. This is the size of both the "rows" and "columns" dimensions in the
  21. // terminology of the SHA3 specification.
  22. const sliceSize = 5
  23. // numLanes represents the total number of lanes in the state.
  24. const numLanes = sliceSize * sliceSize
  25. // stateSize is the size in bytes of the internal state of SHA3 (5 * 5 * WSize).
  26. const stateSize = laneSize * numLanes
  27. // digest represents the partial evaluation of a checksum.
  28. // Note that capacity, and not outputSize, is the critical security parameter, as SHA3 can output
  29. // an arbitrary number of bytes for any given capacity. The Keccak proposal recommends that
  30. // capacity = 2*outputSize to ensure that finding a collision of size outputSize requires
  31. // O(2^{outputSize/2}) computations (the birthday lower bound). Future standards may modify the
  32. // capacity/outputSize ratio to allow for more output with lower cryptographic security.
  33. type digest struct {
  34. a [numLanes]uint64 // main state of the hash
  35. outputSize int // desired output size in bytes
  36. capacity int // number of bytes to leave untouched during squeeze/absorb
  37. absorbed int // number of bytes absorbed thus far
  38. }
  39. // minInt returns the lesser of two integer arguments, to simplify the absorption routine.
  40. func minInt(v1, v2 int) int {
  41. if v1 <= v2 {
  42. return v1
  43. }
  44. return v2
  45. }
  46. // rate returns the number of bytes of the internal state which can be absorbed or squeezed
  47. // in between calls to the permutation function.
  48. func (d *digest) rate() int {
  49. return stateSize - d.capacity
  50. }
  51. // Reset clears the internal state by zeroing bytes in the state buffer.
  52. // This can be skipped for a newly-created hash state; the default zero-allocated state is correct.
  53. func (d *digest) Reset() {
  54. d.absorbed = 0
  55. for i := range d.a {
  56. d.a[i] = 0
  57. }
  58. }
  59. // BlockSize, required by the hash.Hash interface, does not have a standard intepretation
  60. // for a sponge-based construction like SHA3. We return the data rate: the number of bytes which
  61. // can be absorbed per invocation of the permutation function. For Merkle-Damgård based hashes
  62. // (ie SHA1, SHA2, MD5) the output size of the internal compression function is returned.
  63. // We consider this to be roughly equivalent because it represents the number of bytes of output
  64. // produced per cryptographic operation.
  65. func (d *digest) BlockSize() int { return d.rate() }
  66. // Size returns the output size of the hash function in bytes.
  67. func (d *digest) Size() int {
  68. return d.outputSize
  69. }
  70. // unalignedAbsorb is a helper function for Write, which absorbs data that isn't aligned with an
  71. // 8-byte lane. This requires shifting the individual bytes into position in a uint64.
  72. func (d *digest) unalignedAbsorb(p []byte) {
  73. var t uint64
  74. for i := len(p) - 1; i >= 0; i-- {
  75. t <<= 8
  76. t |= uint64(p[i])
  77. }
  78. offset := (d.absorbed) % d.rate()
  79. t <<= 8 * uint(offset%laneSize)
  80. d.a[offset/laneSize] ^= t
  81. d.absorbed += len(p)
  82. }
  83. // Write "absorbs" bytes into the state of the SHA3 hash, updating as needed when the sponge
  84. // "fills up" with rate() bytes. Since lanes are stored internally as type uint64, this requires
  85. // converting the incoming bytes into uint64s using a little endian interpretation. This
  86. // implementation is optimized for large, aligned writes of multiples of 8 bytes (laneSize).
  87. // Non-aligned or uneven numbers of bytes require shifting and are slower.
  88. func (d *digest) Write(p []byte) (int, error) {
  89. // An initial offset is needed if the we aren't absorbing to the first lane initially.
  90. offset := d.absorbed % d.rate()
  91. toWrite := len(p)
  92. // The first lane may need to absorb unaligned and/or incomplete data.
  93. if (offset%laneSize != 0 || len(p) < 8) && len(p) > 0 {
  94. toAbsorb := minInt(laneSize-(offset%laneSize), len(p))
  95. d.unalignedAbsorb(p[:toAbsorb])
  96. p = p[toAbsorb:]
  97. offset = (d.absorbed) % d.rate()
  98. // For every rate() bytes absorbed, the state must be permuted via the F Function.
  99. if (d.absorbed)%d.rate() == 0 {
  100. keccakF(&d.a)
  101. }
  102. }
  103. // This loop should absorb the bulk of the data into full, aligned lanes.
  104. // It will call the update function as necessary.
  105. for len(p) > 7 {
  106. firstLane := offset / laneSize
  107. lastLane := minInt(d.rate()/laneSize, firstLane+len(p)/laneSize)
  108. // This inner loop absorbs input bytes into the state in groups of 8, converted to uint64s.
  109. for lane := firstLane; lane < lastLane; lane++ {
  110. d.a[lane] ^= binary.LittleEndian.Uint64(p[:laneSize])
  111. p = p[laneSize:]
  112. }
  113. d.absorbed += (lastLane - firstLane) * laneSize
  114. // For every rate() bytes absorbed, the state must be permuted via the F Function.
  115. if (d.absorbed)%d.rate() == 0 {
  116. keccakF(&d.a)
  117. }
  118. offset = 0
  119. }
  120. // If there are insufficient bytes to fill the final lane, an unaligned absorption.
  121. // This should always start at a correct lane boundary though, or else it would be caught
  122. // by the uneven opening lane case above.
  123. if len(p) > 0 {
  124. d.unalignedAbsorb(p)
  125. }
  126. return toWrite, nil
  127. }
  128. // pad computes the SHA3 padding scheme based on the number of bytes absorbed.
  129. // The padding is a 1 bit, followed by an arbitrary number of 0s and then a final 1 bit, such that
  130. // the input bits plus padding bits are a multiple of rate(). Adding the padding simply requires
  131. // xoring an opening and closing bit into the appropriate lanes.
  132. func (d *digest) pad() {
  133. offset := d.absorbed % d.rate()
  134. // The opening pad bit must be shifted into position based on the number of bytes absorbed
  135. padOpenLane := offset / laneSize
  136. d.a[padOpenLane] ^= 0x0000000000000001 << uint(8*(offset%laneSize))
  137. // The closing padding bit is always in the last position
  138. padCloseLane := (d.rate() / laneSize) - 1
  139. d.a[padCloseLane] ^= 0x8000000000000000
  140. }
  141. // finalize prepares the hash to output data by padding and one final permutation of the state.
  142. func (d *digest) finalize() {
  143. d.pad()
  144. keccakF(&d.a)
  145. }
  146. // squeeze outputs an arbitrary number of bytes from the hash state.
  147. // Squeezing can require multiple calls to the F function (one per rate() bytes squeezed),
  148. // although this is not the case for standard SHA3 parameters. This implementation only supports
  149. // squeezing a single time, subsequent squeezes may lose alignment. Future implementations
  150. // may wish to support multiple squeeze calls, for example to support use as a PRNG.
  151. func (d *digest) squeeze(in []byte, toSqueeze int) []byte {
  152. // Because we read in blocks of laneSize, we need enough room to read
  153. // an integral number of lanes
  154. needed := toSqueeze + (laneSize-toSqueeze%laneSize)%laneSize
  155. if cap(in)-len(in) < needed {
  156. newIn := make([]byte, len(in), len(in)+needed)
  157. copy(newIn, in)
  158. in = newIn
  159. }
  160. out := in[len(in) : len(in)+needed]
  161. for len(out) > 0 {
  162. for i := 0; i < d.rate() && len(out) > 0; i += laneSize {
  163. binary.LittleEndian.PutUint64(out[:], d.a[i/laneSize])
  164. out = out[laneSize:]
  165. }
  166. if len(out) > 0 {
  167. keccakF(&d.a)
  168. }
  169. }
  170. return in[:len(in)+toSqueeze] // Re-slice in case we wrote extra data.
  171. }
  172. // Sum applies padding to the hash state and then squeezes out the desired nubmer of output bytes.
  173. func (d *digest) Sum(in []byte) []byte {
  174. // Make a copy of the original hash so that caller can keep writing and summing.
  175. dup := *d
  176. dup.finalize()
  177. return dup.squeeze(in, dup.outputSize)
  178. }
  179. // The NewKeccakX constructors enable initializing a hash in any of the four recommend sizes
  180. // from the Keccak specification, all of which set capacity=2*outputSize. Note that the final
  181. // NIST standard for SHA3 may specify different input/output lengths.
  182. // The output size is indicated in bits but converted into bytes internally.
  183. func NewKeccak224() hash.Hash { return &digest{outputSize: 224 / 8, capacity: 2 * 224 / 8} }
  184. func NewKeccak256() hash.Hash { return &digest{outputSize: 256 / 8, capacity: 2 * 256 / 8} }
  185. func NewKeccak384() hash.Hash { return &digest{outputSize: 384 / 8, capacity: 2 * 384 / 8} }
  186. func NewKeccak512() hash.Hash { return &digest{outputSize: 512 / 8, capacity: 2 * 512 / 8} }