encrypt/decrypt AES data

Questions for programming 8051 microcontrollers in C or assembly using SDCC

encrypt/decrypt AES data

Postby eninru » Fri Mar 08, 2013 8:53 am

Hi,

I'm trying to make a program with encrypt/decrypt AES data, but I have a few problems. I'm using the second SDK (cryptography) that I downloaded of the blog (http://blog.diyembedded.com/2010_06_01_archive.html).
I think encryption is correct but not decryption, when I try to see decryption I see a different message than the origin.

Image

Each time that I call function "arc4_encrypt" or "arc4_decrypt", have I call "arc4_initialize"?? and when I call "arc4_encrypt" this function does a "arc4_encrypt", does it?

This is my code:

Code: Select all
#include <stdio.h>
#include "reg24le1.h"
#include "uart.h"
#include "interrupt.h"
#include "delay.h"
#include "rtc2.h"
#include "rf.h"
#include "arc4.h"
#include "aes.h"
#include "aes_user_options.h"
#include "util.h"
#include <string.h>

#define KEYlength 32
#define TEXTlength 12

void initialize();
void putchar(char c);
char getchar();

unsigned char *encrypt,encrypt_str[13], *decrypt,decrypt_str[13],key[KEYlength];
char text[TEXTlength],*text_str;
arc4_data_t data;

void main()
{
   int i;

   strcpy(key,"12345678901234567890123456789012");
   strcpy(text_str,"text to send");
   initialize();

      arc4_initialize(&data, key, KEYlength);
      memcpy(text,text_str,TEXTlength);
      printf("Origin: %s \r\n\r\n",text_str);

      //Encryption
      arc4_encrypt(&data, text, encrypt, TEXTlength);
      memcpy(encrypt_str,encrypt,12);
      encrypt_str[12]='\0';
      printf("Encryption: ");
      for(i=0;i<12;i++){
               putchar(encrypt[i]);
            }
      printf("\r\n\r\n");

      //decryption
      arc4_initialize(&data, key, KEYlength);
      arc4_decrypt(&data, encrypt, decrypt, TEXTlength);
      memcpy(decrypt_str,decrypt,12);
      decrypt_str[12]='\0';
      printf("Decryption : ");
      for(i=0;i<12;i++){
               putchar(decrypt[i]);
            }
      printf("\r\n\r\n");
}

void initialize()
{
   //Set up CLKLF1 pin
   gpio_pin_configure(GPIO_24P_PIN_ID_FUNC_CLKLF1,
                  GPIO_PIN_CONFIG_OPTION_DIR_OUTPUT |
                  GPIO_PIN_CONFIG_OPTION_OUTPUT_VAL_CLEAR |
                  GPIO_PIN_CONFIG_OPTION_PIN_MODE_OUTPUT_BUFFER_NORMAL_DRIVE_STRENGTH);

   gpio_pin_configure(GPIO_24P_PIN_ID_FUNC_CLKLF2,
                     GPIO_PIN_CONFIG_OPTION_DIR_INPUT |
                     GPIO_PIN_CONFIG_OPTION_PIN_MODE_INPUT_BUFFER_ON_NO_RESISTORS);
   //Set up UART pins
      gpio_pin_configure(GPIO_24P_PIN_ID_FUNC_RXD,
                     GPIO_PIN_CONFIG_OPTION_DIR_INPUT |
                     GPIO_PIN_CONFIG_OPTION_PIN_MODE_INPUT_BUFFER_ON_NO_RESISTORS);

      gpio_pin_configure(GPIO_24P_PIN_ID_FUNC_TXD,
                     GPIO_PIN_CONFIG_OPTION_DIR_OUTPUT |
                     GPIO_PIN_CONFIG_OPTION_OUTPUT_VAL_SET |
                     GPIO_PIN_CONFIG_OPTION_PIN_MODE_OUTPUT_BUFFER_NORMAL_DRIVE_STRENGTH);

      //Set up UART for 38400 baud
      uart_configure_manual_baud_calc(UART_CONFIG_OPTION_ENABLE_RX |
                              UART_CONFIG_OPTION_MODE_1_UART_8_BIT |
                              UART_CONFIG_OPTION_CLOCK_FOR_MODES_1_3_USE_BR_GEN |
                              UART_CONFIG_OPTION_BIT_SMOD_SET,
                              1011);
}


void putchar(char c)
{
   uart_send_wait_for_complete(c);
}

char getchar()
{
   unsigned char retchar;

   uart_wait_for_rx_and_get(&retchar);

   return retchar;
}


Is it wrong? Does anyone see any errors?

Thanks.

P.S: I couldn't put this post in Cryptography forum.
eninru
 
Posts: 2
Joined: Fri Mar 08, 2013 8:33 am

Re: encrypt/decrypt AES data

Postby brennen » Fri Mar 08, 2013 1:21 pm

Have you looked at the example code that is provided in nRF24L01 tutorial 4? I would suggest trying to port that code to the nRF24LE1 first as a test. If the library got broken at some point when I ported it over to the LE1, then that could also be a problem. To my knowledge I tested it, but that was a while back. 8-)
brennen
Site Admin
 
Posts: 395
Joined: Sun Aug 17, 2008 2:15 pm

Re: encrypt/decrypt AES data

Postby eninru » Mon Mar 11, 2013 9:35 am

Hi brennen,

I already found the mistake and I can correct it.

thank you for respond!
eninru
 
Posts: 2
Joined: Fri Mar 08, 2013 8:33 am

Re: encrypt/decrypt AES data

Postby brennen » Mon Mar 11, 2013 12:24 pm

No problem! I'm glad to hear you got it working. :)
brennen
Site Admin
 
Posts: 395
Joined: Sun Aug 17, 2008 2:15 pm

Re: encrypt/decrypt AES data

Postby anlpereira » Fri Apr 19, 2013 9:09 am

Can you post what was the error?
anlpereira
 
Posts: 3
Joined: Fri Apr 19, 2013 1:22 am


Return to 8051 Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron