-
Notifications
You must be signed in to change notification settings - Fork 14
/
kdf.go
66 lines (50 loc) · 1.25 KB
/
kdf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2015 The GoTor Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
)
func KDFTOR(bytes int, random []byte) []byte {
tmp := make([]byte, len(random)+1)
copy(tmp, random)
result := make([]byte, bytes+(20-(bytes%20)))
for i, left := 0, bytes; left > 0; left -= 20 {
tmp[len(random)] = byte(i)
sha := sha1.Sum(tmp)
copy(result[20*i:20*(i+1)], sha[:])
i++
}
return result[:bytes]
}
func KDFHKDF(bytes int, secretInput, key, mExpand []byte) []byte {
if bytes == 0 {
return nil
}
result := make([]byte, bytes+(32-(bytes%32)))
// Calculate "KEY_SEED"
kSeed := hmac.New(sha256.New, key)
kSeed.Write(secretInput)
keySeed := kSeed.Sum(nil)
mac := hmac.New(sha256.New, keySeed)
var singleByte [1]byte
singleByte[0] = 1
mac.Write(mExpand)
mac.Write(singleByte[:])
copy(result[0:32], mac.Sum(nil))
gotBytes := 32
i := 0
for bytes > gotBytes {
mac.Reset()
i++
mac.Write(result[gotBytes-32 : gotBytes])
mac.Write(mExpand)
singleByte[0] = byte(i + 1)
mac.Write(singleByte[:])
copy(result[gotBytes:gotBytes+32], mac.Sum(nil))
gotBytes += 32
}
return result[0:bytes]
}