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 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Author: Stef Walter <stef@memberwebs.com>
24 #ifndef EGG_SECURE_MEMORY_H
25 #define EGG_SECURE_MEMORY_H
29 /* -------------------------------------------------------------------
30 * Low Level Secure Memory
32 * IMPORTANT: This is pure vanila standard C, no glib. We need this
33 * because certain consumers of this protocol need to be built
34 * without linking in any special libraries. ie: the PKCS#11 module.
38 * In order to use these functions in a module the following functions
39 * must be defined somewhere, and provide appropriate locking for
40 * secure memory between threads:
45 void (* unlock) (void);
46 void * (* fallback) (void *pointer,
49 const char * pool_version;
52 #define EGG_SECURE_POOL_VER_STR "1.0"
53 #define EGG_SECURE_GLOBALS SECMEM_pool_data_v1_0
55 #define EGG_SECURE_DEFINE_GLOBALS(lock, unlock, fallback) \
56 egg_secure_glob EGG_SECURE_GLOBALS = { \
57 lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
59 #define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
60 static GMutex memory_mutex = { NULL, }; \
61 static void egg_memory_lock (void) \
62 { g_mutex_lock (&memory_mutex); } \
63 static void egg_memory_unlock (void) \
64 { g_mutex_unlock (&memory_mutex); } \
65 EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
67 extern egg_secure_glob EGG_SECURE_GLOBALS;
72 * Allocations return NULL on failure.
75 #define EGG_SECURE_USE_FALLBACK 0x0001
77 #define EGG_SECURE_DECLARE(tag) \
78 static inline void* egg_secure_alloc (size_t length) { \
79 return egg_secure_alloc_full (G_STRINGIFY (tag), length, EGG_SECURE_USE_FALLBACK); \
81 static inline void* egg_secure_realloc (void *p, size_t length) { \
82 return egg_secure_realloc_full (G_STRINGIFY (tag), p, length, EGG_SECURE_USE_FALLBACK); \
84 static inline void* egg_secure_strdup (const char *str) { \
85 return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \
87 static inline void* egg_secure_strndup (const char *str, size_t length) { \
88 return egg_secure_strndup_full (G_STRINGIFY (tag), str, length, EGG_SECURE_USE_FALLBACK); \
91 void* egg_secure_alloc_full (const char *tag, size_t length, int options);
93 void* egg_secure_realloc_full (const char *tag, void *p, size_t length, int options);
95 void egg_secure_free (void* p);
97 void egg_secure_free_full (void* p, int fallback);
99 void egg_secure_clear (void *p, size_t length);
101 int egg_secure_check (const void* p);
103 void egg_secure_validate (void);
105 char* egg_secure_strdup_full (const char *tag, const char *str, int options);
107 char* egg_secure_strndup_full (const char *tag, const char *str, size_t length, int options);
109 void egg_secure_strclear (char *str);
111 void egg_secure_strfree (char *str);
115 size_t request_length;
119 egg_secure_rec * egg_secure_records (unsigned int *count);
121 #endif /* EGG_SECURE_MEMORY_H */