This repository has been archived by the owner on May 24, 2022. It is now read-only.
/
c3crypto.go
441 lines (360 loc) · 9.89 KB
/
c3crypto.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package c3crypto
import (
"bufio"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
"math/big"
"os"
log "github.com/sirupsen/logrus"
"github.com/c3systems/c3-go/common/hexutil"
"github.com/obscuren/ecies"
)
const (
// EncryptionCipher ...
EncryptionCipher = x509.PEMCipherAES256
// PrivateKeyPEMType ...
PrivateKeyPEMType = "ECDSA PRIVATE KEY"
// PublicKeyPEMType ...
PublicKeyPEMType = "ECDSA PUBLIC KEY"
)
var (
// ErrNilPrivateKey ...
ErrNilPrivateKey = errors.New("private key is nil")
// ErrNilPublicKey ...
ErrNilPublicKey = errors.New("public key is nil")
// ErrNilData ...
ErrNilData = errors.New("data is nil")
// ErrNilSigParams ...
ErrNilSigParams = errors.New("r and s params cannot be nil")
// ErrInvalidPublicKey ...
ErrInvalidPublicKey = errors.New("invalid public key")
// ErrGeneratingECIESPublicKey ...
ErrGeneratingECIESPublicKey = errors.New("could not generate ecies public key from ecda public key")
// ErrGeneratingECIESPrivateKey ...
ErrGeneratingECIESPrivateKey = errors.New("could not generate ecies private key from ecdsa private key")
// ErrGeneratingECDSAPublicKey ...
ErrGeneratingECDSAPublicKey = errors.New("could not generate ecdsa public key from ecdsa private key")
// ErrNotECDSAPubKey ...
ErrNotECDSAPubKey = errors.New("not an ecdsa public key")
// Curve ...
Curve = elliptic.P256()
//Curve = elliptic.P384() // note: causes a shared key params are too big err
//Curve = elliptic.P521() // note: causes an out of range panic at ecies.go L106!!
//Curve = secp256k1.S256() // note: what ethereum uses
)
// NewKeyPair ...
func NewKeyPair() (*ecdsa.PrivateKey, *ecdsa.PublicKey, error) {
priv, err := NewPrivateKey()
if err != nil {
return nil, nil, err
}
pub, err := GetPublicKey(priv)
return priv, pub, err
}
// NewPrivateKey ...
func NewPrivateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(Curve, rand.Reader)
}
// GetPublicKey ...
func GetPublicKey(priv *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
if priv == nil {
return nil, ErrNilPrivateKey
}
return &priv.PublicKey, nil
}
// Sign ...
func Sign(priv *ecdsa.PrivateKey, data []byte) (*big.Int, *big.Int, error) {
if data == nil {
return nil, nil, ErrNilData
}
if priv == nil {
return nil, nil, ErrNilPrivateKey
}
return ecdsa.Sign(rand.Reader, priv, data)
}
// Verify ...
func Verify(pub *ecdsa.PublicKey, data []byte, r, s *big.Int) (bool, error) {
if pub == nil {
return false, ErrNilPublicKey
}
if data == nil {
return false, ErrNilData
}
if r == nil || s == nil {
return false, ErrNilSigParams
}
return ecdsa.Verify(pub, data, r, s), nil
}
// Encrypt ...
func Encrypt(pub *ecdsa.PublicKey, data []byte) ([]byte, error) {
if pub == nil {
return nil, ErrNilPublicKey
}
if data == nil {
return nil, ErrNilData
}
eciesPubKey := ecies.ImportECDSAPublic(pub)
if eciesPubKey == nil {
return nil, ErrGeneratingECIESPublicKey
}
return ecies.Encrypt(rand.Reader, eciesPubKey, data, nil, nil)
}
// Decrypt ...
func Decrypt(priv *ecdsa.PrivateKey, data []byte) ([]byte, error) {
if priv == nil {
return nil, ErrNilPrivateKey
}
if data == nil {
return nil, ErrNilData
}
eciesPrivKey := ecies.ImportECDSA(priv)
if eciesPrivKey == nil {
return nil, ErrGeneratingECIESPrivateKey
}
return eciesPrivKey.Decrypt(rand.Reader, data, nil, nil)
}
// SerializePrivateKey ...
func SerializePrivateKey(priv *ecdsa.PrivateKey) ([]byte, error) {
if priv == nil {
return nil, ErrNilPrivateKey
}
return x509.MarshalECPrivateKey(priv)
}
// SerializePublicKey ...
func SerializePublicKey(pub *ecdsa.PublicKey) ([]byte, error) {
if pub == nil {
return nil, ErrNilPublicKey
}
return x509.MarshalPKIXPublicKey(pub)
}
// DeserializePrivateKey ...
func DeserializePrivateKey(privBytes []byte) (*ecdsa.PrivateKey, error) {
return x509.ParseECPrivateKey(privBytes)
}
// DeserializePublicKey ...
func DeserializePublicKey(pubBytes []byte) (*ecdsa.PublicKey, error) {
pubIfc, err := x509.ParsePKIXPublicKey(pubBytes)
if err != nil {
return nil, err
}
pub, ok := pubIfc.(*ecdsa.PublicKey)
if !ok {
return nil, ErrNotECDSAPubKey
}
return pub, nil
}
// EncodePrivateKeyToPem ...
// note password is optional
func EncodePrivateKeyToPem(priv *ecdsa.PrivateKey, password *string) (*pem.Block, error) {
bytes, err := SerializePrivateKey(priv)
if err != nil {
return nil, err
}
block := &pem.Block{
Type: PrivateKeyPEMType,
Bytes: bytes,
}
if password != nil {
return x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, []byte(*password), EncryptionCipher)
}
return block, nil
}
// EncodePublicKeyToPem ...
// note: password is optional
func EncodePublicKeyToPem(pub *ecdsa.PublicKey, password *string) (*pem.Block, error) {
bytes, err := SerializePublicKey(pub)
if err != nil {
return nil, err
}
block := &pem.Block{
Type: PublicKeyPEMType,
Bytes: bytes,
}
if password != nil {
return x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, []byte(*password), EncryptionCipher)
}
return block, nil
}
// DecodePemToPrivateKey ...
func DecodePemToPrivateKey(block *pem.Block, password *string) (*ecdsa.PrivateKey, error) {
if password == nil && x509.IsEncryptedPEMBlock(block) {
return nil, errors.New("pem block is encrypted but no password provided")
}
if password != nil && !x509.IsEncryptedPEMBlock(block) {
return nil, errors.New("a password was provided but pem block is not encrypted")
}
if password != nil {
bytes, err := x509.DecryptPEMBlock(block, []byte(*password))
if err != nil {
return nil, err
}
return DeserializePrivateKey(bytes)
}
return DeserializePrivateKey(block.Bytes)
}
// DecodePemToPublicKey ...
func DecodePemToPublicKey(block *pem.Block, password *string) (*ecdsa.PublicKey, error) {
if password == nil && x509.IsEncryptedPEMBlock(block) {
return nil, errors.New("pem block is encrypted but no password provided")
}
if password != nil && !x509.IsEncryptedPEMBlock(block) {
return nil, errors.New("a password was provided but pem block is not encrypted")
}
if password != nil {
bytes, err := x509.DecryptPEMBlock(block, []byte(*password))
if err != nil {
return nil, err
}
return DeserializePublicKey(bytes)
}
return DeserializePublicKey(block.Bytes)
}
// WritePrivateKeyToPemFile ...
func WritePrivateKeyToPemFile(priv *ecdsa.PrivateKey, password *string, fileName string) error {
block, err := EncodePrivateKeyToPem(priv, password)
if err != nil {
return err
}
f, err := os.Create(fileName)
if err != nil {
log.Printf("[c3crypto] err creating file %s", err)
return err
}
defer f.Close()
w := bufio.NewWriter(f)
if err := pem.Encode(w, block); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return f.Sync()
}
// WritePublicKeyToPemFile ...
func WritePublicKeyToPemFile(pub *ecdsa.PublicKey, password *string, fileName string) error {
block, err := EncodePublicKeyToPem(pub, password)
if err != nil {
return err
}
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
if err := pem.Encode(w, block); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return f.Sync()
}
// ReadPrivateKeyFromPem ...
func ReadPrivateKeyFromPem(fileName string, password *string) (*ecdsa.PrivateKey, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
block, _ := pem.Decode(bytes)
return DecodePemToPrivateKey(block, password)
}
// ReadPublicKeyFromPem ...
func ReadPublicKeyFromPem(fileName string, password *string) (*ecdsa.PublicKey, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
block, _ := pem.Decode(bytes)
return DecodePemToPublicKey(block, password)
}
// WriteKeyPairToPem ...
func WriteKeyPairToPem(priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, password *string, fileName string) error {
privBlock, err := EncodePrivateKeyToPem(priv, password)
if err != nil {
return err
}
pubBlock, err := EncodePublicKeyToPem(pub, password)
if err != nil {
return err
}
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
if err := pem.Encode(w, privBlock); err != nil {
return err
}
if err := pem.Encode(w, pubBlock); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return f.Sync()
}
// ReadKeyPairFromPem ...
func ReadKeyPairFromPem(fileName string, password *string) (*ecdsa.PrivateKey, *ecdsa.PublicKey, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, nil, err
}
privBlock, rest := pem.Decode(bytes)
pubBlock, _ := pem.Decode(rest)
priv, err := DecodePemToPrivateKey(privBlock, password)
if err != nil {
return nil, nil, err
}
pub, err := DecodePemToPublicKey(pubBlock, password)
if err != nil {
return nil, nil, err
}
return priv, pub, nil
}
// EncodeAddress ...
func EncodeAddress(pub *ecdsa.PublicKey) (string, error) {
bytes, err := SerializePublicKey(pub)
if err != nil {
return "", err
}
return hexutil.EncodeToString(bytes), nil
}
// DecodeAddress [for now] decodes public address hex to ECDSA public key.
// Public key are treated as public address at the moment.
func DecodeAddress(address string) (*ecdsa.PublicKey, error) {
byteStr, err := hexutil.StripLeader(address)
if err != nil {
return nil, err
}
bytes, err := hexutil.DecodeBytes([]byte(byteStr))
if err != nil {
return nil, err
}
pub, err := DeserializePublicKey(bytes)
if err != nil {
return nil, err
}
return pub, nil
}
// PublicKeyToBytes ...
func PublicKeyToBytes(pub *ecdsa.PublicKey) ([]byte, error) {
if pub == nil {
return nil, ErrInvalidPublicKey
}
return elliptic.Marshal(Curve, pub.X, pub.Y), nil
}
// PublicKeyFromBytes ...
func PublicKeyFromBytes(pub []byte) (*ecdsa.PublicKey, error) {
x, y := elliptic.Unmarshal(Curve, pub)
if x == nil {
return nil, ErrInvalidPublicKey
}
return &ecdsa.PublicKey{Curve: Curve, X: x, Y: y}, nil
}