Lines Matching refs:cd
55 void crypt_log(struct crypt_device *cd, int level, const char *msg) in crypt_log() argument
57 if (cd && cd->log) in crypt_log()
58 cd->log(level, msg, cd->log_usrptr); in crypt_log()
63 void logger(struct crypt_device *cd, int level, const char *file, in logger() argument
73 crypt_log(cd, level, target); in logger()
93 static char *process_key(struct crypt_device *cd, const char *hash_name, in process_key() argument
103 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"), in process_key()
115 log_err(cd, _("Key processing error (using hash algorithm %s).\n"), in process_key()
165 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot) in keyslot_verify_or_find_empty() argument
168 *keyslot = LUKS_keyslot_find_empty(&cd->hdr); in keyslot_verify_or_find_empty()
170 log_err(cd, _("All key slots full.\n")); in keyslot_verify_or_find_empty()
175 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) { in keyslot_verify_or_find_empty()
177 … log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"), in keyslot_verify_or_find_empty()
183 log_err(cd, _("Key slot %d is full, please select another one.\n"), in keyslot_verify_or_find_empty()
191 static int verify_other_keyslot(struct crypt_device *cd, in verify_other_keyslot() argument
203 &passwordLen, 0, key_file, cd->timeout, flags, cd); in verify_other_keyslot()
207 ki = crypt_keyslot_status(cd, keyIndex); in verify_other_keyslot()
209 LUKS_keyslot_set(&cd->hdr, keyIndex, 0); in verify_other_keyslot()
211 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, in verify_other_keyslot()
213 &cd->hdr, &mk, cd); in verify_other_keyslot()
216 LUKS_keyslot_set(&cd->hdr, keyIndex, 1); in verify_other_keyslot()
223 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex); in verify_other_keyslot()
227 static int find_keyslot_by_passphrase(struct crypt_device *cd, in find_keyslot_by_passphrase() argument
238 cd->timeout, flags, cd); in find_keyslot_by_passphrase()
242 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, in find_keyslot_by_passphrase()
243 passwordLen, &cd->hdr, &mk, cd); in find_keyslot_by_passphrase()
250 static int device_check_and_adjust(struct crypt_device *cd, in device_check_and_adjust() argument
257 if (!device || get_device_infos(device, &infos, cd) < 0) { in device_check_and_adjust()
258 log_err(cd, _("Cannot get info about device %s.\n"), in device_check_and_adjust()
266 log_err(cd, _("Device %s has zero size.\n"), device); in device_check_and_adjust()
270 log_err(cd, _("Device %s is too small.\n"), device); in device_check_and_adjust()
284 static int luks_remove_helper(struct crypt_device *cd, in luks_remove_helper() argument
294 key_slot = find_keyslot_by_passphrase(cd, key_file, 0, in luks_remove_helper()
301 log_std(cd, _("key slot %d selected for deletion.\n"), key_slot); in luks_remove_helper()
304 ki = crypt_keyslot_status(cd, key_slot); in luks_remove_helper()
306 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"), in luks_remove_helper()
312 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot); in luks_remove_helper()
317 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm && in luks_remove_helper()
318 !(cd->confirm(_("This is the last keyslot." in luks_remove_helper()
320 cd->confirm_usrptr))) { in luks_remove_helper()
326 r = verify_other_keyslot(cd, other_key_file, 0, key_slot); in luks_remove_helper()
331 r = crypt_keyslot_destroy(cd, key_slot); in luks_remove_helper()
336 static int create_device_helper(struct crypt_device *cd, in create_device_helper() argument
358 ci = crypt_status(cd, name); in create_device_helper()
366 log_err(cd, _("Device %s already exists.\n"), name); in create_device_helper()
371 log_err(cd, _("Invalid key size %d.\n"), key_size); in create_device_helper()
375 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only); in create_device_helper()
382 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen); in create_device_helper()
386 … r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset, in create_device_helper()
394 static int open_from_hdr_and_mk(struct crypt_device *cd, in open_from_hdr_and_mk() argument
404 offset = crypt_get_data_offset(cd); in open_from_hdr_and_mk()
408 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only); in open_from_hdr_and_mk()
412 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd), in open_from_hdr_and_mk()
413 crypt_get_cipher_mode(cd)) < 0) in open_from_hdr_and_mk()
416 r = dm_create_device(name, cd->device, cipher, cd->type, in open_from_hdr_and_mk()
417 no_uuid ? NULL : crypt_get_uuid(cd), in open_from_hdr_and_mk()
436 int crypt_confirm(struct crypt_device *cd, const char *msg) in crypt_confirm() argument
438 if (!cd || !cd->confirm) in crypt_confirm()
441 return cd->confirm(msg, cd->confirm_usrptr); in crypt_confirm()
444 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key, in key_from_terminal() argument
449 if (cd->password) { in key_from_terminal()
453 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr); in key_from_terminal()
460 if (force_verify || cd->password_verify) in key_from_terminal()
462 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd); in key_from_terminal()
466 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot, in volume_key_by_terminal_passphrase() argument
471 int r = -EINVAL, tries = cd->tries; in volume_key_by_terminal_passphrase()
473 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0) in volume_key_by_terminal_passphrase()
482 key_from_terminal(cd, prompt, &passphrase_read, in volume_key_by_terminal_passphrase()
489 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read, in volume_key_by_terminal_passphrase()
490 passphrase_size_read, &cd->hdr, mk, cd); in volume_key_by_terminal_passphrase()
505 static void key_from_file(struct crypt_device *cd, char *msg, in key_from_file() argument
509 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd); in key_from_file()
512 static int _crypt_init(struct crypt_device **cd, in _crypt_init() argument
528 r = crypt_init_by_name(cd, options->name); in _crypt_init()
530 r = crypt_init(cd, options->device); in _crypt_init()
537 crypt_set_log_callback(*cd, log_wrapper, options->icb->log); in _crypt_init()
538 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog); in _crypt_init()
540 crypt_set_timeout(*cd, options->timeout); in _crypt_init()
541 crypt_set_password_retry(*cd, options->tries); in _crypt_init()
542 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000); in _crypt_init()
543 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY); in _crypt_init()
546 r = crypt_load(*cd, type, NULL); in _crypt_init()
548 if (!r && type && !(*cd)->type) { in _crypt_init()
549 (*cd)->type = strdup(type); in _crypt_init()
550 if (!(*cd)->type) in _crypt_init()
555 crypt_free(*cd); in _crypt_init()
560 void crypt_set_log_callback(struct crypt_device *cd, in crypt_set_log_callback() argument
564 if (!cd) in crypt_set_log_callback()
567 cd->log = log; in crypt_set_log_callback()
568 cd->log_usrptr = usrptr; in crypt_set_log_callback()
572 void crypt_set_confirm_callback(struct crypt_device *cd, in crypt_set_confirm_callback() argument
576 cd->confirm = confirm; in crypt_set_confirm_callback()
577 cd->confirm_usrptr = usrptr; in crypt_set_confirm_callback()
580 void crypt_set_password_callback(struct crypt_device *cd, in crypt_set_password_callback() argument
584 cd->password = password; in crypt_set_password_callback()
585 cd->password_usrptr = usrptr; in crypt_set_password_callback()
593 struct crypt_device *cd = NULL; in crypt_create_and_update_device() local
598 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1); in crypt_create_and_update_device()
603 options->key_file, cd->timeout, options->flags, cd); in crypt_create_and_update_device()
607 r = create_device_helper(cd, options->name, options->hash, in crypt_create_and_update_device()
614 crypt_free(cd); in crypt_create_and_update_device()
631 struct crypt_device *cd = NULL; in crypt_resize_device() local
663 r = _crypt_init(&cd, type, options, 1, 1); in crypt_resize_device()
668 r = device_check_and_adjust(cd, device, &size, &offset, &read_only); in crypt_resize_device()
673 crypt_get_uuid(cd), size, skip, offset, in crypt_resize_device()
682 crypt_free(cd); in crypt_resize_device()
723 struct crypt_device *cd = NULL; in crypt_remove_device() local
726 r = crypt_init_by_name(&cd, options->name); in crypt_remove_device()
728 r = crypt_deactivate(cd, options->name); in crypt_remove_device()
730 crypt_free(cd); in crypt_remove_device()
743 struct crypt_device *cd = NULL; in crypt_luksFormat() local
752 log_err(cd, _("No known cipher specification pattern detected.\n")); in crypt_luksFormat()
756 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1))) in crypt_luksFormat()
760 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"), in crypt_luksFormat()
767 options->new_key_file, options->timeout, options->flags, cd); in crypt_luksFormat()
774 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode, in crypt_luksFormat()
780 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0, in crypt_luksFormat()
783 crypt_free(cd); in crypt_luksFormat()
791 struct crypt_device *cd = NULL; in crypt_luksOpen() local
798 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1); in crypt_luksOpen()
809 r = crypt_activate_by_keyfile(cd, options->name, in crypt_luksOpen()
813 r = crypt_activate_by_passphrase(cd, options->name, in crypt_luksOpen()
818 crypt_free(cd); in crypt_luksOpen()
825 struct crypt_device *cd = NULL; in crypt_luksKillSlot() local
828 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1); in crypt_luksKillSlot()
832 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL, in crypt_luksKillSlot()
835 crypt_free(cd); in crypt_luksKillSlot()
842 struct crypt_device *cd = NULL; in crypt_luksRemoveKey() local
845 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1); in crypt_luksRemoveKey()
849 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file, in crypt_luksRemoveKey()
852 crypt_free(cd); in crypt_luksRemoveKey()
861 struct crypt_device *cd = NULL; in crypt_luksAddKey() local
864 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1); in crypt_luksAddKey()
869 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot, in crypt_luksAddKey()
873 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot, in crypt_luksAddKey()
876 crypt_free(cd); in crypt_luksAddKey()
883 struct crypt_device *cd = NULL; in crypt_luksUUID() local
887 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0); in crypt_luksUUID()
891 uuid = (char *)crypt_get_uuid(cd); in crypt_luksUUID()
892 log_std(cd, uuid ?: ""); in crypt_luksUUID()
893 log_std(cd, "\n"); in crypt_luksUUID()
894 crypt_free(cd); in crypt_luksUUID()
901 struct crypt_device *cd = NULL; in crypt_isLuks() local
907 log_err(cd, _("Cannot initialize crypto backend.\n")); in crypt_isLuks()
911 r = crypt_init(&cd, options->device); in crypt_isLuks()
916 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0; in crypt_isLuks()
918 crypt_free(cd); in crypt_isLuks()
925 struct crypt_device *cd = NULL; in crypt_luksDump() local
928 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0); in crypt_luksDump()
932 r = crypt_dump(cd); in crypt_luksDump()
934 crypt_free(cd); in crypt_luksDump()
976 int crypt_init(struct crypt_device **cd, const char *device) in crypt_init() argument
980 if (!cd) in crypt_init()
1010 *cd = h; in crypt_init()
1014 int crypt_init_by_name(struct crypt_device **cd, const char *name) in crypt_init_by_name() argument
1040 r = crypt_init(cd, device); in crypt_init_by_name()
1046 static int _crypt_format_plain(struct crypt_device *cd, in _crypt_format_plain() argument
1053 log_err(cd, _("Invalid plain crypt parameters.\n")); in _crypt_format_plain()
1057 if (cd->volume_key->keyLength > 1024) { in _crypt_format_plain()
1058 log_err(cd, _("Invalid key size.\n")); in _crypt_format_plain()
1062 cd->plain_cipher = strdup(cipher); in _crypt_format_plain()
1063 cd->plain_cipher_mode = strdup(cipher_mode); in _crypt_format_plain()
1066 cd->plain_uuid = strdup(uuid); in _crypt_format_plain()
1069 cd->plain_hdr.hash = strdup(params->hash); in _crypt_format_plain()
1071 cd->plain_hdr.offset = params ? params->offset : 0; in _crypt_format_plain()
1072 cd->plain_hdr.skip = params ? params->skip : 0; in _crypt_format_plain()
1074 if (!cd->plain_cipher || !cd->plain_cipher_mode) in _crypt_format_plain()
1080 static int _crypt_format_luks1(struct crypt_device *cd, in _crypt_format_luks1() argument
1090 if (!cd->device) { in _crypt_format_luks1()
1091 log_err(cd, _("Can't format LUKS without device.\n")); in _crypt_format_luks1()
1098 get_topology_alignment(cd->device, &required_alignment, in _crypt_format_luks1()
1101 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode, in _crypt_format_luks1()
1106 cd->iteration_time, &cd->PBKDF2_per_sec, cd); in _crypt_format_luks1()
1111 r = wipe_device_header(cd->device, 8); in _crypt_format_luks1()
1113 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device); in _crypt_format_luks1()
1117 r = LUKS_write_phdr(cd->device, &cd->hdr, cd); in _crypt_format_luks1()
1122 int crypt_format(struct crypt_device *cd, in crypt_format() argument
1133 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)"); in crypt_format()
1140 log_err(cd, _("Cannot initialize crypto backend.\n")); in crypt_format()
1145 cd->volume_key = LUKS_alloc_masterkey(volume_key_size, in crypt_format()
1148 cd->volume_key = LUKS_generate_masterkey(volume_key_size); in crypt_format()
1150 if(!cd->volume_key) in crypt_format()
1154 r = _crypt_format_plain(cd, cipher, cipher_mode, in crypt_format()
1157 r = _crypt_format_luks1(cd, cipher, cipher_mode, in crypt_format()
1161 log_err(cd, _("Unknown crypt device type %s requested.\n"), type); in crypt_format()
1165 if (!r && !(cd->type = strdup(type))) in crypt_format()
1169 LUKS_dealloc_masterkey(cd->volume_key); in crypt_format()
1170 cd->volume_key = NULL; in crypt_format()
1176 int crypt_load(struct crypt_device *cd, in crypt_load() argument
1184 requested_type ?: "any", cd->device ?: "(none)"); in crypt_load()
1186 if (!cd->device) in crypt_load()
1194 log_err(cd, _("Cannot initialize crypto backend.\n")); in crypt_load()
1198 r = LUKS_read_phdr(cd->device, &hdr, 1, cd); in crypt_load()
1201 memcpy(&cd->hdr, &hdr, sizeof(hdr)); in crypt_load()
1202 cd->type = strdup(requested_type); in crypt_load()
1203 if (!cd->type) in crypt_load()
1210 int crypt_header_backup(struct crypt_device *cd, in crypt_header_backup() argument
1219 log_err(cd, _("Cannot initialize crypto backend.\n")); in crypt_header_backup()
1224 "file %s.", cd->device, requested_type, backup_file); in crypt_header_backup()
1226 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd); in crypt_header_backup()
1229 int crypt_header_restore(struct crypt_device *cd, in crypt_header_restore() argument
1238 log_err(cd, _("Cannot initialize crypto backend.\n")); in crypt_header_restore()
1243 "file %s.", cd->device, requested_type, backup_file); in crypt_header_restore()
1245 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd); in crypt_header_restore()
1248 void crypt_free(struct crypt_device *cd) in crypt_free() argument
1250 if (cd) { in crypt_free()
1251 log_dbg("Releasing crypt device %s context.", cd->device); in crypt_free()
1254 if (cd->volume_key) in crypt_free()
1255 LUKS_dealloc_masterkey(cd->volume_key); in crypt_free()
1257 free(cd->device); in crypt_free()
1258 free(cd->type); in crypt_free()
1261 free((char*)cd->plain_hdr.hash); in crypt_free()
1262 free(cd->plain_cipher); in crypt_free()
1263 free(cd->plain_cipher_mode); in crypt_free()
1264 free(cd->plain_uuid); in crypt_free()
1266 free(cd); in crypt_free()
1270 int crypt_suspend(struct crypt_device *cd, in crypt_suspend() argument
1280 log_err(cd, _("Volume %s is not active.\n"), name); in crypt_suspend()
1284 if (!cd && dm_init(NULL, 1) < 0) in crypt_suspend()
1293 log_err(cd, _("Volume %s is already suspended.\n"), name); in crypt_suspend()
1300 log_err(cd, "Suspend is not supported for device %s.\n", name); in crypt_suspend()
1302 log_err(cd, "Error during suspending device %s.\n", name); in crypt_suspend()
1304 if (!cd) in crypt_suspend()
1309 int crypt_resume_by_passphrase(struct crypt_device *cd, in crypt_resume_by_passphrase() argument
1320 if (!isLUKS(cd->type)) { in crypt_resume_by_passphrase()
1321 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_resume_by_passphrase()
1332 log_err(cd, _("Volume %s is not suspended.\n"), name); in crypt_resume_by_passphrase()
1337 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase, in crypt_resume_by_passphrase()
1338 passphrase_size, &cd->hdr, &mk, cd); in crypt_resume_by_passphrase()
1340 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk); in crypt_resume_by_passphrase()
1346 log_err(cd, "Resume is not supported for device %s.\n", name); in crypt_resume_by_passphrase()
1348 log_err(cd, "Error during resuming device %s.\n", name); in crypt_resume_by_passphrase()
1356 int crypt_resume_by_keyfile(struct crypt_device *cd, in crypt_resume_by_keyfile() argument
1369 if (!isLUKS(cd->type)) { in crypt_resume_by_keyfile()
1370 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_resume_by_keyfile()
1381 log_err(cd, _("Volume %s is not suspended.\n"), name); in crypt_resume_by_keyfile()
1388 key_from_file(cd, _("Enter passphrase: "), &passphrase_read, in crypt_resume_by_keyfile()
1394 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read, in crypt_resume_by_keyfile()
1395 passphrase_size_read, &cd->hdr, &mk, cd); in crypt_resume_by_keyfile()
1403 log_err(cd, "Error during resuming device %s.\n", name); in crypt_resume_by_keyfile()
1412 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd, in crypt_keyslot_add_by_passphrase() argument
1428 if (!isLUKS(cd->type)) { in crypt_keyslot_add_by_passphrase()
1429 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_keyslot_add_by_passphrase()
1433 r = keyslot_verify_or_find_empty(cd, &keyslot); in crypt_keyslot_add_by_passphrase()
1437 if (!LUKS_keyslot_active_count(&cd->hdr)) { in crypt_keyslot_add_by_passphrase()
1439 if (cd->volume_key) { in crypt_keyslot_add_by_passphrase()
1440 … mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key); in crypt_keyslot_add_by_passphrase()
1443 … log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n")); in crypt_keyslot_add_by_passphrase()
1448 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase, in crypt_keyslot_add_by_passphrase()
1449 passphrase_size, &cd->hdr, &mk, cd); in crypt_keyslot_add_by_passphrase()
1452 key_from_terminal(cd, _("Enter any passphrase: "), in crypt_keyslot_add_by_passphrase()
1459 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, in crypt_keyslot_add_by_passphrase()
1460 passwordLen, &cd->hdr, &mk, cd); in crypt_keyslot_add_by_passphrase()
1471 key_from_terminal(cd, _("Enter new passphrase for key slot: "), in crypt_keyslot_add_by_passphrase()
1479 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen, in crypt_keyslot_add_by_passphrase()
1480 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd); in crypt_keyslot_add_by_passphrase()
1491 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd, in crypt_keyslot_add_by_keyfile() argument
1506 if (!isLUKS(cd->type)) { in crypt_keyslot_add_by_keyfile()
1507 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_keyslot_add_by_keyfile()
1511 r = keyslot_verify_or_find_empty(cd, &keyslot); in crypt_keyslot_add_by_keyfile()
1515 if (!LUKS_keyslot_active_count(&cd->hdr)) { in crypt_keyslot_add_by_keyfile()
1517 if (cd->volume_key) { in crypt_keyslot_add_by_keyfile()
1518 … mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key); in crypt_keyslot_add_by_keyfile()
1521 … log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n")); in crypt_keyslot_add_by_keyfile()
1527 … key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen, in crypt_keyslot_add_by_keyfile()
1530 key_from_terminal(cd, _("Enter any passphrase: "), in crypt_keyslot_add_by_keyfile()
1536 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen, in crypt_keyslot_add_by_keyfile()
1537 &cd->hdr, &mk, cd); in crypt_keyslot_add_by_keyfile()
1545 key_from_file(cd, _("Enter new passphrase for key slot: "), in crypt_keyslot_add_by_keyfile()
1549 key_from_terminal(cd, _("Enter new passphrase for key slot: "), in crypt_keyslot_add_by_keyfile()
1557 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen, in crypt_keyslot_add_by_keyfile()
1558 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd); in crypt_keyslot_add_by_keyfile()
1565 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd, in crypt_keyslot_add_by_volume_key() argument
1578 if (!isLUKS(cd->type)) { in crypt_keyslot_add_by_volume_key()
1579 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_keyslot_add_by_volume_key()
1585 else if (cd->volume_key) in crypt_keyslot_add_by_volume_key()
1586 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key); in crypt_keyslot_add_by_volume_key()
1591 r = LUKS_verify_master_key(&cd->hdr, mk); in crypt_keyslot_add_by_volume_key()
1593 log_err(cd, _("Volume key does not match the volume.\n")); in crypt_keyslot_add_by_volume_key()
1597 r = keyslot_verify_or_find_empty(cd, &keyslot); in crypt_keyslot_add_by_volume_key()
1602 key_from_terminal(cd, _("Enter new passphrase for key slot: "), in crypt_keyslot_add_by_volume_key()
1608 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size, in crypt_keyslot_add_by_volume_key()
1609 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd); in crypt_keyslot_add_by_volume_key()
1617 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot) in crypt_keyslot_destroy() argument
1623 if (!isLUKS(cd->type)) { in crypt_keyslot_destroy()
1624 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_keyslot_destroy()
1628 ki = crypt_keyslot_status(cd, keyslot); in crypt_keyslot_destroy()
1630 log_err(cd, _("Key slot %d is invalid.\n"), keyslot); in crypt_keyslot_destroy()
1635 log_err(cd, _("Key slot %d is not used.\n"), keyslot); in crypt_keyslot_destroy()
1639 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd); in crypt_keyslot_destroy()
1643 int crypt_activate_by_passphrase(struct crypt_device *cd, in crypt_activate_by_passphrase() argument
1663 if (isPLAIN(cd->type)) in crypt_activate_by_passphrase()
1664 return create_device_helper(cd, name, cd->plain_hdr.hash, in crypt_activate_by_passphrase()
1665 … cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size, in crypt_activate_by_passphrase()
1666 cd->volume_key->keyLength, 0, cd->plain_hdr.skip, in crypt_activate_by_passphrase()
1667 … cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0); in crypt_activate_by_passphrase()
1674 log_err(cd, _("Device %s already exists.\n"), name); in crypt_activate_by_passphrase()
1679 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0) in crypt_activate_by_passphrase()
1684 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase, in crypt_activate_by_passphrase()
1685 passphrase_size, &cd->hdr, &mk, cd); in crypt_activate_by_passphrase()
1687 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk); in crypt_activate_by_passphrase()
1692 r = open_from_hdr_and_mk(cd, mk, name, flags); in crypt_activate_by_passphrase()
1701 int crypt_activate_by_keyfile(struct crypt_device *cd, in crypt_activate_by_keyfile() argument
1717 if (!isLUKS(cd->type)) { in crypt_activate_by_keyfile()
1718 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_activate_by_keyfile()
1727 log_err(cd, _("Device %s already exists.\n"), name); in crypt_activate_by_keyfile()
1735 key_from_file(cd, _("Enter passphrase: "), &passphrase_read, in crypt_activate_by_keyfile()
1740 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read, in crypt_activate_by_keyfile()
1741 passphrase_size_read, &cd->hdr, &mk, cd); in crypt_activate_by_keyfile()
1747 r = open_from_hdr_and_mk(cd, mk, name, flags); in crypt_activate_by_keyfile()
1755 int crypt_activate_by_volume_key(struct crypt_device *cd, in crypt_activate_by_volume_key() argument
1768 if (isPLAIN(cd->type)) in crypt_activate_by_volume_key()
1769 return create_device_helper(cd, name, NULL, in crypt_activate_by_volume_key()
1770 … cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size, in crypt_activate_by_volume_key()
1771 cd->volume_key->keyLength, 0, cd->plain_hdr.skip, in crypt_activate_by_volume_key()
1772 … cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0); in crypt_activate_by_volume_key()
1774 if (!isLUKS(cd->type)) { in crypt_activate_by_volume_key()
1775 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_activate_by_volume_key()
1784 log_err(cd, _("Device %s already exists.\n"), name); in crypt_activate_by_volume_key()
1792 r = LUKS_verify_master_key(&cd->hdr, mk); in crypt_activate_by_volume_key()
1795 log_err(cd, _("Volume key does not match the volume.\n")); in crypt_activate_by_volume_key()
1798 r = open_from_hdr_and_mk(cd, mk, name, flags); in crypt_activate_by_volume_key()
1805 int crypt_deactivate(struct crypt_device *cd, const char *name) in crypt_deactivate() argument
1814 if (!cd && dm_init(NULL, 1) < 0) in crypt_deactivate()
1817 switch (crypt_status(cd, name)) { in crypt_deactivate()
1822 log_err(cd, _("Device %s is busy.\n"), name); in crypt_deactivate()
1826 log_err(cd, _("Device %s is not active.\n"), name); in crypt_deactivate()
1830 log_err(cd, _("Invalid device %s.\n"), name); in crypt_deactivate()
1834 if (!cd) in crypt_deactivate()
1841 int crypt_volume_key_get(struct crypt_device *cd, in crypt_volume_key_get() argument
1852 key_len = crypt_get_volume_key_size(cd); in crypt_volume_key_get()
1854 log_err(cd, _("Volume key buffer too small.\n")); in crypt_volume_key_get()
1858 if (isPLAIN(cd->type) && cd->plain_hdr.hash) { in crypt_volume_key_get()
1859 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len, in crypt_volume_key_get()
1862 log_err(cd, _("Cannot retrieve volume key for plain device.\n")); in crypt_volume_key_get()
1871 if (isLUKS(cd->type)) { in crypt_volume_key_get()
1872 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase, in crypt_volume_key_get()
1873 passphrase_size, &cd->hdr, &mk, cd); in crypt_volume_key_get()
1884 … log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)"); in crypt_volume_key_get()
1888 int crypt_volume_key_verify(struct crypt_device *cd, in crypt_volume_key_verify() argument
1895 if (!isLUKS(cd->type)) { in crypt_volume_key_verify()
1896 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_volume_key_verify()
1904 r = LUKS_verify_master_key(&cd->hdr, mk); in crypt_volume_key_verify()
1907 log_err(cd, _("Volume key does not match the volume.\n")); in crypt_volume_key_verify()
1914 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec) in crypt_set_timeout() argument
1917 cd->timeout = timeout_sec; in crypt_set_timeout()
1920 void crypt_set_password_retry(struct crypt_device *cd, int tries) in crypt_set_password_retry() argument
1923 cd->tries = tries; in crypt_set_password_retry()
1926 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms) in crypt_set_iterarion_time() argument
1929 cd->iteration_time = iteration_time_ms; in crypt_set_iterarion_time()
1932 void crypt_set_password_verify(struct crypt_device *cd, int password_verify) in crypt_set_password_verify() argument
1935 cd->password_verify = password_verify ? 1 : 0; in crypt_set_password_verify()
1938 int crypt_memory_lock(struct crypt_device *cd, int lock) in crypt_memory_lock() argument
1940 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd); in crypt_memory_lock()
1944 crypt_status_info crypt_status(struct crypt_device *cd, const char *name) in crypt_status() argument
1948 if (!cd && dm_init(NULL, 1) < 0) in crypt_status()
1953 if (!cd) in crypt_status()
1968 static void hexprintICB(struct crypt_device *cd, char *d, int n) in hexprintICB() argument
1972 log_std(cd, "%02hhx ", (char)d[i]); in hexprintICB()
1975 int crypt_dump(struct crypt_device *cd) in crypt_dump() argument
1978 if (!isLUKS(cd->type)) { //FIXME in crypt_dump()
1979 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_dump()
1983 log_std(cd, "LUKS header information for %s\n\n", cd->device); in crypt_dump()
1984 log_std(cd, "Version: \t%d\n", cd->hdr.version); in crypt_dump()
1985 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName); in crypt_dump()
1986 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode); in crypt_dump()
1987 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec); in crypt_dump()
1988 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset); in crypt_dump()
1989 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8); in crypt_dump()
1990 log_std(cd, "MK digest: \t"); in crypt_dump()
1991 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE); in crypt_dump()
1992 log_std(cd, "\n"); in crypt_dump()
1993 log_std(cd, "MK salt: \t"); in crypt_dump()
1994 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2); in crypt_dump()
1995 log_std(cd, "\n \t"); in crypt_dump()
1996 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2); in crypt_dump()
1997 log_std(cd, "\n"); in crypt_dump()
1998 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations); in crypt_dump()
1999 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid); in crypt_dump()
2001 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) { in crypt_dump()
2002 log_std(cd, "Key Slot %d: ENABLED\n",i); in crypt_dump()
2003 log_std(cd, "\tIterations: \t%d\n", in crypt_dump()
2004 cd->hdr.keyblock[i].passwordIterations); in crypt_dump()
2005 log_std(cd, "\tSalt: \t"); in crypt_dump()
2006 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt, in crypt_dump()
2008 log_std(cd, "\n\t \t"); in crypt_dump()
2009 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt + in crypt_dump()
2011 log_std(cd, "\n"); in crypt_dump()
2013 log_std(cd, "\tKey material offset:\t%d\n", in crypt_dump()
2014 cd->hdr.keyblock[i].keyMaterialOffset); in crypt_dump()
2015 log_std(cd, "\tAF stripes: \t%d\n", in crypt_dump()
2016 cd->hdr.keyblock[i].stripes); in crypt_dump()
2019 log_std(cd, "Key Slot %d: DISABLED\n", i); in crypt_dump()
2024 const char *crypt_get_cipher(struct crypt_device *cd) in crypt_get_cipher() argument
2026 if (isPLAIN(cd->type)) in crypt_get_cipher()
2027 return cd->plain_cipher; in crypt_get_cipher()
2029 if (isLUKS(cd->type)) in crypt_get_cipher()
2030 return cd->hdr.cipherName; in crypt_get_cipher()
2035 const char *crypt_get_cipher_mode(struct crypt_device *cd) in crypt_get_cipher_mode() argument
2037 if (isPLAIN(cd->type)) in crypt_get_cipher_mode()
2038 return cd->plain_cipher_mode; in crypt_get_cipher_mode()
2040 if (isLUKS(cd->type)) in crypt_get_cipher_mode()
2041 return cd->hdr.cipherMode; in crypt_get_cipher_mode()
2046 const char *crypt_get_uuid(struct crypt_device *cd) in crypt_get_uuid() argument
2048 if (isLUKS(cd->type)) in crypt_get_uuid()
2049 return cd->hdr.uuid; in crypt_get_uuid()
2054 int crypt_get_volume_key_size(struct crypt_device *cd) in crypt_get_volume_key_size() argument
2056 if (isPLAIN(cd->type)) in crypt_get_volume_key_size()
2057 return cd->volume_key->keyLength; in crypt_get_volume_key_size()
2059 if (isLUKS(cd->type)) in crypt_get_volume_key_size()
2060 return cd->hdr.keyBytes; in crypt_get_volume_key_size()
2065 uint64_t crypt_get_data_offset(struct crypt_device *cd) in crypt_get_data_offset() argument
2067 if (isPLAIN(cd->type)) in crypt_get_data_offset()
2068 return cd->plain_hdr.offset; in crypt_get_data_offset()
2070 if (isLUKS(cd->type)) in crypt_get_data_offset()
2071 return cd->hdr.payloadOffset; in crypt_get_data_offset()
2076 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot) in crypt_keyslot_status() argument
2078 if (!isLUKS(cd->type)) { in crypt_keyslot_status()
2079 log_err(cd, _("This operation is supported only for LUKS device.\n")); in crypt_keyslot_status()
2083 return LUKS_keyslot_info(&cd->hdr, keyslot); in crypt_keyslot_status()