-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcrypto.c
More file actions
3147 lines (2786 loc) · 116 KB
/
crypto.c
File metadata and controls
3147 lines (2786 loc) · 116 KB
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
/*
* Copyright (C) 2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Vratislav Podzimek <vpodzime@redhat.com>
*/
#include <string.h>
#include <glib.h>
#include <libcryptsetup.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/random.h>
#include <locale.h>
#include <unistd.h>
#include <errno.h>
#include <blkid.h>
#include <sys/types.h>
#include <keyutils.h>
#include <blockdev/utils.h>
#ifdef WITH_BD_ESCROW
#include <nss.h>
#include <volume_key/libvolume_key.h>
#endif
#include "crypto.h"
#ifndef CRYPT_LUKS
#define CRYPT_LUKS NULL
#endif
#ifdef __clang__
#define ZERO_INIT {}
#else
#define ZERO_INIT {0}
#endif
#define SECTOR_SIZE 512
#define DEFAULT_LUKS_KEYSIZE_BITS 256
#define DEFAULT_LUKS_CIPHER "aes-xts-plain64"
#ifdef LIBCRYPTSETUP_23
/* 0 for autodetect since 2.3.0 */
#define DEFAULT_INTEGRITY_TAG_SIZE 0
#else
/* we need some sane default for older versions, users should specify tag size when using
other algorithms than the default crc32c */
#define DEFAULT_INTEGRITY_TAG_SIZE 4
#endif
#ifdef LIBCRYPTSETUP_24
/* 0 for autodetect since 2.4.0 */
#define DEFAULT_LUKS2_SECTOR_SIZE 0
#else
#define DEFAULT_LUKS2_SECTOR_SIZE 512
#endif
#define UNUSED __attribute__((unused))
#if !defined(HAVE_STRERROR_L)
static char *strerror_l(int errnum, locale_t locale UNUSED)
{
return strerror(errnum);
}
#endif
/**
* SECTION: crypto
* @short_description: plugin for operations with encrypted devices
* @title: Crypto
* @include: crypto.h
*
* A plugin for operations with encrypted devices. For now, only
* LUKS devices are supported.
*
* Functions taking a parameter called "device" require the backing device to be
* passed. On the other hand functions taking the "luks_device" parameter
* require the LUKS device (/dev/mapper/SOMETHING").
*
* Sizes are given in bytes unless stated otherwise.
*/
BDCryptoLUKSPBKDF* bd_crypto_luks_pbkdf_copy (BDCryptoLUKSPBKDF *pbkdf) {
if (pbkdf == NULL)
return NULL;
BDCryptoLUKSPBKDF *new_pbkdf = g_new0 (BDCryptoLUKSPBKDF, 1);
new_pbkdf->type = g_strdup (pbkdf->type);
new_pbkdf->hash = g_strdup (pbkdf->hash);
new_pbkdf->max_memory_kb = pbkdf->max_memory_kb;
new_pbkdf->iterations = pbkdf->iterations;
new_pbkdf->time_ms = pbkdf->time_ms;
new_pbkdf->parallel_threads = pbkdf->parallel_threads;
return new_pbkdf;
}
void bd_crypto_luks_pbkdf_free (BDCryptoLUKSPBKDF *pbkdf) {
if (pbkdf == NULL)
return;
g_free (pbkdf->type);
g_free (pbkdf->hash);
g_free (pbkdf);
}
BDCryptoLUKSPBKDF* bd_crypto_luks_pbkdf_new (const gchar *type, const gchar *hash, guint32 max_memory_kb, guint32 iterations, guint32 time_ms, guint32 parallel_threads) {
BDCryptoLUKSPBKDF *ret = g_new0 (BDCryptoLUKSPBKDF, 1);
ret->type = g_strdup (type);
ret->hash = g_strdup (hash);
ret->max_memory_kb = max_memory_kb;
ret->iterations = iterations;
ret->time_ms = time_ms;
ret->parallel_threads = parallel_threads;
return ret;
}
BDCryptoLUKSExtra* bd_crypto_luks_extra_copy (BDCryptoLUKSExtra *extra) {
if (extra == NULL)
return NULL;
BDCryptoLUKSExtra *new_extra = g_new0 (BDCryptoLUKSExtra, 1);
new_extra->integrity = g_strdup (extra->integrity);
new_extra->data_alignment = extra->data_alignment;
new_extra->data_device = g_strdup (extra->data_device);
new_extra->sector_size = extra->sector_size;
new_extra->label = g_strdup (extra->label);
new_extra->subsystem = g_strdup (extra->subsystem);
new_extra->pbkdf = bd_crypto_luks_pbkdf_copy (extra->pbkdf);
return new_extra;
}
void bd_crypto_luks_extra_free (BDCryptoLUKSExtra *extra) {
if (extra == NULL)
return;
g_free (extra->integrity);
g_free (extra->data_device);
g_free (extra->label);
g_free (extra->subsystem);
bd_crypto_luks_pbkdf_free (extra->pbkdf);
g_free (extra);
}
BDCryptoLUKSExtra* bd_crypto_luks_extra_new (guint64 data_alignment, const gchar *data_device, const gchar *integrity, guint64 sector_size, const gchar *label, const gchar *subsystem, BDCryptoLUKSPBKDF *pbkdf) {
BDCryptoLUKSExtra *ret = g_new0 (BDCryptoLUKSExtra, 1);
ret->integrity = g_strdup (integrity);
ret->data_alignment = data_alignment;
ret->data_device = g_strdup (data_device);
ret->sector_size = sector_size;
ret->label = g_strdup (label);
ret->subsystem = g_strdup (subsystem);
ret->pbkdf = bd_crypto_luks_pbkdf_copy (pbkdf);
return ret;
}
BDCryptoIntegrityExtra* bd_crypto_integrity_extra_new (guint64 sector_size, guint64 journal_size, guint journal_watermark, guint journal_commit_time, guint64 interleave_sectors, guint64 tag_size, guint64 buffer_sectors) {
BDCryptoIntegrityExtra *ret = g_new0 (BDCryptoIntegrityExtra, 1);
ret->sector_size = sector_size;
ret->journal_size = journal_size;
ret->journal_watermark = journal_watermark;
ret->journal_commit_time = journal_commit_time;
ret->interleave_sectors = interleave_sectors;
ret->tag_size = tag_size;
ret->buffer_sectors = buffer_sectors;
return ret;
}
BDCryptoIntegrityExtra* bd_crypto_integrity_extra_copy (BDCryptoIntegrityExtra *extra) {
if (extra == NULL)
return NULL;
BDCryptoIntegrityExtra *new_extra = g_new0 (BDCryptoIntegrityExtra, 1);
new_extra->sector_size = extra->sector_size;
new_extra->journal_size = extra->journal_size;
new_extra->journal_watermark = extra->journal_watermark;
new_extra->journal_commit_time = extra->journal_commit_time;
new_extra->interleave_sectors = extra->interleave_sectors;
new_extra->tag_size = extra->tag_size;
new_extra->buffer_sectors = extra->buffer_sectors;
return new_extra;
}
void bd_crypto_integrity_extra_free (BDCryptoIntegrityExtra *extra) {
if (extra == NULL)
return;
g_free (extra);
}
void bd_crypto_luks_info_free (BDCryptoLUKSInfo *info) {
if (info == NULL)
return;
g_free (info->cipher);
g_free (info->mode);
g_free (info->uuid);
g_free (info->backing_device);
g_free (info);
}
BDCryptoLUKSInfo* bd_crypto_luks_info_copy (BDCryptoLUKSInfo *info) {
if (info == NULL)
return NULL;
BDCryptoLUKSInfo *new_info = g_new0 (BDCryptoLUKSInfo, 1);
new_info->version = info->version;
new_info->cipher = g_strdup (info->cipher);
new_info->mode = g_strdup (info->mode);
new_info->uuid = g_strdup (info->uuid);
new_info->backing_device = g_strdup (info->backing_device);
new_info->sector_size = info->sector_size;
return new_info;
}
void bd_crypto_integrity_info_free (BDCryptoIntegrityInfo *info) {
if (info == NULL)
return;
g_free (info->algorithm);
g_free (info->journal_crypt);
g_free (info->journal_integrity);
g_free (info);
}
BDCryptoIntegrityInfo* bd_crypto_integrity_info_copy (BDCryptoIntegrityInfo *info) {
if (info == NULL)
return NULL;
BDCryptoIntegrityInfo *new_info = g_new0 (BDCryptoIntegrityInfo, 1);
new_info->algorithm = g_strdup (info->algorithm);
new_info->key_size = info->key_size;
new_info->sector_size = info->sector_size;
new_info->tag_size = info->tag_size;
new_info->interleave_sectors = info->interleave_sectors;
new_info->journal_size = info->journal_size;
new_info->journal_crypt = g_strdup (info->journal_crypt);
new_info->journal_integrity = g_strdup (info->journal_integrity);
return new_info;
}
void bd_crypto_luks_token_info_free (BDCryptoLUKSTokenInfo *info) {
if (info == NULL)
return;
g_free (info->type);
g_free (info);
}
BDCryptoLUKSTokenInfo* bd_crypto_luks_token_info_copy (BDCryptoLUKSTokenInfo *info) {
if (info == NULL)
return NULL;
BDCryptoLUKSTokenInfo *new_info = g_new0 (BDCryptoLUKSTokenInfo, 1);
new_info->id = info->id;
new_info->type = g_strdup (info->type);
new_info->keyslot = info->keyslot;
return new_info;
}
/* "C" locale to get the locale-agnostic error messages */
static locale_t c_locale = (locale_t) 0;
/**
* bd_crypto_check_deps:
*
* Returns: whether the plugin's runtime dependencies are satisfied or not
*
* Function checking plugin's runtime dependencies.
*
*/
gboolean bd_crypto_check_deps (void) {
/* nothing to do here */
return TRUE;
}
static void crypto_log_redirect (gint level, const gchar *msg, void *usrptr __attribute__((unused))) {
gchar *message = NULL;
switch (level) {
case CRYPT_LOG_DEBUG:
case CRYPT_LOG_VERBOSE:
message = g_strdup_printf ("[cryptsetup] %s", msg);
bd_utils_log (BD_UTILS_LOG_DEBUG, message);
g_free (message);
break;
case CRYPT_LOG_NORMAL:
case CRYPT_LOG_ERROR:
message = g_strdup_printf ("[cryptsetup] %s", msg);
bd_utils_log (BD_UTILS_LOG_INFO, message);
g_free (message);
break;
default:
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Unknown cryptsetup log level %d.", level);
message = g_strdup_printf ("[cryptsetup] %s", msg);
bd_utils_log (BD_UTILS_LOG_INFO, message);
g_free (message);
break;
}
}
/**
* bd_crypto_init:
*
* Initializes the plugin. **This function is called automatically by the
* library's initialization functions.**
*
*/
gboolean bd_crypto_init (void) {
#ifdef DEBUG
crypt_set_debug_level (CRYPT_DEBUG_ALL);
#endif
c_locale = newlocale (LC_ALL_MASK, "C", c_locale);
crypt_set_log_callback (NULL, &crypto_log_redirect, NULL);
return TRUE;
}
/**
* bd_crypto_close:
*
* Cleans up after the plugin. **This function is called automatically by the
* library's functions that unload it.**
*
*/
void bd_crypto_close (void) {
c_locale = (locale_t) 0;
crypt_set_log_callback (NULL, NULL, NULL);
crypt_set_debug_level (CRYPT_DEBUG_NONE);
}
/**
* bd_crypto_is_tech_avail:
* @tech: the queried tech
* @mode: a bit mask of queried modes of operation (#BDCryptoTechMode) for @tech
* @error: (out) (allow-none): place to store error (details about why the @tech-@mode combination is not available)
*
* Returns: whether the @tech-@mode combination is available -- supported by the
* plugin implementation and having all the runtime dependencies available
*/
gboolean bd_crypto_is_tech_avail (BDCryptoTech tech, guint64 mode, GError **error) {
guint64 ret = 0;
switch (tech) {
case BD_CRYPTO_TECH_LUKS:
ret = mode & (BD_CRYPTO_TECH_MODE_CREATE|BD_CRYPTO_TECH_MODE_OPEN_CLOSE|BD_CRYPTO_TECH_MODE_QUERY|
BD_CRYPTO_TECH_MODE_ADD_KEY|BD_CRYPTO_TECH_MODE_REMOVE_KEY|BD_CRYPTO_TECH_MODE_RESIZE|
BD_CRYPTO_TECH_MODE_SUSPEND_RESUME|BD_CRYPTO_TECH_MODE_BACKUP_RESTORE);
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'create', 'open', 'query', 'add-key', 'remove-key', 'resize', 'suspend-resume', 'backup-restore' supported for LUKS");
return FALSE;
} else
return TRUE;
case BD_CRYPTO_TECH_LUKS2:
#ifndef LIBCRYPTSETUP_2
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"LUKS 2 technology requires libcryptsetup >= 2.0");
return FALSE;
#endif
ret = mode & (BD_CRYPTO_TECH_MODE_CREATE|BD_CRYPTO_TECH_MODE_OPEN_CLOSE|BD_CRYPTO_TECH_MODE_QUERY|
BD_CRYPTO_TECH_MODE_ADD_KEY|BD_CRYPTO_TECH_MODE_REMOVE_KEY|BD_CRYPTO_TECH_MODE_RESIZE|
BD_CRYPTO_TECH_MODE_SUSPEND_RESUME|BD_CRYPTO_TECH_MODE_BACKUP_RESTORE);
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'create', 'open', 'query', 'add-key', 'remove-key', 'resize', 'suspend-resume', 'backup-restore' supported for LUKS 2");
return FALSE;
} else
return TRUE;
case BD_CRYPTO_TECH_TRUECRYPT:
ret = mode & BD_CRYPTO_TECH_MODE_OPEN_CLOSE;
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'open' supported for TrueCrypt");
return FALSE;
} else
return TRUE;
case BD_CRYPTO_TECH_ESCROW:
#ifndef WITH_BD_ESCROW
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Escrow technology is not available, libblockdev has been compiled without escrow support.");
return FALSE;
#endif
ret = mode & BD_CRYPTO_TECH_MODE_CREATE;
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'create' supported for device escrow");
return FALSE;
} else
return TRUE;
case BD_CRYPTO_TECH_INTEGRITY:
#ifndef LIBCRYPTSETUP_2
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Integrity technology requires libcryptsetup >= 2.0");
return FALSE;
#endif
ret = mode & (BD_CRYPTO_TECH_MODE_CREATE|BD_CRYPTO_TECH_MODE_OPEN_CLOSE|BD_CRYPTO_TECH_MODE_QUERY);
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'create', 'open' and 'query' supported for Integrity");
return FALSE;
} else
return TRUE;
case BD_CRYPTO_TECH_BITLK:
#ifndef LIBCRYPTSETUP_23
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"BITLK technology requires libcryptsetup >= 2.3.0");
return FALSE;
#endif
ret = mode & BD_CRYPTO_TECH_MODE_OPEN_CLOSE;
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'open' supported for BITLK");
return FALSE;
} else
return TRUE;
case BD_CRYPTO_TECH_KEYRING:
ret = mode & BD_CRYPTO_TECH_MODE_ADD_KEY;
if (ret != mode) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Only 'add key' supported for kernel keyring");
return FALSE;
} else
return TRUE;
default:
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL, "Unknown technology");
return FALSE;
}
return TRUE;
}
/**
* bd_crypto_error_quark: (skip)
*/
GQuark bd_crypto_error_quark (void)
{
return g_quark_from_static_string ("g-bd-crypto-error-quark");
}
/**
* bd_crypto_generate_backup_passphrase:
* @error: (out) (allow-none): place to store error (if any)
*
* Returns: (transfer full): A newly generated %BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH-long passphrase.
*
* See %BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET for the definition of the charset used for the passphrase.
*
* Tech category: always available
*/
gchar* bd_crypto_generate_backup_passphrase(GError **error __attribute__((unused))) {
guint8 i = 0;
guint8 offset = 0;
guint8 charset_length = strlen (BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET);
/* passphrase with groups of 5 characters separated with dashes, plus a null terminator */
gchar *ret = g_new0 (gchar, BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH + (BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH / 5));
for (i=0; i < BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH; i++) {
if (i > 0 && (i % 5 == 0)) {
/* put a dash between every 5 characters */
ret[i+offset] = '-';
offset++;
}
ret[i+offset] = BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET[g_random_int_range(0, charset_length)];
}
return ret;
}
/**
* bd_crypto_device_is_luks:
* @device: the queried device
* @error: (out) (allow-none): place to store error (if any)
*
* Returns: %TRUE if the given @device is a LUKS device or %FALSE if not or
* failed to determine (the @error) is populated with the error in such
* cases)
*
* Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
*/
gboolean bd_crypto_device_is_luks (const gchar *device, GError **error) {
blkid_probe probe = NULL;
gint fd = 0;
gint status = 0;
const gchar *value = NULL;
guint n_try = 0;
probe = blkid_new_probe ();
if (!probe) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to create a new probe");
return FALSE;
}
fd = open (device, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to open the device '%s'", device);
blkid_free_probe (probe);
return FALSE;
}
/* we may need to try multiple times with some delays in case the device is
busy at the very moment */
for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) {
status = blkid_probe_set_device (probe, fd, 0, 0);
if (status != 0)
g_usleep (100 * 1000); /* microseconds */
}
if (status != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to create a probe for the device '%s'", device);
blkid_free_probe (probe);
close (fd);
return FALSE;
}
blkid_probe_enable_partitions (probe, 1);
blkid_probe_set_partitions_flags (probe, BLKID_PARTS_MAGIC);
blkid_probe_enable_superblocks (probe, 1);
blkid_probe_set_superblocks_flags (probe, BLKID_SUBLKS_USAGE | BLKID_SUBLKS_TYPE |
BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM);
/* we may need to try multiple times with some delays in case the device is
busy at the very moment */
for (n_try=5, status=-1; !(status == 0 || status == 1) && (n_try > 0); n_try--) {
status = blkid_do_safeprobe (probe);
if (status < 0)
g_usleep (100 * 1000); /* microseconds */
}
if (status < 0) {
/* -1 or -2 = error during probing*/
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to probe the device '%s'", device);
blkid_free_probe (probe);
close (fd);
return FALSE;
} else if (status == 1) {
/* 1 = nothing detected */
blkid_free_probe (probe);
close (fd);
return FALSE;
}
status = blkid_probe_lookup_value (probe, "USAGE", &value, NULL);
if (status != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to get usage for the device '%s'", device);
blkid_free_probe (probe);
close (fd);
return FALSE;
}
if (g_strcmp0 (value, "crypto") != 0) {
blkid_free_probe (probe);
close (fd);
return FALSE;
}
status = blkid_probe_lookup_value (probe, "TYPE", &value, NULL);
if (status != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to get filesystem type for the device '%s'", device);
blkid_free_probe (probe);
close (fd);
return FALSE;
}
if (g_strcmp0 (value, "crypto_LUKS") != 0) {
blkid_free_probe (probe);
close (fd);
return FALSE;
}
blkid_free_probe (probe);
close (fd);
return TRUE;
}
/**
* bd_crypto_luks_uuid:
* @device: the queried device
* @error: (out) (allow-none): place to store error (if any)
*
* Returns: (transfer full): UUID of the @device or %NULL if failed to determine (@error
* is populated with the error in such cases)
*
* Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
*/
gchar* bd_crypto_luks_uuid (const gchar *device, GError **error) {
struct crypt_device *cd = NULL;
gint ret_num;
gchar *ret;
ret_num = crypt_init (&cd, device);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror_l(-ret_num, c_locale));
return NULL;
}
ret_num = crypt_load (cd, CRYPT_LUKS, NULL);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device: %s", strerror_l(-ret_num, c_locale));
crypt_free (cd);
return NULL;
}
ret = g_strdup (crypt_get_uuid (cd));
crypt_free (cd);
return ret;
}
/**
* bd_crypto_get_luks_metadata_size:
* @device: the queried device
* @error: (out) (allow-none): place to store error (if any)
*
* Returns: luks device metadata size of the @device
* or 0 if failed to determine (@error is populated
* with the error in such cases)
*
* Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
*/
guint64 bd_crypto_luks_get_metadata_size (const gchar *device, GError **error) {
struct crypt_device *cd = NULL;
gint ret_num;
guint64 ret;
ret_num = crypt_init (&cd, device);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror_l(-ret_num, c_locale));
return 0;
}
ret_num = crypt_load (cd, CRYPT_LUKS, NULL);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device: %s", strerror_l(-ret_num, c_locale));
crypt_free (cd);
return 0;
}
ret = SECTOR_SIZE * crypt_get_data_offset (cd);
crypt_free (cd);
return ret;
}
/**
* bd_crypto_luks_status:
* @luks_device: the queried LUKS device
* @error: (out) (allow-none): place to store error (if any)
*
* Returns: (transfer none): one of "invalid", "inactive", "active" or "busy" or
* %NULL if failed to determine (@error is populated with the error in
* such cases)
*
* Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
*/
gchar* bd_crypto_luks_status (const gchar *luks_device, GError **error) {
struct crypt_device *cd = NULL;
gint ret_num;
const gchar *ret = NULL;
crypt_status_info status;
ret_num = crypt_init_by_name (&cd, luks_device);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror_l(-ret_num, c_locale));
return NULL;
}
status = crypt_status (cd, luks_device);
switch (status) {
case CRYPT_INVALID:
ret = "invalid";
break;
case CRYPT_INACTIVE:
ret = "inactive";
break;
case CRYPT_ACTIVE:
ret = "active";
break;
case CRYPT_BUSY:
ret = "busy";
break;
default:
ret = NULL;
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_STATE,
"Unknown device's state");
}
crypt_free (cd);
/* cast the "const" away because this API requires returning a
non-const string, though the caller isn't allowed to modify its
contents */
return (gchar *)ret;
}
#ifdef LIBCRYPTSETUP_2
static struct crypt_pbkdf_type *get_pbkdf_params (BDCryptoLUKSPBKDF *user_pbkdf, GError **error) {
const struct crypt_pbkdf_type *default_pbkdf = NULL;
struct crypt_pbkdf_type *new_pbkdf = NULL;
if (user_pbkdf == NULL)
return NULL;
/* crypt_get_pbkdf_default returns default pbkdf parameters only based
on the luks version -- so for LUKS2 it returns default values for
argon2 but we also need to be able to provide default values if user
wants pbkdf2 and only specifies type -- we will use the defaults for
argon2 and ignore parameters specific to it
better API for this should be part of cryptsetup 2.0.4
*/
default_pbkdf = crypt_get_pbkdf_default (CRYPT_LUKS2);
if (!default_pbkdf) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
"Failed to get default values for pbkdf.");
return NULL;
}
new_pbkdf = g_new0 (struct crypt_pbkdf_type, 1);
new_pbkdf->flags = default_pbkdf->flags;
if (user_pbkdf->type)
new_pbkdf->type = user_pbkdf->type;
else
new_pbkdf->type = default_pbkdf->type;
if (user_pbkdf->hash)
new_pbkdf->hash = user_pbkdf->hash;
else
new_pbkdf->hash = default_pbkdf->hash;
if (user_pbkdf->time_ms)
new_pbkdf->time_ms = user_pbkdf->time_ms;
else
new_pbkdf->time_ms = default_pbkdf->time_ms;
if (user_pbkdf->iterations) {
new_pbkdf->iterations = user_pbkdf->iterations;
/* iterations set manually -> do not run benchmark */
new_pbkdf->flags = CRYPT_PBKDF_NO_BENCHMARK;
} else
new_pbkdf->iterations = default_pbkdf->iterations;
/* 'max_memory_kb' and 'parallel_threads' are not used in pbkdf2 */
if (g_strcmp0 (user_pbkdf->type, CRYPT_KDF_PBKDF2) == 0) {
if (user_pbkdf->max_memory_kb)
bd_utils_log_format (LOG_WARNING, "'max_memory_kb' is not valid option for 'pbkdf2', ignoring.");
new_pbkdf->max_memory_kb = 0;
new_pbkdf->parallel_threads = 0;
} else {
if (user_pbkdf->max_memory_kb)
new_pbkdf->max_memory_kb = user_pbkdf->max_memory_kb;
else
new_pbkdf->max_memory_kb = default_pbkdf->max_memory_kb;
if (user_pbkdf->parallel_threads)
new_pbkdf->parallel_threads = user_pbkdf->parallel_threads;
else
new_pbkdf->parallel_threads = default_pbkdf->parallel_threads;
}
return new_pbkdf;
}
#endif
static gboolean luks_format (const gchar *device, const gchar *cipher, guint64 key_size, const guint8 *pass_data, gsize data_size, const gchar *key_file, guint64 min_entropy, BDCryptoLUKSVersion luks_version, BDCryptoLUKSExtra *extra, GError **error) {
struct crypt_device *cd = NULL;
gint ret;
gchar **cipher_specs = NULL;
guint32 current_entropy = 0;
gint dev_random_fd = -1;
gboolean success = FALSE;
gchar *key_buffer = NULL;
gsize buf_len = 0;
guint64 progress_id = 0;
gchar *msg = NULL;
const gchar* crypt_version = NULL;
GError *l_error = NULL;
msg = g_strdup_printf ("Started formatting '%s' as LUKS device", device);
progress_id = bd_utils_report_started (msg);
g_free (msg);
if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS1)
crypt_version = CRYPT_LUKS1;
#ifdef LIBCRYPTSETUP_2
else if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS2)
crypt_version = CRYPT_LUKS2;
#endif
else {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
"Unknown or unsupported LUKS version specified");
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
if ((data_size == 0) && !key_file) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_NO_KEY,
"At least one of passphrase and key file have to be specified!");
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
cipher = cipher ? cipher : DEFAULT_LUKS_CIPHER;
cipher_specs = g_strsplit (cipher, "-", 2);
if (g_strv_length (cipher_specs) != 2) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_SPEC,
"Invalid cipher specification: '%s'", cipher);
crypt_free (cd);
g_strfreev (cipher_specs);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
if (key_size == 0) {
if (g_str_has_prefix (cipher_specs[1], "xts-"))
key_size = DEFAULT_LUKS_KEYSIZE_BITS * 2;
else
key_size = DEFAULT_LUKS_KEYSIZE_BITS;
}
/* key_size should be in bytes */
key_size = key_size / 8;
/* wait for enough random data entropy (if requested) */
if (min_entropy > 0) {
dev_random_fd = open ("/dev/random", O_RDONLY);
if (dev_random_fd >= 0) {
ioctl (dev_random_fd, RNDGETENTCNT, ¤t_entropy);
while (current_entropy < min_entropy) {
bd_utils_report_progress (progress_id, 0, "Waiting for enough random data entropy");
sleep (1);
ioctl (dev_random_fd, RNDGETENTCNT, ¤t_entropy);
}
close (dev_random_fd);
} else {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
"Failed to check random data entropy level");
crypt_free (cd);
g_strfreev (cipher_specs);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
}
if (extra) {
if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS1) {
if (extra->integrity || extra->sector_size || extra->label || extra->subsystem || extra->pbkdf) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
"Invalid extra arguments specified. Only `data_alignment`"
"and `data_device` are valid for LUKS 1.");
crypt_free (cd);
g_strfreev (cipher_specs);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
struct crypt_params_luks1 params = ZERO_INIT;
params.data_alignment = extra->data_alignment;
params.data_device = extra->data_device;
ret = crypt_format (cd, crypt_version, cipher_specs[0], cipher_specs[1],
NULL, NULL, key_size, ¶ms);
}
#ifdef LIBCRYPTSETUP_2
else if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS2) {
struct crypt_params_luks2 params = ZERO_INIT;
struct crypt_pbkdf_type *pbkdf = get_pbkdf_params (extra->pbkdf, &l_error);
if (pbkdf == NULL && l_error != NULL) {
crypt_free (cd);
g_strfreev (cipher_specs);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_prefixed_error (error, l_error,
"Failed to get PBKDF parameters for '%s'.", device);
return FALSE;
}
params.pbkdf = pbkdf;
params.integrity = extra->integrity;
params.integrity_params = NULL;
params.data_alignment = extra->data_alignment;
params.data_device = extra->data_device;
params.sector_size = extra->sector_size ? extra->sector_size : DEFAULT_LUKS2_SECTOR_SIZE;
params.label = extra->label;
params.subsystem = extra->subsystem;
ret = crypt_format (cd, crypt_version, cipher_specs[0], cipher_specs[1],
NULL, NULL, key_size, ¶ms);
g_free (pbkdf);
}
#endif
} else
ret = crypt_format (cd, crypt_version, cipher_specs[0], cipher_specs[1],
NULL, NULL, key_size, NULL);
g_strfreev (cipher_specs);
if (ret != 0) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
"Failed to format device: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
bd_utils_report_progress (progress_id, ((data_size != 0) && key_file) ? 40 : 50, "Format created");
if (data_size != 0) {
ret = crypt_keyslot_add_by_volume_key (cd, CRYPT_ANY_SLOT, NULL, 0,
(const char*) pass_data, data_size);
if (ret < 0) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
"Failed to add passphrase: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
bd_utils_report_progress (progress_id, ((data_size != 0) && key_file) ? 70 : 100, "Added key");
}
if (key_file) {
success = g_file_get_contents (key_file, &key_buffer, &buf_len, &l_error);
if (!success) {
g_prefix_error (&l_error, "Failed to add key file: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
ret = crypt_keyslot_add_by_volume_key (cd, CRYPT_ANY_SLOT, NULL, 0,
(const char*) key_buffer, buf_len);
g_free (key_buffer);
if (ret < 0) {
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
"Failed to add key file: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
bd_utils_report_finished (progress_id, l_error->message);
g_propagate_error (error, l_error);
return FALSE;
}
}
crypt_free (cd);
bd_utils_report_finished (progress_id, "Completed");
return TRUE;
}
/**