-
Notifications
You must be signed in to change notification settings - Fork 40
/
encryption.go
executable file
·185 lines (130 loc) · 3.33 KB
/
encryption.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
package ipc
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"errors"
"io"
"net"
)
func (sc *Server) keyExchange() ([32]byte, error) {
var shared [32]byte
priv, pub, err := generateKeys()
if err != nil {
return shared, err
}
// send servers public key
err = sendPublic(sc.conn, pub)
if err != nil {
return shared, err
}
// received clients public key
pubRecvd, err := recvPublic(sc.conn)
if err != nil {
return shared, err
}
b, _ := pubRecvd.Curve.ScalarMult(pubRecvd.X, pubRecvd.Y, priv.D.Bytes())
shared = sha256.Sum256(b.Bytes())
return shared, nil
}
func (cc *Client) keyExchange() ([32]byte, error) {
var shared [32]byte
priv, pub, err := generateKeys()
if err != nil {
return shared, err
}
// received servers public key
pubRecvd, err := recvPublic(cc.conn)
if err != nil {
return shared, err
}
// send clients public key
err = sendPublic(cc.conn, pub)
if err != nil {
return shared, err
}
b, _ := pubRecvd.Curve.ScalarMult(pubRecvd.X, pubRecvd.Y, priv.D.Bytes())
shared = sha256.Sum256(b.Bytes())
return shared, nil
}
func generateKeys() (*ecdsa.PrivateKey, *ecdsa.PublicKey, error) {
priva, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
return nil, nil, err
}
puba := &priva.PublicKey
if !priva.IsOnCurve(puba.X, puba.Y) {
return nil, nil, errors.New("keys created arn't on curve")
}
return priva, puba, err
}
func sendPublic(conn net.Conn, pub *ecdsa.PublicKey) error {
pubSend := publicKeyToBytes(pub)
if pubSend == nil {
return errors.New("public key cannot be converted to bytes")
}
_, err := conn.Write(pubSend)
if err != nil {
return errors.New("could not sent public key")
}
return nil
}
func recvPublic(conn net.Conn) (*ecdsa.PublicKey, error) {
buff := make([]byte, 98)
i, err := conn.Read(buff)
if err != nil {
return nil, errors.New("didn't received public key")
}
if i != 97 {
return nil, errors.New("public key received isn't valid length")
}
recvdPub := bytesToPublicKey(buff[:i])
if !recvdPub.IsOnCurve(recvdPub.X, recvdPub.Y) {
return nil, errors.New("didn't received valid public key")
}
return recvdPub, nil
}
func publicKeyToBytes(pub *ecdsa.PublicKey) []byte {
if pub == nil || pub.X == nil || pub.Y == nil {
return nil
}
return elliptic.Marshal(elliptic.P384(), pub.X, pub.Y)
}
func bytesToPublicKey(recvdPub []byte) *ecdsa.PublicKey {
if len(recvdPub) == 0 {
return nil
}
x, y := elliptic.Unmarshal(elliptic.P384(), recvdPub)
return &ecdsa.PublicKey{Curve: elliptic.P384(), X: x, Y: y}
}
func createCipher(shared [32]byte) (*cipher.AEAD, error) {
b, err := aes.NewCipher(shared[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(b)
if err != nil {
return nil, err
}
return &gcm, nil
}
func encrypt(g cipher.AEAD, data []byte) ([]byte, error) {
nonce := make([]byte, g.NonceSize())
_, err := io.ReadFull(rand.Reader, nonce)
return g.Seal(nonce, nonce, data, nil), err
}
func decrypt(g cipher.AEAD, recdData []byte) ([]byte, error) {
nonceSize := g.NonceSize()
if len(recdData) < nonceSize {
return nil, errors.New("not enough data to decrypt")
}
nonce, recdData := recdData[:nonceSize], recdData[nonceSize:]
plain, err := g.Open(nil, nonce, recdData, nil)
if err != nil {
return nil, err
}
return plain, nil
}