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