3 Replies Latest reply on Nov 13, 2019 12:04 AM by ThGr_4523916

    How to encrypt message using RSA.

    ThGr_4523916

      I'm trying to get a message RSA encrypted using the crypto server. I've used rsa_keygen.bat from the secure_image example to generate a 2048 bit public key which is input for the crypto server. But whenever I call Cy_Crypto_Rsa_Proc it does not generate a 245 bytes long encoded message. It only seems to generate a output message based on the input message length...

       

      Am I doing something wrong with the configuration for the crypto server?

       

      Is there a code example available which RSA encrypts messages using the crypto server?

       

       

      message to encrypt:

      message.PNG

      processed message after encryption:

      procmessage.PNG

      crypto config:

       

       

      /* Crypto configuration structure */

      const cy_stc_crypto_config_t cryptoConfig = {

      3,

      1,

      2,

      cpuss_interrupts_ipc_2_IRQn,

      2u, },

      NULL,

      NULL,

      NULL,

      {

      cpuss_interrupts_ipc_1_IRQn,

      2u, },

      {

      cpuss_interrupt_crypto_IRQn,

      2u, } };

       

      #include "cy_device_headers.h"#include "cycfg.h"#include "cy_crypto.h"#include "cy_crypto_server.h"#include "stdio_user.h"#include <stdio.h>#include <string.h>
      /******************************************************************************* * Macro definitions ********************************************************************************/
      /* Macros to configure the Crypto block *//* IPC data channel for the Crypto */#define MY_CHAN_CRYPTO         (uint32_t)(3u)/* IPC interrupt structure for the Crypto server */#define MY_INTR_CRYPTO_SRV     (uint32_t)(1u)/* IPC interrupt structure for the Crypto client */#define MY_INTR_CRYPTO_CLI     (uint32_t)(2u)/* CM0+ IPC interrupt mux number the Crypto server */#define MY_INTR_CRYPTO_SRV_MUX (IRQn_Type)(2u)/* CM0+ IPC interrupt mux number the Crypto client */#define MY_INTR_CRYPTO_CLI_MUX (IRQn_Type)(3u)/* CM0+ ERROR interrupt mux number the Crypto server */#define MY_INTR_CRYPTO_ERR_MUX (IRQn_Type)(4u)
      /* The input message size (inclusive of the string terminating character '\0'). * Edit this macro to suit your message size */#define MAX_MESSAGE_SIZE    (100u)
      /******************************************************************************* * Data type definitions ********************************************************************************/
      /* Data type definition to access the data in 1-byte format as well as 4-byte * aligned format. This is required since the Crypto block operates on 4-byte * aligned values. */
      /* Variables to store Crypto internal states */cy_stc_crypto_context_t cryptoScratch;cy_stc_crypto_server_context_t cryptoServerContext;cy_stc_crypto_context_rsa_t cryptoRSA;
      /* Crypto configuration structure */const cy_stc_crypto_config_t cryptoConfig = {/* .ipcChannel             */MY_CHAN_CRYPTO,/* .acquireNotifierChannel */MY_INTR_CRYPTO_SRV,/* .releaseNotifierChannel */MY_INTR_CRYPTO_CLI,/* .releaseNotifierConfig */{#if (CY_CPU_CORTEX_M0P) /* .intrSrc            */MY_INTR_CRYPTO_CLI_MUX, /* .cm0pSrc            */cpuss_interrupts_ipc_2_IRQn, /* depends on selected releaseNotifierChannel value */#else /* .intrSrc            */cpuss_interrupts_ipc_2_IRQn, /* depends on selected releaseNotifierChannel value */#endif /* .intrPriority       */2u, },/* .userCompleteCallback   */NULL,/* .userGetDataHandler     */NULL,/* .userErrorHandler       */NULL,/* .acquireNotifierConfig */{#if (CY_CPU_CORTEX_M0P) /* .intrSrc            */MY_INTR_CRYPTO_SRV_MUX, /* to use with DeepSleep mode should be in DeepSleep capable muxer's range */ /* .cm0pSrc            */cpuss_interrupts_ipc_1_IRQn, /* depends on selected acquireNotifierChannel value */#else /* .intrSrc            */cpuss_interrupts_ipc_1_IRQn, /* depends on selected acquireNotifierChannel value */#endif /* .intrPriority       */2u, },/* .cryptoErrorIntrConfig */{#if (CY_CPU_CORTEX_M0P) /* .intrSrc            */MY_INTR_CRYPTO_ERR_MUX, /* .cm0pSrc            */cpuss_interrupt_crypto_IRQn,#else /* .intrSrc            */cpuss_interrupt_crypto_IRQn,#endif /* .intrPriority       */2u, } };
      /* Variables to hold the user message and the corresponding encrypted message */CY_ALIGN(4) char message[MAX_MESSAGE_SIZE];CY_ALIGN(4) char procmessage[512];
      uint8_t moduloData[] = { 0xB5u, 0xACu, 0xADu, 0x6Du, 0xD0u, 0xF8u, 0x64u, 0x2Fu, 0x16u, 0xA9u, 0x07u, 0x78u, 0x62u, 0x81u, 0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C7u, 0x7Au, 0xE5u, 0xA8u, 0x59u, 0x24u, 0x6Fu, 0x31u, 0xD6u, 0xC0u, 0xD1u, 0x12u, 0x99u, 0x85u, 0x8Du, 0x20u, 0x1Du, 0xD9u, 0x0Du, 0x2Eu, 0x38u, 0xE8u, 0x43u, 0x3Fu, 0x8Eu, 0x89u, 0x33u, 0x3Eu, 0x9Cu, 0x5Du, 0x52u, 0xACu, 0xD0u, 0x70u, 0xAAu, 0x05u, 0xBFu, 0x5Au, 0xB8u, 0x9Au, 0xF3u, 0x58u, 0xD8u, 0x43u, 0xA8u, 0xB3u, 0xE3u, 0xACu, 0x3Cu, 0xB8u, 0x93u, 0x18u, 0x4Du, 0x4Cu, 0xEDu, 0x17u, 0x59u, 0x92u, 0xF8u, 0xD1u, 0xE0u, 0x22u, 0x45u, 0x45u, 0x3Eu, 0x6Bu, 0x06u, 0xDCu, 0xF5u, 0x38u, 0x2Fu, 0x39u, 0xAEu, 0x94u, 0x90u, 0xC3u, 0x1Du, 0xEAu, 0xDEu, 0x50u, 0x6Au, 0xD2u, 0xF2u, 0xCAu, 0x80u, 0x86u, 0x39u, 0xA4u, 0x29u, 0x56u, 0x39u, 0x96u, 0x4Eu, 0xC0u, 0xB9u, 0x40u, 0xC4u, 0x88u, 0x04u, 0x7Cu, 0xDCu, 0xD8u, 0xB4u, 0x76u, 0x39u, 0x34u, 0x79u, 0x7Du, 0x18u, 0xE5u, 0x89u, 0x6Fu, 0x03u, 0xF3u, 0x25u, 0x74u, 0x32u, };
      //* Barrett coefficient, inverse modulo, and r-bar are optional.
      const cy_stc_crypto_rsa_pub_key_t cryptoRsaPubKey ={    /* .moduloPtr        */ moduloData,    /* .moduloLength     */ sizeof(moduloData)/sizeof(moduloData[0]),    /* .pubExpPtr        */ expData,    /* .pubExpLength     */ sizeof(expData)/sizeof(expData[0]),    /* .barretCoefPtr    */ barrettData,    /* .inverseModuloPtr */ inverseModuloData,    /* .rBarPtr          */ rBarData};
      int main(void) { /* Set up the device based on configurator selections */ init_cycfg_all();
      __enable_irq();
      uint8_t messagesize = 0;
      /* Start the Crypto Server */ Cy_Crypto_Server_Start(&cryptoConfig, &cryptoServerContext);
      /*Initialization of Crypto Driver */ Cy_Crypto_Init(&cryptoConfig, &cryptoScratch);
      /* Enable Crypto Hardware */ Cy_Crypto_Enable();
      memset(message, 0, MAX_MESSAGE_SIZE); messagesize = 0;
      strcpy(message, "hello world!"); messagesize = sizeof("hello world!") / sizeof(char);
      /* Wait for Crypto Block to be available */ Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
      volatile cy_en_crypto_status_t cryptstat = Cy_Crypto_Rsa_Proc(&cryptoRsaPubKey, (uint32_t*) message, messagesize, (uint32_t*) procmessage,         &cryptoRSA);
      /* Wait for Crypto Block to be available */ Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);}