Blame cbits/cryptonite_sha256.c

Packit 141393
/*
Packit 141393
 * Copyright (C) 2006-2009 Vincent Hanquez <vincent@snarc.org>
Packit 141393
 *
Packit 141393
 * Redistribution and use in source and binary forms, with or without
Packit 141393
 * modification, are permitted provided that the following conditions
Packit 141393
 * are met:
Packit 141393
 * 1. Redistributions of source code must retain the above copyright
Packit 141393
 *    notice, this list of conditions and the following disclaimer.
Packit 141393
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 141393
 *    notice, this list of conditions and the following disclaimer in the
Packit 141393
 *    documentation and/or other materials provided with the distribution.
Packit 141393
 *
Packit 141393
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit 141393
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 141393
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 141393
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 141393
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 141393
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 141393
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 141393
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 141393
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 141393
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 141393
 */
Packit 141393
Packit 141393
#include <string.h>
Packit 141393
#include "cryptonite_sha256.h"
Packit 141393
#include "cryptonite_bitfn.h"
Packit 141393
#include "cryptonite_align.h"
Packit 141393
Packit 141393
void cryptonite_sha224_init(struct sha224_ctx *ctx)
Packit 141393
{
Packit 141393
	memset(ctx, 0, sizeof(*ctx));
Packit 141393
Packit 141393
	ctx->h[0] = 0xc1059ed8;
Packit 141393
	ctx->h[1] = 0x367cd507;
Packit 141393
	ctx->h[2] = 0x3070dd17;
Packit 141393
	ctx->h[3] = 0xf70e5939;
Packit 141393
	ctx->h[4] = 0xffc00b31;
Packit 141393
	ctx->h[5] = 0x68581511;
Packit 141393
	ctx->h[6] = 0x64f98fa7;
Packit 141393
	ctx->h[7] = 0xbefa4fa4;
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_sha256_init(struct sha256_ctx *ctx)
Packit 141393
{
Packit 141393
	memset(ctx, 0, sizeof(*ctx));
Packit 141393
Packit 141393
	ctx->h[0] = 0x6a09e667;
Packit 141393
	ctx->h[1] = 0xbb67ae85;
Packit 141393
	ctx->h[2] = 0x3c6ef372;
Packit 141393
	ctx->h[3] = 0xa54ff53a;
Packit 141393
	ctx->h[4] = 0x510e527f;
Packit 141393
	ctx->h[5] = 0x9b05688c;
Packit 141393
	ctx->h[6] = 0x1f83d9ab;
Packit 141393
	ctx->h[7] = 0x5be0cd19;
Packit 141393
}
Packit 141393
Packit 141393
/* 232 times the cube root of the first 64 primes 2..311 */
Packit 141393
static const uint32_t k[] = {
Packit 141393
	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
Packit 141393
	0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
Packit 141393
	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
Packit 141393
	0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
Packit 141393
	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
Packit 141393
	0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
Packit 141393
	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
Packit 141393
	0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
Packit 141393
	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
Packit 141393
	0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
Packit 141393
	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
Packit 141393
};
Packit 141393
Packit 141393
#define e0(x)       (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22))
Packit 141393
#define e1(x)       (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25))
Packit 141393
#define s0(x)       (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))
Packit 141393
#define s1(x)       (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))
Packit 141393
Packit 141393
static void sha256_do_chunk(struct sha256_ctx *ctx, uint32_t buf[])
Packit 141393
{
Packit 141393
	uint32_t a, b, c, d, e, f, g, h, t1, t2;
Packit 141393
	int i;
Packit 141393
	uint32_t w[64];
Packit 141393
Packit 141393
	cpu_to_be32_array(w, buf, 16);
Packit 141393
	for (i = 16; i < 64; i++)
Packit 141393
		w[i] = s1(w[i - 2]) + w[i - 7] + s0(w[i - 15]) + w[i - 16];
Packit 141393
Packit 141393
	a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3];
Packit 141393
	e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7];
Packit 141393
Packit 141393
#define R(a, b, c, d, e, f, g, h, k, w)			\
Packit 141393
	t1 = h + e1(e) + (g ^ (e & (f ^ g))) + k + w; 	\
Packit 141393
	t2 = e0(a) + ((a & b) | (c & (a | b)));		\
Packit 141393
	d += t1;					\
Packit 141393
	h = t1 + t2;
Packit 141393
Packit 141393
	for (i = 0; i < 64; i += 8) {
Packit 141393
		R(a, b, c, d, e, f, g, h, k[i + 0], w[i + 0]);
Packit 141393
		R(h, a, b, c, d, e, f, g, k[i + 1], w[i + 1]);
Packit 141393
		R(g, h, a, b, c, d, e, f, k[i + 2], w[i + 2]);
Packit 141393
		R(f, g, h, a, b, c, d, e, k[i + 3], w[i + 3]);
Packit 141393
		R(e, f, g, h, a, b, c, d, k[i + 4], w[i + 4]);
Packit 141393
		R(d, e, f, g, h, a, b, c, k[i + 5], w[i + 5]);
Packit 141393
		R(c, d, e, f, g, h, a, b, k[i + 6], w[i + 6]);
Packit 141393
		R(b, c, d, e, f, g, h, a, k[i + 7], w[i + 7]);
Packit 141393
	}
Packit 141393
Packit 141393
#undef R
Packit 141393
Packit 141393
	ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d;
Packit 141393
	ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h;
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_sha224_update(struct sha224_ctx *ctx, const uint8_t *data, uint32_t len)
Packit 141393
{
Packit 141393
	return cryptonite_sha256_update(ctx, data, len);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_sha256_update(struct sha256_ctx *ctx, const uint8_t *data, uint32_t len)
Packit 141393
{
Packit 141393
	uint32_t index, to_fill;
Packit 141393
Packit 141393
	/* check for partial buffer */
Packit 141393
	index = (uint32_t) (ctx->sz & 0x3f);
Packit 141393
	to_fill = 64 - index;
Packit 141393
Packit 141393
	ctx->sz += len;
Packit 141393
Packit 141393
	/* process partial buffer if there's enough data to make a block */
Packit 141393
	if (index && len >= to_fill) {
Packit 141393
		memcpy(ctx->buf + index, data, to_fill);
Packit 141393
		sha256_do_chunk(ctx, (uint32_t *) ctx->buf);
Packit 141393
		len -= to_fill;
Packit 141393
		data += to_fill;
Packit 141393
		index = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	if (need_alignment(data, 4)) {
Packit 141393
		uint32_t tramp[16];
Packit 141393
		ASSERT_ALIGNMENT(tramp, 4);
Packit 141393
		for (; len >= 64; len -= 64, data += 64) {
Packit 141393
			memcpy(tramp, data, 64);
Packit 141393
			sha256_do_chunk(ctx, tramp);
Packit 141393
		}
Packit 141393
	} else {
Packit 141393
		/* process as much 64-block as possible */
Packit 141393
		for (; len >= 64; len -= 64, data += 64)
Packit 141393
			sha256_do_chunk(ctx, (uint32_t *) data);
Packit 141393
	}
Packit 141393
Packit 141393
	/* append data into buf */
Packit 141393
	if (len)
Packit 141393
		memcpy(ctx->buf + index, data, len);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_sha224_finalize(struct sha224_ctx *ctx, uint8_t *out)
Packit 141393
{
Packit 141393
	uint8_t intermediate[SHA256_DIGEST_SIZE];
Packit 141393
Packit 141393
	cryptonite_sha256_finalize(ctx, intermediate);
Packit 141393
	memcpy(out, intermediate, SHA224_DIGEST_SIZE);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_sha256_finalize(struct sha256_ctx *ctx, uint8_t *out)
Packit 141393
{
Packit 141393
	static uint8_t padding[64] = { 0x80, };
Packit 141393
	uint64_t bits;
Packit 141393
	uint32_t i, index, padlen;
Packit 141393
Packit 141393
	/* cpu -> big endian */
Packit 141393
	bits = cpu_to_be64(ctx->sz << 3);
Packit 141393
Packit 141393
	/* pad out to 56 */
Packit 141393
	index = (uint32_t) (ctx->sz & 0x3f);
Packit 141393
	padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
Packit 141393
	cryptonite_sha256_update(ctx, padding, padlen);
Packit 141393
Packit 141393
	/* append length */
Packit 141393
	cryptonite_sha256_update(ctx, (uint8_t *) &bits, sizeof(bits));
Packit 141393
Packit 141393
	/* store to digest */
Packit 141393
	for (i = 0; i < 8; i++)
Packit 141393
		store_be32(out+4*i, ctx->h[i]);
Packit 141393
}