1 /***************************************************************************
2  *
3  * Copyright 2015-2020 BES.
4  * All rights reserved. All unpublished rights reserved.
5  *
6  * No part of this work may be used or reproduced in any form or by any
7  * means, or stored in a database or retrieval system, without prior written
8  * permission of BES.
9  *
10  * Use of this work is governed by a license granted by BES.
11  * This work contains confidential and proprietary information of
12  * BES. which is protected by copyright, trade secret,
13  * trademark and other intellectual property rights.
14  *
15  ****************************************************************************/
16 #ifndef __HAL_SEC_ENG_H__
17 #define __HAL_SEC_ENG_H__
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #include "plat_types.h"
24 #include "stdbool.h"
25 #include "stdint.h"
26 #include "hal_dma.h"
27 
28 enum HAL_SE_RET_T {
29     HAL_SE_OK,
30     HAL_SE_ERR,
31     HAL_SE_ALREADY_OPENED,
32     HAL_SE_NOT_OPENED,
33     HAL_SE_ENG_BUSY,
34     HAL_SE_DMA_BUSY,
35     HAL_SE_CFG_NULL,
36     HAL_SE_INPUT_NULL,
37     HAL_SE_OUTPUT_NULL,
38     HAL_SE_KEY_NULL,
39     HAL_SE_IV_NULL,
40     HAL_SE_KEY2_NULL,
41     HAL_SE_BAD_INPUT_LEN,
42     HAL_SE_BAD_OUTPUT_LEN,
43     HAL_SE_BAD_KEY_LEN,
44     HAL_SE_BAD_AES_MODE,
45     HAL_SE_BAD_AES_MODULAR,
46     HAL_SE_BAD_MODE,
47     HAL_SE_BAD_OP,
48 };
49 
50 enum HAL_SE_AES_MODE_T {
51     HAL_SE_AES_EBC                      = 0,
52     HAL_SE_AES_CBC                      = 1,
53     HAL_SE_AES_CTR                      = 2,
54     HAL_SE_AES_XTS                      = 3,
55     HAL_SE_AES_KEY_WRAP                 = 4,
56 
57     HAL_SE_AES_MODE_QTY,
58 };
59 
60 enum HAL_SE_DONE_ERR_T {
61     HAL_SE_DONE_OK                      = 0,
62     HAL_SE_DONE_ERR_DMA_IN,
63     HAL_SE_DONE_ERR_DMA_OUT,
64     HAL_SE_DONE_ERR_DMA_IN_REMAIN,
65     HAL_SE_DONE_ERR_DMA_OUT_REMAIN,
66     HAL_SE_DONE_ERR_ENG_ERR1,
67     HAL_SE_DONE_ERR_ENG_ERR2,
68     HAL_SE_DONE_ERR_ENG_ERR1_ERR2,
69 };
70 
71 enum HAL_SE_HASH_MODE_T {
72     HAL_SE_HASH_SHA1                    = 0,
73     HAL_SE_HASH_SHA256,
74     HAL_SE_HASH_SHA224,
75     HAL_SE_HASH_MD5,
76     HAL_SE_HASH_SHA512,
77     HAL_SE_HASH_SHA384,
78 
79     HAL_SE_HASH_QTY,
80 };
81 
82 typedef void (*HAL_SC_DONE_HANDLER_T)(void *buf, uint32_t len, enum HAL_SE_DONE_ERR_T err);
83 
84 struct HAL_SE_AES_CFG_T {
85     enum HAL_SE_AES_MODE_T mode;
86     const void *key;
87     uint16_t key_len;
88     uint8_t ctr_modular;
89     const void *key2;
90     const void *iv;
91     const void *in;
92     uint32_t in_len;
93     void *out;
94     uint32_t out_len;
95     HAL_SC_DONE_HANDLER_T done_hdlr;
96 };
97 
98 struct HAL_SE_HASH_CFG_T {
99     const void *in;
100     uint32_t in_len;
101     HAL_SC_DONE_HANDLER_T done_hdlr;
102 };
103 
104 enum HAL_SE_RET_T hal_se_open(void);
105 
106 enum HAL_SE_RET_T hal_se_close(void);
107 
108 enum HAL_SE_RET_T hal_se_aes_encrypt(const struct HAL_SE_AES_CFG_T *cfg);
109 
110 enum HAL_SE_RET_T hal_se_aes_decrypt(const struct HAL_SE_AES_CFG_T *cfg);
111 
112 int hal_se_aes_busy(void);
113 
114 enum HAL_SE_RET_T hal_se_aes_reset(void);
115 
116 enum HAL_SE_RET_T hal_se_hash_init(enum HAL_SE_HASH_MODE_T mode);
117 
118 enum HAL_SE_RET_T hal_se_hash_update(const struct HAL_SE_HASH_CFG_T *cfg);
119 
120 enum HAL_SE_RET_T hal_se_hash_final(const struct HAL_SE_HASH_CFG_T *cfg, uint64_t total_in_len);
121 
122 enum HAL_SE_RET_T hal_se_hash(enum HAL_SE_HASH_MODE_T mode, const struct HAL_SE_HASH_CFG_T *cfg);
123 
124 enum HAL_SE_RET_T hal_se_hash_get_digest(void *out, uint32_t out_len, uint32_t *real_len);
125 
126 int hal_se_hash_busy(void);
127 
128 enum HAL_SE_RET_T hal_se_hash_reset(void);
129 
130 enum HAL_SE_RET_T hal_se_hmac_init(enum HAL_SE_HASH_MODE_T mode, const void *key, uint32_t key_len);
131 
132 enum HAL_SE_RET_T hal_se_hmac_update(const struct HAL_SE_HASH_CFG_T *cfg);
133 
134 enum HAL_SE_RET_T hal_se_hmac_final(const struct HAL_SE_HASH_CFG_T *cfg, uint64_t total_in_len);
135 
136 enum HAL_SE_RET_T hal_se_hmac(enum HAL_SE_HASH_MODE_T mode, const void *key, uint32_t key_len, const struct HAL_SE_HASH_CFG_T *cfg);
137 
138 enum HAL_SE_RET_T hal_se_hmac_get_digest(void *out, uint32_t out_len, uint32_t *real_len);
139 
140 int hal_se_hmac_busy(void);
141 
142 enum HAL_SE_RET_T hal_se_hmac_reset(void);
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif
149 
150