Skip to content

Commit 08d281c

Browse files
committedDec 5, 2019
introduce MP_MAX_DIGIT_COUNT to prevent overflow
1 parent 3a744dc commit 08d281c

File tree

7 files changed

+39
-17
lines changed

7 files changed

+39
-17
lines changed
 

‎demo/shared.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ void print_header(void)
4545
printf("Size of mp_digit: %u\n", (unsigned int)sizeof(mp_digit));
4646
printf("Size of mp_word: %u\n", (unsigned int)sizeof(mp_word));
4747
printf("MP_DIGIT_BIT: %d\n", MP_DIGIT_BIT);
48-
printf("MP_PREC: %d\n", MP_PREC);
48+
printf("MP_DEFAULT_DIGIT_COUNT: %d\n", MP_DEFAULT_DIGIT_COUNT);
4949
}

‎demo/test.c

+7
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,8 @@ static int test_s_mp_radix_size_overestimate(void)
22202220
284u, 283u, 281u, 280u, 279u, 278u, 277u, 276u, 275u,
22212221
273u, 272u
22222222
};
2223+
2224+
#if 0
22232225
size_t big_results[65] = {
22242226
0u, 0u, 0u, 1354911329u, 1073741825u,
22252227
924870867u, 830760078u, 764949110u, 715827883u, 677455665u,
@@ -2235,6 +2237,7 @@ static int test_s_mp_radix_size_overestimate(void)
22352237
371449582u, 369786879u, 368168034u, 366591092u, 365054217u,
22362238
363555684u, 362093873u, 360667257u, 359274399u, 357913942
22372239
};
2240+
#endif
22382241

22392242
/* *INDENT-ON* */
22402243
if ((err = mp_init(&a)) != MP_OKAY) goto LBL_ERR;
@@ -2265,6 +2268,8 @@ static int test_s_mp_radix_size_overestimate(void)
22652268
}
22662269
a.sign = MP_ZPOS;
22672270
}
2271+
2272+
#if 0
22682273
if ((err = mp_2expt(&a, INT_MAX - 1)) != MP_OKAY) {
22692274
goto LBL_ERR;
22702275
}
@@ -2292,6 +2297,8 @@ static int test_s_mp_radix_size_overestimate(void)
22922297
}
22932298
a.sign = MP_ZPOS;
22942299
}
2300+
#endif
2301+
22952302
mp_clear(&a);
22962303
return EXIT_SUCCESS;
22972304
LBL_ERR:

‎mp_grow.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ mp_err mp_grow(mp_int *a, int size)
88
{
99
/* if the alloc size is smaller alloc more ram */
1010
if (a->alloc < size) {
11-
/* TODO */
11+
mp_digit *dp;
12+
13+
if (size > MP_MAX_DIGIT_COUNT) {
14+
return MP_MEM;
15+
}
16+
1217
/* reallocate the array a->dp
1318
*
1419
* We store the return in a temporary variable
1520
* in case the operation failed we don't want
1621
* to overwrite the dp member of a.
1722
*/
18-
mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp,
19-
(size_t)a->alloc * sizeof(mp_digit),
20-
(size_t)size * sizeof(mp_digit));
23+
dp = (mp_digit *) MP_REALLOC(a->dp,
24+
(size_t)a->alloc * sizeof(mp_digit),
25+
(size_t)size * sizeof(mp_digit));
2126
if (dp == NULL) {
2227
/* reallocation failed but "a" is still valid [can be freed] */
2328
return MP_MEM;

‎mp_init.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
mp_err mp_init(mp_int *a)
88
{
99
/* allocate memory required and clear it */
10-
a->dp = (mp_digit *) MP_CALLOC((size_t)MP_PREC, sizeof(mp_digit));
10+
a->dp = (mp_digit *) MP_CALLOC((size_t)MP_DEFAULT_DIGIT_COUNT, sizeof(mp_digit));
1111
if (a->dp == NULL) {
1212
return MP_MEM;
1313
}
1414

1515
/* set the used to zero, allocated digits to the default precision
1616
* and sign to positive */
1717
a->used = 0;
18-
a->alloc = MP_PREC;
18+
a->alloc = MP_DEFAULT_DIGIT_COUNT;
1919
a->sign = MP_ZPOS;
2020

2121
return MP_OKAY;

‎mp_init_size.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
/* init an mp_init for a given size */
77
mp_err mp_init_size(mp_int *a, int size)
88
{
9-
size = MP_MAX(MP_MIN_PREC, size);
9+
size = MP_MAX(MP_MIN_DIGIT_COUNT, size);
10+
11+
if (size > MP_MAX_DIGIT_COUNT) {
12+
return MP_MEM;
13+
}
1014

11-
/*TODO*/
1215
/* alloc mem */
1316
a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));
1417
if (a->dp == NULL) {

‎mp_shrink.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* shrink a bignum */
77
mp_err mp_shrink(mp_int *a)
88
{
9-
int alloc = MP_MAX(MP_MIN_PREC, a->used);
9+
int alloc = MP_MAX(MP_MIN_DIGIT_COUNT, a->used);
1010
if (a->alloc != alloc) {
1111
mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp,
1212
(size_t)a->alloc * sizeof(mp_digit),

‎tommath_private.h

+14-7
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,29 @@ typedef uint64_t mp_word;
140140

141141
MP_STATIC_ASSERT(correct_word_size, sizeof(mp_word) == (2u * sizeof(mp_digit)))
142142

143-
/* default precision */
144-
#ifndef MP_PREC
143+
/* default number of digits */
144+
#ifndef MP_DEFAULT_DIGIT_COUNT
145145
# ifndef MP_LOW_MEM
146-
# define MP_PREC 32 /* default digits of precision */
146+
# define MP_DEFAULT_DIGIT_COUNT 32
147147
# else
148-
# define MP_PREC 8 /* default digits of precision */
148+
# define MP_DEFAULT_DIGIT_COUNT 8
149149
# endif
150150
#endif
151151

152-
/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC
152+
/* Minimum number of available digits in mp_int, MP_DEFAULT_DIGIT_COUNT >= MP_MIN_DIGIT_COUNT
153153
* - Must be at least 3 for s_mp_div_school.
154154
* - Must be large enough such that the mp_set_u64 setter can
155155
* store uint64_t in the mp_int without growing
156156
*/
157-
#define MP_MIN_PREC MP_MAX(3, (((int)MP_SIZEOF_BITS(uint64_t) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
158-
MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
157+
#define MP_MIN_DIGIT_COUNT MP_MAX(3, (((int)MP_SIZEOF_BITS(uint64_t) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
158+
MP_STATIC_ASSERT(prec_geq_min_prec, MP_DEFAULT_DIGIT_COUNT >= MP_MIN_DIGIT_COUNT)
159+
160+
/* Maximum number of digits.
161+
* - Must be small enough such that mp_bit_count does not overflow.
162+
* - Must be small enough such that mp_radix_size for base 2 does not overflow.
163+
* mp_radix_size needs two additional bytes for zero termination and sign.
164+
*/
165+
#define MP_MAX_DIGIT_COUNT ((INT_MAX - 2) / MP_DIGIT_BIT)
159166

160167
/* random number source */
161168
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);

0 commit comments

Comments
 (0)
Please sign in to comment.