Skip to content
George edited this page Mar 18, 2019 · 4 revisions

The AES (Advanced Encryption Standard) is a symmetric encryption technology that the CC430 chip has embedded in it for accelerating AES related tasks. Symmetric meaning that both the transmitter and the receiver must have a predefined common key stored for encrypting and decrypting the data sent between them. Panstamp has implemented 2 ways of controlling AES. One is using the aes class and another using ctr control registers. Bellow are the instructions, in order, for encrypting and then decrypting a message:

Example (aes class object)

#include "HardwareSerial.h"
#include "cc430aes.h"

CC430AES aes;

void setup()
{ 
  Serial.begin(9600);
  
  // Declaring our variables: our 128-bit key, our data...
  byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  byte data[] = "Any sort of message"; //Data to be encrypted/decrypted needs to be aligned to 16 bytes..
  Serial.print("Input string: ");
  Serial.println((char*) data);
  
    
  // Set key
  Serial.println("Setting AES-128 key");
  aes.setKey(key);
 
  // Encrypt
  Serial.println("Encripting...");
  aes.aesEncrypt(data, sizeof(data)-1);
  Serial.print("Data encrypted: ");
  Serial.println((char*) data);

  // Decrypt
  Serial.println("Now decripting...");
  aes.aesDecrypt(data, sizeof(data)-1);  
  Serial.print("Data decrypted: ");
  Serial.println((char*) data);
}

void loop()
{
}

Example (w/ ctr cipher)

#include "HardwareSerial.h"
#include "cc430aes.h"

void setup()
{ 
  Serial.begin(9600);
  
  //Place data, nonce and 128-bit key here. Data does not need to be aligned to 16 bytes.
  byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  byte data[] = "Some data";
  long nonce = 1654;
  
  // Set key
  Serial.println("Setting AES-128 key");
  CC430AES::setKey(key);
    

  // Encrypt
  Serial.print("Input string: ");Serial.println((char*) data);
  Serial.println("Encripting...");
  CC430AES::ctrCrypto(data, sizeof(data)-1, nonce);
  Serial.print("Data encrypted: ");Serial.println((char*) data);
  Serial.println("");
  
  // Decrypt
  Serial.println("Now decripting...");
  CC430AES::ctrCrypto(data, sizeof(data)-1, nonce);
  Serial.print("Data decrypted: ");Serial.println((char*) data);
}

void loop()
{
}