From 16a6b9c3c11a42703b6a95fdd027633b5e1cd1fa Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Mon, 7 Oct 2019 15:29:26 +0000 Subject: Sync with libtommath's "develop" again --- libtommath/bn_deprecated.c | 9 ++++++--- libtommath/bn_mp_fwrite.c | 15 +++++++++++---- libtommath/bn_mp_radix_size.c | 6 +++--- libtommath/bn_mp_to_radix.c | 24 +++++++++++++++++++----- libtommath/tommath.h | 10 +++++----- 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/libtommath/bn_deprecated.c b/libtommath/bn_deprecated.c index 19b0e49..60b3e16 100644 --- a/libtommath/bn_deprecated.c +++ b/libtommath/bn_deprecated.c @@ -113,7 +113,10 @@ mp_err mp_toom_sqr(const mp_int *a, mp_int *b) #ifdef S_MP_REVERSE_C void bn_reverse(unsigned char *s, int len) { - s_mp_reverse(s, len); + if (len < 0) { + return MP_VAL; + } + s_mp_reverse(s, (size_t)len); } #endif #ifdef BN_MP_TC_AND_C @@ -304,13 +307,13 @@ mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) if (maxlen < 0) { return MP_VAL; } - return mp_to_radix(a, str, (size_t)maxlen, radix); + return mp_to_radix(a, str, (size_t)maxlen, NULL, radix); } #endif #ifdef BN_MP_TORADIX_C mp_err mp_toradix(const mp_int *a, char *str, int radix) { - return mp_to_radix(a, str, SIZE_MAX, radix); + return mp_to_radix(a, str, SIZE_MAX, NULL, radix); } #endif #endif diff --git a/libtommath/bn_mp_fwrite.c b/libtommath/bn_mp_fwrite.c index eaa5d06..abe2e67 100644 --- a/libtommath/bn_mp_fwrite.c +++ b/libtommath/bn_mp_fwrite.c @@ -9,9 +9,15 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) char *buf; mp_err err; int len; + size_t written; - if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { - return err; + /* TODO: this function is not in this PR */ + if (MP_HAS(MP_RADIX_SIZE_OVERESTIMATE)) { + /* if ((err = mp_radix_size_overestimate(&t, base, &len)) != MP_OKAY) goto LBL_ERR; */ + } else { + if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { + return err; + } } buf = (char *) MP_MALLOC((size_t)len); @@ -19,16 +25,17 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) return MP_MEM; } - if ((err = mp_to_radix(a, buf, (size_t)len, radix)) != MP_OKAY) { + if ((err = mp_to_radix(a, buf, (size_t)len, &written, radix)) != MP_OKAY) { goto LBL_ERR; } - if (fwrite(buf, (size_t)len, 1uL, stream) != 1uL) { + if (fwrite(buf, written, 1uL, stream) != 1uL) { err = MP_ERR; goto LBL_ERR; } err = MP_OKAY; + LBL_ERR: MP_FREE_BUFFER(buf, (size_t)len); return err; diff --git a/libtommath/bn_mp_radix_size.c b/libtommath/bn_mp_radix_size.c index 339b21b..b96f487 100644 --- a/libtommath/bn_mp_radix_size.c +++ b/libtommath/bn_mp_radix_size.c @@ -3,11 +3,11 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* returns size of ASCII reprensentation */ +/* returns size of ASCII representation */ mp_err mp_radix_size(const mp_int *a, int radix, int *size) { mp_err err; - int digs; + int digs; mp_int t; mp_digit d; @@ -25,7 +25,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size) /* special case for binary */ if (radix == 2) { - *size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1; + *size = (mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1); return MP_OKAY; } diff --git a/libtommath/bn_mp_to_radix.c b/libtommath/bn_mp_to_radix.c index 0af0bb7..ad12142 100644 --- a/libtommath/bn_mp_to_radix.c +++ b/libtommath/bn_mp_to_radix.c @@ -5,9 +5,10 @@ /* stores a bignum as a ASCII string in a given radix (2..64) * - * Stores upto maxlen-1 chars and always a NULL byte + * Stores upto "size - 1" chars and always a NULL byte, puts the number of characters + * written, including the '\0', in "written". */ -mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) +mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) { size_t digs; mp_err err; @@ -15,7 +16,13 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) mp_digit d; char *_s = str; - /* check range of the maxlen, radix */ + + /* If we want to fill a bucket we need a bucket in the first place. */ + if (str == NULL) { + return MP_VAL; + } + + /* check range of radix and size*/ if ((maxlen < 2u) || (radix < 2) || (radix > 64)) { return MP_VAL; } @@ -24,6 +31,9 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) if (MP_IS_ZERO(a)) { *str++ = '0'; *str = '\0'; + if (written != NULL) { + *written = 2u; + } return MP_OKAY; } @@ -43,11 +53,12 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) /* subtract a char */ --maxlen; } - digs = 0u; while (!MP_IS_ZERO(&t)) { if (--maxlen < 1u) { /* no more room */ + /* TODO: It could mimic mp_to_radix_n if that is not an error + or at least not this error (MP_ITER or a new one?). */ err = MP_VAL; break; } @@ -57,7 +68,6 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) *str++ = mp_s_rmap[d]; ++digs; } - /* reverse the digits of the string. In this case _s points * to the first digit [exluding the sign] of the number */ @@ -65,6 +75,10 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) /* append a NULL so the string is properly terminated */ *str = '\0'; + digs++; + if (written != NULL) { + *written = (a->sign == MP_NEG) ? digs + 1u: digs; + } LBL_ERR: mp_clear(&t); diff --git a/libtommath/tommath.h b/libtommath/tommath.h index 8c67c0e..0b04a5c 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -718,7 +718,7 @@ mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR; MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR; MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR; -mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) MP_WUR; +mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR; mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR; #ifndef MP_NO_FILE @@ -738,10 +738,10 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR; #define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10)) #define mp_tohex(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex") mp_toradix((M), (S), 16)) -#define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), 2) -#define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), 8) -#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), 10) -#define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), 16) +#define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), NULL, 2) +#define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), NULL, 8) +#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), NULL, 10) +#define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), NULL, 16) #ifdef __cplusplus } -- cgit v0.12