From 8d7011c2eeda41c31bf9d90863c6e1ef6ce7a917 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Sat, 14 Dec 2013 18:32:19 +0100 Subject: [PATCH] use better names for files --- lib/accelerated/x86/Makefile.am | 2 +- lib/accelerated/x86/hmac-x86-ssse3.c | 300 ++++++++++++++++++++++++++++ lib/accelerated/x86/hmac-x86.c | 300 ---------------------------- lib/accelerated/x86/sha-x86-ssse3.c | 365 +++++++++++++++++++++++++++++++++++ lib/accelerated/x86/sha-x86.c | 365 ----------------------------------- 5 files changed, 666 insertions(+), 666 deletions(-) create mode 100644 lib/accelerated/x86/hmac-x86-ssse3.c delete mode 100644 lib/accelerated/x86/hmac-x86.c create mode 100644 lib/accelerated/x86/sha-x86-ssse3.c delete mode 100644 lib/accelerated/x86/sha-x86.c diff --git a/lib/accelerated/x86/Makefile.am b/lib/accelerated/x86/Makefile.am index d552590..ec8bd3e 100644 --- a/lib/accelerated/x86/Makefile.am +++ b/lib/accelerated/x86/Makefile.am @@ -36,7 +36,7 @@ EXTRA_DIST = README license.txt files.mk noinst_LTLIBRARIES = libx86.la libx86_la_SOURCES = sha-padlock.c hmac-padlock.c aes-x86.c aes-padlock.c aes-gcm-padlock.c \ - aes-padlock.h aes-x86.h x86.h sha-padlock.h sha-x86.c sha-x86.h hmac-x86.c \ + aes-padlock.h aes-x86.h x86.h sha-padlock.h sha-x86-ssse3.c sha-x86.h hmac-x86-ssse3.c \ aes-gcm-x86-ssse3.c include files.mk diff --git a/lib/accelerated/x86/hmac-x86-ssse3.c b/lib/accelerated/x86/hmac-x86-ssse3.c new file mode 100644 index 0000000..3058ad6 --- /dev/null +++ b/lib/accelerated/x86/hmac-x86-ssse3.c @@ -0,0 +1,300 @@ +/* + * Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc. + * + * Author: Nikos Mavrogiannopoulos + * + * This file is part of GNUTLS. + * + * The GNUTLS library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/* This file provides the backend hash/mac implementation for + * VIA Padlock hardware acceleration. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_LIBNETTLE + +typedef void (*update_func) (void *, unsigned, const uint8_t *); +typedef void (*digest_func) (void *, unsigned, uint8_t *); +typedef void (*set_key_func) (void *, unsigned, const uint8_t *); + +struct x86_hmac_ctx { + union { + struct hmac_sha1_ctx sha1; + struct hmac_sha224_ctx sha224; + struct hmac_sha256_ctx sha256; +#ifdef ENABLE_SHA512 + struct hmac_sha384_ctx sha384; + struct hmac_sha512_ctx sha512; +#endif + } ctx; + + void *ctx_ptr; + gnutls_mac_algorithm_t algo; + size_t length; + update_func update; + digest_func digest; + set_key_func setkey; +}; + +static void +x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx, + unsigned key_length, const uint8_t * key) +{ + HMAC_SET_KEY(ctx, &x86_sha1, key_length, key); +} + +static void +x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx, + unsigned length, const uint8_t * data) +{ + x86_sha1_update(&ctx->state, length, data); +} + +static void +x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx, + unsigned length, uint8_t * digest) +{ + HMAC_DIGEST(ctx, &x86_sha1, length, digest); +} + +static void +x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx, + unsigned key_length, const uint8_t * key) +{ + HMAC_SET_KEY(ctx, &x86_sha256, key_length, key); +} + +static void +x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx, + unsigned length, const uint8_t * data) +{ + x86_sha256_update(&ctx->state, length, data); +} + +static void +x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx, + unsigned length, uint8_t * digest) +{ + HMAC_DIGEST(ctx, &x86_sha256, length, digest); +} + +static void +x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx, + unsigned key_length, const uint8_t * key) +{ + HMAC_SET_KEY(ctx, &x86_sha224, key_length, key); +} + +static void +x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx, + unsigned length, uint8_t * digest) +{ + HMAC_DIGEST(ctx, &x86_sha224, length, digest); +} + +#ifdef ENABLE_SHA512 +static void +x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx, + unsigned key_length, const uint8_t * key) +{ + HMAC_SET_KEY(ctx, &x86_sha384, key_length, key); +} + +static void +x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx, + unsigned length, uint8_t * digest) +{ + HMAC_DIGEST(ctx, &x86_sha384, length, digest); +} + +static void +x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx, + unsigned key_length, const uint8_t * key) +{ + HMAC_SET_KEY(ctx, &x86_sha512, key_length, key); +} + +static void +x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx, + unsigned length, const uint8_t * data) +{ + x86_sha512_update(&ctx->state, length, data); +} + +static void +x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx, + unsigned length, uint8_t * digest) +{ + HMAC_DIGEST(ctx, &x86_sha512, length, digest); +} +#endif + +static int +_hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx) +{ + switch (algo) { + case GNUTLS_MAC_SHA1: + ctx->update = (update_func) x86_hmac_sha1_update; + ctx->digest = (digest_func) x86_hmac_sha1_digest; + ctx->setkey = (set_key_func) x86_hmac_sha1_set_key; + ctx->ctx_ptr = &ctx->ctx.sha1; + ctx->length = SHA1_DIGEST_SIZE; + break; + case GNUTLS_MAC_SHA224: + ctx->update = (update_func) x86_hmac_sha256_update; + ctx->digest = (digest_func) x86_hmac_sha224_digest; + ctx->setkey = (set_key_func) x86_hmac_sha224_set_key; + ctx->ctx_ptr = &ctx->ctx.sha224; + ctx->length = SHA224_DIGEST_SIZE; + break; + case GNUTLS_MAC_SHA256: + ctx->update = (update_func) x86_hmac_sha256_update; + ctx->digest = (digest_func) x86_hmac_sha256_digest; + ctx->setkey = (set_key_func) x86_hmac_sha256_set_key; + ctx->ctx_ptr = &ctx->ctx.sha256; + ctx->length = SHA256_DIGEST_SIZE; + break; +#ifdef ENABLE_SHA512 + case GNUTLS_MAC_SHA384: + ctx->update = (update_func) x86_hmac_sha512_update; + ctx->digest = (digest_func) x86_hmac_sha384_digest; + ctx->setkey = (set_key_func) x86_hmac_sha384_set_key; + ctx->ctx_ptr = &ctx->ctx.sha384; + ctx->length = SHA384_DIGEST_SIZE; + break; + case GNUTLS_MAC_SHA512: + ctx->update = (update_func) x86_hmac_sha512_update; + ctx->digest = (digest_func) x86_hmac_sha512_digest; + ctx->setkey = (set_key_func) x86_hmac_sha512_set_key; + ctx->ctx_ptr = &ctx->ctx.sha512; + ctx->length = SHA512_DIGEST_SIZE; + break; +#endif + default: + gnutls_assert(); + return GNUTLS_E_INVALID_REQUEST; + } + + return 0; +} + + +static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx) +{ + struct x86_hmac_ctx *ctx; + int ret; + + ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx)); + if (ctx == NULL) { + gnutls_assert(); + return GNUTLS_E_MEMORY_ERROR; + } + + ctx->algo = algo; + + ret = _hmac_ctx_init(algo, ctx); + if (ret < 0) + return gnutls_assert_val(ret); + + *_ctx = ctx; + + return 0; +} + +static int +wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen) +{ + struct x86_hmac_ctx *ctx = _ctx; + + ctx->setkey(ctx->ctx_ptr, keylen, key); + + return GNUTLS_E_SUCCESS; +} + +static int +wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize) +{ + struct x86_hmac_ctx *ctx = _ctx; + + ctx->update(ctx->ctx_ptr, textsize, text); + + return GNUTLS_E_SUCCESS; +} + +static int +wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize) +{ + struct x86_hmac_ctx *ctx; + ctx = src_ctx; + + if (digestsize < ctx->length) { + gnutls_assert(); + return GNUTLS_E_SHORT_MEMORY_BUFFER; + } + + ctx->digest(ctx->ctx_ptr, digestsize, digest); + + return 0; +} + +static void wrap_x86_hmac_deinit(void *hd) +{ + gnutls_free(hd); +} + +static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo, + const void *nonce, size_t nonce_size, + const void *key, size_t key_size, + const void *text, size_t text_size, + void *digest) +{ + struct x86_hmac_ctx ctx; + int ret; + + ret = _hmac_ctx_init(algo, &ctx); + if (ret < 0) + return gnutls_assert_val(ret); + + ctx.setkey(&ctx, key_size, key); + ctx.update(&ctx, text_size, text); + ctx.digest(&ctx, ctx.length, digest); + + zeroize_temp_key(&ctx, sizeof(ctx)); + + return 0; +} + +const gnutls_crypto_mac_st hmac_sha_x86_ssse3 = { + .init = wrap_x86_hmac_init, + .setkey = wrap_x86_hmac_setkey, + .setnonce = NULL, + .hash = wrap_x86_hmac_update, + .output = wrap_x86_hmac_output, + .deinit = wrap_x86_hmac_deinit, + .fast = wrap_x86_hmac_fast, +}; + +#endif /* HAVE_LIBNETTLE */ diff --git a/lib/accelerated/x86/hmac-x86.c b/lib/accelerated/x86/hmac-x86.c deleted file mode 100644 index 3058ad6..0000000 --- a/lib/accelerated/x86/hmac-x86.c +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc. - * - * Author: Nikos Mavrogiannopoulos - * - * This file is part of GNUTLS. - * - * The GNUTLS library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see - * - */ - -/* This file provides the backend hash/mac implementation for - * VIA Padlock hardware acceleration. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef HAVE_LIBNETTLE - -typedef void (*update_func) (void *, unsigned, const uint8_t *); -typedef void (*digest_func) (void *, unsigned, uint8_t *); -typedef void (*set_key_func) (void *, unsigned, const uint8_t *); - -struct x86_hmac_ctx { - union { - struct hmac_sha1_ctx sha1; - struct hmac_sha224_ctx sha224; - struct hmac_sha256_ctx sha256; -#ifdef ENABLE_SHA512 - struct hmac_sha384_ctx sha384; - struct hmac_sha512_ctx sha512; -#endif - } ctx; - - void *ctx_ptr; - gnutls_mac_algorithm_t algo; - size_t length; - update_func update; - digest_func digest; - set_key_func setkey; -}; - -static void -x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx, - unsigned key_length, const uint8_t * key) -{ - HMAC_SET_KEY(ctx, &x86_sha1, key_length, key); -} - -static void -x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx, - unsigned length, const uint8_t * data) -{ - x86_sha1_update(&ctx->state, length, data); -} - -static void -x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx, - unsigned length, uint8_t * digest) -{ - HMAC_DIGEST(ctx, &x86_sha1, length, digest); -} - -static void -x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx, - unsigned key_length, const uint8_t * key) -{ - HMAC_SET_KEY(ctx, &x86_sha256, key_length, key); -} - -static void -x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx, - unsigned length, const uint8_t * data) -{ - x86_sha256_update(&ctx->state, length, data); -} - -static void -x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx, - unsigned length, uint8_t * digest) -{ - HMAC_DIGEST(ctx, &x86_sha256, length, digest); -} - -static void -x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx, - unsigned key_length, const uint8_t * key) -{ - HMAC_SET_KEY(ctx, &x86_sha224, key_length, key); -} - -static void -x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx, - unsigned length, uint8_t * digest) -{ - HMAC_DIGEST(ctx, &x86_sha224, length, digest); -} - -#ifdef ENABLE_SHA512 -static void -x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx, - unsigned key_length, const uint8_t * key) -{ - HMAC_SET_KEY(ctx, &x86_sha384, key_length, key); -} - -static void -x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx, - unsigned length, uint8_t * digest) -{ - HMAC_DIGEST(ctx, &x86_sha384, length, digest); -} - -static void -x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx, - unsigned key_length, const uint8_t * key) -{ - HMAC_SET_KEY(ctx, &x86_sha512, key_length, key); -} - -static void -x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx, - unsigned length, const uint8_t * data) -{ - x86_sha512_update(&ctx->state, length, data); -} - -static void -x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx, - unsigned length, uint8_t * digest) -{ - HMAC_DIGEST(ctx, &x86_sha512, length, digest); -} -#endif - -static int -_hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx) -{ - switch (algo) { - case GNUTLS_MAC_SHA1: - ctx->update = (update_func) x86_hmac_sha1_update; - ctx->digest = (digest_func) x86_hmac_sha1_digest; - ctx->setkey = (set_key_func) x86_hmac_sha1_set_key; - ctx->ctx_ptr = &ctx->ctx.sha1; - ctx->length = SHA1_DIGEST_SIZE; - break; - case GNUTLS_MAC_SHA224: - ctx->update = (update_func) x86_hmac_sha256_update; - ctx->digest = (digest_func) x86_hmac_sha224_digest; - ctx->setkey = (set_key_func) x86_hmac_sha224_set_key; - ctx->ctx_ptr = &ctx->ctx.sha224; - ctx->length = SHA224_DIGEST_SIZE; - break; - case GNUTLS_MAC_SHA256: - ctx->update = (update_func) x86_hmac_sha256_update; - ctx->digest = (digest_func) x86_hmac_sha256_digest; - ctx->setkey = (set_key_func) x86_hmac_sha256_set_key; - ctx->ctx_ptr = &ctx->ctx.sha256; - ctx->length = SHA256_DIGEST_SIZE; - break; -#ifdef ENABLE_SHA512 - case GNUTLS_MAC_SHA384: - ctx->update = (update_func) x86_hmac_sha512_update; - ctx->digest = (digest_func) x86_hmac_sha384_digest; - ctx->setkey = (set_key_func) x86_hmac_sha384_set_key; - ctx->ctx_ptr = &ctx->ctx.sha384; - ctx->length = SHA384_DIGEST_SIZE; - break; - case GNUTLS_MAC_SHA512: - ctx->update = (update_func) x86_hmac_sha512_update; - ctx->digest = (digest_func) x86_hmac_sha512_digest; - ctx->setkey = (set_key_func) x86_hmac_sha512_set_key; - ctx->ctx_ptr = &ctx->ctx.sha512; - ctx->length = SHA512_DIGEST_SIZE; - break; -#endif - default: - gnutls_assert(); - return GNUTLS_E_INVALID_REQUEST; - } - - return 0; -} - - -static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx) -{ - struct x86_hmac_ctx *ctx; - int ret; - - ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx)); - if (ctx == NULL) { - gnutls_assert(); - return GNUTLS_E_MEMORY_ERROR; - } - - ctx->algo = algo; - - ret = _hmac_ctx_init(algo, ctx); - if (ret < 0) - return gnutls_assert_val(ret); - - *_ctx = ctx; - - return 0; -} - -static int -wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen) -{ - struct x86_hmac_ctx *ctx = _ctx; - - ctx->setkey(ctx->ctx_ptr, keylen, key); - - return GNUTLS_E_SUCCESS; -} - -static int -wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize) -{ - struct x86_hmac_ctx *ctx = _ctx; - - ctx->update(ctx->ctx_ptr, textsize, text); - - return GNUTLS_E_SUCCESS; -} - -static int -wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize) -{ - struct x86_hmac_ctx *ctx; - ctx = src_ctx; - - if (digestsize < ctx->length) { - gnutls_assert(); - return GNUTLS_E_SHORT_MEMORY_BUFFER; - } - - ctx->digest(ctx->ctx_ptr, digestsize, digest); - - return 0; -} - -static void wrap_x86_hmac_deinit(void *hd) -{ - gnutls_free(hd); -} - -static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo, - const void *nonce, size_t nonce_size, - const void *key, size_t key_size, - const void *text, size_t text_size, - void *digest) -{ - struct x86_hmac_ctx ctx; - int ret; - - ret = _hmac_ctx_init(algo, &ctx); - if (ret < 0) - return gnutls_assert_val(ret); - - ctx.setkey(&ctx, key_size, key); - ctx.update(&ctx, text_size, text); - ctx.digest(&ctx, ctx.length, digest); - - zeroize_temp_key(&ctx, sizeof(ctx)); - - return 0; -} - -const gnutls_crypto_mac_st hmac_sha_x86_ssse3 = { - .init = wrap_x86_hmac_init, - .setkey = wrap_x86_hmac_setkey, - .setnonce = NULL, - .hash = wrap_x86_hmac_update, - .output = wrap_x86_hmac_output, - .deinit = wrap_x86_hmac_deinit, - .fast = wrap_x86_hmac_fast, -}; - -#endif /* HAVE_LIBNETTLE */ diff --git a/lib/accelerated/x86/sha-x86-ssse3.c b/lib/accelerated/x86/sha-x86-ssse3.c new file mode 100644 index 0000000..29dcd2d --- /dev/null +++ b/lib/accelerated/x86/sha-x86-ssse3.c @@ -0,0 +1,365 @@ +/* + * Copyright (C) 2011-2012 Free Software Foundation, Inc. + * + * Author: Nikos Mavrogiannopoulos + * + * This file is part of GnuTLS. + * + * The GnuTLS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void sha1_block_data_order(void *c, const void *p, size_t len); +void sha256_block_data_order(void *c, const void *p, size_t len); +void sha512_block_data_order(void *c, const void *p, size_t len); + +typedef void (*update_func) (void *, unsigned, const uint8_t *); +typedef void (*digest_func) (void *, unsigned, uint8_t *); +typedef void (*set_key_func) (void *, unsigned, const uint8_t *); +typedef void (*init_func) (void *); + +struct x86_hash_ctx { + union { + struct sha1_ctx sha1; + struct sha224_ctx sha224; + struct sha256_ctx sha256; +#ifdef ENABLE_SHA512 + struct sha384_ctx sha384; + struct sha512_ctx sha512; +#endif + } ctx; + void *ctx_ptr; + gnutls_digest_algorithm_t algo; + size_t length; + update_func update; + digest_func digest; + init_func init; +}; + +static int +wrap_x86_hash_update(void *_ctx, const void *text, size_t textsize) +{ + struct x86_hash_ctx *ctx = _ctx; + + ctx->update(ctx->ctx_ptr, textsize, text); + + return GNUTLS_E_SUCCESS; +} + +static void wrap_x86_hash_deinit(void *hd) +{ + gnutls_free(hd); +} + +void x86_sha1_update(struct sha1_ctx *ctx, size_t length, + const uint8_t * data) +{ + struct { + uint32_t h0, h1, h2, h3, h4; + uint32_t Nl, Nh; + uint32_t data[16]; + unsigned int num; + } octx; + size_t res; + unsigned t2, i; + + if ((res = ctx->index)) { + res = SHA1_DATA_SIZE - res; + if (length < res) + res = length; + sha1_update(ctx, res, data); + data += res; + length -= res; + } + + octx.h0 = ctx->state[0]; + octx.h1 = ctx->state[1]; + octx.h2 = ctx->state[2]; + octx.h3 = ctx->state[3]; + octx.h4 = ctx->state[4]; + + memcpy(octx.data, ctx->block, SHA1_DATA_SIZE); + octx.num = ctx->index; + + res = length % SHA1_DATA_SIZE; + length -= res; + + if (length > 0) { + + t2 = length / SHA1_DATA_SIZE; + + sha1_block_data_order(&octx, data, t2); + + for (i=0;istate[0] = octx.h0; + ctx->state[1] = octx.h1; + ctx->state[2] = octx.h2; + ctx->state[3] = octx.h3; + ctx->state[4] = octx.h4; + + memcpy(ctx->block, octx.data, octx.num); + ctx->index = octx.num; + + if (res > 0) { + sha1_update(ctx, res, data); + } + +} + +void x86_sha256_update(struct sha256_ctx *ctx, size_t length, + const uint8_t * data) +{ + struct { + uint32_t h[8]; + uint32_t Nl, Nh; + uint32_t data[16]; + unsigned int num; + unsigned md_len; + } octx; + size_t res; + unsigned t2, i; + + if ((res = ctx->index)) { + res = SHA256_DATA_SIZE - res; + if (length < res) + res = length; + sha256_update(ctx, res, data); + data += res; + length -= res; + } + + memcpy(octx.h, ctx->state, sizeof(octx.h)); + memcpy(octx.data, ctx->block, SHA256_DATA_SIZE); + octx.num = ctx->index; + + res = length % SHA256_DATA_SIZE; + length -= res; + + if (length > 0) { + t2 = length / SHA1_DATA_SIZE; + sha256_block_data_order(&octx, data, t2); + + for (i=0;istate, octx.h, sizeof(octx.h)); + + memcpy(ctx->block, octx.data, octx.num); + ctx->index = octx.num; + + if (res > 0) { + sha256_update(ctx, res, data); + } +} + +#ifdef ENABLE_SHA512 +void x86_sha512_update(struct sha512_ctx *ctx, size_t length, + const uint8_t * data) +{ + struct { + uint64_t h[8]; + uint64_t Nl, Nh; + union { + uint64_t d[16]; + uint8_t p[16*8]; + } u; + unsigned int num; + unsigned md_len; + } octx; + size_t res; + unsigned t2, i; + + if ((res = ctx->index)) { + res = SHA512_DATA_SIZE - res; + if (length < res) + res = length; + sha512_update(ctx, res, data); + data += res; + length -= res; + } + + memcpy(octx.h, ctx->state, sizeof(octx.h)); + memcpy(octx.u.p, ctx->block, SHA512_DATA_SIZE); + octx.num = ctx->index; + + res = length % SHA512_DATA_SIZE; + length -= res; + + if (length > 0) { + t2 = length / SHA512_DATA_SIZE; + sha512_block_data_order(&octx, data, t2); + + for (i=0;istate, octx.h, sizeof(octx.h)); + + memcpy(ctx->block, octx.u.p, octx.num); + ctx->index = octx.num; + + if (res > 0) { + sha512_update(ctx, res, data); + } +} +#endif + +static int _ctx_init(gnutls_digest_algorithm_t algo, + struct x86_hash_ctx *ctx) +{ + switch (algo) { + case GNUTLS_DIG_SHA1: + sha1_init(&ctx->ctx.sha1); + ctx->update = (update_func) x86_sha1_update; + ctx->digest = (digest_func) sha1_digest; + ctx->init = (init_func) sha1_init; + ctx->ctx_ptr = &ctx->ctx.sha1; + ctx->length = SHA1_DIGEST_SIZE; + break; + case GNUTLS_DIG_SHA224: + sha224_init(&ctx->ctx.sha224); + ctx->update = (update_func) x86_sha256_update; + ctx->digest = (digest_func) sha256_digest; + ctx->init = (init_func) sha224_init; + ctx->ctx_ptr = &ctx->ctx.sha224; + ctx->length = SHA224_DIGEST_SIZE; + break; + case GNUTLS_DIG_SHA256: + sha256_init(&ctx->ctx.sha256); + ctx->update = (update_func) x86_sha256_update; + ctx->digest = (digest_func) sha256_digest; + ctx->init = (init_func) sha256_init; + ctx->ctx_ptr = &ctx->ctx.sha256; + ctx->length = SHA256_DIGEST_SIZE; + break; +#ifdef ENABLE_SHA512 + case GNUTLS_DIG_SHA384: + sha384_init(&ctx->ctx.sha384); + ctx->update = (update_func) x86_sha512_update; + ctx->digest = (digest_func) sha512_digest; + ctx->init = (init_func) sha384_init; + ctx->ctx_ptr = &ctx->ctx.sha384; + ctx->length = SHA384_DIGEST_SIZE; + break; + case GNUTLS_DIG_SHA512: + sha512_init(&ctx->ctx.sha512); + ctx->update = (update_func) x86_sha512_update; + ctx->digest = (digest_func) sha512_digest; + ctx->init = (init_func) sha512_init; + ctx->ctx_ptr = &ctx->ctx.sha512; + ctx->length = SHA512_DIGEST_SIZE; + break; +#endif + default: + gnutls_assert(); + return GNUTLS_E_INVALID_REQUEST; + } + + return 0; +} + + +static int wrap_x86_hash_init(gnutls_digest_algorithm_t algo, void **_ctx) +{ + struct x86_hash_ctx *ctx; + int ret; + + ctx = gnutls_malloc(sizeof(struct x86_hash_ctx)); + if (ctx == NULL) { + gnutls_assert(); + return GNUTLS_E_MEMORY_ERROR; + } + + ctx->algo = algo; + + if ((ret = _ctx_init(algo, ctx)) < 0) { + gnutls_assert(); + return ret; + } + + *_ctx = ctx; + + return 0; +} + +static int +wrap_x86_hash_output(void *src_ctx, void *digest, size_t digestsize) +{ + struct x86_hash_ctx *ctx; + ctx = src_ctx; + + if (digestsize < ctx->length) + return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); + + ctx->digest(ctx->ctx_ptr, digestsize, digest); + + return 0; +} + +static int wrap_x86_hash_fast(gnutls_digest_algorithm_t algo, + const void *text, size_t text_size, + void *digest) +{ + struct x86_hash_ctx ctx; + int ret; + + ret = _ctx_init(algo, &ctx); + if (ret < 0) + return gnutls_assert_val(ret); + + ctx.update(&ctx, text_size, text); + ctx.digest(&ctx, ctx.length, digest); + + return 0; +} + +const struct nettle_hash x86_sha1 = +NN_HASH(sha1, x86_sha1_update, sha1_digest, SHA1); +const struct nettle_hash x86_sha224 = +NN_HASH(sha224, x86_sha256_update, sha224_digest, SHA224); +const struct nettle_hash x86_sha256 = +NN_HASH(sha256, x86_sha256_update, sha256_digest, SHA256); + +#ifdef ENABLE_SHA512 +const struct nettle_hash x86_sha384 = +NN_HASH(sha384, x86_sha512_update, sha384_digest, SHA384); +const struct nettle_hash x86_sha512 = +NN_HASH(sha512, x86_sha512_update, sha512_digest, SHA512); +#endif + +const gnutls_crypto_digest_st sha_x86_ssse3 = { + .init = wrap_x86_hash_init, + .hash = wrap_x86_hash_update, + .output = wrap_x86_hash_output, + .deinit = wrap_x86_hash_deinit, + .fast = wrap_x86_hash_fast, +}; diff --git a/lib/accelerated/x86/sha-x86.c b/lib/accelerated/x86/sha-x86.c deleted file mode 100644 index 29dcd2d..0000000 --- a/lib/accelerated/x86/sha-x86.c +++ /dev/null @@ -1,365 +0,0 @@ -/* - * Copyright (C) 2011-2012 Free Software Foundation, Inc. - * - * Author: Nikos Mavrogiannopoulos - * - * This file is part of GnuTLS. - * - * The GnuTLS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -void sha1_block_data_order(void *c, const void *p, size_t len); -void sha256_block_data_order(void *c, const void *p, size_t len); -void sha512_block_data_order(void *c, const void *p, size_t len); - -typedef void (*update_func) (void *, unsigned, const uint8_t *); -typedef void (*digest_func) (void *, unsigned, uint8_t *); -typedef void (*set_key_func) (void *, unsigned, const uint8_t *); -typedef void (*init_func) (void *); - -struct x86_hash_ctx { - union { - struct sha1_ctx sha1; - struct sha224_ctx sha224; - struct sha256_ctx sha256; -#ifdef ENABLE_SHA512 - struct sha384_ctx sha384; - struct sha512_ctx sha512; -#endif - } ctx; - void *ctx_ptr; - gnutls_digest_algorithm_t algo; - size_t length; - update_func update; - digest_func digest; - init_func init; -}; - -static int -wrap_x86_hash_update(void *_ctx, const void *text, size_t textsize) -{ - struct x86_hash_ctx *ctx = _ctx; - - ctx->update(ctx->ctx_ptr, textsize, text); - - return GNUTLS_E_SUCCESS; -} - -static void wrap_x86_hash_deinit(void *hd) -{ - gnutls_free(hd); -} - -void x86_sha1_update(struct sha1_ctx *ctx, size_t length, - const uint8_t * data) -{ - struct { - uint32_t h0, h1, h2, h3, h4; - uint32_t Nl, Nh; - uint32_t data[16]; - unsigned int num; - } octx; - size_t res; - unsigned t2, i; - - if ((res = ctx->index)) { - res = SHA1_DATA_SIZE - res; - if (length < res) - res = length; - sha1_update(ctx, res, data); - data += res; - length -= res; - } - - octx.h0 = ctx->state[0]; - octx.h1 = ctx->state[1]; - octx.h2 = ctx->state[2]; - octx.h3 = ctx->state[3]; - octx.h4 = ctx->state[4]; - - memcpy(octx.data, ctx->block, SHA1_DATA_SIZE); - octx.num = ctx->index; - - res = length % SHA1_DATA_SIZE; - length -= res; - - if (length > 0) { - - t2 = length / SHA1_DATA_SIZE; - - sha1_block_data_order(&octx, data, t2); - - for (i=0;istate[0] = octx.h0; - ctx->state[1] = octx.h1; - ctx->state[2] = octx.h2; - ctx->state[3] = octx.h3; - ctx->state[4] = octx.h4; - - memcpy(ctx->block, octx.data, octx.num); - ctx->index = octx.num; - - if (res > 0) { - sha1_update(ctx, res, data); - } - -} - -void x86_sha256_update(struct sha256_ctx *ctx, size_t length, - const uint8_t * data) -{ - struct { - uint32_t h[8]; - uint32_t Nl, Nh; - uint32_t data[16]; - unsigned int num; - unsigned md_len; - } octx; - size_t res; - unsigned t2, i; - - if ((res = ctx->index)) { - res = SHA256_DATA_SIZE - res; - if (length < res) - res = length; - sha256_update(ctx, res, data); - data += res; - length -= res; - } - - memcpy(octx.h, ctx->state, sizeof(octx.h)); - memcpy(octx.data, ctx->block, SHA256_DATA_SIZE); - octx.num = ctx->index; - - res = length % SHA256_DATA_SIZE; - length -= res; - - if (length > 0) { - t2 = length / SHA1_DATA_SIZE; - sha256_block_data_order(&octx, data, t2); - - for (i=0;istate, octx.h, sizeof(octx.h)); - - memcpy(ctx->block, octx.data, octx.num); - ctx->index = octx.num; - - if (res > 0) { - sha256_update(ctx, res, data); - } -} - -#ifdef ENABLE_SHA512 -void x86_sha512_update(struct sha512_ctx *ctx, size_t length, - const uint8_t * data) -{ - struct { - uint64_t h[8]; - uint64_t Nl, Nh; - union { - uint64_t d[16]; - uint8_t p[16*8]; - } u; - unsigned int num; - unsigned md_len; - } octx; - size_t res; - unsigned t2, i; - - if ((res = ctx->index)) { - res = SHA512_DATA_SIZE - res; - if (length < res) - res = length; - sha512_update(ctx, res, data); - data += res; - length -= res; - } - - memcpy(octx.h, ctx->state, sizeof(octx.h)); - memcpy(octx.u.p, ctx->block, SHA512_DATA_SIZE); - octx.num = ctx->index; - - res = length % SHA512_DATA_SIZE; - length -= res; - - if (length > 0) { - t2 = length / SHA512_DATA_SIZE; - sha512_block_data_order(&octx, data, t2); - - for (i=0;istate, octx.h, sizeof(octx.h)); - - memcpy(ctx->block, octx.u.p, octx.num); - ctx->index = octx.num; - - if (res > 0) { - sha512_update(ctx, res, data); - } -} -#endif - -static int _ctx_init(gnutls_digest_algorithm_t algo, - struct x86_hash_ctx *ctx) -{ - switch (algo) { - case GNUTLS_DIG_SHA1: - sha1_init(&ctx->ctx.sha1); - ctx->update = (update_func) x86_sha1_update; - ctx->digest = (digest_func) sha1_digest; - ctx->init = (init_func) sha1_init; - ctx->ctx_ptr = &ctx->ctx.sha1; - ctx->length = SHA1_DIGEST_SIZE; - break; - case GNUTLS_DIG_SHA224: - sha224_init(&ctx->ctx.sha224); - ctx->update = (update_func) x86_sha256_update; - ctx->digest = (digest_func) sha256_digest; - ctx->init = (init_func) sha224_init; - ctx->ctx_ptr = &ctx->ctx.sha224; - ctx->length = SHA224_DIGEST_SIZE; - break; - case GNUTLS_DIG_SHA256: - sha256_init(&ctx->ctx.sha256); - ctx->update = (update_func) x86_sha256_update; - ctx->digest = (digest_func) sha256_digest; - ctx->init = (init_func) sha256_init; - ctx->ctx_ptr = &ctx->ctx.sha256; - ctx->length = SHA256_DIGEST_SIZE; - break; -#ifdef ENABLE_SHA512 - case GNUTLS_DIG_SHA384: - sha384_init(&ctx->ctx.sha384); - ctx->update = (update_func) x86_sha512_update; - ctx->digest = (digest_func) sha512_digest; - ctx->init = (init_func) sha384_init; - ctx->ctx_ptr = &ctx->ctx.sha384; - ctx->length = SHA384_DIGEST_SIZE; - break; - case GNUTLS_DIG_SHA512: - sha512_init(&ctx->ctx.sha512); - ctx->update = (update_func) x86_sha512_update; - ctx->digest = (digest_func) sha512_digest; - ctx->init = (init_func) sha512_init; - ctx->ctx_ptr = &ctx->ctx.sha512; - ctx->length = SHA512_DIGEST_SIZE; - break; -#endif - default: - gnutls_assert(); - return GNUTLS_E_INVALID_REQUEST; - } - - return 0; -} - - -static int wrap_x86_hash_init(gnutls_digest_algorithm_t algo, void **_ctx) -{ - struct x86_hash_ctx *ctx; - int ret; - - ctx = gnutls_malloc(sizeof(struct x86_hash_ctx)); - if (ctx == NULL) { - gnutls_assert(); - return GNUTLS_E_MEMORY_ERROR; - } - - ctx->algo = algo; - - if ((ret = _ctx_init(algo, ctx)) < 0) { - gnutls_assert(); - return ret; - } - - *_ctx = ctx; - - return 0; -} - -static int -wrap_x86_hash_output(void *src_ctx, void *digest, size_t digestsize) -{ - struct x86_hash_ctx *ctx; - ctx = src_ctx; - - if (digestsize < ctx->length) - return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); - - ctx->digest(ctx->ctx_ptr, digestsize, digest); - - return 0; -} - -static int wrap_x86_hash_fast(gnutls_digest_algorithm_t algo, - const void *text, size_t text_size, - void *digest) -{ - struct x86_hash_ctx ctx; - int ret; - - ret = _ctx_init(algo, &ctx); - if (ret < 0) - return gnutls_assert_val(ret); - - ctx.update(&ctx, text_size, text); - ctx.digest(&ctx, ctx.length, digest); - - return 0; -} - -const struct nettle_hash x86_sha1 = -NN_HASH(sha1, x86_sha1_update, sha1_digest, SHA1); -const struct nettle_hash x86_sha224 = -NN_HASH(sha224, x86_sha256_update, sha224_digest, SHA224); -const struct nettle_hash x86_sha256 = -NN_HASH(sha256, x86_sha256_update, sha256_digest, SHA256); - -#ifdef ENABLE_SHA512 -const struct nettle_hash x86_sha384 = -NN_HASH(sha384, x86_sha512_update, sha384_digest, SHA384); -const struct nettle_hash x86_sha512 = -NN_HASH(sha512, x86_sha512_update, sha512_digest, SHA512); -#endif - -const gnutls_crypto_digest_st sha_x86_ssse3 = { - .init = wrap_x86_hash_init, - .hash = wrap_x86_hash_update, - .output = wrap_x86_hash_output, - .deinit = wrap_x86_hash_deinit, - .fast = wrap_x86_hash_fast, -}; -- 2.1.4