mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-13 11:50:10 +01:00
Require device device-mapper to build and do not use backend wrapper for dm calls.
Signed-off-by: Milan Broz <mbroz@redhat.com> git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@90 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
@@ -33,9 +33,9 @@
|
||||
#include <signal.h>
|
||||
|
||||
#include "luks.h"
|
||||
#include "../lib/libcryptsetup.h"
|
||||
//#include "../lib/libcryptsetup.h"
|
||||
#include "../lib/internal.h"
|
||||
#include "../lib/blockdev.h"
|
||||
//#include "../lib/blockdev.h"
|
||||
|
||||
#define div_round_up(a,b) ({ \
|
||||
typeof(a) __a = (a); \
|
||||
@@ -47,22 +47,18 @@ static inline int round_up_modulo(int x, int m) {
|
||||
return div_round_up(x, m) * m;
|
||||
}
|
||||
|
||||
static struct setup_backend *cleaner_backend=NULL;
|
||||
static const char *cleaner_name=NULL;
|
||||
static uint64_t cleaner_size = 0;
|
||||
static int devfd=-1;
|
||||
|
||||
static int setup_mapping(const char *cipher, const char *name,
|
||||
static int setup_mapping(const char *cipher, const char *name,
|
||||
const char *device, unsigned int payloadOffset,
|
||||
const char *key, size_t keyLength,
|
||||
unsigned int sector, size_t srcLength,
|
||||
struct setup_backend *backend,
|
||||
const char *key, size_t keyLength,
|
||||
unsigned int sector, size_t srcLength,
|
||||
int mode)
|
||||
{
|
||||
struct crypt_options k = {0};
|
||||
struct crypt_options *options = &k;
|
||||
int device_sector_size = sector_size_for_device(device);
|
||||
int r;
|
||||
uint64_t size;
|
||||
|
||||
/*
|
||||
* we need to round this to nearest multiple of the underlying
|
||||
@@ -72,45 +68,24 @@ static int setup_mapping(const char *cipher, const char *name,
|
||||
set_error(_("Unable to obtain sector size for %s"),device);
|
||||
return -EINVAL;
|
||||
}
|
||||
options->size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
|
||||
cleaner_size = options->size;
|
||||
|
||||
options->offset = sector;
|
||||
options->cipher = cipher;
|
||||
options->key_size = keyLength;
|
||||
options->skip = 0;
|
||||
options->flags = 0;
|
||||
options->name = name;
|
||||
options->device = device;
|
||||
|
||||
if (mode == O_RDONLY) {
|
||||
options->flags |= CRYPT_FLAG_READONLY;
|
||||
}
|
||||
|
||||
set_error(NULL);
|
||||
size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
|
||||
cleaner_size = size;
|
||||
|
||||
r = backend->create(0, options, key, NULL);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static int clear_mapping(const char *name, uint64_t size, struct setup_backend *backend)
|
||||
{
|
||||
struct crypt_options options = {0};
|
||||
options.name=name;
|
||||
options.size = size;
|
||||
return backend->remove(1, &options);
|
||||
return dm_create_device(name, device, cipher, NULL, size, 0, sector,
|
||||
keyLength, key, (mode == O_RDONLY), 0);
|
||||
}
|
||||
|
||||
static void sigint_handler(int sig)
|
||||
{
|
||||
if(devfd >= 0)
|
||||
close(devfd);
|
||||
devfd = -1;
|
||||
if(cleaner_backend && cleaner_name)
|
||||
clear_mapping(cleaner_name, cleaner_size, cleaner_backend);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
kill(getpid(), SIGINT);
|
||||
if(devfd >= 0)
|
||||
close(devfd);
|
||||
devfd = -1;
|
||||
if(cleaner_name)
|
||||
dm_remove_device(cleaner_name, 1, cleaner_size);
|
||||
|
||||
signal(SIGINT, SIG_DFL);
|
||||
kill(getpid(), SIGINT);
|
||||
}
|
||||
|
||||
static char *_error_hint(char *cipherName, char *cipherMode, size_t keyLength)
|
||||
@@ -148,14 +123,14 @@ static int LUKS_endec_template(char *src, size_t srcLength,
|
||||
struct luks_phdr *hdr,
|
||||
char *key, size_t keyLength,
|
||||
const char *device,
|
||||
unsigned int sector, struct setup_backend *backend,
|
||||
unsigned int sector,
|
||||
ssize_t (*func)(int, void *, size_t),
|
||||
int mode)
|
||||
{
|
||||
char *name = NULL;
|
||||
char *fullpath = NULL;
|
||||
char *dmCipherSpec = NULL;
|
||||
const char *dmDir = backend->dir();
|
||||
const char *dmDir = dm_get_dir();
|
||||
int r = -1;
|
||||
|
||||
if(dmDir == NULL) {
|
||||
@@ -171,9 +146,9 @@ static int LUKS_endec_template(char *src, size_t srcLength,
|
||||
|
||||
signal(SIGINT, sigint_handler);
|
||||
cleaner_name = name;
|
||||
cleaner_backend = backend;
|
||||
|
||||
r = setup_mapping(dmCipherSpec,name,device,hdr->payloadOffset,key,keyLength,sector,srcLength,backend,mode);
|
||||
r = setup_mapping(dmCipherSpec, name, device, hdr->payloadOffset,
|
||||
key, keyLength, sector, srcLength, mode);
|
||||
if(r < 0) {
|
||||
set_error("Failed to setup dm-crypt key mapping for device %s.\n"
|
||||
"Check that kernel supports %s cipher (check syslog for more info).\n%s",
|
||||
@@ -184,49 +159,50 @@ static int LUKS_endec_template(char *src, size_t srcLength,
|
||||
}
|
||||
|
||||
devfd = open(fullpath, mode | O_DIRECT | O_SYNC); /* devfd is a global var */
|
||||
if(devfd == -1) { r = -EIO; goto out2; }
|
||||
if(devfd == -1) {
|
||||
r = -EIO;
|
||||
goto out2;
|
||||
}
|
||||
|
||||
r = func(devfd,src,srcLength);
|
||||
if(r < 0) { r = -EIO; goto out3; }
|
||||
if(r < 0) {
|
||||
r = -EIO;
|
||||
goto out3;
|
||||
}
|
||||
|
||||
r = 0;
|
||||
out3:
|
||||
close(devfd);
|
||||
devfd = -1;
|
||||
out2:
|
||||
clear_mapping(cleaner_name, cleaner_size, cleaner_backend);
|
||||
dm_remove_device(cleaner_name, 1, cleaner_size);
|
||||
out1:
|
||||
signal(SIGINT, SIG_DFL);
|
||||
cleaner_name = NULL;
|
||||
cleaner_backend = NULL;
|
||||
cleaner_size = 0;
|
||||
free(dmCipherSpec);
|
||||
free(fullpath);
|
||||
free(name);
|
||||
free(fullpath);
|
||||
free(name);
|
||||
return r;
|
||||
}
|
||||
|
||||
int LUKS_encrypt_to_storage(char *src, size_t srcLength,
|
||||
struct luks_phdr *hdr,
|
||||
char *key, size_t keyLength,
|
||||
const char *device,
|
||||
unsigned int sector, struct setup_backend *backend)
|
||||
int LUKS_encrypt_to_storage(char *src, size_t srcLength,
|
||||
struct luks_phdr *hdr,
|
||||
char *key, size_t keyLength,
|
||||
const char *device,
|
||||
unsigned int sector)
|
||||
{
|
||||
|
||||
return LUKS_endec_template(src,srcLength,hdr,key,keyLength, device, sector, backend,
|
||||
(ssize_t (*)(int, void *, size_t)) write_blockwise, O_RDWR);
|
||||
}
|
||||
|
||||
int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
|
||||
struct luks_phdr *hdr,
|
||||
char *key, size_t keyLength,
|
||||
const char *device,
|
||||
unsigned int sector, struct setup_backend *backend)
|
||||
{
|
||||
return LUKS_endec_template(dst,dstLength,hdr,key,keyLength, device, sector, backend, read_blockwise, O_RDONLY);
|
||||
return LUKS_endec_template(src,srcLength,hdr,key,keyLength, device, sector,
|
||||
(ssize_t (*)(int, void *, size_t)) write_blockwise,
|
||||
O_RDWR);
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// c-basic-offset: 8
|
||||
// indent-tabs-mode: nil
|
||||
// End:
|
||||
int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
|
||||
struct luks_phdr *hdr,
|
||||
char *key, size_t keyLength,
|
||||
const char *device,
|
||||
unsigned int sector)
|
||||
{
|
||||
return LUKS_endec_template(dst,dstLength,hdr,key,keyLength, device,
|
||||
sector, read_blockwise, O_RDONLY);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user