You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note this issues has been edited since first created. Additional details and examples of fp_cmp problems have been added.
This issue is directly related, and very likely part of the root cause of #6205 and #6286.
The fp_add problem is addressed in #6382. Issue with fp_cmp TBD.
These problems have been manifested by plugging in alternative hardware acceleration algorithms into the wolfcrypt/tfm library. See blog about math libraries.
fp_cmp problem
One of the first interesting things is the TFM fp_cmp()only compares the number of used words in when calling fp_cmp_mag().
After numerous tests of HW acceleration vs software calculations claimed the properly matching results, imagine my surprise when I added this code and the breakpoint was actually hit:
Yes, in this case the fp_cmp() claimed the values were equal, yet memcmp detected a difference.
The cause of the difference was the presence of non-zero bytes in positions beyond the used length. For example:
In theory, if everything actually honored the used length, we'd have no problem. It makes sense to only look at the bytes actually being used, those less than the used value.
Note that #6382 does not address the fundamental problem with fp_cmp(), or more specifically the incorrect assumption that if (a->used) > (b->used) that the result must be FP_GT or that if (a->used) < (b->used) that the result must be FP_LT.
Take for example:
In the case shown above, both memcmp and fp_cmp fail for the same reason: the used values for Y and Y2 are different. Yet the actual fp_int value is the same: a value of 1. I have a different Espressif HW solution that trims any trailing zeros and adjusts the z.used size.
Any thoughts on if the fp_cmp should be modified? Or perhaps have just some notes and possibly optional debug code that gives a warning?
Note that I asked the OpenAI ChatGPT this question:
And received this answer:
After reviewing those issues, I asked again:
This time there was a more interesting answer (yes, for some time, I thought the 1 was a hardware calc error).
So yes: over the course of calculating Z = X^Y mod M sometimes the answer is indeed just one, even when not obviously expected.
fp_add problem
Although at first glance, it looks like s_fp_add() honors the fp_int used length, it does not. It currently just grabs the longest length of the two operands and assumes all values beyond a possible shorter length in the other operand are zeros. If the values are not zero, an incorrect result is returned.
Here's where the problem occurred during with the hardware-accelerated call to mp_exptmod for Y = (G ^ X) mod P:
In this case the interesting value (non-zero words beyond used length) is in &tmpb which is later used in an addition with mp_add().
mp_add() returns an incorrect result. The rest of the entire operation is now corrupted.
Yes, there's probably yet another lesson in variable initialization here. ;)
The wolfcrypt tests do not currently test or detect this issue.
I have a fix for this. PR coming soon. Update: see #6382
gojimmypi
changed the title
[Bug]: TFM math library inconsistency not honoring fp_int [used] length: s_fp_add error
[Bug]: TFM math library not honoring fp_int [used] length: s_fp_add error
May 5, 2023
gojimmypi
changed the title
[Bug]: TFM math library not honoring fp_int [used] length: s_fp_add error
[Bug]: TFM math with library not honoring fp_int [used] length: s_fp_add error
May 5, 2023
gojimmypi
changed the title
[Bug]: TFM math with library not honoring fp_int [used] length: s_fp_add error
[Bug]: TFM math with Espressif HW not honoring fp_int [used] length: s_fp_add error
May 5, 2023
As this issue is so specific to Espressif HW acceleration values, I'm pulling the PR as draft and will reconsider making changes to the hardware acceleration code instead.
Contact Details
No response
Version
latest master
Description
Note this issues has been edited since first created. Additional details and examples of
fp_cmp
problems have been added.This issue is directly related, and very likely part of the root cause of #6205 and #6286.
The
fp_add
problem is addressed in #6382. Issue withfp_cmp
TBD.These problems have been manifested by plugging in alternative hardware acceleration algorithms into the wolfcrypt/tfm library. See blog about math libraries.
fp_cmp problem
One of the first interesting things is the TFM fp_cmp() only compares the number of
used
words in when calling fp_cmp_mag().After numerous tests of HW acceleration vs software calculations claimed the properly matching results, imagine my surprise when I added this code and the breakpoint was actually hit:
Yes, in this case the
fp_cmp()
claimed the values were equal, yetmemcmp
detected a difference.The cause of the difference was the presence of non-zero bytes in positions beyond the
used
length. For example:In theory, if everything actually honored the
used
length, we'd have no problem. It makes sense to only look at the bytes actually being used, those less than theused
value.Note that #6382 does not address the fundamental problem with
fp_cmp()
, or more specifically the incorrect assumption that if (a->used) > (b->used) that the result must beFP_GT
or that if (a->used) < (b->used) that the result must beFP_LT
.Take for example:
In the case shown above, both
memcmp
andfp_cmp
fail for the same reason: theused
values forY
andY2
are different. Yet the actualfp_int
value is the same: a value of 1. I have a different Espressif HW solution that trims any trailing zeros and adjusts thez.used
size.Any thoughts on if the
fp_cmp
should be modified? Or perhaps have just some notes and possibly optional debug code that gives a warning?Note that I asked the OpenAI ChatGPT this question:
And received this answer:
After reviewing those issues, I asked again:
This time there was a more interesting answer (yes, for some time, I thought the
1
was a hardware calc error).So yes: over the course of calculating
Z = X^Y mod M
sometimes the answer is indeed just one, even when not obviously expected.fp_add problem
Although at first glance, it looks like s_fp_add() honors the
fp_int
used length, it does not. It currently just grabs the longest length of the two operands and assumes all values beyond a possible shorter length in the other operand are zeros. If the values are not zero, an incorrect result is returned.Here's where the problem occurred during with the hardware-accelerated call to
mp_exptmod
forY = (G ^ X) mod P
:In this case the interesting value (non-zero words beyond
used
length) is in&tmpb
which is later used in an addition withmp_add()
.mp_add()
returns an incorrect result. The rest of the entire operation is now corrupted.Yes, there's probably yet another lesson in variable initialization here. ;)
The wolfcrypt tests do not currently test or detect this issue.
I have a fix for this. PR coming soon. Update: see #6382
Reproduction steps
I have a key generating example. Included in the output is a certificate request:
Although the certificate was apparently generated successfully, some of the math operations were incorrect and undetected.
I have this script that attempts to sign the request:
Relevant log output
The text was updated successfully, but these errors were encountered: