Skip to content

Commit f9701d2

Browse files
kolyshkinseankhliao
authored andcommitted
crypto: use clear built-in
Replace for loops with clear built-in, available since Go 1.21. Change-Id: I16a2691a68042e9c5cd9bc4197690fa541a081eb Reviewed-on: https://go-review.googlesource.com/c/go/+/704877 Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a58afe4 commit f9701d2

File tree

5 files changed

+6
-18
lines changed

5 files changed

+6
-18
lines changed

src/crypto/internal/fips140/mlkem/field.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,7 @@ func sliceForAppend(in []byte, n int) (head, tail []byte) {
203203
// followed by ByteEncode₁, according to FIPS 203, Algorithm 5.
204204
func ringCompressAndEncode1(s []byte, f ringElement) []byte {
205205
s, b := sliceForAppend(s, encodingSize1)
206-
for i := range b {
207-
b[i] = 0
208-
}
206+
clear(b)
209207
for i := range f {
210208
b[i/8] |= uint8(compress(f[i], 1) << (i % 8))
211209
}

src/crypto/internal/fips140/sha3/sha3.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ func (d *Digest) Size() int { return d.outputLen }
6161
// Reset resets the Digest to its initial state.
6262
func (d *Digest) Reset() {
6363
// Zero the permutation's state.
64-
for i := range d.a {
65-
d.a[i] = 0
66-
}
64+
clear(d.a[:])
6765
d.state = spongeAbsorbing
6866
d.n = 0
6967
}

src/crypto/rc4/rc4.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ func NewCipher(key []byte) (*Cipher, error) {
5555
// Deprecated: Reset can't guarantee that the key will be entirely removed from
5656
// the process's memory.
5757
func (c *Cipher) Reset() {
58-
for i := range c.s {
59-
c.s[i] = 0
60-
}
58+
clear(c.s[:])
6159
c.i, c.j = 0, 0
6260
}
6361

src/crypto/tls/conn.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ func (hc *halfConn) changeCipherSpec() error {
220220
hc.mac = hc.nextMac
221221
hc.nextCipher = nil
222222
hc.nextMac = nil
223-
for i := range hc.seq {
224-
hc.seq[i] = 0
225-
}
223+
clear(hc.seq[:])
226224
return nil
227225
}
228226

@@ -231,9 +229,7 @@ func (hc *halfConn) setTrafficSecret(suite *cipherSuiteTLS13, level QUICEncrypti
231229
hc.level = level
232230
key, iv := suite.trafficKey(secret)
233231
hc.cipher = suite.aead(key, iv)
234-
for i := range hc.seq {
235-
hc.seq[i] = 0
236-
}
232+
clear(hc.seq[:])
237233
}
238234

239235
// incSeq increments the sequence number.

src/crypto/tls/handshake_server_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,9 +1590,7 @@ var getConfigForClientTests = []struct {
15901590
},
15911591
func(clientHello *ClientHelloInfo) (*Config, error) {
15921592
config := testConfig.Clone()
1593-
for i := range config.SessionTicketKey {
1594-
config.SessionTicketKey[i] = 0
1595-
}
1593+
clear(config.SessionTicketKey[:])
15961594
config.sessionTicketKeys = nil
15971595
return config, nil
15981596
},

0 commit comments

Comments
 (0)