jpake.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include "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. JPAKE_CTX_release(ctx);
  108. OPENSSL_free(ctx);
  109. }
  110. static void hashlength(SHA_CTX *sha, size_t l)
  111. {
  112. unsigned char b[2];
  113. OPENSSL_assert(l <= 0xffff);
  114. b[0] = l >> 8;
  115. b[1] = l & 0xff;
  116. SHA1_Update(sha, b, 2);
  117. }
  118. static void hashstring(SHA_CTX *sha, const char *string)
  119. {
  120. size_t l = strlen(string);
  121. hashlength(sha, l);
  122. SHA1_Update(sha, string, l);
  123. }
  124. static void hashbn(SHA_CTX *sha, const BIGNUM *bn)
  125. {
  126. size_t l = BN_num_bytes(bn);
  127. unsigned char *bin = OPENSSL_malloc(l);
  128. if (bin == NULL)
  129. return;
  130. hashlength(sha, l);
  131. BN_bn2bin(bn, bin);
  132. SHA1_Update(sha, bin, l);
  133. OPENSSL_free(bin);
  134. }
  135. /* h=hash(g, g^r, g^x, name) */
  136. static void zkp_hash(BIGNUM *h, const BIGNUM *zkpg, const JPAKE_STEP_PART *p,
  137. const char *proof_name)
  138. {
  139. unsigned char md[SHA_DIGEST_LENGTH];
  140. SHA_CTX sha;
  141. /*
  142. * XXX: hash should not allow moving of the boundaries - Java code
  143. * is flawed in this respect. Length encoding seems simplest.
  144. */
  145. SHA1_Init(&sha);
  146. hashbn(&sha, zkpg);
  147. OPENSSL_assert(!BN_is_zero(p->zkpx.gr));
  148. hashbn(&sha, p->zkpx.gr);
  149. hashbn(&sha, p->gx);
  150. hashstring(&sha, proof_name);
  151. SHA1_Final(md, &sha);
  152. BN_bin2bn(md, SHA_DIGEST_LENGTH, h);
  153. }
  154. /*
  155. * Prove knowledge of x
  156. * Note that p->gx has already been calculated
  157. */
  158. static void generate_zkp(JPAKE_STEP_PART *p, const BIGNUM *x,
  159. const BIGNUM *zkpg, JPAKE_CTX *ctx)
  160. {
  161. BIGNUM *r = BN_new();
  162. BIGNUM *h = BN_new();
  163. BIGNUM *t = BN_new();
  164. /*-
  165. * r in [0,q)
  166. * XXX: Java chooses r in [0, 2^160) - i.e. distribution not uniform
  167. */
  168. BN_rand_range(r, ctx->p.q);
  169. /* g^r */
  170. BN_mod_exp(p->zkpx.gr, zkpg, r, ctx->p.p, ctx->ctx);
  171. /* h=hash... */
  172. zkp_hash(h, zkpg, p, ctx->p.name);
  173. /* b = r - x*h */
  174. BN_mod_mul(t, x, h, ctx->p.q, ctx->ctx);
  175. BN_mod_sub(p->zkpx.b, r, t, ctx->p.q, ctx->ctx);
  176. /* cleanup */
  177. BN_free(t);
  178. BN_free(h);
  179. BN_free(r);
  180. }
  181. static int verify_zkp(const JPAKE_STEP_PART *p, const BIGNUM *zkpg,
  182. JPAKE_CTX *ctx)
  183. {
  184. BIGNUM *h = BN_new();
  185. BIGNUM *t1 = BN_new();
  186. BIGNUM *t2 = BN_new();
  187. BIGNUM *t3 = BN_new();
  188. int ret = 0;
  189. if (h == NULL || t1 == NULL || t2 == NULL || t3 == NULL)
  190. goto end;
  191. zkp_hash(h, zkpg, p, ctx->p.peer_name);
  192. /* t1 = g^b */
  193. BN_mod_exp(t1, zkpg, p->zkpx.b, ctx->p.p, ctx->ctx);
  194. /* t2 = (g^x)^h = g^{hx} */
  195. BN_mod_exp(t2, p->gx, h, ctx->p.p, ctx->ctx);
  196. /* t3 = t1 * t2 = g^{hx} * g^b = g^{hx+b} = g^r (allegedly) */
  197. BN_mod_mul(t3, t1, t2, ctx->p.p, ctx->ctx);
  198. /* verify t3 == g^r */
  199. if (BN_cmp(t3, p->zkpx.gr) == 0)
  200. ret = 1;
  201. else
  202. JPAKEerr(JPAKE_F_VERIFY_ZKP, JPAKE_R_ZKP_VERIFY_FAILED);
  203. end:
  204. /* cleanup */
  205. BN_free(t3);
  206. BN_free(t2);
  207. BN_free(t1);
  208. BN_free(h);
  209. return ret;
  210. }
  211. static void generate_step_part(JPAKE_STEP_PART *p, const BIGNUM *x,
  212. const BIGNUM *g, JPAKE_CTX *ctx)
  213. {
  214. BN_mod_exp(p->gx, g, x, ctx->p.p, ctx->ctx);
  215. generate_zkp(p, x, g, ctx);
  216. }
  217. /* Generate each party's random numbers. xa is in [0, q), xb is in [1, q). */
  218. static void genrand(JPAKE_CTX *ctx)
  219. {
  220. BIGNUM *qm1;
  221. /* xa in [0, q) */
  222. BN_rand_range(ctx->xa, ctx->p.q);
  223. /* q-1 */
  224. qm1 = BN_new();
  225. BN_copy(qm1, ctx->p.q);
  226. BN_sub_word(qm1, 1);
  227. /* ... and xb in [0, q-1) */
  228. BN_rand_range(ctx->xb, qm1);
  229. /* [1, q) */
  230. BN_add_word(ctx->xb, 1);
  231. /* cleanup */
  232. BN_free(qm1);
  233. }
  234. int JPAKE_STEP1_generate(JPAKE_STEP1 *send, JPAKE_CTX *ctx)
  235. {
  236. genrand(ctx);
  237. generate_step_part(&send->p1, ctx->xa, ctx->p.g, ctx);
  238. generate_step_part(&send->p2, ctx->xb, ctx->p.g, ctx);
  239. return 1;
  240. }
  241. /* g^x is a legal value */
  242. static int is_legal(const BIGNUM *gx, const JPAKE_CTX *ctx)
  243. {
  244. BIGNUM *t;
  245. int res;
  246. if (BN_is_negative(gx) || BN_is_zero(gx) || BN_cmp(gx, ctx->p.p) >= 0)
  247. return 0;
  248. t = BN_new();
  249. BN_mod_exp(t, gx, ctx->p.q, ctx->p.p, ctx->ctx);
  250. res = BN_is_one(t);
  251. BN_free(t);
  252. return res;
  253. }
  254. int JPAKE_STEP1_process(JPAKE_CTX *ctx, const JPAKE_STEP1 *received)
  255. {
  256. if (!is_legal(received->p1.gx, ctx)) {
  257. JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS,
  258. JPAKE_R_G_TO_THE_X3_IS_NOT_LEGAL);
  259. return 0;
  260. }
  261. if (!is_legal(received->p2.gx, ctx)) {
  262. JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS,
  263. JPAKE_R_G_TO_THE_X4_IS_NOT_LEGAL);
  264. return 0;
  265. }
  266. /* verify their ZKP(xc) */
  267. if (!verify_zkp(&received->p1, ctx->p.g, ctx)) {
  268. JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_VERIFY_X3_FAILED);
  269. return 0;
  270. }
  271. /* verify their ZKP(xd) */
  272. if (!verify_zkp(&received->p2, ctx->p.g, ctx)) {
  273. JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_VERIFY_X4_FAILED);
  274. return 0;
  275. }
  276. /* g^xd != 1 */
  277. if (BN_is_one(received->p2.gx)) {
  278. JPAKEerr(JPAKE_F_JPAKE_STEP1_PROCESS, JPAKE_R_G_TO_THE_X4_IS_ONE);
  279. return 0;
  280. }
  281. /* Save the bits we need for later */
  282. BN_copy(ctx->p.gxc, received->p1.gx);
  283. BN_copy(ctx->p.gxd, received->p2.gx);
  284. return 1;
  285. }
  286. int JPAKE_STEP2_generate(JPAKE_STEP2 *send, JPAKE_CTX *ctx)
  287. {
  288. BIGNUM *t1 = BN_new();
  289. BIGNUM *t2 = BN_new();
  290. /*-
  291. * X = g^{(xa + xc + xd) * xb * s}
  292. * t1 = g^xa
  293. */
  294. BN_mod_exp(t1, ctx->p.g, ctx->xa, ctx->p.p, ctx->ctx);
  295. /* t2 = t1 * g^{xc} = g^{xa} * g^{xc} = g^{xa + xc} */
  296. BN_mod_mul(t2, t1, ctx->p.gxc, ctx->p.p, ctx->ctx);
  297. /* t1 = t2 * g^{xd} = g^{xa + xc + xd} */
  298. BN_mod_mul(t1, t2, ctx->p.gxd, ctx->p.p, ctx->ctx);
  299. /* t2 = xb * s */
  300. BN_mod_mul(t2, ctx->xb, ctx->secret, ctx->p.q, ctx->ctx);
  301. /*-
  302. * ZKP(xb * s)
  303. * XXX: this is kinda funky, because we're using
  304. *
  305. * g' = g^{xa + xc + xd}
  306. *
  307. * as the generator, which means X is g'^{xb * s}
  308. * X = t1^{t2} = t1^{xb * s} = g^{(xa + xc + xd) * xb * s}
  309. */
  310. generate_step_part(send, t2, t1, ctx);
  311. /* cleanup */
  312. BN_free(t1);
  313. BN_free(t2);
  314. return 1;
  315. }
  316. /* gx = g^{xc + xa + xb} * xd * s */
  317. static int compute_key(JPAKE_CTX *ctx, const BIGNUM *gx)
  318. {
  319. BIGNUM *t1 = BN_new();
  320. BIGNUM *t2 = BN_new();
  321. BIGNUM *t3 = BN_new();
  322. /*-
  323. * K = (gx/g^{xb * xd * s})^{xb}
  324. * = (g^{(xc + xa + xb) * xd * s - xb * xd *s})^{xb}
  325. * = (g^{(xa + xc) * xd * s})^{xb}
  326. * = g^{(xa + xc) * xb * xd * s}
  327. * [which is the same regardless of who calculates it]
  328. */
  329. /* t1 = (g^{xd})^{xb} = g^{xb * xd} */
  330. BN_mod_exp(t1, ctx->p.gxd, ctx->xb, ctx->p.p, ctx->ctx);
  331. /* t2 = -s = q-s */
  332. BN_sub(t2, ctx->p.q, ctx->secret);
  333. /* t3 = t1^t2 = g^{-xb * xd * s} */
  334. BN_mod_exp(t3, t1, t2, ctx->p.p, ctx->ctx);
  335. /* t1 = gx * t3 = X/g^{xb * xd * s} */
  336. BN_mod_mul(t1, gx, t3, ctx->p.p, ctx->ctx);
  337. /* K = t1^{xb} */
  338. BN_mod_exp(ctx->key, t1, ctx->xb, ctx->p.p, ctx->ctx);
  339. /* cleanup */
  340. BN_free(t3);
  341. BN_free(t2);
  342. BN_free(t1);
  343. return 1;
  344. }
  345. int JPAKE_STEP2_process(JPAKE_CTX *ctx, const JPAKE_STEP2 *received)
  346. {
  347. BIGNUM *t1 = BN_new();
  348. BIGNUM *t2 = BN_new();
  349. int ret = 0;
  350. /*-
  351. * g' = g^{xc + xa + xb} [from our POV]
  352. * t1 = xa + xb
  353. */
  354. BN_mod_add(t1, ctx->xa, ctx->xb, ctx->p.q, ctx->ctx);
  355. /* t2 = g^{t1} = g^{xa+xb} */
  356. BN_mod_exp(t2, ctx->p.g, t1, ctx->p.p, ctx->ctx);
  357. /* t1 = g^{xc} * t2 = g^{xc + xa + xb} */
  358. BN_mod_mul(t1, ctx->p.gxc, t2, ctx->p.p, ctx->ctx);
  359. if (verify_zkp(received, t1, ctx))
  360. ret = 1;
  361. else
  362. JPAKEerr(JPAKE_F_JPAKE_STEP2_PROCESS, JPAKE_R_VERIFY_B_FAILED);
  363. compute_key(ctx, received->gx);
  364. /* cleanup */
  365. BN_free(t2);
  366. BN_free(t1);
  367. return ret;
  368. }
  369. static void quickhashbn(unsigned char *md, const BIGNUM *bn)
  370. {
  371. SHA_CTX sha;
  372. SHA1_Init(&sha);
  373. hashbn(&sha, bn);
  374. SHA1_Final(md, &sha);
  375. }
  376. void JPAKE_STEP3A_init(JPAKE_STEP3A *s3a)
  377. {
  378. }
  379. int JPAKE_STEP3A_generate(JPAKE_STEP3A *send, JPAKE_CTX *ctx)
  380. {
  381. quickhashbn(send->hhk, ctx->key);
  382. SHA1(send->hhk, sizeof send->hhk, send->hhk);
  383. return 1;
  384. }
  385. int JPAKE_STEP3A_process(JPAKE_CTX *ctx, const JPAKE_STEP3A *received)
  386. {
  387. unsigned char hhk[SHA_DIGEST_LENGTH];
  388. quickhashbn(hhk, ctx->key);
  389. SHA1(hhk, sizeof hhk, hhk);
  390. if (memcmp(hhk, received->hhk, sizeof hhk)) {
  391. JPAKEerr(JPAKE_F_JPAKE_STEP3A_PROCESS,
  392. JPAKE_R_HASH_OF_HASH_OF_KEY_MISMATCH);
  393. return 0;
  394. }
  395. return 1;
  396. }
  397. void JPAKE_STEP3A_release(JPAKE_STEP3A *s3a)
  398. {
  399. }
  400. void JPAKE_STEP3B_init(JPAKE_STEP3B *s3b)
  401. {
  402. }
  403. int JPAKE_STEP3B_generate(JPAKE_STEP3B *send, JPAKE_CTX *ctx)
  404. {
  405. quickhashbn(send->hk, ctx->key);
  406. return 1;
  407. }
  408. int JPAKE_STEP3B_process(JPAKE_CTX *ctx, const JPAKE_STEP3B *received)
  409. {
  410. unsigned char hk[SHA_DIGEST_LENGTH];
  411. quickhashbn(hk, ctx->key);
  412. if (memcmp(hk, received->hk, sizeof hk)) {
  413. JPAKEerr(JPAKE_F_JPAKE_STEP3B_PROCESS, JPAKE_R_HASH_OF_KEY_MISMATCH);
  414. return 0;
  415. }
  416. return 1;
  417. }
  418. void JPAKE_STEP3B_release(JPAKE_STEP3B *s3b)
  419. {
  420. }
  421. const BIGNUM *JPAKE_get_shared_key(JPAKE_CTX *ctx)
  422. {
  423. return ctx->key;
  424. }