jpake.c 12 KB

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