forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concatkdf.go
55 lines (45 loc) · 1.05 KB
/
concatkdf.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
package concatkdf
import (
"crypto"
"encoding/binary"
"hash"
"github.com/lestrrat/go-jwx/buffer"
)
type KDF struct {
buf []byte
hash hash.Hash
otherinfo []byte
round uint32
z []byte
}
func New(hash crypto.Hash, alg, Z, apu, apv, pubinfo, privinfo []byte) *KDF {
algbuf := buffer.Buffer(alg).NData()
apubuf := buffer.Buffer(apu).NData()
apvbuf := buffer.Buffer(apv).NData()
concat := make([]byte, len(algbuf)+len(apubuf)+len(apvbuf)+len(pubinfo)+len(privinfo))
n := copy(concat, algbuf)
n += copy(concat[n:], apubuf)
n += copy(concat[n:], apvbuf)
n += copy(concat[n:], pubinfo)
n += copy(concat[n:], privinfo)
return &KDF{
hash: hash.New(),
otherinfo: concat,
round: 1,
z: Z,
}
}
func (k *KDF) Read(buf []byte) (int, error) {
h := k.hash
for len(buf) > len(k.buf) {
h.Reset()
binary.Write(h, binary.BigEndian, k.round)
h.Write(k.z)
h.Write(k.otherinfo)
k.buf = append(k.buf, h.Sum(nil)...)
k.round++
}
n := copy(buf, k.buf[:len(buf)])
k.buf = k.buf[len(buf):]
return n, nil
}