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 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 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.
Message: the input, bits long, with .
Word: 32 bits. Every operation in SHA-256 works on words.
Block: 512 bits, which is 64 bytes or 16 words.
Hash value: the state, eight words, 256 bits in total.
Initial hash value: eight fixed words, the first 32 bits of the fractional parts of the square roots of the first eight prime numbers, 2 to 19.
Round constants: 64 fixed words, the first 32 bits of the fractional parts of the cube roots of the first 64 prime numbers, 2 to 311.
The computation runs in five steps.
Pad. Append a single 1 bit to the message, then 0 bits, then the message length written as a 64-bit number with its most significant bit first. The number is the smallest value, zero or more, that makes the total length a multiple of 512 bits.
Split. Cut the padded message into blocks of 512 bits. Read each block as sixteen words, each with its most significant byte first.
Start. Set the hash value to the initial hash value.
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.
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.
Bitwise AND §4.50: a result bit is 1 where both input bits are 1.
Bitwise XOR (exclusive OR): a result bit is 1 where exactly one of the two input bits is 1.
Bitwise complement: every bit is inverted.
Bit rotation to the right: the bits move right, and bits pushed out on the right re-enter on the left.
Logical shift to the right: the bits move right, bits pushed out are lost, and zeros enter on the left.
Addition modulo : the sum is kept to 32 bits and any carry beyond them is dropped.
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 bits.
Padding. The message, the 1 bit, the 0 bits and the 64-bit length must fill one block: , so . 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 0 bits are bytes of 0x00. The padded message is one block of bytes:
61 62 63 80, then 52 bytes00, then00 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 , so it fits in one block with . A message of 56 bytes is 448 bits, and already exceeds 512. The padding then runs on into a second block, with . In general a message of bytes needs blocks, where means 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 , with , to bit strings of length 256. Padding rule: to a message of bits append one 1 bit, then 0 bits, then as a 64-bit number with the most significant bit first, where is the smallest whole number, zero or more, with This notation means that 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 bits means that the best known attack needs about operations.
Collision resistance §5.5, the difficulty of finding any two messages with the same digest: 128 bits.
Preimage resistance §5.3, the difficulty of finding a message for a given digest: 256 bits.
Second-preimage resistance §5.4, the difficulty of finding a different message with the same digest as a given message: 201 to 256 bits. For a given message of blocks the value is about , so it is lowest for the longest messages.
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 for bitwise XOR, for bitwise AND, for the bitwise complement of , for the word rotated right by bits, and for shifted right by bits. Every is addition modulo . FIPS 180-4 defines six functions on words: In each bit position, takes the bit of where has a 1 and the bit of where has a 0, and gives the value that at least two of the three inputs have.
Let be the message schedule and the round constants. The words to are the sixteen words of the block. For , The eight working variables are named to . Round , for to 63, computes two temporary words, and then moves the variables along: takes the value of , of , of , becomes , takes the value of , of , of , and becomes .
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 bits of the digest is permitted, but the collision strength falls to bits and the preimage strength to 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 hash computations, roughly . 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 digests there are pairs, and each pair is equal with probability . The expected number of equal pairs is therefore , which reaches about one half at . The probability of at least one collision is then about 0.39, so a collision becomes likely once about 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 bits gives at most 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].