Topic: Manually populate and store a RsaKey structure

Hello !

I'm new on this forum and I recently started working with wolfCrypt on STM32(H725).

I would like to get a secured communication with a Python application with which the STM32 is communicating using RSA data encryption/decryption and digital signature. I started small trying to manually populate the keys its components (n, e for public key, n, d, p, q, dP, dQ, u for private key) in order for each entity to know the public key of the other entity. When doing so and verifying the key using wc_CheckRsaKey, I got ret = RSA_KEY_PAIR_E (-262, from rsa.c -> wc_CheckRsaKey -> _ifc_pairwise_consistency_test -> wc_RsaSSL_Sign -> ret = -234 -> ret = -262).

I know it is possible to create a RsaKey object in Python using Crypto.PublicKey.RSA with (n, e, d, p, q, dP, dQ, u) and I would like to do the same thing using wolfCrypt on STM32. Is it possible ?

Also, I would like to store the STM32 RSA private key on internal flash memory to get it back after a reboot. Is there a solution ?

Thanks for your help !

Share

Re: Manually populate and store a RsaKey structure

Hi Adrian,

My name is Anthony and I am a member of the wolfSSL team.

We do have the function wc_RsaExportKey() which looks like this:

WOLFSSL_API int wc_RsaExportKey(RsaKey* key,
                                byte* e, word32* eSz,
                                byte* n, word32* nSz,
                                byte* d, word32* dSz,
                                byte* p, word32* pSz,
                                byte* q, word32* qSz);

But I don't think we have the corresponding API for importing it in this format.  That said, would you like to register this as a feature request?

If so, please send an email to support@wolfssl.com.  The process will only take a couple of minutes of your time.  Please also reference this forum post in that email message.

Warm regards, Anthony

Share

Re: Manually populate and store a RsaKey structure

I'm sorry, I think I was mistaken!! Please see wc_RsaPrivateKeyDecodeRaw()

It has the form:

WOLFSSL_API int wc_RsaPrivateKeyDecodeRaw(const byte* n, word32 nSz,
        const byte* e, word32 eSz, const byte* d, word32 dSz,
        const byte* u, word32 uSz, const byte* p, word32 pSz,
        const byte* q, word32 qSz, const byte* dP, word32 dPSz,
        const byte* dQ, word32 dQSz, RsaKey* key);

It can be found in wolfssl/wolfcrypt/rsa.h

Warm regards, Anthony

Share

Re: Manually populate and store a RsaKey structure

Hi Anthony,

Thank you very much for your help !
I was looking for such a function but didn't find it, I finally found it on your GitHub !
I think this will fix my issue. Thanks again !

Best regards, Adrien

Share

Re: Manually populate and store a RsaKey structure

Hi,

I may have one more question. In order to use wc_RsaPrivateKeyDecodeRaw, we need the different values n, e, d, p and q we can easily get from wc_RsaExportKey, however, however there's no such a function to get dP & dQ, right ? Do I need to flatten these parameters by mysleft or is there a hidden function to do this ?

Share

Re: Manually populate and store a RsaKey structure

Hi,

I'm sorry for the delayed response. I had lost track of this as I thought I had answered your questions.

dP and dQ will have to be calculated by you.  That said, they are only used in the Chinese Remainder Theorem (CRT).  If you define `RSA_LOW_MEM`, that is using a non-CRT implementation then you won't need to worry about dP and dQ.  That said, execution time will increase. 

Let me know if this helps.

Warm regards, Anthony

Share

Re: Manually populate and store a RsaKey structure

Hi,

I will need to take a closer look at this then but this helps.

I first used wolfSSL through the STM32CubeIDE and the Software Packs Manager that includes the 5.7.0 release version of wolfSSL from March. However, with this version, I don't have the last updates including the new wc_RsaPrivateKeyDecodeRaw function from a few weeks ago and I need to figure out how to integrate the github repository to my project.

If you have any suggestions, I take it !

Best regards, Adrien

Share

Re: Manually populate and store a RsaKey structure

Hi Adrien,

Indeed, that release does not have it yet. 

You could manually patch your copy with the following diff:

https://github.com/wolfSSL/wolfssl/comm … f2d79.diff

However, I'm not sure if/when STM32CubeIDE will overwrite your changes to wolfSSL source code.  I seem to recall that when you save certain configuration settings, the wolfSSL code gets regenerated.

Another option is to simply wait for the next release.  We are currently in the process of preparing for the next release and should be out at the beginning of July.

Perhaps another option is to just prototype on Linux with the github master branch first while you wait for the next release.

I'm very interested to know which path you choose. 

Warm regards, Anthony

Share

Re: Manually populate and store a RsaKey structure

Hi Anthony,

Thank you for your tips, I tried to import the function and I added a custom function to import all the parameters from the generated key to other keys (private and public) using the available functions. It seems to work fine. However, I don't think there is a way to check if the imported keys (using wc_RsaPrivateKeyDecodeRaw & wc_RsaPublicKeyDecodeRaw) are good or not. The wc_CheckRsaKey function works well for the generated RSA key, but I don't think I can use it to check if the public key is good, nor the key pair (public + private) are a match, and even when I try to check the private key which contains all the parameters and seems to be identical to the generated key, it doesn't work. Also, wc_RsaPrivateKeyDecodeRaw and wc_RsaPublicKeyDecodeRaw always returned 0 for me, even when I put random numbers such as the d parameter for the private key and without filling dP and dQ so that the function needs to calculate them, which seems weird.

I'm not sure I will play with the library for the next days, so maybe I will wait for the next release, even though I'm not sure it will fix all my issues.

Sorry for being annoying with these problems !

Warm regards, Adrien

Share