Include pack_ambe and unpack_ambe functions and use during certain keystream applications;

This commit is contained in:
lwvmobile 2024-07-20 16:41:35 -04:00
parent 8b76daa35f
commit f6fc91a051
3 changed files with 60 additions and 184 deletions

View File

@ -1145,6 +1145,10 @@ void pack_bit_array_into_byte_array (uint8_t * input, uint8_t * output, int len)
void pack_bit_array_into_byte_array_asym (uint8_t * input, uint8_t * output, int len);
void unpack_byte_array_into_bit_array (uint8_t * input, uint8_t * output, int len);
//ambe pack and unpack functions
void pack_ambe (char * input, uint8_t * output, int len);
void unpack_ambe (uint8_t * input, char * ambe);
void ncursesOpen (dsd_opts * opts, dsd_state * state);
void ncursesPrinter (dsd_opts * opts, dsd_state * state);
void ncursesClose ();

View File

@ -600,4 +600,36 @@ void unpack_byte_array_into_bit_array (uint8_t * input, uint8_t * output, int le
output[k++] = (input[i] >> 1) & 1;
output[k++] = (input[i] >> 0) & 1;
}
}
//take len amount of bits and pack into x amount of bytes (asymmetrical)
void pack_ambe (char * input, uint8_t * output, int len)
{
int i = 0; int k = len % 8;
for (i = 0; i < len; i++)
{
output[i/8] <<= 1;
output[i/8] |= (uint8_t)input[i];
}
//if any leftover bits that don't flush the last byte fully packed, shift them over left
if (k)
output[i/8] <<= 8-k;
}
//unpack byte array with ambe data into a 49-bit bitwise array
void unpack_ambe (uint8_t * input, char * ambe)
{
int i = 0, k = 0;
for (i = 0; i < 7; i++)
{
ambe[k++] = (input[i] >> 7) & 1;
ambe[k++] = (input[i] >> 6) & 1;
ambe[k++] = (input[i] >> 5) & 1;
ambe[k++] = (input[i] >> 4) & 1;
ambe[k++] = (input[i] >> 3) & 1;
ambe[k++] = (input[i] >> 2) & 1;
ambe[k++] = (input[i] >> 1) & 1;
ambe[k++] = (input[i] >> 0) & 1;
}
ambe[48] = input[6] >> 7;
}

View File

@ -554,35 +554,8 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
rckey[7] = ((state->payload_mi & 0xFF00) >> 8);
rckey[8] = ((state->payload_mi & 0xFF) >> 0);
// if (opts->payload == 1)
// {
// fprintf (stderr, "%s", KYEL);
// fprintf (stderr, " RC4K ");
// for (short o = 0; o < 9; o++)
// {
// fprintf (stderr, "%02X", rckey[o]);
// }
// fprintf (stderr, "%s", KNRM);
// fprintf (stderr, "\n");
// }
short k = 0;
for (short o = 0; o < 7; o++)
{
short b = 0;
for (short p = 0; p < 8; p++)
{
b = b << 1;
b = b + ambe_d[k];
k++;
if (k == 49)
{
cipher[6] = ((b << 3) & 0x80); //set 7th octet/bit 49 accordingly
break;
}
}
cipher[o] = b;
}
//pack cipher byte array from ambe_d bit array
pack_ambe(ambe_d, cipher, 49);
//only run keystream application if errs < 3 -- this is a fix to the pop sound
//that may occur on some systems that preempt VC6 voice for a RC opportuninity (TXI)
@ -594,23 +567,10 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
state->dropL += 7;
short z = 0;
for (short p = 0; p <= 6; p++)
{
//convert bytes back to bits and load into ambe_d array.
short b = 0; //reset b to use again
for (short o = 0; o < 8; o++)
{
b = (( (plain[p] << o) & 0x80) >> 7);
ambe_d[z] = b;
z++;
if (z == 49)
{
ambe_d[48] = ( (plain[p] << o) & 0x80);
break;
}
}
}
//unpack deciphered plain array back into ambe_d bit array
memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d);
}
//P25p2 RC4 Handling, VCH 0
@ -639,56 +599,16 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
rckey[11] = ((state->payload_miP & 0xFF00) >> 8);
rckey[12] = ((state->payload_miP & 0xFF) >> 0);
// if (opts->payload == 1)
// {
// fprintf (stderr, "%s", KYEL);
// fprintf (stderr, " RC4K ");
// for (short o = 0; o < 13; o++)
// {
// fprintf (stderr, "%02X", rckey[o]);
// }
// fprintf (stderr, "%s", KNRM);
// fprintf (stderr, "\n");
// }
short k = 0;
for (short o = 0; o < 7; o++)
{
short b = 0;
for (short p = 0; p < 8; p++)
{
b = b << 1;
b = b + ambe_d[k];
k++;
if (k == 49)
{
cipher[6] = ((b << 3) & 0x80); //set 7th octet/bit 49 accordingly
break;
}
}
cipher[o] = b;
}
//pack cipher byte array from ambe_d bit array
pack_ambe(ambe_d, cipher, 49);
RC4(state->dropL, 13, 7, rckey, cipher, plain);
state->dropL += 7;
short z = 0;
for (short p = 0; p <= 6; p++)
{
//convert bytes back to bits and load into ambe_d array.
short b = 0; //reset b to use again
for (short o = 0; o < 8; o++)
{
b = (( (plain[p] << o) & 0x80) >> 7);
ambe_d[z] = b;
z++;
if (z == 49)
{
ambe_d[48] = ( (plain[p] << o) & 0x80);
break;
}
}
}
//unpack deciphered plain array back into ambe_d bit array
memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d);
}
mbe_processAmbe2450Dataf (state->audio_out_temp_buf, &state->errs, &state->errs2, state->err_str,
@ -840,35 +760,8 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
rckey[7] = ((state->payload_miR & 0xFF00) >> 8);
rckey[8] = ((state->payload_miR & 0xFF) >> 0);
// if (opts->payload == 1)
// {
// fprintf (stderr, "%s", KYEL);
// fprintf (stderr, " RC4K ");
// for (short o = 0; o < 9; o++)
// {
// fprintf (stderr, "%02X", rckey[o]);
// }
// fprintf (stderr, "%s", KNRM);
// fprintf (stderr, "\n");
// }
short k = 0;
for (short o = 0; o < 7; o++)
{
short b = 0;
for (short p = 0; p < 8; p++)
{
b = b << 1;
b = b + ambe_d[k];
k++;
if (k == 49)
{
cipher[6] = ((b << 3) & 0x80); //set 7th octet/bit 49 accordingly
break;
}
}
cipher[o] = b;
}
//pack cipher byte array from ambe_d bit array
pack_ambe(ambe_d, cipher, 49);
//only run keystream application if errs < 3 -- this is a fix to the pop sound
//that may occur on some systems that preempt VC6 voice for a RC opportuninity (TXI)
@ -879,23 +772,10 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
else memcpy (plain, cipher, sizeof(plain));
state->dropR += 7;
short z = 0;
for (short p = 0; p <= 6; p++)
{
//convert bytes back to bits and load into ambe_d array.
short b = 0; //reset b to use again
for (short o = 0; o < 8; o++)
{
b = (( (plain[p] << o) & 0x80) >> 7);
ambe_d[z] = b;
z++;
if (z == 49)
{
ambe_d[48] = ( (plain[p] << o) & 0x80);
break;
}
}
}
//unpack deciphered plain array back into ambe_d bit array
memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d);
}
//P25p2 RC4 Handling, VCH 1
@ -924,56 +804,16 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
rckey[11] = ((state->payload_miN & 0xFF00) >> 8);
rckey[12] = ((state->payload_miN & 0xFF) >> 0);
// if (opts->payload == 1)
// {
// fprintf (stderr, "%s", KYEL);
// fprintf (stderr, " RC4K ");
// for (short o = 0; o < 13; o++)
// {
// fprintf (stderr, "%02X", rckey[o]);
// }
// fprintf (stderr, "%s", KNRM);
// fprintf (stderr, "\n");
// }
short k = 0;
for (short o = 0; o < 7; o++)
{
short b = 0;
for (short p = 0; p < 8; p++)
{
b = b << 1;
b = b + ambe_d[k];
k++;
if (k == 49)
{
cipher[6] = ((b << 3) & 0x80); //set 7th octet/bit 49 accordingly
break;
}
}
cipher[o] = b;
}
//pack cipher byte array from ambe_d bit array
pack_ambe(ambe_d, cipher, 49);
RC4(state->dropR, 13, 7, rckey, cipher, plain);
state->dropR += 7;
short z = 0;
for (short p = 0; p <= 6; p++)
{
//convert bytes back to bits and load into ambe_d array.
short b = 0; //reset b to use again
for (short o = 0; o < 8; o++)
{
b = (( (plain[p] << o) & 0x80) >> 7);
ambe_d[z] = b;
z++;
if (z == 49)
{
ambe_d[48] = ( (plain[p] << o) & 0x80);
break;
}
}
}
//unpack deciphered plain array back into ambe_d bit array
memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d);
}
mbe_processAmbe2450Dataf (state->audio_out_temp_bufR, &state->errsR, &state->errs2R, state->err_strR,