|
|
@@ -9,8 +9,6 @@
|
|
|
#include <iostream>
|
|
|
using namespace std;
|
|
|
|
|
|
-#include <stdio.h>
|
|
|
-
|
|
|
#ifndef RVC_MAX_HEADER_LEN
|
|
|
#define RVC_MAX_HEADER_LEN 512
|
|
|
#endif
|
|
|
@@ -33,7 +31,7 @@ using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
-static int encrypt_asf_file(char* poutfilename, char* phead, size_t uheaderlen, unsigned char* pstrkey, size_t ukeylen, FILE * pSrcFile, const filecryption_callback_t * pcallback)
|
|
|
+static int encrypt_asf_file(char* poutfilename, char* phead, uint32_t uheaderlen, unsigned char* pstrkey, uint32_t ukeylen, FILE * pSrcFile, const filecryption_callback_t * pcallback)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
|
|
|
@@ -96,7 +94,7 @@ static int encrypt_asf_file(char* poutfilename, char* phead, size_t uheaderlen,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int encrypt_mp4_file(char* poutfilename, char* phead, size_t uheaderlen, unsigned char* pstrkey, size_t ukeylen, FILE* pSrcFile, const filecryption_callback_t* pcallback)
|
|
|
+static int encrypt_mp4_file(char* poutfilename, char* phead, uint32_t uheaderlen, unsigned char* pstrkey, uint32_t ukeylen, FILE* pSrcFile, const filecryption_callback_t* pcallback)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
|
|
|
@@ -152,7 +150,7 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, size_t uheaderlen,
|
|
|
safe_log(pcallback, "after encode, moov box length is %u.", iencdatalen);
|
|
|
|
|
|
//3. 填充加密后的moov box长度
|
|
|
- fwrite(&iencdatalen, 1, sizeof(int32_t), pDestFile);
|
|
|
+ fwrite(&iencdatalen, 1, sizeof(uint32_t), pDestFile);
|
|
|
|
|
|
//4. 填充加密后的moov box内容
|
|
|
if (iencdatalen > 0) {
|
|
|
@@ -171,7 +169,7 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, size_t uheaderlen,
|
|
|
}
|
|
|
|
|
|
|
|
|
-int encryption_file(char* poutfile, size_t uoutlen, const char* pfilename, const filecryption_callback_t* pcallback, eRvcCryptionVersion eversion)
|
|
|
+int encryption_file(char* poutfile, uint32_t uoutlen, const char* pfilename, const filecryption_callback_t* pcallback, eRvcCryptionVersion eversion)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
|
|
|
@@ -180,14 +178,14 @@ int encryption_file(char* poutfile, size_t uoutlen, const char* pfilename, const
|
|
|
}
|
|
|
|
|
|
char pbuffer[RVC_MAX_HEADER_LEN] = {0};
|
|
|
- size_t uheaderlen = RVC_MAX_HEADER_LEN;
|
|
|
+ uint32_t uheaderlen = RVC_MAX_HEADER_LEN;
|
|
|
int ilen = constrcut_rvc_file_header(pbuffer, &uheaderlen, pfilename, pcallback, eversion);
|
|
|
if (0 != ilen){
|
|
|
return iret;
|
|
|
}
|
|
|
|
|
|
unsigned char strkey[SM4ENC_BLOCK_SIZE] = {0};
|
|
|
- size_t uindex = (RVC_FILE_HEADER_FLAG_LEN + sizeof(size_t));
|
|
|
+ uint32_t uindex = (RVC_FILE_HEADER_FLAG_LEN + sizeof(uint32_t));
|
|
|
if (get_key_from_header_info(strkey, SM4ENC_BLOCK_SIZE, pbuffer + uindex, uheaderlen - uindex, pcallback)){
|
|
|
return iret;
|
|
|
}
|
|
|
@@ -214,7 +212,7 @@ int encryption_file(char* poutfile, size_t uoutlen, const char* pfilename, const
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int decrypt_asf_file(char* poutfilename, FILE* pSrcFile, unsigned char* pstrkey, size_t ukeylen, const filecryption_callback_t* pcallback)
|
|
|
+static int decrypt_asf_file(char* poutfilename, FILE* pSrcFile, unsigned char* pstrkey, uint32_t ukeylen, const filecryption_callback_t* pcallback)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
FILE* pDestFile = fopen(poutfilename, "wb");
|
|
|
@@ -258,7 +256,7 @@ static int decrypt_asf_file(char* poutfilename, FILE* pSrcFile, unsigned char* p
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* pstrkey, size_t ukeylen, const filecryption_callback_t* pcallback)
|
|
|
+static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* pstrkey, uint32_t ukeylen, const filecryption_callback_t* pcallback)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
FILE* pDestFile = fopen(poutfilename, "wb");
|
|
|
@@ -290,7 +288,7 @@ static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* p
|
|
|
|
|
|
//2. 获取加密后的moov box大小
|
|
|
int32_t uencryptboxsize = 0;
|
|
|
- fread(&uencryptboxsize, 1, sizeof(int32_t), pSrcFile);
|
|
|
+ fread(&uencryptboxsize, 1, sizeof(uint32_t), pSrcFile);
|
|
|
safe_log(pcallback, "encrypt moov box size is = %d.", uencryptboxsize);
|
|
|
|
|
|
//3. 读取加密后的moov box
|
|
|
@@ -312,10 +310,12 @@ static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* p
|
|
|
safe_free(pdecmoovboxdata);
|
|
|
safe_free(pmoovboxdata);
|
|
|
|
|
|
+ iret = 0;
|
|
|
+
|
|
|
return iret;
|
|
|
}
|
|
|
|
|
|
-int decryption_file(char* poutfile, size_t uoutlen, const char* pfilename, const filecryption_callback_t* pcallback, eRvcCryptionVersion eversion)
|
|
|
+int decryption_file(char* poutfile, uint32_t uoutlen, const char* pfilename, const filecryption_callback_t* pcallback, eRvcCryptionVersion eversion)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
|
|
|
@@ -335,9 +335,10 @@ int decryption_file(char* poutfile, size_t uoutlen, const char* pfilename, const
|
|
|
if (0 != memcmp(strrvcflag, rvc_header, RVC_FILE_HEADER_FLAG_LEN)){
|
|
|
safe_log(pcallback,"file %s is not encryption!",pfilename);
|
|
|
fclose(pSrcFile);
|
|
|
- iret = 0;
|
|
|
return iret;
|
|
|
}
|
|
|
+
|
|
|
+ safe_log(pcallback, "%s:%d.", __FUNCTION__, __LINE__);
|
|
|
|
|
|
int irvcheadlen = 0;
|
|
|
fread(&irvcheadlen, 1, sizeof(int), pSrcFile);
|
|
|
@@ -345,24 +346,30 @@ int decryption_file(char* poutfile, size_t uoutlen, const char* pfilename, const
|
|
|
memset(prvcbuffer, 0, irvcheadlen);
|
|
|
fread(prvcbuffer, 1, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(int), pSrcFile);
|
|
|
|
|
|
- if (0 != memcmp(prvcbuffer+sizeof(size_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN)){
|
|
|
- if (0 == memcmp(prvcbuffer+sizeof(size_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN-1)){
|
|
|
+ safe_log(pcallback, "%s:%d.", __FUNCTION__, __LINE__);
|
|
|
+
|
|
|
+ if (0 != memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN)){
|
|
|
+ if (0 == memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN-1)){
|
|
|
safe_log(pcallback,"file %s encryption and decryption version is not matched!",pfilename);
|
|
|
}
|
|
|
fclose(pSrcFile);
|
|
|
return iret;
|
|
|
}
|
|
|
+
|
|
|
+ safe_log(pcallback, "%s:%d.", __FUNCTION__, __LINE__);
|
|
|
|
|
|
unsigned char strkey[SM4ENC_BLOCK_SIZE] = {0};
|
|
|
if (get_key_from_header_info(strkey, SM4ENC_BLOCK_SIZE, prvcbuffer, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(int), pcallback)){
|
|
|
return iret;
|
|
|
}
|
|
|
|
|
|
+ safe_log(pcallback, "%s:%d.", __FUNCTION__, __LINE__);
|
|
|
|
|
|
if (get_decrytion_filename(poutfile, uoutlen, RVC_FILEDEC_STR, strlen(RVC_FILEDEC_STR), pfilename)){
|
|
|
return iret;
|
|
|
}
|
|
|
|
|
|
+ safe_log(pcallback, "%s:%d.", __FUNCTION__, __LINE__);
|
|
|
|
|
|
if (eVerA == eversion) {
|
|
|
iret = decrypt_asf_file(poutfile, pSrcFile, strkey, SM4ENC_BLOCK_SIZE, pcallback);
|
|
|
@@ -410,7 +417,7 @@ bool is_file_encrypted(const char* pfilename, const filecryption_callback_t* pca
|
|
|
输入:文件路径
|
|
|
输出:文件明文头数据,文件明文头长度,加解密前后文件偏移量,自定义头文件头信息
|
|
|
*/
|
|
|
-int rvc_file_decrypt(unsigned char** pdechead, size_t* udecheadlen, int* ioffset, char** pstrjson, size_t* ujsonlen, const char* pfilename, const filecryption_callback_t* pcallback, eRvcCryptionVersion eversion)
|
|
|
+int rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffset, char** pstrjson, uint32_t* ujsonlen, const char* pfilename, const filecryption_callback_t* pcallback, eRvcCryptionVersion eversion)
|
|
|
{
|
|
|
int iret = -1;
|
|
|
if (NULL == pfilename || eversion >= sizeof(cryption_ver_flag_table)/sizeof(char*) || eversion < 0){
|
|
|
@@ -440,13 +447,13 @@ int rvc_file_decrypt(unsigned char** pdechead, size_t* udecheadlen, int* ioffset
|
|
|
}
|
|
|
else{
|
|
|
//safe_log(pcallback,"file %s is encrypted!",pfilename);
|
|
|
- fread(&irvcheadlen, 1, sizeof(int), pSrcFile);
|
|
|
+ fread(&irvcheadlen, 1, sizeof(uint32_t), pSrcFile);
|
|
|
char* prvcbuffer = (char*)malloc(irvcheadlen);
|
|
|
memset(prvcbuffer, 0, irvcheadlen);
|
|
|
- fread(prvcbuffer, 1, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(int), pSrcFile);
|
|
|
+ fread(prvcbuffer, 1, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(uint32_t), pSrcFile);
|
|
|
|
|
|
- if (0 != memcmp(prvcbuffer+sizeof(size_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN)){
|
|
|
- if (0 == memcmp(prvcbuffer+sizeof(size_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN-1)){
|
|
|
+ if (0 != memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN)){
|
|
|
+ if (0 == memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN-1)){
|
|
|
safe_log(pcallback,"file %s encryption and decrption version is not matched!",pfilename);
|
|
|
}
|
|
|
safe_free(prvcbuffer);
|
|
|
@@ -454,13 +461,13 @@ int rvc_file_decrypt(unsigned char** pdechead, size_t* udecheadlen, int* ioffset
|
|
|
return iret;
|
|
|
}
|
|
|
|
|
|
- if (get_key_from_header_info(strkey, SM4ENC_BLOCK_SIZE, prvcbuffer, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(int), pcallback)){
|
|
|
+ if (get_key_from_header_info(strkey, SM4ENC_BLOCK_SIZE, prvcbuffer, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(uint32_t), pcallback)){
|
|
|
safe_free(prvcbuffer);
|
|
|
fclose(pSrcFile);
|
|
|
return iret;
|
|
|
}
|
|
|
|
|
|
- int ijson = get_file_json_infos_from_rvc_header(pstrjson, ujsonlen, prvcbuffer, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(int));
|
|
|
+ int ijson = get_file_json_infos_from_rvc_header(pstrjson, ujsonlen, prvcbuffer, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(uint32_t));
|
|
|
if (0 == ijson){
|
|
|
safe_log(pcallback,"get_file_json_infos_from_rvc_header success!");
|
|
|
}
|