gmsm/drbg/hash_drbg.go

220 lines
6.1 KiB
Go
Raw Normal View History

2022-10-19 09:57:58 +08:00
package drbg
import (
"encoding/binary"
"errors"
"hash"
"time"
2022-10-20 11:30:20 +08:00
"github.com/emmansun/gmsm/sm3"
2022-10-19 09:57:58 +08:00
)
const HASH_DRBG_SEED_SIZE = 55
const HASH_DRBG_MAX_SEED_SIZE = 111
type HashDrbg struct {
2022-10-20 11:30:20 +08:00
BaseDrbg
md hash.Hash
c []byte
2022-10-19 09:57:58 +08:00
}
// NewHashDrbg create one hash DRBG instance
func NewHashDrbg(md hash.Hash, securityLevel SecurityLevel, gm bool, entropy, nonce, personalization []byte) (*HashDrbg, error) {
hd := &HashDrbg{}
hd.gm = gm
2022-10-21 10:22:10 +08:00
hd.md = md
hd.setSecurityLevel(securityLevel)
2022-10-19 09:57:58 +08:00
// here for the min length, we just check <=0 now
2022-10-21 10:22:10 +08:00
if len(entropy) == 0 || (hd.gm && len(entropy) < hd.md.Size()) || len(entropy) >= MAX_BYTES {
2022-10-19 09:57:58 +08:00
return nil, errors.New("invalid entropy length")
}
// here for the min length, we just check <=0 now
2022-10-21 10:46:18 +08:00
if len(nonce) == 0 || (hd.gm && len(nonce) < hd.md.Size()/2) || len(nonce) >= MAX_BYTES>>1 {
2022-10-19 09:57:58 +08:00
return nil, errors.New("invalid nonce length")
}
if len(personalization) >= MAX_BYTES {
return nil, errors.New("personalization is too long")
}
2022-10-20 11:30:20 +08:00
if md.Size() <= sm3.Size {
2022-10-19 09:57:58 +08:00
hd.v = make([]byte, HASH_DRBG_SEED_SIZE)
hd.c = make([]byte, HASH_DRBG_SEED_SIZE)
hd.seedLength = HASH_DRBG_SEED_SIZE
} else {
hd.v = make([]byte, HASH_DRBG_MAX_SEED_SIZE)
hd.c = make([]byte, HASH_DRBG_MAX_SEED_SIZE)
hd.seedLength = HASH_DRBG_MAX_SEED_SIZE
}
2022-10-21 10:22:10 +08:00
// seed_material = entropy_input || instantiation_nonce || personalization_string
2022-10-19 09:57:58 +08:00
seedMaterial := make([]byte, len(entropy)+len(nonce)+len(personalization))
copy(seedMaterial, entropy)
copy(seedMaterial[len(entropy):], nonce)
copy(seedMaterial[len(entropy)+len(nonce):], personalization)
2022-10-21 10:22:10 +08:00
// seed = Hash_df(seed_material, seed_length)
2022-10-19 09:57:58 +08:00
seed := hd.derive(seedMaterial, hd.seedLength)
2022-10-21 10:22:10 +08:00
// V = seed
2022-10-19 09:57:58 +08:00
copy(hd.v, seed)
2022-10-21 10:22:10 +08:00
// C = Hash_df(0x00 || V, seed_length)
2022-10-19 09:57:58 +08:00
temp := make([]byte, hd.seedLength+1)
temp[0] = 0
copy(temp[1:], seed)
seed = hd.derive(temp, hd.seedLength)
copy(hd.c, seed)
2022-10-21 10:22:10 +08:00
2022-10-19 09:57:58 +08:00
hd.reseedCounter = 1
hd.reseedTime = time.Now()
return hd, nil
}
2022-10-20 11:30:20 +08:00
// NewNISTHashDrbg return hash DRBG implementation which follows NIST standard
2022-10-19 09:57:58 +08:00
func NewNISTHashDrbg(md hash.Hash, securityLevel SecurityLevel, entropy, nonce, personalization []byte) (*HashDrbg, error) {
return NewHashDrbg(md, securityLevel, false, entropy, nonce, personalization)
}
2022-10-20 11:30:20 +08:00
// NewGMHashDrbg return hash DRBG implementation which follows GM/T 0105-2021 standard
func NewGMHashDrbg(securityLevel SecurityLevel, entropy, nonce, personalization []byte) (*HashDrbg, error) {
return NewHashDrbg(sm3.New(), securityLevel, true, entropy, nonce, personalization)
2022-10-19 09:57:58 +08:00
}
// Reseed hash DRBG reseed process. GM/T 0105-2021 has a little different with NIST.
func (hd *HashDrbg) Reseed(entropy, additional []byte) error {
// here for the min length, we just check <=0 now
2022-10-21 10:22:10 +08:00
if len(entropy) == 0 || (hd.gm && len(entropy) < hd.md.Size()) || len(entropy) >= MAX_BYTES {
2022-10-19 09:57:58 +08:00
return errors.New("invalid entropy length")
}
if len(additional) >= MAX_BYTES {
return errors.New("additional input too long")
}
seedMaterial := make([]byte, len(entropy)+hd.seedLength+len(additional)+1)
seedMaterial[0] = 1
2022-10-21 10:22:10 +08:00
if hd.gm { // seed_material = 0x01 || entropy_input || V || additional_input
2022-10-19 09:57:58 +08:00
copy(seedMaterial[1:], entropy)
copy(seedMaterial[len(entropy)+1:], hd.v)
2022-10-21 10:22:10 +08:00
} else { // seed_material = 0x01 || V || entropy_input || additional_input
2022-10-19 09:57:58 +08:00
copy(seedMaterial[1:], hd.v)
copy(seedMaterial[hd.seedLength+1:], entropy)
}
copy(seedMaterial[len(entropy)+hd.seedLength+1:], additional)
2022-10-21 10:22:10 +08:00
// seed = Hash_df(seed_material, seed_length)
2022-10-19 09:57:58 +08:00
seed := hd.derive(seedMaterial, hd.seedLength)
2022-10-21 10:22:10 +08:00
// V = seed
2022-10-19 09:57:58 +08:00
copy(hd.v, seed)
temp := make([]byte, hd.seedLength+1)
2022-10-21 10:22:10 +08:00
// C = Hash_df(0x01 || V, seed_length)
2022-10-19 09:57:58 +08:00
temp[0] = 0
copy(temp[1:], seed)
seed = hd.derive(temp, hd.seedLength)
copy(hd.c, seed)
2022-10-21 10:22:10 +08:00
2022-10-19 09:57:58 +08:00
hd.reseedCounter = 1
hd.reseedTime = time.Now()
return nil
}
func (hd *HashDrbg) addW(w []byte) {
t := make([]byte, hd.seedLength)
copy(t[hd.seedLength-len(w):], w)
add(t, hd.v, hd.seedLength)
}
func (hd *HashDrbg) addC() {
add(hd.c, hd.v, hd.seedLength)
}
func (hd *HashDrbg) addH() {
hd.md.Write([]byte{0x03})
hd.md.Write(hd.v)
hd.addW(hd.md.Sum(nil))
hd.md.Reset()
}
func (hd *HashDrbg) addReseedCounter() {
t := make([]byte, hd.seedLength)
binary.BigEndian.PutUint64(t[hd.seedLength-8:], hd.reseedCounter)
add(t, hd.v, hd.seedLength)
}
func (hd *HashDrbg) MaxBytesPerRequest() int {
if hd.gm {
return hd.md.Size()
}
return MAX_BYTES_PER_GENERATE
}
2022-10-21 10:22:10 +08:00
// Generate hash DRBG pseudorandom bits process. GM/T 0105-2021 has a little different with NIST.
2022-10-19 09:57:58 +08:00
// GM/T 0105-2021 can only generate no more than hash.Size bytes once.
func (hd *HashDrbg) Generate(b, additional []byte) error {
if hd.NeedReseed() {
return ErrReseedRequired
2022-10-19 09:57:58 +08:00
}
if (hd.gm && len(b) > hd.md.Size()) || (!hd.gm && len(b) > MAX_BYTES_PER_GENERATE) {
return errors.New("too many bytes requested")
}
md := hd.md
m := len(b)
2022-10-21 10:22:10 +08:00
// if len(additional_input) > 0, then
// w = Hash(0x02 || V || additional_input)
2022-10-19 09:57:58 +08:00
if len(additional) > 0 {
md.Write([]byte{0x02})
md.Write(hd.v)
md.Write(additional)
w := md.Sum(nil)
md.Reset()
hd.addW(w)
}
2022-10-21 10:22:10 +08:00
if hd.gm { // leftmost(Hash(V))
2022-10-19 09:57:58 +08:00
md.Write(hd.v)
copy(b, md.Sum(nil))
md.Reset()
} else {
limit := uint64(m+md.Size()-1) / uint64(md.Size())
data := make([]byte, hd.seedLength)
copy(data, hd.v)
for i := 0; i < int(limit); i++ {
md.Write(data)
copy(b[i*md.Size():], md.Sum(nil))
2022-10-20 11:30:20 +08:00
addOne(data, hd.seedLength)
2022-10-19 09:57:58 +08:00
md.Reset()
}
}
2022-10-21 10:22:10 +08:00
// V = (V + H + C + reseed_counter) mode 2^seed_length
2022-10-19 09:57:58 +08:00
hd.addH()
hd.addC()
hd.addReseedCounter()
hd.reseedCounter++
return nil
}
2022-10-21 10:22:10 +08:00
// derive Hash_df
2022-10-20 11:30:20 +08:00
func (hd *HashDrbg) derive(seedMaterial []byte, len int) []byte {
2022-10-19 09:57:58 +08:00
md := hd.md
limit := uint64(len+md.Size()-1) / uint64(md.Size())
var requireBytes [4]byte
binary.BigEndian.PutUint32(requireBytes[:], uint32(len<<3))
var ct byte = 1
k := make([]byte, len)
for i := 0; i < int(limit); i++ {
2022-10-21 10:22:10 +08:00
// Hash( counter_byte || return_bits || seed_material )
2022-10-19 09:57:58 +08:00
md.Write([]byte{ct})
md.Write(requireBytes[:])
2022-10-20 11:30:20 +08:00
md.Write(seedMaterial)
2022-10-19 09:57:58 +08:00
copy(k[i*md.Size():], md.Sum(nil))
ct++
md.Reset()
}
return k
}