-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyer.c
509 lines (388 loc) · 15.1 KB
/
keyer.c
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
#include "keyer.h"
#include "core/record.h"
#include "lib/monocypher/monocypher.h"
#include "storage/filesystem_api_defines.h"
#include <furi.h>
#include <furi_hal_random.h>
#include <storage/storage.h>
// #include <toolbox/buffer_stream.h>
#include <toolbox/stream/stream.h>
#include <toolbox/stream/file_stream.h>
Identity keyer_identity_init(uint8_t* sk, uint8_t* pk) {
// Identity* identity = malloc(sizeof(Identity));
// identity->public_key = pk;
// identity->secret_key = sk;
// return identity;
return (Identity){.public_key = pk, .secret_key = sk};
}
// void keyer_identity_free(Identity* identity) {
// keyer_identity_clear(identity);
// free(identity);
// }
void keyer_identity_clear(Identity* idn) {
if(idn == NULL) return;
if(idn->secret_key != NULL) {
crypto_wipe(idn->secret_key, 32);
free(idn->secret_key);
}
if(idn->public_key != NULL) {
crypto_wipe(idn->public_key, 32);
free(idn->public_key);
}
idn->secret_key = NULL;
idn->public_key = NULL;
}
// bool keyer_generate_key_pair_and_save(char* name, Storage* storage) {
// UNUSED(storage);
// uint8_t sk[32];
// furi_hal_random_fill_buf(sk, 32);
// uint8_t pk[32];
// crypto_x25519_public_key(pk, sk);
// FuriString* sk_filepath = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
// FuriString* pk_filepath = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
// furi_string_cat_str(sk_filepath, "/");
// furi_string_cat_str(
// sk_filepath, name); // will be = to DATA PATH + name, so perfect time to make folder
// storage_common_mkdir(storage, furi_string_get_cstr(sk_filepath));
// furi_string_cat_str(sk_filepath, "/x25519");
// furi_string_cat_str(pk_filepath, "/");
// furi_string_cat_str(pk_filepath, name);
// furi_string_cat_str(pk_filepath, "/x25519_pub");
// File* file = storage_file_alloc(storage);
// if(!storage_file_open(file, furi_string_get_cstr(sk_filepath), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
// return false;
// }
// if(!storage_file_write(file, sk, 32)) {
// return false;
// }
// storage_file_close(file);
// storage_file_free(file);
// File* pub_file = storage_file_alloc(storage);
// if(!storage_file_open(
// pub_file, furi_string_get_cstr(pk_filepath), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
// return false;
// }
// if(!storage_file_write(pub_file, pk, 32)) {
// return false;
// }
// furi_string_free(sk_filepath);
// furi_string_free(pk_filepath);
// storage_file_close(pub_file);
// storage_file_free(pub_file);
// return true;
// }
Identity keyer_generate_keypair() {
// uint8_t sk[32];
uint8_t* sk = malloc(32 * sizeof(uint8_t));
furi_hal_random_fill_buf(sk, 32);
uint8_t* pk = malloc(32 * sizeof(uint8_t));
// uint8_t pk[32];
crypto_x25519_public_key(pk, sk);
return keyer_identity_init(sk, pk);
// return (Identity){.public_key = pk, .secret_key = sk};
}
// size_t string_to_bbuffer(uint8_t* buffer, char* str) {
// int loop;
// size_t i;
// loop = 0;
// i = 0;
// while(str[loop] != '\0') {
// buffer[i++] = str[loop++];
// }
// return i;
// }
size_t str_len(char* str) {
size_t i;
i = 0;
while(str[i] != '\0') {
i++;
}
return i;
}
bool keyer_save_identity(Identity* idn, Saves* saves, Storage* storage, char* name) {
if(idn == NULL) {
return false;
}
FuriString* filepath = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
furi_string_cat_str(filepath, "/");
furi_string_cat_str(filepath, name);
if(!storage_common_exists(storage, furi_string_get_cstr(filepath))) {
storage_common_mkdir(storage, furi_string_get_cstr(filepath));
// add to save memory
saves_add(saves, name);
// add to savee file
File* conf_file = storage_file_alloc(storage);
size_t o_name_len = str_len(name);
name[o_name_len] = '\n';
name[o_name_len + 1] = '\0'; // I don't need this as im just copying the bytes before it ?
if(!storage_file_open(conf_file, SAVED_FILE, FSAM_WRITE, FSOM_OPEN_APPEND) ||
!storage_file_write(conf_file, name, (o_name_len + 1) * sizeof(char))) {
return false;
}
name[o_name_len] = '\0';
storage_file_close(conf_file);
storage_file_free(conf_file);
}
furi_string_cat_str(filepath, "/x25519");
if(idn->secret_key != NULL) {
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, furi_string_get_cstr(filepath), FSAM_WRITE, FSOM_OPEN_ALWAYS) ||
!storage_file_write(file, idn->secret_key, 32)) {
furi_string_free(filepath);
return false;
}
storage_file_close(file);
storage_file_free(file);
}
// does fd need to be freed?
if(idn->public_key != NULL) {
furi_string_cat_str(filepath, "_pub");
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, furi_string_get_cstr(filepath), FSAM_WRITE, FSOM_OPEN_ALWAYS) ||
!storage_file_write(file, idn->public_key, 32)) {
furi_string_free(filepath);
return false;
}
storage_file_close(file);
storage_file_free(file);
}
furi_string_free(filepath);
return true;
}
Identity keyer_get_pub_identity(Storage* storage, const char* name) {
FuriString* filepath = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
furi_string_cat_str(filepath, "/");
furi_string_cat_str(filepath, name);
furi_string_cat_str(filepath, "/x25519_pub");
Identity idn = keyer_identity_init(NULL, NULL);
if(storage_file_exists(storage, furi_string_get_cstr(filepath))) { // pub key
File* pk_file = storage_file_alloc(storage);
if(storage_file_open(
pk_file, furi_string_get_cstr(filepath), FSAM_READ, FSOM_OPEN_EXISTING)) {
uint8_t* pk_buf = malloc(32 * sizeof(uint8_t));
if(!storage_file_read(pk_file, pk_buf, 32)) {
free(pk_buf);
pk_buf = NULL;
}
idn.public_key = pk_buf;
storage_file_close(pk_file);
}
storage_file_free(pk_file);
}
furi_string_free(filepath);
return idn;
}
Identity keyer_get_identity(Storage* storage, const char* name) {
// FuriString* filepath = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
FuriString* filepath = furi_string_alloc_set("/any/apps_data/keyhold/"); // bizare. the /data path only sometimes works. Fix this as this path may be obsoluted.
// furi_string_cat_str(filepath, "/");
furi_string_cat_str(filepath, name);
furi_string_cat_str(filepath, "/x25519");
FURI_LOG_D("keyhold", "%s", furi_string_get_cstr(filepath));
Identity idn = keyer_identity_init(NULL, NULL);
FURI_LOG_D("keyhold", "bruh0");
if(storage_file_exists(storage, furi_string_get_cstr(filepath))) { // sec key
FURI_LOG_D("keyhold", "bruh1");
File* sk_file = storage_file_alloc(storage);
if(storage_file_open(
sk_file, furi_string_get_cstr(filepath), FSAM_READ, FSOM_OPEN_EXISTING)) {
uint8_t* sk_buf = malloc(32 * sizeof(uint8_t));
FURI_LOG_D("keyhold", "bruh2");
if(!storage_file_read(sk_file, sk_buf, 32)) {
free(sk_buf);
sk_buf = NULL;
}
idn.secret_key = sk_buf;
storage_file_close(sk_file);
}
storage_file_free(sk_file);
}
furi_string_cat_str(filepath, "_pub");
// Duplicated code to prevent extra file existance check
if(storage_file_exists(storage, furi_string_get_cstr(filepath))) { // pub key
File* pk_file = storage_file_alloc(storage);
if(storage_file_open(
pk_file, furi_string_get_cstr(filepath), FSAM_READ, FSOM_OPEN_EXISTING)) {
uint8_t* pk_buf = malloc(32 * sizeof(uint8_t));
if(!storage_file_read(pk_file, pk_buf, 32)) {
free(pk_buf);
pk_buf = NULL;
}
idn.public_key = pk_buf;
storage_file_close(pk_file);
}
storage_file_free(pk_file);
}
furi_string_free(filepath);
return idn;
}
typedef struct EncryptorConfig {
Identity encrypt_as;
Identity encrypt_for;
uint8_t* plain_bytes;
/**
* encrypt_bytes is more like nonce + encrypted bytes + mac . size is size + sizeof(nonce) + sizeof(mac)
*/
uint8_t* encrypt_bytes;
size_t size; // size of cipher/encrypted parts of encrypt_bytes in "length" like [size]
} EncryptorConfig;
EncryptorConfig* encryptor_config_alloc() {
EncryptorConfig* ecf = malloc(sizeof(EncryptorConfig));
ecf->encrypt_as = keyer_identity_init(NULL, NULL);
ecf->encrypt_for = keyer_identity_init(NULL, NULL);
ecf->plain_bytes = NULL;
ecf->encrypt_bytes = NULL;
ecf->size = 0;
return ecf;
}
// make one for non str, MUST GO THROUGH THIS FOR THE MALLOC AND SIZE SET AND BUFFER SYNC
void encryptor_config_sync_pbuffer_str(EncryptorConfig* ecf, char* buf) {
ecf->plain_bytes = (uint8_t*)strdup(buf); // SIGNED TO UNSIGNED CAST
ecf->size = str_len((char*)ecf->plain_bytes);
ecf->encrypt_bytes = malloc((ecf->size + 24 + 16) * sizeof(uint8_t));
}
void encryptor_config_reset(EncryptorConfig* ecf) {
if(ecf == NULL) return;
if(ecf->plain_bytes != NULL) {
crypto_wipe(ecf->plain_bytes, ecf->size);
free(ecf->plain_bytes);
}
if(ecf->encrypt_bytes != NULL) {
crypto_wipe(ecf->encrypt_bytes, ecf->size + 24 + 16);
free(ecf->encrypt_bytes);
}
ecf->plain_bytes = NULL;
ecf->encrypt_bytes = NULL;
// keyer_identity_clear(&ecf->encrypt_as);
// keyer_identity_clear(&ecf->encrypt_for);
ecf->size = 0;
}
void encryptor_config_free(EncryptorConfig* ecf) {
encryptor_config_reset(ecf);
free(ecf);
}
uint8_t* encryptor_config_get_plain_buffer(EncryptorConfig* ecf) {
return ecf->plain_bytes;
}
void encryptor_config_set_as_identity(EncryptorConfig* ecf, Identity idn) {
ecf->encrypt_as = idn;
}
void encryptor_config_set_to_identity(EncryptorConfig* ecf, Identity idn) {
ecf->encrypt_for = idn;
}
uint8_t* encryptor_config_encrypt(EncryptorConfig* ecf) {
uint8_t* from_sk = ecf->encrypt_as.secret_key;
// uint8_t* from_pk = ecf->encrypt_as.public_key;
uint8_t* to_pk = ecf->encrypt_for.public_key;
size_t size = ecf->size;
uint8_t shared_secret[32];
crypto_x25519(shared_secret, from_sk, to_pk);
uint8_t shared_key[32];
crypto_chacha20_h(shared_key, shared_secret, (uint8_t[16]){0});
// encrypt-bytes is passed in as nonce as first 24 bytes is nonce
uint8_t* nonce = ecf->encrypt_bytes; // first 24 bytes
furi_hal_random_fill_buf(ecf->encrypt_bytes, 24); //nonce
uint8_t* enc_bytes = ecf->encrypt_bytes + 24; // between first and last 24 and 16 bytes
uint8_t* mac = ecf->encrypt_bytes + 24 + size; // last 16 bytes
crypto_aead_lock(
enc_bytes, // encryption part
mac, // mac
shared_key,
nonce, // nonce
NULL,
0,
ecf->plain_bytes,
size);
// nonce cipher mac
// memcpy(ecf->encrypt_bytes, nonce, 24);
// memcpy(ecf->encrypt_bytes + 24, enc_bytes, size);
// memcpy(ecf->encrypt_bytes + 24 + size, mac, 16);
// nonce does not need to be wiped, let it fall off stack
// enc_bytes does not need to be wiped, let it fall off stack
// mac might need to be wiped
crypto_wipe(ecf->plain_bytes, size);
ecf->plain_bytes = NULL;
crypto_wipe(shared_secret, 32);
crypto_wipe(shared_key, 32);
// keyer_identity_clear(&ecf->encrypt_as);
// keyer_identity_clear(&ecf->encrypt_for);
return ecf->encrypt_bytes;
}
size_t encryptor_config_get_size(EncryptorConfig* ecf) {
return ecf->size;
}
/**
* sets to decryption mode. initializes the buffers and returns the encryption buffer. updates size.
*/
uint8_t* encryptor_config_encrypted_bytes_buf_init(EncryptorConfig* ecf, size_t size) {
ecf->encrypt_bytes = malloc(size); // might have to be * sizeof(uint8_t)
ecf->plain_bytes = malloc(size);
ecf->size = size - (24 + 16);
return ecf->encrypt_bytes;
}
/**
* Size should be full message size, after this it will just be size of deciphered part
*/
uint8_t* encryptor_config_decrypt(EncryptorConfig* ecf) {
// size should be of whole thing (nonce mac included)
uint8_t* sender_pk =
ecf->encrypt_as.public_key; // keep in mind for future the anonymous feature
uint8_t* our_sk = ecf->encrypt_for.secret_key;
uint8_t shared_secret[32];
crypto_x25519(shared_secret, our_sk, sender_pk);
uint8_t shared_key[32];
crypto_chacha20_h(shared_key, shared_secret, (uint8_t[16]){0});
if(crypto_aead_unlock(
ecf->plain_bytes,
ecf->encrypt_bytes + ecf->size + 24,
shared_key,
ecf->encrypt_bytes,
NULL,
0,
ecf->encrypt_bytes + 24,
ecf->size)) {
crypto_wipe(shared_secret, 32);
crypto_wipe(shared_key, 32);
return NULL;
}
crypto_wipe(shared_secret, 32);
crypto_wipe(shared_key, 32);
// keyer_identity_clear(&ecf->encrypt_for); // this could be a pointer to encrypted keys.
// keyer_identity_clear(&ecf->encrypt_as);
free(ecf->encrypt_bytes);
ecf->encrypt_bytes = NULL;
return ecf->plain_bytes;
}
void keyer_encrypt_x25509_key_file_chacha20(Storage* storage, uint8_t* encryptor_key, const char* save_name, uint8_t* save_sk_key) {
// uint8_t nonce[24] = {0}; // LOOK INTO THIS TODO REMEMBER. IS SAME NONCE OK FOR SAME MESSAGE?
// furi_hal_random_fill_buf(nonce, 24);
uint8_t* nonce = encryptor_key; // overlapping nonce with encryptor_key, should use first 24 bytes.
uint8_t ciphered_sk_key[32];
crypto_chacha20_x(ciphered_sk_key, save_sk_key, 32, encryptor_key, nonce, 0);
FuriString* filepath = furi_string_alloc_set("/any/apps_data/keyhold/");
// furi_string_cat_str(filepath, "/");
furi_string_cat_str(filepath, save_name);
furi_string_cat_str(filepath, "/x25519");
File* sk_file = storage_file_alloc(storage);
storage_file_open(sk_file, furi_string_get_cstr(filepath), FSAM_WRITE, FSOM_OPEN_ALWAYS);
storage_file_write(sk_file, ciphered_sk_key, 32);
storage_file_close(sk_file);
storage_file_free(sk_file);
furi_string_free(filepath);
}
/**
* Checks if we have our private key in our record containing plain text private keys.
* If we don't, then we use the normal file reading method.
* This should not be used for Identities who we are non owned (no private key), or we don't need a private key.
* If this is the case, then use keyer_get_pub_identity.
*
* size_t idx: index associated with the name of the identity.
*/
Identity keyer_get_correct_identity(Storage* storage, uint8_t** encrypted_keys, const char* name, size_t idx) {
if (encrypted_keys == NULL || encrypted_keys[idx] == NULL) {
return keyer_get_identity(storage, name);
}
Identity toReturn = keyer_get_pub_identity(storage, name);
toReturn.secret_key = encrypted_keys[idx];
return toReturn;
}