# Assembly Language Programming

## July 17, 2013

### Serpent-1 Encryption Algorithm

The Serpent-1 Encryption Algorithm was a candidate for the AES and came second to the eventual winner Rijndael.

The homepage for Serpent is here:

Serpent uses a 256 bit key to encrypt 128 bit blocks. The 128 bit input block is encrypted in 32 rounds.

## Related Post

The x86 assembly language version can be found here: FASM – Serpent-1 Encryption

## The Key Schedule

The 256 bit input key is used to generate 33 x 128 bit sub-keys. The first 32 sub-keys are used in the 32 rounds of the encryption process. The 33rd sub-key is used in the final round.

The following pseudo-code shows the sub-key generation process. An array of 140 32-bit words is created. The first 8 elements are filled with the original 256 bit input key. Then the remaining 132 words are generated as follows:

 ```for(i = 8 ; i < 140 ; i++) { Key[i] = Key[i-8] xor Key[i-5] xor Key[i-3] xor Key[i-1] xor 0x9e3779b9 xor i; Key[i] = ROTL(Key[i],11); } ```

The 33 sub-keys (of 128 bits each) are then put through the s-boxes as follows:

 ```s_index = 3; for(i = 8; i < 136 ; i += 4) { Key[i],Key[i+1],Key[i+2],Key[i+3] = S-Box[s_index](Key[i],Key[i+1],Key[i+2],Key[i+3]); s_index--; if(s_index < 0) s_index = 7; } Key[i],Key[i+1],Key[i+2],Key[i+3] = S-Box[s_index](Key[i],Key[i+1],Key[i+2],Key[i+3]); ```

## Encryption

The 128 bit input block is encrypted in 32 rounds as follows:

 ```C = input block; for(round = 0 ; round < 32 ; round++) { C = C xor SubKey[round]; C = S-Box[round mod 8](C); if(round==31) break; C = LT(C); } C = C xor SubKey[32]; ```

C is the 128 bit cipher text. SubKey[round] is one of the 33 x 128 bit sub-keys, indexed from 0 to 32. LT is the linear transform as described below.

## The Linear Transform

The linear transform is applied to a 128 bit block. The block is divided into 4 x 32 bit words as follows: |w0|w1|w2|w3|

 ``` w0 = RotLeft(w0,13); w2 = RotLeft(w2,3); w1 = w1 xor w0 xor w2; w3 = w3 xor w2 xor ShiftLeft(w0,3); w1 = RotLeft(w1,1); w3 = RotLeft(w3,7); w0 = w0 xor w1 xor w3; w2 = w2 xor w3 xor ShiftLeft(w1,7); w0 = RotLeft(w0,5); w2 = RotLeft(w2,22); ```

## The S-Boxes

Serpent uses 8 s-boxes. Each s-box contains 16 elements. The input and output to a s-box is a 4 bit value, as follows:

n’ = s-box[n], (n = 0-15)

My program implements the bitslice mode of the serpent cipher algorithm. The diagram below shows how the s-boxes are implemented in bitslice mode. The 128 bit input block is divided up into 4 x 32 bit words: |w0|w1|w2|w3|. These are then arranged as shown in the diagram. The substitution is then applied by taking 32 slices of 4 bits each and putting then through 32 copies of the s-box in parallel. Bit 0 is the lsb in the 4 bit value.

## Input Mode

The program has two input/output modes: standard and byte array (NESSIE) mode.

In standard mode, both the input key and plain text are entered as hexadecimal numbers. Internally the input numbers are broken up into 32 bit words, which are stored in little endian byte order. The first 8 hex digits in the number are word 0, the next 8 digits are word 1, and so on. The first 2 digits in any given word are the most significant byte in that word.

In byte array (NESSIE) mode, the input/output is still represented as a hexadecimal number but each pair of hex digits correspond directly to a byte in an internal byte array. Let the internal array be called input_array[]. Then the first 2 digits in the input correspond to the byte at input_array[0], the next 2 digits correspond to the byte at input_array[1], and so on. Of course the input bytes are still organised internally into 32 bit words, with input_array[0] – input_array[3] being the first word, input_array[4] – input_array[7] being the second word, and so on.

Test vectors for Serpent-1 with 256 bit keys are specified in byte array (NESSIE) format here:

## The HTML/Javascript Code

The program encrypts only.

To use the program enter a 256 bit key (64 hex digits) and press [Set Key]. This will display the key schedule. Then enter the 128 bit plain text (32 hex digits) and press [Encrypt]. Press the [I/O Mode] button to toggle the input mode. Note that the program expects a full 256 bit key, it will not pad keys shorter than this in the way specified by the algorithm.

The program displays the result from each round of the 32 rounds of encryption, with the final round being the result (the cipher text).

 ``` Serpent One Encryption

Standard I/O Mode

Key:

Input:

Output:
```