| 1 |
// Copyright 2017 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 blake2b |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/binary" |
| 9 |
"errors" |
| 10 |
"io" |
| 11 |
) |
| 12 |
|
| 13 |
// XOF defines the interface to hash functions that |
| 14 |
// support arbitrary-length output. |
| 15 |
// |
| 16 |
// New callers should prefer the standard library [hash.XOF]. |
| 17 |
type XOF interface { |
| 18 |
// Write absorbs more data into the hash's state. It panics if called |
| 19 |
// after Read. |
| 20 |
io.Writer |
| 21 |
|
| 22 |
// Read reads more output from the hash. It returns io.EOF if the limit |
| 23 |
// has been reached. |
| 24 |
io.Reader |
| 25 |
|
| 26 |
// Clone returns a copy of the XOF in its current state. |
| 27 |
Clone() XOF |
| 28 |
|
| 29 |
// Reset resets the XOF to its initial state. |
| 30 |
Reset() |
| 31 |
} |
| 32 |
|
| 33 |
// NewXOF creates a new variable-output-length hash. The hash either produce a |
| 34 |
// known number of bytes (1 <= size < 2**32-1), or an unknown number of bytes |
| 35 |
// (size == OutputLengthUnknown). In the latter case, an absolute limit of |
| 36 |
// 256GiB applies. |
| 37 |
// |
| 38 |
// A non-nil key turns the hash into a MAC. The key must between |
| 39 |
// zero and 32 bytes long. |
| 40 |
// |
| 41 |
// The result can be safely interface-upgraded to [hash.XOF]. |
| 42 |
func NewXOF(size uint32, key []byte) (XOF, error) { |
| 43 |
if len(key) > Size { |
| 44 |
return nil, errKeySize |
| 45 |
} |
| 46 |
if size == magicUnknownOutputLength { |
| 47 |
// 2^32-1 indicates an unknown number of bytes and thus isn't a |
| 48 |
// valid length. |
| 49 |
return nil, errors.New("blake2b: XOF length too large") |
| 50 |
} |
| 51 |
if size == OutputLengthUnknown { |
| 52 |
size = magicUnknownOutputLength |
| 53 |
} |
| 54 |
x := &xof{ |
| 55 |
d: digest{ |
| 56 |
size: Size, |
| 57 |
keyLen: len(key), |
| 58 |
}, |
| 59 |
length: size, |
| 60 |
} |
| 61 |
copy(x.d.key[:], key) |
| 62 |
x.Reset() |
| 63 |
return x, nil |
| 64 |
} |
| 65 |
|
| 66 |
type xof struct { |
| 67 |
d digest |
| 68 |
length uint32 |
| 69 |
remaining uint64 |
| 70 |
cfg, root, block [Size]byte |
| 71 |
offset int |
| 72 |
nodeOffset uint32 |
| 73 |
readMode bool |
| 74 |
} |
| 75 |
|
| 76 |
func (x *xof) Write(p []byte) (n int, err error) { |
| 77 |
if x.readMode { |
| 78 |
panic("blake2b: write to XOF after read") |
| 79 |
} |
| 80 |
return x.d.Write(p) |
| 81 |
} |
| 82 |
|
| 83 |
func (x *xof) Clone() XOF { |
| 84 |
clone := *x |
| 85 |
return &clone |
| 86 |
} |
| 87 |
|
| 88 |
func (x *xof) BlockSize() int { |
| 89 |
return x.d.BlockSize() |
| 90 |
} |
| 91 |
|
| 92 |
func (x *xof) Reset() { |
| 93 |
x.cfg[0] = byte(Size) |
| 94 |
binary.LittleEndian.PutUint32(x.cfg[4:], uint32(Size)) // leaf length |
| 95 |
binary.LittleEndian.PutUint32(x.cfg[12:], x.length) // XOF length |
| 96 |
x.cfg[17] = byte(Size) // inner hash size |
| 97 |
|
| 98 |
x.d.Reset() |
| 99 |
x.d.h[1] ^= uint64(x.length) << 32 |
| 100 |
|
| 101 |
x.remaining = uint64(x.length) |
| 102 |
if x.remaining == magicUnknownOutputLength { |
| 103 |
x.remaining = maxOutputLength |
| 104 |
} |
| 105 |
x.offset, x.nodeOffset = 0, 0 |
| 106 |
x.readMode = false |
| 107 |
} |
| 108 |
|
| 109 |
func (x *xof) Read(p []byte) (n int, err error) { |
| 110 |
if !x.readMode { |
| 111 |
x.d.finalize(&x.root) |
| 112 |
x.readMode = true |
| 113 |
} |
| 114 |
|
| 115 |
if x.remaining == 0 { |
| 116 |
return 0, io.EOF |
| 117 |
} |
| 118 |
|
| 119 |
n = len(p) |
| 120 |
if uint64(n) > x.remaining { |
| 121 |
n = int(x.remaining) |
| 122 |
p = p[:n] |
| 123 |
} |
| 124 |
|
| 125 |
if x.offset > 0 { |
| 126 |
blockRemaining := Size - x.offset |
| 127 |
if n < blockRemaining { |
| 128 |
x.offset += copy(p, x.block[x.offset:]) |
| 129 |
x.remaining -= uint64(n) |
| 130 |
return |
| 131 |
} |
| 132 |
copy(p, x.block[x.offset:]) |
| 133 |
p = p[blockRemaining:] |
| 134 |
x.offset = 0 |
| 135 |
x.remaining -= uint64(blockRemaining) |
| 136 |
} |
| 137 |
|
| 138 |
for len(p) >= Size { |
| 139 |
binary.LittleEndian.PutUint32(x.cfg[8:], x.nodeOffset) |
| 140 |
x.nodeOffset++ |
| 141 |
|
| 142 |
x.d.initConfig(&x.cfg) |
| 143 |
x.d.Write(x.root[:]) |
| 144 |
x.d.finalize(&x.block) |
| 145 |
|
| 146 |
copy(p, x.block[:]) |
| 147 |
p = p[Size:] |
| 148 |
x.remaining -= uint64(Size) |
| 149 |
} |
| 150 |
|
| 151 |
if todo := len(p); todo > 0 { |
| 152 |
if x.remaining < uint64(Size) { |
| 153 |
x.cfg[0] = byte(x.remaining) |
| 154 |
} |
| 155 |
binary.LittleEndian.PutUint32(x.cfg[8:], x.nodeOffset) |
| 156 |
x.nodeOffset++ |
| 157 |
|
| 158 |
x.d.initConfig(&x.cfg) |
| 159 |
x.d.Write(x.root[:]) |
| 160 |
x.d.finalize(&x.block) |
| 161 |
|
| 162 |
x.offset = copy(p, x.block[:todo]) |
| 163 |
x.remaining -= uint64(todo) |
| 164 |
} |
| 165 |
return |
| 166 |
} |
| 167 |
|
| 168 |
func (d *digest) initConfig(cfg *[Size]byte) { |
| 169 |
d.offset, d.c[0], d.c[1] = 0, 0, 0 |
| 170 |
for i := range d.h { |
| 171 |
d.h[i] = iv[i] ^ binary.LittleEndian.Uint64(cfg[i*8:]) |
| 172 |
} |
| 173 |
} |