OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / golang.org / x / crypto / blake2s / blake2s.go
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 blake2s implements the BLAKE2s hash algorithm defined by RFC 7693
6 // and the extendable output function (XOF) BLAKE2Xs.
7 //
8 // For a detailed specification of BLAKE2s see https://blake2.net/blake2.pdf
9 // and for BLAKE2Xs see https://blake2.net/blake2x.pdf
10 //
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.
14 //
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"
18
19 import (
20         "encoding/binary"
21         "errors"
22         "hash"
23 )
24
25 const (
26         // The blocksize of BLAKE2s in bytes.
27         BlockSize = 64
28
29         // The hash size of BLAKE2s-256 in bytes.
30         Size = 32
31
32         // The hash size of BLAKE2s-128 in bytes.
33         Size128 = 16
34 )
35
36 var errKeySize = errors.New("blake2s: invalid key size")
37
38 var iv = [8]uint32{
39         0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
40         0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
41 }
42
43 // Sum256 returns the BLAKE2s-256 checksum of the data.
44 func Sum256(data []byte) [Size]byte {
45         var sum [Size]byte
46         checkSum(&sum, Size, data)
47         return sum
48 }
49
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) }
53
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
57 // is not optional.
58 func New128(key []byte) (hash.Hash, error) {
59         if len(key) == 0 {
60                 return nil, errors.New("blake2s: a key is required for a 128-bit hash")
61         }
62         return newDigest(Size128, key)
63 }
64
65 func newDigest(hashSize int, key []byte) (*digest, error) {
66         if len(key) > Size {
67                 return nil, errKeySize
68         }
69         d := &digest{
70                 size:   hashSize,
71                 keyLen: len(key),
72         }
73         copy(d.key[:], key)
74         d.Reset()
75         return d, nil
76 }
77
78 func checkSum(sum *[Size]byte, hashSize int, data []byte) {
79         var (
80                 h [8]uint32
81                 c [2]uint32
82         )
83
84         h = iv
85         h[0] ^= uint32(hashSize) | (1 << 16) | (1 << 24)
86
87         if length := len(data); length > BlockSize {
88                 n := length &^ (BlockSize - 1)
89                 if length == n {
90                         n -= BlockSize
91                 }
92                 hashBlocks(&h, &c, 0, data[:n])
93                 data = data[n:]
94         }
95
96         var block [BlockSize]byte
97         offset := copy(block[:], data)
98         remaining := uint32(BlockSize - offset)
99
100         if c[0] < remaining {
101                 c[1]--
102         }
103         c[0] -= remaining
104
105         hashBlocks(&h, &c, 0xFFFFFFFF, block[:])
106
107         for i, v := range h {
108                 binary.LittleEndian.PutUint32(sum[4*i:], v)
109         }
110 }
111
112 type digest struct {
113         h      [8]uint32
114         c      [2]uint32
115         size   int
116         block  [BlockSize]byte
117         offset int
118
119         key    [BlockSize]byte
120         keyLen int
121 }
122
123 func (d *digest) BlockSize() int { return BlockSize }
124
125 func (d *digest) Size() int { return d.size }
126
127 func (d *digest) Reset() {
128         d.h = iv
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
131         if d.keyLen > 0 {
132                 d.block = d.key
133                 d.offset = BlockSize
134         }
135 }
136
137 func (d *digest) Write(p []byte) (n int, err error) {
138         n = len(p)
139
140         if d.offset > 0 {
141                 remaining := BlockSize - d.offset
142                 if n <= remaining {
143                         d.offset += copy(d.block[d.offset:], p)
144                         return
145                 }
146                 copy(d.block[d.offset:], p[:remaining])
147                 hashBlocks(&d.h, &d.c, 0, d.block[:])
148                 d.offset = 0
149                 p = p[remaining:]
150         }
151
152         if length := len(p); length > BlockSize {
153                 nn := length &^ (BlockSize - 1)
154                 if length == nn {
155                         nn -= BlockSize
156                 }
157                 hashBlocks(&d.h, &d.c, 0, p[:nn])
158                 p = p[nn:]
159         }
160
161         d.offset += copy(d.block[:], p)
162         return
163 }
164
165 func (d *digest) Sum(sum []byte) []byte {
166         var hash [Size]byte
167         d.finalize(&hash)
168         return append(sum, hash[:d.size]...)
169 }
170
171 func (d *digest) finalize(hash *[Size]byte) {
172         var block [BlockSize]byte
173         h := d.h
174         c := d.c
175
176         copy(block[:], d.block[:d.offset])
177         remaining := uint32(BlockSize - d.offset)
178         if c[0] < remaining {
179                 c[1]--
180         }
181         c[0] -= remaining
182
183         hashBlocks(&h, &c, 0xFFFFFFFF, block[:])
184         for i, v := range h {
185                 binary.LittleEndian.PutUint32(hash[4*i:], v)
186         }
187 }