gcrypt hasher 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
20 #include <library.h>
21 #include <debug.h>
22
23 #include <errno.h>
24 #include <gcrypt.h>
25 #include <pthread.h>
26
27 typedef struct private_gcrypt_plugin_t private_gcrypt_plugin_t;
28
29 /**
30 * private data of gcrypt_plugin
31 */
32 struct private_gcrypt_plugin_t {
33
34 /**
35 * public functions
36 */
37 gcrypt_plugin_t public;
38 };
39
40 /**
41 * Thread callback implementations for pthread
42 */
43 GCRY_THREAD_OPTION_PTHREAD_IMPL;
44
45 /**
46 * Implementation of gcrypt_plugin_t.destroy
47 */
48 static void destroy(private_gcrypt_plugin_t *this)
49 {
50 lib->crypto->remove_hasher(lib->crypto,
51 (hasher_constructor_t)gcrypt_hasher_create);
52 free(this);
53 }
54
55 /*
56 * see header file
57 */
58 plugin_t *plugin_create()
59 {
60 private_gcrypt_plugin_t *this;
61
62 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
63
64 if (!gcry_check_version(GCRYPT_VERSION))
65 {
66 DBG1("libgcrypt version mismatch");
67 return NULL;
68 }
69
70 /* we currently do not use secure memory */
71 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
72 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
73
74 this = malloc_thing(private_gcrypt_plugin_t);
75
76 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
77
78 /* hashers */
79 lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
80 (hasher_constructor_t)gcrypt_hasher_create);
81 lib->crypto->add_hasher(lib->crypto, HASH_MD2,
82 (hasher_constructor_t)gcrypt_hasher_create);
83 lib->crypto->add_hasher(lib->crypto, HASH_MD4,
84 (hasher_constructor_t)gcrypt_hasher_create);
85 lib->crypto->add_hasher(lib->crypto, HASH_MD5,
86 (hasher_constructor_t)gcrypt_hasher_create);
87 lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
88 (hasher_constructor_t)gcrypt_hasher_create);
89 lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
90 (hasher_constructor_t)gcrypt_hasher_create);
91 lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
92 (hasher_constructor_t)gcrypt_hasher_create);
93
94 return &this->public.plugin;
95 }
96