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