1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 // [Start crypt_decrypt_sm4_cbc]
17 #include "CryptoArchitectureKit/crypto_common.h"
18 #include "CryptoArchitectureKit/crypto_sym_cipher.h"
19 #include <cstring>
20 // [StartExclude crypt_decrypt_sm4_cbc]
21 #include "file.h"
22 // [EndExclude crypt_decrypt_sm4_cbc]
23
doTestSm4Cbc()24 OH_Crypto_ErrCode doTestSm4Cbc()
25 {
26 OH_CryptoSymKeyGenerator *genCtx = nullptr;
27 OH_CryptoSymCipher *encCtx = nullptr;
28 OH_CryptoSymCipher *decCtx = nullptr;
29 OH_CryptoSymKey *keyCtx = nullptr;
30 OH_CryptoSymCipherParams *params = nullptr;
31 Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0};
32 Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0};
33
34 char *plainText = const_cast<char *>("this is test!");
35 Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
36 uint8_t iv[16] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4, 3, 1, 0, 10}; // iv使用安全随机数生成
37 Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)};
38 // 生成对称密钥
39 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx);
40 if (ret != CRYPTO_SUCCESS) {
41 goto end;
42 }
43 ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
44 if (ret != CRYPTO_SUCCESS) {
45 goto end;
46 }
47
48 // 设置参数
49 ret = OH_CryptoSymCipherParams_Create(¶ms);
50 if (ret != CRYPTO_SUCCESS) {
51 goto end;
52 }
53 ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob);
54 if (ret != CRYPTO_SUCCESS) {
55 goto end;
56 }
57
58 // 加密
59 ret = OH_CryptoSymCipher_Create("SM4_128|CBC|PKCS7", &encCtx);
60 if (ret != CRYPTO_SUCCESS) {
61 goto end;
62 }
63 ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params);
64 if (ret != CRYPTO_SUCCESS) {
65 goto end;
66 }
67 ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &outUpdate);
68 if (ret != CRYPTO_SUCCESS) {
69 goto end;
70 }
71
72 // 解密
73 ret = OH_CryptoSymCipher_Create("SM4_128|CBC|PKCS7", &decCtx);
74 if (ret != CRYPTO_SUCCESS) {
75 goto end;
76 }
77 ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params);
78 if (ret != CRYPTO_SUCCESS) {
79 goto end;
80 }
81 ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate);
82 if (ret != CRYPTO_SUCCESS) {
83 goto end;
84 }
85
86 // 资源释放
87 end:
88 OH_CryptoSymCipherParams_Destroy(params);
89 OH_CryptoSymCipher_Destroy(encCtx);
90 OH_CryptoSymCipher_Destroy(decCtx);
91 OH_CryptoSymKeyGenerator_Destroy(genCtx);
92 OH_CryptoSymKey_Destroy(keyCtx);
93 OH_Crypto_FreeDataBlob(&outUpdate);
94 OH_Crypto_FreeDataBlob(&decUpdate);
95 return ret;
96 }
97 // [End crypt_decrypt_sm4_cbc]
98