5568de8d47ddde0da6d9011d926007dffb2ca4b8
[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
24 #include <errno.h>
25 #include <gcrypt.h>
26 #include <pthread.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 * Thread callback implementations for pthread
43 */
44 GCRY_THREAD_OPTION_PTHREAD_IMPL;
45
46 /**
47 * Implementation of gcrypt_plugin_t.destroy
48 */
49 static void destroy(private_gcrypt_plugin_t *this)
50 {
51 lib->crypto->remove_hasher(lib->crypto,
52 (hasher_constructor_t)gcrypt_hasher_create);
53 lib->crypto->remove_crypter(lib->crypto,
54 (crypter_constructor_t)gcrypt_crypter_create);
55 free(this);
56 }
57
58 /*
59 * see header file
60 */
61 plugin_t *plugin_create()
62 {
63 private_gcrypt_plugin_t *this;
64
65 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
66
67 if (!gcry_check_version(GCRYPT_VERSION))
68 {
69 DBG1("libgcrypt version mismatch");
70 return NULL;
71 }
72
73 /* we currently do not use secure memory */
74 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
75 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
76
77 this = malloc_thing(private_gcrypt_plugin_t);
78
79 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
80
81 /* hashers */
82 lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
83 (hasher_constructor_t)gcrypt_hasher_create);
84 lib->crypto->add_hasher(lib->crypto, HASH_MD2,
85 (hasher_constructor_t)gcrypt_hasher_create);
86 lib->crypto->add_hasher(lib->crypto, HASH_MD4,
87 (hasher_constructor_t)gcrypt_hasher_create);
88 lib->crypto->add_hasher(lib->crypto, HASH_MD5,
89 (hasher_constructor_t)gcrypt_hasher_create);
90 lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
91 (hasher_constructor_t)gcrypt_hasher_create);
92 lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
93 (hasher_constructor_t)gcrypt_hasher_create);
94 lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
95 (hasher_constructor_t)gcrypt_hasher_create);
96
97 /* crypters */
98 lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
99 (crypter_constructor_t)gcrypt_crypter_create);
100 lib->crypto->add_crypter(lib->crypto, ENCR_IDEA,
101 (crypter_constructor_t)gcrypt_crypter_create);
102 lib->crypto->add_crypter(lib->crypto, ENCR_CAST,
103 (crypter_constructor_t)gcrypt_crypter_create);
104 lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH,
105 (crypter_constructor_t)gcrypt_crypter_create);
106 lib->crypto->add_crypter(lib->crypto, ENCR_DES,
107 (crypter_constructor_t)gcrypt_crypter_create);
108 lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB,
109 (crypter_constructor_t)gcrypt_crypter_create);
110 lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC,
111 (crypter_constructor_t)gcrypt_crypter_create);
112 lib->crypto->add_crypter(lib->crypto, ENCR_CAMELLIA_CBC,
113 (crypter_constructor_t)gcrypt_crypter_create);
114 lib->crypto->add_crypter(lib->crypto, ENCR_SERPENT_CBC,
115 (crypter_constructor_t)gcrypt_crypter_create);
116 lib->crypto->add_crypter(lib->crypto, ENCR_TWOFISH_CBC,
117 (crypter_constructor_t)gcrypt_crypter_create);
118
119 return &this->public.plugin;
120 }
121