1526fd984Sopenharmony_ci/*
2526fd984Sopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
3526fd984Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4526fd984Sopenharmony_ci * you may not use this file except in compliance with the License.
5526fd984Sopenharmony_ci * You may obtain a copy of the License at
6526fd984Sopenharmony_ci *
7526fd984Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
8526fd984Sopenharmony_ci *
9526fd984Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10526fd984Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11526fd984Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12526fd984Sopenharmony_ci * See the License for the specific language governing permissions and
13526fd984Sopenharmony_ci * limitations under the License.
14526fd984Sopenharmony_ci */
15526fd984Sopenharmony_ci
16526fd984Sopenharmony_ci#ifndef HKS_CLIENT_H
17526fd984Sopenharmony_ci#define HKS_CLIENT_H
18526fd984Sopenharmony_ci
19526fd984Sopenharmony_ci#include "hks_types.h"
20526fd984Sopenharmony_ci
21526fd984Sopenharmony_ci#ifdef __cplusplus
22526fd984Sopenharmony_ciextern "C" {
23526fd984Sopenharmony_ci#endif
24526fd984Sopenharmony_ci
25526fd984Sopenharmony_ci/*
26526fd984Sopenharmony_ci * hks_get_sdk_version
27526fd984Sopenharmony_ci * get sdk version
28526fd984Sopenharmony_ci * return value: none
29526fd984Sopenharmony_ci */
30526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC void hks_get_sdk_version(struct hks_blob *sdk_version);
31526fd984Sopenharmony_ci
32526fd984Sopenharmony_ci/*
33526fd984Sopenharmony_ci * hks init
34526fd984Sopenharmony_ci * load file hks_keystore to buffer
35526fd984Sopenharmony_ci * parameter: none
36526fd984Sopenharmony_ci * return value: none
37526fd984Sopenharmony_ci */
38526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_init(void);
39526fd984Sopenharmony_ci
40526fd984Sopenharmony_ci/*
41526fd984Sopenharmony_ci * destroy
42526fd984Sopenharmony_ci * parameter: none
43526fd984Sopenharmony_ci * return value: none
44526fd984Sopenharmony_ci */
45526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC void hks_destroy(void);
46526fd984Sopenharmony_ci
47526fd984Sopenharmony_ci/*
48526fd984Sopenharmony_ci * refresh key info and root key info
49526fd984Sopenharmony_ci * Reproduce the hks_keystore file header and clear the old key
50526fd984Sopenharmony_ci * Reproduce root key info
51526fd984Sopenharmony_ci * parameter: none
52526fd984Sopenharmony_ci * return value: none
53526fd984Sopenharmony_ci */
54526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_refresh_key_info(void);
55526fd984Sopenharmony_ci
56526fd984Sopenharmony_ci/*
57526fd984Sopenharmony_ci * generate key
58526fd984Sopenharmony_ci * Only ED25519 algorithm key pair generation is supported
59526fd984Sopenharmony_ci * The number of local storage keys (including
60526fd984Sopenharmony_ci * generated ED25519 public-private key pairs
61526fd984Sopenharmony_ci * imported ED25519 public keys) is limited to 16
62526fd984Sopenharmony_ci * key_alias: key alias, constraint condition:
63526fd984Sopenharmony_ci * key_alias->size <= 64
64526fd984Sopenharmony_ci * key_param: The parameter of the key which need to generate
65526fd984Sopenharmony_ci * constraint condition:
66526fd984Sopenharmony_ci * key_param cannot be NULL
67526fd984Sopenharmony_ci * key_param->key_type must be HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519
68526fd984Sopenharmony_ci * return 0 OK, other error
69526fd984Sopenharmony_ci */
70526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_generate_key(const struct hks_blob *key_alias,
71526fd984Sopenharmony_ci    const struct hks_key_param *key_param);
72526fd984Sopenharmony_ci
73526fd984Sopenharmony_ci/*
74526fd984Sopenharmony_ci * generate asymmetric key
75526fd984Sopenharmony_ci * Only X25519 algorithm key pair generation is supported
76526fd984Sopenharmony_ci * key_param: The parameter of the key which need to generate
77526fd984Sopenharmony_ci * constraint condition:
78526fd984Sopenharmony_ci * key_param.key_type must be
79526fd984Sopenharmony_ci * HKS_KEY_TYPE_ECC_KEYPAIR_CURVE25519
80526fd984Sopenharmony_ci * key_param.usage must be
81526fd984Sopenharmony_ci * hks_alg_ecdh(HKS_ALG_SELECT_RAW)
82526fd984Sopenharmony_ci * return 0 OK, other error
83526fd984Sopenharmony_ci */
84526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_generate_asymmetric_key(
85526fd984Sopenharmony_ci    const struct hks_key_param *key_param, struct hks_blob *pri_key,
86526fd984Sopenharmony_ci    struct hks_blob *pub_key);
87526fd984Sopenharmony_ci
88526fd984Sopenharmony_ci/*
89526fd984Sopenharmony_ci * import public key
90526fd984Sopenharmony_ci * Only ED25519 public key import is supported
91526fd984Sopenharmony_ci * The number of local storage keys (including
92526fd984Sopenharmony_ci * generated ED25519 public-private key pairs
93526fd984Sopenharmony_ci * imported ED25519 public keys) is limited to no more than 16
94526fd984Sopenharmony_ci * key_param: The parameter of the key which need to generate
95526fd984Sopenharmony_ci * constraint condition:
96526fd984Sopenharmony_ci * key_param.key_type must be
97526fd984Sopenharmony_ci * HKS_KEY_TYPE_EDDSA_PUBLIC_KEY_ED25519
98526fd984Sopenharmony_ci * return 0 OK, other error
99526fd984Sopenharmony_ci */
100526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_import_public_key(
101526fd984Sopenharmony_ci    const struct hks_blob *key_alias,
102526fd984Sopenharmony_ci    const struct hks_key_param *key_param, const struct hks_blob *key);
103526fd984Sopenharmony_ci
104526fd984Sopenharmony_ci/*
105526fd984Sopenharmony_ci * export public key
106526fd984Sopenharmony_ci * Only ED25519 public key export is supported
107526fd984Sopenharmony_ci * key_alias: constraint condition: key_alias->size <= 64
108526fd984Sopenharmony_ci * return 0 OK, other error
109526fd984Sopenharmony_ci */
110526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_export_public_key(
111526fd984Sopenharmony_ci    const struct hks_blob *key_alias, struct hks_blob *key);
112526fd984Sopenharmony_ci
113526fd984Sopenharmony_ci/*
114526fd984Sopenharmony_ci * delete public key
115526fd984Sopenharmony_ci * Only ED25519 public key delete is supported
116526fd984Sopenharmony_ci * key_alias: constraint condition: key_alias->size <= 64
117526fd984Sopenharmony_ci * return 0 OK, other error
118526fd984Sopenharmony_ci */
119526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_delete_key(const struct hks_blob *key_alias);
120526fd984Sopenharmony_ci
121526fd984Sopenharmony_ci/*
122526fd984Sopenharmony_ci * get key param
123526fd984Sopenharmony_ci * key_alias: constraint condition: key_alias->size <= 64
124526fd984Sopenharmony_ci * return 0 OK, other error
125526fd984Sopenharmony_ci */
126526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_get_key_param(const struct hks_blob *key_alias,
127526fd984Sopenharmony_ci    struct hks_key_param *key_param);
128526fd984Sopenharmony_ci
129526fd984Sopenharmony_ci/*
130526fd984Sopenharmony_ci * is key exist
131526fd984Sopenharmony_ci * key_alias: constraint condition: key_alias->size <= 64
132526fd984Sopenharmony_ci * return: 0 - exist; other - Non-existent
133526fd984Sopenharmony_ci */
134526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_is_key_exist(const struct hks_blob *key_alias);
135526fd984Sopenharmony_ci
136526fd984Sopenharmony_ci/*
137526fd984Sopenharmony_ci * generate random
138526fd984Sopenharmony_ci * random: random->size must be specified by the caller
139526fd984Sopenharmony_ci * constraint condition: random->size <= 1024
140526fd984Sopenharmony_ci * return 0 OK, other error
141526fd984Sopenharmony_ci */
142526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_generate_random(struct hks_blob *random);
143526fd984Sopenharmony_ci
144526fd984Sopenharmony_ci/*
145526fd984Sopenharmony_ci * sign
146526fd984Sopenharmony_ci * Only ED25519 local storage private key signature is supported
147526fd984Sopenharmony_ci * key_alias: constraint condition: key_alias->size <= 64
148526fd984Sopenharmony_ci * key_param: constraint condition:
149526fd984Sopenharmony_ci * key_param.key_type must be
150526fd984Sopenharmony_ci * HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519
151526fd984Sopenharmony_ci * signature: constraint condition:
152526fd984Sopenharmony_ci * signature->size >=  64
153526fd984Sopenharmony_ci * return 0 OK, other error
154526fd984Sopenharmony_ci */
155526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_asymmetric_sign(
156526fd984Sopenharmony_ci    const struct hks_blob *key_alias,
157526fd984Sopenharmony_ci    const struct hks_key_param *key_param, const struct hks_blob *hash,
158526fd984Sopenharmony_ci    struct hks_blob *signature);
159526fd984Sopenharmony_ci
160526fd984Sopenharmony_ci/*
161526fd984Sopenharmony_ci * verify
162526fd984Sopenharmony_ci * Only ED25519 public key verify is supported
163526fd984Sopenharmony_ci * key: key alias or the key value itself,
164526fd984Sopenharmony_ci * differentiate by key.type:
165526fd984Sopenharmony_ci * if it is the key alias, key.type must be HKS_BLOB_TYPE_ALIAS
166526fd984Sopenharmony_ci * it is the key value itself, key.type must be HKS_BLOB_TYPE_KEY
167526fd984Sopenharmony_ci * signature: constraint condition:
168526fd984Sopenharmony_ci * signature->size >=  64
169526fd984Sopenharmony_ci * return 0 OK, other error
170526fd984Sopenharmony_ci */
171526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_asymmetric_verify(const struct hks_blob *key,
172526fd984Sopenharmony_ci    const struct hks_key_param *key_param, const struct hks_blob *hash,
173526fd984Sopenharmony_ci    const struct hks_blob *signature);
174526fd984Sopenharmony_ci
175526fd984Sopenharmony_ci/*
176526fd984Sopenharmony_ci * encrypt
177526fd984Sopenharmony_ci * only support AES-128-GCM encrypt
178526fd984Sopenharmony_ci * key: used to ecrypt plain_text
179526fd984Sopenharmony_ci * key_param: constraint condition:
180526fd984Sopenharmony_ci * key_param.key_type is HKS_KEY_TYPE_AES;
181526fd984Sopenharmony_ci * key_param.key_len is 128 or 192 or 256;
182526fd984Sopenharmony_ci * key_param.key_usage is HKS_KEY_USAGE_ENCRYPT;
183526fd984Sopenharmony_ci * key_param.key_mode is HKS_ALG_GCM;
184526fd984Sopenharmony_ci * key_param.key_pad is HKS_PADDING_NONE;
185526fd984Sopenharmony_ci * crypt_param:
186526fd984Sopenharmony_ci * crypt_param.nonce.size suggest to be 16
187526fd984Sopenharmony_ci * crypt_param.aad.size suggest to be 16
188526fd984Sopenharmony_ci * return 0 OK, other error
189526fd984Sopenharmony_ci */
190526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_aead_encrypt(const struct hks_blob *key,
191526fd984Sopenharmony_ci    const struct hks_key_param *key_param,
192526fd984Sopenharmony_ci    const struct hks_crypt_param *crypt_param,
193526fd984Sopenharmony_ci    const struct hks_blob *plain_text,
194526fd984Sopenharmony_ci    struct hks_blob *cipher_text_with_tag);
195526fd984Sopenharmony_ci
196526fd984Sopenharmony_ci/*
197526fd984Sopenharmony_ci * decrypt
198526fd984Sopenharmony_ci * only support AES-128-GCM decrypt
199526fd984Sopenharmony_ci * key: used to decrypt cipher_text_with_tag
200526fd984Sopenharmony_ci * key_param: constraint condition:
201526fd984Sopenharmony_ci * key_param.key_type is HKS_KEY_TYPE_AES;
202526fd984Sopenharmony_ci * key_param.key_len is 128 or 192 or 256;
203526fd984Sopenharmony_ci * key_param.key_usage is HKS_KEY_USAGE_DECRYPT;
204526fd984Sopenharmony_ci * key_param.key_mode is HKS_ALG_GCM;
205526fd984Sopenharmony_ci * key_param.key_pad is HKS_PADDING_NONE;
206526fd984Sopenharmony_ci * crypt_param:
207526fd984Sopenharmony_ci * crypt_param.nonce.size suggest to be 16
208526fd984Sopenharmony_ci * crypt_param.aad.size suggest to be 16
209526fd984Sopenharmony_ci * return 0 OK, other error
210526fd984Sopenharmony_ci */
211526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_aead_decrypt(const struct hks_blob *key,
212526fd984Sopenharmony_ci    const struct hks_key_param *key_param,
213526fd984Sopenharmony_ci    const struct hks_crypt_param *crypt_param,
214526fd984Sopenharmony_ci    struct hks_blob *plain_text,
215526fd984Sopenharmony_ci    const struct hks_blob *cipher_text_with_tag);
216526fd984Sopenharmony_ci
217526fd984Sopenharmony_ci/*
218526fd984Sopenharmony_ci * key agreement
219526fd984Sopenharmony_ci * private_key_param: constraint condition:
220526fd984Sopenharmony_ci * private_key_param.key_type is HKS_KEY_TYPE_ECC_KEYPAIR_CURVE25519
221526fd984Sopenharmony_ci * private_key_param.key_usage is HKS_KEY_USAGE_DERIVE
222526fd984Sopenharmony_ci * private_key_param.key_mode is the same as agreement_alg
223526fd984Sopenharmony_ci * private_key: constraint condition:
224526fd984Sopenharmony_ci * private_key.size must be 32
225526fd984Sopenharmony_ci * peer_public_key: constraint condition:
226526fd984Sopenharmony_ci * peer_public_key.size must be 32
227526fd984Sopenharmony_ci * agreement_alg: constraint condition:
228526fd984Sopenharmony_ci * agreement_alg must be
229526fd984Sopenharmony_ci * hks_alg_ecdh(HKS_ALG_SELECT_RAW)
230526fd984Sopenharmony_ci * return 0 OK, other error
231526fd984Sopenharmony_ci */
232526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_key_agreement(struct hks_blob *agreed_key,
233526fd984Sopenharmony_ci    const struct hks_key_param *private_key_param,
234526fd984Sopenharmony_ci    const uint32_t agreement_alg, const struct hks_blob *private_key,
235526fd984Sopenharmony_ci    const struct hks_blob *peer_public_key);
236526fd984Sopenharmony_ci
237526fd984Sopenharmony_ci/*
238526fd984Sopenharmony_ci * key derivation
239526fd984Sopenharmony_ci * derived_key and data cannot be null, and size >= 16
240526fd984Sopenharmony_ci * key_param: constraint condition:
241526fd984Sopenharmony_ci * key_param.key_type is HKS_KEY_TYPE_DERIVE
242526fd984Sopenharmony_ci * key_param.key_usage is HKS_KEY_USAGE_DERIVE
243526fd984Sopenharmony_ci * key_param.key_mode is hks_alg_hkdf(HKS_ALG_HASH_SHA_256) or
244526fd984Sopenharmony_ci * hks_alg_hkdf(HKS_ALG_HASH_SHA_512)
245526fd984Sopenharmony_ci * key_param.key_len is 128 or 256
246526fd984Sopenharmony_ci * salt: constraint condition:
247526fd984Sopenharmony_ci * salt.size must be greater than or equal to 16
248526fd984Sopenharmony_ci * label: constraint condition:
249526fd984Sopenharmony_ci * lable.size must be greater than or equal to 16
250526fd984Sopenharmony_ci * return 0 OK, other error
251526fd984Sopenharmony_ci */
252526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_key_derivation(struct hks_blob *derived_key,
253526fd984Sopenharmony_ci    const struct hks_key_param *key_param, const struct hks_blob *kdf_key,
254526fd984Sopenharmony_ci    const struct hks_blob *salt, const struct hks_blob *label);
255526fd984Sopenharmony_ci
256526fd984Sopenharmony_ci/*
257526fd984Sopenharmony_ci * hks_hmac
258526fd984Sopenharmony_ci * key: data cannot be null,and size > 0
259526fd984Sopenharmony_ci * src_data: data cannot be null,and size > 0
260526fd984Sopenharmony_ci * alg: hks_alg_hmac(HKS_ALG_HASH_SHA_256) or
261526fd984Sopenharmony_ci * hks_alg_hmac(HKS_ALG_HASH_SHA_512)
262526fd984Sopenharmony_ci * output: output and output->data cannot be null
263526fd984Sopenharmony_ci * constraint condition:
264526fd984Sopenharmony_ci * when alg is hks_alg_hmac(HKS_ALG_HASH_SHA_256), output->size must be
265526fd984Sopenharmony_ci * greater than or equal to 32
266526fd984Sopenharmony_ci * when alg is hks_alg_hmac(HKS_ALG_HASH_SHA_512), output->size must be
267526fd984Sopenharmony_ci * greater than or equal to 64
268526fd984Sopenharmony_ci * return 0 OK, other error
269526fd984Sopenharmony_ci */
270526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_hmac(const struct hks_blob *key,
271526fd984Sopenharmony_ci    const uint32_t alg, const struct hks_blob *src_data,
272526fd984Sopenharmony_ci    struct hks_blob *output);
273526fd984Sopenharmony_ci
274526fd984Sopenharmony_ci/*
275526fd984Sopenharmony_ci * hks_hash
276526fd984Sopenharmony_ci * alg: Hash algorithm, Only spuuort SHA256/SHA512
277526fd984Sopenharmony_ci * src_data: data cannot be null, and size > 0
278526fd984Sopenharmony_ci * hash: hash and hash->data cannot be null
279526fd984Sopenharmony_ci * constraint condition:
280526fd984Sopenharmony_ci * when alg is HKS_ALG_HASH_SHA_256, hash->size must be
281526fd984Sopenharmony_ci * greater than or equal to 32
282526fd984Sopenharmony_ci * when alg is HKS_ALG_HASH_SHA_512, hash->size must be
283526fd984Sopenharmony_ci * greater than or equal to 64
284526fd984Sopenharmony_ci * return 0 OK, other error
285526fd984Sopenharmony_ci */
286526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_hash(const uint32_t alg,
287526fd984Sopenharmony_ci    const struct hks_blob *src_data, struct hks_blob *hash);
288526fd984Sopenharmony_ci
289526fd984Sopenharmony_ci/*
290526fd984Sopenharmony_ci * hks_bn_exp_mod
291526fd984Sopenharmony_ci * x: output, x and x->data cannot be null, x->size >= n.size
292526fd984Sopenharmony_ci * a, e, n: input, data cannot be null, size > 0
293526fd984Sopenharmony_ci * return 0 OK, other error
294526fd984Sopenharmony_ci */
295526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_bn_exp_mod(struct hks_blob *x,
296526fd984Sopenharmony_ci    const struct hks_blob *a, const struct hks_blob *e,
297526fd984Sopenharmony_ci    const struct hks_blob *n);
298526fd984Sopenharmony_ci
299526fd984Sopenharmony_ci/*
300526fd984Sopenharmony_ci * register log Interface
301526fd984Sopenharmony_ci * log: designated by the caller, invoked by hks
302526fd984Sopenharmony_ci * return 0 OK, other error
303526fd984Sopenharmony_ci */
304526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_register_log_interface(
305526fd984Sopenharmony_ci    const struct hks_log_f_group *log);
306526fd984Sopenharmony_ci
307526fd984Sopenharmony_ci/*
308526fd984Sopenharmony_ci * get public key alias list Interface
309526fd984Sopenharmony_ci * key_alias_list: struct hks_blob array, alloc and free memory by the caller
310526fd984Sopenharmony_ci * list_count: public key alias number, alloc and free memory by the caller
311526fd984Sopenharmony_ci * return 0 OK, other error
312526fd984Sopenharmony_ci */
313526fd984Sopenharmony_ciHKS_DLL_API_PUBLIC int32_t hks_get_pub_key_alias_list(
314526fd984Sopenharmony_ci    struct hks_blob *key_alias_list, uint32_t *list_count);
315526fd984Sopenharmony_ci
316526fd984Sopenharmony_ci#ifdef __cplusplus
317526fd984Sopenharmony_ci}
318526fd984Sopenharmony_ci#endif
319526fd984Sopenharmony_ci
320526fd984Sopenharmony_ci#endif /* HKS_CLIENT_H */
321