15
15
* You should have received a copy of the GNU General Public License
16
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
20
#include <string.h>
50
50
static int devfd=-1;
52
52
static int setup_mapping(const char *cipher, const char *name,
53
const char *device, unsigned int payloadOffset,
54
const char *key, size_t keyLength,
54
struct volume_key *vk,
55
55
unsigned int sector, size_t srcLength,
56
56
int mode, struct crypt_device *ctx)
58
58
int device_sector_size = sector_size_for_device(device);
59
struct crypt_dm_active_device dmd = {
67
.flags = (mode == O_RDONLY) ? CRYPT_ACTIVATE_READONLY : 0
62
71
* we need to round this to nearest multiple of the underlying
66
75
log_err(ctx, _("Unable to obtain sector size for %s"), device);
69
size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
72
return dm_create_device(name, device, cipher, "TEMP", NULL, size, 0, sector,
73
keyLength, key, (mode == O_RDONLY), 0);
79
dmd.size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
80
cleaner_size = dmd.size;
82
return dm_create_device(name, "TEMP", &dmd, 0);
76
static void sigint_handler(int sig)
85
static void sigint_handler(int sig __attribute__((unused)))
85
94
kill(getpid(), SIGINT);
88
static char *_error_hint(char *cipherName, char *cipherMode, size_t keyLength)
97
static const char *_error_hint(char *cipherMode, size_t keyLength)
94
int i = 0, kernel_minor;
96
/* Nothing to suggest here */
97
if (uname(&uts) || strncmp(uts.release, "2.6.", 4))
100
/* Get kernel minor without suffixes */
101
while (i < 3 && (c = uts.release[i + 4]))
102
tmp[i++] = isdigit(c) ? c : '\0';
103
kernel_minor = atoi(tmp);
105
101
if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
106
102
hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
107
else if (!strncmp(cipherMode, "xts", 3) && kernel_minor < 24)
108
hint = _("Block mode XTS is available since kernel 2.6.24.\n");
109
if (!strncmp(cipherMode, "lrw", 3) && (keyLength != 256 && keyLength != 512))
110
hint = _("Key size in LRW mode must be 256 or 512 bits.\n");
111
else if (!strncmp(cipherMode, "lrw", 3) && kernel_minor < 20)
112
hint = _("Block mode LRW is available since kernel 2.6.20.\n");
118
108
handler and global vars for cleaning */
119
109
static int LUKS_endec_template(char *src, size_t srcLength,
120
110
struct luks_phdr *hdr,
121
char *key, size_t keyLength,
111
struct volume_key *vk,
122
112
const char *device,
123
113
unsigned int sector,
124
114
ssize_t (*func)(int, void *, size_t),
145
135
signal(SIGINT, sigint_handler);
146
136
cleaner_name = name;
148
r = setup_mapping(dmCipherSpec, name, device, hdr->payloadOffset,
149
key, keyLength, sector, srcLength, mode, ctx);
138
r = setup_mapping(dmCipherSpec, name, device,
139
vk, sector, srcLength, mode, ctx);
151
141
log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
152
142
"Check that kernel supports %s cipher (check syslog for more info).\n%s"),
153
143
device, dmCipherSpec,
154
_error_hint(hdr->cipherName, hdr->cipherMode, keyLength * 8));
144
_error_hint(hdr->cipherMode, vk->keylength * 8));
189
179
int LUKS_encrypt_to_storage(char *src, size_t srcLength,
190
180
struct luks_phdr *hdr,
191
char *key, size_t keyLength,
181
struct volume_key *vk,
192
182
const char *device,
193
183
unsigned int sector,
194
184
struct crypt_device *ctx)
196
return LUKS_endec_template(src,srcLength,hdr,key,keyLength, device, sector,
197
(ssize_t (*)(int, void *, size_t)) write_blockwise,
186
return LUKS_endec_template(src,srcLength,hdr,vk, device,
187
sector, write_blockwise, O_RDWR, ctx);
201
190
int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
202
191
struct luks_phdr *hdr,
203
char *key, size_t keyLength,
192
struct volume_key *vk,
204
193
const char *device,
205
194
unsigned int sector,
206
195
struct crypt_device *ctx)
208
return LUKS_endec_template(dst,dstLength,hdr,key,keyLength, device,
197
return LUKS_endec_template(dst,dstLength,hdr,vk, device,
209
198
sector, read_blockwise, O_RDONLY, ctx);