Skip to content

Commit 3c2c8e6

Browse files
committed
cryptsetup: use crypt_token_max if available
New API added upstream: https://gitlab.com/cryptsetup/cryptsetup/-/commit/8a12f6dc2c75f8fd0c4969fbdc421895eb418072
1 parent 0a9fb9b commit 3c2c8e6

File tree

9 files changed

+25
-11
lines changed

9 files changed

+25
-11
lines changed

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,8 @@ if want_libcryptsetup != 'false' and not skip_deps
10561056
have and cc.has_function('crypt_set_metadata_size', dependencies : libcryptsetup))
10571057
conf.set10('HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY',
10581058
have and cc.has_function('crypt_activate_by_signed_key', dependencies : libcryptsetup))
1059+
conf.set10('HAVE_CRYPT_TOKEN_MAX',
1060+
have and cc.has_function('crypt_token_max', dependencies : libcryptsetup))
10591061
else
10601062
have = false
10611063
libcryptsetup = []

src/cryptenroll/cryptenroll-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int list_enrolled(struct crypt_device *cd) {
3737

3838
/* Second step, enumerate through all tokens, and update the slot table, indicating what kind of
3939
* token they are assigned to */
40-
for (int token = 0; token < LUKS2_TOKENS_MAX; token++) {
40+
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
4141
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
4242
const char *type;
4343
JsonVariant *w, *z;

src/cryptenroll/cryptenroll-tpm2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int search_policy_hash(
2020
if (hash_size == 0)
2121
return 0;
2222

23-
for (int token = 0; token < LUKS2_TOKENS_MAX; token ++) {
23+
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token ++) {
2424
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
2525
_cleanup_free_ void *thash = NULL;
2626
size_t thash_size = 0;

src/cryptenroll/cryptenroll-wipe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int find_slots_by_mask(
9999

100100
/* Find all slots that are associated with a token of a type in the specified token type mask */
101101

102-
for (int token = 0; token < LUKS2_TOKENS_MAX; token++) {
102+
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
103103
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
104104
JsonVariant *w, *z;
105105
EnrollType t;
@@ -199,7 +199,7 @@ static int find_slot_tokens(struct crypt_device *cd, Set *wipe_slots, Set *keep_
199199
/* Find all tokens matching the slots we want to wipe, so that we can wipe them too. Also, for update
200200
* the slots sets according to the token data: add any other slots listed in the tokens we act on. */
201201

202-
for (int token = 0; token < LUKS2_TOKENS_MAX; token++) {
202+
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
203203
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
204204
bool shall_wipe = false;
205205
JsonVariant *w, *z;

src/cryptsetup/cryptsetup-fido2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int find_fido2_auto_data(
119119

120120
/* Loads FIDO2 metadata from LUKS2 JSON token headers. */
121121

122-
for (int token = 0; token < LUKS2_TOKENS_MAX; token ++) {
122+
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token ++) {
123123
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
124124
JsonVariant *w;
125125

src/cryptsetup/cryptsetup-pkcs11.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ int find_pkcs11_auto_data(
180180

181181
/* Loads PKCS#11 metadata from LUKS2 JSON token headers. */
182182

183-
for (int token = 0; token < LUKS2_TOKENS_MAX; token++) {
183+
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
184184
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
185185
JsonVariant *w;
186186

src/cryptsetup/cryptsetup-tpm2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int find_tpm2_auto_data(
8484

8585
assert(cd);
8686

87-
for (token = start_token; token < LUKS2_TOKENS_MAX; token++) {
87+
for (token = start_token; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
8888
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
8989
JsonVariant *w, *e;
9090

src/shared/cryptsetup-util.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ int (*sym_crypt_set_pbkdf_type)(struct crypt_device *cd, const struct crypt_pbkd
3131
int (*sym_crypt_token_json_get)(struct crypt_device *cd, int token, const char **json) = NULL;
3232
int (*sym_crypt_token_json_set)(struct crypt_device *cd, int token, const char *json) = NULL;
3333
int (*sym_crypt_volume_key_get)(struct crypt_device *cd, int keyslot, char *volume_key, size_t *volume_key_size, const char *passphrase, size_t passphrase_size);
34+
#if HAVE_CRYPT_TOKEN_MAX
35+
int (*sym_crypt_token_max)(const char *type);
36+
#endif
3437

3538
int dlopen_cryptsetup(void) {
3639
_cleanup_(dlclosep) void *dl = NULL;
@@ -69,6 +72,9 @@ int dlopen_cryptsetup(void) {
6972
DLSYM_ARG(crypt_token_json_get),
7073
DLSYM_ARG(crypt_token_json_set),
7174
DLSYM_ARG(crypt_volume_key_get),
75+
#if HAVE_CRYPT_TOKEN_MAX
76+
DLSYM_ARG(crypt_token_max),
77+
#endif
7278
NULL);
7379
if (r < 0)
7480
return r;

src/shared/cryptsetup-util.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ extern int (*sym_crypt_set_pbkdf_type)(struct crypt_device *cd, const struct cry
3737
extern int (*sym_crypt_token_json_get)(struct crypt_device *cd, int token, const char **json);
3838
extern int (*sym_crypt_token_json_set)(struct crypt_device *cd, int token, const char *json);
3939
extern int (*sym_crypt_volume_key_get)(struct crypt_device *cd, int keyslot, char *volume_key, size_t *volume_key_size, const char *passphrase, size_t passphrase_size);
40+
#if HAVE_CRYPT_TOKEN_MAX
41+
extern int (*sym_crypt_token_max)(const char *type);
42+
#else
43+
/* As a fallback, use the same hard-coded value libcryptsetup uses internally. */
44+
static inline int sym_crypt_token_max(_unused_ const char *type) {
45+
assert(streq(type, CRYPT_LUKS2));
46+
47+
return 32;
48+
}
49+
#endif
4050

4151
int dlopen_cryptsetup(void);
4252

@@ -51,8 +61,4 @@ int cryptsetup_get_token_as_json(struct crypt_device *cd, int idx, const char *v
5161
int cryptsetup_get_keyslot_from_token(JsonVariant *v);
5262
int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v);
5363

54-
/* Stolen from cryptsetup's sources. We use to iterate through all tokens defined for a volume. Ideally, we'd
55-
* be able to query this via some API, but there appears to be none currently in libcryptsetup. */
56-
#define LUKS2_TOKENS_MAX 32
57-
5864
#endif

0 commit comments

Comments
 (0)