04eb512d3919216d3d80155fc690c9150aad959b
[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 #include "gcrypt_rng.h"
21 #include "gcrypt_dh.h"
22
23 #include <library.h>
24 #include <debug.h>
25 #include <utils/mutex.h>
26
27 #include <errno.h>
28 #include <gcrypt.h>
29
30 typedef struct private_gcrypt_plugin_t private_gcrypt_plugin_t;
31
32 /**
33 * private data of gcrypt_plugin
34 */
35 struct private_gcrypt_plugin_t {
36
37 /**
38 * public functions
39 */
40 gcrypt_plugin_t public;
41 };
42
43 /**
44 * gcrypt mutex initialization wrapper
45 */
46 static int mutex_init(void **lock)
47 {
48 *lock = mutex_create(MUTEX_DEFAULT);
49 return 0;
50 }
51
52 /**
53 * gcrypt mutex cleanup wrapper
54 */
55 static int mutex_destroy(void **lock)
56 {
57 mutex_t *mutex = *lock;
58
59 mutex->destroy(mutex);
60 return 0;
61 }
62
63 /**
64 * gcrypt mutex lock wrapper
65 */
66 static int mutex_lock(void **lock)
67 {
68 mutex_t *mutex = *lock;
69
70 mutex->lock(mutex);
71 return 0;
72 }
73
74 /**
75 * gcrypt mutex unlock wrapper
76 */
77 static int mutex_unlock(void **lock)
78 {
79 mutex_t *mutex = *lock;
80
81 mutex->unlock(mutex);
82 return 0;
83 }
84
85 /**
86 * gcrypt locking functions using our mutex_t
87 */
88 static struct gcry_thread_cbs thread_functions = {
89 GCRY_THREAD_OPTION_USER, NULL,
90 mutex_init, mutex_destroy, mutex_lock, mutex_unlock,
91 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
92 };
93
94 /**
95 * Implementation of gcrypt_plugin_t.destroy
96 */
97 static void destroy(private_gcrypt_plugin_t *this)
98 {
99 lib->crypto->remove_hasher(lib->crypto,
100 (hasher_constructor_t)gcrypt_hasher_create);
101 lib->crypto->remove_crypter(lib->crypto,
102 (crypter_constructor_t)gcrypt_crypter_create);
103 lib->crypto->remove_rng(lib->crypto,
104 (rng_constructor_t)gcrypt_rng_create);
105 lib->crypto->remove_dh(lib->crypto,
106 (dh_constructor_t)gcrypt_dh_create);
107 free(this);
108 }
109
110 /*
111 * see header file
112 */
113 plugin_t *plugin_create()
114 {
115 private_gcrypt_plugin_t *this;
116
117 gcry_control(GCRYCTL_SET_THREAD_CBS, &thread_functions);
118
119 if (!gcry_check_version(GCRYPT_VERSION))
120 {
121 DBG1("libgcrypt version mismatch");
122 return NULL;
123 }
124
125 /* we currently do not use secure memory */
126 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
127 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
128
129 this = malloc_thing(private_gcrypt_plugin_t);
130
131 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
132
133 /* hashers */
134 lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
135 (hasher_constructor_t)gcrypt_hasher_create);
136 lib->crypto->add_hasher(lib->crypto, HASH_MD2,
137 (hasher_constructor_t)gcrypt_hasher_create);
138 lib->crypto->add_hasher(lib->crypto, HASH_MD4,
139 (hasher_constructor_t)gcrypt_hasher_create);
140 lib->crypto->add_hasher(lib->crypto, HASH_MD5,
141 (hasher_constructor_t)gcrypt_hasher_create);
142 lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
143 (hasher_constructor_t)gcrypt_hasher_create);
144 lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
145 (hasher_constructor_t)gcrypt_hasher_create);
146 lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
147 (hasher_constructor_t)gcrypt_hasher_create);
148
149 /* crypters */
150 lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
151 (crypter_constructor_t)gcrypt_crypter_create);
152 lib->crypto->add_crypter(lib->crypto, ENCR_IDEA,
153 (crypter_constructor_t)gcrypt_crypter_create);
154 lib->crypto->add_crypter(lib->crypto, ENCR_CAST,
155 (crypter_constructor_t)gcrypt_crypter_create);
156 lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH,
157 (crypter_constructor_t)gcrypt_crypter_create);
158 lib->crypto->add_crypter(lib->crypto, ENCR_DES,
159 (crypter_constructor_t)gcrypt_crypter_create);
160 lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB,
161 (crypter_constructor_t)gcrypt_crypter_create);
162 lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC,
163 (crypter_constructor_t)gcrypt_crypter_create);
164 lib->crypto->add_crypter(lib->crypto, ENCR_CAMELLIA_CBC,
165 (crypter_constructor_t)gcrypt_crypter_create);
166 lib->crypto->add_crypter(lib->crypto, ENCR_SERPENT_CBC,
167 (crypter_constructor_t)gcrypt_crypter_create);
168 lib->crypto->add_crypter(lib->crypto, ENCR_TWOFISH_CBC,
169 (crypter_constructor_t)gcrypt_crypter_create);
170
171 /* random numbers */
172 lib->crypto->add_rng(lib->crypto, RNG_WEAK,
173 (rng_constructor_t)gcrypt_rng_create);
174 lib->crypto->add_rng(lib->crypto, RNG_STRONG,
175 (rng_constructor_t)gcrypt_rng_create);
176 lib->crypto->add_rng(lib->crypto, RNG_TRUE,
177 (rng_constructor_t)gcrypt_rng_create);
178
179 /* diffie hellman groups, using modp */
180 lib->crypto->add_dh(lib->crypto, MODP_2048_BIT,
181 (dh_constructor_t)gcrypt_dh_create);
182 lib->crypto->add_dh(lib->crypto, MODP_1536_BIT,
183 (dh_constructor_t)gcrypt_dh_create);
184 lib->crypto->add_dh(lib->crypto, MODP_3072_BIT,
185 (dh_constructor_t)gcrypt_dh_create);
186 lib->crypto->add_dh(lib->crypto, MODP_4096_BIT,
187 (dh_constructor_t)gcrypt_dh_create);
188 lib->crypto->add_dh(lib->crypto, MODP_6144_BIT,
189 (dh_constructor_t)gcrypt_dh_create);
190 lib->crypto->add_dh(lib->crypto, MODP_8192_BIT,
191 (dh_constructor_t)gcrypt_dh_create);
192 lib->crypto->add_dh(lib->crypto, MODP_1024_BIT,
193 (dh_constructor_t)gcrypt_dh_create);
194 lib->crypto->add_dh(lib->crypto, MODP_768_BIT,
195 (dh_constructor_t)gcrypt_dh_create);
196
197 return &this->public.plugin;
198 }
199