RSA public operation results in panic, with particular odd modulus size (IDFGH-10615) #11850
Closed
3 tasks done
Labels
Resolution: NA
Issue resolution is unavailable
Status: Done
Issue is done internally
Type: Bug
bugs in IDF
Answers checklist.
IDF version.
v5.1
Operating System used.
Windows
How did you build your project?
Eclipse IDE
If you are using Windows, please specify command line type.
None
Development Kit.
ESP32-S3
Power Supply used.
USB
What is the expected behavior?
The RSA library should not panic when running a public operation. Instead it should finish execution and return a status code.
What is the actual behavior?
Panic
Steps to reproduce.
The actual contents of the message hash and signature are irrelevant, it is when deriving "rinv" for the modulus that the bug is triggered.
Debug Logs.
More Information.
It seems the bug got introduced by this commit: dc34d49, which was made to catch errors on ESP32 (#8710).
Other relevant issues:
#10403
#11366
My conclusion is that the assertion is incorrect, and only works in some cases. The added assertion is:
A proposed solution is instead:
Description of proposed solution:
The
mbedtls_mpi_mul_mpi
function, which is the only function that callsmpi_mult_mpi_failover_mod_mult
, calculates:It determines that the result will fit in
x_bits + y_bits
bits. But this is only the worst case. In some cases, the result fits inx_bits + y_bits - 1
bits. Take for example0b1111 * 0b1111 = 0b11100001
which fits in 8 bits, but0b1000 * 0b1000 = 0b01000000
which fits in 7 bits. The code rounds up to the nearest word size, so the bug will only be triggered when the worst case number of bits is 1 modulo 32 and at the same time the actual number of bits is 0 modulo 32, for the result.In my sample code, the modulus is 0x45454545... and at some point during
calculate_rinv
, the modulus times 3 is being calculated, which is 0xcfcfcfcf..., which only requires 1 extra bit, not 2, and hence fits in one word less than the worst case.The text was updated successfully, but these errors were encountered: