/
crypto_rsa.go
313 lines (273 loc) · 7.3 KB
/
crypto_rsa.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
package rsa
/*
-= Notes on the crypto =-
"The message must be no longer than the length of the public
modulus less twice the hash length plus 2."
The modulus for a 4096 bit key is 512 bytes.
The hash length for SHA256 is 32 bytes.
(32*2)+2 = 68. 512 - 68 = 444. So we can have 444 bytes of data.
AES256 keys are 32 bytes long. AES256 IVs are 16 bytes long.
Message Format:
--rsa begin---
(32) AES256 key
(32) SHA256 enc(msg) hash
(32) SHA256 header excluding this field hash TOTAL=96 bytes
--rsa end---
--aes begin---
message
--aes end---
*/
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/subtle"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"io"
"log"
"github.com/awgh/bencrypt"
"github.com/awgh/bencrypt/bc"
)
var (
// NAME - human-readable name of this Crypto implementation
NAME = "RSA-4096,AES-CBC-256,HMAC-SHA-256"
)
func init() {
bencrypt.KeypairTypes[NAME] = func() bc.KeyPair { return new(KeyPair) }
}
// PubKey : Implements bc.PubKey interface
type PubKey struct {
Pubkey *rsa.PublicKey
}
// ToB64 : Returns Public Key as a Base64 encoded string
func (e *PubKey) ToB64() string {
return base64.StdEncoding.EncodeToString(e.ToBytes())
}
// FromB64 : Sets Public Key from a Base64 encoded string
func (e *PubKey) FromB64(s string) error {
pubkeypem, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
return e.FromBytes(pubkeypem)
}
// ToBytes : Returns Public Key as bytes (PEM text)
func (e *PubKey) ToBytes() []byte {
var b pem.Block
b.Type = "PUBLIC KEY"
b.Bytes, _ = x509.MarshalPKIXPublicKey(e.Pubkey)
data := pem.EncodeToMemory(&b)
return data
}
// FromBytes : Sets Public Key from bytes (PEM text)
func (e *PubKey) FromBytes(b []byte) error {
p, _ := pem.Decode(b)
if p != nil && p.Type == "PUBLIC KEY" {
pub, err := x509.ParsePKIXPublicKey(p.Bytes)
if err != nil {
return err
}
e.Pubkey = pub.(*rsa.PublicKey)
if e.Pubkey == nil {
return errors.New("Nil Public Key in PubKey.FromBytes")
}
return nil
}
return errors.New("No Public Key Found in PubKey.FromBytes")
}
// Clone : Returns a new PubKey of the same type as this one
func (e *PubKey) Clone() bc.PubKey {
return new(PubKey)
}
// Nil : Returns the interface-to-nil-pointer type for this PubKey
func (e *PubKey) Nil() interface{} {
return (*PubKey)(nil)
}
// KeyPair for RSA : Bencrypt Implementation of a RSA-4096,AES-CBC-256,HMAC-SHA-256 system
type KeyPair struct {
privkey *rsa.PrivateKey
pubkey *PubKey
keyHash []byte
}
// GetName : Returns the common language name for this cryptosystem
func (r *KeyPair) GetName() string {
return NAME
}
// GetPubKey : Returns the Public portion of this KeyPair
func (r *KeyPair) GetPubKey() bc.PubKey {
return r.pubkey
}
// Precompute : Precomputes key
func (r *KeyPair) Precompute() {
keybytes, _ := x509.MarshalPKIXPublicKey(r.privkey)
sha := sha256.New()
if _, err := sha.Write(keybytes); err != nil {
log.Fatal(err.Error())
}
r.keyHash = sha.Sum(nil)
r.privkey.Precompute()
if err := r.privkey.Validate(); err != nil {
log.Fatal(err.Error())
}
if r.pubkey == nil {
r.pubkey = new(PubKey)
}
r.pubkey.Pubkey = &r.privkey.PublicKey
}
// GenerateKey : Generates a new keypair inside this KeyPair object
func (r *KeyPair) GenerateKey() {
p, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
log.Fatal(err.Error())
}
r.privkey = p
r.Precompute()
}
// ToB64 : Returns the private portion of this keypair as a Base64-encoded string
func (r *KeyPair) ToB64() string {
var b pem.Block
b.Type = "RSA PRIVATE KEY"
b.Bytes = x509.MarshalPKCS1PrivateKey(r.privkey)
data := pem.EncodeToMemory(&b)
s := base64.StdEncoding.EncodeToString(data)
return s
}
// FromB64 : Sets the private portion of this keypair from a Base64-encoded string
func (r *KeyPair) FromB64(s string) error {
dec, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
p, _ := pem.Decode(dec)
if p != nil && p.Type == "RSA PRIVATE KEY" {
priv, err := x509.ParsePKCS1PrivateKey(p.Bytes)
if err != nil {
return err
}
r.privkey = priv
r.Precompute()
return nil
}
return errors.New("No Private Key Found")
}
/*
EncryptMessage : Encrypts a message
1) generate aes key (32 random bytes)
2) encrypt the message with aes key
3) sha256 the encrypted message
4) sha256 the key and msg hash
5) encrypt the header with rsa
6) PEM encode encrypted header and encrypted message -> return
*/
func (r *KeyPair) EncryptMessage(clear []byte, pubkey bc.PubKey) ([]byte, error) {
public, ok := pubkey.(*PubKey)
if !ok {
log.Fatal("KeyPair.EncryptMessage requires an PubKey, not some different kind of key")
}
// 1) generate aes key (32 random bytes)
key := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
return nil, err
}
// 2) encrypt the message with aes key
ciphertext, err := bc.AesEncrypt(clear, key)
if err != nil {
return nil, err
}
// 3) sha256 the encrypted message
sha := sha256.New()
_, err = sha.Write(ciphertext)
if err != nil {
return nil, err
}
msgsum := sha.Sum(nil)
// 4) sha256 the key and msg hash
sha.Reset()
_, err = sha.Write(key)
if err != nil {
return nil, err
}
_, err = sha.Write(msgsum)
if err != nil {
return nil, err
}
hdrsum := sha.Sum(nil)
// 5) encrypt the header with rsa
header := make([]byte, 32+32+32)
copy(header, key)
copy(header[32:], msgsum)
copy(header[32+32:], hdrsum)
label := []byte("")
enchdr, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, public.Pubkey, header, label)
if err != nil {
return nil, err
}
//6) PEM encode encrypted header and encrypted message -> return
var h, t pem.Block
h.Type = "HEADS"
h.Bytes = enchdr
t.Type = "TAILS"
t.Bytes = ciphertext
outval := pem.EncodeToMemory(&h)
return append(outval, pem.EncodeToMemory(&t)...), nil
}
/*
DecryptMessage : Decrypts a message
1) PEM decode header and body
2) decrypt header
3) verify the header signature
4) verify the msg signature
5) decrypt the msg
*/
func (r *KeyPair) DecryptMessage(data []byte) (bool, []byte, error) {
h, rest := pem.Decode(data)
t, _ := pem.Decode(rest)
if h == nil || t == nil {
return false, nil, errors.New("Invalid message - header or body missing")
}
label := []byte("")
header, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, r.privkey, h.Bytes, label)
if err != nil {
return false, nil, err
}
sha := sha256.New()
_, err = sha.Write(header[:32+32])
if err != nil {
return false, nil, err
}
hdrsum := sha.Sum(nil)
x := subtle.ConstantTimeCompare(hdrsum, header[32+32:])
if x != 1 {
return false, nil, errors.New("Invalid message - header checksum failed")
}
sha.Reset()
_, err = sha.Write(t.Bytes)
if err != nil {
return false, nil, err
}
msgsum := sha.Sum(nil)
x = subtle.ConstantTimeCompare(msgsum, header[32:32+32])
if x != 1 {
return false, nil, errors.New("Invalid message - message checksum failed")
}
clear, err := bc.AesDecrypt(t.Bytes, header[:32])
if err != nil {
return false, nil, err
}
return true, clear, nil
}
// ValidatePubKey : Returns true if and only if the argument is a valid PubKey to this KeyPair
func (r *KeyPair) ValidatePubKey(s string) bool {
pk := new(PubKey)
if err := pk.FromB64(s); err != nil || pk.Pubkey == nil {
return false
}
return true
}
// Clone : Returns a new node of the same type as this one
func (r *KeyPair) Clone() bc.KeyPair {
return new(KeyPair)
}