CSS411 Cryptography

SHA

Slide deck 14: SHA-1 history and family, 512-bit block processing with the F/W/K machinery, SHA-1 versus MD5, and the SHA-512 round function.

31 min read

SHA history and familySearch on YouTube

OriginsSearch on YouTube

  • NIST and NSA designed SHA in 1993.
  • In 1995, it was revised as SHA-1.
  • It is the US standard for use with the DSA signature scheme.
  • The standard is FIPS 180-1 1995, also Internet RFC 3174.
  • The algorithm is SHA. The standard is SHS.
  • It is based on the design of MD5, with key differences.
  • It produces 160-bit hash values.
  • Results from 2005 on the security of SHA-1 raised concerns about its use in future applications.

SHA familySearch on YouTube

VersionYearWhat to recall
SHA-01993First version, later withdrawn due to security flaws
SHA-11995160-bit (20-byte) hash; widely used in SSL/TLS, Git, digital signatures; broken: collision attacks are feasible, not secure now
SHA-22001SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256; still secure; Merkle-Damgard construction, 512 or 1024 bit blocks; used in SSL/TLS, certificates, Bitcoin, blockchain
SHA-32015Keccak algorithm, designed differently from SHA-2, resistant even if SHA-2 is broken; SHA3-224/256/384/512, plus extendable-output functions SHAKE128 and SHAKE256

The bit length in the name gives the output size.

Family order by year: 0-1-2-3 = withdrawn, broken, secure, new. SHA-0 is withdrawn. Collision attacks break SHA-1. SHA-2 is still secure. SHA-3 (Keccak) is a different design.

Revised secure hash standardSearch on YouTube

  • NIST issued the revision FIPS 180-2 in 2002.
  • It adds 3 more versions of SHA: SHA-256, SHA-384, SHA-512. Each has a different digest length in bits.
  • Their design gives security compatible with the increased security of the AES cipher.
  • The structure and detail are similar to SHA-1, so the analysis is similar.
  • The security levels are higher.

SHA-1 algorithmSearch on YouTube

ParametersSearch on YouTube

The "How SHA works" slide gives these values for SHA-1:

ParameterValue
Digest length160 bit
Input text (block)512 bit
Sub-block size32 bit
Total sub-blocks512/32=16512/32 = 16
Number of rounds4
Iterations per round20
Chaining variable5×32=1605 \times 32 = 160 bit
K[t]K[t] constantt=0t = 0 to 7979

The slide gives the output as "four 32-bit blocks". This is an error. The buffer (A,B,C,D,E)(A, B, C, D, E) has five 32-bit words, and 5×32=1605 \times 32 = 160 bits.

All sizes are multiples of the 32-bit word:

512=16×32,160=5×32,80=4×20512 = 16 \times 32, \qquad 160 = 5 \times 32, \qquad 80 = 4 \times 20

One 512-bit block expands to 80 words. The 80 words go through 4 rounds of 20 steps and update a 5-word chaining buffer.

Overview stepsSearch on YouTube

  1. Padding: pad the message until its length is 64 bits short of a multiple of 512.
  2. Append length: append a 64-bit value that gives the length of the original message.
  3. Divide the input into 512-bit blocks.
  4. Initialise the CV, a 5-word (160-bit) buffer (A,B,C,D,E)(A, B, C, D, E).
  5. Process the blocks in 16-word (512-bit) chunks:
    • Copy the CV into a single register. This register holds the intermediate and the final results.
    • Divide the current 512-bit block into 16 sub-blocks of 32 bits each.
    • Do 4 rounds of 20 steps each on the message block and the buffer.
    • Expand the 16 words into 80 words (20×420 \times 4) by mixing and shifting. Use K[t]K[t] for t=0t = 0 to 7979.
    • Add the output to the input to get the new buffer value.
  6. Output: the hash value is the final buffer value.

Initial bufferSearch on YouTube

The slide gives the initial values byte by byte:

WordSlide valueFIPS 180 word value
A01 23 45 6767452301
B89 AB CD EFEFCDAB89
CFE DC BA 9898BADCFE
D76 54 32 1010325476
EC3 D2 E1 F0C3D2E1F0

The slide lists the bytes of A to D from low-order to high-order. The standard writes each word as a big-endian hex number.

SHA-1 compression functionSearch on YouTube

Block processingSearch on YouTube

Figure 12.5 shows how SHA-1 processes one 512-bit block YqY_q:

RoundFunctionWords usedSteps
1f1f_1KK, W[0...19]W[0...19]20
2f2f_2KK, W[20...39]W[20...39]20
3f3f_3KK, W[40...59]W[40...59]20
4f4f_4KK, W[60...79]W[60...79]20

The chaining value CVqCV_q comes in as 160 bits (five 32-bit words A,B,C,D,EA, B, C, D, E). After round 4, five adders add each word to the matching word of CVqCV_q. The result is CVq+1CV_{q+1} (160 bits). Addition is mod 2322^{32}.

CVq​160 bitsMessage block Yq​ (512 bits) → W0​ … W79​Round 1f1​20 stepsRound 2f2​20 stepsRound 3f3​20 stepsRound 4f4​20 steps+ (×5)mod 232​CVq​ also goes to the addersCVq+1​Round t uses words W20(t−1)​ to W20t−1​ of the schedule.
SHA-1 compression: four rounds of 20 steps update the buffer A to E, then five adders add the input chaining value.

Step functionSearch on YouTube

The slide gives the step as:

ABCDE=F[t]+E+S5(A)+W[t]+K[t]ABCDE = F[t] + E + S^5(A) + W[t] + K[t]

The slide says that the buffer shifts "right by 1 bit" for the next step. The figure shows a shift of one word position, not one bit. In full notation, each of the 20 steps in a round replaces the 5 buffer words thus:

(A,B,C,D,E)←(E+f(t,B,C,D)+(A≪5)+Wt+Kt,  A,  (B≪30),  C,  D)(A, B, C, D, E) \leftarrow (E + f(t, B, C, D) + (A \ll 5) + W_t + K_t,\; A,\; (B \ll 30),\; C,\; D)

TermMeaning
A,B,C,D,EA, B, C, D, EThe 5 words of the buffer
ttStep number, 0≤t≤790 \le t \le 79
f(t,B,C,D)f(t, B, C, D)Non-linear function for the round
WtW_tWord derived from the message block
KtK_tConstant value
St(X)S^t(X)Circular left shift of the 32-bit sub-block XX by tt bits

How to read the update:

  • The new AA is the sum E+f(t,B,C,D)+S5(A)+Wt+KtE + f(t,B,C,D) + S^5(A) + W_t + K_t.
  • The old AA moves to BB.
  • The old BB rotates left 30 bits and moves to CC.
  • The old CC moves to DD, and the old DD moves to EE.

Round functionsSearch on YouTube

RoundStepsFunction f(t,b,c,d)f(t, b, c, d)
Round 1t=0..19t = 0..19(b AND c) OR ((NOT b) AND d)(b \text{ AND } c) \text{ OR } ((\text{NOT } b) \text{ AND } d), same as MD5
Round 2t=20..39t = 20..39b XOR c XOR db \text{ XOR } c \text{ XOR } d
Round 3t=40..59t = 40..59(b AND c) OR (b AND d) OR (c AND d)(b \text{ AND } c) \text{ OR } (b \text{ AND } d) \text{ OR } (c \text{ AND } d)
Round 4t=60..79t = 60..79b XOR c XOR db \text{ XOR } c \text{ XOR } d

Round 1 is a selection: if bb then cc, else dd. Rounds 2 and 4 give the parity. Round 3 gives the majority, which is true when two or three of b,c,db, c, d are true.

Round functions in order: If, Parity, Majority, Parity (IPMP).

ConstantsSearch on YouTube

The slides define K[t]K[t] as a constant for each step, t=0t = 0 to 7979. The standard (FIPS 180-1) uses one value for each round:

Roundtt rangeKtK_t (hex)Source
10..190..195A8279995A827999floor(230×2)(2^{30} \times \sqrt{2})
220..3920..396ED9EBA16ED9EBA1floor(230×3)(2^{30} \times \sqrt{3})
340..5940..598F1BBCDC8F1BBCDCfloor(230×5)(2^{30} \times \sqrt{5})
460..7960..79CA62C1D6CA62C1D6floor(230×10)(2^{30} \times \sqrt{10})

Message scheduleSearch on YouTube

Figure 12.7 shows the creation of the 80-word input WtW_t. The expansion adds redundancy and interdependence among message blocks.

  • Words W0…W15W_0 \dots W_{15} are the 16 words of the 512-bit block YqY_q.
  • For t=16t = 16 to 7979, four earlier words go into an XOR, and S1S^1 rotates the result 1 bit to the left:

Wt=S1(Wt−16⊕Wt−14⊕Wt−8⊕Wt−3)W_t = S^1\left(W_{t-16} \oplus W_{t-14} \oplus W_{t-8} \oplus W_{t-3}\right)

Word builtInputs (figure)
W16W_{16}W0⊕W2⊕W8⊕W13W_0 \oplus W_2 \oplus W_8 \oplus W_{13}, then S1S^1
W79W_{79}W63⊕W65⊕W71⊕W76W_{63} \oplus W_{65} \oplus W_{71} \oplus W_{76}, then S1S^1

Each new word depends on many earlier words. Thus a change in one message word spreads through the schedule.

ComparisonsSearch on YouTube

SHA-1 versus MD5Search on YouTube

PointSHA-1MD5
Brute-force attackHarder: 160-bit digestEasier: 128-bit digest
Known attacksNot vulnerable to any known attacks (compared to MD4/5), at the time of the slideMD4/5 have known attacks
SpeedA little slower (80 steps)Faster (64 steps)
DesignSimple and compactSimple and compact
EndiannessOptimised for big-endian CPUs (SUN)Optimised for little-endian CPUs (PC)

SHA-1 has 32 more digest bits than MD5, but it needs 16 more steps. MD5 is faster. SHA-1 is stronger.

Table 12.3Search on YouTube

PropertySHA-1SHA-256SHA-384SHA-512
Message digest size160256384512
Message size<264< 2^{64}<264< 2^{64}<2128< 2^{128}<2128< 2^{128}
Block size51251210241024
Word size32326464
Number of steps80808080
Security80128192256

Notes from the table: all sizes are in bits. "Security" means that a birthday attack on a message digest of size nn produces a collision with a work factor of approximately 2n/22^{n/2}.

SHA-1 versus SHA-512Search on YouTube

StageSHA-1SHA-512
Block size512 bits1024 bits
Length field64 bits128 bits
BufferA,B,C,D,EA, B, C, D, E (160 bits)A,B,C,D,E,F,G,HA, B, C, D, E, F, G, H (512 bits)
Word size32 bits64 bits
Steps4 rounds of 20 steps80 rounds
Schedule16 words to 80, S1S^1 of a four-way XOR16 words to 80, with σ0\sigma_0 and σ1\sigma_1
Additionmod 2322^{32}mod 2642^{64}
ConstantsKtK_t, one for each roundKtK_t from cube roots of the first 80 primes

SHA-512Search on YouTube

Setup stepsSearch on YouTube

  1. Padding.
  2. Append length.
  3. Divide the input into 1024-bit blocks. The padded message is the message (LL bits), then a 1 and zeros (100...0), then a 128-bit length field. The total is N×1024N \times 1024 bits.
  4. Initialise the chaining variable: 8×64=5128 \times 64 = 512 bits, in eight 64-bit words A,B,C,D,E,F,G,HA, B, C, D, E, F, G, H.
  5. Process the blocks. This is the heart of the algorithm:
    • Process the message in 1024-bit blocks.
    • Each block goes through 80 rounds that update the 512-bit buffer (the temporary chaining variables).
    • Each round uses a 64-bit value WtW_t from the current message block.
    • Each round uses a round constant KtK_t based on the cube roots of the first 80 prime numbers.

The chaining is as follows. IV=H0IV = H_0 and block M1M_1 go into function FF. Word-by-word addition mod 2642^{64} of H0H_0 and the output of FF gives H1H_1. This continues to HNH_N, which is the hash code.

Round functionSearch on YouTube

The slide describes each of the 80 steps for one block. The slide says "512-bit block", but the block is 1024 bits and the buffer is 512 bits. Each round uses these equations:

T1=h+Ch(e,f,g)+(Σ1512e)+Wt+KtT_1 = h + \text{Ch}(e, f, g) + \left(\Sigma_1^{512} e\right) + W_t + K_t

T2=(Σ0512a)+Maj(a,b,c)T_2 = \left(\Sigma_0^{512} a\right) + \text{Maj}(a, b, c)

a=T1+T2,b=a,c=b,d=c,e=d+T1,f=e,g=f,h=ga = T_1 + T_2, \quad b = a, \quad c = b, \quad d = c, \quad e = d + T_1, \quad f = e, \quad g = f, \quad h = g

TermDefinition
ttStep (round) number, 0≤t≤790 \le t \le 79
Ch(e,f,g)\text{Ch}(e, f, g)(e AND f) XOR ((NOT e) AND g)(e \text{ AND } f) \text{ XOR } ((\text{NOT } e) \text{ AND } g), the conditional function: if ee then ff else gg
Maj(a,b,c)\text{Maj}(a,b,c)(a AND b) XOR (a AND c) XOR (b AND c)(a \text{ AND } b) \text{ XOR } (a \text{ AND } c) \text{ XOR } (b \text{ AND } c), true only if a majority (two or three) of the arguments are true
Σ0512(a)\Sigma_0^{512}(a)ROTR28(a) XOR ROTR34(a) XOR ROTR39(a)\text{ROTR}^{28}(a) \text{ XOR } \text{ROTR}^{34}(a) \text{ XOR } \text{ROTR}^{39}(a)
Σ1512(e)\Sigma_1^{512}(e)ROTR14(e) XOR ROTR18(e) XOR ROTR41(e)\text{ROTR}^{14}(e) \text{ XOR } \text{ROTR}^{18}(e) \text{ XOR } \text{ROTR}^{41}(e)
ROTRn(x)\text{ROTR}^n(x)Circular right shift (rotation) of the 64-bit argument xx by nn bits
WtW_tA 64-bit word derived from the current input block
KtK_tA 64-bit additive constant
++Addition modulo 2642^{64}

T1T_1 collects hh, Ch, Σ1(e)\Sigma_1(e), WtW_t and KtK_t. T2T_2 collects Σ0(a)\Sigma_0(a) and Maj. The new aa gets both: a=T1+T2a = T_1 + T_2. The new ee gets only T1T_1: e=d+T1e = d + T_1. All other words move down one register.

Message scheduleSearch on YouTube

The schedule expands the 16 words of block MiM_i (64 bits each) into 80 words. For t=16t = 16 to 7979, the figure combines four earlier words:

Wt=σ1(Wt−2)+Wt−7+σ0(Wt−15)+Wt−16W_t = \sigma_1(W_{t-2}) + W_{t-7} + \sigma_0(W_{t-15}) + W_{t-16}

For example, W16W_{16} uses W0W_0, σ0(W1)\sigma_0(W_1), W9W_9 and σ1(W14)\sigma_1(W_{14}). The figure labels the combining box "XOR". The standard (FIPS 180) uses addition mod 2642^{64}.

σ0(x)=ROTR1(x)⊕ROTR8(x)⊕SHR7(x)\sigma_0(x) = \text{ROTR}^{1}(x) \oplus \text{ROTR}^{8}(x) \oplus \text{SHR}^{7}(x)

σ1(x)=ROTR19(x)⊕ROTR61(x)⊕SHR6(x)\sigma_1(x) = \text{ROTR}^{19}(x) \oplus \text{ROTR}^{61}(x) \oplus \text{SHR}^{6}(x)

SymbolMeaning
ROTRn\text{ROTR}^nRotate right by nn bits (within 64 bits)
SHRn\text{SHR}^nLogical right shift by nn bits (zero-fill)
⊕\oplusBitwise XOR

The slide prints the σ1\sigma_1 line as ROTR19\text{ROTR}^{1}9 and ROTR61\text{ROTR}^{6}1. The correct exponents are 1919 and 6161.

Quick self-testSearch on YouTube

Who designed SHA, and what are the standards and revisions?

NIST and NSA designed SHA in 1993. It was revised in 1995 as SHA-1. The standard is FIPS 180-1 (1995), also Internet RFC 3174. The algorithm is SHA, and the standard is SHS. FIPS 180-2 (2002) adds SHA-256, SHA-384 and SHA-512 for compatibility with AES security.

Summarise the four SHA family generations.

SHA-0 (1993): first version, withdrawn due to security flaws. SHA-1 (1995): 160-bit hash, used in SSL/TLS, Git and signatures, now broken by collision attacks. SHA-2 (2001): SHA-224/256/384/512, SHA-512/224 and SHA-512/256, Merkle-Damgard, still secure. SHA-3 (2015): Keccak, a different design, includes SHAKE128 and SHAKE256.

Give the SHA-1 parameters from the "How SHA works" slide.

Digest length 160 bit. Input text 512 bit. Sub-block size 32 bit, so 512/32=16512/32 = 16 sub-blocks. 4 rounds with 20 iterations each. Chaining variable 5×32=1605 \times 32 = 160 bit. K[t]K[t] constant for t=0t = 0 to 7979.

List the six SHA-1 overview steps and the buffer initial values.

Pad to 64 bits short of a multiple of 512. Append the 64-bit message length. Divide into 512-bit blocks. Initialise the buffer: A = 01234567, B = 89ABCDEF, C = FEDCBA98, D = 76543210, E = C3D2E1F0. Process each block in 4 rounds of 20 steps and add the output to the input. Output the final buffer.

Write the SHA-1 buffer update equation and name its terms.

(A,B,C,D,E)←(E+f(t,B,C,D)+(A≪5)+Wt+Kt,  A,  (B≪30),  C,  D)(A,B,C,D,E) \leftarrow (E + f(t,B,C,D) + (A \ll 5) + W_t + K_t,\; A,\; (B \ll 30),\; C,\; D). The terms are the 5 buffer words, the step number tt, the round function ff, the message word WtW_t and the constant KtK_t. StS^t is a circular left shift of a 32-bit word by tt bits.

Give the four SHA-1 round functions.

Round 1: (b AND c) OR ((NOT b) AND d)(b \text{ AND } c) \text{ OR } ((\text{NOT } b) \text{ AND } d), same as MD5. Round 2: b XOR c XOR db \text{ XOR } c \text{ XOR } d. Round 3: (b AND c) OR (b AND d) OR (c AND d)(b \text{ AND } c) \text{ OR } (b \text{ AND } d) \text{ OR } (c \text{ AND } d). Round 4: b XOR c XOR db \text{ XOR } c \text{ XOR } d.

How does SHA-1 create the 80-word input WtW_t, and why?

W0..W15W_0..W_{15} are the message words. For t≥16t \ge 16, Wt=S1(Wt−16⊕Wt−14⊕Wt−8⊕Wt−3)W_t = S^1(W_{t-16} \oplus W_{t-14} \oplus W_{t-8} \oplus W_{t-3}). For example, W16W_{16} uses W0,W2,W8,W13W_0, W_2, W_8, W_{13}. The expansion adds redundancy and interdependence among message blocks.

Compare SHA-1 with MD5.

A brute-force attack is harder on SHA-1 (160 bits against 128 bits). At the time of the slide, SHA-1 had no known attacks, unlike MD4/5. SHA-1 is a little slower (80 steps against 64). Both are simple and compact. SHA-1 is optimised for big-endian CPUs (SUN), MD5 for little-endian CPUs (PC).

Fill table 12.3 for SHA-1 and SHA-512.

SHA-1: digest 160, message <264< 2^{64}, block 512, word 32, steps 80, security 80. SHA-512: digest 512, message <2128< 2^{128}, block 1024, word 64, steps 80, security 256. Security is the birthday work factor, about 2n/22^{n/2}.

Give the SHA-512 round equations T1T_1 and T2T_2 with their functions.

T1=h+Ch(e,f,g)+Σ1512(e)+Wt+KtT_1 = h + \text{Ch}(e,f,g) + \Sigma_1^{512}(e) + W_t + K_t, with Σ1=ROTR14⊕ROTR18⊕ROTR41\Sigma_1 = \text{ROTR}^{14} \oplus \text{ROTR}^{18} \oplus \text{ROTR}^{41}. T2=Σ0512(a)+Maj(a,b,c)T_2 = \Sigma_0^{512}(a) + \text{Maj}(a,b,c), with Σ0=ROTR28⊕ROTR34⊕ROTR39\Sigma_0 = \text{ROTR}^{28} \oplus \text{ROTR}^{34} \oplus \text{ROTR}^{39}. Then a=T1+T2a = T_1 + T_2 and e=d+T1e = d + T_1. The other words move down one register.

  • NIST and NSA designed SHA in 1993. It became SHA-1 in 1995 (FIPS 180-1, RFC 3174). SHA-0 is withdrawn, SHA-1 is broken for collisions, SHA-2 is secure and SHA-3 (Keccak) is a new design.
  • SHA-1: 512-bit blocks, 16 sub-blocks of 32 bits, 4 rounds of 20 steps, a 160-bit buffer (A,B,C,D,E)(A,B,C,D,E) and K[t]K[t] for t=0t = 0 to 7979.
  • Steps: pad, append the 64-bit length, divide into 512-bit blocks, initialise the buffer, process the blocks, output the final buffer.
  • The step adds f(t,B,C,D)+S5(A)+Wt+Ktf(t,B,C,D) + S^5(A) + W_t + K_t to EE, and BB rotates left 30 bits. Round functions: If, Parity, Majority, Parity. The schedule expands 16 words to 80 with a four-way XOR and S1S^1.
  • SHA-1 against MD5: 160 against 128 digest bits, 80 against 64 steps, big-endian against little-endian.
  • FIPS 180-2 (2002) adds SHA-256/384/512. Table 12.3 gives security 80, 128, 192 and 256 bits.
  • SHA-512: 1024-bit blocks, a 128-bit length field, eight 64-bit words, 80 rounds mod 2642^{64}. a=T1+T2a = T_1 + T_2 and e=d+T1e = d + T_1.