API参考
载入中...
搜索中...
未找到
crypt.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_H
17#define CRYPT_H
18
19#include <stdint.h>
20#include "hitls_crypt_type.h"
21#include "tls.h"
22#include "hitls_crypt_reg.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/* The maximum length of the RSA signature is 512. The maximum length of the ECC signature does not reach 1024. */
29#define MAX_SIGN_SIZE 1024
30
31/* Used to transfer key derivation parameters. */
32typedef struct {
33 HITLS_HashAlgo hashAlgo; /* Hash algorithm */
34 const uint8_t *secret; /* Initialization key */
35 uint32_t secretLen; /* Key length */
36 const uint8_t *label; /* Label */
37 uint32_t labelLen; /* Label length */
38 const uint8_t *seed; /* Seed */
39 uint32_t seedLen; /* Seed length */
40 HITLS_Lib_Ctx *libCtx;
41 const char *attrName;
43
44enum HITLS_CryptoCallBack {
45 HITLS_CRYPT_CALLBACK_RAND_BYTES = 0,
46 HITLS_CRYPT_CALLBACK_HMAC_SIZE,
47 HITLS_CRYPT_CALLBACK_HMAC_INIT,
48 HITLS_CRYPT_CALLBACK_HMAC_FREE,
49 HITLS_CRYPT_CALLBACK_HMAC_UPDATE,
50 HITLS_CRYPT_CALLBACK_HMAC_FINAL,
51 HITLS_CRYPT_CALLBACK_HMAC,
52 HITLS_CRYPT_CALLBACK_DIGEST_SIZE,
53 HITLS_CRYPT_CALLBACK_DIGEST_INIT,
54 HITLS_CRYPT_CALLBACK_DIGEST_COPY,
55 HITLS_CRYPT_CALLBACK_DIGEST_FREE,
56 HITLS_CRYPT_CALLBACK_DIGEST_UPDATE,
57 HITLS_CRYPT_CALLBACK_DIGEST_FINAL,
58 HITLS_CRYPT_CALLBACK_DIGEST,
59 HITLS_CRYPT_CALLBACK_ENCRYPT,
60 HITLS_CRYPT_CALLBACK_DECRYPT,
61
62 HITLS_CRYPT_CALLBACK_GENERATE_ECDH_KEY_PAIR,
63 HITLS_CRYPT_CALLBACK_FREE_ECDH_KEY,
64 HITLS_CRYPT_CALLBACK_GET_ECDH_ENCODED_PUBKEY,
65 HITLS_CRYPT_CALLBACK_CALC_ECDH_SHARED_SECRET,
66 HITLS_CRYPT_CALLBACK_SM2_CALC_ECDH_SHARED_SECRET,
67
68 HITLS_CRYPT_CALLBACK_GENERATE_DH_KEY_BY_SECBITS,
69 HITLS_CRYPT_CALLBACK_GENERATE_DH_KEY_BY_PARAMS,
70 HITLS_CRYPT_CALLBACK_DUP_DH_KEY,
71 HITLS_CRYPT_CALLBACK_FREE_DH_KEY,
72 HITLS_CRYPT_CALLBACK_DH_GET_PARAMETERS,
73 HITLS_CRYPT_CALLBACK_GET_DH_ENCODED_PUBKEY,
74 HITLS_CRYPT_CALLBACK_CALC_DH_SHARED_SECRET,
75
76 HITLS_CRYPT_CALLBACK_HKDF_EXTRACT,
77 HITLS_CRYPT_CALLBACK_HKDF_EXPAND,
78 HITLS_CRYPT_CALLBACK_KEM_ENCAPSULATE,
79 HITLS_CRYPT_CALLBACK_KEM_DECAPSULATE,
80};
81
93int32_t SAL_CRYPT_Rand(HITLS_Lib_Ctx *libCtx, uint8_t *buf, uint32_t len);
94
102uint32_t SAL_CRYPT_HmacSize(HITLS_HashAlgo hashAlgo);
103
122HITLS_HMAC_Ctx *SAL_CRYPT_HmacInit(HITLS_Lib_Ctx *libCtx, const char *attrName,
123 HITLS_HashAlgo hashAlgo, const uint8_t *key, uint32_t len);
124
132int32_t SAL_CRYPT_HmacReInit(HITLS_HMAC_Ctx *ctx);
133
139void SAL_CRYPT_HmacFree(HITLS_HMAC_Ctx *hmac);
140
152int32_t SAL_CRYPT_HmacUpdate(HITLS_HMAC_Ctx *hmac, const uint8_t *data, uint32_t len);
153
165int32_t SAL_CRYPT_HmacFinal(HITLS_HMAC_Ctx *hmac, uint8_t *out, uint32_t *len);
166
188int32_t SAL_CRYPT_Hmac(HITLS_Lib_Ctx *libCtx, const char *attrName,
189 HITLS_HashAlgo hashAlgo, const uint8_t *key, uint32_t keyLen,
190 const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
191
204int32_t SAL_CRYPT_PRF(CRYPT_KeyDeriveParameters *input, uint8_t *out, uint32_t outLen);
205
213uint32_t SAL_CRYPT_DigestSize(HITLS_HashAlgo hashAlgo);
214
232HITLS_HASH_Ctx *SAL_CRYPT_DigestInit(HITLS_Lib_Ctx *libCtx, const char *attrName, HITLS_HashAlgo hashAlgo);
233
241HITLS_HASH_Ctx *SAL_CRYPT_DigestCopy(HITLS_HASH_Ctx *ctx);
242
248void SAL_CRYPT_DigestFree(HITLS_HASH_Ctx *ctx);
249
261int32_t SAL_CRYPT_DigestUpdate(HITLS_HASH_Ctx *ctx, const uint8_t *data, uint32_t len);
262
274int32_t SAL_CRYPT_DigestFinal(HITLS_HASH_Ctx *ctx, uint8_t *out, uint32_t *len);
275
303int32_t SAL_CRYPT_Digest(HITLS_Lib_Ctx *libCtx, const char *attrName,
304 HITLS_HashAlgo hashAlgo, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
305
322int32_t SAL_CRYPT_Encrypt(HITLS_Lib_Ctx *libCtx, const char *attrName,
323 const HITLS_CipherParameters *cipher, const uint8_t *in, uint32_t inLen,
324 uint8_t *out, uint32_t *outLen);
325
342int32_t SAL_CRYPT_Decrypt(HITLS_Lib_Ctx *libCtx, const char *attrName,
343 const HITLS_CipherParameters *cipher, const uint8_t *in, uint32_t inLen,
344 uint8_t *out, uint32_t *outLen);
345
351void SAL_CRYPT_CipherFree(HITLS_Cipher_Ctx *ctx);
352
360HITLS_CRYPT_Key *SAL_CRYPT_GenEcdhKeyPair(TLS_Ctx *ctx, const HITLS_ECParameters *curveParams);
361
367void SAL_CRYPT_FreeEcdhKey(HITLS_CRYPT_Key *key);
368
381int32_t SAL_CRYPT_EncodeEcdhPubKey(HITLS_CRYPT_Key *key, uint8_t *pubKeyBuf, uint32_t bufLen, uint32_t *usedLen);
382
399int32_t SAL_CRYPT_CalcEcdhSharedSecret(HITLS_Lib_Ctx *libCtx, const char *attrName,
400 HITLS_CRYPT_Key *key, uint8_t *peerPubkey, uint32_t pubKeyLen,
401 uint8_t *sharedSecret, uint32_t *sharedSecretLen);
402
417int32_t SAL_CRYPT_CalcSm2dhSharedSecret(HITLS_Lib_Ctx *libCtx, const char *attrName,
418 HITLS_Sm2GenShareKeyParameters *sm2ShareKeyParam, uint8_t *sharedSecret,
419 uint32_t *sharedSecretLen);
420
429HITLS_CRYPT_Key *SAL_CRYPT_GenerateDhKeyBySecbits(TLS_Ctx *ctx,
430 int32_t secBits);
431
445HITLS_CRYPT_Key *SAL_CRYPT_GenerateDhKeyByParams(HITLS_Lib_Ctx *libCtx,
446 const char *attrName, uint8_t *p, uint16_t plen, uint8_t *g, uint16_t glen);
447
455HITLS_CRYPT_Key *SAL_CRYPT_DupDhKey(HITLS_CRYPT_Key *key);
456
462void SAL_CRYPT_FreeDhKey(HITLS_CRYPT_Key *key);
463
475int32_t SAL_CRYPT_GetDhParameters(HITLS_CRYPT_Key *key, uint8_t *p, uint16_t *plen,
476 uint8_t *g, uint16_t *glen);
477
490int32_t SAL_CRYPT_EncodeDhPubKey(HITLS_CRYPT_Key *key, uint8_t *pubKeyBuf, uint32_t bufLen, uint32_t *usedLen);
491
508int32_t SAL_CRYPT_CalcDhSharedSecret(HITLS_Lib_Ctx *libCtx, const char *attrName,
509 HITLS_CRYPT_Key *key, uint8_t *peerPubkey, uint32_t pubKeyLen,
510 uint8_t *sharedSecret, uint32_t *sharedSecretLen);
511
526int32_t SAL_CRYPT_HkdfExtract(HITLS_Lib_Ctx *libCtx, const char *attrName,
527 HITLS_CRYPT_HkdfExtractInput *input, uint8_t *prk, uint32_t *prkLen);
528
543int32_t SAL_CRYPT_HkdfExpand(HITLS_Lib_Ctx *libCtx, const char *attrName,
544 HITLS_CRYPT_HkdfExpandInput *input, uint8_t *okm, uint32_t okmLen);
545
558int32_t SAL_CRYPT_HkdfExpandLabel(CRYPT_KeyDeriveParameters *deriveInfo,
559 uint8_t *outSecret, uint32_t outLen);
560
569int32_t SAL_CRYPT_KemEncapsulate(TLS_Ctx *ctx, HITLS_KemEncapsulateParams *params);
570
584int32_t SAL_CRYPT_KemDecapsulate(HITLS_CRYPT_Key *key, const uint8_t *ciphertext, uint32_t ciphertextLen,
585 uint8_t *sharedSecret, uint32_t *sharedSecretLen);
586
587
588#ifdef __cplusplus
589}
590#endif
591#endif
定义 crypt.h:32
HKDF-Expand Input
定义 hitls_crypt_type.h:281
HKDF-Extract Input
定义 hitls_crypt_type.h:269
Key parameters
定义 hitls_crypt_type.h:235
Elliptic curve parameter
定义 hitls_crypt_type.h:222
Input parameters for KEM encapsulation
定义 hitls_crypt_reg.h:36
sm2 ecdhe negotiation key parameters
定义 hitls_crypt_type.h:254