PSoC 62 generate deterministic random number based on seed

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

cross mob
JeHu_3414236
Level 5
Level 5
10 likes received First like received

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.

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

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.

pastedImage_1.png

The output of PRNG will remain same based on the seed value. Hope this helps

Regards,

Dheeraj

View solution in original post

3 Replies
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

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.

pastedImage_1.png

The output of PRNG will remain same based on the seed value. Hope this helps

Regards,

Dheeraj

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.

0 Likes

I'm sorry, you are right. Change the macro MAX_PRNG_VALUE to whatever max value you want the PRNG to generate.

Regards,

Dheeraj

0 Likes