-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathhelpers.go
164 lines (121 loc) · 3.62 KB
/
helpers.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
package encryption
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/rand"
"encoding/binary"
"errors"
"io"
"time"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/crypto/ecies"
)
const keyBumpValue = uint64(10)
// GetCurrentTime64 returns the current unix time in milliseconds
func GetCurrentTime() uint64 {
return (uint64)(time.Now().UnixNano() / int64(time.Millisecond))
}
// bumpKeyID takes a timestampID and returns its value incremented by the keyBumpValue
func bumpKeyID(timestampID uint64) uint64 {
return timestampID + keyBumpValue
}
func generateHashRatchetKeyID(groupID []byte, timestamp uint64, keyBytes []byte) []byte {
var keyMaterial []byte
keyMaterial = append(keyMaterial, groupID...)
timestampBytes := make([]byte, 8) // 8 bytes for a uint64
binary.LittleEndian.PutUint64(timestampBytes, timestamp)
keyMaterial = append(keyMaterial, timestampBytes...)
keyMaterial = append(keyMaterial, keyBytes...)
return crypto.Keccak256(keyMaterial)
}
func publicKeyMostRelevantBytes(key *ecdsa.PublicKey) uint32 {
keyBytes := crypto.FromECDSAPub(key)
return binary.LittleEndian.Uint32(keyBytes[1:5])
}
func encrypt(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
func generateSharedKey(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) ([]byte, error) {
const encryptedPayloadKeyLength = 16
return ecies.ImportECDSA(privateKey).GenerateShared(
ecies.ImportECDSAPublic(publicKey),
encryptedPayloadKeyLength,
encryptedPayloadKeyLength,
)
}
func buildGroupRekeyMessage(privateKey *ecdsa.PrivateKey, groupID []byte, timestamp uint64, keyMaterial []byte, keys []*ecdsa.PublicKey) (*RekeyGroup, error) {
message := &RekeyGroup{
Timestamp: timestamp,
}
message.Keys = make(map[uint32][]byte)
for _, k := range keys {
sharedKey, err := generateSharedKey(privateKey, k)
if err != nil {
return nil, err
}
encryptedKey, err := encrypt(keyMaterial, sharedKey, rand.Reader)
if err != nil {
return nil, err
}
kBytes := publicKeyMostRelevantBytes(k)
if message.Keys[kBytes] == nil {
message.Keys[kBytes] = encryptedKey
} else {
message.Keys[kBytes] = append(message.Keys[kBytes], encryptedKey...)
}
}
return message, nil
}
const nonceLength = 12
func decrypt(cyphertext []byte, key []byte) ([]byte, error) {
if len(cyphertext) < nonceLength {
return nil, errors.New("invalid cyphertext length")
}
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := cyphertext[:nonceLength]
return gcm.Open(nil, nonce, cyphertext[nonceLength:], nil)
}
const keySize = 60
func decryptGroupRekeyMessage(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, message *RekeyGroup) ([]byte, error) {
kBytes := publicKeyMostRelevantBytes(&privateKey.PublicKey)
if message.Keys == nil || message.Keys[kBytes] == nil {
return nil, nil
}
sharedKey, err := generateSharedKey(privateKey, publicKey)
if err != nil {
return nil, err
}
keys := message.Keys[kBytes]
nKeys := len(keys) / keySize
var decryptedKey []byte
for i := 0; i < nKeys; i++ {
encryptedKey := keys[i*keySize : i*keySize+keySize]
decryptedKey, err = decrypt(encryptedKey, sharedKey)
if err != nil {
continue
} else {
break
}
}
return decryptedKey, nil
}