Files
OpenCellular/chip/g/dcrypto/hmac.c
nagendra modadugu ed1532bf81 CR50: replace dcrypto_memset with always_memset
always_memset() implements a version of memset
that survives compiler optimization.  This change
replaces instances of the (placeholder) call
dcrypto_memset() with always_memset().

Also add a couple of missing memsets and
fix related TODOs by replacing memset()
with always_memset().

BRANCH=none
BUG=none
TEST=TCG tests pass

Change-Id: I742393852ed5be9f74048eea7244af7be027dd0e
Signed-off-by: nagendra modadugu <ngm@google.com>
Reviewed-on: https://chromium-review.googlesource.com/501368
Commit-Ready: Nagendra Modadugu <ngm@google.com>
Tested-by: Nagendra Modadugu <ngm@google.com>
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
2017-05-15 17:34:30 -07:00

60 lines
1.5 KiB
C

/* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "internal.h"
#include "dcrypto.h"
#include <stdint.h>
#include "cryptoc/sha256.h"
#include "cryptoc/util.h"
/* TODO(ngm): add support for hardware hmac. */
static void HMAC_init(LITE_HMAC_CTX *ctx, const void *key, unsigned int len)
{
unsigned int i;
memset(&ctx->opad[0], 0, sizeof(ctx->opad));
if (len > sizeof(ctx->opad)) {
DCRYPTO_SHA256_init(&ctx->hash, 0);
HASH_update(&ctx->hash, key, len);
memcpy(&ctx->opad[0], HASH_final(&ctx->hash),
HASH_size(&ctx->hash));
} else {
memcpy(&ctx->opad[0], key, len);
}
for (i = 0; i < sizeof(ctx->opad); ++i)
ctx->opad[i] ^= 0x36;
DCRYPTO_SHA256_init(&ctx->hash, 0);
/* hash ipad */
HASH_update(&ctx->hash, ctx->opad, sizeof(ctx->opad));
for (i = 0; i < sizeof(ctx->opad); ++i)
ctx->opad[i] ^= (0x36 ^ 0x5c);
}
void DCRYPTO_HMAC_SHA256_init(LITE_HMAC_CTX *ctx, const void *key,
unsigned int len)
{
HMAC_init(ctx, key, len);
}
const uint8_t *DCRYPTO_HMAC_final(LITE_HMAC_CTX *ctx)
{
uint8_t digest[SHA_DIGEST_MAX_BYTES]; /* upto SHA2 */
memcpy(digest, HASH_final(&ctx->hash),
(HASH_size(&ctx->hash) <= sizeof(digest) ?
HASH_size(&ctx->hash) : sizeof(digest)));
DCRYPTO_SHA256_init(&ctx->hash, 0);
HASH_update(&ctx->hash, ctx->opad, sizeof(ctx->opad));
HASH_update(&ctx->hash, digest, HASH_size(&ctx->hash));
always_memset(&ctx->opad[0], 0, sizeof(ctx->opad)); /* wipe key */
return HASH_final(&ctx->hash);
}