initialize gcrypt threadsave, currently for pthread only
[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 <library.h>
19 #include <debug.h>
20
21 #include <errno.h>
22 #include <gcrypt.h>
23 #include <pthread.h>
24
25 typedef struct private_gcrypt_plugin_t private_gcrypt_plugin_t;
26
27 /**
28 * private data of gcrypt_plugin
29 */
30 struct private_gcrypt_plugin_t {
31
32 /**
33 * public functions
34 */
35 gcrypt_plugin_t public;
36 };
37
38 /**
39 * Thread callback implementations for pthread
40 */
41 GCRY_THREAD_OPTION_PTHREAD_IMPL;
42
43 /**
44 * Implementation of gcrypt_plugin_t.destroy
45 */
46 static void destroy(private_gcrypt_plugin_t *this)
47 {
48 free(this);
49 }
50
51 /*
52 * see header file
53 */
54 plugin_t *plugin_create()
55 {
56 private_gcrypt_plugin_t *this;
57
58 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
59
60 if (!gcry_check_version(GCRYPT_VERSION))
61 {
62 DBG1("libgcrypt version mismatch");
63 return NULL;
64 }
65
66 /* we currently do not use secure memory */
67 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
68 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
69
70 this = malloc_thing(private_gcrypt_plugin_t);
71
72 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
73
74 return &this->public.plugin;
75 }
76