comparison svr-kex.c @ 755:b07eb3dc23ec ecc

refactor kexdh code a bit, start working on ecdh etc
author Matt Johnston <matt@ucc.asn.au>
date Tue, 26 Mar 2013 01:35:22 +0800
parents 3aa74a4d83ae
children 76fba0856749
comparison
equal deleted inserted replaced
725:49f68a7b7a55 755:b07eb3dc23ec
34 #include "bignum.h" 34 #include "bignum.h"
35 #include "random.h" 35 #include "random.h"
36 #include "runopts.h" 36 #include "runopts.h"
37 37
38 38
39 static void send_msg_kexdh_reply(mp_int *dh_e); 39 static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs);
40 40
41 /* Handle a diffie-hellman key exchange initialisation. This involves 41 /* Handle a diffie-hellman key exchange initialisation. This involves
42 * calculating a session key reply value, and corresponding hash. These 42 * calculating a session key reply value, and corresponding hash. These
43 * are carried out by send_msg_kexdh_reply(). recv_msg_kexdh_init() calls 43 * are carried out by send_msg_kexdh_reply(). recv_msg_kexdh_init() calls
44 * that function, then brings the new keys into use */ 44 * that function, then brings the new keys into use */
45 void recv_msg_kexdh_init() { 45 void recv_msg_kexdh_init() {
46 46
47 DEF_MP_INT(dh_e); 47 DEF_MP_INT(dh_e);
48 buffer *ecdh_qs = NULL;
48 49
49 TRACE(("enter recv_msg_kexdh_init")) 50 TRACE(("enter recv_msg_kexdh_init"))
50 if (!ses.kexstate.recvkexinit) { 51 if (!ses.kexstate.recvkexinit) {
51 dropbear_exit("Premature kexdh_init message received"); 52 dropbear_exit("Premature kexdh_init message received");
52 } 53 }
53 54
54 m_mp_init(&dh_e); 55 if (IS_NORMAL_DH(ses.newkeys->algo_kex)) {
55 if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) { 56 m_mp_init(&dh_e);
56 dropbear_exit("Failed to get kex value"); 57 if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
58 dropbear_exit("Failed to get kex value");
59 }
60 } else {
61 #ifdef DROPBEAR_ECDH
62 #endif
57 } 63 }
58 64
59 send_msg_kexdh_reply(&dh_e); 65 send_msg_kexdh_reply(&dh_e, ecdh_qs);
60 66
61 mp_clear(&dh_e); 67 mp_clear(&dh_e);
68 if (ecdh_qs) {
69 buf_free(ecdh_qs);
70 }
62 71
63 send_msg_newkeys(); 72 send_msg_newkeys();
64 ses.requirenext = SSH_MSG_NEWKEYS; 73 ses.requirenext = SSH_MSG_NEWKEYS;
65 TRACE(("leave recv_msg_kexdh_init")) 74 TRACE(("leave recv_msg_kexdh_init"))
66 } 75 }
68 /* Generate our side of the diffie-hellman key exchange value (dh_f), and 77 /* Generate our side of the diffie-hellman key exchange value (dh_f), and
69 * calculate the session key using the diffie-hellman algorithm. Following 78 * calculate the session key using the diffie-hellman algorithm. Following
70 * that, the session hash is calculated, and signed with RSA or DSS. The 79 * that, the session hash is calculated, and signed with RSA or DSS. The
71 * result is sent to the client. 80 * result is sent to the client.
72 * 81 *
73 * See the transport rfc 4253 section 8 for details */ 82 * See the transport RFC4253 section 8 for details
74 static void send_msg_kexdh_reply(mp_int *dh_e) { 83 * or RFC5656 section 4 for elliptic curve variant. */
75 84 static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
76 DEF_MP_INT(dh_y);
77 DEF_MP_INT(dh_f);
78
79 TRACE(("enter send_msg_kexdh_reply")) 85 TRACE(("enter send_msg_kexdh_reply"))
80 m_mp_init_multi(&dh_y, &dh_f, NULL);
81
82 gen_kexdh_vals(&dh_f, &dh_y);
83
84 kexdh_comb_key(&dh_f, &dh_y, dh_e, svr_opts.hostkey);
85 mp_clear(&dh_y);
86 86
87 /* we can start creating the kexdh_reply packet */ 87 /* we can start creating the kexdh_reply packet */
88 CHECKCLEARTOWRITE(); 88 CHECKCLEARTOWRITE();
89 buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY); 89 buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY);
90 buf_put_pub_key(ses.writepayload, svr_opts.hostkey, 90 buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
91 ses.newkeys->algo_hostkey); 91 ses.newkeys->algo_hostkey);
92 92
93 /* put f */ 93 if (IS_NORMAL_DH(ses.newkeys->algo_kex)) {
94 buf_putmpint(ses.writepayload, &dh_f); 94 // Normal diffie-hellman
95 mp_clear(&dh_f); 95 struct kex_dh_param * dh_param = gen_kexdh_param();
96 kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
97
98 /* put f */
99 buf_putmpint(ses.writepayload, &dh_param->pub);
100 free_kexdh_param(dh_param);
101 } else {
102 #ifdef DROPBEAR_ECDH
103 struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
104 kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
105
106 buf_put_ecc_pub(ses.writepayload, &ecdh_param->key);
107 free_kexecdh_param(ecdh_param);
108 #endif
109 }
96 110
97 /* calc the signature */ 111 /* calc the signature */
98 buf_put_sign(ses.writepayload, svr_opts.hostkey, 112 buf_put_sign(ses.writepayload, svr_opts.hostkey,
99 ses.newkeys->algo_hostkey, ses.hash, SHA1_HASH_SIZE); 113 ses.newkeys->algo_hostkey, ses.hash, SHA1_HASH_SIZE);
100 114