From b4bd23b4d2a4c640880b49069e02cd598dd03416 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 26 May 2020 17:36:47 +0200 Subject: Update LibTomMath to 1.2.0 (#84) * 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. --- libtomcrypt/src/math/ltm_desc.c | 63 +++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'libtomcrypt') diff --git a/libtomcrypt/src/math/ltm_desc.c b/libtomcrypt/src/math/ltm_desc.c index 3e2a0c9..24e8611 100644 --- a/libtomcrypt/src/math/ltm_desc.c +++ b/libtomcrypt/src/math/ltm_desc.c @@ -15,11 +15,14 @@ #include 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}, }; /** @@ -27,11 +30,11 @@ static const struct { @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; } @@ -39,17 +42,27 @@ static int mpi_to_ltc_error(int err) 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); } @@ -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) @@ -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; @@ -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; @@ -175,14 +186,14 @@ 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 */ @@ -190,7 +201,7 @@ 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 */ @@ -198,7 +209,7 @@ 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 */ @@ -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; @@ -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, -- cgit v1.2.3