Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 TFM fix for RSA key size 512 and 2048 #6286

Closed
wants to merge 1 commit into from

Conversation

gojimmypi
Copy link
Contributor

Description

As described in #6205 RSA signature fails for certain key sizes on the ESP32.

This is an interim fix as described here that reverts to software calculations in the known cases that the hardware acceleration is problematic.

Specifically:

  1. For key size 2048 esp_mp_mulmod is not used, instead fp_mulmod is called.

  2. For key size 512 esp_mp_exptmod is not used, instead the SW version is used in fp_exptmod.

There's also a new compile-time warning these functions that need a key size but one is not defined: "WOLFSSL_RSA_KEY_SIZE not defined"

See #6234 for a roadmap of all Espressif Improvements.

Fixes zd# n/a

Testing

How did you test?

See details in #6205

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

Copy link
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason for these issues understood or documented in HW or is the underlying bug/issue not understood yet? I read the #6205 and it seems like the real bug is not identified yet.

@@ -2818,8 +2818,17 @@ int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)

#if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
int x = fp_count_bits (X);
#endif
#if defined(WOLFSSL_RSA_KEY_SIZE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is WOLFSSL_RSA_KEY_SIZE documented? Did you consider RSA_MAX_SIZE? Why is the logic not a >= 512? If this is ESP32 specific please use a macro that includes it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason for these issues understood? is the underlying bug/issue not understood yet?

No, not yet. I do not know exactly why the hardware-accelerated calc fails for certain RSA lengths. I wanted to at least get a fix in place before diving into the HW. It may take some time to fully resolve this. I chose to instead get other, higher visibility changes in place and backburner this for now.

Where is WOLFSSL_RSA_KEY_SIZE documented?

I neglected to add documentation on WOLFSSL_RSA_KEY_SIZE. I can add it if you are in favor of keeping this, or some other equivalent functionality.

The intent is for this value to be used for explicit user RSA key sizes. We could put this in the default ESP32 user_settings.h.

I didn't use RSA_MAX_SIZE as I wanted to define an explicit size, not a maximum size. Also, it appears other settings may imply a specific RSA_MAX_SIZE value.

Why is the logic not a >= 512

There's a specific problem for lengths of exactly 512 and a different problem for 2048 length.

If this is ESP32 specific please use a macro that includes it.

The TFM library only uses this gate when the WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI is enabled. Otherwise, I use it in the demo app that I used when testing #6205.

Would you prefer an ESP32-specific name? It would seem best to have a macro for RSA lengths for the end user. The use of this macro in TFM does however, leave a bit to be desired. Once the HW math is fixed, the only use of this would be be the end user when calling something like wc_MakeRsaKey.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code is only for ESP32 I would remove WOLFSSL_RSA_KEY_SIZE logic and have this check always enabled and document the workaround for HW issue. The WOLFSSL_RSA_KEY_SIZE logic here is too limiting. RSA should support a range of key sizes.

@dgarske dgarske removed their assignment Apr 11, 2023
@gojimmypi gojimmypi requested a review from dgarske April 11, 2023 18:47
@@ -2818,8 +2818,17 @@ int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)

#if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
int x = fp_count_bits (X);
#endif
#if defined(WOLFSSL_RSA_KEY_SIZE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code is only for ESP32 I would remove WOLFSSL_RSA_KEY_SIZE logic and have this check always enabled and document the workaround for HW issue. The WOLFSSL_RSA_KEY_SIZE logic here is too limiting. RSA should support a range of key sizes.

@dgarske dgarske removed their assignment Apr 11, 2023
@gojimmypi
Copy link
Contributor Author

Since this code is only for ESP32 I would remove WOLFSSL_RSA_KEY_SIZE logic and have this check always enabled and document the workaround for HW issue. The WOLFSSL_RSA_KEY_SIZE logic here is too limiting. RSA should support a range of key sizes.

I partly agree, but I don't want to disable HW acceleration for all RSA key sizes just because some are problematic. I do agree that my solution is a bit wonky, thus the "interim fix" description.

I'm going to convert this to draft and see if I can find the root problem of the HW math so that all sizes work as they should. This also means that in the meantime, any users will still encounter problems on the current release of wolfSSL, as described in #6205

@gojimmypi gojimmypi marked this pull request as draft April 18, 2023 15:54
@gojimmypi gojimmypi changed the title ESP32 TFM interim fix for RSA key size 512 and 2048 ESP32 TFM fix for RSA key size 512 and 2048 May 3, 2023
@gojimmypi
Copy link
Contributor Author

It was definitely a worthwhile exercise to find the root cause. See #6380

So far, I've only been testing the faster keysize = 512, so I have not determined if the TFM problems with fp_add and fp_cmp are the only ones needed for all other key sizes.

@gojimmypi gojimmypi mentioned this pull request Jul 16, 2023
4 tasks
@gojimmypi
Copy link
Contributor Author

This lingering draft PR was fixed some time ago. See also #6205

@gojimmypi gojimmypi closed this Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants