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.
5 // Package blake2s implements the BLAKE2s hash algorithm defined by RFC 7693
6 // and the extendable output function (XOF) BLAKE2Xs.
8 // For a detailed specification of BLAKE2s see https://blake2.net/blake2.pdf
9 // and for BLAKE2Xs see https://blake2.net/blake2x.pdf
11 // If you aren't sure which function you need, use BLAKE2s (Sum256 or New256).
12 // If you need a secret-key MAC (message authentication code), use the New256
13 // function with a non-nil key.
15 // BLAKE2X is a construction to compute hash values larger than 32 bytes. It
16 // can produce hash values between 0 and 65535 bytes.
17 package blake2s // import "golang.org/x/crypto/blake2s"
26 // The blocksize of BLAKE2s in bytes.
29 // The hash size of BLAKE2s-256 in bytes.
32 // The hash size of BLAKE2s-128 in bytes.
36 var errKeySize = errors.New("blake2s: invalid key size")
39 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
40 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
43 // Sum256 returns the BLAKE2s-256 checksum of the data.
44 func Sum256(data []byte) [Size]byte {
46 checkSum(&sum, Size, data)
50 // New256 returns a new hash.Hash computing the BLAKE2s-256 checksum. A non-nil
51 // key turns the hash into a MAC. The key must between zero and 32 bytes long.
52 func New256(key []byte) (hash.Hash, error) { return newDigest(Size, key) }
54 // New128 returns a new hash.Hash computing the BLAKE2s-128 checksum given a
55 // non-empty key. Note that a 128-bit digest is too small to be secure as a
56 // cryptographic hash and should only be used as a MAC, thus the key argument
58 func New128(key []byte) (hash.Hash, error) {
60 return nil, errors.New("blake2s: a key is required for a 128-bit hash")
62 return newDigest(Size128, key)
65 func newDigest(hashSize int, key []byte) (*digest, error) {
67 return nil, errKeySize
78 func checkSum(sum *[Size]byte, hashSize int, data []byte) {
85 h[0] ^= uint32(hashSize) | (1 << 16) | (1 << 24)
87 if length := len(data); length > BlockSize {
88 n := length &^ (BlockSize - 1)
92 hashBlocks(&h, &c, 0, data[:n])
96 var block [BlockSize]byte
97 offset := copy(block[:], data)
98 remaining := uint32(BlockSize - offset)
100 if c[0] < remaining {
105 hashBlocks(&h, &c, 0xFFFFFFFF, block[:])
107 for i, v := range h {
108 binary.LittleEndian.PutUint32(sum[4*i:], v)
116 block [BlockSize]byte
123 func (d *digest) BlockSize() int { return BlockSize }
125 func (d *digest) Size() int { return d.size }
127 func (d *digest) Reset() {
129 d.h[0] ^= uint32(d.size) | (uint32(d.keyLen) << 8) | (1 << 16) | (1 << 24)
130 d.offset, d.c[0], d.c[1] = 0, 0, 0
137 func (d *digest) Write(p []byte) (n int, err error) {
141 remaining := BlockSize - d.offset
143 d.offset += copy(d.block[d.offset:], p)
146 copy(d.block[d.offset:], p[:remaining])
147 hashBlocks(&d.h, &d.c, 0, d.block[:])
152 if length := len(p); length > BlockSize {
153 nn := length &^ (BlockSize - 1)
157 hashBlocks(&d.h, &d.c, 0, p[:nn])
161 d.offset += copy(d.block[:], p)
165 func (d *digest) Sum(sum []byte) []byte {
168 return append(sum, hash[:d.size]...)
171 func (d *digest) finalize(hash *[Size]byte) {
172 var block [BlockSize]byte
176 copy(block[:], d.block[:d.offset])
177 remaining := uint32(BlockSize - d.offset)
178 if c[0] < remaining {
183 hashBlocks(&h, &c, 0xFFFFFFFF, block[:])
184 for i, v := range h {
185 binary.LittleEndian.PutUint32(hash[4*i:], v)