• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "crypt_algid.h"
20 #include "crypt_types.h"
21 #include "bsl_params.h"
22 #include "crypt_params_key.h"
23 #include "crypt_eal_provider.h"
24 
25 #ifdef __cplusplus
26 extern "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 */
33 typedef int32_t (*GetLenFunc)(const void *ctx);
34 
35 /* Prototype of the MD algorithm operation functions */
36 typedef void* (*MdNewCtx)(void);
37 typedef void* (*MdProvNewCtx)(void *provCtx, int32_t algId);
38 typedef int32_t (*MdInit)(void *data, const BSL_Param *param);
39 typedef int32_t (*MdUpdate)(void *data, const uint8_t *input, uint32_t len);
40 typedef int32_t (*MdFinal)(void *data, uint8_t *out, uint32_t *len);
41 typedef void (*MdDeinit)(void *data);
42 typedef int32_t (*MdCopyCtx)(void *dst, void *src);
43 typedef void* (*MdDupCtx)(const void *src);
44 typedef void (*MdFreeCtx)(void *data);
45 typedef int32_t (*MdCtrl)(void *data, int32_t cmd, void *val, uint32_t valLen);
46 typedef int32_t (*MdSqueeze)(void *data, uint8_t *out, uint32_t len);
47 
48 typedef struct {
49     uint16_t blockSize; // Block size processed by the hash algorithm at a time, which is used with other algorithms.
50     uint16_t mdSize;    // Output length of the HASH algorithm
51     MdNewCtx newCtx;    // generate md context
52     MdInit init;        // Initialize the MD context.
53     MdUpdate update;    // Add block data for MD calculation.
54     MdFinal final;      // Complete the MD calculation and obtain the MD result.
55     MdDeinit deinit;    // Clear the key information of the MD context.
56     MdCopyCtx copyCtx; // Copy the MD context.
57     MdDupCtx dupCtx;  // Dup the MD context.
58     MdFreeCtx freeCtx;   // free md context
59     MdCtrl ctrl;        // get/set md param
60     MdSqueeze squeeze;  // squeeze the MD context.
61 } EAL_MdMethod;
62 
63 typedef struct {
64     uint16_t blockSize;
65     uint16_t mdSize;
66     MdNewCtx newCtx;
67     MdProvNewCtx provNewCtx;
68     MdInit init;
69     MdUpdate update;
70     MdFinal final;
71     MdDeinit deinit;
72     MdDupCtx dupCtx;
73     MdFreeCtx freeCtx;
74     MdCtrl ctrl;
75     MdSqueeze squeeze;  // squeeze the MD context.
76 } EAL_MdUnitaryMethod;
77 
78 typedef struct {
79     uint16_t hashSize;              // Output length of the Siphash algorithm
80     uint16_t compressionRounds;     // the number of compression rounds
81     uint16_t finalizationRounds;    // the number of finalization rounds
82 } EAL_SiphashMethod;
83 
84 typedef struct {
85     uint32_t id;
86     EAL_MdMethod *mdMeth;
87 } EAL_CidToMdMeth;
88 
89 /* provide asymmetric primitive method */
90 typedef void *(*PkeyNew)(void);
91 typedef void* (*PkeyProvNew)(void *provCtx, int32_t algId);
92 typedef void *(*PkeyDup)(void *key);
93 typedef void (*PkeyFree)(void *key);
94 typedef void *(*PkeyNewParaById)(int32_t id);
95 typedef CRYPT_PKEY_ParaId (*PkeyGetParaId)(const void *key);
96 typedef void (*PkeyFreePara)(void *para);
97 typedef int32_t (*PkeySetPara)(void *key, const void *para);
98 typedef int32_t (*PkeyGetPara)(const void *key, void *para);
99 typedef int32_t (*PkeyGen)(void *key);
100 typedef uint32_t (*PkeyBits)(void *key);
101 typedef uint32_t (*PkeyGetSignLen)(void *key);
102 typedef int32_t (*PkeyCtrl)(void *key, int32_t opt, void *val, uint32_t len);
103 typedef int32_t (*PkeySetPrv)(void *key, const void *para);
104 typedef int32_t (*PkeySetPub)(void *key, const void *para);
105 typedef int32_t (*PkeyGetPrv)(const void *key, void *para);
106 typedef int32_t (*PkeyGetPub)(const void *key, void *para);
107 typedef void *(*PkeyNewPara)(const void *para);
108 typedef int32_t (*PkeySign)(void *key, int32_t mdAlgId, const uint8_t *data, uint32_t dataLen,
109     uint8_t *sign, uint32_t *signLen);
110 typedef int32_t (*PkeySignData)(void *key, const uint8_t *data, uint32_t dataLen,
111     uint8_t *sign, uint32_t *signLen);
112 typedef int32_t (*PkeyVerify)(const void *key, int32_t mdAlgId, const uint8_t *data, uint32_t dataLen,
113     const uint8_t *sign, uint32_t signLen);
114 typedef int32_t (*PkeyVerifyData)(const void *key, const uint8_t *data, uint32_t dataLen,
115     const uint8_t *sign, uint32_t signLen);
116 typedef int32_t (*PkeyRecover)(const void *key, const uint8_t *sign, uint32_t signLen,
117     uint8_t *data, uint32_t *dataLen);
118 typedef int32_t (*PkeyComputeShareKey)(const void *key, const void *pub, uint8_t *share, uint32_t *shareLen);
119 typedef int32_t (*PkeyCrypt)(const void *key, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen);
120 typedef int32_t (*PkeyCheck)(const void *prv, const void *pub);
121 typedef int32_t (*PkeyCmp)(const void *key1, const void *key2);
122 typedef int32_t (*PkeyCopyParam)(const void *src, void *dest);
123 typedef int32_t (*PkeyGetSecBits)(const void *key);
124 typedef int32_t (*PkeyEncapsulate)(const void *key, uint8_t *cipher, uint32_t *cipherLen,
125     uint8_t *share, uint32_t *shareLen);
126 typedef int32_t (*PkeyDecapsulate)(const void *key, uint8_t *cipher, uint32_t cipherLen,
127     uint8_t *share, uint32_t *shareLen);
128 
129 typedef int32_t (*PkeyEncapsulateInit)(const void *key, const BSL_Param *params);
130 typedef int32_t (*PkeyDecapsulateInit)(const void *key, const BSL_Param *params);
131 typedef int32_t (*PkeyBlind)(void *pkey, int32_t mdAlgId, const uint8_t *input, uint32_t inputLen,
132     uint8_t *out, uint32_t *outLen);
133 typedef int32_t (*PkeyUnBlind)(const void *pkey, const uint8_t *input, uint32_t inputLen,
134     uint8_t *out, uint32_t *outLen);
135 
136 typedef int32_t (*PkeyImport)(void *key, const BSL_Param *params);
137 
138 typedef int32_t (*PkeyExport)(const void *key, BSL_Param *params);
139 
140 /**
141 * @ingroup  EAL
142 *
143 * Method structure of the EAL
144 */
145 
146 typedef struct EAL_PkeyMethod {
147     uint32_t id;
148     PkeyNew newCtx;                         // Apply for a key pair structure resource.
149     PkeyDup dupCtx;                         // Copy key pair structure resource.
150     PkeyFree freeCtx;                       // Free the key structure.
151     PkeySetPara setPara;                    // Set parameters of the key pair structure.
152     PkeyGetPara getPara;                    // Obtain parameters from the key pair structure.
153     PkeyGen gen;                            // Generate a key pair.
154     PkeyCtrl ctrl;                          // Control function.
155     PkeySetPub setPub;                      // Set the public key.
156     PkeySetPrv setPrv;                      // Set the private key.
157     PkeyGetPub getPub;                      // Obtain the public key.
158     PkeyGetPrv getPrv;                      // Obtain the private key.
159     PkeySign sign;                          // Sign the signature.
160     PkeySignData signData;                  // sign the raw data
161     PkeyVerify verify;                      // Verify the signature.
162     PkeyVerifyData verifyData;              // Verify the raw data
163     PkeyRecover recover;                    // Signature recovery.
164     PkeyComputeShareKey computeShareKey;    // Calculate the shared key.
165     PkeyCrypt encrypt;                      // Encrypt.
166     PkeyCrypt decrypt;                      // Decrypt.
167     PkeyCheck check;                        // Check the consistency of the key pair.
168     PkeyCmp cmp;                            // Compare keys and parameters.
169     PkeyCopyParam copyPara;                 // Copy parameter from source to destination
170     PkeyEncapsulate encaps;                // Key encapsulation.
171     PkeyDecapsulate decaps;                // Key decapsulation.
172     PkeyBlind blind;                        // msg blind
173     PkeyUnBlind unBlind;                    // sig unBlind.
174 } EAL_PkeyMethod;
175 
176 typedef struct EAL_PkeyUnitaryMethod {
177     PkeyNew newCtx;                         // Apply for a key pair structure resource.
178     PkeyProvNew provNewCtx;                 // Creat a key pair structure resource for provider
179     PkeyDup dupCtx;                         // Copy key pair structure resource.
180     PkeyFree freeCtx;                       // Free the key structure.
181     PkeySetPara setPara;                    // Set parameters of the key pair structure.
182     PkeyGetPara getPara;                    // Obtain parameters from the key pair structure.
183     PkeyGen gen;                            // Generate a key pair.
184     PkeyCtrl ctrl;                          // Control function.
185     PkeySetPub setPub;                      // Set the public key.
186     PkeySetPrv setPrv;                      // Set the private key.
187     PkeyGetPub getPub;                      // Obtain the public key.
188     PkeyGetPrv getPrv;                      // Obtain the private key.
189     PkeySign sign;                          // Sign the signature.
190     PkeySignData signData;                  // sign the raw data
191     PkeyVerify verify;                      // Verify the signature.
192     PkeyVerifyData verifyData;              // Verify the raw data
193     PkeyRecover recover;                    // Signature recovery.
194     PkeyComputeShareKey computeShareKey;    // Calculate the shared key.
195     PkeyCrypt encrypt;                      // Encrypt.
196     PkeyCrypt decrypt;                      // Decrypt.
197     PkeyCheck check;                        // Check the consistency of the key pair.
198     PkeyCmp cmp;                            // Compare keys and parameters.
199     PkeyEncapsulateInit encapsInit;        // Key encapsulation init.
200     PkeyDecapsulateInit decapsInit;        // Key decapsulation init.
201     PkeyEncapsulate encaps;                // Key encapsulation.
202     PkeyDecapsulate decaps;                // Key decapsulation.
203     PkeyBlind blind;                        // msg blind
204     PkeyUnBlind unBlind;                    // sig unBlind.
205     PkeyImport import;                      // import key
206     PkeyExport export;                      // export key
207 } EAL_PkeyUnitaryMethod;
208 /**
209  * @ingroup  sym_algid
210  * Symmetric encryption/decryption algorithm ID
211  */
212 typedef enum {
213     CRYPT_SYM_AES128 = 0,
214     CRYPT_SYM_AES192,
215     CRYPT_SYM_AES256,
216     CRYPT_SYM_CHACHA20,
217     CRYPT_SYM_SM4,
218     CRYPT_SYM_MAX
219 } CRYPT_SYM_AlgId;
220 
221 typedef void *(*CipherNewCtx)(int32_t alg);
222 typedef void *(*CipherProvNewCtx)(void *provCtx, int32_t alg);
223 typedef int32_t (*CipherInitCtx)(void *ctx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
224     uint32_t ivLen, const BSL_Param *param, bool enc);
225 typedef int32_t (*CipherDeInitCtx)(void *ctx);
226 typedef int32_t (*CipherUpdate)(void *ctx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
227 typedef int32_t (*CipherFinal)(void *ctx, uint8_t *out, uint32_t *outLen);
228 typedef int32_t (*CipherCtrl)(void *ctx, int32_t opt, void *val, uint32_t len);
229 typedef void (*CipherFreeCtx)(void *ctx);
230 
231 typedef int32_t (*SetEncryptKey)(void *ctx, const uint8_t *key, uint32_t len);
232 typedef int32_t (*SetDecryptKey)(void *ctx, const uint8_t *key, uint32_t len);
233 typedef int32_t (*SetKey)(void *ctx, const uint8_t *key, uint32_t len);
234 // process block or blocks
235 typedef int32_t (*EncryptBlock)(void *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
236 typedef int32_t (*DecryptBlock)(void *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
237 typedef void (*DeInitBlockCtx)(void *ctx);
238 typedef int32_t (*CipherStreamProcess)(void *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
239 
240 typedef struct {
241     SetEncryptKey setEncryptKey;
242     SetDecryptKey setDecryptKey;
243     EncryptBlock encryptBlock;
244     DecryptBlock decryptBlock;
245     DeInitBlockCtx cipherDeInitCtx;
246     CipherCtrl cipherCtrl;
247     uint8_t blockSize;
248     uint16_t ctxSize;
249     CRYPT_SYM_AlgId algId;
250 } EAL_SymMethod;
251 
252 typedef struct {
253     CipherNewCtx newCtx;
254     CipherInitCtx initCtx;
255     CipherDeInitCtx deinitCtx;
256     CipherUpdate update;
257     CipherFinal final;
258     CipherCtrl ctrl;
259     CipherFreeCtx freeCtx;
260 } EAL_CipherMethod;
261 
262 typedef struct {
263     CipherNewCtx newCtx;
264     CipherProvNewCtx provNewCtx;
265     CipherInitCtx initCtx;
266     CipherDeInitCtx deinitCtx;
267     CipherUpdate update;
268     CipherFinal final;
269     CipherCtrl ctrl;
270     CipherFreeCtx freeCtx;
271 } EAL_CipherUnitaryMethod;
272 
273 /* prototype of MAC algorithm operation functions */
274 typedef void* (*MacNewCtx)(CRYPT_MAC_AlgId id);
275 typedef void* (*MacProvNewCtx)(void *provCtx, int32_t algId);
276 // Complete key initialization.
277 typedef int32_t (*MacInit)(void *ctx, const uint8_t *key, uint32_t len, const BSL_Param *param);
278 typedef int32_t (*MacUpdate)(void *ctx, const uint8_t *in, uint32_t len);
279 typedef int32_t (*MacFinal)(void *ctx, const uint8_t *out, uint32_t *len);
280 typedef void    (*MacDeinit)(void *ctx);
281 // The action is opposite to the initCtx. Sensitive data is deleted.
282 typedef void    (*MacReinit)(void *ctx);
283 typedef int32_t (*MacCtrl)(void *data, int32_t cmd, void *val, uint32_t valLen);
284 typedef void (*MacFreeCtx)(void *ctx);
285 
286 /* set of MAC algorithm operation methods */
287 typedef struct {
288     MacNewCtx newCtx;
289     MacInit init;           // Initialize the MAC context.
290     MacUpdate update;       // Add block data for MAC calculation.
291     MacFinal final;         // Complete MAC calculation and obtain the MAC result.
292     MacDeinit deinit;       // Clear the key information in MAC context.
293     // Re-initialize the key. This method is used where the keys are the same during multiple MAC calculations.
294     MacReinit reinit;
295     MacCtrl ctrl;
296     MdFreeCtx freeCtx;
297 } EAL_MacMethod;
298 
299 typedef struct {
300     MacNewCtx newCtx;
301     MdFreeCtx freeCtx;
302     MacProvNewCtx provNewCtx;
303     MacInit init;           // Initialize the MAC context.
304     MacUpdate update;       // Add block data for MAC calculation.
305     MacFinal final;         // Complete MAC calculation and obtain the MAC result.
306     MacDeinit deinit;       // Clear the key information in MAC context.
307     // Re-initialize the key. This method is used where the keys are the same during multiple MAC calculations.
308     MacReinit reinit;
309     MacCtrl ctrl;
310 } EAL_MacUnitaryMethod;
311 
312 typedef struct {
313     const EAL_MacMethod *macMethod;
314     union {
315         const EAL_MdMethod *md;        // MD algorithm which HMAC depends on
316         const EAL_SymMethod *ciph;  // AES function wihch CMAC depends on
317         const EAL_SiphashMethod *sip;  // siphash method
318         const void *depMeth;           // Pointer to the dependent algorithm, which is reserved for extension.
319     };
320 } EAL_MacMethLookup;
321 
322 /**
323  * @ingroup  mode_algid
324  * Symmetric encryption/decryption mode ID
325  */
326 typedef enum {
327     CRYPT_MODE_CBC = 0,
328     CRYPT_MODE_ECB,
329     CRYPT_MODE_CTR,
330     CRYPT_MODE_XTS,
331     CRYPT_MODE_CCM,
332     CRYPT_MODE_GCM,
333     CRYPT_MODE_CHACHA20_POLY1305,
334     CRYPT_MODE_CFB,
335     CRYPT_MODE_OFB,
336     CRYPT_MODE_MAX
337 } CRYPT_MODE_AlgId;
338 
339 /**
340  * @ingroup crypt_eal_pkey
341  *
342  * Structure of the PSS padding mode when RSA is used for signature
343  */
344 typedef struct {
345     int32_t saltLen;               /**< pss salt length. -1 indicates hashLen, -2 indicates MaxLen, -3 is AutoLen */
346     const EAL_MdMethod *mdMeth;    /**< pss mdid method when padding */
347     const EAL_MdMethod *mgfMeth;   /**< pss mgfid method when padding */
348     CRYPT_MD_AlgId mdId;           /**< pss mdid when padding */
349     CRYPT_MD_AlgId mgfId;          /**< pss mgfid when padding */
350 } RSA_PadingPara;
351 
352 /* Prototype of the KDF algorithm operation functions */
353 typedef void* (*KdfNewCtx)(void);
354 typedef void* (*KdfProvNewCtx)(void *provCtx, int32_t algId);
355 typedef int32_t (*KdfSetParam)(void *ctx, const BSL_Param *param);
356 typedef int32_t (*KdfDerive)(void *ctx, uint8_t *key, uint32_t keyLen);
357 typedef int32_t (*KdfDeinit)(void *ctx);
358 typedef int32_t (*KdfCtrl)(void *data, int32_t cmd, void *val, uint32_t valLen);
359 typedef void (*KdfFreeCtx)(void *ctx);
360 
361 typedef struct {
362     KdfNewCtx newCtx;
363     KdfSetParam setParam;
364     KdfDerive derive;
365     KdfDeinit deinit;
366     KdfFreeCtx freeCtx;
367     KdfCtrl ctrl;
368 } EAL_KdfMethod;
369 
370 typedef struct {
371     KdfNewCtx newCtx;
372     KdfProvNewCtx provNewCtx;
373     KdfSetParam setParam;
374     KdfDerive derive;
375     KdfDeinit deinit;
376     KdfFreeCtx freeCtx;
377     KdfCtrl ctrl;
378 } EAL_KdfUnitaryMethod;
379 
380 typedef struct {
381     uint32_t id;
382     EAL_KdfMethod *kdfMeth;
383 } EAL_CidToKdfMeth;
384 
385 /* Prototype of the RAND algorithm operation functions */
386 typedef void *(*RandNewCtx)(int32_t algId, BSL_Param *param);
387 typedef void *(*RandDrbgNewCtx)(void *provCtx, int32_t algId, BSL_Param *param);
388 typedef int32_t (*RandDrbgInst)(void *ctx, const uint8_t *pers, uint32_t persLen, BSL_Param *param);
389 typedef int32_t (*RandDrbgUnInst)(void *ctx);
390 typedef int32_t (*RandDrbgGen)(void *ctx, uint8_t *bytes, uint32_t len,
391     const uint8_t *addin, uint32_t addinLen, BSL_Param *param);
392 typedef int32_t (*RandDrbgReSeed)(void *ctx, const uint8_t *addin, uint32_t addinLen, BSL_Param *param);
393 typedef int32_t (*RandDrbgCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen);
394 typedef void (*RandDrbgFreeCtx)(void *ctx);
395 
396 typedef struct {
397     RandNewCtx newCtx;
398     RandDrbgNewCtx provNewCtx;
399     RandDrbgInst inst;
400     RandDrbgUnInst unInst;
401     RandDrbgGen gen;
402     RandDrbgReSeed reSeed;
403     RandDrbgCtrl ctrl;
404     RandDrbgFreeCtx freeCtx;
405 } EAL_RandUnitaryMethod;
406 
407 typedef struct {
408     uint32_t type;
409     int32_t methodId;
410     const void *method;
411 } EAL_RandMethLookup;
412 
413 /**
414  * @ingroup crypt_ctrl_param
415  *
416  * Set and obtain internal parameters of Pbkdf2.
417  */
418 typedef enum {
419     CRYPT_CTRL_GET_MACID = 0,       /* kdf get macId . */
420     CRYPT_CTRL_GET_SALTLEN,         /* kdf get saltlen . */
421     CRYPT_CTRL_GET_ITER,            /* kdf get iter . */
422     CRYPT_CTRL_GET_KEYLEN           /* kdf get keyLen . */
423 } CRYPT_KdfCtrl;
424 
425 #ifdef __cplusplus
426 }
427 #endif // __cplusplus
428 
429 #endif // EAL_LOCAL_TYPES_H
430