add scrambler key field in decoder block

pull/17/head
Jean-Michel Friedt 2024-12-24 08:59:07 +01:00
parent 203850491b
commit f9d2d7c7b1
6 changed files with 95 additions and 44 deletions

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,7 @@ asserts:
templates:
imports: from gnuradio import m17
make: m17.m17_decoder(${debug_data},${debug_ctrl},${threshold},${callsign},${signed_str},${encr_type},${key})
make: m17.m17_decoder(${debug_data},${debug_ctrl},${threshold},${callsign},${signed_str},${encr_type},${key},${seed})
callbacks:
- set_debug_data(${debug_data})
@ -53,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

@ -43,13 +43,14 @@ namespace gr
*/
static sptr make (bool debug_data, bool debug_ctrl, float threshold,
bool callsign, bool signed_str, int encr_type,
std::string key);
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

@ -240,7 +240,7 @@ namespace gr
void m17_coder_impl::set_seed (std::string arg) // *UTF-8* encoded byte array
{
int length;
printf ("new key: ");
printf ("new seed: ");
length = arg.size ();
int i = 0, j = 0;
while ((j < 3) && (i < length))

View File

@ -49,7 +49,6 @@ namespace gr
time_t epoch = 1577836800L; //Jan 1, 2020, 00:00:00 UTC
#endif
uint8_t _seed[3]; //24-bit is the largest seed value
int _encr_subtype, _can;
lsf_t _lsf, _next_lsf;
std::string _meta;
@ -67,6 +66,7 @@ namespace gr
#ifdef ECC
//Scrambler
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;

View File

@ -44,12 +44,12 @@ namespace gr
m17_decoder::sptr
m17_decoder::make (bool debug_data, bool debug_ctrl, float threshold,
bool callsign, bool signed_str, int encr_type,
std::string key)
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));
}
@ -59,8 +59,9 @@ namespace gr
m17_decoder_impl::m17_decoder_impl (bool debug_data, bool debug_ctrl,
float threshold, bool callsign,
bool signed_str, int encr_type,
std::string key):gr::
block ("m17_decoder", gr::io_signature::make (1, 1, sizeof (float)),
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)
@ -165,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)
@ -203,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);
@ -221,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
@ -242,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: ");
}
@ -250,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
@ -262,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");
}
@ -490,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 ();
@ -504,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,21 +74,22 @@ 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, int encr_type,
std::string key);
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);