From 3993071522f81426a74e0294841ff7399358bf78 Mon Sep 17 00:00:00 2001 From: Jean-Michel Friedt Date: Sun, 22 Dec 2024 13:54:14 +0100 Subject: [PATCH] replace int with encr_type enum --- include/gnuradio/m17/m17_coder.h | 13 +++++-- lib/m17_coder_impl.cc | 35 +++++++++-------- lib/m17_coder_impl.h | 51 +++++++++++-------------- python/m17/bindings/m17_coder_python.cc | 2 +- 4 files changed, 50 insertions(+), 51 deletions(-) diff --git a/include/gnuradio/m17/m17_coder.h b/include/gnuradio/m17/m17_coder.h index ca91f7b..d4e66e6 100644 --- a/include/gnuradio/m17/m17_coder.h +++ b/include/gnuradio/m17/m17_coder.h @@ -23,6 +23,13 @@ class M17_API m17_coder : virtual public gr::block { public: typedef std::shared_ptr sptr; + typedef enum + { + ENCR_NONE, + ENCR_SCRAM, + ENCR_AES, + ENCR_RES //reserved + } encr_t; /*! * \brief Return a shared_ptr to a new instance of m17::m17_coder. @@ -32,17 +39,17 @@ public: * class. m17::m17_coder::make is the public interface for * creating new instances. */ - static sptr make(std::string src_id,std::string dst_id,int mode,int data,int encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug, bool signed_str); + static sptr make(std::string src_id,std::string dst_id,int mode,int data,encr_t encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug, bool signed_str); virtual void set_key(std::string meta)=0; virtual void set_meta(std::string meta)=0; virtual void set_src_id(std::string src_id)=0; virtual void set_dst_id(std::string dst_id)=0; virtual void set_debug(bool debug)=0; virtual void set_signed(bool signed_str)=0; - virtual void set_type(int mode,int data,int encr_type,int encr_subtype,int can)=0; + virtual void set_type(int mode,int data,encr_t encr_type,int encr_subtype,int can)=0; virtual void set_mode(int mode)=0; virtual void set_data(int data)=0; - virtual void set_encr_type(int encr_type)=0; + virtual void set_encr_type(encr_t encr_type)=0; virtual void set_encr_subtype(int encr_subtype)=0; virtual void set_can(int can)=0; }; diff --git a/lib/m17_coder_impl.cc b/lib/m17_coder_impl.cc index 1a7b957..c2443fb 100644 --- a/lib/m17_coder_impl.cc +++ b/lib/m17_coder_impl.cc @@ -48,7 +48,7 @@ namespace gr { namespace m17 { m17_coder::sptr - m17_coder::make(std::string src_id,std::string dst_id,int mode,int data,int encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug, bool signed_str) + m17_coder::make(std::string src_id,std::string dst_id,int mode,int data,encr_t encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug, bool signed_str) { return gnuradio::get_initial_sptr (new m17_coder_impl(src_id,dst_id,mode,data,encr_type,encr_subtype,can,meta,key,debug,signed_str)); @@ -57,7 +57,7 @@ namespace gr { /* * The private constructor */ - m17_coder_impl::m17_coder_impl(std::string src_id,std::string dst_id,int mode,int data,int encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug,bool signed_str) + m17_coder_impl::m17_coder_impl(std::string src_id,std::string dst_id,int mode,int data,encr_t encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug,bool signed_str) : gr::block("m17_coder", gr::io_signature::make(1, 1, sizeof(char)), gr::io_signature::make(1, 1, sizeof(float))) @@ -75,15 +75,14 @@ namespace gr { _got_lsf=0; //have we filled the LSF struct yet? _fn=0; //16-bit Frame Number (for the stream mode) _finished=false; - if(_encr_type==2) - +#ifdef AES + srand(time(NULL)); //random number generator (for IV rand() seed value) + memset(_key, 0, 32*sizeof(uint8_t)); + memset(_iv, 0, 16*sizeof(uint8_t)); +#endif + if(_encr_type==ENCR_AES) { set_key(key); // read key -#ifdef AES - srand(time(NULL)); //random number generator (for IV rand() seed value) - memset(_key, 0, 32*sizeof(uint8_t)); - memset(_iv, 0, 16*sizeof(uint8_t)); -#endif *((int32_t*)&_iv[0])=(uint32_t)time(NULL)-(uint32_t)epoch; //timestamp for(uint8_t i=4; i<4+10; i++) _iv[i]=0; //10 random bytes TODO: replace with a rand() or pass through an additional arg } @@ -180,7 +179,7 @@ void m17_coder_impl::set_data(int data) set_type(_mode,_data,_encr_type,_encr_subtype,_can); } -void m17_coder_impl::set_encr_type(int encr_type) +void m17_coder_impl::set_encr_type(encr_t encr_type) {_encr_type=encr_type; printf("new encr type: %x -> ",_encr_type); set_type(_mode,_data,_encr_type,_encr_subtype,_can); @@ -198,7 +197,7 @@ void m17_coder_impl::set_can(int can) set_type(_mode,_data,_encr_type,_encr_subtype,_can); } -void m17_coder_impl::set_type(int mode,int data,int encr_type,int encr_subtype,int can) +void m17_coder_impl::set_type(int mode,int data,encr_t encr_type,int encr_subtype,int can) {short tmptype; tmptype = mode | (data<<1) | (encr_type<<3) | (encr_subtype<<5) | (can<<7); _lsf.type[0]=tmptype>>8; // MSB @@ -401,12 +400,12 @@ void m17_coder_impl::parse_raw_key_string(uint8_t* dest, const char* inp) _next_lsf.src[4] = 0xD1; _next_lsf.src[5] = 0x06; - if(_encryption==ENCR_AES) //AES ENC, 3200 voice + if(_encr_type==ENCR_AES) //AES ENC, 3200 voice { _next_lsf.type[0] = 0x03; _next_lsf.type[1] = 0x95; } - else if(_encryption==ENCR_SCRAM) //Scrambler ENC, 3200 Voice + else if(_encr_type==ENCR_SCRAM) //Scrambler ENC, 3200 Voice { _next_lsf.type[0] = 0x00; _next_lsf.type[1] = 0x00; @@ -450,7 +449,7 @@ void m17_coder_impl::parse_raw_key_string(uint8_t* dest, const char* inp) */ //AES encryption enabled - use 112 bits of IV - if(_encryption==ENCR_AES) + if(_encr_type==ENCR_AES) { memcpy(&(_lsf.meta), _iv, 14); _iv[14] = (_fn >> 8) & 0x7F; @@ -504,12 +503,12 @@ void m17_coder_impl::parse_raw_key_string(uint8_t* dest, const char* inp) _next_lsf.src[4] = 0xD1; _next_lsf.src[5] = 0x06; - if(_encryption==ENCR_AES) //AES ENC, 3200 voice + if(_encr_type==ENCR_AES) //AES ENC, 3200 voice { _next_lsf.type[0] = 0x03; _next_lsf.type[1] = 0x95; } - else if(_encryption==ENCR_SCRAM) //Scrambler ENC, 3200 Voice + else if(_encr_type==ENCR_SCRAM) //Scrambler ENC, 3200 Voice { _next_lsf.type[0] = 0x00; _next_lsf.type[1] = 0x00; @@ -555,7 +554,7 @@ void m17_coder_impl::parse_raw_key_string(uint8_t* dest, const char* inp) } //AES - if(_encryption==ENCR_AES) + if(_encr_type==ENCR_AES) { memcpy(&(_next_lsf.meta), _iv, 14); _iv[14] = (_fn >> 8) & 0x7F; @@ -564,7 +563,7 @@ void m17_coder_impl::parse_raw_key_string(uint8_t* dest, const char* inp) } //Scrambler - else if(_encryption==ENCR_SCRAM) + else if(_encr_type==ENCR_SCRAM) { scrambler_sequence_generator(); for(uint8_t i=0; i<16; i++) diff --git a/lib/m17_coder_impl.h b/lib/m17_coder_impl.h index 5c678b8..6f799e0 100644 --- a/lib/m17_coder_impl.h +++ b/lib/m17_coder_impl.h @@ -29,7 +29,25 @@ class m17_coder_impl : public m17_coder { private: unsigned char _src_id[10],_dst_id[10]; // 9 character callsign - int _mode,_data,_encr_type,_encr_subtype,_can; + int _mode,_data; +//encryption +#ifdef AES +//encryption +const struct uECC_Curve_t* _curve = uECC_secp256r1(); +encr_t _encr_type=ENCR_NONE; +//AES +typedef enum +{ + AES128, + AES192, + AES256 +} aes_t; +uint8_t _key[32]; +uint8_t _iv[16]; +time_t epoch = 1577836800L; //Jan 1, 2020, 00:00:00 UTC +#endif + + int _encr_subtype,_can; lsf_t _lsf, _next_lsf; std::string _meta; int _got_lsf=0; @@ -43,31 +61,6 @@ uint8_t _priv_key_loaded=0; //do we have a sig key loaded? uint8_t _priv_key[32]={0}; //private key uint8_t _sig[64]={0}; //ECDSA signature -//encryption -#ifdef AES -//encryption -const struct uECC_Curve_t* _curve = uECC_secp256r1(); - -typedef enum -{ - ENCR_NONE, - ENCR_SCRAM, - ENCR_AES, - ENCR_RES //reserved -} encr_t; -encr_t _encryption=ENCR_NONE; - -//AES -typedef enum -{ - AES128, - AES192, - AES256 -} aes_t; -uint8_t _key[32]; -uint8_t _iv[16]; -time_t epoch = 1577836800L; //Jan 1, 2020, 00:00:00 UTC -#endif #ifdef ECC //Scrambler @@ -84,15 +77,15 @@ public: void set_dst_id(std::string dst_id); void set_key(std::string key); void set_meta(std::string meta); - void set_type(int mode,int data,int encr_type,int encr_subtype,int can); + void set_type(int mode,int data,encr_t encr_type,int encr_subtype,int can); void set_mode(int mode); void set_data(int data); - void set_encr_type(int encr_type); + void set_encr_type(encr_t encr_type); void set_encr_subtype(int encr_subtype); void set_can(int can); void set_debug(bool debug); void set_signed(bool signed_str); - m17_coder_impl(std::string src_id,std::string dst_id,int mode,int data,int encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug,bool signed_str); + m17_coder_impl(std::string src_id,std::string dst_id,int mode,int data,encr_t encr_type,int encr_subtype,int can,std::string meta, std::string key, bool debug,bool signed_str); ~m17_coder_impl(); // Where all the action really happens diff --git a/python/m17/bindings/m17_coder_python.cc b/python/m17/bindings/m17_coder_python.cc index fa751be..8f2bddf 100644 --- a/python/m17/bindings/m17_coder_python.cc +++ b/python/m17/bindings/m17_coder_python.cc @@ -16,7 +16,7 @@ /* BINDTOOL_GEN_AUTOMATIC(0) */ /* BINDTOOL_USE_PYGCCXML(0) */ /* BINDTOOL_HEADER_FILE(m17_coder.h) */ -/* BINDTOOL_HEADER_FILE_HASH(27a5be021e5302ff55aaec73ed21e81b) */ +/* BINDTOOL_HEADER_FILE_HASH(b7a33ca6c9d8718b66bcf7e7bf5a7569) */ /***********************************************************************************/ #include