utils_crypt.c 5.34 KB
Newer Older
1
/*
Milan Broz's avatar
Milan Broz committed
2
 * utils_crypt - cipher utilities for cryptsetup
3 4
 *
 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
Milan Broz's avatar
Milan Broz committed
5 6
 * Copyright (C) 2009-2018, Red Hat, Inc. All rights reserved.
 * Copyright (C) 2009-2018, Milan Broz
7 8 9
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
10 11
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
12 13 14 15 16 17 18 19
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 22
 */

23
#include <stdlib.h>
24 25 26 27
#include <stdio.h>
#include <string.h>
#include <errno.h>

28
#include "libcryptsetup.h"
Milan Broz's avatar
Milan Broz committed
29
#include "utils_crypt.h"
30

31
int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
32
			      char *cipher_mode)
33
{
34 35 36
	if (!s || !cipher || !cipher_mode)
		return -EINVAL;

37 38
	if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
		   cipher, cipher_mode) == 2) {
39
		if (!strcmp(cipher_mode, "plain"))
40
			strcpy(cipher_mode, "cbc-plain");
41 42 43 44 45 46 47
		if (key_nums) {
			char *tmp = strchr(cipher, ':');
			*key_nums = tmp ? atoi(++tmp) : 1;
			if (!*key_nums)
				return -EINVAL;
		}

48 49 50
		return 0;
	}

51
	/* Short version for "empty" cipher */
52 53 54
	if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) {
		strcpy(cipher, "cipher_null");
		strcpy(cipher_mode, "ecb");
55 56 57 58 59
		if (key_nums)
			*key_nums = 0;
		return 0;
	}

60
	if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
61
		strcpy(cipher_mode, "cbc-plain");
62 63
		if (key_nums)
			*key_nums = 1;
64 65 66 67 68
		return 0;
	}

	return -EINVAL;
}
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
int crypt_parse_hash_integrity_mode(const char *s, char *integrity)
{
	char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN];
	int r;

	if (!s || !integrity || strchr(s, '(') || strchr(s, ')'))
		return -EINVAL;

	r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash);
	if (r == 2)
		r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash);
	else if (r == 1)
		r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode);
	else
		return -EINVAL;

	if (r < 0 || r == MAX_CIPHER_LEN)
		return -EINVAL;

	return 0;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
int crypt_parse_integrity_mode(const char *s, char *integrity,
			       int *integrity_key_size)
{
	int ks = 0, r = 0;

	if (!s || !integrity)
		return -EINVAL;

	// FIXME: do not hardcode it here

	/* AEAD modes */
	if (!strcmp(s, "aead") ||
	    !strcmp(s, "poly1305") ||
	    !strcmp(s, "none")) {
		strncpy(integrity, s, MAX_CIPHER_LEN);
		ks = 0;
	} else if (!strcmp(s, "hmac-sha256")) {
		strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN);
		ks = 32;
	} else if (!strcmp(s, "hmac-sha512")) {
		ks = 64;
		strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN);
	} else if (!strcmp(s, "cmac-aes")) {
		ks = 16;
		strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN);
	} else
		r = -EINVAL;

	if (integrity_key_size)
		*integrity_key_size = ks;

	return r;
}

Milan Broz's avatar
Milan Broz committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
int crypt_parse_pbkdf(const char *s, const char **pbkdf)
{
	const char *tmp = NULL;

	if (!s)
		return -EINVAL;

	if (!strcasecmp(s, CRYPT_KDF_PBKDF2))
		tmp = CRYPT_KDF_PBKDF2;
	else if (!strcasecmp(s, CRYPT_KDF_ARGON2I))
		tmp = CRYPT_KDF_ARGON2I;
	else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID))
		tmp = CRYPT_KDF_ARGON2ID;

	if (!tmp)
		return -EINVAL;

	if (pbkdf)
		*pbkdf = tmp;

	return 0;
}

149 150 151 152 153 154 155 156 157 158 159 160
/*
 * Replacement for memset(s, 0, n) on stack that can be optimized out
 * Also used in safe allocations for explicit memory wipe.
 */
void crypt_memzero(void *s, size_t n)
{
	volatile uint8_t *p = (volatile uint8_t *)s;

	while(n--)
		*p++ = 0;
}

161 162 163 164 165
/* safe allocations */
void *crypt_safe_alloc(size_t size)
{
	struct safe_allocation *alloc;

166
	if (!size || size > (SIZE_MAX - offsetof(struct safe_allocation, data)))
167 168 169 170 171 172 173
		return NULL;

	alloc = malloc(size + offsetof(struct safe_allocation, data));
	if (!alloc)
		return NULL;

	alloc->size = size;
174
	crypt_memzero(&alloc->data, size);
175

176
	/* coverity[leaked_storage] */
177 178 179 180 181 182 183 184 185 186
	return &alloc->data;
}

void crypt_safe_free(void *data)
{
	struct safe_allocation *alloc;

	if (!data)
		return;

187 188
	alloc = (struct safe_allocation *)
		((char *)data - offsetof(struct safe_allocation, data));
189

190
	crypt_memzero(data, alloc->size);
191 192 193 194 195 196 197

	alloc->size = 0x55aa55aa;
	free(alloc);
}

void *crypt_safe_realloc(void *data, size_t size)
{
198
	struct safe_allocation *alloc;
199 200 201 202 203 204
	void *new_data;

	new_data = crypt_safe_alloc(size);

	if (new_data && data) {

205 206
		alloc = (struct safe_allocation *)
			((char *)data - offsetof(struct safe_allocation, data));
207 208 209 210 211 212 213 214 215 216

		if (size > alloc->size)
			size = alloc->size;

		memcpy(new_data, data, size);
	}

	crypt_safe_free(data);
	return new_data;
}
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
{
	char buf[3] = "xx\0", *endp, *bytes;
	size_t i, len;

	len = strlen(hex);
	if (len % 2)
		return -EINVAL;
	len /= 2;

	bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len);
	if (!bytes)
		return -ENOMEM;

	for (i = 0; i < len; i++) {
		memcpy(buf, &hex[i * 2], 2);
		bytes[i] = strtoul(buf, &endp, 16);
		if (endp != &buf[2]) {
			safe_alloc ? crypt_safe_free(bytes) : free(bytes);
			return -EINVAL;
		}
	}
	*result = bytes;
	return i;
}