Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / luks2 / luks2_internal.h
1 /*
2  * LUKS - Linux Unified Key Setup v2
3  *
4  * Copyright (C) 2015-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2015-2020 Milan Broz
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef _CRYPTSETUP_LUKS2_INTERNAL_H
23 #define _CRYPTSETUP_LUKS2_INTERNAL_H
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <json-c/json.h>
28
29 #include "internal.h"
30 #include "base64.h"
31 #include "luks2.h"
32
33 #define UNUSED(x) (void)(x)
34
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
38 #endif
39
40 /*
41  * On-disk access function prototypes
42  */
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);
47
48 /*
49  * JSON struct access helpers
50  */
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);
57
58 void hexprint_base64(struct crypt_device *cd, json_object *jobj,
59                      const char *sep, const char *line_sep);
60
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);
64
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);
68
69 void JSON_DBG(struct crypt_device *cd, json_object *jobj, const char *desc);
70
71 /*
72  * LUKS2 JSON validation
73  */
74
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);
79
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);
85
86 /*
87  * LUKS2 JSON repair for known glitches
88  */
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);
91
92 /*
93  * JSON array helpers
94  */
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);
97
98 /*
99  * Plugins API
100  */
101
102 /**
103  * LUKS2 keyslots handlers (EXPERIMENTAL)
104  */
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);
120
121 /* see LUKS2_luks2_to_luks1 */
122 int placeholder_keyslot_alloc(struct crypt_device *cd,
123         int keyslot,
124         uint64_t area_offset,
125         uint64_t area_length,
126         size_t volume_key_len);
127
128 /* validate all keyslot implementations in hdr json */
129 int LUKS2_keyslots_validate(struct crypt_device *cd, json_object *hdr_jobj);
130
131 typedef struct  {
132         const char *name;
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;
141 } keyslot_handler;
142
143 /* can not fit prototype alloc function */
144 int reenc_keyslot_alloc(struct crypt_device *cd,
145         struct luks2_hdr *hdr,
146         int keyslot,
147         const struct crypt_params_reencrypt *params);
148
149 /**
150  * LUKS2 digest handlers (EXPERIMENTAL)
151  */
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);
157
158 typedef struct  {
159         const char *name;
160         digest_verify_func verify;
161         digest_store_func  store;
162         digest_dump_func   dump;
163 } digest_handler;
164
165 /**
166  * LUKS2 token handlers (internal use only)
167  */
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);
170
171 typedef struct {
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;
177 } token_handler;
178
179 int token_keyring_set(json_object **, const void *);
180 int token_keyring_get(json_object *, void *);
181
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);
186
187 int LUKS2_check_cipher(struct crypt_device *cd,
188                       size_t keylength,
189                       const char *cipher,
190                       const char *cipher_mode);
191
192 static inline const char *crypt_reencrypt_mode_to_str(crypt_reencrypt_mode_info mi)
193 {
194         if (mi == CRYPT_REENCRYPT_REENCRYPT)
195                 return "reencrypt";
196         if (mi == CRYPT_REENCRYPT_ENCRYPT)
197                 return "encrypt";
198         if (mi == CRYPT_REENCRYPT_DECRYPT)
199                 return "decrypt";
200         return "<unknown>";
201 }
202
203 #endif