This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
contactDiscoveryCrypto.go
200 lines (175 loc) · 5.04 KB
/
contactDiscoveryCrypto.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
package contactDiscoveryCrypto
import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"github.com/signal-golang/textsecure/contactsDiscovery"
)
type QueryEnvelope struct {
Data []byte `json:"data"`
Iv []byte `json:"iv"`
Mac []byte `json:"mac"`
RequestId []byte `json:"requestId"`
}
type DiscoveryRequest struct {
AddressCount int `json:"addressCount"`
Commitment []byte `json:"commitment"`
Data []byte `json:"data"`
Envelopes map[string]*QueryEnvelope `json:"envelopes"`
Iv []byte `json:"iv"`
Mac []byte `json:"mac"`
}
type RemoteAttestation struct {
RequestId []byte
Keys RemoteAttestationKeys
Cookies []string
}
type RemoteAttestationKeys struct {
ClientKey []byte
ServerKey []byte
}
func CreateDiscoveryRequest(addressBook []string, remoteAttestations map[string]*contactsDiscovery.RemoteAttestation) (*DiscoveryRequest, error) {
queryDataKey, err := getRandomBytes(32)
if err != nil {
return nil, err
}
queryData, err := buildQueryData(addressBook)
if err != nil {
return nil, err
}
encryptedQueryData, err := aesEncrypt(queryDataKey, nil, queryData)
if err != nil {
return nil, err
}
commitment := hashSha256(queryData)
envelopes := make(map[string]*QueryEnvelope)
for key, attestation := range remoteAttestations {
envelopes[key], err = buildQueryEnvelopeFromAttestation(attestation, queryDataKey)
if err != nil {
return nil, err
}
}
return &DiscoveryRequest{
AddressCount: len(addressBook),
Commitment: commitment,
Data: encryptedQueryData.data,
Envelopes: envelopes,
Iv: encryptedQueryData.iv,
Mac: encryptedQueryData.mac}, nil
}
func getRandomBytes(length int) ([]byte, error) {
random := make([]byte, length)
_, err := rand.Read(random)
if err != nil {
return nil, err
}
return random, nil
}
func buildQueryData(addressBook []string) ([]byte, error) {
nonce, err := getRandomBytes(32)
if err != nil {
return nil, err
}
return buildQueryDataWithNonce(addressBook, nonce)
}
func buildQueryDataWithNonce(addressBook []string, nonce []byte) ([]byte, error) {
var addressData []byte
for _, address := range addressBook {
addressID, err := parse(address)
if err != nil {
return nil, err
}
addressData = append(addressData, toByteArray(addressID)...)
}
return append(nonce, addressData...), nil
}
func parse(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
func toByteArray(i int64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
return b
}
func toLong(a []byte) int64 {
return int64(binary.BigEndian.Uint64(a))
}
func buildQueryEnvelopeFromAttestation(attestation *contactsDiscovery.RemoteAttestation, queryDataKey []byte) (*QueryEnvelope, error) {
return buildQueryEnvelope(attestation.RequestId, attestation.Keys.ClientKey, queryDataKey)
}
func buildQueryEnvelope(requestID, clientKey, queryDataKey []byte) (*QueryEnvelope, error) {
result, err := aesEncrypt(clientKey, requestID, queryDataKey)
if err != nil {
return nil, err
}
return &QueryEnvelope{
Data: result.data,
Mac: result.mac,
Iv: result.iv,
RequestId: requestID,
}, nil
}
type AESEncryptedResult struct {
iv,
data,
mac,
aad []byte
}
func aesEncrypt(key, aad, data []byte) (*AESEncryptedResult, error) {
nonce, err := getRandomBytes(12) //iv
if err != nil {
return nil, err
}
return aesEncryptNonce(key, aad, data, nonce)
}
const TAG_LENGTH_BYTES = 16
func aesEncryptNonce(key, aad, data, nonce []byte) (*AESEncryptedResult, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCMWithTagSize(block, TAG_LENGTH_BYTES)
if err != nil {
return nil, err
}
ciphertext := aesgcm.Seal(nil, nonce, data, aad)
macStart := len(ciphertext) - TAG_LENGTH_BYTES
macPart := ciphertext[macStart:]
dataPart := ciphertext[:macStart]
return &AESEncryptedResult{nonce, dataPart, macPart, aad}, nil
}
func hashSha256(queryData []byte) []byte {
hash := sha256.Sum256(queryData)
return hash[:]
}
func GetDiscoveryResponseData(response DiscoveryResponse, remoteAttestations map[string]*contactsDiscovery.RemoteAttestation) ([]byte, error) {
for _, attestation := range remoteAttestations {
if bytes.Equal(response.RequestId, attestation.RequestId) {
return aesDecrypt(attestation.Keys.ServerKey, response.Iv, response.Data, response.Mac, nil)
}
}
return nil, errors.New("No matching RequestId")
}
type DiscoveryResponse struct {
Data []byte `json:"data"`
Iv []byte `json:"iv"`
Mac []byte `json:"mac"`
RequestId []byte `json:"requestId"`
}
func aesDecrypt(key, nonce, data, mac, aad []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCMWithTagSize(block, TAG_LENGTH_BYTES)
if err != nil {
return nil, err
}
ciphertext := append(data, mac...)
return aesgcm.Open(nil, nonce, ciphertext, aad)
}