podman
1// Copyright 2014 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
5package sha36
7// spongeDirection indicates the direction bytes are flowing through the sponge.
8type spongeDirection int9
10const (11// spongeAbsorbing indicates that the sponge is absorbing input.12spongeAbsorbing spongeDirection = iota13// spongeSqueezing indicates that the sponge is being squeezed.14spongeSqueezing
15)
16
17const (18// maxRate is the maximum size of the internal buffer. SHAKE-25619// currently needs the largest buffer.20maxRate = 16821)
22
23type state struct {24// Generic sponge components.25a [25]uint64 // main state of the hash26buf []byte // points into storage27rate int // the number of bytes of state to use28
29// dsbyte contains the "domain separation" bits and the first bit of30// the padding. Sections 6.1 and 6.2 of [1] separate the outputs of the31// SHA-3 and SHAKE functions by appending bitstrings to the message.32// Using a little-endian bit-ordering convention, these are "01" for SHA-333// and "1111" for SHAKE, or 00000010b and 00001111b, respectively. Then the34// padding rule from section 5.1 is applied to pad the message to a multiple35// of the rate, which involves adding a "1" bit, zero or more "0" bits, and36// a final "1" bit. We merge the first "1" bit from the padding into dsbyte,37// giving 00000110b (0x06) and 00011111b (0x1f).38// [1] http://csrc.nist.gov/publications/drafts/fips-202/fips_202_draft.pdf39// "Draft FIPS 202: SHA-3 Standard: Permutation-Based Hash and40// Extendable-Output Functions (May 2014)"41dsbyte byte42
43storage storageBuf
44
45// Specific to SHA-3 and SHAKE.46outputLen int // the default output size in bytes47state spongeDirection // whether the sponge is absorbing or squeezing48}
49
50// BlockSize returns the rate of sponge underlying this hash function.
51func (d *state) BlockSize() int { return d.rate }52
53// Size returns the output size of the hash function in bytes.
54func (d *state) Size() int { return d.outputLen }55
56// Reset clears the internal state by zeroing the sponge state and
57// the byte buffer, and setting Sponge.state to absorbing.
58func (d *state) Reset() {59// Zero the permutation's state.60for i := range d.a {61d.a[i] = 062}63d.state = spongeAbsorbing64d.buf = d.storage.asBytes()[:0]65}
66
67func (d *state) clone() *state {68ret := *d69if ret.state == spongeAbsorbing {70ret.buf = ret.storage.asBytes()[:len(ret.buf)]71} else {72ret.buf = ret.storage.asBytes()[d.rate-cap(d.buf) : d.rate]73}74
75return &ret76}
77
78// permute applies the KeccakF-1600 permutation. It handles
79// any input-output buffering.
80func (d *state) permute() {81switch d.state {82case spongeAbsorbing:83// If we're absorbing, we need to xor the input into the state84// before applying the permutation.85xorIn(d, d.buf)86d.buf = d.storage.asBytes()[:0]87keccakF1600(&d.a)88case spongeSqueezing:89// If we're squeezing, we need to apply the permutation before90// copying more output.91keccakF1600(&d.a)92d.buf = d.storage.asBytes()[:d.rate]93copyOut(d, d.buf)94}95}
96
97// pads appends the domain separation bits in dsbyte, applies
98// the multi-bitrate 10..1 padding rule, and permutes the state.
99func (d *state) padAndPermute(dsbyte byte) {100if d.buf == nil {101d.buf = d.storage.asBytes()[:0]102}103// Pad with this instance's domain-separator bits. We know that there's104// at least one byte of space in d.buf because, if it were full,105// permute would have been called to empty it. dsbyte also contains the106// first one bit for the padding. See the comment in the state struct.107d.buf = append(d.buf, dsbyte)108zerosStart := len(d.buf)109d.buf = d.storage.asBytes()[:d.rate]110for i := zerosStart; i < d.rate; i++ {111d.buf[i] = 0112}113// This adds the final one bit for the padding. Because of the way that114// bits are numbered from the LSB upwards, the final bit is the MSB of115// the last byte.116d.buf[d.rate-1] ^= 0x80117// Apply the permutation118d.permute()119d.state = spongeSqueezing120d.buf = d.storage.asBytes()[:d.rate]121copyOut(d, d.buf)122}
123
124// Write absorbs more data into the hash's state. It panics if any
125// output has already been read.
126func (d *state) Write(p []byte) (written int, err error) {127if d.state != spongeAbsorbing {128panic("sha3: Write after Read")129}130if d.buf == nil {131d.buf = d.storage.asBytes()[:0]132}133written = len(p)134
135for len(p) > 0 {136if len(d.buf) == 0 && len(p) >= d.rate {137// The fast path; absorb a full "rate" bytes of input and apply the permutation.138xorIn(d, p[:d.rate])139p = p[d.rate:]140keccakF1600(&d.a)141} else {142// The slow path; buffer the input until we can fill the sponge, and then xor it in.143todo := d.rate - len(d.buf)144if todo > len(p) {145todo = len(p)146}147d.buf = append(d.buf, p[:todo]...)148p = p[todo:]149
150// If the sponge is full, apply the permutation.151if len(d.buf) == d.rate {152d.permute()153}154}155}156
157return158}
159
160// Read squeezes an arbitrary number of bytes from the sponge.
161func (d *state) Read(out []byte) (n int, err error) {162// If we're still absorbing, pad and apply the permutation.163if d.state == spongeAbsorbing {164d.padAndPermute(d.dsbyte)165}166
167n = len(out)168
169// Now, do the squeezing.170for len(out) > 0 {171n := copy(out, d.buf)172d.buf = d.buf[n:]173out = out[n:]174
175// Apply the permutation if we've squeezed the sponge dry.176if len(d.buf) == 0 {177d.permute()178}179}180
181return182}
183
184// Sum applies padding to the hash state and then squeezes out the desired
185// number of output bytes. It panics if any output has already been read.
186func (d *state) Sum(in []byte) []byte {187if d.state != spongeAbsorbing {188panic("sha3: Sum after Read")189}190
191// Make a copy of the original hash so that caller can keep writing192// and summing.193dup := d.clone()194hash := make([]byte, dup.outputLen, 64) // explicit cap to allow stack allocation195dup.Read(hash)196return append(in, hash...)197}
198