执行程序
./media_test 0 xx.h264 xx.txt xx.pem
//软件初始化
SSL_init_crypto()
//硬件初始化
HwInit();
//码流加密
//1. 创建句柄
MediaCipherCreate()
//2. 设置参数
MediaCipherEncSetParam()
//3. 更新Vkek
MediaCipherEncUpdateVkek()
//4. 更新签名私钥
MediaCipherEncUpdatePrikey()
//5. 打开媒体文件
open()
//6. 读文件
read()
//7. 加密处理
MediaCipherEncProcess(encHandle, &tIn, &tOut, NULL)
//8. 关闭文件、句柄
//支持的视频类型
typedef enum EExtmediaType
{
XX_EXT_UNKNOW_VIDEO,
XX_EXT_H264,
XX_EXT_H265,
XX_EXT_SVAC2,
} EExtmediaType;
//帧类型
typedef enum EExtFrameType
{
XX_EXT_UNKNOW_FRAME,
XX_EXT_I_FRAME,
XX_EXT_NOT_I_FRAME,
} EExtFrameType;
//帧 数据结构
typedef struct tagFrameData
{
EExtmediaType m_emediaType; //视频类型
unsigned char *m_pbyFrameBuff; //帧缓冲区
unsigned int m_dwFrameLen; //帧长度
EExtFrameType m_eFrameType; //帧类型
} TFrameData;
<
TFrameData tIn, tOut;
memset(&tIn, 0, sizeof(tIn));
tIn.m_pbyFrameBuff = malloc(2 * 1024 *1024);
memse(&tOut, 0, sizeof(tOut));
tOut.m_pbyFrameBuff = malloc(2*1024*1024);
void *encHandle;
encHandle = MediaCipherCreate(&tCreateParam);
>
// MediaCipherCreate()创建句柄;返回结果是结构体类型 struct media_cipher *handle;
//VKEK结构体
typedef struct tagExtVkekInfo
{
unsigned char m_abyVkek[17];
unsigned int m_dwVkekLen;
unsigned char m_abyVesion[33];
unsgined int m_dwVesionLen;
}TExtVkekInfom;
// 公钥私钥结构
typedef struct tagPKey
{
unsigned char *m_pbyPkeyBuff;
unsigned int m_dwPKeyLen;
EASymmetricCrypto m_eCryptoType;
EKeyType m_eKeyType;
EDataType m_eDataType;
char *m_pbyPassword;
}TPKey;
//非对称算法类型
typedef enum EASymmetricCrypto
{
XX_EXT_RSA=0x1000,
XX_EXT_SM2=0x2000,
} EASymmetricCrypto;
//非对称密钥类型
typedef enum EKeyType
{
XX_EXT_KEY_PRIKEY,
XX_EXT_KEY_PUBKEY,
XX_EXT_KEY_PUBKEY_REQ,
XX_EXT_KEY_CERT,
}EKeyType;
//非对称密钥数据类型
typedef enum EDataType
{
XX_EXT_KEY_FILE_NAME,
XX_EXT_KEY_FILE_DATA,
XX_EXT_KEY_SRTING,
XX_EXT_KEY_ID_IN_HARDWARE,
}EDataType;
//对称加密算法枚举
typedef enum ESymmetricCrypto
{
XX_EXT_SM1_ECB,
XX_EXT_SM1_CBC,
XX_EXT_SM1_CFB,
XX_EXT_SM1_OFB,
XX_EXT_SM4_ECB,
XX_EXT_SM4_CBC,
XX_EXT_SM4_CFB,
XX_EXT_SM4_OFB,
XX_EXT_ZUC,
}ESymmetricCrypto;
// 散列算法枚举
typedef enum EHashAlgorithm
{
XX_EXT_SM3 = 0x10000,
XX_EXT_SHA1 = 0x20000,
XX_EXT_SHA256 = 0x30000,
} EHashAlgorithm;
// MediaCipher加密用参数
typedef struct tagMediaCipherEncParam
{
int m_bEncryptFlag;
ESymmetricCrypto m_eEncryptType;
int m_bAuthFlag;
EASymmetricCrypto m_eAuthType;
EHashAlgorithm m_eHashType;
int m_bHashOnlyIDR;
unsigned int m_dwHashPeriod;
} TMediaCipherEncParam;
// MediaCipher解密用参数
typedef struct tagMediaCipherDecParam
{
int m_bAuthFlag;
}TMediaCipherDecParam;
struct hash_cache
{
unsigned char dat[32];
unsigned int len;
};
//安全参数集信息
typedef struct tagSecurityParam
{
unsigned char m_abyVesion[33];
unsigned int m_dwVesionLen;
unsigned char m_abyCameraIdc[20];
unsigned char m_abyCameraId[21];
TMediaCipherEncParam m_tParam;
} TSecurityParam;
typedef int ( *PFSECURITYPARAMCB)( TSecurityParam *ptParam, void* pvContext);
//nal信息
struct nal_info
{
unsigned char *start;
unsigned int nal_len;
int encryption_idc;
int authentication_idc;
int nal_type;
};
//句柄结构体
struct media_cipher
{
int magic;
int mode;
int id;
struct nal_info nal[100];
unsigned int nal_num;
int engine_id_rand;
int engine_id_md;
int engine_id_cipher;
int engine_id_pkey;
EExtMediaType media_type;
void* md_ctx;
void* cipher_ctx;
void* cipher_ctx_evek;
void* pkey_ctx;
unsigned char vek[16];
unsigned char evek[16];
unsigned int vek_len;
unsigned char iv[16];
unsigned int iv_len;
//vkek
TExtVkekInfom vkek;
TExtVkekInfom new_vkek_flag;
int new_vkek_flag;
int has_vkek;
TExtVkekInfom vkek_list[64];
int list_num;
int list_header;
//iv
int new_iv_flag;
unsigned char new_iv[16];
unsigned int new_iv_len;
//enc
int gop_count;
int vek_update_period;
int iv_flag;
int vek_flag;
//安全参数集
int encrypt_flag;
ESymmetricCrypto encrypt_type;
ESymmetricCrypto encrypt_type;
int auth_flag;
EASymmetricCrypto auth_type;
EHashAlgorithm hash_type;
int hash_only_IDR;
int hash_period;
int hash_count;
int new_param_flag;
TMediaCipherEncParam new_enc_param;
TMediaCipherDecParam new_dec_param;
int camera_idc_flag;
unsigned char camera_idc[20];
unsigned char camera_id[21];
TPKey key;
unsigned char key_buff[4096];
char key_passwd[128];
//time
unsigned char hour;
unsigned char minute;
unsigned char second;
//sign
unsigned char *b64_buff;
unsigned char b64_len;
unsigned char *sign_buff;
unsigned char sign_len;
int sign_frame_num;
int sign_data_flag;
unsigned char *hash_buff;
unsigned char *tmp;
unsigned char hash_len;
unsigned char find_auth_set;
int user_auth_flag;
//for dec verify
int frame_num;
sturct hash_cache *cache;
//dec sps callback
PFSECURITYPARAMCB spc_callback_fun;
void *sps_callback_content;
//debug log
int fix_vek, fix_iv;
int medianet_using;
int in_len, out_len;
int save_in, save_out;
int save_in_fd, save_out_fd;
FILE *inLenStream, *outLenStream;
unsigned int bypass_count, success_count, in_count, idr_count, vid_width, vid_height;
unsigned int err1_count, err2_count, err3_count, err4_count, err5_count;
int create_hash_nal, hash_nal_count;
};
MediaCipherEncProcess(encHandle, &tIn, &tOut, NULL) //加密
unsigned int MediaCipherEncProcess(MCIPHER_HANDLE pHandle, TFrameData *ptIn, TFrameData *ptOut, TFrameData *ptInExt)
{
int ret;
struct media_cipher *handle = (struct media_cipher *)pHandle;
CHECK_ENC_HANDLE(handle);
ret = mediaCipherEncProcess(pHandle, ptIn, ptOut, ptInExt);
return ret;
}
//加密处理函数
static unsigned int mediaCipherEncProcess(MCIPHER_HANDLE pHandle, TFrameData *ptIn, TFrameData *ptOut, TFrameData *ptInExt)
{
}