int dc_change_pass(wchar_t *dev_name, dc_pass *old_pass, dc_pass *new_pass) { dc_header *header = NULL; dev_hook *hook = NULL; int wp_init = 0; int resl; wipe_ctx wipe; do { if ( (hook = dc_find_hook(dev_name)) == NULL ) { resl = ST_NF_DEVICE; break; } wait_object_infinity(&hook->busy_lock); if ( !(hook->flags & F_ENABLED) ) { resl = ST_NO_MOUNT; break; } if (hook->flags & (F_SYNC | F_FORMATTING | F_CDROM)) { resl = ST_ERROR; break; } /* read old volume header */ if ( (resl = io_read_header(hook, &header, NULL, old_pass)) != ST_OK ) { break; } /* init data wipe */ if ( (resl = dc_wipe_init( &wipe, hook, hook->head_len, WP_GUTMANN, hook->crypt.cipher_id)) == ST_OK ) { wp_init = 1; } else break; /* wipe volume header */ dc_wipe_process(&wipe, 0, hook->head_len); /* write new volume header */ resl = io_write_header(hook, header, NULL, new_pass); } while (0); if (wp_init != 0) { dc_wipe_free(&wipe); } if (hook != NULL) { KeReleaseMutex(&hook->busy_lock, FALSE); dc_deref_hook(hook); } if (header != NULL) mm_free(header); return resl; }
int dc_restore_header(wchar_t *dev_name, dc_pass *password, void *in) { dc_header *header = NULL; xts_key *hdr_key = NULL; dev_hook *hook = NULL; int resl; do { if ( (hook = dc_find_hook(dev_name)) == NULL ) { resl = ST_NF_DEVICE; break; } wait_object_infinity(&hook->busy_lock); if (hook->flags & (F_ENABLED | F_CDROM)) { resl = ST_ERROR; break; } /* get device params */ if (hook->dsk_size == 0) { if ( (resl = dc_fill_disk_info(hook)) != ST_OK ) break; } if ( (header = mm_alloc(sizeof(dc_header), MEM_SECURE)) == NULL ) { resl = ST_NOMEM; break; } /* copy header from input */ memcpy(header, in, sizeof(dc_header)); if ( (hdr_key = mm_alloc(sizeof(xts_key), MEM_SECURE)) == NULL ) { resl = ST_NOMEM; break; } /* decrypt header */ if (cp_decrypt_header(hdr_key, header, password) == 0) { resl = ST_PASS_ERR; break; } /* write new volume header */ resl = io_write_header(hook, header, NULL, password); } while (0); if (hook != NULL) { KeReleaseMutex(&hook->busy_lock, FALSE); dc_deref_hook(hook); } if (hdr_key != NULL) mm_free(hdr_key); if (header != NULL) mm_free(header); return resl; }
int dc_mount_device(wchar_t *dev_name, dc_pass *password, u32 mnt_flags) { dc_header *hcopy = NULL; dev_hook *hook = NULL; xts_key *hdr_key = NULL; int resl; DbgMsg("dc_mount_device %ws\n", dev_name); do { if ( (hook = dc_find_hook(dev_name)) == NULL ) { resl = ST_NF_DEVICE; break; } wait_object_infinity(&hook->busy_lock); if (hook->flags & F_ENABLED) { resl = ST_ALR_MOUNT; break; } if (hook->flags & (F_UNSUPRT | F_DISABLE | F_FORMATTING)) { resl = ST_ERROR; break; } if ( !NT_SUCCESS(dc_fill_device_info(hook)) ) { resl = ST_IO_ERROR; break; } if ( ( (hook->flags & F_CDROM) && (hook->bps != CD_SECTOR_SIZE) ) || (!(hook->flags & F_CDROM) && IS_INVALID_SECTOR_SIZE(hook->bps) ) ) { hook->flags |= F_UNSUPRT; resl = ST_ERROR; break; } if ( (resl = dc_probe_decrypt(hook, &hcopy, &hdr_key, password)) != ST_OK ) { break; } /* check volume header */ if ( (IS_INVALID_VOL_FLAGS(hcopy->flags) != 0) || ( (hcopy->flags & VF_STORAGE_FILE) && (hcopy->stor_off == 0) ) || (hcopy->alg_1 >= CF_CIPHERS_NUM) || (hcopy->alg_2 >= CF_CIPHERS_NUM) || (hcopy->tmp_wp_mode >= WP_NUM) || ( (hook->flags & F_CDROM) && (hcopy->flags & (VF_TMP_MODE | VF_STORAGE_FILE)) ) ) { resl = ST_INV_VOLUME; break; } if (hcopy->version > DC_HDR_VERSION) { resl = ST_VOLUME_TOO_NEW; break; } #if 0 /* update volume header if needed */ if ( (hcopy->version < DC_HDR_VERSION) && !(hook->flags & F_CDROM) ) { hcopy->version = DC_HDR_VERSION; memset(hcopy->deprecated, 0, sizeof(hcopy->deprecated)); io_write_header(hook, hcopy, hdr_key, NULL); } #endif DbgMsg("hdr_key=%p, dsk_key=%p\n", hdr_key, hook->dsk_key); // initialize volume key if ( (hook->dsk_key = (xts_key*)mm_secure_alloc(sizeof(xts_key))) == NULL ) { resl = ST_NOMEM; break; } xts_set_key(hcopy->key_1, hcopy->alg_1, hook->dsk_key); DbgMsg("device mounted\n"); if (hcopy->flags & VF_STORAGE_FILE) { hook->use_size = hook->dsk_size; hook->stor_off = hcopy->stor_off; } else { hook->use_size = hook->dsk_size - hook->head_len; hook->stor_off = hook->use_size; } hook->crypt.cipher_id = d8(hcopy->alg_1); hook->disk_id = hcopy->disk_id; hook->vf_version = hcopy->version; hook->tmp_size = 0; hook->mnt_flags = mnt_flags; DbgMsg("hook->vf_version %d\n", hook->vf_version); DbgMsg("hook->bps %d\n", hook->bps); DbgMsg("hook->head_len %d\n", hook->head_len); DbgMsg("flags %x\n", hcopy->flags); if (hcopy->flags & VF_STORAGE_FILE) { hook->flags |= F_PROTECT_DCSYS; } if (hcopy->flags & VF_NO_REDIR) { hook->flags |= F_NO_REDIRECT; hook->stor_off = 0; } if (hcopy->flags & VF_TMP_MODE) { hook->tmp_size = hcopy->tmp_size; hook->hdr_key = hdr_key; hook->crypt.wp_mode = hcopy->tmp_wp_mode; if (hcopy->flags & VF_REENCRYPT) { hook->sync_init_type = S_CONTINUE_RE_ENC; } else { hook->sync_init_type = S_CONTINUE_ENC; } /* copy decrypted header to device data */ memcpy(&hook->tmp_header, hcopy, sizeof(dc_header)); if ( (resl = dc_enable_sync_mode(hook)) != ST_OK ) { burn(&hook->tmp_header, sizeof(dc_header)); hdr_key = hook->hdr_key; } else { hdr_key = NULL; /* prevent key wiping */ } } else { hook->flags |= F_ENABLED; resl = ST_OK; } if (resl == ST_OK) { /* increment mount changes counter */ lock_inc(&hook->chg_mount); } } while (0); if (hdr_key != NULL) mm_secure_free(hdr_key); if (hcopy != NULL) mm_secure_free(hcopy); if (hook != NULL) { if ((hook->flags & F_ENABLED) == 0 && hook->dsk_key != NULL) { mm_secure_free(hook->dsk_key); hook->dsk_key = NULL; } KeReleaseMutex(&hook->busy_lock, FALSE); dc_deref_hook(hook); } return resl; }
int dc_format_start(wchar_t *dev_name, dc_pass *password, crypt_info *crypt) { IO_STATUS_BLOCK iosb; NTSTATUS status; dc_header *header = NULL; dev_hook *hook = NULL; xts_key *tmp_key = NULL; HANDLE h_dev = NULL; u8 *buff = NULL; int w_init = 0; u8 key_buf[32]; int resl; DbgMsg("dc_format_start\n"); do { if ( (hook = dc_find_hook(dev_name)) == NULL ) { resl = ST_NF_DEVICE; break; } wait_object_infinity(&hook->busy_lock); if (hook->flags & (F_ENABLED | F_UNSUPRT | F_DISABLE | F_CDROM)) { resl = ST_ERROR; break; } /* verify encryption info */ if ( (crypt->cipher_id >= CF_CIPHERS_NUM) || (crypt->wp_mode >= WP_NUM) ) { resl = ST_ERROR; break; } /* get device params */ if ( (resl = dc_fill_disk_info(hook)) != ST_OK ) { break; } if ( (header = mm_alloc(sizeof(dc_header), MEM_SECURE)) == NULL ) { resl = ST_NOMEM; break; } if ( (buff = mm_alloc(ENC_BLOCK_SIZE, 0)) == NULL ) { resl = ST_NOMEM; break; } if ( (tmp_key = mm_alloc(sizeof(xts_key), MEM_SECURE)) == NULL ) { resl = ST_NOMEM; break; } /* temporary disable automounting */ hook->flags |= F_NO_AUTO_MOUNT; /* open volume device */ if ( (h_dev = io_open_device(hook->dev_name)) == NULL ) { resl = ST_LOCK_ERR; break; } /* lock volume */ status = ZwFsControlFile( h_dev, NULL, NULL, NULL, &iosb, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0); if (NT_SUCCESS(status) == FALSE) { resl = ST_LOCK_ERR; break; } /* enable automounting */ hook->flags &= ~F_NO_AUTO_MOUNT; /* set encryption info */ hook->crypt = *crypt; /* init data wiping */ resl = dc_wipe_init( &hook->wp_ctx, hook, ENC_BLOCK_SIZE, crypt->wp_mode, crypt->cipher_id); if (resl == ST_OK) { w_init = 1; } else break; /* wipe first sectors */ dc_wipe_process(&hook->wp_ctx, 0, hook->head_len); /* create random temporary key */ cp_rand_bytes(key_buf, sizeof(key_buf)); xts_set_key(key_buf, crypt->cipher_id, tmp_key); /* create volume header */ memset(header, 0, sizeof(dc_header)); cp_rand_bytes(pv(header->salt), PKCS5_SALT_SIZE); cp_rand_bytes(pv(&header->disk_id), sizeof(u32)); cp_rand_bytes(pv(header->key_1), DISKKEY_SIZE); header->sign = DC_VOLM_SIGN; header->version = DC_HDR_VERSION; header->alg_1 = crypt->cipher_id; /* write volume header */ if ( (resl = io_write_header(hook, header, NULL, password)) != ST_OK ) { break; } /* mount device */ if ( (resl = dc_mount_device(dev_name, password, 0)) != ST_OK ) { break; } /* set hook fields */ hook->flags |= F_FORMATTING; hook->tmp_size = hook->head_len; hook->tmp_buff = buff; hook->tmp_key = tmp_key; } while (0); if ( (resl != ST_OK) ) { if (w_init != 0) { dc_wipe_free(&hook->wp_ctx); } if (buff != NULL) { mm_free(buff); } if (tmp_key != NULL) { mm_free(tmp_key); } } if (header != NULL) { mm_free(header); } if (hook != NULL) { KeReleaseMutex(&hook->busy_lock, FALSE); dc_deref_hook(hook); } /* prevent leaks */ burn(key_buf, sizeof(key_buf)); if (h_dev != NULL) { if (resl != ST_LOCK_ERR) { /* dismount volume */ ZwFsControlFile( h_dev, NULL, NULL, NULL, &iosb, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0); /* unlock volume */ ZwFsControlFile( h_dev, NULL, NULL, NULL, &iosb, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0); } /* close device */ ZwClose(h_dev); } return resl; }