-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpeks.c
246 lines (204 loc) · 5.37 KB
/
peks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* peks.c
*
* Created on: Jul 7, 2015
* Author: Atul Mahind
*/
#include "peks.h"
void sha512(const char *word, int word_size,
char hashed_word[SHA512_DIGEST_LENGTH*2+1])
{
int i;
unsigned char digest[SHA512_DIGEST_LENGTH];
SHA512_CTX ctx;
SHA512_Init(&ctx);
SHA512_Update(&ctx, word, word_size);
SHA512_Final(digest, &ctx);
for (i = 0; i < SHA512_DIGEST_LENGTH; i++)
sprintf(&hashed_word[i*2], "%02x", (unsigned int)digest[i]);
}
void get_n_bits(char* src, char* out, int bitswanted)
{
char * ptr = out;
char byte;
int i;
while(bitswanted)
{
byte = *src++;
for(i = 7; i >= 0 && bitswanted > 0 ; i--, bitswanted--)
*ptr++ = '0' + ((byte >> i) & 0x01);
if(bitswanted == 0)
break;
}
}
void key_printf(key key)
{
element_printf("α %B\n", key.priv);
element_printf("g %B\n", key.pub.g);
element_printf("h %B\n", key.pub.h);
}
void peks_printf(peks peks)
{
element_printf("A %B\n", peks.A);
printf("B %s\n", peks.B);
}
void init_pbc_param_pairing(pbc_param_t param, pairing_t pairing)
{
int rbits = 160;
int qbits = 512;
/* Generate type A pairing parameters */
pbc_param_init_a_gen(param, rbits, qbits);
/* Initialize pairing */
pairing_init_pbc_param(pairing, param);
#if defined(DEBUG)
printf("Symmetric %d\n", pairing_is_symmetric(pairing));
#endif
}
void KeyGen(key *key, pbc_param_t param, pairing_t pairing)
{
/* Private key - α */
element_init_Zr(key->priv, pairing);
element_random(key->priv);
/* Public key - Apub = [g, h=g^α] */
element_init_G1(key->pub.g, pairing);
element_random(key->pub.g);
element_init_G1(key->pub.h, pairing);
element_pow_zn(key->pub.h, key->pub.g, key->priv);
}
void PEKS(peks *peks, key_pub *pub, pairing_t pairing,
element_t H1_W, int bitswanted)
{
char *H2_t = peks->B;
element_t r, hR, t;
/* hR = h^r */
element_init_Zr(r ,pairing);
element_random(r);
element_init_G1(hR, pairing);
element_pow_zn(hR, pub->h, r);
/* t = hasedW1 X hR */
element_init_GT(t, pairing);
pairing_apply(t, H1_W, hR, pairing);
/* gR = g^r */
element_init_G1(peks->A, pairing);
element_pow_zn(peks->A, pub->g, r);
/* H2(t) */
char *char_t = malloc(sizeof(char)*element_length_in_bytes(t));
char *buffer = malloc(sizeof(char)*SHA512_DIGEST_LENGTH*2+1);
element_snprint(char_t, element_length_in_bytes(t), t);
sha512(char_t, element_length_in_bytes(t), buffer);
get_n_bits(buffer, H2_t, bitswanted);
free(char_t); char_t = NULL;
free(buffer); buffer = NULL;
}
void Trapdoor(element_t Tw, pairing_t pairing, element_t alpha,
element_t H1_W)
{
/* H1(W)^α = hashedW1^alpha */
element_init_G1(Tw, pairing);
element_pow_zn(Tw, H1_W, alpha);
}
int Test(char *W2, int lenW2, key_pub *pub, element_t Tw, pairing_t pairing)
{
/* PEKS for W2S */
/* PEKS = [A, B] i.e. A=g^r and B=H2(t) */
peks peks;
element_t H1_W2;
element_t temp;
double P = mpz_get_d(pairing->r);
#if defined(DEBUG)
printf("P %lf\n", P);
#endif
int nlogP = log2(P);
#if defined(DEBUG)
printf("log2(P) %d\n", nlogP);
#endif
/* H1(W2S) */
char *hashedW2 = malloc(sizeof(char)*SHA512_DIGEST_LENGTH*2+1);
sha512(W2, lenW2, hashedW2);
element_init_G1(H1_W2, pairing);
element_from_hash(H1_W2, hashedW2, strlen(hashedW2));
#if defined(DEBUG)
element_printf("H1_W2 %B\n", H1_W2);
#endif
/* PEKS(key_pub, W2) */
peks.B = malloc(sizeof(char)*(nlogP));
PEKS(&peks, pub, pairing, H1_W2, nlogP);
#if defined(DEBUG)
element_printf("A %B\n", peks.A);
#endif
element_init_GT(temp, pairing);
pairing_apply(temp, Tw, peks.A, pairing);
/* H2(temp) */
char *char_temp = malloc(sizeof(char)*element_length_in_bytes(temp));
char *hashed_temp = malloc(sizeof(char)*SHA512_DIGEST_LENGTH*2+1);
element_snprint(char_temp, element_length_in_bytes(temp), temp);
sha512(char_temp, element_length_in_bytes(temp), hashed_temp);
char *H2_lhs = malloc(sizeof(char)*(nlogP));
get_n_bits(hashed_temp, H2_lhs, nlogP);
/* Check for equality */
#if defined(DEBUG)
int i;
printf("lhs ");
for(i = 0; i < nlogP; i++)
printf("%c", H2_lhs[i]);
printf("\nrhs ");
for(i = 0; i < nlogP; i++)
printf("%c", peks.B[i]);
printf("\n");
#endif
int match;
if(!memcmp(H2_lhs, peks.B, nlogP))
match = 1;
else
match = 0;
/* Free the memory */
free(H2_lhs); H2_lhs = NULL;
free(peks.B); peks.B = NULL;
free(char_temp); char_temp = NULL;
free(hashed_temp); hashed_temp = NULL;
free(hashedW2); hashedW2 = NULL;
return match;
}
int peks_scheme(char* W1, char *W2)
{
/* Order of group G1 and G2 */
double P;
/* Apriv = α and Apub = [g, h=g^α] */
key key;
/* Trapdoor */
element_t Tw;
/* H1(W) */
element_t H1_W1;
/* PBC data types */
pbc_param_t param;
pairing_t pairing;
/* Initialize pairing */
init_pbc_param_pairing(param, pairing);
/* Get the order of G1 */
P = mpz_get_d(pairing->r);
#if defined(DEBUG)
printf("P %lf\n", P);
#endif
//int nlogP = log2(P);
/* KeyGen */
KeyGen(&key, param, pairing);
#if defined(DEBUG)
element_printf("α %e\n", key.priv);
#endif
/* H1(W) */
char *hashedW = malloc(sizeof(char)*SHA512_DIGEST_LENGTH*2+1);
sha512(W1, (int)strlen(W1), hashedW);
element_init_G1(H1_W1, pairing);
element_from_hash(H1_W1, hashedW, (int)strlen(hashedW));
#if defined(DEBUG)
element_printf("H1_W1 %B\n", H1_W1);
#endif
/* Trapdoor */
Trapdoor(Tw, pairing, key.priv, H1_W1);
int match;
match = Test(W2, (int)strlen(W2), &key.pub, Tw, pairing);
free(hashedW); hashedW = NULL;
//free(peks.B); peks.B = NULL;
pbc_param_clear(param);
return match;
}