Annotation of src/usr.bin/ssh/jpake.c, Revision 1.5
1.5 ! djm 1: /* $OpenBSD: jpake.c,v 1.4 2010/07/13 23:13:16 djm Exp $ */
1.1 djm 2: /*
3: * Copyright (c) 2008 Damien Miller. All rights reserved.
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17:
18: /*
19: * Shared components of zero-knowledge password auth using J-PAKE protocol
20: * as described in:
21: *
22: * F. Hao, P. Ryan, "Password Authenticated Key Exchange by Juggling",
23: * 16th Workshop on Security Protocols, Cambridge, April 2008
24: *
25: * http://grouper.ieee.org/groups/1363/Research/contributions/hao-ryan-2008.pdf
26: */
27:
28: #include <sys/types.h>
29:
30: #include <stdio.h>
31: #include <string.h>
32: #include <stdarg.h>
33:
34: #include <openssl/bn.h>
35: #include <openssl/evp.h>
36:
37: #include "xmalloc.h"
38: #include "ssh2.h"
39: #include "key.h"
40: #include "hostfile.h"
41: #include "auth.h"
42: #include "buffer.h"
43: #include "packet.h"
44: #include "dispatch.h"
45: #include "log.h"
46:
47: #include "jpake.h"
1.2 djm 48: #include "schnorr.h"
1.1 djm 49:
50: #ifdef JPAKE
51:
52: /* RFC3526 group 5, 1536 bits */
53: #define JPAKE_GROUP_G "2"
54: #define JPAKE_GROUP_P \
55: "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74" \
56: "020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437" \
57: "4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
58: "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05" \
59: "98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" \
60: "9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
61:
1.2 djm 62: struct modp_group *
1.1 djm 63: jpake_default_group(void)
64: {
1.2 djm 65: return modp_group_from_g_and_safe_p(JPAKE_GROUP_G, JPAKE_GROUP_P);
1.1 djm 66: }
67:
68: struct jpake_ctx *
69: jpake_new(void)
70: {
71: struct jpake_ctx *ret;
72:
73: ret = xcalloc(1, sizeof(*ret));
74:
75: ret->grp = jpake_default_group();
76:
77: ret->s = ret->k = NULL;
78: ret->x1 = ret->x2 = ret->x3 = ret->x4 = NULL;
79: ret->g_x1 = ret->g_x2 = ret->g_x3 = ret->g_x4 = NULL;
80: ret->a = ret->b = NULL;
81:
82: ret->client_id = ret->server_id = NULL;
83: ret->h_k_cid_sessid = ret->h_k_sid_sessid = NULL;
84:
85: debug3("%s: alloc %p", __func__, ret);
86:
87: return ret;
88: }
89:
90: void
91: jpake_free(struct jpake_ctx *pctx)
92: {
93: debug3("%s: free %p", __func__, pctx);
94:
95: #define JPAKE_BN_CLEAR_FREE(v) \
96: do { \
97: if ((v) != NULL) { \
98: BN_clear_free(v); \
99: (v) = NULL; \
100: } \
101: } while (0)
102: #define JPAKE_BUF_CLEAR_FREE(v, l) \
103: do { \
104: if ((v) != NULL) { \
105: bzero((v), (l)); \
106: xfree(v); \
107: (v) = NULL; \
108: (l) = 0; \
109: } \
110: } while (0)
111:
112: JPAKE_BN_CLEAR_FREE(pctx->s);
113: JPAKE_BN_CLEAR_FREE(pctx->k);
114: JPAKE_BN_CLEAR_FREE(pctx->x1);
115: JPAKE_BN_CLEAR_FREE(pctx->x2);
116: JPAKE_BN_CLEAR_FREE(pctx->x3);
117: JPAKE_BN_CLEAR_FREE(pctx->x4);
118: JPAKE_BN_CLEAR_FREE(pctx->g_x1);
119: JPAKE_BN_CLEAR_FREE(pctx->g_x2);
120: JPAKE_BN_CLEAR_FREE(pctx->g_x3);
121: JPAKE_BN_CLEAR_FREE(pctx->g_x4);
122: JPAKE_BN_CLEAR_FREE(pctx->a);
123: JPAKE_BN_CLEAR_FREE(pctx->b);
124:
125: JPAKE_BUF_CLEAR_FREE(pctx->client_id, pctx->client_id_len);
126: JPAKE_BUF_CLEAR_FREE(pctx->server_id, pctx->server_id_len);
127: JPAKE_BUF_CLEAR_FREE(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
128: JPAKE_BUF_CLEAR_FREE(pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
129:
130: #undef JPAKE_BN_CLEAR_FREE
131: #undef JPAKE_BUF_CLEAR_FREE
132:
133: bzero(pctx, sizeof(pctx));
134: xfree(pctx);
135: }
136:
137: /* dump entire jpake_ctx. NB. includes private values! */
138: void
139: jpake_dump(struct jpake_ctx *pctx, const char *fmt, ...)
140: {
141: char *out;
142: va_list args;
143:
144: out = NULL;
145: va_start(args, fmt);
146: vasprintf(&out, fmt, args);
147: va_end(args);
148: if (out == NULL)
149: fatal("%s: vasprintf failed", __func__);
150:
151: debug3("%s: %s (ctx at %p)", __func__, out, pctx);
152: if (pctx == NULL) {
153: free(out);
154: return;
155: }
156:
157: #define JPAKE_DUMP_BN(a) do { \
158: if ((a) != NULL) \
159: JPAKE_DEBUG_BN(((a), "%s = ", #a)); \
160: } while (0)
161: #define JPAKE_DUMP_BUF(a, b) do { \
162: if ((a) != NULL) \
163: JPAKE_DEBUG_BUF((a, b, "%s", #a)); \
164: } while (0)
165:
166: JPAKE_DUMP_BN(pctx->s);
167: JPAKE_DUMP_BN(pctx->k);
168: JPAKE_DUMP_BN(pctx->x1);
169: JPAKE_DUMP_BN(pctx->x2);
170: JPAKE_DUMP_BN(pctx->x3);
171: JPAKE_DUMP_BN(pctx->x4);
172: JPAKE_DUMP_BN(pctx->g_x1);
173: JPAKE_DUMP_BN(pctx->g_x2);
174: JPAKE_DUMP_BN(pctx->g_x3);
175: JPAKE_DUMP_BN(pctx->g_x4);
176: JPAKE_DUMP_BN(pctx->a);
177: JPAKE_DUMP_BN(pctx->b);
178:
179: JPAKE_DUMP_BUF(pctx->client_id, pctx->client_id_len);
180: JPAKE_DUMP_BUF(pctx->server_id, pctx->server_id_len);
181: JPAKE_DUMP_BUF(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
182: JPAKE_DUMP_BUF(pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
183:
184: debug3("%s: %s done", __func__, out);
185: free(out);
186: }
187:
188: /* Shared parts of step 1 exchange calculation */
189: void
1.2 djm 190: jpake_step1(struct modp_group *grp,
1.1 djm 191: u_char **id, u_int *id_len,
192: BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
193: u_char **priv1_proof, u_int *priv1_proof_len,
194: u_char **priv2_proof, u_int *priv2_proof_len)
195: {
196: BN_CTX *bn_ctx;
197:
198: if ((bn_ctx = BN_CTX_new()) == NULL)
199: fatal("%s: BN_CTX_new", __func__);
200:
201: /* Random nonce to prevent replay */
202: *id = xmalloc(KZP_ID_LEN);
203: *id_len = KZP_ID_LEN;
204: arc4random_buf(*id, *id_len);
205:
206: /*
207: * x1/x3 is a random element of Zq
208: * x2/x4 is a random element of Z*q
209: * We also exclude [1] from x1/x3 candidates and [0, 1] from
210: * x2/x4 candiates to avoid possible degeneracy (i.e. g^0, g^1).
211: */
212: if ((*priv1 = bn_rand_range_gt_one(grp->q)) == NULL ||
213: (*priv2 = bn_rand_range_gt_one(grp->q)) == NULL)
214: fatal("%s: bn_rand_range_gt_one", __func__);
215:
216: /*
217: * client: g_x1 = g^x1 mod p / server: g_x3 = g^x3 mod p
218: * client: g_x2 = g^x2 mod p / server: g_x4 = g^x4 mod p
219: */
220: if ((*g_priv1 = BN_new()) == NULL ||
221: (*g_priv2 = BN_new()) == NULL)
222: fatal("%s: BN_new", __func__);
223: if (BN_mod_exp(*g_priv1, grp->g, *priv1, grp->p, bn_ctx) == -1)
224: fatal("%s: BN_mod_exp", __func__);
225: if (BN_mod_exp(*g_priv2, grp->g, *priv2, grp->p, bn_ctx) == -1)
226: fatal("%s: BN_mod_exp", __func__);
227:
228: /* Generate proofs for holding x1/x3 and x2/x4 */
1.2 djm 229: if (schnorr_sign_buf(grp->p, grp->q, grp->g,
1.1 djm 230: *priv1, *g_priv1, *id, *id_len,
231: priv1_proof, priv1_proof_len) != 0)
232: fatal("%s: schnorr_sign", __func__);
1.2 djm 233: if (schnorr_sign_buf(grp->p, grp->q, grp->g,
1.1 djm 234: *priv2, *g_priv2, *id, *id_len,
235: priv2_proof, priv2_proof_len) != 0)
236: fatal("%s: schnorr_sign", __func__);
237:
238: BN_CTX_free(bn_ctx);
239: }
240:
241: /* Shared parts of step 2 exchange calculation */
242: void
1.2 djm 243: jpake_step2(struct modp_group *grp, BIGNUM *s,
1.1 djm 244: BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
245: const u_char *theirid, u_int theirid_len,
246: const u_char *myid, u_int myid_len,
247: const u_char *theirpub1_proof, u_int theirpub1_proof_len,
248: const u_char *theirpub2_proof, u_int theirpub2_proof_len,
249: BIGNUM **newpub,
250: u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
251: {
252: BN_CTX *bn_ctx;
253: BIGNUM *tmp, *exponent;
254:
255: /* Validate peer's step 1 values */
256: if (BN_cmp(theirpub1, BN_value_one()) <= 0)
257: fatal("%s: theirpub1 <= 1", __func__);
1.5 ! djm 258: if (BN_cmp(theirpub1, grp->p) >= 0)
! 259: fatal("%s: theirpub1 >= p", __func__);
1.1 djm 260: if (BN_cmp(theirpub2, BN_value_one()) <= 0)
261: fatal("%s: theirpub2 <= 1", __func__);
1.5 ! djm 262: if (BN_cmp(theirpub2, grp->p) >= 0)
! 263: fatal("%s: theirpub2 >= p", __func__);
1.1 djm 264:
1.2 djm 265: if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub1,
1.1 djm 266: theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1)
267: fatal("%s: schnorr_verify theirpub1 failed", __func__);
1.2 djm 268: if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub2,
1.1 djm 269: theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1)
270: fatal("%s: schnorr_verify theirpub2 failed", __func__);
271:
272: if ((bn_ctx = BN_CTX_new()) == NULL)
273: fatal("%s: BN_CTX_new", __func__);
274:
275: if ((*newpub = BN_new()) == NULL ||
276: (tmp = BN_new()) == NULL ||
277: (exponent = BN_new()) == NULL)
278: fatal("%s: BN_new", __func__);
279:
280: /*
281: * client: exponent = x2 * s mod p
282: * server: exponent = x4 * s mod p
283: */
284: if (BN_mod_mul(exponent, mypriv2, s, grp->q, bn_ctx) != 1)
285: fatal("%s: BN_mod_mul (exponent = mypriv2 * s mod p)",
286: __func__);
287:
288: /*
289: * client: tmp = g^(x1 + x3 + x4) mod p
290: * server: tmp = g^(x1 + x2 + x3) mod p
291: */
292: if (BN_mod_mul(tmp, mypub1, theirpub1, grp->p, bn_ctx) != 1)
293: fatal("%s: BN_mod_mul (tmp = mypub1 * theirpub1 mod p)",
294: __func__);
295: if (BN_mod_mul(tmp, tmp, theirpub2, grp->p, bn_ctx) != 1)
296: fatal("%s: BN_mod_mul (tmp = tmp * theirpub2 mod p)", __func__);
297:
298: /*
299: * client: a = tmp^exponent = g^((x1+x3+x4) * x2 * s) mod p
300: * server: b = tmp^exponent = g^((x1+x2+x3) * x4 * s) mod p
301: */
302: if (BN_mod_exp(*newpub, tmp, exponent, grp->p, bn_ctx) != 1)
303: fatal("%s: BN_mod_mul (newpub = tmp^exponent mod p)", __func__);
304:
305: JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__));
306: JPAKE_DEBUG_BN((exponent, "%s: exponent = ", __func__));
307:
308: /* Note the generator here is 'tmp', not g */
1.2 djm 309: if (schnorr_sign_buf(grp->p, grp->q, tmp, exponent, *newpub,
1.1 djm 310: myid, myid_len,
311: newpub_exponent_proof, newpub_exponent_proof_len) != 0)
312: fatal("%s: schnorr_sign newpub", __func__);
313:
314: BN_clear_free(tmp); /* XXX stash for later use? */
315: BN_clear_free(exponent); /* XXX stash for later use? (yes, in conf) */
316:
317: BN_CTX_free(bn_ctx);
318: }
319:
320: /* Confirmation hash calculation */
321: void
322: jpake_confirm_hash(const BIGNUM *k,
323: const u_char *endpoint_id, u_int endpoint_id_len,
324: const u_char *sess_id, u_int sess_id_len,
325: u_char **confirm_hash, u_int *confirm_hash_len)
326: {
327: Buffer b;
328:
329: /*
330: * Calculate confirmation proof:
331: * client: H(k || client_id || session_id)
332: * server: H(k || server_id || session_id)
333: */
334: buffer_init(&b);
335: buffer_put_bignum2(&b, k);
336: buffer_put_string(&b, endpoint_id, endpoint_id_len);
337: buffer_put_string(&b, sess_id, sess_id_len);
338: if (hash_buffer(buffer_ptr(&b), buffer_len(&b), EVP_sha256(),
339: confirm_hash, confirm_hash_len) != 0)
340: fatal("%s: hash_buffer", __func__);
341: buffer_free(&b);
342: }
343:
344: /* Shared parts of key derivation and confirmation calculation */
345: void
1.2 djm 346: jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1.1 djm 347: BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
348: BIGNUM *theirpub1, BIGNUM *theirpub2,
349: const u_char *my_id, u_int my_id_len,
350: const u_char *their_id, u_int their_id_len,
351: const u_char *sess_id, u_int sess_id_len,
352: const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
353: BIGNUM **k,
354: u_char **confirm_hash, u_int *confirm_hash_len)
355: {
356: BN_CTX *bn_ctx;
357: BIGNUM *tmp;
358:
359: if ((bn_ctx = BN_CTX_new()) == NULL)
360: fatal("%s: BN_CTX_new", __func__);
361: if ((tmp = BN_new()) == NULL ||
362: (*k = BN_new()) == NULL)
363: fatal("%s: BN_new", __func__);
364:
365: /* Validate step 2 values */
366: if (BN_cmp(step2_val, BN_value_one()) <= 0)
367: fatal("%s: step2_val <= 1", __func__);
1.5 ! djm 368: if (BN_cmp(step2_val, grp->p) >= 0)
! 369: fatal("%s: step2_val >= p", __func__);
1.1 djm 370:
371: /*
372: * theirpriv2_s_proof is calculated with a different generator:
373: * tmp = g^(mypriv1+mypriv2+theirpub1) = g^mypub1*g^mypub2*g^theirpub1
374: * Calculate it here so we can check the signature.
375: */
376: if (BN_mod_mul(tmp, mypub1, mypub2, grp->p, bn_ctx) != 1)
377: fatal("%s: BN_mod_mul (tmp = mypub1 * mypub2 mod p)", __func__);
378: if (BN_mod_mul(tmp, tmp, theirpub1, grp->p, bn_ctx) != 1)
379: fatal("%s: BN_mod_mul (tmp = tmp * theirpub1 mod p)", __func__);
380:
381: JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__));
382:
1.2 djm 383: if (schnorr_verify_buf(grp->p, grp->q, tmp, step2_val,
1.1 djm 384: their_id, their_id_len,
385: theirpriv2_s_proof, theirpriv2_s_proof_len) != 1)
386: fatal("%s: schnorr_verify theirpriv2_s_proof failed", __func__);
387:
388: /*
389: * Derive shared key:
390: * client: k = (b / g^(x2*x4*s))^x2 = g^((x1+x3)*x2*x4*s)
391: * server: k = (a / g^(x2*x4*s))^x4 = g^((x1+x3)*x2*x4*s)
392: *
393: * Computed as:
394: * client: k = (g_x4^(q - (x2 * s)) * b)^x2 mod p
395: * server: k = (g_x2^(q - (x4 * s)) * b)^x4 mod p
396: */
397: if (BN_mul(tmp, mypriv2, s, bn_ctx) != 1)
398: fatal("%s: BN_mul (tmp = mypriv2 * s)", __func__);
399: if (BN_mod_sub(tmp, grp->q, tmp, grp->q, bn_ctx) != 1)
400: fatal("%s: BN_mod_sub (tmp = q - tmp mod q)", __func__);
401: if (BN_mod_exp(tmp, theirpub2, tmp, grp->p, bn_ctx) != 1)
402: fatal("%s: BN_mod_exp (tmp = theirpub2^tmp) mod p", __func__);
403: if (BN_mod_mul(tmp, tmp, step2_val, grp->p, bn_ctx) != 1)
404: fatal("%s: BN_mod_mul (tmp = tmp * step2_val) mod p", __func__);
405: if (BN_mod_exp(*k, tmp, mypriv2, grp->p, bn_ctx) != 1)
406: fatal("%s: BN_mod_exp (k = tmp^mypriv2) mod p", __func__);
407:
408: BN_CTX_free(bn_ctx);
409: BN_clear_free(tmp);
410:
411: jpake_confirm_hash(*k, my_id, my_id_len, sess_id, sess_id_len,
412: confirm_hash, confirm_hash_len);
413: }
414:
415: /*
416: * Calculate and check confirmation hash from peer. Returns 1 on success
417: * 0 on failure/mismatch.
418: */
419: int
420: jpake_check_confirm(const BIGNUM *k,
421: const u_char *peer_id, u_int peer_id_len,
422: const u_char *sess_id, u_int sess_id_len,
423: const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
424: {
425: u_char *expected_confirm_hash;
426: u_int expected_confirm_hash_len;
427: int success = 0;
428:
429: /* Calculate and verify expected confirmation hash */
430: jpake_confirm_hash(k, peer_id, peer_id_len, sess_id, sess_id_len,
431: &expected_confirm_hash, &expected_confirm_hash_len);
432:
433: JPAKE_DEBUG_BUF((expected_confirm_hash, expected_confirm_hash_len,
434: "%s: expected confirm hash", __func__));
435: JPAKE_DEBUG_BUF((peer_confirm_hash, peer_confirm_hash_len,
436: "%s: received confirm hash", __func__));
437:
438: if (peer_confirm_hash_len != expected_confirm_hash_len)
439: error("%s: confirmation length mismatch (my %u them %u)",
440: __func__, expected_confirm_hash_len, peer_confirm_hash_len);
1.4 djm 441: else if (timingsafe_bcmp(peer_confirm_hash, expected_confirm_hash,
1.1 djm 442: expected_confirm_hash_len) == 0)
443: success = 1;
444: bzero(expected_confirm_hash, expected_confirm_hash_len);
445: xfree(expected_confirm_hash);
446: debug3("%s: success = %d", __func__, success);
447: return success;
448: }
449:
450: /* XXX main() function with tests */
451:
452: #endif /* JPAKE */
453: