目录

Generating a CMAC (ArkTS)

The cipher-based message authentication code (CMAC) uses a block cipher (such as AES) and a key to generate an authentication code, which verifies that a message has not been alerted during transmission.

How to Develop

During the CMAC operation, you can pass in all the data at a time or pass in data by segment. The same data will produce the same result no matter how the data is passed. Use the appropriate method based on the data size.

The following provides examples of CMAC operations with different data passing methods.

[h2]Generating a CMAC by Passing in Full Data

  1. Call cryptoFramework.createMac with the MAC algorithm set to CMAC and symmetric algorithm set to AES128 to create a Mac instance.

  2. Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a symmetric key (SymKey) with the AES128 algorithm.

    For details, see Converting Binary Data into a Symmetric Key.

  3. Call Mac.init to initialize the Mac instance using the shared symmetric key (SymKey).

  4. Call Mac.update to pass in the full data. The amount of data to be passed in by a single Mac.update() call is not limited.

  5. Call Mac.doFinal to generate a MAC.

  6. Call Mac.getMacLength to obtain the length of the MAC, in bytes.

[h2]Generating a CMAC by Passing In Data by Segment

  1. Call cryptoFramework.createMac with the MAC algorithm set to CMAC and the symmetric algorithm set to AES128 to generate a message authentication code instance (Mac).

  2. Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a symmetric key (SymKey) with the AES128 algorithm.

    For details, see Converting Binary Data into a Symmetric Key.

  3. Call Mac.init to initialize the Mac instance using the shared symmetric key (SymKey).

  4. Call Mac.update multiple times to pass in 20 bytes each time.

  5. Call Mac.doFinal to generate a MAC.

  6. Call Mac.getMacLength to obtain the length of the MAC, in bytes.