WolfSSL got back to me on the problem. Essentially they told me that the handshake negotiation was failing because we were not generating a random key like we were suppose to. The issue had to do with MQX's 4.2.0 Cyassl Distribution of code using the default "stub" function for the seed generation, i.e., frdm-k64 demo of https is using the following code for generating the seed:
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
int i;
for (i = 0; i < sz; i++ )
output[i] = i;
return 0;
}
Which as you can see is not random and was causing the "bad MAC" errors. So I ended up using the following:
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
TIME_STRUCT st_Time = { 0 };
_time_get( &st_Time );
srand( ((st_Time.SECONDS * 1000) + st_Time.MILLISECONDS) * 25);
for( int i = 0; i < sz; i++ ) {
output[i] = rand() % 256;
if ( (i % 8) == 7)
srand(((st_Time.SECONDS * 1000) + st_Time.MILLISECONDS)* 25);
}
return 0;
}
and it totally fixed the problem. I don't want to mark this resolved because I first attempted to use the following:
/*
* 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;
/* turn on RNGA module */
SIM_SCGC3 |= SIM_SCGC3_RNGA_MASK;
/* 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;
Although I kept getting a hard fault as soon as we would write to RNG_CR register. In case you are wondering what I'm talking about: I wanted to use K64's random number generator accelerator as apposed to the software solution that I came up with. When I looked at the preprocessor output I noticed that the pointer addresses were all being expanded out properly, below is a snippet from the preprocessor output (IAR's .i file)
/*
* 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;
/* turn on RNGA module */
((((SIM_MemMapPtr)0x40047000u))->SCGC3) |= 0x1u; -------------------- This is where the fault would occur.
/* set SLP bit to 0 - "RNGA is not in sleep mode" */
((((RNG_MemMapPtr)0x40029000u))->CR) &= ~0x10u;
/* set HA bit to 1 - "security violations masked" */
((((RNG_MemMapPtr)0x40029000u))->CR) |= 0x2u;
/* set GO bit to 1 - "output register loaded with data" */
((((RNG_MemMapPtr)0x40029000u))->CR) |= 0x1u;
for (i = 0; i < sz; i++) {
/* wait for RNG FIFO to be full */
while((((((RNG_MemMapPtr)0x40029000u))->SR) & (((uint32_t)(((uint32_t)(0xF))<<8))&0xFF00u)) == 0) {}
/* get value */
output[i] = ((((RNG_MemMapPtr)0x40029000u))->OR);
}
return 0;
}