PIC32MZ RNG

This commit is contained in:
Takashi Kojo 2013-11-11 12:15:19 +09:00
parent 23cada35ba
commit 0048c20fe5

View File

@ -460,29 +460,78 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#elif defined(MICROCHIP_PIC32)
#ifdef MICROCHIP_MPLAB_HARMONY
#define PIC32_SEED_COUNT _CP0_GET_COUNT
#else
#if !defined(CYASSL_MICROCHIP_PIC32MZ)
#include <peripheral/timer.h>
#ifdef MICROCHIP_MPLAB_HARMONY
#define PIC32_SEED_COUNT _CP0_GET_COUNT
#else
#if !defined(CYASSL_MICROCHIP_PIC32MZ)
#include <peripheral/timer.h>
#endif
#define PIC32_SEED_COUNT ReadCoreTimer
#endif
#define PIC32_SEED_COUNT ReadCoreTimer
#ifdef CYASSL_MIC32MZ_RNG
#include "xc.h"
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
int i ;
byte rnd[8] ;
word32 *rnd32 = (word32 *)rnd ;
word32 size = sz ;
byte* op = output ;
RNGNUMGEN1 = ReadCoreTimer();
RNGPOLY1 = 0x01020304;
RNGPOLY2 = 0x05060709;
RNGNUMGEN2 = ReadCoreTimer();
#ifdef DEBUG_CYASSL
printf("GenerateSeed::Seed=%08x, %08x\n", RNGNUMGEN1, RNGNUMGEN2) ;
#endif
RNGCONbits.PLEN = 0x40;
RNGCONbits.PRNGEN = 1;
for(i=0; i<3; i++) { /* wait for RNGNUMGEN ready */
volatile int x ;
x = RNGNUMGEN1 ;
x = RNGNUMGEN2 ;
}
do {
rnd32[0] = RNGNUMGEN1;
rnd32[1] = RNGNUMGEN2;
/* uses the core timer, in nanoseconds to seed srand */
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
int i;
srand(PIC32_SEED_COUNT() * 25);
for(i=0; i<8; i++, op++) {
*op = rnd[i] ;
size -- ;
if(size==0)break ;
}
} while(size) ;
for (i = 0; i < sz; i++ ) {
output[i] = rand() % 256;
if ( (i % 8) == 7)
#ifdef DEBUG_CYASSL
printf("\nReturn=") ;
for(i=0; i<sz; i++) {
printf("%02x", output[(i/4)*4 + (3-(i%4))]) ;
if((i+1)%4==0)putchar(',') ;
}
putchar('\n') ;
#endif
return 0;
}
#else
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
int i;
srand(PIC32_SEED_COUNT() * 25);
}
return 0;
}
for (i = 0; i < sz; i++ ) {
output[i] = rand() % 256;
if ( (i % 8) == 7)
srand(PIC32_SEED_COUNT() * 25);
}
return 0;
}
#endif
#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK)
@ -573,7 +622,7 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
return 0;
}
#else
#else
#warning "write a real random seed!!!!, just for testing now"
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
@ -584,7 +633,7 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
return 0;
}
#endif /* FREESCALE_K70_RNGA */
#endif /* FREESCALE_K70_RNGA */
#elif defined(STM32F2_RNG)
#undef RNG