2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\brief Init global Whitewood netRandom context
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 Success
|
|
|
|
|
|
\return BAD_FUNC_ARG Either configFile is null or timeout is negative.
|
|
|
|
|
|
\return RNG_FAILURE_E There was a failure initializing the rng.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\param configFile Path to configuration file
|
|
|
|
|
|
\param hmac_cb Optional to create HMAC callback.
|
|
|
|
|
|
\param timeout A timeout duration.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
char* config = "path/to/config/example.conf";
|
|
|
|
|
|
int time = // Some sufficient timeout value;
|
|
|
|
|
|
|
|
|
|
|
|
if (wc_InitNetRandom(config, NULL, time) != 0)
|
|
|
|
|
|
{
|
2019-12-24 12:29:33 -06:00
|
|
|
|
// Some error occurred
|
2017-12-29 10:45:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_FreeNetRandom
|
|
|
|
|
|
*/
|
2018-04-27 10:50:34 -06:00
|
|
|
|
WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int);
|
2018-05-02 13:28:17 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\brief Free global Whitewood netRandom context.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 Success
|
|
|
|
|
|
\return BAD_MUTEX_E Error locking mutex on wnr_mutex
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\param none No returns.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
int ret = wc_FreeNetRandom();
|
|
|
|
|
|
if(ret != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Handle the error
|
|
|
|
|
|
}
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_InitNetRandom
|
|
|
|
|
|
*/
|
2018-04-27 10:50:34 -06:00
|
|
|
|
WOLFSSL_API int wc_FreeNetRandom(void);
|
2018-05-02 13:28:17 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
|
|
|
|
|
\brief Gets the seed (from OS) and key cipher for rng. rng->drbg
|
|
|
|
|
|
(deterministic random bit generator) allocated (should be deallocated
|
2017-12-29 10:45:37 -07:00
|
|
|
|
with wc_FreeRng). This is a blocking operation.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 on success.
|
|
|
|
|
|
\return MEMORY_E XMALLOC failed
|
|
|
|
|
|
\return WINCRYPT_E wc_GenerateSeed: failed to acquire context
|
|
|
|
|
|
\return CRYPTGEN_E wc_GenerateSeed: failed to get random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\return BAD_FUNC_ARG wc_RNG_GenerateBlock input is null or sz exceeds
|
2017-12-29 10:45:37 -07:00
|
|
|
|
MAX_REQUEST_LEN
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\return DRBG_CONT_FIPS_E wc_RNG_GenerateBlock: Hash_gen returned
|
2017-12-29 10:45:37 -07:00
|
|
|
|
DRBG_CONT_FAILURE
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\return RNG_FAILURE_E wc_RNG_GenerateBlock: Default error. rng’s
|
2017-12-29 10:45:37 -07:00
|
|
|
|
status originally not ok, or set to DRBG_FAILED
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
|
|
|
|
|
\param rng random number generator to be initialized for use
|
2017-12-29 10:45:37 -07:00
|
|
|
|
with a seed and key cipher
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
RNG rng;
|
|
|
|
|
|
int ret;
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
#ifdef HAVE_CAVIUM
|
|
|
|
|
|
ret = wc_InitRngCavium(&rng, CAVIUM_DEV_ID);
|
2018-06-27 16:22:12 -06:00
|
|
|
|
if (ret != 0){
|
2017-12-29 10:45:37 -07:00
|
|
|
|
printf(“RNG Nitrox init for device: %d failed”, CAVIUM_DEV_ID);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
ret = wc_InitRng(&rng);
|
|
|
|
|
|
if (ret != 0){
|
|
|
|
|
|
printf(“RNG init failed”);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_InitRngCavium
|
|
|
|
|
|
\sa wc_RNG_GenerateBlock
|
|
|
|
|
|
\sa wc_RNG_GenerateByte
|
|
|
|
|
|
\sa wc_FreeRng
|
|
|
|
|
|
\sa wc_RNG_HealthTest
|
|
|
|
|
|
*/
|
|
|
|
|
|
WOLFSSL_API int wc_InitRng(WC_RNG*);
|
2018-05-02 13:28:17 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
|
|
|
|
|
\brief Copies a sz bytes of pseudorandom data to output. Will
|
2017-12-29 10:45:37 -07:00
|
|
|
|
reseed rng if needed (blocking).
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 on success
|
|
|
|
|
|
\return BAD_FUNC_ARG an input is null or sz exceeds MAX_REQUEST_LEN
|
|
|
|
|
|
\return DRBG_CONT_FIPS_E Hash_gen returned DRBG_CONT_FAILURE
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\return RNG_FAILURE_E Default error. rng’s status originally not
|
2017-12-29 10:45:37 -07:00
|
|
|
|
ok, or set to DRBG_FAILED
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\param rng random number generator initialized with wc_InitRng
|
|
|
|
|
|
\param output buffer to which the block is copied
|
|
|
|
|
|
\param sz size of output in bytes
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
RNG rng;
|
|
|
|
|
|
int sz = 32;
|
|
|
|
|
|
byte block[sz];
|
|
|
|
|
|
|
|
|
|
|
|
int ret = wc_InitRng(&rng);
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
|
return -1; //init of rng failed!
|
|
|
|
|
|
}
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
ret = wc_RNG_GenerateBlock(&rng, block, sz);
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
|
return -1; //generating block failed!
|
|
|
|
|
|
}
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_InitRngCavium, wc_InitRng
|
|
|
|
|
|
\sa wc_RNG_GenerateByte
|
|
|
|
|
|
\sa wc_FreeRng
|
|
|
|
|
|
\sa wc_RNG_HealthTest
|
|
|
|
|
|
*/
|
|
|
|
|
|
WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz);
|
2018-05-02 13:28:17 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
|
|
|
|
|
\brief Calls wc_RNG_GenerateBlock to copy a byte of pseudorandom
|
2017-12-29 10:45:37 -07:00
|
|
|
|
data to b. Will reseed rng if needed.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 on success
|
|
|
|
|
|
\return BAD_FUNC_ARG an input is null or sz exceeds MAX_REQUEST_LEN
|
|
|
|
|
|
\return DRBG_CONT_FIPS_E Hash_gen returned DRBG_CONT_FAILURE
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\return RNG_FAILURE_E Default error. rng’s status originally not
|
2017-12-29 10:45:37 -07:00
|
|
|
|
ok, or set to DRBG_FAILED
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\param rng: random number generator initialized with wc_InitRng
|
|
|
|
|
|
\param b one byte buffer to which the block is copied
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
RNG rng;
|
|
|
|
|
|
int sz = 32;
|
|
|
|
|
|
byte b[1];
|
|
|
|
|
|
|
|
|
|
|
|
int ret = wc_InitRng(&rng);
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
|
return -1; //init of rng failed!
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ret = wc_RNG_GenerateByte(&rng, b);
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
|
return -1; //generating block failed!
|
|
|
|
|
|
}
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_InitRngCavium
|
|
|
|
|
|
\sa wc_InitRng
|
|
|
|
|
|
\sa wc_RNG_GenerateBlock
|
|
|
|
|
|
\sa wc_FreeRng
|
|
|
|
|
|
\sa wc_RNG_HealthTest
|
|
|
|
|
|
*/
|
|
|
|
|
|
WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG*, byte*);
|
2018-05-02 13:28:17 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
|
|
|
|
|
\brief Should be called when RNG no longer needed in order to securely
|
2017-12-29 10:45:37 -07:00
|
|
|
|
free drgb. Zeros and XFREEs rng-drbg.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 on success
|
|
|
|
|
|
\return BAD_FUNC_ARG rng or rng->drgb null
|
|
|
|
|
|
\return RNG_FAILURE_E Failed to deallocated drbg
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\param rng random number generator initialized with wc_InitRng
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
RNG rng;
|
|
|
|
|
|
int ret = wc_InitRng(&rng);
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
|
return -1; //init of rng failed!
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ret = wc_FreeRng(&rng);
|
2018-06-27 16:22:12 -06:00
|
|
|
|
if (ret != 0) {
|
2017-12-29 10:45:37 -07:00
|
|
|
|
return -1; //free of rng failed!
|
|
|
|
|
|
}
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_InitRngCavium
|
|
|
|
|
|
\sa wc_InitRng
|
|
|
|
|
|
\sa wc_RNG_GenerateBlock
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\sa wc_RNG_GenerateByte,
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_RNG_HealthTest
|
|
|
|
|
|
*/
|
|
|
|
|
|
WOLFSSL_API int wc_FreeRng(WC_RNG*);
|
2018-05-02 13:28:17 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
/*!
|
|
|
|
|
|
\ingroup Random
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\brief Creates and tests functionality of drbg.
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\return 0 on success
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\return BAD_FUNC_ARG entropyA and output must not be null. If reseed
|
2017-12-29 10:45:37 -07:00
|
|
|
|
set entropyB must not be null
|
|
|
|
|
|
\return -1 test failed
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\param int reseed: if set, will test reseed functionality
|
|
|
|
|
|
\param entropyA: entropy to instantiate drgb with
|
|
|
|
|
|
\param entropyASz: size of entropyA in bytes
|
|
|
|
|
|
\param entropyB: If reseed set, drbg will be reseeded with entropyB
|
|
|
|
|
|
\param entropyBSz: size of entropyB in bytes
|
2018-06-27 16:22:12 -06:00
|
|
|
|
\param output: initialized to random data seeded with entropyB if
|
2017-12-29 10:45:37 -07:00
|
|
|
|
seedrandom is set, and entropyA otherwise
|
|
|
|
|
|
\param outputSz: length of output in bytes
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
_Example_
|
|
|
|
|
|
\code
|
|
|
|
|
|
byte output[SHA256_DIGEST_SIZE * 4];
|
|
|
|
|
|
const byte test1EntropyB[] = ....; // test input for reseed false
|
|
|
|
|
|
const byte test1Output[] = ....; // testvector: expected output of
|
|
|
|
|
|
// reseed false
|
|
|
|
|
|
ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
|
|
|
|
|
|
output, sizeof(output));
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
|
return -1;//healthtest without reseed failed
|
|
|
|
|
|
|
|
|
|
|
|
if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
|
|
|
|
|
|
return -1; //compare to testvector failed: unexpected output
|
|
|
|
|
|
|
|
|
|
|
|
const byte test2EntropyB[] = ....; // test input for reseed
|
|
|
|
|
|
const byte test2Output[] = ....; // testvector expected output of reseed
|
|
|
|
|
|
ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
|
|
|
|
|
|
test2EntropyB, sizeof(test2EntropyB),
|
|
|
|
|
|
output, sizeof(output));
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
|
|
|
|
|
|
return -1; //compare to testvector failed
|
|
|
|
|
|
\endcode
|
2018-06-27 16:22:12 -06:00
|
|
|
|
|
2017-12-29 10:45:37 -07:00
|
|
|
|
\sa wc_InitRngCavium
|
|
|
|
|
|
\sa wc_InitRng
|
|
|
|
|
|
\sa wc_RNG_GenerateBlock
|
|
|
|
|
|
\sa wc_RNG_GenerateByte
|
|
|
|
|
|
\sa wc_FreeRng
|
|
|
|
|
|
*/
|
2018-04-27 10:50:34 -06:00
|
|
|
|
WOLFSSL_API int wc_RNG_HealthTest(int reseed,
|
2017-12-29 10:45:37 -07:00
|
|
|
|
const byte* entropyA, word32 entropyASz,
|
|
|
|
|
|
const byte* entropyB, word32 entropyBSz,
|
|
|
|
|
|
byte* output, word32 outputSz);
|