blob: 041385783994cf87763d8c6b36e7ec5548314bcb [file] [log] [blame]
Miguel Osorio43c73822019-09-13 01:54:26 -07001// Copyright lowRISC contributors.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
Miguel Osorioae67f6b2019-10-18 18:56:08 -07005#include "sw/device/lib/hw_sha256.h"
Miguel Osorio43c73822019-09-13 01:54:26 -07006
Miguel Young de la Sota9ac2ac82020-02-03 16:04:50 -05007#include "sw/device/lib/hmac.h"
Miguel Osorio43c73822019-09-13 01:54:26 -07008
Miguel Young de la Sotad6f4c7f2019-10-18 14:24:17 -05009static const HASH_VTAB HW_SHA256_VTAB = {.init = &hw_SHA256_init,
10 .update = &hw_SHA256_update,
11 .final = &hw_SHA256_final,
12 .hash = &hw_SHA256_hash,
13 .size = SHA256_DIGEST_SIZE};
Miguel Osorio43c73822019-09-13 01:54:26 -070014
15static void sha256_init() {
16 hmac_cfg_t config = {.mode = HMAC_OP_SHA256,
17 .input_endian_swap = 1,
18 .digest_endian_swap = 1,
19 .keys = {0}};
20 hmac_init(config);
21}
22
23void hw_SHA256_init(HW_SHA256_CTX *ctx) {
24 // TODO: For security, need to make sure HMAC is not stuck in progress.
25 ctx->f = &HW_SHA256_VTAB;
26 sha256_init();
27}
28
29void hw_SHA256_update(HW_SHA256_CTX *ctx, const void *data, size_t len) {
30 hmac_update(data, len);
31}
32
33const uint8_t *hw_SHA256_final(HW_SHA256_CTX *ctx) {
34 hmac_done((uint32_t *)ctx->buf);
35 return ctx->buf;
36}
37
38const uint8_t *hw_SHA256_hash(const void *data, size_t len, uint8_t *digest) {
39 sha256_init();
40 hmac_update(data, len);
41 hmac_done((uint32_t *)digest);
42 return digest;
43}