| 
									
										
										
										
											2014-10-12 02:31:53 +08:00
										 |  |  | #include <openssl/jpake.h>
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | #include <openssl/crypto.h>
 | 
					
						
							|  |  |  | #include <openssl/sha.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <memory.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-05 22:53:51 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * In the definition, (xa, xb, xc, xd) are Alice's (x1, x2, x3, x4) or | 
					
						
							|  |  |  |  * Bob's (x3, x4, x1, x2). If you see what I mean. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     char *name;                 /* Must be unique */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     char *peer_name; | 
					
						
							|  |  |  |     BIGNUM *p; | 
					
						
							|  |  |  |     BIGNUM *g; | 
					
						
							|  |  |  |     BIGNUM *q; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BIGNUM *gxc;                /* Alice's g^{x3} or Bob's g^{x1} */ | 
					
						
							|  |  |  |     BIGNUM *gxd;                /* Alice's g^{x4} or Bob's g^{x2} */ | 
					
						
							|  |  |  | } JPAKE_CTX_PUBLIC; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | struct JPAKE_CTX { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     JPAKE_CTX_PUBLIC p; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BIGNUM *secret;             /* The shared secret */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_CTX *ctx; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BIGNUM *xa;                 /* Alice's x1 or Bob's x3 */ | 
					
						
							|  |  |  |     BIGNUM *xb;                 /* Alice's x2 or Bob's x4 */ | 
					
						
							|  |  |  |     BIGNUM *key;                /* The calculated (shared) key */ | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void JPAKE_ZKP_init(JPAKE_ZKP *zkp) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     zkp->gr = BN_new(); | 
					
						
							|  |  |  |     zkp->b = BN_new(); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void JPAKE_ZKP_release(JPAKE_ZKP *zkp) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(zkp->b); | 
					
						
							|  |  |  |     BN_free(zkp->gr); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  | /* Two birds with one stone - make the global name as expected */ | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | #define JPAKE_STEP_PART_init    JPAKE_STEP2_init
 | 
					
						
							|  |  |  | #define JPAKE_STEP_PART_release JPAKE_STEP2_release
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP_PART_init(JPAKE_STEP_PART *p) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     p->gx = BN_new(); | 
					
						
							|  |  |  |     JPAKE_ZKP_init(&p->zkpx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP_PART_release(JPAKE_STEP_PART *p) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     JPAKE_ZKP_release(&p->zkpx); | 
					
						
							|  |  |  |     BN_free(p->gx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP1_init(JPAKE_STEP1 *s1) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     JPAKE_STEP_PART_init(&s1->p1); | 
					
						
							|  |  |  |     JPAKE_STEP_PART_init(&s1->p2); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP1_release(JPAKE_STEP1 *s1) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     JPAKE_STEP_PART_release(&s1->p2); | 
					
						
							|  |  |  |     JPAKE_STEP_PART_release(&s1->p1); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void JPAKE_CTX_init(JPAKE_CTX *ctx, const char *name, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                            const char *peer_name, const BIGNUM *p, | 
					
						
							|  |  |  |                            const BIGNUM *g, const BIGNUM *q, | 
					
						
							|  |  |  |                            const BIGNUM *secret) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     ctx->p.name = OPENSSL_strdup(name); | 
					
						
							|  |  |  |     ctx->p.peer_name = OPENSSL_strdup(peer_name); | 
					
						
							|  |  |  |     ctx->p.p = BN_dup(p); | 
					
						
							|  |  |  |     ctx->p.g = BN_dup(g); | 
					
						
							|  |  |  |     ctx->p.q = BN_dup(q); | 
					
						
							|  |  |  |     ctx->secret = BN_dup(secret); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->p.gxc = BN_new(); | 
					
						
							|  |  |  |     ctx->p.gxd = BN_new(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->xa = BN_new(); | 
					
						
							|  |  |  |     ctx->xb = BN_new(); | 
					
						
							|  |  |  |     ctx->key = BN_new(); | 
					
						
							|  |  |  |     ctx->ctx = BN_CTX_new(); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | static void JPAKE_CTX_release(JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_CTX_free(ctx->ctx); | 
					
						
							|  |  |  |     BN_clear_free(ctx->key); | 
					
						
							|  |  |  |     BN_clear_free(ctx->xb); | 
					
						
							|  |  |  |     BN_clear_free(ctx->xa); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BN_free(ctx->p.gxd); | 
					
						
							|  |  |  |     BN_free(ctx->p.gxc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BN_clear_free(ctx->secret); | 
					
						
							|  |  |  |     BN_free(ctx->p.q); | 
					
						
							|  |  |  |     BN_free(ctx->p.g); | 
					
						
							|  |  |  |     BN_free(ctx->p.p); | 
					
						
							|  |  |  |     OPENSSL_free(ctx->p.peer_name); | 
					
						
							|  |  |  |     OPENSSL_free(ctx->p.name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-02 11:10:31 +08:00
										 |  |  |     memset(ctx, '\0', sizeof(*ctx)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | JPAKE_CTX *JPAKE_CTX_new(const char *name, const char *peer_name, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                          const BIGNUM *p, const BIGNUM *g, const BIGNUM *q, | 
					
						
							|  |  |  |                          const BIGNUM *secret) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-02 11:10:31 +08:00
										 |  |  |     JPAKE_CTX *ctx = OPENSSL_malloc(sizeof(*ctx)); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:10 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     JPAKE_CTX_init(ctx, name, peer_name, p, g, q, secret); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ctx; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_CTX_free(JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-01 06:10:52 +08:00
										 |  |  |     if (!ctx) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     JPAKE_CTX_release(ctx); | 
					
						
							|  |  |  |     OPENSSL_free(ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void hashlength(SHA_CTX *sha, size_t l) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     unsigned char b[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-02 23:41:30 +08:00
										 |  |  |     OPENSSL_assert(l <= 0xffff); | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     b[0] = l >> 8; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     b[1] = l & 0xff; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     SHA1_Update(sha, b, 2); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void hashstring(SHA_CTX *sha, const char *string) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     size_t l = strlen(string); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hashlength(sha, l); | 
					
						
							|  |  |  |     SHA1_Update(sha, string, l); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  | static int hashbn(SHA_CTX *sha, const BIGNUM *bn) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     size_t l = BN_num_bytes(bn); | 
					
						
							| 
									
										
										
										
											2008-10-27 20:31:13 +08:00
										 |  |  |     unsigned char *bin = OPENSSL_malloc(l); | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (bin == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     hashlength(sha, l); | 
					
						
							|  |  |  |     BN_bn2bin(bn, bin); | 
					
						
							|  |  |  |     SHA1_Update(sha, bin, l); | 
					
						
							| 
									
										
										
										
											2008-10-27 20:31:13 +08:00
										 |  |  |     OPENSSL_free(bin); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  | /* h=hash(g, g^r, g^x, name) */ | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  | static int zkp_hash(BIGNUM *h, const BIGNUM *zkpg, const JPAKE_STEP_PART *p, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                     const char *proof_name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     unsigned char md[SHA_DIGEST_LENGTH]; | 
					
						
							|  |  |  |     SHA_CTX sha; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * XXX: hash should not allow moving of the boundaries - Java code | 
					
						
							|  |  |  |      * is flawed in this respect. Length encoding seems simplest. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     SHA1_Init(&sha); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!hashbn(&sha, zkpg)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-11-02 23:41:30 +08:00
										 |  |  |     OPENSSL_assert(!BN_is_zero(p->zkpx.gr)); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!hashbn(&sha, p->zkpx.gr)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!hashbn(&sha, p->gx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     hashstring(&sha, proof_name); | 
					
						
							|  |  |  |     SHA1_Final(md, &sha); | 
					
						
							|  |  |  |     BN_bin2bn(md, SHA_DIGEST_LENGTH, h); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Prove knowledge of x | 
					
						
							|  |  |  |  * Note that p->gx has already been calculated | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  | static int generate_zkp(JPAKE_STEP_PART *p, const BIGNUM *x, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                         const BIGNUM *zkpg, JPAKE_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     int res = 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BIGNUM *r = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *h = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t = BN_new(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-16 22:43:29 +08:00
										 |  |  |    /*-
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  |     * r in [0,q) | 
					
						
							|  |  |  |     * XXX: Java chooses r in [0, 2^160) - i.e. distribution not uniform | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_rand_range(r, ctx->p.q); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* g^r */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(p->zkpx.gr, zkpg, r, ctx->p.p, ctx->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* h=hash... */ | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!zkp_hash(h, zkpg, p, ctx->p.name)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto end; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* b = r - x*h */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t, x, h, ctx->p.q, ctx->ctx); | 
					
						
							|  |  |  |     BN_mod_sub(p->zkpx.b, r, t, ctx->p.q, ctx->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     res = 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |  end: | 
					
						
							|  |  |  |     /* cleanup */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(t); | 
					
						
							|  |  |  |     BN_free(h); | 
					
						
							|  |  |  |     BN_free(r); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int verify_zkp(const JPAKE_STEP_PART *p, const BIGNUM *zkpg, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                       JPAKE_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BIGNUM *h = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t1 = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t2 = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t3 = BN_new(); | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!zkp_hash(h, zkpg, p, ctx->p.peer_name)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto end; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t1 = g^b */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(t1, zkpg, p->zkpx.b, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t2 = (g^x)^h = g^{hx} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(t2, p->gx, h, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t3 = t1 * t2 = g^{hx} * g^b = g^{hx+b} = g^r (allegedly) */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t3, t1, t2, ctx->p.p, ctx->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* verify t3 == g^r */ | 
					
						
							|  |  |  |     if (BN_cmp(t3, p->zkpx.gr) == 0) | 
					
						
							|  |  |  |         ret = 1; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         JPAKEerr(JPAKE_F_VERIFY_ZKP, JPAKE_R_ZKP_VERIFY_FAILED); | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |  end: | 
					
						
							|  |  |  |     /* cleanup */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(t3); | 
					
						
							|  |  |  |     BN_free(t2); | 
					
						
							|  |  |  |     BN_free(t1); | 
					
						
							|  |  |  |     BN_free(h); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  | static int generate_step_part(JPAKE_STEP_PART *p, const BIGNUM *x, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                               const BIGNUM *g, JPAKE_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(p->gx, g, x, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!generate_zkp(p, x, g, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  | /* Generate each party's random numbers. xa is in [0, q), xb is in [1, q). */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | static void genrand(JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BIGNUM *qm1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* xa in [0, q) */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_rand_range(ctx->xa, ctx->p.q); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* q-1 */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     qm1 = BN_new(); | 
					
						
							|  |  |  |     BN_copy(qm1, ctx->p.q); | 
					
						
							|  |  |  |     BN_sub_word(qm1, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* ... and xb in [0, q-1) */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_rand_range(ctx->xb, qm1); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* [1, q) */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_add_word(ctx->xb, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* cleanup */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(qm1); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP1_generate(JPAKE_STEP1 *send, JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     genrand(ctx); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!generate_step_part(&send->p1, ctx->xa, ctx->p.g, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!generate_step_part(&send->p2, ctx->xb, ctx->p.g, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-30 02:32:05 +08:00
										 |  |  | /* g^x is a legal value */ | 
					
						
							|  |  |  | static int is_legal(const BIGNUM *gx, const JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-30 02:32:05 +08:00
										 |  |  |     BIGNUM *t; | 
					
						
							|  |  |  |     int res; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (BN_is_negative(gx) || BN_is_zero(gx) || BN_cmp(gx, ctx->p.p) >= 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2010-11-30 02:32:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     t = BN_new(); | 
					
						
							|  |  |  |     BN_mod_exp(t, gx, ctx->p.q, ctx->p.p, ctx->ctx); | 
					
						
							|  |  |  |     res = BN_is_one(t); | 
					
						
							|  |  |  |     BN_free(t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-11-30 02:32:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | int JPAKE_STEP1_process(JPAKE_CTX *ctx, const JPAKE_STEP1 *received) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!is_legal(received->p1.gx, ctx)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, | 
					
						
							|  |  |  |                  JPAKE_R_G_TO_THE_X3_IS_NOT_LEGAL); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!is_legal(received->p2.gx, ctx)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, | 
					
						
							|  |  |  |                  JPAKE_R_G_TO_THE_X4_IS_NOT_LEGAL); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* verify their ZKP(xc) */ | 
					
						
							|  |  |  |     if (!verify_zkp(&received->p1, ctx->p.g, ctx)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_VERIFY_X3_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* verify their ZKP(xd) */ | 
					
						
							|  |  |  |     if (!verify_zkp(&received->p2, ctx->p.g, ctx)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_VERIFY_X4_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* g^xd != 1 */ | 
					
						
							|  |  |  |     if (BN_is_one(received->p2.gx)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_G_TO_THE_X4_IS_ONE); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Save the bits we need for later */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_copy(ctx->p.gxc, received->p1.gx); | 
					
						
							|  |  |  |     BN_copy(ctx->p.gxd, received->p2.gx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP2_generate(JPAKE_STEP2 *send, JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BIGNUM *t1 = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t2 = BN_new(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 10:48:40 +08:00
										 |  |  |    /*-
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  |     * X = g^{(xa + xc + xd) * xb * s} | 
					
						
							|  |  |  |     * t1 = g^xa | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(t1, ctx->p.g, ctx->xa, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t2 = t1 * g^{xc} = g^{xa} * g^{xc} = g^{xa + xc} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t2, t1, ctx->p.gxc, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t1 = t2 * g^{xd} = g^{xa + xc + xd} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t1, t2, ctx->p.gxd, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t2 = xb * s */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t2, ctx->xb, ctx->secret, ctx->p.q, ctx->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 10:48:40 +08:00
										 |  |  |    /*-
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  |     * ZKP(xb * s) | 
					
						
							|  |  |  |     * XXX: this is kinda funky, because we're using | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * g' = g^{xa + xc + xd} | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * as the generator, which means X is g'^{xb * s} | 
					
						
							|  |  |  |     * X = t1^{t2} = t1^{xb * s} = g^{(xa + xc + xd) * xb * s} | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     ret = generate_step_part(send, t2, t1, ctx); | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* cleanup */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(t1); | 
					
						
							|  |  |  |     BN_free(t2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  | /* gx = g^{xc + xa + xb} * xd * s */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | static int compute_key(JPAKE_CTX *ctx, const BIGNUM *gx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BIGNUM *t1 = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t2 = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t3 = BN_new(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 10:48:40 +08:00
										 |  |  |    /*-
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  |     * K = (gx/g^{xb * xd * s})^{xb} | 
					
						
							|  |  |  |     *   = (g^{(xc + xa + xb) * xd * s - xb * xd *s})^{xb} | 
					
						
							|  |  |  |     *   = (g^{(xa + xc) * xd * s})^{xb} | 
					
						
							|  |  |  |     *   = g^{(xa + xc) * xb * xd * s} | 
					
						
							|  |  |  |     * [which is the same regardless of who calculates it] | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t1 = (g^{xd})^{xb} = g^{xb * xd} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(t1, ctx->p.gxd, ctx->xb, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t2 = -s = q-s */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_sub(t2, ctx->p.q, ctx->secret); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t3 = t1^t2 = g^{-xb * xd * s} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(t3, t1, t2, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t1 = gx * t3 = X/g^{xb * xd * s} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t1, gx, t3, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* K = t1^{xb} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(ctx->key, t1, ctx->xb, ctx->p.p, ctx->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* cleanup */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(t3); | 
					
						
							|  |  |  |     BN_free(t2); | 
					
						
							|  |  |  |     BN_free(t1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP2_process(JPAKE_CTX *ctx, const JPAKE_STEP2 *received) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BIGNUM *t1 = BN_new(); | 
					
						
							|  |  |  |     BIGNUM *t2 = BN_new(); | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 10:48:40 +08:00
										 |  |  |    /*-
 | 
					
						
							| 
									
										
										
										
											2008-10-27 20:02:52 +08:00
										 |  |  |     * g' = g^{xc + xa + xb} [from our POV] | 
					
						
							|  |  |  |     * t1 = xa + xb | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_add(t1, ctx->xa, ctx->xb, ctx->p.q, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t2 = g^{t1} = g^{xa+xb} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_exp(t2, ctx->p.g, t1, ctx->p.p, ctx->ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* t1 = g^{xc} * t2 = g^{xc + xa + xb} */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_mod_mul(t1, ctx->p.gxc, t2, ctx->p.p, ctx->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (verify_zkp(received, t1, ctx)) | 
					
						
							|  |  |  |         ret = 1; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP2_PROCESS, JPAKE_R_VERIFY_B_FAILED); | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     compute_key(ctx, received->gx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* cleanup */ | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     BN_free(t2); | 
					
						
							|  |  |  |     BN_free(t1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  | static int quickhashbn(unsigned char *md, const BIGNUM *bn) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     SHA_CTX sha; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SHA1_Init(&sha); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!hashbn(&sha, bn)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     SHA1_Final(md, &sha); | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP3A_init(JPAKE_STEP3A *s3a) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP3A_generate(JPAKE_STEP3A *send, JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!quickhashbn(send->hhk, ctx->key)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     SHA1(send->hhk, sizeof send->hhk, send->hhk); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP3A_process(JPAKE_CTX *ctx, const JPAKE_STEP3A *received) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     unsigned char hhk[SHA_DIGEST_LENGTH]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!quickhashbn(hhk, ctx->key)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     SHA1(hhk, sizeof hhk, hhk); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (memcmp(hhk, received->hhk, sizeof hhk)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP3A_PROCESS, | 
					
						
							|  |  |  |                  JPAKE_R_HASH_OF_HASH_OF_KEY_MISMATCH); | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP3A_release(JPAKE_STEP3A *s3a) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP3B_init(JPAKE_STEP3B *s3b) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP3B_generate(JPAKE_STEP3B *send, JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!quickhashbn(send->hk, ctx->key)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int JPAKE_STEP3B_process(JPAKE_CTX *ctx, const JPAKE_STEP3B *received) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     unsigned char hk[SHA_DIGEST_LENGTH]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 01:14:59 +08:00
										 |  |  |     if (!quickhashbn(hk, ctx->key)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     if (memcmp(hk, received->hk, sizeof hk)) { | 
					
						
							|  |  |  |         JPAKEerr(JPAKE_F_JPAKE_STEP3B_PROCESS, JPAKE_R_HASH_OF_KEY_MISMATCH); | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void JPAKE_STEP3B_release(JPAKE_STEP3B *s3b) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const BIGNUM *JPAKE_get_shared_key(JPAKE_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-27 02:40:52 +08:00
										 |  |  |     return ctx->key; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } |