2018-01-24 17:52:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "sha.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/* Allocate new context. */
|
2018-01-29 18:39:30 +00:00
|
|
|
sha_ctx_t *new_sha_ctx(hash_type_t type, int hmac) {
|
2018-01-24 17:52:25 +00:00
|
|
|
sha_ctx_t *ctx;
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-01-24 17:52:25 +00:00
|
|
|
if ((ctx = malloc(sizeof(*ctx))) == NULL) {
|
|
|
|
FATAL_ERROR("Failed to allocate sha_ctx_t!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-01-29 18:39:30 +00:00
|
|
|
mbedtls_md_init(&ctx->digest);
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-01-29 18:39:30 +00:00
|
|
|
if (mbedtls_md_setup(&ctx->digest, mbedtls_md_info_from_type(type), hmac)) {
|
|
|
|
FATAL_ERROR("Failed to set up hash context!");
|
2018-01-24 17:52:25 +00:00
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-01-29 18:39:30 +00:00
|
|
|
if (mbedtls_md_starts(&ctx->digest)) {
|
|
|
|
FATAL_ERROR("Failed to start hash context!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-01-24 17:52:25 +00:00
|
|
|
return ctx;
|
2018-01-29 18:39:30 +00:00
|
|
|
}
|
2018-01-24 17:52:25 +00:00
|
|
|
|
2018-01-29 18:39:30 +00:00
|
|
|
/* Free an allocated context. */
|
|
|
|
void free_sha_ctx(sha_ctx_t *ctx) {
|
|
|
|
/* Explicitly allow NULL. */
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-01-29 18:39:30 +00:00
|
|
|
mbedtls_md_free(&ctx->digest);
|
|
|
|
free(ctx);
|
2018-01-24 17:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update digest with new data. */
|
|
|
|
void sha_update(sha_ctx_t *ctx, const void *data, size_t l) {
|
2018-01-29 18:39:30 +00:00
|
|
|
mbedtls_md_update(&ctx->digest, data, l);
|
2018-01-24 17:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read hash from context. */
|
|
|
|
void sha_get_hash(sha_ctx_t *ctx, unsigned char *hash) {
|
2018-01-29 18:39:30 +00:00
|
|
|
mbedtls_md_finish(&ctx->digest, hash);
|
2018-01-24 17:52:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 18:39:30 +00:00
|
|
|
/* SHA256 digest. */
|
|
|
|
void sha256_hash_buffer(unsigned char *digest, const void *data, size_t l) {
|
|
|
|
sha_ctx_t *sha_ctx = new_sha_ctx(HASH_TYPE_SHA256, 0);
|
|
|
|
sha_update(sha_ctx, data, l);
|
|
|
|
sha_get_hash(sha_ctx, digest);
|
|
|
|
free_sha_ctx(sha_ctx);
|
2018-04-03 22:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SHA256-HMAC digest. */
|
|
|
|
void sha256_get_buffer_hmac(void *digest, const void *secret, size_t s_l, const void *data, size_t d_l) {
|
|
|
|
sha_ctx_t *ctx;
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
if ((ctx = malloc(sizeof(*ctx))) == NULL) {
|
|
|
|
FATAL_ERROR("Failed to allocate sha_ctx_t!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
mbedtls_md_init(&ctx->digest);
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
if (mbedtls_md_setup(&ctx->digest, mbedtls_md_info_from_type(HASH_TYPE_SHA256), 1)) {
|
|
|
|
FATAL_ERROR("Failed to set up hash context!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
if (mbedtls_md_hmac_starts(&ctx->digest, secret, s_l)) {
|
|
|
|
FATAL_ERROR("Failed to set up HMAC secret context!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
if (mbedtls_md_hmac_update(&ctx->digest, data, d_l)) {
|
|
|
|
FATAL_ERROR("Failed processing HMAC input!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
if (mbedtls_md_hmac_finish(&ctx->digest, digest)) {
|
|
|
|
FATAL_ERROR("Failed getting HMAC output!");
|
|
|
|
}
|
2020-01-15 10:07:22 +00:00
|
|
|
|
2018-04-03 22:26:56 +00:00
|
|
|
mbedtls_md_free(&ctx->digest);
|
|
|
|
free(ctx);
|
2019-05-05 04:26:34 +00:00
|
|
|
}
|