• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HMAC (C/C++)
2
3<!--Kit: Universal Keystore Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @wutiantian-gitee-->
6<!--Designer: @HighLowWorld-->
7<!--Tester: @wxy1234564846-->
8<!--Adviser: @zengyawen-->
9
10Hash-based Message Authentication Code (HMAC) is a specific type of message authentication code (MAC) involving a cryptographic has function and a secret cryptographic key. For details about the scenarios and supported algorithm specifications, see [HMAC Overview and Algorithm Specifications](huks-hmac-overview.md).
11
12## Add the dynamic library in the CMake script.
13```txt
14target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
15```
16
17## How to Develop
18
19**Key Generation**
20
211. Specify the key alias. For details about the naming rules, see [Key Generation Overview and Algorithm Specifications](huks-key-generation-overview.md).
22
232. Initialize the key property set.
24
253. Use [OH_Huks_GenerateKeyItem](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_generatekeyitem) to generate a key. For details about the HMAC specifications supported, see [Supported Algorithms](huks-key-generation-overview.md#supported-algorithms).
26
27You can also import a key. For details about the supported algorithms, see [Supported Algorithms](huks-key-import-overview.md#supported-algorithms).
28
29**HMAC Generation**
30
311. Obtain the key alias.
32
332. Obtains the data to be calculated.
34
353. Use [OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/capi-native-huks-param-h.md#oh_huks_initparamset) to set algorithm parameters.
36
374. Use [OH_Huks_InitSession](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_initsession) to initialize a key session and obtain the session handle.
38
395. Call [OH_Huks_FinishSession](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_finishsession) to finish the key session and obtain the hashed data.
40
41```c++
42#include "huks/native_huks_api.h"
43#include "huks/native_huks_param.h"
44#include "napi/native_api.h"
45#include <string.h>
46OH_Huks_Result InitParamSet(
47    struct OH_Huks_ParamSet **paramSet,
48    const struct OH_Huks_Param *params,
49    uint32_t paramCount)
50{
51    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
52    if (ret.errorCode != OH_HUKS_SUCCESS) {
53        return ret;
54    }
55    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
56    if (ret.errorCode != OH_HUKS_SUCCESS) {
57        OH_Huks_FreeParamSet(paramSet);
58        return ret;
59    }
60    ret = OH_Huks_BuildParamSet(paramSet);
61    if (ret.errorCode != OH_HUKS_SUCCESS) {
62        OH_Huks_FreeParamSet(paramSet);
63        return ret;
64    }
65    return ret;
66}
67
68static struct OH_Huks_Param g_genHmacParams[] = {
69    {
70        .tag = OH_HUKS_TAG_ALGORITHM,
71        .uint32Param = OH_HUKS_ALG_HMAC
72    }, {
73        .tag = OH_HUKS_TAG_PURPOSE,
74        .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
75    }, {
76        .tag = OH_HUKS_TAG_KEY_SIZE,
77        .uint32Param = OH_HUKS_AES_KEY_SIZE_256
78    }, {
79        .tag = OH_HUKS_TAG_DIGEST,
80        .uint32Param = OH_HUKS_DIGEST_SHA384
81    }
82};
83
84static const uint32_t HMAC_COMMON_SIZE = 1024;
85OH_Huks_Result HksHmacTest(
86        const struct OH_Huks_Blob *keyAlias,
87        const struct OH_Huks_ParamSet *hmacParamSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *hashText)
88{
89    uint8_t handleE[sizeof(uint64_t)] = {0};
90    struct OH_Huks_Blob handle = {sizeof(uint64_t), handleE};
91    OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, hmacParamSet, &handle, nullptr);
92    if (ret.errorCode != OH_HUKS_SUCCESS) {
93        return ret;
94    }
95    ret = OH_Huks_FinishSession(&handle, hmacParamSet, inData, hashText);
96    return ret;
97}
98
99static napi_value HmacKey(napi_env env, napi_callback_info info)
100{
101    /* 1. Generate Key */
102    /*
103    * Simulate the key generation scenario.
104    * 1.1. Set the key alias.
105    */
106    char tmpKeyAlias[] = "test_hmac";
107    struct OH_Huks_Blob keyAlias = { (uint32_t)strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
108    struct OH_Huks_ParamSet *hmacParamSet = nullptr;
109    OH_Huks_Result ohResult;
110    do {
111        /*
112        * 1.2. Obtain the parameters for key generation.
113        */
114        ohResult = InitParamSet(&hmacParamSet, g_genHmacParams, sizeof(g_genHmacParams) / sizeof(OH_Huks_Param));
115        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
116            break;
117        }
118        /*
119        * 1.3. Call generateKeyItem to generate a key.
120        */
121        ohResult = OH_Huks_GenerateKeyItem(&keyAlias, hmacParamSet, nullptr);
122        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
123            break;
124        }
125        /* 2. HMAC */
126        /*
127        * Simulate a hash scenario.
128        * 2.1. Obtain the key alias.
129        */
130        /*
131        * 2.2. Obtain the data to be hashed.
132        */
133        char tmpInData[] = "HMAC_MAC_INDATA_1";
134        struct OH_Huks_Blob inData = { (uint32_t)strlen(tmpInData), (uint8_t *)tmpInData };
135        uint8_t cipher[HMAC_COMMON_SIZE] = {0};
136        struct OH_Huks_Blob hashText = {HMAC_COMMON_SIZE, cipher};
137        /*
138        * 2.3. Call initSession to obtain a session handle.
139        */
140        /*
141        * 2.4. Call finishSession to obtain the hashed data.
142        */
143        ohResult = HksHmacTest(&keyAlias, hmacParamSet, &inData, &hashText);
144        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
145            break;
146        }
147    } while (0);
148    OH_Huks_FreeParamSet(&hmacParamSet);
149    napi_value ret;
150    napi_create_int32(env, ohResult.errorCode, &ret);
151    return ret;
152}
153```
154