Debian Codegraph @

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