summaryrefslogtreecommitdiff
path: root/jpake.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2009-03-06 00:58:22 +1100
committerDamien Miller <djm@mindrot.org>2009-03-06 00:58:22 +1100
commitcee85233149eb16c45132170d3f067496f17c368 (patch)
treecb0e423d84441222ab9bff564057f2b8e64d7066 /jpake.c
parentfaec50b554730338c0e9f34966c11368920b6a78 (diff)
- djm@cvs.openbsd.org 2009/03/05 07:18:19
[auth2-jpake.c jpake.c jpake.h monitor_wrap.c monitor_wrap.h schnorr.c] [sshconnect2.c] refactor the (disabled) Schnorr proof code to make it a little more generally useful
Diffstat (limited to 'jpake.c')
-rw-r--r--jpake.c181
1 files changed, 13 insertions, 168 deletions
diff --git a/jpake.c b/jpake.c
index 565f2e255..130661069 100644
--- a/jpake.c
+++ b/jpake.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: jpake.c,v 1.1 2008/11/04 08:22:12 djm Exp $ */ 1/* $OpenBSD: jpake.c,v 1.2 2009/03/05 07:18:19 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2008 Damien Miller. All rights reserved. 3 * Copyright (c) 2008 Damien Miller. All rights reserved.
4 * 4 *
@@ -47,6 +47,7 @@
47#include "log.h" 47#include "log.h"
48 48
49#include "jpake.h" 49#include "jpake.h"
50#include "schnorr.h"
50 51
51#ifdef JPAKE 52#ifdef JPAKE
52 53
@@ -60,165 +61,10 @@
60 "98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" \ 61 "98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" \
61 "9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF" 62 "9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
62 63
63struct jpake_group * 64struct modp_group *
64jpake_default_group(void) 65jpake_default_group(void)
65{ 66{
66 struct jpake_group *ret; 67 return modp_group_from_g_and_safe_p(JPAKE_GROUP_G, JPAKE_GROUP_P);
67
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__);
78
79 return ret;
80}
81
82/*
83 * Generate uniformly distributed random number in range (1, high).
84 * Return number on success, NULL on failure.
85 */
86BIGNUM *
87bn_rand_range_gt_one(const BIGNUM *high)
88{
89 BIGNUM *r, *tmp;
90 int success = -1;
91
92 if ((tmp = BN_new()) == NULL) {
93 error("%s: BN_new", __func__);
94 return NULL;
95 }
96 if ((r = BN_new()) == NULL) {
97 error("%s: BN_new failed", __func__);
98 goto out;
99 }
100 if (BN_set_word(tmp, 2) != 1) {
101 error("%s: BN_set_word(tmp, 2)", __func__);
102 goto out;
103 }
104 if (BN_sub(tmp, high, tmp) == -1) {
105 error("%s: BN_sub failed (tmp = high - 2)", __func__);
106 goto out;
107 }
108 if (BN_rand_range(r, tmp) == -1) {
109 error("%s: BN_rand_range failed", __func__);
110 goto out;
111 }
112 if (BN_set_word(tmp, 2) != 1) {
113 error("%s: BN_set_word(tmp, 2)", __func__);
114 goto out;
115 }
116 if (BN_add(r, r, tmp) == -1) {
117 error("%s: BN_add failed (r = r + 2)", __func__);
118 goto out;
119 }
120 success = 0;
121 out:
122 BN_clear_free(tmp);
123 if (success == 0)
124 return r;
125 BN_clear_free(r);
126 return NULL;
127}
128
129/*
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.
133 */
134int
135hash_buffer(const u_char *buf, u_int len, const EVP_MD *md,
136 u_char **digestp, u_int *lenp)
137{
138 u_char digest[EVP_MAX_MD_SIZE];
139 u_int digest_len;
140 EVP_MD_CTX evp_md_ctx;
141 int success = -1;
142
143 EVP_MD_CTX_init(&evp_md_ctx);
144
145 if (EVP_DigestInit_ex(&evp_md_ctx, md, NULL) != 1) {
146 error("%s: EVP_DigestInit_ex", __func__);
147 goto out;
148 }
149 if (EVP_DigestUpdate(&evp_md_ctx, buf, len) != 1) {
150 error("%s: EVP_DigestUpdate", __func__);
151 goto out;
152 }
153 if (EVP_DigestFinal_ex(&evp_md_ctx, digest, &digest_len) != 1) {
154 error("%s: EVP_DigestFinal_ex", __func__);
155 goto out;
156 }
157 *digestp = xmalloc(digest_len);
158 *lenp = digest_len;
159 memcpy(*digestp, digest, *lenp);
160 success = 0;
161 out:
162 EVP_MD_CTX_cleanup(&evp_md_ctx);
163 bzero(digest, sizeof(digest));
164 digest_len = 0;
165 return success;
166}
167
168/* print formatted string followed by bignum */
169void
170jpake_debug3_bn(const BIGNUM *n, const char *fmt, ...)
171{
172 char *out, *h;
173 va_list args;
174
175 out = NULL;
176 va_start(args, fmt);
177 vasprintf(&out, fmt, args);
178 va_end(args);
179 if (out == NULL)
180 fatal("%s: vasprintf failed", __func__);
181
182 if (n == NULL)
183 debug3("%s(null)", out);
184 else {
185 h = BN_bn2hex(n);
186 debug3("%s0x%s", out, h);
187 free(h);
188 }
189 free(out);
190}
191
192/* print formatted string followed by buffer contents in hex */
193void
194jpake_debug3_buf(const u_char *buf, u_int len, const char *fmt, ...)
195{
196 char *out, h[65];
197 u_int i, j;
198 va_list args;
199
200 out = NULL;
201 va_start(args, fmt);
202 vasprintf(&out, fmt, args);
203 va_end(args);
204 if (out == NULL)
205 fatal("%s: vasprintf failed", __func__);
206
207 debug3("%s length %u%s", out, len, buf == NULL ? " (null)" : "");
208 free(out);
209 if (buf == NULL)
210 return;
211
212 *h = '\0';
213 for (i = j = 0; i < len; i++) {
214 snprintf(h + j, sizeof(h) - j, "%02x", buf[i]);
215 j += 2;
216 if (j >= sizeof(h) - 1 || i == len - 1) {
217 debug3(" %s", h);
218 *h = '\0';
219 j = 0;
220 }
221 }
222} 68}
223 69
224struct jpake_ctx * 70struct jpake_ctx *
@@ -243,7 +89,6 @@ jpake_new(void)
243 return ret; 89 return ret;
244} 90}
245 91
246
247void 92void
248jpake_free(struct jpake_ctx *pctx) 93jpake_free(struct jpake_ctx *pctx)
249{ 94{
@@ -344,7 +189,7 @@ jpake_dump(struct jpake_ctx *pctx, const char *fmt, ...)
344 189
345/* Shared parts of step 1 exchange calculation */ 190/* Shared parts of step 1 exchange calculation */
346void 191void
347jpake_step1(struct jpake_group *grp, 192jpake_step1(struct modp_group *grp,
348 u_char **id, u_int *id_len, 193 u_char **id, u_int *id_len,
349 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2, 194 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
350 u_char **priv1_proof, u_int *priv1_proof_len, 195 u_char **priv1_proof, u_int *priv1_proof_len,
@@ -383,11 +228,11 @@ jpake_step1(struct jpake_group *grp,
383 fatal("%s: BN_mod_exp", __func__); 228 fatal("%s: BN_mod_exp", __func__);
384 229
385 /* Generate proofs for holding x1/x3 and x2/x4 */ 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 *priv1, *g_priv1, *id, *id_len, 232 *priv1, *g_priv1, *id, *id_len,
388 priv1_proof, priv1_proof_len) != 0) 233 priv1_proof, priv1_proof_len) != 0)
389 fatal("%s: schnorr_sign", __func__); 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 *priv2, *g_priv2, *id, *id_len, 236 *priv2, *g_priv2, *id, *id_len,
392 priv2_proof, priv2_proof_len) != 0) 237 priv2_proof, priv2_proof_len) != 0)
393 fatal("%s: schnorr_sign", __func__); 238 fatal("%s: schnorr_sign", __func__);
@@ -397,7 +242,7 @@ jpake_step1(struct jpake_group *grp,
397 242
398/* Shared parts of step 2 exchange calculation */ 243/* Shared parts of step 2 exchange calculation */
399void 244void
400jpake_step2(struct jpake_group *grp, BIGNUM *s, 245jpake_step2(struct modp_group *grp, BIGNUM *s,
401 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2, 246 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
402 const u_char *theirid, u_int theirid_len, 247 const u_char *theirid, u_int theirid_len,
403 const u_char *myid, u_int myid_len, 248 const u_char *myid, u_int myid_len,
@@ -415,10 +260,10 @@ jpake_step2(struct jpake_group *grp, BIGNUM *s,
415 if (BN_cmp(theirpub2, BN_value_one()) <= 0) 260 if (BN_cmp(theirpub2, BN_value_one()) <= 0)
416 fatal("%s: theirpub2 <= 1", __func__); 261 fatal("%s: theirpub2 <= 1", __func__);
417 262
418 if (schnorr_verify(grp->p, grp->q, grp->g, theirpub1, 263 if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub1,
419 theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1) 264 theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1)
420 fatal("%s: schnorr_verify theirpub1 failed", __func__); 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 theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1) 267 theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1)
423 fatal("%s: schnorr_verify theirpub2 failed", __func__); 268 fatal("%s: schnorr_verify theirpub2 failed", __func__);
424 269
@@ -459,7 +304,7 @@ jpake_step2(struct jpake_group *grp, BIGNUM *s,
459 JPAKE_DEBUG_BN((exponent, "%s: exponent = ", __func__)); 304 JPAKE_DEBUG_BN((exponent, "%s: exponent = ", __func__));
460 305
461 /* Note the generator here is 'tmp', not g */ 306 /* Note the generator here is 'tmp', not g */
462 if (schnorr_sign(grp->p, grp->q, tmp, exponent, *newpub, 307 if (schnorr_sign_buf(grp->p, grp->q, tmp, exponent, *newpub,
463 myid, myid_len, 308 myid, myid_len,
464 newpub_exponent_proof, newpub_exponent_proof_len) != 0) 309 newpub_exponent_proof, newpub_exponent_proof_len) != 0)
465 fatal("%s: schnorr_sign newpub", __func__); 310 fatal("%s: schnorr_sign newpub", __func__);
@@ -496,7 +341,7 @@ jpake_confirm_hash(const BIGNUM *k,
496 341
497/* Shared parts of key derivation and confirmation calculation */ 342/* Shared parts of key derivation and confirmation calculation */
498void 343void
499jpake_key_confirm(struct jpake_group *grp, BIGNUM *s, BIGNUM *step2_val, 344jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
500 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2, 345 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
501 BIGNUM *theirpub1, BIGNUM *theirpub2, 346 BIGNUM *theirpub1, BIGNUM *theirpub2,
502 const u_char *my_id, u_int my_id_len, 347 const u_char *my_id, u_int my_id_len,
@@ -531,7 +376,7 @@ jpake_key_confirm(struct jpake_group *grp, BIGNUM *s, BIGNUM *step2_val,
531 376
532 JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__)); 377 JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__));
533 378
534 if (schnorr_verify(grp->p, grp->q, tmp, step2_val, 379 if (schnorr_verify_buf(grp->p, grp->q, tmp, step2_val,
535 their_id, their_id_len, 380 their_id, their_id_len,
536 theirpriv2_s_proof, theirpriv2_s_proof_len) != 1) 381 theirpriv2_s_proof, theirpriv2_s_proof_len) != 1)
537 fatal("%s: schnorr_verify theirpriv2_s_proof failed", __func__); 382 fatal("%s: schnorr_verify theirpriv2_s_proof failed", __func__);