Skip to content

Commit 62a72aa

Browse files
committed
TFM bounds checks for safety
fp_to_unsigned_bin_len did not deal with negative input length - fails early now. No code in wolfSSL calls mp_to_unsigned_bin_len with a negative len. fp_is_bit_set and fp_set_bit check for greater than or equal to FP_MAX_BITS instead of just greater. Check doesn't cause issues as FP_MAX_SIZE has padding but it was the wrong check.
1 parent 4a7695e commit 62a72aa

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

wolfcrypt/src/tfm.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3969,6 +3969,10 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c)
39693969
int j = 0;
39703970
int x;
39713971

3972+
if (c < 0) {
3973+
return FP_VAL;
3974+
}
3975+
39723976
for (x=c-1; x >= 0 && i < a->used; x--) {
39733977
b[x] = (unsigned char)(a->dp[i] >> j);
39743978
j += 8;
@@ -3990,6 +3994,10 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c)
39903994
int x;
39913995
WC_DECLARE_VAR(t, fp_int, 1, 0);
39923996

3997+
if (c < 0) {
3998+
return FP_VAL;
3999+
}
4000+
39934001
WC_ALLOC_VAR_EX(t, fp_int, 1, NULL, DYNAMIC_TYPE_BIGINT, return FP_MEM);
39944002

39954003
fp_init_copy(t, a);
@@ -4071,7 +4079,7 @@ int fp_is_bit_set (fp_int *a, fp_digit b)
40714079
{
40724080
fp_digit i;
40734081

4074-
if (b > FP_MAX_BITS)
4082+
if (b >= FP_MAX_BITS)
40754083
return FP_VAL;
40764084

40774085
i = b/DIGIT_BIT;
@@ -4087,7 +4095,7 @@ int fp_set_bit (fp_int * a, fp_digit b)
40874095
{
40884096
fp_digit i;
40894097

4090-
if (b > FP_MAX_BITS)
4098+
if (b >= FP_MAX_BITS)
40914099
return FP_VAL;
40924100

40934101
i = b/DIGIT_BIT;

0 commit comments

Comments
 (0)