60
61
"98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" \
61
62
"9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
64
65
jpake_default_group(void)
66
struct jpake_group *ret;
68
ret = xmalloc(sizeof(*ret));
69
ret->p = ret->q = ret->g = NULL;
70
if (BN_hex2bn(&ret->p, JPAKE_GROUP_P) == 0 ||
71
BN_hex2bn(&ret->g, JPAKE_GROUP_G) == 0)
72
fatal("%s: BN_hex2bn", __func__);
73
/* Subgroup order is p/2 (p is a safe prime) */
74
if ((ret->q = BN_new()) == NULL)
75
fatal("%s: BN_new", __func__);
76
if (BN_rshift1(ret->q, ret->p) != 1)
77
fatal("%s: BN_rshift1", __func__);
83
* Generate uniformly distributed random number in range (1, high).
84
* Return number on success, NULL on failure.
87
bn_rand_range_gt_one(const BIGNUM *high)
92
if ((tmp = BN_new()) == NULL) {
93
error("%s: BN_new", __func__);
96
if ((r = BN_new()) == NULL) {
97
error("%s: BN_new failed", __func__);
100
if (BN_set_word(tmp, 2) != 1) {
101
error("%s: BN_set_word(tmp, 2)", __func__);
104
if (BN_sub(tmp, high, tmp) == -1) {
105
error("%s: BN_sub failed (tmp = high - 2)", __func__);
108
if (BN_rand_range(r, tmp) == -1) {
109
error("%s: BN_rand_range failed", __func__);
112
if (BN_set_word(tmp, 2) != 1) {
113
error("%s: BN_set_word(tmp, 2)", __func__);
116
if (BN_add(r, r, tmp) == -1) {
117
error("%s: BN_add failed (r = r + 2)", __func__);
130
* Hash contents of buffer 'b' with hash 'md'. Returns 0 on success,
131
* with digest via 'digestp' (caller to free) and length via 'lenp'.
132
* Returns -1 on failure.
135
hash_buffer(const u_char *buf, u_int len, const EVP_MD *md,
136
u_char **digestp, u_int *lenp)
138
u_char digest[EVP_MAX_MD_SIZE];
140
EVP_MD_CTX evp_md_ctx;
143
EVP_MD_CTX_init(&evp_md_ctx);
145
if (EVP_DigestInit_ex(&evp_md_ctx, md, NULL) != 1) {
146
error("%s: EVP_DigestInit_ex", __func__);
149
if (EVP_DigestUpdate(&evp_md_ctx, buf, len) != 1) {
150
error("%s: EVP_DigestUpdate", __func__);
153
if (EVP_DigestFinal_ex(&evp_md_ctx, digest, &digest_len) != 1) {
154
error("%s: EVP_DigestFinal_ex", __func__);
157
*digestp = xmalloc(digest_len);
159
memcpy(*digestp, digest, *lenp);
162
EVP_MD_CTX_cleanup(&evp_md_ctx);
163
bzero(digest, sizeof(digest));
168
/* print formatted string followed by bignum */
170
jpake_debug3_bn(const BIGNUM *n, const char *fmt, ...)
177
vasprintf(&out, fmt, args);
180
fatal("%s: vasprintf failed", __func__);
183
debug3("%s(null)", out);
186
debug3("%s0x%s", out, h);
192
/* print formatted string followed by buffer contents in hex */
194
jpake_debug3_buf(const u_char *buf, u_int len, const char *fmt, ...)
202
vasprintf(&out, fmt, args);
205
fatal("%s: vasprintf failed", __func__);
207
debug3("%s length %u%s", out, len, buf == NULL ? " (null)" : "");
213
for (i = j = 0; i < len; i++) {
214
snprintf(h + j, sizeof(h) - j, "%02x", buf[i]);
216
if (j >= sizeof(h) - 1 || i == len - 1) {
67
return modp_group_from_g_and_safe_p(JPAKE_GROUP_G, JPAKE_GROUP_P);
224
70
struct jpake_ctx *
383
228
fatal("%s: BN_mod_exp", __func__);
385
230
/* Generate proofs for holding x1/x3 and x2/x4 */
386
if (schnorr_sign(grp->p, grp->q, grp->g,
231
if (schnorr_sign_buf(grp->p, grp->q, grp->g,
387
232
*priv1, *g_priv1, *id, *id_len,
388
233
priv1_proof, priv1_proof_len) != 0)
389
234
fatal("%s: schnorr_sign", __func__);
390
if (schnorr_sign(grp->p, grp->q, grp->g,
235
if (schnorr_sign_buf(grp->p, grp->q, grp->g,
391
236
*priv2, *g_priv2, *id, *id_len,
392
237
priv2_proof, priv2_proof_len) != 0)
393
238
fatal("%s: schnorr_sign", __func__);
415
260
if (BN_cmp(theirpub2, BN_value_one()) <= 0)
416
261
fatal("%s: theirpub2 <= 1", __func__);
418
if (schnorr_verify(grp->p, grp->q, grp->g, theirpub1,
263
if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub1,
419
264
theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1)
420
265
fatal("%s: schnorr_verify theirpub1 failed", __func__);
421
if (schnorr_verify(grp->p, grp->q, grp->g, theirpub2,
266
if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub2,
422
267
theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1)
423
268
fatal("%s: schnorr_verify theirpub2 failed", __func__);