libtc  20160415
Threshold Cryptography functions library
structs_serialization.c
Go to the documentation of this file.
1 #define _POSIX_C_SOURCE 200809L
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <arpa/inet.h>
6 #include "tc_internal.h"
7 
8 static const uint16_t version = 1;
9 
10 #define SERIALIZE_VARIABLE(dst, x) \
11  do { memcpy((dst), &x, sizeof x); (dst) += sizeof x; } while(0)
12 #define SERIALIZE_BYTES(dst, bs) \
13  do { \
14  bytes_t *__b = (bs); \
15  uint32_t __net_len = htonl(__b->data_len); \
16  SERIALIZE_VARIABLE((dst), __net_len); \
17  memcpy((dst), __b->data, __b->data_len); \
18  (dst) += __b->data_len; \
19  } while(0)
20 
22  /*
23  * We are going to write each struct component directly in memory, and then write that as a base64 string.
24  */
25 
26  /* First we get the size of each component */
27  uint16_t net_version = htons(version);
28  uint16_t net_id = htons(ks->id);
29 
30  /* We prepare the buffer */
31  size_t buffer_size = sizeof(net_version) + sizeof(net_id) + sizeof(ks->n->data_len) +
32  sizeof(ks->s_i->data_len) + ks->n->data_len + ks->s_i->data_len;
33  uint8_t *buffer = malloc(buffer_size);
34 
35  /* Copy each field to the buffer */
36  uint8_t *p = buffer;
37 
38  SERIALIZE_VARIABLE(p, net_version);
39  SERIALIZE_VARIABLE(p, net_id);
40  SERIALIZE_BYTES(p, ks->n);
41  SERIALIZE_BYTES(p, ks->s_i);
42 
43  bytes_t bs = {buffer, (uint32_t) buffer_size};
44  char *b64 = tc_bytes_b64(&bs);
45 
46  free(buffer);
47 
48  return b64;
49 }
50 
52  uint16_t net_version = htons(version);
53  uint16_t net_id = htons(ss->id);
54 
55  size_t buffer_size = sizeof(net_version) + sizeof(net_id) + sizeof(ss->x_i->data_len) +
56  sizeof(ss->c->data_len) + sizeof(ss->z->data_len) + ss->x_i->data_len + ss->c->data_len +
57  ss->z->data_len;
58  uint8_t *buffer = malloc(buffer_size);
59 
60  uint8_t *p = buffer;
61  SERIALIZE_VARIABLE(p, net_version);
62  SERIALIZE_VARIABLE(p, net_id);
63  SERIALIZE_BYTES(p, ss->x_i);
64  SERIALIZE_BYTES(p, ss->c);
65  SERIALIZE_BYTES(p, ss->z);
66 
67  bytes_t bs = {buffer, (uint32_t) buffer_size};
68  char *b64 = tc_bytes_b64(&bs);
69  free(buffer);
70  return b64;
71 }
72 
73 static bytes_t *serialize_public_key(const public_key_t *pk) {
74  size_t buffer_size = sizeof(pk->n->data_len) + sizeof(pk->e->data_len) +
75  pk->n->data_len + pk->e->data_len;
76  uint8_t *buffer = malloc(buffer_size);
77  uint8_t *p = buffer;
78  SERIALIZE_BYTES(p, pk->n);
79  SERIALIZE_BYTES(p, pk->e);
80 
81  bytes_t *bs = tc_init_bytes(buffer, buffer_size);
82 
83  return bs;
84 }
85 
87  size_t buffer_size = 0;
88 
89  uint16_t net_version = htons(version);
90  buffer_size += sizeof net_version;
91 
92  bytes_t *pk = serialize_public_key(kmi->public_key);
93  buffer_size += sizeof pk->data_len;
94  buffer_size += pk->data_len;
95 
96  uint16_t net_k = htons(kmi->k);
97  buffer_size += sizeof net_k;
98 
99  uint16_t net_l = htons(kmi->l);
100  buffer_size += sizeof net_l;
101 
102  bytes_t *vk_v = kmi->vk_v;
103  bytes_t *vk_u = kmi->vk_u;
104 
105  buffer_size += sizeof vk_v->data_len;
106  buffer_size += vk_v->data_len;
107 
108  buffer_size += sizeof vk_u->data_len;
109  buffer_size += vk_u->data_len;
110 
111 
112  for (int i = 0; i < kmi->l; i++) {
113  buffer_size += sizeof(kmi->vk_i[i].data_len);
114  buffer_size += kmi->vk_i[i].data_len;
115  }
116 
117  uint8_t *buffer = malloc(buffer_size);
118  uint8_t *p = buffer;
119 
120  SERIALIZE_VARIABLE(p, net_version);
121  SERIALIZE_BYTES(p, pk);
122  SERIALIZE_VARIABLE(p, net_k);
123  SERIALIZE_VARIABLE(p, net_l);
124  SERIALIZE_BYTES(p, vk_v);
125  SERIALIZE_BYTES(p, vk_u);
126  for (int i = 0; i < kmi->l; i++) {
127  bytes_t *v = kmi->vk_i + i;
128  SERIALIZE_BYTES(p, v);
129  }
130 
131  bytes_t bs = {buffer, buffer_size};
132  char *b64 = tc_bytes_b64(&bs);
133  tc_clear_bytes(pk);
134  free(buffer);
135 
136  return b64;
137 }
138 
139 #define DESERIALIZE_SHORT(dst, buf) \
140  do { \
141  memcpy(&dst, buf, sizeof dst); \
142  dst = ntohs(dst); \
143  (buf) += sizeof dst; \
144  } while (0)
145 
146 #define DESERIALIZE_BYTES(dst, buf) \
147  do { \
148  bytes_t * __b = (dst); \
149  uint32_t len; \
150  memcpy(&len, (buf), sizeof(len)); \
151  len = ntohl(len); \
152  (buf) += sizeof(len); \
153  __b->data = alloc(len); \
154  __b->data_len = len; \
155  memcpy(__b->data, (buf), len); \
156  (buf) += len; \
157  } while(0)
158 
160  bytes_t *buffer = tc_b64_bytes(b64);
161  uint8_t *p = buffer->data;
162 
163  uint16_t message_version;
164  DESERIALIZE_SHORT(message_version, p);
165 
166  if (message_version != version) {
167  fprintf(stderr, "KeyShare, Version mismatch: (Message=%x) != (Library=%x)\n", message_version, version);
168  tc_clear_bytes(buffer);
169  return NULL;
170  }
171 
173 
174  DESERIALIZE_SHORT(ks->id, p);
175  DESERIALIZE_BYTES(ks->n, p);
176  DESERIALIZE_BYTES(ks->s_i, p);
177 
178  tc_clear_bytes(buffer);
179 
180  return ks;
181 }
182 
184  bytes_t *buffer = tc_b64_bytes(b64);
185  uint8_t *p = buffer->data;
186 
187 
188  uint16_t message_version;
189  DESERIALIZE_SHORT(message_version, p);
190 
191  if (message_version != version) {
192  fprintf(stderr, "SignatureShare, Version mismatch: (Message=%d) != (Library=%d)\n", message_version, version);
193  tc_clear_bytes(buffer);
194  return NULL;
195  }
196 
198  DESERIALIZE_SHORT(ss->id, p);
199  DESERIALIZE_BYTES(ss->x_i, p);
200  DESERIALIZE_BYTES(ss->c, p);
201  DESERIALIZE_BYTES(ss->z, p);
202 
203  tc_clear_bytes(buffer);
204 
205  return ss;
206 }
207 
209  bytes_t *buffer = tc_b64_bytes(b64);
210  uint8_t *p = buffer->data;
211 
212  uint16_t message_version;
213  DESERIALIZE_SHORT(message_version, p);
214 
215  if (message_version != version) {
216  fprintf(stderr, "KeyMetaInfo, Version mismatch: (Message=%d) != (Library=%d)\n", message_version, version);
217  tc_clear_bytes(buffer);
218  return NULL;
219  }
220 
221  bytes_t *pk = tc_init_bytes(NULL, 0);
222  DESERIALIZE_BYTES(pk, p);
223 
224  uint16_t k;
225  DESERIALIZE_SHORT(k, p);
226 
227  uint16_t l;
228  DESERIALIZE_SHORT(l, p);
229 
231  DESERIALIZE_BYTES(kmi->vk_v, p);
232  DESERIALIZE_BYTES(kmi->vk_u, p);
233  // We have to do this here, because init_key_meta_info initializes the vk_i array.
234  for (int i = 0; i < l; i++) {
235  bytes_t *v = kmi->vk_i + i;
236  DESERIALIZE_BYTES(v, p);
237  }
238  p = pk->data;
239  DESERIALIZE_BYTES(kmi->public_key->n, p);
240  DESERIALIZE_BYTES(kmi->public_key->e, p);
241 
242  tc_clear_bytes(buffer);
243  tc_clear_bytes(pk);
244  return kmi;
245 }
246 
247 
public_key_t * public_key
Definition: tc_internal.h:14
void tc_clear_bytes(bytes_t *bytes)
Definition: structs_init.c:39
char * tc_bytes_b64(const bytes_t *b)
bytes_t * c
Definition: tc_internal.h:30
bytes_t * s_i
Definition: tc_internal.h:23
#define DESERIALIZE_SHORT(dst, buf)
bytes_t * n
Definition: tc_internal.h:24
Structure that&#39;s stores a pointer that points to data_len bytes.
Definition: tc.h:14
Structure that represents a signature share, to be joined generating a standard RSA signatures...
Definition: tc_internal.h:28
bytes_t * n
Definition: tc_internal.h:9
key_share_t * tc_init_key_share()
Definition: structs_init.c:145
char * tc_serialize_signature_share(const signature_share_t *ss)
Structure that represents the data about a key share, including its public key.
Definition: tc_internal.h:13
bytes_t * vk_i
Definition: tc_internal.h:19
uint16_t id
Definition: tc_internal.h:25
bytes_t * z
Definition: tc_internal.h:31
signature_share_t * tc_init_signature_share()
Definition: structs_init.c:180
void * data
Definition: tc.h:15
signature_share_t * tc_deserialize_signature_share(const char *b64)
char * tc_serialize_key_metainfo(const key_metainfo_t *kmi)
uint32_t data_len
Definition: tc.h:16
Structure that represents a standard RSA public key. Used to verify signatures.
Definition: tc_internal.h:8
bytes_t * vk_v
Definition: tc_internal.h:17
#define DESERIALIZE_BYTES(dst, buf)
char * tc_serialize_key_share(const key_share_t *ks)
uint16_t k
Definition: tc_internal.h:15
uint16_t l
Definition: tc_internal.h:16
key_metainfo_t * tc_init_key_metainfo(uint16_t k, uint16_t l)
Definition: structs_init.c:90
bytes_t * vk_u
Definition: tc_internal.h:18
key_metainfo_t * tc_deserialize_key_metainfo(const char *b64)
key_share_t * tc_deserialize_key_share(const char *b64)
Structure that represents one key share, to be used to generate a signature share.
Definition: tc_internal.h:22
bytes_t * x_i
Definition: tc_internal.h:29
#define SERIALIZE_VARIABLE(dst, x)
bytes_t * e
Definition: tc_internal.h:10
#define SERIALIZE_BYTES(dst, bs)
bytes_t * tc_init_bytes(void *bs, size_t len)
Definition: structs_init.c:18
bytes_t * tc_b64_bytes(const char *b64)