getting ready for AES and ECC, not yet implemented

main
Jean-Michel Friedt 2024-06-20 17:57:55 +02:00
parent b7036eab41
commit 3e11b7bf45
8 changed files with 189 additions and 57 deletions

View File

@ -28,7 +28,7 @@ options:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 4.0]
coordinate: [16, 8.0]
rotation: 0
state: enabled
@ -76,7 +76,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [384, 332.0]
coordinate: [384, 348.0]
rotation: 0
state: enabled
- name: enc_type
@ -106,7 +106,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [248, 332.0]
coordinate: [248, 348.0]
rotation: 0
state: enabled
- name: mode
@ -136,7 +136,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [16, 332.0]
coordinate: [16, 348.0]
rotation: 0
state: enabled
- name: samp_rate
@ -151,6 +151,29 @@ blocks:
coordinate: [192, 12.0]
rotation: 0
state: true
- name: signed_str
id: variable_qtgui_toggle_button_msg
parameters:
comment: ''
gui_hint: ''
initPressed: 'False'
label: SignedStr
outputmsgname: value
pressBackgroundColor: default
pressFontColor: default
pressed: 'True'
relBackgroundColor: default
relFontColor: default
released: 'False'
type: bool
value: 'False'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [544, 352.0]
rotation: 0
state: enabled
- name: src_str
id: variable_qtgui_entry
parameters:
@ -194,7 +217,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [120, 332.0]
coordinate: [120, 348.0]
rotation: 0
state: enabled
- name: blocks_file_sink_0
@ -212,7 +235,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [960, 152.0]
coordinate: [960, 160.0]
rotation: 0
state: enabled
- name: blocks_null_sink_0
@ -250,7 +273,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [568, 160.0]
coordinate: [568, 168.0]
rotation: 0
state: true
- name: blocks_vector_source_x_0_2
@ -271,7 +294,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [16, 152.0]
coordinate: [16, 160.0]
rotation: 0
state: true
- name: m17_m17_coder_0
@ -292,6 +315,7 @@ blocks:
meta: '''\x00\x00\x65\x41\xb0\x93\xff\x00'''
minoutbuf: '0'
mode: mode
signed_str: signed_str
src_id: src_str
type: type_val
states:
@ -317,7 +341,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [784, 144.0]
coordinate: [784, 152.0]
rotation: 0
state: true
- name: note_0_2

View File

@ -43,6 +43,15 @@ parameters:
label: Meta
dtype: string
default: ''
- id: key
label: AES Key
dtype: string
default: ''
- id: signed_str
label: SignedStr
dtype: bool
default: 'False'
options: ['True', 'False']
- id: debug
label: Debug
dtype: bool
@ -51,11 +60,12 @@ parameters:
asserts:
- ${ can <= 15 }
- ${ len(key) <= 32 }
- ${ len(dst_id) < 10 }
- ${ len(src_id) < 10 }
templates:
imports: from gnuradio import m17
make: m17.m17_coder(${src_id},${dst_id},${mode},${type},${encr_type},${encr_subtype},${can},${meta},${debug})
make: m17.m17_coder(${src_id},${dst_id},${mode},${type},${encr_type},${encr_subtype},${can},${meta},${key},${debug},${signed_str})
callbacks:
- set_meta(${meta})
- set_src_id(${src_id})
@ -65,7 +75,9 @@ templates:
- set_encr_type(${encr_type})
- set_encr_subtype(${encr_subtype})
- set_can(${can})
- set_key(${key})
- set_debug(${debug})
- set_signed(${signed_str})
# Make one 'inputs' list entry per input and one 'outputs' list entry per output.
# Keys include:

View File

@ -32,11 +32,13 @@ 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,bool debug);
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);
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_mode(int mode)=0;
virtual void set_data(int data)=0;

View File

@ -29,6 +29,7 @@ list(APPEND m17_sources
../M17_Implementations/libm17/phy/randomize.c
../M17_Implementations/libm17/phy/sync.c
../M17_Implementations/libm17/phy/slice.c
# ../M17_Implementations/tiny-AES-c/aes.c
)
set(m17_sources "${m17_sources}" PARENT_SCOPE)
@ -42,6 +43,7 @@ target_link_libraries(gnuradio-m17 gnuradio::gnuradio-runtime)
target_include_directories(gnuradio-m17
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../M17_Implementations/libm17/>
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../M17_Implementations/tiny-AES-c/>
PUBLIC $<INSTALL_INTERFACE:include>
)
set_target_properties(gnuradio-m17 PROPERTIES DEFINE_SYMBOL "gnuradio_m17_EXPORTS")

View File

@ -18,6 +18,8 @@
* Boston, MA 02110-1301, USA.
*/
// 240620: todo uncomment #idef AES for cryptography and #ifdef ECC for signature
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -32,30 +34,39 @@
#include "m17.h"
#ifdef AES
#include "aes.hpp"
#endif
#ifdef ECC
#include "../../micro-ecc/uECC.h"
#endif
namespace gr {
namespace m17 {
struct LSF lsf;
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, bool debug)
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)
{
return gnuradio::get_initial_sptr
(new m17_coder_impl(src_id,dst_id,mode,data,encr_type,encr_subtype,can,meta,debug));
(new m17_coder_impl(src_id,dst_id,mode,data,encr_type,encr_subtype,can,meta,key,debug,signed_str));
}
/*
* 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, bool debug)
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)
: gr::block("m17_coder",
gr::io_signature::make(1, 1, sizeof(char)),
gr::io_signature::make(1, 1, sizeof(float)))
, _mode(mode),_data(data),_encr_type(encr_type),_encr_subtype(encr_subtype), _can(can),_meta(meta), _debug(debug)
, _mode(mode),_data(data),_encr_type(encr_type),_encr_subtype(encr_subtype), _can(can), _meta(meta), _debug(debug), _signed_str(signed_str)
{ set_type(mode, data, encr_type, encr_subtype, can);
set_meta(meta); // depends on ^^^ encr_subtype
set_src_id(src_id);
set_dst_id(dst_id);
set_signed(signed_str);
set_debug(debug);
set_output_multiple(192);
uint16_t ccrc=LSF_CRC(&lsf);
@ -63,6 +74,22 @@ struct LSF lsf;
lsf.crc[1]=ccrc&0xFF;
_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)
{
set_key(key); // read key
#ifdef AES
AES_init_ctx(&_ctx, _key);
#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
}
}
void m17_coder_impl::set_signed(bool signed_str)
{_signed_str=signed_str;
if (_signed_str==true) printf("Signed\n"); else printf("Unsigned\n");
}
void m17_coder_impl::set_debug(bool debug)
@ -93,6 +120,24 @@ void m17_coder_impl::set_dst_id(std::string dst_id)
lsf.crc[1]=ccrc&0xFF;
}
void m17_coder_impl::set_key(std::string arg) // *UTF-8* encoded byte array
{int length;
printf("new key: ");
length=arg.size();
int i=0,j=0;
while ((j<32) && (i<length))
{if ((unsigned int)arg.data()[i]<0xc2) // https://www.utf8-chartable.de/
{_key[j]=arg.data()[i];i++;j++;}
else
{_key[j]=(arg.data()[i]-0xc2)*0x40+arg.data()[i+1];i+=2;j++;}
}
length=j; // index from 0 to length-1
printf("%d bytes: ",length);
for (i=0;i<length;i++) printf("%02X ",_key[i]);
printf("\n");
fflush(stdout);
}
void m17_coder_impl::set_meta(std::string meta) // either an ASCII string if encr_subtype==0 or *UTF-8* encoded byte array
{int length;
printf("new meta: ");
@ -213,8 +258,13 @@ void m17_coder_impl::set_type(int mode,int data,int encr_type,int encr_subtype,i
unpack_LICH(enc_bits, lich_encoded);
//encode the rest of the frame (starting at bit 96 - 0..95 are filled with LICH)
// conv_encode_stream_frame(&enc_bits[96], data, finished ? (_fn | 0x8000) : _fn); JMF review
conv_encode_stream_frame(&enc_bits[96], data, _fn);
if(!_signed_str)
conv_encode_stream_frame(&enc_bits[96], data, _finished ? (_fn|0x8000) : _fn);
else //dont set the MSB is the stream is signed
{
conv_encode_stream_frame(&enc_bits[96], data, _fn);
}
//reorder bits
reorder_bits(rf_bits, enc_bits);
@ -222,32 +272,40 @@ void m17_coder_impl::set_type(int mode,int data,int encr_type,int encr_subtype,i
//randomize
randomize_bits(rf_bits);
//send dummy symbols (debug)
/*float s=0.0;
for(uint8_t i=0; i<SYM_PER_PLD; i++) //40ms * 4800 - 8 (syncword)
write(STDOUT_FILENO, (uint8_t*)&s, sizeof(float));*/
//send frame data
send_data(out, &countout, rf_bits);
float s;
for(uint16_t i=0; i<SYM_PER_PLD; i++) //40ms * 4800 - 8 (syncword)
{
s=symbol_map[rf_bits[2*i]*2+rf_bits[2*i+1]];
// write(STDOUT_FILENO, (uint8_t*)&s, sizeof(float));
out[countout]=s;
countout++;
}
/*
if (_debug==true)
{printf("\tTX DATA: ");
for(uint8_t i=0; i<16; i++)
printf("%02X", data[i]);
printf("\n");
}
*/
//increment the Frame Number
_fn = (_fn + 1) % 0x8000;
//increment the LICH counter
lich_cnt = (lich_cnt + 1) % 6;
if(_finished && _signed_str) //if we are done, and the stream is signed, so we need to transmit the signature (4 frames)
{
for(uint8_t i=0; i<sizeof(_priv_key); i++) //test fill
_priv_key[i]=i;
#ifdef ECC
uECC_sign(priv_key, digest, sizeof(digest), _sig, curve);
#endif
//4 frames with 512-bit signature
_fn = 0x7FFC; //signature has to start at 0x7FFC to end at 0x7FFF (0xFFFF with EoT marker set)
for(uint8_t i=0; i<4; i++)
{
send_syncword(out, &countout, SYNC_STR);
extract_LICH(lich, lich_cnt, &lsf); //continue with next LICH_CNT
encode_LICH(lich_encoded, lich);
unpack_LICH(enc_bits, lich_encoded);
conv_encode_stream_frame(&enc_bits[96], &_sig[i*16], _fn);
reorder_bits(rf_bits, enc_bits);
randomize_bits(rf_bits);
send_data(out, &countout, rf_bits);
_fn = (_fn<0x7FFE) ? _fn+1 : (0x7FFF|0x8000);
lich_cnt = (lich_cnt + 1) % 6;
}
}
}
else //LSF
{
@ -272,23 +330,23 @@ void m17_coder_impl::set_type(int mode,int data,int encr_type,int encr_subtype,i
send_data(out, &countout, rf_bits);
if (_debug==true)
{printf("TX DST: ");
for(uint8_t i=0; i<6; i++)
printf("%hhX", lsf.dst[i]);
printf(" SRC: ");
for(uint8_t i=0; i<6; i++)
printf("%hhX", lsf.src[i]);
printf(" TYPE: ");
for(uint8_t i=0; i<2; i++)
printf("%hhX", lsf.type[i]);
printf(" META: ");
for(uint8_t i=0; i<14; i++)
printf("%hhX", lsf.meta[i]);
printf(" CRC: ");
for(uint8_t i=0; i<2; i++)
printf("%hhX", lsf.crc[i]);
printf("\n");
}
{printf("TX DST: ");
for(uint8_t i=0; i<6; i++)
printf("%hhX", lsf.dst[i]);
printf(" SRC: ");
for(uint8_t i=0; i<6; i++)
printf("%hhX", lsf.src[i]);
printf(" TYPE: ");
for(uint8_t i=0; i<2; i++)
printf("%hhX", lsf.type[i]);
printf(" META: ");
for(uint8_t i=0; i<14; i++)
printf("%hhX", lsf.meta[i]);
printf(" CRC: ");
for(uint8_t i=0; i<2; i++)
printf("%hhX", lsf.crc[i]);
printf("\n");
}
}
}
}

View File

@ -10,6 +10,10 @@
#include <gnuradio/m17/m17_coder.h>
#ifdef AES
#include "aes.hpp"
#endif
namespace gr {
namespace m17 {
@ -23,11 +27,29 @@ private:
int _got_lsf=0;
uint16_t _fn=0; //16-bit Frame Number (for the stream mode)
bool _debug=0;
bool _signed_str,_finished;
//encryption
uint8_t _encryption=0;
#ifdef AES
struct AES_ctx _ctx;
#else
#define AES_KEYLEN 32
#define AES_BLOCKLEN 32
#endif
uint8_t _key[AES_KEYLEN]; // ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}; //TODO: replace with a `-K` arg key entry
uint8_t _priv_key[32];
uint8_t _sig[64];
uint8_t iv[AES_BLOCKLEN];
time_t epoch = 1577836800L; //Jan 1, 2020, 00:00:00 UTC
#ifdef ECC
const struct uECC_Curve_t* curve = uECC_secp256r1();
#endif
public:
void set_src_id(std::string src_id);
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_mode(int mode);
@ -36,7 +58,8 @@ public:
void set_encr_subtype(int encr_subtype);
void set_can(int can);
void set_debug(bool debug);
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, 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();
// Where all the action really happens

View File

@ -22,6 +22,8 @@ static const char *__doc_gr_m17_m17_coder_m17_coder_1 = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_make = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_key = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_meta = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_src_id = R"doc()doc";
@ -30,6 +32,8 @@ static const char *__doc_gr_m17_m17_coder_set_dst_id = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_debug = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_signed = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_type = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_mode = R"doc()doc";

View File

@ -16,7 +16,7 @@
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(m17_coder.h) */
/* BINDTOOL_HEADER_FILE_HASH(14e1973d699725f2a2502c854f1af466) */
/* BINDTOOL_HEADER_FILE_HASH(27a5be021e5302ff55aaec73ed21e81b) */
/***********************************************************************************/
#include <pybind11/complex.h>
@ -39,7 +39,11 @@ void bind_m17_coder(py::module &m) {
.def(py::init(&m17_coder::make), py::arg("src_id"), py::arg("dst_id"),
py::arg("mode"), py::arg("data"), py::arg("encr_type"),
py::arg("encr_subtype"), py::arg("can"), py::arg("meta"),
py::arg("debug"), D(m17_coder, make))
py::arg("key"), py::arg("debug"), py::arg("signed_str"),
D(m17_coder, make))
.def("set_key", &m17_coder::set_key, py::arg("meta"),
D(m17_coder, set_key))
.def("set_meta", &m17_coder::set_meta, py::arg("meta"),
D(m17_coder, set_meta))
@ -53,6 +57,9 @@ void bind_m17_coder(py::module &m) {
.def("set_debug", &m17_coder::set_debug, py::arg("debug"),
D(m17_coder, set_debug))
.def("set_signed", &m17_coder::set_signed, py::arg("signed_str"),
D(m17_coder, set_signed))
.def("set_type", &m17_coder::set_type, py::arg("mode"), py::arg("data"),
py::arg("encr_type"), py::arg("encr_subtype"), py::arg("can"),
D(m17_coder, set_type))