1e41f4b71Sopenharmony_ci# HUKS Development
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### HUKS
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciOpenHarmony Universal KeyStore (HUKS) provides lifecycle key management capabilities, including generating, storing, and destroying keys, and provides attestation for the keys stored in the HUKS. In the HUKS hierarchical architecture, the HUKS core layer (HUKS Core) at the bottom implements key management functions and runs in a secure hardware environment, such as a Trusted Execution Environment (TEE) or secure chipset. The implementation of the HUKS core layer varies depending on the secure hardware environment of the vendor. To ensure consistency of the architecture and interfaces between the service layer and application layer, the HUKS core layer defines a set of Hardware Device Interface (HDI) APIs. 
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciThis document describes how to develop the HUKS core layer functions using the HUKS HDI APIs.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciThe HUKS core layer is supposed to support the following functions:
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci- Generation of keys.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci- Import of keys.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci- Key operations, including encryption and decryption, signing and signature verification, key derivation, key agreement, and generation of message authentication codes (MACs).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci- Key access control.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci- Key attestation.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci- Export of the public key from a chipset.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci### Basic Concepts
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci- HUKS Core
28e41f4b71Sopenharmony_ci  
29e41f4b71Sopenharmony_ci  HUKS Core is a core component that implements functions, including cryptographic calculation of keys, encryption and decryption, and key access control. Generally, it runs in a secure environment (such as a TEE or secure chipset) of a device to ensure that the keys in plaintext are never exposed outside the HUKS Core.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci- Key session
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci  A key session holds the key information, including the key operation data, key properties, and access control attributes, when a key is used. You need to pass in a key alias to create a session for the key. The HUKS generates a globally unique handle for each session. A general key operation involves creating a session, passing in data and parameters, and finishing the session (or aborting the session).
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci- TEE
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci  A TEE is a secure area created by isolating software and hardware resources to protect the applications and data in the secure area from unauthorized access. This isolation mechanism yields two execution environments: TEE and Rich Execution Environment (REE). Each execution environment has independent internal data path and memory, protecting the data inside the TEE from being disclosed. The applications in an REE cannot access resources in a TEE. The applications in a TEE cannot access resources in another TEE without authorization.
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci## Working Principles
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciThe HUKS is divided into the following layers:
44e41f4b71Sopenharmony_ci- Application layer: provides APIs for applications.
45e41f4b71Sopenharmony_ci- Service layer: processes key management requests from applications and performs key ciphertext management, identity verification, and key session management.
46e41f4b71Sopenharmony_ci- Core layer: implements core functions, such as key generation, key operations, key access control, and key attestation.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**Figure 1** HUKS architecture
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci![huks_architect](./figures/HUKS-architecture.png)
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci## Constraints
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci - Keys in a secure environment in lifecycle
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci   In the lifecycle of a key, the plaintext will never be exposed outside the HUKS Core. For the devices with a TEE or secure chipset, the HUKS Core runs in the TEE or secure chipset. This prevents the key plaintext from being disclosed even if the REE is cracked. That is why the key materials used in all HUKS passthrough HDI APIs are in ciphertext.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci- Encrypted keys for storage
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci  The service keys are encrypted based on the device root key. If supported by the device, certain keys can be further protected by a password.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci- Strict access control over keys
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci  Only authorized services can access keys. For security-critical services, user identity authentication can be enabled for key access.
65e41f4b71Sopenharmony_ci- Key attestation
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci  The HUKS provides attestation for hardware-backed key storage. It proves that the key has not been tampered with, is stored in the hardware-backed HUKS Core, and has correct key properties.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci- Key material format
70e41f4b71Sopenharmony_ci  
71e41f4b71Sopenharmony_ci  When a key (key pair, public key, or private key) is imported or exported, the key material format must meet HUKS requirements. For details, see [Key Material Formats](../../application-dev/security/huks-appendix.md#key-material-formats).
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci- Certificate chain format
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci  The certificate chain returned by **AttestKey()** must be assembled in the sequence of the application certificate, device certificate, CA certificate, and root certificate, with the certificate length added before each certificate. The certificate chain and its length are in the binary large object (BLOB) format. If you want to define the certificate format, the format must be the same as that parsed by the server.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci  ![CertChain format](figures/HUKS-CertChain.png)
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci- KeyBlob
80e41f4b71Sopenharmony_ciThe key returned by the APIs must be assembled into a **KeyBlob** based on the key storage status. For details about the APIs that must comply with this constraint, see [Available APIs](#available-apis).
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci  ![KeyBlob format](figures/HUKS-KeyBlob.png)
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci## How to Develop
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci### When to Use
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ciThe HUKS Core provides KeyStore capabilities for applications, including key management and cryptographic operations. If you want to replace the HUKS Core with your own implementation, you need to implement the following APIs.
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci### Available APIs
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**Table 1** Available APIs
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci| API                                                      | Description                                 | Constraints                    | JS API                                       |
95e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ---------------------------------------- | ----------------------------- | ------------------------------------------------------------ |
96e41f4b71Sopenharmony_ci| [ModuleInit()](#moduleinit)                   | Initializes the HUKS Core.                           |  N/A                          | N/A|
97e41f4b71Sopenharmony_ci| [ModuleDestroy()](#moduledestroy)                   | Destroys the HUKS Core.                           |  N/A                          | N/A|
98e41f4b71Sopenharmony_ci| [GenerateKey()](#generatekey)                  | Generates a key based on the cryptographic algorithm parameters.                               |  The key output must be in the **KeyBlob** format.         |generateKey(keyAlias: string, options: HuksOptions)|
99e41f4b71Sopenharmony_ci| [ImportKey()](#importkey)                     | Imports a key in plaintext.                           |  The key output must be in the **KeyBlob** format.          | importKey(keyAlias: string, options: HuksOptions)|
100e41f4b71Sopenharmony_ci| [ImportWrappedKey()](#importwrappedkey)        |Imports a wrapped (encrypted) key.                             |  The key output must be in the **KeyBlob** format.         | importWrappedKey(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions)|
101e41f4b71Sopenharmony_ci| [ExportPublicKey()](#exportpublickey)         | Exports the public key of a key pair.                                |N/A                            | exportKey(keyAlias: string, options: HuksOptions) |
102e41f4b71Sopenharmony_ci| [Init()](#init)                              | Initializes a key session. This API returns a key session handle and an authentication token (optional).                      |N/A                             | init(keyAlias: string, options: HuksOptions) |
103e41f4b71Sopenharmony_ci| [Update()](#update)                           | Updates key operation data.                    |The input parameters for signature verification must be the raw data.         | update(handle: number, token?: Uint8Array, options: HuksOptions) |
104e41f4b71Sopenharmony_ci| [Finish()](#finish)                           | Finishes a key session.                    |The input parameter for signature verification must be the signed data.       | finish(handle: number, options: HuksOptions) |
105e41f4b71Sopenharmony_ci| [Abort()](#abort)                         | Aborts a key session.                              |N/A                            | abort(handle: number, options: HuksOptions) |
106e41f4b71Sopenharmony_ci| [CheckKeyValidity()](#checkkeyvalidity)        | Checks the key material (ciphertext) validity.                             |N/A                           | N/A|
107e41f4b71Sopenharmony_ci| [AttestKey()](#attestkey)        | Attests a key.                             |The output parameter must be in the certificate chain format.                     | attestKey(keyAlias: string, options: HuksOptions)|
108e41f4b71Sopenharmony_ci| [ExportChipsetPlatformPublicKey()](#exportchipsetplatformpublickey)        | Exports the public key of a chipset key pair.    | The output parameters are the raw data of ECC P-256 x-axis and y-axis values, each of which are of 32 bytes.                     | N/A|
109e41f4b71Sopenharmony_ci| [UpgradeKey()](#upgradekey)        | Updates the key file.    | N/A                     | N/A|
110e41f4b71Sopenharmony_ci| [GenerateRandom()](#generaterandom)        | Generates a random number.    | N/A                     | N/A|
111e41f4b71Sopenharmony_ci| [Encrypt()](#encrypt)        | Encrypts data.    | N/A                     | N/A|
112e41f4b71Sopenharmony_ci| [Decrypt()](#decrypt)        | Decrypts data.    | N/A                     | N/A|
113e41f4b71Sopenharmony_ci| [Sign()](#sign)        | Signs data.    | N/A                     | N/A|
114e41f4b71Sopenharmony_ci| [Verify()](#verify)        | Verifies a signature.    | N/A                     | N/A|
115e41f4b71Sopenharmony_ci| [AgreeKey()](#agreekey)        | Performs key agreement.    | N/A                     | N/A|
116e41f4b71Sopenharmony_ci| [DeriveKey()](#derivekey)        | Derives a key.    | N/A                     | N/A|
117e41f4b71Sopenharmony_ci| [Mac()](#mac)        | Generates a MAC.    | N/A                     | N/A|
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci- - -
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci#### ModuleInit
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**API Description**
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ciInitializes the HUKS Core. You can use this API to initialize global variables, such as the global thread locks, algorithm library, and the AuthToken key and root key used for access control.
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**Prototype**
128e41f4b71Sopenharmony_ci<pre><code>int32_t ModuleInit(struct IHuks *self);</code></pre>
129e41f4b71Sopenharmony_ci<details>
130e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
131e41f4b71Sopenharmony_ci  <pre>
132e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
133e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
134e41f4b71Sopenharmony_ci  </pre>
135e41f4b71Sopenharmony_ci</details>
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci<details>
138e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci  - **HKS_SUCCESS** (the value is **0**): The operation is successful.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci  - Other values (negative number): The operation fails. For details, see <a href="https://gitee.com/openharmony/security_huks/blob/master/interfaces/inner_api/huks_standard/main/include/hks_type.h">HksErrorCode</a>.
143e41f4b71Sopenharmony_ci</details>
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci- - -
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci#### ModuleDestroy
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**API Description**
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciDestroys the HUKS Core. You can use this API to release global variables including the locks and destroy the AuthToken key and root key in the memory.
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci**Prototype**
155e41f4b71Sopenharmony_ci<pre><code>int32_t ModuleDestroy(struct IHuks *self);</code></pre>
156e41f4b71Sopenharmony_ci<details>
157e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
158e41f4b71Sopenharmony_ci  <pre>
159e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
160e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
161e41f4b71Sopenharmony_ci  </pre>
162e41f4b71Sopenharmony_ci</details>
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci<details>
165e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci  - Other values: The operation fails.
170e41f4b71Sopenharmony_ci</details>
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci- - -
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci#### GenerateKey
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci**API Description**
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ciGenerate a key based on the **paramSet**.
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**Prototype**
181e41f4b71Sopenharmony_ci<pre><code>int32_t GenerateKey(struct IHuks *self, const struct HuksBlob *keyAlias, const struct HuksParamSet *paramSet,
182e41f4b71Sopenharmony_ci    const struct HuksBlob *keyIn, struct HuksBlob *encKeyOut);</code></pre>
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci<details>
185e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
186e41f4b71Sopenharmony_ci  <pre>
187e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
188e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
189e41f4b71Sopenharmony_ci  <br></br>
190e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *keyAlias</strong>
191e41f4b71Sopenharmony_ci  Pointer to the alias of the key to generate. The value must meet the following requirements:
192e41f4b71Sopenharmony_ci  1. keyAlias != null
193e41f4b71Sopenharmony_ci  2. keyAlias -> data != null
194e41f4b71Sopenharmony_ci  3. keyAlias -> dataLen != 0
195e41f4b71Sopenharmony_ci  <br></br>
196e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
197e41f4b71Sopenharmony_ci  Pointer to the parameters for generating the key.
198e41f4b71Sopenharmony_ci  <br></br>
199e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *keyIn</strong>
200e41f4b71Sopenharmony_ci  Pointer to the original key material to be passed in if the key is generated through key agreement or key derivation. This parameter is optional.
201e41f4b71Sopenharmony_ci  <br></br>
202e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *encKeyOut</strong>
203e41f4b71Sopenharmony_ci  Pointer to the key generated in ciphertext. It holds the **paramSet** and the key ciphertext in the KeyBlob format.
204e41f4b71Sopenharmony_ci  </pre>
205e41f4b71Sopenharmony_ci</details>
206e41f4b71Sopenharmony_ci<br></br>
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci<details>
209e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci  1. Check parameters in the API. For example, check for null pointers and whether the key algorithm is supported.
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci  2. **keyOut** must be in the **KeyBlob** format.
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci</details>
216e41f4b71Sopenharmony_ci<br></br>
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci<details>
219e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci  - Other values: The operation fails.
224e41f4b71Sopenharmony_ci</details>
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci- - -
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci#### ImportKey
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci**API Description**
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ciImports a key in plaintext.
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci**Prototype**
235e41f4b71Sopenharmony_ci<pre><code>int32_t ImportKey(struct IHuks *self, const struct HuksBlob *keyAlias, const struct HuksBlob *key,
236e41f4b71Sopenharmony_ci    const struct HuksParamSet *paramSet, struct HuksBlob *encKeyOut);</code></pre>
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci<details>
239e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
240e41f4b71Sopenharmony_ci  <pre>
241e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
242e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
243e41f4b71Sopenharmony_ci  <br></br>
244e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *keyAlias</strong>
245e41f4b71Sopenharmony_ci  Pointer to the alias of the key to import. The alias must meet the following requirements:
246e41f4b71Sopenharmony_ci  1. keyAlias != null
247e41f4b71Sopenharmony_ci  2. keyAlias -> data != null
248e41f4b71Sopenharmony_ci  3. keyAlias -> dataLen != 0
249e41f4b71Sopenharmony_ci  <br></br>
250e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *key</strong>
251e41f4b71Sopenharmony_ci  Pointer to the plaintext key material to import. For details about the key material format, see <a href="../application-dev/security/huks-appendix.md#key-material-formats">Key Material Formats</a>. The value must meet the following requirements:
252e41f4b71Sopenharmony_ci  1. key != null
253e41f4b71Sopenharmony_ci  2. key -> data != null
254e41f4b71Sopenharmony_ci  3. key -> dataLen != 0
255e41f4b71Sopenharmony_ci  <br></br>
256e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
257e41f4b71Sopenharmony_ci  Pointer to the parameters of the key to import.
258e41f4b71Sopenharmony_ci  <br></br>
259e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *encKeyOut</strong>
260e41f4b71Sopenharmony_ci  Pointer to the imported key in ciphertext. It holds the **paramSet** and the imported key ciphertext in the KeyBlob format.
261e41f4b71Sopenharmony_ci  </pre>
262e41f4b71Sopenharmony_ci</details>
263e41f4b71Sopenharmony_ci<br></br>
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci<details>
266e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci  1. Check parameters in the API. For example, check for null pointers and whether the key algorithm is supported.
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci  2. Check that **encKeyOut** is in the KeyBlob format.
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci</details>
273e41f4b71Sopenharmony_ci<br></br>
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci<details>
276e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci  - Other values: The operation fails.
281e41f4b71Sopenharmony_ci</details>
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci- - -
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci#### ImportWrappedKey
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci**API Description**
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ciImports a wrapped key.
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci**Prototype**
292e41f4b71Sopenharmony_ci<pre><code>int32_t ImportWrappedKey(struct IHuks *self, const struct HuksBlob *wrappingKeyAlias,
293e41f4b71Sopenharmony_ci    const struct HuksBlob *wrappingEncKey, const struct HuksBlob *wrappedKeyData, const struct HuksParamSet *paramSet,
294e41f4b71Sopenharmony_ci    struct HuksBlob *encKeyOut);</code></pre>
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ci<details>
297e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
298e41f4b71Sopenharmony_ci  <pre>
299e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
300e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
301e41f4b71Sopenharmony_ci  <br></br>
302e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *wrappingKeyAlias</strong>
303e41f4b71Sopenharmony_ci  Pointer to the alias of the key used to encrypt the key to import (it is not the alias of the key to import). The value must meet the following requirements:
304e41f4b71Sopenharmony_ci  1. wrappingKeyAlias != null
305e41f4b71Sopenharmony_ci  2. wrappingKeyAlias -> data != null
306e41f4b71Sopenharmony_ci  3. wrappingKeyAlias -> dataLen != 0
307e41f4b71Sopenharmony_ci  <br></br>
308e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *wrappingEncKey</strong>
309e41f4b71Sopenharmony_ci  Pointer to the key used to encrypt the key to import. The value must meet the following requirements:
310e41f4b71Sopenharmony_ci  1. wrappingEncKey != null
311e41f4b71Sopenharmony_ci  2. wrappingEncKey -> data != null
312e41f4b71Sopenharmony_ci  3. wrappingEncKey -> dataLen != 0
313e41f4b71Sopenharmony_ci  <br></br>
314e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *wrappedKeyData</strong>
315e41f4b71Sopenharmony_ci  Pointer to the key material of the key to import. For details abut the key material format, see <a href="../../application-dev/security/huks-guidelines.md#importing-a-key-securely">Importing a Key Securely</a>. The value must meet the following requirements:
316e41f4b71Sopenharmony_ci  1. wrappedKeyData != null
317e41f4b71Sopenharmony_ci  2. wrappedKeyData -> data != null
318e41f4b71Sopenharmony_ci  3. wrappedKeyData -> dataLen != 0
319e41f4b71Sopenharmony_ci  <br></br>
320e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
321e41f4b71Sopenharmony_ci  Pointer to the properties of the key to import.
322e41f4b71Sopenharmony_ci  <br></br>
323e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *encKeyOut</strong>
324e41f4b71Sopenharmony_ci  Pointer to the imported key material (ciphertext) in the KeyBlob format.
325e41f4b71Sopenharmony_ci  </pre>
326e41f4b71Sopenharmony_ci</details>
327e41f4b71Sopenharmony_ci<br></br>
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci<details>
330e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci  1. Check parameters in the API. For example, check for null pointers and whether the key algorithm is supported.
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci  2. Check that **encKeyOut** is in the KeyBlob format.
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci</details>
337e41f4b71Sopenharmony_ci<br></br>
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_ci<details>
340e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci  - Other values: The operation fails.
345e41f4b71Sopenharmony_ci</details>
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ci- - -
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci#### ExportPublicKey
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci**API Description**
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_ciExports the public key of a key pair.
354e41f4b71Sopenharmony_ci
355e41f4b71Sopenharmony_ci**Prototype**
356e41f4b71Sopenharmony_ci<pre><code>int32_t ExportPublicKey(struct IHuks *self, const struct HuksBlob *encKey,
357e41f4b71Sopenharmony_ci    const struct HuksParamSet *paramSet, struct HuksBlob *keyOut);</code></pre>
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci<details>
360e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
361e41f4b71Sopenharmony_ci  <pre>
362e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
363e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
364e41f4b71Sopenharmony_ci  <br></br>
365e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
366e41f4b71Sopenharmony_ci  Pointer to the key pair material. The value must meet the following requirements:
367e41f4b71Sopenharmony_ci  1. encKey != null
368e41f4b71Sopenharmony_ci  2. encKey -> data != null
369e41f4b71Sopenharmony_ci  3. encKey -> dataLen != 0
370e41f4b71Sopenharmony_ci  <br></br>
371e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
372e41f4b71Sopenharmony_ci  Pointer to the parameters for exporting the public key. By default, this parameter is left blank.
373e41f4b71Sopenharmony_ci  <br></br>
374e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *keyOut</strong>
375e41f4b71Sopenharmony_ci  Pointer to the public key exported.
376e41f4b71Sopenharmony_ci  </pre>
377e41f4b71Sopenharmony_ci</details>
378e41f4b71Sopenharmony_ci<br></br>
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci<details>
381e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci  - Other values: The operation fails.
386e41f4b71Sopenharmony_ci</details>
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci- - -
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci#### Init
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci**API Description**
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ciInitializes a key session. You need to pass in the key material in ciphertext. The HUKS Core decrypts the ciphertext and generates a key session handle and an authentication token (if required).
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci**Prototype**
397e41f4b71Sopenharmony_ci<pre><code>int32_t Init(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
398e41f4b71Sopenharmony_ci    struct HuksBlob *handle, struct HuksBlob *token);</code></pre>
399e41f4b71Sopenharmony_ci
400e41f4b71Sopenharmony_ci<details>
401e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
402e41f4b71Sopenharmony_ci  <pre>
403e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
404e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
405e41f4b71Sopenharmony_ci  <br></br>
406e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
407e41f4b71Sopenharmony_ci  Pointer to the ciphertext material of the key to be operated. The value must meet the following requirements:
408e41f4b71Sopenharmony_ci  1. encKey != null
409e41f4b71Sopenharmony_ci  2. encKey -> data != null
410e41f4b71Sopenharmony_ci  3. encKey -> dataLen != 0
411e41f4b71Sopenharmony_ci  <br></br>
412e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
413e41f4b71Sopenharmony_ci  Pointer to the parameters for initializing the key session.
414e41f4b71Sopenharmony_ci  <br></br>
415e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *handle</strong>
416e41f4b71Sopenharmony_ci  Pointer to the key session handle generated, which identifies the key session in Update(), Finish(), and Abort().
417e41f4b71Sopenharmony_ci  <br></br>
418e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *token</strong>
419e41f4b71Sopenharmony_ci  Pointer to the authentication token generated for key access control (if required).
420e41f4b71Sopenharmony_ci  </pre>
421e41f4b71Sopenharmony_ci</details>
422e41f4b71Sopenharmony_ci<br></br>
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci<details>
425e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
426e41f4b71Sopenharmony_ci
427e41f4b71Sopenharmony_ciThis API must be used with **Update()**, **Finish()**, and **Abort()** together.
428e41f4b71Sopenharmony_ci
429e41f4b71Sopenharmony_ci</details>
430e41f4b71Sopenharmony_ci<br></br>
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_ci<details>
433e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
436e41f4b71Sopenharmony_ci
437e41f4b71Sopenharmony_ci  - Other values: The operation fails.
438e41f4b71Sopenharmony_ci</details>
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_ci- - -
441e41f4b71Sopenharmony_ci
442e41f4b71Sopenharmony_ci#### Update
443e41f4b71Sopenharmony_ci
444e41f4b71Sopenharmony_ci**API Description**
445e41f4b71Sopenharmony_ci
446e41f4b71Sopenharmony_ciUpdates data for the key operation by segment according to the cryptographic algorithm requirements.
447e41f4b71Sopenharmony_ci
448e41f4b71Sopenharmony_ci**Prototype**
449e41f4b71Sopenharmony_ci<pre><code>int32_t Update(struct IHuks *self, const struct HuksBlob *handle, const struct HuksParamSet *paramSet,
450e41f4b71Sopenharmony_ci    const struct HuksBlob *inData, struct HuksBlob *outData);</code></pre>
451e41f4b71Sopenharmony_ci
452e41f4b71Sopenharmony_ci<details>
453e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
454e41f4b71Sopenharmony_ci  <pre>
455e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
456e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
457e41f4b71Sopenharmony_ci  <br></br>
458e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *handle</strong>
459e41f4b71Sopenharmony_ci  Pointer to the handle of the key session.
460e41f4b71Sopenharmony_ci  <br></br>
461e41f4b71Sopenharmony_ci  <strong> const struct HuksParamSet *paramSet</strong>
462e41f4b71Sopenharmony_ci  Pointer to the parameters for the update operation.
463e41f4b71Sopenharmony_ci  <br></br>
464e41f4b71Sopenharmony_ci  <strong> const struct HuksBlob *inData</strong>
465e41f4b71Sopenharmony_ci  Pointer to the data to be passed in.
466e41f4b71Sopenharmony_ci  <br></br>
467e41f4b71Sopenharmony_ci  <strong> struct HuksBlob *outData</strong>
468e41f4b71Sopenharmony_ci  Pointer to the result of the update operation.
469e41f4b71Sopenharmony_ci  </pre>
470e41f4b71Sopenharmony_ci</details>
471e41f4b71Sopenharmony_ci<br></br>
472e41f4b71Sopenharmony_ci
473e41f4b71Sopenharmony_ci<details>
474e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ci  1. This API must be used with **Init()**, **Finish()**, and **Abort()** together.
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ci  2. **inData** must pass in the raw data when signature verification is performed.
479e41f4b71Sopenharmony_ci
480e41f4b71Sopenharmony_ci</details>
481e41f4b71Sopenharmony_ci<br></br>
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci<details>
484e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
485e41f4b71Sopenharmony_ci
486e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
487e41f4b71Sopenharmony_ci
488e41f4b71Sopenharmony_ci  - Other values: The operation fails.
489e41f4b71Sopenharmony_ci</details>
490e41f4b71Sopenharmony_ci
491e41f4b71Sopenharmony_ci- - -
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ci#### Finish
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_ci**API Description**
496e41f4b71Sopenharmony_ci
497e41f4b71Sopenharmony_ciFinishes the key session.
498e41f4b71Sopenharmony_ci
499e41f4b71Sopenharmony_ci**Prototype**
500e41f4b71Sopenharmony_ci<pre><code>int32_t Finish(struct IHuks *self, const struct HuksBlob *handle, const struct HuksParamSet *paramSet,
501e41f4b71Sopenharmony_ci    const struct HuksBlob *inData, struct HuksBlob *outData);</code></pre>
502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ci<details>
504e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
505e41f4b71Sopenharmony_ci  <pre>
506e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
507e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
508e41f4b71Sopenharmony_ci  <br></br>
509e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *handle</strong>
510e41f4b71Sopenharmony_ci  Pointer to the handle of the key session.
511e41f4b71Sopenharmony_ci  <br></br>
512e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
513e41f4b71Sopenharmony_ci  Pointer to the parameters for the last operation.
514e41f4b71Sopenharmony_ci  <br></br>
515e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *inData</strong>
516e41f4b71Sopenharmony_ci  Pointer to the last data to be passed in.
517e41f4b71Sopenharmony_ci  <br></br>
518e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *outData</strong>
519e41f4b71Sopenharmony_ci  Pointer to the result of the key operation.
520e41f4b71Sopenharmony_ci  </pre>
521e41f4b71Sopenharmony_ci</details>
522e41f4b71Sopenharmony_ci<br></br>
523e41f4b71Sopenharmony_ci
524e41f4b71Sopenharmony_ci<details>
525e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
526e41f4b71Sopenharmony_ci
527e41f4b71Sopenharmony_ci  1. This API must be used with **Init()**, **Update()**, and **Abort()** together.
528e41f4b71Sopenharmony_ci
529e41f4b71Sopenharmony_ci  2. In signature verification, **inData** must pass in the signature data to be verified. The return value indicates whether the signature has passed the verification.
530e41f4b71Sopenharmony_ci
531e41f4b71Sopenharmony_ci</details>
532e41f4b71Sopenharmony_ci<br></br>
533e41f4b71Sopenharmony_ci
534e41f4b71Sopenharmony_ci<details>
535e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
536e41f4b71Sopenharmony_ci
537e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_ci  - Other values: The operation fails.
540e41f4b71Sopenharmony_ci</details>
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ci- - -
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci#### Abort
545e41f4b71Sopenharmony_ci
546e41f4b71Sopenharmony_ci**API Description**
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_ciAborts the key session. When an error occurs in any of the **Init**, **Update**, and **Finish** operations, call this API to terminate the key session.
549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_ci**Prototype**
551e41f4b71Sopenharmony_ci<pre><code>int32_t Abort(struct IHuks *self, const struct HuksBlob *handle, const struct HuksParamSet *paramSet);</code></pre>
552e41f4b71Sopenharmony_ci<details>
553e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
554e41f4b71Sopenharmony_ci  <pre>
555e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
556e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
557e41f4b71Sopenharmony_ci  <br></br>
558e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *handle</strong>
559e41f4b71Sopenharmony_ci  Pointer to the handle of the key session.
560e41f4b71Sopenharmony_ci  <br></br>
561e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
562e41f4b71Sopenharmony_ci  Pointer to the parameters of the **Abort** operation.
563e41f4b71Sopenharmony_ci  </pre>
564e41f4b71Sopenharmony_ci</details>
565e41f4b71Sopenharmony_ci<br></br>
566e41f4b71Sopenharmony_ci
567e41f4b71Sopenharmony_ci<details>
568e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
569e41f4b71Sopenharmony_ci
570e41f4b71Sopenharmony_ciThis API must be used with **Init()**, **Update()**, and **Finish()** together.
571e41f4b71Sopenharmony_ci
572e41f4b71Sopenharmony_ci</details>
573e41f4b71Sopenharmony_ci<br></br>
574e41f4b71Sopenharmony_ci
575e41f4b71Sopenharmony_ci<details>
576e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
579e41f4b71Sopenharmony_ci
580e41f4b71Sopenharmony_ci  - Other values: The operation fails.
581e41f4b71Sopenharmony_ci</details>
582e41f4b71Sopenharmony_ci
583e41f4b71Sopenharmony_ci- - -
584e41f4b71Sopenharmony_ci
585e41f4b71Sopenharmony_ci#### CheckKeyValidity
586e41f4b71Sopenharmony_ci
587e41f4b71Sopenharmony_ci**API Description**
588e41f4b71Sopenharmony_ci
589e41f4b71Sopenharmony_ciChecks key validity.
590e41f4b71Sopenharmony_ci
591e41f4b71Sopenharmony_ci**Prototype**
592e41f4b71Sopenharmony_ci<pre><code>int32_t CheckKeyValidity(struct IHuks *self, const struct HuksParamSet *paramSet,
593e41f4b71Sopenharmony_ci    const struct HuksBlob *encKey);</code></pre>
594e41f4b71Sopenharmony_ci
595e41f4b71Sopenharmony_ci<details>
596e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
597e41f4b71Sopenharmony_ci  <pre>
598e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
599e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
600e41f4b71Sopenharmony_ci  <br></br>
601e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
602e41f4b71Sopenharmony_ci  Pointer to the parameters for checking the key integrity. By default, this parameter is left empty.
603e41f4b71Sopenharmony_ci  <br></br>
604e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
605e41f4b71Sopenharmony_ci  Pointer to the key material (ciphertext) to be checked.
606e41f4b71Sopenharmony_ci  </pre>
607e41f4b71Sopenharmony_ci</details>
608e41f4b71Sopenharmony_ci<br></br>
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_ci<details>
611e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
612e41f4b71Sopenharmony_ci
613e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
614e41f4b71Sopenharmony_ci
615e41f4b71Sopenharmony_ci  - Other values: The operation fails.
616e41f4b71Sopenharmony_ci</details>
617e41f4b71Sopenharmony_ci
618e41f4b71Sopenharmony_ci- - -
619e41f4b71Sopenharmony_ci
620e41f4b71Sopenharmony_ci#### AttestKey
621e41f4b71Sopenharmony_ci
622e41f4b71Sopenharmony_ci**API Description**
623e41f4b71Sopenharmony_ci
624e41f4b71Sopenharmony_ciAttests a key.
625e41f4b71Sopenharmony_ci
626e41f4b71Sopenharmony_ci**Prototype**
627e41f4b71Sopenharmony_ci<pre><code>int32_t AttestKey(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
628e41f4b71Sopenharmony_ci    struct HuksBlob *certChain);</code></pre>
629e41f4b71Sopenharmony_ci
630e41f4b71Sopenharmony_ci<details>
631e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
632e41f4b71Sopenharmony_ci  <pre>
633e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
634e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
635e41f4b71Sopenharmony_ci  <br></br>
636e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
637e41f4b71Sopenharmony_ci  Pointer to the key pair material in ciphertext.
638e41f4b71Sopenharmony_ci  <br></br>
639e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
640e41f4b71Sopenharmony_ci  Pointer to the parameters (such as the challenge) for obtaining the key certificate chain.
641e41f4b71Sopenharmony_ci  <br></br>
642e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *certChain</strong>
643e41f4b71Sopenharmony_ci  Pointer to the certificate chain obtained.
644e41f4b71Sopenharmony_ci  </pre>
645e41f4b71Sopenharmony_ci</details>
646e41f4b71Sopenharmony_ci<br></br>
647e41f4b71Sopenharmony_ci
648e41f4b71Sopenharmony_ci<details>
649e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
650e41f4b71Sopenharmony_ci
651e41f4b71Sopenharmony_ci**certChain** must comply with the certificate chain described in [Constraints](#constraints).
652e41f4b71Sopenharmony_ci
653e41f4b71Sopenharmony_ci</details>
654e41f4b71Sopenharmony_ci<br></br>
655e41f4b71Sopenharmony_ci
656e41f4b71Sopenharmony_ci<details>
657e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
658e41f4b71Sopenharmony_ci
659e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
660e41f4b71Sopenharmony_ci
661e41f4b71Sopenharmony_ci  - Other values: The operation fails.
662e41f4b71Sopenharmony_ci</details>
663e41f4b71Sopenharmony_ci
664e41f4b71Sopenharmony_ci- - -
665e41f4b71Sopenharmony_ci
666e41f4b71Sopenharmony_ci#### ExportChipsetPlatformPublicKey
667e41f4b71Sopenharmony_ci
668e41f4b71Sopenharmony_ci**API Description**
669e41f4b71Sopenharmony_ci
670e41f4b71Sopenharmony_ciExports the public key of a chipset key pair.
671e41f4b71Sopenharmony_ci
672e41f4b71Sopenharmony_ci**Prototype**
673e41f4b71Sopenharmony_ci<pre><code>int32_t ExportChipsetPlatformPublicKey(struct IHuks *self, const struct HuksBlob *salt,
674e41f4b71Sopenharmony_ci    enum HuksChipsetPlatformDecryptScene scene, struct HuksBlob *publicKey);</code></pre>
675e41f4b71Sopenharmony_ci
676e41f4b71Sopenharmony_ci<details>
677e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
678e41f4b71Sopenharmony_ci  <pre>
679e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
680e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
681e41f4b71Sopenharmony_ci  <br></br>
682e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *salt</strong>
683e41f4b71Sopenharmony_ci  Pointer to the factor used to derive the chipset key pair.
684e41f4b71Sopenharmony_ci  <br></br>
685e41f4b71Sopenharmony_ci  <strong>enum HuksChipsetPlatformDecryptScene scene</strong>
686e41f4b71Sopenharmony_ci  Expected chipset decryption scenario.
687e41f4b71Sopenharmony_ci  <br></br>
688e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *publicKey</strong>
689e41f4b71Sopenharmony_ci  Pointer to the raw data of ECC P-256 x-axis and y-axis values, each of which are of 32 bytes.
690e41f4b71Sopenharmony_ci  </pre>
691e41f4b71Sopenharmony_ci</details>
692e41f4b71Sopenharmony_ci<br></br>
693e41f4b71Sopenharmony_ci
694e41f4b71Sopenharmony_ci<details>
695e41f4b71Sopenharmony_ci  <summary><strong>Constraints</strong></summary>
696e41f4b71Sopenharmony_ci
697e41f4b71Sopenharmony_ciThe input parameter **salt** must be of 16 bytes, and the content of the last byte will be ignored and filled by HUKS based on **scene**.
698e41f4b71Sopenharmony_ciCurrently, the chipset key pairs of HUKS are implemented by software. An ECC P-256 key pair is hard-coded, and the **salt** value is ignored. That is, the derived keys are the same regardless of the **salt**. In the hardware-based implementation of chipset key pairs, **salt** is a factor used to derive the key. That is, the key pair derived varies with the **salt** value.
699e41f4b71Sopenharmony_ci
700e41f4b71Sopenharmony_ci</details>
701e41f4b71Sopenharmony_ci<br></br>
702e41f4b71Sopenharmony_ci
703e41f4b71Sopenharmony_ci<details>
704e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
705e41f4b71Sopenharmony_ci
706e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
707e41f4b71Sopenharmony_ci
708e41f4b71Sopenharmony_ci  - Other values: The operation fails.
709e41f4b71Sopenharmony_ci</details>
710e41f4b71Sopenharmony_ci
711e41f4b71Sopenharmony_ci- - -
712e41f4b71Sopenharmony_ci
713e41f4b71Sopenharmony_ci#### UpgradeKey
714e41f4b71Sopenharmony_ci
715e41f4b71Sopenharmony_ci**API Description**
716e41f4b71Sopenharmony_ci
717e41f4b71Sopenharmony_ciUpdates the key file when the key file version is earlier than the latest version.
718e41f4b71Sopenharmony_ci
719e41f4b71Sopenharmony_ci**Prototype**
720e41f4b71Sopenharmony_ci<pre><code>int32_t UpgradeKey(struct IHuks *self, const struct HuksBlob *encOldKey, const struct HuksParamSet *paramSet,
721e41f4b71Sopenharmony_ci    struct HuksBlob *encNewKey);</code></pre>
722e41f4b71Sopenharmony_ci
723e41f4b71Sopenharmony_ci<details>
724e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
725e41f4b71Sopenharmony_ci  <pre>
726e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
727e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
728e41f4b71Sopenharmony_ci  <br></br>
729e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encOldKey</strong>
730e41f4b71Sopenharmony_ci  Pointer to the key file data to update.
731e41f4b71Sopenharmony_ci  <br></br>
732e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
733e41f4b71Sopenharmony_ci  Pointer to the parameters for updating the key file data.
734e41f4b71Sopenharmony_ci  <br></br>
735e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *newKey</strong>
736e41f4b71Sopenharmony_ci  Pointer to the new key file data.
737e41f4b71Sopenharmony_ci  </pre>
738e41f4b71Sopenharmony_ci</details>
739e41f4b71Sopenharmony_ci<br></br>
740e41f4b71Sopenharmony_ci
741e41f4b71Sopenharmony_ci<details>
742e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
743e41f4b71Sopenharmony_ci
744e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
745e41f4b71Sopenharmony_ci
746e41f4b71Sopenharmony_ci  - Other values: The operation fails.
747e41f4b71Sopenharmony_ci</details>
748e41f4b71Sopenharmony_ci
749e41f4b71Sopenharmony_ci- - -
750e41f4b71Sopenharmony_ci
751e41f4b71Sopenharmony_ci#### GenerateRandom
752e41f4b71Sopenharmony_ci
753e41f4b71Sopenharmony_ci**API Description**
754e41f4b71Sopenharmony_ci
755e41f4b71Sopenharmony_ciGenerates a random number.
756e41f4b71Sopenharmony_ci
757e41f4b71Sopenharmony_ci**Prototype**
758e41f4b71Sopenharmony_ci<pre><code>int32_t GenerateRandom(struct IHuks *self, const struct HuksParamSet *paramSet, struct HuksBlob *random);</code></pre>
759e41f4b71Sopenharmony_ci<details>
760e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
761e41f4b71Sopenharmony_ci  <pre>
762e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
763e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
764e41f4b71Sopenharmony_ci  <br></br>
765e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
766e41f4b71Sopenharmony_ci  Pointer to the parameters of the random number to generate, such as the length.
767e41f4b71Sopenharmony_ci  <br></br>
768e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *random</strong>
769e41f4b71Sopenharmony_ci  Pointer to the random number generated.
770e41f4b71Sopenharmony_ci  </pre>
771e41f4b71Sopenharmony_ci</details>
772e41f4b71Sopenharmony_ci<br></br>
773e41f4b71Sopenharmony_ci
774e41f4b71Sopenharmony_ci<details>
775e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
778e41f4b71Sopenharmony_ci
779e41f4b71Sopenharmony_ci  - Other values: The operation fails.
780e41f4b71Sopenharmony_ci</details>
781e41f4b71Sopenharmony_ci
782e41f4b71Sopenharmony_ci- - -
783e41f4b71Sopenharmony_ci
784e41f4b71Sopenharmony_ci#### Sign
785e41f4b71Sopenharmony_ci
786e41f4b71Sopenharmony_ci**API Description**
787e41f4b71Sopenharmony_ci
788e41f4b71Sopenharmony_ciSigns data.
789e41f4b71Sopenharmony_ci
790e41f4b71Sopenharmony_ci**Prototype**
791e41f4b71Sopenharmony_ci<pre><code>int32_t Sign(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
792e41f4b71Sopenharmony_ci    const struct HuksBlob *srcData, struct HuksBlob *signature);</code></pre>
793e41f4b71Sopenharmony_ci
794e41f4b71Sopenharmony_ci<details>
795e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
796e41f4b71Sopenharmony_ci  <pre>
797e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
798e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
799e41f4b71Sopenharmony_ci  <br></br>
800e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
801e41f4b71Sopenharmony_ci  Pointer to the key pair material (ciphertext) used for signing.
802e41f4b71Sopenharmony_ci  <br></br>
803e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
804e41f4b71Sopenharmony_ci  Pointer to the parameters used for signing, such as the digest mode.
805e41f4b71Sopenharmony_ci  <br></br>
806e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *srcData</strong>
807e41f4b71Sopenharmony_ci  Pointer to the data to be signed.
808e41f4b71Sopenharmony_ci  <br></br>
809e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *signature</strong>
810e41f4b71Sopenharmony_ci  Pointer to the signature generated.
811e41f4b71Sopenharmony_ci  </pre>
812e41f4b71Sopenharmony_ci</details>
813e41f4b71Sopenharmony_ci<br></br>
814e41f4b71Sopenharmony_ci
815e41f4b71Sopenharmony_ci<details>
816e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
817e41f4b71Sopenharmony_ci
818e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
819e41f4b71Sopenharmony_ci
820e41f4b71Sopenharmony_ci  - Other values: The operation fails.
821e41f4b71Sopenharmony_ci</details>
822e41f4b71Sopenharmony_ci
823e41f4b71Sopenharmony_ci- - -
824e41f4b71Sopenharmony_ci
825e41f4b71Sopenharmony_ci#### Verify
826e41f4b71Sopenharmony_ci
827e41f4b71Sopenharmony_ci**API Description**
828e41f4b71Sopenharmony_ci
829e41f4b71Sopenharmony_ciVerifies a digital signature.
830e41f4b71Sopenharmony_ci
831e41f4b71Sopenharmony_ci**Prototype**
832e41f4b71Sopenharmony_ci<pre><code>int32_t Verify(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
833e41f4b71Sopenharmony_ci    const struct HuksBlob *srcData, const struct HuksBlob *signature);</code></pre>
834e41f4b71Sopenharmony_ci
835e41f4b71Sopenharmony_ci<details>
836e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
837e41f4b71Sopenharmony_ci  <pre>
838e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
839e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
840e41f4b71Sopenharmony_ci  <br></br>
841e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
842e41f4b71Sopenharmony_ci  Pointer to the key pair material (ciphertext) used for signature verification.
843e41f4b71Sopenharmony_ci  <br></br>
844e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
845e41f4b71Sopenharmony_ci  Pointer to the parameters for signature verification, such as the digest mode.
846e41f4b71Sopenharmony_ci  <br></br>
847e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *srcData</strong>
848e41f4b71Sopenharmony_ci  Pointer to the data with the signature to be verified.
849e41f4b71Sopenharmony_ci  <br></br>
850e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *signature</strong>
851e41f4b71Sopenharmony_ci  Pointer to the signature to verify.
852e41f4b71Sopenharmony_ci  </pre>
853e41f4b71Sopenharmony_ci</details>
854e41f4b71Sopenharmony_ci<br></br>
855e41f4b71Sopenharmony_ci
856e41f4b71Sopenharmony_ci<details>
857e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
858e41f4b71Sopenharmony_ci
859e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ci  - Other values: The operation fails.
862e41f4b71Sopenharmony_ci</details>
863e41f4b71Sopenharmony_ci
864e41f4b71Sopenharmony_ci- - -
865e41f4b71Sopenharmony_ci
866e41f4b71Sopenharmony_ci#### Encrypt
867e41f4b71Sopenharmony_ci
868e41f4b71Sopenharmony_ci**API Description**
869e41f4b71Sopenharmony_ci
870e41f4b71Sopenharmony_ciEncrypts data. Different from the key session APIs that must be used together, this API completes data encryption in a single call.
871e41f4b71Sopenharmony_ci
872e41f4b71Sopenharmony_ci**Prototype**
873e41f4b71Sopenharmony_ci<pre><code>int32_t Encrypt(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
874e41f4b71Sopenharmony_ci     const struct HuksBlob *plainText, struct HuksBlob *cipherText);</code></pre>
875e41f4b71Sopenharmony_ci
876e41f4b71Sopenharmony_ci<details>
877e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
878e41f4b71Sopenharmony_ci  <pre>
879e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
880e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
881e41f4b71Sopenharmony_ci  <br></br>
882e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
883e41f4b71Sopenharmony_ci  Pointer to the key material (ciphertext) used for encryption.
884e41f4b71Sopenharmony_ci  <br></br>
885e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
886e41f4b71Sopenharmony_ci  Pointer to the key parameters for encryption, such as the key working mode and padding mode.
887e41f4b71Sopenharmony_ci  <br></br>
888e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *plainText</strong>
889e41f4b71Sopenharmony_ci  Pointer to the plaintext data to encrypt.
890e41f4b71Sopenharmony_ci  <br></br>
891e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *cipherText</strong>
892e41f4b71Sopenharmony_ci  Pointer to the encrypted data (data in ciphertext).
893e41f4b71Sopenharmony_ci  </pre>
894e41f4b71Sopenharmony_ci</details>
895e41f4b71Sopenharmony_ci<br></br>
896e41f4b71Sopenharmony_ci
897e41f4b71Sopenharmony_ci<details>
898e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
899e41f4b71Sopenharmony_ci
900e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
901e41f4b71Sopenharmony_ci
902e41f4b71Sopenharmony_ci  - Other values: The operation fails.
903e41f4b71Sopenharmony_ci</details>
904e41f4b71Sopenharmony_ci
905e41f4b71Sopenharmony_ci- - -
906e41f4b71Sopenharmony_ci
907e41f4b71Sopenharmony_ci#### Decrypt
908e41f4b71Sopenharmony_ci
909e41f4b71Sopenharmony_ci**API Description**
910e41f4b71Sopenharmony_ci
911e41f4b71Sopenharmony_ciDecrypts data. Different from the key session APIs that must be used together, this API completes data decryption in a single call.
912e41f4b71Sopenharmony_ci
913e41f4b71Sopenharmony_ci**Prototype**
914e41f4b71Sopenharmony_ci<pre><code>int32_t Decrypt(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
915e41f4b71Sopenharmony_ci    const struct HuksBlob *cipherText, struct HuksBlob *plainText);</code></pre>
916e41f4b71Sopenharmony_ci
917e41f4b71Sopenharmony_ci<details>
918e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
919e41f4b71Sopenharmony_ci  <pre>
920e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
921e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
922e41f4b71Sopenharmony_ci  <br></br>
923e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
924e41f4b71Sopenharmony_ci  Pointer to the key material (ciphertext) used for decryption.
925e41f4b71Sopenharmony_ci  <br></br>
926e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
927e41f4b71Sopenharmony_ci  Pointer to the key parameters for decryption, such as the key working mode and padding mode.
928e41f4b71Sopenharmony_ci  <br></br>
929e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *cipherText</strong>
930e41f4b71Sopenharmony_ci  Pointer to the data to decrypt.
931e41f4b71Sopenharmony_ci  <br></br>
932e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *plainText</strong>
933e41f4b71Sopenharmony_ci  Pointer to the encrypted data in plaintext.
934e41f4b71Sopenharmony_ci  </pre>
935e41f4b71Sopenharmony_ci</details>
936e41f4b71Sopenharmony_ci<br></br>
937e41f4b71Sopenharmony_ci
938e41f4b71Sopenharmony_ci<details>
939e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
940e41f4b71Sopenharmony_ci
941e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
942e41f4b71Sopenharmony_ci
943e41f4b71Sopenharmony_ci  - Other values: The operation fails.
944e41f4b71Sopenharmony_ci</details>
945e41f4b71Sopenharmony_ci
946e41f4b71Sopenharmony_ci- - -
947e41f4b71Sopenharmony_ci
948e41f4b71Sopenharmony_ci#### AgreeKey
949e41f4b71Sopenharmony_ci
950e41f4b71Sopenharmony_ci**API Description**
951e41f4b71Sopenharmony_ci
952e41f4b71Sopenharmony_ciPerforms key agreement. Different from the key session APIs that must be used together, this API returns an agreed key in a single call.
953e41f4b71Sopenharmony_ci
954e41f4b71Sopenharmony_ci**Prototype**
955e41f4b71Sopenharmony_ci<pre><code>int32_t AgreeKey(struct IHuks *self, const struct HuksParamSet *paramSet,
956e41f4b71Sopenharmony_ci    const struct HuksBlob *encPrivateKey, const struct HuksBlob *peerPublicKey, struct HuksBlob *agreedKey);</code></pre>
957e41f4b71Sopenharmony_ci
958e41f4b71Sopenharmony_ci<details>
959e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
960e41f4b71Sopenharmony_ci  <pre>
961e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
962e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
963e41f4b71Sopenharmony_ci  <br></br>
964e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
965e41f4b71Sopenharmony_ci  Pointer to the parameters for key agreement, such as the length of the agreed key.
966e41f4b71Sopenharmony_ci  <br></br>
967e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encPrivateKey</strong>
968e41f4b71Sopenharmony_ci  Pointer to the private key material (ciphertext) used for key agreement.
969e41f4b71Sopenharmony_ci  <br></br>
970e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *peerPublicKey</strong>
971e41f4b71Sopenharmony_ci  Pointer to the public key (plaintext) used for key agreement.
972e41f4b71Sopenharmony_ci  <br></br>
973e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *agreedKey</strong>
974e41f4b71Sopenharmony_ci  Pointer to the agreed key in plaintext.
975e41f4b71Sopenharmony_ci  </pre>
976e41f4b71Sopenharmony_ci</details>
977e41f4b71Sopenharmony_ci<br></br>
978e41f4b71Sopenharmony_ci
979e41f4b71Sopenharmony_ci<details>
980e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
981e41f4b71Sopenharmony_ci
982e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
983e41f4b71Sopenharmony_ci
984e41f4b71Sopenharmony_ci  - Other values: The operation fails.
985e41f4b71Sopenharmony_ci</details>
986e41f4b71Sopenharmony_ci
987e41f4b71Sopenharmony_ci- - -
988e41f4b71Sopenharmony_ci
989e41f4b71Sopenharmony_ci#### DeriveKey
990e41f4b71Sopenharmony_ci
991e41f4b71Sopenharmony_ci**API Description**
992e41f4b71Sopenharmony_ci
993e41f4b71Sopenharmony_ciDerives a key. Different from the key session APIs that must be used together, this API derives a key in a single call.
994e41f4b71Sopenharmony_ci
995e41f4b71Sopenharmony_ci**Prototype**
996e41f4b71Sopenharmony_ci<pre><code>int32_t DeriveKey(struct IHuks *self, const struct HuksParamSet *paramSet, const struct HuksBlob *encKdfKey,
997e41f4b71Sopenharmony_ci     struct HuksBlob *derivedKey);</code></pre>
998e41f4b71Sopenharmony_ci
999e41f4b71Sopenharmony_ci<details>
1000e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
1001e41f4b71Sopenharmony_ci  <pre>
1002e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
1003e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
1004e41f4b71Sopenharmony_ci  <br></br>
1005e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
1006e41f4b71Sopenharmony_ci  Pointer to the parameters for key derivation, such as the length of the derived key.
1007e41f4b71Sopenharmony_ci  <br></br>
1008e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKdfKey</strong>
1009e41f4b71Sopenharmony_ci  Pointer to the key material (ciphertext) used for deriving a key.
1010e41f4b71Sopenharmony_ci  <br></br>
1011e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *derivedKey</strong>
1012e41f4b71Sopenharmony_ci  Pointer to the derived key in plaintext.
1013e41f4b71Sopenharmony_ci  </pre>
1014e41f4b71Sopenharmony_ci</details>
1015e41f4b71Sopenharmony_ci<br></br>
1016e41f4b71Sopenharmony_ci
1017e41f4b71Sopenharmony_ci<details>
1018e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
1019e41f4b71Sopenharmony_ci
1020e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
1021e41f4b71Sopenharmony_ci
1022e41f4b71Sopenharmony_ci  - Other values: The operation fails.
1023e41f4b71Sopenharmony_ci</details>
1024e41f4b71Sopenharmony_ci
1025e41f4b71Sopenharmony_ci- - -
1026e41f4b71Sopenharmony_ci
1027e41f4b71Sopenharmony_ci#### Mac
1028e41f4b71Sopenharmony_ci
1029e41f4b71Sopenharmony_ci**API Description**
1030e41f4b71Sopenharmony_ci
1031e41f4b71Sopenharmony_ciGenerates a MAC based on the given key.
1032e41f4b71Sopenharmony_ci
1033e41f4b71Sopenharmony_ci**Prototype**
1034e41f4b71Sopenharmony_ci<pre><code>int32_t Mac(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
1035e41f4b71Sopenharmony_ci     const struct HuksBlob *srcData, struct HuksBlob *mac);</code></pre>
1036e41f4b71Sopenharmony_ci
1037e41f4b71Sopenharmony_ci<details>
1038e41f4b71Sopenharmony_ci  <summary><strong>Parameters</strong></summary>
1039e41f4b71Sopenharmony_ci  <pre>
1040e41f4b71Sopenharmony_ci  <strong>struct IHuks *self</strong>
1041e41f4b71Sopenharmony_ci  Pointer to the HUKS HDI struct.
1042e41f4b71Sopenharmony_ci  <br></br>
1043e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *encKey</strong>
1044e41f4b71Sopenharmony_ci  Pointer to the key material (ciphertext) used to generate the MAC.
1045e41f4b71Sopenharmony_ci  <br></br>
1046e41f4b71Sopenharmony_ci  <strong>const struct HuksParamSet *paramSet</strong>
1047e41f4b71Sopenharmony_ci  Pointer to the parameters for generating the MAC.
1048e41f4b71Sopenharmony_ci  <br></br>
1049e41f4b71Sopenharmony_ci  <strong>const struct HuksBlob *srcData</strong>
1050e41f4b71Sopenharmony_ci  Pointre to the source data for which the MAC is to be generated.
1051e41f4b71Sopenharmony_ci  <br></br>
1052e41f4b71Sopenharmony_ci  <strong>struct HuksBlob *mac</strong>
1053e41f4b71Sopenharmony_ci  Pointer to the MAC generated.
1054e41f4b71Sopenharmony_ci  </pre>
1055e41f4b71Sopenharmony_ci</details>
1056e41f4b71Sopenharmony_ci<br></br>
1057e41f4b71Sopenharmony_ci
1058e41f4b71Sopenharmony_ci<details>
1059e41f4b71Sopenharmony_ci  <summary><strong>Return Value</strong></summary>
1060e41f4b71Sopenharmony_ci
1061e41f4b71Sopenharmony_ci  - **HKS_SUCCESS**: The operation is successful.
1062e41f4b71Sopenharmony_ci
1063e41f4b71Sopenharmony_ci  - Other values: The operation fails.
1064e41f4b71Sopenharmony_ci</details>
1065e41f4b71Sopenharmony_ci
1066e41f4b71Sopenharmony_ci- - -
1067e41f4b71Sopenharmony_ci### Development Procedure
1068e41f4b71Sopenharmony_ci
1069e41f4b71Sopenharmony_ci#### Directory Structure
1070e41f4b71Sopenharmony_ci
1071e41f4b71Sopenharmony_ciThe code for HDI adaptation is in the following directory.
1072e41f4b71Sopenharmony_ci
1073e41f4b71Sopenharmony_ci```undefined
1074e41f4b71Sopenharmony_ci//drivers_peripheral/huks
1075e41f4b71Sopenharmony_ci├── BUILD.gn                           # Build script.
1076e41f4b71Sopenharmony_ci├── hdi_service                        # Dependency loaded from libhuks_engine_core_standard.z.so (software implementation of the HUKS Core for reference only) by using dlopen().
1077e41f4b71Sopenharmony_ci    ├── huks_sa_type.h                 # Defines the data structs used in the HUKS service layer.
1078e41f4b71Sopenharmony_ci    ├── huks_sa_hdi_struct.h           # Defines the function pointer structs in libhuks_engine_core_standard.z.so.
1079e41f4b71Sopenharmony_ci    ├── huks_hdi_template.h            # Defines the conversion between the data structs of the HUKS service layer and the HDI.
1080e41f4b71Sopenharmony_ci    ├── huks_hdi_service.c             # Implementation of the HUKS passthrough HDI APIs.
1081e41f4b71Sopenharmony_ci    └── huks_hdi_passthrough_adapter.c # Adaptation of HUKS passthrough HDI APIs to the HUKS Core.
1082e41f4b71Sopenharmony_ci└── test                               # Test code for the HUKS HDI APIs. 
1083e41f4b71Sopenharmony_ci    ├── BUILD.gn                       # Build script.
1084e41f4b71Sopenharmony_ci    ├── fuzztest                       # Fuzz test.
1085e41f4b71Sopenharmony_ci    └── unittest                       # Unit test.
1086e41f4b71Sopenharmony_ci```
1087e41f4b71Sopenharmony_ci
1088e41f4b71Sopenharmony_ciThe code of the HUKS Core software implementation is in the following directory.
1089e41f4b71Sopenharmony_ci
1090e41f4b71Sopenharmony_ci```undefined
1091e41f4b71Sopenharmony_ci//base/security/huks/services/huks_standard/huks_engine
1092e41f4b71Sopenharmony_ci├── BUILD.gn                       # Build script.
1093e41f4b71Sopenharmony_ci├── core_dependency                # HUKS Core dependency.
1094e41f4b71Sopenharmony_ci└── core                           # Software implementation of the HUKS Core.
1095e41f4b71Sopenharmony_ci    ├── BUILD.gn                   # Build script.
1096e41f4b71Sopenharmony_ci    ├── include 
1097e41f4b71Sopenharmony_ci    └── src
1098e41f4b71Sopenharmony_ci        ├── hks_core_interfaces.c  # Adaptation of the HDI to the HUKS Core.
1099e41f4b71Sopenharmony_ci        └── hks_core_service.c     # HUKS Core implementation.
1100e41f4b71Sopenharmony_ci        └── ...                    # Code of other functions.
1101e41f4b71Sopenharmony_ci```
1102e41f4b71Sopenharmony_ci**CAUTION**
1103e41f4b71Sopenharmony_ci
1104e41f4b71Sopenharmony_ci<summary><strong>The software implementation of the HUKS Core contains hard-coded sensitive data, including the root key, AuthToken key for access control, key used to encrypt AuthToken, and certificate. You need to replace these code with your own implementation. /summary>
1105e41f4b71Sopenharmony_ci
1106e41f4b71Sopenharmony_ci  - Root key
1107e41f4b71Sopenharmony_ci
1108e41f4b71Sopenharmony_ci    The root key is used to encrypt the HUKS service key and is generally derived from the device root key. It is hard-coded in the HUKS Core software implementation. For details, see <a href="https://gitee.com/openharmony/security_huks/blob/master/frameworks/huks_standard/main/crypto_engine/openssl/src/hks_openssl_get_main_key.c">hks_openssl_get_main_key.c</a>.
1109e41f4b71Sopenharmony_ci
1110e41f4b71Sopenharmony_ci - Key for generating an HMAC on AuthToken
1111e41f4b71Sopenharmony_ci
1112e41f4b71Sopenharmony_ci   The key is used to generate an HMAC on AuthToken by the UserIAM for access control. The value is **huks_default_user_auth_token_key** and hard-coded in the HUKS Core software implementation. For details about the code, see <a href="https://gitee.com/openharmony/security_huks/blob/master/services/huks_standard/huks_engine/main/core/src/hks_keyblob.c">hks_keyblob.c</a>.
1113e41f4b71Sopenharmony_ci
1114e41f4b71Sopenharmony_ci - Key for encrypting sensitive fields of AuthToken
1115e41f4b71Sopenharmony_ci
1116e41f4b71Sopenharmony_ci   The key is used to encrypt sensitive fields of AuthToken in access control. The value is **huks_default_user_auth_token_key** and hard-coded in the HUKS Core software implementation. For details about the code, see <a href="https://gitee.com/openharmony/security_huks/blob/master/services/huks_standard/huks_engine/main/core/src/hks_keyblob.c">hks_keyblob.c</a>.
1117e41f4b71Sopenharmony_ci
1118e41f4b71Sopenharmony_ci  - Root certificate, device CA, and device certificate
1119e41f4b71Sopenharmony_ci
1120e41f4b71Sopenharmony_ci    The root certificate, device CA, and device certificate are used for key attestation and preset in the secure storage of the hardware device by the device certificate management module. These certificates are hard-coded in the HUKS Core software implementation. For details about the code, see <a href="https://gitee.com/openharmony/security_huks/blob/master/services/huks_standard/huks_engine/main/device_cert_manager/include/dcm_certs_and_key.h">dcm_certs_and_key.h</a>.
1121e41f4b71Sopenharmony_ci
1122e41f4b71Sopenharmony_ci#### Example
1123e41f4b71Sopenharmony_ci
1124e41f4b71Sopenharmony_ciThe following uses the adaptation of the key session APIs **Init**, **Update**, and **Finish** in the HUKS Core as an example. The sample code is for reference only and cannot be used directly. For details about the executable code, see [HUKS source code](https://gitee.com/openharmony/security_huks).
1125e41f4b71Sopenharmony_ci
1126e41f4b71Sopenharmony_ci1. Create a handle to identify information about key operations in a session. With this handle, multiple operations on the same key can be performed.
1127e41f4b71Sopenharmony_ci
1128e41f4b71Sopenharmony_ci   ```c
1129e41f4b71Sopenharmony_ci
1130e41f4b71Sopenharmony_ci   // Initialize the key session.
1131e41f4b71Sopenharmony_ci
1132e41f4b71Sopenharmony_ci   int32_t HksCoreInit(const struct  HuksBlob *key, const struct HuksParamSet *paramSet, struct HuksBlob *handle,
1133e41f4b71Sopenharmony_ci    struct HuksBlob *token)
1134e41f4b71Sopenharmony_ci   {
1135e41f4b71Sopenharmony_ci       HKS_LOG_D("HksCoreInit in Core start");
1136e41f4b71Sopenharmony_ci       uint32_t pur = 0;
1137e41f4b71Sopenharmony_ci       uint32_t alg = 0;
1138e41f4b71Sopenharmony_ci       // Check parameters.
1139e41f4b71Sopenharmony_ci       if (key == NULL || paramSet == NULL || handle == NULL || token == NULL) {
1140e41f4b71Sopenharmony_ci           HKS_LOG_E("the pointer param entered is invalid");
1141e41f4b71Sopenharmony_ci           return HKS_FAILURE;
1142e41f4b71Sopenharmony_ci        }
1143e41f4b71Sopenharmony_ci
1144e41f4b71Sopenharmony_ci        if (handle->size < sizeof(uint64_t)) {
1145e41f4b71Sopenharmony_ci            HKS_LOG_E("handle size is too small, size : %u", handle->size);
1146e41f4b71Sopenharmony_ci            return HKS_ERROR_INSUFFICIENT_MEMORY;
1147e41f4b71Sopenharmony_ci        }
1148e41f4b71Sopenharmony_ci        // Decrypt the key file.
1149e41f4b71Sopenharmony_ci        struct HuksKeyNode *keyNode = HksCreateKeyNode(key, paramSet);
1150e41f4b71Sopenharmony_ci        if (keyNode == NULL || handle == NULL) {
1151e41f4b71Sopenharmony_ci            HKS_LOG_E("the pointer param entered is invalid");
1152e41f4b71Sopenharmony_ci            return HKS_ERROR_BAD_STATE;
1153e41f4b71Sopenharmony_ci        }
1154e41f4b71Sopenharmony_ci        // Store information in a session based on the handle. The information stored can be used for the Update() and Finish() operations on the key.
1155e41f4b71Sopenharmony_ci        handle->size = sizeof(uint64_t);
1156e41f4b71Sopenharmony_ci        (void)memcpy_s(handle->data, handle->size, &(keyNode->handle), handle->size);
1157e41f4b71Sopenharmony_ci        // Obtain the algorithm from the parameters.
1158e41f4b71Sopenharmony_ci        int32_t ret = GetPurposeAndAlgorithm(paramSet, &pur, &alg);
1159e41f4b71Sopenharmony_ci        if (ret != HKS_SUCCESS) {
1160e41f4b71Sopenharmony_ci            HksDeleteKeyNode(keyNode->handle);
1161e41f4b71Sopenharmony_ci            return ret;
1162e41f4b71Sopenharmony_ci        }
1163e41f4b71Sopenharmony_ci        // Check key parameters.
1164e41f4b71Sopenharmony_ci        ret = HksCoreSecureAccessInitParams(keyNode, paramSet, token);
1165e41f4b71Sopenharmony_ci        if (ret != HKS_SUCCESS) {
1166e41f4b71Sopenharmony_ci            HKS_LOG_E("init secure access params failed");
1167e41f4b71Sopenharmony_ci            HksDeleteKeyNode(keyNode->handle);
1168e41f4b71Sopenharmony_ci            return ret;
1169e41f4b71Sopenharmony_ci        }
1170e41f4b71Sopenharmony_ci        // Obtain the initialization handler from the algorithm library based on the key purpose. 
1171e41f4b71Sopenharmony_ci        uint32_t i;
1172e41f4b71Sopenharmony_ci        uint32_t size = HKS_ARRAY_SIZE(g_hksCoreInitHandler);
1173e41f4b71Sopenharmony_ci        for (i = 0; i < size; i++) {
1174e41f4b71Sopenharmony_ci           if (g_hksCoreInitHandler[i].pur == pur) {
1175e41f4b71Sopenharmony_ci               HKS_LOG_E("Core HksCoreInit [pur] = %d, pur = %d", g_hksCoreInitHandler[i].pur, pur);
1176e41f4b71Sopenharmony_ci               ret = g_hksCoreInitHandler[i].handler(keyNode, paramSet, alg);
1177e41f4b71Sopenharmony_ci               break;
1178e41f4b71Sopenharmony_ci        }
1179e41f4b71Sopenharmony_ci        }
1180e41f4b71Sopenharmony_ci        // Check exception results.
1181e41f4b71Sopenharmony_ci        if (ret != HKS_SUCCESS) {
1182e41f4b71Sopenharmony_ci            HksDeleteKeyNode(keyNode->handle);
1183e41f4b71Sopenharmony_ci            HKS_LOG_E("CoreInit failed, ret : %d", ret);
1184e41f4b71Sopenharmony_ci            return ret;
1185e41f4b71Sopenharmony_ci        }
1186e41f4b71Sopenharmony_ci    
1187e41f4b71Sopenharmony_ci        if (i == size) {
1188e41f4b71Sopenharmony_ci            HksDeleteKeyNode(keyNode->handle);
1189e41f4b71Sopenharmony_ci            HKS_LOG_E("don't found purpose, pur : %u", pur);
1190e41f4b71Sopenharmony_ci            return HKS_FAILURE;
1191e41f4b71Sopenharmony_ci        }
1192e41f4b71Sopenharmony_ci    
1193e41f4b71Sopenharmony_ci        HKS_LOG_D("HksCoreInit in Core end");
1194e41f4b71Sopenharmony_ci        return ret;
1195e41f4b71Sopenharmony_ci    }
1196e41f4b71Sopenharmony_ci   ```
1197e41f4b71Sopenharmony_ci
1198e41f4b71Sopenharmony_ci2. Obtain the context based on the handle, and pass in data by segment to obtain the operation result or append data.
1199e41f4b71Sopenharmony_ci   
1200e41f4b71Sopenharmony_ci    ```c
1201e41f4b71Sopenharmony_ci    // Update data by segment.
1202e41f4b71Sopenharmony_ci    int32_t HksCoreUpdate(const struct HuksBlob *handle, const struct HuksParamSet *paramSet, const struct HuksBlob *inData,
1203e41f4b71Sopenharmony_ci        struct HuksBlob *outData)
1204e41f4b71Sopenharmony_ci    {
1205e41f4b71Sopenharmony_ci        HKS_LOG_D("HksCoreUpdate in Core start");
1206e41f4b71Sopenharmony_ci        uint32_t pur = 0;
1207e41f4b71Sopenharmony_ci        uint32_t alg = 0;
1208e41f4b71Sopenharmony_ci        // Check parameters.
1209e41f4b71Sopenharmony_ci        if (handle == NULL || paramSet == NULL || inData == NULL) {
1210e41f4b71Sopenharmony_ci            HKS_LOG_E("the pointer param entered is invalid");
1211e41f4b71Sopenharmony_ci            return HKS_FAILURE;
1212e41f4b71Sopenharmony_ci        }
1213e41f4b71Sopenharmony_ci        
1214e41f4b71Sopenharmony_ci        uint64_t sessionId;
1215e41f4b71Sopenharmony_ci        struct HuksKeyNode *keyNode = NULL;
1216e41f4b71Sopenharmony_ci        // Obtain the context required for the key session operation based on the handle.
1217e41f4b71Sopenharmony_ci        int32_t ret = GetParamsForUpdateAndFinish(handle, &sessionId, &keyNode, &pur, &alg);
1218e41f4b71Sopenharmony_ci        if (ret != HKS_SUCCESS) {
1219e41f4b71Sopenharmony_ci            HKS_LOG_E("GetParamsForCoreUpdate failed");
1220e41f4b71Sopenharmony_ci            return ret;
1221e41f4b71Sopenharmony_ci        }
1222e41f4b71Sopenharmony_ci        // Verify key parameters.
1223e41f4b71Sopenharmony_ci        ret = HksCoreSecureAccessVerifyParams(keyNode, paramSet);
1224e41f4b71Sopenharmony_ci        if (ret != HKS_SUCCESS) {
1225e41f4b71Sopenharmony_ci            HksDeleteKeyNode(sessionId);
1226e41f4b71Sopenharmony_ci            HKS_LOG_E("HksCoreUpdate secure access verify failed");
1227e41f4b71Sopenharmony_ci            return ret;
1228e41f4b71Sopenharmony_ci        }
1229e41f4b71Sopenharmony_ci        // Call the key handler from the corresponding algorithm library.
1230e41f4b71Sopenharmony_ci        uint32_t i;
1231e41f4b71Sopenharmony_ci        uint32_t size = HKS_ARRAY_SIZE(g_hksCoreUpdateHandler);
1232e41f4b71Sopenharmony_ci        for (i = 0; i < size; i++) {
1233e41f4b71Sopenharmony_ci            if (g_hksCoreUpdateHandler[i].pur == pur) {
1234e41f4b71Sopenharmony_ci                struct HuksBlob appendInData = { 0, NULL };
1235e41f4b71Sopenharmony_ci                ret = HksCoreAppendAuthInfoBeforeUpdate(keyNode, pur, paramSet, inData, &appendInData);
1236e41f4b71Sopenharmony_ci                if (ret != HKS_SUCCESS) {
1237e41f4b71Sopenharmony_ci                    HKS_LOG_E("before update: append auth info failed");
1238e41f4b71Sopenharmony_ci                    break;
1239e41f4b71Sopenharmony_ci                }
1240e41f4b71Sopenharmony_ci                ret = g_hksCoreUpdateHandler[i].handler(keyNode, paramSet,
1241e41f4b71Sopenharmony_ci                     appendInData.data == NULL ? inData : &appendInData, outData, alg);
1242e41f4b71Sopenharmony_ci                if (appendInData.data != NULL) {
1243e41f4b71Sopenharmony_ci                    HKS_FREE_BLOB(appendInData);
1244e41f4b71Sopenharmony_ci                }
1245e41f4b71Sopenharmony_ci                break;
1246e41f4b71Sopenharmony_ci            }
1247e41f4b71Sopenharmony_ci        }
1248e41f4b71Sopenharmony_ci        // Check exception results.
1249e41f4b71Sopenharmony_ci        if (ret != HKS_SUCCESS) {
1250e41f4b71Sopenharmony_ci            HksDeleteKeyNode(keyNode->handle);
1251e41f4b71Sopenharmony_ci            HKS_LOG_E("CoreUpdate failed, ret : %d", ret);
1252e41f4b71Sopenharmony_ci            return ret;
1253e41f4b71Sopenharmony_ci        }
1254e41f4b71Sopenharmony_ci        
1255e41f4b71Sopenharmony_ci        if (i == size) {
1256e41f4b71Sopenharmony_ci            HksDeleteKeyNode(sessionId);
1257e41f4b71Sopenharmony_ci            HKS_LOG_E("don't found purpose, pur : %u", pur);
1258e41f4b71Sopenharmony_ci            return HKS_FAILURE;
1259e41f4b71Sopenharmony_ci        }
1260e41f4b71Sopenharmony_ci        return ret;
1261e41f4b71Sopenharmony_ci    }
1262e41f4b71Sopenharmony_ci    ```
1263e41f4b71Sopenharmony_ci
1264e41f4b71Sopenharmony_ci3. Finish the key operation to obtain the result, and destroy the handle.
1265e41f4b71Sopenharmony_ci
1266e41f4b71Sopenharmony_ci   ```c
1267e41f4b71Sopenharmony_ci   // Finish the key session operation.
1268e41f4b71Sopenharmony_ci   int32_t HksCoreFinish(const struct HuksBlob *handle, const struct HuksParamSet *paramSet, const struct HuksBlob *inData,
1269e41f4b71Sopenharmony_ci    struct HuksBlob *outData)
1270e41f4b71Sopenharmony_ci   {
1271e41f4b71Sopenharmony_ci       HKS_LOG_D("HksCoreFinish in Core start");
1272e41f4b71Sopenharmony_ci       uint32_t pur = 0;
1273e41f4b71Sopenharmony_ci       uint32_t alg = 0;
1274e41f4b71Sopenharmony_ci       // Check parameters.
1275e41f4b71Sopenharmony_ci       if (handle == NULL || paramSet == NULL || inData == NULL) {
1276e41f4b71Sopenharmony_ci           HKS_LOG_E("the pointer param entered is invalid");
1277e41f4b71Sopenharmony_ci           return HKS_FAILURE;
1278e41f4b71Sopenharmony_ci       }
1279e41f4b71Sopenharmony_ci    
1280e41f4b71Sopenharmony_ci       uint64_t sessionId;
1281e41f4b71Sopenharmony_ci       struct HuksKeyNode *keyNode = NULL;
1282e41f4b71Sopenharmony_ci       // Obtain the context required for the key session operation based on the handle.
1283e41f4b71Sopenharmony_ci       int32_t ret = GetParamsForUpdateAndFinish(handle, &sessionId, &keyNode, &pur, &alg);
1284e41f4b71Sopenharmony_ci       if (ret != HKS_SUCCESS) {
1285e41f4b71Sopenharmony_ci           HKS_LOG_E("GetParamsForCoreUpdate failed");
1286e41f4b71Sopenharmony_ci           return ret;
1287e41f4b71Sopenharmony_ci       }
1288e41f4b71Sopenharmony_ci       // Verify key parameters.
1289e41f4b71Sopenharmony_ci       ret = HksCoreSecureAccessVerifyParams(keyNode, paramSet);
1290e41f4b71Sopenharmony_ci       if (ret != HKS_SUCCESS) {
1291e41f4b71Sopenharmony_ci           HksDeleteKeyNode(sessionId);
1292e41f4b71Sopenharmony_ci           HKS_LOG_E("HksCoreFinish secure access verify failed");
1293e41f4b71Sopenharmony_ci           return ret;
1294e41f4b71Sopenharmony_ci       }
1295e41f4b71Sopenharmony_ci       // Call the key handler from the corresponding algorithm library.
1296e41f4b71Sopenharmony_ci       uint32_t i;
1297e41f4b71Sopenharmony_ci       uint32_t size = HKS_ARRAY_SIZE(g_hksCoreFinishHandler);
1298e41f4b71Sopenharmony_ci       for (i = 0; i < size; i++) {
1299e41f4b71Sopenharmony_ci           if (g_hksCoreFinishHandler[i].pur == pur) {
1300e41f4b71Sopenharmony_ci               uint32_t outDataBufferSize = (outData == NULL) ? 0 : outData->size;
1301e41f4b71Sopenharmony_ci               struct HuksBlob appendInData = { 0, NULL };
1302e41f4b71Sopenharmony_ci               ret = HksCoreAppendAuthInfoBeforeFinish(keyNode, pur, paramSet, inData, &appendInData);
1303e41f4b71Sopenharmony_ci               if (ret != HKS_SUCCESS) {
1304e41f4b71Sopenharmony_ci                   HKS_LOG_E("before finish: append auth info failed");
1305e41f4b71Sopenharmony_ci                   break;
1306e41f4b71Sopenharmony_ci               }
1307e41f4b71Sopenharmony_ci               ret = g_hksCoreFinishHandler[i].handler(keyNode, paramSet,
1308e41f4b71Sopenharmony_ci                   appendInData.data == NULL ? inData : &appendInData, outData, alg);
1309e41f4b71Sopenharmony_ci               if (appendInData.data != NULL) {
1310e41f4b71Sopenharmony_ci                   HKS_FREE_BLOB(appendInData);
1311e41f4b71Sopenharmony_ci               }
1312e41f4b71Sopenharmony_ci               if (ret != HKS_SUCCESS) {
1313e41f4b71Sopenharmony_ci                   break;
1314e41f4b71Sopenharmony_ci               }
1315e41f4b71Sopenharmony_ci               // Append the end label of the key operation.
1316e41f4b71Sopenharmony_ci               ret = HksCoreAppendAuthInfoAfterFinish(keyNode, pur, paramSet, outDataBufferSize, outData);
1317e41f4b71Sopenharmony_ci               break;
1318e41f4b71Sopenharmony_ci           }
1319e41f4b71Sopenharmony_ci       }
1320e41f4b71Sopenharmony_ci       if (i == size) {
1321e41f4b71Sopenharmony_ci           HKS_LOG_E("don't found purpose, pur : %d", pur);
1322e41f4b71Sopenharmony_ci           ret = HKS_FAILURE;
1323e41f4b71Sopenharmony_ci       }
1324e41f4b71Sopenharmony_ci       // Delete the session.
1325e41f4b71Sopenharmony_ci       HksDeleteKeyNode(sessionId);
1326e41f4b71Sopenharmony_ci       HKS_LOG_D("HksCoreFinish in Core end");
1327e41f4b71Sopenharmony_ci       return ret;
1328e41f4b71Sopenharmony_ci   }
1329e41f4b71Sopenharmony_ci   ```
1330e41f4b71Sopenharmony_ci
1331e41f4b71Sopenharmony_ci### Debugging
1332e41f4b71Sopenharmony_ci
1333e41f4b71Sopenharmony_ciUse [HUKS JS APIs](https://gitee.com/openharmony/security_huks/blob/master/interfaces/kits/js/@ohos.security.huks.d.ts) to develop a JavaScript application to verify HUKS capabilities.
1334e41f4b71Sopenharmony_ci
1335e41f4b71Sopenharmony_ciThe JS APIs corresponding to HDI APIs are provided in [Available APIs](#available-apis). You can use the JS APIs to verify the capabilities of the corresponding HDI APIs or perform complete key operations to verify the capabilities of the APIs.
1336e41f4b71Sopenharmony_ci
1337e41f4b71Sopenharmony_ciThe following JS test code is for reference only. If the entire process goes properly, the HDI APIs are functioning. For more key operation types and samples, see [huks-guidelines.md](../../application-dev/security/huks-guidelines.md).
1338e41f4b71Sopenharmony_ci
1339e41f4b71Sopenharmony_ci**Generating an AES Key and Encrypting Data**
1340e41f4b71Sopenharmony_ci
1341e41f4b71Sopenharmony_ci1. Import the HUKS module.
1342e41f4b71Sopenharmony_ci
1343e41f4b71Sopenharmony_ci   ```ts
1344e41f4b71Sopenharmony_ci   import huks from '@ohos.security.huks'
1345e41f4b71Sopenharmony_ci   ```
1346e41f4b71Sopenharmony_ci
1347e41f4b71Sopenharmony_ci2. Use **generateKey()** to generate a key.
1348e41f4b71Sopenharmony_ci
1349e41f4b71Sopenharmony_ci   ```ts
1350e41f4b71Sopenharmony_ci    import { BusinessError } from '@ohos.base';
1351e41f4b71Sopenharmony_ci    let aesKeyAlias = 'test_aesKeyAlias';
1352e41f4b71Sopenharmony_ci    let handle = 0;
1353e41f4b71Sopenharmony_ci    let IV = '001122334455';
1354e41f4b71Sopenharmony_ci
1355e41f4b71Sopenharmony_ci    class HuksProperties {
1356e41f4b71Sopenharmony_ci      tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM;
1357e41f4b71Sopenharmony_ci      value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose = huks.HuksKeyAlg.HUKS_ALG_ECC;
1358e41f4b71Sopenharmony_ci    }
1359e41f4b71Sopenharmony_ci
1360e41f4b71Sopenharmony_ci    class HuksProperties1 {
1361e41f4b71Sopenharmony_ci      tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM;
1362e41f4b71Sopenharmony_ci      value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyPadding | huks.HuksCipherMode | Uint8Array = huks.HuksKeyAlg.HUKS_ALG_ECC;
1363e41f4b71Sopenharmony_ci    }
1364e41f4b71Sopenharmony_ci
1365e41f4b71Sopenharmony_ci    function GetAesGenerateProperties() {
1366e41f4b71Sopenharmony_ci      let properties: HuksProperties[] = [
1367e41f4b71Sopenharmony_ci        {
1368e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
1369e41f4b71Sopenharmony_ci          value: huks.HuksKeyAlg.HUKS_ALG_AES
1370e41f4b71Sopenharmony_ci        },
1371e41f4b71Sopenharmony_ci        {
1372e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
1373e41f4b71Sopenharmony_ci          value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
1374e41f4b71Sopenharmony_ci        },
1375e41f4b71Sopenharmony_ci        {
1376e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_PURPOSE,
1377e41f4b71Sopenharmony_ci          value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT |
1378e41f4b71Sopenharmony_ci          huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
1379e41f4b71Sopenharmony_ci        }
1380e41f4b71Sopenharmony_ci      ];
1381e41f4b71Sopenharmony_ci      return properties;
1382e41f4b71Sopenharmony_ci    }
1383e41f4b71Sopenharmony_ci
1384e41f4b71Sopenharmony_ci    async function GenerateAesKey() {
1385e41f4b71Sopenharmony_ci      let genProperties = GetAesGenerateProperties();
1386e41f4b71Sopenharmony_ci      let options: huks.HuksOptions = {
1387e41f4b71Sopenharmony_ci        properties: genProperties
1388e41f4b71Sopenharmony_ci      }
1389e41f4b71Sopenharmony_ci      await huks.generateKeyItem(aesKeyAlias, options).then((data) => {
1390e41f4b71Sopenharmony_ci        console.log("generateKeyItem success");
1391e41f4b71Sopenharmony_ci      }).catch((error: BusinessError) => {
1392e41f4b71Sopenharmony_ci        console.log("generateKeyItem failed");
1393e41f4b71Sopenharmony_ci      })
1394e41f4b71Sopenharmony_ci    }
1395e41f4b71Sopenharmony_ci   ```
1396e41f4b71Sopenharmony_ci
1397e41f4b71Sopenharmony_ci3. Use **huks.initSession** and **huks.finishSession** to encrypt data.
1398e41f4b71Sopenharmony_ci
1399e41f4b71Sopenharmony_ci   ```ts
1400e41f4b71Sopenharmony_ci    let plainText = '123456';
1401e41f4b71Sopenharmony_ci
1402e41f4b71Sopenharmony_ci    function StringToUint8Array(str: string) {
1403e41f4b71Sopenharmony_ci      let arr: number[] = [];
1404e41f4b71Sopenharmony_ci      for (let i = 0, j = str.length; i < j; ++i) {
1405e41f4b71Sopenharmony_ci        arr.push(str.charCodeAt(i));
1406e41f4b71Sopenharmony_ci      }
1407e41f4b71Sopenharmony_ci      return new Uint8Array(arr);
1408e41f4b71Sopenharmony_ci    }
1409e41f4b71Sopenharmony_ci
1410e41f4b71Sopenharmony_ci    function GetAesEncryptProperties() {
1411e41f4b71Sopenharmony_ci      let properties: HuksProperties1[] = [
1412e41f4b71Sopenharmony_ci        {
1413e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
1414e41f4b71Sopenharmony_ci          value: huks.HuksKeyAlg.HUKS_ALG_AES
1415e41f4b71Sopenharmony_ci        },
1416e41f4b71Sopenharmony_ci        {
1417e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
1418e41f4b71Sopenharmony_ci          value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
1419e41f4b71Sopenharmony_ci        },
1420e41f4b71Sopenharmony_ci        {
1421e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_PURPOSE,
1422e41f4b71Sopenharmony_ci          value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
1423e41f4b71Sopenharmony_ci        },
1424e41f4b71Sopenharmony_ci        {
1425e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_PADDING,
1426e41f4b71Sopenharmony_ci          value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7
1427e41f4b71Sopenharmony_ci        },
1428e41f4b71Sopenharmony_ci        {
1429e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
1430e41f4b71Sopenharmony_ci          value: huks.HuksCipherMode.HUKS_MODE_CBC
1431e41f4b71Sopenharmony_ci        },
1432e41f4b71Sopenharmony_ci        {
1433e41f4b71Sopenharmony_ci          tag: huks.HuksTag.HUKS_TAG_IV,
1434e41f4b71Sopenharmony_ci          value: StringToUint8Array(IV)
1435e41f4b71Sopenharmony_ci        }
1436e41f4b71Sopenharmony_ci      ]
1437e41f4b71Sopenharmony_ci      return properties;
1438e41f4b71Sopenharmony_ci    }
1439e41f4b71Sopenharmony_ci
1440e41f4b71Sopenharmony_ci    async function EncryptData() {
1441e41f4b71Sopenharmony_ci      let encryptProperties = GetAesEncryptProperties();
1442e41f4b71Sopenharmony_ci      let options: huks.HuksOptions = {
1443e41f4b71Sopenharmony_ci        properties: encryptProperties,
1444e41f4b71Sopenharmony_ci        inData: StringToUint8Array(plainText)
1445e41f4b71Sopenharmony_ci      }
1446e41f4b71Sopenharmony_ci      await huks.initSession(aesKeyAlias, options).then((data) => {
1447e41f4b71Sopenharmony_ci        handle = data.handle;
1448e41f4b71Sopenharmony_ci      }).catch((error: BusinessError) => {
1449e41f4b71Sopenharmony_ci        console.log("initSession failed");
1450e41f4b71Sopenharmony_ci      })
1451e41f4b71Sopenharmony_ci      await huks.finishSession(handle, options).then((data) => {
1452e41f4b71Sopenharmony_ci        console.log("finishSession success");
1453e41f4b71Sopenharmony_ci      }).catch((error: BusinessError) => {
1454e41f4b71Sopenharmony_ci        console.log("finishSession failed");
1455e41f4b71Sopenharmony_ci      })
1456e41f4b71Sopenharmony_ci    }
1457e41f4b71Sopenharmony_ci
1458e41f4b71Sopenharmony_ci   ```
1459