GNUnet  0.10.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, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0)
40 
41 
50 static void
51 adjust (void *buf,
52  size_t size,
53  size_t target)
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 }
63 
64 
74 void
76  size_t size,
77  gcry_mpi_t val)
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 }
116 
117 
127 void
129  const void *data,
130  size_t size)
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 }
144 
145 /* end of crypto_mpi.c */
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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:128
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:75
cryptographic primitives for GNUnet
#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 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:51
uint32_t data
The data value.