diff svr-kex.c @ 848:6c69e7df3621 ecc

curve25519
author Matt Johnston <matt@ucc.asn.au>
date Fri, 08 Nov 2013 23:11:43 +0800
parents 7dcb46da72d9
children 754d7bee1068
line wrap: on
line diff
--- a/svr-kex.c	Fri Nov 01 00:21:59 2013 +0800
+++ b/svr-kex.c	Fri Nov 08 23:11:43 2013 +0800
@@ -52,15 +52,22 @@
 		dropbear_exit("Premature kexdh_init message received");
 	}
 
-	if (IS_NORMAL_DH(ses.newkeys->algo_kex)) {
-		m_mp_init(&dh_e);
-		if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
-			dropbear_exit("Failed to get kex value");
-		}
-	} else {
-#ifdef DROPBEAR_ECDH
-		ecdh_qs = buf_getstringbuf(ses.payload);
+	switch (ses.newkeys->algo_kex->mode) {
+		case DROPBEAR_KEX_NORMAL_DH:
+			m_mp_init(&dh_e);
+			if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
+				dropbear_exit("Bad kex value");
+			}
+			break;
+		case DROPBEAR_KEX_ECDH:
+		case DROPBEAR_KEX_CURVE25519:
+#if defined(DROPBEAR_ECDH) || defined(DROPBEAR_CURVE25519)
+			ecdh_qs = buf_getstringbuf(ses.payload);
+			if (ses.payload->pos != ses.payload->len) {
+				dropbear_exit("Bad kex value");
+			}
 #endif
+			break;
 	}
 
 	send_msg_kexdh_reply(&dh_e, ecdh_qs);
@@ -92,22 +99,38 @@
 	buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
 			ses.newkeys->algo_hostkey);
 
-	if (IS_NORMAL_DH(ses.newkeys->algo_kex)) {
-		// Normal diffie-hellman
-		struct kex_dh_param * dh_param = gen_kexdh_param();
-		kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
+	switch (ses.newkeys->algo_kex->mode) {
+		case DROPBEAR_KEX_NORMAL_DH:
+			{
+			struct kex_dh_param * dh_param = gen_kexdh_param();
+			kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
 
-		/* put f */
-		buf_putmpint(ses.writepayload, &dh_param->pub);
-		free_kexdh_param(dh_param);
-	} else {
+			/* put f */
+			buf_putmpint(ses.writepayload, &dh_param->pub);
+			free_kexdh_param(dh_param);
+			}
+			break;
+		case DROPBEAR_KEX_ECDH:
 #ifdef DROPBEAR_ECDH
-		struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
-		kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
+			{
+			struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
+			kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
 
-		buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
-		free_kexecdh_param(ecdh_param);
+			buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
+			free_kexecdh_param(ecdh_param);
+			}
 #endif
+			break;
+		case DROPBEAR_KEX_CURVE25519:
+#ifdef DROPBEAR_CURVE25519
+			{
+			struct kex_curve25519_param *param = gen_kexecdh_param();
+			kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey);
+			buf_putstring(ses.writepayload, param->priv, CURVE25519_LEN);
+			free_kexcurve25519_param(param);
+			}
+#endif
+			break;
 	}
 
 	/* calc the signature */