Skip to content

Commit 5213bbb

Browse files
parmi93TofMassilia13320
authored andcommitted
fix(stse_generate_random): fix index overflow and incorrect chunk increment
The previous implementation incremented the index by STSAFEA_MAXIMUM_RNG_SIZE even for the last chunk, which could cause an integer overflow and potentially an infinite loop when random_size is close to UINT16_MAX. Replaced index-based iteration with pointer arithmetic and random_size decrement to avoid overflow. Added early return STSE_OK for random_size == 0.
1 parent a966ec3 commit 5213bbb

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

api/stse_random.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ stse_ReturnCode_t stse_generate_random(
3030
return (STSE_API_HANDLER_NOT_INITIALISED);
3131
}
3232

33+
if (random_size == 0) {
34+
return (STSE_OK);
35+
}
36+
3337
if (pRandom == NULL) {
3438
return (STSE_API_INVALID_PARAMETER);
3539
}
@@ -38,17 +42,18 @@ stse_ReturnCode_t stse_generate_random(
3842
#ifdef STSE_CONF_STSAFE_L_SUPPORT
3943
if (pSTSE->device_type != STSAFE_L010) {
4044
#endif /* STSE_CONF_STSAFE_L_SUPPORT */
41-
for (PLAT_UI16 index = 0; index < random_size;) {
42-
ret = stsafea_generate_random(
43-
pSTSE,
44-
&pRandom[index],
45-
((random_size - index) < STSAFEA_MAXIMUM_RNG_SIZE) ? (random_size - index) : STSAFEA_MAXIMUM_RNG_SIZE);
45+
while (0 < random_size) {
46+
PLAT_UI16 chunk = (random_size < STSAFEA_MAXIMUM_RNG_SIZE) ?
47+
random_size : STSAFEA_MAXIMUM_RNG_SIZE;
48+
49+
ret = stsafea_generate_random(pSTSE, pRandom, chunk);
4650

4751
if (ret != STSE_OK) {
4852
break;
4953
}
5054

51-
index += STSAFEA_MAXIMUM_RNG_SIZE;
55+
random_size -= chunk;
56+
pRandom += chunk;
5257
}
5358
#ifdef STSE_CONF_STSAFE_L_SUPPORT
5459
}

0 commit comments

Comments
 (0)