-
Notifications
You must be signed in to change notification settings - Fork 1
[stm32wb_rng, rng] Implement RNG device type. Implement RNG driver ad test for stm32wb #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #include <stdint.h> | ||
| #include <wolfHAL/rng/rng.h> | ||
| #include <wolfHAL/error.h> | ||
|
|
||
| inline whal_Error whal_Rng_Init(whal_Rng *rngDev) | ||
| { | ||
| if (!rngDev || !rngDev->driver || !rngDev->driver->Init) { | ||
| return WHAL_EINVAL; | ||
| } | ||
|
|
||
| return rngDev->driver->Init(rngDev); | ||
| } | ||
|
|
||
| inline whal_Error whal_Rng_Deinit(whal_Rng *rngDev) | ||
| { | ||
| if (!rngDev || !rngDev->driver || !rngDev->driver->Deinit) { | ||
| return WHAL_EINVAL; | ||
| } | ||
|
|
||
| return rngDev->driver->Deinit(rngDev); | ||
| } | ||
|
|
||
| inline whal_Error whal_Rng_Generate(whal_Rng *rngDev, uint8_t *rngData, size_t rngDataSz) | ||
| { | ||
| if (!rngDev || !rngDev->driver || !rngDev->driver->Generate || !rngData) { | ||
| return WHAL_EINVAL; | ||
| } | ||
|
|
||
| return rngDev->driver->Generate(rngDev, rngData, rngDataSz); | ||
| } | ||
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #include <stdint.h> | ||
| #include <wolfHAL/rng/stm32wb_rng.h> | ||
| #include <wolfHAL/rng/rng.h> | ||
| #include <wolfHAL/clock/clock.h> | ||
| #include <wolfHAL/error.h> | ||
| #include <wolfHAL/regmap.h> | ||
| #include <wolfHAL/bitops.h> | ||
|
|
||
| /* | ||
| * STM32WB RNG Register Definitions | ||
| * | ||
| * The RNG peripheral uses an analog noise source to produce 32-bit | ||
| * random values. One value is available at a time in DR, signaled | ||
| * by the DRDY flag in SR. | ||
| */ | ||
|
|
||
| /* Control Register */ | ||
| #define SRNG_CR_REG 0x00 | ||
| #define SRNG_CR_RNGEN WHAL_MASK(2) /* RNG enable */ | ||
| #define SRNG_CR_CED WHAL_MASK(5) /* Clock error detection disable */ | ||
|
|
||
| /* Status Register */ | ||
| #define SRNG_SR_REG 0x04 | ||
| #define SRNG_SR_DRDY WHAL_MASK(0) /* Data ready */ | ||
| #define SRNG_SR_CECS WHAL_MASK(1) /* Clock error current status */ | ||
| #define SRNG_SR_SECS WHAL_MASK(2) /* Seed error current status */ | ||
| #define SRNG_SR_CEIS WHAL_MASK(5) /* Clock error interrupt status */ | ||
| #define SRNG_SR_SEIS WHAL_MASK(6) /* Seed error interrupt status */ | ||
|
|
||
| /* Data Register - 32-bit random value */ | ||
| #define SRNG_DR_REG 0x08 | ||
|
|
||
| whal_Error whal_Stm32wbRng_Init(whal_Rng *rngDev) | ||
| { | ||
| whal_Error err; | ||
| whal_Stm32wbRng_Cfg *cfg; | ||
|
|
||
| if (!rngDev || !rngDev->cfg) { | ||
| return WHAL_EINVAL; | ||
| } | ||
|
|
||
| cfg = (whal_Stm32wbRng_Cfg *)rngDev->cfg; | ||
|
|
||
| err = whal_Clock_Enable(cfg->clkCtrl, cfg->clk); | ||
| if (err != WHAL_SUCCESS) { | ||
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return err; | ||
| } | ||
|
|
||
| return WHAL_SUCCESS; | ||
| } | ||
|
|
||
| whal_Error whal_Stm32wbRng_Deinit(whal_Rng *rngDev) | ||
| { | ||
| whal_Error err; | ||
|
|
||
| if (!rngDev || !rngDev->cfg) { | ||
| return WHAL_EINVAL; | ||
| } | ||
|
|
||
| whal_Stm32wbRng_Cfg *cfg = (whal_Stm32wbRng_Cfg *)rngDev->cfg; | ||
|
|
||
| err = whal_Clock_Disable(cfg->clkCtrl, cfg->clk); | ||
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (err) { | ||
| return err; | ||
| } | ||
|
|
||
| return WHAL_SUCCESS; | ||
| } | ||
|
|
||
| whal_Error whal_Stm32wbRng_Generate(whal_Rng *rngDev, uint8_t *rngData, size_t rngDataSz) | ||
| { | ||
| if (!rngDev || !rngData) { | ||
| return WHAL_EINVAL; | ||
| } | ||
|
|
||
| whal_Error err = WHAL_SUCCESS; | ||
| const whal_Regmap *reg = &rngDev->regmap; | ||
| size_t status; | ||
| size_t offset = 0; | ||
|
|
||
| /* Enable the RNG peripheral */ | ||
| whal_Reg_Update(reg->base, SRNG_CR_REG, SRNG_CR_RNGEN, | ||
| whal_SetBits(SRNG_CR_RNGEN, 1)); | ||
|
|
||
| while (offset < rngDataSz) { | ||
| /* Wait for a random value to be ready */ | ||
| do { | ||
| /* Check for seed or clock error */ | ||
| whal_Reg_Get(reg->base, SRNG_SR_REG, SRNG_SR_SECS, &status); | ||
| if (status) { | ||
| err = WHAL_EHARDWARE; | ||
| goto exit; | ||
| } | ||
| whal_Reg_Get(reg->base, SRNG_SR_REG, SRNG_SR_CECS, &status); | ||
| if (status) { | ||
| err = WHAL_EHARDWARE; | ||
| goto exit; | ||
| } | ||
|
|
||
| whal_Reg_Get(reg->base, SRNG_SR_REG, SRNG_SR_DRDY, &status); | ||
| } while (!status); | ||
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* Read 32-bit random value */ | ||
| uint32_t rnd = *(volatile uint32_t *)(reg->base + SRNG_DR_REG); | ||
|
|
||
| /* Copy bytes into output buffer */ | ||
| for (size_t i = 0; i < 4 && offset < rngDataSz; i++, offset++) { | ||
| rngData[offset] = (uint8_t)(rnd >> (i * 8)); | ||
| } | ||
| } | ||
|
|
||
| exit: | ||
| /* Disable the RNG peripheral */ | ||
| whal_Reg_Update(reg->base, SRNG_CR_REG, SRNG_CR_RNGEN, | ||
| whal_SetBits(SRNG_CR_RNGEN, 0)); | ||
|
|
||
| return err; | ||
| } | ||
|
|
||
| const whal_RngDriver whal_Stm32wbRng_Driver = { | ||
| .Init = whal_Stm32wbRng_Init, | ||
| .Deinit = whal_Stm32wbRng_Deinit, | ||
| .Generate = whal_Stm32wbRng_Generate, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #include <stdint.h> | ||
| #include <wolfHAL/wolfHAL.h> | ||
| #include <wolfHAL/clock/stm32wb_rcc.h> | ||
| #include <wolfHAL/rng/stm32wb_rng.h> | ||
| #include <wolfHAL/bitops.h> | ||
| #include "stm32wb55xx_nucleo.h" | ||
| #include "../test.h" | ||
|
|
||
| static void test_rng_init_deinit(void) | ||
| { | ||
| WHAL_ASSERT_EQ(whal_Rng_Init(&g_whalRng), WHAL_SUCCESS); | ||
| WHAL_ASSERT_EQ(whal_Rng_Deinit(&g_whalRng), WHAL_SUCCESS); | ||
| } | ||
|
|
||
| static void test_rng_generate_nonzero(void) | ||
| { | ||
| uint8_t buf[16] = {0}; | ||
| int allZero = 1; | ||
|
|
||
| whal_Stm32wbRcc_Ext_EnableHsi48(&g_whalClock, 1); | ||
| WHAL_ASSERT_EQ(whal_Rng_Init(&g_whalRng), WHAL_SUCCESS); | ||
| WHAL_ASSERT_EQ(whal_Rng_Generate(&g_whalRng, buf, sizeof(buf)), WHAL_SUCCESS); | ||
| WHAL_ASSERT_EQ(whal_Rng_Deinit(&g_whalRng), WHAL_SUCCESS); | ||
| whal_Stm32wbRcc_Ext_EnableHsi48(&g_whalClock, 0); | ||
|
|
||
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (size_t i = 0; i < sizeof(buf); i++) { | ||
| if (buf[i] != 0) { | ||
| allZero = 0; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /* 16 zero bytes from a TRNG is astronomically unlikely */ | ||
| WHAL_ASSERT_EQ(allZero, 0); | ||
| } | ||
|
|
||
| static void test_rng_generate_unique(void) | ||
| { | ||
| uint8_t buf1[16] = {0}; | ||
| uint8_t buf2[16] = {0}; | ||
| int same = 1; | ||
|
|
||
| whal_Stm32wbRcc_Ext_EnableHsi48(&g_whalClock, 1); | ||
| WHAL_ASSERT_EQ(whal_Rng_Init(&g_whalRng), WHAL_SUCCESS); | ||
| WHAL_ASSERT_EQ(whal_Rng_Generate(&g_whalRng, buf1, sizeof(buf1)), WHAL_SUCCESS); | ||
| WHAL_ASSERT_EQ(whal_Rng_Generate(&g_whalRng, buf2, sizeof(buf2)), WHAL_SUCCESS); | ||
| WHAL_ASSERT_EQ(whal_Rng_Deinit(&g_whalRng), WHAL_SUCCESS); | ||
| whal_Stm32wbRcc_Ext_EnableHsi48(&g_whalClock, 0); | ||
|
|
||
AlexLanzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (size_t i = 0; i < sizeof(buf1); i++) { | ||
| if (buf1[i] != buf2[i]) { | ||
| same = 0; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /* Two consecutive 16-byte outputs should differ */ | ||
| WHAL_ASSERT_EQ(same, 0); | ||
| } | ||
|
|
||
| void test_rng(void) | ||
| { | ||
| WHAL_TEST_SUITE_START("rng"); | ||
| WHAL_TEST(test_rng_init_deinit); | ||
| WHAL_TEST(test_rng_generate_nonzero); | ||
| WHAL_TEST(test_rng_generate_unique); | ||
| WHAL_TEST_SUITE_END(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.