lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [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 Young de la Sota | 960fd8e | 2020-01-14 13:52:13 -0500 | [diff] [blame] | 5 | #ifndef OPENTITAN_SW_DEVICE_LIB_HMAC_H_ |
| 6 | #define OPENTITAN_SW_DEVICE_LIB_HMAC_H_ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Supported HMAC operations |
| 13 | */ |
| 14 | typedef enum hmac_ops { HMAC_OP_HMAC = 0, HMAC_OP_SHA256 = 1 } hmac_ops_t; |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 15 | |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 16 | /** |
| 17 | * HMAC Configuration options. |
| 18 | */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 19 | typedef struct hmac_cfg { |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 20 | /** Operational mode @see hmac_ops. */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 21 | hmac_ops_t mode; |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 22 | /** Set to 1 to swap input bytes. */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 23 | uint32_t input_endian_swap; |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 24 | /** Set to 1 to swap output bytes. */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 25 | uint32_t digest_endian_swap; |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 26 | /** Input key used in HMAC mode. */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 27 | uint32_t keys[8]; |
| 28 | } hmac_cfg_t; |
| 29 | |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Intialize HMAC to desired mode. |
| 32 | * |
| 33 | * @param hmac_cfg HMAC configuration settings. |
| 34 | */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 35 | void hmac_init(hmac_cfg_t hmac_cfg); |
| 36 | |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 37 | /** |
Sam Elliott | 812eb33 | 2020-03-31 17:29:35 +0100 | [diff] [blame] | 38 | * Write `size_in_bytes` bytes of `data` to HMAC input buffer |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 39 | * |
| 40 | * @param data pointer to input buffer. |
Tim Shepard | ae281b6 | 2019-10-17 10:33:33 -0400 | [diff] [blame] | 41 | * @param size_in_bytes number of bytes to write. |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 42 | */ |
Miguel Osorio | 068002c | 2019-09-13 11:20:39 -0700 | [diff] [blame] | 43 | void hmac_update(const void *data, size_t size_in_bytes); |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 44 | |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Poll for hmac done and read out digest. |
| 47 | * |
Tim Shepard | ae281b6 | 2019-10-17 10:33:33 -0400 | [diff] [blame] | 48 | * @param digest pointer to output digest buffer. |
Miguel Osorio | 06f18b2 | 2019-09-13 14:25:15 -0700 | [diff] [blame] | 49 | */ |
Miguel Osorio | 068002c | 2019-09-13 11:20:39 -0700 | [diff] [blame] | 50 | void hmac_done(uint32_t *digest); |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 51 | |
Miguel Young de la Sota | 960fd8e | 2020-01-14 13:52:13 -0500 | [diff] [blame] | 52 | #endif // OPENTITAN_SW_DEVICE_LIB_HMAC_H_ |