Reputation: 53
Background: I am trying to encrypt/decrypt 128 byte of data [using AES128] in an Arduino Uno. When I try to encrypt 128 bytes of data, only 128 bits of it get encrypted. So, in the serial monitor it is showing 16 bytes of encrypted data + 112 bytes of plaintext data.
Question: How can I encrypt the whole data? I am new to programming and I probably need to divide the data into small sizes but I am not sure how to do that.
Please be informed that for encryption and decryption purpose I am using AES code using this source.
Simplified code:
#include //include headerfile and library for AES
void setup(){
Serial.begin(9600);
}
void loop(){
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data[] = "ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF12345 67890ABCDEF1234567890ABCDEF1234567890";//128 byte
aes128_enc_single(key, data);
Serial.println(data);
aes128_dec_single(key, data);
Serial.println(data);
delay(15000);
}
Upvotes: 4
Views: 7584
Reputation: 2819
That library uses 128 bit keys and 128 bit block size.
The function ase128_enc_single(), encrypts a single block, which is 16 characters. This is just as documented in the library:
// encrypt single 128bit block. data is assumed to be 16 uint8_t's
// key is assumed to be 128bit thus 16 uint8_t's
void aes128_enc_single(const uint8_t* key, void* data);
The simple way to complete the long string is to do in 8 chunks:
for(size_t ix = 0; ix < 128; ix += 16) {
aes128_enc_single(key, data+ix);
}
The more common method is block chaining:
unsigned long sendcount = 0;
uint8_t iv[] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
aes128_cbc_enc(key, iv, data, 128);
The initialization vector, iv, should be random and never repeat to the extent possible. Use a simple counter to keep from repeating. To add random bits, use the current time, an unused analog pin input, and or the not so random rand(). Pack all those into the iv:
unsigned long t = micros();
int a = analogRead(pinConnectedToNothing);
long r = random(0x8fff);
sendcount += 1;
memcpy(iv, &sendcount, 4);
memcpy(iv+6, &t, 4);
memcpy(iv+10, a, 2);
memcpy(iv+12, r, 4);
You have to exchange the iv with the receiver so that the encoded text can be decoded.
Upvotes: 4