| 1 |
// Copyright 2016 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 implements the BLAKE2b hash algorithm defined by RFC 7693 |
| 6 |
// and the extendable output function (XOF) BLAKE2Xb. |
| 7 |
// |
| 8 |
// BLAKE2b is optimized for 64-bit platforms—including NEON-enabled ARMs—and |
| 9 |
// produces digests of any size between 1 and 64 bytes. |
| 10 |
// For a detailed specification of BLAKE2b see https://blake2.net/blake2.pdf |
| 11 |
// and for BLAKE2Xb see https://blake2.net/blake2x.pdf |
| 12 |
// |
| 13 |
// If you aren't sure which function you need, use BLAKE2b (Sum512 or New512). |
| 14 |
// If you need a secret-key MAC (message authentication code), use the New512 |
| 15 |
// function with a non-nil key. |
| 16 |
// |
| 17 |
// BLAKE2X is a construction to compute hash values larger than 64 bytes. It |
| 18 |
// can produce hash values between 0 and 4 GiB. |
| 19 |
package blake2b |
| 20 |
|
| 21 |
import ( |
| 22 |
"encoding/binary" |
| 23 |
"errors" |
| 24 |
"hash" |
| 25 |
) |
| 26 |
|
| 27 |
var ( |
| 28 |
useAVX2 bool |
| 29 |
useAVX bool |
| 30 |
useSSE4 bool |
| 31 |
) |
| 32 |
|
| 33 |
var ( |
| 34 |
errKeySize = errors.New("blake2b: invalid key size") |
| 35 |
errHashSize = errors.New("blake2b: invalid hash size") |
| 36 |
) |
| 37 |
|
| 38 |
var iv = [8]uint64{ |
| 39 |
0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, |
| 40 |
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, |
| 41 |
} |
| 42 |
|
| 43 |
// Sum512 returns the BLAKE2b-512 checksum of the data. |
| 44 |
func Sum512(data []byte) [Size]byte { |
| 45 |
var sum [Size]byte |
| 46 |
checkSum(&sum, Size, data) |
| 47 |
return sum |
| 48 |
} |
| 49 |
|
| 50 |
// Sum384 returns the BLAKE2b-384 checksum of the data. |
| 51 |
func Sum384(data []byte) [Size384]byte { |
| 52 |
var sum [Size]byte |
| 53 |
var sum384 [Size384]byte |
| 54 |
checkSum(&sum, Size384, data) |
| 55 |
copy(sum384[:], sum[:Size384]) |
| 56 |
return sum384 |
| 57 |
} |
| 58 |
|
| 59 |
// Sum256 returns the BLAKE2b-256 checksum of the data. |
| 60 |
func Sum256(data []byte) [Size256]byte { |
| 61 |
var sum [Size]byte |
| 62 |
var sum256 [Size256]byte |
| 63 |
checkSum(&sum, Size256, data) |
| 64 |
copy(sum256[:], sum[:Size256]) |
| 65 |
return sum256 |
| 66 |
} |
| 67 |
|
| 68 |
// New512 returns a new hash.Hash computing the BLAKE2b-512 checksum. A non-nil |
| 69 |
// key turns the hash into a MAC. The key must be between zero and 64 bytes long. |
| 70 |
func New512(key []byte) (hash.Hash, error) { return newDigest(Size, key) } |
| 71 |
|
| 72 |
// New384 returns a new hash.Hash computing the BLAKE2b-384 checksum. A non-nil |
| 73 |
// key turns the hash into a MAC. The key must be between zero and 64 bytes long. |
| 74 |
func New384(key []byte) (hash.Hash, error) { return newDigest(Size384, key) } |
| 75 |
|
| 76 |
// New256 returns a new hash.Hash computing the BLAKE2b-256 checksum. A non-nil |
| 77 |
// key turns the hash into a MAC. The key must be between zero and 64 bytes long. |
| 78 |
func New256(key []byte) (hash.Hash, error) { return newDigest(Size256, key) } |
| 79 |
|
| 80 |
// New returns a new hash.Hash computing the BLAKE2b checksum with a custom length. |
| 81 |
// A non-nil key turns the hash into a MAC. The key must be between zero and 64 bytes long. |
| 82 |
// The hash size can be a value between 1 and 64 but it is highly recommended to use |
| 83 |
// values equal or greater than: |
| 84 |
// - 32 if BLAKE2b is used as a hash function (The key is zero bytes long). |
| 85 |
// - 16 if BLAKE2b is used as a MAC function (The key is at least 16 bytes long). |
| 86 |
// When the key is nil, the returned hash.Hash implements BinaryMarshaler |
| 87 |
// and BinaryUnmarshaler for state (de)serialization as documented by hash.Hash. |
| 88 |
func New(size int, key []byte) (hash.Hash, error) { return newDigest(size, key) } |
| 89 |
|
| 90 |
func newDigest(hashSize int, key []byte) (*digest, error) { |
| 91 |
if hashSize < 1 || hashSize > Size { |
| 92 |
return nil, errHashSize |
| 93 |
} |
| 94 |
if len(key) > Size { |
| 95 |
return nil, errKeySize |
| 96 |
} |
| 97 |
d := &digest{ |
| 98 |
size: hashSize, |
| 99 |
keyLen: len(key), |
| 100 |
} |
| 101 |
copy(d.key[:], key) |
| 102 |
d.Reset() |
| 103 |
return d, nil |
| 104 |
} |
| 105 |
|
| 106 |
func checkSum(sum *[Size]byte, hashSize int, data []byte) { |
| 107 |
h := iv |
| 108 |
h[0] ^= uint64(hashSize) | (1 << 16) | (1 << 24) |
| 109 |
var c [2]uint64 |
| 110 |
|
| 111 |
if length := len(data); length > BlockSize { |
| 112 |
n := length &^ (BlockSize - 1) |
| 113 |
if length == n { |
| 114 |
n -= BlockSize |
| 115 |
} |
| 116 |
hashBlocks(&h, &c, 0, data[:n]) |
| 117 |
data = data[n:] |
| 118 |
} |
| 119 |
|
| 120 |
var block [BlockSize]byte |
| 121 |
offset := copy(block[:], data) |
| 122 |
remaining := uint64(BlockSize - offset) |
| 123 |
if c[0] < remaining { |
| 124 |
c[1]-- |
| 125 |
} |
| 126 |
c[0] -= remaining |
| 127 |
|
| 128 |
hashBlocks(&h, &c, 0xFFFFFFFFFFFFFFFF, block[:]) |
| 129 |
|
| 130 |
for i, v := range h[:(hashSize+7)/8] { |
| 131 |
binary.LittleEndian.PutUint64(sum[8*i:], v) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
type digest struct { |
| 136 |
h [8]uint64 |
| 137 |
c [2]uint64 |
| 138 |
size int |
| 139 |
block [BlockSize]byte |
| 140 |
offset int |
| 141 |
|
| 142 |
key [BlockSize]byte |
| 143 |
keyLen int |
| 144 |
} |
| 145 |
|
| 146 |
const ( |
| 147 |
magic = "b2b" |
| 148 |
marshaledSize = len(magic) + 8*8 + 2*8 + 1 + BlockSize + 1 |
| 149 |
) |
| 150 |
|
| 151 |
func (d *digest) MarshalBinary() ([]byte, error) { |
| 152 |
if d.keyLen != 0 { |
| 153 |
return nil, errors.New("crypto/blake2b: cannot marshal MACs") |
| 154 |
} |
| 155 |
b := make([]byte, 0, marshaledSize) |
| 156 |
b = append(b, magic...) |
| 157 |
for i := 0; i < 8; i++ { |
| 158 |
b = appendUint64(b, d.h[i]) |
| 159 |
} |
| 160 |
b = appendUint64(b, d.c[0]) |
| 161 |
b = appendUint64(b, d.c[1]) |
| 162 |
// Maximum value for size is 64 |
| 163 |
b = append(b, byte(d.size)) |
| 164 |
b = append(b, d.block[:]...) |
| 165 |
b = append(b, byte(d.offset)) |
| 166 |
return b, nil |
| 167 |
} |
| 168 |
|
| 169 |
func (d *digest) UnmarshalBinary(b []byte) error { |
| 170 |
if len(b) < len(magic) || string(b[:len(magic)]) != magic { |
| 171 |
return errors.New("crypto/blake2b: invalid hash state identifier") |
| 172 |
} |
| 173 |
if len(b) != marshaledSize { |
| 174 |
return errors.New("crypto/blake2b: invalid hash state size") |
| 175 |
} |
| 176 |
b = b[len(magic):] |
| 177 |
for i := 0; i < 8; i++ { |
| 178 |
b, d.h[i] = consumeUint64(b) |
| 179 |
} |
| 180 |
b, d.c[0] = consumeUint64(b) |
| 181 |
b, d.c[1] = consumeUint64(b) |
| 182 |
d.size = int(b[0]) |
| 183 |
b = b[1:] |
| 184 |
copy(d.block[:], b[:BlockSize]) |
| 185 |
b = b[BlockSize:] |
| 186 |
d.offset = int(b[0]) |
| 187 |
return nil |
| 188 |
} |
| 189 |
|
| 190 |
func (d *digest) BlockSize() int { return BlockSize } |
| 191 |
|
| 192 |
func (d *digest) Size() int { return d.size } |
| 193 |
|
| 194 |
func (d *digest) Reset() { |
| 195 |
d.h = iv |
| 196 |
d.h[0] ^= uint64(d.size) | (uint64(d.keyLen) << 8) | (1 << 16) | (1 << 24) |
| 197 |
d.offset, d.c[0], d.c[1] = 0, 0, 0 |
| 198 |
if d.keyLen > 0 { |
| 199 |
d.block = d.key |
| 200 |
d.offset = BlockSize |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func (d *digest) Write(p []byte) (n int, err error) { |
| 205 |
n = len(p) |
| 206 |
|
| 207 |
if d.offset > 0 { |
| 208 |
remaining := BlockSize - d.offset |
| 209 |
if n <= remaining { |
| 210 |
d.offset += copy(d.block[d.offset:], p) |
| 211 |
return |
| 212 |
} |
| 213 |
copy(d.block[d.offset:], p[:remaining]) |
| 214 |
hashBlocks(&d.h, &d.c, 0, d.block[:]) |
| 215 |
d.offset = 0 |
| 216 |
p = p[remaining:] |
| 217 |
} |
| 218 |
|
| 219 |
if length := len(p); length > BlockSize { |
| 220 |
nn := length &^ (BlockSize - 1) |
| 221 |
if length == nn { |
| 222 |
nn -= BlockSize |
| 223 |
} |
| 224 |
hashBlocks(&d.h, &d.c, 0, p[:nn]) |
| 225 |
p = p[nn:] |
| 226 |
} |
| 227 |
|
| 228 |
if len(p) > 0 { |
| 229 |
d.offset += copy(d.block[:], p) |
| 230 |
} |
| 231 |
|
| 232 |
return |
| 233 |
} |
| 234 |
|
| 235 |
func (d *digest) Sum(sum []byte) []byte { |
| 236 |
var hash [Size]byte |
| 237 |
d.finalize(&hash) |
| 238 |
return append(sum, hash[:d.size]...) |
| 239 |
} |
| 240 |
|
| 241 |
func (d *digest) finalize(hash *[Size]byte) { |
| 242 |
var block [BlockSize]byte |
| 243 |
copy(block[:], d.block[:d.offset]) |
| 244 |
remaining := uint64(BlockSize - d.offset) |
| 245 |
|
| 246 |
c := d.c |
| 247 |
if c[0] < remaining { |
| 248 |
c[1]-- |
| 249 |
} |
| 250 |
c[0] -= remaining |
| 251 |
|
| 252 |
h := d.h |
| 253 |
hashBlocks(&h, &c, 0xFFFFFFFFFFFFFFFF, block[:]) |
| 254 |
|
| 255 |
for i, v := range h { |
| 256 |
binary.LittleEndian.PutUint64(hash[8*i:], v) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func appendUint64(b []byte, x uint64) []byte { |
| 261 |
var a [8]byte |
| 262 |
binary.BigEndian.PutUint64(a[:], x) |
| 263 |
return append(b, a[:]...) |
| 264 |
} |
| 265 |
|
| 266 |
func appendUint32(b []byte, x uint32) []byte { |
| 267 |
var a [4]byte |
| 268 |
binary.BigEndian.PutUint32(a[:], x) |
| 269 |
return append(b, a[:]...) |
| 270 |
} |
| 271 |
|
| 272 |
func consumeUint64(b []byte) ([]byte, uint64) { |
| 273 |
x := binary.BigEndian.Uint64(b) |
| 274 |
return b[8:], x |
| 275 |
} |
| 276 |
|
| 277 |
func consumeUint32(b []byte) ([]byte, uint32) { |
| 278 |
x := binary.BigEndian.Uint32(b) |
| 279 |
return b[4:], x |
| 280 |
} |