231 lines
7.7 KiB
C
Executable File
231 lines
7.7 KiB
C
Executable File
/* 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 返回是否解密成功,0:不成功,1:成功
|
||
*/
|
||
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、为1,bit2、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:16倍数、AES192:24倍数、AES256:32倍数。
|
||
* 输出参数: pPlainText -- 解密出来的明文
|
||
* 返回值: 返回解密后的数据长度。
|
||
*****************************************************************************/
|
||
unsigned int AESDecrypt(AESInfo_t *aesInfoP, unsigned char *pPlainText, const unsigned char *pCipherText, unsigned int dataLen);
|
||
|
||
#endif
|
||
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
|
||
#endif
|
||
#endif
|
||
|