-
Notifications
You must be signed in to change notification settings - Fork 0
/
daps_id2_gq.c
573 lines (540 loc) · 17 KB
/
daps_id2_gq.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/********************************************************************************************
* DAPS: double-authentication preventing signatures
*
* Based on the paper:
* Mihir Bellare, Bertram Poettering, and Douglas Stebila.
* Deterring Certificate Subversion: Efficient Double-Authentication-Preventing Signatures.
* IACR Cryptology ePrint Archive, Report 2016/1016. October, 2016.
* https://eprint.iacr.org/2016/1016
*
* Software originally developed by Douglas Stebila.
*
* Released into the public domain; see LICENSE.txt for details.
********************************************************************************************/
/** \file daps_id2_gq.c
* ID2[GQ] DAPS scheme.
*/
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>
#include "common.h"
#include "daps_id2_gq.h"
#include "bn_extra.h"
#define NUM_ROUNDS 20
void DAPS_ID2_GQ_VK_free(DAPS_ID2_GQ_VK *vk) {
if (vk == NULL) {
return;
}
ID_GQ_PK_free(vk->ipk);
OPENSSL_free(vk->TDK);
OPENSSL_free(vk);
}
void DAPS_ID2_GQ_SK_free(DAPS_ID2_GQ_SK *sk) {
if (sk == NULL) {
return;
}
ID_GQ_SK_free(sk->isk);
ID_GQ_TDK_free(sk->itdk);
OPENSSL_free(sk);
}
void DAPS_ID2_GQ_SIG_free(DAPS_ID2_GQ_SIG *sig) {
if (sig == NULL) {
return;
}
ID_GQ_CH_free(sig->ch1);
ID_GQ_RESP_free(sig->resp2);
OPENSSL_free(sig);
}
void DAPS_ID2_GQ_VK_print_fp(FILE *fp, const DAPS_ID2_GQ_VK *vk) {
if (vk == NULL) {
return;
}
ID_GQ_PK_print_fp(fp, vk->ipk);
}
void DAPS_ID2_GQ_SK_print_fp(FILE *fp, const DAPS_ID2_GQ_SK *sk) {
if (sk == NULL) {
return;
}
ID_GQ_SK_print_fp(fp, sk->isk);
ID_GQ_TDK_print_fp(fp, sk->itdk);
}
void DAPS_ID2_GQ_SIG_print_fp(FILE *fp, const DAPS_ID2_GQ_SIG *sig) {
if (sig == NULL) {
return;
}
ID_GQ_CH_print_fp(fp, sig->ch1);
ID_GQ_RESP_print_fp(fp, sig->resp2);
}
// x_len must be a multiple of 32 bytes = 256 bits
// out must have length x_len
static int perm_F(const unsigned char i, const unsigned char *x, const size_t x_len, unsigned char *out) {
int ret, ok;
EVP_MD_CTX *mdctx = NULL;
for (unsigned char j = 0; j < x_len / 32; j++) {
// compute H(i || j || x)
CHECK_NONNULL(mdctx = EVP_MD_CTX_create());
CHECK_IS_ONE(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL));
CHECK_IS_ONE(EVP_DigestUpdate(mdctx, &i, 1));
CHECK_IS_ONE(EVP_DigestUpdate(mdctx, &j, 1));
CHECK_IS_ONE(EVP_DigestUpdate(mdctx, x, x_len));
unsigned int digest_len;
CHECK_IS_ONE(EVP_DigestFinal_ex(mdctx, out + (j * 32), &digest_len));
if (digest_len != 32) {
goto err;
}
EVP_MD_CTX_destroy(mdctx);
mdctx = NULL;
}
ret = 1;
goto cleanup;
err:
ret = 0;
cleanup:
if (mdctx != NULL) {
EVP_MD_CTX_destroy(mdctx);
mdctx = NULL;
}
return ret;
}
// x_len must be a multiple of 32 bytes = 256 bits
// out must have length x_len
static int perm_E_forward(const unsigned char *x, const size_t x_len, unsigned char *out) {
int ret, ok;
unsigned char *x_im1 = NULL, *x_i = NULL, *x_ip1 = NULL;
CHECK_NONNULL(x_im1 = OPENSSL_malloc(x_len / 2));
CHECK_NONNULL(x_i = OPENSSL_malloc(x_len / 2));
CHECK_NONNULL(x_ip1 = OPENSSL_malloc(x_len / 2));
memcpy(x_im1, x, x_len / 2);
memcpy(x_i, x + x_len / 2, x_len / 2);
for (int i = 1; i <= NUM_ROUNDS; i++) {
CHECK_IS_ONE(perm_F(i, x_i, x_len / 2, x_ip1));
for (size_t j = 0; j < x_len / 2; j++) {
x_ip1[j] ^= x_im1[j];
}
memcpy(x_im1, x_i, x_len / 2);
if (i < NUM_ROUNDS) {
memcpy(x_i, x_ip1, x_len / 2);
}
}
memcpy(out, x_i, x_len / 2);
memcpy(out + x_len / 2, x_ip1, x_len / 2);
ret = 1;
goto cleanup;
err:
ret = 0;
cleanup:
OPENSSL_free(x_im1);
OPENSSL_free(x_i);
OPENSSL_free(x_ip1);
return ret;
}
// x_len must be a multiple of 32 bytes = 256 bits
// out must have length x_len
static int perm_E_reverse(const unsigned char *x, const size_t x_len, unsigned char *out) {
int ret, ok;
unsigned char *x_im1 = NULL, *x_i = NULL, *x_ip1 = NULL;
CHECK_NONNULL(x_im1 = OPENSSL_malloc(x_len / 2));
CHECK_NONNULL(x_i = OPENSSL_malloc(x_len / 2));
CHECK_NONNULL(x_ip1 = OPENSSL_malloc(x_len / 2));
memcpy(x_i, x, x_len / 2);
memcpy(x_ip1, x + x_len / 2, x_len / 2);
for (int i = NUM_ROUNDS; i >= 1; i--) {
CHECK_IS_ONE(perm_F(i, x_i, x_len / 2, x_im1));
for (size_t j = 0; j < x_len / 2; j++) {
x_im1[j] ^= x_ip1[j];
}
memcpy(x_ip1, x_i, x_len / 2);
if (i > 1) {
memcpy(x_i, x_im1, x_len / 2);
}
}
memcpy(out, x_im1, x_len / 2);
memcpy(out + x_len / 2, x_i, x_len / 2);
ret = 1;
goto cleanup;
err:
ret = 0;
cleanup:
OPENSSL_free(x_im1);
OPENSSL_free(x_i);
OPENSSL_free(x_ip1);
return ret;
}
static int perm_Z(BIGNUM *out, const BIGNUM *in, const BIGNUM *modulus, const int forward, BN_CTX *ctx) {
int ret, ok;
unsigned char *in_b = NULL, *out_b = NULL;
BIGNUM *tmp = NULL;
int m_len = BN_num_bytes(modulus);
if (m_len % 32 != 0) {
goto err;
}
if (BN_num_bytes(in) > m_len) {
goto err;
}
CHECK_NONNULL(in_b = OPENSSL_malloc(m_len));
CHECK_NONNULL(out_b = OPENSSL_malloc(m_len));
bzero(in_b, m_len);
bzero(out_b, m_len);
int in_len = BN_num_bytes(in);
ok = BN_bn2bin(in, in_b + (m_len - in_len));
if (ok > m_len) {
goto err;
}
if (forward) {
CHECK_IS_ONE(perm_E_forward(in_b, m_len, out_b));
} else {
CHECK_IS_ONE(perm_E_reverse(in_b, m_len, out_b));
}
CHECK_NONNULL(BN_bin2bn(out_b, m_len, out));
CHECK_NONNULL(tmp = BN_new());
CHECK_IS_ONE(BN_gcd(tmp, out, modulus, ctx));
if ((BN_cmp(out, modulus) >= 1) || (!BN_is_one(tmp))) {
OPENSSL_free(in_b);
OPENSSL_free(out_b);
BN_free(tmp);
return perm_Z(out, out, modulus, forward, ctx);
}
ret = 1;
goto cleanup;
err:
ret = 0;
cleanup:
OPENSSL_free(in_b);
OPENSSL_free(out_b);
BN_free(tmp);
return ret;
}
static int DAPS_ID2_GQ_perm(const DAPS_ID2_GQ_VK *vk, const BIGNUM *x, BIGNUM **y, BN_CTX *bn_ctx) {
int ret, ok;
CHECK_NONNULL(*y = BN_new());
CHECK_IS_ONE(perm_Z(*y, x, vk->ipk->n, 1, bn_ctx));
ret = 1;
goto cleanup;
err:
ret = 0;
BN_free(*y);
cleanup:
return ret;
}
static int DAPS_ID2_GQ_perm_inv(const DAPS_ID2_GQ_VK *vk, const BIGNUM *y, BIGNUM **x, BN_CTX *bn_ctx) {
int ret, ok;
CHECK_NONNULL(*x = BN_new());
CHECK_IS_ONE(perm_Z(*x, y, vk->ipk->n, 0, bn_ctx));
ret = 1;
goto cleanup;
err:
ret = 0;
BN_free(*x);
cleanup:
return ret;
}
// allocates *vk and *sk which must be later freed
// returns 1 on success and 0 on error
int DAPS_ID2_GQ_keygen(DAPS_ID2_GQ_VK **vk, DAPS_ID2_GQ_SK **sk, const int bits, const int chlen, BN_CTX *bn_ctx) {
int ret, ok;
DAPS_ID2_GQ_VK *rvk = NULL;
DAPS_ID2_GQ_SK *rsk = NULL;
unsigned char *x = NULL;
unsigned char *h = NULL;
CHECK_NONNULL(rvk = (DAPS_ID2_GQ_VK *) OPENSSL_malloc(sizeof(DAPS_ID2_GQ_VK)));
CHECK_NONNULL(rsk = (DAPS_ID2_GQ_SK *) OPENSSL_malloc(sizeof(DAPS_ID2_GQ_SK)));
CHECK_IS_ONE(ID_GQ_keygen(&(rvk->ipk), &(rsk->isk), &(rsk->itdk), bits, chlen, bn_ctx));
// put d into TDK
rvk->TDK_length = BN_num_bytes(rsk->itdk->d);
CHECK_NONNULL(rvk->TDK = (unsigned char *) OPENSSL_malloc(BN_num_bytes(rsk->itdk->d)));
CHECK_GT_ZERO(BN_bn2bin(rsk->itdk->d, rvk->TDK));
// convert sk to binary
CHECK_NONNULL(x = (unsigned char *) OPENSSL_malloc(BN_num_bytes(rsk->isk->x)));
CHECK_GT_ZERO(BN_bn2bin(rsk->isk->x, x));
// hash sk
CHECK_NONNULL(h = SHA256_arbitrary(x, BN_num_bytes(rsk->isk->x), rvk->TDK_length));
// XOR hash into TDK
for (int i = 0; i < rvk->TDK_length; i++) {
rvk->TDK[i] = rvk->TDK[i] ^ h[i];
}
*vk = rvk;
*sk = rsk;
ret = 1;
goto cleanup;
err:
ret = 0;
DAPS_ID2_GQ_VK_free(rvk);
DAPS_ID2_GQ_SK_free(rsk);
cleanup:
OPENSSL_free(x);
OPENSSL_free(h);
return ret;
}
int DAPS_ID2_GQ_sign(const DAPS_ID2_GQ_VK *vk, const DAPS_ID2_GQ_SK *sk, const int hashlen, const unsigned char *msg_subj, const int msg_subj_length, const unsigned char *msg_body, const int msg_body_length, DAPS_ID2_GQ_SIG **sig, BN_CTX *bn_ctx) {
int ret, ok;
DAPS_ID2_GQ_SIG *rsig = NULL;
unsigned char c[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256_ctx;
ID_GQ_CMT cmt1, cmt2;
ID_GQ_STATE *state1 = NULL, *state2 = NULL;
ID_GQ_CH *ch2 = NULL;
ID_GQ_RESP *resp1 = NULL;
CHECK_NONNULL(rsig = (DAPS_ID2_GQ_SIG *) OPENSSL_malloc(sizeof(DAPS_ID2_GQ_SIG)));
// Y1 <- H(msg_subj) mod n
CHECK_NONNULL(cmt1.Y = SHA256_mod(msg_subj, msg_subj_length, vk->ipk->n, bn_ctx));
// c1 <-$ {0,1}
CHECK_IS_ONE(ID_GQ_ch_rand(&(rsig->ch1), 1));
// y1 <- ID.CmtInv(Y1)
CHECK_IS_ONE(ID_GQ_cmt_inv(sk->itdk, &cmt1, &state1, bn_ctx));
// z1 <- ID.Rsp(c1, y1)
CHECK_IS_ONE(ID_GQ_resp(sk->isk, state1, rsig->ch1, &resp1, bn_ctx));
// Y2 <- Pi(z1)
CHECK_IS_ONE(DAPS_ID2_GQ_perm(vk, resp1->z, &(cmt2.Y), bn_ctx));
// c2 <- H(msg_subj || msg_body)
CHECK_IS_ONE(SHA256_Init(&sha256_ctx));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_subj, msg_subj_length));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_body, msg_body_length));
CHECK_IS_ONE(SHA256_Final(c, &sha256_ctx));
CHECK_IS_ONE(ID_GQ_ch_hash(&ch2, hashlen, c, SHA256_DIGEST_LENGTH));
// y2 <- ID.CmtInv(Y2)
CHECK_IS_ONE(ID_GQ_cmt_inv(sk->itdk, &cmt2, &state2, bn_ctx));
// z2 <- ID.Rsp(c2, y2)
CHECK_IS_ONE(ID_GQ_resp(sk->isk, state2, ch2, &(rsig->resp2), bn_ctx));
// sig <- (c1, z2)
*sig = rsig;
ret = 1;
goto cleanup;
err:
ret = 0;
DAPS_ID2_GQ_SIG_free(rsig);
cleanup:
ID_GQ_STATE_free(state1);
ID_GQ_RESP_free(resp1);
BN_free(cmt2.Y);
ID_GQ_CH_free(ch2);
ID_GQ_STATE_free(state2);
return ret;
}
int DAPS_ID2_GQ_verify(const DAPS_ID2_GQ_VK *vk, const int hashlen, const unsigned char *msg_subj, const int msg_subj_length, const unsigned char *msg_body, const int msg_body_length, const DAPS_ID2_GQ_SIG *sig, BN_CTX *bn_ctx) {
int ret, ok = 0;
unsigned char c[SHA256_DIGEST_LENGTH];
ID_GQ_CMT *cmt1 = NULL, *cmt2 = NULL;
ID_GQ_CH *ch2 = NULL;
ID_GQ_RESP resp1;
resp1.z = NULL;
SHA256_CTX sha256_ctx;
BIGNUM *Y1prime = NULL;
// c2 <- H(msg_subj || msg_body)
CHECK_IS_ONE(SHA256_Init(&sha256_ctx));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_subj, msg_subj_length));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_body, msg_body_length));
CHECK_IS_ONE(SHA256_Final(c, &sha256_ctx));
CHECK_IS_ONE(ID_GQ_ch_hash(&ch2, hashlen, c, SHA256_DIGEST_LENGTH));
// Y2 <- ID.RspInv(c2, z2)
CHECK_IS_ONE(ID_GQ_resp_inv(vk->ipk, &cmt2, ch2, sig->resp2, bn_ctx));
// z1 <- PiInv(Y2)
CHECK_IS_ONE(DAPS_ID2_GQ_perm_inv(vk, cmt2->Y, &(resp1.z), bn_ctx));
// Y1 <- ID.RspInv(c1, z1)
CHECK_IS_ONE(ID_GQ_resp_inv(vk->ipk, &cmt1, sig->ch1, &resp1, bn_ctx));
// Y1 =? H(msg_subj)
CHECK_NONNULL(Y1prime = SHA256_mod(msg_subj, msg_subj_length, vk->ipk->n, bn_ctx));
if (BN_cmp(cmt1->Y, Y1prime) == 0) {
ret = 1;
} else {
ret = 0;
}
goto cleanup;
err:
ret = -1;
cleanup:
ID_GQ_CMT_free(cmt1);
ID_GQ_CMT_free(cmt2);
ID_GQ_CH_free(ch2);
BN_free(resp1.z);
BN_free(Y1prime);
return ret;
}
int DAPS_ID2_GQ_extract(const DAPS_ID2_GQ_VK *vk, const int hashlen, const unsigned char *msg_subj, const int msg_subj_length, const unsigned char *msg_body1, const int msg_body1_length, const DAPS_ID2_GQ_SIG *sig1, const unsigned char *msg_body2, const int msg_body2_length, const DAPS_ID2_GQ_SIG *sig2, DAPS_ID2_GQ_SK **sk, BN_CTX *bn_ctx) {
int ret, ok;
int b;
unsigned char c[SHA256_DIGEST_LENGTH];
unsigned char *x = NULL;
unsigned char *d = NULL;
unsigned char *h = NULL;
ID_GQ_CMT *cmt11 = NULL, *cmt12 = NULL;
ID_GQ_CMT *cmt21 = NULL, *cmt22 = NULL;
ID_GQ_CH *ch21 = NULL, *ch22 = NULL;
ID_GQ_RESP resp11, resp12;
resp11.z = NULL;
resp12.z = NULL;
SHA256_CTX sha256_ctx;
DAPS_ID2_GQ_SK *rsk = NULL;
CHECK_NONNULL(rsk = (DAPS_ID2_GQ_SK *) OPENSSL_malloc(sizeof(DAPS_ID2_GQ_SK)));
// (c_1i, z_2i) <- sig_i
ID_GQ_CH *ch11 = sig1->ch1;
ID_GQ_RESP *resp21 = sig1->resp2;
ID_GQ_CH *ch12 = sig2->ch1;
ID_GQ_RESP *resp22 = sig2->resp2;
// c_2i <- H(a_i, p_i)
CHECK_IS_ONE(SHA256_Init(&sha256_ctx));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_subj, msg_subj_length));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_body1, msg_body1_length));
CHECK_IS_ONE(SHA256_Final(c, &sha256_ctx));
CHECK_IS_ONE(ID_GQ_ch_hash(&ch21, hashlen, c, SHA256_DIGEST_LENGTH));
CHECK_IS_ONE(SHA256_Init(&sha256_ctx));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_subj, msg_subj_length));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_body2, msg_body2_length));
CHECK_IS_ONE(SHA256_Final(c, &sha256_ctx));
CHECK_IS_ONE(ID_GQ_ch_hash(&ch22, hashlen, c, SHA256_DIGEST_LENGTH));
// Y_2i <- ID.RspInv(ivk, c_2i, z_2i)
CHECK_IS_ONE(ID_GQ_resp_inv(vk->ipk, &cmt21, ch21, resp21, bn_ctx));
CHECK_IS_ONE(ID_GQ_resp_inv(vk->ipk, &cmt22, ch22, resp22, bn_ctx));
// z_1i <- PermInv(Y_2i)
CHECK_IS_ONE(DAPS_ID2_GQ_perm_inv(vk, cmt21->Y, &(resp11.z), bn_ctx));
CHECK_IS_ONE(DAPS_ID2_GQ_perm_inv(vk, cmt22->Y, &(resp12.z), bn_ctx));
// Y_1i <- ID.RspInv(iv, c_1i, z_1i)
CHECK_IS_ONE(ID_GQ_resp_inv(vk->ipk, &cmt11, ch11, &resp11, bn_ctx));
CHECK_IS_ONE(ID_GQ_resp_inv(vk->ipk, &cmt12, ch12, &resp12, bn_ctx));
// if Y_21 = Y_22
b = BN_cmp(cmt21->Y, cmt22->Y);
if (b == 0) {
// if c_21 = c_22: return bot
CHECK_NONZERO(BN_cmp(ch21->c, ch22->c));
// isk <- ID.Ex(ivk, Y_21 || c_21 || z_21, Y_22 || c_22 || z_22)
CHECK_IS_ONE(ID_GQ_extract(vk->ipk, cmt21, ch21, resp21, ch22, resp22, &(rsk->isk), bn_ctx));
} else {
// isk <- ID.Ex(ivk, Y_11 || c_11 || z_11, Y_12 || c_12 || z_12)
CHECK_IS_ONE(ID_GQ_extract(vk->ipk, cmt11, ch11, &resp11, ch12, &resp12, &(rsk->isk), bn_ctx));
}
// unmask TDK
// convert sk to binary
CHECK_NONNULL(x = (unsigned char *) OPENSSL_malloc(BN_num_bytes(rsk->isk->x)));
CHECK_GT_ZERO(BN_bn2bin(rsk->isk->x, x));
// hash sk
CHECK_NONNULL(h = SHA256_arbitrary(x, BN_num_bytes(rsk->isk->x), vk->TDK_length));
CHECK_NONNULL(d = (unsigned char *) OPENSSL_malloc(vk->TDK_length));
// XOR hash off of TDK
for (int i = 0; i < vk->TDK_length; i++) {
d[i] = vk->TDK[i] ^ h[i];
}
// convert back to integer
CHECK_NONNULL(rsk->itdk = (ID_GQ_TDK *) OPENSSL_malloc(sizeof(ID_GQ_TDK)));
CHECK_NONNULL(rsk->itdk->d = BN_bin2bn(d, vk->TDK_length, NULL));
CHECK_NONNULL(rsk->itdk->n = BN_dup(vk->ipk->n));
*sk = rsk;
ret = 1;
goto cleanup;
err:
ret = 0;
OPENSSL_free(rsk);
cleanup:
ID_GQ_CMT_free(cmt11);
ID_GQ_CMT_free(cmt12);
ID_GQ_CMT_free(cmt21);
ID_GQ_CMT_free(cmt22);
ID_GQ_CH_free(ch21);
ID_GQ_CH_free(ch22);
BN_free(resp11.z);
BN_free(resp12.z);
return ret;
}
int DAPS_ID2_GQ_test(int keylen, int hashlen, int print) {
int ret, ok;
int ver;
DAPS_ID2_GQ_VK *vk = NULL;
DAPS_ID2_GQ_SK *sk = NULL, *skprime = NULL;
DAPS_ID2_GQ_SIG *sig = NULL, *sig2 = NULL;
BN_CTX *bn_ctx = NULL;
CHECK_NONNULL(bn_ctx = BN_CTX_new());
CHECK_IS_ONE(DAPS_ID2_GQ_keygen(&vk, &sk, keylen, hashlen, bn_ctx));
char *msg_subj = "www.google.com";
char *msg_body = "My public key certificate is 42.";
CHECK_IS_ONE(DAPS_ID2_GQ_sign(vk, sk, hashlen, (unsigned char *) msg_subj, strlen(msg_subj), (unsigned char *) msg_body, strlen(msg_body), &sig, bn_ctx));
ver = DAPS_ID2_GQ_verify(vk, hashlen, (unsigned char *) msg_subj, strlen(msg_subj), (unsigned char *) msg_body, strlen(msg_body), sig, bn_ctx);
if (print) {
if (ver == 1) {
printf("verifies\n");
} else {
printf("!!! DOES NOT VERIFY !!!\n");
}
DAPS_ID2_GQ_VK_print_fp(stdout, vk);
DAPS_ID2_GQ_SK_print_fp(stdout, sk);
DAPS_ID2_GQ_SIG_print_fp(stdout, sig);
}
if (ver != 1) {
goto err;
}
char *msg_body2 = "My public key certificate is 43.";
CHECK_IS_ONE(DAPS_ID2_GQ_sign(vk, sk, hashlen, (unsigned char *) msg_subj, strlen(msg_subj), (unsigned char *) msg_body2, strlen(msg_body2), &sig2, bn_ctx));
CHECK_IS_ONE(DAPS_ID2_GQ_extract(vk, hashlen, (unsigned char *) msg_subj, strlen(msg_subj), (unsigned char *) msg_body, strlen(msg_body), sig, (unsigned char *) msg_body2, strlen(msg_body2), sig2, &skprime, bn_ctx));
ver = (BN_cmp(sk->isk->n, skprime->isk->n) == 0);
ver &= (BN_cmp(sk->isk->x, skprime->isk->x) == 0);
ver &= (BN_cmp(sk->itdk->n, skprime->itdk->n) == 0);
ver &= (BN_cmp(sk->itdk->d, skprime->itdk->d) == 0);
if (print) {
if (ver == 1) {
printf("extracts\n");
} else {
printf("!!! DOES NOT EXTRACT !!!\n");
}
DAPS_ID2_GQ_SIG_print_fp(stdout, sig2);
DAPS_ID2_GQ_SK_print_fp(stdout, skprime);
}
if (ver != 1) {
goto err;
}
ret = 1;
goto cleanup;
err:
fprintf(stderr, "An error occurred.\n");
ret = 0;
cleanup:
fflush(stdout);
DAPS_ID2_GQ_VK_free(vk);
DAPS_ID2_GQ_SK_free(sk);
DAPS_ID2_GQ_SIG_free(sig);
BN_CTX_free(bn_ctx);
return ret;
}
int DAPS_ID2_GQ_test_perm(int keylen, int hashlen, int print) {
int ret, ok;
DAPS_ID2_GQ_VK *vk = NULL;
DAPS_ID2_GQ_SK *sk = NULL;
BIGNUM *x = NULL, *y = NULL, *z = NULL;
BN_CTX *bn_ctx = NULL;
CHECK_NONNULL(bn_ctx = BN_CTX_new());
CHECK_NONNULL(x = BN_new());
CHECK_IS_ONE(DAPS_ID2_GQ_keygen(&vk, &sk, keylen, hashlen, bn_ctx));
if (print) {
printf("---\n");
printf("n = ");
BN_print_fp(stdout, vk->ipk->n);
printf("\n");
}
CHECK_IS_ONE(BN_rand_range(x, vk->ipk->n));
CHECK_IS_ONE(DAPS_ID2_GQ_perm(vk, x, &y, bn_ctx));
CHECK_IS_ONE(DAPS_ID2_GQ_perm_inv(vk, y, &z, bn_ctx));
if (print) {
printf("x = ");
BN_print_fp(stdout, x);
printf("\n");
printf("y = ");
BN_print_fp(stdout, y);
printf("\n");
printf("z = ");
BN_print_fp(stdout, z);
printf("\n");
}
CHECK_IS_ZERO(BN_cmp(x, z));
ret = 1;
goto cleanup;
err:
ret = 0;
fprintf(stderr, "An error occurred.\n");
cleanup:
DAPS_ID2_GQ_VK_free(vk);
DAPS_ID2_GQ_SK_free(sk);
BN_free(x);
BN_free(y);
BN_free(z);
BN_CTX_free(bn_ctx);
return ret;
}