| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package blake2s |
| 11 |
|
| 12 |
import ( |
| 13 |
"errors" |
| 14 |
"hash" |
| 15 |
"strconv" |
| 16 |
|
| 17 |
"github.com/enceve/crypto" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
|
| 22 |
BlockSize = 64 |
| 23 |
|
| 24 |
Size = 32 |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type Config struct { |
| 33 |
Key []byte |
| 34 |
Salt []byte |
| 35 |
Personal []byte |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 117 |
|
| 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 |
| 143 |
hVal, hValCpy [8]uint32 |
| 144 |
ctr [2]uint32 |
| 145 |
block [BlockSize]byte |
| 146 |
off int |
| 147 |
|
| 148 |
hasKey bool |
| 149 |
key [BlockSize]byte |
| 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)) |
| 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 |
} |