GNUnet  0.11.x
crypto_mpi.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012, 2013 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include <gcrypt.h>
29 #include "gnunet_crypto_lib.h"
30 
31 
32 #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)
33 
39 #define LOG_GCRY(level, cmd, rc) do { LOG (level, _ ( \
40  "`%s' failed at %s:%d with error: %s\n"), \
41  cmd, __FILE__, __LINE__, \
42  gcry_strerror (rc)); } while (0)
43 
44 
53 static void
54 adjust (void *buf,
55  size_t size,
56  size_t target)
57 {
58  char *p = buf;
59 
60  if (size < target)
61  {
62  memmove (&p[target - size], buf, size);
63  memset (buf, 0, target - size);
64  }
65 }
66 
67 
77 void
79  size_t size,
80  gcry_mpi_t val)
81 {
82  size_t rsize;
83  int rc;
84 
85  if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
86  {
87  /* Store opaque MPIs left aligned into the buffer. */
88  unsigned int nbits;
89  const void *p;
90 
91  p = gcry_mpi_get_opaque (val, &nbits);
92  GNUNET_assert (p);
93  rsize = (nbits + 7) / 8;
94  if (rsize > size)
95  rsize = size;
96  GNUNET_memcpy (buf, p, rsize);
97  if (rsize < size)
98  memset (buf + rsize, 0, size - rsize);
99  }
100  else
101  {
102  /* Store regular MPIs as unsigned integers right aligned into
103  the buffer. */
104  rsize = size;
105  if (0 !=
106  (rc = gcry_mpi_print (GCRYMPI_FMT_USG,
107  buf,
108  rsize, &rsize,
109  val)))
110  {
112  "gcry_mpi_print",
113  rc);
114  GNUNET_assert (0);
115  }
116  adjust (buf, rsize, size);
117  }
118 }
119 
120 
130 void
132  const void *data,
133  size_t size)
134 {
135  int rc;
136 
137  if (0 != (rc = gcry_mpi_scan (result,
138  GCRYMPI_FMT_USG,
139  data, size, &size)))
140  {
142  "gcry_mpi_scan",
143  rc);
144  GNUNET_assert (0);
145  }
146 }
147 
148 
149 /* end of crypto_mpi.c */
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_CRYPTO_mpi_scan_unsigned(gcry_mpi_t *result, const void *data, size_t size)
Convert data buffer into MPI value.
Definition: crypto_mpi.c:131
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.
Definition: crypto_mpi.c:78
cryptographic primitives for GNUnet
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 int result
Global testing status.
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:54
uint32_t data
The data value.