Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / keyslot_context.h
1 /*
2  * LUKS - Linux Unified Key Setup, keyslot unlock helpers
3  *
4  * Copyright (C) 2022-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2022-2023 Ondrej Kozina
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 KEYSLOT_CONTEXT_H
23 #define KEYSLOT_CONTEXT_H
24
25 #include <stdbool.h>
26 #include <stdint.h>
27
28 #include "internal.h"
29
30 typedef int (*keyslot_context_get_key) (
31         struct crypt_device *cd,
32         struct crypt_keyslot_context *kc,
33         int keyslot,
34         int segment,
35         struct volume_key **r_vk);
36
37 typedef int (*keyslot_context_get_volume_key) (
38         struct crypt_device *cd,
39         struct crypt_keyslot_context *kc,
40         int keyslot,
41         struct volume_key **r_vk);
42
43 typedef int (*keyslot_context_get_passphrase) (
44         struct crypt_device *cd,
45         struct crypt_keyslot_context *kc,
46         const char **r_passphrase,
47         size_t *r_passphrase_size);
48
49 /* crypt_keyslot_context */
50 struct crypt_keyslot_context {
51         int type;
52
53         union {
54         struct {
55                 const char *passphrase;
56                 size_t passphrase_size;
57         } p;
58         struct {
59                 const char *keyfile;
60                 uint64_t keyfile_offset;
61                 size_t keyfile_size;
62         } kf;
63         struct {
64                 int id;
65                 const char *type;
66                 const char *pin;
67                 size_t pin_size;
68                 void *usrptr;
69         } t;
70         struct {
71                 const char *volume_key;
72                 size_t volume_key_size;
73         } k;
74         } u;
75
76         int error;
77
78         char *i_passphrase;
79         size_t i_passphrase_size;
80
81         keyslot_context_get_key         get_luks2_key;
82         keyslot_context_get_volume_key  get_luks1_volume_key;
83         keyslot_context_get_volume_key  get_luks2_volume_key;
84         keyslot_context_get_passphrase  get_passphrase;
85 };
86
87 void crypt_keyslot_context_destroy_internal(struct crypt_keyslot_context *method);
88
89 void crypt_keyslot_unlock_by_key_init_internal(struct crypt_keyslot_context *kc,
90         const char *volume_key,
91         size_t volume_key_size);
92
93 void crypt_keyslot_unlock_by_passphrase_init_internal(struct crypt_keyslot_context *kc,
94         const char *passphrase,
95         size_t passphrase_size);
96
97 void crypt_keyslot_unlock_by_keyfile_init_internal(struct crypt_keyslot_context *kc,
98         const char *keyfile,
99         size_t keyfile_size,
100         uint64_t keyfile_offset);
101
102 void crypt_keyslot_unlock_by_token_init_internal(struct crypt_keyslot_context *kc,
103         int token,
104         const char *type,
105         const char *pin,
106         size_t pin_size,
107         void *usrptr);
108
109 const char *keyslot_context_type_string(const struct crypt_keyslot_context *kc);
110
111 #endif /* KEYSLOT_CONTEXT_H */