Coverage Report

Created: 2025-06-13 06:36

/src/cryptsetup/lib/luks2/luks2_luks1_convert.c
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * LUKS - Linux Unified Key Setup v2, LUKS1 conversion code
4
 *
5
 * Copyright (C) 2015-2025 Red Hat, Inc. All rights reserved.
6
 * Copyright (C) 2015-2025 Ondrej Kozina
7
 * Copyright (C) 2015-2025 Milan Broz
8
 */
9
10
#include "luks2_internal.h"
11
#include "../luks1/luks.h"
12
#include "../luks1/af.h"
13
14
/* This differs from LUKS_check_cipher() that it does not check dm-crypt fallback. */
15
int LUKS2_check_cipher(struct crypt_device *cd,
16
          size_t keylength,
17
          const char *cipher,
18
          const char *cipher_mode)
19
0
{
20
0
  int r;
21
0
  struct crypt_storage *s;
22
0
  char buf[SECTOR_SIZE], *empty_key;
23
24
0
  log_dbg(cd, "Checking if cipher %s-%s is usable (storage wrapper).", cipher, cipher_mode);
25
26
0
  empty_key = crypt_safe_alloc(keylength);
27
0
  if (!empty_key)
28
0
    return -ENOMEM;
29
30
  /* No need to get KEY quality random but it must avoid known weak keys. */
31
0
  r = crypt_random_get(cd, empty_key, keylength, CRYPT_RND_NORMAL);
32
0
  if (r < 0)
33
0
    goto out;
34
35
0
  r = crypt_storage_init(&s, SECTOR_SIZE, cipher, cipher_mode, empty_key, keylength, false);
36
0
  if (r < 0)
37
0
    goto out;
38
39
0
  memset(buf, 0, sizeof(buf));
40
0
  r = crypt_storage_decrypt(s, 0, sizeof(buf), buf);
41
0
  crypt_storage_destroy(s);
42
0
out:
43
0
  crypt_safe_free(empty_key);
44
0
  crypt_safe_memzero(buf, sizeof(buf));
45
0
  return r;
46
0
}
47
48
static int json_luks1_keyslot(const struct luks_phdr *hdr_v1, int keyslot, struct json_object **keyslot_object)
49
0
{
50
0
  char *base64_str, cipher[LUKS_CIPHERNAME_L+LUKS_CIPHERMODE_L];
51
0
  size_t base64_len;
52
0
  struct json_object *keyslot_obj, *field, *jobj_kdf, *jobj_af, *jobj_area;
53
0
  uint64_t offset, area_size, length;
54
0
  int r;
55
56
0
  keyslot_obj = json_object_new_object();
57
0
  if (!keyslot_obj) {
58
0
    r = -ENOMEM;
59
0
    goto err;
60
0
  }
61
62
0
  json_object_object_add(keyslot_obj, "type", json_object_new_string("luks2"));
63
0
  json_object_object_add(keyslot_obj, "key_size", json_object_new_int64(hdr_v1->keyBytes));
64
65
  /* KDF */
66
0
  jobj_kdf = json_object_new_object();
67
0
  if (!jobj_kdf) {
68
0
    r = -ENOMEM;
69
0
    goto err;
70
0
  }
71
72
0
  json_object_object_add(jobj_kdf, "type", json_object_new_string(CRYPT_KDF_PBKDF2));
73
0
  json_object_object_add(jobj_kdf, "hash", json_object_new_string(hdr_v1->hashSpec));
74
0
  json_object_object_add(jobj_kdf, "iterations", json_object_new_int64(hdr_v1->keyblock[keyslot].passwordIterations));
75
  /* salt field */
76
0
  r = crypt_base64_encode(&base64_str, &base64_len, hdr_v1->keyblock[keyslot].passwordSalt, LUKS_SALTSIZE);
77
0
  if (r < 0) {
78
0
    json_object_put(keyslot_obj);
79
0
    json_object_put(jobj_kdf);
80
0
    return r;
81
0
  }
82
0
  field = json_object_new_string_len(base64_str, base64_len);
83
0
  free(base64_str);
84
0
  json_object_object_add(jobj_kdf, "salt", field);
85
0
  json_object_object_add(keyslot_obj, "kdf", jobj_kdf);
86
87
  /* AF */
88
0
  jobj_af = json_object_new_object();
89
0
  if (!jobj_af) {
90
0
    r = -ENOMEM;
91
0
    goto err;
92
0
  }
93
94
0
  json_object_object_add(jobj_af, "type", json_object_new_string("luks1"));
95
0
  json_object_object_add(jobj_af, "hash", json_object_new_string(hdr_v1->hashSpec));
96
  /* stripes field ignored, fixed to LUKS_STRIPES (4000) */
97
0
  json_object_object_add(jobj_af, "stripes", json_object_new_int(LUKS_STRIPES));
98
0
  json_object_object_add(keyslot_obj, "af", jobj_af);
99
100
  /* Area */
101
0
  jobj_area = json_object_new_object();
102
0
  if (!jobj_area) {
103
0
    r = -ENOMEM;
104
0
    goto err;
105
0
  }
106
107
0
  json_object_object_add(jobj_area, "type", json_object_new_string("raw"));
108
109
  /* encryption algorithm field */
110
0
  if (*hdr_v1->cipherMode != '\0') {
111
0
    if (snprintf(cipher, sizeof(cipher), "%s-%s", hdr_v1->cipherName, hdr_v1->cipherMode) < 0) {
112
0
      json_object_put(keyslot_obj);
113
0
      json_object_put(jobj_area);
114
0
      return -EINVAL;
115
0
    }
116
0
    json_object_object_add(jobj_area, "encryption", json_object_new_string(cipher));
117
0
  } else
118
0
    json_object_object_add(jobj_area, "encryption", json_object_new_string(hdr_v1->cipherName));
119
120
  /* area */
121
0
  if (LUKS_keyslot_area(hdr_v1, keyslot, &offset, &length)) {
122
0
    json_object_put(keyslot_obj);
123
0
    json_object_put(jobj_area);
124
0
    return -EINVAL;
125
0
  }
126
0
  area_size = size_round_up(length, 4096);
127
0
  json_object_object_add(jobj_area, "key_size", json_object_new_int(hdr_v1->keyBytes));
128
0
  json_object_object_add(jobj_area, "offset", crypt_jobj_new_uint64(offset));
129
0
  json_object_object_add(jobj_area, "size", crypt_jobj_new_uint64(area_size));
130
0
  json_object_object_add(keyslot_obj, "area", jobj_area);
131
132
0
  *keyslot_object = keyslot_obj;
133
0
  return 0;
134
0
err:
135
0
  json_object_put(keyslot_obj);
136
0
  return r;
137
0
}
138
139
static int json_luks1_keyslots(const struct luks_phdr *hdr_v1, struct json_object **keyslots_object)
140
0
{
141
0
  int keyslot, r;
142
0
  struct json_object *keyslot_obj, *field;
143
144
0
  keyslot_obj = json_object_new_object();
145
0
  if (!keyslot_obj)
146
0
    return -ENOMEM;
147
148
0
  for (keyslot = 0; keyslot < LUKS_NUMKEYS; keyslot++) {
149
0
    if (hdr_v1->keyblock[keyslot].active != LUKS_KEY_ENABLED)
150
0
      continue;
151
0
    r = json_luks1_keyslot(hdr_v1, keyslot, &field);
152
0
    if (r) {
153
0
      json_object_put(keyslot_obj);
154
0
      return r;
155
0
    }
156
0
    r = json_object_object_add_by_uint(keyslot_obj, keyslot, field);
157
0
    if (r) {
158
0
      json_object_put(field);
159
0
      json_object_put(keyslot_obj);
160
0
      return r;
161
0
    }
162
0
  }
163
164
0
  *keyslots_object = keyslot_obj;
165
0
  return 0;
166
0
}
167
168
static int json_luks1_segment(const struct luks_phdr *hdr_v1, struct json_object **segment_object)
169
0
{
170
0
  const char *c;
171
0
  char cipher[LUKS_CIPHERNAME_L+LUKS_CIPHERMODE_L];
172
0
  struct json_object *segment_obj, *field;
173
0
  uint64_t number;
174
175
0
  segment_obj = json_object_new_object();
176
0
  if (!segment_obj)
177
0
    return -ENOMEM;
178
179
  /* type field */
180
0
  field = json_object_new_string("crypt");
181
0
  if (!field) {
182
0
    json_object_put(segment_obj);
183
0
    return -ENOMEM;
184
0
  }
185
0
  json_object_object_add(segment_obj, "type", field);
186
187
  /* offset field */
188
0
  number = (uint64_t)hdr_v1->payloadOffset * SECTOR_SIZE;
189
190
0
  field = crypt_jobj_new_uint64(number);
191
0
  if (!field) {
192
0
    json_object_put(segment_obj);
193
0
    return -ENOMEM;
194
0
  }
195
0
  json_object_object_add(segment_obj, "offset", field);
196
197
  /* iv_tweak field */
198
0
  field = json_object_new_string("0");
199
0
  if (!field) {
200
0
    json_object_put(segment_obj);
201
0
    return -ENOMEM;
202
0
  }
203
0
  json_object_object_add(segment_obj, "iv_tweak", field);
204
205
  /* length field */
206
0
  field = json_object_new_string("dynamic");
207
0
  if (!field) {
208
0
    json_object_put(segment_obj);
209
0
    return -ENOMEM;
210
0
  }
211
0
  json_object_object_add(segment_obj, "size", field);
212
213
  /* cipher field */
214
0
  if (*hdr_v1->cipherMode != '\0') {
215
0
    if (snprintf(cipher, sizeof(cipher), "%s-%s", hdr_v1->cipherName, hdr_v1->cipherMode) < 0) {
216
0
      json_object_put(segment_obj);
217
0
      return -EINVAL;
218
0
    }
219
0
    c = cipher;
220
0
  } else
221
0
    c = hdr_v1->cipherName;
222
223
0
  field = json_object_new_string(c);
224
0
  if (!field) {
225
0
    json_object_put(segment_obj);
226
0
    return -ENOMEM;
227
0
  }
228
0
  json_object_object_add(segment_obj, "encryption", field);
229
230
  /* block field */
231
0
  field = json_object_new_int(SECTOR_SIZE);
232
0
  if (!field) {
233
0
    json_object_put(segment_obj);
234
0
    return -ENOMEM;
235
0
  }
236
0
  json_object_object_add(segment_obj, "sector_size", field);
237
238
0
  *segment_object = segment_obj;
239
0
  return 0;
240
0
}
241
242
static int json_luks1_segments(const struct luks_phdr *hdr_v1, struct json_object **segments_object)
243
0
{
244
0
  int r;
245
0
  struct json_object *segments_obj, *field;
246
247
0
  segments_obj = json_object_new_object();
248
0
  if (!segments_obj)
249
0
    return -ENOMEM;
250
251
0
  r = json_luks1_segment(hdr_v1, &field);
252
0
  if (r) {
253
0
    json_object_put(segments_obj);
254
0
    return r;
255
0
  }
256
0
  r = json_object_object_add_by_uint(segments_obj, 0, field);
257
0
  if (r) {
258
0
    json_object_put(field);
259
0
    json_object_put(segments_obj);
260
0
    return r;
261
0
  }
262
263
0
  *segments_object = segments_obj;
264
0
  return 0;
265
0
}
266
267
static int json_luks1_digest(const struct luks_phdr *hdr_v1, struct json_object **digest_object)
268
0
{
269
0
  char keyslot_str[16], *base64_str;
270
0
  int r, ks;
271
0
  size_t base64_len;
272
0
  struct json_object *digest_obj, *array, *field;
273
274
0
  digest_obj = json_object_new_object();
275
0
  if (!digest_obj)
276
0
    return -ENOMEM;
277
278
  /* type field */
279
0
  field = json_object_new_string("pbkdf2");
280
0
  if (!field) {
281
0
    json_object_put(digest_obj);
282
0
    return -ENOMEM;
283
0
  }
284
0
  json_object_object_add(digest_obj, "type", field);
285
286
  /* keyslots array */
287
0
  array = json_object_new_array();
288
0
  if (!array) {
289
0
    json_object_put(digest_obj);
290
0
    return -ENOMEM;
291
0
  }
292
0
  json_object_object_add(digest_obj, "keyslots", json_object_get(array));
293
294
0
  for (ks = 0; ks < LUKS_NUMKEYS; ks++) {
295
0
    if (hdr_v1->keyblock[ks].active != LUKS_KEY_ENABLED)
296
0
      continue;
297
0
    if (snprintf(keyslot_str, sizeof(keyslot_str), "%d", ks) < 0) {
298
0
      json_object_put(field);
299
0
      json_object_put(array);
300
0
      json_object_put(digest_obj);
301
0
      return -EINVAL;
302
0
    }
303
304
0
    field = json_object_new_string(keyslot_str);
305
0
    if (!field || json_object_array_add(array, field) < 0) {
306
0
      json_object_put(field);
307
0
      json_object_put(array);
308
0
      json_object_put(digest_obj);
309
0
      return -ENOMEM;
310
0
    }
311
0
  }
312
313
0
  json_object_put(array);
314
315
  /* segments array */
316
0
  array = json_object_new_array();
317
0
  if (!array) {
318
0
    json_object_put(digest_obj);
319
0
    return -ENOMEM;
320
0
  }
321
0
  json_object_object_add(digest_obj, "segments", json_object_get(array));
322
323
0
  field = json_object_new_string("0");
324
0
  if (!field || json_object_array_add(array, field) < 0) {
325
0
    json_object_put(field);
326
0
    json_object_put(array);
327
0
    json_object_put(digest_obj);
328
0
    return -ENOMEM;
329
0
  }
330
331
0
  json_object_put(array);
332
333
  /* hash field */
334
0
  field = json_object_new_string(hdr_v1->hashSpec);
335
0
  if (!field) {
336
0
    json_object_put(digest_obj);
337
0
    return -ENOMEM;
338
0
  }
339
0
  json_object_object_add(digest_obj, "hash", field);
340
341
  /* salt field */
342
0
  r = crypt_base64_encode(&base64_str, &base64_len, hdr_v1->mkDigestSalt, LUKS_SALTSIZE);
343
0
  if (r < 0) {
344
0
    json_object_put(digest_obj);
345
0
    return r;
346
0
  }
347
348
0
  field = json_object_new_string_len(base64_str, base64_len);
349
0
  free(base64_str);
350
0
  if (!field) {
351
0
    json_object_put(digest_obj);
352
0
    return -ENOMEM;
353
0
  }
354
0
  json_object_object_add(digest_obj, "salt", field);
355
356
  /* digest field */
357
0
  r = crypt_base64_encode(&base64_str, &base64_len, hdr_v1->mkDigest, LUKS_DIGESTSIZE);
358
0
  if (r < 0) {
359
0
    json_object_put(digest_obj);
360
0
    return r;
361
0
  }
362
363
0
  field = json_object_new_string_len(base64_str, base64_len);
364
0
  free(base64_str);
365
0
  if (!field) {
366
0
    json_object_put(digest_obj);
367
0
    return -ENOMEM;
368
0
  }
369
0
  json_object_object_add(digest_obj, "digest", field);
370
371
  /* iterations field */
372
0
  field = json_object_new_int64(hdr_v1->mkDigestIterations);
373
0
  if (!field) {
374
0
    json_object_put(digest_obj);
375
0
    return -ENOMEM;
376
0
  }
377
0
  json_object_object_add(digest_obj, "iterations", field);
378
379
0
  *digest_object = digest_obj;
380
0
  return 0;
381
0
}
382
383
static int json_luks1_digests(const struct luks_phdr *hdr_v1, struct json_object **digests_object)
384
0
{
385
0
  int r;
386
0
  struct json_object *digests_obj, *field;
387
388
0
  digests_obj = json_object_new_object();
389
0
  if (!digests_obj)
390
0
    return -ENOMEM;
391
392
0
  r = json_luks1_digest(hdr_v1, &field);
393
0
  if (r) {
394
0
    json_object_put(digests_obj);
395
0
    return r;
396
0
  }
397
0
  json_object_object_add(digests_obj, "0", field);
398
399
0
  *digests_object = digests_obj;
400
0
  return 0;
401
0
}
402
403
static int json_luks1_object(struct luks_phdr *hdr_v1, struct json_object **luks1_object, uint64_t keyslots_size)
404
0
{
405
0
  int r;
406
0
  struct json_object *luks1_obj, *field;
407
0
  uint64_t json_size;
408
409
0
  luks1_obj = json_object_new_object();
410
0
  if (!luks1_obj)
411
0
    return -ENOMEM;
412
413
  /* keyslots field */
414
0
  r = json_luks1_keyslots(hdr_v1, &field);
415
0
  if (r) {
416
0
    json_object_put(luks1_obj);
417
0
    return r;
418
0
  }
419
0
  json_object_object_add(luks1_obj, "keyslots", field);
420
421
  /* tokens field */
422
0
  field = json_object_new_object();
423
0
  if (!field) {
424
0
    json_object_put(luks1_obj);
425
0
    return -ENOMEM;
426
0
  }
427
0
  json_object_object_add(luks1_obj, "tokens", field);
428
429
  /* segments field */
430
0
  r = json_luks1_segments(hdr_v1, &field);
431
0
  if (r) {
432
0
    json_object_put(luks1_obj);
433
0
    return r;
434
0
  }
435
0
  json_object_object_add(luks1_obj, "segments", field);
436
437
  /* digests field */
438
0
  r = json_luks1_digests(hdr_v1, &field);
439
0
  if (r) {
440
0
    json_object_put(luks1_obj);
441
0
    return r;
442
0
  }
443
0
  json_object_object_add(luks1_obj, "digests", field);
444
445
  /* config field */
446
  /* anything else? */
447
0
  field = json_object_new_object();
448
0
  if (!field) {
449
0
    json_object_put(luks1_obj);
450
0
    return -ENOMEM;
451
0
  }
452
0
  json_object_object_add(luks1_obj, "config", field);
453
454
0
  json_size = LUKS2_HDR_16K_LEN - LUKS2_HDR_BIN_LEN;
455
0
  json_object_object_add(field, "json_size", crypt_jobj_new_uint64(json_size));
456
0
  keyslots_size -= (keyslots_size % 4096);
457
0
  json_object_object_add(field, "keyslots_size", crypt_jobj_new_uint64(keyslots_size));
458
459
0
  *luks1_object = luks1_obj;
460
0
  return 0;
461
0
}
462
463
static void move_keyslot_offset(json_object *jobj, int offset_add)
464
0
{
465
0
  json_object *jobj1, *jobj2, *jobj_area;
466
0
  uint64_t offset = 0;
467
468
0
  json_object_object_get_ex(jobj, "keyslots", &jobj1);
469
0
  json_object_object_foreach(jobj1, key, val) {
470
0
    UNUSED(key);
471
0
    json_object_object_get_ex(val, "area", &jobj_area);
472
0
    json_object_object_get_ex(jobj_area, "offset", &jobj2);
473
0
    offset = crypt_jobj_get_uint64(jobj2) + offset_add;
474
0
    json_object_object_add(jobj_area, "offset", crypt_jobj_new_uint64(offset));
475
0
  }
476
0
}
477
478
static int move_keyslot_areas(struct crypt_device *cd, off_t offset_from,
479
            off_t offset_to, size_t buf_size)
480
0
{
481
0
  int devfd, r = -EIO;
482
0
  struct device *device = crypt_metadata_device(cd);
483
0
  void *buf = NULL;
484
485
0
  log_dbg(cd, "Moving keyslot areas of size %zu from %jd to %jd.",
486
0
    buf_size, (intmax_t)offset_from, (intmax_t)offset_to);
487
488
0
  if (posix_memalign(&buf, crypt_getpagesize(), buf_size))
489
0
    return -ENOMEM;
490
491
0
  devfd = device_open(cd, device, O_RDWR);
492
0
  if (devfd < 0) {
493
0
    free(buf);
494
0
    return -EIO;
495
0
  }
496
497
  /* This can safely fail (for block devices). It only allocates space if it is possible. */
498
0
  if (posix_fallocate(devfd, offset_to, buf_size))
499
0
    log_dbg(cd, "Preallocation (fallocate) of new keyslot area not available.");
500
501
  /* Try to read *new* area to check that area is there (trimmed backup). */
502
0
  if (read_lseek_blockwise(devfd, device_block_size(cd, device),
503
0
         device_alignment(device), buf, buf_size,
504
0
         offset_to)!= (ssize_t)buf_size)
505
0
    goto out;
506
507
0
  if (read_lseek_blockwise(devfd, device_block_size(cd, device),
508
0
         device_alignment(device), buf, buf_size,
509
0
         offset_from)!= (ssize_t)buf_size)
510
0
    goto out;
511
512
0
  if (write_lseek_blockwise(devfd, device_block_size(cd, device),
513
0
          device_alignment(device), buf, buf_size,
514
0
          offset_to) != (ssize_t)buf_size)
515
0
    goto out;
516
517
0
  r = 0;
518
0
out:
519
0
  device_sync(cd, device);
520
0
  crypt_safe_memzero(buf, buf_size);
521
0
  free(buf);
522
523
0
  return r;
524
0
}
525
526
static int luks_header_in_use(struct crypt_device *cd)
527
0
{
528
0
  int r;
529
530
0
  r = lookup_dm_dev_by_uuid(cd, crypt_get_uuid(cd), crypt_get_type(cd));
531
0
  if (r < 0)
532
0
    log_err(cd, _("Cannot check status of device with uuid: %s."), crypt_get_uuid(cd));
533
534
0
  return r;
535
0
}
536
537
/* Check if there is a luksmeta area (foreign metadata created by the luksmeta package) */
538
static int luksmeta_header_present(struct crypt_device *cd, off_t luks1_size)
539
0
{
540
0
  int devfd, r = 0;
541
0
  static const uint8_t LM_MAGIC[] = { 'L', 'U', 'K', 'S', 'M', 'E', 'T', 'A' };
542
0
  struct device *device = crypt_metadata_device(cd);
543
0
  void *buf = NULL;
544
545
0
  if (posix_memalign(&buf, crypt_getpagesize(), sizeof(LM_MAGIC)))
546
0
    return -ENOMEM;
547
548
0
  devfd = device_open(cd, device, O_RDONLY);
549
0
  if (devfd < 0) {
550
0
    free(buf);
551
0
    return -EIO;
552
0
  }
553
554
  /* Note: we must not detect failure as problem here, header can be trimmed. */
555
0
  if (read_lseek_blockwise(devfd, device_block_size(cd, device), device_alignment(device),
556
0
    buf, sizeof(LM_MAGIC), luks1_size) == (ssize_t)sizeof(LM_MAGIC) &&
557
0
    !memcmp(LM_MAGIC, buf, sizeof(LM_MAGIC))) {
558
0
      log_err(cd, _("Unable to convert header with LUKSMETA additional metadata."));
559
0
      r = -EBUSY;
560
0
  }
561
562
0
  free(buf);
563
0
  return r;
564
0
}
565
566
/* Convert LUKS1 -> LUKS2 */
567
int LUKS2_luks1_to_luks2(struct crypt_device *cd, struct luks_phdr *hdr1, struct luks2_hdr *hdr2)
568
0
{
569
0
  int r;
570
0
  json_object *jobj = NULL;
571
0
  size_t buf_size, buf_offset, luks1_size, luks1_shift = 2 * LUKS2_HDR_16K_LEN - LUKS_ALIGN_KEYSLOTS;
572
0
  uint64_t required_size, max_size = crypt_get_data_offset(cd) * SECTOR_SIZE;
573
0
  char cipher_spec[MAX_CAPI_LEN];
574
575
  /* for detached headers max size == device size */
576
0
  if (!max_size && (r = device_size(crypt_metadata_device(cd), &max_size)))
577
0
    return r;
578
579
0
  luks1_size = LUKS_device_sectors(hdr1) << SECTOR_SHIFT;
580
0
  luks1_size = size_round_up(luks1_size, LUKS_ALIGN_KEYSLOTS);
581
0
  if (!luks1_size)
582
0
    return -EINVAL;
583
584
0
  if (LUKS_keyslots_offset(hdr1) != (LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE)) {
585
0
    log_dbg(cd, "Unsupported keyslots material offset: %zu.", LUKS_keyslots_offset(hdr1));
586
0
    return -EINVAL;
587
0
  }
588
589
0
  if (LUKS2_check_cipher(cd, hdr1->keyBytes, hdr1->cipherName, hdr1->cipherMode)) {
590
0
    log_err(cd, _("Unable to use cipher specification %s-%s for LUKS2."),
591
0
      hdr1->cipherName, hdr1->cipherMode);
592
0
    return -EINVAL;
593
0
  }
594
595
0
  r = snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", hdr1->cipherName, hdr1->cipherMode);
596
0
  if (r < 0 || (size_t)r >= sizeof(cipher_spec))
597
0
    return -EINVAL;
598
0
  if (LUKS2_keyslot_cipher_incompatible(cd, cipher_spec)) {
599
0
    log_err(cd, _("Unable to use cipher specification %s-%s for LUKS2 keyslot."),
600
0
      hdr1->cipherName, hdr1->cipherMode);
601
0
    return -EINVAL;
602
0
  }
603
604
0
  if (luksmeta_header_present(cd, luks1_size))
605
0
    return -EINVAL;
606
607
0
  log_dbg(cd, "Max size: %" PRIu64 ", LUKS1 (full) header size %zu , required shift: %zu",
608
0
    max_size, luks1_size, luks1_shift);
609
610
0
  required_size = luks1_size + luks1_shift;
611
612
0
  if ((max_size < required_size) &&
613
0
      device_fallocate(crypt_metadata_device(cd), required_size)) {
614
0
    log_err(cd, _("Unable to move keyslot area. Not enough space."));
615
0
    return -EINVAL;
616
0
  }
617
618
0
  if (max_size < required_size)
619
0
    max_size = required_size;
620
621
  /* fix coverity false positive integer underflow */
622
0
  if (max_size < 2 * LUKS2_HDR_16K_LEN)
623
0
    return -EINVAL;
624
625
0
  r = json_luks1_object(hdr1, &jobj, max_size - 2 * LUKS2_HDR_16K_LEN);
626
0
  if (r < 0)
627
0
    return r;
628
629
0
  move_keyslot_offset(jobj, luks1_shift);
630
631
  /* Create and fill LUKS2 hdr */
632
0
  memset(hdr2, 0, sizeof(*hdr2));
633
0
  hdr2->hdr_size = LUKS2_HDR_16K_LEN;
634
0
  hdr2->seqid = 1;
635
0
  hdr2->version = 2;
636
0
  strncpy(hdr2->checksum_alg, "sha256", LUKS2_CHECKSUM_ALG_L);
637
0
  crypt_random_get(cd, (char*)hdr2->salt1, sizeof(hdr2->salt1), CRYPT_RND_SALT);
638
0
  crypt_random_get(cd, (char*)hdr2->salt2, sizeof(hdr2->salt2), CRYPT_RND_SALT);
639
0
  strncpy(hdr2->uuid, crypt_get_uuid(cd), LUKS2_UUID_L-1); /* UUID should be max 36 chars */
640
0
  hdr2->jobj = jobj;
641
642
  /*
643
   * It duplicates check in LUKS2_hdr_write() but we don't want to move
644
   * keyslot areas in case it would fail later
645
   */
646
0
  if (max_size < LUKS2_hdr_and_areas_size(hdr2)) {
647
0
    r = -EINVAL;
648
0
    goto out;
649
0
  }
650
651
  /* check future LUKS2 metadata before moving keyslots area */
652
0
  if (LUKS2_hdr_validate(cd, hdr2->jobj, hdr2->hdr_size - LUKS2_HDR_BIN_LEN)) {
653
0
    log_err(cd, _("Cannot convert to LUKS2 format - invalid metadata."));
654
0
    r = -EINVAL;
655
0
    goto out;
656
0
  }
657
658
0
  if ((r = luks_header_in_use(cd))) {
659
0
    if (r > 0)
660
0
      r = -EBUSY;
661
0
    goto out;
662
0
  }
663
664
  /* move keyslots 4k -> 32k offset */
665
0
  buf_offset = 2 * LUKS2_HDR_16K_LEN;
666
0
  buf_size   = luks1_size - LUKS_ALIGN_KEYSLOTS;
667
668
  /* check future LUKS2 keyslots area is at least as large as LUKS1 keyslots area */
669
0
  if (buf_size > LUKS2_keyslots_size(hdr2)) {
670
0
    log_err(cd, _("Unable to move keyslot area. LUKS2 keyslots area too small."));
671
0
    r = -EINVAL;
672
0
    goto out;
673
0
  }
674
675
0
  if ((r = move_keyslot_areas(cd, 8 * SECTOR_SIZE, buf_offset, buf_size)) < 0) {
676
0
    log_err(cd, _("Unable to move keyslot area."));
677
0
    goto out;
678
0
  }
679
680
  /* Write new LUKS2 JSON */
681
0
  r = LUKS2_hdr_write(cd, hdr2);
682
0
out:
683
0
  LUKS2_hdr_free(cd, hdr2);
684
685
0
  return r;
686
0
}
687
688
static int keyslot_LUKS1_compatible(struct crypt_device *cd, struct luks2_hdr *hdr,
689
            int keyslot, uint32_t key_size, const char *hash)
690
0
{
691
0
  json_object *jobj_keyslot, *jobj, *jobj_kdf, *jobj_af;
692
0
  uint64_t l2_offset, l2_length;
693
0
  size_t ks_key_size;
694
0
  const char *ks_cipher, *data_cipher;
695
696
0
  jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
697
0
  if (!jobj_keyslot)
698
0
    return 1;
699
700
  /* Keyslot type */
701
0
  if (!json_object_object_get_ex(jobj_keyslot, "type", &jobj))
702
0
    return 0;
703
0
  if (strcmp(json_object_get_string(jobj), "luks2")) {
704
0
    log_dbg(cd, "Keyslot %d type %s is not compatible.",
705
0
      keyslot, json_object_get_string(jobj));
706
0
    return 0;
707
0
  }
708
709
  /* Keyslot uses PBKDF2, this implies memory and parallel is not used. */
710
0
  jobj = NULL;
711
0
  if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf) ||
712
0
      !json_object_object_get_ex(jobj_kdf, "type", &jobj))
713
0
    return 0;
714
0
  if (strcmp(json_object_get_string(jobj), CRYPT_KDF_PBKDF2)) {
715
0
    log_dbg(cd, "Keyslot %d does not use PBKDF2.", keyslot);
716
0
    return 0;
717
0
  }
718
719
  /* Keyslot KDF hash is the same as the digest hash. */
720
0
  jobj = NULL;
721
0
  if (!json_object_object_get_ex(jobj_kdf, "hash", &jobj))
722
0
    return 0;
723
0
  if (strcmp(json_object_get_string(jobj), hash)) {
724
0
    log_dbg(cd, "Keyslot %d PBKDF uses different hash %s than digest hash %s.",
725
0
      keyslot, json_object_get_string(jobj), hash);
726
0
    return 0;
727
0
  }
728
729
  /* Keyslot AF use compatible striptes. */
730
0
  jobj = NULL;
731
0
  if (!json_object_object_get_ex(jobj_keyslot, "af", &jobj_af) ||
732
0
      !json_object_object_get_ex(jobj_af, "stripes", &jobj))
733
0
    return 0;
734
0
  if (json_object_get_int(jobj) != LUKS_STRIPES) {
735
0
    log_dbg(cd, "Keyslot %d AF uses incompatible stripes count.", keyslot);
736
0
    return 0;
737
0
  }
738
739
  /* Keyslot AF hash is the same as the digest hash. */
740
0
  jobj = NULL;
741
0
  if (!json_object_object_get_ex(jobj_af, "hash", &jobj))
742
0
    return 0;
743
0
  if (strcmp(json_object_get_string(jobj), hash)) {
744
0
    log_dbg(cd, "Keyslot %d AF uses different hash %s than digest hash %s.",
745
0
      keyslot, json_object_get_string(jobj), hash);
746
0
    return 0;
747
0
  }
748
749
0
  ks_cipher = LUKS2_get_keyslot_cipher(hdr, keyslot, &ks_key_size);
750
0
  data_cipher = LUKS2_get_cipher(hdr, CRYPT_DEFAULT_SEGMENT);
751
0
  if (!ks_cipher || !data_cipher || key_size != ks_key_size || strcmp(ks_cipher, data_cipher)) {
752
0
    log_dbg(cd, "Cipher in keyslot %d is different from volume key encryption.", keyslot);
753
0
    return 0;
754
0
  }
755
756
0
  if (LUKS2_keyslot_area(hdr, keyslot, &l2_offset, &l2_length))
757
0
    return 0;
758
759
0
  if (l2_length != (size_round_up(AF_split_sectors(key_size, LUKS_STRIPES) * SECTOR_SIZE, 4096))) {
760
0
    log_dbg(cd, "Area length in LUKS2 keyslot (%d) is not compatible with LUKS1", keyslot);
761
0
    return 0;
762
0
  }
763
764
0
  return 1;
765
0
}
766
767
/* Convert LUKS2 -> LUKS1 */
768
int LUKS2_luks2_to_luks1(struct crypt_device *cd, struct luks2_hdr *hdr2, struct luks_phdr *hdr1)
769
0
{
770
0
  size_t buf_size, buf_offset;
771
0
  char cipher[LUKS_CIPHERNAME_L], cipher_mode[LUKS_CIPHERMODE_L];
772
0
  char *digest, *digest_salt;
773
0
  const char *hash;
774
0
  size_t len;
775
0
  json_object *jobj_keyslot, *jobj_digest, *jobj_segment, *jobj_kdf, *jobj_area, *jobj1, *jobj2;
776
0
  uint32_t key_size;
777
0
  int i, r, last_active = 0;
778
0
  uint64_t offset, area_length;
779
0
  char *buf, luksMagic[] = LUKS_MAGIC;
780
0
  crypt_keyslot_info ki;
781
782
0
  jobj_digest  = LUKS2_get_digest_jobj(hdr2, 0);
783
0
  if (!jobj_digest)
784
0
    return -EINVAL;
785
786
0
  jobj_segment = LUKS2_get_segment_jobj(hdr2, CRYPT_DEFAULT_SEGMENT);
787
0
  if (!jobj_segment)
788
0
    return -EINVAL;
789
790
0
  if (json_segment_get_sector_size(jobj_segment) != SECTOR_SIZE) {
791
0
    log_err(cd, _("Cannot convert to LUKS1 format - default segment encryption sector size is not 512 bytes."));
792
0
    return -EINVAL;
793
0
  }
794
795
0
  json_object_object_get_ex(hdr2->jobj, "digests", &jobj1);
796
0
  if (!json_object_object_get_ex(jobj_digest, "type", &jobj2) ||
797
0
      strcmp(json_object_get_string(jobj2), "pbkdf2") ||
798
0
      json_object_object_length(jobj1) != 1) {
799
0
    log_err(cd, _("Cannot convert to LUKS1 format - key slot digests are not LUKS1 compatible."));
800
0
    return -EINVAL;
801
0
  }
802
0
  if (!json_object_object_get_ex(jobj_digest, "hash", &jobj2))
803
0
    return -EINVAL;
804
0
  hash = json_object_get_string(jobj2);
805
0
  if (crypt_hash_size(hash) < 0)
806
0
    return -EINVAL;
807
808
0
  r = crypt_parse_name_and_mode(LUKS2_get_cipher(hdr2, CRYPT_DEFAULT_SEGMENT), cipher, NULL, cipher_mode);
809
0
  if (r < 0)
810
0
    return r;
811
812
0
  if (crypt_cipher_wrapped_key(cipher, cipher_mode)) {
813
0
    log_err(cd, _("Cannot convert to LUKS1 format - device uses wrapped key cipher %s."), cipher);
814
0
    return -EINVAL;
815
0
  }
816
817
0
  if (json_segments_count(LUKS2_get_segments_jobj(hdr2)) != 1) {
818
0
    log_err(cd, _("Cannot convert to LUKS1 format - device uses more segments."));
819
0
    return -EINVAL;
820
0
  }
821
822
0
  r = LUKS2_tokens_count(hdr2);
823
0
  if (r < 0)
824
0
    return r;
825
0
  if (r > 0) {
826
0
    log_err(cd, _("Cannot convert to LUKS1 format - LUKS2 header contains %u token(s)."), r);
827
0
    return -EINVAL;
828
0
  }
829
830
0
  r = LUKS2_get_volume_key_size(hdr2, 0);
831
0
  if (r < 0) {
832
0
    log_err(cd, _("Cannot convert to LUKS1 format - there are no active keyslots."), r);
833
0
    return -EINVAL;
834
0
  }
835
0
  key_size = r;
836
837
0
  for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
838
0
    ki = LUKS2_keyslot_info(hdr2, i);
839
840
0
    if (ki == CRYPT_SLOT_INACTIVE)
841
0
      continue;
842
843
0
    if (ki == CRYPT_SLOT_INVALID) {
844
0
      log_err(cd, _("Cannot convert to LUKS1 format - keyslot %u is in invalid state."), i);
845
0
      return -EINVAL;
846
0
    }
847
848
0
    if (ki == CRYPT_SLOT_UNBOUND) {
849
0
      log_err(cd, _("Cannot convert to LUKS1 format - keyslot %u is unbound."), i);
850
0
      return -EINVAL;
851
0
    }
852
853
0
    if (i >= LUKS_NUMKEYS) {
854
0
      log_err(cd, _("Cannot convert to LUKS1 format - slot %u (over maximum slots) is still active."), i);
855
0
      return -EINVAL;
856
0
    }
857
858
0
    if (!keyslot_LUKS1_compatible(cd, hdr2, i, key_size, hash)) {
859
0
      log_err(cd, _("Cannot convert to LUKS1 format - keyslot %u is not LUKS1 compatible."), i);
860
0
      return -EINVAL;
861
0
    }
862
0
  }
863
864
0
  memset(hdr1, 0, sizeof(*hdr1));
865
866
0
  for (i = 0; i < LUKS_NUMKEYS; i++) {
867
0
    hdr1->keyblock[i].active = LUKS_KEY_DISABLED;
868
0
    hdr1->keyblock[i].stripes = LUKS_STRIPES;
869
870
0
    jobj_keyslot = LUKS2_get_keyslot_jobj(hdr2, i);
871
872
0
    if (jobj_keyslot) {
873
0
      if (!json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
874
0
        return -EINVAL;
875
0
      if (!json_object_object_get_ex(jobj_area, "offset", &jobj1))
876
0
        return -EINVAL;
877
0
      offset = crypt_jobj_get_uint64(jobj1);
878
0
    } else {
879
0
      if (LUKS2_find_area_gap(cd, hdr2, key_size, &offset, &area_length))
880
0
        return -EINVAL;
881
      /*
882
       * We have to create placeholder luks2 keyslots in place of all
883
       * inactive keyslots. Otherwise we would allocate all
884
       * inactive luks1 keyslots over same binary keyslot area.
885
       */
886
0
      if (placeholder_keyslot_alloc(cd, i, offset, area_length))
887
0
        return -EINVAL;
888
0
    }
889
890
0
    offset /= SECTOR_SIZE;
891
0
    if (offset > UINT32_MAX)
892
0
      return -EINVAL;
893
894
0
    hdr1->keyblock[i].keyMaterialOffset = offset;
895
0
    hdr1->keyblock[i].keyMaterialOffset -=
896
0
        ((2 * LUKS2_HDR_16K_LEN - LUKS_ALIGN_KEYSLOTS) / SECTOR_SIZE);
897
898
0
    if (!jobj_keyslot)
899
0
      continue;
900
901
0
    hdr1->keyblock[i].active = LUKS_KEY_ENABLED;
902
0
    last_active = i;
903
904
0
    if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf))
905
0
      continue;
906
907
0
    if (!json_object_object_get_ex(jobj_kdf, "iterations", &jobj1))
908
0
      continue;
909
0
    hdr1->keyblock[i].passwordIterations = crypt_jobj_get_uint32(jobj1);
910
911
0
    if (!json_object_object_get_ex(jobj_kdf, "salt", &jobj1))
912
0
      continue;
913
914
0
    if (crypt_base64_decode(&buf, &len, json_object_get_string(jobj1),
915
0
          json_object_get_string_len(jobj1)))
916
0
      continue;
917
0
    if (len > 0 && len != LUKS_SALTSIZE) {
918
0
      free(buf);
919
0
      continue;
920
0
    }
921
0
    memcpy(hdr1->keyblock[i].passwordSalt, buf, LUKS_SALTSIZE);
922
0
    free(buf);
923
0
  }
924
925
0
  if (!jobj_keyslot) {
926
0
    jobj_keyslot = LUKS2_get_keyslot_jobj(hdr2, last_active);
927
0
    if (!jobj_keyslot)
928
0
      return -EINVAL;
929
0
  }
930
931
0
  if (!json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
932
0
    return -EINVAL;
933
0
  if (!json_object_object_get_ex(jobj_area, "encryption", &jobj1))
934
0
    return -EINVAL;
935
0
  r = crypt_parse_name_and_mode(json_object_get_string(jobj1), cipher, NULL, cipher_mode);
936
0
  if (r < 0)
937
0
    return r;
938
939
0
  strncpy(hdr1->cipherName, cipher, LUKS_CIPHERNAME_L - 1);
940
0
  hdr1->cipherName[LUKS_CIPHERNAME_L-1] = '\0';
941
0
  strncpy(hdr1->cipherMode, cipher_mode, LUKS_CIPHERMODE_L - 1);
942
0
  hdr1->cipherMode[LUKS_CIPHERMODE_L-1] = '\0';
943
944
0
  if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf))
945
0
    return -EINVAL;
946
0
  if (!json_object_object_get_ex(jobj_kdf, "hash", &jobj1))
947
0
    return -EINVAL;
948
0
  strncpy(hdr1->hashSpec, json_object_get_string(jobj1), sizeof(hdr1->hashSpec) - 1);
949
950
0
  hdr1->keyBytes = key_size;
951
952
0
  if (!json_object_object_get_ex(jobj_digest, "iterations", &jobj1))
953
0
    return -EINVAL;
954
0
  hdr1->mkDigestIterations = crypt_jobj_get_uint32(jobj1);
955
956
0
  if (!json_object_object_get_ex(jobj_digest, "digest", &jobj1))
957
0
    return -EINVAL;
958
0
  r = crypt_base64_decode(&digest, &len, json_object_get_string(jobj1),
959
0
        json_object_get_string_len(jobj1));
960
0
  if (r < 0)
961
0
    return r;
962
  /* We can store full digest here, not only sha1 length */
963
0
  if (len < LUKS_DIGESTSIZE) {
964
0
    free(digest);
965
0
    return -EINVAL;
966
0
  }
967
0
  memcpy(hdr1->mkDigest, digest, LUKS_DIGESTSIZE);
968
0
  free(digest);
969
970
0
  if (!json_object_object_get_ex(jobj_digest, "salt", &jobj1))
971
0
    return -EINVAL;
972
0
  r = crypt_base64_decode(&digest_salt, &len, json_object_get_string(jobj1),
973
0
        json_object_get_string_len(jobj1));
974
0
  if (r < 0)
975
0
    return r;
976
0
  if (len != LUKS_SALTSIZE) {
977
0
    free(digest_salt);
978
0
    return -EINVAL;
979
0
  }
980
0
  memcpy(hdr1->mkDigestSalt, digest_salt, LUKS_SALTSIZE);
981
0
  free(digest_salt);
982
983
0
  if (!json_object_object_get_ex(jobj_segment, "offset", &jobj1))
984
0
    return -EINVAL;
985
0
  offset = crypt_jobj_get_uint64(jobj1) / SECTOR_SIZE;
986
0
  if (offset > UINT32_MAX)
987
0
    return -EINVAL;
988
0
  hdr1->payloadOffset = offset;
989
990
0
  strncpy(hdr1->uuid, hdr2->uuid, UUID_STRING_L); /* max 36 chars */
991
0
  hdr1->uuid[UUID_STRING_L-1] = '\0';
992
993
0
  memcpy(hdr1->magic, luksMagic, LUKS_MAGIC_L);
994
995
0
  hdr1->version = 1;
996
997
0
  r = luks_header_in_use(cd);
998
0
  if (r)
999
0
    return r > 0 ? -EBUSY : r;
1000
1001
  /* move keyslots 32k -> 4k offset */
1002
0
  buf_offset = 2 * LUKS2_HDR_16K_LEN;
1003
0
  buf_size   = LUKS2_keyslots_size(hdr2);
1004
0
  r = move_keyslot_areas(cd, buf_offset, 8 * SECTOR_SIZE, buf_size);
1005
0
  if (r < 0) {
1006
0
    log_err(cd, _("Unable to move keyslot area."));
1007
0
    return r;
1008
0
  }
1009
1010
0
  crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0,
1011
0
        8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
1012
1013
  /* Write new LUKS1 hdr */
1014
0
  return LUKS_write_phdr(hdr1, cd);
1015
0
}