diff --git a/ACKS b/ACKS index d9d410e..ca15d1a 100644 --- a/ACKS +++ b/ACKS @@ -35,6 +35,7 @@ Bryan Olson Wallace Owen Colin Plumb Robey Pointer +Lorenz Quack Jeethu Rao James P. Rutledge Matt Schreiner diff --git a/lib/Crypto/SelfTest/Hash/__init__.py b/lib/Crypto/SelfTest/Hash/__init__.py index 6f6df2b..2439549 100644 --- a/lib/Crypto/SelfTest/Hash/__init__.py +++ b/lib/Crypto/SelfTest/Hash/__init__.py @@ -35,6 +35,7 @@ def get_tests(config={}): import test_RIPEMD; tests += test_RIPEMD.get_tests(config=config) import test_SHA; tests += test_SHA.get_tests(config=config) import test_SHA256; tests += test_SHA256.get_tests(config=config) + import test_SHA512; tests += test_SHA512.get_tests(config=config) return tests if __name__ == '__main__': diff --git a/lib/Crypto/SelfTest/Hash/test_SHA512.py b/lib/Crypto/SelfTest/Hash/test_SHA512.py new file mode 100644 index 0000000..202c1da --- /dev/null +++ b/lib/Crypto/SelfTest/Hash/test_SHA512.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +# +# SelfTest/Hash/test_SHA512.py: Self-test for the SHA-512 hash function +# +# Written in 2010 by Lorenz Quack based on +# SelfTest/Hash/test_SHA256.py by Dwayne C. Litzenberger +# +# =================================================================== +# The contents of this file are dedicated to the public domain. To +# the extent that dedication to the public domain is not available, +# everyone is granted a worldwide, perpetual, royalty-free, +# non-exclusive license to exercise all rights associated with the +# contents of this file for any purpose whatsoever. +# No rights are reserved. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# =================================================================== + +"""Self-test suite for Crypto.Hash.SHA512""" + +__revision__ = "$Id$" + +import unittest + + +def get_tests(config={}): + # This is a list of (expected_result, input[, description]) tuples. + test_data = [ + # FIPS PUB 180-2, C.1 - "One-Block Message" + ('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', + 'abc', + 'FIPS PUB 180-2, C.1 - "One-Block Message"'), + + # FIPS PUB 180-2, C.2 - "Multi-Block Message" + ('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909', + 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', + 'FIPS PUB 180-2, C.2 - "Multi-Block Message"'), + + # FIPS PUB 180-2, C.3 - "Long Message" + ('e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b', + 'a' * 10**6, + 'FIPS PUB 180-2, C.3 - "Long Message"'), + + # Empty String taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm + ('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', + '', + 'empty string'), + # Example taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm + ('af9ed2de700433b803240a552b41b5a472a6ef3fe1431a722b2063c75e9f07451f67a28e37d09cde769424c96aea6f8971389db9e1993d6c565c3c71b855723c', + 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'), + ] + + from Crypto.Hash import SHA512 + from common import make_hash_tests + tests = make_hash_tests(SHA512, "SHA512", test_data) + + return tests + +if __name__ == '__main__': + import unittest + suite = lambda: unittest.TestSuite(get_tests()) + unittest.main(defaultTest='suite') + +# vim:set ts=4 sw=4 sts=4 expandtab: diff --git a/src/SHA512.c b/src/SHA512.c new file mode 100644 index 0000000..54813e6 --- /dev/null +++ b/src/SHA512.c @@ -0,0 +1,246 @@ +/* + * An implementation of the SHA-512 hash function, this is endian neutral + * so should work just about anywhere. + * + * This code works much like the MD5 code provided by RSA. You sha_init() + * a "sha_state" then sha_process() the bytes you want and sha_done() to get + * the output. + * + * Written by Lorenz Quack 2010 adapted from + * the SHA-256 implementation by Tom St Denis -- http://tomstdenis.home.dhs.org + * which was adapted for PyCrypto by Jeethu Rao, Taylor Boon, and others. + * + * =================================================================== + * The contents of this file are dedicated to the public domain. To + * the extent that dedication to the public domain is not available, + * everyone is granted a worldwide, perpetual, royalty-free, + * non-exclusive license to exercise all rights associated with the + * contents of this file for any purpose whatsoever. + * No rights are reserved. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * =================================================================== + * + */ +#include "Python.h" +#define MODULE_NAME SHA512 +#define DIGEST_SIZE 64 + + +typedef unsigned char U8; +#ifdef __alpha__ +typedef unsigned int U32; +#elif defined(__amd64__) +#include +typedef uint32_t U32; +#ifdef INT64_MAX +typedef uint64_t U64; +#else +#error No 64-bit integer type +#endif +#else +typedef unsigned int U32; +#endif + + + +typedef struct { + U64 state[8], curlen; + U64 length_upper, length_lower; + unsigned char buf[128]; +} +hash_state; + +static const U64 K[80] = { + 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, + 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, + 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, + 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694, + 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, + 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, + 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, + 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70, + 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, + 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, + 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, + 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, + 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, + 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, + 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, + 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, + 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, + 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b, + 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, + 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 +}; + +/* Various logical functions */ +#define Ch(x,y,z) ((x & y) ^ (~x & z)) +#define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z)) +#define R(x, n) (((x)>>((n)&63))|((x)<<(64-((n)&63)))) +#define S(x, n) ((x)>>(n)) +#define Sigma0(x) (R(x, 28) ^ R(x, 34) ^ R(x, 39)) +#define Sigma1(x) (R(x, 14) ^ R(x, 18) ^ R(x, 41)) +#define Gamma0(x) (R(x, 1) ^ R(x, 8) ^ S(x, 7)) +#define Gamma1(x) (R(x, 19) ^ R(x, 61) ^ S(x, 6)) + +/* compress 1024-bits */ +static void sha_compress(hash_state * md) +{ + U64 S[8], W[80], T1, T2; + int i; + + /* copy state into S */ + for (i = 0; i < 8; i++) + S[i] = md->state[i]; + + /* copy the state into 1024-bits into W[0..15] */ + for (i = 0; i < 16; i++){ + W[i] = ((((U64) md->buf[(8 * i) + 0]) << 56) | + (((U64) md->buf[(8 * i) + 1]) << 48) | + (((U64) md->buf[(8 * i) + 2]) << 40) | + (((U64) md->buf[(8 * i) + 3]) << 32) | + (((U64) md->buf[(8 * i) + 4]) << 24) | + (((U64) md->buf[(8 * i) + 5]) << 16) | + (((U64) md->buf[(8 * i) + 6]) << 8) | + (((U64) md->buf[(8 * i) + 7]))); + } + /* fill W[16..79] */ + for (i = 16; i < 80; i++) + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + + /* Compress */ + for (i = 0; i < 80; i++) { + T1 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i]; + T2 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]); + S[7] = S[6]; + S[6] = S[5]; + S[5] = S[4]; + S[4] = S[3] + T1; + S[3] = S[2]; + S[2] = S[1]; + S[1] = S[0]; + S[0] = T1 + T2; + } + + /* feedback */ + for (i = 0; i < 8; i++) + md->state[i] += S[i]; +} + +/* init the SHA state */ +static void sha_init(hash_state * md) +{ + md->curlen = md->length_upper = md->length_lower = 0; + md->state[0] = 0x6a09e667f3bcc908; + md->state[1] = 0xbb67ae8584caa73b; + md->state[2] = 0x3c6ef372fe94f82b; + md->state[3] = 0xa54ff53a5f1d36f1; + md->state[4] = 0x510e527fade682d1; + md->state[5] = 0x9b05688c2b3e6c1f; + md->state[6] = 0x1f83d9abfb41bd6b; + md->state[7] = 0x5be0cd19137e2179; +} + +static void sha_process(hash_state * md, unsigned char *buf, int len) +{ + while (len--) { + /* copy byte */ + md->buf[md->curlen++] = *buf++; + + /* is 128 bytes full? */ + if (md->curlen == 128) { + U64 orig_length; + sha_compress(md); + orig_length = md->length_lower; + md->length_lower += 1024; + if (orig_length > md->length_lower) { + md->length_upper++; + } + md->curlen = 0; + } + } +} + +static void sha_done(hash_state * md, unsigned char *hash) +{ + int i; + U64 orig_length; + + /* increase the length of the message */ + orig_length = md->length_lower; + md->length_lower += md->curlen * 8; + if (orig_length > md->length_lower) { + md->length_upper++; + } + + /* append the '1' bit */ + md->buf[md->curlen++] = 0x80; + + /* if the length is currently above 112 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->curlen > 112) { + for (; md->curlen < 128;) + md->buf[md->curlen++] = 0; + sha_compress(md); + md->curlen = 0; + } + + /* pad upto 112 bytes of zeroes */ + for (; md->curlen < 112;) + md->buf[md->curlen++] = 0; + + /* append length */ + for (i = 112; i < 120; i++) + md->buf[i] = (md->length_upper >> ((119 - i) * 8)) & 0xFF; + for (i = 120; i < 128; i++) + md->buf[i] = (md->length_lower >> ((127 - i) * 8)) & 0xFF; + sha_compress(md); + + /* copy output */ + for (i = 0; i < 64; i++) + hash[i] = (md->state[i / 8] >> (((7 - i) & 7) * 8)) & 0xFF; +} + +// Done +static void hash_init (hash_state *ptr) +{ + sha_init(ptr); +} + +// Done +static void +hash_update (hash_state *self, const U8 *buf, int len) +{ + sha_process(self,(unsigned char *)buf, len); +} + +// Done +static void +hash_copy(hash_state *src, hash_state *dest) +{ + memcpy(dest,src,sizeof(hash_state)); +} + +// Done +static PyObject * +hash_digest (const hash_state *self) +{ + unsigned char digest[64]; + hash_state temp; + + hash_copy((hash_state*)self,&temp); + sha_done(&temp,digest); + return PyString_FromStringAndSize((char *)digest, 64); +} + +#include "hash_template.c"