Implementation of Cy_Crypto_Rsa_CalcCoefs

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
user_1669321
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

Hi,

I need to use Cy_Crypto_Rsa_CalcCoefs to populate the barretCoefPtr, rBarPtr, and inverseModuloPtr fields of my cy_stc_crypto_rsa_pub_key_t structure. However, I would prefer the arrays to be in flash instead of RAM, so I would need to compute those outside of the firmware, and then declare them as const. Is there a code example of how to compute those arrays (basically what the Cy_Crypto_Rsa_CalcCoefs does)?

Thank you,

Fred

0 Likes
1 Solution

I just found out that Cypress provides a tool to generate all those tables into a C file. That's exactly what I needed.

"C:\Program Files (x86)\Cypress\PDL\version\security\secure_image\src\scripts\rsa_to_c.py"

View solution in original post

0 Likes
5 Replies
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Hello Fred,

You can calculate these coefficients directly by a call to Cy_Crypto_Rsa_CalcCoefs and print it after calculation. You can then store these values in flash by declaring them as const.

Important note: if you use RSA process function once you don’t need to calculate these coefficients before.

Please look into the key data structure for allocation details:

/**

* All fields for the context structure are internal. Firmware never reads or

* writes these values. Firmware allocates the structure and provides the

* address of the structure to the driver in the function calls. Firmware must

* ensure that the defined instance of this structure remains in scope

* while the drive is in use.

*

* The driver uses this structure to store and manipulate the RSA public key and

* additional coefficients to accelerate RSA calculation.

*

*  RSA key contained from two fields:

*  - n - modulus part of the key

*  - e - exponent part of the key.

*

* Other fields are accelerating coefficients and can be calculated by

* \ref Cy_Crypto_Rsa_CalcCoefs.

*

* \note The <b>modulus</b> and <b>exponent</b> values in the

* \ref cy_stc_crypto_rsa_pub_key_t must also be in little-endian order.<br>

* Use \ref Cy_Crypto_InvertEndianness function to convert to or from

* little-endian order.

*/

typedef struct

{

/** \cond INTERNAL */

/** The pointer to the modulus part of public key. */

uint8_t *moduloPtr;

/** The modulus length, in bits, maximum supported size is 2048Bit */

uint32_t moduloLength;

    /** The pointer to the exponent part of public key */

uint8_t *pubExpPtr;

/** The exponent length, in bits, maximum supported size is 256Bit */

uint32_t pubExpLength;

/** The pointer to the Barrett coefficient. Memory for it should be

allocated by user with size moduloLength + 1. */

uint8_t *barretCoefPtr;

/** The pointer to the binary inverse of the modulo. Memory for it

should be allocated by user with size moduloLength. */

uint8_t *inverseModuloPtr;

/** The pointer to the (2^moduloLength mod modulo). Memory for it should

be allocated by user with size moduloLength */

uint8_t *rBarPtr;

/** \endcond */

} cy_stc_crypto_rsa_pub_key_t;

Regards,

Dheeraj

0 Likes

Thanks for the answer, Dheeraj. However, it's exactly because I don't want to do this process that I'm asking how to compute those arrays outside of the firmware.

Is the computation process proprietary?

0 Likes
lock attach
Attachments are accessible only for community members.

Oh sorry, I misread your query. You can calculate all the RSA coefficients manually too. Please find the project and document attached detailing the steps. Let me know if you have any queries.

Regards,
Dheeraj

0 Likes

Hi Dheeraj,

While your linked example is useful for a how-to generate rsa key pairs, it does not talk about barretCoefPtr, rBarPtr, or inverseModuloPtr.

0 Likes

I just found out that Cypress provides a tool to generate all those tables into a C file. That's exactly what I needed.

"C:\Program Files (x86)\Cypress\PDL\version\security\secure_image\src\scripts\rsa_to_c.py"

0 Likes