2013-02-12

Google Authenticator watch

TIMESQUARE is an LED matrix watch, sold by Adafruit in kit form. It has an ATmega328 chip inside, which is interesting, because it means it is programmable using the Arduino IDE. That opens up several possibilities. You could design your own watch faces or you could extend lunchtime by an extra 12 minutes every day. I thought it would be cool to turn my watch into a 2-step authentication token for my Google account.

If you enabled 2-step authentication on your Google account (which you should definitely do, BTW), you know the way it works is you have an app on your phone called Google Authenticator that displays six-digit codes that change every 30 seconds. When you log in, you enter the currently displayed code in addition to your password. That way when bad people get a hold of your password, they still cannot use your account, because they don't have the code. (BTW, this isn't specific to Google accounts, the authentication scheme is described in RFC 6238 and is used by other services like Dropbox and LastPass. You can even set it up so that your own servers ask you for a code when you ssh into them.)

Where do the codes come from? When you set up 2-step authentication, the service (for example Google) gives you a secret that is then stored in the app (it's usually done through a QR code so that you don't have to type in long sequences of numbers). The app generates codes by taking the secret together with current time and performing some cryptographic operations on them (mostly the SHA-1 hash function). There's no reason we can't do the same on our TIMESQUARE watch and indeed someone has already implemented the algorithm on Arduino. I took that implementation and put it as another display mode on the watch, showing the six-digit code using the marquee effect normally used to display time.



If you want to try it on your watch, you can download the code here. You will need to install Adafruit's original code for the watch and their GFX library in your Arduino IDE. You're also going to need a way to program the watch from your computer. I used Adafruit's FTDI Friend. They have a tutorial on how to do this.

The authentication secret is currently hardcoded in the TOTP.ino file. I should probably make it so that it's possible to set the secret using the buttons on the watch, but right now you're going to have to set it manually in the hmacKey1 variable. Google gives you the secret in Base32-encoded form, but you can easily convert it to an array of bytes using the following Python code (remove the spaces from the Base32 string Google gives you):
import base64
print ', '.join([hex(ord(i)) for i in base64.b32decode('JBSWY3DPEHPK3PXP')])
Be aware that compiling code using the Arduino IDE can leave some intermediate files in the /tmp directory (or its equivalent on other operating systems). You should clean them up to remove traces of the secret from your computer.

2013-02-11

Laser tripwire

Somebody call Catherine Zeta-Jones, because I have a laser tripwire.



I made it using an Arduino, a laser diode, a photocell (light dependent resistor) and a piezo buzzer. The laser and the detector are in the same place, so it requires a mirror to bounce the laser beam off (this way I only need power in one place). Here's what it looks like on a breadboard:



The laser diode is just connected to 5V and ground, so it's always on. The piezo buzzer is connected to digital pin 3 on the Arduino and ground so that we can sound the alarm when the laser beam is broken. The photocell is connected in a voltage divider configuration. One leg of the photocell is connected to 5V, the other to ground through a resistor and to analog pin 0 on the Arduino. The way a photocell works is that it changes its resistance depending on light intensity. When the laser beam hits it, the resistance will be lower than when it doesn't. With the voltage divider, we can read a value on the Arduino's analog pin that will depend on the photocell's resistance and thus detect if the laser beam was broken.

The output voltage in the voltage divider depends on the values of the resistors in the following manner:

Vout = Rfixed/(Rphotocell+Rfixed)*Vin

Depending on the photocell's resistance range, we choose the value of the fixed resistor so that we can have a reasonable threshold value for the output voltage.

Here's the sketch that's running on the Arduino.
const int PHOTOCELL_PIN = A0;
const int BUZZER_PIN = 3;
// voltage readings are in 0-1023 range
const int THRESHOLD = 500;

void setup() {
  pinMode(PHOTOCELL_PIN, INPUT);
  Serial.begin(9600);
}

long alarmEndTime = 0;

void loop() {
  int level = analogRead(PHOTOCELL_PIN);
  Serial.println(level);
  long time = millis();
  if (time < alarmEndTime) {
    long timeLeft = alarmEndTime - time;
    if (timeLeft % 1000 > 300) {
      tone(BUZZER_PIN, 4000);
    } else {
      noTone(BUZZER_PIN);
    }
  } else {
    noTone(BUZZER_PIN);
    if (level < THRESHOLD) {
      alarmEndTime = time + 3000;
    }
  }
}

Blinkenlights

I think we all agree that every computer is better with blinking lights. In an effort to overcome my fear of electronics, I have recently acquired an Arduino and one of my first projects was a CPU load indicator for my computer:



It was of course inspired by the blinkenlights on BeBoxes and the way it works is very simple. I got a 10 segment LED bar graph and two six-pin 330 ohm resistor networks (they're just five resistors in a single package with one leg of each resistor connected to the sixth common pin). I connected the positive legs of the LEDs to ten digital pins on the Arduino and I connected the negative legs to ground through the resistors. Here's what it looked like during testing:



Then I needed a way to tell the Arduino which LEDs to turn on. An obvious choice was to communicate over USB (which will also provide power). Here's the sketch that's running on the Arduino:
const int FIRST_PIN = 3;

void setup() {
  Serial.begin(115200);
  for (int i=0; i<10; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

void loop() {
  if (!Serial.available()) {
    return;
  }
  int cpu_load = Serial.read();
  int n = (cpu_load+5)/10;
  for (int i=0; i<10; i++) {
    digitalWrite(FIRST_PIN + i, i<n ? HIGH : LOW);
  }
}
And here's the Python program that's running on the computer:
import serial
import psutil

ser = serial.Serial('/dev/ttyACM0')
while True:
    cpu_load = psutil.cpu_percent(interval=0.1)
    ser.write(chr(int(cpu_load)))
As you can see it sends a byte with the CPU load value (0-100) ten times per second. I only tested it under Linux, but the psutil module should also work on Windows and OS X. It can also give you per-CPU core percentages, but that would require more LEDs and some other way to drive them when we run out of I/O pins on the Arduino.