Add a []byte argument to hash.Hash to allow an allocation to be saved.

This is the result of running `gofix -r hashsum` over the tree, changing
the hash function implementations by hand and then fixing a couple of
instances where gofix didn't catch something.

The changed implementations are as simple as possible while still
working: I'm not trying to optimise in this CL.

R=rsc, cw, rogpeppe
CC=golang-dev
https://golang.org/cl/5448065
This commit is contained in:
Adam Langley 2011-12-01 12:35:37 -05:00
parent 37bb6649e2
commit c08ab14bad
5 changed files with 22 additions and 22 deletions

View File

@ -127,13 +127,13 @@ func (s ssl30MAC) MAC(seq, record []byte) []byte {
s.h.Write(record[:1]) s.h.Write(record[:1])
s.h.Write(record[3:5]) s.h.Write(record[3:5])
s.h.Write(record[recordHeaderLen:]) s.h.Write(record[recordHeaderLen:])
digest := s.h.Sum() digest := s.h.Sum(nil)
s.h.Reset() s.h.Reset()
s.h.Write(s.key) s.h.Write(s.key)
s.h.Write(ssl30Pad2[:padLength]) s.h.Write(ssl30Pad2[:padLength])
s.h.Write(digest) s.h.Write(digest)
return s.h.Sum() return s.h.Sum(nil)
} }
// tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
@ -149,7 +149,7 @@ func (s tls10MAC) MAC(seq, record []byte) []byte {
s.h.Reset() s.h.Reset()
s.h.Write(seq) s.h.Write(seq)
s.h.Write(record) s.h.Write(record)
return s.h.Sum() return s.h.Sum(nil)
} }
func rsaKA() keyAgreement { func rsaKA() keyAgreement {

View File

@ -232,8 +232,8 @@ func (c *Conn) clientHandshake() error {
if cert != nil { if cert != nil {
certVerify := new(certificateVerifyMsg) certVerify := new(certificateVerifyMsg)
var digest [36]byte var digest [36]byte
copy(digest[0:16], finishedHash.serverMD5.Sum()) copy(digest[0:16], finishedHash.serverMD5.Sum(nil))
copy(digest[16:36], finishedHash.serverSHA1.Sum()) copy(digest[16:36], finishedHash.serverSHA1.Sum(nil))
signed, err := rsa.SignPKCS1v15(c.config.rand(), c.config.Certificates[0].PrivateKey, crypto.MD5SHA1, digest[0:]) signed, err := rsa.SignPKCS1v15(c.config.rand(), c.config.Certificates[0].PrivateKey, crypto.MD5SHA1, digest[0:])
if err != nil { if err != nil {
return c.sendAlert(alertInternalError) return c.sendAlert(alertInternalError)

View File

@ -235,8 +235,8 @@ FindCipherSuite:
} }
digest := make([]byte, 36) digest := make([]byte, 36)
copy(digest[0:16], finishedHash.serverMD5.Sum()) copy(digest[0:16], finishedHash.serverMD5.Sum(nil))
copy(digest[16:36], finishedHash.serverSHA1.Sum()) copy(digest[16:36], finishedHash.serverSHA1.Sum(nil))
err = rsa.VerifyPKCS1v15(pub, crypto.MD5SHA1, digest, certVerify.signature) err = rsa.VerifyPKCS1v15(pub, crypto.MD5SHA1, digest, certVerify.signature)
if err != nil { if err != nil {
c.sendAlert(alertBadCertificate) c.sendAlert(alertBadCertificate)

View File

@ -90,13 +90,13 @@ func md5SHA1Hash(slices ...[]byte) []byte {
for _, slice := range slices { for _, slice := range slices {
hmd5.Write(slice) hmd5.Write(slice)
} }
copy(md5sha1, hmd5.Sum()) copy(md5sha1, hmd5.Sum(nil))
hsha1 := sha1.New() hsha1 := sha1.New()
for _, slice := range slices { for _, slice := range slices {
hsha1.Write(slice) hsha1.Write(slice)
} }
copy(md5sha1[md5.Size:], hsha1.Sum()) copy(md5sha1[md5.Size:], hsha1.Sum(nil))
return md5sha1 return md5sha1
} }

26
prf.go
View File

@ -22,14 +22,14 @@ func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {
func pHash(result, secret, seed []byte, hash func() hash.Hash) { func pHash(result, secret, seed []byte, hash func() hash.Hash) {
h := hmac.New(hash, secret) h := hmac.New(hash, secret)
h.Write(seed) h.Write(seed)
a := h.Sum() a := h.Sum(nil)
j := 0 j := 0
for j < len(result) { for j < len(result) {
h.Reset() h.Reset()
h.Write(a) h.Write(a)
h.Write(seed) h.Write(seed)
b := h.Sum() b := h.Sum(nil)
todo := len(b) todo := len(b)
if j+todo > len(result) { if j+todo > len(result) {
todo = len(result) - j todo = len(result) - j
@ -39,7 +39,7 @@ func pHash(result, secret, seed []byte, hash func() hash.Hash) {
h.Reset() h.Reset()
h.Write(a) h.Write(a)
a = h.Sum() a = h.Sum(nil)
} }
} }
@ -84,13 +84,13 @@ func pRF30(result, secret, label, seed []byte) {
hashSHA1.Write(b[:i+1]) hashSHA1.Write(b[:i+1])
hashSHA1.Write(secret) hashSHA1.Write(secret)
hashSHA1.Write(seed) hashSHA1.Write(seed)
digest := hashSHA1.Sum() digest := hashSHA1.Sum(nil)
hashMD5.Reset() hashMD5.Reset()
hashMD5.Write(secret) hashMD5.Write(secret)
hashMD5.Write(digest) hashMD5.Write(digest)
done += copy(result[done:], hashMD5.Sum()) done += copy(result[done:], hashMD5.Sum(nil))
i++ i++
} }
} }
@ -182,24 +182,24 @@ func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic [4]byte) []by
md5.Write(magic[:]) md5.Write(magic[:])
md5.Write(masterSecret) md5.Write(masterSecret)
md5.Write(ssl30Pad1[:]) md5.Write(ssl30Pad1[:])
md5Digest := md5.Sum() md5Digest := md5.Sum(nil)
md5.Reset() md5.Reset()
md5.Write(masterSecret) md5.Write(masterSecret)
md5.Write(ssl30Pad2[:]) md5.Write(ssl30Pad2[:])
md5.Write(md5Digest) md5.Write(md5Digest)
md5Digest = md5.Sum() md5Digest = md5.Sum(nil)
sha1.Write(magic[:]) sha1.Write(magic[:])
sha1.Write(masterSecret) sha1.Write(masterSecret)
sha1.Write(ssl30Pad1[:40]) sha1.Write(ssl30Pad1[:40])
sha1Digest := sha1.Sum() sha1Digest := sha1.Sum(nil)
sha1.Reset() sha1.Reset()
sha1.Write(masterSecret) sha1.Write(masterSecret)
sha1.Write(ssl30Pad2[:40]) sha1.Write(ssl30Pad2[:40])
sha1.Write(sha1Digest) sha1.Write(sha1Digest)
sha1Digest = sha1.Sum() sha1Digest = sha1.Sum(nil)
ret := make([]byte, len(md5Digest)+len(sha1Digest)) ret := make([]byte, len(md5Digest)+len(sha1Digest))
copy(ret, md5Digest) copy(ret, md5Digest)
@ -217,8 +217,8 @@ func (h finishedHash) clientSum(masterSecret []byte) []byte {
return finishedSum30(h.clientMD5, h.clientSHA1, masterSecret, ssl3ClientFinishedMagic) return finishedSum30(h.clientMD5, h.clientSHA1, masterSecret, ssl3ClientFinishedMagic)
} }
md5 := h.clientMD5.Sum() md5 := h.clientMD5.Sum(nil)
sha1 := h.clientSHA1.Sum() sha1 := h.clientSHA1.Sum(nil)
return finishedSum10(md5, sha1, clientFinishedLabel, masterSecret) return finishedSum10(md5, sha1, clientFinishedLabel, masterSecret)
} }
@ -229,7 +229,7 @@ func (h finishedHash) serverSum(masterSecret []byte) []byte {
return finishedSum30(h.serverMD5, h.serverSHA1, masterSecret, ssl3ServerFinishedMagic) return finishedSum30(h.serverMD5, h.serverSHA1, masterSecret, ssl3ServerFinishedMagic)
} }
md5 := h.serverMD5.Sum() md5 := h.serverMD5.Sum(nil)
sha1 := h.serverSHA1.Sum() sha1 := h.serverSHA1.Sum(nil)
return finishedSum10(md5, sha1, serverFinishedLabel, masterSecret) return finishedSum10(md5, sha1, serverFinishedLabel, masterSecret)
} }