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