github.com/immesys/bw2bc@v1.1.0/crypto/sha3/sha3.go (about)

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