#include <openssl/hmac.h>
#include <assert.h>
#include <string.h>
#include <openssl/digest.h>
#include <openssl/mem.h>
#include "../../internal.h"
#include "../service_indicator/internal.h"
typedef int (*HashInit)(void *);
typedef int (*HashUpdate)(void *, const void*, size_t);
typedef int (*HashFinal)(uint8_t *, void*);
struct hmac_methods_st {
const EVP_MD* evp_md;
HashInit init;
HashUpdate update;
HashFinal finalize; };
#define MD_TRAMPOLINES_EXPLICIT(HASH_NAME, HASH_CTX, HASH_CBLOCK) \
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Init(void *); \
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Update(void *, const void *, \
size_t); \
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Final(uint8_t *, void *); \
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Init(void *ctx) { \
return HASH_NAME##_Init((HASH_CTX *)ctx); \
} \
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Update( \
void *ctx, const void *key, size_t key_len) { \
return HASH_NAME##_Update((HASH_CTX *)ctx, key, key_len); \
} \
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Final(uint8_t *out, void *ctx) { \
return HASH_NAME##_Final(out, (HASH_CTX *)ctx); \
} \
OPENSSL_STATIC_ASSERT(HASH_CBLOCK% 8 == 0, \
HASH_NAME##_has_blocksize_not_divisible_by_eight_t) \
OPENSSL_STATIC_ASSERT(HASH_CBLOCK <= EVP_MAX_MD_BLOCK_SIZE, \
HASH_NAME##_has_overlarge_blocksize_t) \
OPENSSL_STATIC_ASSERT( \
sizeof(HASH_CTX) <= sizeof(union md_ctx_union), \
HASH_NAME##_has_overlarge_context_t)
#define HMAC_METHOD_MAX 7
MD_TRAMPOLINES_EXPLICIT(MD5, MD5_CTX, MD5_CBLOCK);
MD_TRAMPOLINES_EXPLICIT(SHA1, SHA_CTX, SHA_CBLOCK);
MD_TRAMPOLINES_EXPLICIT(SHA224, SHA256_CTX, SHA256_CBLOCK);
MD_TRAMPOLINES_EXPLICIT(SHA256, SHA256_CTX, SHA256_CBLOCK);
MD_TRAMPOLINES_EXPLICIT(SHA384, SHA512_CTX, SHA512_CBLOCK);
MD_TRAMPOLINES_EXPLICIT(SHA512, SHA512_CTX, SHA512_CBLOCK);
MD_TRAMPOLINES_EXPLICIT(SHA512_256, SHA512_CTX, SHA512_CBLOCK);
struct hmac_method_array_st {
HmacMethods methods[HMAC_METHOD_MAX];
};
#define DEFINE_IN_PLACE_METHODS(EVP_MD, HASH_NAME) { \
out->methods[idx].evp_md = EVP_MD; \
out->methods[idx].init = AWS_LC_TRAMPOLINE_##HASH_NAME##_Init; \
out->methods[idx].update = AWS_LC_TRAMPOLINE_##HASH_NAME##_Update; \
out->methods[idx].finalize = AWS_LC_TRAMPOLINE_##HASH_NAME##_Final; \
idx++; \
assert(idx <= HMAC_METHOD_MAX); \
}
DEFINE_LOCAL_DATA(struct hmac_method_array_st, AWSLC_hmac_in_place_methods) {
OPENSSL_memset((void*) out->methods, 0, sizeof(out->methods));
int idx = 0;
DEFINE_IN_PLACE_METHODS(EVP_sha256(), SHA256);
DEFINE_IN_PLACE_METHODS(EVP_sha1(), SHA1);
DEFINE_IN_PLACE_METHODS(EVP_sha384(), SHA384);
DEFINE_IN_PLACE_METHODS(EVP_sha512(), SHA512);
DEFINE_IN_PLACE_METHODS(EVP_md5(), MD5);
DEFINE_IN_PLACE_METHODS(EVP_sha224(), SHA224);
DEFINE_IN_PLACE_METHODS(EVP_sha512_256(), SHA512_256);
}
static const HmacMethods *GetInPlaceMethods(const EVP_MD *evp_md) {
const struct hmac_method_array_st *method_array = AWSLC_hmac_in_place_methods();
const HmacMethods *methods = method_array->methods;
for (size_t idx = 0; idx < sizeof(method_array->methods) / sizeof(struct hmac_methods_st); idx++) {
if (methods[idx].evp_md == evp_md) {
return &methods[idx];
}
}
return NULL;
}
#define HMAC_STATE_UNINITIALIZED 0
#define HMAC_STATE_INIT_NO_DATA 1
#define HMAC_STATE_IN_PROGRESS 2
#define HMAC_STATE_READY_NEEDS_INIT 3
OPENSSL_STATIC_ASSERT(HMAC_STATE_UNINITIALIZED == 0, HMAC_STATE_UNINITIALIZED_is_not_zero_t)
#define hmac_ctx_is_initialized(ctx) ((HMAC_STATE_INIT_NO_DATA == (ctx)->state || HMAC_STATE_IN_PROGRESS == (ctx)->state))
uint8_t *HMAC(const EVP_MD *evp_md, const void *key, size_t key_len,
const uint8_t *data, size_t data_len, uint8_t *out,
unsigned int *out_len) {
HMAC_CTX ctx;
OPENSSL_memset(&ctx, 0, sizeof(HMAC_CTX));
int result;
FIPS_service_indicator_lock_state();
result = HMAC_Init_ex(&ctx, key, key_len, evp_md, NULL) &&
HMAC_Update(&ctx, data, data_len) &&
HMAC_Final(&ctx, out, out_len);
FIPS_service_indicator_unlock_state();
HMAC_CTX_cleanup(&ctx);
if (result) {
HMAC_verify_service_indicator(evp_md);
return out;
} else {
OPENSSL_cleanse(out, EVP_MD_size(evp_md));
return NULL;
}
}
void HMAC_CTX_init(HMAC_CTX *ctx) {
OPENSSL_memset(ctx, 0, sizeof(HMAC_CTX));
}
HMAC_CTX *HMAC_CTX_new(void) {
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
if (ctx != NULL) {
HMAC_CTX_init(ctx);
}
return ctx;
}
void HMAC_CTX_cleanup(HMAC_CTX *ctx) {
OPENSSL_cleanse(ctx, sizeof(HMAC_CTX));
}
void HMAC_CTX_cleanse(HMAC_CTX *ctx) {
HMAC_CTX_cleanup(ctx);
}
void HMAC_CTX_free(HMAC_CTX *ctx) {
if (ctx == NULL) {
return;
}
HMAC_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len,
const EVP_MD *md, ENGINE *impl) {
assert(impl == NULL);
if (HMAC_STATE_READY_NEEDS_INIT == ctx->state) {
ctx->state = HMAC_STATE_INIT_NO_DATA; }
if (HMAC_STATE_INIT_NO_DATA == ctx->state) {
if (key == NULL && (md == NULL || md == ctx->md)) {
return 1;
}
}
if (md && (HMAC_STATE_UNINITIALIZED == ctx->state || ctx->md != md)) {
ctx->methods = GetInPlaceMethods(md);
if (ctx->methods == NULL) {
return 0;
}
ctx->md = md;
} else if (!hmac_ctx_is_initialized(ctx)) {
return 0;
}
const HmacMethods *methods = ctx->methods;
size_t block_size = EVP_MD_block_size(methods->evp_md);
assert(block_size % 8 == 0);
assert(block_size <= EVP_MAX_MD_BLOCK_SIZE);
FIPS_service_indicator_lock_state();
int result = 0;
uint64_t pad[EVP_MAX_MD_BLOCK_SIZE_BYTES] = {0};
uint64_t key_block[EVP_MAX_MD_BLOCK_SIZE_BYTES] = {0};
if (block_size < key_len) {
if (!methods->init(&ctx->md_ctx) ||
!methods->update(&ctx->md_ctx, key, key_len) ||
!methods->finalize((uint8_t *) key_block, &ctx->md_ctx)) {
goto end;
}
} else {
assert(key_len <= sizeof(key_block));
OPENSSL_memcpy(key_block, key, key_len);
}
for (size_t i = 0; i < block_size / 8; i++) {
pad[i] = 0x3636363636363636 ^ key_block[i];
}
if (!methods->init(&ctx->i_ctx) ||
!methods->update(&ctx->i_ctx, pad, block_size)) {
goto end;
}
for (size_t i = 0; i < block_size / 8; i++) {
pad[i] = 0x5c5c5c5c5c5c5c5c ^ key_block[i];
}
if (!methods->init(&ctx->o_ctx) ||
!methods->update(&ctx->o_ctx, pad, block_size)) {
goto end;
}
OPENSSL_memcpy(&ctx->md_ctx, &ctx->i_ctx, sizeof(ctx->i_ctx));
ctx->state = HMAC_STATE_INIT_NO_DATA;
result = 1;
end:
OPENSSL_cleanse(pad, EVP_MAX_MD_BLOCK_SIZE_BYTES);
OPENSSL_cleanse(key_block, EVP_MAX_MD_BLOCK_SIZE_BYTES);
FIPS_service_indicator_unlock_state();
if (result != 1) {
HMAC_CTX_cleanup(ctx);
}
return result;
}
int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data, size_t data_len) {
if (!hmac_ctx_is_initialized(ctx)) {
return 0;
}
ctx->state = HMAC_STATE_IN_PROGRESS;
return ctx->methods->update(&ctx->md_ctx, data, data_len);
}
int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) {
const HmacMethods *methods = ctx->methods;
if (!hmac_ctx_is_initialized(ctx)) {
return 0;
}
FIPS_service_indicator_lock_state();
int result = 0;
const EVP_MD *evp_md = ctx->md;
int hmac_len = EVP_MD_size(evp_md);
uint8_t tmp[EVP_MAX_MD_SIZE];
if (!methods->finalize(tmp, &ctx->md_ctx)) {
goto end;
}
OPENSSL_memcpy(&ctx->md_ctx, &ctx->o_ctx, sizeof(ctx->o_ctx));
if (!ctx->methods->update(&ctx->md_ctx, tmp, hmac_len)) {
goto end;
}
result = methods->finalize(out, &ctx->md_ctx);
OPENSSL_memcpy(&ctx->md_ctx, &ctx->i_ctx, sizeof(ctx->i_ctx));
ctx->state = HMAC_STATE_READY_NEEDS_INIT; end:
FIPS_service_indicator_unlock_state();
if (result) {
HMAC_verify_service_indicator(evp_md);
if (out_len) {
*out_len = hmac_len;
}
return 1;
} else {
if (out_len) {
*out_len = 0;
}
return 0;
}
}
size_t HMAC_size(const HMAC_CTX *ctx) { return EVP_MD_size(ctx->md); }
const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) { return ctx->md; }
int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src) {
OPENSSL_memcpy(dest, src, sizeof(HMAC_CTX));
return 1;
}
void HMAC_CTX_reset(HMAC_CTX *ctx) {
HMAC_CTX_cleanup(ctx);
}
int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md) {
if (key && md) {
HMAC_CTX_init(ctx);
}
return HMAC_Init_ex(ctx, key, key_len, md, NULL);
}
int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src) {
HMAC_CTX_init(dest);
return HMAC_CTX_copy_ex(dest, src);
}