6 #include "libcryptsetup.h"
10 #define GCRYPT_REQ_VERSION "1.1.42"
14 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
15 //if (!gcry_check_version (GCRYPT_VERSION))
17 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
18 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
19 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
20 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
26 static int gcrypt_hash(void *data, int size, char *key,
27 int sizep, const char *passphrase)
30 int algo = *((int *)data);
31 int len = gcry_md_get_algo_dlen(algo);
34 if (gcry_md_open(&md, algo, 0))
37 for(round = 0; size; round++) {
38 /* hack from hashalot to avoid null bytes in key */
39 for(i = 0; i < round; i++)
40 gcry_md_write(md, "A", 1);
42 gcry_md_write(md, passphrase, sizep);
46 memcpy(key, gcry_md_read(md, algo), len);
58 static struct hash_type *gcrypt_get_hashes(void)
60 struct hash_type *hashes;
61 int size = MAX_DIGESTS;
66 if (!gcry_check_version(GCRYPT_REQ_VERSION))
69 list = (int *)malloc(sizeof(*list) * size);
73 r = gcry_md_list(list, &size);
79 hashes = malloc(sizeof(*hashes) * (size + 1));
85 for(i = 0; i < size; i++) {
86 hashes[i].name = NULL;
87 hashes[i].private = NULL;
90 for(i = 0; i < size; i++) {
93 hashes[i].name = strdup(gcry_md_algo_name(list[i]));
96 for(p = (char *)hashes[i].name; *p; p++)
98 hashes[i].private = malloc(sizeof(int));
99 if(!hashes[i].private)
101 *((int *)hashes[i].private) = list[i];
102 hashes[i].fn = gcrypt_hash;
104 hashes[i].name = NULL;
105 hashes[i].private = NULL;
114 for(i = 0; i < size; i++) {
115 free(hashes[i].name);
116 free(hashes[i].private);
122 static void gcrypt_free_hashes(struct hash_type *hashes)
124 struct hash_type *hash;
126 for(hash = hashes; hash->name; hash++) {
134 struct hash_backend hash_gcrypt_backend = {
136 .get_hashes = gcrypt_get_hashes,
137 .free_hashes = gcrypt_free_hashes