libtc  20160415
Threshold Cryptography functions library
main.c
Go to the documentation of this file.
1 #define _GNU_SOURCE
2 
3 #include "tc.h"
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <assert.h>
10 
11 static const char * _message = "Hello world";
12 static char * message;
13 static int key_size = 1024;
14 static int k=3;
15 static int l=5;
16 
17 void set_parameters(int argc, char ** argv)
18 {
19 
20  message = strdup(_message);
21 #ifndef __CPROVER__
22  int opt;
23  while((opt = getopt(argc, argv, "m:k:l:s:")) != -1){
24  switch(opt) {
25  case 'm':
26  free(message);
27  message = strdup(optarg);
28  break;
29  case 'k':
30  k = strtol(optarg, NULL, 10);
31  break;
32  case 'l':
33  l = strtol(optarg, NULL, 10);
34  break;
35  case 's':
36  key_size = strtol(optarg, NULL, 10);
37  break;
38  }
39  }
40 #endif
41 }
42 
43 
44 int main(int argc, char ** argv)
45 {
46  set_parameters(argc, argv);
47  char * b64;
48 
49  key_metainfo_t * info;
50  key_share_t ** shares = tc_generate_keys(&info, 512, k, l, NULL);
51 
52  bytes_t * doc = tc_init_bytes( message, strlen(message));
53  bytes_t * doc_pkcs1 = tc_prepare_document(doc, TC_SHA256, info);
54 
55  b64 = tc_bytes_b64(doc_pkcs1);
56  printf("Document: %s\n", b64);
57  free(b64);
58 
59  signature_share_t * signatures[l];
60 
61  for (int i=0; i<l; i++) {
62  signatures[i] = tc_node_sign(shares[i], doc_pkcs1, info);
63  int verify = tc_verify_signature(signatures[i], doc_pkcs1, info);
64  assert(verify);
65  }
66 
67  bytes_t * signature = tc_join_signatures((void*) signatures, doc_pkcs1, info);
68  int verify = tc_rsa_verify(signature, doc, info, TC_SHA256);
69  assert(verify);
70 
71  b64 = tc_bytes_b64(signature);
72  printf("Signature: %s\n", b64);
73  free(b64);
74 
75  tc_clear_bytes_n(doc, doc_pkcs1, NULL);
76  tc_clear_bytes(signature);
77  for(int i=0; i<l; i++) {
78  tc_clear_signature_share(signatures[i]);
79  }
80  tc_clear_key_shares(shares, info);
82 }
bytes_t * tc_prepare_document(const bytes_t *doc, tc_hash_type_t hash_type, const key_metainfo_t *metainfo)
void set_parameters(int argc, char **argv)
Definition: main.c:17
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
char * tc_bytes_b64(const bytes_t *b)
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
signature_share_t * tc_node_sign(const key_share_t *share, const bytes_t *doc, const key_metainfo_t *info)
Structure that represents the data about a key share, including its public key.
Definition: tc_internal.h:13
void tc_clear_bytes_n(bytes_t *bytes,...)
Definition: structs_init.c:61
Definition: tc.h:48
int tc_verify_signature(const signature_share_t *signature, const bytes_t *doc, const key_metainfo_t *info)
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
key_share_t ** tc_generate_keys(key_metainfo_t **out, size_t bit_size, uint16_t k, uint16_t l, bytes_t *public_e)
int tc_rsa_verify(bytes_t *signature, bytes_t *doc, key_metainfo_t *info, tc_hash_type_t hashtype)
bytes_t * tc_join_signatures(const signature_share_t **signatures, const bytes_t *document, const key_metainfo_t *info)
int main(int argc, char **argv)
Definition: main.c:44
bytes_t * tc_init_bytes(void *bs, size_t len)
Definition: structs_init.c:18
void tc_clear_signature_share(signature_share_t *ss)
Definition: structs_init.c:191