summaryrefslogtreecommitdiff
path: root/src/stdmem.c
blob: d0ebef0ccc34669ad875536fb816fa0621a34b3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* stdmem.c  -	private memory allocator
 * Copyright (C) 1998, 2000, 2002, 2005, 2008 Free Software Foundation, Inc.
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser general Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Description of the layered memory management in Libgcrypt:
 *
 *                                  [User]
 *                                    |
 *                                    |
 *                                   \ /
 *                   global.c: [MM entrance points]   -----> [user callbacks]
 *                               |          |
 *                               |          |
 *                              \ /        \ /
 *
 *      stdmem.c: [non-secure handlers] [secure handlers]
 *
 *                               |         |
 *                               |         |
 *                              \ /       \ /
 *
 *                  stdmem.c: [ memory guard ]
 *
 *                               |         |
 *                               |         |
 *                              \ /       \ /
 *
 *           libc: [ MM functions ]     secmem.c: [ secure MM functions]
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>

#include "g10lib.h"
#include "stdmem.h"
#include "secmem.h"



/*
 * Allocate memory of size n.
 * Return NULL if we are out of memory.
 */
void *
_gcry_private_malloc (size_t n)
{
  if (!n)
    {
      gpg_err_set_errno (EINVAL);
      return NULL; /* Allocating 0 bytes is undefined - we better return
                      an error to detect such coding errors.  */
    }

  return malloc( n );
}


/*
 * Allocate memory of size N from the secure memory pool.  Return NULL
 * if we are out of memory.  XHINT tells the allocator that the caller
 * used an xmalloc style call.
 */
void *
_gcry_private_malloc_secure (size_t n, int xhint)
{
  if (!n)
    {
      gpg_err_set_errno (EINVAL);
      return NULL; /* Allocating 0 bytes is undefined - better return an
                      error to detect such coding errors.  */
    }

  return _gcry_secmem_malloc (n, xhint);
}


/*
 * Realloc and clear the old space.  XHINT tells the allocator that
 * the caller used an xmalloc style call.  Returns NULL if there is
 * not enough memory.
 */
void *
_gcry_private_realloc (void *a, size_t n, int xhint)
{
  if ( _gcry_private_is_secure(a) )
    {
      return _gcry_secmem_realloc (a, n, xhint);
    }
  else
    {
      return realloc( a, n );
    }
}


/*
 * Free a memory block allocated by this or the secmem module
 */
void
_gcry_private_free (void *a)
{
  unsigned char *p = a;
  unsigned char *freep;

  if (!p)
    return;

  freep = p;

  if (!_gcry_private_is_secure (freep) ||
      !_gcry_secmem_free (freep))
    {
      free (freep);
    }
}