1 #include "jpake.h"
2
3 #include <openssl/crypto.h>
4 #include <openssl/sha.h>
5 #include <openssl/err.h>
6 #include <memory.h>
7
8 /*
9 * In the definition, (xa, xb, xc, xd) are Alice's (x1, x2, x3, x4) or
10 * Bob's (x3, x4, x1, x2). If you see what I mean.
11 */
12
13 typedef struct
14 {
15 char *name; /* Must be unique */
16 char *peer_name;
17 BIGNUM *p;
18 BIGNUM *g;
19 BIGNUM *q;
20 BIGNUM *gxc; /* Alice's g^{x3} or Bob's g^{x1} */
21 BIGNUM *gxd; /* Alice's g^{x4} or Bob's g^{x2} */
22 } JPAKE_CTX_PUBLIC;
23
24 struct JPAKE_CTX
25 {
26 JPAKE_CTX_PUBLIC p;
27 BIGNUM *secret; /* The shared secret */
28 BN_CTX *ctx;
29 BIGNUM *xa; /* Alice's x1 or Bob's x3 */
30 BIGNUM *xb; /* Alice's x2 or Bob's x4 */
31 BIGNUM *key; /* The calculated (shared) key */
32 };
33
JPAKE_ZKP_init(JPAKE_ZKP * zkp)34 static void JPAKE_ZKP_init(JPAKE_ZKP *zkp)
35 {
36 zkp->gr = BN_new();
37 zkp->b = BN_new();
38 }
39
JPAKE_ZKP_release(JPAKE_ZKP * zkp)40 static void JPAKE_ZKP_release(JPAKE_ZKP *zkp)
41 {
42 BN_free(zkp->b);
43 BN_free(zkp->gr);
44 }
45
46 /* Two birds with one stone - make the global name as expected */
47 #define JPAKE_STEP_PART_init JPAKE_STEP2_init
48 #define JPAKE_STEP_PART_release JPAKE_STEP2_release
49
JPAKE_STEP_PART_init(JPAKE_STEP_PART * p)50 void JPAKE_STEP_PART_init(JPAKE_STEP_PART *p)
51 {
52 p->gx = BN_new();
53 JPAKE_ZKP_init(&p->zkpx);
54 }
55
JPAKE_STEP_PART_release(JPAKE_STEP_PART * p)56 void JPAKE_STEP_PART_release(JPAKE_STEP_PART *p)
57 {
58 JPAKE_ZKP_release(&p->zkpx);
59 BN_free(p->gx);
60 }
61
JPAKE_STEP1_init(JPAKE_STEP1 * s1)62 void JPAKE_STEP1_init(JPAKE_STEP1 *s1)
63 {
64 JPAKE_STEP_PART_init(&s1->p1);
65 JPAKE_STEP_PART_init(&s1->p2);
66 }
67
JPAKE_STEP1_release(JPAKE_STEP1 * s1)68 void JPAKE_STEP1_release(JPAKE_STEP1 *s1)
69 {
70 JPAKE_STEP_PART_release(&s1->p2);
71 JPAKE_STEP_PART_release(&s1->p1);
72 }
73
JPAKE_CTX_init(JPAKE_CTX * ctx,const char * name,const char * peer_name,const BIGNUM * p,const BIGNUM * g,const BIGNUM * q,const BIGNUM * secret)74 static void JPAKE_CTX_init(JPAKE_CTX *ctx, const char *name,
75 const char *peer_name, const BIGNUM *p,
76 const BIGNUM *g, const BIGNUM *q,
77 const BIGNUM *secret)
78 {
79 ctx->p.name = OPENSSL_strdup(name);
80 ctx->p.peer_name = OPENSSL_strdup(peer_name);
81 ctx->p.p = BN_dup(p);
82 ctx->p.g = BN_dup(g);
83 ctx->p.q = BN_dup(q);
84 ctx->secret = BN_dup(secret);
85
86 ctx->p.gxc = BN_new();
87 ctx->p.gxd = BN_new();
88
89 ctx->xa = BN_new();
90 ctx->xb = BN_new();
91 ctx->key = BN_new();
92 ctx->ctx = BN_CTX_new();
93 }
94
JPAKE_CTX_release(JPAKE_CTX * ctx)95 static void JPAKE_CTX_release(JPAKE_CTX *ctx)
96 {
97 BN_CTX_free(ctx->ctx);
98 BN_clear_free(ctx->key);
99 BN_clear_free(ctx->xb);
100 BN_clear_free(ctx->xa);
101
102 BN_free(ctx->p.gxd);
103 BN_free(ctx->p.gxc);
104
105 BN_clear_free(ctx->secret);
106 BN_free(ctx->p.q);
107 BN_free(ctx->p.g);
108 BN_free(ctx->p.p);
109 OPENSSL_free(ctx->p.peer_name);
110 OPENSSL_free(ctx->p.name);
111
112 memset(ctx, '\0', sizeof *ctx);
113 }
114
JPAKE_CTX_new(const char * name,const char * peer_name,const BIGNUM * p,const BIGNUM * g,const BIGNUM * q,const BIGNUM * secret)115 JPAKE_CTX *JPAKE_CTX_new(const char *name, const char *peer_name,
116 const BIGNUM *p, const BIGNUM *g, const BIGNUM *q,
117 const BIGNUM *secret)
118 {
119 JPAKE_CTX *ctx = OPENSSL_malloc(sizeof *ctx);
120
121 JPAKE_CTX_init(ctx, name, peer_name, p, g, q, secret);
122
123 return ctx;
124 }
125
JPAKE_CTX_free(JPAKE_CTX * ctx)126 void JPAKE_CTX_free(JPAKE_CTX *ctx)
127 {
128 JPAKE_CTX_release(ctx);
129 OPENSSL_free(ctx);
130 }
131
hashlength(SHA_CTX * sha,size_t l)132 static void hashlength(SHA_CTX *sha, size_t l)
133 {
134 unsigned char b[2];
135
136 OPENSSL_assert(l <= 0xffff);
137 b[0] = l >> 8;
138 b[1] = l&0xff;
139 SHA1_Update(sha, b, 2);
140 }
141
hashstring(SHA_CTX * sha,const char * string)142 static void hashstring(SHA_CTX *sha, const char *string)
143 {
144 size_t l = strlen(string);
145
146 hashlength(sha, l);
147 SHA1_Update(sha, string, l);
148 }
149
hashbn(SHA_CTX * sha,const BIGNUM * bn)150 static void hashbn(SHA_CTX *sha, const BIGNUM *bn)
151 {
152 size_t l = BN_num_bytes(bn);
153 unsigned char *bin = OPENSSL_malloc(l);
154
155 hashlength(sha, l);
156 BN_bn2bin(bn, bin);
157 SHA1_Update(sha, bin, l);
158 OPENSSL_free(bin);
159 }
160
161 /* h=hash(g, g^r, g^x, name) */
zkp_hash(BIGNUM * h,const BIGNUM * zkpg,const JPAKE_STEP_PART * p,const char * proof_name)162 static void zkp_hash(BIGNUM *h, const BIGNUM *zkpg, const JPAKE_STEP_PART *p,
163 const char *proof_name)
164 {
165 unsigned char md[SHA_DIGEST_LENGTH];
166 SHA_CTX sha;
167
168 /*
169 * XXX: hash should not allow moving of the boundaries - Java code
170 * is flawed in this respect. Length encoding seems simplest.
171 */
172 SHA1_Init(&sha);
173 hashbn(&sha, zkpg);
174 OPENSSL_assert(!BN_is_zero(p->zkpx.gr));
175 hashbn(&sha, p->zkpx.gr);
176 hashbn(&sha, p->gx);
177 hashstring(&sha, proof_name);
178 SHA1_Final(md, &sha);
179 BN_bin2bn(md, SHA_DIGEST_LENGTH, h);
180 }
181
182 /*
183 * Prove knowledge of x
184 * Note that p->gx has already been calculated
185 */
generate_zkp(JPAKE_STEP_PART * p,const BIGNUM * x,const BIGNUM * zkpg,JPAKE_CTX * ctx)186 static void generate_zkp(JPAKE_STEP_PART *p, const BIGNUM *x,
187 const BIGNUM *zkpg, JPAKE_CTX *ctx)
188 {
189 BIGNUM *r = BN_new();
190 BIGNUM *h = BN_new();
191 BIGNUM *t = BN_new();
192
193 /*
194 * r in [0,q)
195 * XXX: Java chooses r in [0, 2^160) - i.e. distribution not uniform
196 */
197 BN_rand_range(r, ctx->p.q);
198 /* g^r */
199 BN_mod_exp(p->zkpx.gr, zkpg, r, ctx->p.p, ctx->ctx);
200
201 /* h=hash... */
202 zkp_hash(h, zkpg, p, ctx->p.name);
203
204 /* b = r - x*h */
205 BN_mod_mul(t, x, h, ctx->p.q, ctx->ctx);
206 BN_mod_sub(p->zkpx.b, r, t, ctx->p.q, ctx->ctx);
207
208 /* cleanup */
209 BN_free(t);
210 BN_free(h);
211 BN_free(r);
212 }
213
verify_zkp(const JPAKE_STEP_PART * p,const BIGNUM * zkpg,JPAKE_CTX * ctx)214 static int verify_zkp(const JPAKE_STEP_PART *p, const BIGNUM *zkpg,
215 JPAKE_CTX *ctx)
216 {
217 BIGNUM *h = BN_new();
218 BIGNUM *t1 = BN_new();
219 BIGNUM *t2 = BN_new();
220 BIGNUM *t3 = BN_new();
221 int ret = 0;
222
223 zkp_hash(h, zkpg, p, ctx->p.peer_name);
224
225 /* t1 = g^b */
226 BN_mod_exp(t1, zkpg, p->zkpx.b, ctx->p.p, ctx->ctx);
227 /* t2 = (g^x)^h = g^{hx} */
228 BN_mod_exp(t2, p->gx, h, ctx->p.p, ctx->ctx);
229 /* t3 = t1 * t2 = g^{hx} * g^b = g^{hx+b} = g^r (allegedly) */
230 BN_mod_mul(t3, t1, t2, ctx->p.p, ctx->ctx);
231
232 /* verify t3 == g^r */
233 if(BN_cmp(t3, p->zkpx.gr) == 0)
234 ret = 1;
235 else
236 JPAKEerr(JPAKE_F_VERIFY_ZKP, JPAKE_R_ZKP_VERIFY_FAILED);
237
238 /* cleanup */
239 BN_free(t3);
240 BN_free(t2);
241 BN_free(t1);
242 BN_free(h);
243
244 return ret;
245 }
246
generate_step_part(JPAKE_STEP_PART * p,const BIGNUM * x,const BIGNUM * g,JPAKE_CTX * ctx)247 static void generate_step_part(JPAKE_STEP_PART *p, const BIGNUM *x,
248 const BIGNUM *g, JPAKE_CTX *ctx)
249 {
250 BN_mod_exp(p->gx, g, x, ctx->p.p, ctx->ctx);
251 generate_zkp(p, x, g, ctx);
252 }
253
254 /* Generate each party's random numbers. xa is in [0, q), xb is in [1, q). */
genrand(JPAKE_CTX * ctx)255 static void genrand(JPAKE_CTX *ctx)
256 {
257 BIGNUM *qm1;
258
259 /* xa in [0, q) */
260 BN_rand_range(ctx->xa, ctx->p.q);
261
262 /* q-1 */
263 qm1 = BN_new();
264 BN_copy(qm1, ctx->p.q);
265 BN_sub_word(qm1, 1);
266
267 /* ... and xb in [0, q-1) */
268 BN_rand_range(ctx->xb, qm1);
269 /* [1, q) */
270 BN_add_word(ctx->xb, 1);
271
272 /* cleanup */
273 BN_free(qm1);
274 }
275
JPAKE_STEP1_generate(JPAKE_STEP1 * send,JPAKE_CTX * ctx)276 int JPAKE_STEP1_generate(JPAKE_STEP1 *send, JPAKE_CTX *ctx)
277 {
278 genrand(ctx);
279 generate_step_part(&send->p1, ctx->xa, ctx->p.g, ctx);
280 generate_step_part(&send->p2, ctx->xb, ctx->p.g, ctx);
281
282 return 1;
283 }
284
JPAKE_STEP1_process(JPAKE_CTX * ctx,const JPAKE_STEP1 * received)285 int JPAKE_STEP1_process(JPAKE_CTX *ctx, const JPAKE_STEP1 *received)
286 {
287 /* verify their ZKP(xc) */
288 if(!verify_zkp(&received->p1, ctx->p.g, ctx))
289 {
290 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_VERIFY_X3_FAILED);
291 return 0;
292 }
293
294 /* verify their ZKP(xd) */
295 if(!verify_zkp(&received->p2, ctx->p.g, ctx))
296 {
297 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_VERIFY_X4_FAILED);
298 return 0;
299 }
300
301 /* g^xd != 1 */
302 if(BN_is_one(received->p2.gx))
303 {
304 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_G_TO_THE_X4_IS_ONE);
305 return 0;
306 }
307
308 /* Save the bits we need for later */
309 BN_copy(ctx->p.gxc, received->p1.gx);
310 BN_copy(ctx->p.gxd, received->p2.gx);
311
312 return 1;
313 }
314
315
JPAKE_STEP2_generate(JPAKE_STEP2 * send,JPAKE_CTX * ctx)316 int JPAKE_STEP2_generate(JPAKE_STEP2 *send, JPAKE_CTX *ctx)
317 {
318 BIGNUM *t1 = BN_new();
319 BIGNUM *t2 = BN_new();
320
321 /*
322 * X = g^{(xa + xc + xd) * xb * s}
323 * t1 = g^xa
324 */
325 BN_mod_exp(t1, ctx->p.g, ctx->xa, ctx->p.p, ctx->ctx);
326 /* t2 = t1 * g^{xc} = g^{xa} * g^{xc} = g^{xa + xc} */
327 BN_mod_mul(t2, t1, ctx->p.gxc, ctx->p.p, ctx->ctx);
328 /* t1 = t2 * g^{xd} = g^{xa + xc + xd} */
329 BN_mod_mul(t1, t2, ctx->p.gxd, ctx->p.p, ctx->ctx);
330 /* t2 = xb * s */
331 BN_mod_mul(t2, ctx->xb, ctx->secret, ctx->p.q, ctx->ctx);
332
333 /*
334 * ZKP(xb * s)
335 * XXX: this is kinda funky, because we're using
336 *
337 * g' = g^{xa + xc + xd}
338 *
339 * as the generator, which means X is g'^{xb * s}
340 * X = t1^{t2} = t1^{xb * s} = g^{(xa + xc + xd) * xb * s}
341 */
342 generate_step_part(send, t2, t1, ctx);
343
344 /* cleanup */
345 BN_free(t1);
346 BN_free(t2);
347
348 return 1;
349 }
350
351 /* gx = g^{xc + xa + xb} * xd * s */
compute_key(JPAKE_CTX * ctx,const BIGNUM * gx)352 static int compute_key(JPAKE_CTX *ctx, const BIGNUM *gx)
353 {
354 BIGNUM *t1 = BN_new();
355 BIGNUM *t2 = BN_new();
356 BIGNUM *t3 = BN_new();
357
358 /*
359 * K = (gx/g^{xb * xd * s})^{xb}
360 * = (g^{(xc + xa + xb) * xd * s - xb * xd *s})^{xb}
361 * = (g^{(xa + xc) * xd * s})^{xb}
362 * = g^{(xa + xc) * xb * xd * s}
363 * [which is the same regardless of who calculates it]
364 */
365
366 /* t1 = (g^{xd})^{xb} = g^{xb * xd} */
367 BN_mod_exp(t1, ctx->p.gxd, ctx->xb, ctx->p.p, ctx->ctx);
368 /* t2 = -s = q-s */
369 BN_sub(t2, ctx->p.q, ctx->secret);
370 /* t3 = t1^t2 = g^{-xb * xd * s} */
371 BN_mod_exp(t3, t1, t2, ctx->p.p, ctx->ctx);
372 /* t1 = gx * t3 = X/g^{xb * xd * s} */
373 BN_mod_mul(t1, gx, t3, ctx->p.p, ctx->ctx);
374 /* K = t1^{xb} */
375 BN_mod_exp(ctx->key, t1, ctx->xb, ctx->p.p, ctx->ctx);
376
377 /* cleanup */
378 BN_free(t3);
379 BN_free(t2);
380 BN_free(t1);
381
382 return 1;
383 }
384
JPAKE_STEP2_process(JPAKE_CTX * ctx,const JPAKE_STEP2 * received)385 int JPAKE_STEP2_process(JPAKE_CTX *ctx, const JPAKE_STEP2 *received)
386 {
387 BIGNUM *t1 = BN_new();
388 BIGNUM *t2 = BN_new();
389 int ret = 0;
390
391 /*
392 * g' = g^{xc + xa + xb} [from our POV]
393 * t1 = xa + xb
394 */
395 BN_mod_add(t1, ctx->xa, ctx->xb, ctx->p.q, ctx->ctx);
396 /* t2 = g^{t1} = g^{xa+xb} */
397 BN_mod_exp(t2, ctx->p.g, t1, ctx->p.p, ctx->ctx);
398 /* t1 = g^{xc} * t2 = g^{xc + xa + xb} */
399 BN_mod_mul(t1, ctx->p.gxc, t2, ctx->p.p, ctx->ctx);
400
401 if(verify_zkp(received, t1, ctx))
402 ret = 1;
403 else
404 JPAKEerr(JPAKE_F_JPAKE_STEP2_PROCESS, JPAKE_R_VERIFY_B_FAILED);
405
406 compute_key(ctx, received->gx);
407
408 /* cleanup */
409 BN_free(t2);
410 BN_free(t1);
411
412 return ret;
413 }
414
quickhashbn(unsigned char * md,const BIGNUM * bn)415 static void quickhashbn(unsigned char *md, const BIGNUM *bn)
416 {
417 SHA_CTX sha;
418
419 SHA1_Init(&sha);
420 hashbn(&sha, bn);
421 SHA1_Final(md, &sha);
422 }
423
JPAKE_STEP3A_init(JPAKE_STEP3A * s3a)424 void JPAKE_STEP3A_init(JPAKE_STEP3A *s3a)
425 {}
426
JPAKE_STEP3A_generate(JPAKE_STEP3A * send,JPAKE_CTX * ctx)427 int JPAKE_STEP3A_generate(JPAKE_STEP3A *send, JPAKE_CTX *ctx)
428 {
429 quickhashbn(send->hhk, ctx->key);
430 SHA1(send->hhk, sizeof send->hhk, send->hhk);
431
432 return 1;
433 }
434
JPAKE_STEP3A_process(JPAKE_CTX * ctx,const JPAKE_STEP3A * received)435 int JPAKE_STEP3A_process(JPAKE_CTX *ctx, const JPAKE_STEP3A *received)
436 {
437 unsigned char hhk[SHA_DIGEST_LENGTH];
438
439 quickhashbn(hhk, ctx->key);
440 SHA1(hhk, sizeof hhk, hhk);
441 if(memcmp(hhk, received->hhk, sizeof hhk))
442 {
443 JPAKEerr(JPAKE_F_JPAKE_STEP3A_PROCESS, JPAKE_R_HASH_OF_HASH_OF_KEY_MISMATCH);
444 return 0;
445 }
446 return 1;
447 }
448
JPAKE_STEP3A_release(JPAKE_STEP3A * s3a)449 void JPAKE_STEP3A_release(JPAKE_STEP3A *s3a)
450 {}
451
JPAKE_STEP3B_init(JPAKE_STEP3B * s3b)452 void JPAKE_STEP3B_init(JPAKE_STEP3B *s3b)
453 {}
454
JPAKE_STEP3B_generate(JPAKE_STEP3B * send,JPAKE_CTX * ctx)455 int JPAKE_STEP3B_generate(JPAKE_STEP3B *send, JPAKE_CTX *ctx)
456 {
457 quickhashbn(send->hk, ctx->key);
458
459 return 1;
460 }
461
JPAKE_STEP3B_process(JPAKE_CTX * ctx,const JPAKE_STEP3B * received)462 int JPAKE_STEP3B_process(JPAKE_CTX *ctx, const JPAKE_STEP3B *received)
463 {
464 unsigned char hk[SHA_DIGEST_LENGTH];
465
466 quickhashbn(hk, ctx->key);
467 if(memcmp(hk, received->hk, sizeof hk))
468 {
469 JPAKEerr(JPAKE_F_JPAKE_STEP3B_PROCESS, JPAKE_R_HASH_OF_KEY_MISMATCH);
470 return 0;
471 }
472 return 1;
473 }
474
JPAKE_STEP3B_release(JPAKE_STEP3B * s3b)475 void JPAKE_STEP3B_release(JPAKE_STEP3B *s3b)
476 {}
477
JPAKE_get_shared_key(JPAKE_CTX * ctx)478 const BIGNUM *JPAKE_get_shared_key(JPAKE_CTX *ctx)
479 {
480 return ctx->key;
481 }
482
483