Last 3 bytes of encrypted message after Cy_Crypto_Rsa_Proc are always zero

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

Is it normal? Using openssl, that's not the case, so I don't see how that would be normal for the PSoC 6.

0 Likes
1 Solution

I found how it happens, but not why.

So basically, I had made an error when initializing my cy_stc_crypto_rsa_pub_key_t structure. Instead of

    rsaPublicKey.moduloPtr        = (uint8_t*) moduloData;

    rsaPublicKey.moduloLength     = CRYPTO_RSA_KEY_BIT_LENGTH;

    rsaPublicKey.pubExpPtr        = (uint8_t*) rsaExponent;

    rsaPublicKey.pubExpLength     = rsaExponentLength;

    rsaPublicKey.barretCoefPtr    = (uint8_t*) barretData;

    rsaPublicKey.inverseModuloPtr = (uint8_t*) inverseModuloData;

    rsaPublicKey.rBarPtr          = (uint8_t*) rBarData;

I had

    rsaPublicKey.moduloPtr        = (uint8_t*) &moduloData;

    rsaPublicKey.moduloLength     = CRYPTO_RSA_KEY_BIT_LENGTH;

    rsaPublicKey.pubExpPtr        = (uint8_t*) &rsaExponent;

    rsaPublicKey.pubExpLength     = rsaExponentLength;

    rsaPublicKey.barretCoefPtr    = (uint8_t*) &barretData;

    rsaPublicKey.inverseModuloPtr = (uint8_t*) &inverseModuloData;

    rsaPublicKey.rBarPtr          = (uint8_t*) &rBarData;

I was providing the address of the key pointer instead of the key itself.

I don't get why it would result in 3 fixed zeros for RSA encryption, but pointer bugs are the weirdest behaviors.

View solution in original post

0 Likes
2 Replies
user_1669321
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

It seems that it only happens when I use an input buffer that didn't have an initial value.

i.e, if I use:

CY_ALIGN(4) uint8_t msgToEncrypt[256];

instead of

CY_ALIGN(4) uint8_t msgToEncrypt[] = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";

Then the last 3 bytes of encryptedBuffer are always zero after calling the following routine:

Cy_Crypto_Rsa_Proc(&rsaPublicKey,

                   (const uint32_t*)msgToEncrypt,

                   CY_CRYPTO_RSA2048_MESSAGE_SIZE,

                   (uint32_t*)encryptedBuffer,

                   &cryptoRsaContext);

while(Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING) != CY_CRYPTO_SUCCESS);

It doesn't matter if I initialize msgToEncrypt in a function, it has to have an initial value.

Why does that happen?

0 Likes

I found how it happens, but not why.

So basically, I had made an error when initializing my cy_stc_crypto_rsa_pub_key_t structure. Instead of

    rsaPublicKey.moduloPtr        = (uint8_t*) moduloData;

    rsaPublicKey.moduloLength     = CRYPTO_RSA_KEY_BIT_LENGTH;

    rsaPublicKey.pubExpPtr        = (uint8_t*) rsaExponent;

    rsaPublicKey.pubExpLength     = rsaExponentLength;

    rsaPublicKey.barretCoefPtr    = (uint8_t*) barretData;

    rsaPublicKey.inverseModuloPtr = (uint8_t*) inverseModuloData;

    rsaPublicKey.rBarPtr          = (uint8_t*) rBarData;

I had

    rsaPublicKey.moduloPtr        = (uint8_t*) &moduloData;

    rsaPublicKey.moduloLength     = CRYPTO_RSA_KEY_BIT_LENGTH;

    rsaPublicKey.pubExpPtr        = (uint8_t*) &rsaExponent;

    rsaPublicKey.pubExpLength     = rsaExponentLength;

    rsaPublicKey.barretCoefPtr    = (uint8_t*) &barretData;

    rsaPublicKey.inverseModuloPtr = (uint8_t*) &inverseModuloData;

    rsaPublicKey.rBarPtr          = (uint8_t*) &rBarData;

I was providing the address of the key pointer instead of the key itself.

I don't get why it would result in 3 fixed zeros for RSA encryption, but pointer bugs are the weirdest behaviors.

0 Likes