1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* egg-secure-memory.h - library for allocating memory that is non-pageable
4 Copyright (C) 2007 Stefan Walter
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 see <http://www.gnu.org/licenses/>.
20 Author: Stef Walter <stef@memberwebs.com>
23 #ifndef EGG_SECURE_MEMORY_H
24 #define EGG_SECURE_MEMORY_H
28 /* -------------------------------------------------------------------
29 * Low Level Secure Memory
31 * IMPORTANT: This is pure vanila standard C, no glib. We need this
32 * because certain consumers of this protocol need to be built
33 * without linking in any special libraries. ie: the PKCS#11 module.
37 * In order to use these functions in a module the following functions
38 * must be defined somewhere, and provide appropriate locking for
39 * secure memory between threads:
44 void (* unlock) (void);
45 void * (* fallback) (void *pointer,
48 const char * pool_version;
51 #define EGG_SECURE_POOL_VER_STR "1.0"
52 #define EGG_SECURE_GLOBALS SECMEM_pool_data_v1_0
54 #define EGG_SECURE_DEFINE_GLOBALS(lock, unlock, fallback) \
55 egg_secure_glob EGG_SECURE_GLOBALS = { \
56 lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
58 #define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
59 static GMutex memory_mutex = { NULL, }; \
60 static void egg_memory_lock (void) \
61 { g_mutex_lock (&memory_mutex); } \
62 static void egg_memory_unlock (void) \
63 { g_mutex_unlock (&memory_mutex); } \
64 EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
66 extern egg_secure_glob EGG_SECURE_GLOBALS;
71 * Allocations return NULL on failure.
74 #define EGG_SECURE_USE_FALLBACK 0x0001
76 #define EGG_SECURE_DECLARE(tag) \
77 static inline void* egg_secure_alloc (size_t length) { \
78 return egg_secure_alloc_full (G_STRINGIFY (tag), length, EGG_SECURE_USE_FALLBACK); \
80 static inline void* egg_secure_realloc (void *p, size_t length) { \
81 return egg_secure_realloc_full (G_STRINGIFY (tag), p, length, EGG_SECURE_USE_FALLBACK); \
83 static inline void* egg_secure_strdup (const char *str) { \
84 return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \
86 static inline void* egg_secure_strndup (const char *str, size_t length) { \
87 return egg_secure_strndup_full (G_STRINGIFY (tag), str, length, EGG_SECURE_USE_FALLBACK); \
90 void* egg_secure_alloc_full (const char *tag, size_t length, int options);
92 void* egg_secure_realloc_full (const char *tag, void *p, size_t length, int options);
94 void egg_secure_free (void* p);
96 void egg_secure_free_full (void* p, int fallback);
98 void egg_secure_clear (void *p, size_t length);
100 int egg_secure_check (const void* p);
102 void egg_secure_validate (void);
104 char* egg_secure_strdup_full (const char *tag, const char *str, int options);
106 char* egg_secure_strndup_full (const char *tag, const char *str, size_t length, int options);
108 void egg_secure_strclear (char *str);
110 void egg_secure_strfree (char *str);
114 size_t request_length;
118 egg_secure_rec * egg_secure_records (unsigned int *count);
120 #endif /* EGG_SECURE_MEMORY_H */