-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsignature_cgo.go
282 lines (263 loc) · 7.81 KB
/
signature_cgo.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build !nacl,!js,cgo
package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"errors"
"fmt"
"github.com/taiyuechain/taiyuechain/common/math"
"github.com/taiyuechain/taiyuechain/crypto/gm/sm2"
"github.com/taiyuechain/taiyuechain/crypto/p256"
"github.com/taiyuechain/taiyuechain/crypto/secp256k1"
"github.com/taiyuechain/taiyuechain/log"
"math/big"
)
// Ecrecover returns the uncompressed public key that created the given signature.
/*func Ecrecover(hash, sig []byte) ([]byte, error) {
return secp256k1.RecoverPubkey(hash, sig)
}*/
func Ecrecover(hash, sig []byte) ([]byte, error) {
if len(sig) != SignatureLength {
log.Info("sig length", "is", len(sig))
return nil, errors.New("sig length is err")
}
if CryptoType == CRYPTO_P256_SH3_AES {
//p256pub,err:=p256.ECRecovery(hash, sig)nil
p256pub, err := DecompressPubkey(sig[65:])
if err != nil {
return nil, err
}
return FromECDSAPub(p256pub), nil
}
//guomi
if CryptoType == CRYPTO_SM2_SM3_SM4 {
ee := new(big.Int).SetBytes(sig[65:])
smpub, err := sm2.SigToPub(hash, sig[:65], nil, ee)
if err != nil {
return nil, err
}
return FromECDSAPub(smpub), nil
}
//guoji S256
if CryptoType == CRYPTO_S256_SH3_AES {
return secp256k1.RecoverPubkey(hash, sig[:65])
}
return nil, nil
}
// SigToPub returns the public key that created the given signature.
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
if len(sig) != SignatureLength || len(hash) != 32 {
return nil, errors.New("SigToPub sign length is wrong ")
}
if CryptoType == CRYPTO_P256_SH3_AES {
//p256pub,err:=p256.ECRecovery(hash, sig)nil
p256pub, err := DecompressPubkey(sig[65:])
if err != nil {
return nil, err
}
return p256pub, nil
}
//guomi
if CryptoType == CRYPTO_SM2_SM3_SM4 {
// smpub, err := DecompressPubkey(sig[65:])
ee := new(big.Int).SetBytes(sig[65:])
smpub, err := sm2.SigToPub(hash, sig[:65], nil, ee)
if err != nil {
return nil, err
}
return smpub, nil
}
//guoji S256
if CryptoType == CRYPTO_S256_SH3_AES {
s, err := Ecrecover(hash, sig)
if err != nil {
return nil, err
}
x, y := elliptic.Unmarshal(S256(), s)
return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil
}
return nil, nil
}
// Sign calculates an ECDSA signature.
//
// This function is susceptible to chosen plaintext attacks that can leak
// information about the private key that is used for signing. Callers must
// be aware that the given digest cannot be chosen by an adversery. Common
// solution is to hash any input before calculating the signature.
//
// The produced signature is in the [R || S || V] format where V is 0 or 1.
func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
if len(digestHash) != 32 {
return nil, errors.New("sign digestHash is wrong")
}
if prv == nil {
return nil, errors.New("invalid private key")
}
if CryptoType == CRYPTO_P256_SH3_AES {
p256sign, err := p256.Sign(prv, digestHash)
if err != nil {
return nil, err
}
if len(p256sign) != 65 {
return nil, errors.New("sig length is wrong !!!" + string(len(p256sign)))
}
pubtype := CompressPubkey(&prv.PublicKey)
p256sign = append(p256sign, pubtype...)
return p256sign, nil
}
//guomi
if CryptoType == CRYPTO_SM2_SM3_SM4 {
smsign, e, err := sm2.Sign(sm2.ToSm2privatekey(prv), nil, digestHash)
if err != nil {
return nil, err
}
if len(smsign) != 65 {
log.Warn("Sign", "digestHash", hex.EncodeToString(digestHash), "priv", hex.EncodeToString(FromECDSA(prv)), " smsign", len(smsign))
return nil, errors.New("sig length is wrong !!! " + string(len(smsign)))
}
var pad [32]byte
buf := e.Bytes()
copy(pad[32-len(buf):], buf)
smsign = append(smsign, pad[:]...)
return smsign, nil
}
//guoji S256
if CryptoType == CRYPTO_S256_SH3_AES {
if len(digestHash) != DigestLength {
return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash))
}
seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
defer zeroBytes(seckey)
smsign, err := secp256k1.Sign(digestHash, seckey)
if err != nil {
return nil, err
}
var pad [32]byte
smsign = append(smsign, pad[:]...)
return smsign, nil
}
return nil, nil
}
// VerifySignature checks that the given public key created signature over digest.
// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
// The signature should have the 64 byte [R || S] format.
func VerifySignature(pubkey, digestHash, signature []byte) bool {
if len(signature) != SignatureLength || len(digestHash) != 32 {
return false
}
if CryptoType == CRYPTO_P256_SH3_AES {
if len(pubkey) == 33 {
p256pub, err := DecompressPubkey(pubkey)
if err != nil {
return false
}
return p256.Verify(p256pub, digestHash, signature)
}
p256pub, err := UnmarshalPubkey(pubkey)
if err != nil {
return false
}
return p256.Verify(p256pub, digestHash, signature)
}
//guomi
if CryptoType == CRYPTO_SM2_SM3_SM4 {
if len(pubkey) == 33 {
smpub, err := DecompressPubkey(pubkey)
if err != nil {
return false
}
return sm2.Verify(sm2.ToSm2Publickey(smpub), nil, digestHash, signature)
}
smpub, err := UnmarshalPubkey(pubkey)
if err != nil {
return false
}
return sm2.Verify(sm2.ToSm2Publickey(smpub), nil, digestHash, signature)
}
//guoji S256
if CryptoType == CRYPTO_S256_SH3_AES {
return secp256k1.VerifySignature(pubkey, digestHash, signature[:64])
}
return false
}
func getPubFromBytes(pk []byte) (*ecdsa.PublicKey, error) {
if len(pk) == 33 {
smpub, err := DecompressPubkey(pk)
if err != nil {
return nil, err
}
return smpub, nil
}
if len(pk) != 65 {
return nil, errors.New("len not equal 65")
}
p256pub, err := UnmarshalPubkey(pk)
if err != nil {
return nil, err
}
return p256pub, nil
}
// DecompressPubkey parses a public key in the 33-byte compressed format.
func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
if len(pubkey) != 33 {
return nil, errors.New("DecompressPubkey length is wrong !" + string(len(pubkey)))
}
if CryptoType == CRYPTO_P256_SH3_AES {
p256pub, err := p256.DecompressPubkey(pubkey)
if err != nil {
return nil, err
}
return p256pub, nil
}
//guomi
if CryptoType == CRYPTO_SM2_SM3_SM4 {
return sm2.ToECDSAPublickey(sm2.Decompress(pubkey)), nil
}
//guoji S256
if CryptoType == CRYPTO_S256_SH3_AES {
x, y := secp256k1.DecompressPubkey(pubkey)
if x == nil {
return nil, fmt.Errorf("invalid public key")
}
return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
}
return nil, nil
}
// CompressPubkey encodes a public key to the 33-byte compressed format.
func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
if pubkey == nil {
return nil
}
if CryptoType == CRYPTO_P256_SH3_AES {
return p256.CompressPubkey(pubkey)
}
//guomi
if CryptoType == CRYPTO_SM2_SM3_SM4 {
return sm2.Compress(sm2.ToSm2Publickey(pubkey))
}
//guoji S256
if CryptoType == CRYPTO_S256_SH3_AES {
return secp256k1.CompressPubkey(pubkey.X, pubkey.Y)
}
return nil
}
// S256 returns an instance of the secp256k1 curve.
func S256() elliptic.Curve {
return secp256k1.S256()
}