-
Notifications
You must be signed in to change notification settings - Fork 242
/
payload_encryptor.go
120 lines (98 loc) · 3.04 KB
/
payload_encryptor.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
package pairing
import (
"crypto/rand"
"github.com/status-im/status-go/protocol/common"
)
// EncryptionPayload represents the plain text and encrypted text of payload data
type EncryptionPayload struct {
plain []byte
encrypted []byte
locked bool
}
func (ep *EncryptionPayload) lock() {
ep.locked = true
}
// PayloadEncryptor is responsible for encrypting and decrypting payload data
type PayloadEncryptor struct {
aesKey []byte
payload *EncryptionPayload
}
func NewPayloadEncryptor(aesKey []byte) *PayloadEncryptor {
return &PayloadEncryptor{
aesKey,
new(EncryptionPayload),
}
}
// Renew regenerates the whole PayloadEncryptor and returns the new instance, only the aesKey is preserved
func (pem *PayloadEncryptor) Renew() *PayloadEncryptor {
return &PayloadEncryptor{
aesKey: pem.aesKey,
payload: new(EncryptionPayload),
}
}
// encryptPlain encrypts any given plain text using the internal AES key and returns the encrypted value
// This function is different to Encrypt as the internal EncryptionPayload.encrypted value is not set
func (pem *PayloadEncryptor) encryptPlain(plaintext []byte) ([]byte, error) {
return common.Encrypt(plaintext, pem.aesKey, rand.Reader)
}
// decryptPlain decrypts any given plain text using the internal AES key and returns the encrypted value
// This function is different to Decrypt as the internal EncryptionPayload.plain value is not set
func (pem *PayloadEncryptor) decryptPlain(plaintext []byte) ([]byte, error) {
return common.Decrypt(plaintext, pem.aesKey)
}
func (pem *PayloadEncryptor) encrypt(data []byte) error {
ep, err := common.Encrypt(data, pem.aesKey, rand.Reader)
if err != nil {
return err
}
pem.payload.plain = data
pem.payload.encrypted = ep
return nil
}
func (pem *PayloadEncryptor) decrypt(data []byte) error {
pd, err := common.Decrypt(data, pem.aesKey)
if err != nil {
return err
}
pem.payload.encrypted = data
pem.payload.plain = pd
return nil
}
func (pem *PayloadEncryptor) getEncrypted() []byte {
if pem.payload.locked {
return nil
}
return pem.payload.encrypted
}
func (pem *PayloadEncryptor) getDecrypted() []byte {
if pem.payload.locked {
return nil
}
return pem.payload.plain
}
func (pem *PayloadEncryptor) lockPayload() {
pem.payload.lock()
}
// PayloadLockPayload Embeds a *PayloadEncryptor to give all embedding structs EncryptionPayload Locking
type PayloadLockPayload struct {
*PayloadEncryptor
}
func (pl *PayloadLockPayload) LockPayload() {
pl.lockPayload()
}
// PayloadToSend Embeds a *PayloadEncryptor to give all embedding structs EncryptionPayload ToSend() functionality
// Useful to securely implement the PayloadMounter interface
type PayloadToSend struct {
*PayloadEncryptor
}
func (pts *PayloadToSend) ToSend() []byte {
return pts.getEncrypted()
}
// PayloadReceived Embeds a *PayloadEncryptor to give all embedding structs EncryptionPayload Received() functionality
// Useful to securely implement the PayloadReceiver interface
type PayloadReceived struct {
*PayloadEncryptor
}
func (pr *PayloadReceived) Received() []byte {
return pr.getDecrypted()
}