1 #include "openssl/bn.h"
2 #include "openssl/sha.h"
7 /* Copyright (C) 2008 Ben Laurie (ben@links.org) */
10 * Implement J-PAKE, as described in
11 * http://grouper.ieee.org/groups/1363/Research/contributions/hao-ryan-2008.pdf
13 * With hints from http://www.cl.cam.ac.uk/~fh240/software/JPAKE2.java.
16 static void showbn(const char *name, const BIGNUM *bn)
20 BN_print_fp(stdout, bn);
26 BN_CTX *ctx; // Perhaps not the best place for this?
32 static void JPakeParametersInit(JPakeParameters *params)
34 params->ctx = BN_CTX_new();
36 // For now use p, q, g from Java sample code. Later, generate them.
38 BN_hex2bn(¶ms->p, "fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7");
40 BN_hex2bn(¶ms->q, "9760508f15230bccb292b982a2eb840bf0581cf5");
42 BN_hex2bn(¶ms->g, "f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a");
44 showbn("p", params->p);
45 showbn("q", params->q);
46 showbn("g", params->g);
51 BIGNUM *gr; // g^r (r random)
52 BIGNUM *b; // b = r - x*h, h=hash(g, g^r, g^x, name)
58 JPakeZKP zkpx; // ZKP(x)
63 BIGNUM *X; // g^(xa + xc + xd) * xb * s
64 JPakeZKP zkpxbs; // ZKP(xb * s)
69 const char *name; // Must be unique
70 int base; // 1 for Alice, 3 for Bob. Only used for printing stuff.
71 JPakeStep1 s1c; // Alice's g^x3, ZKP(x3) or Bob's g^x1, ZKP(x1)
72 JPakeStep1 s1d; // Alice's g^x4, ZKP(x4) or Bob's g^x2, ZKP(x2)
73 JPakeStep2 s2; // Alice's A, ZKP(x2 * s) or Bob's B, ZKP(x4 * s)
77 * The user structure. In the definition, (xa, xb, xc, xd) are Alice's
78 * (x1, x2, x3, x4) or Bob's (x3, x4, x1, x2). If you see what I mean.
83 BIGNUM *secret; // The shared secret
84 BIGNUM *key; // The calculated (shared) key
85 BIGNUM *xa; // Alice's x1 or Bob's x3
86 BIGNUM *xb; // Alice's x2 or Bob's x4
89 // Generate each party's random numbers. xa is in [0, q), xb is in [1, q).
90 static void genrand(JPakeUser *user, const JPakeParameters *params)
96 BN_rand_range(user->xa, params->q);
100 BN_copy(qm1, params->q);
103 // ... and xb in [0, q-1)
105 BN_rand_range(user->xb, qm1);
107 BN_add_word(user->xb, 1);
113 printf("x%d", user->p.base);
114 showbn("", user->xa);
115 printf("x%d", user->p.base+1);
116 showbn("", user->xb);
119 static void hashlength(SHA_CTX *sha, size_t l)
126 SHA1_Update(sha, b, 2);
129 static void hashstring(SHA_CTX *sha, const char *string)
131 size_t l = strlen(string);
134 SHA1_Update(sha, string, l);
137 static void hashbn(SHA_CTX *sha, const BIGNUM *bn)
139 size_t l = BN_num_bytes(bn);
140 unsigned char *bin = alloca(l);
144 SHA1_Update(sha, bin, l);
147 // h=hash(g, g^r, g^x, name)
148 static void zkpHash(BIGNUM *h, const JPakeZKP *zkp, const BIGNUM *gx,
149 const JPakeUserPublic *from, const JPakeParameters *params)
151 unsigned char md[SHA_DIGEST_LENGTH];
154 // XXX: hash should not allow moving of the boundaries - Java code
155 // is flawed in this respect. Length encoding seems simplest.
157 hashbn(&sha, params->g);
158 hashbn(&sha, zkp->gr);
160 hashstring(&sha, from->name);
161 SHA1_Final(md, &sha);
162 BN_bin2bn(md, SHA_DIGEST_LENGTH, h);
165 // Prove knowledge of x
166 // Note that we don't send g^x because, as it happens, we've always
167 // sent it elsewhere. Also note that because of that, we could avoid
168 // calculating it here, but we don't, for clarity...
169 static void CreateZKP(JPakeZKP *zkp, const BIGNUM *x, const JPakeUser *us,
170 const BIGNUM *zkpg, const JPakeParameters *params,
171 int n, const char *suffix)
173 BIGNUM *r = BN_new();
174 BIGNUM *gx = BN_new();
175 BIGNUM *h = BN_new();
176 BIGNUM *t = BN_new();
179 // XXX: Java chooses r in [0, 2^160) - i.e. distribution not uniform
180 BN_rand_range(r, params->q);
183 BN_mod_exp(zkp->gr, zkpg, r, params->p, params->ctx);
185 BN_mod_exp(gx, zkpg, x, params->p, params->ctx);
188 zkpHash(h, zkp, gx, &us->p, params);
191 BN_mod_mul(t, x, h, params->q, params->ctx);
193 BN_mod_sub(zkp->b, r, t, params->q, params->ctx);
196 printf(" ZKP(x%d%s)\n", n, suffix);
197 showbn(" zkpg", zkpg);
199 showbn(" g^r", zkp->gr);
200 showbn(" b", zkp->b);
209 static int VerifyZKP(const JPakeZKP *zkp, BIGNUM *gx,
210 const JPakeUserPublic *them, const BIGNUM *zkpg,
211 const JPakeParameters *params, int n, const char *suffix)
213 BIGNUM *h = BN_new();
214 BIGNUM *t1 = BN_new();
215 BIGNUM *t2 = BN_new();
216 BIGNUM *t3 = BN_new();
219 zkpHash(h, zkp, gx, them, params);
222 BN_mod_exp(t1, zkpg, zkp->b, params->p, params->ctx);
223 // t2 = (g^x)^h = g^{hx}
224 BN_mod_exp(t2, gx, h, params->p, params->ctx);
225 // t3 = t1 * t2 = g^{hx} * g^b = g^{hx+b} = g^r (allegedly)
226 BN_mod_mul(t3, t1, t2, params->p, params->ctx);
228 printf(" ZKP(x%d%s)\n", n, suffix);
229 showbn(" zkpg", zkpg);
233 if(BN_cmp(t3, zkp->gr) == 0)
250 static void sendstep1_substep(JPakeStep1 *s1, const BIGNUM *x,
252 const JPakeParameters *params, int n)
255 BN_mod_exp(s1->gx, params->g, x, params->p, params->ctx);
256 printf(" g^{x%d}", n);
259 CreateZKP(&s1->zkpx, x, us, params->g, params, n, "");
262 static void sendstep1(const JPakeUser *us, JPakeUserPublic *them,
263 const JPakeParameters *params)
265 printf("\n%s sends %s:\n\n", us->p.name, them->name);
267 // from's g^xa (which becomes to's g^xc) and ZKP(xa)
268 sendstep1_substep(&them->s1c, us->xa, us, params, us->p.base);
269 // from's g^xb (which becomes to's g^xd) and ZKP(xb)
270 sendstep1_substep(&them->s1d, us->xb, us, params, us->p.base+1);
273 static int verifystep1(const JPakeUser *us, const JPakeUserPublic *them,
274 const JPakeParameters *params)
276 printf("\n%s verifies %s:\n\n", us->p.name, them->name);
278 // verify their ZKP(xc)
279 if(!VerifyZKP(&us->p.s1c.zkpx, us->p.s1c.gx, them, params->g, params,
283 // verify their ZKP(xd)
284 if(!VerifyZKP(&us->p.s1d.zkpx, us->p.s1d.gx, them, params->g, params,
289 printf(" g^{x%d} != 1: ", them->base+1);
290 if(BN_is_one(us->p.s1d.gx))
300 static void sendstep2(const JPakeUser *us, JPakeUserPublic *them,
301 const JPakeParameters *params)
303 BIGNUM *t1 = BN_new();
304 BIGNUM *t2 = BN_new();
306 printf("\n%s sends %s:\n\n", us->p.name, them->name);
308 // X = g^{(xa + xc + xd) * xb * s}
310 BN_mod_exp(t1, params->g, us->xa, params->p, params->ctx);
311 // t2 = t1 * g^{xc} = g^{xa} * g^{xc} = g^{xa + xc}
312 BN_mod_mul(t2, t1, us->p.s1c.gx, params->p, params->ctx);
313 // t1 = t2 * g^{xd} = g^{xa + xc + xd}
314 BN_mod_mul(t1, t2, us->p.s1d.gx, params->p, params->ctx);
316 BN_mod_mul(t2, us->xb, us->secret, params->q, params->ctx);
317 // X = t1^{t2} = t1^{xb * s} = g^{(xa + xc + xd) * xb * s}
318 them->s2.X = BN_new();
319 BN_mod_exp(them->s2.X, t1, t2, params->p, params->ctx);
322 printf(" g^{(x%d + x%d + x%d) * x%d * s)", us->p.base, them->base,
323 them->base+1, us->p.base+1);
324 showbn("", them->s2.X);
327 // XXX: this is kinda funky, because we're using
329 // g' = g^{xa + xc + xd}
331 // as the generator, which means X is g'^{xb * s}
332 CreateZKP(&them->s2.zkpxbs, t2, us, t1, params, us->p.base+1, " * s");
339 static int verifystep2(const JPakeUser *us, const JPakeUserPublic *them,
340 const JPakeParameters *params)
342 BIGNUM *t1 = BN_new();
343 BIGNUM *t2 = BN_new();
346 printf("\n%s verifies %s:\n\n", us->p.name, them->name);
348 // g' = g^{xc + xa + xb} [from our POV]
350 BN_mod_add(t1, us->xa, us->xb, params->q, params->ctx);
351 // t2 = g^{t1} = g^{xa+xb}
352 BN_mod_exp(t2, params->g, t1, params->p, params->ctx);
353 // t1 = g^{xc} * t2 = g^{xc + xa + xb}
354 BN_mod_mul(t1, us->p.s1c.gx, t2, params->p, params->ctx);
356 if(VerifyZKP(&us->p.s2.zkpxbs, us->p.s2.X, them, t1, params, them->base+1,
367 static void computekey(JPakeUser *us, const JPakeParameters *params)
369 BIGNUM *t1 = BN_new();
370 BIGNUM *t2 = BN_new();
371 BIGNUM *t3 = BN_new();
373 printf("\n%s calculates the shared key:\n\n", us->p.name);
375 // K = (X/g^{xb * xd * s})^{xb}
376 // = (g^{(xc + xa + xb) * xd * s - xb * xd *s})^{xb}
377 // = (g^{(xa + xc) * xd * s})^{xb}
378 // = g^{(xa + xc) * xb * xd * s}
379 // [which is the same regardless of who calculates it]
381 // t1 = (g^{xd})^{xb} = g^{xb * xd}
382 BN_mod_exp(t1, us->p.s1d.gx, us->xb, params->p, params->ctx);
384 BN_sub(t2, params->q, us->secret);
385 // t3 = t1^t2 = g^{-xb * xd * s}
386 BN_mod_exp(t3, t1, t2, params->p, params->ctx);
387 // t1 = X * t3 = X/g^{xb * xd * s}
388 BN_mod_mul(t1, us->p.s2.X, t3, params->p, params->ctx);
391 BN_mod_exp(us->key, t1, us->xb, params->p, params->ctx);
394 showbn(" K", us->key);
402 int main(int argc, char **argv)
404 JPakeParameters params;
405 JPakeUser alice, bob;
407 alice.p.name = "Alice";
412 JPakeParametersInit(¶ms);
415 alice.secret = BN_new();
416 BN_rand(alice.secret, 32, -1, 0);
417 bob.secret = alice.secret;
418 showbn("secret", alice.secret);
420 assert(BN_cmp(alice.secret, params.q) < 0);
423 genrand(&alice, ¶ms);
426 genrand(&bob, ¶ms);
428 // Now send stuff to each other...
429 sendstep1(&alice, &bob.p, ¶ms);
430 sendstep1(&bob, &alice.p, ¶ms);
432 // And verify what each other sent
433 if(!verifystep1(&alice, &bob.p, ¶ms))
435 if(!verifystep1(&bob, &alice.p, ¶ms))
439 sendstep2(&alice, &bob.p, ¶ms);
440 sendstep2(&bob, &alice.p, ¶ms);
443 if(!verifystep2(&alice, &bob.p, ¶ms))
445 if(!verifystep2(&bob, &alice.p, ¶ms))
448 // Compute common key
449 computekey(&alice, ¶ms);
450 computekey(&bob, ¶ms);
452 // Confirm the common key is identical
453 // XXX: if the two secrets are not the same, everything works up
454 // to this point, so the only way to detect a failure is by the
455 // difference in the calculated keys.
456 // Since we're all the same code, just compare them directly. In a
457 // real system, Alice sends Bob H(H(K)), Bob checks it, then sends
458 // back H(K), which Alice checks, or something equivalent.
459 puts("\nAlice and Bob check keys are the same:");
460 if(BN_cmp(alice.key, bob.key) == 0)