Exemple #1
0
static void
hamsi_small_close(sph_hamsi_small_context *sc,
	unsigned ub, unsigned n, void *dst, size_t out_size_w32)
{
	unsigned char pad[12];
	size_t ptr, u;
	unsigned z;
	unsigned char *out;

	ptr = sc->partial_len;
	memcpy(pad, sc->partial, ptr);
#if SPH_64
	sph_enc64be(pad + 4, sc->count + (ptr << 3) + n);
#else
	sph_enc32be(pad + 4, sc->count_high);
	sph_enc32be(pad + 8, sc->count_low + (ptr << 3) + n);
#endif
	z = 0x80 >> n;
	pad[ptr ++] = ((ub & -z) | z) & 0xFF;
	while (ptr < 4)
		pad[ptr ++] = 0;
	hamsi_small(sc, pad, 2);
	hamsi_small_final(sc, pad + 8);
	out = dst;
	for (u = 0; u < out_size_w32; u ++)
		sph_enc32be(out + (u << 2), sc->h[u]);
}
Exemple #2
0
static void
luffa3_close(sph_luffa224_context *sc, unsigned ub, unsigned n,
	void *dst, unsigned out_size_w32)
{
	unsigned char *buf, *out;
	size_t ptr;
	unsigned z;
	int i;
	DECL_STATE3

	buf = sc->buf;
	ptr = sc->ptr;
	z = 0x80 >> n;
	buf[ptr ++] = ((ub & -z) | z) & 0xFF;
	memset(buf + ptr, 0, (sizeof sc->buf) - ptr);
	READ_STATE3(sc);
	for (i = 0; i < 2; i ++) {
		MI3;
		P3;
		memset(buf, 0, sizeof sc->buf);
	}
	out = dst;
	sph_enc32be(out +  0, V00 ^ V10 ^ V20);
	sph_enc32be(out +  4, V01 ^ V11 ^ V21);
	sph_enc32be(out +  8, V02 ^ V12 ^ V22);
	sph_enc32be(out + 12, V03 ^ V13 ^ V23);
	sph_enc32be(out + 16, V04 ^ V14 ^ V24);
	sph_enc32be(out + 20, V05 ^ V15 ^ V25);
	sph_enc32be(out + 24, V06 ^ V16 ^ V26);
	if (out_size_w32 > 7)
		sph_enc32be(out + 28, V07 ^ V17 ^ V27);
}
Exemple #3
0
static void
jh_close(sph_jh_context *sc, unsigned ub, unsigned n,
	void *dst, size_t out_size_w32, const void *iv)
{
	unsigned z;
	unsigned char buf[128];
	size_t numz, u;
#if SPH_64
	sph_u64 l0, l1;
#else
	sph_u32 l0, l1, l2, l3;
#endif

	z = 0x80 >> n;
	buf[0] = ((ub & -z) | z) & 0xFF;
	if (sc->ptr == 0 && n == 0) {
		numz = 47;
	} else {
		numz = 111 - sc->ptr;
	}
	memset(buf + 1, 0, numz);
#if SPH_64
	l0 = SPH_T64(sc->block_count << 9) + (sc->ptr << 3) + n;
	l1 = SPH_T64(sc->block_count >> 55);
	sph_enc64be(buf + numz + 1, l1);
	sph_enc64be(buf + numz + 9, l0);
#else
	l0 = SPH_T32(sc->block_count_low << 9) + (sc->ptr << 3) + n;
	l1 = SPH_T32(sc->block_count_low >> 23)
		+ SPH_T32(sc->block_count_high << 9);
	l2 = SPH_T32(sc->block_count_high >> 23);
	l3 = 0;
	sph_enc32be(buf + numz +  1, l3);
	sph_enc32be(buf + numz +  5, l2);
	sph_enc32be(buf + numz +  9, l1);
	sph_enc32be(buf + numz + 13, l0);
#endif
	jh_core(sc, buf, numz + 17);
#if SPH_JH_64
	for (u = 0; u < 8; u ++)
		enc64e(buf + (u << 3), sc->H.wide[u + 8]);
#else
	for (u = 0; u < 16; u ++)
		enc32e(buf + (u << 2), sc->H.narrow[u + 16]);
#endif
	memcpy(dst, buf + ((16 - out_size_w32) << 2), out_size_w32 << 2);
	jh_init(sc, iv);
}
static void
blake32_close(sph_blake_small_context *sc,
	unsigned ub, unsigned n, void *dst, size_t out_size_w32)
{
	union {
		unsigned char buf[64];
		sph_u32 dummy;
	} u;
	size_t ptr, k;
	unsigned bit_len;
	unsigned z;
	sph_u32 th, tl;
	unsigned char *out;

	ptr = sc->ptr;
	bit_len = ((unsigned)ptr << 3) + n;
	z = 0x80 >> n;
	u.buf[ptr] = ((ub & -z) | z) & 0xFF;
	tl = sc->T0 + bit_len;
	th = sc->T1;
	if (ptr == 0 && n == 0) {
		sc->T0 = SPH_C32(0xFFFFFE00);
		sc->T1 = SPH_C32(0xFFFFFFFF);
	} else if (sc->T0 == 0) {
		sc->T0 = SPH_C32(0xFFFFFE00) + bit_len;
		sc->T1 = SPH_T32(sc->T1 - 1);
	} else {
		sc->T0 -= 512 - bit_len;
	}
	if (bit_len <= 446) {
		memset(u.buf + ptr + 1, 0, 55 - ptr);
		if (out_size_w32 == 8)
			u.buf[55] |= 1;
		sph_enc32be_aligned(u.buf + 56, th);
		sph_enc32be_aligned(u.buf + 60, tl);
		blake32(sc, u.buf + ptr, 64 - ptr);
	} else {
		memset(u.buf + ptr + 1, 0, 63 - ptr);
		blake32(sc, u.buf + ptr, 64 - ptr);
		sc->T0 = SPH_C32(0xFFFFFE00);
		sc->T1 = SPH_C32(0xFFFFFFFF);
		memset(u.buf, 0, 56);
		if (out_size_w32 == 8)
			u.buf[55] = 1;
		sph_enc32be_aligned(u.buf + 56, th);
		sph_enc32be_aligned(u.buf + 60, tl);
		blake32(sc, u.buf, 64);
	}
	out = dst;
	for (k = 0; k < out_size_w32; k ++)
		sph_enc32be(out + (k << 2), sc->H[k]);
}
Exemple #5
0
static void
fugue2_close(sph_fugue_context *sc, unsigned ub, unsigned n,
	void *dst, size_t out_size_w32)
{
	int i;

	CLOSE_ENTRY(30, 6, fugue2_core)
	for (i = 0; i < 10; i ++) {
		ROR(3, 30);
		CMIX30(S[0], S[1], S[2], S[4], S[5], S[6], S[15], S[16], S[17]);
		SMIX(S[0], S[1], S[2], S[3]);
	}
	for (i = 0; i < 13; i ++) {
		S[4] ^= S[0];
		S[15] ^= S[0];
		ROR(15, 30);
		SMIX(S[0], S[1], S[2], S[3]);
		S[4] ^= S[0];
		S[16] ^= S[0];
		ROR(14, 30);
		SMIX(S[0], S[1], S[2], S[3]);
	}
	S[4] ^= S[0];
	S[15] ^= S[0];
	out = dst;
	sph_enc32be(out +  0, S[ 1]);
	sph_enc32be(out +  4, S[ 2]);
	sph_enc32be(out +  8, S[ 3]);
	sph_enc32be(out + 12, S[ 4]);
	sph_enc32be(out + 16, S[15]);
	sph_enc32be(out + 20, S[16]);
	sph_enc32be(out + 24, S[17]);
	if (out_size_w32 == 8) {
		sph_enc32be(out + 28, S[18]);
		sph_fugue256_init(sc);
	} else {
		sph_fugue224_init(sc);
	}
}
Exemple #6
0
static void
hamsi_big_close(sph_hamsi_big_context *sc,
	unsigned ub, unsigned n, void *dst, size_t out_size_w32)
{
	unsigned char pad[8];
	size_t ptr, u;
	unsigned z;
	unsigned char *out;

	ptr = sc->partial_len;
#if SPH_64
	sph_enc64be(pad, sc->count + (ptr << 3) + n);
#else
	sph_enc32be(pad, sc->count_high);
	sph_enc32be(pad + 4, sc->count_low + (ptr << 3) + n);
#endif
	z = 0x80 >> n;
	sc->partial[ptr ++] = ((ub & -z) | z) & 0xFF;
	while (ptr < 8)
		sc->partial[ptr ++] = 0;
	hamsi_big(sc, sc->partial, 1);
	hamsi_big_final(sc, pad);
	out = dst;
	if (out_size_w32 == 12) {
		sph_enc32be(out +  0, sc->h[ 0]);
		sph_enc32be(out +  4, sc->h[ 1]);
		sph_enc32be(out +  8, sc->h[ 3]);
		sph_enc32be(out + 12, sc->h[ 4]);
		sph_enc32be(out + 16, sc->h[ 5]);
		sph_enc32be(out + 20, sc->h[ 6]);
		sph_enc32be(out + 24, sc->h[ 8]);
		sph_enc32be(out + 28, sc->h[ 9]);
		sph_enc32be(out + 32, sc->h[10]);
		sph_enc32be(out + 36, sc->h[12]);
		sph_enc32be(out + 40, sc->h[13]);
		sph_enc32be(out + 44, sc->h[15]);
	} else {
		for (u = 0; u < 16; u ++)
			sph_enc32be(out + (u << 2), sc->h[u]);
	}
}
Exemple #7
0
static void
fugue4_close(sph_fugue_context *sc, unsigned ub, unsigned n, void *dst)
{
	int i;

	CLOSE_ENTRY(36, 12, fugue4_core)
	for (i = 0; i < 32; i ++) {
		ROR(3, 36);
		CMIX36(S[0], S[1], S[2], S[4], S[5], S[6], S[18], S[19], S[20]);
		SMIX(S[0], S[1], S[2], S[3]);
	}
	for (i = 0; i < 13; i ++) {
		S[4] ^= S[0];
		S[9] ^= S[0];
		S[18] ^= S[0];
		S[27] ^= S[0];
		ROR(9, 36);
		SMIX(S[0], S[1], S[2], S[3]);
		S[4] ^= S[0];
		S[10] ^= S[0];
		S[18] ^= S[0];
		S[27] ^= S[0];
		ROR(9, 36);
		SMIX(S[0], S[1], S[2], S[3]);
		S[4] ^= S[0];
		S[10] ^= S[0];
		S[19] ^= S[0];
		S[27] ^= S[0];
		ROR(9, 36);
		SMIX(S[0], S[1], S[2], S[3]);
		S[4] ^= S[0];
		S[10] ^= S[0];
		S[19] ^= S[0];
		S[28] ^= S[0];
		ROR(8, 36);
		SMIX(S[0], S[1], S[2], S[3]);
	}
	S[4] ^= S[0];
	S[9] ^= S[0];
	S[18] ^= S[0];
	S[27] ^= S[0];
	out = dst;
	sph_enc32be(out +  0, S[ 1]);
	sph_enc32be(out +  4, S[ 2]);
	sph_enc32be(out +  8, S[ 3]);
	sph_enc32be(out + 12, S[ 4]);
	sph_enc32be(out + 16, S[ 9]);
	sph_enc32be(out + 20, S[10]);
	sph_enc32be(out + 24, S[11]);
	sph_enc32be(out + 28, S[12]);
	sph_enc32be(out + 32, S[18]);
	sph_enc32be(out + 36, S[19]);
	sph_enc32be(out + 40, S[20]);
	sph_enc32be(out + 44, S[21]);
	sph_enc32be(out + 48, S[27]);
	sph_enc32be(out + 52, S[28]);
	sph_enc32be(out + 56, S[29]);
	sph_enc32be(out + 60, S[30]);
	sph_fugue512_init(sc);
}
Exemple #8
0
static void
fugue3_close(sph_fugue_context *sc, unsigned ub, unsigned n, void *dst)
{
	int i;

	CLOSE_ENTRY(36, 9, fugue3_core)
	for (i = 0; i < 18; i ++) {
		ROR(3, 36);
		CMIX36(S[0], S[1], S[2], S[4], S[5], S[6], S[18], S[19], S[20]);
		SMIX(S[0], S[1], S[2], S[3]);
	}
	for (i = 0; i < 13; i ++) {
		S[4] ^= S[0];
		S[12] ^= S[0];
		S[24] ^= S[0];
		ROR(12, 36);
		SMIX(S[0], S[1], S[2], S[3]);
		S[4] ^= S[0];
		S[13] ^= S[0];
		S[24] ^= S[0];
		ROR(12, 36);
		SMIX(S[0], S[1], S[2], S[3]);
		S[4] ^= S[0];
		S[13] ^= S[0];
		S[25] ^= S[0];
		ROR(11, 36);
		SMIX(S[0], S[1], S[2], S[3]);
	}
	S[4] ^= S[0];
	S[12] ^= S[0];
	S[24] ^= S[0];
	out = dst;
	sph_enc32be(out +  0, S[ 1]);
	sph_enc32be(out +  4, S[ 2]);
	sph_enc32be(out +  8, S[ 3]);
	sph_enc32be(out + 12, S[ 4]);
	sph_enc32be(out + 16, S[12]);
	sph_enc32be(out + 20, S[13]);
	sph_enc32be(out + 24, S[14]);
	sph_enc32be(out + 28, S[15]);
	sph_enc32be(out + 32, S[24]);
	sph_enc32be(out + 36, S[25]);
	sph_enc32be(out + 40, S[26]);
	sph_enc32be(out + 44, S[27]);
	sph_fugue384_init(sc);
}
Exemple #9
0
static void
luffa5_close(sph_luffa512_context *sc, unsigned ub, unsigned n, void *dst)
{
	unsigned char *buf, *out;
	size_t ptr;
	unsigned z;
	int i;
	DECL_STATE5

	buf = sc->buf;
	ptr = sc->ptr;
	out = dst;
	z = 0x80 >> n;
	buf[ptr ++] = ((ub & -z) | z) & 0xFF;
	memset(buf + ptr, 0, (sizeof sc->buf) - ptr);
	READ_STATE5(sc);
	for (i = 0; i < 3; i ++) {
		MI5;
		P5;
		switch (i) {
		case 0:
			memset(buf, 0, sizeof sc->buf);
			break;
		case 1:
			sph_enc32be(out +  0, V00 ^ V10 ^ V20 ^ V30 ^ V40);
			sph_enc32be(out +  4, V01 ^ V11 ^ V21 ^ V31 ^ V41);
			sph_enc32be(out +  8, V02 ^ V12 ^ V22 ^ V32 ^ V42);
			sph_enc32be(out + 12, V03 ^ V13 ^ V23 ^ V33 ^ V43);
			sph_enc32be(out + 16, V04 ^ V14 ^ V24 ^ V34 ^ V44);
			sph_enc32be(out + 20, V05 ^ V15 ^ V25 ^ V35 ^ V45);
			sph_enc32be(out + 24, V06 ^ V16 ^ V26 ^ V36 ^ V46);
			sph_enc32be(out + 28, V07 ^ V17 ^ V27 ^ V37 ^ V47);
			break;
		case 2:
			sph_enc32be(out + 32, V00 ^ V10 ^ V20 ^ V30 ^ V40);
			sph_enc32be(out + 36, V01 ^ V11 ^ V21 ^ V31 ^ V41);
			sph_enc32be(out + 40, V02 ^ V12 ^ V22 ^ V32 ^ V42);
			sph_enc32be(out + 44, V03 ^ V13 ^ V23 ^ V33 ^ V43);
			sph_enc32be(out + 48, V04 ^ V14 ^ V24 ^ V34 ^ V44);
			sph_enc32be(out + 52, V05 ^ V15 ^ V25 ^ V35 ^ V45);
			sph_enc32be(out + 56, V06 ^ V16 ^ V26 ^ V36 ^ V46);
			sph_enc32be(out + 60, V07 ^ V17 ^ V27 ^ V37 ^ V47);
			break;
		}
	}
}
Exemple #10
0
static void
test_types32(void)
{
	unsigned i;
	union {
		unsigned char bytes[64];
		sph_u32 v32;
	} u;

#if SPH_LITTLE_ENDIAN || SPH_BIG_ENDIAN
	ASSERT(sizeof(sph_u32) == 4);
#else
	ASSERT(sizeof(sph_u32) >= 4);
#endif

	for (i = 0; i < sizeof u.bytes; i ++)
		u.bytes[i] = i;
	for (i = 0; (i + 3) < sizeof u.bytes; i ++) {
		sph_u32 v, w;

		v = ((sph_u32)i << 24)
			| ((sph_u32)(i + 1) << 16)
			| ((sph_u32)(i + 2) << 8)
			| (sph_u32)(i + 3);
		w = ((sph_u32)(i + 3) << 24)
			| ((sph_u32)(i + 2) << 16)
			| ((sph_u32)(i + 1) << 8)
			| (sph_u32)i;
		ASSERT(sph_dec32be(u.bytes + i) == v);
		ASSERT(sph_dec32le(u.bytes + i) == w);
		if (i % 4 == 0) {
			ASSERT(sph_dec32be_aligned(u.bytes + i) == v);
			ASSERT(sph_dec32le_aligned(u.bytes + i) == w);
		}
	}
	memset(u.bytes, 0, sizeof u.bytes);
	for (i = 0; (i + 3) < sizeof u.bytes; i ++) {
		sph_u32 v, w;

		v = ((sph_u32)i << 24)
			| ((sph_u32)(i + 1) << 16)
			| ((sph_u32)(i + 2) << 8)
			| (sph_u32)(i + 3);
		w = ((sph_u32)(i + 3) << 24)
			| ((sph_u32)(i + 2) << 16)
			| ((sph_u32)(i + 1) << 8)
			| (sph_u32)i;
		if (i % 4 == 0) {
			sph_enc32be_aligned(u.bytes + i, v);
		} else {
			sph_enc32be(u.bytes + i, v);
		}
		ASSERT(u.bytes[i + 0] == i + 0);
		ASSERT(u.bytes[i + 1] == i + 1);
		ASSERT(u.bytes[i + 2] == i + 2);
		ASSERT(u.bytes[i + 3] == i + 3);
		memset(u.bytes, 0, sizeof u.bytes);
		if (i % 4 == 0) {
			sph_enc32le_aligned(u.bytes + i, w);
		} else {
			sph_enc32le(u.bytes + i, w);
		}
		ASSERT(u.bytes[i + 0] == i + 0);
		ASSERT(u.bytes[i + 1] == i + 1);
		ASSERT(u.bytes[i + 2] == i + 2);
		ASSERT(u.bytes[i + 3] == i + 3);
	}
}