Esempio n. 1
0
File: jh.c Progetto: atcsecure/XC2
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);
}
Esempio n. 2
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]);
}
Esempio n. 3
0
static void
blake64_close(sph_blake_big_context *sc,
	unsigned ub, unsigned n, void *dst, size_t out_size_w64)
{
	union {
		unsigned char buf[128];
		sph_u64 dummy;
	} u;
	size_t ptr, k;
	unsigned bit_len;
	unsigned z;
	sph_u64 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_C64(0xFFFFFFFFFFFFFC00);
		sc->T1 = SPH_C64(0xFFFFFFFFFFFFFFFF);
	} else if (sc->T0 == 0) {
		sc->T0 = SPH_C64(0xFFFFFFFFFFFFFC00) + bit_len;
		sc->T1 = SPH_T64(sc->T1 - 1);
	} else {
		sc->T0 -= 1024 - bit_len;
	}
	if (bit_len <= 894) {
		memset(u.buf + ptr + 1, 0, 111 - ptr);
		if (out_size_w64 == 8)
			u.buf[111] |= 1;
		sph_enc64be_aligned(u.buf + 112, th);
		sph_enc64be_aligned(u.buf + 120, tl);
		blake64(sc, u.buf + ptr, 128 - ptr);
	} else {
		memset(u.buf + ptr + 1, 0, 127 - ptr);
		blake64(sc, u.buf + ptr, 128 - ptr);
		sc->T0 = SPH_C64(0xFFFFFFFFFFFFFC00);
		sc->T1 = SPH_C64(0xFFFFFFFFFFFFFFFF);
		memset(u.buf, 0, 112);
		if (out_size_w64 == 8)
			u.buf[111] = 1;
		sph_enc64be_aligned(u.buf + 112, th);
		sph_enc64be_aligned(u.buf + 120, tl);
		blake64(sc, u.buf, 128);
	}
	out = dst;
	for (k = 0; k < out_size_w64; k ++)
		sph_enc64be(out + (k << 3), sc->H[k]);
}
Esempio n. 4
0
void CBLAKE512::Finalize(unsigned char hash[OUTPUT_SIZE])
{
    union {
        unsigned char buf[128];
        sph_u64 dummy;
    } u;
    size_t ptr, k;
    unsigned bit_len;
    unsigned z;
    sph_u64 th, tl;

    ptr = s.ptr;
    bit_len = ((unsigned)ptr << 3);
    z = 0x80 >> 0;
    u.buf[ptr] = ((0 & -z) | z) & 0xFF;
    tl = s.T0 + bit_len;
    th = s.T1;
    if (ptr == 0) {
        s.T0 = SPH_C64(0xFFFFFFFFFFFFFC00);
        s.T1 = SPH_C64(0xFFFFFFFFFFFFFFFF);
    } else if (s.T0 == 0) {
        s.T0 = SPH_C64(0xFFFFFFFFFFFFFC00) + bit_len;
        s.T1 = SPH_T64(s.T1 - 1);
    } else {
        s.T0 -= 1024 - bit_len;
    }
    if (bit_len <= 894) {
        memset(u.buf + ptr + 1, 0, 111 - ptr);
        u.buf[111] |= 1;
        sph_enc64be_aligned(u.buf + 112, th);
        sph_enc64be_aligned(u.buf + 120, tl);
        Write(u.buf + ptr, 128 - ptr);
    } else {
        memset(u.buf + ptr + 1, 0, 127 - ptr);
        Write(u.buf + ptr, 128 - ptr);
        s.T0 = SPH_C64(0xFFFFFFFFFFFFFC00);
        s.T1 = SPH_C64(0xFFFFFFFFFFFFFFFF);
        memset(u.buf, 0, 112);
        u.buf[111] = 1;
        sph_enc64be_aligned(u.buf + 112, th);
        sph_enc64be_aligned(u.buf + 120, tl);
        Write(u.buf, 128);
    }
    for (k = 0; k < 8; k ++)
        sph_enc64be(hash + (k << 3), s.H[k]);
    Reset();
}
Esempio n. 5
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]);
	}
}
Esempio n. 6
0
static void
test_types64(void)
{
	unsigned i;
	union {
		unsigned char bytes[64];
		sph_u64 v64;
	} u;

#if SPH_LITTLE_ENDIAN || SPH_BIG_ENDIAN
	ASSERT(sizeof(sph_u64) == 8);
#else
	ASSERT(sizeof(sph_u64) >= 8);
#endif

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

		v = ((sph_u64)i << 56)
			| ((sph_u64)(i + 1) << 48)
			| ((sph_u64)(i + 2) << 40)
			| ((sph_u64)(i + 3) << 32)
			| ((sph_u64)(i + 4) << 24)
			| ((sph_u64)(i + 5) << 16)
			| ((sph_u64)(i + 6) << 8)
			| (sph_u64)(i + 7);
		w = ((sph_u64)(i + 7) << 56)
			| ((sph_u64)(i + 6) << 48)
			| ((sph_u64)(i + 5) << 40)
			| ((sph_u64)(i + 4) << 32)
			| ((sph_u64)(i + 3) << 24)
			| ((sph_u64)(i + 2) << 16)
			| ((sph_u64)(i + 1) << 8)
			| (sph_u64)i;
		ASSERT(sph_dec64be(u.bytes + i) == v);
		ASSERT(sph_dec64le(u.bytes + i) == w);
		if (i % 8 == 0) {
			ASSERT(sph_dec64be_aligned(u.bytes + i) == v);
			ASSERT(sph_dec64le_aligned(u.bytes + i) == w);
		}
	}
	memset(u.bytes, 0, sizeof u.bytes);
	for (i = 0; (i + 7) < sizeof u.bytes; i ++) {
		sph_u64 v, w;

		v = ((sph_u64)i << 56)
			| ((sph_u64)(i + 1) << 48)
			| ((sph_u64)(i + 2) << 40)
			| ((sph_u64)(i + 3) << 32)
			| ((sph_u64)(i + 4) << 24)
			| ((sph_u64)(i + 5) << 16)
			| ((sph_u64)(i + 6) << 8)
			| (sph_u64)(i + 7);
		w = ((sph_u64)(i + 7) << 56)
			| ((sph_u64)(i + 6) << 48)
			| ((sph_u64)(i + 5) << 40)
			| ((sph_u64)(i + 4) << 32)
			| ((sph_u64)(i + 3) << 24)
			| ((sph_u64)(i + 2) << 16)
			| ((sph_u64)(i + 1) << 8)
			| (sph_u64)i;
		if (i % 8 == 0) {
			sph_enc64be_aligned(u.bytes + i, v);
		} else {
			sph_enc64be(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);
		ASSERT(u.bytes[i + 4] == i + 4);
		ASSERT(u.bytes[i + 5] == i + 5);
		ASSERT(u.bytes[i + 6] == i + 6);
		ASSERT(u.bytes[i + 7] == i + 7);
		memset(u.bytes, 0, sizeof u.bytes);
		if (i % 8 == 0) {
			sph_enc64le_aligned(u.bytes + i, w);
		} else {
			sph_enc64le(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);
		ASSERT(u.bytes[i + 4] == i + 4);
		ASSERT(u.bytes[i + 5] == i + 5);
		ASSERT(u.bytes[i + 6] == i + 6);
		ASSERT(u.bytes[i + 7] == i + 7);
	}
}