GNU Radio's M17 Package
m17_coder_impl.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2023 jmfriedt.
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8#ifndef INCLUDED_M17_M17_CODER_IMPL_H
9#define INCLUDED_M17_M17_CODER_IMPL_H
10
11#include <atomic>
13
14#include "m17.h" // lsf_t declaration
15#include "aes.h"
16#include "uECC.h"
17
18namespace gr
19{
20 namespace m17
21 {
22
24 {
25 private:
26 // M17
27 static constexpr int PAYLOAD_BYTES = 16;
28
29 char _src_id[10], _dst_id[10]; // 9 character callsign
30 int _mode, _data;
31 uint16_t _send_preamble;
32 int _encr_subtype;
33
34 // encryption
35 int8_t _aes_subtype = -1;
36 const struct uECC_Curve_t *_curve = uECC_secp256r1();
37 encr_t _encr_type = ENCR_NONE;
38
39 // AES
40 typedef enum
41 {
42 AES128,
43 AES192,
44 AES256
45 } aes_t;
46 uint8_t _key[32] = {0};
47 uint8_t _iv[PAYLOAD_BYTES];
48 const time_t epoch = 1577836800L; // Jan 1, 2020, 00:00:00 UTC
49
50 int _can;
51 lsf_t _lsf;
52 uint8_t _meta_block[4][14]; // Text Data blocks (Control Byte + 13 bytes)
53 int _meta_blocks = 0; // number of Text Data blocks (0: no text)
54 int _meta_idx = 0; // block currently in META
55 std::string _meta_text; // the complete text
56 std::string _meta;
57 int _got_lsf = 0;
58 uint16_t _fn = 0; // 16-bit Frame Number (for the stream mode)
59 uint8_t _lich_cnt = 0; // 0..5 LICH counter, derived from the Frame Number
60
61 // packet mode (spec 2.0.x): Packet Data = protocol byte + data (+ null for text) + 2-byte CRC,
62 // split into up to 33 frames of 25 bytes -> at most 821 bytes of SMS text
63 static const int PKT_MAX_FRAMES = 33;
64 static const int PKT_CHUNK = 25;
65 static const int SMS_MAX_LEN = PKT_MAX_FRAMES * PKT_CHUNK - 4;
66 char _text_msg[SMS_MAX_LEN + 1];
67 uint8_t _pkt_data[PKT_MAX_FRAMES * PKT_CHUNK]; // assembled Packet Data incl. CRC
68 int _pkt_len = 0; // bytes in _pkt_data
69 int _pkt_frames = 0; // number of Packet Frames
70 int _pkt_stage = -1; // -1: idle, 0: preamble, 1: LSF, 2..: frames, then EoT
71 lsf_t _pkt_lsf; // LSF used for the packet transmission
72 std::atomic<size_t> _text_len;
73 std::atomic<bool> _pkt_pend = false; // pending packet transmission?
74
75 bool _debug = 0;
76 bool _signed_str = false;
77 bool _finalizing = false;
78 bool _stale_flushed = false;
79 std::atomic<bool> _finished = false, _active = false;
80
81 uint8_t _digest[PAYLOAD_BYTES] = {0}; // 16-byte field for the stream digest
82 bool _priv_key_loaded = false; // do we have a sig key loaded?
83 uint8_t _priv_key[32] = {0}; // private key
84 uint8_t _sig[64] = {0}; // ECDSA signature
85 int _eot_cnt = 1;
86 bool _init_frame;
87
88 // Scrambler
89 uint8_t _seed[3]; // 24-bit is the largest seed value
90 uint8_t _scr_bytes[PAYLOAD_BYTES];
91 uint8_t _scrambler_pn[128];
92 uint32_t _scrambler_seed = 0;
93 uint32_t _scrambler_key = 0; // initial LFSR value (the seed)
94 int8_t _scrambler_subtype = -1;
95
96 public:
97 void parse_raw_key_string(uint8_t *, const char *);
99 void set_src_id(std::string src_id);
100 void set_dst_id(std::string dst_id);
101 void set_key(std::string key);
102 void set_priv_key(std::string key);
103 void set_meta(std::string meta);
104 void set_seed(std::string seed);
105 void set_eot_cnt(int eot_cnt);
106 void set_type(int mode, int data, encr_t encr_type, int encr_subtype,
107 int can);
108 void set_mode(int mode);
109 void set_data(int data);
110 void set_encr_type(int encr_type);
111 void set_encr_subtype(int encr_subtype);
112 void set_aes_subtype(int aes_subtype, int encr_type);
113 void set_can(int can);
114 void set_debug(bool debug);
115 bool start() override;
116 void set_signed(bool signed_str);
117 void switch_state(const pmt::pmt_t &msg);
118 void init_state(void);
119 std::string tag() const;
120 std::string meta_desc(uint16_t type);
121 bool _started = false; // flowgraph started: setters report runtime changes
122 bool _continuous = false; // continuous mode: one stream from flowgraph start to end of input
124 bool stop() override;
125 int _tx_frames = 0; // data frames of the current transmission
126 void new_nonce(void);
127 void encrypt_payload(uint8_t *data);
128 void update_digest(const uint8_t *data);
129
130 m17_coder_impl(std::string src_id, std::string dst_id,
131 int data, int encr_type, int encr_subtype, int aes_subtype, int can,
132 std::string meta, std::string key, std::string priv_key,
133 bool debug, bool signed_str, std::string seed, int eot_cnt, bool continuous);
135
136 // Where all the action really happens
137 void forecast(int noutput_items,
138 gr_vector_int &ninput_items_required);
139
140 int general_work(int noutput_items,
141 gr_vector_int &ninput_items,
142 gr_vector_const_void_star &input_items,
143 gr_vector_void_star &output_items);
144 };
145
146 } // namespace m17
147} // namespace gr
148
149#endif /* INCLUDED_M17_M17_CODER_IMPL_H */
void forecast(int noutput_items, gr_vector_int &ninput_items_required)
void switch_state(const pmt::pmt_t &msg)
std::string meta_desc(uint16_t type)
void encrypt_payload(uint8_t *data)
void set_seed(std::string seed)
void set_dst_id(std::string dst_id)
void set_priv_key(std::string key)
bool _started
Definition m17_coder_impl.h:121
m17_coder_impl(std::string src_id, std::string dst_id, int data, int encr_type, int encr_subtype, int aes_subtype, int can, std::string meta, std::string key, std::string priv_key, bool debug, bool signed_str, std::string seed, int eot_cnt, bool continuous)
bool start() override
int _tx_frames
Definition m17_coder_impl.h:125
bool stop() override
void set_meta(std::string meta)
void set_aes_subtype(int aes_subtype, int encr_type)
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
void set_key(std::string key)
void set_type(int mode, int data, encr_t encr_type, int encr_subtype, int can)
void set_encr_subtype(int encr_subtype)
void set_eot_cnt(int eot_cnt)
void set_mode(int mode)
bool _continuous
Definition m17_coder_impl.h:122
std::string tag() const
void set_signed(bool signed_str)
void update_digest(const uint8_t *data)
void set_src_id(std::string src_id)
void set_encr_type(int encr_type)
void set_data(int data)
<+description of block+>
Definition m17_coder.h:25
encr_t
Definition m17_coder.h:29
@ ENCR_NONE
Definition m17_coder.h:30
Definition codec2_decoder.h:17
Definition codec2_decoder.h:15