# SHA hash functions

The **SHA** (**Secure Hash Algorithm**) family is a set of related cryptographic hash functions. The most commonly used function in the family, **SHA-1**, is employed in a large variety of popular security applications and protocols, including TLS, SSL, PGP, SSH, S/MIME, and IPSec. SHA-1 is considered to be the successor to MD5, an earlier, widely-used hash function. The SHA algorithms were designed by the National Security Agency (NSA) and published as a US government standard.

The first member of the family, published in 1993, is officially called **SHA**; however, it is often called **SHA-0** to avoid confusion with its successors. Two years later, SHA-1, the first successor to SHA, was published. Four more variants have since been issued with increased output ranges and a slightly different design: **SHA-224**, **SHA-256**, **SHA-384**, and **SHA-512** — sometimes collectively referred to as **SHA-2**.

Attacks have been found for both SHA-0 and SHA-1, while no attacks have been reported on the SHA-2 variants.

## Contents

## SHA-0 and SHA-1

The original specification of the algorithm was published in 1993 as the **Secure Hash Standard**, FIPS PUB 180, by US government standards agency NIST (National Institute of Standards and Technology). This version is now often referred to as "SHA-0". It was withdrawn by the NSA shortly after publication and was superseded by the revised version, published in 1995 in FIPS PUB 180-1 and commonly referred to as "SHA-1". This was done, according to the NSA, to correct a flaw in the original algorithm which reduced its cryptographic security. However, the NSA did not provide any further explanation or identify what flaw was corrected. Weaknesses have subsequently been reported in both SHA-0 and SHA-1. SHA-1 appears to provide greater resistance to attacks, supporting the NSA's assertion that the change increased the security.

SHA-0 and SHA-1 produce a 160-bit digest from a message with a maximum size of 2^{64} bits, and is based on principles similar to those used by Professor Ronald L. Rivest of MIT in the design of the MD4 and MD5 message digest algorithms.

### Cryptanalysis of SHA-0

At CRYPTO 98, two French researchers presented an attack on SHA-0 (Chabaud and Joux, 1998): collisions can be found with complexity 2^{61}; fewer than the 2^{80} for an ideal hash function of the same size.

In 2004, Biham and Chen found near-collisions for SHA-0 — two messages that hash to nearly the same value; in this case, 142 out of the 160 bits are equal. They also found full collisions of SHA-0 reduced to 62 out of its 80 rounds.

Subsequently, on 12 August 2004, a collision for the full SHA-0 algorithm was announced by Joux, Carribault, Lemuet and Jalby. This was done by using a generalization of the Chabaud and Joux attack. Finding the collision had complexity 2^{51} and took about 80,000 CPU hours on a supercomputer with 256 Itanium 2 processors.

On 17 August 2004, at the Rump Session of CRYPTO 2004, preliminary results were announced by Wang, Feng, Lai, and Yu, about an attack on MD5, SHA-0 and other hash functions. The complexity of their attack on SHA-0 is 2^{40}, significantly better than the attack by Joux et al [1], [2].

In February 2005, an attack by Xiaoyun Wang, Yiqun Lisa Yin, and Hongbo Yu was announced which could find collisions in SHA-0 in 2^{39} operations [3], [4].

### Cryptanalysis of SHA-1

In the light of the results on SHA-0, some experts suggested that plans for the use of SHA-1 in new cryptosystems should be reconsidered. After these results were published, NIST announced that they planned to phase out the use of SHA-1 by 2010 in favour of the SHA-2 variants [5].

In early 2005, Rijmen and Oswald published an attack on a reduced version of SHA-1 — 53 out of 80 rounds — which finds collisions with a complexity of fewer than 2^{80} operations [6].

In February 2005, an attack by Xiaoyun Wang, Yiqun Lisa Yin, and Hongbo Yu was announced [7]. The attacks can find collisions in the full version of SHA-1, requiring fewer than 2^{69} operations (a brute-force search would require 2^{80}.)

The authors write:
"In particular, our analysis is built up on the original differential attack on SHA0, the near collision attack on SHA0, the multi-block collision techniques, as well as the message modification techniques used in the collision search attack on MD5. Breaking SHA1 would not be possible without these powerful analytical techniques." [8] The authors have presented a collision for 58-round SHA-1, found with 2^{33} hash operations.
The paper with the full attack description was published in August 2005 at the CRYPTO conference.

In an interview, Yin states that, "Roughly, we exploit the following two weaknesses: One is that the file preprocessing step is not complicated enough; another is that certain math operations in the first 20 rounds have unexpected security problems." [9].

On 17 August 2005, an improvement on the SHA-1 attack was announced on behalf of Xiaoyun Wang, Andrew Yao and Frances Yao at the CRYPTO 2005 rump session, lowering the complexity required for finding a collision in SHA-1 to 2^{63}. [10]

In academic cryptography, any attack that has less computational complexity than the expected time needed for brute force is considered a break [11]. This does not, however, necessarily mean that the attack can be practically exploited. It has been speculated that finding a collision for SHA-1 is within reach of massive distributed Internet search.

In terms of practical security, the major concern about this new attack is that it might pave the way to more efficient attacks. Whether this is the case has yet to be seen, but a migration to stronger hashes is believed to be prudent. A collision attack does not present the same kinds of risks that a preimage attack would. Many of the applications that use cryptographic hashes, such as password storage or document signing, are only minimally affected by a collision attack. In the case of document signing, for example, an attacker could not simply fake a signature from an existing document -- the attacker would have to fool the private key holder into signing a preselected document. Reversing password "encryption" (e.g. to obtain a password to try against a user's account elsewhere) is not made possible by the attacks. Constructing a password that works for a given account requires a preimage attack, and access to the hash of the original password (typically in the *shadow* file) which may or may not be trivial.

## Longer variants

NIST has published four additional hash functions in the SHA family, each with longer digests, collectively known as SHA-2. The individual variants are named after their digest lengths (in bits): "SHA-256", "SHA-384", and "SHA-512". They were first published in 2001 in the draft FIPS PUB 180-2, at which time review and comment were accepted. FIPS PUB 180-2, which also includes SHA-1, was released as an official standard in 2002. In February 2004, a change notice was published for FIPS PUB 180-2, specifying an additional variant, "SHA-224", defined to match the key length of two-key Triple DES. These variants are patented in Template:US patent.

SHA-256 and SHA-512 are novel hash functions computed with 32- and 64-bit words, respectively. They use different shift amounts and additive constants, but their structures are otherwise virtually identical, differing only in the number of rounds. SHA-224 and SHA-384 are simply truncated versions of the first two, computed with different initial values.

These new hash functions have not received as much scrutiny by the public cryptographic community as SHA-1 has, and so their cryptographic security is not yet as well-established. Gilbert and Handschuh (2003) have studied the newer variants and found no weaknesses.

## Applications

SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are the required secure hash algorithms for use in U.S. Federal applications, including use by other cryptographic algorithms and protocols, for the protection of sensitive unclassified information. FIPS PUB 180-1 also encouraged adoption and use of SHA-1 by private and commercial organizations.

A prime motivation for the publication of the Secure Hash Algorithm was the Digital Signature Standard, in which it is incorporated.

The SHA hash functions have been used as the basis for the SHACAL block ciphers.

The copy prevention system of Microsoft's Xbox game console relies on the security of SHA-1.

SHA-1 hashing has also been employed by many file sharing applications to link multiple sources for the same file, that may not have the same name, as well as to avoid matching nonidentical sources that may have the same name.

## SHA1 hashes

The following are some examples of SHA1 digests:

SHA1("The quick brown fox jumps over the lazy dog") == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"

Even a small change in the message will, with overwhelming probability, result in a completely different hash due to the avalanche effect. For example, changing `d` to `c`:

SHA1("The quick brown fox jumps over the lazy cog") == "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3"

The hash of the zero-length string is:

SHA1("") == "da39a3ee5e6b4b0d3255bfef95601890afd80709"

## Implementations

The US and Canadian governments (NIST and CSE) have a validation program for SHA implementations [12]. There are also example validation tests that can be used to test routines without going through validation. The examples (and the real validations) require some tens of thousands of hashes, and were designed to detect some buffer fence post errors and other common implementation errors.

As of mid August, 2005, there are 370 validated implementations of SHA, with seven handling arbitrary binary data instead of just bytes.

There are some implementations available on the net that do not pass the examples on the NIST validation pages (although they do pass the examples in the SHA-1 standard.)

## A description of SHA-1

Pseudocode for the SHA-1 algorithm follows:

Note: All variables are unsigned 32 bits and wrap modulo 2^32 when calculatingInitialize variables:h0 := 0x67452301 h1 := 0xEFCDAB89 h2 := 0x98BADCFE h3 := 0x10325476 h4 := 0xC3D2E1F0Pre-processing:append a single "1" bit to message append "0" bits until message length ≡ 448 ≡ -64 (mod 512) append length of message (before pre-processing), inbitsas 64-bit big-endian integer to messageProcess the message in successive 512-bit chunks:break message into 512-bit chunksforeach chunk break chunk into sixteen 32-bit big-endian words w(i), 0 ≤ i ≤ 15Extend the sixteen 32-bit words into eighty 32-bit words:forifrom16 to 79 w(i) := (w(i-3)xorw(i-8)xorw(i-14)xorw(i-16))leftrotate1Initialize hash value for this chunk:a := h0 b := h1 c := h2 d := h3 e := h4Main loop:forifrom0 to 79if0 ≤ i ≤ 19thenf := (bandc)or((notb)andd) k := 0x5A827999else if20 ≤ i ≤ 39 f := bxorcxord k := 0x6ED9EBA1else if40 ≤ i ≤ 59 f := (bandc)or(bandd)or(candd) k := 0x8F1BBCDCelse if60 ≤ i ≤ 79 f := bxorcxord k := 0xCA62C1D6 temp := (aleftrotate5) + f + e + k + w(i) e := d d := c c := bleftrotate30 b := a a := tempAdd this chunk's hash to result so far:h0 := h0 + a h1 := h1 + b h2 := h2 + c h3 := h3 + d h4 := h4 + e digest = hash = h0appendh1appendh2appendh3appendh4(expressed as big-endian)

*Note: Instead of the formulation from the original FIPS PUB 180-1 shown, the following equivalent expressions may be substituted into the appropriate ranges of the above pseudocode for improved efficiency:*

(0 ≤ i ≤ 19): f := dxor(band(cxord))(alternative)(20 ≤ i ≤ 39): f := bxorcxord(unchanged)(40 ≤ i ≤ 59): f := (bandc)or(dand(borc))(alternative 1)(40 ≤ i ≤ 59): f := (bandc) + (dand(bxorc))(alternative 2, due to Colin Plumb)(60 ≤ i ≤ 79): f := bxorcxord(unchanged)

## A description of SHA-256

Pseudocode for the SHA-256 algorithm follows. Note the great increase in mixing between bits of the words compared to SHA-1.

//Note: All variables are unsigned 32 bits and wrap modulo 2^32 when calculating//Initialize variables:h0 := 0x6a09e667 //2^{32}times the square root of the first 8 primes 2..19 h1 := 0xbb67ae85 h2 := 0x3c6ef372 h3 := 0xa54ff53a h4 := 0x510e527f h5 := 0x9b05688c h6 := 0x1f83d9ab h7 := 0x5be0cd19 //Initialize table of round constants:k(0..63) := //2^{32}times the cube root of the first 64 primes 2..311 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 //Pre-processing:append a single "1" bit to message append "0" bits until message length ≡ 448 ≡ -64 (mod 512) append length of message (before pre-processing), inbitsas 64-bit big-endian integer to message //Process the message in successive 512-bit chunks:break message into 512-bit chunksforeach chunk break chunk into sixteen 32-bit big-endian words w(i), 0 ≤ i ≤ 15 //Extend the sixteen 32-bit words into sixty-four 32-bit words:forifrom16 to 63 s0 := (w(i-15)rightrotate7)xor(w(i-15)rightrotate18)xor(w(i-15)rightshift3) s1 := (w(i-2)rightrotate17)xor(w(i-2)rightrotate19)xor(w(i-2)rightshift10) w(i) := w(i-16)+s0+w(i-7)+s1 //Initialize hash value for this chunk:a := h0 b := h1 c := h2 d := h3 e := h4 f := h5 g := h6 h := h7 //Main loop:forifrom0 to 63 s0 := (arightrotate2)xor(arightrotate13)xor(arightrotate22) maj := (aandb)or(bandc)or(canda) t0 := s0 + maj s1 := (erightrotate6)xor(erightrotate11)xor(erightrotate25) ch := (eandf)or((note)andg) t1 := h + s1 + ch + k(i) + w(i) h := g g := f f := e e := d + t1 d := c c := b b := a a := t0 + t1 //Add this chunk's hash to result so far:h0 := h0 + a h1 := h1 + b h2 := h2 + c h3 := h3 + d h4 := h4 + e h5 := h5 + f h6 := h6 + g h7 := h7 + h //Output the final hash value (big-endian):digest = hash = h0appendh1appendh2appendh3appendh4appendh5appendh6appendh7

*Note: The ch and maj functions can be optimized the same way as described in SHA-1.*

SHA-512 is identical in structure, except that:

- All numbers are 64 bits long,
- There are 80 rounds instead of 64,
- The initial values and additive constants are different, and
- The shift and rotate amounts used are different.

## See also

## References

- Eli Biham, Rafi Chen, Near-Collisions of SHA-0, Cryptology ePrint Archive, Report 2004/146, 2004 (to appear CRYPTO 2004) [13]
- Florent Chabaud, Antoine Joux: Differential Collisions in SHA-0. CRYPTO 1998. pp56–71
- Henri Gilbert, Helena Handschuh: Security Analysis of SHA-256 and Sisters. Selected Areas in Cryptography 2003: pp175–193
- Xiaoyun Wang, Hongbo Yu and Yiqun Lisa Yin, Efficient Collision Search Attacks on SHA-0, CRYPTO 2005 [14].
- Xiaoyun Wang, Yiqun Lisa Yin and Hongbo Yu, Finding Collisions in the Full SHA-1, CRYPTO 2005 [15].

## External links

### Description of the algorithms

- FIPS PUB 180-2,
*Secure Hash Standard*(including SHA-1, SHA-256, SHA-384, and SHA-512) - RFC 3174,
*US Secure Hash Algorithm 1 (SHA1)*

### Cryptanalysis

- Interview with Yiqun Lisa Yin concerning the attack on SHA-1
- Lenstra's Summary of impact of the February 2005 cryptanalytic results

### Implementations

- Serversniff.net Online-Tool to compute several SHA-Hashes from strings
- Calculate md5 or sha1 hash of a string — Javascript implementation of MD5 and SHA1 by Pavel Pavlov. Free for any use.
- open source (GPL, BSD) code for SHA1 (in Ada) Officially validated by the US and Canadian Governments (Certificate #39, issued on September 20th, 2000)
- A JavaScript SHA-1 calculator showing intermediate values in the calculation
- Online SHA1 and MD5 calculator (does not use JavaScript)
- Calculate SHA-1 hashes from strings and files online (does not use JavaScript)
- Jacksum — by Dipl.-Inf. (FH) Johann N. Löfflmann in Java. Various message verification functions, supporting SHA-0, SHA-1 and all four variants of SHA-2. Released under the GPL.
- Optimized SHA-1 source code in C (GPL license)
- Optimized SHA-2 source code in C (BSD license)
- Encryption Calculator - JavaScript encryption calculator that supports SHA-1 and SHA-256.
- JavaScript implementation of SHA-1
- PHP implementation of SHA256
- PHP implementation of SHA256 and other hashes like MD5, MD4, TIGER, etc.
- Tcl implementation of SHA-256 and SHA-224
- Paj's Home: Cryptography — by Paj in JavaScript. Also supports MD4 and MD5. Released under the BSD License. Contains links to several other implementations.

Template:Cryptographic hash functions

de:Sicherer Hash-Algorithmus es:SHA fr:SHA-1 nl:SHA-familie ja:SHA pt:SHA-1 ru:SHA-1 zh:SHA 家族