-
Notifications
You must be signed in to change notification settings - Fork 0
/
daps_ps.c
288 lines (272 loc) · 8.63 KB
/
daps_ps.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
/********************************************************************************************
* 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_ps.c
* PS DAPS scheme.
*/
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include "common.h"
#include "daps_ps.h"
#include "bn_extra.h"
void DAPS_PS_VK_free(DAPS_PS_VK *vk) {
if (vk == NULL) {
return;
}
TDF_PS_PK_free(vk->tdfpk);
OPENSSL_free(vk);
}
void DAPS_PS_SK_free(DAPS_PS_SK *sk) {
if (sk == NULL) {
return;
}
TDF_PS_TDK_free(sk->tdftdk);
OPENSSL_free(sk);
}
void DAPS_PS_SIG_free(DAPS_PS_SIG *sig) {
if (sig == NULL) {
return;
}
BN_free(sig->s);
if ((sig->a != NULL) && (sig->a_length > 0)) {
for (int i = 0; i < sig->a_length; i++) {
BN_free(sig->a[i]);
}
}
OPENSSL_free(sig);
}
void DAPS_PS_VK_print_fp(FILE *fp, const DAPS_PS_VK *vk) {
if (vk == NULL) {
return;
}
TDF_PS_PK_print_fp(fp, vk->tdfpk);
}
void DAPS_PS_SK_print_fp(FILE *fp, const DAPS_PS_SK *sk) {
if (sk == NULL) {
return;
}
TDF_PS_TDK_print_fp(fp, sk->tdftdk);
}
void DAPS_PS_SIG_print_fp(FILE *fp, const DAPS_PS_SIG *sig) {
if (sig == NULL) {
return;
}
BN_printdec_fp(fp, "sig_s", sig->s);
for (int i = 0; i < sig->a_length; i++) {
fprintf(fp, "sig_a_%d", i);
BN_printdec_fp(fp, "", sig->a[i]);
}
}
// allocates *vk and *sk which must be later freed
// returns 1 on success and 0 on error
int DAPS_PS_keygen(DAPS_PS_VK **vk, DAPS_PS_SK **sk, const int bits, BN_CTX *bn_ctx) {
int ret, ok;
DAPS_PS_VK *rvk = NULL;
DAPS_PS_SK *rsk = NULL;
CHECK_NONNULL(rvk = (DAPS_PS_VK *) OPENSSL_malloc(sizeof(DAPS_PS_VK)));
CHECK_NONNULL(rsk = (DAPS_PS_SK *) OPENSSL_malloc(sizeof(DAPS_PS_SK)));
CHECK_IS_ONE(TDF_PS_keygen(&(rvk->tdfpk), &(rsk->tdftdk), bits, bn_ctx));
*vk = rvk;
*sk = rsk;
ret = 1;
goto cleanup;
err:
ret = 0;
DAPS_PS_VK_free(rvk);
DAPS_PS_SK_free(rsk);
cleanup:
return ret;
}
int DAPS_PS_sign(const DAPS_PS_VK *vk, const DAPS_PS_SK *sk, const unsigned char *msg_subj, const int msg_subj_length, const unsigned char *msg_body, const int msg_body_length, const int hash_length, DAPS_PS_SIG **sig, BN_CTX *bn_ctx) {
int ret, ok;
DAPS_PS_SIG *rsig = NULL;
SHA256_CTX sha256_ctx;
BIGNUM *h = NULL;
unsigned char *d = NULL;
unsigned char *s_bin = NULL;
unsigned char *b_i_bin = NULL;
BIGNUM *b_i = NULL;
if (hash_length > SHA256_DIGEST_LENGTH * 8) {
goto err;
}
CHECK_NONNULL(h = BN_new());
CHECK_NONNULL(rsig = (DAPS_PS_SIG *) OPENSSL_malloc(sizeof(DAPS_PS_SIG)));
// h <- H_pub(subj)
CHECK_IS_ONE(TDF_PS_hash_onto_range(vk->tdfpk, msg_subj, msg_subj_length, h, bn_ctx));
CHECK_NONNULL(rsig->s = BN_new());
// s <- Reverse(td, h, 0)
CHECK_IS_ONE(TDF_PS_inv(rsig->s, sk->tdftdk, h, 0, bn_ctx));
CHECK_NONNULL(s_bin = OPENSSL_malloc(BN_num_bytes(rsig->s)));
CHECK_GT_ZERO(BN_bn2bin(rsig->s, s_bin));
// d <- H(subj, s, body)
CHECK_NONNULL(d = OPENSSL_malloc(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, s_bin, BN_num_bytes(rsig->s)));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_body, msg_body_length));
CHECK_IS_ONE(SHA256_Final(d, &sha256_ctx));
// for 1 <= i <= lambda:
// b_i <- H(subj, s, i)
// a_i <- Reverse(td, b_i, d_i)
rsig->a_length = hash_length;
CHECK_NONNULL(rsig->a = (BIGNUM **) OPENSSL_malloc(sizeof(BIGNUM *) * rsig->a_length));
CHECK_NONNULL(b_i_bin = OPENSSL_malloc(SHA256_DIGEST_LENGTH));
CHECK_NONNULL(b_i = BN_new());
for (int i = 0; i < rsig->a_length / 8; i++) {
for (int j = 0; j < 8; j++) {
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, s_bin, BN_num_bytes(rsig->s)));
int ij = i * 8 + j;
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, (unsigned char *) &ij, sizeof(int)));
CHECK_IS_ONE(SHA256_Final(b_i_bin, &sha256_ctx));
CHECK_IS_ONE(TDF_PS_hash_onto_range(vk->tdfpk, b_i_bin, SHA256_DIGEST_LENGTH, b_i, bn_ctx));
unsigned char dij = d[i] >> j;
dij &= 1;
CHECK_NONNULL(rsig->a[ij] = BN_new());
CHECK_IS_ONE(TDF_PS_inv(rsig->a[ij], sk->tdftdk, b_i, dij, bn_ctx));
}
}
ret = 1;
*sig = rsig;
goto cleanup;
err:
ret = 0;
DAPS_PS_SIG_free(rsig);
cleanup:
OPENSSL_free(d);
OPENSSL_free(s_bin);
OPENSSL_free(b_i_bin);
BN_free(h);
BN_free(b_i);
return ret;
}
int DAPS_PS_verify(const DAPS_PS_VK *vk, const unsigned char *msg_subj, const int msg_subj_length, const unsigned char *msg_body, const int msg_body_length, const DAPS_PS_SIG *sig, BN_CTX *bn_ctx) {
int ret, ok;
SHA256_CTX sha256_ctx;
BIGNUM *h_rhs = NULL, *h_lhs = NULL;
BIGNUM *aprime_rhs = NULL, *aprime_lhs = NULL;
unsigned char *d = NULL;
unsigned char *s_bin = NULL;
unsigned char *aprime_rhs_bin = NULL;
// If Decide(pk, s) != 0, abort
ok = TDF_PS_decide(vk->tdfpk, sig->s, bn_ctx);
if (ok == -1) {
goto err;
}
if (ok == 1) {
ret = 0;
goto cleanup;
}
// If Apply(pub, s) != H_pub(subj), abort
CHECK_NONNULL(h_rhs = BN_new());
CHECK_IS_ONE(TDF_PS_hash_onto_range(vk->tdfpk, msg_subj, msg_subj_length, h_rhs, bn_ctx));
CHECK_NONNULL(h_lhs = BN_new());
CHECK_IS_ONE(TDF_PS_apply(h_lhs, vk->tdfpk, sig->s, bn_ctx));
CHECK_IS_ZERO(BN_cmp(h_rhs, h_lhs));
// d <- H(subj, s, body)
CHECK_NONNULL(s_bin = OPENSSL_malloc(BN_num_bytes(sig->s)));
CHECK_GT_ZERO(BN_bn2bin(sig->s, s_bin));
CHECK_NONNULL(d = OPENSSL_malloc(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, s_bin, BN_num_bytes(sig->s)));
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, msg_body, msg_body_length));
CHECK_IS_ONE(SHA256_Final(d, &sha256_ctx));
// for 1 <= i <= lambda:
// If Apply(pub, a_i) != H_pub(subj, s, i), abort
// If Decide(pub, a_i) != d_i, abort
CHECK_NONNULL(aprime_rhs_bin = OPENSSL_malloc(SHA256_DIGEST_LENGTH));
CHECK_NONNULL(aprime_rhs = BN_new());
CHECK_NONNULL(aprime_lhs = BN_new());
for (int i = 0; i < sig->a_length / 8; i++) {
for (int j = 0; j < 8; j++) {
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, s_bin, BN_num_bytes(sig->s)));
int ij = i * 8 + j;
CHECK_IS_ONE(SHA256_Update(&sha256_ctx, (unsigned char *) &ij, sizeof(int)));
CHECK_IS_ONE(SHA256_Final(aprime_rhs_bin, &sha256_ctx));
CHECK_IS_ONE(TDF_PS_hash_onto_range(vk->tdfpk, aprime_rhs_bin, SHA256_DIGEST_LENGTH, aprime_rhs, bn_ctx));
CHECK_IS_ONE(TDF_PS_apply(aprime_lhs, vk->tdfpk, sig->a[ij], bn_ctx));
ok = BN_cmp(aprime_lhs, aprime_rhs);
if (ok != 0) {
ret = 0;
goto cleanup;
}
unsigned char dij = d[i] >> j;
dij &= 1;
ok = TDF_PS_decide(vk->tdfpk, sig->a[ij], bn_ctx);
if (ok == -1) {
goto err;
}
if (ok != dij) {
ret = 0;
goto cleanup;
}
}
}
ret = 1;
goto cleanup;
err:
ret = -1;
cleanup:
BN_free(h_rhs);
BN_free(h_lhs);
BN_free(aprime_rhs);
BN_free(aprime_lhs);
OPENSSL_free(aprime_rhs_bin);
OPENSSL_free(s_bin);
return ret;
}
int DAPS_PS_test(int keylen, int hashlen, int print) {
int ret, ok;
int ver;
DAPS_PS_VK *vk = NULL;
DAPS_PS_SK *sk = NULL;
DAPS_PS_SIG *sig = NULL;
BN_CTX *bn_ctx = NULL;
CHECK_NONNULL(bn_ctx = BN_CTX_new());
CHECK_IS_ONE(DAPS_PS_keygen(&vk, &sk, keylen, bn_ctx));
char *msg_subj = "www.google.com";
char *msg_body = "My public key certificate is 42.";
CHECK_IS_ONE(DAPS_PS_sign(vk, sk, (unsigned char *) msg_subj, strlen(msg_subj), (unsigned char *) msg_body, strlen(msg_body), hashlen, &sig, bn_ctx));
ver = DAPS_PS_verify(vk, (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_PS_VK_print_fp(stdout, vk);
DAPS_PS_SK_print_fp(stdout, sk);
DAPS_PS_SIG_print_fp(stdout, sig);
}
if (ver != 1) {
goto err;
}
ret = 1;
goto cleanup;
err:
fprintf(stderr, "An error occurred.\n");
ret = 0;
cleanup:
fflush(stdout);
DAPS_PS_VK_free(vk);
DAPS_PS_SK_free(sk);
DAPS_PS_SIG_free(sig);
BN_CTX_free(bn_ctx);
return ret;
}