gmsm/kdf/kdf.go

29 lines
718 B
Go
Raw Normal View History

2023-01-31 13:50:14 +08:00
// Package kdf implements ShangMi(SM) used Key Derivation Function, compliances with GB/T 32918.4-2016 5.4.3.
2022-08-25 11:48:41 +08:00
package kdf
import (
"encoding/binary"
"hash"
)
// Kdf key derivation function, compliance with GB/T 32918.4-2016 5.4.3.
// ANSI-X9.63-KDF
func Kdf(md hash.Hash, z []byte, len int) []byte {
limit := uint64(len+md.Size()-1) / uint64(md.Size())
if limit >= uint64(1<<32)-1 {
panic("kdf: key length too long")
}
var countBytes [4]byte
var ct uint32 = 1
2022-10-19 09:57:58 +08:00
k := make([]byte, len)
2022-08-25 11:48:41 +08:00
for i := 0; i < int(limit); i++ {
binary.BigEndian.PutUint32(countBytes[:], ct)
md.Write(z)
md.Write(countBytes[:])
copy(k[i*md.Size():], md.Sum(nil))
ct++
md.Reset()
}
2022-10-19 09:57:58 +08:00
return k
2022-08-25 11:48:41 +08:00
}