drbg: replace with for range

This commit is contained in:
Sun Yimin 2025-03-25 08:49:56 +08:00 committed by GitHub
parent a84fec09af
commit dd69d32930
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 10 deletions

View File

@ -120,12 +120,12 @@ func (hd *CtrDrbg) MaxBytesPerRequest() int {
} }
// Generate CTR DRBG pseudorandom bits generate process. // 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() { if hd.NeedReseed() {
return ErrReseedRequired return ErrReseedRequired
} }
outlen := len(hd.v) 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") 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) block := hd.newBlockCipher(hd.key)
temp := make([]byte, outlen) temp := make([]byte, outlen)
m := len(b) m := len(out)
limit := uint64(m+outlen-1) / uint64(outlen) 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) // V = (V + 1) mod 2^outlen)
addOne(hd.v, outlen) addOne(hd.v, outlen)
// output_block = Encrypt(Key, V) // output_block = Encrypt(Key, V)
block.Encrypt(temp, hd.v) block.Encrypt(temp, hd.v)
copy(b[i*outlen:], temp) copy(out[i*outlen:], temp)
} }
hd.update(additional) hd.update(additional)
hd.reseedCounter++ hd.reseedCounter++
@ -162,7 +162,7 @@ func (cd *CtrDrbg) update(seedMaterial []byte) {
v := make([]byte, outlen) v := make([]byte, outlen)
output := make([]byte, outlen) output := make([]byte, outlen)
copy(v, cd.v) 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 // V = (V + 1) mod 2^outlen
addOne(v, outlen) addOne(v, outlen)
// output_block = Encrypt(Key, V) // output_block = Encrypt(Key, V)
@ -191,7 +191,7 @@ func (cd *CtrDrbg) derive(seedMaterial []byte, returnBytes int) []byte {
S[outlen+8+len(seedMaterial)] = 0x80 S[outlen+8+len(seedMaterial)] = 0x80
key := make([]byte, cd.keyLen) key := make([]byte, cd.keyLen)
for i := 0; i < cd.keyLen; i++ { for i := range cd.keyLen {
key[i] = byte(i) key[i] = byte(i)
} }
blocks := (cd.seedLength + outlen - 1) / outlen blocks := (cd.seedLength + outlen - 1) / outlen

View File

@ -187,7 +187,7 @@ func (hd *HashDrbg) Generate(b, additional []byte) error {
limit := uint64(m+md.Size()-1) / uint64(md.Size()) limit := uint64(m+md.Size()-1) / uint64(md.Size())
data := make([]byte, hd.seedLength) data := make([]byte, hd.seedLength)
copy(data, hd.v) copy(data, hd.v)
for i := 0; i < int(limit); i++ { for i := range int(limit) {
md.Write(data) md.Write(data)
copy(b[i*md.Size():], md.Sum(nil)) copy(b[i*md.Size():], md.Sum(nil))
addOne(data, hd.seedLength) addOne(data, hd.seedLength)
@ -211,7 +211,7 @@ func (hd *HashDrbg) derive(seedMaterial []byte, len int) []byte {
byteorder.BEPutUint32(requireBytes[:], uint32(len<<3)) byteorder.BEPutUint32(requireBytes[:], uint32(len<<3))
var ct byte = 1 var ct byte = 1
k := make([]byte, len) k := make([]byte, len)
for i := 0; i < int(limit); i++ { for i := range int(limit) {
// Hash( counter_byte || return_bits || seed_material ) // Hash( counter_byte || return_bits || seed_material )
md.Write([]byte{ct}) md.Write([]byte{ct})
md.Write(requireBytes[:]) md.Write(requireBytes[:])

View File

@ -45,7 +45,7 @@ func NewHmacDrbg(newHash func() hash.Hash, securityLevel SecurityLevel, gm bool,
// HMAC_DRBG_Instantiate_process // HMAC_DRBG_Instantiate_process
hd.key = make([]byte, hd.hashSize) hd.key = make([]byte, hd.hashSize)
hd.v = 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.key[i] = 0x00
hd.v[i] = 0x01 hd.v[i] = 0x01
} }