From d9e790e7dc7737158c9affb7087035dd2c31c003 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 23 May 2013 22:18:16 +0800 Subject: [PATCH] Add m_mp_alloc_init_multi() helper --HG-- branch : ecc --- bignum.c | 16 ++++++++++++++++ bignum.h | 1 + common-kex.c | 3 +-- dss.c | 9 ++------- ecc.c | 9 +++------ gendss.c | 7 +------ genrsa.c | 11 ++--------- rsa.c | 11 +++-------- 8 files changed, 29 insertions(+), 38 deletions(-) diff --git a/bignum.c b/bignum.c index 886568d..e9810b3 100644 --- a/bignum.c +++ b/bignum.c @@ -52,6 +52,22 @@ void m_mp_init_multi(mp_int *mp, ...) va_end(args); } +void m_mp_alloc_init_multi(mp_int **mp, ...) +{ + mp_int** cur_arg = mp; + va_list args; + + va_start(args, mp); /* init args to next argument from caller */ + while (cur_arg != NULL) { + *cur_arg = m_malloc(sizeof(mp_int)); + if (mp_init(*cur_arg) != MP_OKAY) { + dropbear_exit("Mem alloc error"); + } + cur_arg = va_arg(args, mp_int**); + } + va_end(args); +} + void bytes_to_mp(mp_int *mp, const unsigned char* bytes, unsigned int len) { if (mp_read_unsigned_bin(mp, (unsigned char*)bytes, len) != MP_OKAY) { diff --git a/bignum.h b/bignum.h index 11353c6..f9710d7 100644 --- a/bignum.h +++ b/bignum.h @@ -30,6 +30,7 @@ void m_mp_init(mp_int *mp); void m_mp_init_multi(mp_int *mp, ...) ATTRIB_SENTINEL; +void m_mp_alloc_init_multi(mp_int **mp, ...) ATTRIB_SENTINEL; void bytes_to_mp(mp_int *mp, const unsigned char* bytes, unsigned int len); void hash_process_mp(const struct ltc_hash_descriptor *hash_desc, hash_state *hs, mp_int *mp); diff --git a/common-kex.c b/common-kex.c index 0cd3db3..a32ca6d 100644 --- a/common-kex.c +++ b/common-kex.c @@ -633,8 +633,7 @@ void kexdh_comb_key(struct kex_dh_param *param, mp_int *dh_pub_them, } /* K = e^y mod p = f^x mod p */ - ses.dh_K = (mp_int*)m_malloc(sizeof(mp_int)); - m_mp_init(ses.dh_K); + m_mp_alloc_init_multi(&ses.dh_K, NULL); if (mp_exptmod(dh_pub_them, ¶m->priv, &dh_p, ses.dh_K) != MP_OKAY) { dropbear_exit("Diffie-Hellman error"); } diff --git a/dss.c b/dss.c index 9817392..b3b2bb1 100644 --- a/dss.c +++ b/dss.c @@ -47,11 +47,7 @@ int buf_get_dss_pub_key(buffer* buf, dropbear_dss_key *key) { TRACE(("enter buf_get_dss_pub_key")) dropbear_assert(key != NULL); - key->p = m_malloc(sizeof(mp_int)); - key->q = m_malloc(sizeof(mp_int)); - key->g = m_malloc(sizeof(mp_int)); - key->y = m_malloc(sizeof(mp_int)); - m_mp_init_multi(key->p, key->q, key->g, key->y, NULL); + m_mp_alloc_init_multi(&key->p, &key->q, &key->g, &key->y, NULL); key->x = NULL; buf_incrpos(buf, 4+SSH_SIGNKEY_DSS_LEN); /* int + "ssh-dss" */ @@ -87,8 +83,7 @@ int buf_get_dss_priv_key(buffer* buf, dropbear_dss_key *key) { return DROPBEAR_FAILURE; } - key->x = m_malloc(sizeof(mp_int)); - m_mp_init(key->x); + m_mp_alloc_init_multi(&key->x, NULL); ret = buf_getmpint(buf, key->x); if (ret == DROPBEAR_FAILURE) { m_free(key->x); diff --git a/ecc.c b/ecc.c index 03f8864..3e0763c 100644 --- a/ecc.c +++ b/ecc.c @@ -72,11 +72,8 @@ struct dropbear_ecc_curve* curve_for_dp(const ltc_ecc_set_type *dp) { ecc_key * new_ecc_key(void) { ecc_key *key = m_malloc(sizeof(*key)); - key->pubkey.x = m_malloc(sizeof(mp_int)); - key->pubkey.y = m_malloc(sizeof(mp_int)); - key->pubkey.z = 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_alloc_init_multi(&key->pubkey.x, &key->pubkey.y, + &key->pubkey.z, &key->k, NULL); return key; } @@ -92,7 +89,7 @@ static int ecc_is_point(ecc_key *key) t1 = m_malloc(sizeof(mp_int)); t2 = m_malloc(sizeof(mp_int)); - m_mp_init_multi(prime, b, t1, t2, NULL); + m_mp_alloc_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; } diff --git a/gendss.c b/gendss.c index be8f89f..2785aec 100644 --- a/gendss.c +++ b/gendss.c @@ -53,12 +53,7 @@ dropbear_dss_key * gen_dss_priv_key(unsigned int size) { key = m_malloc(sizeof(*key)); - key->p = (mp_int*)m_malloc(sizeof(mp_int)); - key->q = (mp_int*)m_malloc(sizeof(mp_int)); - key->g = (mp_int*)m_malloc(sizeof(mp_int)); - key->y = (mp_int*)m_malloc(sizeof(mp_int)); - key->x = (mp_int*)m_malloc(sizeof(mp_int)); - m_mp_init_multi(key->p, key->q, key->g, key->y, key->x, NULL); + m_mp_alloc_init_multi(&key->p, &key->q, &key->g, &key->y, &key->x, NULL); getq(key); getp(key, size/8); diff --git a/genrsa.c b/genrsa.c index 465502b..5df191e 100644 --- a/genrsa.c +++ b/genrsa.c @@ -50,15 +50,8 @@ dropbear_rsa_key * gen_rsa_priv_key(unsigned int size) { } key = m_malloc(sizeof(*key)); - - key->e = (mp_int*)m_malloc(sizeof(mp_int)); - key->n = (mp_int*)m_malloc(sizeof(mp_int)); - key->d = (mp_int*)m_malloc(sizeof(mp_int)); - key->p = (mp_int*)m_malloc(sizeof(mp_int)); - key->q = (mp_int*)m_malloc(sizeof(mp_int)); - - m_mp_init_multi(key->e, key->n, key->d, key->p, key->q, - &pminus, &lcm, &qminus, NULL); + m_mp_alloc_init_multi(&key->e, &key->n, &key->d, &key->p, &key->q, NULL); + m_mp_init_multi(&pminus, &lcm, &qminus, NULL); if (mp_set_int(key->e, RSA_E) != MP_OKAY) { fprintf(stderr, "RSA generation failed\n"); diff --git a/rsa.c b/rsa.c index 6fd30b8..92adee4 100644 --- a/rsa.c +++ b/rsa.c @@ -50,9 +50,7 @@ int buf_get_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) { int ret = DROPBEAR_FAILURE; TRACE(("enter buf_get_rsa_pub_key")) dropbear_assert(key != NULL); - key->e = m_malloc(sizeof(mp_int)); - key->n = m_malloc(sizeof(mp_int)); - m_mp_init_multi(key->e, key->n, NULL); + m_mp_alloc_init_multi(&key->e, &key->n, NULL); key->d = NULL; key->p = NULL; key->q = NULL; @@ -98,8 +96,7 @@ int buf_get_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) { key->p = NULL; key->q = NULL; - key->d = m_malloc(sizeof(mp_int)); - m_mp_init(key->d); + m_mp_alloc_init_multi(&key->d); if (buf_getmpint(buf, key->d) == DROPBEAR_FAILURE) { TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE")) goto out; @@ -108,9 +105,7 @@ int buf_get_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) { if (buf->pos == buf->len) { /* old Dropbear private keys didn't keep p and q, so we will ignore them*/ } else { - key->p = m_malloc(sizeof(mp_int)); - key->q = m_malloc(sizeof(mp_int)); - m_mp_init_multi(key->p, key->q, NULL); + m_mp_alloc_init_multi(&key->p, &key->q, NULL); if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE) { TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE"))