Don't Apply Keystream when Silence or Zero DMR AMBE Frames; #319

This is primarily a fix for those CCR Radios that use the static silence frame and doesn't encrypt it either at the start of, or end of TX;

The Zeroes Fix (000D2C00000000) is for Anytone or TXI who has a load of zeroes starting, or ending a TX, or during the TXI window on certain VC-F frames;
This commit is contained in:
lwvmobile
2025-10-31 08:06:39 -04:00
parent fa47d0321b
commit f8521fda99
2 changed files with 204 additions and 114 deletions

View File

@@ -74,14 +74,6 @@ void MD2II_end(unsigned char h4[n1])
for (i = 0; i < n1global; i++) h4[i] = h1[i]; for (i = 0; i < n1global; i++) h4[i] = h1[i];
} }
// /* Convert 64-bit integer to bytes (big-endian) */
// static void u64_to_bytes_be(uint64_t val, unsigned char *out)
// {
// for (int i = 0; i < 8; i++) {
// out[i] = (unsigned char)((val >> (56 - 8 * i)) & 0xFF);
// }
// }
void print_hex(const unsigned char *buf, int len) void print_hex(const unsigned char *buf, int len)
{ {
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)

View File

@@ -175,6 +175,14 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
unsigned long long int k; unsigned long long int k;
int x; int x;
//keystream and silence conditional items
uint64_t silence = 0xF801A99F8CE080; //AMBE+2 default silence vector expressed as a 56-bit hex value.
char ambe_silence[49];
for (i = 0; i < 49; i++)
ambe_silence[i] = (silence >> (55-i)) & 1;
char zeroes[49]; memset(zeroes, 0, sizeof(zeroes));
size_t zeroes_threshold = 24; //zeroed ambe_d after golay is 000D2C00000000, look at +24 position for 24 bits
//these conditions should ensure no clashing with the BP/HBP/Scrambler key loading machanisms already coded in //these conditions should ensure no clashing with the BP/HBP/Scrambler key loading machanisms already coded in
if (state->currentslot == 0 && state->payload_algid != 0 && state->payload_algid != 0x80 && state->keyloader == 1) if (state->currentslot == 0 && state->payload_algid != 0 && state->payload_algid != 0x80 && state->keyloader == 1)
keyring (opts, state); keyring (opts, state);
@@ -773,18 +781,17 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
} }
} }
//now we do the bit by bit xor depending on the frame and position of the state bit counter //skip keystream if silence or zeroes (some CCR), else apply keystream directly and increment counter
//run 6 instead of 7 so we can just do bit 49 outside of loop to keep extra bits overloading the array if (memcmp(ambe_d, ambe_silence, 49) == 0)
z = 0; state->bit_counterL += 49;
for (i = 0; i < 6; i++) else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->bit_counterL += 49;
else
{ {
for (j = 0; j < 8; j++) for (i = 0; i < 49; i++)
ambe_d[z++] ^= state->ks_bitstreamL[state->bit_counterL++]; ambe_d[i] ^= state->ks_bitstreamL[state->bit_counterL++];
} }
//last bit
ambe_d[48] ^= state->ks_bitstreamL[state->bit_counterL++];
//skip the next 7 bits of the array (if not Hytera Enhanced) //skip the next 7 bits of the array (if not Hytera Enhanced)
if(state->payload_algid != 0x02) if(state->payload_algid != 0x02)
state->bit_counterL += 7; state->bit_counterL += 7;
@@ -822,6 +829,13 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
//that may occur on some systems that preempt VC6 voice for a RC opportuninity (TXI) //that may occur on some systems that preempt VC6 voice for a RC opportuninity (TXI)
//this occurs because we are supposed to either have a a 'repeat' frame, or 'silent' frame play //this occurs because we are supposed to either have a a 'repeat' frame, or 'silent' frame play
//due to the error, but the keystream application makes it random 'pfft pop' sound instead //due to the error, but the keystream application makes it random 'pfft pop' sound instead
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->dropL += 7;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->dropL += 7;
else
{
if (state->errs < 3) if (state->errs < 3)
rc4_voice_decrypt(state->dropL, 9, 7, rckey, cipher, plain); rc4_voice_decrypt(state->dropL, 9, 7, rckey, cipher, plain);
else memcpy (plain, cipher, sizeof(plain)); else memcpy (plain, cipher, sizeof(plain));
@@ -834,6 +848,8 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
} }
}
//P25p2 RC4 Handling, VCH 0 //P25p2 RC4 Handling, VCH 0
if (state->currentslot == 0 && state->payload_algid == 0xAA && state->R != 0 && ((state->synctype == 35) || (state->synctype == 36))) if (state->currentslot == 0 && state->payload_algid == 0xAA && state->R != 0 && ((state->synctype == 35) || (state->synctype == 36)))
{ {
@@ -875,7 +891,10 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
//DMR Retevis AP, Either Slot (static single key'd enforced KS) //DMR Retevis AP, Either Slot (static single key'd enforced KS)
if (state->retevis_ap == 1) if (state->retevis_ap == 1)
{ {
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
uint8_t frame1_cipher[49]; uint8_t frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
@@ -884,26 +903,34 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = frame1_cipher[i]; for (int i = 0; i < 49; i++) ambe_d[i] = frame1_cipher[i];
}
} }
//DMR TYT AP, Either Slot (static single key'd enforced KS) //DMR TYT AP, Either Slot (static single key'd enforced KS)
if (state->tyt_ap == 1) if (state->tyt_ap == 1)
{ {
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
short frame1_cipher[49]; short frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
decrypt_frame_49(frame1_cipher); decrypt_frame_49(frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = ctx.bits[i]; for (int i = 0; i < 49; i++) ambe_d[i] = ctx.bits[i];
}
} }
//DMR BAOFENG AP, Either Slot (static single key'd enforced KS) //DMR BAOFENG AP, Either Slot (static single key'd enforced KS)
if (state->baofeng_ap == 1) if (state->baofeng_ap == 1)
{ {
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
short frame1_cipher[49]; short frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
@@ -911,32 +938,57 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = ctxpc5.bits[i]; for (int i = 0; i < 49; i++) ambe_d[i] = ctxpc5.bits[i];
}
} }
//DMR TYT EP, Either Slot (static single key'd enforced KS) //DMR TYT EP, Either Slot (static single key'd enforced KS)
if (state->tyt_ep == 1) if (state->tyt_ep == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{ {
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(ctx.bits[i] & 1); ambe_d[i] ^= (uint8_t)(ctx.bits[i] & 1);
} }
}
//DMR Kenwood Scrambler, Either Slot (static single key'd enforced KS) //should probably break this up, but this is a test for now //DMR Kenwood Scrambler, Either Slot (static single key'd enforced KS)
if (state->ken_sc == 1) if (state->ken_sc == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->static_ks_counter[state->currentslot] += 49;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->static_ks_counter[state->currentslot] += 49;
else
{ {
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%882] & 1); //Yikes! ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%882] & 1); //Yikes!
} }
}
//DMR Anytone BP, Either Slot (static single key'd enforced KS) //DMR Anytone BP, Either Slot (static single key'd enforced KS)
if (state->any_bp == 1) if (state->any_bp == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->static_ks_counter[state->currentslot] += 49;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->static_ks_counter[state->currentslot] += 49;
else
{ {
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%16] & 1); //Yikes! ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%16] & 1); //Yikes!
} }
}
//Generic Straight Static Keystream //Generic Straight Static Keystream
if (state->straight_ks == 1) if (state->straight_ks == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->static_ks_counter[state->currentslot] += 49;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->static_ks_counter[state->currentslot] += 49;
else
{ {
//disable enc identifiers, if present //disable enc identifiers, if present
state->dmr_so = 0; state->dmr_so = 0;
@@ -944,6 +996,7 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%state->straight_mod] & 1); //Yikes! ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%state->straight_mod] & 1); //Yikes!
} }
}
mbe_processAmbe2450Dataf (state->audio_out_temp_buf, &state->errs, &state->errs2, state->err_str, mbe_processAmbe2450Dataf (state->audio_out_temp_buf, &state->errs, &state->errs2, state->err_str,
ambe_d, state->cur_mp, state->prev_mp, state->prev_mp_enhanced, opts->uvquality); ambe_d, state->cur_mp, state->prev_mp, state->prev_mp_enhanced, opts->uvquality);
@@ -1190,18 +1243,17 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
} }
} }
//now we do the bit by bit xor depending on the frame and position of the state bit counter //skip keystream if silence or zeroes (some CCR), else apply keystream directly and increment counter
//run 6 instead of 7 so we can just do bit 49 outside of loop to keep extra bits overloading the array if (memcmp(ambe_d, ambe_silence, 49) == 0)
z = 0; state->bit_counterR += 49;
for (i = 0; i < 6; i++) else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->bit_counterR += 49;
else
{ {
for (j = 0; j < 8; j++) for (i = 0; i < 49; i++)
ambe_d[z++] ^= state->ks_bitstreamR[state->bit_counterR++]; ambe_d[i] ^= state->ks_bitstreamR[state->bit_counterR++];
} }
//last bit
ambe_d[48] ^= state->ks_bitstreamR[state->bit_counterR++];
//skip the next 7 bits of the array (if not Hytera Enhanced) //skip the next 7 bits of the array (if not Hytera Enhanced)
if(state->payload_algidR != 0x02) if(state->payload_algidR != 0x02)
state->bit_counterR += 7; state->bit_counterR += 7;
@@ -1239,6 +1291,12 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
//that may occur on some systems that preempt VC6 voice for a RC opportuninity (TXI) //that may occur on some systems that preempt VC6 voice for a RC opportuninity (TXI)
//this occurs because we are supposed to either have a a 'repeat' frame, or 'silent' frame play //this occurs because we are supposed to either have a a 'repeat' frame, or 'silent' frame play
//due to the error, but the keystream application makes it random 'pfft pop' sound instead //due to the error, but the keystream application makes it random 'pfft pop' sound instead
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->dropR += 7;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->dropR += 7;
else
{
if (state->errsR < 3) if (state->errsR < 3)
rc4_voice_decrypt(state->dropR, 9, 7, rckey, cipher, plain); rc4_voice_decrypt(state->dropR, 9, 7, rckey, cipher, plain);
else memcpy (plain, cipher, sizeof(plain)); else memcpy (plain, cipher, sizeof(plain));
@@ -1250,6 +1308,8 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
} }
}
//P25p2 RC4 Handling, VCH 1 //P25p2 RC4 Handling, VCH 1
if (state->currentslot == 1 && state->payload_algidR == 0xAA && state->RR != 0 && ((state->synctype == 35) || (state->synctype == 36))) if (state->currentslot == 1 && state->payload_algidR == 0xAA && state->RR != 0 && ((state->synctype == 35) || (state->synctype == 36)))
{ {
@@ -1291,7 +1351,10 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
//DMR Retevis AP, Either Slot (static single key'd enforced KS) //DMR Retevis AP, Either Slot (static single key'd enforced KS)
if (state->retevis_ap == 1) if (state->retevis_ap == 1)
{ {
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
uint8_t frame1_cipher[49]; uint8_t frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
@@ -1300,26 +1363,34 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = frame1_cipher[i]; for (int i = 0; i < 49; i++) ambe_d[i] = frame1_cipher[i];
}
} }
//DMR TYT AP, Either Slot (static single key'd enforced KS) //DMR TYT AP, Either Slot (static single key'd enforced KS)
if (state->tyt_ap == 1) if (state->tyt_ap == 1)
{ {
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
short frame1_cipher[49]; short frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
decrypt_frame_49(frame1_cipher); decrypt_frame_49(frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = ctx.bits[i]; for (int i = 0; i < 49; i++) ambe_d[i] = ctx.bits[i];
}
} }
//DMR BAOFENG AP, Either Slot (static single key'd enforced KS) //DMR BAOFENG AP, Either Slot (static single key'd enforced KS)
if (state->baofeng_ap == 1) if (state->baofeng_ap == 1)
{ {
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
short frame1_cipher[49]; short frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
@@ -1327,32 +1398,58 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = ctxpc5.bits[i]; for (int i = 0; i < 49; i++) ambe_d[i] = ctxpc5.bits[i];
}
} }
//DMR TYT EP, Either Slot (static single key'd enforced KS) //DMR TYT EP, Either Slot (static single key'd enforced KS)
if (state->tyt_ep == 1) if (state->tyt_ep == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{ {
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(ctx.bits[i] & 1); ambe_d[i] ^= (uint8_t)(ctx.bits[i] & 1);
} }
}
//DMR Kenwood Scrambler, Either Slot (static single key'd enforced KS) //should probably break this up, but this is a test for now //DMR Kenwood Scrambler, Either Slot (static single key'd enforced KS) //should probably break this up, but this is a test for now
if (state->ken_sc == 1) if (state->ken_sc == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->static_ks_counter[state->currentslot] += 49;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->static_ks_counter[state->currentslot] += 49;
else
{ {
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%882] & 1); //Yikes! ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%882] & 1); //Yikes!
} }
}
//DMR Anytone BP, Either Slot (static single key'd enforced KS) //DMR Anytone BP, Either Slot (static single key'd enforced KS)
if (state->any_bp == 1) if (state->any_bp == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->static_ks_counter[state->currentslot] += 49;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->static_ks_counter[state->currentslot] += 49;
else
{ {
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%16] & 1); //Yikes! ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%16] & 1); //Yikes!
} }
}
//Generic Straight Static Keystream //Generic Straight Static Keystream
if (state->straight_ks == 1) if (state->straight_ks == 1)
{
if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->static_ks_counter[state->currentslot] += 49;
else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->static_ks_counter[state->currentslot] += 49;
else
{ {
//disable enc identifiers, if present //disable enc identifiers, if present
state->dmr_soR = 0; state->dmr_soR = 0;
@@ -1360,6 +1457,7 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
for (int i = 0; i < 49; i++) for (int i = 0; i < 49; i++)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%state->straight_mod] & 1); //Yikes! ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%state->straight_mod] & 1); //Yikes!
} }
}
mbe_processAmbe2450Dataf (state->audio_out_temp_bufR, &state->errsR, &state->errs2R, state->err_strR, mbe_processAmbe2450Dataf (state->audio_out_temp_bufR, &state->errsR, &state->errs2R, state->err_strR,
ambe_d, state->cur_mp2, state->prev_mp2, state->prev_mp_enhanced2, opts->uvquality); ambe_d, state->cur_mp2, state->prev_mp2, state->prev_mp_enhanced2, opts->uvquality);