aboutsummaryrefslogtreecommitdiff
path: root/src/contrib/service/secretsharing/secretsharing_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/service/secretsharing/secretsharing_common.c')
-rw-r--r--src/contrib/service/secretsharing/secretsharing_common.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/src/contrib/service/secretsharing/secretsharing_common.c b/src/contrib/service/secretsharing/secretsharing_common.c
new file mode 100644
index 000000000..44b96b1c8
--- /dev/null
+++ b/src/contrib/service/secretsharing/secretsharing_common.c
@@ -0,0 +1,159 @@
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
24/**
25 * Read a share from its binary representation.
26 *
27 * @param data Binary representation of the share.
28 * @param len Length of @a data.
29 * @param[out] readlen Number of bytes read,
30 * ignored if NULL.
31 * @return The share, or NULL on error.
32 */
33struct GNUNET_SECRETSHARING_Share *
34GNUNET_SECRETSHARING_share_read (const void *data,
35 size_t len,
36 size_t *readlen)
37{
38 struct GNUNET_SECRETSHARING_Share *share;
39 const struct GNUNET_SECRETSHARING_ShareHeaderNBO *sh = data;
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
46 GNUNET_SECRETSHARING_FieldElement)
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,
71 struct GNUNET_SECRETSHARING_FieldElement);
72 GNUNET_memcpy (share->sigmas, p, n);
73 p += n;
74
75 n = share->num_peers * sizeof(uint16_t);
76 share->original_indices = GNUNET_new_array (share->num_peers,
77 uint16_t);
78 GNUNET_memcpy (share->original_indices, p, n);
79
80 return share;
81}
82
83
84/**
85 * Convert a share to its binary representation.
86 * Can be called with a NULL @a buf to get the size of the share.
87 *
88 * @param share Share to write.
89 * @param buf Buffer to write to.
90 * @param buflen Number of writable bytes in @a buf.
91 * @param[out] writelen Pointer to store number of bytes written,
92 * ignored if NULL.
93 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
94 */
95int
96GNUNET_SECRETSHARING_share_write (const struct
97 GNUNET_SECRETSHARING_Share *share,
98 void *buf, size_t buflen, size_t *writelen)
99{
100 uint16_t payload_size;
101 struct GNUNET_SECRETSHARING_ShareHeaderNBO *sh;
102 char *p;
103 int n;
104
105 payload_size = share->num_peers
106 * (sizeof(uint16_t) + sizeof(struct
107 GNUNET_SECRETSHARING_FieldElement)
108 + sizeof(struct GNUNET_PeerIdentity));
109
110 if (NULL != writelen)
111 *writelen = payload_size + sizeof(struct
112 GNUNET_SECRETSHARING_ShareHeaderNBO);
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
120 GNUNET_SECRETSHARING_ShareHeaderNBO))
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
149void
150GNUNET_SECRETSHARING_share_destroy (struct GNUNET_SECRETSHARING_Share *share)
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}