目录

Calculating the MAC

Uses the 3DES or DES encryption algorithm to calculate the MAC.

Start MAC calculation from step 1 or 2 as follows:

  1. Calculate the MAC for line packets.
    1. Create an initial variable by concatenating a 4-byte random number and 00 00 00 00.
    2. Perform the calculation.

      Concatenate the following data to form a data block:

      • CLA, INS, P1, P2, and Lc

      Lc is the actual length of the data field in the instruction sent by the device, including the length of the 4-byte MAC.

      • Plaintext or ciphertext data contained in the data field in the instruction.
  2. Calculate the MAC for fixed data of a specific app.
    1. Use the 8-byte 00 00 00 00 00 00 00 00 as the initial variable.
    2. Perform the calculation.

      Concatenate the fixed data of a specific app to form a data block.

  3. Split the data block generated into smaller 8-byte data blocks, and number them as D1, D2, D3, D4, and so on. Note that the last block may contain 1 to 8 bytes.
  4. Pad the last data block to 8 bytes.
    • If the last data block contains 8 bytes, you still need to add the hexadecimal number 80 00 00 00 00 00 00 00 to the end of the block.
    • If the last data block contains less than 8 bytes, add the hexadecimal number 80 to the end of the block. If the number of bytes is still less than 8, add the hexadecimal number 00 until the block contains 8 bytes.
  5. Encrypt the 8-byte data blocks using the application maintenance key (AMK) and 3DES algorithm, as shown in Figure 1.

    The number of 8-byte data blocks encrypted in this step depends on the length of the data block formed in step 1 or 2. MAC is the first four bytes from the left of the last 8-byte data block that is encrypted.

    Figure 1 SM-MAC algorithm using a double-length DEA key
    • I: input
    • : XOR operation
    • O: output
    • DEA (e): data encryption mode
    • D: data block
    • DEA(d): data decryption mode
    • KMA: left 8 bytes of the double-length key
    • KMB: right 8 bytes of the double-length key