|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* Author: Nikos Mavrogiannopoulos
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* This file is part of GNUTLS.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* The GNUTLS library is free software; you can redistribute it and/or
|
|
Packit |
aea12f |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit |
aea12f |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit |
aea12f |
* the License, or (at your option) any later version.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* This library is distributed in the hope that it will be useful, but
|
|
Packit |
aea12f |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
aea12f |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
aea12f |
* Lesser General Public License for more details.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
aea12f |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* This file provides the backend hash/mac implementation for
|
|
Packit |
aea12f |
* VIA Padlock hardware acceleration.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "gnutls_int.h"
|
|
Packit |
aea12f |
#include <hash_int.h>
|
|
Packit |
aea12f |
#include "errors.h"
|
|
Packit |
aea12f |
#include <nettle/sha.h>
|
|
Packit |
aea12f |
#include <nettle/hmac.h>
|
|
Packit |
aea12f |
#include <nettle/macros.h>
|
|
Packit |
aea12f |
#include <aes-x86.h>
|
|
Packit |
aea12f |
#include <sha-x86.h>
|
|
Packit |
aea12f |
#include <algorithms.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef HAVE_LIBNETTLE
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
typedef void (*update_func) (void *, size_t, const uint8_t *);
|
|
Packit |
aea12f |
typedef void (*digest_func) (void *, size_t, uint8_t *);
|
|
Packit |
aea12f |
typedef void (*set_key_func) (void *, size_t, const uint8_t *);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
struct x86_hmac_ctx {
|
|
Packit |
aea12f |
union {
|
|
Packit |
aea12f |
struct hmac_sha1_ctx sha1;
|
|
Packit |
aea12f |
struct hmac_sha224_ctx sha224;
|
|
Packit |
aea12f |
struct hmac_sha256_ctx sha256;
|
|
Packit |
aea12f |
struct hmac_sha384_ctx sha384;
|
|
Packit |
aea12f |
struct hmac_sha512_ctx sha512;
|
|
Packit |
aea12f |
} ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void *ctx_ptr;
|
|
Packit |
aea12f |
gnutls_mac_algorithm_t algo;
|
|
Packit |
aea12f |
size_t length;
|
|
Packit |
aea12f |
update_func update;
|
|
Packit |
aea12f |
digest_func digest;
|
|
Packit |
aea12f |
set_key_func setkey;
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
|
|
Packit |
aea12f |
size_t key_length, const uint8_t * key)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_SET_KEY(ctx, &x86_sha1, key_length, key);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, const uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
x86_sha1_update(&ctx->state, length, data);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, uint8_t * digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_DIGEST(ctx, &x86_sha1, length, digest);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
|
|
Packit |
aea12f |
size_t key_length, const uint8_t * key)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_SET_KEY(ctx, &x86_sha256, key_length, key);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, const uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
x86_sha256_update(&ctx->state, length, data);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, uint8_t * digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_DIGEST(ctx, &x86_sha256, length, digest);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
|
|
Packit |
aea12f |
size_t key_length, const uint8_t * key)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_SET_KEY(ctx, &x86_sha224, key_length, key);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, uint8_t * digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_DIGEST(ctx, &x86_sha224, length, digest);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx,
|
|
Packit |
aea12f |
size_t key_length, const uint8_t * key)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_SET_KEY(ctx, &x86_sha384, key_length, key);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, uint8_t * digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_DIGEST(ctx, &x86_sha384, length, digest);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
|
|
Packit |
aea12f |
size_t key_length, const uint8_t * key)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_SET_KEY(ctx, &x86_sha512, key_length, key);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, const uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
x86_sha512_update(&ctx->state, length, data);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void
|
|
Packit |
aea12f |
x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx,
|
|
Packit |
aea12f |
size_t length, uint8_t * digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
HMAC_DIGEST(ctx, &x86_sha512, length, digest);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
_hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
switch (algo) {
|
|
Packit |
aea12f |
case GNUTLS_MAC_SHA1:
|
|
Packit |
aea12f |
ctx->update = (update_func) x86_hmac_sha1_update;
|
|
Packit |
aea12f |
ctx->digest = (digest_func) x86_hmac_sha1_digest;
|
|
Packit |
aea12f |
ctx->setkey = (set_key_func) x86_hmac_sha1_set_key;
|
|
Packit |
aea12f |
ctx->ctx_ptr = &ctx->ctx.sha1;
|
|
Packit |
aea12f |
ctx->length = SHA1_DIGEST_SIZE;
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
case GNUTLS_MAC_SHA224:
|
|
Packit |
aea12f |
ctx->update = (update_func) x86_hmac_sha256_update;
|
|
Packit |
aea12f |
ctx->digest = (digest_func) x86_hmac_sha224_digest;
|
|
Packit |
aea12f |
ctx->setkey = (set_key_func) x86_hmac_sha224_set_key;
|
|
Packit |
aea12f |
ctx->ctx_ptr = &ctx->ctx.sha224;
|
|
Packit |
aea12f |
ctx->length = SHA224_DIGEST_SIZE;
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
case GNUTLS_MAC_SHA256:
|
|
Packit |
aea12f |
ctx->update = (update_func) x86_hmac_sha256_update;
|
|
Packit |
aea12f |
ctx->digest = (digest_func) x86_hmac_sha256_digest;
|
|
Packit |
aea12f |
ctx->setkey = (set_key_func) x86_hmac_sha256_set_key;
|
|
Packit |
aea12f |
ctx->ctx_ptr = &ctx->ctx.sha256;
|
|
Packit |
aea12f |
ctx->length = SHA256_DIGEST_SIZE;
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
case GNUTLS_MAC_SHA384:
|
|
Packit |
aea12f |
ctx->update = (update_func) x86_hmac_sha512_update;
|
|
Packit |
aea12f |
ctx->digest = (digest_func) x86_hmac_sha384_digest;
|
|
Packit |
aea12f |
ctx->setkey = (set_key_func) x86_hmac_sha384_set_key;
|
|
Packit |
aea12f |
ctx->ctx_ptr = &ctx->ctx.sha384;
|
|
Packit |
aea12f |
ctx->length = SHA384_DIGEST_SIZE;
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
case GNUTLS_MAC_SHA512:
|
|
Packit |
aea12f |
ctx->update = (update_func) x86_hmac_sha512_update;
|
|
Packit |
aea12f |
ctx->digest = (digest_func) x86_hmac_sha512_digest;
|
|
Packit |
aea12f |
ctx->setkey = (set_key_func) x86_hmac_sha512_set_key;
|
|
Packit |
aea12f |
ctx->ctx_ptr = &ctx->ctx.sha512;
|
|
Packit |
aea12f |
ctx->length = SHA512_DIGEST_SIZE;
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
default:
|
|
Packit |
aea12f |
gnutls_assert();
|
|
Packit |
aea12f |
return GNUTLS_E_INVALID_REQUEST;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct x86_hmac_ctx *ctx;
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx));
|
|
Packit |
aea12f |
if (ctx == NULL) {
|
|
Packit |
aea12f |
gnutls_assert();
|
|
Packit |
aea12f |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ctx->algo = algo;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = _hmac_ctx_init(algo, ctx);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
return gnutls_assert_val(ret);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
*_ctx = ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit Service |
991b93 |
static void *
|
|
Packit Service |
991b93 |
wrap_x86_hmac_copy(const void *_ctx)
|
|
Packit Service |
991b93 |
{
|
|
Packit Service |
991b93 |
struct x86_hmac_ctx *new_ctx;
|
|
Packit Service |
991b93 |
const struct x86_hmac_ctx *ctx=_ctx;
|
|
Packit Service |
991b93 |
ptrdiff_t off = (uint8_t *)ctx->ctx_ptr - (uint8_t *)(&ctx->ctx);
|
|
Packit Service |
991b93 |
|
|
Packit Service |
991b93 |
new_ctx = gnutls_malloc(sizeof(struct x86_hmac_ctx));
|
|
Packit Service |
991b93 |
if (new_ctx == NULL) {
|
|
Packit Service |
991b93 |
gnutls_assert();
|
|
Packit Service |
991b93 |
return NULL;
|
|
Packit Service |
991b93 |
}
|
|
Packit Service |
991b93 |
|
|
Packit Service |
991b93 |
memcpy(new_ctx, ctx, sizeof(*new_ctx));
|
|
Packit Service |
991b93 |
new_ctx->ctx_ptr = (uint8_t *)&new_ctx->ctx + off;
|
|
Packit Service |
991b93 |
|
|
Packit Service |
991b93 |
return new_ctx;
|
|
Packit Service |
991b93 |
}
|
|
Packit Service |
991b93 |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct x86_hmac_ctx *ctx = _ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ctx->setkey(ctx->ctx_ptr, keylen, key);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return GNUTLS_E_SUCCESS;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct x86_hmac_ctx *ctx = _ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ctx->update(ctx->ctx_ptr, textsize, text);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return GNUTLS_E_SUCCESS;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct x86_hmac_ctx *ctx;
|
|
Packit |
aea12f |
ctx = src_ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (digestsize < ctx->length) {
|
|
Packit |
aea12f |
gnutls_assert();
|
|
Packit |
aea12f |
return GNUTLS_E_SHORT_MEMORY_BUFFER;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ctx->digest(ctx->ctx_ptr, digestsize, digest);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void wrap_x86_hmac_deinit(void *hd)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
gnutls_free(hd);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo,
|
|
Packit |
aea12f |
const void *nonce, size_t nonce_size,
|
|
Packit |
aea12f |
const void *key, size_t key_size,
|
|
Packit |
aea12f |
const void *text, size_t text_size,
|
|
Packit |
aea12f |
void *digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct x86_hmac_ctx ctx;
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = _hmac_ctx_init(algo, &ctx;;
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
return gnutls_assert_val(ret);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ctx.setkey(&ctx, key_size, key);
|
|
Packit |
aea12f |
ctx.update(&ctx, text_size, text);
|
|
Packit |
aea12f |
ctx.digest(&ctx, ctx.length, digest);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
zeroize_temp_key(&ctx, sizeof(ctx));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
const gnutls_crypto_mac_st _gnutls_hmac_sha_x86_ssse3 = {
|
|
Packit |
aea12f |
.init = wrap_x86_hmac_init,
|
|
Packit |
aea12f |
.setkey = wrap_x86_hmac_setkey,
|
|
Packit |
aea12f |
.setnonce = NULL,
|
|
Packit |
aea12f |
.hash = wrap_x86_hmac_update,
|
|
Packit |
aea12f |
.output = wrap_x86_hmac_output,
|
|
Packit Service |
991b93 |
.copy = wrap_x86_hmac_copy,
|
|
Packit |
aea12f |
.deinit = wrap_x86_hmac_deinit,
|
|
Packit |
aea12f |
.fast = wrap_x86_hmac_fast,
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif /* HAVE_LIBNETTLE */
|