cancel
Showing results for 
Search instead for 
Did you mean: 

RSA Signature Verification Issue - function STM32_RSA_Verify_SHA256 not return any status

PPrashant
Associate II

Please check attached Source code file with complete test code.

I am using STM32WB55xx with algorithm : RSA/PKCS#1v15/rsa_pkcs1v15.h

Note - Public key is 2048 bit

Issue -

  1. If we set following modulus(publicModulus[2048 / 8] ) not getting any result/status.

  uint8_t publicModulus[2048 / 8] = { 0xd4, 0x3a, 0x8a, 0xcd, 0x56, 0xdc, 0x46, 0x83, 0x8d, 0x7d, 0x15, 0xb8, 0xe0, 0xed, 0x34, 0xa2, 0xa1, 0x0e, 0x0a, 0x50, 0x07, 0x3a, 0xec, 0xb2, 0x0e, 0x8b, 0x55, 0xa0, 0x12, 0x38, 0xda, 0x2c, 0x16, 0x8c, 0xb0, 0xa6, 0xe2, 0x57, 0xa7, 0xea, 0x1c, 0xba, 0x8f, 0xe0, 0x1d, 0x67, 0xd6, 0xa1, 0x40, 0x26, 0xfe, 0x62, 0x3f, 0x3d, 0x9c, 0x17, 0x1d, 0xa4, 0xd3, 0xcd, 0x31, 0x2f, 0x08, 0x23, 0xbd, 0x75, 0x4e, 0x5a, 0xe3, 0xa4, 0x3d, 0x80, 0x7b, 0x0f, 0x3e, 0x6f, 0xa2, 0xfe, 0x31, 0x1d, 0xb7, 0x50, 0xff, 0x35, 0x4a, 0xa1, 0x95, 0x33, 0x5f, 0xf0, 0x5c, 0x9a, 0x79, 0x90, 0xf4, 0xda, 0xdc, 0xc8, 0x12, 0x4e, 0xae, 0x34, 0x5b, 0x5e, 0x9b, 0xeb, 0xc2, 0xf8, 0x84, 0xb7, 0x16, 0x03, 0x0c, 0xe5, 0xcf, 0xe6, 0x93, 0x2b, 0x2c, 0xe7, 0xdd, 0x57, 0x26, 0x94, 0x69, 0xc0, 0x3a, 0xac, 0x0c, 0x80, 0x68, 0x51, 0xcb, 0x3e, 0xce, 0x74, 0x21, 0x68, 0x07, 0xc2, 0x65, 0x16, 0x96, 0x83, 0x4d, 0xb6, 0xad, 0x6b, 0xdf, 0x86, 0xad, 0x21, 0xa1, 0xf8, 0x57, 0xbe, 0xa2, 0x6f, 0x87,

          0x61, 0x19, 0x23, 0xee, 0xaa, 0xa1, 0xcb, 0x7b, 0xe1, 0xe5, 0xcf, 0x8b, 0xa9, 0x40, 0xf9, 0x86, 0xd6, 0x1b, 0x5a, 0xd0, 0xaf, 0x31, 0x4a, 0x50, 0x44, 0x40, 0x4d, 0xf9, 0x17, 0xbd, 0x07, 0x82, 0x2d, 0xca, 0x9f, 0x6a, 0xcd, 0xdd, 0xb5, 0x8e, 0xd2, 0xdc, 0xbd, 0x04, 0x26, 0xd1, 0x87, 0xa1, 0x3a, 0x7f, 0x82, 0x26, 0x83, 0x88, 0xb8, 0x3e, 0x22, 0x5e, 0x7d, 0xd3, 0x89, 0xf4, 0x93, 0xc5, 0x59, 0x73, 0xef, 0xe9, 0x0e, 0x69, 0xc4, 0xcc, 0x91, 0x3f, 0x97, 0xaf, 0xde, 0x8e, 0x16, 0xde, 0xec, 0x92, 0x59, 0xb2, 0xb2, 0xac, 0x34, 0x79, 0x39, 0x25, 0xd6, 0x70, 0x74, 0x39, 0xc3, 0xc9, 0xa3 };

  uint8_t public_exponent[3] = { 0x01, 0x00, 0x01 };

  uint8_t signature_test[2048 / 8] = { 0xae, 0xc3, 0x23, 0x90, 0xec, 0x63, 0x4b, 0x4a, 0x8c, 0x77, 0x71, 0x1f, 0x48, 0x6a, 0xcf, 0xf3, 0xed, 0xf8, 0xff, 0xf4, 0xe8, 0x38, 0x69, 0x23, 0x4d, 0x56, 0x59, 0x94, 0xcf, 0xf7, 0xfb, 0x41, 0xb3, 0x48, 0x58, 0xa2, 0x7d, 0x6c, 0xb7, 0x84, 0x63, 0x37, 0xf0, 0xa7, 0x28, 0x14, 0x37, 0xa2, 0x96, 0x5a, 0xfc, 0x2c, 0x93, 0x15, 0x8b, 0xac, 0x9a, 0x17, 0x8a, 0x97, 0x93, 0xbb, 0x78, 0xfd, 0x6c, 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e8, 0x78, 0x4d, 0x96, 0x6a, 0xf7, };

  int32_t status = HASH_SUCCESS;

  RSApubKey_stt rsaPubKey_stta;

  rsaPubKey_stta.mExponentSize = sizeof(public_exponent);

  rsaPubKey_stta.pmExponent = (uint8_t*) public_exponent;

  rsaPubKey_stta.mModulusSize = sizeof(publicModulus);

  rsaPubKey_stta.pmModulus = (uint8_t*) publicModulus;

int32_t STM32_RSA_Verify_SHA256(RSApubKey_stt *P_pPubKey, const uint8_t *inputMessage, int32_t messageSize, uint8_t *pSignature)

{

  uint8_t Digest[CRL_SHA256_SIZE];

  int32_t status = HASH_SUCCESS;

  int32_t outputSize;

  membuf_stt mb_st;

  mb_st.mSize = sizeof(preallocated_buffer);

  mb_st.mUsed = 0;

  mb_st.pmBuf = preallocated_buffer;

  SHA256ctx_stt SHA256ctx_st;

  SHA256ctx_st.mFlags = E_HASH_DEFAULT;

  SHA256ctx_st.mTagSize = CRL_SHA256_SIZE;

  status = SHA256_Init(&SHA256ctx_st);

  if (status == HASH_SUCCESS)

  {

    status = SHA256_Append(&SHA256ctx_st , inputMessage , messageSize);

    if (status == HASH_SUCCESS)

    {

      status = SHA256_Finish(&SHA256ctx_st , Digest , &outputSize);

      if (status == HASH_SUCCESS)

      {

        status = RSA_PKCS1v15_Verify(P_pPubKey , Digest , E_SHA256 , pSignature , &mb_st);

      } else

      {

        //Error_Handler( );

      }

    }

  }

  return (status);

}

2 REPLIES 2
PPrashant
Associate II

0693W000003OrxzQAC.png0693W000003OrxpQAC.png0693W000003OrxaQAC.png 

Tested and working fine with following settings.

Check and enable following code

  1. main.c =>

 /* Enable CRC clock */

 __CRC_CLK_ENABLE();

 2. stm32wbxx_hal_conf.h =>

Enable following flag within stm32wbxx_hal_conf.h file

#define HAL_CRYP_MODULE_ENABLED

#define HAL_CRC_MODULE_ENABLED

Also check following files present or not in your Drivers/STM32WBxx_HAL_Driver/Inc & Drivers/STM32WBxx_HAL_Driver/Src Path

  1. stm32wbxx_hal_crc.h & stm32wbxx_hal_crc.c
  2. stm32wbxx_hal_cryp_ex.h & stm32wbxx_hal_cryp_ex.c
  3. stm32wbxx_hal_cryp.h & stm32wbxx_hal_cryp.c

4) check following code

declare as global variable

uint8_t pre_allocated_buffer[4096];

if you not declear pre_allocated_buffer as global it will stuck at RSA_PKCS1v15_Verify method

 membuf_stt mb_st;

  mb_st.mSize = sizeof(preallocated_buffer);

  mb_st.mUsed = 0;

  mb_st.pmBuf = preallocated_buffer;

 For more information please check attached screenshots

1. Right click on your project and select properties

2. Select Settings under C/C++ Build

3. Select Tool Settings

4. Go to Libraries tab under MCU GCC Linker 

  Add following path 

  a. => :STM32CryptographicV3.1.3_CM4_GCC_ot.a under Libraries

  b. => "${ProjDirPath}/../Middlewares/ST/STM32_WPAN/ble/cryptographic/Lib"

      under Library search path

6. Go to Miscellaneous tab under MCU GCC Compiler 

  Add following flag

  -Wno-strict-aliasing

7. Check following setting under MCU Settings

   

  a. => Floating-Point ABK - Mix HW/SW implementation

   

  

8. Go to include paths under MCU GCC compiler and add following path

  ../../Middlewares/ST/STM32_WPAN/ble/cryptographic/Inc

 A couple things to check.

The crypto lib use to allocate the buffers for the algorithms in the binary. What this meant that the max SRAM was always used because the buffer was sized for a worse case entropy. In the release of ver 3.x.x, this was changed and the user now has to make sure that the buffers are large enough.

a.   What’s this mean? The preallocated buffer and the heap need to be set by the user and need to be large enough. 

uint8_t preallocated_buffer[4096] __attribute__((aligned(4))); /* buffer required for internal allocation of memory */

/* Initialize the membuf_st that must be passed to the RSA functions */

 mb_st.mSize = sizeof(preallocated_buffer);

 mb_st.mUsed = 0;

 mb_st.pmBuf = preallocated_buffer;

I did some comparison of the RSA 1024 versus 2049… the 1024 uses about 2k of the buffer and the 2048 uses 4050 bytes… so make sure this is larger enough.

stack is set at 0x4000 and the heap = 0 (not used)…. So make sure the stack is large.

when changing the library to the …GCC_ot.a, CubeIDE requires the FPU to be a mix for the crypto lib.

PPrashant
Associate II

PFA code