1# Encryption and Decryption with an AES Symmetric Key (CCM Mode) (C/C++)
2
3
4For details about the algorithm specifications, see [AES](crypto-sym-encrypt-decrypt-spec.md#aes).
5
6
7## Adding the Dynamic Library in the CMake Script
8```txt
9   target_link_libraries(entry PUBLIC libohcrypto.so)
10```
11
12## How to Develop
13
14**Encryption**
15
16
171. Use [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_create) and [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_generate) to generate a 128-bit AES symmetric key (**OH_CryptoSymKey**).
18
19   In addition to the example in this topic, [AES](crypto-sym-key-generation-conversion-spec.md#aes) and [Randomly Generating a Symmetric Key](crypto-generate-sym-key-randomly-ndk.md) may help you better understand how to generate an AES symmetric key. Note that the input parameters in the reference documents may be different from those in the example below.
20
212. Use [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_create) with the string parameter **'AES128|CCM'** to create a **Cipher** instance. The key type is AES128, and the block cipher mode is CCM.
22
233. Use [OH_CryptoSymCipherParams_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_create) to create a symmetric cipher parameter instance, and use [OH_CryptoSymCipherParams_SetParam](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_setparam) to set cipher parameters.
24
254. Use [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_init) to initialize the **Cipher** instance. Specifically, set **mode** to **CRYPTO_ENCRYPT_MODE**, and specify the key for encryption (**OH_CryptoSymKey**) and the encryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CCM mode.
26
275. Use [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_update) to update the data (plaintext) to be encrypted.
28
29   Currently, the amount of data to be passed in by a single **OH_CryptoSymCipher_Update** is not limited. You can determine how to pass in data based on the data volume.
30   > **NOTE**<br>
31   > The CCM mode does not support segment-based encryption and decryption.
32
336. Use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the ciphertext.
34   - If data has been passed in by **OH_CryptoSymCipher_Update**, pass in **null** in the **data** parameter of **OH_CryptoSymCipher_Final**.
35   - The output of **OH_CryptoSymCipher_Final** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data.
36
377. Use [OH_CryptoSymCipherParams_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_create) to create a **Params** instance, and use [OH_CryptoSymCipherParams_SetParam](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_setparam) to set **authTag** as the authentication information for decryption.
38
39   In CCM mode, extract the last 12 bytes from the encrypted data as the authentication information for initializing the **Cipher** instance in decryption. In the example, **authTag** is of 12 bytes.
40
418. Use [OH_CryptoSymKeyGenerator_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_destroy), [OH_CryptoSymCipher_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_destroy), and [OH_CryptoSymCipherParams_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_destroy) to destroy the instances created.
42
43
44**Decryption**
45
46
471. Use [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_init) to initialize the **Cipher** instance. Specifically, set **mode** to **CRYPTO_DECRYPT_MODE**, and specify the key for decryption (**OH_CryptoSymKey**) and the decryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CCM mode.
48
492. Use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the plaintext.
50
51
52**Example **
53
54```c++
55#include "CryptoArchitectureKit/crypto_common.h"
56#include "CryptoArchitectureKit/crypto_sym_cipher.h"
57#include <string.h>
58
59static OH_Crypto_ErrCode doTestAesCcm()
60{
61    OH_CryptoSymKeyGenerator *genCtx = nullptr;
62    OH_CryptoSymCipher *encCtx = nullptr;
63    OH_CryptoSymCipher *decCtx = nullptr;
64    OH_CryptoSymKey *keyCtx = nullptr;
65    OH_CryptoSymCipherParams *params = nullptr;
66
67    Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0};
68    Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0};
69
70    uint8_t aad[8] = {1, 2, 3, 4, 5, 6, 7, 8};
71    uint8_t tag[12] = {0};
72    uint8_t iv[7] = {1, 2, 4, 12, 3, 4, 2}; // iv is generated from an array of secure random numbers.
73    Crypto_DataBlob ivData = {.data = iv, .len = sizeof(iv)};
74    Crypto_DataBlob aadData = {.data = aad, .len = sizeof(aad)};
75    Crypto_DataBlob tagData = {.data = tag, .len = sizeof(tag)};
76    Crypto_DataBlob tagOutPut = {.data = nullptr, .len = 0};
77    char *plainText = const_cast<char *>("this is test!");
78    Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
79    // Generate a symmetric key.
80    OH_Crypto_ErrCode ret;
81    ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx);
82    if (ret != CRYPTO_SUCCESS) {
83        goto end;
84    }
85    ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
86    if (ret != CRYPTO_SUCCESS) {
87        goto end;
88    }
89
90    // Set parameters.
91    ret = OH_CryptoSymCipherParams_Create(&params);
92    if (ret != CRYPTO_SUCCESS) {
93        goto end;
94    }
95    ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivData);
96    if (ret != CRYPTO_SUCCESS) {
97        goto end;
98    }
99    ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadData);
100    if (ret != CRYPTO_SUCCESS) {
101        goto end;
102    }
103    ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagData);
104    if (ret != CRYPTO_SUCCESS) {
105        goto end;
106    }
107
108    // Encrypt data.
109    ret = OH_CryptoSymCipher_Create("AES128|CCM", &encCtx);
110    if (ret != CRYPTO_SUCCESS) {
111        goto end;
112    }
113    ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params);
114    if (ret != CRYPTO_SUCCESS) {
115        goto end;
116    }
117    ret = OH_CryptoSymCipher_Update(encCtx, &msgBlob, &outUpdate);
118    if (ret != CRYPTO_SUCCESS) {
119        goto end;
120    }
121    ret = OH_CryptoSymCipher_Final(encCtx, nullptr, &tagOutPut);
122    if (ret != CRYPTO_SUCCESS) {
123        goto end;
124    }
125
126    // Decrypt data.
127    ret = OH_CryptoSymCipher_Create("AES128|CCM", &decCtx);
128    if (ret != CRYPTO_SUCCESS) {
129        goto end;
130    }
131    ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagOutPut);
132    if (ret != CRYPTO_SUCCESS) {
133        goto end;
134    }
135    ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params);
136    if (ret != CRYPTO_SUCCESS) {
137        goto end;
138    }
139    ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate);
140    if (ret != CRYPTO_SUCCESS) {
141        goto end;
142    }
143
144end:
145    OH_CryptoSymCipherParams_Destroy(params);
146    OH_CryptoSymCipher_Destroy(encCtx);
147    OH_CryptoSymCipher_Destroy(decCtx);
148    OH_CryptoSymKeyGenerator_Destroy(genCtx);
149    OH_CryptoSymKey_Destroy(keyCtx);
150    OH_Crypto_FreeDataBlob(&outUpdate);
151    OH_Crypto_FreeDataBlob(&decUpdate);
152    OH_Crypto_FreeDataBlob(&tagOutPut);
153    return ret;
154}
155```
156