From dd69d3293004fe0513e61d83574a556e3accfd01 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Tue, 25 Mar 2025 08:49:56 +0800 Subject: [PATCH] drbg: replace with for range --- drbg/ctr_drbg.go | 14 +++++++------- drbg/hash_drbg.go | 4 ++-- drbg/hmac_drbg.go | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/drbg/ctr_drbg.go b/drbg/ctr_drbg.go index f55e917..26b64e7 100644 --- a/drbg/ctr_drbg.go +++ b/drbg/ctr_drbg.go @@ -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 diff --git a/drbg/hash_drbg.go b/drbg/hash_drbg.go index a8d51da..dcc0d19 100644 --- a/drbg/hash_drbg.go +++ b/drbg/hash_drbg.go @@ -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[:]) diff --git a/drbg/hmac_drbg.go b/drbg/hmac_drbg.go index b19ccb6..6b39db5 100644 --- a/drbg/hmac_drbg.go +++ b/drbg/hmac_drbg.go @@ -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 }