crypt_plain.c 3 KB
Newer Older
1 2 3
/*
 * cryptsetup plain device helper functions
 *
Milan Broz's avatar
Milan Broz committed
4
 * Copyright (C) 2004, Jana Saout <jana@saout.de>
Milan Broz's avatar
Milan Broz committed
5 6
 * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2010-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 <string.h>
24
#include <stdio.h>
25 26
#include <errno.h>

27
#include "libcryptsetup.h"
28 29
#include "internal.h"

30 31
static int hash(const char *hash_name, size_t key_size, char *key,
		size_t passphrase_size, const char *passphrase)
32 33
{
	struct crypt_hash *md = NULL;
34 35
	size_t len;
	int round, i, r = 0;
36 37 38 39

	if (crypt_hash_init(&md, hash_name))
		return -ENOENT;

40 41 42
	len = crypt_hash_size(hash_name);

	for(round = 0; key_size && !r; round++) {
43 44
		/* hack from hashalot to avoid null bytes in key */
		for(i = 0; i < round; i++)
45 46 47 48 49
			if (crypt_hash_write(md, "A", 1))
				r = 1;

		if (crypt_hash_write(md, passphrase, passphrase_size))
			r = 1;
50

51 52
		if (len > key_size)
			len = key_size;
53

54 55
		if (crypt_hash_final(md, key, len))
			r = 1;
56 57

		key += len;
58
		key_size -= len;
59 60 61
	}

	crypt_hash_destroy(md);
62
	return r;
63 64
}

65 66
#define PLAIN_HASH_LEN_MAX 256

67
int crypt_plain_hash(struct crypt_device *ctx __attribute__((unused)),
68 69 70
		     const char *hash_name,
		     char *key, size_t key_size,
		     const char *passphrase, size_t passphrase_size)
71
{
72 73
	char hash_name_buf[PLAIN_HASH_LEN_MAX], *s;
	size_t hash_size, pad_size;
74 75
	int r;

76 77 78
	log_dbg("Plain: hashing passphrase using %s.", hash_name);

	if (strlen(hash_name) >= PLAIN_HASH_LEN_MAX)
79
		return -EINVAL;
80 81
	strncpy(hash_name_buf, hash_name, PLAIN_HASH_LEN_MAX);
	hash_name_buf[PLAIN_HASH_LEN_MAX - 1] = '\0';
82

83 84 85
	/* hash[:hash_length] */
	if ((s = strchr(hash_name_buf, ':'))) {
		*s = '\0';
86 87 88 89 90
		s++;
		if (!*s || sscanf(s, "%zd", &hash_size) != 1) {
			log_dbg("Hash length is not a number");
			return -EINVAL;
		}
91 92 93
		if (hash_size > key_size) {
			log_dbg("Hash length %zd > key length %zd",
				hash_size, key_size);
94 95
			return -EINVAL;
		}
96 97 98 99
		pad_size = key_size - hash_size;
	} else {
		hash_size = key_size;
		pad_size = 0;
100 101
	}

102 103 104 105 106 107 108 109 110 111
	/* No hash, copy passphrase directly */
	if (!strcmp(hash_name_buf, "plain")) {
		if (passphrase_size < hash_size) {
			log_dbg("Too short plain passphrase.");
			return -EINVAL;
		}
		memcpy(key, passphrase, hash_size);
		r = 0;
	} else
		r = hash(hash_name_buf, hash_size, key, passphrase_size, passphrase);
112

113 114
	if (r == 0 && pad_size)
		memset(key + hash_size, 0, pad_size);
115 116 117

	return r;
}