X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/6e773413c0c0d4bb52b7a9af4c23ab83e26aa26b..87804cc1f325a3eb5a562b708deb0293402e1f8f:/src/src/hash.c diff --git a/src/src/hash.c b/src/src/hash.c index c2be85d17..f1a6c4096 100644 --- a/src/src/hash.c +++ b/src/src/hash.c @@ -1,8 +1,8 @@ /* * Exim - an Internet mail transport agent * - * Copyright (C) 2016 Exim maintainers - * Copyright (c) University of Cambridge 1995 - 2016 + * Copyright (C) 2010 - 2018 Exim maintainers + * Copyright (c) University of Cambridge 1995 - 2009 * * Hash interface functions */ @@ -25,20 +25,37 @@ typedef struct sha1 { sha1; #endif /*STAND_ALONE*/ - +#include /******************************************************************************/ #ifdef SHA_OPENSSL -void +BOOL exim_sha_init(hctx * h, hashmethod m) { switch (h->method = m) { - case HASH_SHA1: h->hashlen = 20; SHA1_Init (&h->u.sha1); break; - case HASH_SHA256: h->hashlen = 32; SHA256_Init(&h->u.sha2); break; - default: h->hashlen = 0; break; + case HASH_SHA1: h->hashlen = 20; SHA1_Init (&h->u.sha1); break; + case HASH_SHA2_256: h->hashlen = 32; SHA256_Init(&h->u.sha2_256); break; + case HASH_SHA2_384: h->hashlen = 48; SHA384_Init(&h->u.sha2_512); break; + case HASH_SHA2_512: h->hashlen = 64; SHA512_Init(&h->u.sha2_512); break; +#ifdef EXIM_HAVE_SHA3 + case HASH_SHA3_224: h->hashlen = 28; + EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_224()); + break; + case HASH_SHA3_256: h->hashlen = 32; + EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_256()); + break; + case HASH_SHA3_384: h->hashlen = 48; + EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_384()); + break; + case HASH_SHA3_512: h->hashlen = 64; + EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_512()); + break; +#endif + default: h->hashlen = 0; return FALSE; } +return TRUE; } @@ -47,8 +64,19 @@ exim_sha_update(hctx * h, const uschar * data, int len) { switch (h->method) { - case HASH_SHA1: SHA1_Update (&h->u.sha1, data, len); break; - case HASH_SHA256: SHA256_Update(&h->u.sha2, data, len); break; + case HASH_SHA1: SHA1_Update (&h->u.sha1, data, len); break; + case HASH_SHA2_256: SHA256_Update(&h->u.sha2_256, data, len); break; + case HASH_SHA2_384: SHA384_Update(&h->u.sha2_512, data, len); break; + case HASH_SHA2_512: SHA512_Update(&h->u.sha2_512, data, len); break; +#ifdef EXIM_HAVE_SHA3 + case HASH_SHA3_224: + case HASH_SHA3_256: + case HASH_SHA3_384: + case HASH_SHA3_512: EVP_DigestUpdate(h->u.mctx, data, len); break; +#endif + /* should be blocked by init not handling these, but be explicit to + guard against accidents later (and hush up clang -Wswitch) */ + default: assert(0); } } @@ -56,11 +84,21 @@ switch (h->method) void exim_sha_finish(hctx * h, blob * b) { -b->data = store_get(b->len = h->hashlen); +/* Hashing is sufficient to purify any tainted input */ +b->data = store_get(b->len = h->hashlen, FALSE); switch (h->method) { - case HASH_SHA1: SHA1_Final (b->data, &h->u.sha1); break; - case HASH_SHA256: SHA256_Final(b->data, &h->u.sha2); break; + case HASH_SHA1: SHA1_Final (b->data, &h->u.sha1); break; + case HASH_SHA2_256: SHA256_Final(b->data, &h->u.sha2_256); break; + case HASH_SHA2_384: SHA384_Final(b->data, &h->u.sha2_512); break; + case HASH_SHA2_512: SHA512_Final(b->data, &h->u.sha2_512); break; +#ifdef EXIM_HAVE_SHA3 + case HASH_SHA3_224: + case HASH_SHA3_256: + case HASH_SHA3_384: + case HASH_SHA3_512: EVP_DigestFinal(h->u.mctx, b->data, NULL); break; +#endif + default: assert(0); } } @@ -69,18 +107,24 @@ switch (h->method) #elif defined(SHA_GNUTLS) /******************************************************************************/ -void +BOOL exim_sha_init(hctx * h, hashmethod m) { switch (h->method = m) { - case HASH_SHA1: h->hashlen = 20; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA1); break; - case HASH_SHA256: h->hashlen = 32; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA256); break; + case HASH_SHA1: h->hashlen = 20; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA1); break; + case HASH_SHA2_256: h->hashlen = 32; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA256); break; + case HASH_SHA2_384: h->hashlen = 48; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA384); break; + case HASH_SHA2_512: h->hashlen = 64; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA512); break; #ifdef EXIM_HAVE_SHA3 + case HASH_SHA3_224: h->hashlen = 28; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_224); break; case HASH_SHA3_256: h->hashlen = 32; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_256); break; + case HASH_SHA3_384: h->hashlen = 48; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_384); break; + case HASH_SHA3_512: h->hashlen = 64; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_512); break; #endif - default: h->hashlen = 0; break; + default: h->hashlen = 0; return FALSE; } +return TRUE; } @@ -94,7 +138,7 @@ gnutls_hash(h->sha, data, len); void exim_sha_finish(hctx * h, blob * b) { -b->data = store_get(b->len = h->hashlen); +b->data = store_get(b->len = h->hashlen, FALSE); gnutls_hash_output(h->sha, b->data); } @@ -103,15 +147,21 @@ gnutls_hash_output(h->sha, b->data); #elif defined(SHA_GCRYPT) /******************************************************************************/ -void +BOOL exim_sha_init(hctx * h, hashmethod m) { switch (h->method = m) { - case HASH_SHA1: h->hashlen = 20; gcry_md_open(&h->sha, GCRY_MD_SHA1, 0); break; - case HASH_SHA256: h->hashlen = 32; gcry_md_open(&h->sha, GCRY_MD_SHA256, 0); break; - default: h->hashlen = 0; break; + case HASH_SHA1: h->hashlen = 20; gcry_md_open(&h->sha, GCRY_MD_SHA1, 0); break; + case HASH_SHA2_256: h->hashlen = 32; gcry_md_open(&h->sha, GCRY_MD_SHA256, 0); break; + case HASH_SHA2_384: h->hashlen = 48; gcry_md_open(&h->sha, GCRY_MD_SHA384, 0); break; + case HASH_SHA2_512: h->hashlen = 64; gcry_md_open(&h->sha, GCRY_MD_SHA512, 0); break; + case HASH_SHA3_256: h->hashlen = 32; gcry_md_open(&h->sha, GCRY_MD_SHA3_256, 0); break; + case HASH_SHA3_384: h->hashlen = 48; gcry_md_open(&h->sha, GCRY_MD_SHA3_384, 0); break; + case HASH_SHA3_512: h->hashlen = 64; gcry_md_open(&h->sha, GCRY_MD_SHA3_512, 0); break; + default: h->hashlen = 0; return FALSE; } +return TRUE; } @@ -125,7 +175,7 @@ gcry_md_write(h->sha, data, len); void exim_sha_finish(hctx * h, blob * b) { -b->data = store_get(b->len = h->hashlen); +b->data = store_get(b->len = h->hashlen, FALSE); memcpy(b->data, gcry_md_read(h->sha, 0), h->hashlen); } @@ -135,15 +185,17 @@ memcpy(b->data, gcry_md_read(h->sha, 0), h->hashlen); #elif defined(SHA_POLARSSL) /******************************************************************************/ -void +BOOL exim_sha_init(hctx * h, hashmethod m) { +/*XXX extend for sha512 */ switch (h->method = m) { case HASH_SHA1: h->hashlen = 20; sha1_starts(&h->u.sha1); break; - case HASH_SHA256: h->hashlen = 32; sha2_starts(&h->u.sha2, 0); break; - default: h->hashlen = 0; break; + case HASH_SHA2_256: h->hashlen = 32; sha2_starts(&h->u.sha2, 0); break; + default: h->hashlen = 0; return FALSE; } +return TRUE; } @@ -153,7 +205,7 @@ exim_sha_update(hctx * h, const uschar * data, int len) switch (h->method) { case HASH_SHA1: sha1_update(h->u.sha1, US data, len); break; - case HASH_SHA256: sha2_update(h->u.sha2, US data, len); break; + case HASH_SHA2_256: sha2_update(h->u.sha2, US data, len); break; } } @@ -161,11 +213,11 @@ switch (h->method) void exim_sha_finish(hctx * h, blob * b) { -b->data = store_get(b->len = h->hashlen); +b->data = store_get(b->len = h->hashlen, FALSE); switch (h->method) { case HASH_SHA1: sha1_finish(h->u.sha1, b->data); break; - case HASH_SHA256: sha2_finish(h->u.sha2, b->data); break; + case HASH_SHA2_256: sha2_finish(h->u.sha2, b->data); break; } } @@ -214,19 +266,18 @@ Returns: nothing static void native_sha1_mid(sha1 *base, const uschar *text) { -int i; uint A, B, C, D, E; uint W[80]; base->length += 64; -for (i = 0; i < 16; i++) +for (int i = 0; i < 16; i++) { W[i] = ((uint)text[0] << 24) | (text[1] << 16) | (text[2] << 8) | text[3]; text += 4; } -for (i = 16; i < 80; i++) +for (int i = 16; i < 80; i++) { register unsigned int x = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16]; W[i] = (x << 1) | (x >> 31); @@ -238,7 +289,7 @@ C = base->H[2]; D = base->H[3]; E = base->H[4]; -for (i = 0; i < 20; i++) +for (int i = 0; i < 20; i++) { unsigned int T; T = ((A << 5) | (A >> 27)) + ((B & C) | ((~B) & D)) + E + W[i] + 0x5a827999; @@ -249,7 +300,7 @@ for (i = 0; i < 20; i++) A = T; } -for (i = 20; i < 40; i++) +for (int i = 20; i < 40; i++) { unsigned int T; T = ((A << 5) | (A >> 27)) + (B ^ C ^ D) + E + W[i] + 0x6ed9eba1; @@ -260,7 +311,7 @@ for (i = 20; i < 40; i++) A = T; } -for (i = 40; i < 60; i++) +for (int i = 40; i < 60; i++) { unsigned int T; T = ((A << 5) | (A >> 27)) + ((B & C) | (B & D) | (C & D)) + E + W[i] + @@ -272,7 +323,7 @@ for (i = 40; i < 60; i++) A = T; } -for (i = 60; i < 80; i++) +for (int i = 60; i < 80; i++) { unsigned int T; T = ((A << 5) | (A >> 27)) + (B ^ C ^ D) + E + W[i] + 0xca62c1d6; @@ -313,7 +364,6 @@ Returns: nothing static void native_sha1_end(sha1 *base, const uschar *text, int length, uschar *digest) { -int i; uschar work[64]; /* Process in chunks of 64 until we have less than 64 bytes left. */ @@ -366,7 +416,7 @@ native_sha1_mid(base, work); /* Pass back the result, high-order byte first in each word. */ -for (i = 0; i < 5; i++) +for (int i = 0; i < 5; i++) { register int x = base->H[i]; *digest++ = (x >> 24) & 0xff; @@ -382,11 +432,12 @@ for (i = 0; i < 5; i++) # ifdef notdef -void +BOOL exim_sha_init(hctx * h, hashmethod m) { h->hashlen = 20; native_sha1_start(&h->sha1); +return TRUE; } @@ -400,7 +451,7 @@ native_sha1_mid(&h->sha1, US data); /* implicit size always 64 */ void exim_sha_finish(hctx * h, blob * b) { -b->data = store_get(b->len = h->hashlen); +b->data = store_get(b->len = h->hashlen, FALSE); native_sha1_end(&h->sha1, NULL, 0, b->data); } @@ -408,16 +459,6 @@ native_sha1_end(&h->sha1, NULL, 0, b->data); #endif -/******************************************************************************/ - -/* Common to all library versions */ -int -exim_sha_hashlen(hctx * h) -{ -return h->method == HASH_SHA1 ? 20 - : h->method == HASH_SHA256 ? 32 - : 0; -} /******************************************************************************/ @@ -452,7 +493,7 @@ native_sha1_end(&h->sha1, data, len, digest); void sha1_start(hctx * h) { -exim_sha_init(h, HASH_SHA1); +(void) exim_sha_init(h, HASH_SHA1); } void @@ -767,7 +808,7 @@ int main(void) sha1 base; int j; int i = 0x01020304; -uschar *ctest = (uschar *)(&i); +uschar *ctest = US (&i); uschar buffer[256]; uschar digest[20]; uschar s[41];