Pablo
Pablo

Reputation: 29519

Symmetric encryption algorithm for embedded system

Looking for recommendations and some reference code for encrypting byte array in C. The problem is that I have to fit into 1KByte memory along with other routines and MCU is only 8MHz. So the size and speed is the key. I've checked Rijndael but it has huge tables for my MCU. Basically I am going to encrypt intel hex format on PC, probably only data area, then decrypt in MCU.

Using dynamic memory allocation routines is not desirable.

My google search brings me all to C# implementations, using libraries.

UPDATE:

decryption side constraints:

RAM: 512 byte
MAX code size: 512-1024 words
CPU: 8 bit, 8MHz

Upvotes: 11

Views: 7769

Answers (4)

Mihai Galos
Mihai Galos

Reputation: 1897

Have a look at my mofified version of XTEA I called Simple Tea:

#include <avr/io.h>
#include "simple_tea.h"

uint8_t secret_data[] = {0xFE, 0x67};
SimpleTEA<sizeof(secret_data) / 2> tea;

int main()
{

    unsigned int rounds{16};
    uint8_t key[] = {0x45, 0x74, 0x32, 0x11, 0x98, 0x94, 0xAB, 0xCF, 0x90, 0xAE, 0xBA, 0xDC, 0x06, 0x16, 0x81, 0x95};

    secret_data[0] = PINB;
    tea.encrypt(rounds, secret_data, key);
    if (secret_data[0] & (PINB))
    {
        PORTC = 0x05;
    }
    secret_data[1] = PINB & (1 << 2);
    tea.decrypt(rounds, secret_data, key);
    if (secret_data[0] & (PINB))
    {
        PORTC = 0x50;
    }
}

produces <250 bytes of opcode on AVR (the ifs here are only needed to avoid the compiler optimizing the encrypt and decrypt out completely):

➜  Encryption git:(master) ✗ avr-gcc -std=c++17  -Os -mmcu=atmega328p  main.cpp -o main
➜  Encryption git:(master) ✗ avr-size main                                             
   text    data     bss     dec     hex filename
    214       2       1     217      d9 main
➜  Encryption git:(master) ✗ avr-nm --size-sort -C -r --radix=d main
00000044 T main
00000022 T __do_copy_data
00000016 T __do_clear_bss
00000002 D secret_data
00000001 B tea

Upvotes: 0

jdr5ca
jdr5ca

Reputation: 2819

I have read that Elliptic curve crypto (ECC) is the new thing intended to bring strong crypto to tiny processors.

A Google of:
elliptic curve cryptography microcontrollers
yields discussion of implementations on 8-bit micros at the top of the list.

But maybe consider moving to Stack Exchange. For example, this may be useful:

Stack Exchange Crypto - Types of Cryptography for a 4-8 bit microcontroller

Upvotes: -1

Aki Suihkonen
Aki Suihkonen

Reputation: 20017

All the tables in Rijndael are defined as simplish operations in GF2. As such, I'd be tempted to say it's possible to write eg. 128-bit AES in 1k.

See also: https://electronics.stackexchange.com/questions/13275/smallest-aes-implementation-for-microcontrollers

But any chosen algorithm is a minor factor in security eg. if the key is distributed with the binary. In that case, the simplest mechanism to unveil the original binary is to run the code through emulator and store the memory.

Upvotes: 4

sellibitze
sellibitze

Reputation: 28087

A very simple encryption algorithm that I saw being used in the embedded world is XXTEA

Upvotes: 14

Related Questions