Using CAU in K60 Chip [CW]

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using CAU in K60 Chip [CW]

Jump to solution
5,127 Views
alexpaul
Contributor I

Hello
I have problem with my project, The point is to send simple text to hardware hashing, and send back the result hash, i made the code
Here is the code: http://wklej.to/lh0DD
And password for open this link : d32f32

The problem is whatever i sent no matter which letter or number im always getting as result decoded 536806312, and before something like this
z P?rşőQ[©¸¤JĐä?`ŻčîĄqEs

Even when i changed code to

  for(;;)
  {
  Error = AS1_ReceiveBlock(MySerialPtr, InpData, 1U);
  while (!DataReceivedFlg);
  mmcau_sha256_initialize_output(out);
  sprintf(hashed, "%d ", out);
  Error = AS1_SendBlock(MySerialPtr, hashed, sizeof(hashed)); /*
  DataReceivedFlg = FALSE;
  }

Or without sprintf, always was the same result, i really dont know where the problem is, i tried to rewiev few's pdf, change the variables type with no positive result. say nothing of week waiting for library , it should be included in CW btw. Im looking forward for helpful reply.


Regards Alex

Labels (1)
Tags (3)
0 Kudos
Reply
1 Solution
4,215 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

The device on your board is a silicon rev 2.x, so it is right to create a new project for it, and your issue is due to you didn't enable the methods that can be selected when the "Expert" is clicked. Please kindly refer to the following for details;

4.PNG.png

Hope that helps,

B.R

Kan

View solution in original post

0 Kudos
Reply
25 Replies
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

the "out" for mmcau_sha256_initialize_output is a pointer to 32bit data, so if you want to use sprintf to store the integer pointed by "out" in a string, I think you have to use the following instead: sprintf(hashed, "%d ", (*out));

Hope that helps,

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi Kan,
Thanks for your reply, but sadly this tip didn't help. I got the same result, but after receive one "result" im getting PE_debughalt,
Result should be 0 or 1 as it is in mmcau/Cau User Guide, Can you give me correct code to send text and receive hash?
i begins to doubt that hardware hashing is really presented in this chip.


Kindly Regrads Alex

PS. Here is the code:

/* ###################################################################

**     Filename    : ProcessorExpert.c

**     Project     : ProcessorExpert

**     Processor   : MK60DN512VLQ10

**     Version     : Driver 01.01

**     Compiler    : GNU C Compiler

**     Date/Time   : 2013-06-20, 18:36, # CodeGen: 0

**     Abstract    :

**         Main module.

**         This module contains user's application code.

**     Settings    :

**     Contents    :

**         No public methods

**

** ###################################################################*/

/*!

** @file ProcessorExpert.c

** @version 01.01

** @brief

**         Main module.

**         This module contains user's application code.

*/        

/*!

**  @addtogroup ProcessorExpert_module ProcessorExpert module documentation

**  @{

*/        

/* MODULE ProcessorExpert */

/* Including needed modules to compile this module/procedure */

#include "Cpu.h"

#include "Events.h"

#include "AS1.h"

#include "PE_Types.h"

#include "PE_Error.h"

#include "cau_api.h"

#include "PE_Const.h"

#include "IO_Map.h"

#define FREESCALE_MMCAU

/* User includes (#include below this line is not maintained by Processor Expert) */

volatile bool DataReceivedFlg = FALSE;

char InpData[10];

unsigned char hashed[32];

int num_blks_sha256;

unsigned int *out;

int i;

char padded[192];  

LDD_TError Error;

LDD_TDeviceData *MySerialPtr;

int main(void)

/*lint -restore Enable MISRA rule (6.3) checking. */

{

  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/

  PE_low_level_init();

  /*** End of Processor Expert internal initialization.                    ***/

  MySerialPtr = AS1_Init(NULL); 

    for(;;)

    {

        Error = AS1_ReceiveBlock(MySerialPtr, InpData, 1U);             /* Start reception of one character */

        while (!DataReceivedFlg);

        mmcau_sha256_initialize_output(*out);

        sprintf(hashed, " %d ", (*out));

        Error = AS1_SendBlock(MySerialPtr, hashed, sizeof(hashed)); /* Send block of characters */

        DataReceivedFlg = FALSE;

    }

   

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/

  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/

  #ifdef PEX_RTOS_START

    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */

  #endif

  /*** End of RTOS startup code.  ***/

  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/

  for(;;){}

  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/

} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

void pad_sha256(char *p, char *padded)

{

    int i, nbits, nbytes;

    for (i=0; ; i++) {

       padded[i] = p[i];

       if (p[i] == 0) break;

    }

    nbytes = i;

    padded[i++] = 0x80;

    num_blks_sha256 = (((nbytes + 9) % 64) == 0)

             ?  ((nbytes + 9) / 64)

             :  ((nbytes + 9) / 64) + 1;

    for (; i < (64 * num_blks_sha256) - 4;)

       padded[i++] = 0;

    nbits = nbytes << 3;

    padded[i++] = nbits >> 24;

    padded[i++] = nbits >> 16;

    padded[i++] = nbits >> 8;

    padded[i] = nbits;

}

/* END ProcessorExpert */

/*!

** @}

*/

/*

** ###################################################################

**

**     This file was created by Processor Expert 10.2 [05.06]

**     for the Freescale Kinetis series of microcontrollers.

**

** ###################################################################

*/    

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

The MMCAU is a coprocessor that is connected to the processor's Private Peripheral Bus (PPB).It supports acceleration of the DES, 3DES, AES, MD5, SHA-1 and SHA-256 algorithms. and the proper usage for mmcau_sha256_initialize_output() is like below:

unsigned int out[8]; // encrypted output

mmcau_sha256_initialize_output (out);

Please kindly refer to the attached sample code for more details.

BTW, It looks like you use the UART to output the hashed data, so I think maybe you can use printf() to do that, it can be achieved by adding PE's component "ConsoleIO" instead of "Serial_LDD". Please kindly refer to the following for details.

3.PNG.png

Hope that helps,

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi, Kan
thanks for reply, i tried to add ConsoleIO but there is nothing happend after prinft(out);  i got no result in terra term,
When i changed variable, and send by the Serial_LDD , i got " 1779033703 " Does it mean that conversion sprintf(hashed, "%d ", out);  is wrong? Any idea to stay on Serial_ldd and make this workable?

Regards.

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

I think maybe you didn't use printf() correctly, the following is my code for your case,

  mmcau_sha256_initialize_output(out);

  for(i=0;i<8;i++)

  printf("out[%d]=%d\n\r",i,out[i]);

and the output is like below:

4.PNG.png

Please kindly refer to the attached project for more details. The project is based on TWR-K60N512.

Hope that helps,

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi Kan,
Im a little bit confused right now, i dont understund why the results aren't 0 or 1, i though that mmcau_sha256_initialize_output (out); alocates the output to Out and returns the value 0 or -1 does HW support exists and then its needed use mmcau_sha256_hash function to calculate checksum.

Best Regards
Thanks for your patience and helpfulness.

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

mmcau_sha256_initialize_output() is used to initialize the first 8 hash values which are defined by the sha256 algorithm , it is not generated by HW, they are fixed values for each start of sha256 calculation, please kindly refer to the following for details.

1.PNG.png

You may also refer  to http://en.wikipedia.org/wiki/SHA-2 for more details.


Hope that helps,

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi
But take a look what CAUAPIUG.pdf says Untitled.jpg
Anyway thanks for clarify using this functions.
Can you adapt or modify my code that may send whatever text to hash, by UART(serial_ldd) and next receive computed checksum back in TerraTerm by UART.
I would be very grateful, I tried for a long time run this project but allways with failure.
http://wklej.to/lh0DD
And password for open this link : d32f32


0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

Please kindly refer to the attached project for details, it is based on TWR-K60N512, and you will have the same output as I provided before.

Hope that helps,

B.R

Kan

3,513 Views
alexpaul
Contributor I

Hi, Kan

Thanks, it works fine, but this isn't end, becouse im trying to for ex. send "abcd" and receive from k60 by uart "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589"  , its possible? i tried few ways, but with failure.
Im really gratefull for your patience and help.
BR
Alex

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

It is possible to implement, please use the following code instead.

  /* Write your code here */

  /* For example: for(;;) { } */

  AS1_Handle = AS1_Init(NULL);

  mmcau_sha256_initialize_output(out);

  AS1_ReceiveBlock(AS1_Handle,buffer,4);

  while(AS1_GetReceivedDataNum(AS1_Handle)<4);

  buffer[4] = '\0';

  if(strcmp(buffer,"abcd")==0)

  {

   sprintf(buffer,"%x%x%x%x%x%x\n\r",out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]);

   AS1_SendBlock(AS1_Handle, buffer, strlen(buffer));

   while(!AS1_GetTxCompleteStatus(AS1_Handle));

  }

The output is "6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c" when you input "abcd" via UART.

Hope that helps,

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi, Kan
Im sorry but i mean that for example, i need to send abcd and get hashed value for sha256 abcd its=" 6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c" with  mmcau_sha256_hash  ,or send whatever text to hash and get hash of it from mmcau, and so on and so forth for next hashes.

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

Sorry for the misunderstanding!! Please refer to the attached project for your requirement. You may input any string ended by pressing Enter, and the hashed value will be output via UART. BTW, 6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c you mentioned is not the hashed value for "abcd", it is the first 6 initial hashed value for sha256. You may refer to my previous post for details.

Hope that helps,

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi Kan, Thanks for upload this project. Now i thereabout know how it works, but sadly when i imported your project, and tried to compile i got,
Undefined refernce to 'mmcau_sha256_hash and mmcau_sha256_initialize_output, i tried to readd cau_api.h with no success, but i think the problem is in lib_mmcau.a but i can't remeber how i added this in my earlier project. So i opened my old project and replaced procesor expert code with your code, and tried to compile but i got few error's1.JPG.jpgT
Tried to fix it, but when finally compiled and started debugging i got an PE_debughalt; sorry if these problems stem from my lack of knowledge in the fundamentals of using code warrior.i really appreciate your patience. Regards :smileyhappy:

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

Are you referring to the FLASH configuration of the example? If you are using the SRAM configuration you will have issue as I didn't set it up properly. Please refer to the following for details.

4.PNG.png

I also asked my colleague to open it in his laptop and compile, the result is good as expected.

Please kindly let me know if you have any issue.

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi, Kan
Sadly im getting pe_debug halt, i tried on PEE 100mhz and FEI 95mhzsdas.JPG.jpg

0 Kudos
Reply
3,513 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

Is the example running on TWR-K60N512? If you run it on a custom board, you will have issue like that as the mismatch of configuration.

Hope that helps,

B.R

Kan

0 Kudos
Reply
3,513 Views
alexpaul
Contributor I

Hi, Kan
I have ma own board(FEI mode,withour oscilator) but i tried too on Mcu mini board(PEE on oscilator 50mhz,from china), but whats difference? The k60 is the same as in Tower module, any idea to fix this?

Kindly Regards

0 Kudos
Reply
3,512 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Alex,

What is the mask set on your device? You know K60 has two rev type, one is Rev 1.x which has 0M33Z,0N30D,!N30D,2N30D and 4N30D, the other is rev 2.x, which has 2N22D. My project is for Rev 1.x, so if you are using Rev 2.x, you will have problem.

B.R

Kan

3,512 Views
alexpaul
Contributor I

Hi Kan,
I was pretty sure that Tower board have version without Z as in my project, anyway i made a new project with your main code, But i got 3 critical errors, and dont know where is the issue.
Untitled.png
Btw Now I think this project is too difficult at the beginning of the arm, but i learned few intersting things, thanks. Regards Alex

0 Kudos
Reply