Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop fastsha256 usage in favor of crypto/sha256 #632

Merged
merged 1 commit into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions rpc/legacyrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package legacyrpc

import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
Expand All @@ -18,7 +19,6 @@ import (
"sync/atomic"
"time"

"github.com/btcsuite/fastsha256"
"github.com/btcsuite/websocket"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrwallet/chain"
Expand Down Expand Up @@ -67,7 +67,7 @@ type Server struct {
handlerMu sync.Mutex

listeners []net.Listener
authsha [fastsha256.Size]byte
authsha [sha256.Size]byte
upgrader websocket.Upgrader

maxPostClients int64 // Max concurrent HTTP POST clients.
Expand Down Expand Up @@ -108,7 +108,7 @@ func NewServer(opts *Options, walletLoader *loader.Loader, listeners []net.Liste
listeners: listeners,
// A hash of the HTTP basic auth string is used for a constant
// time comparison.
authsha: fastsha256.Sum256(httpBasicAuth(opts.Username, opts.Password)),
authsha: sha256.Sum256(httpBasicAuth(opts.Username, opts.Password)),
upgrader: websocket.Upgrader{
// Allow all origins.
CheckOrigin: func(r *http.Request) bool { return true },
Expand Down Expand Up @@ -305,7 +305,7 @@ func (s *Server) checkAuthHeader(r *http.Request) error {
return ErrNoAuth
}

authsha := fastsha256.Sum256([]byte(authhdr[0]))
authsha := sha256.Sum256([]byte(authhdr[0]))
cmp := subtle.ConstantTimeCompare(authsha[:], s.authsha[:])
if cmp != 1 {
return errors.New("bad auth")
Expand Down Expand Up @@ -382,7 +382,7 @@ func (s *Server) invalidAuth(req *dcrjson.Request) bool {
// Check credentials.
login := authCmd.Username + ":" + authCmd.Passphrase
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
authSha := fastsha256.Sum256([]byte(auth))
authSha := sha256.Sum256([]byte(auth))
return subtle.ConstantTimeCompare(authSha[:], s.authsha[:]) != 1
}

Expand Down
24 changes: 12 additions & 12 deletions snacl/snacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package snacl

import (
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/binary"
"errors"
Expand All @@ -14,7 +15,6 @@ import (

"github.com/decred/dcrwallet/internal/zero"

"github.com/btcsuite/fastsha256"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/scrypt"
)
Expand Down Expand Up @@ -97,7 +97,7 @@ func GenerateCryptoKey() (*CryptoKey, error) {
// Parameters are not secret and can be stored in plain text.
type Parameters struct {
Salt [KeySize]byte
Digest [fastsha256.Size]byte
Digest [sha256.Size]byte
N int
R int
P int
Expand Down Expand Up @@ -150,14 +150,14 @@ func (sk *SecretKey) Marshal() []byte {
// The marshalled format for the the params is as follows:
// <salt><digest><N><R><P>
//
// KeySize + fastsha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
marshalled := make([]byte, KeySize+fastsha256.Size+24)
// KeySize + sha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
marshalled := make([]byte, KeySize+sha256.Size+24)

b := marshalled
copy(b[:KeySize], params.Salt[:])
b = b[KeySize:]
copy(b[:fastsha256.Size], params.Digest[:])
b = b[fastsha256.Size:]
copy(b[:sha256.Size], params.Digest[:])
b = b[sha256.Size:]
binary.LittleEndian.PutUint64(b[:8], uint64(params.N))
b = b[8:]
binary.LittleEndian.PutUint64(b[:8], uint64(params.R))
Expand All @@ -177,16 +177,16 @@ func (sk *SecretKey) Unmarshal(marshalled []byte) error {
// The marshalled format for the the params is as follows:
// <salt><digest><N><R><P>
//
// KeySize + fastsha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
if len(marshalled) != KeySize+fastsha256.Size+24 {
// KeySize + sha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
if len(marshalled) != KeySize+sha256.Size+24 {
return ErrMalformed
}

params := &sk.Parameters
copy(params.Salt[:], marshalled[:KeySize])
marshalled = marshalled[KeySize:]
copy(params.Digest[:], marshalled[:fastsha256.Size])
marshalled = marshalled[fastsha256.Size:]
copy(params.Digest[:], marshalled[:sha256.Size])
marshalled = marshalled[sha256.Size:]
params.N = int(binary.LittleEndian.Uint64(marshalled[:8]))
marshalled = marshalled[8:]
params.R = int(binary.LittleEndian.Uint64(marshalled[:8]))
Expand All @@ -212,7 +212,7 @@ func (sk *SecretKey) DeriveKey(password *[]byte) error {
}

// verify password
digest := fastsha256.Sum256(sk.Key[:])
digest := sha256.Sum256(sk.Key[:])
if subtle.ConstantTimeCompare(digest[:], sk.Parameters.Digest[:]) != 1 {
return ErrInvalidPassword
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func NewSecretKey(password *[]byte, N, r, p int) (*SecretKey, error) {
}

// store digest
sk.Parameters.Digest = fastsha256.Sum256(sk.Key[:])
sk.Parameters.Digest = sha256.Sum256(sk.Key[:])

return &sk, nil
}