aboutsummaryrefslogtreecommitdiff
path: root/internal/cryptocore/hkdf.go
blob: 6944825a7a846ea2d5f2a88ce29e9919d6fd9fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cryptocore

import (
	"crypto/sha256"
	"log"

	"golang.org/x/crypto/hkdf"
)

// hkdfDerive derives "outLen" bytes from "masterkey" and "info" using
// HKDF-SHA256.
// It returns the derived bytes or panics.
func hkdfDerive(masterkey []byte, info string, outLen int) (out []byte) {
	h := hkdf.New(sha256.New, masterkey, nil, []byte(info))
	out = make([]byte, outLen)
	n, err := h.Read(out)
	if n != outLen || err != nil {
		log.Panicf("hkdfDerive: hkdf read failed, got %d bytes, error: %v", n, err)
	}
	return out
}