Skip to content

Commit 53515fb

Browse files
committed
crypto/tls: use hash.Cloner
A hash object needs to be cloned when doing certain steps in a TLS 1.3 server handshake. It is more efficient to use the hash.Cloner interface to clone a hash than to encode and decode the hash object using the binary encoding interfaces. We still need to support the binary encoding path in case the hash objects come from the fips140 v1.0.0 module, given that this module doesn't support the hash.Cloner interface. Change-Id: I8425e14e481dcefafc9aa1e5bfd63b61c22675ad Reviewed-on: https://go-review.googlesource.com/c/go/+/682597 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 13bb48e commit 53515fb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/crypto/tls/handshake_server_tls13.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,17 @@ func (hs *serverHandshakeStateTLS13) checkForResumption() error {
468468
return nil
469469
}
470470

471-
// cloneHash uses the encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
471+
// cloneHash uses [hash.Cloner] to clone in. If [hash.Cloner]
472+
// is not implemented or not supported, then it falls back to the
473+
// [encoding.BinaryMarshaler] and [encoding.BinaryUnmarshaler]
472474
// interfaces implemented by standard library hashes to clone the state of in
473475
// to a new instance of h. It returns nil if the operation fails.
474476
func cloneHash(in hash.Hash, h crypto.Hash) hash.Hash {
477+
if cloner, ok := in.(hash.Cloner); ok {
478+
if out, err := cloner.Clone(); err == nil {
479+
return out
480+
}
481+
}
475482
// Recreate the interface to avoid importing encoding.
476483
type binaryMarshaler interface {
477484
MarshalBinary() (data []byte, err error)

0 commit comments

Comments
 (0)