mirror of
https://github.com/SciresM/hactool
synced 2024-11-10 06:34:14 +00:00
55 lines
No EOL
1.3 KiB
C
55 lines
No EOL
1.3 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "sha.h"
|
|
#include "types.h"
|
|
#include "utils.h"
|
|
|
|
/* Allocate new context. */
|
|
sha_ctx_t *new_sha_ctx(hash_type_t type, int hmac) {
|
|
sha_ctx_t *ctx;
|
|
|
|
if ((ctx = malloc(sizeof(*ctx))) == NULL) {
|
|
FATAL_ERROR("Failed to allocate sha_ctx_t!");
|
|
}
|
|
|
|
mbedtls_md_init(&ctx->digest);
|
|
|
|
if (mbedtls_md_setup(&ctx->digest, mbedtls_md_info_from_type(type), hmac)) {
|
|
FATAL_ERROR("Failed to set up hash context!");
|
|
}
|
|
|
|
if (mbedtls_md_starts(&ctx->digest)) {
|
|
FATAL_ERROR("Failed to start hash context!");
|
|
}
|
|
|
|
return ctx;
|
|
}
|
|
|
|
/* Free an allocated context. */
|
|
void free_sha_ctx(sha_ctx_t *ctx) {
|
|
/* Explicitly allow NULL. */
|
|
if (ctx == NULL) {
|
|
return;
|
|
}
|
|
|
|
mbedtls_md_free(&ctx->digest);
|
|
free(ctx);
|
|
}
|
|
|
|
/* Update digest with new data. */
|
|
void sha_update(sha_ctx_t *ctx, const void *data, size_t l) {
|
|
mbedtls_md_update(&ctx->digest, data, l);
|
|
}
|
|
|
|
/* Read hash from context. */
|
|
void sha_get_hash(sha_ctx_t *ctx, unsigned char *hash) {
|
|
mbedtls_md_finish(&ctx->digest, hash);
|
|
}
|
|
|
|
/* 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);
|
|
} |