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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
#include <signal_helper.h>
#include <signal_protocol_internal.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/err.h>
signal_type_base* signal_type_ref_vapi(signal_type_base* instance) {
if (instance->ref_count > 100 || instance->ref_count < 1)
printf("REF %x -> %d\n", instance, instance->ref_count+1);
signal_type_ref(instance);
return instance;
}
signal_type_base* signal_type_unref_vapi(signal_type_base* instance) {
if (instance->ref_count > 100 || instance->ref_count < 0)
printf("UNREF %x -> %d\n", instance, instance->ref_count-1);
signal_type_unref(instance);
return 0;
}
signal_protocol_address* signal_protocol_address_new() {
signal_protocol_address* address = malloc(sizeof(signal_protocol_address));
address->name = 0;
address->device_id = 0;
return address;
}
void signal_protocol_address_free(signal_protocol_address* ptr) {
if (ptr->name) {
g_free((void*)ptr->name);
}
return free(ptr);
}
void signal_protocol_address_set_name(signal_protocol_address* self, const gchar* name) {
gchar* n = g_malloc(strlen(name)+1);
memcpy(n, name, strlen(name));
n[strlen(name)] = 0;
if (self->name) {
g_free((void*)self->name);
}
self->name = n;
self->name_len = strlen(n);
}
gchar* signal_protocol_address_get_name(signal_protocol_address* self) {
if (self->name == 0) return 0;
gchar* res = g_malloc(sizeof(char) * (self->name_len + 1));
memcpy(res, self->name, self->name_len);
res[self->name_len] = 0;
return res;
}
session_pre_key* session_pre_key_new(uint32_t pre_key_id, ec_key_pair* pair, int* err) {
session_pre_key* res;
*err = session_pre_key_create(&res, pre_key_id, pair);
return res;
}
session_signed_pre_key* session_signed_pre_key_new(uint32_t id, uint64_t timestamp, ec_key_pair* pair, uint8_t* key, int key_len, int* err) {
session_signed_pre_key* res;
*err = session_signed_pre_key_create(&res, id, timestamp, pair, key, key_len);
return res;
}
int signal_vala_random_generator(uint8_t *data, size_t len, void *user_data)
{
if(RAND_bytes(data, len)) {
return 0;
}
else {
return SG_ERR_UNKNOWN;
}
}
int signal_vala_hmac_sha256_init(void **hmac_context, const uint8_t *key, size_t key_len, void *user_data)
{
HMAC_CTX *ctx = malloc(sizeof(HMAC_CTX));
if(!ctx) {
return SG_ERR_NOMEM;
}
HMAC_CTX_init(ctx);
*hmac_context = ctx;
if(HMAC_Init_ex(ctx, key, key_len, EVP_sha256(), 0) != 1) {
return SG_ERR_UNKNOWN;
}
return 0;
}
int signal_vala_hmac_sha256_update(void *hmac_context, const uint8_t *data, size_t data_len, void *user_data)
{
HMAC_CTX *ctx = hmac_context;
int result = HMAC_Update(ctx, data, data_len);
return (result == 1) ? 0 : -1;
}
int signal_vala_hmac_sha256_final(void *hmac_context, signal_buffer **output, void *user_data)
{
int result = 0;
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int len = 0;
HMAC_CTX *ctx = hmac_context;
if(HMAC_Final(ctx, md, &len) != 1) {
return SG_ERR_UNKNOWN;
}
signal_buffer *output_buffer = signal_buffer_create(md, len);
if(!output_buffer) {
result = SG_ERR_NOMEM;
goto complete;
}
*output = output_buffer;
complete:
return result;
}
void signal_vala_hmac_sha256_cleanup(void *hmac_context, void *user_data)
{
if(hmac_context) {
HMAC_CTX *ctx = hmac_context;
HMAC_CTX_cleanup(ctx);
free(ctx);
}
}
const EVP_CIPHER *aes_cipher(int cipher, size_t key_len)
{
if(cipher == SG_CIPHER_AES_CBC_PKCS5) {
if(key_len == 16) {
return EVP_aes_128_cbc();
}
else if(key_len == 24) {
return EVP_aes_192_cbc();
}
else if(key_len == 32) {
return EVP_aes_256_cbc();
}
}
else if(cipher == SG_CIPHER_AES_CTR_NOPADDING) {
if(key_len == 16) {
return EVP_aes_128_ctr();
}
else if(key_len == 24) {
return EVP_aes_192_ctr();
}
else if(key_len == 32) {
return EVP_aes_256_ctr();
}
}
else if (cipher == SG_CIPHER_AES_GCM_NOPADDING) {
if(key_len == 16) {
return EVP_aes_128_gcm();
}
else if(key_len == 24) {
return EVP_aes_192_gcm();
}
else if(key_len == 32) {
return EVP_aes_256_gcm();
}
}
return 0;
}
int signal_vala_sha512_digest_init(void **digest_context, void *user_data)
{
int result = 0;
EVP_MD_CTX *ctx;
ctx = EVP_MD_CTX_create();
if(!ctx) {
result = SG_ERR_NOMEM;
goto complete;
}
result = EVP_DigestInit_ex(ctx, EVP_sha512(), 0);
if(result == 1) {
result = SG_SUCCESS;
}
else {
result = SG_ERR_UNKNOWN;
}
complete:
if(result < 0) {
if(ctx) {
EVP_MD_CTX_destroy(ctx);
}
}
else {
*digest_context = ctx;
}
return result;
}
int signal_vala_sha512_digest_update(void *digest_context, const uint8_t *data, size_t data_len, void *user_data)
{
EVP_MD_CTX *ctx = digest_context;
int result = EVP_DigestUpdate(ctx, data, data_len);
return (result == 1) ? SG_SUCCESS : SG_ERR_UNKNOWN;
}
int signal_vala_sha512_digest_final(void *digest_context, signal_buffer **output, void *user_data)
{
int result = 0;
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int len = 0;
EVP_MD_CTX *ctx = digest_context;
result = EVP_DigestFinal_ex(ctx, md, &len);
if(result == 1) {
result = SG_SUCCESS;
}
else {
result = SG_ERR_UNKNOWN;
goto complete;
}
result = EVP_DigestInit_ex(ctx, EVP_sha512(), 0);
if(result == 1) {
result = SG_SUCCESS;
}
else {
result = SG_ERR_UNKNOWN;
goto complete;
}
signal_buffer *output_buffer = signal_buffer_create(md, len);
if(!output_buffer) {
result = SG_ERR_NOMEM;
goto complete;
}
*output = output_buffer;
complete:
return result;
}
void signal_vala_sha512_digest_cleanup(void *digest_context, void *user_data)
{
EVP_MD_CTX *ctx = digest_context;
EVP_MD_CTX_destroy(ctx);
}
int signal_vala_encrypt(signal_buffer **output,
int cipher,
const uint8_t *key, size_t key_len,
const uint8_t *iv, size_t iv_len,
const uint8_t *plaintext, size_t plaintext_len,
void *user_data)
{
int result = 0;
uint8_t *out_buf = 0;
const EVP_CIPHER *evp_cipher = aes_cipher(cipher, key_len);
if(!evp_cipher) {
fprintf(stderr, "invalid AES mode or key size: %zu\n", key_len);
return SG_ERR_UNKNOWN;
}
if(iv_len != 16) {
fprintf(stderr, "invalid AES IV size: %zu\n", iv_len);
return SG_ERR_UNKNOWN;
}
if(plaintext_len > INT_MAX - EVP_CIPHER_block_size(evp_cipher)) {
fprintf(stderr, "invalid plaintext length: %zu\n", plaintext_len);
return SG_ERR_UNKNOWN;
}
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int buf_extra = 0;
if(cipher == SG_CIPHER_AES_GCM_NOPADDING) {
// In GCM mode we use the last 16 bytes as auth tag
buf_extra += 16;
result = EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, NULL, NULL);
if(!result) {
fprintf(stderr, "cannot initialize cipher\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
result = EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL);
if(!result) {
fprintf(stderr, "cannot set iv size\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
result = EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv);
if(!result) {
fprintf(stderr, "cannot set key/iv\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
} else {
result = EVP_EncryptInit_ex(&ctx, evp_cipher, 0, key, iv);
if(!result) {
fprintf(stderr, "cannot initialize cipher\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
}
if(cipher == SG_CIPHER_AES_CTR_NOPADDING || cipher == SG_CIPHER_AES_GCM_NOPADDING) {
result = EVP_CIPHER_CTX_set_padding(&ctx, 0);
if(!result) {
fprintf(stderr, "cannot set padding\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
}
out_buf = malloc(sizeof(uint8_t) * (plaintext_len + EVP_CIPHER_block_size(evp_cipher) + buf_extra));
if(!out_buf) {
fprintf(stderr, "cannot allocate output buffer\n");
result = SG_ERR_NOMEM;
goto complete;
}
int out_len = 0;
result = EVP_EncryptUpdate(&ctx,
out_buf, &out_len, plaintext, plaintext_len);
if(!result) {
fprintf(stderr, "cannot encrypt plaintext\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
int final_len = 0;
result = EVP_EncryptFinal_ex(&ctx, out_buf + out_len, &final_len);
if(!result) {
fprintf(stderr, "cannot finish encrypting plaintext\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
if(cipher == SG_CIPHER_AES_GCM_NOPADDING) {
result = EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, 16, out_buf + (out_len + final_len));
if(!result) {
fprintf(stderr, "cannot get tag\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
}
*output = signal_buffer_create(out_buf, out_len + final_len + buf_extra);
complete:
EVP_CIPHER_CTX_cleanup(&ctx);
if(out_buf) {
free(out_buf);
}
return result;
}
int signal_vala_decrypt(signal_buffer **output,
int cipher,
const uint8_t *key, size_t key_len,
const uint8_t *iv, size_t iv_len,
const uint8_t *ciphertext, size_t ciphertext_len,
void *user_data)
{
int result = 0;
uint8_t *out_buf = 0;
const EVP_CIPHER *evp_cipher = aes_cipher(cipher, key_len);
if(!evp_cipher) {
fprintf(stderr, "invalid AES mode or key size: %zu\n", key_len);
return SG_ERR_INVAL;
}
if(iv_len != 16) {
fprintf(stderr, "invalid AES IV size: %zu\n", iv_len);
return SG_ERR_INVAL;
}
if(ciphertext_len > INT_MAX - EVP_CIPHER_block_size(evp_cipher)) {
fprintf(stderr, "invalid ciphertext length: %zu\n", ciphertext_len);
return SG_ERR_UNKNOWN;
}
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
if(cipher == SG_CIPHER_AES_GCM_NOPADDING) {
// In GCM mode we use the last 16 bytes as auth tag
ciphertext_len -= 16;
result = EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, NULL, NULL);
if(!result) {
fprintf(stderr, "cannot initialize cipher\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
result = EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL);
if(!result) {
fprintf(stderr, "cannot set iv size\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
result = EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv);
if(!result) {
fprintf(stderr, "cannot set key/iv\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
} else {
result = EVP_DecryptInit_ex(&ctx, evp_cipher, 0, key, iv);
if(!result) {
fprintf(stderr, "cannot initialize cipher\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
}
if(cipher == SG_CIPHER_AES_CTR_NOPADDING || cipher == SG_CIPHER_AES_GCM_NOPADDING) {
result = EVP_CIPHER_CTX_set_padding(&ctx, 0);
if(!result) {
fprintf(stderr, "cannot set padding\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
}
out_buf = malloc(sizeof(uint8_t) * (ciphertext_len + EVP_CIPHER_block_size(evp_cipher)));
if(!out_buf) {
fprintf(stderr, "cannot allocate output buffer\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
int out_len = 0;
result = EVP_DecryptUpdate(&ctx,
out_buf, &out_len, ciphertext, ciphertext_len);
if(!result) {
fprintf(stderr, "cannot decrypt ciphertext\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
if(cipher == SG_CIPHER_AES_GCM_NOPADDING) {
result = EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, 16, (uint8_t*)ciphertext + ciphertext_len);
if(!result) {
fprintf(stderr, "cannot set tag\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
}
int final_len = 0;
result = EVP_DecryptFinal_ex(&ctx, out_buf + out_len, &final_len);
if(!result) {
fprintf(stderr, "cannot finish decrypting ciphertexts\n");
result = SG_ERR_UNKNOWN;
goto complete;
}
*output = signal_buffer_create(out_buf, out_len + final_len);
complete:
EVP_CIPHER_CTX_cleanup(&ctx);
if(out_buf) {
free(out_buf);
}
return result;
}
void setup_signal_vala_crypto_provider(signal_context *context)
{
signal_crypto_provider provider = {
.random_func = signal_vala_random_generator,
.hmac_sha256_init_func = signal_vala_hmac_sha256_init,
.hmac_sha256_update_func = signal_vala_hmac_sha256_update,
.hmac_sha256_final_func = signal_vala_hmac_sha256_final,
.hmac_sha256_cleanup_func = signal_vala_hmac_sha256_cleanup,
.sha512_digest_init_func = signal_vala_sha512_digest_init,
.sha512_digest_update_func = signal_vala_sha512_digest_update,
.sha512_digest_final_func = signal_vala_sha512_digest_final,
.sha512_digest_cleanup_func = signal_vala_sha512_digest_cleanup,
.encrypt_func = signal_vala_encrypt,
.decrypt_func = signal_vala_decrypt,
.user_data = 0
};
signal_context_set_crypto_provider(context, &provider);
}
|