From 717040c9453370196efb0a3706c83f17456cc374 Mon Sep 17 00:00:00 2001 From: David Hill Date: Wed, 24 Aug 2016 12:18:48 -0400 Subject: [PATCH] Replace fastsha256 with crypto/sha256 (#319) --- chaincfg/chainhash/hashfuncs.go | 2 +- dcrec/edwards/ecdsa.go | 4 ++-- dcrec/secp256k1/schnorr/threshold_test.go | 4 ++-- dcrec/secp256k1/signature.go | 5 ++--- dcrec/secp256k1/signature_test.go | 5 ++--- glide.lock | 18 ++++++++---------- glide.yaml | 1 - rpcserver.go | 12 ++++++------ rpcwebsocket.go | 4 ++-- 9 files changed, 25 insertions(+), 30 deletions(-) diff --git a/chaincfg/chainhash/hashfuncs.go b/chaincfg/chainhash/hashfuncs.go index 03e6f0a4..d0f0dd45 100644 --- a/chaincfg/chainhash/hashfuncs.go +++ b/chaincfg/chainhash/hashfuncs.go @@ -11,7 +11,7 @@ import ( // HashFunc calculates the hash of the supplied bytes. // TODO(jcv) Should modify blake256 so it has the same interface as blake2 -// and fastsha256 so these function can look more like btcsuite. Then should +// and sha256 so these function can look more like btcsuite. Then should // try to get it to the upstream blake256 repo func HashFunc(data []byte) [blake256.Size]byte { var outB [blake256.Size]byte diff --git a/dcrec/edwards/ecdsa.go b/dcrec/edwards/ecdsa.go index c9f7aac0..77bc91e8 100644 --- a/dcrec/edwards/ecdsa.go +++ b/dcrec/edwards/ecdsa.go @@ -7,6 +7,7 @@ package edwards import ( "bytes" "crypto/hmac" + "crypto/sha256" "fmt" "hash" "io" @@ -14,7 +15,6 @@ import ( "crypto/sha512" - "github.com/btcsuite/fastsha256" "github.com/decred/ed25519" "github.com/decred/ed25519/edwards25519" ) @@ -102,7 +102,7 @@ func NonceRFC6979(curve *TwistedEdwardsCurve, privkey *big.Int, hash []byte, extra []byte, version []byte) *big.Int { q := curve.Params().N x := privkey - alg := fastsha256.New + alg := sha256.New qlen := q.BitLen() holen := alg().Size() diff --git a/dcrec/secp256k1/schnorr/threshold_test.go b/dcrec/secp256k1/schnorr/threshold_test.go index 850a06be..54c99d74 100644 --- a/dcrec/secp256k1/schnorr/threshold_test.go +++ b/dcrec/secp256k1/schnorr/threshold_test.go @@ -6,11 +6,11 @@ package schnorr import ( "bytes" + "crypto/sha256" "encoding/hex" "math/rand" "testing" - "github.com/btcsuite/fastsha256" "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/dcrec/secp256k1" "github.com/stretchr/testify/assert" @@ -26,7 +26,7 @@ type signerHex struct { // Sha256. The internal tests from secp256k1 are kind of screwy and for // partial signatures call this hash function instead of testSchnorrHash. func testSchnorrSha256Hash(msg []byte) []byte { - sha := fastsha256.Sum256(msg) + sha := sha256.Sum256(msg) return sha[:] } diff --git a/dcrec/secp256k1/signature.go b/dcrec/secp256k1/signature.go index ed2bac26..0deaae5a 100644 --- a/dcrec/secp256k1/signature.go +++ b/dcrec/secp256k1/signature.go @@ -10,12 +10,11 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/hmac" + "crypto/sha256" "errors" "fmt" "hash" "math/big" - - "github.com/btcsuite/fastsha256" ) // Errors returned by canonicalPadding. @@ -455,7 +454,7 @@ func NonceRFC6979(privkey *big.Int, hash []byte, extra []byte, curve := S256() q := curve.Params().N x := privkey - alg := fastsha256.New + alg := sha256.New qlen := q.BitLen() holen := alg().Size() diff --git a/dcrec/secp256k1/signature_test.go b/dcrec/secp256k1/signature_test.go index c28ea7ce..7aea899b 100644 --- a/dcrec/secp256k1/signature_test.go +++ b/dcrec/secp256k1/signature_test.go @@ -8,14 +8,13 @@ package secp256k1_test import ( "bytes" "crypto/rand" + "crypto/sha256" "encoding/hex" "fmt" "math/big" "testing" "github.com/decred/dcrd/dcrec/secp256k1" - - "github.com/btcsuite/fastsha256" ) type signatureTest struct { @@ -561,7 +560,7 @@ func TestRFC6979(t *testing.T) { for i, test := range tests { privKey, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), decodeHex(test.key)) - hash := fastsha256.Sum256([]byte(test.msg)) + hash := sha256.Sum256([]byte(test.msg)) // Ensure deterministically generated nonce is the expected value. gotNonce := secp256k1.TstNonceRFC6979(privKey.D, hash[:]).Bytes() diff --git a/glide.lock b/glide.lock index a6959674..a61be67b 100644 --- a/glide.lock +++ b/glide.lock @@ -1,10 +1,8 @@ -hash: 4648b5e89ce577ac30492bc77edc33a1176f4cbbb021495b1cb522c2588a5e90 -updated: 2016-08-24T09:44:38.019002376-05:00 +hash: 26b232a0701585082422dc9f718ad82932d29309789896663ce3c4a5925cb84b +updated: 2016-08-24T12:05:43.171730574-04:00 imports: - name: github.com/btcsuite/btclog version: 73889fb79bd687870312b6e40effcecffbd57d30 -- name: github.com/btcsuite/fastsha256 - version: 637e656429416087660c84436a2a035d69d54e2e - name: github.com/btcsuite/go-flags version: 6c288d648c1cc1befcb90cb5511dcacf64ae8e61 - name: github.com/btcsuite/go-socks @@ -19,17 +17,17 @@ imports: version: 7834afc9e8cd15233b6c3d97e12674a31ca24602 subpackages: - leveldb + - leveldb/cache - leveldb/comparer - leveldb/errors - leveldb/filter - leveldb/iterator - - leveldb/opt - - leveldb/util - - leveldb/cache - leveldb/journal - leveldb/memdb + - leveldb/opt - leveldb/storage - leveldb/table + - leveldb/util - name: github.com/btcsuite/seelog version: ae8891d029dd3c269dcfd6f261ad23e761acd99f - name: github.com/btcsuite/snappy-go @@ -41,8 +39,8 @@ imports: subpackages: - eventlog - mgr - - svc - registry + - svc - winapi - name: github.com/davecgh/go-spew version: 6cf5744a041a0022271cefed95ba843f6d87fd51 @@ -57,14 +55,14 @@ imports: - name: github.com/decred/dcrutil version: 4fc91a08eea88e74539d42d6301fd298b9bd8230 subpackages: - - bloom - base58 + - bloom - name: github.com/decred/ed25519 version: b0909d3f798b97a03c9e77023f97a5301a2a7900 subpackages: - edwards25519 - name: golang.org/x/crypto - version: b35ccbc95a0eaae49fb65c5d627cb7149ed8d1ab + version: 986d3313588aa5c68f1df95eac956f79cf3b2c01 subpackages: - ssh/terminal testImports: diff --git a/glide.yaml b/glide.yaml index 32efb38d..2781ec82 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,7 +1,6 @@ package: github.com/decred/dcrd import: - package: github.com/btcsuite/btclog -- package: github.com/btcsuite/fastsha256 - package: github.com/btcsuite/go-flags - package: github.com/btcsuite/go-socks subpackages: diff --git a/rpcserver.go b/rpcserver.go index 3b482c54..f7abef37 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -7,6 +7,7 @@ package main import ( "bytes" + "crypto/sha256" "crypto/subtle" "crypto/tls" "encoding/base64" @@ -30,7 +31,6 @@ import ( "sync/atomic" "time" - "github.com/btcsuite/fastsha256" "github.com/btcsuite/websocket" "github.com/decred/bitset" @@ -5723,8 +5723,8 @@ type rpcServer struct { policy *mining.Policy server *server chain *blockchain.BlockChain - authsha [fastsha256.Size]byte - limitauthsha [fastsha256.Size]byte + authsha [sha256.Size]byte + limitauthsha [sha256.Size]byte ntfnMgr *wsNotificationManager numClients int32 statusLines map[int]string @@ -5881,7 +5881,7 @@ func (s *rpcServer) checkAuth(r *http.Request, require bool) (bool, bool, error) return false, false, nil } - authsha := fastsha256.Sum256([]byte(authhdr[0])) + authsha := sha256.Sum256([]byte(authhdr[0])) // Check for limited auth first as in environments with limited users, those // are probably expected to have a higher volume of calls @@ -6218,12 +6218,12 @@ func newRPCServer(listenAddrs []string, policy *mining.Policy, s *server) (*rpcS if cfg.RPCUser != "" && cfg.RPCPass != "" { login := cfg.RPCUser + ":" + cfg.RPCPass auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login)) - rpc.authsha = fastsha256.Sum256([]byte(auth)) + rpc.authsha = sha256.Sum256([]byte(auth)) } if cfg.RPCLimitUser != "" && cfg.RPCLimitPass != "" { login := cfg.RPCLimitUser + ":" + cfg.RPCLimitPass auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login)) - rpc.limitauthsha = fastsha256.Sum256([]byte(auth)) + rpc.limitauthsha = sha256.Sum256([]byte(auth)) } rpc.ntfnMgr = newWsNotificationManager(&rpc) diff --git a/rpcwebsocket.go b/rpcwebsocket.go index 71c38da7..67bf5da7 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -8,6 +8,7 @@ package main import ( "bytes" "container/list" + "crypto/sha256" "crypto/subtle" "encoding/base64" "encoding/hex" @@ -20,7 +21,6 @@ import ( "sync" "time" - "github.com/btcsuite/fastsha256" "github.com/btcsuite/golangcrypto/ripemd160" "github.com/btcsuite/websocket" @@ -1325,7 +1325,7 @@ func (c *wsClient) handleMessage(msg []byte) { // Check credentials. login := authCmd.Username + ":" + authCmd.Passphrase auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login)) - authSha := fastsha256.Sum256([]byte(auth)) + authSha := sha256.Sum256([]byte(auth)) cmp := subtle.ConstantTimeCompare(authSha[:], c.server.authsha[:]) limitcmp := subtle.ConstantTimeCompare(authSha[:], c.server.limitauthsha[:]) if cmp != 1 && limitcmp != 1 {