diff --git a/examples/platforms/cc2538/random.c b/examples/platforms/cc2538/random.c index 88f4df97f..30dd8223a 100644 --- a/examples/platforms/cc2538/random.c +++ b/examples/platforms/cc2538/random.c @@ -34,6 +34,9 @@ * This implementation is not a true random number generator and does @em satisfy the Thread requirements. */ +#include + +#include #include #include "platform-cc2538.h" @@ -66,3 +69,20 @@ uint32_t otPlatRandomGet(void) return mlcg; } + +ThreadError otPlatSecureRandomGet(uint16_t aInputLength, uint8_t *aOutput, uint16_t *aOutputLength) +{ + ThreadError error = kThreadError_None; + + VerifyOrExit(aOutput && aOutputLength, error = kThreadError_InvalidArgs); + + for (uint16_t length = 0; length < aInputLength; length++) + { + aOutput[length] = (uint8_t)otPlatRandomGet(); + } + + *aOutputLength = aInputLength; + +exit: + return error; +} diff --git a/examples/platforms/posix/random.c b/examples/platforms/posix/random.c index a7eb9f0be..fd1984418 100644 --- a/examples/platforms/posix/random.c +++ b/examples/platforms/posix/random.c @@ -34,6 +34,9 @@ * This implementation is not a true random number generator and does @em satisfy the Thread requirements. */ +#include + +#include #include #include "platform-posix.h" @@ -65,3 +68,20 @@ uint32_t otPlatRandomGet(void) return mlcg; } + +ThreadError otPlatSecureRandomGet(uint16_t aInputLength, uint8_t *aOutput, uint16_t *aOutputLength) +{ + ThreadError error = kThreadError_None; + + VerifyOrExit(aOutput && aOutputLength, error = kThreadError_InvalidArgs); + + for (uint16_t length = 0; length < aInputLength; length++) + { + aOutput[length] = (uint8_t)otPlatRandomGet(); + } + + *aOutputLength = aInputLength; + +exit: + return error; +} diff --git a/include/platform/random.h b/include/platform/random.h index 656a51335..c5e629b82 100644 --- a/include/platform/random.h +++ b/include/platform/random.h @@ -52,6 +52,21 @@ extern "C" { * */ +/** + * Get random stream. + * + * @param[in] aInputLength The expected size of random values. + * @param[out] aOutput A pointer to the buffer for the generated random stream. The pointer should never be NULL. + * @param[out] aOutputLength A pointer to the generated size of random stream. + * It is supposed to be the same as aInputLength, but maybe less than aInputLength. + * The pointer should never be NULL. + * + * @retval kThreadError_None Generate random successfully. + * @retval kThreadError_Fail Generate random fail. + * @retval kThreadError_InvalidArgs Invalid args. + */ +ThreadError otPlatSecureRandomGet(uint16_t aInputLength, uint8_t *aOutput, uint16_t *aOutputLength); + /** * Get a 32-bit true random value. *