Section 5.2

SHA-256

Full name: Secure Hash Algorithm with a 256-bit output.

Definition

SHA-256 is a cryptographic hash function §5.1: a public algorithm §14.1 that turns a message of any length below 2642^{64} bits into a 256-bit value called the message digest. It is designed so that finding a message for a given digest, or two different messages with the same digest, would take more computation than anyone can perform. SHA stands for Secure Hash Algorithm, and 256 is the digest length in bits (32 bytes). SHA-256 uses no secret key. NIST specifies it in the standard FIPS 180-4, among the functions known as SHA-2.

The idea

Two parties can compare digests only if both compute exactly the same function, bit for bit, on every kind of computer. SHA-256 is such an agreed function. A public standard fixes every step and every constant, so two correct implementations always give the same 32 bytes for the same message.

The core idea is a small state of fixed size that absorbs the message one piece at a time. SHA-256 keeps 256 bits of state and cuts the message into blocks of 512 bits. For each block it replaces the state with a thorough mixture of the old state and the block. After the last block, the state is the digest. A long message and a short one therefore give digests of the same size. The mixing step is the hash compression function §15.9 of SHA-256: it takes 256+512=768256 + 512 = 768 bits in and gives 256 bits out. Building a hash function by repeating a compression function over the blocks of a padded message is called the Merkle–Damgård construction §15.8.

How it works

FIPS 180-4 uses these units and fixed values.

The computation runs in five steps.

  1. Pad. Append a single 1 bit to the message, then kk 0 bits, then the message length ℓ\ell written as a 64-bit number with its most significant bit first. The number kk is the smallest value, zero or more, that makes the total length a multiple of 512 bits.

  2. Split. Cut the padded message into blocks of 512 bits. Read each block as sixteen words, each with its most significant byte first.

  3. Start. Set the hash value to the initial hash value.

  4. Compress each block in order. Expand the sixteen words of the block into 64 words, called the message schedule. Copy the hash value into eight working variables. Run 64 rounds; each round updates the working variables using one schedule word and one round constant. Then add each working variable to the matching word of the hash value, keeping only the lowest 32 bits of each sum. The eight sums are the new hash value.

  5. Output. After the last block, write out the eight words of the hash value, first word first, each word with its most significant byte first. These 32 bytes are the digest.

The message schedule and the rounds use six operations on words.

Because blocks are processed in order and the padding comes last, a program can accept the message in pieces of any size. It keeps only the hash value, the bytes of the unfinished block and the running length. Supplying the same bytes in different pieces gives the same digest.

Worked example

Hash the three ASCII characters abc, which are the bytes 0x61, 0x62, 0x63. The message length is ℓ=3×8=24\ell = 3 \times 8 = 24 bits.

Padding. The message, the 1 bit, the kk 0 bits and the 64-bit length must fill one block: 24+1+k+64=51224 + 1 + k + 64 = 512, so k=512−89=423k = 512 - 89 = 423. The length field holds the number 24, which is 0x18. Counted in bytes, the 1 bit and the first seven 0 bits form the byte 0x80. The remaining 423−7=416423 - 7 = 416 0 bits are 416/8=52416 / 8 = 52 bytes of 0x00. The padded message is one block of 3+1+52+8=643 + 1 + 52 + 8 = 64 bytes:

61 62 63 80, then 52 bytes 00, then 00 00 00 00 00 00 00 18

Read as sixteen words, the block is 61626380, then fourteen words 00000000, then 00000018.

Digest. Compressing this one block, starting from the initial hash value, gives the final hash value. Its eight words are

ba7816bf 8f01cfea 414140de 5dae2223
b00361a3 96177a9c b410ff61 f20015ad

Written without spaces, these 64 hexadecimal digits are the SHA-256 digest of abc. FIPS 180-4 pads this same message as its example, and RFC 6234 lists this digest among its test values.

Number of blocks. A message of 55 bytes is 440 bits, and 440+1+7+64=512440 + 1 + 7 + 64 = 512, so it fits in one block with k=7k = 7. A message of 56 bytes is 448 bits, and 448+1+64=513448 + 1 + 64 = 513 already exceeds 512. The padding then runs on into a second block, with k=1024−513=511k = 1024 - 513 = 511. In general a message of bb bytes needs ⌊(b+8)/64⌋+1\lfloor (b + 8) / 64 \rfloor + 1 blocks, where ⌊x⌋\lfloor x \rfloor means xx rounded down to a whole number.

What counts as the message. SHA-256 hashes bytes, not their meaning. The six ASCII characters 616263 are a different message from the three bytes they spell out in hexadecimal, and their digest begins 86900f25, not ba7816bf.

Stated precisely

SHA-256 is a function from bit strings of length ℓ\ell, with 0≤ℓ<2640 \le \ell < 2^{64}, to bit strings of length 256. Padding rule: to a message of ℓ\ell bits append one 1 bit, then kk 0 bits, then ℓ\ell as a 64-bit number with the most significant bit first, where kk is the smallest whole number, zero or more, with ℓ+1+k≡448(mod512).\ell + 1 + k \equiv 448 \pmod{512}. This notation means that ℓ+1+k\ell + 1 + k leaves the remainder 448 when divided by 512. Adding the 64 length bits then gives a multiple of 512.

The standard defines how to compute the function. It does not prove that the function is secure. NIST Special Publication 800-107 Revision 1 (2012) [310] gives the security strengths that SHA-256 is expected to provide, where a strength of ss bits means that the best known attack needs about 2s2^s operations.

These figures are not proven. They state the cost of the best attacks that were publicly known when NIST published them.

The round function, exactly

Write ⊕\oplus for bitwise XOR, ∧\wedge for bitwise AND, ¬x\neg x for the bitwise complement of xx, ROTRn(x)\mathrm{ROTR}^{n}(x) for the word xx rotated right by nn bits, and SHRn(x)\mathrm{SHR}^{n}(x) for xx shifted right by nn bits. Every ++ is addition modulo 2322^{32}. FIPS 180-4 defines six functions on words: Ch(x,y,z)=(x∧y)⊕(¬x∧z),Maj(x,y,z)=(x∧y)⊕(x∧z)⊕(y∧z),Σ0(x)=ROTR2(x)⊕ROTR13(x)⊕ROTR22(x),Σ1(x)=ROTR6(x)⊕ROTR11(x)⊕ROTR25(x),σ0(x)=ROTR7(x)⊕ROTR18(x)⊕SHR3(x),σ1(x)=ROTR17(x)⊕ROTR19(x)⊕SHR10(x).\begin{aligned} \mathrm{Ch}(x, y, z) &= (x \wedge y) \oplus (\neg x \wedge z), \\ \mathrm{Maj}(x, y, z) &= (x \wedge y) \oplus (x \wedge z) \oplus (y \wedge z), \\ \Sigma_0(x) &= \mathrm{ROTR}^{2}(x) \oplus \mathrm{ROTR}^{13}(x) \oplus \mathrm{ROTR}^{22}(x), \\ \Sigma_1(x) &= \mathrm{ROTR}^{6}(x) \oplus \mathrm{ROTR}^{11}(x) \oplus \mathrm{ROTR}^{25}(x), \\ \sigma_0(x) &= \mathrm{ROTR}^{7}(x) \oplus \mathrm{ROTR}^{18}(x) \oplus \mathrm{SHR}^{3}(x), \\ \sigma_1(x) &= \mathrm{ROTR}^{17}(x) \oplus \mathrm{ROTR}^{19}(x) \oplus \mathrm{SHR}^{10}(x). \end{aligned} In each bit position, Ch\mathrm{Ch} takes the bit of yy where xx has a 1 and the bit of zz where xx has a 0, and Maj\mathrm{Maj} gives the value that at least two of the three inputs have.

Let W0,…,W63W_0, \dots, W_{63} be the message schedule and K0,…,K63K_0, \dots, K_{63} the round constants. The words W0W_0 to W15W_{15} are the sixteen words of the block. For 16≤t≤6316 \le t \le 63, Wt=σ1(Wt−2)+Wt−7+σ0(Wt−15)+Wt−16.W_t = \sigma_1(W_{t-2}) + W_{t-7} + \sigma_0(W_{t-15}) + W_{t-16}. The eight working variables are named aa to hh. Round tt, for t=0t = 0 to 63, computes two temporary words, T1=h+Σ1(e)+Ch(e,f,g)+Kt+Wt,T2=Σ0(a)+Maj(a,b,c),T_1 = h + \Sigma_1(e) + \mathrm{Ch}(e, f, g) + K_t + W_t, \qquad T_2 = \Sigma_0(a) + \mathrm{Maj}(a, b, c), and then moves the variables along: hh takes the value of gg, gg of ff, ff of ee, ee becomes d+T1d + T_1, dd takes the value of cc, cc of bb, bb of aa, and aa becomes T1+T2T_1 + T_2.

Limits and common mistakes

The 256 in the name is the digest length, not the strength against every attack. The strength against collisions is 128 bits.

Two sizes are easy to confuse. The digest is 32 bytes. The block is 64 bytes. The block size is the one that matters inside HMAC §5.8 (hash-based message authentication code), a construction that uses a hash function and a secret key to protect a message against changes. HMAC first hashes any key that is longer than one block.

The digest is the complete final state. Someone who knows the digest of a message and the length of the message, but not the message itself, can continue the computation and obtain the digest of a longer message: the original message, its padding, and then any data of that person’s choice. This is the length-extension attack §5.11. It is the reason why SHA-256 of a secret key followed by a message must not be used to authenticate the message. HMAC is not affected, because it hashes the result a second time under the key.

SHA-256 is fast. A stored SHA-256 digest of a password can be attacked by hashing candidate passwords at high speed. Passwords need a deliberately slow password-hashing function §15.10.

A shortened digest is weaker. Keeping only the leftmost λ\lambda bits of the digest is permitted, but the collision strength falls to λ/2\lambda / 2 bits and the preimage strength to λ\lambda bits.

Questions answered

Is the security level §7.33 against generic collision search 256 bits?

No. Against generic collision search SHA-256 offers about 128 bits of security, which means about 21282^{128} hash computations, roughly 3.4×10383.4 \times 10^{38}. The number 256 is the length of the digest in bits.

A generic attack uses nothing about the inside of SHA-256, only the fact that its digests are 256 bits long. The generic way to find a collision, two different messages with the same digest, is to hash many different messages and wait until any two digests are equal. Among qq digests there are q(q−1)/2q(q-1)/2 pairs, and each pair is equal with probability 2−2562^{-256}. The expected number of equal pairs is therefore q(q−1)/2257q(q-1)/2^{257}, which reaches about one half at q=2128q = 2^{128}. The probability of at least one collision is then about 0.39, so a collision becomes likely once about 21282^{128} messages have been hashed. This square-root effect is the birthday bound §5.6. It halves the exponent for every hash function: a digest of nn bits gives at most n/2n/2 bits of collision resistance.

The figure of 256 bits belongs to a different task: finding a message that matches one given digest, where every attempt has a single target. NIST Special Publication 800-107 Revision 1 (2012) lists SHA-256 at 128 bits for collision resistance, 256 bits for preimage resistance and 201 to 256 bits for second-preimage resistance, the difficulty of finding a different message with the same digest as one given message. A system that depends on collision resistance must therefore count SHA-256 as a 128-bit function.

Related entries: Collision resistance (§5.5), Birthday bound (§5.6), HMAC (§5.8), Cryptographic hash function (§5.1), HMAC-SHA-256 (§5.9), Length-extension attack (§5.11), Merkle–Damgård construction (§15.8), Hash compression function (§15.9). Further reading: [7, 46, 147, 310].