45
45
# endif
46
46
#endif
47
47
48
- /* Noone will ever need more than a window size of 24.
49
- * (The code probably works with window sizes up to 33 but
50
- * it is not tested for it. For WINDOW_G >= 34, the
51
- * expansion of ECMULT_TABLE_SIZE(WINDOW_G) will overflow. */
48
+ /* Noone will ever need more than a window size of 24. The code might
49
+ * be correct for larger values of ECMULT_WINDOW_SIZE but this is not
50
+ * not tested.
51
+ *
52
+ * The following limitations are known, and there are probably more:
53
+ * If WINDOW_G > 27 and size_t has 32 bits, then the code is incorrect
54
+ * because the size of the memory object that we allocate (in bytes)
55
+ * will not fit in a size_t.
56
+ * If WINDOW_G > 31 and int has 32 bits, then the code is incorrect
57
+ * because certain expressions will overflow.
58
+ * */
52
59
#if ECMULT_WINDOW_SIZE < 3 || ECMULT_WINDOW_SIZE > 24
53
60
# error Set ECMULT_WINDOW_SIZE to an integer in range [3..24].
54
61
#endif
@@ -322,7 +329,12 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
322
329
/* get the generator */
323
330
secp256k1_gej_set_ge (& gj , & secp256k1_ge_const_g );
324
331
325
- ctx -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , sizeof ((* ctx -> pre_g )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G ));
332
+ {
333
+ size_t size = sizeof ((* ctx -> pre_g )[0 ]) * ((size_t )ECMULT_TABLE_SIZE (WINDOW_G ));
334
+ /* check for overflow */
335
+ VERIFY_CHECK (size / sizeof ((* ctx -> pre_g )[0 ]) == ((size_t )ECMULT_TABLE_SIZE (WINDOW_G )));
336
+ ctx -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
337
+ }
326
338
327
339
/* precompute the tables with odd multiples */
328
340
secp256k1_ecmult_odd_multiples_table_storage_var (ECMULT_TABLE_SIZE (WINDOW_G ), * ctx -> pre_g , & gj );
@@ -332,7 +344,10 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
332
344
secp256k1_gej g_128j ;
333
345
int i ;
334
346
335
- ctx -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , sizeof ((* ctx -> pre_g_128 )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G ));
347
+ size_t size = sizeof ((* ctx -> pre_g_128 )[0 ]) * ((size_t ) ECMULT_TABLE_SIZE (WINDOW_G ));
348
+ /* check for overflow */
349
+ VERIFY_CHECK (size / sizeof ((* ctx -> pre_g_128 )[0 ]) == ((size_t )ECMULT_TABLE_SIZE (WINDOW_G )));
350
+ ctx -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
336
351
337
352
/* calculate 2^128*generator */
338
353
g_128j = gj ;
@@ -349,15 +364,15 @@ static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
349
364
if (src -> pre_g == NULL ) {
350
365
dst -> pre_g = NULL ;
351
366
} else {
352
- size_t size = sizeof ((* dst -> pre_g )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G );
367
+ size_t size = sizeof ((* dst -> pre_g )[0 ]) * (( size_t ) ECMULT_TABLE_SIZE (WINDOW_G ) );
353
368
dst -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
354
369
memcpy (dst -> pre_g , src -> pre_g , size );
355
370
}
356
371
#ifdef USE_ENDOMORPHISM
357
372
if (src -> pre_g_128 == NULL ) {
358
373
dst -> pre_g_128 = NULL ;
359
374
} else {
360
- size_t size = sizeof ((* dst -> pre_g_128 )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G );
375
+ size_t size = sizeof ((* dst -> pre_g_128 )[0 ]) * (( size_t ) ECMULT_TABLE_SIZE (WINDOW_G ) );
361
376
dst -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
362
377
memcpy (dst -> pre_g_128 , src -> pre_g_128 , size );
363
378
}
0 commit comments