This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AES encryption/decryption code

Other Parts Discussed in Thread: CC2530

Hello,

I´m Trying to use the sample code from the DN108 - Using AES Encryption in C111xFx, CC243x and CC251xFx to try to program an encryption code in my project. I´m using the CC2530, so is it possible?

If not is there any DN, AN or another thing that can help me to encrypt and decrypt my data?

  • I´ve just forget to say that I´m using the code without DMA from section 4.2 of the document I said.

  • Hi,

    Example code for encryption without DMA is also given in this app note as well.

    If you have any specific question, i can try to awnser

  • So...

    I´m using the code for encryption without DMA like you said, but when I try to execute it, I put a breakpoint in every line of the specific code one at a time, and when I put the breakpoint after the last line: while(!(ENCCS & 0x08)); it didn´t get there. I don´t know why, because befores the NOP´s it works, and in this linte it doesn´t work. Can you helo me?

  • Hi,

    well its difficult to tell this way, which mode are you using for encryption.

    If possible can you send the code, i can have a look

     

    regard

    waqar

  • I´m using exactly the code from the section 4.2 of the document, that is the mode without DMA. The code is as follows:

     

    // Define size parameters for the AES processing: 

    #define SIZE_OF_ENC_BUFFER     128 

    #define SIZE_OF_AES_BLOCK      16 

    #define SIZE_OF_ENC_PACKET     8*SIZE_OF_AES_BLOCK 

     

    __no_init BYTE __xdata aes_buffer_1[SIZE_OF_ENC_BUFFER];  // Source buffer 

    __no_init BYTE __xdata aes_buffer_2[SIZE_OF_ENC_BUFFER];  // Target buffer 

     

    unsigned short i, j; 

     

    // Define size of AES block 

    #define SIZE_OF_AES_BLOCK      16 

     

    // Allocate source buffers for AES key and IV/NONCE 

    __no_init unsigned char __xdata key[SIZE_OF_AES_BLOCK]; 

    __no_init unsigned char __xdata iv_nonce[SIZE_OF_AES_BLOCK]; 

     

    unsigned short i; 

     

    // Generate Key: 

    // In a radio application this is typically generated based on 

    // a standard/protocol specification. However, for simplicity, 

    // this design note does not use any particular specification 

    // for generating it. 

    for(i = 0; i < SIZE_OF_AES_BLOCK; i++) { key[i] = i * 2; } 

     

    // Download Key (Set ENCCS.CMD = 10b), 

    // and start corresponding AES process (ENCCS.ST = 1) 

    ENCCS = 0x04 | 0x01; 

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

      ENCDI = key[i]; 

     

    // Monitor AES (ENCCS.RDY) to wait until key downloaded 

    while(!(ENCCS & 0x08)); 

     

    // Generate IV/NONCE: 

    // In a radio application this is typically generated based on 

    // a standard/protocol specification. However, for simplicity, 

    // this design note does not use any particular specification 

    // for generating it. 

    for(i = 0; i < SIZE_OF_AES_BLOCK; i++) { iv_nonce[i] = i; } 

     

    // Dwonload IV/NONCE (Set ENCCS.CMD = 11b) 

    // and start corresponding AES process (ENCCS.ST = 1) 

    ENCCS = 0x06 | 0x01; 

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

      ENCDI = iv_nonce[i]; 

     

    // Monitor AES (ENCCS.RDY) to wait until IV/NONCE downloaded 

    while(!(ENCCS & 0x08));

     

    // Perform AES encryption/decryption on allocated AES buffer ("aes_buffer_1"): 

    for (j = 0; j < SIZE_OF_ENC_PACKET/SIZE_OF_AES_BLOCK; j++) { 

     

      // Configure and start AES coprocessor for the desired AES mode: 

      // Encryption => ENCCS.CMD = 00b, decryption mode => ENCCS.CMD = 01b. 

      // Use so-called Cipher Block Chaining encryption mode => ENCCS.MODE = 000b. 

      // Start AES processing (ENCCS.ST = 1) of source buffer (aes_buffer_1). 

      ENCCS = 0x00 | 0x01;  // Only valid for encryption mode ! 

      //ENCCS = 0x02 | 0x01;  // Only valid for decryption mode ! 

     

      // Download data block (16 bytes) to AES coprocessor: 

      // In encryption mode "aes_buffer_1" represents the data to be encrypted. 

      // In decryption mode "aes_buffer_1" represents the data to be decrypted. 

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

        ENCDI = aes_buffer_1[i+(j*SIZE_OF_AES_BLOCK)]; 

      } 

     

      // Wait until AES download is finished, that is; apply delay 

      // equivalent to 40 NOPs: 

      asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); 

      asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); 

      asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); 

      asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); 

      asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); 

      asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP"); 

     

      // Upload data block (16 bytes) to allocated AES buffer: 

      // In encryption mode "aes_buffer_2" represents the encrypted data. 

      // In decryption mode "aes_buffer_2" represents the decrypted data. 

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

        aes_buffer_2[i+(j*SIZE_OF_AES_BLOCK)] = ENCDO; 

      } 

     

      // Monitor AES (ENCCS.RDY) to wait until data block downloaded 

      while(!(ENCCS & 0x08)); 

     

    I´ve just copied and paste from the document in my code and above.

    The loop for (j = 0; j < SIZE_OF_ENC_PACKET/SIZE_OF_AES_BLOCK; j++) { } is done just one time, and I couldn´t understand why.

    Can you help?