• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 加解密(C/C++)
2
3以AES 256密钥为例,完成加解密。具体的场景介绍及支持的算法规格,请参考[密钥生成支持的算法](huks-key-generation-overview.md#支持的算法)。
4
5## 在CMake脚本中链接相关动态库
6```txt
7target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
8```
9## 开发步骤
10
11**生成密钥**
12
131. 指定密钥别名。
14
152. 初始化密钥属性集。
16
173. 调用[OH_Huks_GenerateKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_generatekeyitem)生成密钥,具体请参考[密钥生成](huks-key-generation-overview.md)。
18
19除此之外,开发者也可以参考[密钥导入](huks-key-import-overview.md),导入已有的密钥。
20
21**加密**
22
231. 获取密钥别名。
24
252. 获取待加密的数据。
26
273. 调用[OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset)指定算法参数配置。
28   在下方示例中,使用算法AES进行加密时,必须要选择其对应分组模式以及填充模式,用例中选取的分组模式为CBC、填充模式为PKCS7,此时必须要填参数IV。
29
304. 调用[OH_Huks_InitSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_initsession)初始化密钥会话,并获取会话的句柄handle。
31
325. 调用[OH_Huks_FinishSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_finishsession)结束密钥会话,获取加密后的密文。
33
34**解密**
35
361. 获取密钥别名。
37
382. 获取待解密的密文。
39
403. 调用[OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset)指定算法参数配置。
41   在下方示例中,使用算法AES进行解密时,必须要选择其对应分组模式以及填充模式,用例中选取的分组模式为CBC、填充模式为PKCS7,此时必须要填参数IV。
42
434. 调用[OH_Huks_InitSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_initsession)初始化密钥会话,并获取会话的句柄handle。
44
455. 调用[OH_Huks_FinishSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_finishsession)结束密钥会话,获取解密后的数据。
46
47**删除密钥**
48
49当密钥废弃不用时,需要调用OH_Huks_DeleteKeyItem删除密钥,具体请参考[密钥删除](huks-delete-key-ndk.md)。
50
51```c++
52#include "huks/native_huks_api.h"
53#include "huks/native_huks_param.h"
54#include "napi/native_api.h"
55#include <string.h>
56OH_Huks_Result InitParamSet(
57    struct OH_Huks_ParamSet **paramSet,
58    const struct OH_Huks_Param *params,
59    uint32_t paramCount)
60{
61    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
62    if (ret.errorCode != OH_HUKS_SUCCESS) {
63        return ret;
64    }
65    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
66    if (ret.errorCode != OH_HUKS_SUCCESS) {
67        OH_Huks_FreeParamSet(paramSet);
68        return ret;
69    }
70    ret = OH_Huks_BuildParamSet(paramSet);
71    if (ret.errorCode != OH_HUKS_SUCCESS) {
72        OH_Huks_FreeParamSet(paramSet);
73        return ret;
74    }
75    return ret;
76}
77static const uint32_t IV_SIZE = 16;
78static uint8_t IV[IV_SIZE] = { 0 }; // this is a test value, for real use the iv should be different every time.
79static struct OH_Huks_Param g_genEncDecParams[] = {
80    {
81        .tag = OH_HUKS_TAG_ALGORITHM,
82        .uint32Param = OH_HUKS_ALG_AES
83    }, {
84        .tag = OH_HUKS_TAG_PURPOSE,
85        .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
86    }, {
87        .tag = OH_HUKS_TAG_KEY_SIZE,
88        .uint32Param = OH_HUKS_AES_KEY_SIZE_256
89    }, {
90        .tag = OH_HUKS_TAG_PADDING,
91        .uint32Param = OH_HUKS_PADDING_NONE
92    }, {
93        .tag = OH_HUKS_TAG_BLOCK_MODE,
94        .uint32Param = OH_HUKS_MODE_CBC
95    }
96};
97static struct OH_Huks_Param g_encryptParams[] = {
98    {
99        .tag = OH_HUKS_TAG_ALGORITHM,
100        .uint32Param = OH_HUKS_ALG_AES
101    }, {
102        .tag = OH_HUKS_TAG_PURPOSE,
103        .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT
104    }, {
105        .tag = OH_HUKS_TAG_KEY_SIZE,
106        .uint32Param = OH_HUKS_AES_KEY_SIZE_256
107    }, {
108        .tag = OH_HUKS_TAG_PADDING,
109        .uint32Param = OH_HUKS_PADDING_NONE
110    }, {
111        .tag = OH_HUKS_TAG_BLOCK_MODE,
112        .uint32Param = OH_HUKS_MODE_CBC
113    }, {
114        .tag = OH_HUKS_TAG_IV,
115        .blob = {
116            .size = IV_SIZE,
117            .data = (uint8_t *)IV // this is a test value, for real use the iv should be different every time.
118        }
119    }
120};
121static struct OH_Huks_Param g_decryptParams[] = {
122    {
123        .tag = OH_HUKS_TAG_ALGORITHM,
124        .uint32Param = OH_HUKS_ALG_AES
125    }, {
126        .tag = OH_HUKS_TAG_PURPOSE,
127        .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT
128    }, {
129        .tag = OH_HUKS_TAG_KEY_SIZE,
130        .uint32Param = OH_HUKS_AES_KEY_SIZE_256
131    }, {
132        .tag = OH_HUKS_TAG_PADDING,
133        .uint32Param = OH_HUKS_PADDING_NONE
134    }, {
135        .tag = OH_HUKS_TAG_BLOCK_MODE,
136        .uint32Param = OH_HUKS_MODE_CBC
137    }, {
138        .tag = OH_HUKS_TAG_IV,
139        .blob = {
140            .size = IV_SIZE,
141            .data = (uint8_t *)IV // this is a test value, for real use the iv should be different every time.
142        }
143    }
144};
145static const uint32_t AES_COMMON_SIZE = 1024;
146OH_Huks_Result HksAesCipherTestEncrypt(
147        const struct OH_Huks_Blob *keyAlias,
148        const struct OH_Huks_ParamSet *encryptParamSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText)
149{
150    uint8_t handleE[sizeof(uint64_t)] = {0};
151    struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE};
152    OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
153    if (ret.errorCode != OH_HUKS_SUCCESS) {
154        return ret;
155    }
156    ret = OH_Huks_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
157    return ret;
158}
159OH_Huks_Result HksAesCipherTestDecrypt(
160    const struct OH_Huks_Blob *keyAlias,
161    const struct OH_Huks_ParamSet *decryptParamSet, const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText,
162    const struct OH_Huks_Blob *inData)
163{
164    uint8_t handleD[sizeof(uint64_t)] = {0};
165    struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD};
166    OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
167    if (ret.errorCode != OH_HUKS_SUCCESS) {
168        return ret;
169    }
170    ret = OH_Huks_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
171    return ret;
172}
173static napi_value EncDecKey(napi_env env, napi_callback_info info)
174{
175    char tmpKeyAlias[] = "test_enc_dec";
176    struct OH_Huks_Blob keyAlias = { (uint32_t)strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
177    struct OH_Huks_ParamSet *genParamSet = nullptr;
178    struct OH_Huks_ParamSet *encryptParamSet = nullptr;
179    struct OH_Huks_ParamSet *decryptParamSet = nullptr;
180    OH_Huks_Result ohResult;
181    do {
182        /* 1. Generate Key */
183        /*
184        * 模拟生成密钥场景
185        * 1.1. 确定密钥别名
186        */
187        /*
188        * 1.2. 获取生成密钥算法参数配置
189        */
190        ohResult = InitParamSet(&genParamSet, g_genEncDecParams, sizeof(g_genEncDecParams) / sizeof(OH_Huks_Param));
191        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
192            break;
193        }
194        /*
195        * 1.3. 调用generateKeyItem
196        */
197        ohResult = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
198        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
199            break;
200        }
201        /* 2. Encrypt */
202        /*
203        * 模拟加密场景
204        * 2.1. 获取密钥别名
205        */
206        /*
207        * 2.2. 获取待加密的数据
208        */
209        /*
210        * 2.3. 获取加密算法参数配置
211        */
212        ohResult = InitParamSet(&encryptParamSet, g_encryptParams, sizeof(g_encryptParams) / sizeof(OH_Huks_Param));
213        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
214            break;
215        }
216        char tmpInData[] = "AES_ECB_INDATA_1";
217        struct OH_Huks_Blob inData = { (uint32_t)strlen(tmpInData), (uint8_t *)tmpInData };
218        uint8_t cipher[AES_COMMON_SIZE] = {0};
219        struct OH_Huks_Blob cipherText = {AES_COMMON_SIZE, cipher};
220        /*
221        * 2.4. 调用initSession获取handle
222        */
223        /*
224        * 2.5. 调用finishSession获取加密后的密文
225        */
226        ohResult = HksAesCipherTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
227        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
228            break;
229        }
230        /* 3. Decrypt */
231        /*
232        * 模拟解密场景
233        * 3.1. 获取密钥别名
234        */
235        /*
236        * 3.2. 获取待解密的密文
237        */
238        /*
239        * 3.3. 获取解密算法参数配置
240        */
241        ohResult = InitParamSet(&decryptParamSet, g_decryptParams, sizeof(g_decryptParams) / sizeof(OH_Huks_Param));
242        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
243            break;
244        }
245        uint8_t plain[AES_COMMON_SIZE] = {0};
246        struct OH_Huks_Blob plainText = {AES_COMMON_SIZE, plain};
247        /*
248        * 3.4. 调用initSession获取handle
249        */
250        /*
251        * 3.5. 调用finishSession获取解密后的数据
252        */
253        ohResult = HksAesCipherTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
254    } while (0);
255    /* 4. Delete Key */
256    /*
257    * 模拟删除密钥场景
258    * 4.1. 获取密钥别名
259    */
260    /*
261    * 4.2. 调用deleteKeyItem删除密钥
262    */
263    (void)OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
264
265    OH_Huks_FreeParamSet(&genParamSet);
266    OH_Huks_FreeParamSet(&encryptParamSet);
267    OH_Huks_FreeParamSet(&decryptParamSet);
268
269    napi_value ret;
270    napi_create_int32(env, ohResult.errorCode, &ret);
271    return ret;
272}
273```
274