AES256 using on Arduino

Hello there!
Does anyone know how to use AES256 on Arduino? I have found a library: arduinolibs/libraries/Crypto at master · rweather/arduinolibs · GitHub . And in the examples I've found this

piece of code: static TestVector const testVectorAES256 = {
    .name        = "AES-256-ECB",
    .key         = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F},
    .plaintext   = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                    0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
    .ciphertext  = {0x8E, 0xA2, 0xB7, 0xCA, 0x51, 0x67, 0x45, 0xBF,
                    0xEA, 0xFC, 0x49, 0x90, 0x4B, 0x49, 0x60, 0x89}
};

How to use it? How can I put my own key and phrase to encrypt?
Thanks!

I too would like a basic example of this, the documentation reads more as a cheat sheet. :slight_smile:

The library includes example programs. Which part of the TestAES.ino didn't you understand?

1 Like

Hey, I would like to revive this old thread.

I understand how the library works, but what I do not understand is why padding is not included?

I can get over the fact that I have to pad, but also, when encrypting data and returning the buffer it only returns 16 bytes.

So in my mind, I could chunk the data out in a couple for loops and then push it into a nother buffer, but when I do, it doesn't return whats expected.

Has anyone used this library for encryption, and if so, can you please provide a snippet of how you overcame the issues I am having?

Alright, well.. Since there is no answer yet I just used my own implementation. If anyone is seasoned in AES256 ECB more than I am I have a question.

What is the normal padding strategy for key > data?

Otherwise, hopefully this helps someone else in their journey. It isn't a true implementation. but if you are passing data between programs as I am, this will work. I would really love for someone with some expertise to correct my mistakes and offer some guidance.

#include <Crypto.h>
#include <AES.h>

AES256 aes256;
BlockCipher *cipher = &aes256;

void setup() {
  Serial.begin(115200);
  while (!Serial.available()) {}

  uint8_t buffer[1024];
  uint8_t blockBuffer[cipher->blockSize()];
  uint8_t messageBuffer[cipher->blockSize()];
  uint8_t key[33] = "P@ssw0rdP@ssw0rdP@ssw0rdP@ssw0rd"; // 32 Byte key
  char message[1024] = "super secret message"; // message of up to 1024 in length

  // Calculate the total length, number of full blocks, and leftover characters < the size of a block.
  int msgLen = strlen((char*)message);
  int blocks = msgLen/16;
  int leftOver = msgLen%16;

  // Loop over the blocks
  for (int i = 0; i <= blocks; i++) {
    int startingPosition = i * cipher->blockSize();
    int endingPosition = startingPosition + cipher->blockSize();

    // Loop over the data in i block and push it into a temp buffer
    for (int k = startingPosition; k < endingPosition; k++) {

      // if we have leftover data, we need to control it by padding. 
      if (i == blocks && leftOver != 0 && k >= startingPosition + leftOver) {
        messageBuffer[k - startingPosition] = 0x00;
      } else {
        messageBuffer[k - startingPosition] = message[k];
      }
    }

    // Perform crypto on the temp buffer and push it to another temp buffer
    crypto_feed_watchdog();
    cipher->setKey(key, cipher->keySize());
    cipher->encryptBlock(blockBuffer, messageBuffer);

    // push the temp buffer to the final buffer
    for (int m = startingPosition; m < endingPosition; m++) {
      buffer[m] = blockBuffer[m - startingPosition];
    }
  }

  // If we have dangling data increment blocks
  if (leftOver != 0){
    blocks = blocks + 1;
  }

  // Print the encrypted hex
  for (int i = 0; i < blocks * (int)cipher->blockSize(); i++) {
    Serial.printf("%02X", buffer[i], HEX);
  }
  Serial.println();
}

void loop() {}
1 Like