vaseboot/VasEBoot-core/lib/libgcrypt_wrap/mem.c

214 lines
3.9 KiB
C

#include <VasEBoot/gcrypt/g10lib.h>
#include <VasEBoot/gcrypt/gpg-error.h>
#include <VasEBoot/term.h>
#include <VasEBoot/crypto.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/env.h>
#include <VasEBoot/safemath.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
void *
gcry_malloc (size_t n)
{
return VasEBoot_malloc (n);
}
void *
gcry_malloc_secure (size_t n)
{
return VasEBoot_malloc (n);
}
void
gcry_free (void *a)
{
VasEBoot_free (a);
}
int
gcry_is_secure (const void *a __attribute__ ((unused)))
{
return 0;
}
/* FIXME: implement "exit". */
void *
gcry_xcalloc (size_t n, size_t m)
{
void *ret;
size_t sz;
if (VasEBoot_mul (n, m, &sz))
VasEBoot_fatal ("gcry_xcalloc would overflow");
ret = VasEBoot_zalloc (sz);
if (!ret)
VasEBoot_fatal ("gcry_xcalloc failed");
return ret;
}
void *
_gcry_calloc (size_t n, size_t m)
{
size_t sz;
if (VasEBoot_mul (n, m, &sz))
return NULL;
return VasEBoot_zalloc (sz);
}
void *
gcry_xmalloc_secure (size_t n)
{
void *ret;
ret = VasEBoot_malloc (n);
if (!ret)
VasEBoot_fatal ("gcry_xmalloc failed");
return ret;
}
void *
gcry_xcalloc_secure (size_t n, size_t m)
{
void *ret;
size_t sz;
if (VasEBoot_mul (n, m, &sz))
VasEBoot_fatal ("gcry_xcalloc would overflow");
ret = VasEBoot_zalloc (sz);
if (!ret)
VasEBoot_fatal ("gcry_xcalloc failed");
return ret;
}
void *
_gcry_calloc_secure (size_t n, size_t m)
{
size_t sz;
if (VasEBoot_mul (n, m, &sz))
return NULL;
return VasEBoot_zalloc (sz);
}
void *
gcry_xmalloc (size_t n)
{
void *ret;
ret = VasEBoot_malloc (n);
if (!ret)
VasEBoot_fatal ("gcry_xmalloc failed");
return ret;
}
void *
gcry_xrealloc (void *a, size_t n)
{
void *ret;
ret = VasEBoot_realloc (a, n);
if (!ret)
VasEBoot_fatal ("gcry_xrealloc failed");
return ret;
}
void *
_gcry_realloc (void *a, size_t n)
{
return VasEBoot_realloc (a, n);
}
void _gcry_log_printf (const char *fmt, ...)
{
va_list args;
const char *debug = VasEBoot_env_get ("debug");
if (! debug)
return;
if (VasEBoot_strword (debug, "all") || VasEBoot_strword (debug, "gcrypt"))
{
VasEBoot_printf ("gcrypt: ");
va_start (args, fmt);
VasEBoot_vprintf (fmt, args);
va_end (args);
VasEBoot_refresh ();
}
}
void _gcry_log_debug (const char *fmt, ...)
{
va_list args;
const char *debug = VasEBoot_env_get ("debug");
if (! debug)
return;
if (VasEBoot_strword (debug, "all") || VasEBoot_strword (debug, "gcrypt"))
{
VasEBoot_printf ("gcrypt: ");
va_start (args, fmt);
VasEBoot_vprintf (fmt, args);
va_end (args);
VasEBoot_refresh ();
}
}
void _gcry_log_bug (const char *fmt, ...)
{
va_list args;
VasEBoot_printf ("gcrypt bug: ");
va_start (args, fmt);
VasEBoot_vprintf (fmt, args);
va_end (args);
VasEBoot_refresh ();
VasEBoot_fatal ("gcrypt bug");
}
gcry_err_code_t
gpg_err_code_from_errno (int err)
{
switch (err)
{
case VAS_EBOOT_ERR_NONE:
return GPG_ERR_NO_ERROR;
case VAS_EBOOT_ERR_OUT_OF_MEMORY:
return GPG_ERR_OUT_OF_MEMORY;
default:
return GPG_ERR_GENERAL;
}
}
gcry_err_code_t
gpg_error_from_syserror (void)
{
return gpg_err_code_from_errno(VasEBoot_errno);
}
gcry_err_code_t
gpg_err_code_from_syserror (void)
{
return gpg_error_from_syserror ();
}
void _gcry_fatal_error(int rc, const char *text )
{
VasEBoot_fatal("gcry fatal %d: %s", rc, text);
}
void _gcry_randomize (void *buffer __attribute__((unused)), size_t length __attribute__((unused)),
enum gcry_random_level level __attribute__((unused)))
{
VasEBoot_fatal("Attempt to get secure random numbers");
}
void *_gcry_random_bytes_secure (size_t nbytes __attribute__((unused)), enum gcry_random_level level __attribute__((unused)))
{
VasEBoot_fatal("Attempt to get secure random numbers");
}
const char *gpg_strerror (gpg_error_t err)
{
static char buf[256];
VasEBoot_snprintf(buf, sizeof(buf) - 5, "gpg error %d\n", err);
return buf;
}