Skip to content

Commit

Permalink
Update LibTomMath to 1.2.0 (#84)
Browse files Browse the repository at this point in the history
* update C files

* update other files

* update headers

* update makefiles

* remove mp_set/get_double()

* use ltm 1.2.0 API

* update ltm_desc

* use bundled tommath if system-tommath is too old

* XMALLOC etc. were changed to MP_MALLOC etc.
  • Loading branch information
sjaeckel authored May 26, 2020
1 parent 724e61f commit b4bd23b
Show file tree
Hide file tree
Showing 229 changed files with 6,033 additions and 31,297 deletions.
2 changes: 1 addition & 1 deletion bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void m_mp_free_multi(mp_int **mp, ...)

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) {
if (mp_from_ubin(mp, (unsigned char*)bytes, len) != MP_OKAY) {
dropbear_exit("Mem alloc error");
}
}
Expand Down
12 changes: 6 additions & 6 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,18 @@ void buf_putbytes(buffer *buf, const unsigned char *bytes, unsigned int len) {
/* for our purposes we only need positive (or 0) numbers, so will
* fail if we get negative numbers */
void buf_putmpint(buffer* buf, mp_int * mp) {

size_t written;
unsigned int len, pad = 0;
TRACE2(("enter buf_putmpint"))

dropbear_assert(mp != NULL);

if (SIGN(mp) == MP_NEG) {
if (mp_isneg(mp)) {
dropbear_exit("negative bignum");
}

/* zero check */
if (USED(mp) == 1 && DIGIT(mp, 0) == 0) {
if (mp_iszero(mp)) {
len = 0;
} else {
/* SSH spec requires padding for mpints with the MSB set, this code
Expand All @@ -339,10 +339,10 @@ void buf_putmpint(buffer* buf, mp_int * mp) {
if (pad) {
buf_putbyte(buf, 0x00);
}
if (mp_to_unsigned_bin(mp, buf_getwriteptr(buf, len-pad)) != MP_OKAY) {
if (mp_to_ubin(mp, buf_getwriteptr(buf, len-pad), len-pad, &written) != MP_OKAY) {
dropbear_exit("mpint error");
}
buf_incrwritepos(buf, len-pad);
buf_incrwritepos(buf, written);
}

TRACE2(("leave buf_putmpint"))
Expand Down Expand Up @@ -370,7 +370,7 @@ int buf_getmpint(buffer* buf, mp_int* mp) {
return DROPBEAR_FAILURE;
}

if (mp_read_unsigned_bin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
if (mp_from_ubin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
return DROPBEAR_FAILURE;
}

Expand Down
4 changes: 1 addition & 3 deletions common-kex.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,7 @@ struct kex_dh_param *gen_kexdh_param() {
/* read the prime and generator*/
load_dh_p(&dh_p);

if (mp_set_int(&dh_g, DH_G_VAL) != MP_OKAY) {
dropbear_exit("Diffie-Hellman error");
}
mp_set_ul(&dh_g, DH_G_VAL);

/* calculate q = (p-1)/2 */
/* dh_priv is just a temp var here */
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -538,15 +538,15 @@ AC_ARG_ENABLE(bundled-libtom,
AC_MSG_NOTICE(Forcing bundled libtom*)
else
BUNDLED_LIBTOM=0
AC_CHECK_LIB(tommath, mp_exptmod, LIBTOM_LIBS="-ltommath $LIBTOM_LIBS",
AC_CHECK_LIB(tommath, mp_to_ubin, LIBTOM_LIBS="-ltommath $LIBTOM_LIBS",
[AC_MSG_ERROR([Missing system libtommath and --disable-bundled-libtom was specified])] )
AC_CHECK_LIB(tomcrypt, register_cipher, LIBTOM_LIBS="-ltomcrypt $LIBTOM_LIBS",
[AC_MSG_ERROR([Missing system libtomcrypt and --disable-bundled-libtom was specified])] )
fi
],
[
BUNDLED_LIBTOM=0
AC_CHECK_LIB(tommath, mp_exptmod, LIBTOM_LIBS="-ltommath $LIBTOM_LIBS", BUNDLED_LIBTOM=1)
AC_CHECK_LIB(tommath, mp_to_ubin, LIBTOM_LIBS="-ltommath $LIBTOM_LIBS", BUNDLED_LIBTOM=1)
AC_CHECK_LIB(tomcrypt, register_cipher, LIBTOM_LIBS="-ltomcrypt $LIBTOM_LIBS", BUNDLED_LIBTOM=1)
]
)
Expand Down
10 changes: 10 additions & 0 deletions dbmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,13 @@ void m_free_direct(void* ptr) {
}

#endif /* DROPBEAR_TRACKING_MALLOC */

void * m_realloc_ltm(void* ptr, size_t oldsize, size_t newsize) {
(void)oldsize;
return m_realloc(ptr, newsize);
}

void m_free_ltm(void *mem, size_t size) {
(void)size;
m_free_direct(mem);
}
13 changes: 7 additions & 6 deletions dss.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ void buf_put_dss_sign(buffer* buf, const dropbear_dss_key *key, const buffer *da
unsigned char msghash[SHA1_HASH_SIZE];
unsigned int writelen;
unsigned int i;
size_t written;
DEF_MP_INT(dss_k);
DEF_MP_INT(dss_m);
DEF_MP_INT(dss_temp1);
Expand Down Expand Up @@ -340,31 +341,31 @@ void buf_put_dss_sign(buffer* buf, const dropbear_dss_key *key, const buffer *da
buf_putstring(buf, SSH_SIGNKEY_DSS, SSH_SIGNKEY_DSS_LEN);
buf_putint(buf, 2*SHA1_HASH_SIZE);

writelen = mp_unsigned_bin_size(&dss_r);
writelen = mp_ubin_size(&dss_r);
dropbear_assert(writelen <= SHA1_HASH_SIZE);
/* need to pad to 160 bits with leading zeros */
for (i = 0; i < SHA1_HASH_SIZE - writelen; i++) {
buf_putbyte(buf, 0);
}
if (mp_to_unsigned_bin(&dss_r, buf_getwriteptr(buf, writelen))
if (mp_to_ubin(&dss_r, buf_getwriteptr(buf, writelen), writelen, &written)
!= MP_OKAY) {
dropbear_exit("DSS error");
}
mp_clear(&dss_r);
buf_incrwritepos(buf, writelen);
buf_incrwritepos(buf, written);

writelen = mp_unsigned_bin_size(&dss_s);
writelen = mp_ubin_size(&dss_s);
dropbear_assert(writelen <= SHA1_HASH_SIZE);
/* need to pad to 160 bits with leading zeros */
for (i = 0; i < SHA1_HASH_SIZE - writelen; i++) {
buf_putbyte(buf, 0);
}
if (mp_to_unsigned_bin(&dss_s, buf_getwriteptr(buf, writelen))
if (mp_to_ubin(&dss_s, buf_getwriteptr(buf, writelen), writelen, &written)
!= MP_OKAY) {
dropbear_exit("DSS error");
}
mp_clear(&dss_s);
buf_incrwritepos(buf, writelen);
buf_incrwritepos(buf, written);

mp_clear_multi(&dss_k, &dss_temp1, &dss_temp2, &dss_r, &dss_s,
&dss_m, NULL);
Expand Down
4 changes: 2 additions & 2 deletions ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ ecc_key * buf_get_ecc_raw_pubkey(buffer *buf, const struct dropbear_ecc_curve *c
key = new_ecc_key();
key->dp = curve->dp;

if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {
if (mp_from_ubin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {
TRACE(("failed to read x"))
goto out;
}
buf_incrpos(buf, size);

if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {
if (mp_from_ubin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {
TRACE(("failed to read y"))
goto out;
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void fuzz_get_socket_address(int UNUSED(fd), char **local_host, char **local_por
void fuzz_fake_send_kexdh_reply(void) {
assert(!ses.dh_K);
m_mp_alloc_init_multi(&ses.dh_K, NULL);
mp_set_int(ses.dh_K, 12345678);
mp_set_ul(ses.dh_K, 12345678uL);
finish_kexhashbuf();
}

Expand Down
5 changes: 1 addition & 4 deletions genrsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ dropbear_rsa_key * gen_rsa_priv_key(unsigned int size) {
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");
exit(1);
}
mp_set_ul(key->e, RSA_E);

while (1) {
getrsaprime(key->p, &pminus, key->e, size/16);
Expand Down
11 changes: 7 additions & 4 deletions keyimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ static sign_key *openssh_read(const char *filename, const char * UNUSED(passphra
goto error;
}
m_mp_alloc_init_multi((mp_int**)&ecc->k, NULL);
if (mp_read_unsigned_bin(ecc->k, private_key_bytes, private_key_len)
if (mp_from_ubin(ecc->k, private_key_bytes, private_key_len)
!= MP_OKAY) {
errmsg = "Error parsing ECC key";
goto error;
Expand Down Expand Up @@ -1142,19 +1142,22 @@ static int openssh_write(const char *filename, sign_key *key,
unsigned long pubkey_size = 2*curve_size+1;
int k_size;
int err = 0;
size_t written;

/* version. less than 10 bytes */
buf_incrwritepos(seq_buf,
ber_write_id_len(buf_getwriteptr(seq_buf, 10), 2, 1, 0));
buf_putbyte(seq_buf, 1);

/* privateKey */
k_size = mp_unsigned_bin_size((*eck)->k);
k_size = mp_ubin_size((*eck)->k);
dropbear_assert(k_size <= curve_size);
buf_incrwritepos(seq_buf,
ber_write_id_len(buf_getwriteptr(seq_buf, 10), 4, k_size, 0));
mp_to_unsigned_bin((*eck)->k, buf_getwriteptr(seq_buf, k_size));
buf_incrwritepos(seq_buf, k_size);
if (mp_to_ubin((*eck)->k, buf_getwriteptr(seq_buf, k_size), k_size, &written) != MP_OKAY) {
dropbear_exit("ECC error");
}
buf_incrwritepos(seq_buf, written);

/* SECGCurveNames */
switch (key->type)
Expand Down
63 changes: 36 additions & 27 deletions libtomcrypt/src/math/ltm_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,54 @@
#include <tommath.h>

static const struct {
int mpi_code, ltc_code;
mp_err mpi_code;
int ltc_code;
} mpi_to_ltc_codes[] = {
{ MP_OKAY , CRYPT_OK},
{ MP_MEM , CRYPT_MEM},
{ MP_VAL , CRYPT_INVALID_ARG},
{ MP_ITER , CRYPT_INVALID_PACKET},
{ MP_BUF , CRYPT_BUFFER_OVERFLOW},
};

/**
Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
@param err The error to convert
@return The equivalent LTC error code or CRYPT_ERROR if none found
*/
static int mpi_to_ltc_error(int err)
static int mpi_to_ltc_error(mp_err err)
{
int x;
size_t x;

for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) {
for (x = 0; x < sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0]); x++) {
if (err == mpi_to_ltc_codes[x].mpi_code) {
return mpi_to_ltc_codes[x].ltc_code;
}
}
return CRYPT_ERROR;
}

static int init(void **a)
static int init_mpi(void **a)
{
int err;

LTC_ARGCHK(a != NULL);

*a = XCALLOC(1, sizeof(mp_int));
if (*a == NULL) {
return CRYPT_MEM;
} else {
return CRYPT_OK;
}
}

static int init(void **a)
{
int err;

LTC_ARGCHK(a != NULL);

if ((err = init_mpi(a)) != CRYPT_OK) {
return err;
}
if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
XFREE(*a);
}
Expand Down Expand Up @@ -79,23 +92,25 @@ static int copy(void *a, void *b)

static int init_copy(void **a, void *b)
{
if (init(a) != CRYPT_OK) {
return CRYPT_MEM;
}
return copy(b, *a);
int err;
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(b != NULL);
if ((err = init_mpi(a)) != CRYPT_OK) return err;
return mpi_to_ltc_error(mp_init_copy(*a, b));
}

/* ---- trivial ---- */
static int set_int(void *a, ltc_mp_digit b)
{
LTC_ARGCHK(a != NULL);
return mpi_to_ltc_error(mp_set_int(a, b));
mp_set_u32(a, b);
return CRYPT_OK;
}

static unsigned long get_int(void *a)
{
LTC_ARGCHK(a != NULL);
return mp_get_int(a);
return mp_get_ul(a);
}

static ltc_mp_digit get_digit(void *a, int n)
Expand All @@ -116,11 +131,9 @@ static int get_digit_count(void *a)

static int compare(void *a, void *b)
{
int ret;
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(b != NULL);
ret = mp_cmp(a, b);
switch (ret) {
switch (mp_cmp(a, b)) {
case MP_LT: return LTC_MP_LT;
case MP_EQ: return LTC_MP_EQ;
case MP_GT: return LTC_MP_GT;
Expand All @@ -130,10 +143,8 @@ static int compare(void *a, void *b)

static int compare_d(void *a, ltc_mp_digit b)
{
int ret;
LTC_ARGCHK(a != NULL);
ret = mp_cmp_d(a, b);
switch (ret) {
switch (mp_cmp_d(a, b)) {
case MP_LT: return LTC_MP_LT;
case MP_EQ: return LTC_MP_EQ;
case MP_GT: return LTC_MP_GT;
Expand Down Expand Up @@ -175,30 +186,30 @@ static int write_radix(void *a, char *b, int radix)
{
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(b != NULL);
return mpi_to_ltc_error(mp_toradix(a, b, radix));
return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, NULL, radix));
}

/* get size as unsigned char string */
static unsigned long unsigned_size(void *a)
{
LTC_ARGCHK(a != NULL);
return mp_unsigned_bin_size(a);
return (unsigned long)mp_ubin_size(a);
}

/* store */
static int unsigned_write(void *a, unsigned char *b)
{
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(b != NULL);
return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
return mpi_to_ltc_error(mp_to_ubin(a, b, SIZE_MAX, NULL));
}

/* read */
static int unsigned_read(void *a, unsigned char *b, unsigned long len)
{
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(b != NULL);
return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
return mpi_to_ltc_error(mp_from_ubin(a, b, (size_t)len));
}

/* add */
Expand Down Expand Up @@ -403,9 +414,7 @@ static int isprime(void *a, int b, int *c)
int err;
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(c != NULL);
if (b == 0) {
b = LTC_MILLER_RABIN_REPS;
} /* if */
b = mp_prime_rabin_miller_trials(mp_count_bits(a));
err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
*c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
return err;
Expand All @@ -420,7 +429,7 @@ static int set_rand(void *a, int size)
const ltc_math_descriptor ltm_desc = {

"LibTomMath",
(int)DIGIT_BIT,
(int)MP_DIGIT_BIT,

&init,
&init_copy,
Expand Down
Loading

0 comments on commit b4bd23b

Please sign in to comment.