GNUnet  0.11.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
46  + sizeof(struct GNUNET_PeerIdentity));
47 
48  if (NULL != readlen)
49  *readlen = payload_size + sizeof *sh;
50 
51  share = GNUNET_new (struct GNUNET_SECRETSHARING_Share);
52 
53  share->threshold = ntohs (sh->threshold);
54  share->num_peers = ntohs (sh->num_peers);
55  share->my_peer = ntohs (sh->my_peer);
56 
57  share->my_share = sh->my_share;
58  share->public_key = sh->public_key;
59 
60  p = (const char *) &sh[1];
61 
62  n = share->num_peers * sizeof(struct GNUNET_PeerIdentity);
63  share->peers = GNUNET_new_array (share->num_peers,
64  struct GNUNET_PeerIdentity);
65  GNUNET_memcpy (share->peers, p, n);
66  p += n;
67 
68  n = share->num_peers * sizeof(struct GNUNET_SECRETSHARING_FieldElement);
69  share->sigmas = GNUNET_new_array (share->num_peers,
71  GNUNET_memcpy (share->sigmas, p, n);
72  p += n;
73 
74  n = share->num_peers * sizeof(uint16_t);
76  uint16_t);
77  GNUNET_memcpy (share->original_indices, p, n);
78 
79  return share;
80 }
81 
82 
94 int
97  void *buf, size_t buflen, size_t *writelen)
98 {
99  uint16_t payload_size;
101  char *p;
102  int n;
103 
104  payload_size = share->num_peers
105  * (sizeof(uint16_t) + sizeof(struct
107  + sizeof(struct GNUNET_PeerIdentity));
108 
109  if (NULL != writelen)
110  *writelen = payload_size + sizeof(struct
112 
113  /* just a query for the writelen */
114  if (buf == NULL)
115  return GNUNET_OK;
116 
117  /* wrong buffer size */
118  if (buflen < payload_size + sizeof(struct
120  return GNUNET_SYSERR;
121 
122  sh = buf;
123 
124  sh->threshold = htons (share->threshold);
125  sh->num_peers = htons (share->num_peers);
126  sh->my_peer = htons (share->my_peer);
127 
128  sh->my_share = share->my_share;
129  sh->public_key = share->public_key;
130 
131  p = (void *) &sh[1];
132 
133  n = share->num_peers * sizeof(struct GNUNET_PeerIdentity);
134  GNUNET_memcpy (p, share->peers, n);
135  p += n;
136 
137  n = share->num_peers * sizeof(struct GNUNET_SECRETSHARING_FieldElement);
138  GNUNET_memcpy (p, share->sigmas, n);
139  p += n;
140 
141  n = share->num_peers * sizeof(uint16_t);
142  GNUNET_memcpy (p, share->original_indices, n);
143 
144  return GNUNET_OK;
145 }
146 
147 
148 void
150 {
151  GNUNET_free (share->original_indices);
152  share->original_indices = NULL;
153  GNUNET_free (share->sigmas);
154  share->sigmas = NULL;
155  GNUNET_free (share->peers);
156  share->peers = NULL;
157  GNUNET_free (share);
158 }
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.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
A share, with all values in in host byte order.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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.
Definition: secretsharing.h:99
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:94
#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...