4f66e3421075f06e92cc96c25de02cdbc7a0ae31
[strongswan.git] / src / libstrongswan / plugins / gcrypt / gcrypt_plugin.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "gcrypt_plugin.h"
17
18 #include "gcrypt_hasher.h"
19 #include "gcrypt_crypter.h"
20
21 #include <library.h>
22 #include <debug.h>
23 #include <utils/mutex.h>
24
25 #include <errno.h>
26 #include <gcrypt.h>
27
28 typedef struct private_gcrypt_plugin_t private_gcrypt_plugin_t;
29
30 /**
31 * private data of gcrypt_plugin
32 */
33 struct private_gcrypt_plugin_t {
34
35 /**
36 * public functions
37 */
38 gcrypt_plugin_t public;
39 };
40
41 /**
42 * gcrypt mutex initialization wrapper
43 */
44 static int mutex_init(void **lock)
45 {
46 *lock = mutex_create(MUTEX_DEFAULT);
47 return 0;
48 }
49
50 /**
51 * gcrypt mutex cleanup wrapper
52 */
53 static int mutex_destroy(void **lock)
54 {
55 mutex_t *mutex = *lock;
56
57 mutex->destroy(mutex);
58 return 0;
59 }
60
61 /**
62 * gcrypt mutex lock wrapper
63 */
64 static int mutex_lock(void **lock)
65 {
66 mutex_t *mutex = *lock;
67
68 mutex->lock(mutex);
69 return 0;
70 }
71
72 /**
73 * gcrypt mutex unlock wrapper
74 */
75 static int mutex_unlock(void **lock)
76 {
77 mutex_t *mutex = *lock;
78
79 mutex->unlock(mutex);
80 return 0;
81 }
82
83 /**
84 * gcrypt locking functions using our mutex_t
85 */
86 static struct gcry_thread_cbs thread_functions = {
87 GCRY_THREAD_OPTION_USER, NULL,
88 mutex_init, mutex_destroy, mutex_lock, mutex_unlock,
89 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
90 };
91
92 /**
93 * Implementation of gcrypt_plugin_t.destroy
94 */
95 static void destroy(private_gcrypt_plugin_t *this)
96 {
97 lib->crypto->remove_hasher(lib->crypto,
98 (hasher_constructor_t)gcrypt_hasher_create);
99 lib->crypto->remove_crypter(lib->crypto,
100 (crypter_constructor_t)gcrypt_crypter_create);
101 free(this);
102 }
103
104 /*
105 * see header file
106 */
107 plugin_t *plugin_create()
108 {
109 private_gcrypt_plugin_t *this;
110
111 gcry_control(GCRYCTL_SET_THREAD_CBS, &thread_functions);
112
113 if (!gcry_check_version(GCRYPT_VERSION))
114 {
115 DBG1("libgcrypt version mismatch");
116 return NULL;
117 }
118
119 /* we currently do not use secure memory */
120 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
121 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
122
123 this = malloc_thing(private_gcrypt_plugin_t);
124
125 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
126
127 /* hashers */
128 lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
129 (hasher_constructor_t)gcrypt_hasher_create);
130 lib->crypto->add_hasher(lib->crypto, HASH_MD2,
131 (hasher_constructor_t)gcrypt_hasher_create);
132 lib->crypto->add_hasher(lib->crypto, HASH_MD4,
133 (hasher_constructor_t)gcrypt_hasher_create);
134 lib->crypto->add_hasher(lib->crypto, HASH_MD5,
135 (hasher_constructor_t)gcrypt_hasher_create);
136 lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
137 (hasher_constructor_t)gcrypt_hasher_create);
138 lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
139 (hasher_constructor_t)gcrypt_hasher_create);
140 lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
141 (hasher_constructor_t)gcrypt_hasher_create);
142
143 /* crypters */
144 lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
145 (crypter_constructor_t)gcrypt_crypter_create);
146 lib->crypto->add_crypter(lib->crypto, ENCR_IDEA,
147 (crypter_constructor_t)gcrypt_crypter_create);
148 lib->crypto->add_crypter(lib->crypto, ENCR_CAST,
149 (crypter_constructor_t)gcrypt_crypter_create);
150 lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH,
151 (crypter_constructor_t)gcrypt_crypter_create);
152 lib->crypto->add_crypter(lib->crypto, ENCR_DES,
153 (crypter_constructor_t)gcrypt_crypter_create);
154 lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB,
155 (crypter_constructor_t)gcrypt_crypter_create);
156 lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC,
157 (crypter_constructor_t)gcrypt_crypter_create);
158 lib->crypto->add_crypter(lib->crypto, ENCR_CAMELLIA_CBC,
159 (crypter_constructor_t)gcrypt_crypter_create);
160 lib->crypto->add_crypter(lib->crypto, ENCR_SERPENT_CBC,
161 (crypter_constructor_t)gcrypt_crypter_create);
162 lib->crypto->add_crypter(lib->crypto, ENCR_TWOFISH_CBC,
163 (crypter_constructor_t)gcrypt_crypter_create);
164
165 return &this->public.plugin;
166 }
167