Merge pull request #17 from jmfriedt/main

add scramble key and various other updates
main
Wojciech Kaczmarski 2024-12-24 12:39:15 +02:00 committed by GitHub
commit 508c77c96e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 330 additions and 145 deletions

View File

@ -76,7 +76,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [384, 348.0]
coordinate: [376, 412.0]
rotation: 0
state: enabled
- name: enc_type
@ -106,7 +106,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [248, 348.0]
coordinate: [240, 412.0]
rotation: 0
state: enabled
- name: mode
@ -136,7 +136,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [16, 348.0]
coordinate: [8, 452.0]
rotation: 0
state: enabled
- name: samp_rate
@ -171,7 +171,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [544, 352.0]
coordinate: [536, 416.0]
rotation: 0
state: enabled
- name: src_str
@ -217,7 +217,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [120, 348.0]
coordinate: [112, 412.0]
rotation: 0
state: enabled
- name: blocks_file_sink_0
@ -235,7 +235,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [968, 168.0]
coordinate: [1056, 184.0]
rotation: 0
state: enabled
- name: blocks_null_sink_0
@ -273,7 +273,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [568, 176.0]
coordinate: [656, 192.0]
rotation: 0
state: true
- name: blocks_vector_source_x_0_2
@ -294,9 +294,27 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [16, 168.0]
coordinate: [8, 168.0]
rotation: 0
state: true
- name: end_of_transmission
id: variable_qtgui_msg_push_button
parameters:
comment: ''
gui_hint: ''
label: End of Transmission
msgName: end_of_transmission
relBackgroundColor: default
relFontColor: default
type: string
value: end_of_transmission
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 272.0]
rotation: 0
state: enabled
- name: m17_m17_coder_0
id: m17_m17_coder
parameters:
@ -316,6 +334,7 @@ blocks:
minoutbuf: '0'
mode: mode
priv_key: ''
seed: ''
signed_str: signed_str
src_id: src_str
type: type_val
@ -323,7 +342,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [320, 92.0]
coordinate: [408, 96.0]
rotation: 0
state: true
- name: m17_m17_decoder_0
@ -339,13 +358,14 @@ blocks:
key: ''
maxoutbuf: '0'
minoutbuf: '0'
seed: ''
signed_str: 'False'
threshold: '2.0'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [784, 132.0]
coordinate: [872, 140.0]
rotation: 0
state: true
- name: note_0_2
@ -365,6 +385,7 @@ blocks:
connections:
- [blocks_throttle2_0, '0', m17_m17_decoder_0, '0']
- [blocks_vector_source_x_0_2, '0', m17_m17_coder_0, '0']
- [end_of_transmission, pressed, m17_m17_coder_0, end_of_transmission]
- [m17_m17_coder_0, '0', blocks_throttle2_0, '0']
- [m17_m17_decoder_0, '0', blocks_file_sink_0, '0']
- [m17_m17_decoder_0, '0', blocks_null_sink_0, '0']

View File

@ -117,13 +117,14 @@ blocks:
key: ''
maxoutbuf: '0'
minoutbuf: '0'
seed: ''
signed_str: 'False'
threshold: '2.0'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [392, 36.0]
coordinate: [392, 28.0]
rotation: 0
state: enabled

View File

@ -126,7 +126,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [776, 184.0]
coordinate: [880, 200.0]
rotation: 0
state: enabled
- name: blocks_throttle2_0
@ -147,7 +147,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [560, 192.0]
coordinate: [664, 208.0]
rotation: 0
state: true
- name: blocks_vector_source_x_0_2
@ -171,6 +171,24 @@ blocks:
coordinate: [8, 184.0]
rotation: 0
state: true
- name: end_of_transmission
id: variable_qtgui_msg_push_button
parameters:
comment: ''
gui_hint: ''
label: End of Transmission
msgName: end_of_transmission
relBackgroundColor: default
relFontColor: default
type: string
value: end_of_transmission
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 288.0]
rotation: 0
state: enabled
- name: m17_m17_coder_0
id: m17_m17_coder
parameters:
@ -182,12 +200,13 @@ blocks:
dst_id: dst_str
encr_subtype: '0'
encr_type: '0'
key: ''
key: '''\x00\x00\x65\x41\xb0\x93\xff\x00'''
maxoutbuf: '0'
meta: meta_str
minoutbuf: '0'
mode: '1'
priv_key: ''
seed: ''
signed_str: 'False'
src_id: src_str
type: type_val
@ -195,7 +214,7 @@ blocks:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [312, 108.0]
coordinate: [416, 112.0]
rotation: 0
state: true
- name: note_0_2
@ -215,6 +234,7 @@ blocks:
connections:
- [blocks_throttle2_0, '0', blocks_file_sink_0, '0']
- [blocks_vector_source_x_0_2, '0', m17_m17_coder_0, '0']
- [end_of_transmission, pressed, m17_m17_coder_0, end_of_transmission]
- [m17_m17_coder_0, '0', blocks_throttle2_0, '0']
metadata:

View File

@ -43,12 +43,16 @@ parameters:
label: Meta
dtype: string
default: ''
- id: seed
label: Scrambler seed
dtype: string
default: ''
- id: key
label: AES Key
dtype: string
default: ''
- id: priv_key
label: Private Key
label: Private Key for ECDSA (32 bytes)
dtype: string
default: ''
- id: signed_str
@ -70,7 +74,7 @@ asserts:
- ${ 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},${key},${priv_key},${debug},${signed_str})
make: m17.m17_coder(${src_id},${dst_id},${mode},${type},${encr_type},${encr_subtype},${can},${meta},${key},${priv_key},${debug},${signed_str},${seed})
callbacks:
- set_meta(${meta})
- set_src_id(${src_id})
@ -84,6 +88,7 @@ templates:
- set_priv_key(${priv_key})
- set_debug(${debug})
- set_signed(${signed_str})
- set_seed(${seed})
# Make one 'inputs' list entry per input and one 'outputs' list entry per output.
# Keys include:
@ -98,6 +103,9 @@ inputs:
dtype: byte
vlen: 1
optional: 0
- label: end_of_transmission
domain: message
optional: 0
outputs:
- label: out

View File

@ -18,6 +18,10 @@ parameters:
dtype: bool
default: 'False'
options: ['True', 'False']
- id: seed
label: Scrambler seed
dtype: string
default: ''
- id: key
label: AES Key
dtype: string
@ -43,7 +47,8 @@ asserts:
templates:
imports: from gnuradio import m17
make: m17.m17_decoder(${debug_data},${debug_ctrl},${threshold},${callsign},${signed_str},${key},${encr_type})
make: m17.m17_decoder(${debug_data},${debug_ctrl},${threshold},${callsign},${signed_str},${encr_type},${key},${seed})
callbacks:
- set_debug_data(${debug_data})
- set_debug_ctrl(${debug_ctrl})
@ -52,6 +57,7 @@ templates:
- set_callsign(${callsign})
- set_signed(${signed_str})
- set_encr_type(${encr_type})
- set_seed(${seed})
# Make one 'inputs' list entry per input and one 'outputs' list entry per output.
# Keys include:

View File

@ -42,11 +42,12 @@ namespace gr
* creating new instances.
*/
static sptr make (std::string src_id, std::string dst_id, int mode,
int data, encr_t encr_type, int encr_subtype, int can,
int data, int encr_type, int encr_subtype, int can,
std::string meta, std::string key,
std::string priv_key, bool debug, bool signed_str);
std::string priv_key, bool debug, bool signed_str, std::string seed);
virtual void set_key (std::string meta) = 0;
virtual void set_priv_key (std::string meta) = 0;
virtual void set_seed (std::string dst_id) = 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;
@ -56,7 +57,7 @@ namespace gr
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 (encr_t encr_type) = 0;
virtual void set_encr_type (int encr_type) = 0;
virtual void set_encr_subtype (int encr_subtype) = 0;
virtual void set_can (int can) = 0;
};

View File

@ -42,14 +42,15 @@ namespace gr
* creating new instances.
*/
static sptr make (bool debug_data, bool debug_ctrl, float threshold,
bool callsign, bool signed_str, encr_t encr_type,
std::string key);
bool callsign, bool signed_str, int encr_type,
std::string key, std::string seed);
virtual void set_debug_data (bool debug) = 0;
virtual void set_debug_ctrl (bool debug) = 0;
virtual void set_callsign (bool callsign) = 0;
virtual void set_threshold (float threshold) = 0;
virtual void set_signed (bool signed_str) = 0;
virtual void set_key (std::string key) = 0;
virtual void set_seed (std::string seed) = 0;
virtual void parse_raw_key_string (uint8_t * dest, const char *inp) = 0;
virtual void scrambler_sequence_generator () = 0;
virtual uint32_t scrambler_seed_calculation (int8_t subtype,

View File

@ -29,6 +29,8 @@ list(APPEND m17_sources
../M17_Implementations/libm17/phy/randomize.c
../M17_Implementations/libm17/phy/sync.c
../M17_Implementations/libm17/phy/slice.c
../M17_Implementations/micro-ecc/uECC.c
../M17_Implementations/tinier-aes/aes.c
)
set(m17_sources "${m17_sources}" PARENT_SCOPE)

View File

@ -51,41 +51,34 @@ namespace gr
m17_coder::sptr
m17_coder::make (std::string src_id, std::string dst_id, int mode,
int data, encr_t encr_type, int encr_subtype, int can,
int data, int encr_type, int encr_subtype, int can,
std::string meta, std::string key,
std::string priv_key, bool debug, bool signed_str)
std::string priv_key, bool debug, bool signed_str, std::string seed)
{
return gnuradio::get_initial_sptr
(new
m17_coder_impl (src_id, dst_id, mode, data, encr_type, encr_subtype,
can, meta, key, priv_key, debug, signed_str));
can, meta, key, priv_key, debug, signed_str, seed));
}
/*
* The private constructor
*/
m17_coder_impl::m17_coder_impl (std::string src_id, std::string dst_id,
int mode, int data, encr_t encr_type,
int mode, int data, int encr_type,
int encr_subtype, int can,
std::string meta, std::string key,
std::string priv_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),
bool signed_str, std::string seed):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_subtype (encr_subtype), _can (can), _meta (meta), _debug (debug),
_signed_str (signed_str)
{
set_type (mode, data, encr_type, encr_subtype, can);
set_encr_type(encr_type); // overwritten by set_seed()
set_type (mode, data, _encr_type, encr_subtype, can);
set_meta (meta); // depends on ^^^ encr_subtype
set_seed (seed); // depends on ^^^ encr_subtype
set_src_id (src_id);
set_dst_id (dst_id);
set_signed (signed_str);
@ -109,6 +102,26 @@ namespace gr
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
}
message_port_register_in(pmt::mp("end_of_transmission"));
set_msg_handler(pmt::mp("end_of_transmission"), [this](const pmt::pmt_t& msg) { end_of_transmission(msg); });
}
void m17_coder_impl::end_of_transmission(const pmt::pmt_t& msg)
{_finished=true;
std::cout << "***** End of Transmission ********\n";
pmt::print(msg);
}
void m17_coder_impl::set_encr_type (int encr_type)
{
switch (encr_type)
{case 0:_encr_type=ENCR_NONE;break;
case 1:_encr_type=ENCR_SCRAM;break;
case 2:_encr_type=ENCR_AES;break;
case 3:_encr_type=ENCR_RES;break;
default:_encr_type=ENCR_NONE;
}
printf ("new encr type: %x -> ", _encr_type);
}
void m17_coder_impl::set_signed (bool signed_str)
@ -232,6 +245,49 @@ namespace gr
fflush (stdout);
}
void m17_coder_impl::set_seed (std::string arg) // *UTF-8* encoded byte array
{
int length;
printf ("new seed: ");
length = arg.size ();
int i = 0, j = 0;
while ((j < 3) && (i < length))
{
if ((unsigned int) arg.data ()[i] < 0xc2) // https://www.utf8-chartable.de/
{
_seed[j] = arg.data ()[i];
i++;
j++;
}
else
{
_seed[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 ", _seed[i]);
printf ("\n");
fflush (stdout);
if(length<=2)
{
_scrambler_seed = _scrambler_seed >> 16;
fprintf(stderr, "Scrambler key: 0x%02X (8-bit)\n", _scrambler_seed);
}
else if(length<=4)
{
_scrambler_seed = _scrambler_seed >> 8;
fprintf(stderr, "Scrambler key: 0x%04X (16-bit)\n", _scrambler_seed);
}
else
fprintf(stderr, "Scrambler key: 0x%06X (24-bit)\n", _scrambler_seed);
_encr_type=ENCR_SCRAM; //Scrambler key was passed
}
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;
@ -297,13 +353,6 @@ namespace gr
set_type (_mode, _data, _encr_type, _encr_subtype, _can);
}
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);
}
void m17_coder_impl::set_encr_subtype (int encr_subtype)
{
_encr_subtype = encr_subtype;
@ -353,20 +402,20 @@ namespace gr
{
int i = 0;
uint32_t lfsr, bit;
lfsr = scrambler_seed;
lfsr = _scrambler_seed;
//only set if not initially set (first run), it is possible (and observed) that the scrambler_subtype can
//change on subsequent passes if the current SEED for the LFSR falls below one of these thresholds
if (scrambler_subtype == -1)
if (_scrambler_subtype == -1)
{
if (lfsr > 0 && lfsr <= 0xFF)
scrambler_subtype = 0; // 8-bit key
_scrambler_subtype = 0; // 8-bit key
else if (lfsr > 0xFF && lfsr <= 0xFFFF)
scrambler_subtype = 1; //16-bit key
_scrambler_subtype = 1; //16-bit key
else if (lfsr > 0xFFFF && lfsr <= 0xFFFFFF)
scrambler_subtype = 2; //24-bit key
_scrambler_subtype = 2; //24-bit key
else
scrambler_subtype = 0; // 8-bit key (default)
_scrambler_subtype = 0; // 8-bit key (default)
}
//TODO: Set Frame Type based on scrambler_subtype value
@ -374,7 +423,7 @@ namespace gr
{
fprintf (stderr,
"\nScrambler Key: 0x%06X; Seed: 0x%06X; Subtype: %02d;",
scrambler_seed, lfsr, scrambler_subtype);
_scrambler_seed, lfsr, _scrambler_subtype);
fprintf (stderr, "\n pN: ");
}
@ -382,11 +431,11 @@ namespace gr
for (i = 0; i < 128; i++)
{
//get feedback bit with specified taps, depending on the scrambler_subtype
if (scrambler_subtype == 0)
if (_scrambler_subtype == 0)
bit = (lfsr >> 7) ^ (lfsr >> 5) ^ (lfsr >> 4) ^ (lfsr >> 3);
else if (scrambler_subtype == 1)
else if (_scrambler_subtype == 1)
bit = (lfsr >> 15) ^ (lfsr >> 14) ^ (lfsr >> 12) ^ (lfsr >> 3);
else if (scrambler_subtype == 2)
else if (_scrambler_subtype == 2)
bit = (lfsr >> 23) ^ (lfsr >> 22) ^ (lfsr >> 21) ^ (lfsr >> 16);
else
bit = 0; //should never get here, but just in case
@ -394,28 +443,28 @@ namespace gr
bit &= 1; //truncate bit to 1 bit (required since I didn't do it above)
lfsr = (lfsr << 1) | bit; //shift LFSR left once and OR bit onto LFSR's LSB
lfsr &= 0xFFFFFF; //truncate lfsr to 24-bit (really doesn't matter)
scrambler_pn[i] = bit;
_scrambler_pn[i] = bit;
}
//pack bit array into byte array for easy data XOR
pack_bit_array_into_byte_array (scrambler_pn, scr_bytes, 16);
pack_bit_array_into_byte_array (_scrambler_pn, _scr_bytes, 16);
//save scrambler seed for next round
scrambler_seed = lfsr;
_scrambler_seed = lfsr;
//truncate seed so subtype will continue to set properly on subsequent passes
if (scrambler_subtype == 0)
scrambler_seed &= 0xFF;
else if (scrambler_subtype == 1)
scrambler_seed &= 0xFFFF;
else if (scrambler_subtype == 2)
scrambler_seed &= 0xFFFFFF;
if (_scrambler_subtype == 0)
_scrambler_seed &= 0xFF;
else if (_scrambler_subtype == 1)
_scrambler_seed &= 0xFFFF;
else if (_scrambler_subtype == 2)
_scrambler_seed &= 0xFFFFFF;
if (_debug == true)
{
//debug packed bytes
for (i = 0; i < 16; i++)
fprintf (stderr, " %02X", scr_bytes[i]);
fprintf (stderr, " %02X", _scr_bytes[i]);
fprintf (stderr, "\n");
}
@ -497,15 +546,8 @@ namespace gr
uint8_t data[16], next_data[16]; //raw payload, packed bits
while (countout < (uint32_t) noutput_items)
while ((countout < (uint32_t) noutput_items) && (countin + 16 <= noutput_items))
{
if (countin + 16 <= noutput_items)
{
for (int i = 0; i < 16; i++)
{
data[i] = in[countin];
countin++;
}
if (!_got_lsf) //stream frames
{
//send LSF syncword
@ -552,11 +594,11 @@ namespace gr
{
_next_lsf.type[0] = 0x00;
_next_lsf.type[1] = 0x00;
if (scrambler_subtype == 0)
if (_scrambler_subtype == 0)
_next_lsf.type[1] = 0x0D;
else if (scrambler_subtype == 1)
else if (_scrambler_subtype == 1)
_next_lsf.type[1] = 0x2D;
else if (scrambler_subtype == 2)
else if (_scrambler_subtype == 2)
_next_lsf.type[1] = 0x4D;
}
else //no enc or subtype field, normal 3200 voice
@ -573,8 +615,8 @@ namespace gr
memset (next_data, 0, sizeof (next_data));
memcpy (data, next_data, sizeof (data));
if (_fn == 60)
_finished = true;
// if (_fn == 60)
// _finished = true;
//debug sig with random payloads (don't play the audio)
for (uint8_t i = 0; i < 16; i++)
@ -590,6 +632,14 @@ namespace gr
dummy=fread(&(lsf.meta), 14, 1, stdin);
dummy=fread(data, 16, 1, stdin);
*/
if (countin + 16 <= noutput_items)
{
for (int i = 0; i < 16; i++)
{
data[i] = in[countin];
countin++;
}
}
//AES encryption enabled - use 112 bits of IV
if (_encr_type == ENCR_AES)
@ -604,7 +654,7 @@ namespace gr
_lsf.crc[1] = ccrc & 0xFF;
}
while (_finished == false)
// while (_finished == false)
{
if (!_got_lsf)
{ //debug
@ -654,11 +704,11 @@ namespace gr
{
_next_lsf.type[0] = 0x00;
_next_lsf.type[1] = 0x00;
if (scrambler_subtype == 0)
if (_scrambler_subtype == 0)
_next_lsf.type[1] = 0x0D;
else if (scrambler_subtype == 1)
else if (_scrambler_subtype == 1)
_next_lsf.type[1] = 0x2D;
else if (scrambler_subtype == 2)
else if (_scrambler_subtype == 2)
_next_lsf.type[1] = 0x4D;
}
else //no enc or subtype field, normal 3200 voice
@ -693,6 +743,14 @@ namespace gr
if(fread(&(next_lsf.meta), 14, 1, stdin)<1) finished=1;
if(fread(next_data, 16, 1, stdin)<1) _finished=true;
*/
if (countin + 16 <= noutput_items)
{
for (int i = 0; i < 16; i++)
{
next_data[i] = in[countin];
countin++;
}
}
}
//AES
@ -710,7 +768,7 @@ namespace gr
scrambler_sequence_generator ();
for (uint8_t i = 0; i < 16; i++)
{
data[i] ^= scr_bytes[i];
data[i] ^= _scr_bytes[i];
}
}
@ -752,7 +810,7 @@ namespace gr
memcpy (data, next_data, 16);
}
else //send last frame(s)
{
{ printf("Sending last frame\n");
send_syncword (out, &countout, SYNC_STR);
extract_LICH (lich, _lich_cnt, &_lsf);
encode_LICH (lich_encoded, lich);
@ -817,14 +875,16 @@ namespace gr
//fprintf(stderr, "Stream has ended. Exiting.\n");
} // finished == true
} // finished == false
} // in enough data left
} // loop on input data
// Tell runtime system how many input items we consumed on
// each input stream.
consume_each (countin);
// printf(" noutput_items=%d countin=%d countout=%d\n",noutput_items,countin,countout);
// Tell runtime system how many output items we produced.
if (_finished==false)
return countout;
else {printf("Killing flowgraph\n"); return -1;} // https://lists.gnu.org/archive/html/discuss-gnuradio/2016-12/msg00206.html
// returning -1 (which is the magical value for "there's nothing coming anymore, you can shut down") would normally end a flow graph
}
} /* namespace m17 */

View File

@ -66,10 +66,11 @@ namespace gr
#ifdef ECC
//Scrambler
uint8_t scr_bytes[16];
uint8_t scrambler_pn[128];
uint32_t scrambler_seed = 0;
int8_t scrambler_subtype = -1;
uint8_t _seed[3]; //24-bit is the largest seed value
uint8_t _scr_bytes[16];
uint8_t _scrambler_pn[128];
uint32_t _scrambler_seed = 0;
int8_t _scrambler_subtype = -1;
#endif
public:
@ -80,19 +81,22 @@ namespace gr
void set_key (std::string key);
void set_priv_key (std::string key);
void set_meta (std::string meta);
void set_seed (std::string meta);
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 (encr_t encr_type);
void set_encr_type (int 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);
void end_of_transmission(const pmt::pmt_t& msg);
m17_coder_impl (std::string src_id, std::string dst_id, int mode,
int data, encr_t encr_type, int encr_subtype, int can,
int data, int encr_type, int encr_subtype, int can,
std::string meta, std::string key, std::string priv_key,
bool debug, bool signed_str);
bool debug, bool signed_str, std::string seed);
~m17_coder_impl ();
// Where all the action really happens

View File

@ -43,13 +43,13 @@ namespace gr
m17_decoder::sptr
m17_decoder::make (bool debug_data, bool debug_ctrl, float threshold,
bool callsign, bool signed_str, encr_t encr_type,
std::string key)
bool callsign, bool signed_str, int encr_type,
std::string key, std::string seed)
{
return gnuradio::get_initial_sptr
(new
m17_decoder_impl (debug_data, debug_ctrl, threshold, callsign,
signed_str, encr_type, key));
signed_str, encr_type, key, seed));
}
@ -58,9 +58,10 @@ namespace gr
*/
m17_decoder_impl::m17_decoder_impl (bool debug_data, bool debug_ctrl,
float threshold, bool callsign,
bool signed_str, encr_t encr_type,
std::string key):gr::
block ("m17_decoder", gr::io_signature::make (1, 1, sizeof (float)),
bool signed_str, int encr_type,
std::string key, std::string seed):
gr::block ("m17_decoder",
gr::io_signature::make (1, 1, sizeof (float)),
gr::io_signature::make (1, 1, sizeof (char))),
_debug_data (debug_data), _debug_ctrl (debug_ctrl),
_threshold (threshold), _callsign (callsign), _signed_str (signed_str)
@ -106,9 +107,15 @@ namespace gr
printf ("Debug control: false\n");
}
void m17_decoder_impl::set_encr_type (encr_t encr_type)
void m17_decoder_impl::set_encr_type (int encr_type)
{
_encr_type = encr_type;
switch (encr_type)
{case 0:_encr_type=ENCR_NONE;break;
case 1:_encr_type=ENCR_SCRAM;break;
case 2:_encr_type=ENCR_AES;break;
case 3:_encr_type=ENCR_RES;break;
default:_encr_type=ENCR_NONE;
}
printf ("new encr type: %x -> ", _encr_type);
}
@ -159,6 +166,49 @@ namespace gr
fflush (stdout);
}
void m17_decoder_impl::set_seed (std::string arg) // *UTF-8* encoded byte array
{
int length;
printf ("new seed: ");
length = arg.size ();
int i = 0, j = 0;
while ((j < 3) && (i < length))
{
if ((unsigned int) arg.data ()[i] < 0xc2) // https://www.utf8-chartable.de/
{
_seed[j] = arg.data ()[i];
i++;
j++;
}
else
{
_seed[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 ", _seed[i]);
printf ("\n");
fflush (stdout);
if(length<=2)
{
_scrambler_seed = _scrambler_seed >> 16;
fprintf(stderr, "Scrambler key: 0x%02X (8-bit)\n", _scrambler_seed);
}
else if(length<=4)
{
_scrambler_seed = _scrambler_seed >> 8;
fprintf(stderr, "Scrambler key: 0x%04X (16-bit)\n", _scrambler_seed);
}
else
fprintf(stderr, "Scrambler key: 0x%06X (24-bit)\n", _scrambler_seed);
_encr_type=ENCR_SCRAM; //Scrambler key was passed
}
void
m17_decoder_impl::forecast (int noutput_items,
gr_vector_int & ninput_items_required)
@ -197,12 +247,12 @@ namespace gr
}
//truncate seed so subtype will continue to set properly on subsequent passes
if (scrambler_subtype == 0)
scrambler_seed &= 0xFF;
else if (scrambler_subtype == 1)
scrambler_seed &= 0xFFFF;
else if (scrambler_subtype == 2)
scrambler_seed &= 0xFFFFFF;
if (_scrambler_subtype == 0)
_scrambler_seed &= 0xFF;
else if (_scrambler_subtype == 1)
_scrambler_seed &= 0xFFFF;
else if (_scrambler_subtype == 2)
_scrambler_seed &= 0xFFFFFF;
//debug
//fprintf (stderr, "\nScrambler Key: 0x%06X; Seed: 0x%06X; Subtype: %02d; FN: %05d; ", key, lfsr, subtype, fn);
@ -215,20 +265,20 @@ namespace gr
{
int i = 0;
uint32_t lfsr, bit;
lfsr = scrambler_seed;
lfsr = _scrambler_seed;
//only set if not initially set (first run), it is possible (and observed) that the scrambler_subtype can
//change on subsequent passes if the current SEED for the LFSR falls below one of these thresholds
if (scrambler_subtype == -1)
if (_scrambler_subtype == -1)
{
if (lfsr > 0 && lfsr <= 0xFF)
scrambler_subtype = 0; // 8-bit key
_scrambler_subtype = 0; // 8-bit key
else if (lfsr > 0xFF && lfsr <= 0xFFFF)
scrambler_subtype = 1; //16-bit key
_scrambler_subtype = 1; //16-bit key
else if (lfsr > 0xFFFF && lfsr <= 0xFFFFFF)
scrambler_subtype = 2; //24-bit key
_scrambler_subtype = 2; //24-bit key
else
scrambler_subtype = 0; // 8-bit key (default)
_scrambler_subtype = 0; // 8-bit key (default)
}
//TODO: Set Frame Type based on scrambler_subtype value
@ -236,7 +286,7 @@ namespace gr
{
fprintf (stderr,
"\nScrambler Key: 0x%06X; Seed: 0x%06X; Subtype: %02d;",
scrambler_seed, lfsr, scrambler_subtype);
_scrambler_seed, lfsr, _scrambler_subtype);
fprintf (stderr, "\n pN: ");
}
@ -244,11 +294,11 @@ namespace gr
for (i = 0; i < 128; i++)
{
//get feedback bit with specified taps, depending on the scrambler_subtype
if (scrambler_subtype == 0)
if (_scrambler_subtype == 0)
bit = (lfsr >> 7) ^ (lfsr >> 5) ^ (lfsr >> 4) ^ (lfsr >> 3);
else if (scrambler_subtype == 1)
else if (_scrambler_subtype == 1)
bit = (lfsr >> 15) ^ (lfsr >> 14) ^ (lfsr >> 12) ^ (lfsr >> 3);
else if (scrambler_subtype == 2)
else if (_scrambler_subtype == 2)
bit = (lfsr >> 23) ^ (lfsr >> 22) ^ (lfsr >> 21) ^ (lfsr >> 16);
else
bit = 0; //should never get here, but just in case
@ -256,29 +306,29 @@ namespace gr
bit &= 1; //truncate bit to 1 bit (required since I didn't do it above)
lfsr = (lfsr << 1) | bit; //shift LFSR left once and OR bit onto LFSR's LSB
lfsr &= 0xFFFFFF; //truncate lfsr to 24-bit (really doesn't matter)
scrambler_pn[i] = bit;
_scrambler_pn[i] = bit;
}
//pack bit array into byte array for easy data XOR
pack_bit_array_into_byte_array (scrambler_pn, scr_bytes, 16);
pack_bit_array_into_byte_array (_scrambler_pn, _scr_bytes, 16);
//save scrambler seed for next round
scrambler_seed = lfsr;
_scrambler_seed = lfsr;
//truncate seed so subtype will continue to set properly on subsequent passes
if (scrambler_subtype == 0)
scrambler_seed &= 0xFF;
else if (scrambler_subtype == 1)
scrambler_seed &= 0xFFFF;
else if (scrambler_subtype == 2)
scrambler_seed &= 0xFFFFFF;
if (_scrambler_subtype == 0)
_scrambler_seed &= 0xFF;
else if (_scrambler_subtype == 1)
_scrambler_seed &= 0xFFFF;
else if (_scrambler_subtype == 2)
_scrambler_seed &= 0xFFFFFF;
if (_debug_ctrl == true)
{
//debug packed bytes
for (i = 0; i < 16; i++)
fprintf (stderr, " %02X", scr_bytes[i]);
fprintf (stderr, " %02X", _scr_bytes[i]);
fprintf (stderr, "\n");
}
@ -484,12 +534,12 @@ namespace gr
if (_encr_type == ENCR_SCRAM)
{
if (fn != 0 && (fn % 0x8000) != _expected_next_fn) //frame skip, etc
scrambler_seed =
scrambler_seed_calculation (scrambler_subtype,
_scrambler_seed =
scrambler_seed_calculation (_scrambler_subtype,
_scrambler_key,
fn & 0x7FFF);
else if (fn == 0)
scrambler_seed = _scrambler_key; //reset back to key value
_scrambler_seed = _scrambler_key; //reset back to key value
if (_signed_str == true && (fn % 0x8000) < 0x7FFC) //signed stream
scrambler_sequence_generator ();
@ -498,7 +548,7 @@ namespace gr
for (uint8_t i = 0; i < 16; i++)
{
frame_data[i + 3] ^= scr_bytes[i];
frame_data[i + 3] ^= _scr_bytes[i];
}
}

View File

@ -74,26 +74,27 @@ namespace gr
uint8_t d_dst[12], d_src[12]; //decoded strings
#ifdef ECC
//Scrambler
uint8_t scr_bytes[16];
uint8_t scrambler_pn[128];
uint32_t scrambler_seed = 0;
int8_t scrambler_subtype = -1;
uint8_t _seed[3]; //24-bit is the largest seed value
uint8_t _scr_bytes[16];
uint8_t _scrambler_pn[128];
uint32_t _scrambler_seed = 0;
int8_t _scrambler_subtype = -1;
#endif
const struct uECC_Curve_t *_curve = uECC_secp256r1 ();
public:
m17_decoder_impl (bool debug_data, bool debug_ctrl, float threshold,
bool callsign, bool signed_str, encr_t encr_type,
std::string key);
bool callsign, bool signed_str, int encr_type,
std::string key, std::string seed);
~m17_decoder_impl ();
void set_debug_data (bool debug);
void set_key (std::string arg);
void set_seed (std::string seed);
void set_debug_ctrl (bool debug);
void set_callsign (bool callsign);
void set_threshold (float threshold);
void set_signed (bool signed_str);
void set_encr_type (encr_t encr_type);
void set_encr_type (int encr_type);
void parse_raw_key_string (uint8_t * dest, const char *inp);
void scrambler_sequence_generator ();
uint32_t scrambler_seed_calculation (int8_t subtype, uint32_t key,

View File

@ -26,6 +26,8 @@ static const char *__doc_gr_m17_m17_coder_set_key = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_priv_key = R"doc()doc";
static const char *__doc_gr_m17_m17_coder_set_seed = 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";

View File

@ -34,6 +34,8 @@ static const char *__doc_gr_m17_m17_decoder_set_signed = R"doc()doc";
static const char *__doc_gr_m17_m17_decoder_set_key = R"doc()doc";
static const char *__doc_gr_m17_m17_decoder_set_seed = R"doc()doc";
static const char *__doc_gr_m17_m17_decoder_parse_raw_key_string = R"doc()doc";
static const char *__doc_gr_m17_m17_decoder_scrambler_sequence_generator =

View File

@ -16,7 +16,7 @@
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(m17_coder.h) */
/* BINDTOOL_HEADER_FILE_HASH(94fa30734504cc76bf84692d850f98e1) */
/* BINDTOOL_HEADER_FILE_HASH(a0706fe6b4d1abbfbb9e0e834826dda9) */
/***********************************************************************************/
#include <pybind11/complex.h>
@ -40,7 +40,7 @@ void bind_m17_coder(py::module &m) {
py::arg("mode"), py::arg("data"), py::arg("encr_type"),
py::arg("encr_subtype"), py::arg("can"), py::arg("meta"),
py::arg("key"), py::arg("priv_key"), py::arg("debug"),
py::arg("signed_str"), D(m17_coder, make))
py::arg("signed_str"), py::arg("seed"), D(m17_coder, make))
.def("set_key", &m17_coder::set_key, py::arg("meta"),
D(m17_coder, set_key))
@ -48,6 +48,9 @@ void bind_m17_coder(py::module &m) {
.def("set_priv_key", &m17_coder::set_priv_key, py::arg("meta"),
D(m17_coder, set_priv_key))
.def("set_seed", &m17_coder::set_seed, py::arg("dst_id"),
D(m17_coder, set_seed))
.def("set_meta", &m17_coder::set_meta, py::arg("meta"),
D(m17_coder, set_meta))

View File

@ -16,7 +16,7 @@
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(m17_decoder.h) */
/* BINDTOOL_HEADER_FILE_HASH(e74f1151dff2abb4329d6d3b2e746f85) */
/* BINDTOOL_HEADER_FILE_HASH(81519a545ddc4652d42874fbdfee0e05) */
/***********************************************************************************/
#include <pybind11/complex.h>
@ -39,7 +39,7 @@ void bind_m17_decoder(py::module &m) {
.def(py::init(&m17_decoder::make), py::arg("debug_data"),
py::arg("debug_ctrl"), py::arg("threshold"), py::arg("callsign"),
py::arg("signed_str"), py::arg("encr_type"), py::arg("key"),
D(m17_decoder, make))
py::arg("seed"), D(m17_decoder, make))
.def("set_debug_data", &m17_decoder::set_debug_data, py::arg("debug"),
D(m17_decoder, set_debug_data))
@ -59,6 +59,9 @@ void bind_m17_decoder(py::module &m) {
.def("set_key", &m17_decoder::set_key, py::arg("key"),
D(m17_decoder, set_key))
.def("set_seed", &m17_decoder::set_seed, py::arg("seed"),
D(m17_decoder, set_seed))
.def("parse_raw_key_string", &m17_decoder::parse_raw_key_string,
py::arg("dest"), py::arg("inp"),
D(m17_decoder, parse_raw_key_string))