# Assembly Language Programming

## November 12, 2013

### GOST Encryption 28147-89 ECB

The GOST cipher was developed by the Soviet government in the 1970’s and is still used by the Russian government to this day.

An English translation of the GOST 28147-89 standard can be found here:

The Russian version can be found here:

It contains some useful diagrams that are not shown in the English version.

## GOST 28147-89 Specification (ECB – Simple Replacement)

 ```- A 256 bit key is used to encrypt data in blocks of 64 bits at a time. - There are 32 rounds. - The 32 bit little endian registers X0 X1 X2 X3 X4 X5 X6 X7 store the key. - The 32 bit little endian registers N1 N2 store the 64 bit input block. - The s-box is defined as K = K8 K7 K6 K5 K4 K3 K2 K1. - CM1, CM2 and R are 32 bit little endian registers. - Pseudo code for the encryption is then: for(round=0;round<32;round++) { CM1 = N1 + X(round); R = K(CM1); R = ROTL(R,11); CM2 = R xor N2; if(round==31) break; N2 = N1; N1 = CM2; } N2 = CM2; - For round = 0-7, 8-15, 16-23 the order of X(round) is X0 X1 X2 X3 X4 X5 X6 X7. - For round = 24-31 the order of X(round) is X7 X6 X5 X4 X3 X2 X1 X0. ```

## The S-Box

The S-box K consists of the 8 substitution points K8 K7 K6 K5 K4 K3 K2 K1.

Each Ki is a lookup table of size 16 containing a permutation of the 4-bit values 0-15. The 4-bit input to each Ki is used as the index to an entry in the table. The 4-bit value at that location is substituted for the 4-bit input and becomes the output.

A 32 bit little endian input value (CM1 in the above pseudo code) is put through the S-box K as follows. The upper 4 bits go through K8, the next 4 bits go through K7, and so on, until the lowest 4 bits go through K1.

Two S-boxes are built into the program below:

(1) The first 8 rows of the DES S-boxes:

 ``` K8 = 14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7 K7 = 15 1 8 14 6 11 3 4 9 7 2 13 12 0 5 10 K6 = 10 0 9 14 6 3 15 5 1 13 12 7 11 4 2 8 K5 = 7 13 14 3 0 6 9 10 1 2 8 5 11 12 4 15 K4 = 2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9 K3 = 12 1 10 15 9 2 6 8 0 13 3 4 14 7 5 11 K2 = 4 11 2 14 15 0 8 13 3 12 9 7 5 10 6 1 K1 = 13 2 8 4 6 15 11 1 10 9 3 14 5 0 12 7 ```

(2) The S-box given in GOST 34.11-94: http://tools.ietf.org/html/rfc5831

 ``` 8 7 6 5 4 3 2 1 0 1 D 4 6 7 5 E 4 1 F B B C D 8 B A 2 D 4 A 7 A 1 4 9 3 0 1 0 1 1 D C 2 4 5 3 7 5 0 A 6 D 5 7 F 2 F 8 3 D 8 6 A 5 1 D 9 4 F 0 7 4 9 D 8 F 2 A E 8 9 0 3 4 E E 2 6 9 2 A 6 A 4 F 3 B 10 3 E 8 9 6 C 8 1 11 E 7 5 E C 7 1 C 12 6 6 9 0 B 6 0 7 13 B 8 C 3 2 0 7 F 14 8 2 F B 5 9 5 5 15 C C E 2 3 B 9 3 ```

Column 8 gives K8, column 7 gives K7, and so on.

## Bit String To Little Endian

GOST 28147-89 specifies how the bit strings containing the 256 bit Key and 64 bit input block are converted to little endian values. First the bit strings are divided up into 32 bit words. Then the order of bits within each 32 bit word is reversed.

As far as the key is concerned, the first 32 bits become X0, the next 32 bits become X1, and so on.

For the 64 bit input block, the first 32 bits become N1 and the last 32 bits become N2.

## The Program

The program is written in Javascript. The input and output values are 32 bit little endian values.

– The key is entered as the little endian words X0 X1 X2 X3 X4 X5 X6 X7.

– The input is entered as the little endian words N1 and N2.

The program also enables the user to convert bit strings to little endian values, either by reversing the order of bits within each 32 bit word, or by reversing the order of bytes within each 32 bit word.

## Test Vectors

(1) The first test vector comes from:

 ```Key: 75 71 31 34 B6 0F EC 45 A6 07 BB 83 AA 37 46 AF 4F F9 9D A6 D1 B5 3B 5B 1B 40 2A 1B AA 03 0D 1B Input: 11 22 33 44 55 66 77 88 Output: 03 25 1E 14 F9 D2 8A CB S-Box: GOST 34.11-94 ```

The bytes in these test vectors are in byte array order. Converting to little endian numbers as required by my program gives:

 ```Key: 34 31 71 75 45 ec 0f b6 83 bb 07 a6 af 46 37 aa a6 9d f9 4f 5b 3b b5 d1 1b 2a 40 1b 1b 0d 03 aa Input: 44 33 22 11 88 77 66 55 Output: 14 1e 25 03 cb 8a d2 f9 ```

(2) The second test vector comes from (in the source code download):

In little endian byte order:

 ```Key: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Input: 00 00 00 00 00 00 00 00 Output: e7 2b 17 d7 02 f1 22 c0 S-Box: DES ```

## The HTML/Javascript Code

To grab this code, select with the mouse, and copy and paste into a text file.

 ``` GOST Encryption

GOST 28147-89 Encryption - ECB Mode

Enter the 256 bit key X0 .. X7 and 64 bit input block N1 N2 as hexadecimal numbers.

Current S-Box = None.

Bit String To Little Endian

Enter a bit string as a hexadecimal number. The bit string is broken up into 32 bit words and converted to little endian.

```