The Hardware Side of Cryptography

6 August 2008

Fast 9600 bps Serial Encryptor based on A5/1 Algorithm

A5/1 is a kind of stream cipher commonly used in GSM networks. This cipher scrambles data from handset to Base Trasceiver Station (BTS). This cipher consists of 3 different LFSR with different tapping and a special clocking method called “Majority Rule”. Majority rule stated that an LFSR will be clocked if middle bit is equal to majority bit and majority bit is defined as a function of all middle bit of three LFSR. Majority bit is set if at least two out of three middle bit is set, otherwise zero.

Here i presented you a way to perform RS232 serial encryption using A5/1 Algorithm. This post is basically an extension of previous post about A5/1 Implementation on PIC16f84. Previous experiment, shown me that a single bit A5/1 output costs about 82 cycles (82 micro seconds) on 4 MHz PIC16F84 microcontroller. In the other hand, RS232 specification said that the bit gap of 9600 bps asynchronous transmission is approximately 104 micro seconds.

Based on those two facts, i was encouraging my self to write another code that implements A5/1 algorithm as a serial encryptor. Since generating 1 bit A5/1 output costs 82 micro seconds and 1 bit gap of 9600 bps transmission is about 104 micro seconds, hence i concluded that A5/1 stream generation can be done while microcontroller is receiving data from RS232 terminal. The concept is basically easy, i just replace bit delay routine by bit generating process, so that 8 bit A5/1 stream is generated when 8 bit data received asynchronously through RS232.

By employing this tricky way, i can reduce the time required to process bit stream. This way also reduce gaps between two bytes. These all characteristics made A5/1 becoming a reliable candidate for RS232 encryptor application.

Now, lets talk about its reality. I coded and compiled this cipher using MPLAB v8.0 and simulated the result using Oshon Soft PIC IDE Simulator. The result was satisfactory. If you wish to apply this in such a real hardware, please use 4MHz PIC16F84 microcontroller. Please make sure that you use RA0 as transmitting bit and RA1 as receiving bit. For additional things such RS232 to TTL converter, there are many way to convert RS232 to TTL or vice versa. You can use any of them as long as it is works.

Screenshot

How to Use: to initialize this encryptor, follow these steps below:

  1. Send 3 bytes frame setting, stare from frame 0 which is MSB.
  2. Send 8 bytes key setting, start from key 0 which is MSB.
  3. Allow 16.63 ms delay for key scheduling
  4. Encryptor is ready to use, please use 9600 bps speed

RELATED STUFF

Source Code | v1.0
PIC16F84 Datasheet
Mid-Range Reference Manual
MPLAB v8.0

Reference:
A5/1 Strem Cipher
- Pedagogical A5/1 Implementation

4 August 2008

Embedding A5/1 GSM Stream Cipher into PIC16F84 Microcontroller

A5/1 was a stream cipher commonly used to encrypt handset to Base Tranceiver Station (BTS) transmission in GSM networks. This cipher is known to pass statistical tests yet it lack of security due to short LFSR usage.

A5/1 consists of 3 LFSR (Linear Feedback Shift Register) called R1, R2 and R3. R1 has 19 bits long, R2 has 22 bits long and R3 has 23 bits long. Each of that LFSR has specific polynomial that represents its tapping terminals. There is also exist another rules called majority rule. This rule stated that an LFSR will be clocked if its middle bit is equal to majority bit, where the majority bit is defined as the sum of all middle bits of all LFSR.

I found that there are two versions of A5/1 implementation exists. Both of them stated different approach of defining R2 and R3. In the other hand, both specification stated the same R1 characteristic. The first one is the one found in Bruce Schneier’s Applied Cryptography book and the other is just the one coded by Marc Briceno, Ian Goldberg, and David Wagner.

Benchmark: (generating 112 bits/14 bytes output)
Key : 12 23 45 67 89 ab cd ef
Frame : 00 01 34
Stream out : 53 4e aa 58 2f e8 15 1a b6 e1 85 5a 72 8c

Version 1.0 Screenshot (deprecated)

Version 2.0 Screenshot (Compatible with Pedagogical A5/1 Implementation)

Here are two versions of them. Please note that first version code is incompatible to Pedagogical C code implementation of A5/1 stream cipher while the second one is fully compatible. Please consider to use the second version if you wish to implement A5/1. Please let me know when you found errors inside my codes. I’ll appreciate you much :mrgreen: .

RELATED STUFF

Download:
Source Code | v1.0 deprecated
Source Code | v2.0
PIC16F84 Datasheet
Mid-Range Reference Manual
MPLAB v8.0

Reference:
A5/1 Strem Cipher
- Pedagogical A5/1 Implementation

Blog at WordPress.com.