libtc  20160415
Threshold Cryptography functions library
algorithms_verify_signature.c
Go to the documentation of this file.
1 #include <gmp.h>
2 #include <mhash.h>
3 
4 #include "tc.h"
5 #include "tc_internal.h"
6 
7 extern const unsigned int HASH_LEN; /* Defined somewhere :P */
8 
9 int tc_verify_signature(const signature_share_t * signature, const bytes_t * doc, const key_metainfo_t * info){
10  mpz_t x, xi, z, c, n, e, v, u, vk_i, delta, xtilde, xi2, neg_c, v_prime, xi_neg_2c, x_prime, aux;
11  mpz_inits(x, xi, z, c, n, e, v, u, vk_i, delta, xtilde, xi2, neg_c, v_prime, xi_neg_2c, x_prime, aux, NULL);
12 
13  TC_BYTES_TO_MPZ(x, doc);
14  TC_BYTES_TO_MPZ(xi, signature->x_i);
15  TC_BYTES_TO_MPZ(z, signature->z);
16  TC_BYTES_TO_MPZ(c, signature->c);
17  TC_BYTES_TO_MPZ(n, info->public_key->n);
18  TC_BYTES_TO_MPZ(e, info->public_key->e);
19  TC_BYTES_TO_MPZ(v, info->vk_v);
20  TC_BYTES_TO_MPZ(u, info->vk_u);
21 
22  int idx = TC_ID_TO_INDEX(signature->id);
23  TC_BYTES_TO_MPZ(vk_i, info->vk_i + idx);
24 
25  if(mpz_jacobi(x, n) == -1) {
26  mpz_t ue;
27  mpz_init(ue);
28  mpz_powm(ue, u, e, n);
29  mpz_mul(x, x, ue);
30  mpz_mod(x, x, n);
31  mpz_clear(ue);
32  }
33 
34  // v
35 
36  // u
37 
38  // x~ = x^4 % n
39  mpz_powm_ui(xtilde, x, 4ul, n);
40 
41  // v_i
42 
43  // xi_2 = xi^2 % n
44  mpz_powm_ui(xi2, xi, 2, n);
45 
46 
47  // v' = v^z * v_i^(-c)
48  mpz_neg(neg_c, c);
49  mpz_powm(v_prime, vk_i, neg_c, n);
50 
51  mpz_powm(aux, v, z, n);
52  mpz_mul(v_prime, v_prime, aux);
53  mpz_mod(v_prime, v_prime, n);
54 
55  // x' = x~^z * x_i^(-2c)
56 
57  mpz_mul_si(aux, neg_c, 2);
58  mpz_powm(xi_neg_2c, xi, aux, n);
59 
60  mpz_powm(aux, xtilde, z, n);
61  mpz_mul(x_prime, aux, xi_neg_2c);
62  mpz_mod(x_prime, x_prime, n);
63 
64  size_t v_len, u_len, xtilde_len, v_i_len, xi2_len, v_prime_len, x_prime_len;
65 
66  void * v_bytes = TC_TO_OCTETS(&v_len, v);
67  void * u_bytes = TC_TO_OCTETS(&u_len, u);
68  void * xtilde_bytes = TC_TO_OCTETS(&xtilde_len, xtilde);
69  void * v_i_bytes = TC_TO_OCTETS(&v_i_len, vk_i);
70  void * xi2_bytes = TC_TO_OCTETS(&xi2_len, xi2);
71  void * v_prime_bytes = TC_TO_OCTETS(&v_prime_len, v_prime);
72  void * x_prime_bytes = TC_TO_OCTETS(&x_prime_len, x_prime);
73 
74  // Initialization of the digest context
75 
76  unsigned char hash[HASH_LEN];
77  MHASH sha = mhash_init(MHASH_SHA256);
78 
79  mhash(sha, v_bytes, v_len);
80  mhash(sha, u_bytes, u_len);
81  mhash(sha, xtilde_bytes, xtilde_len);
82  mhash(sha, v_i_bytes, v_i_len);
83  mhash(sha, xi2_bytes, xi2_len);
84  mhash(sha, v_prime_bytes, v_prime_len);
85  mhash(sha, x_prime_bytes, x_prime_len);
86 
87  mhash_deinit(sha, hash);
88 
89  void (*freefunc) (void *, size_t);
90  mp_get_memory_functions (NULL, NULL, &freefunc);
91 
92  freefunc(v_bytes, v_len);
93  freefunc(u_bytes, u_len);
94  freefunc(xtilde_bytes, xtilde_len);
95  freefunc(v_i_bytes, v_i_len);
96  freefunc(xi2_bytes, xi2_len);
97  freefunc(v_prime_bytes, v_prime_len);
98  freefunc(x_prime_bytes, x_prime_len);
99 
100  mpz_t h;
101  mpz_init(h);
102  TC_GET_OCTETS(h, HASH_LEN, hash);
103  mpz_mod(h, h, n);
104  int result = mpz_cmp(h, c);
105  mpz_clear(h);
106 
107  mpz_clears(x, xi, z, c, n, e, v, u, vk_i, delta, xtilde, xi2, neg_c, v_prime, xi_neg_2c, x_prime, aux, NULL);
108 
109  return result == 0;
110 }
#define TC_ID_TO_INDEX(id)
Definition: tc_internal.h:38
public_key_t * public_key
Definition: tc_internal.h:14
bytes_t * c
Definition: tc_internal.h:30
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
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
bytes_t * z
Definition: tc_internal.h:31
#define TC_GET_OCTETS(z, bcount, op)
Definition: tc_internal.h:36
bytes_t * vk_v
Definition: tc_internal.h:17
const unsigned int HASH_LEN
#define TC_BYTES_TO_MPZ(z, bytes)
Definition: tc_internal.h:42
int tc_verify_signature(const signature_share_t *signature, const bytes_t *doc, const key_metainfo_t *info)
bytes_t * vk_u
Definition: tc_internal.h:18
#define TC_TO_OCTETS(count, op)
Definition: tc_internal.h:37
bytes_t * x_i
Definition: tc_internal.h:29
bytes_t * e
Definition: tc_internal.h:10