• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_CRYPTO_HAL_H
17 #define HKS_CRYPTO_HAL_H
18 
19 #include "hks_type.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 enum HksKeyAlgMode {
26     HKS_ALGORITHM_RSA_MODE_CRT = 1,
27     HKS_ALGORITHM_RSA_MODE_NO_CRT = 2,
28     HKS_ALGORITHM_EC_MODE_ECDH = 3,
29     HKS_ALGORITHM_ED_MODE_SIG_VERIFY = 4,
30     HKS_ALGORITHM_ED_MODE_VERIFY = 5,
31     HKS_ALGORITHM_X25519_MODE = 6,
32 };
33 
34 enum {
35     OPENSSL_CTX_PADDING_NONE = 0, /* set chipher padding none */
36     OPENSSL_CTX_PADDING_ENABLE = 1, /* set chipher padding enable */
37 };
38 
39 struct HksKeySpec {
40     uint32_t algType;
41     uint32_t keyLen;
42     void *algParam; /* for example : struct HksKeyDerivationParam */
43 };
44 
45 struct HksKeyDerivationParam {
46     struct HksBlob salt;
47     struct HksBlob info;
48     uint32_t iterations;
49     uint32_t digestAlg;
50 };
51 
52 struct HksAeadParam {
53     struct HksBlob nonce;
54     struct HksBlob aad;
55     union {
56         struct HksBlob tagDec;
57         uint32_t tagLenEnc;
58     };
59     uint32_t payloadLen;
60 };
61 
62 struct HksCipherParam {
63     struct HksBlob iv;
64 };
65 
66 struct HksUsageSpec {
67     uint32_t algType;
68     uint32_t mode;
69     uint32_t padding;
70     uint32_t mgfDigest;
71     uint32_t digest;
72     uint32_t purpose;
73     uint32_t pssSaltLenType;
74     /*
75      * Different algorithms correspond to different structures,for example:
76      * struct HksAeadParam for aead;
77      * struct HksCipherParam for cipher;
78      */
79     void *algParam;
80 };
81 
82 struct KeyMaterialRsa {
83     enum HksKeyAlg keyAlg;
84     uint32_t keySize;
85     uint32_t nSize;
86     uint32_t eSize;
87     uint32_t dSize;
88 };
89 #define RSA_KEY_MATERIAL_CNT 3U
90 
91 struct KeyMaterialEcc {
92     enum HksKeyAlg keyAlg;
93     uint32_t keySize;
94     uint32_t xSize;
95     uint32_t ySize;
96     uint32_t zSize;
97 };
98 #define ECC_KEY_MATERIAL_CNT 3U
99 
100 struct KeyMaterialDsa {
101     enum HksKeyAlg keyAlg;
102     uint32_t keySize;
103     uint32_t xSize;
104     uint32_t ySize;
105     uint32_t pSize;
106     uint32_t qSize;
107     uint32_t gSize;
108 };
109 
110 struct KeyMaterialDh {
111     enum HksKeyAlg keyAlg;
112     uint32_t keySize;
113     uint32_t pubKeySize;
114     uint32_t priKeySize;
115     uint32_t reserved;
116 };
117 
118 struct KeyMaterial25519 {
119     enum HksKeyAlg keyAlg;
120     uint32_t keySize;
121     uint32_t pubKeySize;
122     uint32_t priKeySize;
123     uint32_t reserved;
124 };
125 
126 typedef int32_t (*GetMainKey)(const struct HksBlob *, struct HksBlob *);
127 
128 typedef int32_t (*GenerateKey)(const struct HksKeySpec *, struct HksBlob *);
129 
130 typedef int32_t (*PubKey)(const struct HksBlob *, struct HksBlob *);
131 
132 typedef int32_t (*DeriveKey)(const struct HksBlob *, const struct HksKeySpec *, struct HksBlob *);
133 
134 typedef int32_t (*FillRandom)(struct HksBlob *);
135 
136 typedef int32_t (*AgreeKey)(const struct HksBlob *, const struct HksBlob *, const struct HksKeySpec *,
137     struct HksBlob *);
138 
139 typedef int32_t (*Sign)(const struct HksBlob *, const struct HksUsageSpec *, const struct HksBlob *,
140     struct HksBlob *);
141 
142 typedef int32_t (*Verify)(const struct HksBlob *, const struct HksUsageSpec *, const struct HksBlob *,
143     const struct HksBlob *);
144 
145 typedef int32_t (*Hmac)(const struct HksBlob *, uint32_t, const struct HksBlob *, struct HksBlob *);
146 
147 typedef int32_t (*HmacInit)(void **, const struct HksBlob *, uint32_t);
148 
149 typedef int32_t (*HmacUpdate)(void *, const struct HksBlob *);
150 
151 typedef int32_t (*HmacFinal)(void **, const struct HksBlob *, struct HksBlob *);
152 
153 typedef int32_t (*CmacInit)(void **, const struct HksBlob *, const struct HksUsageSpec *);
154 
155 typedef int32_t (*CmacUpdate)(void *, const struct HksBlob *, const struct HksUsageSpec *);
156 
157 typedef int32_t (*CmacFinal)(void **, const struct HksBlob *, struct HksBlob *, const struct HksUsageSpec *);
158 
159 typedef int32_t (*Hash)(uint32_t, const struct HksBlob *, struct HksBlob *);
160 
161 typedef int32_t (*HashInit)(void **, uint32_t);
162 
163 typedef int32_t (*HashUpdate)(void *, const struct HksBlob *);
164 
165 typedef int32_t (*HashFinal)(void **, const struct HksBlob *, struct HksBlob *);
166 
167 typedef int32_t (*Encrypt)(const struct HksBlob *, const struct HksUsageSpec *,
168     const struct HksBlob *, struct HksBlob *, struct HksBlob *);
169 
170 typedef int32_t (*EncryptInit)(void **, const struct HksBlob *, const struct HksUsageSpec *, const bool);
171 
172 typedef int32_t (*EncryptUpdate)(void *, const struct HksBlob *, struct HksBlob *, const bool);
173 
174 typedef int32_t (*EncryptFinal)(void **, const struct HksBlob *, struct HksBlob *, struct HksBlob *, const bool);
175 
176 typedef int32_t (*Decrypt)(const struct HksBlob *, const struct HksUsageSpec *,
177     const struct HksBlob *, struct HksBlob *);
178 
179 typedef int32_t (*DecryptInit)(void **, const struct HksBlob *, const struct HksUsageSpec *, const bool);
180 
181 typedef int32_t (*DecryptUpdate)(void *, const struct HksBlob *, struct HksBlob *, const bool);
182 
183 typedef int32_t (*DecryptFinal)(void **, const struct HksBlob *, struct HksBlob *, struct HksBlob *, const bool);
184 
185 typedef int32_t (*DecryptFinalDes)(void **, const struct HksBlob *, struct HksBlob *, const bool);
186 
187 typedef int32_t (*BnExpMod)(struct HksBlob *, const struct HksBlob *,
188     const struct HksBlob *, const struct HksBlob *);
189 
190 typedef void (*FreeCtx)(void **);
191 
192 int32_t HksCryptoHalGetMainKey(const struct HksBlob *message, struct HksBlob *mainKey);
193 
194 int32_t HksCryptoHalGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key);
195 
196 int32_t HksCryptoHalGetPubKey(const struct HksBlob *keyIn, struct HksBlob *keyOut);
197 
198 int32_t HksCryptoHalDeriveKey(const struct HksBlob *mainKey, const struct HksKeySpec *derivationSpec,
199     struct HksBlob *derivedKey);
200 
201 int32_t HksCryptoHalFillRandom(struct HksBlob *randomData);
202 
203 int32_t HksCryptoHalFillPrivRandom(struct HksBlob *randomData);
204 
205 int32_t HksCryptoHalAddEntropy(const struct HksBlob *entropy);
206 
207 int32_t HksCryptoHalAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey,
208     const struct HksKeySpec *spec, struct HksBlob *sharedKey);
209 
210 int32_t HksCryptoHalSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
211     const struct HksBlob *message, struct HksBlob *signature);
212 
213 int32_t HksCryptoHalVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
214     const struct HksBlob *message, const struct HksBlob *signature);
215 
216 int32_t HksCryptoHalSignIsoIec97962(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
217     const struct HksBlob *message, struct HksBlob *signature);
218 
219 int32_t HksCryptoHalVerifyIsoIec97962(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
220     const struct HksBlob *message, const struct HksBlob *signature);
221 
222 int32_t HksCryptoHalHmacInit(const struct HksBlob *key, uint32_t digestAlg, void **ctx);
223 
224 int32_t HksCryptoHalHmacUpdate(const struct HksBlob *chunk, void *ctx);
225 
226 int32_t HksCryptoHalHmacFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *mac);
227 
228 void HksCryptoHalHmacFreeCtx(void **ctx);
229 
230 int32_t HksCryptoHalHmac(const struct HksBlob *key, uint32_t digestAlg, const struct HksBlob *msg,
231     struct HksBlob *mac);
232 
233 int32_t HksCryptoHalCmacInit(const struct HksBlob *key, void **ctx, const struct HksUsageSpec *usageSpec);
234 
235 int32_t HksCryptoHalCmacUpdate(const struct HksBlob *chunk, void *ctx, const struct HksUsageSpec *usageSpec);
236 
237 int32_t HksCryptoHalCmacFinal(
238     const struct HksBlob *msg, void **ctx, struct HksBlob *mac, const struct HksUsageSpec *usageSpec);
239 
240 void HksCryptoHalCmacFreeCtx(void **ctx);
241 
242 int32_t HksCryptoHalHashInit(uint32_t alg, void **ctx);
243 
244 int32_t HksCryptoHalHashUpdate(const struct HksBlob *msg, void *ctx);
245 
246 int32_t HksCryptoHalHashFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *hash);
247 
248 void HksCryptoHalHashFreeCtx(void **ctx);
249 
250 int32_t HksCryptoHalHash(uint32_t alg, const struct HksBlob *msg, struct HksBlob *hash);
251 
252 int32_t HksCryptoHalEncryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx);
253 
254 int32_t HksCryptoHalEncryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out,
255     const uint32_t algtype);
256 
257 int32_t HksCryptoHalEncryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText,
258     struct HksBlob *tagAead, const uint32_t algtype);
259 
260 void HksCryptoHalEncryptFreeCtx(void **ctx, const uint32_t algtype);
261 
262 int32_t HksCryptoHalEncrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
263     const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead);
264 
265 int32_t HksCryptoHalDecryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx);
266 
267 int32_t HksCryptoHalDecryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out,
268     const uint32_t algtype);
269 
270 int32_t HksCryptoHalDecryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText,
271     struct HksBlob *tagAead, const uint32_t algtype);
272 
273 void HksCryptoHalDecryptFreeCtx(void **ctx, const uint32_t algtype);
274 
275 int32_t HksCryptoHalDecrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
276     const struct HksBlob *message, struct HksBlob *cipherText);
277 
278 int32_t HksCryptoHalBnExpMod(struct HksBlob *x, const struct HksBlob *a,
279     const struct HksBlob *e, const struct HksBlob *n);
280 
281 int32_t HksCryptoHalInit(void);
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 
287 #endif /* HKS_CRYPTO_HAL_H */
288