header image

Tinkering with Arduino

I decided to play a bit with my old Arduino again. It’s a pretty dated model (Duemilanove). Based on the ATmega328P microcontroller it’s clocked at 16MHz, has 30kB of usable flash and 2kB of SRAM.

Arduino Duemilanove

Arduino Duemilanove

That’s not much to go with but I always wanted to make something that would transform the board into some kind of security device for the PC: an authentication token, encryption device, whatever. Of course the ATmega328P doesn’t have any “secure memory” so it can’t be anything serious, but I like to learn new things. In the past I’ve made some circuits with sensors, LCDs, the usual basic stuff. I’ve never really tried a two-way communication with PC though.

First things first, I needed a development environment. I never really liked the Arduino IDE, it’s too simplistic and tries to hide every hardware complexity from you. I guess that’s the point of Arduino but I wanted something better. In the past I’ve used Atmel’s AVR Studio (which apparently is just Atmel Studio nowadays, I should take a look at what’s changed). After some searching I found Visual Micro, a plugin for Visual Studio. It’s free (I think the USB debugging functionality requires a paid license) and works with the free Visual Studio Community edition. Perfect.

You need the standard Arduino IDE for the toolchain and libraries so install that before Visual Micro. The Visual Micro extension works fine although it’s apparently written by people that don’t know what multithreading is — every longer operation (like flashing the chip) freezes the whole VS IDE. It’s just a minor annoyance though. After installation you need to configure the Arduino software path and select your board from the configuration menu. After that you’re pretty much good to go.

I decided to write something simple to start — a program that would perform symmetric encryption on the data sent from the PC. The key would only be stored on the Arduino, in the future maybe initialized from user input during “device pairing”.

2kB of memory doesn’t leave much space for anything fancy. After some research I found this implementation of AES128 that’s specifically targeted at small microcontrollers and optimized for size. Of course the input data needed to be processed in chunks because of memory constraints.

Arduino code Show

The communication goes through a virtual serial port over a USB cable. When you connect the board to the PC Windows should install necessary drivers from Windows Update (serial is emulated by a standard FTDI UART chip on the Arduino). Since the serial port is used for data communication I couldn’t really use it for error reporting so I’m just blinking the internal LED if something goes wrong. I could hook up a LCD or something but I’m lazy. 🙂

The PC-side code is equally simple to write: open the appropriate COM port, read some file, send data, read data, write output file, done. But… it didn’t work. I couldn’t get the communication going properly. My Windows program sends the total data size first and the Arduino echoes it to acknowledge. Problem was, I was receiving seemingly random values from the Arduino every time. I tried to tinker with the COM port settings in Windows, to no avail. With some settings the board apparently got reset because I was observing my “42” LED blinking pattern. That got me thinking — because the serial connection is also used for programming the board, maybe it interpreted my data incorrectly. This page explains how to disable said auto-reset by hardware modifications but surely it should be possible without that? And it is. Apparently when my program opens the COM port the connection parameters have some weird default values not inherited from the device settings in the control panel. After setting those setting everything started to work fine. So, here’s the Windows code.

Windows code Show

So, how about performance? The maximum serial speed I managed to get is 250000 baud. If I went over that the connection started to get unreliable or just broke. Buffer size had a big impact and the maximum value I could squeeze was 400 bytes (must be a multiple of 16, the AES128 block size). With these settings I was getting 5.66 kB/s encryption speed and 4.82 kB/s decryption (the difference is typical for AES). It seems I won’t be using this to encrypt my disk or any big files. 🙂 Still, not bad for a tiny 8-bit chip and the code is optimized for size, not speed.

All in all I’m happy with the results. Next project: create a Windows Credential Provider using Arduino? 😉 Or just more experiments with a more powerful chip (Due, a Cortex M3 ARM perhaps?).

~ by omeg on May 10, 2015.

C/C++, code, hardware

Leave a Reply