Class desfire::cmac_provider

Class Documentation

class cmac_provider

Class tasked with computing CMACs using a crypto implementation.

CMAC codes are actually used only for more modern ciphers, like 3DES and AES128, but in principle can be computed on any crypto implementation. This is used internally by crypto_aes_base and crypto_3k3des_base.

Public Types

using mac_t = mlab::tagged_array<mac_tag, 8>

All CMAC codes are 8 bytes long.

Public Functions

inline cmac_provider(std::size_t block_size, std::uint8_t last_byte_xor)

Initialize a new CMAC provider.

You must call initialize_subkeys before compute_cmac can be used.

Parameters:
inline cmac_keychain const &keychain() const

Returns the keychain that holds the keys used for computing a CMAC.

void initialize_subkeys(crypto &crypto)

Computes the subkeys that will be used for compute_cmac.

You must call this method before using compute_cmac, otherwise the subkeys used in the CMAC will be zero-initialized and this will not only compute an incorrect CMAC, but it will also mangle the initialization vector, invalidating the whole session.

This method is identical to cmac_keychain::initialize_subkeys.

mac_t compute_cmac(crypto &crypto, range<std::uint8_t*> iv, range<std::uint8_t const*> data)

Compute a CMAC on the given range of data.

Make sure that the subkeys are initialized with initialize_subkeys before calling. This method performs the following operations:

  1. Pads data with 80 00 .. 00.

  2. XORs the last block with the appropriate key, depending on whether it was padded or not.

  3. Calls crypto::do_crypto with crypto_operation::mac on the resulting data together with iv.

  4. The first 8 bytes of the resulting iv are the CMAC that is returned.

Parameters:
  • crypto – Cryptographic implementation to use for deriving the keys. Make sure that the block size matches to what used in the constructor (i.e. cmac_keychain::block_size).

  • iv – Initialization vector to use. This method passes the initialization vector to the method crypto::do_crypto, therefore upon exit it is modified accordingly (and should contain the resulting initialization vector state after the cryptographic operation).

  • data – Data to compute the CMAC on.

Returns:

A 8-byte message authentication code.