1# HMAC (C/C++) 2 3 4A Hash-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). 5 6## Add the dynamic library in the CMake script. 7```txt 8 target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 9``` 10 11## How to Develop 12 13**Key Generation** 14 151. Set the key alias. 16 172. Initialize the key property set. 18 193. Use [OH_Huks_GenerateKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_generatekeyitem) to generate a key. For details about the HMAC specifications, see [Key Generation](huks-key-generation-overview.md#supported-algorithms). 20 21You can also import a key. For details about the supported algorithms, see [Supported Algorithms](huks-key-import-overview.md#supported-algorithms). 22 23**HMAC Generation** 24 251. Obtain the key alias. 26 272. Obtains the data to be calculated. 28 293. Use [OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset) to configure algorithm parameters. 30 31 324. Use [OH_Huks_InitSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_initsession) to initialize a key session. The session handle is returned after the initialization. 33 345. Use [OH_Huks_FinishSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_finishsession) to obtain the hashed data. 35 36```c++ 37#include "huks/native_huks_api.h" 38#include "huks/native_huks_param.h" 39#include <string.h> 40OH_Huks_Result InitParamSet( 41 struct OH_Huks_ParamSet **paramSet, 42 const struct OH_Huks_Param *params, 43 uint32_t paramCount) 44{ 45 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 46 if (ret.errorCode != OH_HUKS_SUCCESS) { 47 return ret; 48 } 49 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 50 if (ret.errorCode != OH_HUKS_SUCCESS) { 51 OH_Huks_FreeParamSet(paramSet); 52 return ret; 53 } 54 ret = OH_Huks_BuildParamSet(paramSet); 55 if (ret.errorCode != OH_HUKS_SUCCESS) { 56 OH_Huks_FreeParamSet(paramSet); 57 return ret; 58 } 59 return ret; 60} 61 62static struct OH_Huks_Param g_genHmacParams[] = { 63 { 64 .tag = OH_HUKS_TAG_ALGORITHM, 65 .uint32Param = OH_HUKS_ALG_HMAC 66 }, { 67 .tag = OH_HUKS_TAG_PURPOSE, 68 .uint32Param = OH_HUKS_KEY_PURPOSE_MAC 69 }, { 70 .tag = OH_HUKS_TAG_KEY_SIZE, 71 .uint32Param = OH_HUKS_AES_KEY_SIZE_256 72 }, { 73 .tag = OH_HUKS_TAG_DIGEST, 74 .uint32Param = OH_HUKS_DIGEST_SHA384 75 } 76}; 77 78static const uint32_t HMAC_COMMON_SIZE = 1024; 79OH_Huks_Result HksHmacTest( 80 const struct OH_Huks_Blob *keyAlias, 81 const struct OH_Huks_ParamSet *hmacParamSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *hashText) 82{ 83 uint8_t handleE[sizeof(uint64_t)] = {0}; 84 struct OH_Huks_Blob handle = {sizeof(uint64_t), handleE}; 85 OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, hmacParamSet, &handle, nullptr); 86 if (ret.errorCode != OH_HUKS_SUCCESS) { 87 return ret; 88 } 89 ret = OH_Huks_FinishSession(&handle, hmacParamSet, inData, hashText); 90 return ret; 91} 92 93static napi_value HmacKey(napi_env env, napi_callback_info info) 94{ 95 /* 1. Generate Key */ 96 /* 97 * Simulate the key generation scenario. 98 * 1.1. Set the key alias. 99 */ 100 char tmpKeyAlias[] = "test_hmac"; 101 struct OH_Huks_Blob keyAlias = { (uint32_t)strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias }; 102 struct OH_Huks_ParamSet *hmacParamSet = nullptr; 103 OH_Huks_Result ohResult; 104 do { 105 /* 106 * 1.2. Obtain the parameters for key generation. 107 */ 108 ohResult = InitParamSet(&hmacParamSet, g_genHmacParams, sizeof(g_genHmacParams) / sizeof(OH_Huks_Param)); 109 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 110 break; 111 } 112 /* 113 * 1.3. Call generateKeyItem to generate a key. 114 */ 115 ohResult = OH_Huks_GenerateKeyItem(&keyAlias, hmacParamSet, nullptr); 116 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 117 break; 118 } 119 /* 2. HMAC */ 120 /* 121 * Simulate a hash scenario. 122 * 2.1. Obtain the key alias. 123 */ 124 /* 125 * 2.2. Obtain the data to be hashed. 126 */ 127 char tmpInData[] = "HMAC_MAC_INDATA_1"; 128 struct OH_Huks_Blob inData = { (uint32_t)strlen(tmpInData), (uint8_t *)tmpInData }; 129 uint8_t cipher[HMAC_COMMON_SIZE] = {0}; 130 struct OH_Huks_Blob hashText = {HMAC_COMMON_SIZE, cipher}; 131 /* 132 * 2.3. Call initSession to obtain a session handle. 133 */ 134 /* 135 * 2.4. Call finishSession to obtain the hashed data. 136 */ 137 ohResult = HksHmacTest(&keyAlias, hmacParamSet, &inData, &hashText); 138 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 139 break; 140 } 141 } while (0); 142 OH_Huks_FreeParamSet(&hmacParamSet); 143} 144``` 145