2020-06-06 05:11:30 -04:00
|
|
|
package aead
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"hash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func KDF(key []byte, path ...string) []byte {
|
2021-02-22 08:20:36 -05:00
|
|
|
hmacCreator := &hMacCreator{value: []byte(KDFSaltConstVMessAEADKDF)}
|
2020-06-06 05:11:30 -04:00
|
|
|
for _, v := range path {
|
2021-02-22 08:20:36 -05:00
|
|
|
hmacCreator = &hMacCreator{value: []byte(v), parent: hmacCreator}
|
2020-06-06 05:11:30 -04:00
|
|
|
}
|
2021-02-22 08:20:36 -05:00
|
|
|
hmacf := hmacCreator.Create()
|
2020-06-06 05:11:30 -04:00
|
|
|
hmacf.Write(key)
|
|
|
|
return hmacf.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:20:36 -05:00
|
|
|
type hMacCreator struct {
|
|
|
|
parent *hMacCreator
|
|
|
|
value []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hMacCreator) Create() hash.Hash {
|
|
|
|
if h.parent == nil {
|
|
|
|
return hmac.New(sha256.New, h.value)
|
|
|
|
}
|
|
|
|
return hmac.New(h.parent.Create, h.value)
|
|
|
|
}
|
|
|
|
|
2020-06-06 05:11:30 -04:00
|
|
|
func KDF16(key []byte, path ...string) []byte {
|
|
|
|
r := KDF(key, path...)
|
|
|
|
return r[:16]
|
|
|
|
}
|