mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 04:06:18 +08:00
drbg: replace with for range
This commit is contained in:
parent
a84fec09af
commit
dd69d32930
@ -120,12 +120,12 @@ func (hd *CtrDrbg) MaxBytesPerRequest() int {
|
||||
}
|
||||
|
||||
// Generate CTR DRBG pseudorandom bits generate process.
|
||||
func (hd *CtrDrbg) Generate(b, additional []byte) error {
|
||||
func (hd *CtrDrbg) Generate(out, additional []byte) error {
|
||||
if hd.NeedReseed() {
|
||||
return ErrReseedRequired
|
||||
}
|
||||
outlen := len(hd.v)
|
||||
if (hd.gm && len(b) > outlen) || (!hd.gm && len(b) > MAX_BYTES_PER_GENERATE) {
|
||||
if (hd.gm && len(out) > outlen) || (!hd.gm && len(out) > MAX_BYTES_PER_GENERATE) {
|
||||
return errors.New("drbg: too many bytes requested")
|
||||
}
|
||||
|
||||
@ -140,14 +140,14 @@ func (hd *CtrDrbg) Generate(b, additional []byte) error {
|
||||
block := hd.newBlockCipher(hd.key)
|
||||
temp := make([]byte, outlen)
|
||||
|
||||
m := len(b)
|
||||
m := len(out)
|
||||
limit := uint64(m+outlen-1) / uint64(outlen)
|
||||
for i := 0; i < int(limit); i++ {
|
||||
for i := range int(limit) {
|
||||
// V = (V + 1) mod 2^outlen)
|
||||
addOne(hd.v, outlen)
|
||||
// output_block = Encrypt(Key, V)
|
||||
block.Encrypt(temp, hd.v)
|
||||
copy(b[i*outlen:], temp)
|
||||
copy(out[i*outlen:], temp)
|
||||
}
|
||||
hd.update(additional)
|
||||
hd.reseedCounter++
|
||||
@ -162,7 +162,7 @@ func (cd *CtrDrbg) update(seedMaterial []byte) {
|
||||
v := make([]byte, outlen)
|
||||
output := make([]byte, outlen)
|
||||
copy(v, cd.v)
|
||||
for i := 0; i < (cd.seedLength+outlen-1)/outlen; i++ {
|
||||
for i := range (cd.seedLength+outlen-1)/outlen {
|
||||
// V = (V + 1) mod 2^outlen
|
||||
addOne(v, outlen)
|
||||
// output_block = Encrypt(Key, V)
|
||||
@ -191,7 +191,7 @@ func (cd *CtrDrbg) derive(seedMaterial []byte, returnBytes int) []byte {
|
||||
S[outlen+8+len(seedMaterial)] = 0x80
|
||||
|
||||
key := make([]byte, cd.keyLen)
|
||||
for i := 0; i < cd.keyLen; i++ {
|
||||
for i := range cd.keyLen {
|
||||
key[i] = byte(i)
|
||||
}
|
||||
blocks := (cd.seedLength + outlen - 1) / outlen
|
||||
|
@ -187,7 +187,7 @@ func (hd *HashDrbg) Generate(b, additional []byte) error {
|
||||
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++ {
|
||||
for i := range int(limit) {
|
||||
md.Write(data)
|
||||
copy(b[i*md.Size():], md.Sum(nil))
|
||||
addOne(data, hd.seedLength)
|
||||
@ -211,7 +211,7 @@ func (hd *HashDrbg) derive(seedMaterial []byte, len int) []byte {
|
||||
byteorder.BEPutUint32(requireBytes[:], uint32(len<<3))
|
||||
var ct byte = 1
|
||||
k := make([]byte, len)
|
||||
for i := 0; i < int(limit); i++ {
|
||||
for i := range int(limit) {
|
||||
// Hash( counter_byte || return_bits || seed_material )
|
||||
md.Write([]byte{ct})
|
||||
md.Write(requireBytes[:])
|
||||
|
@ -45,7 +45,7 @@ func NewHmacDrbg(newHash func() hash.Hash, securityLevel SecurityLevel, gm bool,
|
||||
// HMAC_DRBG_Instantiate_process
|
||||
hd.key = make([]byte, hd.hashSize)
|
||||
hd.v = make([]byte, hd.hashSize)
|
||||
for i := 0; i < hd.hashSize; i++ {
|
||||
for i := range hd.hashSize {
|
||||
hd.key[i] = 0x00
|
||||
hd.v[i] = 0x01
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user