-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecdsa.go
214 lines (192 loc) · 6.27 KB
/
ecdsa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/json"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)
// An EcdsaPrivateKey is used for signing data and representing verifiable
// identities.
type EcdsaPrivateKey struct {
*ecdsa.PrivateKey
}
// NewEcdsaPrivateKey returns an EcdsaPrivateKey from an existing private key.
// It does not verify that the private key was generated correctly.
func NewEcdsaPrivateKey(privateKey *ecdsa.PrivateKey) EcdsaPrivateKey {
return EcdsaPrivateKey{
PrivateKey: privateKey,
}
}
// RandomEcdsaPrivateKey using a secp256k1 s256 curve.
func RandomEcdsaPrivateKey() (EcdsaPrivateKey, error) {
privateKey, err := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if err != nil {
return EcdsaPrivateKey{}, err
}
return EcdsaPrivateKey{
PrivateKey: privateKey,
}, nil
}
// Sign implements the Signer interface. It uses the ecdsa.PrivateKey to sign
// the data without performing any kind of preprocessing of the data. If the
// data is not exactly 32 bytes, an error is returned.
func (key *EcdsaPrivateKey) Sign(data []byte) ([]byte, error) {
return crypto.Sign(data, key.PrivateKey)
}
// Verify implements the Verifier interface. It uses its own address as the
// expected signatory.
func (key *EcdsaPrivateKey) Verify(data []byte, signature []byte) error {
return NewEcdsaVerifier(key.PublicKey).Verify(data, signature)
}
// Equal returns true if two EcdsaPrivateKeys are exactly equal. The name of
// the elliptic.Curve is not checked.
func (key *EcdsaPrivateKey) Equal(rhs *EcdsaPrivateKey) bool {
return key.D.Cmp(rhs.D) == 0 &&
key.X.Cmp(rhs.X) == 0 &&
key.Y.Cmp(rhs.Y) == 0 &&
key.Curve.Params().P.Cmp(rhs.Curve.Params().P) == 0 &&
key.Curve.Params().N.Cmp(rhs.Curve.Params().N) == 0 &&
key.Curve.Params().B.Cmp(rhs.Curve.Params().B) == 0 &&
key.Curve.Params().Gx.Cmp(rhs.Curve.Params().Gx) == 0 &&
key.Curve.Params().Gy.Cmp(rhs.Curve.Params().Gy) == 0 &&
key.Curve.Params().BitSize == rhs.Curve.Params().BitSize
}
// MarshalJSON implements the json.Marshaler interface. The EcdsaPrivateKey is
// formatted according to the Republic Protocol Keystore specification.
func (key EcdsaPrivateKey) MarshalJSON() ([]byte, error) {
jsonKey := map[string]interface{}{}
// Private key
jsonKey["d"] = key.D.Bytes()
// Public key
jsonKey["x"] = key.X.Bytes()
jsonKey["y"] = key.Y.Bytes()
// Curve
jsonKey["curveParams"] = map[string]interface{}{
"p": secp256k1.S256().P.Bytes(), // the order of the underlying field
"n": secp256k1.S256().N.Bytes(), // the order of the base point
"b": secp256k1.S256().B.Bytes(), // the constant of the curve equation
"x": secp256k1.S256().Gx.Bytes(), // (x,y) of the base point
"y": secp256k1.S256().Gy.Bytes(),
"bits": secp256k1.S256().BitSize, // the size of the underlying field
"name": "s256", // the canonical name of the curve
}
return json.Marshal(jsonKey)
}
// UnmarshalJSON implements the json.Unmarshaler interface. An EcdsaPrivateKey
// is created from data that is assumed to be compliant with the Republic
// Protocol standard. The use of secp256k1 s256 curve is not checked.
func (key *EcdsaPrivateKey) UnmarshalJSON(data []byte) error {
jsonKey := map[string]json.RawMessage{}
if err := json.Unmarshal(data, &jsonKey); err != nil {
return err
}
var err error
// Private key
key.PrivateKey = new(ecdsa.PrivateKey)
key.PrivateKey.D, err = unmarshalBigIntFromMap(jsonKey, "d")
if err != nil {
return err
}
// Public key
key.PrivateKey.PublicKey = ecdsa.PublicKey{}
key.PrivateKey.PublicKey.X, err = unmarshalBigIntFromMap(jsonKey, "x")
if err != nil {
return err
}
key.PrivateKey.PublicKey.Y, err = unmarshalBigIntFromMap(jsonKey, "y")
if err != nil {
return err
}
// Curve
if jsonVal, ok := jsonKey["curveParams"]; ok {
curveParams := elliptic.CurveParams{}
jsonCurveParams := map[string]json.RawMessage{}
if err := json.Unmarshal(jsonVal, &jsonCurveParams); err != nil {
return err
}
curveParams.P, err = unmarshalBigIntFromMap(jsonCurveParams, "p")
if err != nil {
return err
}
curveParams.N, err = unmarshalBigIntFromMap(jsonCurveParams, "n")
if err != nil {
return err
}
curveParams.B, err = unmarshalBigIntFromMap(jsonCurveParams, "b")
if err != nil {
return err
}
curveParams.Gx, err = unmarshalBigIntFromMap(jsonCurveParams, "x")
if err != nil {
return err
}
curveParams.Gy, err = unmarshalBigIntFromMap(jsonCurveParams, "y")
if err != nil {
return err
}
curveParams.BitSize, err = unmarshalIntFromMap(jsonCurveParams, "bits")
if err != nil {
return err
}
curveParams.Name, err = unmarshalStringFromMap(jsonCurveParams, "name")
if err != nil {
return err
}
key.PrivateKey.Curve = &curveParams
} else {
return fmt.Errorf("curveParams is nil")
}
return nil
}
// EcdsaVerifier is used to verify signatures produced by an EcdsaPrivateKey.
type EcdsaVerifier struct {
ecdsa.PublicKey
}
// NewEcdsaVerifier returns an EcdsaVerifier that expects the signatory of all
// signatures that it checks to equal the given address.
func NewEcdsaVerifier(publicKey ecdsa.PublicKey) EcdsaVerifier {
return EcdsaVerifier{
PublicKey: publicKey,
}
}
// Verify implements the Verifier interface.
func (verifier EcdsaVerifier) Verify(data []byte, signature []byte) error {
if data == nil || len(data) == 0 {
return ErrNilData
}
if signature == nil || len(signature) == 0 {
return ErrNilSignature
}
publicKey, err := RecoverPublicKey(data, signature)
if err != nil {
return err
}
if verifier.PublicKey.Curve != publicKey.Curve {
return ErrMalformedSignature
}
if verifier.PublicKey.X.Cmp(publicKey.X) != 0 {
return ErrMalformedSignature
}
if verifier.PublicKey.Y.Cmp(publicKey.Y) != 0 {
return ErrMalformedSignature
}
return nil
}
// RecoverPublicKey used to produce a signature.
func RecoverPublicKey(data []byte, signature []byte) (ecdsa.PublicKey, error) {
// Returns 65-byte uncompress pubkey (0x04 | X | Y)
publicKey, err := crypto.Ecrecover(data, signature)
if err != nil {
return ecdsa.PublicKey{}, err
}
// Rebuild the public key
return ecdsa.PublicKey{
Curve: secp256k1.S256(),
X: big.NewInt(0).SetBytes(publicKey[1:33]),
Y: big.NewInt(0).SetBytes(publicKey[33:65]),
}, nil
}