libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / hcrypto / libtommath / bn_mp_to_radix.c
blob7fa86cae1dabbd175ffa35e2b32424d94c9824b9
1 #include "tommath_private.h"
2 #ifdef BN_MP_TO_RADIX_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* stores a bignum as a ASCII string in a given radix (2..64)
8 * Stores upto "size - 1" chars and always a NULL byte, puts the number of characters
9 * written, including the '\0', in "written".
11 mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix)
13 size_t digs;
14 mp_err err;
15 mp_int t;
16 mp_digit d;
17 char *_s = str;
19 /* check range of radix and size*/
20 if (maxlen < 2u) {
21 return MP_BUF;
23 if ((radix < 2) || (radix > 64)) {
24 return MP_VAL;
27 /* quick out if its zero */
28 if (MP_IS_ZERO(a)) {
29 *str++ = '0';
30 *str = '\0';
31 if (written != NULL) {
32 *written = 2u;
34 return MP_OKAY;
37 if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
38 return err;
41 /* if it is negative output a - */
42 if (t.sign == MP_NEG) {
43 /* we have to reverse our digits later... but not the - sign!! */
44 ++_s;
46 /* store the flag and mark the number as positive */
47 *str++ = '-';
48 t.sign = MP_ZPOS;
50 /* subtract a char */
51 --maxlen;
53 digs = 0u;
54 while (!MP_IS_ZERO(&t)) {
55 if (--maxlen < 1u) {
56 /* no more room */
57 err = MP_BUF;
58 goto LBL_ERR;
60 if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
61 goto LBL_ERR;
63 *str++ = mp_s_rmap[d];
64 ++digs;
66 /* reverse the digits of the string. In this case _s points
67 * to the first digit [exluding the sign] of the number
69 s_mp_reverse((unsigned char *)_s, digs);
71 /* append a NULL so the string is properly terminated */
72 *str = '\0';
73 digs++;
75 if (written != NULL) {
76 *written = (a->sign == MP_NEG) ? (digs + 1u): digs;
79 LBL_ERR:
80 mp_clear(&t);
81 return err;
84 #endif