/* Shared parts of step 1 exchange calculation */ void jpake_step1(struct modp_group *grp, u_char **id, u_int *id_len, BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2, u_char **priv1_proof, u_int *priv1_proof_len, u_char **priv2_proof, u_int *priv2_proof_len) { BN_CTX *bn_ctx; if ((bn_ctx = BN_CTX_new()) == NULL) fatal("%s: BN_CTX_new", __func__); /* Random nonce to prevent replay */ *id = xmalloc(KZP_ID_LEN); *id_len = KZP_ID_LEN; arc4random_buf(*id, *id_len); /* * x1/x3 is a random element of Zq * x2/x4 is a random element of Z*q * We also exclude [1] from x1/x3 candidates and [0, 1] from * x2/x4 candiates to avoid possible degeneracy (i.e. g^0, g^1). */ if ((*priv1 = bn_rand_range_gt_one(grp->q)) == NULL || (*priv2 = bn_rand_range_gt_one(grp->q)) == NULL) fatal("%s: bn_rand_range_gt_one", __func__); /* * client: g_x1 = g^x1 mod p / server: g_x3 = g^x3 mod p * client: g_x2 = g^x2 mod p / server: g_x4 = g^x4 mod p */ if ((*g_priv1 = BN_new()) == NULL || (*g_priv2 = BN_new()) == NULL) fatal("%s: BN_new", __func__); if (BN_mod_exp(*g_priv1, grp->g, *priv1, grp->p, bn_ctx) == -1) fatal("%s: BN_mod_exp", __func__); if (BN_mod_exp(*g_priv2, grp->g, *priv2, grp->p, bn_ctx) == -1) fatal("%s: BN_mod_exp", __func__); /* Generate proofs for holding x1/x3 and x2/x4 */ if (schnorr_sign_buf(grp->p, grp->q, grp->g, *priv1, *g_priv1, *id, *id_len, priv1_proof, priv1_proof_len) != 0) fatal("%s: schnorr_sign", __func__); if (schnorr_sign_buf(grp->p, grp->q, grp->g, *priv2, *g_priv2, *id, *id_len, priv2_proof, priv2_proof_len) != 0) fatal("%s: schnorr_sign", __func__); BN_CTX_free(bn_ctx); }
static void schnorr_selftest_one(const BIGNUM *grp_p, const BIGNUM *grp_q, const BIGNUM *grp_g, const BIGNUM *x) { BIGNUM *g_x; u_char *sig; u_int siglen; BN_CTX *bn_ctx; if ((bn_ctx = BN_CTX_new()) == NULL) fatal("%s: BN_CTX_new", __func__); if ((g_x = BN_new()) == NULL) fatal("%s: BN_new", __func__); if (BN_mod_exp(g_x, grp_g, x, grp_p, bn_ctx) == -1) fatal("%s: g_x", __func__); if (schnorr_sign_buf(grp_p, grp_q, grp_g, x, g_x, "junk", 4, &sig, &siglen)) fatal("%s: schnorr_sign", __func__); if (schnorr_verify_buf(grp_p, grp_q, grp_g, g_x, "junk", 4, sig, siglen) != 1) fatal("%s: verify fail", __func__); if (schnorr_verify_buf(grp_p, grp_q, grp_g, g_x, "JUNK", 4, sig, siglen) != 0) fatal("%s: verify should have failed (bad ID)", __func__); sig[4] ^= 1; if (schnorr_verify_buf(grp_p, grp_q, grp_g, g_x, "junk", 4, sig, siglen) != 0) fatal("%s: verify should have failed (bit error)", __func__); free(sig); BN_free(g_x); BN_CTX_free(bn_ctx); }
/* Shared parts of step 2 exchange calculation */ void jpake_step2(struct modp_group *grp, BIGNUM *s, BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2, const u_char *theirid, u_int theirid_len, const u_char *myid, u_int myid_len, const u_char *theirpub1_proof, u_int theirpub1_proof_len, const u_char *theirpub2_proof, u_int theirpub2_proof_len, BIGNUM **newpub, u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len) { BN_CTX *bn_ctx; BIGNUM *tmp, *exponent; /* Validate peer's step 1 values */ if (BN_cmp(theirpub1, BN_value_one()) <= 0) fatal("%s: theirpub1 <= 1", __func__); if (BN_cmp(theirpub1, grp->p) >= 0) fatal("%s: theirpub1 >= p", __func__); if (BN_cmp(theirpub2, BN_value_one()) <= 0) fatal("%s: theirpub2 <= 1", __func__); if (BN_cmp(theirpub2, grp->p) >= 0) fatal("%s: theirpub2 >= p", __func__); if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub1, theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1) fatal("%s: schnorr_verify theirpub1 failed", __func__); if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub2, theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1) fatal("%s: schnorr_verify theirpub2 failed", __func__); if ((bn_ctx = BN_CTX_new()) == NULL) fatal("%s: BN_CTX_new", __func__); if ((*newpub = BN_new()) == NULL || (tmp = BN_new()) == NULL || (exponent = BN_new()) == NULL) fatal("%s: BN_new", __func__); /* * client: exponent = x2 * s mod p * server: exponent = x4 * s mod p */ if (BN_mod_mul(exponent, mypriv2, s, grp->q, bn_ctx) != 1) fatal("%s: BN_mod_mul (exponent = mypriv2 * s mod p)", __func__); /* * client: tmp = g^(x1 + x3 + x4) mod p * server: tmp = g^(x1 + x2 + x3) mod p */ if (BN_mod_mul(tmp, mypub1, theirpub1, grp->p, bn_ctx) != 1) fatal("%s: BN_mod_mul (tmp = mypub1 * theirpub1 mod p)", __func__); if (BN_mod_mul(tmp, tmp, theirpub2, grp->p, bn_ctx) != 1) fatal("%s: BN_mod_mul (tmp = tmp * theirpub2 mod p)", __func__); /* * client: a = tmp^exponent = g^((x1+x3+x4) * x2 * s) mod p * server: b = tmp^exponent = g^((x1+x2+x3) * x4 * s) mod p */ if (BN_mod_exp(*newpub, tmp, exponent, grp->p, bn_ctx) != 1) fatal("%s: BN_mod_mul (newpub = tmp^exponent mod p)", __func__); JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__)); JPAKE_DEBUG_BN((exponent, "%s: exponent = ", __func__)); /* Note the generator here is 'tmp', not g */ if (schnorr_sign_buf(grp->p, grp->q, tmp, exponent, *newpub, myid, myid_len, newpub_exponent_proof, newpub_exponent_proof_len) != 0) fatal("%s: schnorr_sign newpub", __func__); BN_clear_free(tmp); /* XXX stash for later use? */ BN_clear_free(exponent); /* XXX stash for later use? (yes, in conf) */ BN_CTX_free(bn_ctx); }