Skip to content

Conversation

de-nordic
Copy link
Contributor

Allow enabling MBEDTLS_RSA_C without key exchange enabled. This allows to use RSA without enabling x509 support too.

@de-nordic de-nordic marked this pull request as draft March 19, 2025 15:07
@de-nordic de-nordic force-pushed the mbedtls-expose-rsa branch from 9eb214f to c98ace0 Compare March 19, 2025 15:08
@de-nordic de-nordic marked this pull request as ready for review March 19, 2025 17:43
@de-nordic de-nordic force-pushed the mbedtls-expose-rsa branch from 76a1833 to 60ca040 Compare March 20, 2025 09:28
@de-nordic de-nordic requested a review from tomi-font March 20, 2025 10:38
tomi-font
tomi-font previously approved these changes Mar 20, 2025
Copy link
Contributor

@valeriosetti valeriosetti left a comment

Choose a reason for hiding this comment

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

This is a nice addition, but I think we can shape it slightly better than this, so I wrote my proposal in comments.

Comment on lines 47 to 72
config MBEDTLS_RSA_C
bool "RSA cryptosystem"
help
Base support for RSA, without X.509 key exchange enabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to have a Kconfig for both PKCS1 v1.5 and v2.1 as follows:

if MBEDTLS_RSA_C

config MBEDTLS_PKCS1_V15
    bool "RSA PKCS1 v1.5"

config MBEDTLS_PKCS1_V21
    bool "RSA PKCS1 v2.1"

endif # MBEDTLS_RSA_C

[OPTIONAL]
config MBEDTLS_RSA_PKCS1_V15
     select MBEDTLS_RSA_C
     select MBEDTLS_PKCS1_V15

[OPTIONAL]
config MBEDTLS_RSA_PKCS1_V21
     select MBEDTLS_RSA_C
     select MBEDTLS_PKCS1_V21

The last 2 items that I marked as [OPTIONAL] are meant to ease your life in the TLS key exchanges so that you can simply select MBEDTLS_RSA_PKCS1_V21 to have what you need.

Copy link
Contributor

Choose a reason for hiding this comment

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

Extra nice thing that would be nice to do in this PR: Kconfig MBEDTLS_GENPRIME_ENABLED only makes sense when RSA is enabled, so it would be nice to bring the definition inside the

if MBEDTLS_RSA_C
....
endif 

block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Don't you want choice for

config MBEDTLS_PKCS1_V15
    bool "RSA PKCS1 v1.5"

config MBEDTLS_PKCS1_V21
    bool "RSA PKCS1 v2.1"

?
I guess then MBEDTLS_RSA_PKCS1_V15 and MBEDTLS_RSA_PKCS1_V21 would not be possible.

Comment on lines 371 to 381
#if defined(CONFIG_MBEDTLS_RSA_C)
#define MBEDTLS_RSA_C
#define MBEDTLS_PKCS1_V21
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the suggestion on Kconfig here you should have 1 #define for each Kconfig, i.e.

#if defined(CONFIG_MBEDTLS_RSA_C)
#define MBEDTLS_RSA_C
#endif

#if defined(CONFIG_MBEDTLS_PKCS1_V15)
#define MBEDTLS_PKCS1_V15
#endif

#if defined(CONFIG_MBEDTLS_PKCS1_V15)
#define MBEDTLS_PKCS1_V21
#endif

defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
#define MBEDTLS_RSA_C
#define MBEDTLS_PKCS1_V15
Copy link
Contributor

Choose a reason for hiding this comment

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

If you add the Kconfig also for PKCS v1.5 as I suggested and resolve all the dependencies in Kconfig, we can get finally rid of this #if def block

@de-nordic de-nordic force-pushed the mbedtls-expose-rsa branch 5 times, most recently from 5f3022b to 7cb1cbe Compare March 21, 2025 12:32
valeriosetti
valeriosetti previously approved these changes Mar 24, 2025
Copy link
Contributor

@valeriosetti valeriosetti left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments. LGTM now :)

defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
#define MBEDTLS_RSA_C
#define MBEDTLS_PKCS1_V15
#define MBEDTLS_PKCS1_V21
Copy link
Contributor

@tomi-font tomi-font Mar 24, 2025

Choose a reason for hiding this comment

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

Previously MBEDTLS_PKCS1_V21 would get defined as well, but now you are making the Kconfig options select MBEDTLS_RSA_PKCS1_V15 which will result in only MBEDTLS_PKCS1_V15 always getting defined. Don't know what's the right solution but that seems like quite a change?
@valeriosetti what do you think?

Copy link
Contributor

@valeriosetti valeriosetti Mar 24, 2025

Choose a reason for hiding this comment

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

Ops, you're right, I missed that part, thanks!
Perhaps we can add another Kconfig like:

config MBEDTLS_RSA_FULL
	bool
	select MBEDTLS_RSA_C
	select MBEDTLS_PKCS1_V21
	select MBEDTLS_PKCS1_V15

which can be used for TLS key-exchanges. If so, then we can also remove Kconfigs CONFIG_MBEDTLS_RSA_PKCS1_V15 and CONFIG_MBEDTLS_RSA_PKCS1_V21 since they would not be used anywhere. But this latter part is optional of course.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think better remove those extra helper Kconfig options if they're not used.

Copy link
Contributor

@tomi-font tomi-font left a comment

Choose a reason for hiding this comment

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

valeriosetti
valeriosetti previously approved these changes Mar 26, 2025
Copy link
Contributor

@valeriosetti valeriosetti left a comment

Choose a reason for hiding this comment

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

The PR looks good to me. There's only one small conflict because my PR renaming Mbed TLS' headers got merged yesterday. However the conflict should be extremely easy to fix.

Allow enabling MBEDTLS_RSA_C without key exchange enabled.
This allows to use RSA without enabling x509 support too.

Signed-off-by: Dominik Ermel <[email protected]>
Copy link
Contributor

@d3zd3z d3zd3z left a comment

Choose a reason for hiding this comment

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

So nothing in particular wrong with this change. I don't know what the use is, but pkcs 1 1.5 should not be used for new applications or in new designs. The PSS from v2.1 should be used for new designs. But, otherwise this looks good.

Also, there is a pkcs1 v2.2 now, which as far as I can tell just adds additional signature algorithms. I'm not sure the approach mbedtls is taking on this.

@kartben kartben merged commit fb71031 into zephyrproject-rtos:main Mar 27, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants