AES-128 CMAC calculation with PSA

Hi, I am trying to calculate a AES-128 CMAC using the PSA crypto libraries but running into some issues.

Are there any sample code that I can reference to implement this?

Currently I am doing the following:

psa_key_id_t key_id;
psa_key_attributes_t keyAttributes = PSA_KEY_ATTRIBUTES_INIT;
size_t mac_length;

psa_set_key_usage_flags ( &keyAttributes, ( PSA_KEY_USAGE_SIGN_MESSAGE ) );
psa_set_key_lifetime    ( &keyAttributes, PSA_KEY_LIFETIME_VOLATILE );
psa_set_key_algorithm   ( &keyAttributes, PSA_ALG_CMAC );
psa_set_key_type        ( &keyAttributes, PSA_KEY_TYPE_AES );
psa_set_key_bits        ( &keyAttributes, 128 );

psa_import_key( &keyAttributes, inKey, 16, &key_id );

psa_mac_compute(
        key_id,
        PSA_ALG_CMAC,
        inData,
        dataLen,
        (uint8_t *) outData,
        16,
        &mac_length);

where:

  • inKey is the raw key in bytes
  • inData is the input message
  • outData is the buffer where the MAC value is to be written
  • dataLen is the length of the input message

For some reason I get the following message printed to the console: * buffer overflow detected *

Is this the right way to calculate the CMAC? Is there a different that is recommended?

Thanks

Related