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.
SHA-0 is no longer considered to be secure, and recent claims suggest that SHA-1 is also broken. No attacks have yet been reported on the SHA-2 variants.
Contents |
SHA-0 and SHA-1
SHA-1.png
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 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 NSA, to correct a flaw in the original algorithm which reduced its cryptographic security. However, 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 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 264 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 (http://springerlink.metapress.com/link.asp?id=p795v6nj1vj525kp)): collisions can be found with complexity 261; less than the 280 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 251 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 240, significantly better than the attack by Joux et al [1] (http://www.freedom-to-tinker.com/archives/000664.html), [2] (http://groups.google.com/groups?selm=fgrieu-05A994.05060218082004%40individual.net).
In February 2005, Bruce Schneier reported on an attack by Xiaoyun Wang, Yiqun Lisa Yin, and Hongbo Yu which could find collisions in SHA-0 in 239 operations [3] (http://www.schneier.com/blog/archives/2005/02/sha1_broken.html). As of this writing, the details of the attack have not been made public, but a collision of the full SHA-0 function has been presented.
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 the 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 [4] (http://csrc.nist.gov/hash_standards_comments.pdf).
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 less than 280 operations [5] (http://eprint.iacr.org/2005/010).
In February 2005, Bruce Schneier reported an attack by Xiaoyun Wang, Yiqun Lisa Yin, and Hongbo Yu [6] (http://www.schneier.com/blog/archives/2005/02/sha1_broken.html). The attack is not yet published, but is outlined in a brief note by the authors. The authors assert that their attacks can find collisions in the full version of SHA-1, requiring less than 269 operations (a brute-force search would require 280.)
The details of the attack remain to be seen, but 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." [7] (http://theory.csail.mit.edu/~yiqun/shanote.pdf) The authors have presented a collision for 58-round SHA-1, claimed to be found with 233 hash operations.
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." [8] (http://news.zdnet.com/2100-1009_22-5598536.html).
In academic cryptography, any attack that has less computational complexity than the expected time needed for brute force is considered a break [9] (http://www.schneier.com/paper-self-study.html). This does not, however, necessarily mean that the attack can be practically exploited. Wang et. al. suggest that collisions for SHA-1 reduced to 70 rounds could be "found using today's supercomputers". It might also be possible for the full SHA-1 to be tackled by a relatively large organization willing to invest months of PC idle time organization-wide or invest in custom hardware, assuming that the attack is parallelizable, which is unclear at this time.
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.
Even with this new attack, SHA-1 still appears to be more secure than MD5 was ever thought to be. A brute force collision attack against MD5 takes only 264 operations, less than the 269 operations claimed by the unpublished attack. MD5 has also been broken, so the difference between MD5 and SHA-1 is actually far greater.
A collision attack also does not present the same kinds of risks that a preimage attack would. Most 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. In attacking download verification, the attacker would have to construct a download — with the attacker's trojan payload — that had the same checksum as the single "good" checksum; the unpublished result does not allow this. Reversing password "encryption" (e.g. to obtain a password to try against a user's account elsewhere) is likely not possible. Constructing a password that works for a given account requires a preimage attack (and access to the shadow password file, which may or may not be trivial).
Longer variants
NIST has published four additional hash functions under the name, each with longer digests. These 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.
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. The consequences of the Wang, Feng, et al paper in August of 2004 for these SHA variants is not entirely clear as of September 2004.
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, e.g. changing d to c:
- SHA1("The quick brown fox jumps over the lazy cog") = de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3
The hash of a zero-length string is:
- SHA1("") = da39a3ee5e6b4b0d3255bfef95601890afd80709
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 calculating Initialize variables: h0 := 0x67452301 h1 := 0xEFCDAB89 h2 := 0x98BADCFE h3 := 0x10325476 h4 := 0xC3D2E1F0 Pre-processing: append a single "1" bit to message append "0" bits until message length ≡ 448 ≡ -64 (mod 512) append length of message, in bits as 64-bit big-endian integer to message Process the message in successive 512-bit chunks: break message into 512-bit chunks for each chunk break chunk into sixteen 32-bit big-endian words w(i), 0 ≤ i ≤ 15 Extend the sixteen 32-bit words into eighty 32-bit words: for i from 16 to 79 w(i) := (w(i-3) xor w(i-8) xor w(i-14) xor w(i-16)) leftrotate 1 Initialize hash value for this chunk: a := h0 b := h1 c := h2 d := h3 e := h4 Main loop: for i from 0 to 79 if 0 ≤ i ≤ 19 then f := (b and c) or ((not b) and d) k := 0x5A827999 else if 20 ≤ i ≤ 39 f := b xor c xor d k := 0x6ED9EBA1 else if 40 ≤ i ≤ 59 f := (b and c) or (b and d) or (c and d) k := 0x8F1BBCDC else if 60 ≤ i ≤ 79 f := b xor c xor d k := 0xCA62C1D6 temp := (a leftrotate 5) + f + e + k + w(i) e := d d := c c := b leftrotate 30 b := a a := temp Add 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 = h0 append h1 append h2 append h3 append h4 (expressed as big-endian)
Note: Instead of the formulation from the original FIPS PUB 180-1 shown, the following may be used for improved efficiency:
(0 ≤ i ≤ 19): f := d xor (b and (c xor d)) (40 ≤ i ≤ 59): f := (b and c) or (d and (b or c)) (60 ≤ i ≤ 79): f := (b and c) + (d and (b xor c))
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 232 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) := 232 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, in bits as 64-bit big-endian integer to message Process the message in successive 512-bit chunks: break message into 512-bit chunks for each 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: for i from 16 to 63 s0 := (w(i-15) rightrotate 7) xor (w(i-15) rightrotate 18) xor (w(i-15) rightshift 3) s1 := (w(i-2) rightrotate 17) xor (w(i-2) rightrotate 19) xor (w(i-2) rightshift 10) 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: for i from 0 to 63 s0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22) maj := (a and b) or (b and c) or (c and a) t0 := s0 + maj s1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25) ch := (e and f) or ((not e) and g) 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 = h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7
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) [10] (http://eprint.iacr.org/2004/146/)
- 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
External links
Description of the algorithms
- FIPS PUB 180-2 (http://csrc.nist.gov/encryption/tkhash.html), Secure Hash Standard (including SHA-1, SHA-256, SHA-384, and SHA-512)
- RFC 3174 (http://www.ietf.org/rfc/rfc3174.txt), US Secure Hash Algorithm 1 (SHA1)
Cryptanalysis
- Schneier's Summary of the February 2005 cryptanalytic results on SHA-0 and SHA-1 (http://www.schneier.com/blog/archives/2005/02/sha1_broken.html)
- Interview with Yiqun Lisa Yin concerning the attack on SHA-1 (http://news.zdnet.com/2100-1009_22-5598536.html)
- Lenstra's Summary of impact of the February 2005 cryptanalytic results (http://cm.bell-labs.com/who/akl/hash.pdf)
Implementations
- Online SHA1 and MD5 calculator (http://www.xml-dev.com/blog/sha1.php)
- A Javascript SHA-1 calculator showing intermediate values in the calculation (http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-SHA1.html)
- Jacksum (http://www.jonelo.de/java/jacksum/index.html) (a program with various message verification functions, supporting SHA-1 and three variants of SHA-2)
- Optimized SHA-1 source code in C (GPL license) (http://www.cr0.net:8040/code/crypto/sha1/)
- JavaScript implementation of SHA-1 (http://www.movable-type.co.uk/scripts/SHA-1.html)
Template:Cryptographic hash functionsde:Secure Hash Algorithm fr:SHA-1 nl:SHA-familie ja:SHA ru:SHA zh:SHA 家族