Lably
Skip to content

GOST (hash function)

Russian cryptographic hash function

  • 5 min read
  • 3 chapters

Explore on Lably

ToursExplore Lably’s 3D tours

This function must not be confused with a different Streebog hash function, which is defined in the new revision of the standard GOST R 34.11-2012.

The GOST hash function is based on the GOST block cipher.

Algorithm

GOST processes a variable-length message into a fixed-length output of 256 bits. The input message is broken up into chunks of 256-bit blocks (eight 32-bit little endian integers); the message is padded by appending as many zeros to it as are required to bring the length of the message up to 256 bits. The remaining bits are filled up with a 256-bit integer arithmetic sum of all previously hashed blocks and then a 256-bit integer representing the length of the original message, in bits.

Basic notation

The algorithm descriptions uses the following notation:

  • f0gj — j-bit block filled with zeroes.
  • jMj — length of the M block in bits modulo 2256.
  • k — concatenation of two blocks.
  • + — arithmetic sum of two blocks modulo 2256.
  • ⊕ — logical xor of two blocks.

Further we consider that the little-order bit is located at the left of a block, and the high-order bit at the right.

Description

The input message M is split into 256-bit blocks mn,mn−1,mn−2,…,m1. In the case the last block mn contains less than 256 bits, it is prepended left by zero bits to achieve the desired length.

Each block is processed by the step hash function Hout=f(Hin,m), where Hout, Hin, m are a 256-bit blocks.

Each message block, starting the first one, is processed by the step hash function f, to calculate intermediate hash value

Hi+1=f(Hi,mi)

The H1 value can be arbitrary chosen, and usually is 0256.

After Hn+1 is calculated, the final hash value is obtained in the following way

  • Hn+2=f(Hn+1,L), where L — is the length of the message M in bits modulo 2256
  • h=f(Hn+2,K), where K — is 256-bit control sum of M: m1+m2+m3+…+mn

The h is the desired value of the hash function of the message M.

So, the algorithm works as follows.

  1. Initialization:
    1. h:=initial — Initial 256-bit value of the hash function, determined by user.
    2. Σ:=0 — Control sum
    3. L:=0 — Message length
  2. Compression function of internal iterations: for i = 1 … n — 1 do the following (while |M|>256):
    1. h:=f(h,mi) – apply step hash function
    2. L:=L+256 – recalculate message length
    3. Σ:=Σ+mi – calculate control sum
  3. Compression function of final iteration:
    1. L:=L+jmnj – calculate the full message length in bits
    2. mn:=0256−jmnjkmn – pad the last message with zeroes
    3. Σ:=Σ+mn – update control sum
    4. h:=f(h,mn) – process the last message block
    5. h:=f(h,L) – MD – strengthen up by hashing message length
    6. h:=f(h,Σ) – hash control sum
  4. The output value is h.

Step hash function

The step hash function f maps two 256-bit blocks into one: Hout=f(Hin,m).

It consist of three parts:

  • Generating of keys K1,K2,K3,K4
  • Enciphering transformation Hin using keys K1,K2,K3,K4
  • Shuffle transformation

Key generation

The keys generating algorithm uses:

  • Two transformations of 256-bit blocks:
    • Transformation A(Y)=A(y4ky3ky2ky1)=(y1⊕y2)ky4ky3ky2, where y1,y2,y3,y4 are 64-bit sub-blocks of Y.
    • Transformation P(Y)=P(y32ky31k…ky1)=yφ(32)kyφ(31)k…kyφ(1), where φ(i+1+4(k−1))=8i+k,i=0,…,3,k=1,…,8, and y32,y31,…,y1 are 8-bit sub-blocks of Y.
  • Three constants:
    • C2 = 0
    • C3 = 0xff00ffff000000ffff0000ff00ffff0000ff00ff00ff00ffff00ff00ff00ff00
    • C4 = 0

The algorithm:

  1. U:=Hin,V:=m,W:=U⊕V,K1=P(W)
  2. For j = 2, 3, 4 do the following:
    U:=A(U)⊕Cj,V:=A(A(V)),W:=U⊕V,Kj=P(W)

Enciphering transformation

After the keys generation, the enciphering of Hin is done using GOST 28147-89 in the mode of simple substitution on keys K1,K2,K3,K4. Let's denote the enciphering transformation as E (enciphering 64-bit data using 256-bit key). For enciphering, the Hin is split into four 64-bit blocks: Hin=h4kh3kh2kh1, and each of these blocks is enciphered as:

  • s1=E(h1,K1)
  • s2=E(h2,K2)
  • s3=E(h3,K3)
  • s4=E(h4,K4)

After this, the result blocks are concatenated into one 256-bit block: S=s4ks3ks2ks1.

Shuffle transformation

On the last step, the shuffle transformation is applied to Hin, S and m using a linear-feedback shift register. In the result, the intermediate hash value Hout is obtained.

First we define the ψ function, doing LFSR on a 256-bit block:

ψ(Y)=ψ(y16ky15k…ky2ky1)=(y1⊕y2⊕y3⊕y4⊕y13⊕y16)ky16ky15k…ky3ky2,

where y16,y15,…,y2,y1 are 16-bit sub-blocks of the Y.

The shuffle transformation is Hout=ψ61(Hin⊕ψ(m⊕ψ12(S))), where ψi denotes an i-th power of the ψ function.

Initial values

There are two commonly used sets of initial parameters for GOST R 34.11 94. The starting vector for both the sets is

H1 = 0x00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000.

Although the GOST R 34.11 94 standard itself doesn't specify the algorithm initial value H1 and S-box of the enciphering transformation E, but uses the following "test parameters" in the samples sections.

"Test parameters" S-box

RFC 5831 specifies only these parameters, but RFC 4357 names them as "test parameters" and does not recommend them for use in production applications.

CryptoPro S-box

The CryptoPro S-box comes from "production ready" parameter set developed by CryptoPro company, it is also specified as part of RFC 4357, section 11.2.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication