- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want to use the function Cy_Crypto_Prng_Generate() to generate a deterministic random number based on seed. If the seed is the same, the generated number should be the same. The example CE221295 seems to be a truly random generation example.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the code example CE221295, you can add another function to generate the Pseudo Random Number in the following manner:
void PseudoGeneratePassword(int32_t size, uint8_t* buffer){
/* Initialization seed values fro Pseudo Random Generator */
#define LFSR32_INITSTATE (0xd8959bc9)
#define LFSR31_INITSTATE (0x2bb911f8)
#define LFSR29_INITSTATE (0x060c31b7)
#define MAX_PRNG_VALUE (8UL)
/* Generated Random Number */
uint32_t rndNum = 0;
int32_t index;
cy_stc_crypto_context_prng_t cryptoPrngContext;
cy_en_crypto_status_t cryptoStatus;
/* Initialize Pseudo Random Generator in Crypto Driver */
cryptoStatus = Cy_Crypto_Prng_Init(
LFSR32_INITSTATE,
LFSR31_INITSTATE,
LFSR29_INITSTATE, &cryptoPrngContext);
for(index = 0; index < (size-1); index++)
{
/* Generate Pseudo Random number into rndNum variable */
cryptoStatus = Cy_Crypto_Prng_Generate(MAX_PRNG_VALUE, &rndNum, &cryptoPrngContext);
/* ... check for errors... */
/* Wait crypto become available */
cryptoStatus = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
/* Check if the generated random number is in the range of alpha-numeric,
special characters ASCII codes. If not, convert to that range */
if(rndNum < ASCII_ALPHANUMERIC_LOWER_LIMIT)
{
rndNum += ASCII_ALPHANUMERIC_LOWER_LIMIT;
}
if(rndNum >= ASCII_ALPHANUMERIC_UPPER_LIMIT)
{
rndNum -= ASCII_ALPHANUMERIC_LOWER_LIMIT;
}
buffer[index] = rndNum;
}
buffer[index] = '\0';
/* Display the generated password on the UART Terminal */
printf("Pseudo Password: %s\r\n",buffer);
printf(SCREEN_HEADER1);
}
Just call this function in the for loop of main and you can observe both the TRNG and PRNG output.
The output of PRNG will remain same based on the seed value. Hope this helps
Regards,
Dheeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the code example CE221295, you can add another function to generate the Pseudo Random Number in the following manner:
void PseudoGeneratePassword(int32_t size, uint8_t* buffer){
/* Initialization seed values fro Pseudo Random Generator */
#define LFSR32_INITSTATE (0xd8959bc9)
#define LFSR31_INITSTATE (0x2bb911f8)
#define LFSR29_INITSTATE (0x060c31b7)
#define MAX_PRNG_VALUE (8UL)
/* Generated Random Number */
uint32_t rndNum = 0;
int32_t index;
cy_stc_crypto_context_prng_t cryptoPrngContext;
cy_en_crypto_status_t cryptoStatus;
/* Initialize Pseudo Random Generator in Crypto Driver */
cryptoStatus = Cy_Crypto_Prng_Init(
LFSR32_INITSTATE,
LFSR31_INITSTATE,
LFSR29_INITSTATE, &cryptoPrngContext);
for(index = 0; index < (size-1); index++)
{
/* Generate Pseudo Random number into rndNum variable */
cryptoStatus = Cy_Crypto_Prng_Generate(MAX_PRNG_VALUE, &rndNum, &cryptoPrngContext);
/* ... check for errors... */
/* Wait crypto become available */
cryptoStatus = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
/* Check if the generated random number is in the range of alpha-numeric,
special characters ASCII codes. If not, convert to that range */
if(rndNum < ASCII_ALPHANUMERIC_LOWER_LIMIT)
{
rndNum += ASCII_ALPHANUMERIC_LOWER_LIMIT;
}
if(rndNum >= ASCII_ALPHANUMERIC_UPPER_LIMIT)
{
rndNum -= ASCII_ALPHANUMERIC_LOWER_LIMIT;
}
buffer[index] = rndNum;
}
buffer[index] = '\0';
/* Display the generated password on the UART Terminal */
printf("Pseudo Password: %s\r\n",buffer);
printf(SCREEN_HEADER1);
}
Just call this function in the for loop of main and you can observe both the TRNG and PRNG output.
The output of PRNG will remain same based on the seed value. Hope this helps
Regards,
Dheeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This works but I think the value of MAX_PRNG_VALUE is wrong. I assume you want 8 bits max but the value should be the maximum value not bits.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm sorry, you are right. Change the macro MAX_PRNG_VALUE to whatever max value you want the PRNG to generate.
Regards,
Dheeraj