2 * cryptsetup plain device helper functions
4 * Copyright (C) 2004 Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "crypto_backend.h"
28 static int hash(const char *hash_name, int size, char *key,
29 int sizep, const char *passphrase)
31 struct crypt_hash *md = NULL;
33 int len = crypt_hash_size(hash_name);
36 if (crypt_hash_init(&md, hash_name))
39 for(round = 0; size; round++) {
40 /* hack from hashalot to avoid null bytes in key */
41 for(i = 0; i < round; i++)
42 crypt_hash_write(md, "A", 1);
44 crypt_hash_write(md, passphrase, sizep);
49 crypt_hash_final(md, key, slen);
50 // FIXME: if slen != len
55 crypt_hash_restart(md);
58 crypt_hash_destroy(md);
62 int crypt_plain_hash(struct crypt_device *ctx, const char *hash_name,
63 char *result, size_t size,
64 const char *passphrase, size_t sizep)
66 char hash_name_buf[256], *s;
70 if (strlen(hash_name) >= sizeof(hash_name_buf))
73 if ((s = strchr(hash_name, ':'))) {
74 strcpy(hash_name_buf, hash_name);
75 hash_name_buf[s-hash_name] = '\0';
76 hash_name = hash_name_buf;
79 log_err(ctx, "Requested hash length (%zd) > key length (%zd)\n", hlen, size);
86 r = hash(hash_name, size, result, sizep, passphrase);
88 log_err(ctx, "Hash algorithm %s not supported.\n", hash_name);
91 memset(result+size, 0, pad);