File: hkdf.go

package info (click to toggle)
golang-github-flynn-noise 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,064 kB
  • sloc: makefile: 5
file content (49 lines) | stat: -rw-r--r-- 897 bytes parent folder | download | duplicates (3)
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
package noise

import (
	"crypto/hmac"
	"hash"
)

func hkdf(h func() hash.Hash, outputs int, out1, out2, out3, chainingKey, inputKeyMaterial []byte) ([]byte, []byte, []byte) {
	if len(out1) > 0 {
		panic("len(out1) > 0")
	}
	if len(out2) > 0 {
		panic("len(out2) > 0")
	}
	if len(out3) > 0 {
		panic("len(out3) > 0")
	}
	if outputs > 3 {
		panic("outputs > 3")
	}

	tempMAC := hmac.New(h, chainingKey)
	tempMAC.Write(inputKeyMaterial)
	tempKey := tempMAC.Sum(out2)

	out1MAC := hmac.New(h, tempKey)
	out1MAC.Write([]byte{0x01})
	out1 = out1MAC.Sum(out1)

	if outputs == 1 {
		return out1, nil, nil
	}

	out2MAC := hmac.New(h, tempKey)
	out2MAC.Write(out1)
	out2MAC.Write([]byte{0x02})
	out2 = out2MAC.Sum(out2)

	if outputs == 2 {
		return out1, out2, nil
	}

	out3MAC := hmac.New(h, tempKey)
	out3MAC.Write(out2)
	out3MAC.Write([]byte{0x03})
	out3 = out3MAC.Sum(out3)

	return out1, out2, out3
}