Miguel Osorio | 43c7382 | 2019-09-13 01:54:26 -0700 | [diff] [blame] | 1 | // 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 Osorio | ae67f6b | 2019-10-18 18:56:08 -0700 | [diff] [blame] | 5 | #include "sw/device/lib/hw_sha256.h" |
Miguel Osorio | 43c7382 | 2019-09-13 01:54:26 -0700 | [diff] [blame] | 6 | |
Miguel Young de la Sota | 9ac2ac8 | 2020-02-03 16:04:50 -0500 | [diff] [blame] | 7 | #include "sw/device/lib/hmac.h" |
Miguel Osorio | 43c7382 | 2019-09-13 01:54:26 -0700 | [diff] [blame] | 8 | |
Miguel Young de la Sota | d6f4c7f | 2019-10-18 14:24:17 -0500 | [diff] [blame] | 9 | static 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 Osorio | 43c7382 | 2019-09-13 01:54:26 -0700 | [diff] [blame] | 14 | |
| 15 | static 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 | |
| 23 | void 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 | |
| 29 | void hw_SHA256_update(HW_SHA256_CTX *ctx, const void *data, size_t len) { |
| 30 | hmac_update(data, len); |
| 31 | } |
| 32 | |
| 33 | const uint8_t *hw_SHA256_final(HW_SHA256_CTX *ctx) { |
| 34 | hmac_done((uint32_t *)ctx->buf); |
| 35 | return ctx->buf; |
| 36 | } |
| 37 | |
| 38 | const 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 | } |