• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 ALG_LOADER_H
17 #define ALG_LOADER_H
18 
19 #include "alg_defs.h"
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_type.h"
23 #include "hal_error.h"
24 #include "hc_types.h"
25 
26 #define BITS_PER_BYTE 8
27 #define ECC_PK_LEN 32
28 
29 #define CAL_ARRAY_SIZE(arr) ((sizeof(arr)) / (sizeof((arr)[0])))
30 
31 #define CHECK_LEN_ZERO_RETURN_ERROR_CODE(len, paramTag) \
32     do { \
33         if ((len) == 0) { \
34             LOGE("%" LOG_PUB "s is invalid length.", (paramTag)); \
35             return HAL_ERR_INVALID_LEN; \
36         } \
37     } while (0)
38 
39 #define CHECK_PTR_RETURN_HAL_ERROR_CODE(ptr, paramTag) \
40     do { \
41         if ((ptr) == NULL) { \
42             LOGE("%" LOG_PUB "s is null.", (paramTag)); \
43             return HAL_ERR_NULL_PTR; \
44         } \
45     } while (0)
46 
47 #define CHECK_LEN_LOWER_RETURN(len, min, paramTag) \
48     do { \
49         if ((len) < (min)) { \
50             LOGE("%" LOG_PUB "s is invalid length.", (paramTag)); \
51             return HAL_ERR_INVALID_LEN; \
52         } \
53     } while (0)
54 
55 #define CHECK_LEN_HIGHER_RETURN(len, max, paramTag) \
56     do { \
57         if ((len) > (max)) { \
58             LOGE("%" LOG_PUB "s is invalid length.", (paramTag)); \
59             return HAL_ERR_INVALID_LEN; \
60         } \
61     } while (0)
62 
63 #define CHECK_LEN_EQUAL_RETURN(len, value, paramTag) \
64     do { \
65         if ((len) != (value)) { \
66             LOGE("%" LOG_PUB "s is invalid length.", (paramTag)); \
67             return HAL_ERR_INVALID_LEN; \
68         } \
69     } while (0)
70 
71 struct KeyRoleInfo {
72     uint8_t userType;
73     uint8_t pairType;
74     uint8_t reserved1;
75     uint8_t reserved2;
76 };
77 
78 union KeyRoleInfoUnion {
79     struct KeyRoleInfo roleInfoStruct;
80     uint32_t roleInfo;
81 };
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 int32_t CheckKeyParams(const KeyParams *keyParams);
88 void FreeParamSet(struct HksParamSet *paramSet);
89 int32_t ConstructParamSet(struct HksParamSet **out, const struct HksParam *inParam,
90     const uint32_t inParamNum);
91 int32_t BigNumExpMod(const Uint8Buff *base, const Uint8Buff *exp, const char *bigNumHex,
92     Uint8Buff *outNum);
93 void MoveDeKeyToCe(bool isKeyAlias, int32_t osAccountId, const struct HksBlob *keyAliasBlob);
94 int32_t ConstructCheckParamSet(bool isDeStorage, int32_t osAccountId, struct HksParamSet **paramSet);
95 int32_t ConstructDeleteParamSet(bool isDeStorage, int32_t osAccountId, struct HksParamSet **paramSet);
96 int32_t ConstructHmacParamSet(bool isDeStorage, int32_t osAccountId, bool isAlias,
97     struct HksParamSet **hmacParamSet);
98 int32_t CheckHmacParams(const KeyParams *keyParams, const Uint8Buff *message, const Uint8Buff *outHmac);
99 int32_t ConstructDeriveParamSet(const KeyParams *keyParams, const Uint8Buff *message,
100     struct HksParamSet **deriveParamSet);
101 int32_t ConstructFinishParamSet(const KeyParams *keyParams, struct HksParamSet **finishParamSet);
102 int32_t CheckHmacWithThreeStageParams(const KeyParams *keyParams, const Uint8Buff *message,
103     const Uint8Buff *outHmac);
104 int32_t ConstructHkdfParamSet(bool isDeStorage, const KeyParams *keyParams, const Uint8Buff *salt,
105     const Uint8Buff *keyInfo, struct HksParamSet **paramSet);
106 int32_t CheckHkdfParams(const KeyParams *keyParams, const Uint8Buff *salt, const Uint8Buff *outHkdf);
107 int32_t ConstructPseudonymParamSet(const KeyParams *keyParams, const Uint8Buff *pskKeyAlias,
108     const struct HksBlob *extInfoBlob, uint32_t outLen, struct HksParamSet **paramSet);
109 int32_t CheckPskParams(const KeyParams *keyParams, const Uint8Buff *pskKeyAlias, const Uint8Buff *outPsk);
110 int32_t GetExtInfoByParamSet(const struct HksParamSet *outParamSet, Uint8Buff *outExtInfo);
111 int32_t ConstructGetKeyExtInfoParamSet(const KeyParams *keyParams, struct HksParamSet **paramSet);
112 int32_t CheckAesGcmEncryptParam(const KeyParams *keyParams, const Uint8Buff *plain,
113     const GcmParam *encryptInfo, Uint8Buff *outCipher);
114 int32_t ConstructAesGcmEncryptParamSet(const GcmParam *encryptInfo, const KeyParams *keyParams,
115     struct HksParamSet **paramSet);
116 int32_t CheckAesGcmDecryptParam(const KeyParams *keyParams, const Uint8Buff *cipher,
117     const GcmParam *decryptInfo, Uint8Buff *outPlain);
118 int32_t ConstructAesGcmDecryptParamSet(const GcmParam *decryptInfo, const KeyParams *keyParams,
119     struct HksParamSet **paramSet);
120 int32_t ConstructInitParamsP256(struct HksParamSet **initParamSet, const KeyParams *keyParams);
121 int32_t ConstructFinishParamsP256(struct HksParamSet **finishParamSet, const KeyParams *keyParams,
122     const struct HksBlob *sharedKeyAliasBlob);
123 int32_t ConstructAgreeWithStorageParams(struct HksParamSet **paramSet, uint32_t keyLen, Algorithm algo,
124     const KeyParams *priKeyParams, const KeyBuff *pubKeyBuff);
125 int32_t CheckAgreeWithStorageParams(const KeyParams *priKeyParams, const KeyBuff *pubKeyBuff,
126     uint32_t sharedKeyLen, const Uint8Buff *sharedKeyAlias);
127 void MoveSharedKeyToCe(const KeyParams *priKeyParams, const struct HksBlob *sharedKeyAlias);
128 int32_t CheckAgreeParams(const KeyParams *priKeyParams, const KeyBuff *pubKey, const Uint8Buff *sharedKey);
129 int32_t ConstructAgreeParamSet(const KeyParams *keyParams, Algorithm algo, const Uint8Buff *sharedKey,
130     struct HksParamSet **paramSet);
131 int32_t ConstructGenerateKeyPairWithStorageParams(struct HksParamSet **paramSet, Algorithm algo,
132     uint32_t keyLen, KeyPurpose purpose, const KeyParams *authIdParams);
133 int32_t CheckGenerateKeyPairParams(const KeyParams *keyParams, const ExtraInfo *exInfo, uint32_t keyLen);
134 int32_t ConstructGenerateKeyPairParams(struct HksParamSet **paramSet, Algorithm algo, uint32_t keyLen);
135 int32_t ConstructExportParams(bool isDeStorage, int32_t osAccountId, struct HksParamSet **paramSet);
136 int32_t CheckExportParams(const KeyParams *keyParams, const Uint8Buff *outPubKey);
137 int32_t ConstructSignParams(bool isDeStorage, int32_t osAccountId, struct HksParamSet **paramSet,
138     Algorithm algo);
139 int32_t CheckSignParams(const KeyParams *keyParams, const Uint8Buff *message,
140     const Uint8Buff *outSignature);
141 int32_t ConstructVerifyParams(struct HksParamSet **paramSet, const KeyParams *keyParams, Algorithm algo);
142 int32_t CheckVerifyParams(const KeyParams *keyParams, const Uint8Buff *message,
143     const Uint8Buff *signature);
144 int32_t ConstructImportPublicKeyParams(struct HksParamSet **paramSet, Algorithm algo, uint32_t keyLen,
145     const KeyParams *authIdParams, const union KeyRoleInfoUnion *roleInfoUnion);
146 int32_t CheckImportPubKeyParams(const KeyParams *keyParams, const Uint8Buff *pubKey,
147     const ExtraInfo *exInfo);
148 bool CheckBigNumCompareParams(const Uint8Buff *a, const Uint8Buff *b, int *res);
149 int32_t ConstructImportSymmetricKeyParam(struct HksParamSet **paramSet, const KeyParams *keyParams,
150     uint32_t keyLen, KeyPurpose purpose, const ExtraInfo *exInfo);
151 int32_t CheckImportSymmetricKeyParams(const KeyParams *keyParams, const Uint8Buff *authToken);
152 
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif