Blame cbits/cryptonite_sha3.c

Packit 141393
/*
Packit 141393
 * Copyright (C) 2012 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 <stdint.h>
Packit 141393
#include <string.h>
Packit 141393
#include "cryptonite_bitfn.h"
Packit 141393
#include "cryptonite_align.h"
Packit 141393
#include "cryptonite_sha3.h"
Packit 141393
Packit 141393
#define KECCAK_NB_ROUNDS 24
Packit 141393
Packit 141393
/* rounds constants */
Packit 141393
static const uint64_t keccak_rndc[24] =
Packit 141393
{
Packit 141393
	0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
Packit 141393
	0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
Packit 141393
	0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
Packit 141393
	0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
Packit 141393
	0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
Packit 141393
	0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
Packit 141393
	0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
Packit 141393
	0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL,
Packit 141393
};
Packit 141393
Packit 141393
/* triangular numbers constants */
Packit 141393
static const int keccak_rotc[24] =
Packit 141393
	{ 1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44 };
Packit 141393
Packit 141393
static const int keccak_piln[24] =
Packit 141393
	{ 10,7,11,17,18,3,5,16,8,21,24,4,15,23,19,13,12,2,20,14,22,9,6,1 };
Packit 141393
Packit 141393
static inline void sha3_do_chunk(uint64_t state[25], uint64_t buf[], int bufsz)
Packit 141393
{
Packit 141393
	int i, j, r;
Packit 141393
	uint64_t tmp, bc[5];
Packit 141393
Packit 141393
	/* merge buf with state */
Packit 141393
	for (i = 0; i < bufsz; i++)
Packit 141393
		state[i] ^= le64_to_cpu(buf[i]);
Packit 141393
Packit 141393
	/* run keccak rounds */
Packit 141393
	for (r = 0; r < KECCAK_NB_ROUNDS; r++) {
Packit 141393
		/* compute the parity of each columns */
Packit 141393
		for (i = 0; i < 5; i++)
Packit 141393
			bc[i] = state[i] ^ state[i+5] ^ state[i+10] ^ state[i+15] ^ state[i+20];
Packit 141393
Packit 141393
		for (i = 0; i < 5; i++) {
Packit 141393
			tmp = bc[(i + 4) % 5] ^ rol64(bc[(i + 1) % 5], 1);
Packit 141393
			for (j = 0; j < 25; j += 5)
Packit 141393
				state[j + i] ^= tmp;
Packit 141393
		}
Packit 141393
Packit 141393
		/* rho pi */
Packit 141393
		tmp = state[1];
Packit 141393
		for (i = 0; i < 24; i++) {
Packit 141393
			j = keccak_piln[i];
Packit 141393
			bc[0] = state[j];
Packit 141393
			state[j] = rol64(tmp, keccak_rotc[i]);
Packit 141393
			tmp = bc[0];
Packit 141393
		}
Packit 141393
Packit 141393
		/* bitwise combine along rows using a = a xor (not b and c) */
Packit 141393
		for (j = 0; j < 25; j += 5) {
Packit 141393
			for (i = 0; i < 5; i++)
Packit 141393
				bc[i] = state[j + i];
Packit 141393
			#define andn(b,c) (~(b) & (c))
Packit 141393
			state[j + 0] ^= andn(bc[1], bc[2]);
Packit 141393
			state[j + 1] ^= andn(bc[2], bc[3]);
Packit 141393
			state[j + 2] ^= andn(bc[3], bc[4]);
Packit 141393
			state[j + 3] ^= andn(bc[4], bc[0]);
Packit 141393
			state[j + 4] ^= andn(bc[0], bc[1]);
Packit 141393
			#undef andn
Packit 141393
		}
Packit 141393
Packit 141393
		/* xor the round constant */
Packit 141393
		state[0] ^= keccak_rndc[r];
Packit 141393
	}
Packit 141393
}
Packit 141393
Packit 141393
/*
Packit 141393
 * Initialize a SHA-3 / SHAKE context: hashlen is the security level (and
Packit 141393
 * half the capacity) in bits
Packit 141393
 */
Packit 141393
void cryptonite_sha3_init(struct sha3_ctx *ctx, uint32_t hashlen)
Packit 141393
{
Packit 141393
	/* assert(hashlen >= SHA3_BITSIZE_MIN && hashlen <= SHA3_BITSIZE_MAX) */
Packit 141393
	int bufsz = SHA3_BUF_SIZE(hashlen);
Packit 141393
	memset(ctx, 0, sizeof(*ctx) + bufsz);
Packit 141393
	ctx->bufsz = bufsz;
Packit 141393
}
Packit 141393
Packit 141393
/* Update a SHA-3 / SHAKE context */
Packit 141393
void cryptonite_sha3_update(struct sha3_ctx *ctx, const uint8_t *data, uint32_t len)
Packit 141393
{
Packit 141393
	uint32_t to_fill;
Packit 141393
Packit 141393
	to_fill = ctx->bufsz - ctx->bufindex;
Packit 141393
Packit 141393
	if (ctx->bufindex == ctx->bufsz) {
Packit 141393
		sha3_do_chunk(ctx->state, (uint64_t *) ctx->buf, ctx->bufsz / 8);
Packit 141393
		ctx->bufindex = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	/* process partial buffer if there's enough data to make a block */
Packit 141393
	if (ctx->bufindex && len >= to_fill) {
Packit 141393
		memcpy(ctx->buf + ctx->bufindex, data, to_fill);
Packit 141393
		sha3_do_chunk(ctx->state, (uint64_t *) ctx->buf, ctx->bufsz / 8);
Packit 141393
		len -= to_fill;
Packit 141393
		data += to_fill;
Packit 141393
		ctx->bufindex = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	if (need_alignment(data, 8)) {
Packit 141393
		uint64_t tramp[SHA3_BUF_SIZE_MAX/8];
Packit 141393
		ASSERT_ALIGNMENT(tramp, 8);
Packit 141393
		for (; len >= ctx->bufsz; len -= ctx->bufsz, data += ctx->bufsz) {
Packit 141393
			memcpy(tramp, data, ctx->bufsz);
Packit 141393
			sha3_do_chunk(ctx->state, tramp, ctx->bufsz / 8);
Packit 141393
		}
Packit 141393
	} else {
Packit 141393
		/* process as much ctx->bufsz-block */
Packit 141393
		for (; len >= ctx->bufsz; len -= ctx->bufsz, data += ctx->bufsz)
Packit 141393
			sha3_do_chunk(ctx->state, (uint64_t *) data, ctx->bufsz / 8);
Packit 141393
	}
Packit 141393
Packit 141393
Packit 141393
	/* append data into buf */
Packit 141393
	if (len) {
Packit 141393
		memcpy(ctx->buf + ctx->bufindex, data, len);
Packit 141393
		ctx->bufindex += len;
Packit 141393
	}
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_sha3_finalize_with_pad_byte(struct sha3_ctx *ctx, uint8_t pad_byte)
Packit 141393
{
Packit 141393
	/* process full buffer if needed */
Packit 141393
	if (ctx->bufindex == ctx->bufsz) {
Packit 141393
		sha3_do_chunk(ctx->state, (uint64_t *) ctx->buf, ctx->bufsz / 8);
Packit 141393
		ctx->bufindex = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	/* add the 10*1 padding */
Packit 141393
	ctx->buf[ctx->bufindex++] = pad_byte;
Packit 141393
	memset(ctx->buf + ctx->bufindex, 0, ctx->bufsz - ctx->bufindex);
Packit 141393
	ctx->buf[ctx->bufsz - 1] |= 0x80;
Packit 141393
Packit 141393
	/* process */
Packit 141393
	sha3_do_chunk(ctx->state, (uint64_t *) ctx->buf, ctx->bufsz / 8);
Packit 141393
	ctx->bufindex = 0;
Packit 141393
}
Packit 141393
Packit 141393
/*
Packit 141393
 * Extract some bytes from a finalized SHA-3 / SHAKE context.
Packit 141393
 * May be called multiple times.
Packit 141393
 */
Packit 141393
void cryptonite_sha3_output(struct sha3_ctx *ctx, uint8_t *out, uint32_t len)
Packit 141393
{
Packit 141393
	uint64_t w[25];
Packit 141393
	uint8_t *wptr = (uint8_t *) w;
Packit 141393
	uint32_t still_avail;
Packit 141393
Packit 141393
	still_avail = ctx->bufsz - ctx->bufindex;
Packit 141393
Packit 141393
	if (ctx->bufindex == ctx->bufsz) {
Packit 141393
		/* squeeze the sponge again, without any input */
Packit 141393
		sha3_do_chunk(ctx->state, NULL, 0);
Packit 141393
		ctx->bufindex = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	/* use bytes already available if this block is fully consumed */
Packit 141393
	if (ctx->bufindex && len >= still_avail) {
Packit 141393
		cpu_to_le64_array(w, ctx->state, 25);
Packit 141393
		memcpy(out, wptr + ctx->bufindex, still_avail);
Packit 141393
		sha3_do_chunk(ctx->state, NULL, 0);
Packit 141393
		len -= still_avail;
Packit 141393
		out += still_avail;
Packit 141393
		ctx->bufindex = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	/* output as much ctx->bufsz-block */
Packit 141393
	for (; len > ctx->bufsz; len -= ctx->bufsz, out += ctx->bufsz) {
Packit 141393
		cpu_to_le64_array(w, ctx->state, 25);
Packit 141393
		memcpy(out, w, ctx->bufsz);
Packit 141393
		sha3_do_chunk(ctx->state, NULL, 0);
Packit 141393
	}
Packit 141393
Packit 141393
	/* output from partial buffer */
Packit 141393
	if (len) {
Packit 141393
		cpu_to_le64_array(w, ctx->state, 25);
Packit 141393
		memcpy(out, wptr + ctx->bufindex, len);
Packit 141393
		ctx->bufindex += len;
Packit 141393
	}
Packit 141393
}
Packit 141393
Packit 141393
/* Finalize a SHA-3 context and return the digest value */
Packit 141393
void cryptonite_sha3_finalize(struct sha3_ctx *ctx, uint32_t hashlen, uint8_t *out)
Packit 141393
{
Packit 141393
	cryptonite_sha3_finalize_with_pad_byte(ctx, 0x06);
Packit 141393
	cryptonite_sha3_output(ctx, out, hashlen / 8);
Packit 141393
}
Packit 141393
Packit 141393
/* Finalize a SHAKE context. Output is read using cryptonite_sha3_output. */
Packit 141393
void cryptonite_sha3_finalize_shake(struct sha3_ctx *ctx)
Packit 141393
{
Packit 141393
	cryptonite_sha3_finalize_with_pad_byte(ctx, 0x1F);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_keccak_init(struct sha3_ctx *ctx, uint32_t hashlen)
Packit 141393
{
Packit 141393
	cryptonite_sha3_init(ctx, hashlen);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_keccak_update(struct sha3_ctx *ctx, uint8_t *data, uint32_t len)
Packit 141393
{
Packit 141393
	cryptonite_sha3_update(ctx, data, len);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_keccak_finalize(struct sha3_ctx *ctx, uint32_t hashlen, uint8_t *out)
Packit 141393
{
Packit 141393
	cryptonite_sha3_finalize_with_pad_byte(ctx, 1);
Packit 141393
	cryptonite_sha3_output(ctx, out, hashlen / 8);
Packit 141393
}