~ubuntu-branches/ubuntu/lucid/cryptsetup/lucid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <gcrypt.h>

#include "libcryptsetup.h"
#include "internal.h"

#define MAX_DIGESTS		64
#define GCRYPT_REQ_VERSION	"1.1.42"

int init_crypto(void)
{
	if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
		//if (!gcry_check_version (GCRYPT_VERSION))
		//	return -ENOSYS;
		gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
		gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
		gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
		gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
	}

	return 0;
}

static int gcrypt_hash(void *data, int size, char *key,
                       int sizep, const char *passphrase)
{
	gcry_md_hd_t md;
	int algo = *((int *)data);
	int len = gcry_md_get_algo_dlen(algo);
	int round, i;

	if (gcry_md_open(&md, algo, 0))
		return -1;

	for(round = 0; size; round++) {
		/* hack from hashalot to avoid null bytes in key */
		for(i = 0; i < round; i++)
			gcry_md_write(md, "A", 1);

		gcry_md_write(md, passphrase, sizep);

		if (len > size)
			len = size;
		memcpy(key, gcry_md_read(md, algo), len);

		key += len;
		size -= len;
		if (size)
			gcry_md_reset(md);
	}

	gcry_md_close(md);
	return 0;
}

static struct hash_type *gcrypt_get_hashes(void)
{
	struct hash_type *hashes;
	int size = MAX_DIGESTS;
	int *list;
	int i;
	gcry_error_t r;

	if (!gcry_check_version(GCRYPT_REQ_VERSION))
		return NULL;

	list = (int *)malloc(sizeof(*list) * size);
	if (!list)
		return NULL;

	r = gcry_md_list(list, &size);
	if (r || !size) {
		free(list);
		return NULL;
	}

	hashes = malloc(sizeof(*hashes) * (size + 1));
	if (!hashes) {
		free(list);
		return NULL;
	}

	for(i = 0; i < size; i++) {
		hashes[i].name = NULL;
		hashes[i].private = NULL;
	}

	for(i = 0; i < size; i++) {
		char *p;

		hashes[i].name = strdup(gcry_md_algo_name(list[i]));
		if(!hashes[i].name)
			goto err;
		for(p = (char *)hashes[i].name; *p; p++)
			*p = tolower(*p);
		hashes[i].private = malloc(sizeof(int));
		if(!hashes[i].private)
			goto err;
		*((int *)hashes[i].private) = list[i];
		hashes[i].fn = gcrypt_hash;
	}
	hashes[i].name = NULL;
	hashes[i].private = NULL;
	hashes[i].fn = NULL;

	free(list);

	return hashes;

err:
	free(list);
	for(i = 0; i < size; i++) {
		free(hashes[i].name);
		free(hashes[i].private);
	}
	free(hashes);
	return NULL;
}

static void gcrypt_free_hashes(struct hash_type *hashes)
{
	struct hash_type *hash;

	for(hash = hashes; hash->name; hash++) {
		free(hash->name);
		free(hash->private);
	}

	free(hashes);
}

struct hash_backend hash_gcrypt_backend = {
	.name = "libgcrypt",
	.get_hashes = gcrypt_get_hashes,
	.free_hashes = gcrypt_free_hashes
};