6 #include "libcryptsetup.h"
9 extern struct hash_backend hash_gcrypt_backend;
11 static struct hash_backend *hash_backends[] = {
16 struct hash_backend *get_hash_backend(const char *name)
18 struct hash_backend **backend;
20 for(backend = hash_backends; *backend; backend++)
21 if (!name || strcmp(name, (*backend)->name) == 0)
27 void put_hash_backend(struct hash_backend *backend)
31 int hash(const char *backend_name, const char *hash_name,
32 char *result, size_t size,
33 const char *passphrase, size_t sizep)
35 struct hash_backend *backend;
36 struct hash_type *hashes = NULL, *hash;
37 char hash_name_buf[256], *s;
41 if (strlen(hash_name) >= sizeof(hash_name_buf)) {
42 set_error("hash name too long: %s", hash_name);
46 if ((s = strchr(hash_name, ':'))) {
48 strcpy(hash_name_buf, hash_name);
49 hash_name_buf[s-hash_name] = '\0';
50 hash_name = hash_name_buf;
53 set_error("requested hash length (%zd) > key length (%zd)", hlen, size);
60 backend = get_hash_backend(backend_name);
62 set_error("No hash backend found");
66 hashes = backend->get_hashes();
68 set_error("No hash functions available");
72 for(hash = hashes; hash->name; hash++)
73 if (strcmp(hash->name, hash_name) == 0)
76 set_error("Unknown hash type %s", hash_name);
80 r = hash->fn(hash->private, size, result, sizep, passphrase);
82 set_error("Error hashing passphrase");
87 memset(result+size, 0, pad);
92 backend->free_hashes(hashes);
93 put_hash_backend(backend);