GNUnet 0.28.0-dev.5-30-g71440b34f
 
Loading...
Searching...
No Matches
crypto_gcrypt.c File Reference

global initialization of libgcrypt More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include <gcrypt.h>
Include dependency graph for crypto_gcrypt.c:

Go to the source code of this file.

Functions

static void * w_malloc (size_t n)
 Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
 
static int w_check (const void *p)
 Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
 
void GNUNET_CRYPTO_gcrypt_init (void)
 
void __attribute__ ((constructor))
 Initialize libgcrypt.
 
void GNUNET_CRYPTO_gcrypt_fini (void)
 
void __attribute__ ((destructor))
 Nicely shut down libgcrypt.
 

Detailed Description

global initialization of libgcrypt

Author
Martin Schanzenbach

Randomness generation is done with libsodium (see crypto_random.c), but ECC, RSA, CS, KDF, Paillier and the symmetric primitives still use libgcrypt. libgcrypt requires that gcry_check_version() runs before any other libgcrypt function; most entry points paper over a missing initialization by initializing themselves lazily, but some do not. gcry_mpi_const() – what GCRYMPI_CONST_ONE and friends expand to – calls log_bug() and aborts the process with "MPI subsystem not initialized" instead.

Hence this file, whose only job is to run that global bootstrap from a library constructor. Keep it free of anything else.

Definition in file crypto_gcrypt.c.

Function Documentation

◆ w_malloc()

static void * w_malloc ( size_t  n)
static

Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.

Definition at line 50 of file crypto_gcrypt.c.

51{
52 return calloc (n, 1);
53}

Referenced by __attribute__().

Here is the caller graph for this function:

◆ w_check()

static int w_check ( const void *  p)
static

Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.

Definition at line 61 of file crypto_gcrypt.c.

62{
63 (void) p;
64 return 0; /* not secure memory */
65}
static struct GNUNET_Process * p
Helper process we started.
Definition gnunet-uri.c:38

References p.

Referenced by __attribute__().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_gcrypt_init()

void GNUNET_CRYPTO_gcrypt_init ( void  )

◆ __attribute__() [1/2]

void __attribute__ ( (constructor)  )

Initialize libgcrypt.

Definition at line 74 of file crypto_gcrypt.c.

76{
77 gcry_error_t rc;
78
79 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
80 {
81 fprintf (
82 stderr,
83 _ ("libgcrypt has not the expected version (version %s is required).\n"),
84 NEED_LIBGCRYPT_VERSION);
85 GNUNET_assert (0);
86 }
87 /* set custom allocators */
88 gcry_set_allocation_handler (&w_malloc, &w_malloc, &w_check, &realloc, &free);
89 /* Disable use of secure memory */
90 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
91 fprintf (stderr,
92 "Failed to set libgcrypt option %s: %s\n",
93 "DISABLE_SECMEM",
94 gcry_strerror (rc));
95 /* Otherwise gnunet-ecc takes forever to complete, besides
96 we are fine with "just" using GCRY_STRONG_RANDOM */
97 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
98 fprintf (stderr,
99 "Failed to set libgcrypt option %s: %s\n",
100 "ENABLE_QUICK_RANDOM",
101 gcry_strerror (rc));
102 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
103}
static void * w_malloc(size_t n)
Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
static int w_check(const void *p)
Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define _(String)
GNU gettext support macro.
Definition platform.h:179

References _, GNUNET_assert, w_check(), and w_malloc().

Here is the call graph for this function:

◆ GNUNET_CRYPTO_gcrypt_fini()

void GNUNET_CRYPTO_gcrypt_fini ( void  )

◆ __attribute__() [2/2]

void __attribute__ ( (destructor)  )

Nicely shut down libgcrypt.

Definition at line 112 of file crypto_gcrypt.c.

114{
115 gcry_set_progress_handler (NULL, NULL);
116#ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
117 (void) gcry_control (GCRYCTL_CLOSE_RANDOM_DEVICE, 0);
118#endif
119}