-
Notifications
You must be signed in to change notification settings - Fork 43
/
crypto.go
237 lines (214 loc) · 6.44 KB
/
crypto.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
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"encoding/hex"
"fmt"
"log"
"math/big"
"unsafe"
)
var bigIntZero = big.NewInt(0)
type ecdsaSignature struct {
R *big.Int
S *big.Int
}
func cryptoInit() {
if dbNumPrivateKeys() == 0 {
log.Println("Generating the initial wallet keypair...")
generatePrivateKey(-1)
log.Println("Generated.")
}
}
// Generates a keypair and writes it to the private database
func generatePrivateKey(height int) *ecdsa.PrivateKey {
keys, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatal(err)
}
privateKey, err := x509.MarshalECPrivateKey(keys)
if err != nil {
log.Fatal(err)
}
publicKey, err := x509.MarshalPKIXPublicKey(&keys.PublicKey)
if err != nil {
log.Fatal(err)
}
publicKeyHash := getPubKeyHash(publicKey)
dbWritePublicKey(publicKey, publicKeyHash, height)
dbWritePrivateKey(privateKey, publicKeyHash)
return keys
}
// Returns a hex string prefixed with the hash type and ":",
// e.g. "1:b12d4ac..."
func getPubKeyHash(b []byte) string {
hash := sha256.Sum256(b)
return "1:" + hex.EncodeToString(hash[:])
}
// getAPrivateKey returns a random keypair read from the database
// This is mostly useful when the database has only one keypair ;)
func cryptoGetAPrivateKey() (*ecdsa.PrivateKey, string, error) {
privateKeyBytes, publicKeyHash, err := dbGetAPrivateKey()
if err != nil {
return nil, "", err
}
dbPubKey, err := dbGetPublicKey(publicKeyHash)
if err != nil {
return nil, "", err
}
keys, err := x509.ParseECPrivateKey(privateKeyBytes)
if err != nil {
return nil, "", err
}
pubKey, err := x509.ParsePKIXPublicKey(dbPubKey.publicKeyBytes)
if err != nil {
log.Panicln(err)
return nil, "", err
}
keys.PublicKey = *pubKey.(*ecdsa.PublicKey)
if !elliptic.P256().IsOnCurve(keys.PublicKey.X, keys.PublicKey.Y) {
return nil, "", fmt.Errorf("Elliptic key verification error for %s", publicKeyHash)
}
// Check if we can get the right public key hash back again
testPublicKeyBytes, err := x509.MarshalPKIXPublicKey(&keys.PublicKey)
if err != nil {
log.Panicln(err)
}
testPublicKeyHash := getPubKeyHash(testPublicKeyBytes)
if testPublicKeyHash != publicKeyHash {
return nil, "", fmt.Errorf("Loaded keypair %s, but the calculated public key hash doesn't match: %s", publicKeyHash, testPublicKeyHash)
}
return keys, publicKeyHash, nil
}
// Decodes the given bytes into a public key
func cryptoDecodePublicKeyBytes(key []byte) (*ecdsa.PublicKey, error) {
ikey, err := x509.ParsePKIXPublicKey(key)
return ikey.(*ecdsa.PublicKey), err
}
// Returns a hash of the given public key
func cryptoMustGetPublicKeyHash(key *ecdsa.PublicKey) string {
publicKeyBytes, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
log.Fatalln(err)
}
return getPubKeyHash(publicKeyBytes)
}
// Signs the given public key hash with the given private key and returns the signature as a byte blob.
func cryptoSignPublicKeyHash(myPrivateKey *ecdsa.PrivateKey, publicKeyHash string) ([]byte, error) {
if publicKeyHash[1] != ':' {
return nil, fmt.Errorf("cryptoSignPublicKeyHash() expects a public key in the \"type:hex\" format, not \"%s\"", publicKeyHash)
}
publicKeyHashBytes, err := hex.DecodeString(publicKeyHash[2:])
if err != nil {
return nil, err
}
return cryptoSignBytes(myPrivateKey, publicKeyHashBytes)
}
// Returns nil (i.e. "no error") if the verification succeeds
func cryptoVerifyPublicKeyHashSignature(publicKey *ecdsa.PublicKey, publicKeyHash string, signature []byte) error {
if publicKeyHash[1] != ':' {
return fmt.Errorf("cryptoVerifyPublicKeyHash() expects a public key in the \"type:hex\" format, not \"%s\"", publicKeyHash)
}
publicKeyHashBytes, err := hex.DecodeString(publicKeyHash[2:])
if err != nil {
return err
}
return cryptoVerifyBytes(publicKey, publicKeyHashBytes, signature)
}
// Signs a hex-encoded byte blob. and returns a hex-encoded signature byte blob
func cryptoSignHex(myPrivateKey *ecdsa.PrivateKey, hash string) (string, error) {
hashBytes, err := hex.DecodeString(hash)
if err != nil {
return "", err
}
signatureBytes, err := cryptoSignBytes(myPrivateKey, hashBytes)
if err != nil {
return "", err
}
return hex.EncodeToString(signatureBytes), nil
}
// Verifies the given signature of a hash, both hex-encoded. Returns nil if everything's ok.
func cryptoVerifyHex(publicKey *ecdsa.PublicKey, hash string, signature string) error {
hashBytes, err := hex.DecodeString(hash)
if err != nil {
return err
}
signatureBytes, err := hex.DecodeString(signature)
if err != nil {
return err
}
return cryptoVerifyBytes(publicKey, hashBytes, signatureBytes)
}
// Signs a hex-encoded byte blob. and returns a signature byte blob
func cryptoSignHexBytes(myPrivateKey *ecdsa.PrivateKey, hash string) ([]byte, error) {
hashBytes, err := hex.DecodeString(hash)
if err != nil {
return nil, err
}
signatureBytes, err := cryptoSignBytes(myPrivateKey, hashBytes)
if err != nil {
return nil, err
}
return signatureBytes, nil
}
// Verifies the given signature of a hash. Returns nil if everything's ok.
func cryptoVerifyHexBytes(publicKey *ecdsa.PublicKey, hash string, signatureBytes []byte) error {
hashBytes, err := hex.DecodeString(hash)
if err != nil {
return err
}
return cryptoVerifyBytes(publicKey, hashBytes, signatureBytes)
}
// Signes a byte blob with the given private key.
func cryptoSignBytes(myPrivateKey *ecdsa.PrivateKey, hash []byte) ([]byte, error) {
var sig ecdsaSignature
var err error
var signature []byte
for {
sig.R, sig.S, err = ecdsa.Sign(rand.Reader, myPrivateKey, hash)
if err != nil {
return nil, fmt.Errorf("sign: %v", err)
}
signature, err = asn1.Marshal(sig)
if err != nil {
return nil, fmt.Errorf("marshal: %v", err)
}
if sig.R.Cmp(bigIntZero) != 0 {
break
}
}
return signature, nil
}
// Verifies a signed byte blob
func cryptoVerifyBytes(publicKey *ecdsa.PublicKey, hash []byte, signature []byte) error {
var sig ecdsaSignature
_, err := asn1.Unmarshal(signature, &sig)
if err != nil {
return err
}
if ecdsa.Verify(publicKey, hash, sig.R, sig.S) {
// Verification succeded
return nil
}
return fmt.Errorf("Signature verification failed")
}
// Returns a random positive 63-bit integer
func randInt63() int64 {
buf := make([]byte, 8)
n, err := rand.Read(buf)
if err != nil {
log.Panic(err)
}
if n != len(buf) {
log.Panic("Cannot read 8 random bytes")
}
v := *(*int64)(unsafe.Pointer(&buf[0]))
if v >= 0 {
return v
}
return -v
}