/
key.go
146 lines (130 loc) · 3.08 KB
/
key.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
// Copyright (c) 2018-2019 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package ec
import (
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"io"
"math/big"
"github.com/btcsuite/btcd/btcec"
)
const (
compress_even = 2
compress_odd = 3
nocompress = 4
)
type ECAlgorithm byte
const (
ECDSA ECAlgorithm = iota
SM2
)
type PrivateKey struct {
Algorithm ECAlgorithm
*ecdsa.PrivateKey
}
type PublicKey struct {
Algorithm ECAlgorithm
*ecdsa.PublicKey
}
/*
func (this *PrivateKey) Public() crypto.PublicKey {
return &PublicKey{Algorithm: this.Algorithm, PublicKey: &this.PublicKey}
}
*/
func GenerateECKeyPair(c elliptic.Curve, rand io.Reader, alg ECAlgorithm) (*PrivateKey, *PublicKey, error) {
d, x, y, err := elliptic.GenerateKey(c, rand)
if err != nil {
return nil, nil, errors.New("Generate ec key pair failed, " + err.Error())
}
pri := PrivateKey{
Algorithm: alg,
PrivateKey: &ecdsa.PrivateKey{
D: new(big.Int).SetBytes(d),
PublicKey: ecdsa.PublicKey{
X: x,
Y: y,
Curve: c,
},
},
}
pub := PublicKey{
Algorithm: alg,
PublicKey: &pri.PublicKey,
}
return &pri, &pub, nil
}
func EncodePublicKey(key *ecdsa.PublicKey, compressed bool) []byte {
if key == nil {
panic("invalid argument: public key is nil")
}
length := (key.Curve.Params().BitSize + 7) >> 3
buf := make([]byte, (length*2)+1)
x := key.X.Bytes()
copy(buf[length+1-len(x):], x)
if compressed {
if key.Y.Bit(0) == 0 {
buf[0] = compress_even
} else {
buf[0] = compress_odd
}
return buf[:length+1]
} else {
buf[0] = nocompress
y := key.Y.Bytes()
copy(buf[length*2+1-len(y):], y)
return buf
}
}
func DecodePublicKey(data []byte, curve elliptic.Curve) (*ecdsa.PublicKey, error) {
if curve == nil {
return nil, errors.New("unknown curve")
}
length := (curve.Params().BitSize + 7) >> 3
if len(data) < length+1 {
return nil, errors.New("invalid data length")
}
var x, y *big.Int
x = new(big.Int).SetBytes(data[1 : length+1])
if data[0] == nocompress {
if len(data) < length*2+1 {
return nil, errors.New("invalid data length")
}
y = new(big.Int).SetBytes(data[length+1 : length*2+1])
//TODO verify whether (x,y) is on the curve
//if !IsOnCurve(curve, x, y) {
// return nil, errors.New("Point is not on the curve")
//}
} else if data[0] == compress_even || data[0] == compress_odd {
// curve := crypto.S256()
pk, err := btcec.ParsePubKey(data, btcec.S256())
return (*ecdsa.PublicKey)(pk), err
/*
if err != nil {
return false
}
pubkey, err := ParsePubKey(pb, S256())
return deCompress(int(data[0]&1), data[1:length+1], curve)
*/
} else {
return nil, errors.New("unknown encoding mode")
}
return &ecdsa.PublicKey{
X: x,
Y: y,
Curve: curve,
}, nil
}
func ConstructPrivateKey(data []byte, curve elliptic.Curve) *ecdsa.PrivateKey {
d := new(big.Int).SetBytes(data)
x, y := curve.ScalarBaseMult(data)
return &ecdsa.PrivateKey{
D: d,
PublicKey: ecdsa.PublicKey{
X: x,
Y: y,
Curve: curve,
},
}
}