Use upstream tag
[platform/upstream/libsecret.git] / egg / egg-secure-memory.h
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
3
4    Copyright (C) 2007 Stefan Walter
5
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.
10
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.
15
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/>.
19
20    Author: Stef Walter <stef@memberwebs.com>
21 */
22
23 #ifndef EGG_SECURE_MEMORY_H
24 #define EGG_SECURE_MEMORY_H
25
26 #include <stdlib.h>
27
28 /* -------------------------------------------------------------------
29  * Low Level Secure Memory
30  *
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.
34  *
35  * Thread locking
36  *
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:
40  */
41
42 typedef struct {
43         void       (* lock)        (void);
44         void       (* unlock)      (void);
45         void *     (* fallback)    (void *pointer,
46                                     size_t length);
47         void *        pool_data;
48         const char *  pool_version;
49 } egg_secure_glob;
50
51 #define EGG_SECURE_POOL_VER_STR             "1.0"
52 #define EGG_SECURE_GLOBALS SECMEM_pool_data_v1_0
53
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 };
57
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);
65
66 extern egg_secure_glob EGG_SECURE_GLOBALS;
67
68 /*
69  * Main functionality
70  *
71  * Allocations return NULL on failure.
72  */
73
74 #define EGG_SECURE_USE_FALLBACK     0x0001
75
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); \
79         } \
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); \
82         } \
83         static inline void* egg_secure_strdup (const char *str) { \
84                 return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \
85         } \
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); \
88         }
89
90 void*  egg_secure_alloc_full   (const char *tag, size_t length, int options);
91
92 void*  egg_secure_realloc_full (const char *tag, void *p, size_t length, int options);
93
94 void   egg_secure_free         (void* p);
95
96 void   egg_secure_free_full    (void* p, int fallback);
97
98 void   egg_secure_clear        (void *p, size_t length);
99
100 int    egg_secure_check        (const void* p);
101
102 void   egg_secure_validate     (void);
103
104 char*  egg_secure_strdup_full  (const char *tag, const char *str, int options);
105
106 char*  egg_secure_strndup_full (const char *tag, const char *str, size_t length, int options);
107
108 void   egg_secure_strclear     (char *str);
109
110 void   egg_secure_strfree      (char *str);
111
112 typedef struct {
113         const char *tag;
114         size_t request_length;
115         size_t block_length;
116 } egg_secure_rec;
117
118 egg_secure_rec *   egg_secure_records    (unsigned int *count);
119
120 #endif /* EGG_SECURE_MEMORY_H */