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