GNUnet  0.20.0
secretsharing_common.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2014 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 
21 #include "platform.h"
22 #include "secretsharing.h"
23 
35  size_t len,
36  size_t *readlen)
37 {
38  struct GNUNET_SECRETSHARING_Share *share;
40  const char *p;
41  size_t n;
42  uint16_t payload_size;
43 
44  payload_size = ntohs (sh->num_peers)
45  * (sizeof(uint16_t) + sizeof(struct
47  + sizeof(struct GNUNET_PeerIdentity));
48 
49  if (NULL != readlen)
50  *readlen = payload_size + sizeof *sh;
51 
52  share = GNUNET_new (struct GNUNET_SECRETSHARING_Share);
53 
54  share->threshold = ntohs (sh->threshold);
55  share->num_peers = ntohs (sh->num_peers);
56  share->my_peer = ntohs (sh->my_peer);
57 
58  share->my_share = sh->my_share;
59  share->public_key = sh->public_key;
60 
61  p = (const char *) &sh[1];
62 
63  n = share->num_peers * sizeof(struct GNUNET_PeerIdentity);
64  share->peers = GNUNET_new_array (share->num_peers,
65  struct GNUNET_PeerIdentity);
66  GNUNET_memcpy (share->peers, p, n);
67  p += n;
68 
69  n = share->num_peers * sizeof(struct GNUNET_SECRETSHARING_FieldElement);
70  share->sigmas = GNUNET_new_array (share->num_peers,
72  GNUNET_memcpy (share->sigmas, p, n);
73  p += n;
74 
75  n = share->num_peers * sizeof(uint16_t);
77  uint16_t);
78  GNUNET_memcpy (share->original_indices, p, n);
79 
80  return share;
81 }
82 
83 
95 int
98  void *buf, size_t buflen, size_t *writelen)
99 {
100  uint16_t payload_size;
102  char *p;
103  int n;
104 
105  payload_size = share->num_peers
106  * (sizeof(uint16_t) + sizeof(struct
108  + sizeof(struct GNUNET_PeerIdentity));
109 
110  if (NULL != writelen)
111  *writelen = payload_size + sizeof(struct
113 
114  /* just a query for the writelen */
115  if (buf == NULL)
116  return GNUNET_OK;
117 
118  /* wrong buffer size */
119  if (buflen < payload_size + sizeof(struct
121  return GNUNET_SYSERR;
122 
123  sh = buf;
124 
125  sh->threshold = htons (share->threshold);
126  sh->num_peers = htons (share->num_peers);
127  sh->my_peer = htons (share->my_peer);
128 
129  sh->my_share = share->my_share;
130  sh->public_key = share->public_key;
131 
132  p = (void *) &sh[1];
133 
134  n = share->num_peers * sizeof(struct GNUNET_PeerIdentity);
135  GNUNET_memcpy (p, share->peers, n);
136  p += n;
137 
138  n = share->num_peers * sizeof(struct GNUNET_SECRETSHARING_FieldElement);
139  GNUNET_memcpy (p, share->sigmas, n);
140  p += n;
141 
142  n = share->num_peers * sizeof(uint16_t);
143  GNUNET_memcpy (p, share->original_indices, n);
144 
145  return GNUNET_OK;
146 }
147 
148 
149 void
151 {
152  GNUNET_free (share->original_indices);
153  share->original_indices = NULL;
154  GNUNET_free (share->sigmas);
155  share->sigmas = NULL;
156  GNUNET_free (share->peers);
157  share->peers = NULL;
158  GNUNET_free (share);
159 }
static struct SolverHandle * sh
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static char buf[2048]
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_SECRETSHARING_share_destroy(struct GNUNET_SECRETSHARING_Share *share)
struct GNUNET_SECRETSHARING_Share * GNUNET_SECRETSHARING_share_read(const void *data, size_t len, size_t *readlen)
Read a share from its binary representation.
int GNUNET_SECRETSHARING_share_write(const struct GNUNET_SECRETSHARING_Share *share, void *buf, size_t buflen, size_t *writelen)
Convert a share to its binary representation.
messages used for the secretsharing api
The identity of the host (wraps the signing key of the peer).
A share, with all values in in host byte order.
uint16_t num_peers
Peers that have the share.
uint16_t threshold
Threshold for the key this share belongs to.
struct GNUNET_SECRETSHARING_FieldElement my_share
Share of 'my_peer'.
struct GNUNET_SECRETSHARING_PublicKey public_key
Public key.
struct GNUNET_PeerIdentity * peers
Peer identities (includes 'my_peer')
uint16_t my_peer
Index of our peer in the list.
struct GNUNET_SECRETSHARING_FieldElement * sigmas