libtc  20160415
Threshold Cryptography functions library
structs_init.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include "tc_internal.h"
8 
9 void * alloc(size_t size) {
10  void * b = malloc(size);
11  if (b == NULL) {
12  perror("alloc");
13  abort();
14  }
15  return b;
16 }
17 
18 bytes_t * tc_init_bytes(void * bs, size_t len) {
19  bytes_t * out = alloc(sizeof(bytes_t));
20  out->data = bs;
21  out->data_len = len;
22 
23  return out;
24 }
25 
26 bytes_t *tc_init_bytes_copy(void *bs, size_t len) {
27  bytes_t * out = alloc(sizeof(bytes_t));
28  out->data = memcpy(malloc(len), bs, len);
29  out->data_len = len;
30 
31  return out;
32 }
33 
34 static bytes_t * tc_init_bytes_array(size_t len) {
35  bytes_t * bytes_array = alloc(len*sizeof(bytes_t));
36  return bytes_array;
37 }
38 
40  free(bytes->data);
41  free(bytes);
42 }
43 
44 void* tc_release_bytes(bytes_t *bytes, uint32_t *len) {
45  if(len != NULL) {
46  *len = bytes->data_len;
47  }
48  void *data = bytes->data;
49  free(bytes);
50 
51  return data;
52 }
53 
54 static void tc_clear_bytes_array(bytes_t * b, int count) {
55  for(int i=0; i<count; i++) {
56  free(b[i].data);
57  }
58  free(b);
59 }
60 
62  va_list ap;
63  va_start(ap, bytes);
64 
65  tc_clear_bytes(bytes);
66 
67  bytes_t * cur_arg;
68  while((cur_arg = va_arg(ap, bytes_t *)) != NULL) {
69  tc_clear_bytes(cur_arg);
70  }
71 
72  va_end(ap);
73 }
74 
76  public_key_t * pk = alloc(sizeof(public_key_t));
77 
78  pk->n = tc_init_bytes(NULL, 0);
79  pk->e = tc_init_bytes(NULL, 0);
80 
81  return pk;
82 }
83 
84 
86  tc_clear_bytes_n(pk->e, pk->n, NULL);
87  free(pk);
88 }
89 
90 key_metainfo_t *tc_init_key_metainfo(uint16_t k, uint16_t l) {
91  assert(0 < l);
92  assert(l/2 < k && k <= l);
93 
94  key_metainfo_t * metainfo = alloc(sizeof(key_metainfo_t));
95 
96  metainfo->k = k;
97  metainfo->l = l;
98 
99  metainfo->public_key = tc_init_public_key();
100  metainfo->vk_i = tc_init_bytes_array(l);
101  metainfo->vk_v = tc_init_bytes(NULL, 0);
102  metainfo->vk_u = tc_init_bytes(NULL, 0);
103 
104  assert(metainfo != NULL);
105  return metainfo;
106 }
107 
109  return i->k;
110 }
111 
113  return i->l;
114 }
115 
117  return i->public_key;
118 }
119 
121  return k->id;
122 }
123 
125  return pk->n;
126 }
127 
129  return pk->e;
130 }
131 
133  return s->id;
134 }
135 
137  assert(info != NULL);
139  tc_clear_bytes(info->vk_v);
140  tc_clear_bytes(info->vk_u);
141  tc_clear_bytes_array(info->vk_i, info->l);
142  free(info);
143 }
144 
146  key_share_t * ks = alloc(sizeof(key_share_t));
147 
148  ks->n = tc_init_bytes(NULL, 0);
149  ks->s_i = tc_init_bytes(NULL, 0);
150 
151  return ks;
152 }
153 
155  assert(info != NULL);
156  assert(info->l > 0);
157 
158  key_share_t ** ks = alloc(sizeof(key_share_t*)*info->l);
159  for(int i=0; i<info->l; i++) {
160  ks[i] = tc_init_key_share();
161  }
162 
163  assert(ks != NULL);
164  return ks;
165 }
166 
168  tc_clear_bytes_n(share->s_i, share->n, NULL);
169  free(share);
170 }
171 
173  assert(info != NULL && info->l > 0);
174  for(int i=0; i<info->l; i++) {
175  tc_clear_key_share(shares[i]);
176  }
177  free(shares);
178 }
179 
181  signature_share_t * ss = alloc(sizeof(signature_share_t));
182 
183  ss->z = tc_init_bytes(NULL, 0);
184  ss->c = tc_init_bytes(NULL, 0);
185  ss->x_i = tc_init_bytes(NULL, 0);
186 
187  assert(ss != NULL);
188  return ss;
189 }
190 
192  tc_clear_bytes_n(ss->x_i, ss->c, ss->z, NULL);
193  free(ss);
194 }
public_key_t * public_key
Definition: tc_internal.h:14
void tc_clear_bytes(bytes_t *bytes)
Definition: structs_init.c:39
void tc_clear_key_metainfo(key_metainfo_t *info)
Definition: structs_init.c:136
public_key_t * tc_init_public_key()
Definition: structs_init.c:75
int tc_key_meta_info_k(const key_metainfo_t *i)
Definition: structs_init.c:108
bytes_t * c
Definition: tc_internal.h:30
bytes_t * s_i
Definition: tc_internal.h:23
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
Structure that represents the data about a key share, including its public key.
Definition: tc_internal.h:13
void tc_clear_key_share(key_share_t *share)
Definition: structs_init.c:167
bytes_t * vk_i
Definition: tc_internal.h:19
void tc_clear_bytes_n(bytes_t *bytes,...)
Definition: structs_init.c:61
uint16_t id
Definition: tc_internal.h:25
bytes_t * z
Definition: tc_internal.h:31
const public_key_t * tc_key_meta_info_public_key(const key_metainfo_t *i)
Definition: structs_init.c:116
bytes_t * tc_init_bytes_copy(void *bs, size_t len)
Definition: structs_init.c:26
signature_share_t * tc_init_signature_share()
Definition: structs_init.c:180
void * data
Definition: tc.h:15
void tc_clear_public_key(public_key_t *pk)
Definition: structs_init.c:85
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
int tc_key_meta_info_l(const key_metainfo_t *i)
Definition: structs_init.c:112
bytes_t * vk_v
Definition: tc_internal.h:17
const bytes_t * tc_public_key_e(const public_key_t *pk)
Definition: structs_init.c:128
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
int tc_key_share_id(const key_share_t *k)
Definition: structs_init.c:120
void * alloc(size_t size)
Definition: structs_init.c:9
void * tc_release_bytes(bytes_t *bytes, uint32_t *len)
Definition: structs_init.c:44
int tc_signature_share_id(const signature_share_t *s)
Definition: structs_init.c:132
bytes_t * vk_u
Definition: tc_internal.h:18
void tc_clear_key_shares(key_share_t **shares, key_metainfo_t *info)
Definition: structs_init.c:172
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
bytes_t * e
Definition: tc_internal.h:10
const bytes_t * tc_public_key_n(const public_key_t *pk)
Definition: structs_init.c:124
bytes_t * tc_init_bytes(void *bs, size_t len)
Definition: structs_init.c:18
key_share_t ** tc_init_key_shares(key_metainfo_t *info)
Definition: structs_init.c:154
void tc_clear_signature_share(signature_share_t *ss)
Definition: structs_init.c:191