forked from 33cn/plugin
/
ecdsa.go
243 lines (198 loc) · 5.63 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ecdsa
import (
"bytes"
"errors"
"fmt"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/asn1"
"math/big"
"github.com/33cn/chain33/common/crypto"
)
const (
privateKeyECDSALength = 32
publicKeyECDSALength = 65
)
// Driver driver
type Driver struct{}
// GenKey create private key
func (d Driver) GenKey() (crypto.PrivKey, error) {
privKeyBytes := [privateKeyECDSALength]byte{}
copy(privKeyBytes[:], crypto.CRandBytes(privateKeyECDSALength))
priv, _ := privKeyFromBytes(elliptic.P256(), privKeyBytes[:])
copy(privKeyBytes[:], SerializePrivateKey(priv))
return PrivKeyECDSA(privKeyBytes), nil
}
// PrivKeyFromBytes create private key from bytes
func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) {
if len(b) != privateKeyECDSALength {
return nil, errors.New("invalid priv key byte")
}
privKeyBytes := new([privateKeyECDSALength]byte)
copy(privKeyBytes[:], b[:privateKeyECDSALength])
priv, _ := privKeyFromBytes(elliptic.P256(), privKeyBytes[:])
copy(privKeyBytes[:], SerializePrivateKey(priv))
return PrivKeyECDSA(*privKeyBytes), nil
}
// PubKeyFromBytes create public key from bytes
func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) {
if len(b) != publicKeyECDSALength {
return nil, errors.New("invalid pub key byte")
}
pubKeyBytes := new([publicKeyECDSALength]byte)
copy(pubKeyBytes[:], b[:])
return PubKeyECDSA(*pubKeyBytes), nil
}
// SignatureFromBytes create signature from bytes
func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) {
var certSignature crypto.CertSignature
_, err = asn1.Unmarshal(b, &certSignature)
if err != nil {
return SignatureECDSA(b), nil
}
if len(certSignature.Cert) == 0 {
return SignatureECDSA(b), nil
}
return SignatureECDSA(certSignature.Signature), nil
}
// PrivKeyECDSA PrivKey
type PrivKeyECDSA [privateKeyECDSALength]byte
// Bytes convert to bytes
func (privKey PrivKeyECDSA) Bytes() []byte {
s := make([]byte, privateKeyECDSALength)
copy(s, privKey[:])
return s
}
// Sign create signature
func (privKey PrivKeyECDSA) Sign(msg []byte) crypto.Signature {
priv, pub := privKeyFromBytes(elliptic.P256(), privKey[:])
r, s, err := ecdsa.Sign(rand.Reader, priv, crypto.Sha256(msg))
if err != nil {
return nil
}
s = ToLowS(pub, s)
ecdsaSigByte, _ := MarshalECDSASignature(r, s)
return SignatureECDSA(ecdsaSigByte)
}
// PubKey convert to public key
func (privKey PrivKeyECDSA) PubKey() crypto.PubKey {
_, pub := privKeyFromBytes(elliptic.P256(), privKey[:])
var pubECDSA PubKeyECDSA
copy(pubECDSA[:], SerializePublicKey(pub))
return pubECDSA
}
// Equals check privkey is equal
func (privKey PrivKeyECDSA) Equals(other crypto.PrivKey) bool {
if otherSecp, ok := other.(PrivKeyECDSA); ok {
return bytes.Equal(privKey[:], otherSecp[:])
}
return false
}
// String convert to string
func (privKey PrivKeyECDSA) String() string {
return fmt.Sprintf("PrivKeyECDSA{*****}")
}
// PubKeyECDSA PubKey
// prefixed with 0x02 or 0x03, depending on the y-cord.
type PubKeyECDSA [publicKeyECDSALength]byte
// Bytes convert to bytes
func (pubKey PubKeyECDSA) Bytes() []byte {
s := make([]byte, publicKeyECDSALength)
copy(s, pubKey[:])
return s
}
// VerifyBytes verify signature
func (pubKey PubKeyECDSA) VerifyBytes(msg []byte, sig crypto.Signature) bool {
// unwrap if needed
if wrap, ok := sig.(SignatureS); ok {
sig = wrap.Signature
}
// and assert same algorithm to sign and verify
sigECDSA, ok := sig.(SignatureECDSA)
if !ok {
return false
}
pub, err := parsePubKey(pubKey[:], elliptic.P256())
if err != nil {
return false
}
r, s, err := UnmarshalECDSASignature(sigECDSA)
if err != nil {
return false
}
lowS := IsLowS(s)
if !lowS {
return false
}
return ecdsa.Verify(pub, crypto.Sha256(msg), r, s)
}
// String convert to string
func (pubKey PubKeyECDSA) String() string {
return fmt.Sprintf("PubKeyECDSA{%X}", pubKey[:])
}
// KeyString Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeyECDSA) KeyString() string {
return fmt.Sprintf("%X", pubKey[:])
}
// Equals check public key is equal
func (pubKey PubKeyECDSA) Equals(other crypto.PubKey) bool {
if otherSecp, ok := other.(PubKeyECDSA); ok {
return bytes.Equal(pubKey[:], otherSecp[:])
}
return false
}
type signatureECDSA struct {
R, S *big.Int
}
// SignatureECDSA Signature
type SignatureECDSA []byte
// SignatureS signature struct
type SignatureS struct {
crypto.Signature
}
// Bytes convert signature to bytes
func (sig SignatureECDSA) Bytes() []byte {
s := make([]byte, len(sig))
copy(s, sig[:])
return s
}
// IsZero check signature is zero
func (sig SignatureECDSA) IsZero() bool { return len(sig) == 0 }
// String convert signature to string
func (sig SignatureECDSA) String() string {
fingerprint := make([]byte, len(sig[:]))
copy(fingerprint, sig[:])
return fmt.Sprintf("/%X.../", fingerprint)
}
// Equals check signature equals
func (sig SignatureECDSA) Equals(other crypto.Signature) bool {
if otherEd, ok := other.(SignatureECDSA); ok {
return bytes.Equal(sig[:], otherEd[:])
}
return false
}
// Name name
const Name = "auth_ecdsa"
// ID id
const ID = 257
func init() {
crypto.Register(Name, &Driver{})
crypto.RegisterType(Name, ID)
}
func privKeyFromBytes(curve elliptic.Curve, pk []byte) (*ecdsa.PrivateKey, *ecdsa.PublicKey) {
x, y := curve.ScalarBaseMult(pk)
priv := &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
},
D: new(big.Int).SetBytes(pk),
}
return priv, &priv.PublicKey
}