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,15 +829,24 @@ 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 (state->errs < 3)
rc4_voice_decrypt(state->dropL, 9, 7, rckey, cipher, plain);
else memcpy (plain, cipher, sizeof(plain));
state->dropL += 7; 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)
rc4_voice_decrypt(state->dropL, 9, 7, rckey, cipher, plain);
else memcpy (plain, cipher, sizeof(plain));
//unpack deciphered plain array back into ambe_d bit array state->dropL += 7;
memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d); //unpack deciphered plain array back into ambe_d bit array
memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d);
}
} }
@@ -875,74 +891,111 @@ 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) {}
uint8_t frame1_cipher[49]; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; {
uint8_t frame1_cipher[49];
decrypt_rc2((CryptoContext *)state->rc2_context, frame1_cipher);
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = frame1_cipher[i]; decrypt_rc2((CryptoContext *)state->rc2_context, frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char));
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) {}
short frame1_cipher[49]; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; else
decrypt_frame_49(frame1_cipher); {
short frame1_cipher[49];
memset (ambe_d, 0, 49*sizeof(char)); for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
for (int i = 0; i < 49; i++) ambe_d[i] = ctx.bits[i]; decrypt_frame_49(frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char));
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) {}
short frame1_cipher[49]; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; else
{
decrypt_frame_49_pc5(frame1_cipher); short frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = ctxpc5.bits[i]; decrypt_frame_49_pc5(frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char));
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)
{ {
for (int i = 0; i < 49; i++) if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
ambe_d[i] ^= (uint8_t)(ctx.bits[i] & 1); else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
for (int i = 0; i < 49; i++)
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)
{ {
for (int i = 0; i < 49; i++) if (memcmp(ambe_d, ambe_silence, 49) == 0)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%882] & 1); //Yikes! 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++)
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)
{ {
for (int i = 0; i < 49; i++) if (memcmp(ambe_d, ambe_silence, 49) == 0)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%16] & 1); //Yikes! 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++)
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)
{ {
//disable enc identifiers, if present if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->dmr_so = 0; state->static_ks_counter[state->currentslot] += 49;
state->payload_algid = 0; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
for (int i = 0; i < 49; i++) state->static_ks_counter[state->currentslot] += 49;
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%state->straight_mod] & 1); //Yikes! else
{
//disable enc identifiers, if present
state->dmr_so = 0;
state->payload_algid = 0;
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!
}
} }
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,
@@ -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,14 +1291,22 @@ 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 (state->errsR < 3) if (memcmp(ambe_d, ambe_silence, 49) == 0)
rc4_voice_decrypt(state->dropR, 9, 7, rckey, cipher, plain); state->dropR += 7;
else memcpy (plain, cipher, sizeof(plain)); else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
state->dropR += 7; state->dropR += 7;
else
{
if (state->errsR < 3)
rc4_voice_decrypt(state->dropR, 9, 7, rckey, cipher, plain);
else memcpy (plain, cipher, sizeof(plain));
state->dropR += 7;
//unpack deciphered plain array back into ambe_d bit array //unpack deciphered plain array back into ambe_d bit array
memset (ambe_d, 0, 49*sizeof(char)); memset (ambe_d, 0, 49*sizeof(char));
unpack_ambe(plain, ambe_d); unpack_ambe(plain, ambe_d);
}
} }
@@ -1291,74 +1351,112 @@ 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) {}
uint8_t frame1_cipher[49]; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; {
uint8_t frame1_cipher[49];
decrypt_rc2((CryptoContext *)state->rc2_context, frame1_cipher);
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = frame1_cipher[i]; decrypt_rc2((CryptoContext *)state->rc2_context, frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char));
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) {}
short frame1_cipher[49]; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; else
decrypt_frame_49(frame1_cipher); {
short frame1_cipher[49];
memset (ambe_d, 0, 49*sizeof(char)); for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
for (int i = 0; i < 49; i++) ambe_d[i] = ctx.bits[i]; decrypt_frame_49(frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char));
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) {}
short frame1_cipher[49]; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i]; else
{
decrypt_frame_49_pc5(frame1_cipher); short frame1_cipher[49];
for (int i = 0; i < 49; i++) frame1_cipher[i] = ambe_d[i];
memset (ambe_d, 0, 49*sizeof(char));
for (int i = 0; i < 49; i++) ambe_d[i] = ctxpc5.bits[i]; decrypt_frame_49_pc5(frame1_cipher);
memset (ambe_d, 0, 49*sizeof(char));
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)
{ {
for (int i = 0; i < 49; i++) if (memcmp(ambe_d, ambe_silence, 49) == 0) {}
ambe_d[i] ^= (uint8_t)(ctx.bits[i] & 1); else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0) {}
else
{
for (int i = 0; i < 49; i++)
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)
{ {
for (int i = 0; i < 49; i++) if (memcmp(ambe_d, ambe_silence, 49) == 0)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%882] & 1); //Yikes! 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++)
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)
{ {
for (int i = 0; i < 49; i++) if (memcmp(ambe_d, ambe_silence, 49) == 0)
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%16] & 1); //Yikes! 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++)
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)
{ {
//disable enc identifiers, if present if (memcmp(ambe_d, ambe_silence, 49) == 0)
state->dmr_soR = 0; state->static_ks_counter[state->currentslot] += 49;
state->payload_algidR = 0; else if (memcmp(ambe_d+24, zeroes+24, zeroes_threshold) == 0)
for (int i = 0; i < 49; i++) state->static_ks_counter[state->currentslot] += 49;
ambe_d[i] ^= (uint8_t)(state->static_ks_bits[state->currentslot][(state->static_ks_counter[state->currentslot]++)%state->straight_mod] & 1); //Yikes! else
{
//disable enc identifiers, if present
state->dmr_soR = 0;
state->payload_algidR = 0;
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!
}
} }
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,