6 #include "libcryptsetup.h"
10 #define GCRYPT_REQ_VERSION "1.1.42"
12 static int gcrypt_hash(void *data, int size, char *key,
13 int sizep, const char *passphrase)
16 int algo = *((int *)data);
17 int len = gcry_md_get_algo_dlen(algo);
20 if (gcry_md_open(&md, algo, 0))
23 for(round = 0; size; round++) {
24 /* hack from hashalot to avoid null bytes in key */
25 for(i = 0; i < round; i++)
26 gcry_md_write(md, "A", 1);
28 gcry_md_write(md, passphrase, sizep);
32 memcpy(key, gcry_md_read(md, algo), len);
44 static struct hash_type *gcrypt_get_hashes(void)
46 struct hash_type *hashes;
47 int size = MAX_DIGESTS;
52 if (!gcry_check_version(GCRYPT_REQ_VERSION))
55 list = (int *)malloc(sizeof(*list) * size);
59 r = gcry_md_list(list, &size);
65 hashes = malloc(sizeof(*hashes) * (size + 1));
71 for(i = 0; i < size; i++) {
72 hashes[i].name = NULL;
73 hashes[i].private = NULL;
76 for(i = 0; i < size; i++) {
79 hashes[i].name = strdup(gcry_md_algo_name(list[i]));
82 for(p = (char *)hashes[i].name; *p; p++)
84 hashes[i].private = malloc(sizeof(int));
85 if(!hashes[i].private)
87 *((int *)hashes[i].private) = list[i];
88 hashes[i].fn = gcrypt_hash;
90 hashes[i].name = NULL;
91 hashes[i].private = NULL;
100 for(i = 0; i < size; i++) {
101 free(hashes[i].name);
102 free(hashes[i].private);
108 static void gcrypt_free_hashes(struct hash_type *hashes)
110 struct hash_type *hash;
112 for(hash = hashes; hash->name; hash++) {
120 struct hash_backend hash_gcrypt_backend = {
122 .get_hashes = gcrypt_get_hashes,
123 .free_hashes = gcrypt_free_hashes