Blame lib/accelerated/x86/sha-x86-ssse3.c

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Author: Nikos Mavrogiannopoulos
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * The GnuTLS is free software; you can redistribute it and/or
Packit Service 4684c1
 * modify it under the terms of the GNU Lesser General Public License
Packit Service 4684c1
 * as published by the Free Software Foundation; either version 2.1 of
Packit Service 4684c1
 * the License, or (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * This library is distributed in the hope that it will be useful, but
Packit Service 4684c1
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
 * Lesser General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 4684c1
 *
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
#include "errors.h"
Packit Service 4684c1
#include "gnutls_int.h"
Packit Service 4684c1
#include <gnutls/crypto.h>
Packit Service 4684c1
#include "errors.h"
Packit Service 4684c1
#include <aes-x86.h>
Packit Service 4684c1
#include <nettle/sha.h>
Packit Service 4684c1
#include <nettle/macros.h>
Packit Service 4684c1
#include <nettle/nettle-meta.h>
Packit Service 4684c1
#include <sha-x86.h>
Packit Service 4684c1
#include <x86-common.h>
Packit Service 4684c1
Packit Service 4684c1
void sha1_block_data_order(void *c, const void *p, size_t len);
Packit Service 4684c1
void sha256_block_data_order(void *c, const void *p, size_t len);
Packit Service 4684c1
void sha512_block_data_order(void *c, const void *p, size_t len);
Packit Service 4684c1
Packit Service 4684c1
typedef void (*update_func) (void *, size_t, const uint8_t *);
Packit Service 4684c1
typedef void (*digest_func) (void *, size_t, uint8_t *);
Packit Service 4684c1
typedef void (*set_key_func) (void *, size_t, const uint8_t *);
Packit Service 4684c1
typedef void (*init_func) (void *);
Packit Service 4684c1
Packit Service 4684c1
struct x86_hash_ctx {
Packit Service 4684c1
	union {
Packit Service 4684c1
		struct sha1_ctx sha1;
Packit Service 4684c1
		struct sha224_ctx sha224;
Packit Service 4684c1
		struct sha256_ctx sha256;
Packit Service 4684c1
		struct sha384_ctx sha384;
Packit Service 4684c1
		struct sha512_ctx sha512;
Packit Service 4684c1
	} ctx;
Packit Service 4684c1
	void *ctx_ptr;
Packit Service 4684c1
	gnutls_digest_algorithm_t algo;
Packit Service 4684c1
	size_t length;
Packit Service 4684c1
	update_func update;
Packit Service 4684c1
	digest_func digest;
Packit Service 4684c1
	init_func init;
Packit Service 4684c1
};
Packit Service 4684c1
Packit Service 4684c1
static int
Packit Service 4684c1
wrap_x86_hash_update(void *_ctx, const void *text, size_t textsize)
Packit Service 4684c1
{
Packit Service 4684c1
	struct x86_hash_ctx *ctx = _ctx;
Packit Service 4684c1
Packit Service 4684c1
	ctx->update(ctx->ctx_ptr, textsize, text);
Packit Service 4684c1
Packit Service 4684c1
	return GNUTLS_E_SUCCESS;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static void wrap_x86_hash_deinit(void *hd)
Packit Service 4684c1
{
Packit Service 4684c1
	gnutls_free(hd);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
void x86_sha1_update(struct sha1_ctx *ctx, size_t length,
Packit Service 4684c1
		     const uint8_t * data)
Packit Service 4684c1
{
Packit Service 4684c1
	struct {
Packit Service 4684c1
		uint32_t h0, h1, h2, h3, h4;
Packit Service 4684c1
		uint32_t Nl, Nh;
Packit Service 4684c1
		uint32_t data[16];
Packit Service 4684c1
		unsigned int num;
Packit Service 4684c1
	} octx;
Packit Service 4684c1
	size_t res;
Packit Service 4684c1
	unsigned t2, i;
Packit Service 4684c1
Packit Service 4684c1
	if ((res = ctx->index)) {
Packit Service 4684c1
		res = SHA1_DATA_SIZE - res;
Packit Service 4684c1
		if (length < res)
Packit Service 4684c1
			res = length;
Packit Service 4684c1
		sha1_update(ctx, res, data);
Packit Service 4684c1
		data += res;
Packit Service 4684c1
		length -= res;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	octx.h0 = ctx->state[0];
Packit Service 4684c1
	octx.h1 = ctx->state[1];
Packit Service 4684c1
	octx.h2 = ctx->state[2];
Packit Service 4684c1
	octx.h3 = ctx->state[3];
Packit Service 4684c1
	octx.h4 = ctx->state[4];
Packit Service 4684c1
Packit Service 4684c1
	memcpy(octx.data, ctx->block, SHA1_DATA_SIZE);
Packit Service 4684c1
	octx.num = ctx->index;
Packit Service 4684c1
Packit Service 4684c1
	res = length % SHA1_DATA_SIZE;
Packit Service 4684c1
	length -= res;
Packit Service 4684c1
Packit Service 4684c1
	if (length > 0) {
Packit Service 4684c1
Packit Service 4684c1
		t2 = length / SHA1_DATA_SIZE;
Packit Service 4684c1
Packit Service 4684c1
		sha1_block_data_order(&octx, data, t2);
Packit Service 4684c1
Packit Service 4684c1
		for (i=0;i
Packit Service 4684c1
			ctx->count++;
Packit Service 4684c1
		data += length;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	ctx->state[0] = octx.h0;
Packit Service 4684c1
	ctx->state[1] = octx.h1;
Packit Service 4684c1
	ctx->state[2] = octx.h2;
Packit Service 4684c1
	ctx->state[3] = octx.h3;
Packit Service 4684c1
	ctx->state[4] = octx.h4;
Packit Service 4684c1
Packit Service 4684c1
	memcpy(ctx->block, octx.data, octx.num);
Packit Service 4684c1
	ctx->index = octx.num;
Packit Service 4684c1
Packit Service 4684c1
	if (res > 0) {
Packit Service 4684c1
		sha1_update(ctx, res, data);
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
void x86_sha256_update(struct sha256_ctx *ctx, size_t length,
Packit Service 4684c1
		     const uint8_t * data)
Packit Service 4684c1
{
Packit Service 4684c1
	struct {
Packit Service 4684c1
		uint32_t h[8];
Packit Service 4684c1
		uint32_t Nl, Nh;
Packit Service 4684c1
		uint32_t data[16];
Packit Service 4684c1
		unsigned int num;
Packit Service 4684c1
		unsigned md_len;
Packit Service 4684c1
	} octx;
Packit Service 4684c1
	size_t res;
Packit Service 4684c1
	unsigned t2, i;
Packit Service 4684c1
Packit Service 4684c1
	if ((res = ctx->index)) {
Packit Service 4684c1
		res = SHA256_DATA_SIZE - res;
Packit Service 4684c1
		if (length < res)
Packit Service 4684c1
			res = length;
Packit Service 4684c1
		sha256_update(ctx, res, data);
Packit Service 4684c1
		data += res;
Packit Service 4684c1
		length -= res;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	memcpy(octx.h, ctx->state, sizeof(octx.h));
Packit Service 4684c1
	memcpy(octx.data, ctx->block, SHA256_DATA_SIZE);
Packit Service 4684c1
	octx.num = ctx->index;
Packit Service 4684c1
Packit Service 4684c1
	res = length % SHA256_DATA_SIZE;
Packit Service 4684c1
	length -= res;
Packit Service 4684c1
Packit Service 4684c1
	if (length > 0) {
Packit Service 4684c1
		t2 = length / SHA1_DATA_SIZE;
Packit Service 4684c1
		sha256_block_data_order(&octx, data, t2);
Packit Service 4684c1
		
Packit Service 4684c1
		for (i=0;i
Packit Service 4684c1
			ctx->count++;
Packit Service 4684c1
		data += length;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	memcpy(ctx->state, octx.h, sizeof(octx.h));
Packit Service 4684c1
Packit Service 4684c1
	memcpy(ctx->block, octx.data, octx.num);
Packit Service 4684c1
	ctx->index = octx.num;
Packit Service 4684c1
Packit Service 4684c1
	if (res > 0) {
Packit Service 4684c1
		sha256_update(ctx, res, data);
Packit Service 4684c1
	}
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
void x86_sha512_update(struct sha512_ctx *ctx, size_t length,
Packit Service 4684c1
		     const uint8_t * data)
Packit Service 4684c1
{
Packit Service 4684c1
	struct {
Packit Service 4684c1
		uint64_t h[8];
Packit Service 4684c1
		uint64_t Nl, Nh;
Packit Service 4684c1
		union {
Packit Service 4684c1
			uint64_t d[16];
Packit Service 4684c1
			uint8_t p[16*8];
Packit Service 4684c1
		} u;
Packit Service 4684c1
		unsigned int num;
Packit Service 4684c1
		unsigned md_len;
Packit Service 4684c1
	} octx;
Packit Service 4684c1
	size_t res;
Packit Service 4684c1
	unsigned t2, i;
Packit Service 4684c1
Packit Service 4684c1
	if ((res = ctx->index)) {
Packit Service 4684c1
		res = SHA512_DATA_SIZE - res;
Packit Service 4684c1
		if (length < res)
Packit Service 4684c1
			res = length;
Packit Service 4684c1
		sha512_update(ctx, res, data);
Packit Service 4684c1
		data += res;
Packit Service 4684c1
		length -= res;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	memcpy(octx.h, ctx->state, sizeof(octx.h));
Packit Service 4684c1
	memcpy(octx.u.p, ctx->block, SHA512_DATA_SIZE);
Packit Service 4684c1
	octx.num = ctx->index;
Packit Service 4684c1
Packit Service 4684c1
	res = length % SHA512_DATA_SIZE;
Packit Service 4684c1
	length -= res;
Packit Service 4684c1
Packit Service 4684c1
	if (length > 0) {
Packit Service 4684c1
		t2 = length / SHA512_DATA_SIZE;
Packit Service 4684c1
		sha512_block_data_order(&octx, data, t2);
Packit Service 4684c1
		
Packit Service 4684c1
		for (i=0;i
Packit Service 4684c1
			MD_INCR(ctx);
Packit Service 4684c1
		data += length;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	memcpy(ctx->state, octx.h, sizeof(octx.h));
Packit Service 4684c1
Packit Service 4684c1
	memcpy(ctx->block, octx.u.p, octx.num);
Packit Service 4684c1
	ctx->index = octx.num;
Packit Service 4684c1
Packit Service 4684c1
	if (res > 0) {
Packit Service 4684c1
		sha512_update(ctx, res, data);
Packit Service 4684c1
	}
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static int _ctx_init(gnutls_digest_algorithm_t algo,
Packit Service 4684c1
		     struct x86_hash_ctx *ctx)
Packit Service 4684c1
{
Packit Service 4684c1
	switch (algo) {
Packit Service 4684c1
	case GNUTLS_DIG_SHA1:
Packit Service 4684c1
		sha1_init(&ctx->ctx.sha1);
Packit Service 4684c1
		ctx->update = (update_func) x86_sha1_update;
Packit Service 4684c1
		ctx->digest = (digest_func) sha1_digest;
Packit Service 4684c1
		ctx->init = (init_func) sha1_init;
Packit Service 4684c1
		ctx->ctx_ptr = &ctx->ctx.sha1;
Packit Service 4684c1
		ctx->length = SHA1_DIGEST_SIZE;
Packit Service 4684c1
		break;
Packit Service 4684c1
	case GNUTLS_DIG_SHA224:
Packit Service 4684c1
		sha224_init(&ctx->ctx.sha224);
Packit Service 4684c1
		ctx->update = (update_func) x86_sha256_update;
Packit Service 4684c1
		ctx->digest = (digest_func) sha224_digest;
Packit Service 4684c1
		ctx->init = (init_func) sha224_init;
Packit Service 4684c1
		ctx->ctx_ptr = &ctx->ctx.sha224;
Packit Service 4684c1
		ctx->length = SHA224_DIGEST_SIZE;
Packit Service 4684c1
		break;
Packit Service 4684c1
	case GNUTLS_DIG_SHA256:
Packit Service 4684c1
		sha256_init(&ctx->ctx.sha256);
Packit Service 4684c1
		ctx->update = (update_func) x86_sha256_update;
Packit Service 4684c1
		ctx->digest = (digest_func) sha256_digest;
Packit Service 4684c1
		ctx->init = (init_func) sha256_init;
Packit Service 4684c1
		ctx->ctx_ptr = &ctx->ctx.sha256;
Packit Service 4684c1
		ctx->length = SHA256_DIGEST_SIZE;
Packit Service 4684c1
		break;
Packit Service 4684c1
	case GNUTLS_DIG_SHA384:
Packit Service 4684c1
		sha384_init(&ctx->ctx.sha384);
Packit Service 4684c1
		ctx->update = (update_func) x86_sha512_update;
Packit Service 4684c1
		ctx->digest = (digest_func) sha512_digest;
Packit Service 4684c1
		ctx->init = (init_func) sha384_init;
Packit Service 4684c1
		ctx->ctx_ptr = &ctx->ctx.sha384;
Packit Service 4684c1
		ctx->length = SHA384_DIGEST_SIZE;
Packit Service 4684c1
		break;
Packit Service 4684c1
	case GNUTLS_DIG_SHA512:
Packit Service 4684c1
		sha512_init(&ctx->ctx.sha512);
Packit Service 4684c1
		ctx->update = (update_func) x86_sha512_update;
Packit Service 4684c1
		ctx->digest = (digest_func) sha512_digest;
Packit Service 4684c1
		ctx->init = (init_func) sha512_init;
Packit Service 4684c1
		ctx->ctx_ptr = &ctx->ctx.sha512;
Packit Service 4684c1
		ctx->length = SHA512_DIGEST_SIZE;
Packit Service 4684c1
		break;
Packit Service 4684c1
	default:
Packit Service 4684c1
		gnutls_assert();
Packit Service 4684c1
		return GNUTLS_E_INVALID_REQUEST;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	return 0;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
static int wrap_x86_hash_init(gnutls_digest_algorithm_t algo, void **_ctx)
Packit Service 4684c1
{
Packit Service 4684c1
	struct x86_hash_ctx *ctx;
Packit Service 4684c1
	int ret;
Packit Service 4684c1
Packit Service 4684c1
	ctx = gnutls_malloc(sizeof(struct x86_hash_ctx));
Packit Service 4684c1
	if (ctx == NULL) {
Packit Service 4684c1
		gnutls_assert();
Packit Service 4684c1
		return GNUTLS_E_MEMORY_ERROR;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	ctx->algo = algo;
Packit Service 4684c1
Packit Service 4684c1
	if ((ret = _ctx_init(algo, ctx)) < 0) {
Packit Service 4684c1
		gnutls_assert();
Packit Service 4684c1
		return ret;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	*_ctx = ctx;
Packit Service 4684c1
Packit Service 4684c1
	return 0;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static void *
Packit Service 4684c1
wrap_x86_hash_copy(const void *_ctx)
Packit Service 4684c1
{
Packit Service 4684c1
	struct x86_hash_ctx *new_ctx;
Packit Service 4684c1
	const struct x86_hash_ctx *ctx=_ctx;
Packit Service 4684c1
	ptrdiff_t off = (uint8_t *)ctx->ctx_ptr - (uint8_t *)(&ctx->ctx);
Packit Service 4684c1
Packit Service 4684c1
	new_ctx = gnutls_malloc(sizeof(struct x86_hash_ctx));
Packit Service 4684c1
	if (new_ctx == NULL) {
Packit Service 4684c1
		gnutls_assert();
Packit Service 4684c1
		return NULL;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	memcpy(new_ctx, ctx, sizeof(*new_ctx));
Packit Service 4684c1
	new_ctx->ctx_ptr = (uint8_t *)&new_ctx->ctx + off;
Packit Service 4684c1
Packit Service 4684c1
	return new_ctx;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static int
Packit Service 4684c1
wrap_x86_hash_output(void *src_ctx, void *digest, size_t digestsize)
Packit Service 4684c1
{
Packit Service 4684c1
	struct x86_hash_ctx *ctx;
Packit Service 4684c1
	ctx = src_ctx;
Packit Service 4684c1
Packit Service 4684c1
	if (digestsize < ctx->length)
Packit Service 4684c1
		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
Packit Service 4684c1
Packit Service 4684c1
	ctx->digest(ctx->ctx_ptr, digestsize, digest);
Packit Service 4684c1
Packit Service 4684c1
	return 0;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static int wrap_x86_hash_fast(gnutls_digest_algorithm_t algo,
Packit Service 4684c1
				 const void *text, size_t text_size,
Packit Service 4684c1
				 void *digest)
Packit Service 4684c1
{
Packit Service 4684c1
	struct x86_hash_ctx ctx;
Packit Service 4684c1
	int ret;
Packit Service 4684c1
Packit Service 4684c1
	ret = _ctx_init(algo, &ctx;;
Packit Service 4684c1
	if (ret < 0)
Packit Service 4684c1
		return gnutls_assert_val(ret);
Packit Service 4684c1
Packit Service 4684c1
	ctx.update(&ctx, text_size, text);
Packit Service 4684c1
	ctx.digest(&ctx, ctx.length, digest);
Packit Service 4684c1
Packit Service 4684c1
	return 0;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
const struct nettle_hash x86_sha1 =
Packit Service 4684c1
NN_HASH(sha1, x86_sha1_update, sha1_digest, SHA1);
Packit Service 4684c1
const struct nettle_hash x86_sha224 =
Packit Service 4684c1
NN_HASH(sha224, x86_sha256_update, sha224_digest, SHA224);
Packit Service 4684c1
const struct nettle_hash x86_sha256 =
Packit Service 4684c1
NN_HASH(sha256, x86_sha256_update, sha256_digest, SHA256);
Packit Service 4684c1
Packit Service 4684c1
const struct nettle_hash x86_sha384 =
Packit Service 4684c1
NN_HASH(sha384, x86_sha512_update, sha384_digest, SHA384);
Packit Service 4684c1
const struct nettle_hash x86_sha512 =
Packit Service 4684c1
NN_HASH(sha512, x86_sha512_update, sha512_digest, SHA512);
Packit Service 4684c1
Packit Service 4684c1
const gnutls_crypto_digest_st _gnutls_sha_x86_ssse3 = {
Packit Service 4684c1
	.init = wrap_x86_hash_init,
Packit Service 4684c1
	.hash = wrap_x86_hash_update,
Packit Service 4684c1
	.output = wrap_x86_hash_output,
Packit Service 4684c1
	.copy = wrap_x86_hash_copy,
Packit Service 4684c1
	.deinit = wrap_x86_hash_deinit,
Packit Service 4684c1
	.fast = wrap_x86_hash_fast,
Packit Service 4684c1
};