-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.go
223 lines (187 loc) · 4.48 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
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
package enc
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/gob"
)
const (
cryptoHash = crypto.SHA256
)
func hash(msg *[]byte) []byte {
h := sha256.New()
h.Write(*msg)
return h.Sum(nil)
}
func splitMsg(msg []byte, maxSize int) [][]byte {
noParts := len(msg) / maxSize
if noParts*maxSize < len(msg) {
noParts += 1
}
toEncrypt := make([][]byte, noParts)
for i, _ := range toEncrypt {
if len(msg) > maxSize {
toEncrypt[i] = msg[:maxSize]
msg = msg[maxSize:]
} else {
toEncrypt[i] = msg
}
}
return toEncrypt
}
func joinMsg(parts [][]byte) []byte {
var buf bytes.Buffer
for _, part := range parts {
buf.Write(part)
}
return buf.Bytes()
}
type EncryptedMessage struct { //RENAME me to EncryptedMessage
Msg [][]byte
}
func (e *EncryptedMessage) Bytes() []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(e)
if err != nil {
panic(err.Error())
}
return buf.Bytes()
}
func (e *EncryptedMessage) Decrypt(privateKey *rsa.PrivateKey) (signed *SignedMessage, err error) {
hash := sha256.New()
parts := make([][]byte, len(e.Msg))
for i, part := range e.Msg {
parts[i], err = rsa.DecryptOAEP(hash, rand.Reader, privateKey, part, nil)
}
if err != nil {
return nil, err
}
wholeMsg := joinMsg(parts)
//get signed message
signed = new(SignedMessage)
dec := gob.NewDecoder(bytes.NewReader(wholeMsg))
err = dec.Decode(signed)
if err != nil {
return nil, err
}
return signed, nil
}
func (e *EncryptedMessage) DecryptAndVerify(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) (msg []byte, err error) {
signed, err := e.Decrypt(privateKey)
if err != nil {
return nil, err
}
if _, err := signed.Verify(publicKey); err != nil {
return nil, err
}
//take message out
var message Message
dec := gob.NewDecoder(bytes.NewReader(signed.Msg))
err = dec.Decode(&message)
if err != nil {
return nil, err
}
return message.Msg, nil
}
func (e *EncryptedMessage) DecryptToMsg(privateKey *rsa.PrivateKey) (msg []byte, err error) {
signed, err := e.Decrypt(privateKey)
if err != nil {
return nil, err
}
//take message out
var message Message
dec := gob.NewDecoder(bytes.NewReader(signed.Msg))
err = dec.Decode(&message)
if err != nil {
return nil, err
}
return message.Msg, nil
}
type SignedMessage struct {
Msg []byte
Signature []byte
}
func (s *SignedMessage) Verify(publicKey *rsa.PublicKey) (bool, error) {
hashed := hash(&s.Msg)
err := rsa.VerifyPKCS1v15(publicKey, cryptoHash, hashed, s.Signature)
if err == nil {
return true, nil
}
return false, err
}
func (s *SignedMessage) Encrypt(publicKey *rsa.PublicKey) (encrypted *EncryptedMessage, err error) {
encrypted = new(EncryptedMessage)
hash := sha256.New()
//Join message and signature
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(s)
if err != nil {
return nil, err
}
//Split messages to parts acceptable by encrypter
k := (publicKey.N.BitLen() + 7) / 8
maxSize := k - 2*hash.Size() - 2 //from rsa.go
toEncrypt := splitMsg(buf.Bytes(), maxSize)
//encrypt
parts := make([][]byte, len(toEncrypt))
for i, part := range toEncrypt {
parts[i], err = rsa.EncryptOAEP(hash, rand.Reader, publicKey, part, nil)
if err != nil {
return nil, err
}
}
encrypted.Msg = parts
return encrypted, nil
}
type Message struct {
Msg []byte
}
func (m *Message) SignMessage(privateKey *rsa.PrivateKey) (signed *SignedMessage, err error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(m)
if err != nil {
return nil, err
}
msg := buf.Bytes()
signed = new(SignedMessage)
signed.Msg = msg
hashed := hash(&msg)
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, cryptoHash, hashed)
if err != nil {
return nil, err
}
signed.Signature = sign
return signed, nil
}
func SignAndEncrypt(msg []byte, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) (signedAndEncrypted *EncryptedMessage, err error) {
message := Message{
Msg: msg,
}
signedMessage, err := message.SignMessage(privateKey)
if err != nil {
return nil, err
}
encryptedMessage, err := signedMessage.Encrypt(publicKey)
if err != nil {
return nil, err
}
return encryptedMessage, nil
}
func Decrypt(msg []byte, privateKey *rsa.PrivateKey) ([]byte, error) {
var encryptedMessage EncryptedMessage
dec := gob.NewDecoder(bytes.NewReader(msg))
err := dec.Decode(&encryptedMessage)
if err != nil {
return nil, err
}
msg, err = encryptedMessage.DecryptToMsg(privateKey)
if err != nil {
return nil, err
}
return msg, nil
}