LPT26x-HSF-4MB-Hilink_14.2..../application/ws63/hsf/hfcrypto.h

231 lines
7.7 KiB
C
Raw Normal View History

2025-05-13 22:00:58 +08:00
/* hfcrypto.h
*
* Copyright (C) 2017 ShangHai High-flying Electronics Technology Co.,Ltd.
*
* This file is part of HSF.
*
*/
#ifndef _HF_CRYPTO_H_
#define _HF_CRYPTO_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief encrypt data with aes (ecb mode/128bit key).
*
* @param[in] key: the key of AES
* data: the data want to encrypt
* data_len: the length of data
* @return[out] the length of encrypted data
* @see None.
* @note None.
*/
int HSF_API hfcrypto_aes_ecb_encrypt(const unsigned char *key, unsigned char *data, int data_len);
/**
* @brief decrypt data with aes (ecb mode/128bit key).
*
* @param[in] key: the key of AES
* data: the data want to decrypt
* data_len: the length of data
* @return[out] the length of decrypted data
* @see None.
* @note None.
*/
int HSF_API hfcrypto_aes_ecb_decrypt(const unsigned char *key, unsigned char *data, int data_len);
/**
* @brief encrypt data with aes (cbc mode/128bit key).
*
* @param[in] key: the key of AES
* iv: the initialization vector of AES
* data: the data want to encrypt
* data_len: the length of data
* @return[out] the length of encrypted data
* @see None.
* @note None.
*/
int HSF_API hfcrypto_aes_cbc_encrypt(const unsigned char *key, const unsigned char *iv, unsigned char *data, int data_len);
/**
* @brief decrypt data with aes (cbc mode/128bit key).
*
* @param[in] key: the key of AES
* iv: the initialization vector of AES
* data: the data want to decrypt
* data_len: the length of data
* @return[out] the length of decrypted data
* @see None.
* @note None.
*/
int HSF_API hfcrypto_aes_cbc_decrypt(const unsigned char *key, const unsigned char *iv, unsigned char *data, int data_len);
/**
* @brief calculate the MD5 of the data.
*
* @param[in] data: the data want to calculate MD5
* len: the length of data, in bytes
* digest: store MD5, must > 16 bytes
* @return[out] the length of MD5, in bytes
* @see None.
* @note None.
*/
int HSF_API hfcrypto_md5(const unsigned char *data, int len, unsigned char *digest);
/**
* @brief encode data as base64.
*
* @param[in] src: input byte array
* srclength: size of input byte array
* target: output byte array
* targsize: size of output byte array
* @return[out] returns the number of data bytes stored at the target, or -1 on error.
* @see None.
* @note encode data as base64, converts characters, three at a time,
* starting at src into four base64 characters in the target area
* until the entire input buffer is encoded.
*/
int HSF_API hfcrypto_base64_encode(char *src, uint32_t srclength, char *target, uint32_t targsize);
/**
* @brief decode Base64 data.
*
* @param[in] src: input byte array
* target: output byte array
* targsize: size of output byte array
* @return[out] returns the number of data bytes stored at the target, or -1 on error.
* @see None.
* @note decode Base64 data, skips all whitespace anywhere. converts
* characters, four at a time, starting at (or after) src from Base64
* numbers into three 8 bit bytes in the target area..
*/
int HSF_API hfcrypto_base64_decode(char *src, char *target, uint32_t targsize);
/**
* @brief rsa public key operation (1024bit key).
*
* @param[in] key: the key of RSA
* data: the data want to decrypt
* data_len: the length of data
* @return[out] the length of encrypted data
* @see None.
* @note None.
*/
// int HSF_API hfcrypto_rsa_public(const unsigned char *key, unsigned char *data, int data_len);
/**
* @brief HMAC-SHA1 algorithm.
*
* @param[in] key: the key of HMAC-SHA1
* key_len: the length of key
* data: the data want to calculate HMAC-SHA1
* data_len: the length of data
* mac: the hash (20 bytes)
* @return[out] HF_SUCCESS-successfully, other value is failed
* @see None.
* @note None.
*/
int HSF_API hfcrypto_hmac_sha1(const unsigned char *key, int key_len, const unsigned char *data, int data_len, unsigned char *mac);
/**
* @brief AES CBC模式解密
*
* @param input
* @param input_len
* @param output
* @param output_len
* @param key
* @param key_len
* @param iv
* @param iv_len
* @return uint8_t 01
*/
uint8_t aes_cbc_decipher(uint8_t *input, uint32_t input_len,
uint8_t *output, uint32_t output_len,
uint8_t *key, uint32_t key_len,
uint8_t *iv, uint32_t iv_len);
#if 1
#define Nb 4 //加解密数据块大小固定为4
// GF(2^8) 多项式
#define BPOLY 0x1B //x^4 + x^3 + x^1 + x^0= 从右边开始算bit0、bit1、bit3、bit4、为1bit2、bit5、bit6、bit7为0即00011011=0x1B
//加密类型对应的密匙长度单位bit
typedef enum {
AES128 = 128,
AES192 = 192,
AES256 = 256,
} AESType_t;
//加解密模式
typedef enum {
AES_MODE_ECB = 0, // 电子密码本模式
AES_MODE_CBC = 1, // 密码分组链接模式
} AESMode_t;
typedef struct {
int Nk; //用户不需要填充,密钥长度,单位字节, AES128:Nk=16、AES192:Nk=24、AES256:Nr=32
int Nr; //用户不需要填充,加密的轮数 AES128:Nr=10、AES192:Nr=12、AES256:Nr=14
AESType_t type;//用户需填充关联AESType_t
AESMode_t mode;//用户需填充关联AESMode_t
const void *key;//用户需填充,密匙
const void *pIV;//用户需填充,初始化向量, 当mode=AES_MODE_CBC时需要设置指向unsigned char IV[4*Nb];
//AES拓展密匙, 空间大小 AES128:4*Nb*(10+1):4*Nb*(12+1)、AES256:4*Nb*(14+1)
unsigned char expandKey[4*Nb*(14+1)];//用户不需要填充,[4*Nb*(Nr+1)]、这里按最大的AES256进行初始化
} AESInfo_t;
/*****************************************************************************
* AESInit
*
* aesInfoP --
*
*
*****************************************************************************/
void AESInit(AESInfo_t *aesInfoP);
/*****************************************************************************
* AESEncrypt
*
* aesInfoP -- key
* pPlainText -- dataLen字节
* dataLen --
* pCipherText --
*
*****************************************************************************/
unsigned int AESEncrypt(AESInfo_t *aesInfoP, const unsigned char *pPlainText, unsigned char *pCipherText, unsigned int dataLen);
/*****************************************************************************
* AESDecrypt
*
* aesInfoP -- key
* pCipherText --
* dataLen -- AES128:16AES192:24AES256:32
* pPlainText --
*
*****************************************************************************/
unsigned int AESDecrypt(AESInfo_t *aesInfoP, unsigned char *pPlainText, const unsigned char *pCipherText, unsigned int dataLen);
#endif
#ifdef __cplusplus
}
#endif
#endif