123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- #include "stdafx.h"
- #include "stdlib.h"
- #include "fileanalysis.h"
- #include "CMBSMDLL.h"
- #include <string.h>
- #include "rvcfileheader.h"
- #include "cJSON.h"
- #ifdef _WIN32
- #ifndef FILE_SLASH
- #define FILE_SLASH '\\'
- #endif
- #else
- #ifndef FILE_SLASH
- #define FILE_SLASH '/'
- #endif
- #endif // _WIN32
- static unsigned char seedkey[] = "Q&@09#5*AZWSsdff";
- int get_srcfile_name(char* pname, uint32_t ulen, const char* pfilename)
- {
- int iret = -1;
- if (NULL == pfilename){
- return iret;
- }
- const char* pindex = strrchr(pfilename, FILE_SLASH);
- const char* pend = strrchr(pfilename, '.');
- if (pindex&&pend){
- size_t udatalen = pend - pindex - 1;
- if (ulen >= udatalen){
- memcpy(pname, pindex+1, udatalen);
- iret = 0;
- }
- }
- return iret;
- }
- int get_srcfile_format(char* pformat, uint32_t ulen, const char* pfilename)
- {
- int iret = -1;
- if (NULL == pfilename){
- return iret;
- }
- const char* pindex = strstr(pfilename, ".");
- if (pindex){
- size_t udatalen = strlen(pindex);
- if (ulen >= udatalen){
- memcpy(pformat, pindex+1, udatalen);
- iret = 0;
- }
- }
- return iret;
- }
- int get_srcfile_hash(char* phash, uint32_t ulen, const char* pfilename)
- {
- int iret = -1;
- unsigned char hash[RVC_FILE_HASH_LEN] = { 0 };
- if(0 == CMBSM3FileDigest((char*)pfilename, hash)){
- if (RVC_FILE_HASH_LEN <= ulen){
- memcpy((void*)phash, (void*)hash, RVC_FILE_HASH_LEN);
- iret = 0;
- }
- }
- return iret;
- }
- uint32_t get_srcfile_size(const char* pfilename)
- {
- uint32_t usize = 0;
- if (NULL == pfilename){
- return usize;
- }
- FILE* pFile = fopen(pfilename, "rb");
- if (pFile){
- fseek(pFile, 0, SEEK_END);
- usize = (uint32_t)ftell(pFile);
- fclose(pFile);
- }
- return usize;
- }
- static int bin2str(unsigned char* x, int xlen, char* str, int str_size)
- {
- static const char* hex2char = "0123456789ABCDEF";
- int i, k = 0;
- if (str_size <= xlen * 2)
- return -1;
- for (i = 0; i < xlen; ++i) {
- int h = x[i] >> 4;
- int l = x[i] & 0xf;
- str[k++] = hex2char[h];
- str[k++] = hex2char[l];
- }
- str[k] = 0;
- return k;
- }
- int get_encrytion_filename(char* strbuffer, uint32_t ulen, bool bencname, const char* strprefix, uint32_t uprefixlen, const char* pfilename)
- {
- int iret = -1;
- const char* pIndex = strrchr(pfilename, FILE_SLASH);
- if (pIndex){
- pIndex++;
- int ifolderlen = pIndex - pfilename;
- char strfolder[MAX_PATH] = { 0 };
- if (MAX_PATH > ifolderlen) {
- memcpy(strfolder, pfilename, ifolderlen);
- }
-
- if (false == bencname){
- snprintf(strbuffer, ulen, "%s%s%s", strfolder, strprefix, pIndex);
- }
- else{
- unsigned char strSM[RVC_FILE_HASH_LEN] = {0};
- CMBSM3Digest((unsigned char*)pIndex, strlen(pIndex), strSM);
- char strHash[MAX_PATH] = {0};
- bin2str(strSM, RVC_FILE_HASH_LEN, strHash, MAX_PATH);
- snprintf(strbuffer, ulen, "%s%s", strfolder, strHash);
- }
- iret = 0;
- }
-
- return iret;
- }
- int get_decrytion_filename(char* strbuffer, uint32_t ulen, const char* strprefix, uint32_t uprefixlen, const char* pfilename)
- {
- int iret = -1;
- const char* pIndex = strrchr(pfilename, FILE_SLASH);
- if (pIndex){
- pIndex++;
- int ifolderlen = pIndex - pfilename;
- char strfolder[MAX_PATH] = { 0 };
- if (MAX_PATH > ifolderlen) {
- memcpy(strfolder, pfilename, ifolderlen);
- }
- snprintf(strbuffer, ulen, "%s%s%s", strfolder, strprefix, pIndex);
- iret = 0;
- }
- return iret;
- }
- int SM4EncECBMode(unsigned char key[16], unsigned char *input, int length, unsigned char *output, int *output_len)
- {
- return CMBSM4EncryptWithECB(key, input, length, output, output_len);
- }
- int SM4DecECBMode(unsigned char key[16], unsigned char *input, int length, unsigned char *output, int *output_len)
- {
- return CMBSM4DecryptWithECB(key, input, length, output, output_len);
- }
- int GenerateSM4Key(unsigned char* pkey, uint32_t ukeysize, unsigned char* phash, uint32_t uhashlen, uint32_t ufilesize)
- {
- int iret = -1;
- if (SM4ENC_BLOCK_SIZE > ukeysize || NULL == pkey){
- return iret;
- }
- if (NULL == phash || RVC_FILE_HASH_LEN != uhashlen || 0 == ufilesize){
- memcpy(pkey, seedkey, SM4ENC_BLOCK_SIZE);
- iret = 0;
- return iret;
- }
- char strHash[MAX_PATH] = {0};
- bin2str(phash, RVC_FILE_HASH_LEN, strHash, MAX_PATH);
- unsigned char outputdata[MAX_PATH] = {0};
- int ioutputlen = MAX_PATH;
- CMBSM4EncryptWithECB(seedkey, (unsigned char*)strHash, strlen(strHash), outputdata, &ioutputlen);
- int icount = ioutputlen/SM4ENC_BLOCK_SIZE;
- if (icount > 0){
- unsigned char *pindex = pkey;
- int i = ufilesize % icount;
- for (; i < ioutputlen; i += icount){
- *pindex++ = outputdata[i];
- }
- }
- else{
- memcpy(pkey, seedkey, SM4ENC_BLOCK_SIZE);
- }
- iret = 0;
- return iret;
- }
- int get_file_json_infos_from_rvc_header(char** pstrjson, uint32_t* ujsonlen, char* pheader, uint32_t udatalen)
- {
- int iret = -1;
- rvc_fileheader_t t_param = {0};
- if (0 != get_rvc_file_header_info(&t_param, pheader, udatalen, NULL)){
- return iret;
- }
- char strHash[MAX_PATH] = {0};
- bin2str(t_param.strhash, RVC_FILE_HASH_LEN, strHash, MAX_PATH);
- cJSON* root = cJSON_CreateObject();
- cJSON_AddItemToObject(root, "Hash",cJSON_CreateString(strHash));
- cJSON_AddItemToObject(root, "FileName",cJSON_CreateString(t_param.strsrcfilename));
- cJSON_AddItemToObject(root, "FileFormat",cJSON_CreateString(t_param.strsrcfileformat));
- cJSON_AddItemToObject(root, "FileSize",cJSON_CreateNumber(t_param.usrcfilelen));
- char *pret = cJSON_PrintUnformatted(root);
- if (pret){
- size_t ulen = strlen(pret);
- *pstrjson = (char*)malloc(ulen+1);
- memset(*pstrjson, 0, ulen+1);
- memcpy(*pstrjson, pret, ulen);
- *ujsonlen = ulen;
- iret = 0;
- cJSON_free(pret);
- }
- cJSON_Delete(root);
- return iret;
- }
- int get_file_json_infos_from_file(char** pstrjson, uint32_t* ujsonlen, const char* pfilename, FILE* pFile)
- {
- int iret = -1;
- if (NULL == pFile || NULL == pfilename){
- return iret;
- }
- char strname[MAX_PATH] = {0};
- get_srcfile_name(strname, MAX_PATH, pfilename);
- char strformat[MAX_PATH] = {0};
- get_srcfile_format(strformat, MAX_PATH, pfilename);
- unsigned char strhash[RVC_FILE_HASH_LEN] = {0};
- get_srcfile_hash((char*)strhash, RVC_FILE_HASH_LEN, pfilename);
- char strValue[MAX_PATH] = {0};
- bin2str(strhash, RVC_FILE_HASH_LEN, strValue, MAX_PATH);
- size_t usize = 0;
- if (pFile){
- fseek(pFile, 0, SEEK_END);
- usize = ftell(pFile);
- }
- cJSON* root = cJSON_CreateObject();
- cJSON_AddItemToObject(root, "Hash",cJSON_CreateString(strValue));
- cJSON_AddItemToObject(root, "FileName",cJSON_CreateString(strname));
- cJSON_AddItemToObject(root, "FileFormat",cJSON_CreateString(strformat));
- cJSON_AddItemToObject(root, "FileSize",cJSON_CreateNumber(usize));
- char *pret = cJSON_PrintUnformatted(root);
- if (pret){
- size_t ulen = strlen(pret);
- *pstrjson = (char*)malloc(ulen+1);
- memset(*pstrjson, 0, ulen+1);
- memcpy(*pstrjson, pret, ulen);
- *ujsonlen = ulen;
- iret = 0;
- cJSON_free(pret);
- }
- cJSON_Delete(root);
- return iret;
- }
- int get_file_hashstring(char* pbuffer, uint32_t ubuflen, const char* pfilename)
- {
- int iret = -1;
- if (!pbuffer || !pfilename) {
- return iret;
- }
- unsigned char strhash[RVC_FILE_HASH_LEN] = { 0 };
- if (0 != get_srcfile_hash((char*)strhash, RVC_FILE_HASH_LEN, pfilename)) {
- return iret;
- }
- if (0 == bin2str(strhash, RVC_FILE_HASH_LEN, pbuffer, ubuflen)) {
- iret = 0;
- }
- return iret;
- }
|