目录

CMAC (C/C++)

Cipher-based Message Authentication Code (CMAC) is a type of message authentication code (MAC) based on block ciphers. Currently, it supports 3DES.

This feature is only supported on wearables.

How to Develop

Key Generation

  1. Obtain the parameters for key generation.

  2. Call OH_Huks_GenerateKeyItem to generate a key. The supported key length is 128 bits.

In addition, you can import an existing key by referring to Supported Algorithms.

CMAC Execution

  1. Call OH_Huks_InitParamSet to obtain algorithm parameter settings.

  2. Call OH_Huks_InitSession and OH_Huks_FinishSession to calculate the MAC value.

#include "huks/native_huks_api.h"
#include "huks/native_huks_param.h"
#include "huks/native_huks_type.h"
#include "napi/native_api.h"
#include <string.h>

static const uint32_t CMAC_COMMON_SIZE = 8;
static const uint32_t IV_SIZE = 8;
static uint8_t IV[IV_SIZE] = { 0 };

OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, uint32_t paramCount)
{
    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
    if (ret.errorCode != OH_HUKS_SUCCESS) {
        return ret;
    }
    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
    if (ret.errorCode != OH_HUKS_SUCCESS) {
        OH_Huks_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH_Huks_BuildParamSet(paramSet);
    if (ret.errorCode != OH_HUKS_SUCCESS) {
        OH_Huks_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

static struct OH_Huks_Param g_genParams[] = {
    {
        .tag = OH_HUKS_TAG_ALGORITHM,
        .uint32Param = OH_HUKS_ALG_3DES
    }, {
        .tag = OH_HUKS_TAG_KEY_SIZE,
        .uint32Param = OH_HUKS_3DES_KEY_SIZE_128
    }, {
        .tag = OH_HUKS_TAG_PURPOSE,
        .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
    }
};

static struct OH_Huks_Param g_cmacParams[] = {
    {
        .tag = OH_HUKS_TAG_ALGORITHM,
        .uint32Param = OH_HUKS_ALG_CMAC
    }, {
        .tag = OH_HUKS_TAG_KEY_SIZE,
        .uint32Param = OH_HUKS_3DES_KEY_SIZE_128
    }, {
        .tag = OH_HUKS_TAG_PURPOSE,
        .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
    }, {
        .tag = OH_HUKS_TAG_BLOCK_MODE,
        .uint32Param = OH_HUKS_MODE_CBC
    }, {
        .tag = OH_HUKS_TAG_PADDING,
        .uint32Param = OH_HUKS_PADDING_ISO_IEC_9797_1
    }, {
        .tag = OH_HUKS_TAG_IV,
        .blob = {
            .size = IV_SIZE,
            .data = (uint8_t *)IV
        }
    }
};

OH_Huks_Result HksCmacTest(const struct OH_Huks_Blob *keyAlias, const struct OH_Huks_ParamSet *cmacParamSet,
    const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
{
    uint8_t handleE[sizeof(uint64_t)] = {0};
    struct OH_Huks_Blob handle = {sizeof(uint64_t), handleE};
    OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, cmacParamSet, &handle, nullptr);
    if (ret.errorCode != OH_HUKS_SUCCESS) {
        return ret;
    }
    ret = OH_Huks_FinishSession(&handle, cmacParamSet, inData, outData);
    return ret;
}

static napi_value CmacKey(napi_env env, napi_callback_info info)
{
    char tmpKeyAlias[] = "test_cmac";
    struct OH_Huks_Blob keyAlias = { (uint32_t)strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
    struct OH_Huks_ParamSet *genParamSet = nullptr;
    struct OH_Huks_ParamSet *cmacParamSet = nullptr;
    OH_Huks_Result ohResult;
    do {
      /*       * 1.1 Obtain the parameters for key generation.       */
        ohResult = InitParamSet(&genParamSet, g_genParams, sizeof(g_genParams) / sizeof(OH_Huks_Param));
        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
            break;
        }
       /*               * 1.2 Call OH_Huks_GenerateKeyItem.        */
        ohResult = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
            break;
        }
       /*        * 2.1. Obtain CMAC algorithm parameter settings.        */
        char tmpInData[] = "CMAC_INDATA";
        struct OH_Huks_Blob inData = { (uint32_t)strlen(tmpInData), (uint8_t *)tmpInData };
        uint8_t mac[CMAC_COMMON_SIZE] = { 0 };
        struct OH_Huks_Blob macData= {CMAC_COMMON_SIZE, mac};
        ohResult = InitParamSet(&cmacParamSet, g_cmacParams, sizeof(g_cmacParams) / sizeof(OH_Huks_Param));
        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
            break;
        }
       /*        * 2.2 Call initSession and finishSession to calculate the MAC value.        */
        ohResult = HksCmacTest(&keyAlias, cmacParamSet, &inData, &macData);
        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
            break;
        }
    } while (0);
    OH_Huks_FreeParamSet(&genParamSet);
    OH_Huks_FreeParamSet(&cmacParamSet);
    
    napi_value ret;
    napi_create_int32(env, ohResult.errorCode, &ret);
    return ret;
}