/*
	PIP - Platform Independent Primitives
	Digest algorithms
	Ivan Pelipenko peri4ko@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pidigest_sha2_p.h"

const uint32_t SHA2::initial_224[8] =
	{0xC1059ED8u, 0x367CD507u, 0x3070DD17u, 0xF70E5939u, 0xFFC00B31u, 0x68581511u, 0x64F98FA7u, 0xBEFA4FA4u};
const uint32_t SHA2::initial_256[8] =
	{0x6A09E667u, 0xBB67AE85u, 0x3C6EF372u, 0xA54FF53Au, 0x510E527Fu, 0x9B05688Cu, 0x1F83D9ABu, 0x5BE0CD19u};

const uint64_t SHA2::initial_384[8]     = {0xCBBB9D5DC1059ED8U,
										   0x629A292A367CD507U,
										   0x9159015A3070DD17U,
										   0x152FECD8F70E5939U,
										   0x67332667FFC00B31U,
										   0x8EB44A8768581511U,
										   0xDB0C2E0D64F98FA7U,
										   0x47B5481DBEFA4FA4U};
const uint64_t SHA2::initial_512[8]     = {0X6A09E667F3BCC908U,
										   0XBB67AE8584CAA73BU,
										   0X3C6EF372FE94F82BU,
										   0XA54FF53A5F1D36F1U,
										   0X510E527FADE682D1U,
										   0X9B05688C2B3E6C1FU,
										   0X1F83D9ABFB41BD6BU,
										   0X5BE0CD19137E2179U};
const uint64_t SHA2::initial_512_256[8] = {0x22312194FC2BF72CU,
										   0x9F555FA3C84C64C2U,
										   0x2393B86B6F53B151U,
										   0x963877195940EABDU,
										   0x96283EE2A88EFFE3U,
										   0xBE5E1E2553863992U,
										   0x2B0199FC2C85B8AAU,
										   0x0EB72DDC81C52CA2U};
const uint64_t SHA2::initial_512_224[8] = {0x8C3D37C819544DA2U,
										   0x73E1996689DCD4D6U,
										   0x1DFAB7AE32FF9C82U,
										   0x679DD514582F9FCFU,
										   0x0F6D2B697BD44DA8U,
										   0x77E36F7304C48942U,
										   0x3F9D85A86A1D36C8U,
										   0x1112E6AD91D692A1U};

template<typename T>
inline T rotate_u(T v, int bits) {
	return (v >> bits) | (v << ((sizeof(T) * 8) - bits));
}
template<typename T>
inline T shift_u(T v, int bits) {
	return v >> bits;
}

PIByteArray SHA2::sha2xx(const PIByteArray & in, const uint32_t * initial, int out_bytes) {
	constexpr int part_size             = 64;
	constexpr int rounds                = 64;

	static constexpr uint32_t k[rounds] = {
		0x428A2F98u, 0x71374491u, 0xB5C0FBCFu, 0xE9B5DBA5u, 0x3956C25Bu, 0x59F111F1u, 0x923F82A4u, 0xAB1C5ED5u, 0xD807AA98u, 0x12835B01u,
		0x243185BEu, 0x550C7DC3u, 0x72BE5D74u, 0x80DEB1FEu, 0x9BDC06A7u, 0xC19BF174u, 0xE49B69C1u, 0xEFBE4786u, 0x0FC19DC6u, 0x240CA1CCu,
		0x2DE92C6Fu, 0x4A7484AAu, 0x5CB0A9DCu, 0x76F988DAu, 0x983E5152u, 0xA831C66Du, 0xB00327C8u, 0xBF597FC7u, 0xC6E00BF3u, 0xD5A79147u,
		0x06CA6351u, 0x14292967u, 0x27B70A85u, 0x2E1B2138u, 0x4D2C6DFCu, 0x53380D13u, 0x650A7354u, 0x766A0ABBu, 0x81C2C92Eu, 0x92722C85u,
		0xA2BFE8A1u, 0xA81A664Bu, 0xC24B8B70u, 0xC76C51A3u, 0xD192E819u, 0xD6990624u, 0xF40E3585u, 0x106AA070u, 0x19A4C116u, 0x1E376C08u,
		0x2748774Cu, 0x34B0BCB5u, 0x391C0CB3u, 0x4ED8AA4Au, 0x5B9CCA4Fu, 0x682E6FF3u, 0x748F82EEu, 0x78A5636Fu, 0x84C87814u, 0x8CC70208u,
		0x90BEFFFAu, 0xA4506CEBu, 0xBEF9A3F7u, 0xC67178F2u};

	uint32_t h0     = initial[0];
	uint32_t h1     = initial[1];
	uint32_t h2     = initial[2];
	uint32_t h3     = initial[3];
	uint32_t h4     = initial[4];
	uint32_t h5     = initial[5];
	uint32_t h6     = initial[6];
	uint32_t h7     = initial[7];

	uint64_t l      = in.size() * 8;
	PIByteArray msg = in;
	int add_l       = (part_size - 8) - (msg.size() % (part_size));
	if (add_l <= 0) add_l += part_size;
	msg.append(0x80);
	for (int i = 1; i < add_l; ++i)
		msg.append(0x0);
	msg << piChangedEndian(l);
	int parts = msg.size_s() / part_size;
	// piCout << l << "add" << add_l << "=" << (in.size() + add_l + sizeof(l)) << msg.size() << parts;
	uint32_t w[64];
	for (int p = 0; p < parts; ++p) {
		uint32_t * mw = (uint32_t *)msg.data(p * part_size);
		for (int i = 0; i < 16; ++i)
			w[i] = piChangedEndian(mw[i]);
		for (int i = 16; i < 64; ++i) {
			uint32_t s0 = rotate_u(w[i - 15], 7) ^ rotate_u(w[i - 15], 18) ^ shift_u(w[i - 15], 3);
			uint32_t s1 = rotate_u(w[i - 2], 17) ^ rotate_u(w[i - 2], 19) ^ shift_u(w[i - 2], 10);
			w[i]        = w[i - 16] + s0 + w[i - 7] + s1;
		}
		uint32_t a = h0;
		uint32_t b = h1;
		uint32_t c = h2;
		uint32_t d = h3;
		uint32_t e = h4;
		uint32_t f = h5;
		uint32_t g = h6;
		uint32_t h = h7;
		for (int i = 0; i < rounds; ++i) {
			uint32_t S1  = rotate_u(e, 6) ^ rotate_u(e, 11) ^ rotate_u(e, 25);
			uint32_t ch  = (e & f) ^ (~e & g);
			uint32_t t1  = h + S1 + ch + k[i] + w[i];
			uint32_t S0  = rotate_u(a, 2) ^ rotate_u(a, 13) ^ rotate_u(a, 22);
			uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
			uint32_t t2  = S0 + maj;

			h            = g;
			g            = f;
			f            = e;
			e            = d + t1;
			d            = c;
			c            = b;
			b            = a;
			a            = t1 + t2;
		}
		h0 += a;
		h1 += b;
		h2 += c;
		h3 += d;
		h4 += e;
		h5 += f;
		h6 += g;
		h7 += h;
	}
	PIByteArray ret;
	ret << piChangedEndian(h0) << piChangedEndian(h1) << piChangedEndian(h2) << piChangedEndian(h3) << piChangedEndian(h4)
		<< piChangedEndian(h5) << piChangedEndian(h6) << piChangedEndian(h7);
	ret.resize(out_bytes);
	return ret;
}

PIByteArray SHA2::sha5xx(const PIByteArray & in, const uint64_t * initial, int out_bytes) {
	constexpr int part_size             = 128;
	constexpr int rounds                = 80;

	static constexpr uint64_t k[rounds] = {
		0X428A2F98D728AE22U, 0X7137449123EF65CDU, 0XB5C0FBCFEC4D3B2FU, 0XE9B5DBA58189DBBCU, 0X3956C25BF348B538U, 0X59F111F1B605D019U,
		0X923F82A4AF194F9BU, 0XAB1C5ED5DA6D8118U, 0XD807AA98A3030242U, 0X12835B0145706FBEU, 0X243185BE4EE4B28CU, 0X550C7DC3D5FFB4E2U,
		0X72BE5D74F27B896FU, 0X80DEB1FE3B1696B1U, 0X9BDC06A725C71235U, 0XC19BF174CF692694U, 0XE49B69C19EF14AD2U, 0XEFBE4786384F25E3U,
		0X0FC19DC68B8CD5B5U, 0X240CA1CC77AC9C65U, 0X2DE92C6F592B0275U, 0X4A7484AA6EA6E483U, 0X5CB0A9DCBD41FBD4U, 0X76F988DA831153B5U,
		0X983E5152EE66DFABU, 0XA831C66D2DB43210U, 0XB00327C898FB213FU, 0XBF597FC7BEEF0EE4U, 0XC6E00BF33DA88FC2U, 0XD5A79147930AA725U,
		0X06CA6351E003826FU, 0X142929670A0E6E70U, 0X27B70A8546D22FFCU, 0X2E1B21385C26C926U, 0X4D2C6DFC5AC42AEDU, 0X53380D139D95B3DFU,
		0X650A73548BAF63DEU, 0X766A0ABB3C77B2A8U, 0X81C2C92E47EDAEE6U, 0X92722C851482353BU, 0XA2BFE8A14CF10364U, 0XA81A664BBC423001U,
		0XC24B8B70D0F89791U, 0XC76C51A30654BE30U, 0XD192E819D6EF5218U, 0XD69906245565A910U, 0XF40E35855771202AU, 0X106AA07032BBD1B8U,
		0X19A4C116B8D2D0C8U, 0X1E376C085141AB53U, 0X2748774CDF8EEB99U, 0X34B0BCB5E19B48A8U, 0X391C0CB3C5C95A63U, 0X4ED8AA4AE3418ACBU,
		0X5B9CCA4F7763E373U, 0X682E6FF3D6B2B8A3U, 0X748F82EE5DEFB2FCU, 0X78A5636F43172F60U, 0X84C87814A1F0AB72U, 0X8CC702081A6439ECU,
		0X90BEFFFA23631E28U, 0XA4506CEBDE82BDE9U, 0XBEF9A3F7B2C67915U, 0XC67178F2E372532BU, 0XCA273ECEEA26619CU, 0XD186B8C721C0C207U,
		0XEADA7DD6CDE0EB1EU, 0XF57D4F7FEE6ED178U, 0X06F067AA72176FBAU, 0X0A637DC5A2C898A6U, 0X113F9804BEF90DAEU, 0X1B710B35131C471BU,
		0X28DB77F523047D84U, 0X32CAAB7B40C72493U, 0X3C9EBE0A15C9BEBCU, 0X431D67C49C100D4CU, 0X4CC5D4BECB3E42B6U, 0X597F299CFC657E2AU,
		0X5FCB6FAB3AD6FAECU, 0X6C44198C4A475817U};

	uint64_t h0     = initial[0];
	uint64_t h1     = initial[1];
	uint64_t h2     = initial[2];
	uint64_t h3     = initial[3];
	uint64_t h4     = initial[4];
	uint64_t h5     = initial[5];
	uint64_t h6     = initial[6];
	uint64_t h7     = initial[7];

	uint64_t l      = in.size() * 8;
	PIByteArray msg = in;
	int add_l       = (part_size - 8) - (msg.size() % (part_size));
	if (add_l <= 0) add_l += part_size;
	msg.append(0x80);
	for (int i = 1; i < add_l; ++i)
		msg.append(0x0);
	msg << piChangedEndian(l);
	int parts = msg.size_s() / part_size;
	// piCout << l << "add" << add_l << "=" << (in.size() + add_l + sizeof(l)) << msg.size() << parts;
	uint64_t w[80];
	for (int p = 0; p < parts; ++p) {
		uint64_t * mw = (uint64_t *)msg.data(p * part_size);
		for (int i = 0; i < 16; ++i)
			w[i] = piChangedEndian(mw[i]);
		for (int i = 16; i < 80; ++i) {
			uint64_t s0 = rotate_u(w[i - 15], 1) ^ rotate_u(w[i - 15], 8) ^ shift_u(w[i - 15], 7);
			uint64_t s1 = rotate_u(w[i - 2], 19) ^ rotate_u(w[i - 2], 61) ^ shift_u(w[i - 2], 6);
			w[i]        = w[i - 16] + s0 + w[i - 7] + s1;
		}
		uint64_t a = h0;
		uint64_t b = h1;
		uint64_t c = h2;
		uint64_t d = h3;
		uint64_t e = h4;
		uint64_t f = h5;
		uint64_t g = h6;
		uint64_t h = h7;
		for (int i = 0; i < rounds; ++i) {
			uint64_t S1  = rotate_u(e, 14) ^ rotate_u(e, 18) ^ rotate_u(e, 41);
			uint64_t ch  = (e & f) ^ (~e & g);
			uint64_t t1  = h + S1 + ch + k[i] + w[i];
			uint64_t S0  = rotate_u(a, 28) ^ rotate_u(a, 34) ^ rotate_u(a, 39);
			uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
			uint64_t t2  = S0 + maj;

			h            = g;
			g            = f;
			f            = e;
			e            = d + t1;
			d            = c;
			c            = b;
			b            = a;
			a            = t1 + t2;
		}
		h0 += a;
		h1 += b;
		h2 += c;
		h3 += d;
		h4 += e;
		h5 += f;
		h6 += g;
		h7 += h;
	}
	PIByteArray ret;
	ret << piChangedEndian(h0) << piChangedEndian(h1) << piChangedEndian(h2) << piChangedEndian(h3) << piChangedEndian(h4)
		<< piChangedEndian(h5) << piChangedEndian(h6) << piChangedEndian(h7);
	ret.resize(out_bytes);
	return ret;
}
