File: backends.c

package info (click to toggle)
cryptsetup 20050111-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 252 kB
  • ctags: 156
  • sloc: ansic: 1,305; sh: 675; makefile: 159
file content (108 lines) | stat: -rw-r--r-- 2,059 bytes parent folder | download
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
#include <stdio.h>
#include <string.h>
#include <errno.h>

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

extern struct hash_backend hash_gcrypt_backend;
extern struct setup_backend setup_libdevmapper_backend;

#ifdef USE_PLUGINS
static void init_plugins(void)
{
}
#else /* USE_PLUGINS */
#	define init_plugins()	do { } while(0)
#endif /* USE_PLUGINS */

static struct hash_backend *hash_backends[] = {
#ifdef BUILTIN_GCRYPT
	&hash_gcrypt_backend,
#endif
	NULL
};

static struct setup_backend *setup_backends[] = {
#ifdef BUILTIN_LIBDEVMAPPER
	&setup_libdevmapper_backend,
#endif
	NULL
};

struct hash_backend *get_hash_backend(const char *name)
{
	struct hash_backend **backend;

	init_plugins();

	for(backend = hash_backends; *backend; backend++)
		if (!name || strcmp(name, (*backend)->name) == 0)
			break;

	return *backend;
}

void put_hash_backend(struct hash_backend *backend)
{
}

int hash(const char *backend_name, const char *hash_name,
         char *result, int size, const char *passphrase)
{
	struct hash_backend *backend;
	struct hash_type *hashes = NULL, *hash;
	int r = -ENOENT;

	backend = get_hash_backend(backend_name);
	if (!backend) {
		set_error("No hash backend found");
		return -ENOSYS;
	}

	hashes = backend->get_hashes();
	if (!hashes) {
		set_error("No hash functions available");
		goto out;
	}

	for(hash = hashes; hash->name; hash++)
		if (strcmp(hash->name, hash_name) == 0)
			break;
	if (!hash->name) {
		set_error("Unknown hash type %s", hash_name);
		goto out;
	}

	r = hash->fn(hash->private, size, result, passphrase);
	if (r < 0) {
		set_error("Error hashing passphrase");
		goto out;
	}

out:
	if (hashes)
		backend->free_hashes(hashes);
	put_hash_backend(backend);

	return r;
}

struct setup_backend *get_setup_backend(const char *name)
{
	struct setup_backend **backend;

	init_plugins();

	for(backend = setup_backends; *backend; backend++)
		if (!name || strcmp(name, (*backend)->name) == 0)
			break;

	return *backend;
}

void put_setup_backend(struct setup_backend *backend)
{
#ifdef USE_PLUGINS
#endif
}