7 #include "libcryptsetup.h"
10 #define MAX_DIGESTS 64
11 #define GCRYPT_REQ_VERSION "1.1.42"
15 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
16 if (!gcry_check_version (GCRYPT_REQ_VERSION))
18 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
19 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
20 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
21 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
27 static int gcrypt_hash(void *data, int size, char *key,
28 int sizep, const char *passphrase)
31 int algo = *((int *)data);
32 int len = gcry_md_get_algo_dlen(algo);
35 if (gcry_md_open(&md, algo, 0))
38 for(round = 0; size; round++) {
39 /* hack from hashalot to avoid null bytes in key */
40 for(i = 0; i < round; i++)
41 gcry_md_write(md, "A", 1);
43 gcry_md_write(md, passphrase, sizep);
47 memcpy(key, gcry_md_read(md, algo), len);
59 static struct hash_type *gcrypt_get_hashes(void)
61 struct hash_type *hashes;
62 int size = MAX_DIGESTS;
67 if (!gcry_check_version(GCRYPT_REQ_VERSION))
70 list = (int *)malloc(sizeof(*list) * size);
74 r = gcry_md_list(list, &size);
80 hashes = malloc(sizeof(*hashes) * (size + 1));
86 for(i = 0; i < size; i++) {
87 hashes[i].name = NULL;
88 hashes[i].private = NULL;
91 for(i = 0; i < size; i++) {
94 hashes[i].name = strdup(gcry_md_algo_name(list[i]));
97 for(p = (char *)hashes[i].name; *p; p++)
99 hashes[i].private = malloc(sizeof(int));
100 if(!hashes[i].private)
102 *((int *)hashes[i].private) = list[i];
103 hashes[i].fn = gcrypt_hash;
105 hashes[i].name = NULL;
106 hashes[i].private = NULL;
115 for(i = 0; i < size; i++) {
116 free(hashes[i].name);
117 free(hashes[i].private);
123 static void gcrypt_free_hashes(struct hash_type *hashes)
125 struct hash_type *hash;
127 for(hash = hashes; hash->name; hash++) {
135 struct hash_backend hash_gcrypt_backend = {
137 .get_hashes = gcrypt_get_hashes,
138 .free_hashes = gcrypt_free_hashes