crypto/hmac: make Sum idempotent

Fixes #978.

R=rsc
CC=golang-dev
https://golang.org/cl/1967045
This commit is contained in:
Jukka-Pekka Kekkonen 2010-08-26 13:32:29 -04:00 committed by Russ Cox
parent c6f2f6c1ab
commit f610c8e06b
3 changed files with 7 additions and 9 deletions

View File

@ -8,7 +8,6 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/rc4" "crypto/rc4"
"crypto/rsa" "crypto/rsa"
"crypto/sha1"
"crypto/subtle" "crypto/subtle"
"crypto/x509" "crypto/x509"
"io" "io"
@ -226,7 +225,7 @@ func (c *Conn) clientHandshake() os.Error {
cipher, _ := rc4.NewCipher(clientKey) cipher, _ := rc4.NewCipher(clientKey)
c.out.prepareCipherSpec(cipher, hmac.New(sha1.New(), clientMAC)) c.out.prepareCipherSpec(cipher, hmac.NewSHA1(clientMAC))
c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
finished := new(finishedMsg) finished := new(finishedMsg)
@ -235,7 +234,7 @@ func (c *Conn) clientHandshake() os.Error {
c.writeRecord(recordTypeHandshake, finished.marshal()) c.writeRecord(recordTypeHandshake, finished.marshal())
cipher2, _ := rc4.NewCipher(serverKey) cipher2, _ := rc4.NewCipher(serverKey)
c.in.prepareCipherSpec(cipher2, hmac.New(sha1.New(), serverMAC)) c.in.prepareCipherSpec(cipher2, hmac.NewSHA1(serverMAC))
c.readRecord(recordTypeChangeCipherSpec) c.readRecord(recordTypeChangeCipherSpec)
if c.err != nil { if c.err != nil {
return c.err return c.err

View File

@ -16,7 +16,6 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/rc4" "crypto/rc4"
"crypto/rsa" "crypto/rsa"
"crypto/sha1"
"crypto/subtle" "crypto/subtle"
"crypto/x509" "crypto/x509"
"io" "io"
@ -227,7 +226,7 @@ func (c *Conn) serverHandshake() os.Error {
keysFromPreMasterSecret11(preMasterSecret, clientHello.random, hello.random, suite.hashLength, suite.cipherKeyLength) keysFromPreMasterSecret11(preMasterSecret, clientHello.random, hello.random, suite.hashLength, suite.cipherKeyLength)
cipher, _ := rc4.NewCipher(clientKey) cipher, _ := rc4.NewCipher(clientKey)
c.in.prepareCipherSpec(cipher, hmac.New(sha1.New(), clientMAC)) c.in.prepareCipherSpec(cipher, hmac.NewSHA1(clientMAC))
c.readRecord(recordTypeChangeCipherSpec) c.readRecord(recordTypeChangeCipherSpec)
if err := c.error(); err != nil { if err := c.error(); err != nil {
return err return err
@ -264,7 +263,7 @@ func (c *Conn) serverHandshake() os.Error {
finishedHash.Write(clientFinished.marshal()) finishedHash.Write(clientFinished.marshal())
cipher2, _ := rc4.NewCipher(serverKey) cipher2, _ := rc4.NewCipher(serverKey)
c.out.prepareCipherSpec(cipher2, hmac.New(sha1.New(), serverMAC)) c.out.prepareCipherSpec(cipher2, hmac.NewSHA1(serverMAC))
c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) c.writeRecord(recordTypeChangeCipherSpec, []byte{1})
finished := new(finishedMsg) finished := new(finishedMsg)

6
prf.go
View File

@ -20,7 +20,7 @@ func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {
} }
// pHash implements the P_hash function, as defined in RFC 4346, section 5. // pHash implements the P_hash function, as defined in RFC 4346, section 5.
func pHash(result, secret, seed []byte, hash 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()
@ -46,8 +46,8 @@ func pHash(result, secret, seed []byte, hash hash.Hash) {
// pRF11 implements the TLS 1.1 pseudo-random function, as defined in RFC 4346, section 5. // pRF11 implements the TLS 1.1 pseudo-random function, as defined in RFC 4346, section 5.
func pRF11(result, secret, label, seed []byte) { func pRF11(result, secret, label, seed []byte) {
hashSHA1 := sha1.New() hashSHA1 := sha1.New
hashMD5 := md5.New() hashMD5 := md5.New
labelAndSeed := make([]byte, len(label)+len(seed)) labelAndSeed := make([]byte, len(label)+len(seed))
copy(labelAndSeed, label) copy(labelAndSeed, label)