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