]>
Commit | Line | Data |
---|---|---|
4977018c MW |
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 | ||
f908ff9f | 18 | #include "gcrypt_hasher.h" |
80862c46 | 19 | #include "gcrypt_crypter.h" |
a41d0932 | 20 | #include "gcrypt_rng.h" |
1111088a | 21 | #include "gcrypt_dh.h" |
ff8d3ba3 | 22 | #include "gcrypt_rsa_private_key.h" |
3240cab9 | 23 | #include "gcrypt_rsa_public_key.h" |
f908ff9f | 24 | |
4977018c | 25 | #include <library.h> |
513a1a28 | 26 | #include <debug.h> |
eba64cef | 27 | #include <threading/mutex.h> |
513a1a28 MW |
28 | |
29 | #include <errno.h> | |
30 | #include <gcrypt.h> | |
4977018c MW |
31 | |
32 | typedef struct private_gcrypt_plugin_t private_gcrypt_plugin_t; | |
33 | ||
34 | /** | |
35 | * private data of gcrypt_plugin | |
36 | */ | |
37 | struct private_gcrypt_plugin_t { | |
38 | ||
39 | /** | |
40 | * public functions | |
41 | */ | |
42 | gcrypt_plugin_t public; | |
43 | }; | |
44 | ||
513a1a28 | 45 | /** |
8e97e327 | 46 | * gcrypt mutex initialization wrapper |
513a1a28 | 47 | */ |
8e97e327 MW |
48 | static int mutex_init(void **lock) |
49 | { | |
3901937d | 50 | *lock = mutex_create(MUTEX_TYPE_DEFAULT); |
8e97e327 MW |
51 | return 0; |
52 | } | |
53 | ||
54 | /** | |
55 | * gcrypt mutex cleanup wrapper | |
56 | */ | |
57 | static int mutex_destroy(void **lock) | |
58 | { | |
59 | mutex_t *mutex = *lock; | |
7daf5226 | 60 | |
8e97e327 MW |
61 | mutex->destroy(mutex); |
62 | return 0; | |
63 | } | |
64 | ||
65 | /** | |
66 | * gcrypt mutex lock wrapper | |
67 | */ | |
68 | static int mutex_lock(void **lock) | |
69 | { | |
70 | mutex_t *mutex = *lock; | |
7daf5226 | 71 | |
8e97e327 MW |
72 | mutex->lock(mutex); |
73 | return 0; | |
74 | } | |
75 | ||
76 | /** | |
77 | * gcrypt mutex unlock wrapper | |
78 | */ | |
79 | static int mutex_unlock(void **lock) | |
80 | { | |
81 | mutex_t *mutex = *lock; | |
7daf5226 | 82 | |
8e97e327 MW |
83 | mutex->unlock(mutex); |
84 | return 0; | |
85 | } | |
86 | ||
87 | /** | |
88 | * gcrypt locking functions using our mutex_t | |
89 | */ | |
90 | static struct gcry_thread_cbs thread_functions = { | |
91 | GCRY_THREAD_OPTION_USER, NULL, | |
92 | mutex_init, mutex_destroy, mutex_lock, mutex_unlock, | |
93 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
94 | }; | |
513a1a28 | 95 | |
4977018c MW |
96 | /** |
97 | * Implementation of gcrypt_plugin_t.destroy | |
98 | */ | |
99 | static void destroy(private_gcrypt_plugin_t *this) | |
100 | { | |
f908ff9f MW |
101 | lib->crypto->remove_hasher(lib->crypto, |
102 | (hasher_constructor_t)gcrypt_hasher_create); | |
80862c46 MW |
103 | lib->crypto->remove_crypter(lib->crypto, |
104 | (crypter_constructor_t)gcrypt_crypter_create); | |
a41d0932 MW |
105 | lib->crypto->remove_rng(lib->crypto, |
106 | (rng_constructor_t)gcrypt_rng_create); | |
1111088a MW |
107 | lib->crypto->remove_dh(lib->crypto, |
108 | (dh_constructor_t)gcrypt_dh_create); | |
ff8d3ba3 | 109 | lib->creds->remove_builder(lib->creds, |
a94acb58 | 110 | (builder_function_t)gcrypt_rsa_private_key_gen); |
3240cab9 | 111 | lib->creds->remove_builder(lib->creds, |
a94acb58 MW |
112 | (builder_function_t)gcrypt_rsa_private_key_load); |
113 | lib->creds->remove_builder(lib->creds, | |
114 | (builder_function_t)gcrypt_rsa_public_key_load); | |
4977018c MW |
115 | free(this); |
116 | } | |
117 | ||
118 | /* | |
119 | * see header file | |
120 | */ | |
9ce567f8 | 121 | plugin_t *gcrypt_plugin_create() |
4977018c | 122 | { |
513a1a28 | 123 | private_gcrypt_plugin_t *this; |
7daf5226 | 124 | |
8e97e327 | 125 | gcry_control(GCRYCTL_SET_THREAD_CBS, &thread_functions); |
7daf5226 | 126 | |
513a1a28 MW |
127 | if (!gcry_check_version(GCRYPT_VERSION)) |
128 | { | |
8b0e0910 | 129 | DBG1(DBG_LIB, "libgcrypt version mismatch"); |
513a1a28 MW |
130 | return NULL; |
131 | } | |
7daf5226 | 132 | |
513a1a28 MW |
133 | /* we currently do not use secure memory */ |
134 | gcry_control(GCRYCTL_DISABLE_SECMEM, 0); | |
810ce1f3 MW |
135 | if (lib->settings->get_bool(lib->settings, |
136 | "libstrongswan.plugins.gcrypt.quick_random", FALSE)) | |
137 | { | |
138 | gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); | |
139 | } | |
513a1a28 | 140 | gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); |
7daf5226 | 141 | |
513a1a28 | 142 | this = malloc_thing(private_gcrypt_plugin_t); |
7daf5226 | 143 | |
4977018c | 144 | this->public.plugin.destroy = (void(*)(plugin_t*))destroy; |
7daf5226 | 145 | |
f908ff9f MW |
146 | /* hashers */ |
147 | lib->crypto->add_hasher(lib->crypto, HASH_SHA1, | |
148 | (hasher_constructor_t)gcrypt_hasher_create); | |
f908ff9f MW |
149 | lib->crypto->add_hasher(lib->crypto, HASH_MD4, |
150 | (hasher_constructor_t)gcrypt_hasher_create); | |
151 | lib->crypto->add_hasher(lib->crypto, HASH_MD5, | |
152 | (hasher_constructor_t)gcrypt_hasher_create); | |
b6f739c1 AS |
153 | lib->crypto->add_hasher(lib->crypto, HASH_SHA224, |
154 | (hasher_constructor_t)gcrypt_hasher_create); | |
f908ff9f MW |
155 | lib->crypto->add_hasher(lib->crypto, HASH_SHA256, |
156 | (hasher_constructor_t)gcrypt_hasher_create); | |
157 | lib->crypto->add_hasher(lib->crypto, HASH_SHA384, | |
158 | (hasher_constructor_t)gcrypt_hasher_create); | |
159 | lib->crypto->add_hasher(lib->crypto, HASH_SHA512, | |
160 | (hasher_constructor_t)gcrypt_hasher_create); | |
7daf5226 | 161 | |
80862c46 MW |
162 | /* crypters */ |
163 | lib->crypto->add_crypter(lib->crypto, ENCR_3DES, | |
164 | (crypter_constructor_t)gcrypt_crypter_create); | |
80862c46 MW |
165 | lib->crypto->add_crypter(lib->crypto, ENCR_CAST, |
166 | (crypter_constructor_t)gcrypt_crypter_create); | |
167 | lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH, | |
168 | (crypter_constructor_t)gcrypt_crypter_create); | |
169 | lib->crypto->add_crypter(lib->crypto, ENCR_DES, | |
170 | (crypter_constructor_t)gcrypt_crypter_create); | |
171 | lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB, | |
172 | (crypter_constructor_t)gcrypt_crypter_create); | |
173 | lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC, | |
174 | (crypter_constructor_t)gcrypt_crypter_create); | |
175 | lib->crypto->add_crypter(lib->crypto, ENCR_CAMELLIA_CBC, | |
176 | (crypter_constructor_t)gcrypt_crypter_create); | |
177 | lib->crypto->add_crypter(lib->crypto, ENCR_SERPENT_CBC, | |
178 | (crypter_constructor_t)gcrypt_crypter_create); | |
179 | lib->crypto->add_crypter(lib->crypto, ENCR_TWOFISH_CBC, | |
180 | (crypter_constructor_t)gcrypt_crypter_create); | |
7daf5226 | 181 | |
a41d0932 | 182 | /* random numbers */ |
7daf5226 | 183 | lib->crypto->add_rng(lib->crypto, RNG_WEAK, |
a41d0932 | 184 | (rng_constructor_t)gcrypt_rng_create); |
7daf5226 | 185 | lib->crypto->add_rng(lib->crypto, RNG_STRONG, |
a41d0932 | 186 | (rng_constructor_t)gcrypt_rng_create); |
7daf5226 | 187 | lib->crypto->add_rng(lib->crypto, RNG_TRUE, |
a41d0932 | 188 | (rng_constructor_t)gcrypt_rng_create); |
7daf5226 | 189 | |
1111088a | 190 | /* diffie hellman groups, using modp */ |
7daf5226 | 191 | lib->crypto->add_dh(lib->crypto, MODP_2048_BIT, |
1111088a | 192 | (dh_constructor_t)gcrypt_dh_create); |
4590260b MW |
193 | lib->crypto->add_dh(lib->crypto, MODP_2048_224, |
194 | (dh_constructor_t)gcrypt_dh_create); | |
195 | lib->crypto->add_dh(lib->crypto, MODP_2048_256, | |
196 | (dh_constructor_t)gcrypt_dh_create); | |
7daf5226 | 197 | lib->crypto->add_dh(lib->crypto, MODP_1536_BIT, |
1111088a | 198 | (dh_constructor_t)gcrypt_dh_create); |
7daf5226 | 199 | lib->crypto->add_dh(lib->crypto, MODP_3072_BIT, |
1111088a | 200 | (dh_constructor_t)gcrypt_dh_create); |
7daf5226 | 201 | lib->crypto->add_dh(lib->crypto, MODP_4096_BIT, |
1111088a | 202 | (dh_constructor_t)gcrypt_dh_create); |
7daf5226 | 203 | lib->crypto->add_dh(lib->crypto, MODP_6144_BIT, |
1111088a | 204 | (dh_constructor_t)gcrypt_dh_create); |
7daf5226 | 205 | lib->crypto->add_dh(lib->crypto, MODP_8192_BIT, |
1111088a MW |
206 | (dh_constructor_t)gcrypt_dh_create); |
207 | lib->crypto->add_dh(lib->crypto, MODP_1024_BIT, | |
208 | (dh_constructor_t)gcrypt_dh_create); | |
4590260b MW |
209 | lib->crypto->add_dh(lib->crypto, MODP_1024_160, |
210 | (dh_constructor_t)gcrypt_dh_create); | |
7daf5226 | 211 | lib->crypto->add_dh(lib->crypto, MODP_768_BIT, |
1111088a | 212 | (dh_constructor_t)gcrypt_dh_create); |
7daf5226 | 213 | |
ff8d3ba3 MW |
214 | /* RSA */ |
215 | lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA, | |
a94acb58 MW |
216 | (builder_function_t)gcrypt_rsa_private_key_gen); |
217 | lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA, | |
218 | (builder_function_t)gcrypt_rsa_private_key_load); | |
3240cab9 | 219 | lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA, |
a94acb58 | 220 | (builder_function_t)gcrypt_rsa_public_key_load); |
7daf5226 | 221 | |
4977018c MW |
222 | return &this->public.plugin; |
223 | } | |
224 |