1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file       drv/sha.h
21  * @brief      Header File for SHA Driver
22  * @version    V1.0
23  * @date       9. Oct 2020
24  * @model      sha
25  ******************************************************************************/
26 
27 #ifndef _DRV_SHA_H_
28 #define _DRV_SHA_H_
29 
30 #include <drv/common.h>
31 #include <drv/dma.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /****** SHA mode ******/
38 typedef enum {
39     SHA_MODE_1 = 1U,                     /* SHA_1 mode */
40     SHA_MODE_256,                        /* SHA_256 mode */
41     SHA_MODE_224,                        /* SHA_224 mode */
42     SHA_MODE_512,                        /* SHA_512 mode */
43     SHA_MODE_384,                        /* SHA_384 mode */
44     SHA_MODE_512_256,                    /* SHA_512_256 mode */
45     SHA_MODE_512_224,                    /* SHA_512_224 mode */
46     SHA_MODE_MD5                         /* MD5 mode */
47 } csi_sha_mode_t;
48 
49 /****** SHA State ******/
50 typedef struct {
51     uint32_t busy             : 1;        /* Calculate busy flag */
52     uint32_t error            : 1;        /* Calculate error flag */
53 } csi_sha_state_t;
54 
55 /****** SHA Context ******/
56 typedef struct {
57     csi_sha_mode_t  mode;                 /* SHA mode */
58     uint32_t        total[2];             /* Number of bytes processed */
59     uint32_t         state[16];            /* Intermediate digest state */
60     uint8_t         buffer[128];          /* Data block being processed */
61 } csi_sha_context_t;
62 
63 /****** SHA Event ******/
64 typedef enum {
65     SHA_EVENT_COMPLETE    = 0U,           /*Calculate completed*/
66     SHA_EVENT_ERROR                       /*Calculate error*/
67 } csi_sha_event_t;
68 
69 /****** SHA Ctrl ******/
70 typedef struct csi_sha csi_sha_t;
71 struct csi_sha{
72     csi_dev_t       dev;
73     void (*callback)(csi_sha_t *sha, csi_sha_event_t event, void *arg);   /* SHA event callback for user */
74     void            *arg;                                                 /* SHA custom designed param passed to evt_cb */
75     csi_dma_ch_t    *dma_in;                                              /* SHA in dma handle param */
76     csi_sha_state_t state;                                                /* SHA state */
77     void            *priv;
78 };
79 
80 /**
81   \brief       Initialize SHA Interface. Initializes the resources needed for the SHA interface
82   \param[in]   sha    Operate handle
83   \param[in]   idx    Index of SHA
84   \return      Error code \ref csi_error_t
85 */
86 csi_error_t csi_sha_init(csi_sha_t *sha, uint32_t idx);
87 
88 /**
89   \brief       De-initialize SHA Interface. Stops operation and releases the software resources used by the interface
90   \param[in]   sha    SHA handle to operate
91   \return      None
92 */
93 void csi_sha_uninit(csi_sha_t *sha);
94 
95 /**
96   \brief       Start the engine
97   \param[in]   sha        Handle to operate
98   \param[in]   context    Pointer to the SHA context \ref csi_sha_context_t
99   \param[in]   mode       SHA mode \ref csi_sha_mode_t
100   \return      Error code \ref csi_error_t
101 */
102 csi_error_t csi_sha_start(csi_sha_t *sha, csi_sha_context_t *context, csi_sha_mode_t mode);
103 
104 /**
105   \brief       Update the engine
106   \param[in]   sha        Handle to operate
107   \param[in]   context    Pointer to the SHA context \ref csi_sha_context_t
108   \param[in]   input      Pointer to the Source data
109   \param[in]   size       The data size
110   \return      Error code \ref csi_error_t
111 */
112 csi_error_t csi_sha_update(csi_sha_t *sha, csi_sha_context_t *context, const void *input, uint32_t size);
113 
114 /**
115   \brief       Finish the engine
116   \param[in]   sha         Handle to operate
117   \param[in]   context     Pointer to the SHA context \ref csi_sha_context_t
118   \param[out]  output      Pointer to the result data
119   \param[out]  out_size    Pointer to the result data size(bytes)
120   \return      Error code \ref csi_error_t
121 */
122 csi_error_t csi_sha_finish(csi_sha_t *sha, csi_sha_context_t *context, void *output, uint32_t *out_size);
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif /* _DRV_SHA_H_ */
129