Debian Codegraph @

blake2/blake2.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 blake2 implements the both hash functions BLAKE2b and
5 // BLAKE2s described in RFC 7693.
6 // RFC 7693 only specifies BLAKE2 as pure hash and mac function,
7 // but BLAKE2 supports also salted (randomized) and personalized
8 // hashing. These features are implemented in the blake2b package
9 // for BLAKE2b and in the blake2s package for BLAKE2s.
10 package blake2
11
12 import (
13 "github.com/enceve/crypto/blake2/blake2b"
14 "github.com/enceve/crypto/blake2/blake2s"
15 )
16
17 var (
18 hVal512 = [8]uint64{
19 0x6a09e667f2bdc948, 0xbb67ae8584caa73b,
20 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
21 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
22 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
23 }
24 hVal256b = [8]uint64{
25 0x6a09e667f2bdc928, 0xbb67ae8584caa73b,
26 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
27 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
28 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
29 }
30 hVal256s = [8]uint32{
31 0x6b08e647, 0xbb67ae85,
32 0x3c6ef372, 0xa54ff53a,
33 0x510e527f, 0x9b05688c,
34 0x1f83d9ab, 0x5be0cd19,
35 }
36 hVal160s = [8]uint32{
37 0x6b08e673, 0xbb67ae85,
38 0x3c6ef372, 0xa54ff53a,
39 0x510e527f, 0x9b05688c,
40 0x1f83d9ab, 0x5be0cd19,
41 }
42 )
43
44 // Sum512 computes the 512 bit BLAKE2b checksum (or MAC if a key is specified)
45 // of msg and saves the sum in out. The key is optional and can be nil.
46 func Sum512(out *[64]byte, msg, key []byte) {
47 var (
48 hVal [8]uint64
49 ctr [2]uint64
50 block [blake2b.BlockSize]byte
51 off int
52 )
53 hVal = hVal512
54
55 if k := len(key); k > 0 {
56 hVal[0] ^= uint64(k) << 8 // mixin key-length
57
58 copy(block[:], key)
59 off = blake2b.BlockSize
60 if len(msg) > 0 {
61 blake2b.Core(&hVal, &ctr, blake2b.MsgFlag, block[:])
62 off = 0
63 }
64 }
65
66 if length := len(msg); length > blake2b.BlockSize {
67 n := length & (^(blake2b.BlockSize - 1)) // length -= (length % BlockSize)
68 if length == n {
69 n -= blake2b.BlockSize
70 }
71 blake2b.Core(&hVal, &ctr, blake2b.MsgFlag, msg[:n])
72 msg = msg[n:]
73 }
74
75 if len(msg) > 0 {
76 off += copy(block[:], msg)
77 }
78
79 blake2b.ExtractHash(out, &hVal, &ctr, &block, off)
80 }
81
82 // Sum256b computes the 256 bit BLAKE2b checksum (or MAC if a key is specified)
83 // of msg and saves the sum in out. The key is optional and can be nil.
84 func Sum256b(out *[32]byte, msg, key []byte) {
85 var (
86 hVal [8]uint64
87 ctr [2]uint64
88 block [blake2b.BlockSize]byte
89 off int
90
91 out512 [64]byte
92 )
93 hVal = hVal256b
94
95 if k := len(key); k > 0 {
96 hVal[0] ^= uint64(k) << 8 // mixin key-length
97
98 copy(block[:], key)
99 off = blake2b.BlockSize
100 if len(msg) > 0 {
101 blake2b.Core(&hVal, &ctr, blake2b.MsgFlag, block[:])
102 off = 0
103 }
104 }
105
106 if length := len(msg); length > blake2b.BlockSize {
107 n := length & (^(blake2b.BlockSize - 1)) // length -= (length % BlockSize)
108 if length == n {
109 n -= blake2b.BlockSize
110 }
111 blake2b.Core(&hVal, &ctr, blake2b.MsgFlag, msg[:n])
112 msg = msg[n:]
113 }
114
115 if len(msg) > 0 {
116 off += copy(block[:], msg)
117 }
118
119 blake2b.ExtractHash(&out512, &hVal, &ctr, &block, off)
120 copy(out[:], out512[:32])
121 }
122
123 // Sum256s computes the 256 bit BLAKE2s checksum (or MAC if a key is specified)
124 // of msg and saves the sum in out. The key is optional and can be nil.
125 func Sum256s(out *[32]byte, msg, key []byte) {
126 var (
127 hVal [8]uint32
128 ctr [2]uint32
129 block [blake2s.BlockSize]byte
130 off int
131 )
132 hVal = hVal256s
133
134 if k := len(key); k > 0 {
135 hVal[0] ^= uint32(k) << 8 // mixin key-length
136
137 copy(block[:], key)
138 off = blake2s.BlockSize
139 if len(msg) > 0 {
140 blake2s.Core(&hVal, &ctr, blake2s.MsgFlag, block[:])
141 off = 0
142 }
143 }
144
145 if length := len(msg); length > blake2s.BlockSize {
146 n := length & (^(blake2s.BlockSize - 1)) // length -= (length % BlockSize)
147 if length == n {
148 n -= blake2s.BlockSize
149 }
150 blake2s.Core(&hVal, &ctr, blake2s.MsgFlag, msg[:n])
151 msg = msg[n:]
152 }
153
154 if len(msg) > 0 {
155 off += copy(block[:], msg)
156 }
157
158 blake2s.ExtractHash(out, &hVal, &ctr, &block, off)
159 }
160
161 // Sum160s computes the 160 bit BLAKE2s checksum (or MAC if a key is specified)
162 // of msg and saves the sum in out. The key is optional and can be nil.
163 func Sum160s(out *[20]byte, msg, key []byte) {
164 var (
165 hVal [8]uint32
166 ctr [2]uint32
167 block [blake2s.BlockSize]byte
168 off int
169
170 out256 [32]byte
171 )
172 hVal = hVal160s
173
174 if k := len(key); k > 0 {
175 hVal[0] ^= uint32(k) << 8 // mixin key-length
176
177 copy(block[:], key)
178 off = blake2s.BlockSize
179 if len(msg) > 0 {
180 blake2s.Core(&hVal, &ctr, blake2s.MsgFlag, block[:])
181 off = 0
182 }
183 }
184
185 if length := len(msg); length > blake2s.BlockSize {
186 n := length & (^(blake2s.BlockSize - 1)) // length -= (length % BlockSize)
187 if length == n {
188 n -= blake2s.BlockSize
189 }
190 blake2s.Core(&hVal, &ctr, blake2s.MsgFlag, msg[:n])
191 msg = msg[n:]
192 }
193
194 if len(msg) > 0 {
195 off += copy(block[:], msg)
196 }
197
198 blake2s.ExtractHash(&out256, &hVal, &ctr, &block, off)
199 copy(out[:], out256[:20])
200 }