int LUKS_write_phdr(struct luks_phdr *hdr, struct crypt_device *ctx) { struct device *device = crypt_metadata_device(ctx); ssize_t hdr_size = sizeof(struct luks_phdr); int devfd = 0; unsigned int i; struct luks_phdr convHdr; int r; log_dbg(ctx, "Updating LUKS header of size %zu on device %s", sizeof(struct luks_phdr), device_path(device)); r = LUKS_check_device_size(ctx, hdr, 1); if (r) return r; devfd = device_open(ctx, device, O_RDWR); if (devfd < 0) { if (errno == EACCES) log_err(ctx, _("Cannot write to device %s, permission denied."), device_path(device)); else log_err(ctx, _("Cannot open device %s."), device_path(device)); return -EINVAL; } memcpy(&convHdr, hdr, hdr_size); memset(&convHdr._padding, 0, sizeof(convHdr._padding)); /* Convert every uint16/32_t item to network byte order */ convHdr.version = htons(hdr->version); convHdr.payloadOffset = htonl(hdr->payloadOffset); convHdr.keyBytes = htonl(hdr->keyBytes); convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations); for(i = 0; i < LUKS_NUMKEYS; ++i) { convHdr.keyblock[i].active = htonl(hdr->keyblock[i].active); convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].passwordIterations); convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].keyMaterialOffset); convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].stripes); } r = write_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device), &convHdr, hdr_size, 0) < hdr_size ? -EIO : 0; if (r) log_err(ctx, _("Error during update of LUKS header on device %s."), device_path(device)); device_sync(ctx, device); /* Re-read header from disk to be sure that in-memory and on-disk data are the same. */ if (!r) { r = LUKS_read_phdr(hdr, 1, 0, ctx); if (r) log_err(ctx, _("Error re-reading LUKS header after update on device %s."), device_path(device)); } return r; }
int main(int argc, char** argv) { LOGI("\nvvvv vvvv vvvv"); int width = 128; int height = 128; int channels = 4; auto input = Halide::Buffer<int>::make_interleaved(width, height, channels); LOGI("Allocated memory for %dx%dx%d image", width, height, channels); input.for_each_element([&](int i, int j, int k) { input(i, j, k) = ((i + j) % 2) * 6; }); LOGI("Input :\n"); print(input); auto output = Halide::Buffer<int>::make_interleaved(width, height, channels); two_kernels_filter(input, output); LOGI("Filter is done."); output.device_sync(); LOGI("Sync is done"); output.copy_to_host(); LOGI("Output :\n"); print(output); int count_mismatches = 0; output.for_each_element([&](int i, int j, int k) { int32_t output_value = output(i, j, k); int32_t input_value = input(i, j, k); if (output_value != input_value) { if (count_mismatches < 100) { std::ostringstream str; str << "output and input results differ at " << "(" << i << ", " << j << ", " << k << "):" << output_value << " != " << input_value << "\n"; LOGI("%s", str.str().c_str()); } count_mismatches++; } }); LOGI(count_mismatches == 0 ? "Test passed.\n": "Test failed.\n"); halide_device_release(NULL, halide_openglcompute_device_interface()); LOGI("^^^^ ^^^^ ^^^^\n"); }
int LUKS_hdr_restore( const char *backup_file, struct luks_phdr *hdr, struct crypt_device *ctx) { struct device *device = crypt_metadata_device(ctx); int fd, r = 0, devfd = -1, diff_uuid = 0; ssize_t ret, buffer_size = 0; char *buffer = NULL, msg[200]; struct luks_phdr hdr_file; r = LUKS_read_phdr_backup(backup_file, &hdr_file, 0, ctx); if (r == -ENOENT) return r; if (!r) buffer_size = LUKS_device_sectors(&hdr_file) << SECTOR_SHIFT; if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) { log_err(ctx, _("Backup file doesn't contain valid LUKS header.")); r = -EINVAL; goto out; } buffer = crypt_safe_alloc(buffer_size); if (!buffer) { r = -ENOMEM; goto out; } fd = open(backup_file, O_RDONLY); if (fd == -1) { log_err(ctx, _("Cannot open header backup file %s."), backup_file); r = -EINVAL; goto out; } ret = read_buffer(fd, buffer, buffer_size); close(fd); if (ret < buffer_size) { log_err(ctx, _("Cannot read header backup file %s."), backup_file); r = -EIO; goto out; } r = LUKS_read_phdr(hdr, 0, 0, ctx); if (r == 0) { log_dbg(ctx, "Device %s already contains LUKS header, checking UUID and offset.", device_path(device)); if(hdr->payloadOffset != hdr_file.payloadOffset || hdr->keyBytes != hdr_file.keyBytes) { log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.")); r = -EINVAL; goto out; } if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L)) diff_uuid = 1; } if (snprintf(msg, sizeof(msg), _("Device %s %s%s"), device_path(device), r ? _("does not contain LUKS header. Replacing header can destroy data on that device.") : _("already contains LUKS header. Replacing header will destroy existing keyslots."), diff_uuid ? _("\nWARNING: real device header has different UUID than backup!") : "") < 0) { r = -ENOMEM; goto out; } if (!crypt_confirm(ctx, msg)) { r = -EINVAL; goto out; } log_dbg(ctx, "Storing backup of header (%zu bytes) and keyslot area (%zu bytes) to device %s.", sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(device)); devfd = device_open(ctx, device, O_RDWR); if (devfd < 0) { if (errno == EACCES) log_err(ctx, _("Cannot write to device %s, permission denied."), device_path(device)); else log_err(ctx, _("Cannot open device %s."), device_path(device)); r = -EINVAL; goto out; } if (write_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device), buffer, buffer_size, 0) < buffer_size) { r = -EIO; goto out; } /* Be sure to reload new data */ r = LUKS_read_phdr(hdr, 1, 0, ctx); out: device_sync(ctx, device); crypt_safe_free(buffer); return r; }