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