Debian Codegraph @

blake2/blake2b/blake2b.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 // Package blake2b implements the BLAKE2b hash function.
5 // BLAKE2b produces hash values from 8 to 512 bit (1 to 64 byte),
6 // and can be configured as a MAC. Furthermore BLAKE2b supports
7 // salted/randomized and personalized hashing.
8 // BLAKE2b can process messages up to 2^128 bytes, which is enough
9 // for all practical use cases.
10 package blake2b
11
12 import (
13 "errors"
14 "hash"
15 "strconv"
16
17 "github.com/enceve/crypto"
18 )
19
20 const (
21 // The BLAKE2b block size in bytes.
22 BlockSize = 128
23 // The max. size of the BLAKE2b checksum
24 Size = 64
25 )
26
27 // Config contains the BLAKE2b configuration:
28 // - Key for computing MACs
29 // - Salt for randomized hashing
30 // - Personal for personalized hashing
31 // All fields are optional and can be nil.
32 type Config struct {
33 Key []byte // The key for MAC (length must between 0 and 64)
34 Salt []byte // The salt (length must between 0 and 16)
35 Personal []byte // The personalization for unique hashing (length must between 0 and 16)
36 }
37
38 // Configure takes the hash size and the BLAKE2b configuration and
39 // computes the 8 64-bit chain values. The conf is optional and can be nil,
40 // or must be a valid configuraton struct - otherwise a non-nil error is returned.
41 func Configure(hVal *[8]uint64, hashsize int, conf *Config) error {
42 if hashsize <= 0 || hashsize > Size {
43 return errors.New("illegal hash size " + strconv.Itoa(hashsize))
44 }
45
46 var key, salt, personal []byte
47 if conf != nil {
48 key = conf.Key
49 salt = conf.Salt
50 personal = conf.Personal
51 }
52 if k := len(key); k > Size {
53 return crypto.KeySizeError(k)
54 }
55 if s := len(salt); s > 16 {
56 return errors.New("illegal salt size " + strconv.Itoa(s))
57 }
58 if p := len(personal); p > 16 {
59 return errors.New("illegal personalization size " + strconv.Itoa(p))
60 }
61
62 var p [BlockSize]byte
63 p[0] = byte(hashsize)
64 p[1] = byte(len(key))
65 p[2] = 1
66 p[3] = 1
67 if len(salt) > 0 {
68 copy(p[32:], salt)
69 }
70 if len(personal) > 0 {
71 copy(p[48:], personal)
72 }
73
74 for i := range iv {
75 j := i * 8
76 v := uint64(p[j+0]) | uint64(p[j+1])<<8 | uint64(p[j+2])<<16 | uint64(p[j+3])<<24 |
77 uint64(p[j+4])<<32 | uint64(p[j+5])<<40 | uint64(p[j+6])<<48 | uint64(p[j+7])<<56
78 hVal[i] = iv[i] ^ v
79 }
80 return nil
81 }
82
83 // Sum returns the BLAKE2b checksum with the given hash size of msg using the (optional)
84 // conf for configuration. This function returns a non-nil error if the configuration
85 // is invalid.
86 func Sum(msg []byte, hashsize int, conf *Config) ([]byte, error) {
87 h, err := New(hashsize, conf)
88 if err != nil {
89 return nil, err
90 }
91
92 h.Write(msg)
93
94 return h.Sum(nil), nil
95 }
96
97 // New returns a hash.Hash computing the BLAKE2b checksum with the given hash size
98 // using the (optional) conf for configuration. This function returns a non-nil error
99 // if the configuration is invalid.
100 func New(hashsize int, conf *Config) (hash.Hash, error) {
101 h := new(hashFunc)
102 if err := Configure(&(h.hVal), hashsize, conf); err != nil {
103 return nil, err
104 }
105 h.hashsize = hashsize
106 h.hValCpy = h.hVal
107
108 if conf != nil && len(conf.Key) > 0 {
109 copy(h.key[:], conf.Key)
110 h.block = h.key
111 h.off = BlockSize
112 h.hasKey = true
113 }
114 return h, nil
115 }
116
117 // ExtractHash takes the 8 64-bit chain values, the 128-bit counter, the 128 byte block
118 // and a block-offset and extracts the checksum to out.
119 func ExtractHash(out *[Size]byte, hVal *[8]uint64, ctr *[2]uint64, block *[BlockSize]byte, off int) {
120 diff := uint64(BlockSize - off)
121 if ctr[0] < diff {
122 ctr[1]--
123 }
124 ctr[0] -= diff
125
126 for i := off; i < BlockSize; i++ {
127 block[i] = 0
128 }
129
130 Core(hVal, ctr, FinalFlag, block[:])
131
132 j := 0
133 for _, s := range hVal {
134 out[j+0] = byte(s >> 0)
135 out[j+1] = byte(s >> 8)
136 out[j+2] = byte(s >> 16)
137 out[j+3] = byte(s >> 24)
138 out[j+4] = byte(s >> 32)
139 out[j+5] = byte(s >> 40)
140 out[j+6] = byte(s >> 48)
141 out[j+7] = byte(s >> 56)
142 j += 8
143 }
144 }
145
146 type hashFunc struct {
147 hashsize int // the hash size in bytes
148 hVal, hValCpy [8]uint64 // the chain values
149 ctr [2]uint64 // the counter (max 2^128 bytes)
150 block [BlockSize]byte // the buffer
151 off int // the buffer offset
152
153 hasKey bool // flag indicating MAC usage
154 key [BlockSize]byte // the key for MAC
155 }
156
157 func (h *hashFunc) BlockSize() int { return BlockSize }
158
159 func (h *hashFunc) Size() int { return h.hashsize }
160
161 func (h *hashFunc) Write(p []byte) (int, error) {
162 n := len(p)
163
164 dif := BlockSize - h.off
165 if h.off > 0 && n > dif {
166 h.off += copy(h.block[h.off:], p[:dif])
167 p = p[dif:]
168 if h.off == BlockSize && len(p) > 0 {
169 Core(&(h.hVal), &(h.ctr), MsgFlag, h.block[:])
170 h.off = 0
171 }
172 }
173
174 if length := len(p); length > BlockSize {
175 nb := length & (^(BlockSize - 1)) // length -= (length % BlockSize)
176 if length == nb {
177 nb -= BlockSize
178 }
179 Core(&(h.hVal), &(h.ctr), MsgFlag, p[:nb])
180 p = p[nb:]
181 }
182 if len(p) > 0 {
183 h.off += copy(h.block[h.off:], p)
184 }
185 return n, nil
186 }
187
188 func (h *hashFunc) Reset() {
189 h.hVal = h.hValCpy
190 h.ctr[0], h.ctr[1] = 0, 0
191 for i := range h.block {
192 h.block[i] = 0
193 }
194 h.off = 0
195 if h.hasKey {
196 h.block = h.key
197 h.off = BlockSize
198 }
199 }
200
201 func (h *hashFunc) Sum(b []byte) []byte {
202 hVal := h.hVal
203 ctr := h.ctr
204 buf := h.block
205 off := h.off
206
207 var out [Size]byte
208 ExtractHash(&out, &hVal, &ctr, &buf, off)
209
210 return append(b, out[:h.hashsize]...)
211 }