Updated Czech translation
[platform/upstream/libsecret.git] / egg / egg-libgcrypt.c
1 /* 
2  * gnome-keyring
3  * 
4  * Copyright (C) 2008 Stefan Walter
5  * 
6  * This program is free software; you can redistribute it and/or modify 
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *  
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *  
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  *
21  * Author: Stef Walter <stefw@thewalter.net>
22  */
23
24 #include "config.h"
25
26 #include "egg-libgcrypt.h"
27 #include "egg-secure-memory.h"
28
29 #include <glib.h>
30
31 #include <gcrypt.h>
32
33 EGG_SECURE_DECLARE (libgcrypt);
34
35 static void
36 log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
37 {
38         /* TODO: Figure out additional arguments */
39         g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
40 }
41
42 static int 
43 no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
44 {
45         /* TODO: Figure out additional arguments */
46         g_error ("couldn't allocate %lu bytes of memory", 
47                  (unsigned long int)sz);
48         return 0;
49 }
50
51 static void
52 fatal_handler (gpointer unused, int unknown, const gchar *msg)
53 {
54         /* TODO: Figure out additional arguments */
55         g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
56 }
57
58 static int
59 glib_thread_mutex_init (void **lock)
60 {
61         *lock = g_slice_new (GMutex);
62         g_mutex_init (*lock);
63         return 0;
64 }
65
66 static int 
67 glib_thread_mutex_destroy (void **lock)
68 {
69         g_mutex_clear (*lock);
70         g_slice_free (GMutex, *lock);
71         return 0;
72 }
73
74 static int 
75 glib_thread_mutex_lock (void **lock)
76 {
77         g_mutex_lock (*lock);
78         return 0;
79 }
80
81 static int 
82 glib_thread_mutex_unlock (void **lock)
83 {
84         g_mutex_unlock (*lock);
85         return 0;
86 }
87
88 static struct gcry_thread_cbs glib_thread_cbs = {
89         GCRY_THREAD_OPTION_USER, NULL,
90         glib_thread_mutex_init, glib_thread_mutex_destroy,
91         glib_thread_mutex_lock, glib_thread_mutex_unlock,
92         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
93 };
94
95 void
96 egg_libgcrypt_initialize (void)
97 {
98         static volatile gsize gcrypt_initialized = 0;
99         unsigned seed;
100
101         if (g_once_init_enter (&gcrypt_initialized)) {
102                 
103                 /* Only initialize libgcrypt if it hasn't already been initialized */
104                 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
105                         gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
106                         gcry_check_version (LIBGCRYPT_VERSION);
107                         gcry_set_log_handler (log_handler, NULL);
108                         gcry_set_outofcore_handler (no_mem_handler, NULL);
109                         gcry_set_fatalerror_handler (fatal_handler, NULL);
110                         gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
111                                                      (gcry_handler_alloc_t)egg_secure_alloc, 
112                                                      egg_secure_check, 
113                                                      (gcry_handler_realloc_t)egg_secure_realloc, 
114                                                      egg_secure_free);
115                         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
116                 }
117                 
118                 gcry_create_nonce (&seed, sizeof (seed));
119                 srand (seed);
120
121                 g_once_init_leave (&gcrypt_initialized, 1);
122         }
123 }