SHA-2

SHA-2
General
Designers National Security Agency
First published 2001
Series (SHA-0), SHA-1, SHA-2, SHA-3
Certification FIPS PUB 180-2
Detail
Digest sizes 224/256 bits or 384/512 bits
Structure Merkle–Damgård construction
Rounds 64 or 80
Best public cryptanalysis
A 2008 attack breaks preimage resistance for 46 out of 80 rounds of SHA-512, and 41 out of 64 rounds of SHA-256.[1] Collision attacks against up to 24 steps of SHA-256[1]

In cryptography, SHA-2 is a set of cryptographic hash functions (SHA-224, SHA-256, SHA-384, SHA-512) designed by the National Security Agency (NSA) and published in 2001 by the NIST as a U.S. Federal Information Processing Standard. SHA stands for Secure Hash Algorithm. SHA-2 includes a significant number of changes from its predecessor, SHA-1. SHA-2 consists of a set of four hash functions with digests that are 224, 256, 384 or 512 bits.

In 2005, security flaws were identified in SHA-1, namely that a mathematical weakness might exist, indicating that a stronger hash function would be desirable.[2] Although SHA-2 bears some similarity to the SHA-1 algorithm, these attacks have not been successfully extended to SHA-2.

A new hash standard, SHA-3, is currently under development; an ongoing NIST hash function competition is scheduled to end with the selection of a winning function in 2012. The SHA-3 algorithm will not be derived from SHA-2.

Contents

Hash function

NIST published four additional hash functions in the SHA family, named after their digest lengths (in bits): SHA-224, SHA-256, SHA-384, and SHA-512. The algorithms are collectively known as SHA-2.

The algorithms were first published in 2001 in the draft FIPS PUB 180-2, at which time review and comments 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 US 6829355 . The United States has released the patent under a royalty free license.[3]

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.

The SHA-2 functions are not as widely used as SHA-1, despite their better security. Reasons might include lack of support for SHA-2 on systems running Windows XP SP2 or older,[4] a lack of perceived urgency since SHA-1 collisions have not yet been found, or a desire to wait until SHA-3 is standardized. SHA-256 is used to authenticate Debian Linux software packages[5] and in the DKIM message signing standard; SHA-512 is part of a system to authenticate archival video from the International Criminal Tribunal of the Rwandan genocide.[6] SHA-256 and SHA-512 are proposed for use in DNSSEC.[7] Unix and Linux vendors are moving to using 256- and 512-bit SHA-2 for secure password hashing.[8] NIST's directive that U.S. government agencies must stop uses of SHA-1 after 2010,[9] and the completion of SHA-3, may accelerate migration away from SHA-1.

Currently, the best public attacks break 41 of the 64 rounds of SHA-256 or 46 of the 80 rounds of SHA-512, as discussed in the "Cryptanalysis and Validation" section below.[10]

Comparison of SHA functions

In the table below, internal state means the "internal hash sum" after each compression of a data block.

Algorithm and
variant
Output size (bits) Internal state size (bits) Block size (bits) Max message size (bits) Word size (bits) Rounds Operations Collisions found Example Performance (MiB/s)[11]
MD5 (as reference) 128 128 512 264 − 1 32 64 and,or,xor,rot Yes 255
SHA-0 160 160 512 264 − 1 32 80 +,and,or,xor,rot Yes -
SHA-1 160 160 512 264 − 1 32 80 +,and,or,xor,rot Theoretical attack (251)[12] 153
SHA-2 SHA-256/224 256/224 256 512 264 − 1 32 64 +,and,or,xor,shr,rot None 111
SHA-512/384 512/384 512 1024 2128 − 1 64 80 +,and,or,xor,shr,rot None 99

The performance numbers above were for a single-threaded implementation on an Intel Core 2 1.83 GHz processor under Windows Vista in 32-bit mode, and serve only as a rough point for general comparison. On 64-bit machines, SHA-512 is significantly faster than SHA-256.[13]

Applications

The SHA-2 hash function is implemented in some widely-used security applications and protocols, including TLS and SSL, PGP, SSH, S/MIME, Bitcoin and IPsec.

SHA-1 and SHA-2 are the secure hash algorithms required by law for use in certain U.S. Government applications, including use within 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. SHA-1 is being retired for most government uses; the U.S. National Institute of Standards and Technology says, "Federal agencies should stop using SHA-1 for...applications that require collision resistance as soon as practical, and must use the SHA-2 family of hash functions for these applications after 2010" (emphasis in original).[14]

Cryptanalysis and validation

For a hash function for which L is the number of bits in the message digest, finding a message that corresponds to a given message digest can always be done using a brute force search in 2L evaluations. This is called a preimage attack and may or may not be practical depending on L and the particular computing environment. The second criterion, finding two different messages that produce the same message digest, known as a collision, requires on average only 2L/2 evaluations using a birthday attack.

In terms of practical security, a major concern about these new attacks is that they might pave the way to more efficient ones. Whether this is the case has yet to be seen, but a migration to stronger hashes is believed to be prudent. Some of the applications that use cryptographic hashes, such as password storage, are only minimally affected by a collision attack. Constructing a password that works for a given account requires a preimage attack, as well as access to the hash of the original password (typically in the shadow file) which may or may not be trivial. Reversing password encryption (e.g., to obtain a password to try against a user's account elsewhere) is not made possible by the attacks. (However, even a secure password hash cannot prevent brute-force attacks on weak passwords.)

In the case of document signing, an attacker could not simply fake a signature from an existing document—the attacker would have to produce a pair of documents, one innocuous and one damaging, and get the private key holder to sign the innocuous document. There are practical circumstances in which this is possible; until the end of 2008, it was possible to create forged SSL certificates using an MD5 collision.[15]

There are two meet-in-the-middle preimage attacks against SHA-2 with a reduced number of rounds. The first one attacks 41-round SHA-256 out of 64 rounds with time complexity of 2253.5 and space complexity of 216, and 46-round SHA-512 out of 80 rounds with time 2511.5 and space 23.[1] The second one attacks 42-round SHA-256 with time complexity of 2251.7 and space complexity of 212, and 42-round SHA-512 with time 2502 and space 222.[16]

Official validation

Implementations of all FIPS-approved security functions can be officially validated through the CMVP program, jointly run by the National Institute of Standards and Technology (NIST) and the Communications Security Establishment (CSE). For informal verification, a package to generate a high number of test vectors is made available for download on the NIST site; the resulting verification however does not replace in any way the formal CMVP validation, which is required by law for certain applications..

Examples of SHA-2 variants

Hash values of empty string.

SHA224("")
0x d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
SHA256("")
0x e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
SHA384("")
0x 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
SHA512("")
0x cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

Even a small change in the message will (with overwhelming probability) result in a mostly different hash, due to the avalanche effect. For example, adding a period to the end of the sentence:

SHA224("The quick brown fox jumps over the lazy dog")
0x 730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525
SHA224("The quick brown fox jumps over the lazy dog.")
0x 619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c
SHA256("The quick brown fox jumps over the lazy dog")
0x d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
SHA256("The quick brown fox jumps over the lazy dog.")
0x ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c
SHA384("The quick brown fox jumps over the lazy dog")
0x ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1
SHA384("The quick brown fox jumps over the lazy dog.")
0x ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7
SHA512("The quick brown fox jumps over the lazy dog")
0x 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6
SHA512("The quick brown fox jumps over the lazy dog.")
0x 91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed

SHA-256 (a SHA-2 variant) pseudocode

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

Note 1: All variables are unsigned 32 bits and wrap modulo 232 when calculating
Note 2: All constants in this pseudo code are in big endian
Initialize variables
(first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
h[0..7] :=
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
Initialize table of round constants
(first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
k[0..63] :=
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 the bit '1' to the message
append k bits '0', where k is the minimum number >= 0 such that the resulting message
length (in bits) is congruent to 448 (mod 512)
append length of message (before pre-processing), in bits, as 64-bit big-endian integer
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[0..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) xor (a and c) xor (b and c)
t2 := s0 + maj
s1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
ch := (e and f) xor ((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 := t1 + t2
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
Produce the final hash value (big-endian):
digest = hash = h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7

The computation of the ch and maj values can be optimized the same way as described for SHA-1.

SHA-224 is identical to SHA-256, except that:

Here the initial values for the variables (in big endian):
(The second 32 bits of the fractional parts of the square roots of the 9th through 16th primes 23..53)
h[0..7] :=
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4

SHA-512 is identical in structure, but:

SHA-384 is identical to SHA-512, except that:

See also

Standards: SHA-1, SHA-2

Implementations in common languages

SHA hashing functions are found in many modern programming languages, such as Java,[17] Python,[18] PHP,[19] and Perl[20]

Other implementations

libsparkcrypto
A formally verified implementation of several widely used symmetric cryptographic algorithms using the SPARK programming language and toolset, complete library proofs of the absence of run-time errors like type range violations, division by zero and numerical overflows are available.
Libgcrypt
A general purpose cryptographic library based on the code from GNU Privacy Guard.
OpenSSL
The widely used OpenSSL crypto library includes free, open-source – implementations of SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512
cryptlib
an open source cross-platform software security toolkit library
Crypto++
A public domain C++ class library of cryptographic schemes, including implementations of the SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 algorithms.
Bouncy Castle
The Bouncy Castle Library is a free Java and C# class library that contains implementations of the SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512 algorithms as well as other algorithms like Whirlpool, Tiger, RIPEMD, GOST-3411, MD2, MD4 and MD5.
jsSHA
A cross-browser JavaScript library for client-side calculation of SHA digests, despite the fact that JavaScript does not natively support the 64-bit operations required for SHA-384 and SHA-512.
LibTomCrypt
A portable ISO C cryptographic toolkit, Public Domain.
md5deep
A set of programs to compute MD5, SHA-1, SHA-256, Tiger, or Whirlpool cryptographic message digests on an arbitrary number of files. It is used in computer security, system administration and computer forensics communities for purposes of running large numbers of files through any of several different cryptographic digests. It is similar to sha1sum from GNU Core Utilities and md5sum.
sphlib
A free, open-source library which implements many hash functions, including SHA-1 and SHA-2, both in portable (but optimized) C and in Java.
VHDL
A free, open-source collection of hardware implementations of hash functions (including SHA-1 and SHA-2) in portable (but optimized) VHDL.

References

  1. ^ a b c Yu Sasaki, Lei Wang, and Kazumaro Aoki (2008-11-25). Preimage Attacks on 41-Step SHA-256 and 46-Step SHA-512. http://eprint.iacr.org/2009/479.pdf. 
  2. ^ "Schneier on Security: Cryptanalysis of SHA-1". Schneier.com. http://www.schneier.com/blog/archives/2005/02/cryptanalysis_o.html. Retrieved 2011-11-08. 
  3. ^ Licensing Declaration for US patent 6829355.. https://datatracker.ietf.org/ipr/858/. Retrieved 2008-02-17. 
  4. ^ Microsoft Corporation, Overview of Windows XP Service Pack 3
  5. ^ "Debian codebase in Google Code". Google.com. http://google.com/codesearch/p?hl=en#nywQboHfkw4/apt/apt-pkg/acquire-item.cc&q=SHA256. Retrieved 2011-11-08. 
  6. ^ John Markoff, A Tool to Verify Digital Records, Even as Technology Shifts, New York Times, January 26, 2009
  7. ^ RFC 5702,RFC-Editor.org
  8. ^ Ulrich Drepper, Unix crypt with SHA-256/512
  9. ^ "Secure Hashing - NIST Computer Security Division - Computer Security Resource Center". NIST. http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html. Retrieved 2010-11-25. 
  10. ^ Yu Sasaki, Lei Wang, and Kazumaro Aoki, Preimage Attacks on 41-Step SHA-256 and 46-Step SHA-512, accessed January 3, 2010.
  11. ^ "Crypto++ 5.6.0 Benchmarks". http://www.cryptopp.com/benchmarks.html. Retrieved 2011-02-27. 
  12. ^ "Classification and Generation of Disturbance Vectors for Collision Attacks against SHA-1" (PDF). http://eprint.iacr.org/2008/469.pdf. Retrieved 2011-11-08. 
  13. ^ [SHA-512/256|http://eprint.iacr.org/2010/548.pdf]
  14. ^ National Institute on Standards and Technology Computer Security Resource Center, NIST's Policy on Hash Functions, accessed March 29, 2009.
  15. ^ Alexander Sotirov, Marc Stevens, Jacob Appelbaum, Arjen Lenstra, David Molnar, Dag Arne Osvik, Benne de Weger, MD5 considered harmful today: Creating a rogue CA certificate, accessed March 29, 2009.
  16. ^ Jian Guo, Krystian Matusiewicz (2008-11-25). Preimages for Step-Reduced SHA-2. http://eprint.iacr.org/2009/477.pdf. 
  17. ^ "Hashing Java". OWASP. https://www.owasp.org/index.php/Hashing_Java#Complete_Java_Sample. Retrieved 2011-11-08. 
  18. ^ "14.1. hashlib — Secure hashes and message digests — Python v2.7.2 documentation". Docs.python.org. http://docs.python.org/library/hashlib.html. Retrieved 2011-11-08. 
  19. ^ "hash - Manual". PHP. 2011-11-04. http://php.net/manual/en/function.hash.php. Retrieved 2011-11-08. 
  20. ^ "Digest::SHA". search.cpan.org. http://search.cpan.org/~mshelor/Digest-SHA-5.62/lib/Digest/SHA.pm. Retrieved 2011-11-08. 

External links