2 * cryptsetup plain device helper functions
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2010-2012 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2010-2012, Milan Broz
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "libcryptsetup.h"
29 static int hash(const char *hash_name, size_t key_size, char *key,
30 size_t passphrase_size, const char *passphrase)
32 struct crypt_hash *md = NULL;
36 if (crypt_hash_init(&md, hash_name))
39 len = crypt_hash_size(hash_name);
41 for(round = 0; key_size && !r; round++) {
42 /* hack from hashalot to avoid null bytes in key */
43 for(i = 0; i < round; i++)
44 if (crypt_hash_write(md, "A", 1))
47 if (crypt_hash_write(md, passphrase, passphrase_size))
53 if (crypt_hash_final(md, key, len))
60 crypt_hash_destroy(md);
64 #define PLAIN_HASH_LEN_MAX 256
66 int crypt_plain_hash(struct crypt_device *ctx __attribute__((unused)),
67 const char *hash_name,
68 char *key, size_t key_size,
69 const char *passphrase, size_t passphrase_size)
71 char hash_name_buf[PLAIN_HASH_LEN_MAX], *s;
72 size_t hash_size, pad_size;
75 log_dbg("Plain: hashing passphrase using %s.", hash_name);
77 if (strlen(hash_name) >= PLAIN_HASH_LEN_MAX)
79 strncpy(hash_name_buf, hash_name, PLAIN_HASH_LEN_MAX);
80 hash_name_buf[PLAIN_HASH_LEN_MAX - 1] = '\0';
82 /* hash[:hash_length] */
83 if ((s = strchr(hash_name_buf, ':'))) {
85 hash_size = atoi(++s);
86 if (hash_size > key_size) {
87 log_dbg("Hash length %zd > key length %zd",
91 pad_size = key_size - hash_size;
97 r = hash(hash_name_buf, hash_size, key, passphrase_size, passphrase);
99 if (r == 0 && pad_size)
100 memset(key + hash_size, 0, pad_size);