crypto_mac.h

Overview

Define message authentication code (MAC) APIs.

Header file: <CryptoArchitectureKit/crypto_mac.h>

Library: libohcrypto.so

System capability: SystemCapability.Security.CryptoFramework

Since: 20

Related module: CryptoMacApi

Summary

Structs

Name typedef Keyword Description
OH_CryptoMac OH_CryptoMac Defines a struct for a MAC.

Enums

Name typedef Keyword Description
CryptoMac_ParamType CryptoMac_ParamType Defines the parameter type for the MAC algorithm.

Functions

Name Description
OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) Creates a MAC instance based on the given algorithm name.
Note: The created resource must be destroyed by calling OH_CryptoMac_Destroy.
OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType type, const Crypto_DataBlob *value) Sets MAC parameters.
OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key) Initializes a MAC instance using a symmetric key.
OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *in) Updates a MAC instance.
OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out) Finalizes a MAC operation.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.
OH_Crypto_ErrCode OH_CryptoMac_GetLength(OH_CryptoMac *ctx, uint32_t *length) Obtains the MAC length.
void OH_CryptoMac_Destroy(OH_CryptoMac *ctx) Destroys a MAC instance.

Enum Description

CryptoMac_ParamType

enum CryptoMac_ParamType

Description

Defines the parameter type for the MAC algorithm.

Since: 20

Enum Item Description
CRYPTO_MAC_DIGEST_NAME_STR = 0 Algorithm name of the digest function used by the HMAC, for example, SHA256.
CRYPTO_MAC_CIPHER_NAME_STR = 1 Name of the symmetric encryption algorithm used by the CMAC, for example, AES256.

Function Description

OH_CryptoMac_Create()

OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx)

Description

Creates a MAC instance based on the given algorithm name.
Note: The created resource must be destroyed by calling OH_CryptoMac_Destroy.

Since: 20

Parameters

Name Description
const char *algoName Pointer to the algorithm used to generate the MAC instance.
For example, HMAC or CMAC.
OH_CryptoMac **ctx MAC instance.

Returns

Type Description
OH_Crypto_ErrCode CRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERATION_ERROR: An error occurs when the API of a third-party algorithm library is called.

OH_CryptoMac_SetParam()

OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType type, const Crypto_DataBlob *value)

Description

Sets MAC parameters.

Since: 20

Parameters

Name Description
OH_CryptoMac *ctx MAC instance.
CryptoMac_ParamType type MAC parameter type.
const Crypto_DataBlob *value MAC parameters.

Returns

Type Description
OH_Crypto_ErrCode CRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERATION_ERROR: An error occurs when the API of a third-party algorithm library is called.

OH_CryptoMac_Init()

OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key)

Description

Initializes a MAC instance using a symmetric key.

Since: 20

Parameters

Name Description
OH_CryptoMac *ctx MAC instance.
const OH_CryptoSymKey *key Symmetric key obtained.

Returns

Type Description
OH_Crypto_ErrCode CRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERATION_ERROR: An error occurs when the API of a third-party algorithm library is called.

Reference

OH_CryptoMac_Update

OH_CryptoMac_Final

OH_CryptoMac_Update()

OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *in)

Description

Updates a MAC instance.

Since: 20

Parameters

Name Description
OH_CryptoMac *ctx MAC instance.
const Crypto_DataBlob *in Data to be updated.

Returns

Type Description
OH_Crypto_ErrCode CRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERATION_ERROR: An error occurs when the API of a third-party algorithm library is called.

Reference

OH_CryptoMac_Init

OH_CryptoMac_Final

OH_CryptoMac_Final()

OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out)

Description

Finalizes a MAC operation.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.

Since: 20

Parameters

Name Description
OH_CryptoMac *ctx MAC instance.
Crypto_DataBlob *out MAC value.

Returns

Type Description
OH_Crypto_ErrCode CRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERATION_ERROR: An error occurs when the API of a third-party algorithm library is called.

Reference

OH_CryptoMac_Init

OH_CryptoMac_Update

OH_CryptoMac_GetLength()

OH_Crypto_ErrCode OH_CryptoMac_GetLength(OH_CryptoMac *ctx, uint32_t *length)

Description

Obtains the MAC length.

Since: 20

Parameters

Name Description
OH_CryptoMac *ctx MAC instance.
uint32_t *length MAC length.

Returns

Type Description
OH_Crypto_ErrCode CRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERATION_ERROR: An error occurs when the API of a third-party algorithm library is called.

OH_CryptoMac_Destroy()

void OH_CryptoMac_Destroy(OH_CryptoMac *ctx)

Description

Destroys a MAC instance.

Since: 20

Parameters

Name Description
OH_CryptoMac *ctx MAC instance.