diff --git a/.gitignore b/.gitignore index d1ad3d8..82dd2c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /SP5WWP/grc/*.py /SP5WWP/m17-coder/m17-coder-sym +/SP5WWP/m17-coder/m17-coder-sym-debug /SP5WWP/m17-decoder/m17-decoder-sym /SP5WWP/m17-packet/m17-packet-encode /SP5WWP/m17-packet/m17-packet-decode diff --git a/SP5WWP/m17-coder/Makefile b/SP5WWP/m17-coder/Makefile index e6e6d5f..779527f 100644 --- a/SP5WWP/m17-coder/Makefile +++ b/SP5WWP/m17-coder/Makefile @@ -1,5 +1,10 @@ +all: m17-coder-sym m17-coder-sym-debug + m17-coder-sym: m17-coder-sym.c gcc -I ../../libm17 -L ../../libm17 -O2 -Wall -Wextra m17-coder-sym.c -o m17-coder-sym -lm -lm17 +m17-coder-sym-debug: m17-coder-sym-debug.c + gcc -I ../../libm17 -L ../../libm17 -O2 -Wall -Wextra m17-coder-sym-debug.c -o m17-coder-sym-debug -lm -lm17 + clean: - rm -f m17-coder-sym + rm -f m17-coder-sym m17-coder-sym-debug diff --git a/SP5WWP/m17-coder/m17-coder-sym-debug.c b/SP5WWP/m17-coder/m17-coder-sym-debug.c new file mode 100644 index 0000000..c2eafb3 --- /dev/null +++ b/SP5WWP/m17-coder/m17-coder-sym-debug.c @@ -0,0 +1,276 @@ +#include +#include +#include +#include +#include + +//libm17 +#include +//tinier-aes +#include "../../tinier-aes/aes.c" + +//#define FN60_DEBUG + +struct LSF lsf, next_lsf; + +uint8_t lich[6]; //48 bits packed raw, unencoded LICH +uint8_t lich_encoded[12]; //96 bits packed, encoded LICH +uint8_t enc_bits[SYM_PER_PLD*2]; //type-2 bits, unpacked +uint8_t rf_bits[SYM_PER_PLD*2]; //type-4 bits, unpacked + +float frame_buff[192]; +uint32_t frame_buff_cnt; + +uint8_t data[16], next_data[16]; //raw payload, packed bits +uint16_t fn=0; //16-bit Frame Number (for the stream mode) +uint8_t lich_cnt=0; //0..5 LICH counter +uint8_t got_lsf=0; //have we filled the LSF struct yet? +uint8_t finished=0; + +//encryption + +uint8_t encryption=0; +int type = 1; //1=AES128, 2=AES192, 3=AES256 + +uint8_t key[32]; //TODO: replace with a `-K` arg key entry +uint8_t iv[16]; + +time_t epoch = 1577836800L; //Jan 1, 2020, 00:00:00 UTC + +//main routine +int main(int argc, char* argv[]) +{ + //debug + //printf("%06X\n", golay24_encode(1)); //golay encoder codeword test + //printf("%d -> %d -> %d\n", 1, intrl_seq[1], intrl_seq[intrl_seq[1]]); //interleaver bijective reciprocality test, f(f(x))=x + //return 0; + + //encryption init + if(argc>1 && strstr(argv[1], "-K")) + encryption=2; //AES key was passed + + srand(time(NULL)); //seed random number generator + memset(key, 0, 32*sizeof(uint8_t)); + memset(iv, 0, 16*sizeof(uint8_t)); + + //generate random key + // for (uint8_t i = 0; i < 32; i++) + // key[i] = rand() & 0xFF; + + //hard coded key + for (uint8_t i = 0; i < 32; i++) + key[i] = 0x77; + + //print the random key + fprintf (stderr, "\nAES Key:"); + for (uint8_t i = 0; i < 32; i++) + { + if (i == 16) + fprintf (stderr, "\n "); + fprintf (stderr, " %02X", key[i]); + } + fprintf (stderr, "\n"); + + if(encryption==2) + { + //TODO: read user input key + + // *((int32_t*)&iv[0])=(uint32_t)time(NULL)-(uint32_t)epoch; //timestamp //note: I don't think this works as intended + for(uint8_t i=0; i<4; i++) iv[i] = ((uint32_t)(time(NULL)&0xFFFFFFFF)-(uint32_t)epoch) >> (24-(i*8)); + for(uint8_t i=4; i<4+10; i++) iv[i] = rand() & 0xFF; //10 random bytes + } + + memset(next_lsf.dst, 0xFF, 6*sizeof(uint8_t)); //broadcast + + //AB1CDE + next_lsf.src[0] = 0x00; + next_lsf.src[1] = 0x00; + next_lsf.src[2] = 0x1F; + next_lsf.src[3] = 0x24; + next_lsf.src[4] = 0x5D; + next_lsf.src[5] = 0x51; + + if (encryption == 2) //AES ENC, 3200 voice + { + next_lsf.type[0] = 0x03; + next_lsf.type[1] = 0x95; + } + else //no enc or enc_st field, normal 3200 voice + { + next_lsf.type[0] = 0x00; + next_lsf.type[1] = 0x05; + } + + finished = 0; + + if (encryption == 2) + { + memcpy(&(next_lsf.meta), iv, 14); //AES encryption enabled - use 112 bits of IV + } + else //scrambler, or clear + { + memset(next_lsf.meta, 0, 14*sizeof(uint8_t)); + } + + // while(!finished) + for (uint8_t z = 0; z < 31; z++) //just crank out 5 superframes + { + if(lich_cnt == 0) + { + lsf = next_lsf; + + //calculate LSF CRC + uint16_t ccrc=LSF_CRC(&lsf); + lsf.crc[0]=ccrc>>8; + lsf.crc[1]=ccrc&0xFF; + } + + memset(next_data, 0, sizeof(next_data)); + memcpy(data, next_data, sizeof(data)); + + if (encryption==2) //AES encryption enabled - use 112 bits of IV + { + memcpy(&(next_lsf.meta), iv, 14); + iv[14] = (fn >> 8) & 0xFF; + iv[15] = (fn >> 0) & 0xFF; + } + else //Scrambler, or Clear + { + memset(next_lsf.meta, 0, 14*sizeof(uint8_t)); + } + + if(got_lsf) //stream frames + { + //send stream frame syncword + frame_buff_cnt=0; + send_syncword(frame_buff, &frame_buff_cnt, SYNC_STR); + + //extract LICH from the whole LSF + extract_LICH(lich, lich_cnt, &lsf); + + //encode the LICH + encode_LICH(lich_encoded, lich); + + //unpack LICH (12 bytes) + unpack_LICH(enc_bits, lich_encoded); + + //encrypt + if(encryption==2) + { + fprintf(stderr, "FN: %03d; IV: ", fn); + for(uint8_t i=0; i<16; i++) + fprintf(stderr, "%02X", iv[i]); + fprintf(stderr, "\n"); + + fprintf(stderr, "\t IN: "); + for(uint8_t i=0; i<16; i++) + fprintf(stderr, "%02X", data[i]); + fprintf(stderr, "\n"); + + aes_ctr_bytewise_payload_crypt(iv, key, data, type); + + fprintf(stderr, "\tOUT: "); + for(uint8_t i=0; i<16; i++) + fprintf(stderr, "%02X", data[i]); + fprintf(stderr, "\n"); + } + + //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); + + //reorder bits + reorder_bits(rf_bits, enc_bits); + + //randomize + randomize_bits(rf_bits); + + //send dummy symbols (debug) + /*float s=0.0; + for(uint8_t i=0; i float.sym +//decode debug with -- m17-fme -r -f float.sym -v 1 -E '7777777777777777 7777777777777777 7777777777777777 7777777777777777' \ No newline at end of file