GNUnet  0.19.2
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 
28 #include "platform.h"
29 #include <gcrypt.h>
30 #include "gnunet_util_lib.h"
31 
32 
33 #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)
34 
40 #define LOG_GCRY(level, cmd, rc) do { LOG (level, _ ( \
41  "`%s' failed at %s:%d with error: %s\n"), \
42  cmd, __FILE__, __LINE__, \
43  gcry_strerror (rc)); } while (0)
44 
45 
54 static void
55 adjust (void *buf,
56  size_t size,
57  size_t target)
58 {
59  char *p = buf;
60 
61  if (size < target)
62  {
63  memmove (&p[target - size], buf, size);
64  memset (buf, 0, target - size);
65  }
66 }
67 
68 
78 void
80  size_t size,
81  gcry_mpi_t val)
82 {
83  size_t rsize;
84  int rc;
85 
86  if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
87  {
88  /* Store opaque MPIs left aligned into the buffer. */
89  unsigned int nbits;
90  const void *p;
91 
92  p = gcry_mpi_get_opaque (val, &nbits);
93  GNUNET_assert (p);
94  rsize = (nbits + 7) / 8;
95  if (rsize > size)
96  rsize = size;
97  GNUNET_memcpy (buf, p, rsize);
98  if (rsize < size)
99  memset (buf + rsize, 0, size - rsize);
100  }
101  else
102  {
103  /* Store regular MPIs as unsigned integers right aligned into
104  the buffer. */
105  rsize = size;
106  if (0 !=
107  (rc = gcry_mpi_print (GCRYMPI_FMT_USG,
108  buf,
109  rsize, &rsize,
110  val)))
111  {
113  "gcry_mpi_print",
114  rc);
115  GNUNET_assert (0);
116  }
117  adjust (buf, rsize, size);
118  }
119 }
120 
121 
131 void
133  const void *data,
134  size_t size)
135 {
136  int rc;
137 
138  if (0 != (rc = gcry_mpi_scan (result,
139  GCRYMPI_FMT_USG,
140  data, size, &size)))
141  {
143  "gcry_mpi_scan",
144  rc);
145  GNUNET_assert (0);
146  }
147 }
148 
149 
159 void
161  const void *data,
162  size_t size)
163 {
164  int rc;
165 
166  if (0 != (rc = gcry_mpi_scan (result,
167  GCRYMPI_FMT_USG,
168  data, size, &size)))
169  {
171  "gcry_mpi_scan",
172  rc);
173  GNUNET_assert (0);
174  }
175 }
176 
177 
178 /* end of crypto_mpi.c */
void GNUNET_CRYPTO_mpi_scan_unsigned_le(gcry_mpi_t *result, const void *data, size_t size)
Convert little endian data buffer into MPI value.
Definition: crypto_mpi.c:160
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
Definition: crypto_mpi.c:40
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:55
uint32_t data
The data value.
static int result
Global testing status.
static char buf[2048]
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
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:132
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:79
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
static unsigned int size
Size of the "table".
Definition: peer.c:68