ADD function

hardware sign 
tcp model fro multi rtp cell
This commit is contained in:
AlanRen 2023-05-28 08:02:39 -06:00
parent efffd3cc2a
commit 0517064936
23 changed files with 27335 additions and 116 deletions

View File

@ -61,6 +61,9 @@
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"bit": "cpp"
"bit": "cpp",
"core": "cpp",
"geometry": "cpp",
"*.txx": "cpp"
}
}

View File

@ -48,6 +48,9 @@ add_library(SVAC_ENC STATIC ${src_ENC})
list(APPEND LINK_LIB_SVAC_LIST SVAC_DEC)
list(APPEND LINK_LIB_SVAC_LIST SVAC_ENC)
#
list(APPEND LINK_LIB_SVAC_LIST sm.so)
include_directories(include src)
# add_definitions(-DGENERATE_EXPORT)

View File

@ -85,7 +85,7 @@ namespace SecMedia
int GeneSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel);
int DecSecuritySEI(uint8_t* frame,size_t len,uint8_t prefix,CodecId code);
int GeneHWSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel,uint32_t vid_len, uint8_t sign_len,uint8_t* sign,char* rtp_head,uint16_t rtp_len);
int GeneHWSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel,uint32_t vid_len, uint8_t sign_len,uint8_t* sign,char* rtp_head=nullptr,uint16_t rtp_len=0);
enum class Hi_Packed_type_e{
Reserved=0,
FUA=1,

View File

@ -6,6 +6,7 @@
#include <functional>
#include <list>
#include <memory>
#include "common.h"
#include "HuaWei/queue.h"
#include "SVAC/src/svac_src/svac_dec.h"
#include "SVAC/src/svac_src/SM2_SM3.h"
@ -14,9 +15,11 @@
using namespace std;
#define H265_TYPE(v) (((uint8_t)(v) >> 1) & 0x3f)
#define PRINT(f_,...) printf((f_),##__VA_ARGS__)
#define DEBUGL(x,...)
#define WRNGL(x,...) PRINT(x,##__VA_ARGS__)
#define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
#define PRINT(f_,...) printf((f_),##__VA_ARGS__) //PRINT(x,##__VA_ARGS__)
#define DEBUGL(x,...) PRINT(x,##__VA_ARGS__)
#define INFOL(x,...) PRINT(x,##__VA_ARGS__)
#define WRNGL(x,...) PRINT(x,##__VA_ARGS__)
typedef enum {
NAL_TRAIL_N = 0,
@ -54,6 +57,7 @@ struct SM2Config{
char pubkey_size;
uint8_t prikey[64];
uint8_t pubkey[128];
void * SDF_Session_h;
};
struct HWsign{
uint32_t seq_accu;
@ -86,8 +90,10 @@ typedef struct {
void print_data2(const char * buf, uint32_t len,uint8_t offest);
void print_rtp2(const char * buf, uint32_t len,uint8_t offest);
uint32_t add_racing_code(uint8_t *dst,uint8_t *src, uint32_t src_len);
uint32_t del_racing_code(uint8_t *dst,uint8_t *src, uint32_t src_len);
bool MakeFU(uint8_t in, FU &fu);
void Make265FU(uint8_t in, FU &fu);
uint16_t get_sequence(const char * rtp ,int tcp);
int Hardware_sign(SM2Config * config,const unsigned char * data_in,const size_t in_len, unsigned char * sign_out, uint16_t * sign_len);
#endif /* _HW_COMMON_H */

235
src/HuaWei/HWsec.cpp Normal file
View File

@ -0,0 +1,235 @@
#include "HWsec.h"
#include "DecEnc/NALUdecode.h"
HWsec::HWsec(/* args */)
{
_keyframe_seq=-1;
_code_id=CodecInvalid;
sm3_init(&_sm3);
_sm2_config={
32,
64,
{
0x24,0x88,0xc8,0xdc,0x7f,0xd7,0xe0,0x91,0x30,0x1b,0x5c,0x58,0x2f,0xe7,0x44,0x7d,
0x2f,0x43,0xe4,0xee,0xc8,0x7d,0xc0,0xfb,0xa4,0xb8,0x7d,0x4b,0x8a,0x69,0x7c,0x4e
},
{
0xaa,0xb1,0x3f,0xd7,0x66,0xe2,0x75,0x97,0xc0,0x03,0xe6,0xe4,0x1d,0x77,0x54,0x78,
0xc8,0x29,0xb2,0x0b,0x9e,0xd1,0xff,0xa3,0x6a,0x6f,0xd2,0x7f,0xd6,0x2d,0xaa,0x3f,
0xc9,0x24,0xec,0x6c,0x96,0x0a,0x7b,0x73,0xf6,0xe6,0xfc,0xda,0x3a,0x08,0xfd,0x92,
0xfc,0x00,0x08,0x97,0x78,0x2c,0x71,0x6b,0xe1,0x26,0xf5,0x1e,0xba,0x31,0xf5,0xb2,
}
};
}
HWsec::~HWsec()
{
}
void HWsec::sign_data(const char * buf, const uint32_t len,RTPcell::Ptr &sei_cell){
uint8_t sha[128],sign[128],sei[2048],sei_race[1024];
uint16_t sign_len=64;
sm3_update(&_sm3,(uint8_t*)buf,len);
sm3_final(&_sm3,sha);
do_sm2_sign((char*)_sm2_config.prikey,(char*)_sm2_config.pubkey,(char *) sha, 32, (char *)sign);
// Hardware_sign(&_sm2_config,(const unsigned char *)(buf),len,sign,&sign_len);
DEBUGL("\n&&&&&&&& sign: hash &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)sha,32,32);
DEBUGL("\n&&&&&&&& sign: sign &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)sign,64,64);
auto rtp_len=SecMedia::GeneHWSecritySEI(sei,_code_id,1,len,sign_len,sign);
rtp_len=add_racing_code(sei_race,sei,rtp_len);
DEBUGL("success!!!!!!!!!! sei package\n");
if(sei_cell) sei_cell->_RTPpkg_ptr->insertData(sei_race,rtp_len,sei_cell->_RTPpkg_head_pos,sei_cell->_RTPpkg_head_pos+sei_cell->_RTP_len);
}
int HWsec::decode_265(RTPcell::Ptr rtp_cell){
uint8_t * rtp =rtp_cell->_PT_header;
uint16_t len=rtp_cell->_PT_len;
int nal_type = H265_TYPE(*rtp);
// int nal_suffix = *rtp & (~0x1F);
uint16_t now_seq=rtp_cell->seq;
DEBUGL("\n###### input ########################## nal: %d ", nal_type);
print_data2((char*)rtp,len,0);
// send_rtp(HWSign_hd,buf,len,tcp,id);
// return 1;
// HWSign_hd->rtp_cb(buf,len,id);
// return 1;
switch (nal_type){
case NAL_SEI_PREFIX:{
DEBUGL("SEI!!!!!!!!!! package\n");
if(len<1300){
if(_SEI_rtp ) rtp_cell->_RTPpkg_ptr->setPause(false);
_SEI_rtp=rtp_cell;
rtp_cell->_RTPpkg_ptr->setPause(true);
}
return 1;
}
case 49:{
FU fu;
Make265FU((uint8_t)rtp[2], fu);
if (!(fu.type>=NAL_IDR_W_RADL && fu.type<=NAL_IDR_N_LP))
{
return 1;
}
// HWSign_hd->rtp_cb(buf,len,param);
// return 1;
if (fu.S) { //第一个rtp包
if(!_keyframe_DATA.empty()){
WRNGL("Warning!!!!!!!!!! missing package\n");
if (_SEI_rtp!=nullptr)
{
_SEI_rtp->_RTPpkg_ptr->setPause(false);
_SEI_rtp.reset();
}
}
DEBUGL("!!!!!!!!!! I head\n");
_keyframe_seq=now_seq;
_keyframe_DATA.assign((char*)rtp+3,len-3);
return 1;
}
if(now_seq==_keyframe_seq+1){
_keyframe_seq=now_seq;
_keyframe_DATA.append((char*)rtp+3,len-3);
if (!fu.E) { //中间rtp包
return 1;
}
if (_SEI_rtp!=nullptr )
{
if (_SEI_rtp->timestamp==rtp_cell->timestamp)
{
sign_data(_keyframe_DATA.data(),_keyframe_DATA.size(),_SEI_rtp);
DEBUGL("!!!!!!!!!! sign data len:%lu\n",_keyframe_DATA.size());
}
_SEI_rtp->_RTPpkg_ptr->setPause(false);
_SEI_rtp.reset();
_keyframe_DATA.clear();
}
_keyframe_seq=-1;
return 1;
}else{
return 1;
}
}
default:{
return 1;
}
}
}
int HWsec::decode_264(RTPcell::Ptr rtp_cell){
uint8_t * rtp =rtp_cell->_PT_header;
uint16_t len=rtp_cell->_PT_len;
int nal_type = H264_TYPE(*rtp);
// int nal_suffix = *rtp & (~0x1F);
uint16_t now_seq=rtp_cell->seq;
DEBUGL("\n###### input ########################## nal: %d ", nal_type);
print_data2((char*)rtp,len,0);
// send_rtp(HWSign_hd,buf,len,tcp,id);
// return 1;
// HWSign_hd->rtp_cb(buf,len,id);
// return 1;
switch (nal_type){
case 6:{
DEBUGL("SEI!!!!!!!!!! package\n");
if(len<1300){
if(_SEI_rtp ) rtp_cell->_RTPpkg_ptr->setPause(false);
_SEI_rtp=rtp_cell;
rtp_cell->_RTPpkg_ptr->setPause(true);
}
return 1;
}
case 28:{
FU fu;
MakeFU((uint8_t)rtp[1], fu);
if (fu.type!=5)
{
return 1;
}
// HWSign_hd->rtp_cb(buf,len,param);
// return 1;
if (fu.S) { //第一个rtp包
if(!_keyframe_DATA.empty()){
WRNGL("Warning!!!!!!!!!! missing package\n");
if (_SEI_rtp!=nullptr)
{
_SEI_rtp->_RTPpkg_ptr->setPause(false);
_SEI_rtp.reset();
}
}
DEBUGL("!!!!!!!!!! I head\n");
_keyframe_seq=now_seq;
_keyframe_DATA.assign((char*)rtp+2,len-2);
return 1;
}
if(now_seq==_keyframe_seq+1){
_keyframe_seq=now_seq;
_keyframe_DATA.append((char*)rtp+2,len-2);
if (!fu.E) { //中间rtp包
return 1;
}
if (_SEI_rtp!=nullptr )
{
if (_SEI_rtp->timestamp==rtp_cell->timestamp)
{
sign_data(_keyframe_DATA.data(),_keyframe_DATA.size(),_SEI_rtp);
DEBUGL("!!!!!!!!!! sign data len:%lu\n",_keyframe_DATA.size());
}
_SEI_rtp->_RTPpkg_ptr->setPause(false);
_SEI_rtp.reset();
_keyframe_DATA.clear();
}
_keyframe_seq=-1;
return 1;
}else{
return 1;
}
}
default:{
return 1;
}
}
}
void * HWSign_tcp_init(){
HWsec * HWsec_ptr= new HWsec;
return (void*) HWsec_ptr;
}
void HWSign_tcp_release(void* Handle){
delete (HWsec *)Handle;
}
int HWSign_tcp_rtp_input(void* Handle, const char * buf, const uint32_t len, void * param){
HWsec * HWsec_ptr=(HWsec * )Handle;
if(!HWsec_ptr) return -1;
HWsec_ptr->HWsec_input((uint8_t*)buf,len,param);
return 1;
}
int HWSign_tcp_rtp_out(void* Handle, char * buf, uint32_t * len, void ** param){
HWsec * HWsec_ptr=(HWsec * )Handle;
if(!HWsec_ptr) return -1;
return HWsec_ptr->HWsec_output(buf,len,param);
}

110
src/HuaWei/HWsec.h Normal file
View File

@ -0,0 +1,110 @@
#ifndef _HWsec_H
#define _HWsec_H
#include "RTP.h"
#include "queue.h"
class HWsec_queue: public ThreadsafeQueue<RTPpackage::Ptr>
{
public:
HWsec_queue(){};
~HWsec_queue(){};
RTPpackage::Ptr pop(){
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
return nullptr;
}
RTPpackage::Ptr tmp = queue_.front();
if (tmp->isPause())
{
return nullptr;
}
queue_.pop();
return tmp;
}
};
class HWsec
{
private:
CodecId _code_id;
string _keyframe_DATA;
int _keyframe_seq;
RTPcell::Ptr _SEI_rtp;
RTPcell::Ptr _prv_cell=nullptr;
HWsec_queue _RTPpackage_queue;
SM2Config _sm2_config;
sm3_ctx _sm3;
uint32_t _all_in=0;
uint32_t _all_out=0;
public:
HWsec(/* args */);
~HWsec();
void HWsec_input(const uint8_t* data,uint16_t len, void *param=nullptr){
RTPpackage::rtp_cb_f rtp_cell_cb=[&](const RTPcell::Ptr &rtp_cell){
if(rtp_cell){
rtp_cell->precessRTP();
DEBUGL("mark:%d PT:%d seq:%d stamp:%u \n",rtp_cell->mark,rtp_cell->PT,rtp_cell->seq,rtp_cell->timestamp);
switch (rtp_cell->PT)
{
case 99: //h264
if (_code_id!=CodecId::CodecH264)
{
_code_id=CodecId::CodecH264;
INFOL("Track type is changed to H.264\n");
}
decode_264( rtp_cell);
break;
case 103 ... 108: //h265
if (_code_id!=CodecId::CodecH265)
{
_code_id=CodecId::CodecH265;
INFOL("Track type is changed to H.265\n");
}
decode_265( rtp_cell);
break;
case 0 ... 95: // standard type
break;
default:
WRNGL("Error !!!!!!!!!! Unsupport track type \n");
break;
}
}else
{
WRNGL("error");
}
};
_all_in++;
RTPpackage::Ptr RTPpkg=make_shared<RTPpackage>(rtp_cell_cb,_prv_cell);
_prv_cell=RTPpkg->assignRTPpackage(data,len,param);
_RTPpackage_queue.push(RTPpkg);
}
int HWsec_output(char * buf, uint32_t * len, void ** param){
auto element= _RTPpackage_queue.pop();
// *param=nullptr;
if (!element) return -1;
*len=(uint32_t) element->_buffer.size();
memcpy(buf,element->_buffer.data(),*len);
*param=element->_usr_param;
_all_out++;
DEBUGL("\nHWSign: in %u out %u que: %lu \n",_all_in,_all_out, _RTPpackage_queue.size());
return 1;
}
private:
void sign_data(const char * buf, const uint32_t len,RTPcell::Ptr &sei_cell);
int decode_265(RTPcell::Ptr rtp_cell);
int decode_264(RTPcell::Ptr rtp_cell);
};
#endif //_HWSIGN_TCP_H

View File

@ -6,7 +6,7 @@
#include "DecEnc/NALUdecode.h"
#include "DecEnc/base64.h"
#include "sm/sm.h"
@ -39,6 +39,48 @@ void print_rtp2(const char * buf, uint32_t len,uint8_t offest){
print_data2(buf,len,offest);
}
int hardware_sign_l(SM2Config * config,const unsigned char * data_in,const size_t in_len, unsigned char * sign_out, uint16_t * sign_len){
int rv = 0;
rv = Device_Init();
printf("Device_Init rv = 0x%02x\n", rv);
if(rv != 0)
{
printf("Device_Init Fail\n");
return -1;
}
unsigned char Hash_Data[35];
unsigned int Hash_Data_len = 32;
rv = Generate_Hash(data_in, (const unsigned int)in_len, Hash_Data, &Hash_Data_len, NULL, SGD_SM3);
if(rv != 0)
{
printf("Generate_Hash FAIL\n\n");
return -1;
}
else printf("Generate_Hash SUCCESS\n\n");
unsigned int Sign_Data_len = 65;
*sign_len=Sign_Data_len;
rv = Generate_SignData_ExtPrikey(config->prikey, Hash_Data, Hash_Data_len, sign_out, &Sign_Data_len);
if(rv != 0)
{
printf("Generate_SignData_ExtPrikey Fail\n");
return 0;
}
else printf("Generate_SignData_ExtPrikey SUCCESS\n");
return 1;
}
int Hardware_sign(SM2Config * config,const unsigned char * data_in,const size_t in_len, unsigned char * sign_out, uint16_t * sign_len){
int ret;
ret=hardware_sign_l(config,data_in,in_len,sign_out,sign_len);
Close_Device();
return ret;
}
bool MakeFU(uint8_t in, FU &fu) {
fu.S = in >> 7;
fu.E = (in >> 6) & 0x01;
@ -57,6 +99,67 @@ void Make265FU(uint8_t in, FU &fu) {
}
uint32_t add_racing_code(uint8_t *dst,uint8_t *src, uint32_t src_len)
{
const uint8_t d_zero[]={0x00,0x00};
uint8_t *ptr_zero=src,*dst_head=dst,*src_tail=src+src_len, *noncpy_src_head=src;
uint32_t data_len=0,remain_data_len=src_len;
while(remain_data_len>2)
{
ptr_zero=(uint8_t*) memmem(ptr_zero,remain_data_len-2,&d_zero,sizeof(d_zero));
if(ptr_zero ){ //0x00 1 2 3
ptr_zero+=2;
if(!(*(ptr_zero)&0xFC)){
data_len=ptr_zero-noncpy_src_head;
memcpy(dst_head,noncpy_src_head,data_len);
dst_head+=data_len;
*(dst_head++)=0x03;
noncpy_src_head=ptr_zero; //更新未复制的源数据头
}
remain_data_len=(src_tail-ptr_zero);
}else
{
break;
}
}
data_len=src_tail-noncpy_src_head;
memcpy(dst_head,noncpy_src_head,data_len);
//int finnum=(int)(dst_head-dst)+(int)data_len;
return (uint32_t)(dst_head-dst)+data_len;;
}
uint32_t del_racing_code(uint8_t *dst,uint8_t *src, uint32_t src_len)
{
const uint8_t d_zero[]={0x00,0x00,0x03};
uint8_t *ptr_zero=src,*dst_head=dst,*src_tail=src+src_len, *noncpy_src_head=src;
uint32_t data_len=0,remain_data_len=src_len;
while(remain_data_len>2)
{
ptr_zero=(uint8_t*) memmem(ptr_zero,remain_data_len-2,&d_zero,sizeof(d_zero));
if(ptr_zero ){ //0x00 1 2 3
ptr_zero+=3;
data_len=ptr_zero-noncpy_src_head-1;//末尾0x03 不要
memcpy(dst_head,noncpy_src_head,data_len);
dst_head+=data_len;
noncpy_src_head=ptr_zero; //更新未复制的源数据头
remain_data_len=(src_tail-ptr_zero);
}else
{
break;
}
}
data_len=src_tail-noncpy_src_head;
memcpy(dst_head,noncpy_src_head,data_len);
//int finnum=(int)(dst_head-dst)+(int)data_len;
return (uint32_t)(dst_head-dst)+data_len;;
}
// void * HWSign_init(const function<void(const char * rtp_ptr, const uint32_t rtp_len)> rtp_callback){
void * HWSign_init(){
HWsign* HWSign_hd= new HWsign();
@ -100,6 +203,7 @@ void * HWSign_init(){
HWSign_hd->sm2_hd->pubkey_size=64;
memcpy(HWSign_hd->sm2_hd->prikey,sm2fig.prikey,sizeof(sm2fig.prikey));
memcpy(HWSign_hd->sm2_hd->pubkey,sm2fig.pubkey,sizeof(sm2fig.pubkey));
return (void*)HWSign_hd;
}
@ -150,18 +254,24 @@ void send_rtp(HWsign* Handle,const char * buf, const uint32_t len ,int tcp,void
free(send_buff);
}
void sign_data(HWsign* Handle,const char * buf, const uint32_t len,uint8_t offset,int tcp,void * param,const char * sei_head){
uint8_t sha[128],sign[128],sei[1024];
uint8_t sha[128],sign[128],sei[2048],sei_race[1024];
uint16_t sign_len=64;
sm3_update(Handle->sm3_hd,(uint8_t*)buf+offset,len-offset);
sm3_final(Handle->sm3_hd,sha);
do_sm2_sign((char*)Handle->sm2_hd->prikey,(char*)Handle->sm2_hd->pubkey,(char *) sha, 32, (char *)sign);
// Hardware_sign(Handle->sm2_hd,(const unsigned char *)(buf+offset),len-offset,sign,&sign_len);
DEBUGL("\n&&&&&&&& sign: hash &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)sha,32,32);
DEBUGL("\n&&&&&&&& sign: sign &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)sign,64,64);
Handle->sei_param=nullptr;
auto rtp_len=SecMedia::GeneHWSecritySEI(sei,Handle->track_type,1,len-offset,64,sign,(char *)sei_head,Handle->sei_len);
auto rtp_len=SecMedia::GeneHWSecritySEI(sei,Handle->track_type,1,len-offset,sign_len,sign,(char *)sei_head,Handle->sei_len);
rtp_len=add_racing_code(sei_race,sei+Handle->sei_len,rtp_len-Handle->sei_len);
memcpy(sei+Handle->sei_len,sei_race,rtp_len);
rtp_len+=Handle->sei_len;
DEBUGL("success!!!!!!!!!! sei package\n");
send_rtp(Handle,(char *) sei,rtp_len,tcp,param);
// Handle->seq_accu++;
@ -175,8 +285,10 @@ void none_sign_data(HWsign* Handle,uint8_t offset,int tcp,void * param,const cha
}
int HWSign_rtp_out(void* Handle, char * buf, uint32_t * len, void ** param){
HWsign* HWSign_hd=(HWsign*) Handle;
if (HWSign_hd->buff_que->empty()) return -1;
if(!HWSign_hd) return -1;
// if (HWSign_hd->buff_que->empty()) return -1;
auto element= HWSign_hd->buff_que->pop();
if (element.first->empty()) return -1;
*len=(uint32_t) element.first->size();
memcpy(buf,element.first->data(),*len);
*param=element.second;
@ -242,6 +354,10 @@ int HWSign_rtp_264(HWsign* HWSign_hd, const char * buf, const uint32_t len,int t
// send_rtp(HWSign_hd,buf,len,tcp,param);
return 1;
}
// HWSign_hd->rtp_cb(buf,len,param);
// return 1;
if (fu.S) { //第一个rtp包
if(!HWSign_hd->buff->empty()){
WRNGL("Warning!!!!!!!!!! missing package\n");
@ -329,6 +445,9 @@ int HWSign_rtp_265(HWsign* HWSign_hd, const char * buf, const uint32_t len,int t
// send_rtp(HWSign_hd,buf,len,tcp,param);
return 1;
}
// HWSign_hd->rtp_cb(buf,len,param);
// return 1;
if (fu.S) { //第一个rtp包
if(!HWSign_hd->buff->empty()){
WRNGL("Warning!!!!!!!!!! missing package\n");
@ -394,6 +513,7 @@ int HWSign_rtp_input(void* Handle, const char * buf, const uint32_t len,int tcp,
return -1;
}
uint8_t payload_type=buf[1]& 0x7f;
switch (payload_type)

View File

@ -7,11 +7,21 @@
//#include <functional>
#define API_EXPORT __attribute__((visibility("default")))
// void * HWSign_init(const std::function<void(const char * rtp_ptr, const uint32_t rtp_len)> rtp_callback);
API_EXPORT int SDF_Device_open();
API_EXPORT int SDF_Device_close();
API_EXPORT void * HWSign_init();
API_EXPORT void HWSign_release(void* Handle);
API_EXPORT int HWSign_rtp_input(void* Handle, const char * buf, const uint32_t len,int tcp, void * param);
API_EXPORT int HWSign_rtp_out(void* Handle, char * buf, uint32_t * len, void ** param);
API_EXPORT void * HWSign_tcp_init();
API_EXPORT void HWSign_tcp_release(void* Handle);
API_EXPORT int HWSign_tcp_rtp_input(void* Handle, const char * buf, const uint32_t len, void * param);
API_EXPORT int HWSign_tcp_rtp_out(void* Handle, char * buf, uint32_t * len, void ** param);
API_EXPORT void * HWVerify_init();
API_EXPORT void HWVerify_release(void* Handle);
API_EXPORT int HWVerify_rtp_input(void* Handle, const char * buf, const uint32_t len,int tcp, void * param);

View File

@ -17,13 +17,16 @@ void clear_all(HWsign* HWSign_hd){
inline void find_sign_sei(HWsign* HWSign_hd,uint16_t seq,const char * buf, const uint32_t len){
char * ptr;
const uint8_t uuid[] = { 0x86,0xb1 ,0x16 ,0x6e ,0xad ,0xef ,0x67 ,0x3f ,0x70 ,0xbc ,0xe7 ,0xc7 ,0xe6 ,0x95 ,0xc6 ,0x09 };
char sign_drace[1024];
auto next_start =(char*)memmem(buf, len,uuid,sizeof(uuid));
if(next_start){
if(!(next_start-2 > buf && *(next_start-2)==0x05)) return;
auto remain_len=(len-(next_start-buf));
remain_len=del_racing_code((uint8_t *)sign_drace,(uint8_t *)next_start,remain_len);
uint8_t sign_set_len=*(next_start-1);
if(remain_len<sign_set_len) return;
ptr=next_start +16 +1 +4 +16; //+ uuid + security_byte + vkek +vek
ptr=sign_drace +16 +1 +4 +16;
// ptr=sign_drace +16 +1 +4 +16; //+ uuid + security_byte + vkek +vek
HWSign_hd->I_len= *(ptr++)<<24 | *(ptr++)<<16 | *(ptr++)<<8 | *(ptr++);
HWSign_hd->sei_len=*(ptr++);
if(ptr+HWSign_hd->sei_len>buf+len) {
@ -41,13 +44,16 @@ int verify_data(HWsign* HWSign_hd){
if(HWSign_hd->sei_len==0) return -2;
if(HWSign_hd->I_len!=HWSign_hd->buff->size()) return -2;
uint8_t sha[128];
sm3_update(HWSign_hd->sm3_hd,(uint8_t*)HWSign_hd->buff->data(),HWSign_hd->I_len);
sm3_final(HWSign_hd->sm3_hd,sha);
DEBUGL("\n&&&&&&&& sign: hash &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)sha,32,32);
DEBUGL("\n&&&&&&&& sign: sign &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)HWSign_hd->sei_rtp_head,HWSign_hd->sei_len,HWSign_hd->sei_len);
if(do_sm2_verify((char*)HWSign_hd->sm2_hd->pubkey,(char*)sha,32,HWSign_hd->sei_rtp_head)==0) return 1;
if(do_sm2_verify((char*)HWSign_hd->sm2_hd->pubkey,(char*)sha,32,(char*)HWSign_hd->sei_rtp_head)==0) return 1;
else return -1;
}

277
src/HuaWei/RTP.h Normal file
View File

@ -0,0 +1,277 @@
#ifndef _RTP_H
#define _RTP_H
#include <string>
#include <functional>
#include <memory>
#include "string.h"
#include <arpa/inet.h>
#include "HWcommon.h"
using namespace std;
class RTPpackage;
class RTPcell
{
public:
typedef shared_ptr<RTPcell> Ptr;
public:
RTPcell(uint16_t len, uint8_t prefix, uint16_t buff_pos,shared_ptr<RTPpackage> RTPpkg_ptr){
_assign_len=0;
_RTPpkg_head_pos=buff_pos;
_prefix=prefix;
_missing_head=false;
_buffer=new uint8_t[(len/2048+1)*2048];
_RTPpkg_ptr=RTPpkg_ptr;
if(_prefix==16) _tcp_prefix=4;
else _tcp_prefix=0;
_RTP_len=len+_tcp_prefix;
}
RTPcell(const uint8_t *head_d,uint16_t len, uint8_t prefix, uint16_t buff_pos,shared_ptr<RTPpackage> RTPpkg_ptr){
_missing_head=true;
_RTPpkg_ptr=RTPpkg_ptr;
_assign_len=len;
_RTPpkg_head_pos=buff_pos;
_prefix=prefix;
_RTP_len=prefix;
_buffer=new uint8_t[(len/2048+1)*2048];
memcpy(_buffer,head_d,len);
if(_prefix==16) _tcp_prefix=4;
else _tcp_prefix=0;
}
~RTPcell(){
delete _buffer;
}
void setRTPlen(){
_RTP_len=(_buffer[2]<<8) &0xff00 | (_buffer[3])+_tcp_prefix;
_buffer= (uint8_t*) realloc(_buffer,(_RTP_len/2048+1)*2048);
_missing_head=false;
}
int append_data(const uint8_t * data, uint16_t len){
if(_RTP_len>=_assign_len+len){
memcpy(_assign_len+_buffer,data,len);
_assign_len+=len;
return 1;
}
return -1;
}
int getLackLen(){
return (int)_RTP_len-(int)_assign_len;
}
void precessRTP(){
uint8_t * ptr=_buffer+_tcp_prefix;
mark=ptr[1]>>7;
PT=ptr[1] & 0x7F;
memcpy(&seq, ptr + 2, 2);
seq = ntohs(seq);
memcpy(&timestamp, ptr + 4, 4);
timestamp = ntohl(timestamp);
memcpy(&ssrc, ptr + 8, 4);
ssrc = ntohl(ssrc);
_PT_header=ptr+12;
_PT_len=_RTP_len-12-_tcp_prefix;
}
public:
uint8_t _tcp_prefix=0;
uint8_t _prefix=0;
uint16_t _RTP_len=0;
uint16_t _assign_len=0;
uint16_t _RTPpkg_head_pos=0;
bool _missing_head=false;
uint8_t* _buffer=NULL;
shared_ptr<RTPpackage> _RTPpkg_ptr;
uint8_t *_PT_header=nullptr;
uint16_t _PT_len=0;
////////////////////////////////
bool mark;
uint8_t PT;
uint16_t seq;
uint32_t timestamp;
uint32_t ssrc;
};
class RTPpackage: public enable_shared_from_this<RTPpackage>
{
public:
typedef shared_ptr<RTPpackage> Ptr;
typedef function<void(const RTPcell::Ptr &rtp_cell)> rtp_cb_f;
private:
int isRTP(const uint8_t * data, uint16_t len,bool tail=false){
if(len==0) return tail?0:-1;
if(len>=_prefix)
if(data[0]==0x24 && data[4]==0x80)
return 1; // strong true
else
return -1; // strong false
else
return 0; // I don't no
}
uint16_t to_uint16(uint8_t H, uint8_t L){
return (H<<8) &0xff00 | (L);
}
RTPcell::Ptr makeRTPcell(const uint8_t * data, uint16_t len, uint16_t * buff_pos){
auto ret=isRTP(data,len);
if(ret==1){
uint16_t length=to_uint16(data[2],data[3]);
auto rtpcell = make_shared<RTPcell>(length,_prefix,*buff_pos,shared_from_this());
auto apd_len=min(rtpcell->_RTP_len,len);
rtpcell->append_data(data,apd_len);
*buff_pos+=apd_len;
return rtpcell;
}else if (ret==0)
{
auto rtpcell = make_shared<RTPcell>(data,len,_prefix,*buff_pos,shared_from_this());
*buff_pos+=len;
return rtpcell;
}else
{
return nullptr;
}
}
public:
RTPpackage(const rtp_cb_f & cb,const RTPcell::Ptr & prev_rtp_cell){
_rtp_cb=cb;
_first_start=true;
_Rtp_cell=prev_rtp_cell;
_prefix=16;
};
~RTPpackage(){
};
void insertData(const uint8_t *data, uint16_t len, uint16_t head_pos, uint16_t insert_pos){
uint8_t * head_ptr=(uint8_t *)_buffer.data()+head_pos;
size_t tail_pos=_buffer.size();
if(insert_pos<head_pos) return;
if(tail_pos<insert_pos) return;
if(isRTP(head_ptr,_prefix)<1) return;
_buffer.insert(insert_pos,(char*)data,len);
head_ptr=(uint8_t *)_buffer.data()+head_pos;
uint16_t length=to_uint16(head_ptr[2],head_ptr[3]);
length+=len;
head_ptr[2] = (length) >> 8;
head_ptr[3] = (length) & 0x00FF;
}
void setPause(bool enable){
_pop_pause=enable;
}
bool isPause(){
return _pop_pause;
}
int findStartHead(const uint8_t * package, uint16_t len){
const uint8_t needle[]={0x24,0x00};
uint8_t * sus_head=(uint8_t*)package;
uint16_t rest_len=len;
do{
sus_head=(uint8_t*)memmem(sus_head,rest_len-5,needle,2);
if(sus_head){
if( sus_head[4]==0x80){
return sus_head-package;
}
rest_len-=sus_head-package;
}
return -1;
}while(rest_len<5);
return -1;
}
RTPcell::Ptr assignRTPpackage(const uint8_t * package, uint16_t len,void* usr_param=nullptr){
int ret=0;
_buffer.assign((char*)package,len);
_usr_param=usr_param;
if(!_Rtp_cell && isRTP(package,len)<1){
ret=findStartHead(package,len);
if(ret<0) return nullptr;
}
auto buf_ptr=(uint8_t*)_buffer.data();
return splitRTP(buf_ptr,len,ret);
}
private:
// RTPcell::Ptr missingRTP(const uint8_t * data, uint16_t len, uint16_t * buff_pos){
// const uint8_t needle[]={0x24,0x00};
// uint8_t * sus_head=(uint8_t*)memmem(data,len-5,needle,2);
// if(sus_head[4]==0x80){
// *buff_pos=data-sus_head;
// }else
// {
// /* code */
// }
// }
RTPcell::Ptr splitRTP(const uint8_t * package, uint16_t len,uint16_t ofst=0){
int lacklen=0;
uint16_t offeset=ofst;
if(_Rtp_cell && _Rtp_cell->_missing_head){
auto lacklen=_Rtp_cell->getLackLen();
if(lacklen<=len){
_Rtp_cell->append_data(package,lacklen);
if(isRTP(_Rtp_cell->_buffer,_Rtp_cell->_assign_len)!=1) {
_Rtp_cell=nullptr;
}else
{
offeset+=lacklen;
_Rtp_cell->setRTPlen();
// lacklen=_Rtp_cell->getLackLen();
// _Rtp_cell->append_data(package,lacklen);
// offeset+=lacklen;
}
}
else return nullptr;
}
do
{
if(!_Rtp_cell){
_Rtp_cell=makeRTPcell(package+offeset,len-offeset,&offeset);
if(!_Rtp_cell) return nullptr;
if(_Rtp_cell->_missing_head) return _Rtp_cell;
}
lacklen=_Rtp_cell->getLackLen();
if(lacklen<=0) {
_rtp_cb(_Rtp_cell);
_Rtp_cell.reset();
}
else{
if((int)len-(int)offeset-lacklen<0) return _Rtp_cell;
auto ret=isRTP(package+offeset+lacklen,len-offeset-lacklen,true);
if(ret>=0){
_Rtp_cell->append_data(package+offeset,lacklen);
offeset+=lacklen;
}
else{
return NULL;
}
}
} while (1);
}
private:
bool _pop_pause=false;
rtp_cb_f _rtp_cb;
bool _first_start=true;
RTPcell::Ptr _Rtp_cell=NULL;
uint8_t _prefix=0;
public:
string _buffer;
void* _usr_param=nullptr;
};
#endif

16
src/HuaWei/SDF.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "common.h"
#include "HWcommon.h"
#include "HuaWei/HWsign.h"
int SDF_Device_open(){
return 0;
}
int SDF_Device_close(){
return 0;
}

View File

@ -1,19 +1,23 @@
#ifndef _QUEUE_H
#define _QUEUE_H
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
template<class T>
class ThreadsafeQueue {
std::queue<T> queue_;
mutable std::mutex mutex_;
protected:
std::queue<T> queue_;
mutable std::mutex mutex_;
public:
// Moved out of public interface to prevent races between this
// and pop().
bool empty() const {
return queue_.empty();
}
public:
ThreadsafeQueue() = default;
ThreadsafeQueue(const ThreadsafeQueue<T> &) = delete ;
ThreadsafeQueue& operator=(const ThreadsafeQueue<T> &) = delete ;
@ -26,11 +30,11 @@ class ThreadsafeQueue {
virtual ~ThreadsafeQueue() { }
unsigned long size() const {
std::lock_guard<std::mutex> lock(mutex_);
// std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
T pop() {
virtual T pop() {
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
return {};
@ -45,8 +49,12 @@ class ThreadsafeQueue {
queue_.push(item);
}
bool empty(){
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();
}
};
#endif //_QUEUE_H

BIN
src/sm/libsdf_crypto.so Normal file

Binary file not shown.

BIN
src/sm/libsm.so Normal file

Binary file not shown.

1
src/sm/readme.txt Normal file
View File

@ -0,0 +1 @@
gcc test.c -I. -L. -lsm -lsdf_crypto -o test

438
src/sm/sm.h Normal file
View File

@ -0,0 +1,438 @@
#ifndef AWSM_H
#define AWSM_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef WIN32
#include <Windows.h>
#define DEVAPI __stdcall
#define WINAPI __stdcall
#else
#define __stdcall
#define _stdcall
#define DEVAPI __attribute__((visibility("default")))
#define WINAPI __attribute__((visibility("default")))
#endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define ADMIN_PIN "11111111"
#define USER_PIN "11111111"
#define SM_SUCCESS 0
#define SM_FAIL -1
#define AWSM_FLAG_SIGN 1
#define AWSM_FLAG_ENCRYPT 2
#define SIGN_KEYINDEX 0
#define ENCRYPT_KEYINDEX 1
#define CERT_FILE_SIZE 0x1000
#define ECC_PUBKEY_LEN 64
#define ECC_PRIKEY_LEN 32
#define HASH_DATA_LEN 32
#define SIGN_DATA_LEN 64
#define SGD_SM3 0x00000001
#define SGD_SHA1 0x00000002
#define SGD_SHA256 0x00000004
#define SGD_SHA512 0x00000008
#define SGD_SHA0 0x00000010
#define SGD_SHA224 0x00000020
#define SGD_SHA384 0x00000040
#define DEFAULT_CONFIG "/etc/awconfig/AWSMClient.conf"
/**
* \brief
* \return 0,
*/
unsigned int DEVAPI Device_Init_Conf(const char* conf);
unsigned int DEVAPI Device_Init();
/**
* \brief PIN
* \param admin_PIN
* \param new_user_PIN
* \param retry_count
* \return 0,
*/
unsigned int DEVAPI Unlock_Application_PIN(const char* admin_PIN,
const char* new_user_PIN,
unsigned int* retry_count);
/**
* \brief PIN
* \param old PIN
* \param new PIN
* \param retry_count
* \return 0,
*/
unsigned int DEVAPI Change_Application_PIN(const char* old_pin,
const char* new_pin,
unsigned int* retry_count);
/**
* \brief PIN
* \param PIN PIN
* \param retry_count
* \return 0,
*/
unsigned int DEVAPI Verify_Application_PIN(const char* PIN, unsigned int* retry_count);
/**
* \brief
* \param random
* \param random_len
* \return 0,
*/
unsigned int DEVAPI Generate_Rand(unsigned char* random, const unsigned int random_len);
/**
* \brief SM1_OFB
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM1_OFB_Encrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* enc_data,
unsigned int* enc_data_len);
/**
* \brief SM4_OFB
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM4_OFB_Encrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* enc_data,
unsigned int* enc_data_len);
/**
* \brief SM4_OFB
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM1_OFB_Decrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \brief SM4_OFB
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM4_OFB_Decrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \brief SM1_CBC
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM1_CBC_Encrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* enc_data,
unsigned int* enc_data_len);
/**
* \brief SM4_CBC
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM4_CBC_Encrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* enc_data,
unsigned int* enc_data_len);
/**
* \brief SM4_CBC
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM1_CBC_Decrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \brief SM4_CBC
* \param pIv
* \param key
* \param key_len (16)
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len
* \return 0,
*/
unsigned int DEVAPI SM4_CBC_Decrypt(const unsigned char* pIv,
const unsigned char* key,
const unsigned int key_len,
const unsigned char* data,
const unsigned int data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \brief
* \param eccprikey
* \param data
* \param data_len
* \param sigdata
* \param signdata_len
* \return 0,
*/
unsigned int DEVAPI Generate_SignData_ExtPrikey(const unsigned char* eccprikey,
const unsigned char* data,
const unsigned int datalen,
unsigned char* signdata,
unsigned int* signdata_len);
/**
* \brief
* \param data
* \param data_len
* \param signdata
* \param signdata_len
* \return 0,
*/
unsigned int DEVAPI Generate_SignData_IntPrikey(const unsigned char* data,
const unsigned int data_len,
unsigned char* signdata,
unsigned int* signdata_len);
/**
* \brief
* \param eccpubkey
* \param data
* \param data_len
* \param signdata
* \param signdata_len
* \return 0,
*/
unsigned int DEVAPI Verify_SignData_ExtPubkey(
const unsigned char* eccpubkey,
const unsigned char* data,
const unsigned int data_len,
unsigned char* signdata,
unsigned int signdata_len) ;
/**
* \brief SM2_3
* \param eccpubkey
* \param data
* \param data_len
* \param enc_data
* \param enc_data_len (97)
* \return 0,
*/
unsigned int DEVAPI SM2_3_Encrypt_ExtPubkey(const unsigned char* eccpubkey,
const unsigned char* data,
const unsigned int data_len,
unsigned char* enc_data,
unsigned int* enc_data_len);
/**
* \brief SM2_3
* \param eccprikey
* \param data
* \param data_len
* \param dec_data
* \param dec_data_len
* \return 0,
*/
unsigned int DEVAPI SM2_3_Decrypt_ExtPrikey(const unsigned char* eccprikey,
const unsigned char* data,
const unsigned int data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \brief SM2_3
* \param data
* \param data_len
* \param dec_data
* \param dec_data_len
* \return 0,
*/
unsigned int DEVAPI SM2_3_Decrypt_IntPrikey(const unsigned char* data,
const unsigned int data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \note 使
*/
unsigned int DEVAPI SM2_3_Decrypt_IntCryptPrikey(const unsigned char* enc_data,
const unsigned int enc_data_len,
unsigned char* dec_data,
unsigned int* dec_data_len);
/**
* \brief HASH
* \param data Hash数据
* \param data_len Hash数据长度
* \param hash_data Hash后数据
* \param hash_data_len Hash值长度
* \param eccpubkey Hash公钥
* \param hash_type Hash算法
* (SM3/SHA1/SHA256/SHA512/SHA0/SHA224/SHA384)
* \return 0,
*/
unsigned int DEVAPI Generate_Hash(const unsigned char* data,
const unsigned int data_len,
unsigned char* hash_data,
unsigned int* hash_data_len,
const unsigned char* eccpubkey,
const unsigned int hash_type);
/**
* \brief
* \return 0,
*/
unsigned int DEVAPI Generate_ECCKeyPair();
/**
* \brief ECC
* \param keypairtype 1: ; 2:
* \param eccprikey
* \param eccpubkey
* \return 0,
*/
unsigned int DEVAPI Import_ECCKeyPair(const unsigned long keypairtype,
const unsigned char* eccprikey,
const unsigned char* eccpubkey);
/**
* \brief ECC
* \param keypairtype 1: ; 2:
* \param eccpubkey
* \return 0,
*/
unsigned int DEVAPI Export_publickey(const unsigned long keypairtype, unsigned char* eccpubkey);
/**
* \brief
* \param hwcode
* \return 0,
*/
unsigned int Get_Hwcode(unsigned char* HwCode);
/**
* \brief
* \param cert_type 1: ; 2:
* \param cert_data
* \param cert_data_len
* \return 0,
*/
unsigned int DEVAPI Import_Certificate(const unsigned int cert_type,
const unsigned char* cert_data,
const unsigned int cert_data_len);
/**
* \brief
* \param cert_type 1: ; 2:
* \param cert_data
* \param cert_data_len
* \return 0,
*/
unsigned int DEVAPI Export_Certificate(const unsigned int cert_type,
unsigned char* cert_data,
unsigned int* cert_data_len);
/**
* \brief
* \param rspJson JSON
* \return 0,
* \todo
*/
unsigned int Device_Status();
/**
* \brief
*/
void Clear_Device_App();
/**
* \brief
*/
void Close_Device();
#ifdef __cplusplus
}
#endif
#endif //

286
src/sm/test.c Normal file
View File

@ -0,0 +1,286 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include "sm.h"
#define CP printf("[%s:%d]\n", __FILE__, __LINE__);
#define add_error(str) sprintf(ERROR_list + strlen(ERROR_list), "%s 0x%02x\n", #str, rv)
#define show(str)\
printf("%s_len = %d\n%s = {", #str, str##_len, #str);\
for(int i=0; i < str##_len; i++)\
printf(" 0x%02x,", str[i]);\
printf("};\n\n")
#define title(str)
#define SUCCESS 0
#define CryptKey 0
#define SignKey 1
unsigned char EccPrikey[] =
{
0xa9,0x28,0xa3,0xc2,0x79,0xf1,0x12,0x78,0x4e,0x6b,0x0c,0x59,0xb7,0x22,0xce,0x60,
0xa7,0x70,0x3b,0x93,0x94,0xf0,0xe5,0x93,0x9f,0x7d,0x88,0xfb,0xaf,0xd7,0x98,0xbe
};
unsigned char EccPubkey[] =
{
0x84,0xb1,0xbd,0x45,0x06,0x19,0x9e,0xdb,0x0b,0x50,0x88,0xdf,0xc4,0x89,0x74,0xab,
0x03,0x31,0x54,0x4b,0x8b,0xf0,0xdf,0x84,0x75,0x31,0x4e,0x05,0xb5,0x16,0xf8,0x2d,
0x5d,0x4e,0xc9,0x98,0x22,0x22,0x44,0xfd,0x6d,0x93,0xd3,0xf6,0x6b,0xd9,0x74,0x33,
0x6a,0x4a,0x59,0xe3,0xda,0xfd,0x6a,0x80,0xc9,0x72,0xab,0x55,0xbc,0x20,0xf1,0x1b
};
int main(int argc, char* argv[])
{
int rv = 0;
char ERROR_list[2048];
memset(ERROR_list, 0, sizeof ERROR_list);
title(Device_Init);
rv = Device_Init();
printf("Device_Init rv = 0x%02x\n", rv);
if(rv != SUCCESS)
{
printf("Device_Init Fail\n");
return 0;
}
unsigned char random[20];
unsigned int random_len = 16;
title(Generate_Rand);
rv = Generate_Rand(random, random_len);
if(rv != SUCCESS)
{
printf("Generate_Rand FAIL\n\n");
return 0;
}
else printf("Generate_Rand SUCCESS\n\n");
unsigned char Hash_Data[35];
unsigned int Hash_Data_len = 32;
title(Generate_Hash);
rv = Generate_Hash(random, random_len, Hash_Data, &Hash_Data_len, NULL, SGD_SM3);
if(rv != SUCCESS)
{
printf("Generate_Hash FAIL\n\n");
return 0;
}
else printf("Generate_Hash SUCCESS\n\n");
unsigned char Sign_Data[65];
unsigned int Sign_Data_len = 65;
title(Generate_SignData_ExtPrikey);
rv = Generate_SignData_ExtPrikey(EccPrikey, Hash_Data, Hash_Data_len, Sign_Data, &Sign_Data_len);
if(rv != SUCCESS)
{
printf("Generate_SignData_ExtPrikey Fail\n");
return 0;
}
printf("Generate_SignData_ExtPrikey SUCCESS\n");
title(Verify_SignData_ExtPubkey);
rv = Verify_SignData_ExtPubkey(EccPubkey, Hash_Data, Hash_Data_len, Sign_Data, Sign_Data_len);
if(rv != SUCCESS)
{
printf("Verify_SignData_ExtPubkey Fail\n\n");
return 0;
}
else printf("Verify_SignData_ExtPubkey SUCCESS\n\n");
unsigned char vkek_in[1024];
unsigned int vkek_in_len = 16;
title(Generate_Rand);
rv = Generate_Rand(vkek_in, vkek_in_len);
unsigned char ECC_Encrypt_Data[1024];
unsigned int ECC_Encrypt_Data_len = 1024;
title(SM2_3_Encrypt_ExtPubkey);
rv = SM2_3_Encrypt_ExtPubkey(EccPubkey, vkek_in, vkek_in_len, ECC_Encrypt_Data, &ECC_Encrypt_Data_len);
if(rv != SUCCESS)
{
printf("SM2_3_Encrypt_ExtPubkey Fail\n");
return 0;
}
printf("SM2_3_Encrypt_ExtPubkey SUCCESS\n");
unsigned char vkek_out[1024];
unsigned int vkek_out_len = 16;
title(SM2_3_Decrypt_ExtPrikey);
rv = SM2_3_Decrypt_ExtPrikey(EccPrikey, ECC_Encrypt_Data, ECC_Encrypt_Data_len, vkek_out, &vkek_out_len);
if(rv != SUCCESS)
{
printf("SM2_3_Decrypt_ExtPrikey Fail\n");
return 0;
}
printf("SM2_3_Decrypt_ExtPrikey SUCCESS\n");
unsigned char KeyValue[16] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38 };
unsigned int Key_len = 16;
unsigned char IV[16] = { 0 };
unsigned char EncryptData[200];
unsigned int EncryptData_len = 200;
unsigned char DecryptData[200];
unsigned int DecryptData_len = 200;
unsigned char in[64];
unsigned int in_len = 64;
title(Generate_Rand);
rv = Generate_Rand(in, in_len);
unsigned char Publickey[65];
unsigned char Publickey_len = 64;
/*title(Generate_ECCKeyPair);
rv = Generate_ECCKeyPair();
if(rv != SUCCESS)
{
printf("Generate_ECCKeyPair Fail\n\n");
return 0;
}
else printf("Generate_ECCKeyPair SUCCESS\n\n");
*/
memset(Publickey, 0, sizeof Publickey);
/*
title(Export_publickey);
rv = Export_publickey(CryptKey, Publickey);
if(rv != SUCCESS)
{
printf("Export_Crypt_publickey Fail\n\n");
return 0;
}
else printf("Export_Crypt_publickey SUCCESS\n\n");
*/
title(Export_publickey);
rv = Export_publickey(SignKey, Publickey);
if(rv != SUCCESS)
{
printf("Export_Sign_publickey Fail\n\n");
return 0;
}
else printf("Export_Sign_publickey SUCCESS\n\n");
for(int i=0; i<Publickey_len; i++)
printf("%02x,", Publickey[i]);
unsigned char SignTure[65];
unsigned int SignTure_len = 65;
title(Generate_SignData_IntPrikey);
rv = Generate_SignData_IntPrikey(Hash_Data, Hash_Data_len, SignTure, &SignTure_len);
if(rv != SUCCESS)
{
printf("Generate_SignData_IntPrikey Fail\n\n");
return 0;
}
else printf("Generate_SignData_IntPrikey SUCCESS\n\n");
title(Verify_SignData_ExtPubkey);
rv = Verify_SignData_ExtPubkey(Publickey, Hash_Data, Hash_Data_len, SignTure, SignTure_len);
if(rv != SUCCESS)
{
printf("Verify_SignData_ExtPubkey Fail\n\n");
return 0;
}
else printf("Verify_SignData_ExtPubkey SUCCESS\n\n");
memset(vkek_in, 0, sizeof vkek_in);
memset(vkek_out, 0, sizeof vkek_out);
memset(ECC_Encrypt_Data, 0, sizeof ECC_Encrypt_Data);
vkek_in_len = 128;
vkek_out_len = 512;
ECC_Encrypt_Data_len = 0;
title(Generate_Rand);
rv = Generate_Rand(vkek_in, vkek_in_len);
title(SM2_3_Encrypt_ExtPubkey);
rv = SM2_3_Encrypt_ExtPubkey(Publickey, vkek_in, vkek_in_len, ECC_Encrypt_Data, &ECC_Encrypt_Data_len);
if(rv != SUCCESS)
{
printf("SM2_3_Encrypt_ExtPubkey Fail\n\n");
return 0;
}
else printf("SM2_3_Encrypt_ExtPubkey SUCCESS\n\n");
/*
title(SM2_3_Decrypt_IntPrikey);
rv = SM2_3_Decrypt_IntPrikey(ECC_Encrypt_Data, ECC_Encrypt_Data_len, vkek_out, &vkek_out_len);
if(rv != SUCCESS)
{
printf("SM2_3_Decrypt_IntPrikey Fail\n\n");
return 0;
}
else printf("SM2_3_Decrypt_IntPrikey SUCCESS\n\n");
memset(Publickey, 0, sizeof Publickey);
title(Export_publickey);
rv = Export_publickey(CryptKey, Publickey);
if(rv != SUCCESS)
{
printf("Export_Crypt_publickey Fail\n\n");
return 0;
}
else printf("Export_Crypt_publickey SUCCESS\n\n");
*/
rv = Generate_Rand(vkek_in, vkek_in_len);
title(SM2_3_Encrypt_ExtPubkey);
rv = SM2_3_Encrypt_ExtPubkey(Publickey, vkek_in, vkek_in_len, ECC_Encrypt_Data, &ECC_Encrypt_Data_len);
if(rv != SUCCESS)
{
printf("SM2_3_Encrypt_ExtPubkey Fail\n\n");
return 0;
}
else printf("SM2_3_Encrypt_ExtPubkey SUCCESS\n\n");
/*
title(SM2_3_Decrypt_IntCryptPrikey);
rv = SM2_3_Decrypt_IntCryptPrikey(ECC_Encrypt_Data, ECC_Encrypt_Data_len, vkek_out, &vkek_out_len);
if(rv != SUCCESS)
{
printf("SM2_3_Decrypt_IntCryptPrikey Fail\n\n");
return 0;
}
else printf("SM2_3_Decrypt_IntCryptPrikey SUCCESS\n\n");
*/
unsigned char ans[2049] = {0};
unsigned int ans_len = 2049;
title(Device_Status);
rv = Device_Status();
if(rv != SUCCESS)
{
printf("Device_Init Fail\n");
return 0;
}
else printf("Device_Status SUCCESS\n\n");
unsigned char hw_code[23] = { 0 };
title(Get_Hwcode);
rv = Get_Hwcode(hw_code);
if(rv != SUCCESS)
{
printf("Get_Hwcode Fail\n");
return 0;
}
printf("hw_code is: %s\n\n", hw_code);
Close_Device();
printf("Initial OK\n");
return 0;
}

BIN
src/sm/testsm Normal file

Binary file not shown.

5238
test/H264TCP.h Normal file

File diff suppressed because it is too large Load Diff

20277
test/H265TCP.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,12 +6,16 @@
#include<functional>
#include "common.h"
#include "HuaWei/HWsign.h"
#include "data264.h"
#include "HuaWei/HWsec.h"
// #include "data264.h"
// #include "data265.h"
#include "H265TCP.h"
#include "putdata.h"
#include<netinet/in.h>
#include <arpa/inet.h>
#include<sys/socket.h>
extern void pushdata(RTPpackage::Ptr ptr);
using namespace std;
class UdpSocket{
private:
@ -141,109 +145,144 @@ void print_rtp(const char * buf, uint32_t len){
}
// void UDPtest(){
// // EXPAND(DEFER(A)());
// void * sign_handle=nullptr;
// auto udpsocket=new UdpSocket();
// udpsocket->Socket();
// // udpsocket.Send()
// // [udpsocket](const char * rtp_ptr, const uint32_t rtp_len){
// // udpsocket->Send(rtp_ptr,rtp_len,string("192.168.123.1"),30000);
// // // print_rtp(rtp_ptr,rtp_len);
// // }
// uint8_t mask;
// uint16_t seq=0;
// uint32_t stamp=0;
// int* id;
// int ret,count;
// void * Verify_handle=nullptr;
// SDF_Device_open();
// Verify_handle=HWVerify_init();
// sign_handle=HWSign_init();
// // ARRAY(2,42)
// // ARRAY(3,42)
// // ARRAY(4,42)
// // ARRAY(5,42) //I
// // ARRAY(6,42)
// // ARRAY(7,42)
// // ARRAY(8,42)
// // ARRAY(9,42)
// // ARRAY(10,42)
// // ARRAY(11,42)
// // ARRAY(12,42)
// // ARRAY(13,42)
// // ARRAY(14,42)
// // ARRAY(15,42)
// // ARRAY(16,42)
// // ARRAY(17,42)
// // ARRAY(18,42)
// // ARRAY(19,42)
// for (int loop=0;loop<=1;loop++){
// ARRAY(1,42)
// ARRAY(2,42)
// ARRAY(3,42)
// ARRAY(4,42)
// ARRAY(5,42) //I
// ARRAY(6,42)
// ARRAY(7,42)
// ARRAY(8,42)
// ARRAY(9,42)
// ARRAY(10,42)
// ARRAY(11,42)
// ARRAY(12,42)
// ARRAY(13,42)
// ARRAY(14,42)
// ARRAY(15,42)
// ARRAY(16,42)
// ARRAY(17,42)
// ARRAY(18,42)
// ARRAY(19,42)
// ARRAY(20,42)
// ARRAY(21,42)
// ///Verify init
// int vef_ret=-2;
// //////////////////////
// ret=1;count=0;
// while (ret==1)
// {
// char buf_h[2048];
// uint32_t len;
// int* id;
// count++;
// ret=HWSign_rtp_out(sign_handle,buf_h,&len,(void **)&id);
// // if(len>1400){
// // buf_h[1000]=0x01;
// // }
// vef_ret=HWVerify_rtp_input(Verify_handle,buf_h,len,0,nullptr);
// printf("verify status: %d \n",vef_ret);
// if (ret==-1) break;
// udpsocket->Send((char*)buf_h,len,string("192.168.123.1"),30000);
// // printf("%d %d %d\n",count,*id,len);
// free(id);
// // printf(" modify @@@@@@@@@ id %d seq %d stamp %d mask %d",id,seq,stamp,mask);
// // print_data(buf_h,len);
// // sleep(0.1);
// /* code */
// }
// }
// // printf("raw");
// // print_data((const char*)pkt2+42,sizeof(pkt2)-42);
// // HWSign_rtp_input(sign_handle,(const char*)pkt2+42,sizeof(pkt2)-42,0);
// // printf("raw");
// // print_data((const char*)pkt3+42,sizeof(pkt3)-42);
// // HWSign_rtp_input(sign_handle,(const char*)pkt3+42,sizeof(pkt3)-42,0);
// HWSign_release(sign_handle);
// HWVerify_release(Verify_handle);
// SDF_Device_close();
// }
int main(){
// EXPAND(DEFER(A)());
void * sign_handle=nullptr;
auto udpsocket=new UdpSocket();
udpsocket->Socket();
// udpsocket.Send()
// [udpsocket](const char * rtp_ptr, const uint32_t rtp_len){
// udpsocket->Send(rtp_ptr,rtp_len,string("192.168.123.1"),30000);
// // print_rtp(rtp_ptr,rtp_len);
// }
uint8_t mask;
uint16_t seq=0;
uint32_t stamp=0;
int* id;
int ret,count;
sign_handle=HWSign_init();
// ARRAY(2,42)
// ARRAY(3,42)
// ARRAY(4,42)
// ARRAY(5,42) //I
// ARRAY(6,42)
// ARRAY(7,42)
// ARRAY(8,42)
// ARRAY(9,42)
// ARRAY(10,42)
// ARRAY(11,42)
// ARRAY(12,42)
// ARRAY(13,42)
// ARRAY(14,42)
// ARRAY(15,42)
// ARRAY(16,42)
// ARRAY(17,42)
// ARRAY(18,42)
// ARRAY(19,42)
void * hw_sec_ptr= HWSign_tcp_init();
pushdata(hw_sec_ptr);
ARRAY(1,42)
ARRAY(2,42)
ARRAY(3,42)
ARRAY(4,42)
ARRAY(5,42) //I
ARRAY(6,42)
ARRAY(7,42)
ARRAY(8,42)
ARRAY(9,42)
ARRAY(10,42)
ARRAY(11,42)
ARRAY(12,42)
ARRAY(13,42)
ARRAY(14,42)
ARRAY(15,42)
ARRAY(16,42)
ARRAY(17,42)
ARRAY(18,42)
ARRAY(19,42)
ARRAY(20,42)
ARRAY(21,42)
///Verify init
int vef_ret=-2;
void * Verify_handle=nullptr;
Verify_handle=HWVerify_init();
//////////////////////
ret=1;count=0;
while (ret==1)
{
char buf_h[2048];
uint32_t len;
int* id;
count++;
ret=HWSign_rtp_out(sign_handle,buf_h,&len,(void **)&id);
// if(len>1400){
// buf_h[1000]=0x01;
// }
vef_ret=HWVerify_rtp_input(Verify_handle,buf_h,len,0,nullptr);
printf("verify status: %d \n",vef_ret);
if (ret==-1) break;
udpsocket->Send((char*)buf_h,len,string("192.168.123.1"),30000);
printf("%d %d %d\n",count,*id,len);
free(id);
// printf(" modify @@@@@@@@@ id %d seq %d stamp %d mask %d",id,seq,stamp,mask);
// print_data(buf_h,len);
// sleep(0.1);
/* code */
char buf_out[2048];
int ret=1;
uint32_t * param;
uint32_t len=0;
do
{
ret=HWSign_tcp_rtp_out(hw_sec_ptr,buf_out,&len,(void **)&param);
if(ret!=1) break;
if(param){
WRNGL("param: %u len:%u \n",*param,len);
delete param;
}
// printf("raw");
// print_data((const char*)pkt2+42,sizeof(pkt2)-42);
// HWSign_rtp_input(sign_handle,(const char*)pkt2+42,sizeof(pkt2)-42,0);
// printf("raw");
// print_data((const char*)pkt3+42,sizeof(pkt3)-42);
// HWSign_rtp_input(sign_handle,(const char*)pkt3+42,sizeof(pkt3)-42,0);
}while (ret==1);
HWSign_tcp_release(hw_sec_ptr);
HWSign_release(sign_handle);
HWVerify_release(Verify_handle);
}

129
test/putdata.cpp Normal file
View File

@ -0,0 +1,129 @@
#include "putdata.h"
// #include "data264.h"
// #include "data265.h"
#include "H265TCP.h"
#include "HuaWei/HWsign.h"
#define dataPut(x,offset) { \
DEBUGL("pkg id:"#x"\n"); \
uint32_t *id=(uint32_t *)malloc(sizeof(uint32_t)); *id=x;\
HWSign_tcp_rtp_input(ptr,(char*)pkt##x+offset,sizeof(pkt##x)-offset,(void*)id); \
}
#define TCPdataPut(x) dataPut(x,0x36)
void pushdata(void* ptr){
// for(int i =1 ;i<= 105; i++){
// printf("TCPdataPut(%d)\n",i);
// }
TCPdataPut(1)
TCPdataPut(2)
TCPdataPut(3)
TCPdataPut(4)
TCPdataPut(5)
TCPdataPut(6)
TCPdataPut(7)
TCPdataPut(8)
TCPdataPut(9)
TCPdataPut(10)
TCPdataPut(11)
TCPdataPut(12)
TCPdataPut(13)
TCPdataPut(14)
TCPdataPut(15)
TCPdataPut(16)
TCPdataPut(17)
TCPdataPut(18)
TCPdataPut(19)
TCPdataPut(20)
TCPdataPut(21)
TCPdataPut(22)
TCPdataPut(23)
TCPdataPut(24)
TCPdataPut(25)
TCPdataPut(26)
TCPdataPut(27)
TCPdataPut(28)
TCPdataPut(29)
TCPdataPut(30)
TCPdataPut(31)
TCPdataPut(32)
TCPdataPut(33)
TCPdataPut(34)
TCPdataPut(35)
TCPdataPut(36)
TCPdataPut(37)
TCPdataPut(38)
TCPdataPut(39)
TCPdataPut(40)
TCPdataPut(41)
TCPdataPut(42)
TCPdataPut(43)
TCPdataPut(44)
TCPdataPut(45)
TCPdataPut(46)
TCPdataPut(47)
TCPdataPut(48)
TCPdataPut(49)
TCPdataPut(50)
TCPdataPut(51)
TCPdataPut(52)
TCPdataPut(53)
TCPdataPut(54)
TCPdataPut(55)
TCPdataPut(56)
TCPdataPut(57)
TCPdataPut(58)
TCPdataPut(59)
TCPdataPut(60)
TCPdataPut(61)
TCPdataPut(62)
TCPdataPut(63)
TCPdataPut(64)
TCPdataPut(65)
TCPdataPut(66)
TCPdataPut(67)
TCPdataPut(68)
TCPdataPut(69)
TCPdataPut(70)
TCPdataPut(71)
TCPdataPut(72)
TCPdataPut(73)
TCPdataPut(74)
TCPdataPut(75)
TCPdataPut(76)
TCPdataPut(77)
TCPdataPut(78)
TCPdataPut(79)
TCPdataPut(80)
TCPdataPut(81)
TCPdataPut(82)
TCPdataPut(83)
TCPdataPut(84)
TCPdataPut(85)
TCPdataPut(86)
TCPdataPut(87)
TCPdataPut(88)
TCPdataPut(89)
TCPdataPut(90)
TCPdataPut(91)
TCPdataPut(92)
TCPdataPut(93)
TCPdataPut(94)
TCPdataPut(95)
TCPdataPut(96)
TCPdataPut(97)
TCPdataPut(98)
TCPdataPut(99)
TCPdataPut(100)
TCPdataPut(101)
TCPdataPut(102)
TCPdataPut(103)
TCPdataPut(104)
TCPdataPut(105)
TCPdataPut(106)
// TCPdataPut(107)
// TCPdataPut(108)
// TCPdataPut(109)
}

17
test/putdata.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef _PUTDATA_H
#define _PUTDATA_H
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include<string>
#include<functional>
#include "common.h"
#include "HuaWei/HWsign.h"
#include "HuaWei/HWsec.h"
#include "HuaWei/RTP.h"
void pushdata(void * ptr) ;
#endif