2 * LUKS - Linux Unified Key Setup v2
4 * Copyright (C) 2015-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2015-2020 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef _CRYPTSETUP_LUKS2_INTERNAL_H
23 #define _CRYPTSETUP_LUKS2_INTERNAL_H
27 #include <json-c/json.h>
33 #define UNUSED(x) (void)(x)
35 /* override useless forward slash escape when supported by json-c */
36 #ifndef JSON_C_TO_STRING_NOSLASHESCAPE
37 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
41 * On-disk access function prototypes
43 int LUKS2_disk_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr,
44 struct device *device, int do_recovery, int do_blkprobe);
45 int LUKS2_disk_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr,
46 struct device *device, bool seqid_check);
49 * JSON struct access helpers
51 json_object *LUKS2_get_keyslot_jobj(struct luks2_hdr *hdr, int keyslot);
52 json_object *LUKS2_get_token_jobj(struct luks2_hdr *hdr, int token);
53 json_object *LUKS2_get_digest_jobj(struct luks2_hdr *hdr, int digest);
54 json_object *LUKS2_get_segment_jobj(struct luks2_hdr *hdr, int segment);
55 json_object *LUKS2_get_tokens_jobj(struct luks2_hdr *hdr);
56 json_object *LUKS2_get_segments_jobj(struct luks2_hdr *hdr);
58 void hexprint_base64(struct crypt_device *cd, json_object *jobj,
59 const char *sep, const char *line_sep);
61 uint64_t crypt_jobj_get_uint64(json_object *jobj);
62 uint32_t crypt_jobj_get_uint32(json_object *jobj);
63 json_object *crypt_jobj_new_uint64(uint64_t value);
65 int json_object_object_add_by_uint(json_object *jobj, unsigned key, json_object *jobj_val);
66 void json_object_object_del_by_uint(json_object *jobj, unsigned key);
67 int json_object_copy(json_object *jobj_src, json_object **jobj_dst);
69 void JSON_DBG(struct crypt_device *cd, json_object *jobj, const char *desc);
72 * LUKS2 JSON validation
75 /* validation helper */
76 json_bool validate_json_uint32(json_object *jobj);
77 json_object *json_contains(struct crypt_device *cd, json_object *jobj, const char *name,
78 const char *section, const char *key, json_type type);
80 int LUKS2_hdr_validate(struct crypt_device *cd, json_object *hdr_jobj, uint64_t json_size);
81 int LUKS2_check_json_size(struct crypt_device *cd, const struct luks2_hdr *hdr);
82 int LUKS2_token_validate(struct crypt_device *cd, json_object *hdr_jobj,
83 json_object *jobj_token, const char *key);
84 void LUKS2_token_dump(struct crypt_device *cd, int token);
87 * LUKS2 JSON repair for known glitches
89 void LUKS2_hdr_repair(struct crypt_device *cd, json_object *jobj_hdr);
90 void LUKS2_keyslots_repair(struct crypt_device *cd, json_object *jobj_hdr);
95 struct json_object *LUKS2_array_jobj(struct json_object *array, const char *num);
96 struct json_object *LUKS2_array_remove(struct json_object *array, const char *num);
103 * LUKS2 keyslots handlers (EXPERIMENTAL)
105 typedef int (*keyslot_alloc_func)(struct crypt_device *cd, int keyslot,
106 size_t volume_key_len,
107 const struct luks2_keyslot_params *params);
108 typedef int (*keyslot_update_func)(struct crypt_device *cd, int keyslot,
109 const struct luks2_keyslot_params *params);
110 typedef int (*keyslot_open_func) (struct crypt_device *cd, int keyslot,
111 const char *password, size_t password_len,
112 char *volume_key, size_t volume_key_len);
113 typedef int (*keyslot_store_func)(struct crypt_device *cd, int keyslot,
114 const char *password, size_t password_len,
115 const char *volume_key, size_t volume_key_len);
116 typedef int (*keyslot_wipe_func) (struct crypt_device *cd, int keyslot);
117 typedef int (*keyslot_dump_func) (struct crypt_device *cd, int keyslot);
118 typedef int (*keyslot_validate_func) (struct crypt_device *cd, json_object *jobj_keyslot);
119 typedef void(*keyslot_repair_func) (struct crypt_device *cd, json_object *jobj_keyslot);
121 /* see LUKS2_luks2_to_luks1 */
122 int placeholder_keyslot_alloc(struct crypt_device *cd,
124 uint64_t area_offset,
125 uint64_t area_length,
126 size_t volume_key_len);
128 /* validate all keyslot implementations in hdr json */
129 int LUKS2_keyslots_validate(struct crypt_device *cd, json_object *hdr_jobj);
133 keyslot_alloc_func alloc;
134 keyslot_update_func update;
135 keyslot_open_func open;
136 keyslot_store_func store;
137 keyslot_wipe_func wipe;
138 keyslot_dump_func dump;
139 keyslot_validate_func validate;
140 keyslot_repair_func repair;
143 /* can not fit prototype alloc function */
144 int reenc_keyslot_alloc(struct crypt_device *cd,
145 struct luks2_hdr *hdr,
147 const struct crypt_params_reencrypt *params);
150 * LUKS2 digest handlers (EXPERIMENTAL)
152 typedef int (*digest_verify_func)(struct crypt_device *cd, int digest,
153 const char *volume_key, size_t volume_key_len);
154 typedef int (*digest_store_func) (struct crypt_device *cd, int digest,
155 const char *volume_key, size_t volume_key_len);
156 typedef int (*digest_dump_func) (struct crypt_device *cd, int digest);
160 digest_verify_func verify;
161 digest_store_func store;
162 digest_dump_func dump;
166 * LUKS2 token handlers (internal use only)
168 typedef int (*builtin_token_get_func) (json_object *jobj_token, void *params);
169 typedef int (*builtin_token_set_func) (json_object **jobj_token, const void *params);
172 /* internal only section used by builtin tokens */
173 builtin_token_get_func get;
174 builtin_token_set_func set;
175 /* public token handler */
176 const crypt_token_handler *h;
179 int token_keyring_set(json_object **, const void *);
180 int token_keyring_get(json_object *, void *);
182 int LUKS2_find_area_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
183 size_t keylength, uint64_t *area_offset, uint64_t *area_length);
184 int LUKS2_find_area_max_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
185 uint64_t *area_offset, uint64_t *area_length);
187 int LUKS2_check_cipher(struct crypt_device *cd,
190 const char *cipher_mode);
192 static inline const char *crypt_reencrypt_mode_to_str(crypt_reencrypt_mode_info mi)
194 if (mi == CRYPT_REENCRYPT_REENCRYPT)
196 if (mi == CRYPT_REENCRYPT_ENCRYPT)
198 if (mi == CRYPT_REENCRYPT_DECRYPT)