1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HKS_IPC_SERIALIZATION_H
17 #define HKS_IPC_SERIALIZATION_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 
22 #include "hks_type_inner.h"
23 
24 #define MAX_IPC_BUF_SIZE    0x10000   /* Maximun IPC message buffer size. */
25 #define MAX_IPC_RSV_SIZE    0x400     /* Reserve IPC message buffer size */
26 #define MAX_PROCESS_SIZE    (MAX_IPC_BUF_SIZE - MAX_IPC_RSV_SIZE)
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 int32_t CopyUint32ToBuffer(uint32_t value, const struct HksBlob *destBlob, uint32_t *destOffset);
33 
34 int32_t HksGenerateKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
35     const struct HksParamSet *paramSetIn, const struct HksBlob *keyOut);
36 
37 int32_t HksImportKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
38     const struct HksBlob *key);
39 
40 int32_t HksImportWrappedKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
41     const struct HksBlob *wrappingKeyAlias, const struct HksParamSet *paramSet, const struct HksBlob *wrappedKeyData);
42 
43 int32_t HksDeleteKeyPack(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet, struct HksBlob *destData);
44 
45 int32_t HksExportPublicKeyPack(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
46     const struct HksBlob *key, struct HksBlob *destData);
47 
48 int32_t HksGetKeyParamSetPack(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
49     const struct HksBlob *keyOut, struct HksBlob *destData);
50 
51 int32_t HksKeyExistPack(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet, struct HksBlob *destData);
52 
53 int32_t HksOnceParamPack(struct HksBlob *destData, const struct HksBlob *key, const struct HksParamSet *paramSet,
54     uint32_t *offset);
55 
56 int32_t HksOnceDataPack(struct HksBlob *destData, const struct HksBlob *inputData, const struct HksBlob *rsvData,
57     const struct HksBlob *outputData, uint32_t *offset);
58 
59 int32_t HksAgreeKeyPack(struct HksBlob *destData, const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
60     const struct HksBlob *peerPublicKey, const struct HksBlob *agreedKey);
61 
62 int32_t HksDeriveKeyPack(struct HksBlob *destData, const struct HksParamSet *paramSet, const struct HksBlob *kdfKey,
63     const struct HksBlob *derivedKey);
64 
65 int32_t HksGetKeyInfoListPack(const struct HksParamSet *paramSet, const struct HksKeyInfo *keyInfoList,
66     struct HksBlob *destData, uint32_t listCount);
67 
68 int32_t HksGetKeyInfoListUnpackFromService(const struct HksBlob *srcData, uint32_t *listCount,
69     struct HksKeyInfo *keyInfoList);
70 
71 int32_t HksCertificateChainPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
72     const struct HksParamSet *paramSet, const struct HksBlob *certChainBlob);
73 
74 int32_t HksCertificateChainUnpackFromService(const struct HksBlob *srcData, bool needEncode,
75     struct HksCertChain *certChain);
76 
77 int32_t HksParamsToParamSet(struct HksParam *params, uint32_t cnt, struct HksParamSet **outParamSet);
78 
79 int32_t EncodeCertChain(const struct HksBlob *inBlob, struct HksBlob *outBlob);
80 
81 int32_t HksListAliasesPack(const struct HksParamSet *srcParamSet, struct HksBlob *destData);
82 
83 int32_t HksListAliasesUnpackFromService(const struct HksBlob *srcData, struct HksKeyAliasSet **destData);
84 
85 int32_t HksRenameKeyAliasPack(const struct HksBlob *oldKeyAlias, const struct HksBlob *newKeyAlias,
86     const struct HksParamSet *paramSet, struct HksBlob *destData);
87 
88 int32_t HksChangeStorageLevelPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
89     const struct HksParamSet *srcParamSet, const struct HksParamSet *destParamSet);
90 
91 #ifdef __cplusplus
92 }
93 #endif
94 
95 #endif /* HKS_IPC_SERIALIZATION_H */