- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()

- Reindent ecc.c properly

--HG--
branch : ecc
This commit is contained in:
Matt Johnston 2013-04-08 23:56:31 +08:00
parent a7d1a9cfcb
commit 4f07805d0a
5 changed files with 153 additions and 151 deletions

View File

@ -51,7 +51,7 @@ void send_msg_kexdh_init() {
} else { } else {
#ifdef DROPBEAR_ECDH #ifdef DROPBEAR_ECDH
cli_ses.ecdh_param = gen_kexecdh_param(); cli_ses.ecdh_param = gen_kexecdh_param();
buf_put_ecc_pubkey_string(ses.writepayload, &cli_ses.ecdh_param->key); buf_put_ecc_raw_pubkey_string(ses.writepayload, &cli_ses.ecdh_param->key);
#endif #endif
} }
encrypt_packet(); encrypt_packet();

View File

@ -662,7 +662,7 @@ void kexecdh_comb_key(struct kex_ecdh_param *param, buffer *pub_them,
// public keys from client and server // public keys from client and server
ecc_key *Q_C, *Q_S, *Q_them; ecc_key *Q_C, *Q_S, *Q_them;
Q_them = buf_get_ecc_pubkey(pub_them, algo_kex->ecc_curve); Q_them = buf_get_ecc_raw_pubkey(pub_them, algo_kex->ecc_curve);
ses.dh_K = dropbear_ecc_shared_secret(Q_them, &param->key); ses.dh_K = dropbear_ecc_shared_secret(Q_them, &param->key);
@ -680,9 +680,9 @@ void kexecdh_comb_key(struct kex_ecdh_param *param, buffer *pub_them,
/* K_S, the host key */ /* K_S, the host key */
buf_put_pub_key(ses.kexhashbuf, hostkey, ses.newkeys->algo_hostkey); buf_put_pub_key(ses.kexhashbuf, hostkey, ses.newkeys->algo_hostkey);
/* Q_C, client's ephemeral public key octet string */ /* Q_C, client's ephemeral public key octet string */
buf_put_ecc_pubkey_string(ses.kexhashbuf, Q_C); buf_put_ecc_raw_pubkey_string(ses.kexhashbuf, Q_C);
/* Q_S, server's ephemeral public key octet string */ /* Q_S, server's ephemeral public key octet string */
buf_put_ecc_pubkey_string(ses.kexhashbuf, Q_S); buf_put_ecc_raw_pubkey_string(ses.kexhashbuf, Q_S);
/* K, the shared secret */ /* K, the shared secret */
buf_putmpint(ses.kexhashbuf, ses.dh_K); buf_putmpint(ses.kexhashbuf, ses.dh_K);

290
ecc.c
View File

@ -31,16 +31,74 @@ const struct dropbear_ecc_curve ecc_curve_nistp521 = {
#endif #endif
static ecc_key * new_ecc_key(void) { static ecc_key * new_ecc_key(void) {
ecc_key *key = m_malloc(sizeof(*key)); ecc_key *key = m_malloc(sizeof(*key));
key->pubkey.x = m_malloc(sizeof(mp_int)); key->pubkey.x = m_malloc(sizeof(mp_int));
key->pubkey.y = m_malloc(sizeof(mp_int)); key->pubkey.y = m_malloc(sizeof(mp_int));
key->pubkey.z = m_malloc(sizeof(mp_int)); key->pubkey.z = m_malloc(sizeof(mp_int));
key->k = m_malloc(sizeof(mp_int)); key->k = m_malloc(sizeof(mp_int));
m_mp_init_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL); m_mp_init_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
return key; return key;
} }
void buf_put_ecc_pubkey_string(buffer *buf, ecc_key *key) { // Copied from libtomcrypt ecc_import.c (version there is static), modified
// for different mp_int pointer without LTC_SOURCE
static int ecc_is_point(ecc_key *key)
{
mp_int *prime, *b, *t1, *t2;
int err;
prime = m_malloc(sizeof(mp_int));
b = m_malloc(sizeof(mp_int));
t1 = m_malloc(sizeof(mp_int));
t2 = m_malloc(sizeof(mp_int));
m_mp_init_multi(prime, b, t1, t2, NULL);
/* load prime and b */
if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }
if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }
/* compute y^2 */
if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
/* compute x^3 */
if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }
if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }
if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }
/* compute y^2 - x^3 */
if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
/* compute y^2 - x^3 + 3x */
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }
while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }
}
while (mp_cmp(t1, prime) != LTC_MP_LT) {
if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
}
/* compare to b */
if (mp_cmp(t1, b) != LTC_MP_EQ) {
err = CRYPT_INVALID_PACKET;
} else {
err = CRYPT_OK;
}
error:
mp_clear_multi(prime, b, t1, t2, NULL);
m_free(prime);
m_free(b);
m_free(t1);
m_free(t2);
return err;
}
/* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */
void buf_put_ecc_raw_pubkey_string(buffer *buf, ecc_key *key) {
unsigned long len = key->dp->size*2 + 1; unsigned long len = key->dp->size*2 + 1;
buf_putint(buf, len); buf_putint(buf, len);
int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len); int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len);
@ -50,117 +108,61 @@ void buf_put_ecc_pubkey_string(buffer *buf, ecc_key *key) {
buf_incrwritepos(buf, len); buf_incrwritepos(buf, len);
} }
// Copied from libtomcrypt ecc_import.c (version there is static), modified /* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */
// for different mp_int pointer without LTC_SOURCE ecc_key * buf_get_ecc_raw_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve) {
static int ecc_is_point(ecc_key *key) ecc_key *key = NULL;
{ int ret = DROPBEAR_FAILURE;
mp_int *prime, *b, *t1, *t2; const unsigned int size = curve->dp->size;
int err; buf_setpos(buf, 0);
unsigned int len = buf->len;
unsigned char first = buf_getbyte(buf);
if (first == 2 || first == 3) {
dropbear_log(LOG_WARNING, "Dropbear doesn't support ECC point compression");
return NULL;
}
if (first != 4 || len != 1+2*size) {
return NULL;
}
prime = m_malloc(sizeof(mp_int)); key = new_ecc_key();
b = m_malloc(sizeof(mp_int)); key->dp = curve->dp;
t1 = m_malloc(sizeof(mp_int));
t2 = m_malloc(sizeof(mp_int));
m_mp_init_multi(prime, b, t1, t2, NULL);
/* load prime and b */
if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }
if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }
/* compute y^2 */
if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
/* compute x^3 */
if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }
if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }
if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }
/* compute y^2 - x^3 */
if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
/* compute y^2 - x^3 + 3x */
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }
while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }
}
while (mp_cmp(t1, prime) != LTC_MP_LT) {
if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
}
/* compare to b */
if (mp_cmp(t1, b) != LTC_MP_EQ) {
err = CRYPT_INVALID_PACKET;
} else {
err = CRYPT_OK;
}
error:
mp_clear_multi(prime, b, t1, t2, NULL);
m_free(prime);
m_free(b);
m_free(t1);
m_free(t2);
return err;
}
ecc_key * buf_get_ecc_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve) { if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {
ecc_key *key = NULL; goto out;
int ret = DROPBEAR_FAILURE; }
const unsigned int size = curve->dp->size; buf_incrpos(buf, size);
buf_setpos(buf, 0);
unsigned int len = buf->len;
unsigned char first = buf_getbyte(buf);
if (first == 2 || first == 3) {
dropbear_log(LOG_WARNING, "Dropbear doesn't support ECC point compression");
return NULL;
}
if (first != 4 || len != 1+2*size) {
return NULL;
}
key = new_ecc_key(); if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {
key->dp = curve->dp; goto out;
}
buf_incrpos(buf, size);
if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) { mp_set(key->pubkey.z, 1);
goto out;
}
buf_incrpos(buf, size);
if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) { if (ecc_is_point(key) != CRYPT_OK) {
goto out; goto out;
} }
buf_incrpos(buf, size);
mp_set(key->pubkey.z, 1);
if (ecc_is_point(key) != CRYPT_OK) {
goto out;
}
// SEC1 3.2.3.1 Check that Q != 0 // SEC1 3.2.3.1 Check that Q != 0
if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) { if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) {
goto out; goto out;
} }
if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) { if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) {
goto out; goto out;
} }
ret = DROPBEAR_SUCCESS; ret = DROPBEAR_SUCCESS;
out: out:
if (ret == DROPBEAR_FAILURE) { if (ret == DROPBEAR_FAILURE) {
if (key) { if (key) {
ecc_free(key); ecc_free(key);
m_free(key); m_free(key);
key = NULL; key = NULL;
} }
} }
return key; return key;
} }
@ -168,56 +170,56 @@ out:
// a mp_int instead. // a mp_int instead.
mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key) mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key)
{ {
ecc_point *result = NULL; ecc_point *result = NULL;
mp_int *prime = NULL, *shared_secret = NULL; mp_int *prime = NULL, *shared_secret = NULL;
int err = DROPBEAR_FAILURE; int err = DROPBEAR_FAILURE;
/* type valid? */ /* type valid? */
if (private_key->type != PK_PRIVATE) { if (private_key->type != PK_PRIVATE) {
goto done; goto done;
} }
if (private_key->dp != public_key->dp) { if (private_key->dp != public_key->dp) {
goto done; goto done;
} }
/* make new point */ /* make new point */
result = ltc_ecc_new_point(); result = ltc_ecc_new_point();
if (result == NULL) { if (result == NULL) {
goto done; goto done;
} }
prime = m_malloc(sizeof(*prime)); prime = m_malloc(sizeof(*prime));
m_mp_init(prime); m_mp_init(prime);
if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) { if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) {
goto done; goto done;
} }
if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) { if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) {
goto done; goto done;
} }
err = DROPBEAR_SUCCESS; err = DROPBEAR_SUCCESS;
done: done:
if (err == DROPBEAR_SUCCESS) { if (err == DROPBEAR_SUCCESS) {
shared_secret = m_malloc(sizeof(*shared_secret)); shared_secret = m_malloc(sizeof(*shared_secret));
m_mp_init(shared_secret); m_mp_init(shared_secret);
mp_copy(result->x, shared_secret); mp_copy(result->x, shared_secret);
} }
if (prime) { if (prime) {
mp_clear(prime); mp_clear(prime);
m_free(prime); m_free(prime);
}
if (result)
{
ltc_ecc_del_point(result);
} }
if (result)
{
ltc_ecc_del_point(result);
}
if (err == DROPBEAR_FAILURE) { if (err == DROPBEAR_FAILURE) {
dropbear_exit("ECC error"); dropbear_exit("ECC error");
} }
return shared_secret; return shared_secret;
} }
#endif #endif

4
ecc.h
View File

@ -20,8 +20,8 @@ extern const struct dropbear_ecc_curve ecc_curve_nistp521;
// "pubkey" refers to a point, but LTC uses ecc_key structure for both public // "pubkey" refers to a point, but LTC uses ecc_key structure for both public
// and private keys // and private keys
void buf_put_ecc_pubkey_string(buffer *buf, ecc_key *key); void buf_put_ecc_raw_pubkey_string(buffer *buf, ecc_key *key);
ecc_key * buf_get_ecc_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve); ecc_key * buf_get_ecc_raw_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve);
int buf_get_ecc_privkey_string(buffer *buf, ecc_key *key); int buf_get_ecc_privkey_string(buffer *buf, ecc_key *key);
mp_int * dropbear_ecc_shared_secret(ecc_key *pub_key, ecc_key *priv_key); mp_int * dropbear_ecc_shared_secret(ecc_key *pub_key, ecc_key *priv_key);

View File

@ -104,7 +104,7 @@ static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
struct kex_ecdh_param *ecdh_param = gen_kexecdh_param(); struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey); kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
buf_put_ecc_pubkey_string(ses.writepayload, &ecdh_param->key); buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
free_kexecdh_param(ecdh_param); free_kexecdh_param(ecdh_param);
#endif #endif
} }