* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef IOTC_AES_H
#define IOTC_AES_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define IOTC_AES_CBC_IV_LEN 16
#define IOTC_AES_GCM_TAG_MAX_LEN 16
#define IOTC_AES_GCM_TAG_MIN_LEN 4
#define IOTC_AES_CCM_IV_MAX_LEN 13
#define IOTC_AES_CCM_IV_MIN_LEN 7
#define IOTC_AES_CCM_TAG_MAX_LEN 16
#define IOTC_AES_CCM_TAG_MIN_LEN 4
enum IotcAesKeyByteLen {
IOTC_AES_128_KEY_BYTE_LEN = 16,
IOTC_AES_192_KEY_BYTE_LEN = 24,
IOTC_AES_256_KEY_BYTE_LEN = 32,
};
typedef struct {
const uint8_t *key;
uint32_t keyLen;
const uint8_t *iv;
uint32_t ivLen;
const uint8_t *add;
uint32_t addLen;
const uint8_t *data;
uint32_t dataLen;
} IotcAesGcmParam;
typedef enum {
ADAPTER_PADDING_PKCS7 = 0,
ADAPTER_PADDING_ONE_AND_ZEROS,
ADAPTER_PADDING_ZEROS_AND_LEN,
ADAPTER_PADDING_ZEROS,
ADAPTER_PADDING_NONE,
} IotcPaddingMode;
typedef struct {
IotcPaddingMode mode;
const uint8_t *key;
uint32_t keyLen;
const uint8_t *iv;
const uint8_t *data;
uint32_t dataLen;
} IotcAesCbcParam;
typedef struct {
const uint8_t *key;
uint32_t keyLen;
const uint8_t *iv;
uint32_t ivLen;
const uint8_t *add;
uint32_t addLen;
const uint8_t *data;
uint32_t dataLen;
} IotcAesCcmParam;
* @brief AES-GCM加密
*
* @param param [IN] AES-GCM加密必要的参数
* @param tag [OUT] 消息验证码输出缓冲区,可写长度至少为tagLen
* @param tagLen [IN] 消息验证码缓冲区长度,范围为[4,16]
* @param buf [OUT] 加密数据输出缓冲区,可写长度至少为param->dataLen
* @return 0成功,非0失败
*/
int32_t IotcAesGcmEncrypt(const IotcAesGcmParam *param, uint8_t *tag, uint32_t tagLen, uint8_t *buf);
* @brief AES-GCM解密并校验消息验证码
*
* @param param [IN] AES-GCM解密必要的参数
* @param tag [OUT] 消息验证码,可读长度至少为tagLen
* @param tagLen [IN] 消息验证码长度,范围应为[4,16]
* @param buf [OUT] 解密密数据输出缓冲区,可写长度至少为param->dataLen
* @return 0成功,非0失败
*/
int32_t IotcAesGcmDecrypt(const IotcAesGcmParam *param, const uint8_t *tag, uint32_t tagLen, uint8_t *buf);
* @brief AES-CBC加密
*
* @param param [IN] AES-CBC加密必要的参数
* @param buf [OUT] 加密数据输出缓冲区,可写长度至少为bufLen
* @param bufLen [IN/OUT] 输入为输出缓冲区长度,输出为增加padding后的数据长度
* @return 0成功,非0失败
*/
int32_t IotcAesCbcEncrypt(const IotcAesCbcParam *param, uint8_t *buf, uint32_t *bufLen);
* @brief AES-CBC解密
*
* @param param [IN] AES-CBC解密必要的参数
* @param buf [OUT] 解密数据输出缓冲区,可写长度至少为param->dataLen
* @param bufLen [IN/OUT] 输入为输出缓冲区长度,输出为去除padding后的数据长度
* @return 0成功,非0失败
*/
int32_t IotcAesCbcDecrypt(const IotcAesCbcParam *param, uint8_t *buf, uint32_t *bufLen);
* @brief AES-CCM解密并校验消息验证码
*
* @param param [IN] AES-CCM解密必要的参数
* @param tag [OUT] 消息验证码,可读长度至少为tagLen
* @param tagLen [IN] 消息验证码长度,范围应为[4,16]
* @param buf [OUT] 解密密数据输出缓冲区,可写长度至少为param->dataLen
* @return 0成功,非0失败
*/
int32_t IotcAesCcmDecrypt(const IotcAesCcmParam *param, const uint8_t *tag, uint32_t tagLen, uint8_t *buf);
* @brief AES-CCM加密
*
* @param param [IN] AES-CCM加密必要的参数
* @param tag [OUT] 消息验证码输入缓冲区,可写长度至少为tagLen
* @param tagLen [IN] 消息验证码缓冲区长度,范围应为[4,16]
* @param buf [OUT] 加密数据输出缓冲区,可写长度至少为param->dataLen
* @return 0成功,非0失败
*/
int32_t IotcAesCcmEncrypt(const IotcAesCcmParam *param, uint8_t *tag, uint32_t tagLen, uint8_t *buf);
#ifdef __cplusplus
}
#endif
#endif