1 /*
2  * Copyright (c) 2021-2023, The TrustedFirmware-M Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef CC3XX_HASH_H
9 #define CC3XX_HASH_H
10 
11 #include "cc3xx_error.h"
12 #include "cc3xx_dma.h"
13 
14 #include <stdint.h>
15 #include <stddef.h>
16 
17 #define SHA1_OUTPUT_SIZE (20)
18 #define SHA224_OUTPUT_SIZE (28)
19 #define SHA256_OUTPUT_SIZE (32)
20 
21 typedef enum {
22     CC3XX_HASH_ALG_SHA1  =  0b0001U,
23     CC3XX_HASH_ALG_SHA224 = 0b1010U,
24     CC3XX_HASH_ALG_SHA256 = 0b0010U,
25 } cc3xx_hash_alg_t;
26 
27 /**
28  * @brief Macro that returns the length of the hash associated to the
29  *        algorithm value passed as input
30  */
31 #define CC3XX_HASH_LENGTH(alg)                              \
32     (                                                       \
33         alg == CC3XX_HASH_ALG_SHA1 ? SHA1_OUTPUT_SIZE :     \
34         alg == CC3XX_HASH_ALG_SHA224 ? SHA224_OUTPUT_SIZE : \
35         alg == CC3XX_HASH_ALG_SHA256 ? SHA256_OUTPUT_SIZE : \
36         0                                                   \
37     )
38 
39 struct cc3xx_hash_state_t {
40     cc3xx_hash_alg_t alg;
41     uint64_t curr_len;
42     uint32_t hash_h[8];
43     struct cc3xx_dma_state_t dma_state;
44 };
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /**
51  * @brief                        Initialize a hash operation.
52  *
53  * @param[in]  alg               Which hash algorithm should be initialized.
54  *
55  * @return                       CC3XX_ERR_SUCCESS on success, another
56  *                               cc3xx_err_t on error.
57  */
58 cc3xx_err_t cc3xx_lowlevel_hash_init(cc3xx_hash_alg_t alg);
59 
60 /**
61  * @brief                        Input data into a hash operation.
62  *
63  * @param[in]  buf               A pointer to the data to be input.
64  * @param[in]  length            The size of the data to be input.
65  *
66  * @return                       CC3XX_ERR_SUCCESS on success, another
67  *                               cc3xx_err_t on error.
68  */
69 cc3xx_err_t cc3xx_lowlevel_hash_update(const uint8_t *buf, size_t length);
70 
71 /**
72  * @brief                        Get the current state of the hash operation.
73  *                               Allows for restartable hash operations.
74  *
75  * @param[out] state             The cc3xx_hash_state_t to write the state into.
76  */
77 void cc3xx_lowlevel_hash_get_state(struct cc3xx_hash_state_t *state);
78 
79 /**
80  * @brief                        Set the current state of the hash operation.
81  *                               Allows for restartable hash operations.
82  *
83  * @note                         This function initializes the hardware, there is
84  *                               no need to separately call cc3xx_hash_init.
85  *
86  * @param[in]  state             The cc3xx_hash_state_t to read the state from.
87  */
88 void cc3xx_lowlevel_hash_set_state(const struct cc3xx_hash_state_t *state);
89 
90 /**
91  * @brief                        Finish a hash operation, and output the hash.
92  *
93  * @param[out]  res              The result of the hash operation.
94  * @param[in]   length           The size of the result buffer. Must match the
95  *                               hash output size. Checked by assert only.
96  */
97 void cc3xx_lowlevel_hash_finish(uint32_t *res, size_t length);
98 
99 /**
100  * @brief                        Uninitialize the hash engine.
101  *
102  * @note                         The hash engine is not implicitly uninitialized
103  *                               on an error.
104  *
105  */
106 void cc3xx_lowlevel_hash_uninit(void);
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 
112 #endif /* CC3XX_HASH_H */
113