-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathprotected_files.c
1299 lines (1053 loc) · 44.4 KB
/
protected_files.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2020 Invisible Things Lab
* Rafal Wojdyla <[email protected]>
* Copyright (C) 2019 Intel Corporation
*/
#include "protected_files.h"
#include "protected_files_format.h"
#include "protected_files_internal.h"
#include "api.h"
/* Host callbacks */
static pf_read_f g_cb_read = NULL;
static pf_write_f g_cb_write = NULL;
static pf_fsync_f g_cb_fsync = NULL;
static pf_truncate_f g_cb_truncate = NULL;
static pf_debug_f g_cb_debug = NULL;
static pf_aes_cmac_f g_cb_aes_cmac = NULL;
static pf_aes_gcm_encrypt_f g_cb_aes_gcm_encrypt = NULL;
static pf_aes_gcm_decrypt_f g_cb_aes_gcm_decrypt = NULL;
static pf_random_f g_cb_random = NULL;
#ifdef DEBUG
#define PF_DEBUG_PRINT_SIZE_MAX 4096
/* Debug print with function name prefix. Implicit param: pf (context pointer). */
#define DEBUG_PF(format, ...) \
do { \
if (g_cb_debug) { \
snprintf(pf->debug_buffer, PF_DEBUG_PRINT_SIZE_MAX, "%s: " format, __FUNCTION__, \
##__VA_ARGS__); \
g_cb_debug(pf->debug_buffer); \
} \
} while (0)
#else /* DEBUG */
#define DEBUG_PF(...)
#endif /* DEBUG */
static pf_iv_t g_empty_iv = {0};
static bool g_initialized = false;
static const char* g_pf_error_list[] = {
[PF_STATUS_SUCCESS] = "Success",
[-PF_STATUS_UNKNOWN_ERROR] = "Unknown error",
[-PF_STATUS_UNINITIALIZED] = "Protected Files uninitialized",
[-PF_STATUS_INVALID_PARAMETER] = "Invalid parameter",
[-PF_STATUS_INVALID_MODE] = "Invalid mode",
[-PF_STATUS_NO_MEMORY] = "Not enough memory",
[-PF_STATUS_INVALID_VERSION] = "Invalid version",
[-PF_STATUS_INVALID_HEADER] = "Invalid header",
[-PF_STATUS_INVALID_PATH] = "Invalid path",
[-PF_STATUS_MAC_MISMATCH] = "MAC mismatch",
[-PF_STATUS_NOT_IMPLEMENTED] = "Functionality not implemented",
[-PF_STATUS_CALLBACK_FAILED] = "Callback failed",
[-PF_STATUS_PATH_TOO_LONG] = "Path is too long",
[-PF_STATUS_RECOVERY_NEEDED] = "File recovery needed",
[-PF_STATUS_FLUSH_ERROR] = "Flush error",
[-PF_STATUS_CRYPTO_ERROR] = "Crypto error",
[-PF_STATUS_CORRUPTED] = "File is corrupted",
[-PF_STATUS_WRITE_TO_DISK_FAILED] = "Write to disk failed",
};
/* ipf prefix means "Intel protected files", these are functions from the SGX SDK implementation */
static file_node_t* ipf_append_mht_node(pf_context_t* pf, uint64_t logical_mht_node_number);
static file_node_t* ipf_append_data_node(pf_context_t* pf, uint64_t offset);
static file_node_t* ipf_read_mht_node(pf_context_t* pf, uint64_t logical_mht_node_number);
static file_node_t* ipf_read_data_node(pf_context_t* pf, uint64_t offset);
static void memcpy_or_zero_initialize(void* dest, const void* src, size_t size) {
if (src)
memcpy(dest, src, size);
else
memset(dest, 0, size);
}
const char* pf_strerror(int err) {
unsigned err_idx = err >= 0 ? err : -err;
if (err_idx >= ARRAY_SIZE(g_pf_error_list) || !g_pf_error_list[err_idx]) {
return "Unknown error";
}
return g_pf_error_list[err_idx];
}
static void swap_nodes(file_node_t** data, size_t idx1, size_t idx2) {
file_node_t* tmp = data[idx1];
data[idx1] = data[idx2];
data[idx2] = tmp;
}
// TODO: better sort?
static size_t partition(file_node_t** data, size_t low, size_t high) {
assert(low <= high);
file_node_t* pivot = data[(low + high) / 2];
size_t i = low;
size_t j = high;
while (true) {
while (data[i]->logical_node_number < pivot->logical_node_number)
i++;
while (data[j]->logical_node_number > pivot->logical_node_number)
j--;
if (i >= j)
return j;
swap_nodes(data, i, j);
i++;
j--;
}
}
static void sort_nodes(file_node_t** data, size_t low, size_t high) {
if (high - low == 1) {
if (data[low]->logical_node_number > data[high]->logical_node_number)
swap_nodes(data, low, high);
return;
}
if (low < high) {
size_t pi = partition(data, low, high);
if (pi > 0)
sort_nodes(data, low, pi);
sort_nodes(data, pi + 1, high);
}
}
static bool ipf_generate_random_key(pf_context_t* pf, pf_key_t* output) {
pf_status_t status = g_cb_random((uint8_t*)output, sizeof(*output));
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
return true;
}
// The key derivation function follow recommendations from NIST Special Publication 800-108:
// Recommendation for Key Derivation Using Pseudorandom Functions
// https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-108.pdf
//
// This function derives a metadata key in two modes:
// - restore == false: derives a per-file random key from user_kdk_key using a random nonce, to
// encrypt the metadata node of the protected file; the nonce is stored in
// plaintext part of the metadata node so that the file can be loaded later
// and decrypted using the same key
// - restore == true: derives a key from user_kdk_key + nonce stored in plaintext part of the
// metadata node, to decrypt the encrypted part of the metadata node (and
// thus "restore" access to the whole protected file)
static bool ipf_generate_metadata_key(pf_context_t* pf, bool restore, pf_key_t* output) {
kdf_input_t buf = {0};
pf_status_t status;
buf.counter = 1;
if (!strcpy_static(buf.label, METADATA_KEY_NAME, MAX_LABEL_SIZE))
return false;
if (!restore) {
status = g_cb_random((uint8_t*)&buf.nonce, sizeof(buf.nonce));
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
} else {
COPY_ARRAY(buf.nonce, pf->metadata_node.plaintext_part.metadata_key_nonce);
}
// length of output (128 bits)
buf.output_len = 0x80;
status = g_cb_aes_cmac(&pf->user_kdk_key, &buf, sizeof(buf), output);
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
if (!restore) {
COPY_ARRAY(pf->metadata_node.plaintext_part.metadata_key_nonce, buf.nonce);
}
erase_memory(&buf, sizeof(buf));
return true;
}
static bool ipf_generate_random_metadata_key(pf_context_t* pf, pf_key_t* output) {
return ipf_generate_metadata_key(pf, /*restore=*/false, output);
}
static bool ipf_recreate_metadata_key(pf_context_t* pf, pf_key_t* output) {
return ipf_generate_metadata_key(pf, /*restore=*/true, output);
}
static void ipf_init_root_mht(file_node_t* mht) {
memset(mht, 0, sizeof(*mht));
mht->type = FILE_MHT_NODE_TYPE;
mht->physical_node_number = 1;
mht->logical_node_number = 0;
mht->need_writing = false;
}
static bool ipf_update_all_data_and_mht_nodes(pf_context_t* pf) {
bool ret = false;
file_node_t** mht_array = NULL;
pf_status_t status;
// 1. encrypt the changed data nodes
// 2. set the key + MAC in the parent MHT nodes
// 3. set the need_writing flag for all the parent MHT nodes
for (void* node = lruc_get_first(pf->cache); node != NULL; node = lruc_get_next(pf->cache)) {
if (((file_node_t*)node)->type != FILE_DATA_NODE_TYPE)
continue;
file_node_t* data_node = (file_node_t*)node;
if (!data_node->need_writing)
continue;
gcm_crypto_data_t* gcm_crypto_data = &data_node->parent->decrypted.mht
.data_nodes_crypto[data_node->logical_node_number % ATTACHED_DATA_NODES_COUNT];
if (!ipf_generate_random_key(pf, &gcm_crypto_data->key))
goto out;
// encrypt data node, this also saves MAC in the corresponding array item of MHT node
status = g_cb_aes_gcm_encrypt(&gcm_crypto_data->key, &g_empty_iv, NULL, 0, // aad
data_node->decrypted.data.bytes, PF_NODE_SIZE,
data_node->encrypted.bytes, &gcm_crypto_data->mac);
if (PF_FAILURE(status)) {
pf->last_error = status;
goto out;
}
#ifdef DEBUG
file_node_t* file_mht_node = data_node->parent;
while (file_mht_node->logical_node_number != 0) {
assert(file_mht_node->need_writing == true);
file_mht_node = file_mht_node->parent;
}
#endif
}
// count dirty MHT nodes
size_t dirty_count = 0;
for (void* node = lruc_get_first(pf->cache); node != NULL; node = lruc_get_next(pf->cache)) {
if (((file_node_t*)node)->type == FILE_MHT_NODE_TYPE) {
if (((file_node_t*)node)->need_writing)
dirty_count++;
}
}
// add all the MHT nodes that needs writing to a list
mht_array = malloc(dirty_count * sizeof(*mht_array));
if (!mht_array) {
pf->last_error = PF_STATUS_NO_MEMORY;
goto out;
}
uint64_t dirty_idx = 0;
for (void* node = lruc_get_first(pf->cache); node != NULL; node = lruc_get_next(pf->cache)) {
if (((file_node_t*)node)->type == FILE_MHT_NODE_TYPE) {
file_node_t* file_mht_node = (file_node_t*)node;
if (file_mht_node->need_writing)
mht_array[dirty_idx++] = file_mht_node;
}
}
if (dirty_count > 0)
sort_nodes(mht_array, 0, dirty_count - 1);
// update the keys and MACs in the parents from last node to first (bottom layers first)
for (dirty_idx = dirty_count; dirty_idx > 0; dirty_idx--) {
file_node_t* file_mht_node = mht_array[dirty_idx - 1];
gcm_crypto_data_t* gcm_crypto_data =
&file_mht_node->parent->decrypted.mht
.mht_nodes_crypto[(file_mht_node->logical_node_number - 1) % CHILD_MHT_NODES_COUNT];
if (!ipf_generate_random_key(pf, &gcm_crypto_data->key))
goto out;
status = g_cb_aes_gcm_encrypt(&gcm_crypto_data->key, &g_empty_iv, NULL, 0,
&file_mht_node->decrypted.mht, PF_NODE_SIZE,
&file_mht_node->encrypted.bytes, &gcm_crypto_data->mac);
if (PF_FAILURE(status)) {
pf->last_error = status;
goto out;
}
}
// update root MHT node's key and MAC in the metadata node's headers
if (!ipf_generate_random_key(pf, &pf->metadata_decrypted.root_mht_node_key))
goto out;
status = g_cb_aes_gcm_encrypt(&pf->metadata_decrypted.root_mht_node_key, &g_empty_iv,
NULL, 0,
&pf->root_mht_node.decrypted.mht, PF_NODE_SIZE,
&pf->root_mht_node.encrypted.bytes,
&pf->metadata_decrypted.root_mht_node_mac);
if (PF_FAILURE(status)) {
pf->last_error = status;
goto out;
}
ret = true;
out:
free(mht_array);
return ret;
}
static bool ipf_read_node(pf_context_t* pf, uint64_t physical_node_number, void* buffer) {
uint64_t offset = physical_node_number * PF_NODE_SIZE;
pf_status_t status = g_cb_read(pf->host_file_handle, buffer, offset, PF_NODE_SIZE);
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
return true;
}
static bool ipf_write_node(pf_context_t* pf, uint64_t physical_node_number, void* buffer) {
uint64_t offset = physical_node_number * PF_NODE_SIZE;
pf_status_t status = g_cb_write(pf->host_file_handle, buffer, offset, PF_NODE_SIZE);
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
return true;
}
// this is a very 'specific' function, tied to the architecture of the file layout,
// returning the node numbers according to the data offset in the file
static void get_node_numbers(uint64_t offset, uint64_t* logical_mht_node_number,
uint64_t* logical_data_node_number,
uint64_t* physical_mht_node_number,
uint64_t* physical_data_node_number) {
// physical nodes (file layout):
// node 0 - metadata node
// node 1 - root MHT node
// nodes 2-97 - data nodes (ATTACHED_DATA_NODES_COUNT == 96)
// node 98 - MHT node
// node 99-195 - data nodes
// etc.
uint64_t _physical_mht_node_number;
uint64_t _physical_data_node_number;
// "logical" nodes: sequential index of the corresponding MHT/data node in all MHT/data nodes
uint64_t _logical_mht_node_number;
uint64_t _logical_data_node_number;
assert(offset >= MD_USER_DATA_SIZE);
_logical_data_node_number = (offset - MD_USER_DATA_SIZE) / PF_NODE_SIZE;
_logical_mht_node_number = _logical_data_node_number / ATTACHED_DATA_NODES_COUNT;
_physical_data_node_number = _logical_data_node_number
+ 1 // metadata node
+ 1 // MHT root node
+ _logical_mht_node_number; // number of MHT nodes in the middle
// (the mht_node_number of root MHT node is 0)
_physical_mht_node_number = _physical_data_node_number
- _logical_data_node_number % ATTACHED_DATA_NODES_COUNT
// now we are at the first data node attached to this MHT node
- 1; // and now at the MHT node itself
if (logical_mht_node_number != NULL)
*logical_mht_node_number = _logical_mht_node_number;
if (logical_data_node_number != NULL)
*logical_data_node_number = _logical_data_node_number;
if (physical_mht_node_number != NULL)
*physical_mht_node_number = _physical_mht_node_number;
if (physical_data_node_number != NULL)
*physical_data_node_number = _physical_data_node_number;
}
static bool ipf_write_all_changes_to_disk(pf_context_t* pf) {
if (pf->metadata_decrypted.file_size > MD_USER_DATA_SIZE && pf->root_mht_node.need_writing) {
uint64_t physical_node_number;
void* node;
for (node = lruc_get_first(pf->cache); node != NULL; node = lruc_get_next(pf->cache)) {
file_node_t* file_node = (file_node_t*)node;
if (!file_node->need_writing)
continue;
uint8_t* data_to_write = (uint8_t*)&file_node->encrypted;
physical_node_number = file_node->physical_node_number;
if (!ipf_write_node(pf, physical_node_number, data_to_write))
return false;
file_node->need_writing = false;
}
if (!ipf_write_node(pf, /*physical_node_number=*/1, &pf->root_mht_node.encrypted))
return false;
pf->root_mht_node.need_writing = false;
}
if (!ipf_write_node(pf, /*physical_node_number=*/0, &pf->metadata_node))
return false;
return true;
}
static bool ipf_update_metadata_node(pf_context_t* pf) {
pf_status_t status;
pf_key_t key;
// new key for metadata node encryption, saves the key nonce in metadata plaintext header
if (!ipf_generate_random_metadata_key(pf, &key)) {
// last error already set
return false;
}
// encrypt metadata part-to-be-encrypted, also updating the MAC in metadata plaintext header
status = g_cb_aes_gcm_encrypt(&key, &g_empty_iv, NULL, 0, &pf->metadata_decrypted,
sizeof(metadata_decrypted_t), &pf->metadata_node.encrypted_part,
&pf->metadata_node.plaintext_part.metadata_mac);
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
return true;
}
static bool ipf_internal_flush(pf_context_t* pf) {
if (!pf->need_writing) {
DEBUG_PF("no need to write");
return true;
}
if (pf->metadata_decrypted.file_size > MD_USER_DATA_SIZE && pf->root_mht_node.need_writing) {
if (!ipf_update_all_data_and_mht_nodes(pf)) {
// this is something that shouldn't happen, can't fix this...
pf->file_status = PF_STATUS_CRYPTO_ERROR;
DEBUG_PF("failed to update data and MHT nodes");
return false;
}
}
if (!ipf_update_metadata_node(pf)) {
// this is something that shouldn't happen, can't fix this...
pf->file_status = PF_STATUS_CRYPTO_ERROR;
DEBUG_PF("failed to update metadata node");
return false;
}
if (!ipf_write_all_changes_to_disk(pf)) {
pf->file_status = PF_STATUS_WRITE_TO_DISK_FAILED;
DEBUG_PF("failed to write changes to disk");
return false;
}
pf->need_writing = false;
return true;
}
static file_node_t* ipf_get_mht_node(pf_context_t* pf, uint64_t offset) {
file_node_t* file_mht_node;
uint64_t logical_mht_node_number;
uint64_t physical_mht_node_number;
if (offset < MD_USER_DATA_SIZE) {
pf->last_error = PF_STATUS_UNKNOWN_ERROR;
return NULL;
}
get_node_numbers(offset, &logical_mht_node_number, NULL, &physical_mht_node_number, NULL);
if (logical_mht_node_number == 0)
return &pf->root_mht_node;
if ((offset - MD_USER_DATA_SIZE) % (ATTACHED_DATA_NODES_COUNT * PF_NODE_SIZE) == 0 &&
offset == pf->metadata_decrypted.file_size) {
file_mht_node = ipf_append_mht_node(pf, logical_mht_node_number);
} else {
file_mht_node = ipf_read_mht_node(pf, logical_mht_node_number);
}
return file_mht_node;
}
static file_node_t* ipf_append_mht_node(pf_context_t* pf, uint64_t logical_mht_node_number) {
assert(logical_mht_node_number > 0);
file_node_t* parent_file_mht_node =
ipf_read_mht_node(pf, (logical_mht_node_number - 1) / CHILD_MHT_NODES_COUNT);
if (parent_file_mht_node == NULL)
return NULL;
uint64_t physical_node_number = 1 + // metadata node
// the '1' is for the MHT node preceding every 96 data nodes
logical_mht_node_number * (1 + ATTACHED_DATA_NODES_COUNT);
file_node_t* new_file_mht_node = calloc(1, sizeof(*new_file_mht_node));
if (!new_file_mht_node) {
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
new_file_mht_node->type = FILE_MHT_NODE_TYPE;
new_file_mht_node->parent = parent_file_mht_node;
new_file_mht_node->logical_node_number = logical_mht_node_number;
new_file_mht_node->physical_node_number = physical_node_number;
if (!lruc_add(pf->cache, new_file_mht_node->physical_node_number, new_file_mht_node)) {
free(new_file_mht_node);
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
return new_file_mht_node;
}
static file_node_t* ipf_get_data_node(pf_context_t* pf, uint64_t offset) {
file_node_t* file_data_node = NULL;
if (offset < MD_USER_DATA_SIZE) {
pf->last_error = PF_STATUS_UNKNOWN_ERROR;
return NULL;
}
if ((offset - MD_USER_DATA_SIZE) % PF_NODE_SIZE == 0
&& offset == pf->metadata_decrypted.file_size) {
file_data_node = ipf_append_data_node(pf, offset);
} else {
file_data_node = ipf_read_data_node(pf, offset);
}
// bump all the parent MHT nodes to reside before the data node in the cache
if (file_data_node != NULL) {
file_node_t* file_mht_node = file_data_node->parent;
while (file_mht_node->logical_node_number != 0) {
// bump the MHT node to the head of the LRU
lruc_get(pf->cache, file_mht_node->physical_node_number);
file_mht_node = file_mht_node->parent;
}
}
// even if we didn't get the required data_node, we might have read other nodes in the process
while (lruc_size(pf->cache) > MAX_NODES_IN_CACHE) {
void* node = lruc_get_last(pf->cache);
assert(node);
if (!((file_node_t*)node)->need_writing) {
lruc_remove_last(pf->cache);
// before deleting the memory, need to scrub the plain secrets
file_node_t* file_node = (file_node_t*)node;
erase_memory(&file_node->decrypted, sizeof(file_node->decrypted));
free(file_node);
} else {
if (!ipf_internal_flush(pf)) {
// error, can't flush cache, file status changed to error
assert(pf->file_status != PF_STATUS_SUCCESS);
if (pf->file_status == PF_STATUS_SUCCESS)
pf->file_status = PF_STATUS_FLUSH_ERROR; // for release set this anyway
return NULL; // even if we got the data_node!
}
}
}
return file_data_node;
}
static file_node_t* ipf_append_data_node(pf_context_t* pf, uint64_t offset) {
file_node_t* file_mht_node = ipf_get_mht_node(pf, offset);
if (file_mht_node == NULL)
return NULL;
file_node_t* new_file_data_node = calloc(1, sizeof(*new_file_data_node));
if (!new_file_data_node) {
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
uint64_t logical_node_number, physical_node_number;
get_node_numbers(offset, NULL, &logical_node_number, NULL, &physical_node_number);
new_file_data_node->type = FILE_DATA_NODE_TYPE;
new_file_data_node->parent = file_mht_node;
new_file_data_node->logical_node_number = logical_node_number;
new_file_data_node->physical_node_number = physical_node_number;
if (!lruc_add(pf->cache, new_file_data_node->physical_node_number, new_file_data_node)) {
free(new_file_data_node);
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
return new_file_data_node;
}
static file_node_t* ipf_read_data_node(pf_context_t* pf, uint64_t offset) {
file_node_t* file_mht_node;
pf_status_t status;
uint64_t logical_data_node_number;
uint64_t physical_node_number;
get_node_numbers(offset, NULL, &logical_data_node_number, NULL, &physical_node_number);
file_node_t* file_data_node = (file_node_t*)lruc_get(pf->cache, physical_node_number);
if (file_data_node != NULL)
return file_data_node;
// need to read the data node from the disk
file_mht_node = ipf_get_mht_node(pf, offset);
if (file_mht_node == NULL)
return NULL;
file_data_node = calloc(1, sizeof(*file_data_node));
if (!file_data_node) {
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
file_data_node->type = FILE_DATA_NODE_TYPE;
file_data_node->parent = file_mht_node;
file_data_node->logical_node_number = logical_data_node_number;
file_data_node->physical_node_number = physical_node_number;
if (!ipf_read_node(pf, file_data_node->physical_node_number,
file_data_node->encrypted.bytes)) {
free(file_data_node);
return NULL;
}
gcm_crypto_data_t* gcm_crypto_data =
&file_data_node->parent->decrypted.mht
.data_nodes_crypto[file_data_node->logical_node_number % ATTACHED_DATA_NODES_COUNT];
// decrypt data and check integrity against the MAC in corresponding array item in MHT node
status = g_cb_aes_gcm_decrypt(&gcm_crypto_data->key, &g_empty_iv, NULL, 0,
file_data_node->encrypted.bytes, PF_NODE_SIZE,
file_data_node->decrypted.data.bytes, &gcm_crypto_data->mac);
if (PF_FAILURE(status)) {
free(file_data_node);
pf->last_error = status;
if (status == PF_STATUS_MAC_MISMATCH)
pf->file_status = PF_STATUS_CORRUPTED;
return NULL;
}
if (!lruc_add(pf->cache, file_data_node->physical_node_number, file_data_node)) {
erase_memory(&file_data_node->decrypted, sizeof(file_data_node->decrypted));
free(file_data_node);
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
return file_data_node;
}
static file_node_t* ipf_read_mht_node(pf_context_t* pf, uint64_t logical_mht_node_number) {
pf_status_t status;
if (logical_mht_node_number == 0)
return &pf->root_mht_node;
uint64_t physical_node_number = 1 + // metadata node
// the '1' is for the MHT node preceding every 96 data nodes
logical_mht_node_number * (1 + ATTACHED_DATA_NODES_COUNT);
file_node_t* file_mht_node = (file_node_t*)lruc_find(pf->cache, physical_node_number);
if (file_mht_node != NULL)
return file_mht_node;
file_node_t* parent_file_mht_node =
ipf_read_mht_node(pf, (logical_mht_node_number - 1) / CHILD_MHT_NODES_COUNT);
if (parent_file_mht_node == NULL)
return NULL;
file_mht_node = calloc(1, sizeof(*file_mht_node));
if (!file_mht_node) {
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
file_mht_node->type = FILE_MHT_NODE_TYPE;
file_mht_node->parent = parent_file_mht_node;
file_mht_node->logical_node_number = logical_mht_node_number;
file_mht_node->physical_node_number = physical_node_number;
if (!ipf_read_node(pf, file_mht_node->physical_node_number, file_mht_node->encrypted.bytes)) {
free(file_mht_node);
return NULL;
}
gcm_crypto_data_t* gcm_crypto_data =
&file_mht_node->parent->decrypted.mht
.mht_nodes_crypto[(file_mht_node->logical_node_number - 1) % CHILD_MHT_NODES_COUNT];
// decrypt data and check integrity against the MAC in corresponding array item in parent MHT
// node
status = g_cb_aes_gcm_decrypt(&gcm_crypto_data->key, &g_empty_iv, NULL, 0,
file_mht_node->encrypted.bytes, PF_NODE_SIZE,
&file_mht_node->decrypted.mht, &gcm_crypto_data->mac);
if (PF_FAILURE(status)) {
free(file_mht_node);
pf->last_error = status;
if (status == PF_STATUS_MAC_MISMATCH)
pf->file_status = PF_STATUS_CORRUPTED;
return NULL;
}
if (!lruc_add(pf->cache, file_mht_node->physical_node_number, file_mht_node)) {
erase_memory(&file_mht_node->decrypted, sizeof(file_mht_node->decrypted));
free(file_mht_node);
pf->last_error = PF_STATUS_NO_MEMORY;
return NULL;
}
return file_mht_node;
}
static bool ipf_init_new_file(pf_context_t* pf, const char* path) {
pf->metadata_node.plaintext_part.file_id = PF_FILE_ID;
pf->metadata_node.plaintext_part.major_version = PF_MAJOR_VERSION;
pf->metadata_node.plaintext_part.minor_version = PF_MINOR_VERSION;
// path length is checked in ipf_open()
memcpy(pf->metadata_decrypted.file_path, path, strlen(path) + 1);
pf->need_writing = true;
if (!ipf_internal_flush(pf))
return false;
return true;
}
static bool ipf_init_fields(pf_context_t* pf) {
#ifdef DEBUG
pf->debug_buffer = malloc(PF_DEBUG_PRINT_SIZE_MAX);
if (!pf->debug_buffer) {
pf->last_error = PF_STATUS_NO_MEMORY;
return false;
}
#endif
memset(&pf->metadata_node, 0, sizeof(pf->metadata_node));
memset(&pf->metadata_decrypted, 0, sizeof(pf->metadata_decrypted));
memset(&g_empty_iv, 0, sizeof(g_empty_iv));
ipf_init_root_mht(&pf->root_mht_node);
pf->host_file_handle = NULL;
pf->need_writing = false;
pf->file_status = PF_STATUS_UNINITIALIZED;
pf->last_error = PF_STATUS_SUCCESS;
pf->cache = lruc_create();
return true;
}
static bool ipf_init_existing_file(pf_context_t* pf, const char* path) {
pf_status_t status;
// read metadata node
if (!ipf_read_node(pf, /*physical_node_number=*/0, (uint8_t*)&pf->metadata_node)) {
return false;
}
if (pf->metadata_node.plaintext_part.file_id != PF_FILE_ID) {
// such a file exists, but it is not a protected file
pf->last_error = PF_STATUS_INVALID_HEADER;
return false;
}
if (pf->metadata_node.plaintext_part.major_version != PF_MAJOR_VERSION) {
pf->last_error = PF_STATUS_INVALID_VERSION;
return false;
}
pf_key_t key;
if (!ipf_recreate_metadata_key(pf, &key))
return false;
// decrypt the encrypted part of the metadata node
status = g_cb_aes_gcm_decrypt(&key, &g_empty_iv, NULL, 0,
&pf->metadata_node.encrypted_part,
sizeof(pf->metadata_node.encrypted_part),
&pf->metadata_decrypted,
&pf->metadata_node.plaintext_part.metadata_mac);
if (PF_FAILURE(status)) {
pf->last_error = status;
DEBUG_PF("failed to decrypt metadata: %d", status);
return false;
}
DEBUG_PF("data size %lu", pf->metadata_decrypted.file_size);
if (path) {
size_t path_len = strlen(pf->metadata_decrypted.file_path);
if (path_len != strlen(path)
|| memcmp(path, pf->metadata_decrypted.file_path, path_len) != 0) {
pf->last_error = PF_STATUS_INVALID_PATH;
return false;
}
}
if (pf->metadata_decrypted.file_size > MD_USER_DATA_SIZE) {
// read the root MHT node
if (!ipf_read_node(pf, /*physical_node_number=*/1, &pf->root_mht_node.encrypted.bytes))
return false;
// also verifies root MHT node's MAC against the MAC in metadata node's decrypted header
status = g_cb_aes_gcm_decrypt(&pf->metadata_decrypted.root_mht_node_key, &g_empty_iv,
NULL, 0, // aad
&pf->root_mht_node.encrypted.bytes, PF_NODE_SIZE,
&pf->root_mht_node.decrypted.mht,
&pf->metadata_decrypted.root_mht_node_mac);
if (PF_FAILURE(status)) {
pf->last_error = status;
return false;
}
}
return true;
}
static void ipf_try_clear_error(pf_context_t* pf) {
if (pf->file_status == PF_STATUS_UNINITIALIZED ||
pf->file_status == PF_STATUS_CRYPTO_ERROR ||
pf->file_status == PF_STATUS_CORRUPTED) {
// can't fix these...
DEBUG_PF("Unrecoverable file status: %d", pf->file_status);
return;
}
if (pf->file_status == PF_STATUS_FLUSH_ERROR) {
if (ipf_internal_flush(pf))
pf->file_status = PF_STATUS_SUCCESS;
}
if (pf->file_status == PF_STATUS_WRITE_TO_DISK_FAILED) {
if (ipf_write_all_changes_to_disk(pf)) {
pf->need_writing = false;
pf->file_status = PF_STATUS_SUCCESS;
}
}
if (pf->file_status == PF_STATUS_SUCCESS) {
pf->last_error = PF_STATUS_SUCCESS;
}
}
static pf_context_t* ipf_open(const char* path, pf_file_mode_t mode, bool create, pf_handle_t file,
uint64_t real_size, const pf_key_t* kdk_key, pf_status_t* status) {
*status = PF_STATUS_NO_MEMORY;
pf_context_t* pf = calloc(1, sizeof(*pf));
if (!pf)
goto out;
if (!ipf_init_fields(pf))
goto out;
DEBUG_PF("handle: %d, path: '%s', real size: %lu, mode: 0x%x", *(int*)file, path, real_size,
mode);
if (kdk_key == NULL) {
DEBUG_PF("no key specified");
pf->last_error = PF_STATUS_INVALID_PARAMETER;
goto out;
}
if (path && strlen(path) > PATH_MAX_SIZE - 1) {
pf->last_error = PF_STATUS_PATH_TOO_LONG;
goto out;
}
COPY_ARRAY(pf->user_kdk_key, *kdk_key);
if (!file) {
DEBUG_PF("invalid handle");
pf->last_error = PF_STATUS_INVALID_PARAMETER;
goto out;
}
if (real_size % PF_NODE_SIZE != 0) {
pf->last_error = PF_STATUS_INVALID_HEADER;
goto out;
}
pf->host_file_handle = file;
pf->mode = mode;
if (!create) {
if (!ipf_init_existing_file(pf, path))
goto out;
} else {
if (!ipf_init_new_file(pf, path))
goto out;
}
pf->last_error = pf->file_status = PF_STATUS_SUCCESS;
DEBUG_PF("OK (data size %lu)", pf->metadata_decrypted.file_size);
out:
if (pf)
*status = pf->last_error;
if (pf && PF_FAILURE(pf->last_error)) {
DEBUG_PF("failed: %d", pf->last_error);
free(pf);
pf = NULL;
}
return pf;
}
static bool ipf_check_writable(pf_context_t* pf) {
if (PF_FAILURE(pf->file_status)) {
pf->last_error = pf->file_status;
DEBUG_PF("bad file status %d", pf->last_error);
return false;
}
if (!(pf->mode & PF_FILE_MODE_WRITE)) {
pf->last_error = PF_STATUS_INVALID_MODE;
DEBUG_PF("File is read-only");
return false;
}
return true;
}
// writes zeros if `ptr` is NULL
static size_t ipf_write(pf_context_t* pf, const void* ptr, uint64_t offset, size_t size) {
if (size == 0) {
pf->last_error = PF_STATUS_INVALID_PARAMETER;
return 0;
}
if (!ipf_check_writable(pf))
return 0;
size_t data_left_to_write = size;
const unsigned char* data_to_write = (const unsigned char*)ptr;
// the first MD_USER_DATA_SIZE bytes of user data are written in metadata node's encrypted part
if (offset < MD_USER_DATA_SIZE) {
size_t empty_place_left_in_md = MD_USER_DATA_SIZE - (size_t)offset;
size_t size_to_write = MIN(data_left_to_write, empty_place_left_in_md);
memcpy_or_zero_initialize(&pf->metadata_decrypted.file_data[offset], data_to_write,
size_to_write);
offset += size_to_write;
if (data_to_write)
data_to_write += size_to_write;
data_left_to_write -= size_to_write;
if (offset > pf->metadata_decrypted.file_size)
pf->metadata_decrypted.file_size = offset; // file grew, update the new file size
pf->need_writing = true;
}
while (data_left_to_write > 0) {
file_node_t* file_data_node = NULL;
// return the data node of the current offset, will read it from disk or create new one
// if needed (and also the MHT node(s) if needed)
file_data_node = ipf_get_data_node(pf, offset);
if (file_data_node == NULL) {
DEBUG_PF("failed to get data node");
break;
}
uint64_t offset_in_node = (offset - MD_USER_DATA_SIZE) % PF_NODE_SIZE;
size_t empty_place_left_in_node = PF_NODE_SIZE - offset_in_node;
size_t size_to_write = MIN(data_left_to_write, empty_place_left_in_node);
memcpy_or_zero_initialize(&file_data_node->decrypted.data.bytes[offset_in_node],
data_to_write, size_to_write);
offset += size_to_write;
if (data_to_write)
data_to_write += size_to_write;
data_left_to_write -= size_to_write;
if (offset > pf->metadata_decrypted.file_size) {
pf->metadata_decrypted.file_size = offset; // file grew, update the new file size
}
if (!file_data_node->need_writing) {
file_data_node->need_writing = true;
file_node_t* file_mht_node = file_data_node->parent;
while (file_mht_node->logical_node_number != 0) {
// set all the MHT parent nodes as 'need writing'
file_mht_node->need_writing = true;
file_mht_node = file_mht_node->parent;
}