/
crypto.go
121 lines (102 loc) · 2.19 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
package internal
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha512"
"encoding/json"
"errors"
"log"
"math/big"
"github.com/AutoRoute/node/types"
)
type PrivateKey struct {
K *ecdsa.PrivateKey
}
type encodedprivk struct {
D *big.Int
PK PublicKey
}
func (e PrivateKey) MarshalJSON() ([]byte, error) {
return json.Marshal(encodedprivk{D: e.K.D, PK: e.PublicKey()})
}
func (e *PrivateKey) UnmarshalJSON(b []byte) error {
var s encodedprivk
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
e.K = &ecdsa.PrivateKey{ecdsa.PublicKey(s.PK), s.D}
return nil
}
func (p PrivateKey) PublicKey() PublicKey {
return PublicKey(p.K.PublicKey)
}
func (p PrivateKey) Sign(m []byte) Signature {
r, s, err := ecdsa.Sign(rand.Reader, p.K, m)
if err != nil {
log.Fatal(err)
}
return Signature{r, s, PublicKey(p.K.PublicKey), m}
}
type Signature struct {
R *big.Int
S *big.Int
K PublicKey
M []byte
}
func (e Signature) Key() PublicKey {
return e.K
}
func (e Signature) Verify() error {
k := ecdsa.PublicKey(e.K)
if k.X == nil || k.Y == nil {
return errors.New("Invalid Signature")
}
if e.M == nil || e.S == nil {
return errors.New("Invalid Signature")
}
if !ecdsa.Verify(&k, e.M, e.R, e.S) {
return errors.New("Invalid Signature")
}
return nil
}
func (e Signature) Signed() []byte {
return e.M
}
type PublicKey ecdsa.PublicKey
func hashstring(s string) string {
o := sha512.Sum512([]byte(s))
return string(o[0:sha512.Size])
}
func (e PublicKey) Hash() types.NodeAddress {
// Cannot error
t1, _ := e.X.MarshalText()
t2, _ := e.Y.MarshalText()
return types.NodeAddress(hashstring("ecdsa:P521:" + string(t1) + "," + string(t2)))
}
type encodedpk struct {
X *big.Int
Y *big.Int
}
func (e PublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(encodedpk{X: e.X, Y: e.Y})
}
func (e *PublicKey) UnmarshalJSON(b []byte) error {
var s encodedpk
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
e.X = s.X
e.Y = s.Y
e.Curve = elliptic.P521()
return nil
}
func NewECDSAKey() (PrivateKey, error) {
private, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{private}, nil
}