Merge branch 'custom-seed'

This commit is contained in:
toddouska
2015-11-06 15:37:26 -08:00
6 changed files with 60 additions and 18 deletions

View File

@@ -10,4 +10,4 @@ uint32_t hw_get_time_sec(void);
uint32_t hw_get_time_msec(void); uint32_t hw_get_time_msec(void);
void hw_uart_printchar(int c); void hw_uart_printchar(int c);
void hw_watchdog_disable(void); void hw_watchdog_disable(void);
int hw_rand(void); uint32_t hw_rand(void);

View File

@@ -167,7 +167,7 @@ void hw_uart_printchar(int c)
UART_PORT->D = (uint8_t)c; /* Send the character */ UART_PORT->D = (uint8_t)c; /* Send the character */
} }
int hw_rand(void) uint32_t hw_rand(void)
{ {
while((RNG->SR & RNG_SR_OREG_LVL(0xF)) == 0) {}; /* Wait until FIFO has a value available */ while((RNG->SR & RNG_SR_OREG_LVL(0xF)) == 0) {}; /* Wait until FIFO has a value available */
return RNG->OR; /* Return next value in FIFO output register */ return RNG->OR; /* Return next value in FIFO output register */

View File

@@ -29,7 +29,7 @@ double current_time(int reset)
return time; return time;
} }
int custom_rand_generate(void) uint32_t custom_rand_generate(void)
{ {
return hw_rand(); return hw_rand();
} }

View File

@@ -18,7 +18,8 @@
/* Custom functions */ /* Custom functions */
extern int custom_rand_generate(void); extern int custom_rand_generate(void);
#define CUSTOM_RAND_GENERATE custom_rand_generate #define CUSTOM_RAND_GENERATE custom_rand_generate
#define CUSTOM_RAND_TYPE word32
#define WOLFSSL_USER_CURRTIME #define WOLFSSL_USER_CURRTIME
/* Debugging - Optional */ /* Debugging - Optional */

View File

@@ -10,12 +10,14 @@
project_type="Library" /> project_type="Library" />
<folder Name="Source Files"> <folder Name="Source Files">
<configuration Name="Common" filter="c;cpp;cxx;cc;h;s;inc" /> <configuration Name="Common" filter="c;cpp;cxx;cc;h;s;inc" />
<folder <folder Name="wolfcrypt">
Name="wolfcrypt" <folder Name="benchmark" path="../../wolfcrypt/benchmark" />
exclude="*.asm;*.s" <folder Name="src" path="../../wolfcrypt/src" />
filter="" <folder Name="test" path="../../wolfcrypt/test" />
path="../../wolfcrypt" <folder Name="user-crypto" path="../../wolfcrypt/user-crypto">
recurse="Yes" /> <configuration Name="Common" build_exclude_from_build="Yes" />
</folder>
</folder>
<folder <folder
Name="wolfssl" Name="wolfssl"
exclude="*.asm;*.s" exclude="*.asm;*.s"
@@ -24,6 +26,12 @@
recurse="Yes" /> recurse="Yes" />
<file file_name="user_settings.h" /> <file file_name="user_settings.h" />
<file file_name="README.md" /> <file file_name="README.md" />
<folder
Name="source"
exclude=""
filter=""
path="../../src"
recurse="No" />
</folder> </folder>
</project> </project>
<project Name="test"> <project Name="test">

View File

@@ -32,6 +32,12 @@
#include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/random.h>
#if defined(CUSTOM_RAND_GENERATE) && !defined(CUSTOM_RAND_TYPE)
/* To maintain compatiblity the default return vaule from CUSTOM_RAND_GENERATE is byte */
#define CUSTOM_RAND_TYPE byte
#endif
#ifdef HAVE_FIPS #ifdef HAVE_FIPS
int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz) int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz)
{ {
@@ -973,8 +979,22 @@ static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz)
#endif /* HAVE_INTEL_RDGEN */ #endif /* HAVE_INTEL_RDGEN */
#if defined(USE_WINDOWS_API) /* wc_GenerateSeed Implementations */
#if defined(CUSTOM_RAND_GENERATE_SEED)
/* Implement your own random generation function
* Return 0 to indicate success
* int rand_gen_seed(byte* output, word32 sz);
* #define CUSTOM_RAND_GENERATE_SEED rand_gen_seed */
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
(void)os;
return CUSTOM_RAND_GENERATE_SEED(output, sz);
}
#elif defined(USE_WINDOWS_API)
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{ {
@@ -1088,7 +1108,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) || \ #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) || \
defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS)
#ifdef FREESCALE_K70_RNGA #if defined(FREESCALE_K70_RNGA) || defined(FREESCALE_RNGA)
/* /*
* wc_Generates a RNG seed using the Random Number Generator Accelerator * wc_Generates a RNG seed using the Random Number Generator Accelerator
* on the Kinetis K70. Documentation located in Chapter 37 of * on the Kinetis K70. Documentation located in Chapter 37 of
@@ -1122,7 +1142,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
return 0; return 0;
} }
#elif defined(FREESCALE_K53_RNGB) #elif defined(FREESCALE_K53_RNGB) || defined(FREESCALE_RNGB)
/* /*
* wc_Generates a RNG seed using the Random Number Generator (RNGB) * wc_Generates a RNG seed using the Random Number Generator (RNGB)
* on the Kinetis K53. Documentation located in Chapter 33 of * on the Kinetis K53. Documentation located in Chapter 33 of
@@ -1165,7 +1185,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{ {
TRNG_DRV_GetRandomData(TRNG_INSTANCE, output, sz); TRNG_DRV_GetRandomData(TRNG_INSTANCE, output, sz);
return(0); return 0;
} }
#else #else
@@ -1273,12 +1293,25 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{ {
word32 i; word32 i = 0;
(void)os; (void)os;
for (i = 0; i < sz; i++ ) while (i < sz)
output[i] = CUSTOM_RAND_GENERATE(); {
/* If not aligned or there is odd/remainder */
if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz ||
((wolfssl_word)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0
) {
/* Single byte at a time */
output[i++] = (byte)CUSTOM_RAND_GENERATE();
}
else {
/* Use native 8, 16, 32 or 64 copy instruction */
*((CUSTOM_RAND_TYPE*)&output[i]) = CUSTOM_RAND_GENERATE();
i += sizeof(CUSTOM_RAND_TYPE);
}
}
return 0; return 0;
} }