API参考
载入中...
搜索中...
未找到
crypt_local_types.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16#ifndef CRYPT_LOCAL_TYPES_H
17#define CRYPT_LOCAL_TYPES_H
18
19#include <stdint.h>
20#include <stddef.h>
21#include "crypt_algid.h"
22#include "crypt_types.h"
23#include "crypt_eal_provider.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif // __cplusplus
28
29#define CRYPT_PKEY_FLAG_DUP 0x01
30#define CRYPT_PKEY_FLAG_NEED_EXPORT_CB 0x02
31
32/* length function */
33typedef int32_t (*GetLenFunc)(const void *ctx);
34
35/* Prototype of the MD algorithm operation functions */
36typedef void* (*MdNewCtx)(void *provCtx, int32_t algId);
37typedef int32_t (*MdInit)(void *data, const BSL_Param *param);
38typedef int32_t (*MdUpdate)(void *data, const uint8_t *input, uint32_t len);
39typedef int32_t (*MdFinal)(void *data, uint8_t *out, uint32_t *len);
40typedef int32_t (*MdDeinit)(void *data);
41typedef int32_t (*MdCopyCtx)(void *dst, const void *src);
42typedef void* (*MdDupCtx)(const void *src);
43typedef void (*MdFreeCtx)(void *data);
44typedef int32_t (*MdGetParam)(void *data, BSL_Param *param);
45typedef int32_t (*MdSqueeze)(void *data, uint8_t *out, uint32_t len);
46
47#ifdef HITLS_CRYPTO_MD_MB
48typedef void *(*MdMBNewCtx)(uint32_t num);
49typedef void (*MdMBFreeCtx)(void *ctx);
50typedef int32_t (*MdMBInit)(void *ctx);
51typedef int32_t (*MdMBUpdate)(void *ctx, const uint8_t *data[], uint32_t nbytes[], uint32_t num);
52typedef int32_t (*MdMBFinal)(void *ctx, uint8_t *digest[], uint32_t *outlen, uint32_t num);
53
54typedef struct {
55 MdMBNewCtx newCtx;
56 MdMBFreeCtx freeCtx;
57 MdMBInit init;
58 MdMBUpdate update;
59 MdMBFinal final;
60} EAL_MdMBMethod;
61#endif // HITLS_CRYPTO_MD_MB
62
63typedef struct {
64 int32_t id;
65 uint16_t blockSize; // Block size processed by the hash algorithm at a time, which is used with other algorithms.
66 uint16_t mdSize; // Output length of the HASH algorithm
67 MdNewCtx newCtx; // generate md context
68 MdInit init; // Initialize the MD context.
69 MdUpdate update; // Add block data for MD calculation.
70 MdFinal final; // Complete the MD calculation and obtain the MD result.
71 MdDeinit deinit; // Clear the key information of the MD context.
72 MdCopyCtx copyCtx; // Copy the MD context.
73 MdDupCtx dupCtx; // Dup the MD context.
74 MdFreeCtx freeCtx; // free md context
75 MdGetParam getParam; // get/set md param
76 MdSqueeze squeeze; // squeeze the MD context.
78
79typedef struct {
80 uint16_t hashSize; // Output length of the Siphash algorithm
81 uint16_t compressionRounds; // the number of compression rounds
82 uint16_t finalizationRounds; // the number of finalization rounds
84
85/* provide asymmetric primitive method */
86typedef void *(*PkeyNew)(void);
87typedef void* (*PkeyProvNew)(void *provCtx, int32_t algId);
88typedef void *(*PkeyDup)(void *key);
89typedef void (*PkeyFree)(void *key);
90typedef void *(*PkeyNewParaById)(int32_t id);
91typedef CRYPT_PKEY_ParaId (*PkeyGetParaId)(const void *key);
92typedef void (*PkeyFreePara)(void *para);
93typedef int32_t (*PkeySetPara)(void *key, const void *para);
94typedef int32_t (*PkeyGetPara)(const void *key, void *para);
95typedef int32_t (*PkeyGen)(void *key);
96typedef uint32_t (*PkeyBits)(void *key);
97typedef uint32_t (*PkeyGetSignLen)(void *key);
98typedef int32_t (*PkeyCtrl)(void *key, int32_t opt, void *val, uint32_t len);
99typedef int32_t (*PkeySetPrv)(void *key, const void *para);
100typedef int32_t (*PkeySetPub)(void *key, const void *para);
101typedef int32_t (*PkeyGetPrv)(const void *key, void *para);
102typedef int32_t (*PkeyGetPub)(const void *key, void *para);
103typedef void *(*PkeyNewPara)(const void *para);
104typedef int32_t (*PkeySign)(void *key, int32_t mdAlgId, const uint8_t *data, uint32_t dataLen,
105 uint8_t *sign, uint32_t *signLen);
106typedef int32_t (*PkeySignData)(void *key, const uint8_t *data, uint32_t dataLen,
107 uint8_t *sign, uint32_t *signLen);
108typedef int32_t (*PkeyVerify)(const void *key, int32_t mdAlgId, const uint8_t *data, uint32_t dataLen,
109 const uint8_t *sign, uint32_t signLen);
110typedef int32_t (*PkeyVerifyData)(const void *key, const uint8_t *data, uint32_t dataLen,
111 const uint8_t *sign, uint32_t signLen);
112typedef int32_t (*PkeyRecover)(const void *key, const uint8_t *sign, uint32_t signLen,
113 uint8_t *data, uint32_t *dataLen);
114typedef int32_t (*PkeyComputeShareKey)(const void *key, const void *pub, uint8_t *share, uint32_t *shareLen);
115typedef int32_t (*PkeyCrypt)(const void *key, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen);
116typedef int32_t (*PkeyHEOperation)(const void *ctx, const BSL_Param *input, uint8_t *out, uint32_t *outLen);
117typedef int32_t (*PkeyCheck)(uint32_t checkType, const void *key1, const void *key2);
118typedef int32_t (*PkeyCmp)(const void *key1, const void *key2);
119typedef int32_t (*PkeyCopyParam)(const void *src, void *dest);
120typedef int32_t (*PkeyGetSecBits)(const void *key);
121typedef int32_t (*PkeyEncapsulate)(const void *key, uint8_t *cipher, uint32_t *cipherLen,
122 uint8_t *share, uint32_t *shareLen);
123typedef int32_t (*PkeyDecapsulate)(const void *key, const uint8_t *cipher, uint32_t cipherLen,
124 uint8_t *share, uint32_t *shareLen);
125
126typedef int32_t (*PkeyEncapsInit)(const void *key, const void *para);
127typedef int32_t (*PkeyDecapsInit)(const void *key, const void *para);
128typedef int32_t (*PkeyBlind)(void *pkey, int32_t mdAlgId, const uint8_t *input, uint32_t inputLen,
129 uint8_t *out, uint32_t *outLen);
130typedef int32_t (*PkeyUnBlind)(const void *pkey, const uint8_t *input, uint32_t inputLen,
131 uint8_t *out, uint32_t *outLen);
132
133typedef int32_t (*PkeyImport)(void *key, const BSL_Param *params);
134
135typedef int32_t (*PkeyExport)(const void *key, BSL_Param *params);
136
142
143typedef struct EAL_PkeyMethod {
144 uint32_t id;
145 PkeyNew newCtx; // Apply for a key pair structure resource.
146 PkeyDup dupCtx; // Copy key pair structure resource.
147 PkeyFree freeCtx; // Free the key structure.
148 PkeySetPara setPara; // Set parameters of the key pair structure.
149 PkeyGetPara getPara; // Obtain parameters from the key pair structure.
150 PkeyGen gen; // Generate a key pair.
151 PkeyCtrl ctrl; // Control function.
152 PkeySetPub setPub; // Set the public key.
153 PkeySetPrv setPrv; // Set the private key.
154 PkeyGetPub getPub; // Obtain the public key.
155 PkeyGetPrv getPrv; // Obtain the private key.
156 PkeySign sign; // Sign the signature.
157 PkeySignData signData; // sign the raw data
158 PkeyVerify verify; // Verify the signature.
159 PkeyVerifyData verifyData; // Verify the raw data
160 PkeyRecover recover; // Signature recovery.
161 PkeyComputeShareKey computeShareKey; // Calculate the shared key.
162 PkeyCrypt encrypt; // Encrypt.
163 PkeyCrypt decrypt; // Decrypt.
164 PkeyHEOperation headd; // Add
165 PkeyHEOperation hemul; // Multiply
166 PkeyCheck check; // Check the consistency of the key pair.
167 PkeyCmp cmp; // Compare keys and parameters.
168 PkeyCopyParam copyPara; // Copy parameter from source to destination
169 PkeyEncapsulate pkeyEncaps; // Key encapsulation.
170 PkeyDecapsulate pkeyDecaps; // Key decapsulation.
171 PkeyBlind blind; // msg blind
172 PkeyUnBlind unBlind; // sig unBlind.
174
175typedef struct EAL_PkeyUnitaryMethod {
176 PkeyNew newCtx; // Apply for a key pair structure resource.
177 PkeyProvNew provNewCtx; // Creat a key pair structure resource for provider
178 PkeyDup dupCtx; // Copy key pair structure resource.
179 PkeyFree freeCtx; // Free the key structure.
180 PkeySetPara setPara; // Set parameters of the key pair structure.
181 PkeyGetPara getPara; // Obtain parameters from the key pair structure.
182 PkeyGen gen; // Generate a key pair.
183 PkeyCtrl ctrl; // Control function.
184 PkeySetPub setPub; // Set the public key.
185 PkeySetPrv setPrv; // Set the private key.
186 PkeyGetPub getPub; // Obtain the public key.
187 PkeyGetPrv getPrv; // Obtain the private key.
188 PkeySign sign; // Sign the signature.
189 PkeySignData signData; // sign the raw data
190 PkeyVerify verify; // Verify the signature.
191 PkeyVerifyData verifyData; // Verify the raw data
192 PkeyRecover recover; // Signature recovery.
193 PkeyComputeShareKey computeShareKey; // Calculate the shared key.
194 PkeyCrypt encrypt; // Encrypt.
195 PkeyCrypt decrypt; // Decrypt.
196 PkeyHEOperation headd; // Add
197 PkeyHEOperation hemul; // Multiply
198 PkeyCheck check; // Check the consistency of the key pair.
199 PkeyCmp cmp; // Compare keys and parameters.
200 PkeyEncapsInit encapsInit; // Init the key encapsulation
201 PkeyDecapsInit decapsInit; // Init the key decapsulation
202 PkeyEncapsulate pkeyEncaps; // Key encapsulation.
203 PkeyDecapsulate pkeyDecaps; // Key decapsulation.
204 PkeyBlind blind; // msg blind
205 PkeyUnBlind unBlind; // sig unBlind.
206 PkeyImport import; // import key
207 PkeyExport export; // export key
209
213typedef enum {
214 CRYPT_SYM_AES128 = 0,
215 CRYPT_SYM_AES192,
216 CRYPT_SYM_AES256,
217 CRYPT_SYM_CHACHA20,
218 CRYPT_SYM_SM4,
219 CRYPT_SYM_MAX
220} CRYPT_SYM_AlgId;
221
222typedef void *(*CipherNewCtx)(void *provCtx, int32_t alg);
223typedef int32_t (*CipherInitCtx)(void *ctx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
224 uint32_t ivLen, void *param, bool enc);
225typedef int32_t (*CipherDeInitCtx)(void *ctx);
226typedef int32_t (*CipherUpdate)(void *ctx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
227typedef int32_t (*CipherFinal)(void *ctx, uint8_t *out, uint32_t *outLen);
228typedef int32_t (*CipherCtrl)(void *ctx, int32_t opt, void *val, uint32_t len);
229typedef void (*CipherFreeCtx)(void *ctx);
230typedef void *(*CipherDupCtx)(const void *ctx);
231
232typedef int32_t (*SetEncryptKey)(void *ctx, const uint8_t *key, uint32_t len);
233typedef int32_t (*SetDecryptKey)(void *ctx, const uint8_t *key, uint32_t len);
234typedef int32_t (*SetKey)(void *ctx, const uint8_t *key, uint32_t len);
235// process block or blocks
236typedef int32_t (*EncryptBlock)(void *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
237typedef int32_t (*DecryptBlock)(void *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
238typedef void (*DeInitBlockCtx)(void *ctx);
239typedef int32_t (*CipherStreamProcess)(void *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
240
241typedef struct {
242 SetEncryptKey setEncryptKey;
243 SetDecryptKey setDecryptKey;
244 EncryptBlock encryptBlock;
245 DecryptBlock decryptBlock;
246 DeInitBlockCtx cipherDeInitCtx;
247 CipherCtrl cipherCtrl;
248 uint8_t blockSize;
249 uint16_t ctxSize;
250 CRYPT_SYM_AlgId algId;
252
253typedef struct {
254 CipherNewCtx newCtx;
255 CipherInitCtx initCtx;
256 CipherDeInitCtx deinitCtx;
257 CipherUpdate update;
258 CipherFinal final;
259 CipherCtrl ctrl;
260 CipherFreeCtx freeCtx;
261 CipherDupCtx dupCtx;
263
264/* Prototype of the KDF algorithm operation functions */
265typedef void* (*KdfNewCtx)(void *provCtx, int32_t algId);
266typedef int32_t (*KdfSetParam)(void *ctx, const BSL_Param *param);
267typedef int32_t (*KdfDerive)(void *ctx, uint8_t *key, uint32_t keyLen);
268typedef int32_t (*KdfDeinit)(void *ctx);
269typedef int32_t (*KdfCtrl)(void *data, int32_t cmd, void *val, uint32_t valLen);
270typedef void (*KdfFreeCtx)(void *ctx);
271typedef void *(*KdfDupCtx)(const void *ctx);
272
273typedef struct {
274 KdfNewCtx newCtx;
275 KdfSetParam setParam;
276 KdfDerive derive;
277 KdfDeinit deinit;
278 KdfCtrl ctrl;
279 KdfFreeCtx freeCtx;
280 KdfDupCtx dupCtx;
282
283typedef struct {
284 uint32_t id;
285 EAL_KdfMethod *kdfMeth;
287
288/* prototype of MAC algorithm operation functions */
289typedef void* (*MacNewCtx)(void *provCtx, int32_t algId);
290// Complete key initialization.
291typedef int32_t (*MacInit)(void *ctx, const uint8_t *key, uint32_t len, void *param);
292typedef int32_t (*MacUpdate)(void *ctx, const uint8_t *in, uint32_t len);
293typedef int32_t (*MacFinal)(void *ctx, const uint8_t *out, uint32_t *len);
294typedef int32_t (*MacDeinit)(void *ctx);
295// The action is opposite to the initCtx. Sensitive data is deleted.
296typedef int32_t (*MacReinit)(void *ctx);
297typedef int32_t (*MacCtrl)(void *ctx, uint32_t opt, void *val, uint32_t len);
298typedef int32_t (*MacSetParam)(void *data, const BSL_Param *param);
299typedef void (*MacFreeCtx)(void *ctx);
300typedef void* (*MacDupCtx)(const void *ctx);
301
302/* set of MAC algorithm operation methods */
303typedef struct {
304 MacNewCtx newCtx;
305 MacInit init; // Initialize the MAC context.
306 MacUpdate update; // Add block data for MAC calculation.
307 MacFinal final; // Complete MAC calculation and obtain the MAC result.
308 MacDeinit deinit; // Clear the key information in MAC context.
309 // Re-initialize the key. This method is used where the keys are the same during multiple MAC calculations.
310 MacReinit reinit;
311 MacCtrl ctrl;
312 MacSetParam setParam;
313 MacFreeCtx freeCtx;
314 MacDupCtx dupCtx;
316
317typedef struct {
318 union {
319 CRYPT_MD_AlgId mdId;
320 CRYPT_SYM_AlgId symId;
321 } id;
322 union {
323 EAL_MdMethod *md; // MD algorithm which HMAC depends on
324 const EAL_SymMethod *sym; // AES function wihch CMAC depends on
325 EAL_SiphashMethod *sip; // siphash method
326 } method;
328
333typedef enum {
334 HCRYPT_MODE_CBC,
335 HCRYPT_MODE_ECB,
336 HCRYPT_MODE_CTR,
337 HCRYPT_MODE_XTS,
338 HCRYPT_MODE_CCM,
339 HCRYPT_MODE_GCM,
340 HCRYPT_MODE_CHACHA20_POLY1305,
341 HCRYPT_MODE_CFB,
342 HCRYPT_MODE_OFB,
343 HCRYPT_MODE_WRAP_NOPAD,
344 HCRYPT_MODE_WRAP_PAD,
345 HCRYPT_MODE_HCTR,
346 HCRYPT_MODE_MAX
347} CRYPT_MODE_AlgId;
348
354typedef struct {
355 int32_t saltLen;
360 void *mdProvCtx;
361 void *mgfProvCtx;
363
364/* Prototype of the RAND algorithm operation functions */
365typedef void *(*RandNewCtx)(void *libCtx, int32_t algId, CRYPT_RandSeedMethod *seedMethod, void *seedCtx);
366typedef void *(*RandProvNewCtx)(void *provCtx, int32_t algId, BSL_Param *param);
367typedef int32_t (*RandDrbgInst)(void *ctx, const uint8_t *person, uint32_t persLen, void *param);
368typedef int32_t (*RandDrbgUnInst)(void *ctx);
369typedef int32_t (*RandDrbgGen)(void *ctx, uint8_t *bytes, uint32_t len, const uint8_t *adin,
370 uint32_t adinLen, void *param);
371typedef int32_t (*RandDrbgReSeed)(void *ctx, const uint8_t *addin, uint32_t addinLen, void *param);
372typedef int32_t (*RandDrbgCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen);
373typedef void (*RandDrbgFreeCtx)(void *ctx);
374
375typedef struct {
376 RandNewCtx newCtx;
377 RandProvNewCtx provNewCtx;
378 RandDrbgInst inst;
379 RandDrbgUnInst unInst;
380 RandDrbgGen gen;
381 RandDrbgReSeed reSeed;
382 RandDrbgCtrl ctrl;
383 RandDrbgFreeCtx freeCtx;
385
386typedef struct {
387 uint32_t type;
388 int32_t methodId;
389 const void *method;
391
397typedef enum {
398 CRYPT_CTRL_GET_MACID = 0, /* kdf get macId . */
399 CRYPT_CTRL_GET_SALTLEN, /* kdf get saltlen . */
400 CRYPT_CTRL_GET_ITER, /* kdf get iter . */
401 CRYPT_CTRL_GET_KEYLEN /* kdf get keyLen . */
402} CRYPT_KdfCtrl;
403
404typedef enum {
405 CRYPT_PKEY_CHECK_KEYPAIR = 1,
406 CRYPT_PKEY_CHECK_PRVKEY = 2,
407 CRYPT_PKEY_CHECK_MAX,
408} CRYPT_KeyCheckType;
409
410#ifdef __cplusplus
411}
412#endif // __cplusplus
413
414#endif // EAL_LOCAL_TYPES_H
CRYPT_PKEY_ParaId
定义 crypt_algid.h:208
CRYPT_MD_AlgId
定义 crypt_algid.h:68
定义 crypt_types.h:587
定义 crypt_local_types.h:283
定义 crypt_local_types.h:253
定义 crypt_local_types.h:273
定义 crypt_local_types.h:317
定义 crypt_local_types.h:303
定义 crypt_local_types.h:63
定义 crypt_local_types.h:143
定义 crypt_local_types.h:175
定义 crypt_local_types.h:386
定义 crypt_local_types.h:375
定义 crypt_local_types.h:79
定义 crypt_local_types.h:241
定义 crypt_local_types.h:354
EAL_MdMethod mgfMeth
定义 crypt_local_types.h:357
EAL_MdMethod mdMeth
定义 crypt_local_types.h:356
CRYPT_MD_AlgId mdId
定义 crypt_local_types.h:358
CRYPT_MD_AlgId mgfId
定义 crypt_local_types.h:359
int32_t saltLen
定义 crypt_local_types.h:355