Skip to content

Commit 97cdf37

Browse files
committed
switch to golang.org/x/crypto/ed25519
Fixes NebulousLabs#1544
1 parent a5ceabd commit 97cdf37

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ all: install
77
dependencies:
88
# Consensus Dependencies
99
go get -u github.com/NebulousLabs/demotemutex
10-
go get -u github.com/NebulousLabs/ed25519
1110
go get -u github.com/NebulousLabs/fastrand
1211
go get -u github.com/NebulousLabs/merkletree
1312
go get -u github.com/NebulousLabs/bolt
1413
go get -u golang.org/x/crypto/blake2b
14+
go get -u golang.org/x/crypto/ed25519
1515
# Module + Daemon Dependencies
1616
go get -u github.com/NebulousLabs/entropy-mnemonics
1717
go get -u github.com/NebulousLabs/go-upnp

crypto/signatures.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package crypto
22

33
import (
4+
"bytes"
45
"errors"
56
"io"
67

78
"github.com/NebulousLabs/Sia/encoding"
8-
"github.com/NebulousLabs/ed25519"
99
"github.com/NebulousLabs/fastrand"
10+
11+
"golang.org/x/crypto/ed25519"
1012
)
1113

1214
const (
1315
// EntropySize defines the amount of entropy necessary to do secure
1416
// cryptographic operations, in bytes.
15-
EntropySize = ed25519.EntropySize
17+
EntropySize = 32
1618

1719
// PublicKeySize defines the size of public keys in bytes.
1820
PublicKeySize = ed25519.PublicKeySize
1921

2022
// SecretKeySize defines the size of secret keys in bytes.
21-
SecretKeySize = ed25519.SecretKeySize
23+
SecretKeySize = ed25519.PrivateKeySize
2224

2325
// SignatureSize defines the size of signatures in bytes.
2426
SignatureSize = ed25519.SignatureSize
@@ -51,16 +53,21 @@ func (sk SecretKey) PublicKey() (pk PublicKey) {
5153
// GenerateKeyPair creates a public-secret keypair that can be used to sign and verify
5254
// messages.
5355
func GenerateKeyPair() (sk SecretKey, pk PublicKey) {
54-
var entropy [EntropySize]byte
55-
fastrand.Read(entropy[:])
56-
return GenerateKeyPairDeterministic(entropy)
56+
// no error possible when using fastrand.Reader
57+
epk, esk, _ := ed25519.GenerateKey(fastrand.Reader)
58+
copy(sk[:], esk)
59+
copy(pk[:], epk)
60+
return
5761
}
5862

5963
// GenerateKeyPairDeterministic generates keys deterministically using the input
6064
// entropy. The input entropy must be 32 bytes in length.
61-
func GenerateKeyPairDeterministic(entropy [EntropySize]byte) (SecretKey, PublicKey) {
62-
sk, pk := ed25519.GenerateKey(entropy)
63-
return *sk, *pk
65+
func GenerateKeyPairDeterministic(entropy [EntropySize]byte) (sk SecretKey, pk PublicKey) {
66+
// no error possible when using bytes.Reader
67+
epk, esk, _ := ed25519.GenerateKey(bytes.NewReader(entropy[:]))
68+
copy(sk[:], esk)
69+
copy(pk[:], epk)
70+
return
6471
}
6572

6673
// ReadSignedObject reads a length-prefixed object prefixed by its signature,
@@ -86,16 +93,14 @@ func ReadSignedObject(r io.Reader, obj interface{}, maxLen uint64, pk PublicKey)
8693
}
8794

8895
// SignHash signs a message using a secret key.
89-
func SignHash(data Hash, sk SecretKey) Signature {
90-
skNorm := [SecretKeySize]byte(sk)
91-
return *ed25519.Sign(&skNorm, data[:])
96+
func SignHash(data Hash, sk SecretKey) (sig Signature) {
97+
copy(sig[:], ed25519.Sign(sk[:], data[:]))
98+
return
9299
}
93100

94101
// VerifyHash uses a public key and input data to verify a signature.
95102
func VerifyHash(data Hash, pk PublicKey, sig Signature) error {
96-
pkNorm := [PublicKeySize]byte(pk)
97-
sigNorm := [SignatureSize]byte(sig)
98-
verifies := ed25519.Verify(&pkNorm, data[:], &sigNorm)
103+
verifies := ed25519.Verify(pk[:], data[:], sig[:])
99104
if !verifies {
100105
return ErrInvalidSignature
101106
}

0 commit comments

Comments
 (0)