Debian Codegraph @

src/plugins/crypto.h

raw
1 #include <glib.h>
2 #include <blockdev/utils.h>
3
4 #ifndef BD_CRYPTO
5 #define BD_CRYPTO
6
7 GQuark bd_crypto_error_quark (void);
8 #define BD_CRYPTO_ERROR bd_crypto_error_quark ()
9 typedef enum {
10 BD_CRYPTO_ERROR_TECH_UNAVAIL,
11 BD_CRYPTO_ERROR_DEVICE,
12 BD_CRYPTO_ERROR_STATE,
13 BD_CRYPTO_ERROR_INVALID_SPEC,
14 BD_CRYPTO_ERROR_FORMAT_FAILED,
15 BD_CRYPTO_ERROR_RESIZE_FAILED,
16 BD_CRYPTO_ERROR_RESIZE_PERM,
17 BD_CRYPTO_ERROR_ADD_KEY,
18 BD_CRYPTO_ERROR_REMOVE_KEY,
19 BD_CRYPTO_ERROR_NO_KEY,
20 BD_CRYPTO_ERROR_KEY_SLOT,
21 BD_CRYPTO_ERROR_NSS_INIT_FAILED,
22 BD_CRYPTO_ERROR_CERT_DECODE,
23 BD_CRYPTO_ERROR_ESCROW_FAILED,
24 BD_CRYPTO_ERROR_INVALID_PARAMS,
25 BD_CRYPTO_ERROR_KEYRING,
26 BD_CRYPTO_ERROR_KEYFILE_FAILED,
27 BD_CRYPTO_ERROR_INVALID_CONTEXT,
28 BD_CRYPTO_ERROR_CONVERT_FAILED,
29 } BDCryptoError;
30
31 #define BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
32
33 /* KEEP THIS A MULTIPLE OF 5 SO THAT '-' CAN BE INSERTED BETWEEN EVERY 5 CHARACTERS! */
34 /* 20 chars * 6 bits per char (64-item charset) = 120 "bits of security" */
35 #define BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH 20
36
37 typedef enum {
38 BD_CRYPTO_TECH_LUKS = 0,
39 BD_CRYPTO_TECH_TRUECRYPT,
40 BD_CRYPTO_TECH_ESCROW,
41 BD_CRYPTO_TECH_INTEGRITY,
42 BD_CRYPTO_TECH_BITLK,
43 BD_CRYPTO_TECH_KEYRING,
44 BD_CRYPTO_TECH_FVAULT2,
45 BD_CRYPTO_TECH_SED_OPAL,
46 } BDCryptoTech;
47
48 typedef enum {
49 BD_CRYPTO_TECH_MODE_CREATE = 1 << 0,
50 BD_CRYPTO_TECH_MODE_OPEN_CLOSE = 1 << 1,
51 BD_CRYPTO_TECH_MODE_QUERY = 1 << 2,
52 BD_CRYPTO_TECH_MODE_ADD_KEY = 1 << 3,
53 BD_CRYPTO_TECH_MODE_REMOVE_KEY = 1 << 4,
54 BD_CRYPTO_TECH_MODE_RESIZE = 1 << 5,
55 BD_CRYPTO_TECH_MODE_SUSPEND_RESUME = 1 << 6,
56 BD_CRYPTO_TECH_MODE_BACKUP_RESTORE = 1 << 7,
57 BD_CRYPTO_TECH_MODE_MODIFY = 1 << 8,
58 } BDCryptoTechMode;
59
60 typedef enum {
61 BD_CRYPTO_LUKS_VERSION_LUKS1 = 0,
62 BD_CRYPTO_LUKS_VERSION_LUKS2,
63 } BDCryptoLUKSVersion;
64
65 /**
66 * BDCryptoLUKSPBKDF
67 * @type: PBKDF algorithm
68 * @hash: hash for LUKS header or NULL
69 * @max_memory_kb: requested memory cost (in KiB) or 0 for default (benchmark)
70 * @iterations: requested iterations or 0 for default (benchmark)
71 * @time_ms: requested time cost or 0 for default (benchmark)
72 * @parallel_threads: requested parallel cost (threads) or 0 for default (benchmark)
73 */
74 typedef struct BDCryptoLUKSPBKDF {
75 gchar *type;
76 gchar *hash;
77 guint32 max_memory_kb;
78 guint32 iterations;
79 guint32 time_ms;
80 guint32 parallel_threads;
81 } BDCryptoLUKSPBKDF;
82
83 void bd_crypto_luks_pbkdf_free (BDCryptoLUKSPBKDF *pbkdf);
84 BDCryptoLUKSPBKDF* bd_crypto_luks_pbkdf_copy (BDCryptoLUKSPBKDF *pbkdf);
85 BDCryptoLUKSPBKDF* bd_crypto_luks_pbkdf_new (const gchar *type, const gchar *hash, guint32 max_memory_kb, guint32 iterations, guint32 time_ms, guint32 parallel_threads);
86
87 /**
88 * BDCryptoLUKSExtra:
89 * @data_alignment: data alignment in sectors, 0 for default/auto detection
90 * @data_device: detached encrypted data device or NULL
91 * @integrity: integrity algorithm (e.g. "hmac-sha256") or NULL for no integrity support
92 * Note: this field is valid only for LUKS 2
93 * @sector_size: encryption sector size, 0 for default (512)
94 * Note: this field is valid only for LUKS 2
95 * @label: LUKS header label or NULL
96 * Note: this field is valid only for LUKS 2
97 * @subsystem: LUKS header subsystem or NULL
98 * Note: this field is valid only for LUKS 2
99 * @pbkdf: key derivation function specification or NULL for default
100 * Note: this field is valid only for LUKS 2
101 */
102 typedef struct BDCryptoLUKSExtra {
103 guint64 data_alignment;
104 gchar *data_device;
105 gchar *integrity;
106 guint32 sector_size;
107 gchar *label;
108 gchar *subsystem;
109 BDCryptoLUKSPBKDF *pbkdf;
110 } BDCryptoLUKSExtra;
111
112 void bd_crypto_luks_extra_free (BDCryptoLUKSExtra *extra);
113 BDCryptoLUKSExtra* bd_crypto_luks_extra_copy (BDCryptoLUKSExtra *extra);
114 BDCryptoLUKSExtra* bd_crypto_luks_extra_new (guint64 data_alignment, const gchar *data_device, const gchar *integrity, guint32 sector_size, const gchar *label, const gchar *subsystem, BDCryptoLUKSPBKDF *pbkdf);
115
116 /**
117 * BDCryptoIntegrityExtra:
118 * @sector_size: integrity sector size
119 * @journal_size: size of journal in bytes
120 * @journal_watermark: journal flush watermark in percents; in bitmap mode sectors-per-bit
121 * @journal_commit_time: journal commit time (or bitmap flush time) in ms
122 * @interleave_sectors: number of interleave sectors (power of two)
123 * @tag_size: tag size per-sector in bytes
124 * @buffer_sectors: number of sectors in one buffer
125 */
126 typedef struct BDCryptoIntegrityExtra {
127 guint32 sector_size;
128 guint64 journal_size;
129 guint journal_watermark;
130 guint journal_commit_time;
131 guint32 interleave_sectors;
132 guint32 tag_size;
133 guint32 buffer_sectors;
134 } BDCryptoIntegrityExtra;
135
136 void bd_crypto_integrity_extra_free (BDCryptoIntegrityExtra *extra);
137 BDCryptoIntegrityExtra* bd_crypto_integrity_extra_copy (BDCryptoIntegrityExtra *extra);
138 BDCryptoIntegrityExtra* bd_crypto_integrity_extra_new (guint32 sector_size, guint64 journal_size, guint journal_watermark, guint journal_commit_time, guint64 interleave_sectors, guint64 tag_size, guint64 buffer_sectors);
139
140 typedef enum {
141 BD_CRYPTO_INTEGRITY_OPEN_NO_JOURNAL = 1 << 0,
142 BD_CRYPTO_INTEGRITY_OPEN_RECOVERY = 1 << 1,
143 BD_CRYPTO_INTEGRITY_OPEN_NO_JOURNAL_BITMAP = 1 << 2,
144 BD_CRYPTO_INTEGRITY_OPEN_RECALCULATE = 1 << 3,
145 BD_CRYPTO_INTEGRITY_OPEN_RECALCULATE_RESET = 1 << 4,
146 BD_CRYPTO_INTEGRITY_OPEN_ALLOW_DISCARDS = 1 << 5,
147 } BDCryptoIntegrityOpenFlags;
148
149 /**
150 * BDCryptoLUKSHWEncryptionType:
151 * @BD_CRYPTO_LUKS_HW_ENCRYPTION_UNKNOWN: used for unknown/unsupported hardware encryption or when
152 * error was detected when getting the information
153 * @BD_CRYPTO_LUKS_HW_ENCRYPTION_SW_ONLY: hardware encryption is not configured on this device
154 * @BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_ONLY: only OPAL hardware encryption is configured on this device
155 * @BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_AND_SW: both OPAL hardware encryption and software encryption
156 * (using LUKS/dm-crypt) is configured on this device
157 */
158 typedef enum {
159 BD_CRYPTO_LUKS_HW_ENCRYPTION_UNKNOWN = 0,
160 BD_CRYPTO_LUKS_HW_ENCRYPTION_SW_ONLY,
161 BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_ONLY,
162 BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_AND_SW,
163 } BDCryptoLUKSHWEncryptionType;
164
165 typedef enum {
166 BD_CRYPTO_LUKS_ACTIVATE_ALLOW_DISCARDS = 1 << 0,
167 BD_CRYPTO_LUKS_ACTIVATE_SAME_CPU_CRYPT = 1 << 1,
168 BD_CRYPTO_LUKS_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS = 1 << 2,
169 BD_CRYPTO_LUKS_ACTIVATE_NO_JOURNAL = 1 << 3,
170 BD_CRYPTO_LUKS_ACTIVATE_NO_READ_WORKQUEUE = 1 << 4,
171 BD_CRYPTO_LUKS_ACTIVATE_NO_WRITE_WORKQUEUE = 1 << 5,
172 BD_CRYPTO_LUKS_ACTIVATE_HIGH_PRIORITY = 1 << 6,
173 } BDCryptoLUKSPersistentFlags;
174
175 typedef enum {
176 BD_CRYPTO_OPEN_ALLOW_DISCARDS = 1 << 0,
177 BD_CRYPTO_OPEN_READONLY = 1 << 1,
178 } BDCryptoOpenFlags;
179
180 /**
181 * BDCryptoLUKSInfo:
182 * @version: LUKS version
183 * @cipher: used cipher (e.g. "aes")
184 * @mode: used cipher mode (e.g. "xts-plain")
185 * @uuid: UUID of the LUKS device
186 * @backing_device: name of the underlying block device
187 * @sector_size: size (in bytes) of encryption sector
188 * Note: sector size is valid only for LUKS 2
189 * @metadata_size: LUKS metadata size
190 * @label: label of the LUKS device (valid only for LUKS 2)
191 * @subsystem: subsystem of the LUKS device (valid only for LUKS 2)
192 * @hw_encryption: hardware encryption type
193 */
194 typedef struct BDCryptoLUKSInfo {
195 BDCryptoLUKSVersion version;
196 gchar *cipher;
197 gchar *mode;
198 gchar *uuid;
199 gchar *backing_device;
200 guint32 sector_size;
201 guint64 metadata_size;
202 gchar *label;
203 gchar *subsystem;
204 BDCryptoLUKSHWEncryptionType hw_encryption;
205 } BDCryptoLUKSInfo;
206
207 void bd_crypto_luks_info_free (BDCryptoLUKSInfo *info);
208 BDCryptoLUKSInfo* bd_crypto_luks_info_copy (BDCryptoLUKSInfo *info);
209
210 /**
211 * BDCryptoBITLKInfo:
212 * @cipher: used cipher (e.g. "aes")
213 * @mode: used cipher mode (e.g. "xts-plain")
214 * @uuid: UUID of the BITLK device
215 * @backing_device: name of the underlying block device
216 * @sector_size: size (in bytes) of encryption sector
217 */
218 typedef struct BDCryptoBITLKInfo {
219 gchar *cipher;
220 gchar *mode;
221 gchar *uuid;
222 gchar *backing_device;
223 guint32 sector_size;
224 } BDCryptoBITLKInfo;
225
226 void bd_crypto_bitlk_info_free (BDCryptoBITLKInfo *info);
227 BDCryptoBITLKInfo* bd_crypto_bitlk_info_copy (BDCryptoBITLKInfo *info);
228
229 /**
230 * BDCryptoIntegrityInfo:
231 * @algorithm: integrity algorithm
232 * @key_size: integrity key size in bytes
233 * @sector_size: sector size in bytes
234 * @tag_size: tag size per-sector in bytes
235 * @interleave_sectors: number of interleave sectors
236 * @journal_size: size of journal in bytes
237 * @journal_crypt: journal encryption algorithm
238 * @journal_integrity: journal integrity algorithm
239 */
240 typedef struct BDCryptoIntegrityInfo {
241 gchar *algorithm;
242 guint32 key_size;
243 guint32 sector_size;
244 guint32 tag_size;
245 guint32 interleave_sectors;
246 guint64 journal_size;
247 gchar *journal_crypt;
248 gchar *journal_integrity;
249 } BDCryptoIntegrityInfo;
250
251 void bd_crypto_integrity_info_free (BDCryptoIntegrityInfo *info);
252 BDCryptoIntegrityInfo* bd_crypto_integrity_info_copy (BDCryptoIntegrityInfo *info);
253
254 /**
255 * BDCryptoLUKSTokenInfo:
256 * @id: ID of the token
257 * @type: type of the token
258 * @keyslot: keyslot this token is assigned to or -1 for inactive/unassigned tokens
259 */
260 typedef struct BDCryptoLUKSTokenInfo {
261 guint id;
262 gchar *type;
263 gint keyslot;
264 } BDCryptoLUKSTokenInfo;
265
266 void bd_crypto_luks_token_info_free (BDCryptoLUKSTokenInfo *info);
267 BDCryptoLUKSTokenInfo* bd_crypto_luks_token_info_copy (BDCryptoLUKSTokenInfo *info);
268
269 typedef struct _BDCryptoKeyslotContext BDCryptoKeyslotContext;
270
271 void bd_crypto_keyslot_context_free (BDCryptoKeyslotContext *context);
272 BDCryptoKeyslotContext* bd_crypto_keyslot_context_copy (BDCryptoKeyslotContext *context);
273
274 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_passphrase (const guint8 *pass_data, gsize data_len, GError **error);
275 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_keyfile (const gchar *keyfile, guint64 keyfile_offset, gsize key_size, GError **error);
276 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_keyring (const gchar *key_desc, GError **error);
277 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_volume_key (const guint8 *volume_key, gsize volume_key_size, GError **error);
278
279 /*
280 * If using the plugin as a standalone library, the following functions should
281 * be called to:
282 *
283 * init() - initialize the plugin, returning TRUE on success
284 * close() - clean after the plugin at the end or if no longer used
285 *
286 */
287 gboolean bd_crypto_init (void);
288 void bd_crypto_close (void);
289
290 gboolean bd_crypto_is_tech_avail (BDCryptoTech tech, guint64 mode, GError **error);
291
292 gchar* bd_crypto_generate_backup_passphrase(GError **error);
293 gboolean bd_crypto_device_is_luks (const gchar *device, GError **error);
294 const gchar* bd_crypto_luks_status (const gchar *luks_device, GError **error);
295
296 gboolean bd_crypto_luks_format (const gchar *device, const gchar *cipher, guint64 key_size, BDCryptoKeyslotContext *context, guint64 min_entropy, BDCryptoLUKSVersion luks_version, BDCryptoLUKSExtra *extra,GError **error);
297 gboolean bd_crypto_luks_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, gboolean read_only, GError **error);
298 gboolean bd_crypto_luks_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, BDCryptoOpenFlags flags, GError **error);
299 gboolean bd_crypto_luks_close (const gchar *luks_device, GError **error);
300 gboolean bd_crypto_luks_add_key (const gchar *device, BDCryptoKeyslotContext *context, BDCryptoKeyslotContext *ncontext, GError **error);
301 gboolean bd_crypto_luks_remove_key (const gchar *device, BDCryptoKeyslotContext *context, GError **error);
302 gboolean bd_crypto_luks_change_key (const gchar *device, BDCryptoKeyslotContext *context, BDCryptoKeyslotContext *ncontext, GError **error);
303 gboolean bd_crypto_luks_resize (const gchar *luks_device, guint64 size, BDCryptoKeyslotContext *context, GError **error);
304 gboolean bd_crypto_luks_suspend (const gchar *luks_device, GError **error);
305 gboolean bd_crypto_luks_resume (const gchar *luks_device, BDCryptoKeyslotContext *context, GError **error);
306 gboolean bd_crypto_luks_kill_slot (const gchar *device, gint slot, GError **error);
307 gboolean bd_crypto_luks_header_backup (const gchar *device, const gchar *backup_file, GError **error);
308 gboolean bd_crypto_luks_header_restore (const gchar *device, const gchar *backup_file, GError **error);
309 gboolean bd_crypto_luks_set_label (const gchar *device, const gchar *label, const gchar *subsystem, GError **error);
310 gboolean bd_crypto_luks_check_label (const gchar *label, const gchar *subsystem, GError **error);
311 gboolean bd_crypto_luks_set_uuid (const gchar *device, const gchar *uuid, GError **error);
312 gboolean bd_crypto_luks_convert (const gchar *device, BDCryptoLUKSVersion target_version, GError **error);
313 gboolean bd_crypto_luks_set_persistent_flags (const gchar *device, BDCryptoLUKSPersistentFlags flags, GError **error);
314
315 BDCryptoLUKSInfo* bd_crypto_luks_info (const gchar *device, GError **error);
316 BDCryptoBITLKInfo* bd_crypto_bitlk_info (const gchar *device, GError **error);
317 BDCryptoIntegrityInfo* bd_crypto_integrity_info (const gchar *device, GError **error);
318 BDCryptoLUKSTokenInfo** bd_crypto_luks_token_info (const gchar *device, GError **error);
319
320 gboolean bd_crypto_integrity_format (const gchar *device, const gchar *algorithm, gboolean wipe, BDCryptoKeyslotContext *context, BDCryptoIntegrityExtra *extra, GError **error);
321 gboolean bd_crypto_integrity_open (const gchar *device, const gchar *name, const gchar *algorithm, BDCryptoKeyslotContext *context, BDCryptoIntegrityOpenFlags flags, BDCryptoIntegrityExtra *extra, GError **error);
322 gboolean bd_crypto_integrity_close (const gchar *integrity_device, GError **error);
323
324 gboolean bd_crypto_keyring_add_key (const gchar *key_desc, const guint8 *key_data, gsize data_len, GError **error);
325
326 gboolean bd_crypto_device_seems_encrypted (const gchar *device, GError **error);
327 gboolean bd_crypto_tc_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, const gchar **keyfiles, gboolean hidden, gboolean system, gboolean veracrypt, guint32 veracrypt_pim, gboolean read_only, GError **error);
328 gboolean bd_crypto_tc_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, const gchar **keyfiles, gboolean hidden, gboolean system, gboolean veracrypt, guint32 veracrypt_pim, BDCryptoOpenFlags flags, GError **error);
329 gboolean bd_crypto_tc_close (const gchar *tc_device, GError **error);
330
331 gboolean bd_crypto_bitlk_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, gboolean read_only, GError **error);
332 gboolean bd_crypto_bitlk_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, BDCryptoOpenFlags flags, GError **error);
333 gboolean bd_crypto_bitlk_close (const gchar *bitlk_device, GError **error);
334
335 gboolean bd_crypto_fvault2_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, gboolean read_only, GError **error);
336 gboolean bd_crypto_fvault2_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, BDCryptoOpenFlags flags, GError **error);
337 gboolean bd_crypto_fvault2_close (const gchar *fvault2_device, GError **error);
338
339 gboolean bd_crypto_escrow_device (const gchar *device, const gchar *passphrase, const gchar *cert_data, const gchar *directory, const gchar *backup_passphrase, GError **error);
340
341 gboolean bd_crypto_opal_is_supported (const gchar *device, GError **error);
342 gboolean bd_crypto_opal_wipe_device (const gchar *device, BDCryptoKeyslotContext *context, GError **error);
343 gboolean bd_crypto_opal_reset_device (const gchar *device, BDCryptoKeyslotContext *context, GError **error);
344 gboolean bd_crypto_opal_format (const gchar *device, const gchar *cipher, guint64 key_size, BDCryptoKeyslotContext *context, guint64 min_entropy, BDCryptoLUKSHWEncryptionType hw_encryption,
345 BDCryptoKeyslotContext *opal_context, BDCryptoLUKSExtra *extra, GError **error);
346 #endif /* BD_CRYPTO */