From 11c8e5afb81ade5af4f806467fa5960778c8f487 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Fri, 2 Nov 2012 17:49:30 -0600 Subject: [PATCH] add support for Kinetis K70 HW RNGA --- README | 6 +++++ ctaocrypt/src/random.c | 51 +++++++++++++++++++++++++++++++------ cyassl/ctaocrypt/settings.h | 1 + 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/README b/README index b381d6bf2..58dcded67 100644 --- a/README +++ b/README @@ -32,6 +32,12 @@ SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); before calling SSL_new(); Though it's not recommended. + +Note 3) +The Freescale Kinetis K70 RNGA documentation can be found in Chapter 37 of the +K70 Sub-Family Reference Manual: +http://cache.freescale.com/files/microcontrollers/doc/ref_manual/K70P256M150SF3RM.pdf + *** end Note *** CyaSSL Release 2.4.0 (10/10/2012) diff --git a/ctaocrypt/src/random.c b/ctaocrypt/src/random.c index 43bfbbb97..58d485fcd 100644 --- a/ctaocrypt/src/random.c +++ b/ctaocrypt/src/random.c @@ -161,16 +161,51 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz) #elif defined(FREESCALE_MQX) -#warning "write a real random seed!!!!, just for testing now" + #ifdef FREESCALE_K70_RNGA + /* + * Generates a RNG seed using the Random Number Generator Accelerator + * on the Kinetis K70. Documentation located in Chapter 37 of + * K70 Sub-Family Reference Manual (see Note 3 in the README for link). + */ + int GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + int i; -int GenerateSeed(OS_Seed* os, byte* output, word32 sz) -{ - int i; - for (i = 0; i < sz; i++ ) - output[i] = i; + /* turn on RNGA module */ + SIM_SCGC3 |= SIM_SCGC3_RNGA_MASK; - return 0; -} + /* set SLP bit to 0 - "RNGA is not in sleep mode" */ + RNG_CR &= ~RNG_CR_SLP_MASK; + + /* set HA bit to 1 - "security violations masked" */ + RNG_CR |= RNG_CR_HA_MASK; + + /* set GO bit to 1 - "output register loaded with data" */ + RNG_CR |= RNG_CR_GO_MASK; + + for (i = 0; i < sz; i++) { + + /* wait for RNG FIFO to be full */ + while((RNG_SR & RNG_SR_OREG_LVL(0xF)) == 0) {} + + /* get value */ + output[i] = RNG_OR; + } + + return 0; + } + #else + #warning "write a real random seed!!!!, just for testing now" + + int GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + int i; + for (i = 0; i < sz; i++ ) + output[i] = i; + + return 0; + } + #endif /* FREESCALE_K70_RNGA */ #elif defined(NO_DEV_RANDOM) diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index ac8042cb2..d38956be9 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -203,6 +203,7 @@ #define NO_CYASSL_DIR #define USE_FAST_MATH #define TFM_TIMING_RESISTANT + #define FREESCALE_K70_RNGA #ifndef NO_FILESYSTEM #include "mfs.h" #include "fio.h"