• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #ifndef __EXT_ALG_H_
20 #define __EXT_ALG_H_
21 
22 #include "drv_hash.h"
23 #include "drv_symc.h"
24 #include "drv_cipher_kapi.h"
25 #include "drv_osal_lib.h"
26 
27 /*
28  * brief          Create symc handle
29  *
30  * param handle   symc handle to be initialized
31  * param hard_key symc use hard key or not
32  */
33 hi_void *ext_mbedtls_aead_create(hi_u32 hard_key);
34 
35 /*
36  * brief          Clear symc context
37  *
38  * param handle      symc handle to be destroy
39  */
40 hi_s32 ext_mbedtls_aead_destory(hi_void *ctx);
41 
42 /*
43  * brief          symc iv schedule
44  *
45  * param handle   symc handle
46  * param IV       encryption key
47  * param ivlen    length of iv
48  *
49  * return         0 if successful.
50  */
51 hi_s32 ext_mbedtls_aead_setiv(hi_void *ctx, const hi_u8 *iv, hi_u32 ivlen, hi_u32 usage);
52 
53 /*
54  * brief          symc key schedule
55  *
56  * param ctx      SYMC handle
57  * param key      SYMC key
58  * param keylen   SYMC key length
59  *
60  * return         0 if successful, or HI_SYMC_ERR_KEY_LEN_INVALID
61  */
62 hi_s32 ext_mbedtls_aead_setkey(hi_void *ctx, const hi_u8 *fkey, const hi_u8 *skey, hi_u32 *hisi_klen);
63 
64 /*
65  * brief          SYMC alg and mode
66  *
67  * param ctx      SYMC handle
68  * param aad      Associated Data
69  * param alen     Associated Data Length
70  * param tlen     Tag length
71  *
72  * return         0 if successful.
73  */
74 hi_s32 ext_mbedtls_aead_set_aad(hi_void *ctx, compat_addr aad, hi_u32 alen, hi_u32 tlen);
75 
76 /*
77  * brief  get ccm/gcm tag.
78  * param[in]   chn_num The channel number.
79  * param[out]  tag The tag value.
80  * param[out]  taglen tag length
81  * \retval     On received interception, HI_TRUE is returned  otherwise HI_FALSE is returned.
82  */
83 hi_s32 ext_mbedtls_aead_get_tag(hi_void *ctx, hi_u32 tag[AEAD_TAG_SIZE_IN_WORD], hi_u32 *taglen);
84 
85 /*
86  * brief          aead ccm buffer encryption/decryption.
87  *
88  * Note: Due to the nature of aes you should use the same key schedule for
89  * both encryption and decryption.
90  *
91  * param ctx      symc ctx.
92  * param operation  operation of encrypt or decrypt.
93  * param pack     package of decrypt or encrypt.
94  * param last     last or not.
95  *
96  * return         0 if successful
97  */
98 hi_s32 ext_mbedtls_aead_ccm_crypto(hi_void *ctx, hi_u32 operation, symc_multi_pack *pack, hi_u32 last);
99 
100 /*
101  * brief          aead gcm buffer encryption/decryption.
102  *
103  * Note: Due to the nature of aes you should use the same key schedule for
104  * both encryption and decryption.
105  *
106  * param ctx      symc ctx.
107  * param operation  operation of encrypt or decrypt.
108  * param pack     package of decrypt or encrypt.
109  * param last     last or not.
110  *
111  * return         0 if successful
112  */
113 hi_s32 ext_mbedtls_aead_gcm_crypto(hi_void *ctx, hi_u32 operation, symc_multi_pack *pack, hi_u32 last);
114 
115 /*
116  * brief          Create symc handle
117  *
118  * param handle   symc handle to be initialized
119  * param hard_key symc use hard key or not
120  */
121 hi_void *ext_mbedtls_symc_create(hi_u32 hard_key);
122 
123 /*
124  * brief          Clear symc context
125  *
126  * param handle      symc handle to be destroy
127  */
128 hi_s32 ext_mbedtls_symc_destory(hi_void *ctx);
129 
130 /*
131  * brief          SYMC alg and mode
132  *
133  * param handle   SYMC handle
134  * param alg      Symmetric cipher alg
135  * param mode     Symmetric cipher mode
136  *
137  * return         0 if successful.
138  */
139 hi_void ext_mbedtls_symc_setmode(hi_void *ctx, symc_alg alg, symc_mode mode, symc_width width);
140 
141 /*
142  * brief          symc iv schedule
143  *
144  * param handle   symc handle
145  * param IV       encryption key
146  * param ivlen    length of iv
147  *
148  * return         0 if successful.
149  */
150 hi_s32 ext_mbedtls_symc_setiv(hi_void *ctx, const hi_u8 *iv, hi_u32 ivlen, hi_u32 usage);
151 
152 /*
153  * brief          Symc iv schedule
154  *
155  * param handle   symc handle
156  * param IV       Symc IV
157  * param ivlen    must be 128, 192 or 256
158  *
159  * return         0 if successful.
160  */
161 hi_void ext_mbedtls_symc_getiv(hi_void *ctx, hi_u8 *iv, hi_u32 *ivlen);
162 
163 /*
164  * brief          symc key schedule
165  *
166  * param ctx      SYMC handle
167  * param key      SYMC key
168  * param keylen   SYMC key length
169  *
170  * return         0 if successful, or HI_SYMC_ERR_KEY_LEN_INVALID
171  */
172 hi_s32 ext_mbedtls_symc_setkey(hi_void *ctx, const hi_u8 *fkey, const hi_u8 *skey, hi_u32 *hisi_klen);
173 
174 /*
175  * brief          symc buffer encryption/decryption.
176  *
177  * Note: Due to the nature of aes you should use the same key schedule for
178  * both encryption and decryption.
179  *
180  * param ctx       symc ctx
181  * param operation decrypt or encrypt
182  * param pack     package for encrypt or decrypt.
183  * param last     last or not
184  * return         0 if successful
185  */
186 hi_s32 ext_mbedtls_symc_crypto(hi_void *ctx, hi_u32 operation, symc_multi_pack *pack, hi_u32 last);
187 
188 /*
189  * brief          Create sm4 handle
190  *
191  * param handle   sm4 handle to be initialized
192  * param hard_key sm4 use hard key or not
193  */
194 hi_void *ext_sm4_create(hi_u32 hard_key);
195 
196 /*
197  * brief          Clear sm4 context
198  *
199  * param handle      sm4 handle to be destroy
200  */
201 hi_s32 ext_sm4_destory(hi_void *ctx);
202 
203 /*
204  * brief          sm4 alg and mode
205  *
206  * param handle   sm4 handle
207  * param alg      Symmetric cipher alg
208  * param mode     Symmetric cipher mode
209  *
210  * return         NA.
211  */
212 hi_void ext_sm4_setmode(hi_void *ctx, symc_alg alg, symc_mode mode, symc_width width);
213 
214 /*
215  * brief          sm4 iv schedule
216  *
217  * param handle   sm4 handle
218  * param IV       encryption key
219  * param ivlen    length of iv
220  *
221  * return         0 if successful.
222  */
223 hi_s32 ext_sm4_setiv(hi_void *ctx, const hi_u8 *iv, hi_u32 ivlen, hi_u32 usage);
224 
225 /*
226  * brief          Symc iv schedule
227  *
228  * param handle   symc handle
229  * param IV       Symc IV
230  * param ivlen    must be 128, 192 or 256
231  *
232  * return         NA.
233  */
234 hi_void ext_sm4_getiv(hi_void *ctx, hi_u8 *iv, hi_u32 *ivlen);
235 
236 /*
237  * brief          sm4 key schedule
238  *
239  * param ctx      sm4 handle
240  * param key      sm4 key
241  * param keylen   sm4 key length
242  *
243  * return         0 if successful, or HI_SYMC_ERR_KEY_LEN_INVALID
244  */
245 hi_s32 ext_sm4_setkey(hi_void *ctx, const hi_u8 *fkey, const hi_u8 *skey, hi_u32 *hisi_klen);
246 
247 /*
248  * brief          sm4 buffer encryption/decryption.
249  *
250  * Note: Due to the nature of aes you should use the same key schedule for
251  * both encryption and decryption.
252  *
253  * param ctx       sm4 ctx
254  * param operation decrypt or encrypt
255  * param pack     package for encrypt or decrypt.
256  * param last     last or not
257  *
258  * return         0 if successful
259  */
260 hi_s32 ext_sm4_crypto(hi_void *ctx, hi_u32 operation, symc_multi_pack *pack, hi_u32 last);
261 
262 /*
263  * brief          Create hash mbedtls
264  *
265  * Note: The memory requested here is required call mbedtls_hash_destory to release
266  *
267  * param ctx      hash mode
268  */
269 hi_void *mbedtls_hash_create(hash_mode mode);
270 
271 /*
272  * brief          Hash message chunk calculation
273  *
274  * Note: the message must be write to the buffer
275  * which get from cryp_hash_get_cpu_addr, and the length of message chunk
276  * can't large than the length which get from cryp_hash_get_cpu_addr.
277  *
278  * param ctx      hash handle to be destroy
279  * param chunk    hash message to update
280  * param length   length of hash message
281  * param src      source of hash message
282  */
283 hi_s32 mbedtls_hash_update(hi_void *ctx, const hi_u8 *chunk, hi_u32 chunk_len, hash_chunk_src src);
284 
285 /*
286  * brief          HASH final digest
287  *
288  * param ctx      Hash handle
289  * param hash     HASH checksum result
290  * param hashlen  Length of HASH checksum result
291  */
292 hi_s32 mbedtls_hash_finish(hi_void *ctx,  hi_void *hash, hi_u32 *hashlen);
293 
294 /*
295  * brief          Clear hash context
296  *
297  * param ctx      symc handle to be destroy
298  */
299 hi_s32 mbedtls_hash_destory(hi_void *ctx);
300 
301 /* sm3 */
302 hi_void *ext_sm3_create(hash_mode mode);
303 
304 hi_s32 ext_sm3_update(hi_void *ctx, const hi_u8 *chunk, hi_u32 chunk_len, hash_chunk_src src);
305 
306 hi_s32 ext_sm3_finish(hi_void *ctx,  hi_void *hash, hi_u32 *hashlen);
307 
308 hi_s32 ext_sm3_destory(hi_void *ctx);
309 #endif
310