3 #include <openssl/crypto.h>
4 #include <openssl/sha.h>
5 #include <openssl/err.h>
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.
15 char *name
; /* Must be unique */
20 BIGNUM
*gxc
; /* Alice's g^{x3} or Bob's g^{x1} */
21 BIGNUM
*gxd
; /* Alice's g^{x4} or Bob's g^{x2} */
27 BIGNUM
*secret
; /* The shared secret */
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 */
34 static void JPAKE_ZKP_init(JPAKE_ZKP
*zkp
)
40 static void JPAKE_ZKP_release(JPAKE_ZKP
*zkp
)
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
50 void JPAKE_STEP_PART_init(JPAKE_STEP_PART
*p
)
53 JPAKE_ZKP_init(&p
->zkpx
);
56 void JPAKE_STEP_PART_release(JPAKE_STEP_PART
*p
)
58 JPAKE_ZKP_release(&p
->zkpx
);
62 void JPAKE_STEP1_init(JPAKE_STEP1
*s1
)
64 JPAKE_STEP_PART_init(&s1
->p1
);
65 JPAKE_STEP_PART_init(&s1
->p2
);
68 void JPAKE_STEP1_release(JPAKE_STEP1
*s1
)
70 JPAKE_STEP_PART_release(&s1
->p2
);
71 JPAKE_STEP_PART_release(&s1
->p1
);
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
,
79 ctx
->p
.name
= OPENSSL_strdup(name
);
80 ctx
->p
.peer_name
= OPENSSL_strdup(peer_name
);
84 ctx
->secret
= BN_dup(secret
);
86 ctx
->p
.gxc
= BN_new();
87 ctx
->p
.gxd
= BN_new();
92 ctx
->ctx
= BN_CTX_new();
95 static void JPAKE_CTX_release(JPAKE_CTX
*ctx
)
97 BN_CTX_free(ctx
->ctx
);
98 BN_clear_free(ctx
->key
);
99 BN_clear_free(ctx
->xb
);
100 BN_clear_free(ctx
->xa
);
105 BN_clear_free(ctx
->secret
);
109 OPENSSL_free(ctx
->p
.peer_name
);
110 OPENSSL_free(ctx
->p
.name
);
112 memset(ctx
, '\0', sizeof *ctx
);
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
)
119 JPAKE_CTX
*ctx
= OPENSSL_malloc(sizeof *ctx
);
121 JPAKE_CTX_init(ctx
, name
, peer_name
, p
, g
, q
, secret
);
126 void JPAKE_CTX_free(JPAKE_CTX
*ctx
)
128 JPAKE_CTX_release(ctx
);
132 static void hashlength(SHA_CTX
*sha
, size_t l
)
136 OPENSSL_assert(l
<= 0xffff);
139 SHA1_Update(sha
, b
, 2);
142 static void hashstring(SHA_CTX
*sha
, const char *string
)
144 size_t l
= strlen(string
);
147 SHA1_Update(sha
, string
, l
);
150 static void hashbn(SHA_CTX
*sha
, const BIGNUM
*bn
)
152 size_t l
= BN_num_bytes(bn
);
153 unsigned char *bin
= OPENSSL_malloc(l
);
157 SHA1_Update(sha
, bin
, l
);
161 /* h=hash(g, g^r, g^x, name) */
162 static void zkp_hash(BIGNUM
*h
, const BIGNUM
*zkpg
, const JPAKE_STEP_PART
*p
,
163 const char *proof_name
)
165 unsigned char md
[SHA_DIGEST_LENGTH
];
169 * XXX: hash should not allow moving of the boundaries - Java code
170 * is flawed in this respect. Length encoding seems simplest.
174 OPENSSL_assert(!BN_is_zero(p
->zkpx
.gr
));
175 hashbn(&sha
, p
->zkpx
.gr
);
177 hashstring(&sha
, proof_name
);
178 SHA1_Final(md
, &sha
);
179 BN_bin2bn(md
, SHA_DIGEST_LENGTH
, h
);
183 * Prove knowledge of x
184 * Note that p->gx has already been calculated
186 static void generate_zkp(JPAKE_STEP_PART
*p
, const BIGNUM
*x
,
187 const BIGNUM
*zkpg
, JPAKE_CTX
*ctx
)
189 BIGNUM
*r
= BN_new();
190 BIGNUM
*h
= BN_new();
191 BIGNUM
*t
= BN_new();
195 * XXX: Java chooses r in [0, 2^160) - i.e. distribution not uniform
197 BN_rand_range(r
, ctx
->p
.q
);
199 BN_mod_exp(p
->zkpx
.gr
, zkpg
, r
, ctx
->p
.p
, ctx
->ctx
);
202 zkp_hash(h
, zkpg
, p
, ctx
->p
.name
);
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
);
214 static int verify_zkp(const JPAKE_STEP_PART
*p
, const BIGNUM
*zkpg
,
217 BIGNUM
*h
= BN_new();
218 BIGNUM
*t1
= BN_new();
219 BIGNUM
*t2
= BN_new();
220 BIGNUM
*t3
= BN_new();
223 zkp_hash(h
, zkpg
, p
, ctx
->p
.peer_name
);
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
);
232 /* verify t3 == g^r */
233 if(BN_cmp(t3
, p
->zkpx
.gr
) == 0)
236 JPAKEerr(JPAKE_F_VERIFY_ZKP
, JPAKE_R_ZKP_VERIFY_FAILED
);
247 static void generate_step_part(JPAKE_STEP_PART
*p
, const BIGNUM
*x
,
248 const BIGNUM
*g
, JPAKE_CTX
*ctx
)
250 BN_mod_exp(p
->gx
, g
, x
, ctx
->p
.p
, ctx
->ctx
);
251 generate_zkp(p
, x
, g
, ctx
);
254 /* Generate each party's random numbers. xa is in [0, q), xb is in [1, q). */
255 static void genrand(JPAKE_CTX
*ctx
)
260 BN_rand_range(ctx
->xa
, ctx
->p
.q
);
264 BN_copy(qm1
, ctx
->p
.q
);
267 /* ... and xb in [0, q-1) */
268 BN_rand_range(ctx
->xb
, qm1
);
270 BN_add_word(ctx
->xb
, 1);
276 int JPAKE_STEP1_generate(JPAKE_STEP1
*send
, JPAKE_CTX
*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
);
285 /* g^x is a legal value */
286 static int is_legal(const BIGNUM
*gx
, const JPAKE_CTX
*ctx
)
291 if(BN_is_negative(gx
) || BN_is_zero(gx
) || BN_cmp(gx
, ctx
->p
.p
) >= 0)
295 BN_mod_exp(t
, gx
, ctx
->p
.q
, ctx
->p
.p
, ctx
->ctx
);
302 int JPAKE_STEP1_process(JPAKE_CTX
*ctx
, const JPAKE_STEP1
*received
)
304 if(!is_legal(received
->p1
.gx
, ctx
))
306 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS
, JPAKE_R_G_TO_THE_X3_IS_NOT_LEGAL
);
310 if(!is_legal(received
->p2
.gx
, ctx
))
312 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS
, JPAKE_R_G_TO_THE_X4_IS_NOT_LEGAL
);
316 /* verify their ZKP(xc) */
317 if(!verify_zkp(&received
->p1
, ctx
->p
.g
, ctx
))
319 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS
, JPAKE_R_VERIFY_X3_FAILED
);
323 /* verify their ZKP(xd) */
324 if(!verify_zkp(&received
->p2
, ctx
->p
.g
, ctx
))
326 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS
, JPAKE_R_VERIFY_X4_FAILED
);
331 if(BN_is_one(received
->p2
.gx
))
333 JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS
, JPAKE_R_G_TO_THE_X4_IS_ONE
);
337 /* Save the bits we need for later */
338 BN_copy(ctx
->p
.gxc
, received
->p1
.gx
);
339 BN_copy(ctx
->p
.gxd
, received
->p2
.gx
);
345 int JPAKE_STEP2_generate(JPAKE_STEP2
*send
, JPAKE_CTX
*ctx
)
347 BIGNUM
*t1
= BN_new();
348 BIGNUM
*t2
= BN_new();
351 * X = g^{(xa + xc + xd) * xb * s}
354 BN_mod_exp(t1
, ctx
->p
.g
, ctx
->xa
, ctx
->p
.p
, ctx
->ctx
);
355 /* t2 = t1 * g^{xc} = g^{xa} * g^{xc} = g^{xa + xc} */
356 BN_mod_mul(t2
, t1
, ctx
->p
.gxc
, ctx
->p
.p
, ctx
->ctx
);
357 /* t1 = t2 * g^{xd} = g^{xa + xc + xd} */
358 BN_mod_mul(t1
, t2
, ctx
->p
.gxd
, ctx
->p
.p
, ctx
->ctx
);
360 BN_mod_mul(t2
, ctx
->xb
, ctx
->secret
, ctx
->p
.q
, ctx
->ctx
);
364 * XXX: this is kinda funky, because we're using
366 * g' = g^{xa + xc + xd}
368 * as the generator, which means X is g'^{xb * s}
369 * X = t1^{t2} = t1^{xb * s} = g^{(xa + xc + xd) * xb * s}
371 generate_step_part(send
, t2
, t1
, ctx
);
380 /* gx = g^{xc + xa + xb} * xd * s */
381 static int compute_key(JPAKE_CTX
*ctx
, const BIGNUM
*gx
)
383 BIGNUM
*t1
= BN_new();
384 BIGNUM
*t2
= BN_new();
385 BIGNUM
*t3
= BN_new();
388 * K = (gx/g^{xb * xd * s})^{xb}
389 * = (g^{(xc + xa + xb) * xd * s - xb * xd *s})^{xb}
390 * = (g^{(xa + xc) * xd * s})^{xb}
391 * = g^{(xa + xc) * xb * xd * s}
392 * [which is the same regardless of who calculates it]
395 /* t1 = (g^{xd})^{xb} = g^{xb * xd} */
396 BN_mod_exp(t1
, ctx
->p
.gxd
, ctx
->xb
, ctx
->p
.p
, ctx
->ctx
);
398 BN_sub(t2
, ctx
->p
.q
, ctx
->secret
);
399 /* t3 = t1^t2 = g^{-xb * xd * s} */
400 BN_mod_exp(t3
, t1
, t2
, ctx
->p
.p
, ctx
->ctx
);
401 /* t1 = gx * t3 = X/g^{xb * xd * s} */
402 BN_mod_mul(t1
, gx
, t3
, ctx
->p
.p
, ctx
->ctx
);
404 BN_mod_exp(ctx
->key
, t1
, ctx
->xb
, ctx
->p
.p
, ctx
->ctx
);
414 int JPAKE_STEP2_process(JPAKE_CTX
*ctx
, const JPAKE_STEP2
*received
)
416 BIGNUM
*t1
= BN_new();
417 BIGNUM
*t2
= BN_new();
421 * g' = g^{xc + xa + xb} [from our POV]
424 BN_mod_add(t1
, ctx
->xa
, ctx
->xb
, ctx
->p
.q
, ctx
->ctx
);
425 /* t2 = g^{t1} = g^{xa+xb} */
426 BN_mod_exp(t2
, ctx
->p
.g
, t1
, ctx
->p
.p
, ctx
->ctx
);
427 /* t1 = g^{xc} * t2 = g^{xc + xa + xb} */
428 BN_mod_mul(t1
, ctx
->p
.gxc
, t2
, ctx
->p
.p
, ctx
->ctx
);
430 if(verify_zkp(received
, t1
, ctx
))
433 JPAKEerr(JPAKE_F_JPAKE_STEP2_PROCESS
, JPAKE_R_VERIFY_B_FAILED
);
435 compute_key(ctx
, received
->gx
);
444 static void quickhashbn(unsigned char *md
, const BIGNUM
*bn
)
450 SHA1_Final(md
, &sha
);
453 void JPAKE_STEP3A_init(JPAKE_STEP3A
*s3a
)
456 int JPAKE_STEP3A_generate(JPAKE_STEP3A
*send
, JPAKE_CTX
*ctx
)
458 quickhashbn(send
->hhk
, ctx
->key
);
459 SHA1(send
->hhk
, sizeof send
->hhk
, send
->hhk
);
464 int JPAKE_STEP3A_process(JPAKE_CTX
*ctx
, const JPAKE_STEP3A
*received
)
466 unsigned char hhk
[SHA_DIGEST_LENGTH
];
468 quickhashbn(hhk
, ctx
->key
);
469 SHA1(hhk
, sizeof hhk
, hhk
);
470 if(memcmp(hhk
, received
->hhk
, sizeof hhk
))
472 JPAKEerr(JPAKE_F_JPAKE_STEP3A_PROCESS
, JPAKE_R_HASH_OF_HASH_OF_KEY_MISMATCH
);
478 void JPAKE_STEP3A_release(JPAKE_STEP3A
*s3a
)
481 void JPAKE_STEP3B_init(JPAKE_STEP3B
*s3b
)
484 int JPAKE_STEP3B_generate(JPAKE_STEP3B
*send
, JPAKE_CTX
*ctx
)
486 quickhashbn(send
->hk
, ctx
->key
);
491 int JPAKE_STEP3B_process(JPAKE_CTX
*ctx
, const JPAKE_STEP3B
*received
)
493 unsigned char hk
[SHA_DIGEST_LENGTH
];
495 quickhashbn(hk
, ctx
->key
);
496 if(memcmp(hk
, received
->hk
, sizeof hk
))
498 JPAKEerr(JPAKE_F_JPAKE_STEP3B_PROCESS
, JPAKE_R_HASH_OF_KEY_MISMATCH
);
504 void JPAKE_STEP3B_release(JPAKE_STEP3B
*s3b
)
507 const BIGNUM
*JPAKE_get_shared_key(JPAKE_CTX
*ctx
)