|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2008-2016 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
* Copyright (C) 2016 Red Hat, 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 library 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 |
/* This file provides the backend hash/mac implementation for
|
|
Packit Service |
4684c1 |
* aarch64 hardware.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "gnutls_int.h"
|
|
Packit Service |
4684c1 |
#include <hash_int.h>
|
|
Packit Service |
4684c1 |
#include "errors.h"
|
|
Packit Service |
4684c1 |
#include <nettle/sha.h>
|
|
Packit Service |
4684c1 |
#include <nettle/hmac.h>
|
|
Packit Service |
4684c1 |
#include <nettle/macros.h>
|
|
Packit Service |
4684c1 |
#include <sha-aarch64.h>
|
|
Packit Service |
4684c1 |
#include <algorithms.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_LIBNETTLE
|
|
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 |
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_ctx {
|
|
Packit Service |
4684c1 |
union {
|
|
Packit Service |
4684c1 |
struct hmac_sha1_ctx sha1;
|
|
Packit Service |
4684c1 |
struct hmac_sha224_ctx sha224;
|
|
Packit Service |
4684c1 |
struct hmac_sha256_ctx sha256;
|
|
Packit Service |
4684c1 |
struct hmac_sha384_ctx sha384;
|
|
Packit Service |
4684c1 |
struct hmac_sha512_ctx sha512;
|
|
Packit Service |
4684c1 |
} ctx;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void *ctx_ptr;
|
|
Packit Service |
4684c1 |
gnutls_mac_algorithm_t algo;
|
|
Packit Service |
4684c1 |
size_t length;
|
|
Packit Service |
4684c1 |
update_func update;
|
|
Packit Service |
4684c1 |
digest_func digest;
|
|
Packit Service |
4684c1 |
set_key_func setkey;
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t key_length, const uint8_t * key)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_SET_KEY(ctx, &aarch64_sha1, key_length, key);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha1_update(struct hmac_sha1_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, const uint8_t * data)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
aarch64_sha1_update(&ctx->state, length, data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, uint8_t * digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_DIGEST(ctx, &aarch64_sha1, length, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t key_length, const uint8_t * key)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_SET_KEY(ctx, &aarch64_sha256, key_length, key);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha256_update(struct hmac_sha256_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, const uint8_t * data)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
aarch64_sha256_update(&ctx->state, length, data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, uint8_t * digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_DIGEST(ctx, &aarch64_sha256, length, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t key_length, const uint8_t * key)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_SET_KEY(ctx, &aarch64_sha224, key_length, key);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, uint8_t * digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_DIGEST(ctx, &aarch64_sha224, length, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t key_length, const uint8_t * key)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_SET_KEY(ctx, &aarch64_sha384, key_length, key);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha384_digest(struct hmac_sha384_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, uint8_t * digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_DIGEST(ctx, &aarch64_sha384, length, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t key_length, const uint8_t * key)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_SET_KEY(ctx, &aarch64_sha512, key_length, key);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha512_update(struct hmac_sha512_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, const uint8_t * data)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
aarch64_sha512_update(&ctx->state, length, data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
aarch64_hmac_sha512_digest(struct hmac_sha512_ctx *ctx,
|
|
Packit Service |
4684c1 |
size_t length, uint8_t * digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HMAC_DIGEST(ctx, &aarch64_sha512, length, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
_hmac_ctx_init(gnutls_mac_algorithm_t algo, struct aarch64_hmac_ctx *ctx)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
switch (algo) {
|
|
Packit Service |
4684c1 |
case GNUTLS_MAC_SHA1:
|
|
Packit Service |
4684c1 |
ctx->update = (update_func) aarch64_hmac_sha1_update;
|
|
Packit Service |
4684c1 |
ctx->digest = (digest_func) aarch64_hmac_sha1_digest;
|
|
Packit Service |
4684c1 |
ctx->setkey = (set_key_func) aarch64_hmac_sha1_set_key;
|
|
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_MAC_SHA224:
|
|
Packit Service |
4684c1 |
ctx->update = (update_func) aarch64_hmac_sha256_update;
|
|
Packit Service |
4684c1 |
ctx->digest = (digest_func) aarch64_hmac_sha224_digest;
|
|
Packit Service |
4684c1 |
ctx->setkey = (set_key_func) aarch64_hmac_sha224_set_key;
|
|
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_MAC_SHA256:
|
|
Packit Service |
4684c1 |
ctx->update = (update_func) aarch64_hmac_sha256_update;
|
|
Packit Service |
4684c1 |
ctx->digest = (digest_func) aarch64_hmac_sha256_digest;
|
|
Packit Service |
4684c1 |
ctx->setkey = (set_key_func) aarch64_hmac_sha256_set_key;
|
|
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_MAC_SHA384:
|
|
Packit Service |
4684c1 |
ctx->update = (update_func) aarch64_hmac_sha512_update;
|
|
Packit Service |
4684c1 |
ctx->digest = (digest_func) aarch64_hmac_sha384_digest;
|
|
Packit Service |
4684c1 |
ctx->setkey = (set_key_func) aarch64_hmac_sha384_set_key;
|
|
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_MAC_SHA512:
|
|
Packit Service |
4684c1 |
ctx->update = (update_func) aarch64_hmac_sha512_update;
|
|
Packit Service |
4684c1 |
ctx->digest = (digest_func) aarch64_hmac_sha512_digest;
|
|
Packit Service |
4684c1 |
ctx->setkey = (set_key_func) aarch64_hmac_sha512_set_key;
|
|
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_aarch64_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_ctx *ctx;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ctx = gnutls_calloc(1, sizeof(struct aarch64_hmac_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 |
ret = _hmac_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 = ctx;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void *
|
|
Packit Service |
4684c1 |
wrap_aarch64_hmac_copy(const void *_ctx)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_ctx *new_ctx;
|
|
Packit Service |
4684c1 |
const struct aarch64_hmac_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 aarch64_hmac_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 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
wrap_aarch64_hmac_setkey(void *_ctx, const void *key, size_t keylen)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_ctx *ctx = _ctx;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ctx->setkey(ctx->ctx_ptr, keylen, key);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return GNUTLS_E_SUCCESS;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
wrap_aarch64_hmac_update(void *_ctx, const void *text, size_t textsize)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_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 int
|
|
Packit Service |
4684c1 |
wrap_aarch64_hmac_output(void *src_ctx, void *digest, size_t digestsize)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_ctx *ctx;
|
|
Packit Service |
4684c1 |
ctx = src_ctx;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (digestsize < ctx->length) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return GNUTLS_E_SHORT_MEMORY_BUFFER;
|
|
Packit Service |
4684c1 |
}
|
|
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 void wrap_aarch64_hmac_deinit(void *hd)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_free(hd);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int wrap_aarch64_hmac_fast(gnutls_mac_algorithm_t algo,
|
|
Packit Service |
4684c1 |
const void *nonce, size_t nonce_size,
|
|
Packit Service |
4684c1 |
const void *key, size_t key_size,
|
|
Packit Service |
4684c1 |
const void *text, size_t text_size,
|
|
Packit Service |
4684c1 |
void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct aarch64_hmac_ctx ctx;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _hmac_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.setkey(&ctx, key_size, key);
|
|
Packit Service |
4684c1 |
ctx.update(&ctx, text_size, text);
|
|
Packit Service |
4684c1 |
ctx.digest(&ctx, ctx.length, digest);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
zeroize_temp_key(&ctx, sizeof(ctx));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
const gnutls_crypto_mac_st _gnutls_hmac_sha_aarch64 = {
|
|
Packit Service |
4684c1 |
.init = wrap_aarch64_hmac_init,
|
|
Packit Service |
4684c1 |
.setkey = wrap_aarch64_hmac_setkey,
|
|
Packit Service |
4684c1 |
.setnonce = NULL,
|
|
Packit Service |
4684c1 |
.hash = wrap_aarch64_hmac_update,
|
|
Packit Service |
4684c1 |
.output = wrap_aarch64_hmac_output,
|
|
Packit Service |
4684c1 |
.copy = wrap_aarch64_hmac_copy,
|
|
Packit Service |
4684c1 |
.deinit = wrap_aarch64_hmac_deinit,
|
|
Packit Service |
4684c1 |
.fast = wrap_aarch64_hmac_fast,
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#endif /* HAVE_LIBNETTLE */
|