GNUnet  0.10.x
Macros | Functions
crypto_mpi.c File Reference

Helper functions for libgcrypt MPIs. More...

#include "platform.h"
#include <gcrypt.h>
#include "gnunet_crypto_lib.h"
Include dependency graph for crypto_mpi.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)
 
#define LOG_GCRY(level, cmd, rc)   do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0)
 Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the message given by gcry_strerror(rc). More...
 

Functions

static void adjust (void *buf, size_t size, size_t target)
 If target != size, move target bytes to the end of the size-sized buffer and zero out the first target - size bytes. More...
 
void GNUNET_CRYPTO_mpi_print_unsigned (void *buf, size_t size, gcry_mpi_t val)
 Output the given MPI value to the given buffer in network byte order. More...
 
void GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size)
 Convert data buffer into MPI value. More...
 

Detailed Description

Helper functions for libgcrypt MPIs.

Author
Christian Grothoff
Florian Dold

Definition in file crypto_mpi.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)

Definition at line 32 of file crypto_mpi.c.

◆ LOG_GCRY

#define LOG_GCRY (   level,
  cmd,
  rc 
)    do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0)

Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the message given by gcry_strerror(rc).

Definition at line 39 of file crypto_mpi.c.

Referenced by GNUNET_CRYPTO_mpi_print_unsigned(), and GNUNET_CRYPTO_mpi_scan_unsigned().

Function Documentation

◆ adjust()

static void adjust ( void *  buf,
size_t  size,
size_t  target 
)
static

If target != size, move target bytes to the end of the size-sized buffer and zero out the first target - size bytes.

Parameters
buforiginal buffer
sizenumber of bytes in buf
targettarget size of the buffer

Definition at line 51 of file crypto_mpi.c.

References buf, and p.

Referenced by GNUNET_CRYPTO_mpi_print_unsigned().

54 {
55  char *p = buf;
56 
57  if (size < target)
58  {
59  memmove (&p[target - size], buf, size);
60  memset (buf, 0, target - size);
61  }
62 }
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_mpi_print_unsigned()

void GNUNET_CRYPTO_mpi_print_unsigned ( void *  buf,
size_t  size,
gcry_mpi_t  val 
)

Output the given MPI value to the given buffer in network byte order.

The MPI val may not be negative.

Parameters
bufwhere to output to
sizenumber of bytes in buf
valvalue to write to buf

Definition at line 75 of file crypto_mpi.c.

References adjust(), GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_memcpy, LOG_GCRY, p, and size.

Referenced by decrypt_conclude(), encrypt_fair(), extract_pk(), GNUNET_CRYPTO_ecc_point_to_bin(), GNUNET_CRYPTO_ecdhe_key_create2(), GNUNET_CRYPTO_ecdhe_key_get_public(), GNUNET_CRYPTO_ecdsa_key_create(), GNUNET_CRYPTO_ecdsa_key_get_anonymous(), GNUNET_CRYPTO_ecdsa_key_get_public(), GNUNET_CRYPTO_ecdsa_private_key_derive(), GNUNET_CRYPTO_ecdsa_public_key_derive(), GNUNET_CRYPTO_ecdsa_sign(), GNUNET_CRYPTO_eddsa_key_create(), GNUNET_CRYPTO_eddsa_key_get_public(), GNUNET_CRYPTO_eddsa_sign(), GNUNET_CRYPTO_paillier_create(), GNUNET_CRYPTO_paillier_encrypt(), GNUNET_CRYPTO_paillier_encrypt1(), GNUNET_CRYPTO_paillier_hom_add(), GNUNET_SECRETSHARING_encrypt(), GNUNET_SECRETSHARING_plaintext_generate_i(), insert_decrypt_element(), insert_round1_element(), insert_round2_element(), and keygen_round2_conclude().

78 {
79  size_t rsize;
80  int rc;
81 
82  if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
83  {
84  /* Store opaque MPIs left aligned into the buffer. */
85  unsigned int nbits;
86  const void *p;
87 
88  p = gcry_mpi_get_opaque (val, &nbits);
89  GNUNET_assert (p);
90  rsize = (nbits+7)/8;
91  if (rsize > size)
92  rsize = size;
93  GNUNET_memcpy (buf, p, rsize);
94  if (rsize < size)
95  memset (buf+rsize, 0, size - rsize);
96  }
97  else
98  {
99  /* Store regular MPIs as unsigned integers right aligned into
100  the buffer. */
101  rsize = size;
102  if (0 !=
103  (rc = gcry_mpi_print (GCRYMPI_FMT_USG,
104  buf,
105  rsize, &rsize,
106  val)))
107  {
109  "gcry_mpi_print",
110  rc);
111  GNUNET_assert (0);
112  }
113  adjust (buf, rsize, size);
114  }
115 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
Definition: crypto_mpi.c:39
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
static void adjust(void *buf, size_t size, size_t target)
If target != size, move target bytes to the end of the size-sized buffer and zero out the first targe...
Definition: crypto_mpi.c:51
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_mpi_scan_unsigned()

void GNUNET_CRYPTO_mpi_scan_unsigned ( gcry_mpi_t *  result,
const void *  data,
size_t  size 
)

Convert data buffer into MPI value.

The buffer is interpreted as network byte order, unsigned integer.

Parameters
resultwhere to store MPI value (allocated)
dataraw data (GCRYMPI_FMT_USG)
sizenumber of bytes in data

Definition at line 128 of file crypto_mpi.c.

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, and LOG_GCRY.

Referenced by decrypt_conclude(), decrypt_new_element(), derive_h(), eddsa_d_to_a(), encrypt_fair(), get_fair_encryption_challenge(), GNUNET_CRYPTO_ecc_ecdh(), GNUNET_CRYPTO_ecdh_eddsa(), GNUNET_CRYPTO_ecdsa_ecdh(), GNUNET_CRYPTO_ecdsa_private_key_derive(), GNUNET_CRYPTO_eddsa_ecdh(), GNUNET_CRYPTO_paillier_decrypt(), GNUNET_CRYPTO_paillier_encrypt(), GNUNET_CRYPTO_paillier_encrypt1(), GNUNET_CRYPTO_paillier_hom_add(), GNUNET_SECRETSHARING_encrypt(), insert_decrypt_element(), keygen_reveal_get_exp_coeff(), keygen_reveal_get_exp_preshare(), keygen_round1_new_element(), restore_fair(), and verify_fair().

131 {
132  int rc;
133 
134  if (0 != (rc = gcry_mpi_scan (result,
135  GCRYMPI_FMT_USG,
136  data, size, &size)))
137  {
139  "gcry_mpi_scan",
140  rc);
141  GNUNET_assert (0);
142  }
143 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
Definition: crypto_mpi.c:39
static int result
Global testing status.
static unsigned int size
Size of the "table".
Definition: peer.c:67
uint32_t data
The data value.
Here is the caller graph for this function: