Fix Some Tab Alignment Issues;

NXDN: Make seperate AES IV expansion function;
This commit is contained in:
lwvmobile
2025-03-15 20:26:25 -04:00
parent eac360571e
commit e36672f54c
7 changed files with 121 additions and 85 deletions

View File

@@ -1519,8 +1519,9 @@ void aes_cfb_bytewise_payload_crypt (uint8_t * iv, uint8_t * key, uint8_t * in,
void aes_ctr_bytewise_payload_crypt (uint8_t * iv, uint8_t * key, uint8_t * payload, int type); void aes_ctr_bytewise_payload_crypt (uint8_t * iv, uint8_t * key, uint8_t * payload, int type);
void aes_ctr_bitwise_payload_crypt (uint8_t * iv, uint8_t * key, uint8_t * payload, int type); void aes_ctr_bitwise_payload_crypt (uint8_t * iv, uint8_t * key, uint8_t * payload, int type);
//LFSR to expand either a DMR 32-bit or P25 64-bit MI into a 128-bit IV for AES //LFSR to expand either a DMR 32-bit or P25/NXDN 64-bit MI into a 128-bit IV for AES
void LFSR128(dsd_state * state); void LFSR128(dsd_state * state);
void LFSR128n(dsd_state * state);
void LFSR128d(dsd_state * state); void LFSR128d(dsd_state * state);

View File

@@ -778,54 +778,54 @@ void des56_ca_keystream_output (uint8_t * main_key, uint8_t * iv, uint8_t * ks_b
//transitional function mainly to load key and iv into an array //transitional function mainly to load key and iv into an array
void des_multi_keystream_output (unsigned long long int mi, unsigned long long int key_ulli, uint8_t * output, int type, int len) void des_multi_keystream_output (unsigned long long int mi, unsigned long long int key_ulli, uint8_t * output, int type, int len)
{ {
int i = 0; int i = 0;
uint8_t iv[8]; memset (iv, 0, sizeof(iv)); uint8_t iv[8]; memset (iv, 0, sizeof(iv));
uint8_t key[8]; memset(key, 0, sizeof(key)); uint8_t key[8]; memset(key, 0, sizeof(key));
//convert unsinged long long int values into array values //convert unsinged long long int values into array values
for (i = 7; i >= 0; i--) for (i = 7; i >= 0; i--)
iv[7-i] = (mi >> (8*i)) & 0xFF; iv[7-i] = (mi >> (8*i)) & 0xFF;
for (i = 7; i >= 0; i--) for (i = 7; i >= 0; i--)
key[7-i] = (key_ulli >> (8*i)) & 0xFF; key[7-i] = (key_ulli >> (8*i)) & 0xFF;
//debug //debug
// fprintf (stderr, "\n IV: "); // fprintf (stderr, "\n IV: ");
// for (i = 0; i < 8; i++) // for (i = 0; i < 8; i++)
// fprintf (stderr, "%02X", iv[i]); // fprintf (stderr, "%02X", iv[i]);
// fprintf (stderr, "\n Key: "); // fprintf (stderr, "\n Key: ");
// for (i = 0; i < 8; i++) // for (i = 0; i < 8; i++)
// fprintf (stderr, "%02X", key[i]); // fprintf (stderr, "%02X", key[i]);
//types: 1 = DES56 OFB; 2 = DES56 CA (XL) //types: 1 = DES56 OFB; 2 = DES56 CA (XL)
if (type == 2) if (type == 2)
{ {
if (len == 0) if (len == 0)
des56_ca_keystream_output(key, iv, output, 1, 110+696, 1704); des56_ca_keystream_output(key, iv, output, 1, 110+696, 1704);
else des56_ca_keystream_output(key, iv, output, 1, 110+000, 1704); else des56_ca_keystream_output(key, iv, output, 1, 110+000, 1704);
} }
else des56_ofb_keystream_output(key, iv, output, 1, len); else des56_ofb_keystream_output(key, iv, output, 1, len);
} }
//transitional function mainly to load iv into an array //transitional function mainly to load iv into an array
void tdea_multi_keystream_output (unsigned long long int mi, uint8_t * key, uint8_t * output, int type, int len) void tdea_multi_keystream_output (unsigned long long int mi, uint8_t * key, uint8_t * output, int type, int len)
{ {
int i = 0; int i = 0;
uint8_t iv[8]; memset (iv, 0, sizeof(iv)); uint8_t iv[8]; memset (iv, 0, sizeof(iv));
//convert unsinged long long int values into array values //convert unsinged long long int values into array values
for (i = 7; i >= 0; i--) for (i = 7; i >= 0; i--)
iv[7-i] = (mi >> (8*i)) & 0xFF; iv[7-i] = (mi >> (8*i)) & 0xFF;
//debug //debug
// fprintf (stderr, "\n IV: "); // fprintf (stderr, "\n IV: ");
// for (i = 0; i < 8; i++) // for (i = 0; i < 8; i++)
// fprintf (stderr, "%02X", iv[i]); // fprintf (stderr, "%02X", iv[i]);
//type 1 = TDEA TOFB //TODO: Add more types like 2DES? //type 1 = TDEA TOFB //TODO: Add more types like 2DES?
if (type == 1) if (type == 1)
tdea_tofb_keystream_output(key, key+8, key+16, iv, output, 1, len); tdea_tofb_keystream_output(key, key+8, key+16, iv, output, 1, len);
else tdea_tofb_keystream_output(key, key+8, key+16, iv, output, 1, len); else tdea_tofb_keystream_output(key, key+8, key+16, iv, output, 1, len);
} }

View File

@@ -47,43 +47,43 @@ void rc4_voice_decrypt(int drop, uint8_t keylength, uint8_t messagelength, uint8
//this is for PDU usage //this is for PDU usage
void rc4_block_output (int drop, int keylen, int meslen, uint8_t * key, uint8_t * output_blocks) void rc4_block_output (int drop, int keylen, int meslen, uint8_t * key, uint8_t * output_blocks)
{ {
int i, j, x, count; int i, j, x, count;
unsigned int keylength = (unsigned int)keylen; unsigned int keylength = (unsigned int)keylen;
unsigned int messagelength = (unsigned int)meslen; unsigned int messagelength = (unsigned int)meslen;
unsigned int S[256]; unsigned int S[256];
for(i=0; i<256; i++) for(i=0; i<256; i++)
S[i] = i; S[i] = i;
j = 0; j = 0;
for(i = 0; i<256; i++) for(i = 0; i<256; i++)
{ {
j = (j + S[i] + key[i % keylength]) % 256; j = (j + S[i] + key[i % keylength]) % 256;
unsigned int temp = S[i]; unsigned int temp = S[i];
S[i] = S[j]; S[i] = S[j];
S[j] = temp; S[j] = temp;
} }
//Generate Keystream //Generate Keystream
i = 0; i = 0;
j = 0; j = 0;
x = 0; x = 0;
unsigned int byte; unsigned int byte;
// fprintf (stderr, " Keystream Octets = "); // fprintf (stderr, " Keystream Octets = ");
for(count = 0; count < (messagelength + drop); count++) for(count = 0; count < (messagelength + drop); count++)
{ {
i = (i + 1) % 256; i = (i + 1) % 256;
j = (j + S[i]) % 256; j = (j + S[i]) % 256;
unsigned int temp = S[i]; unsigned int temp = S[i];
S[i] = S[j]; S[i] = S[j];
S[j] = temp; S[j] = temp;
byte = S[(S[i] + S[j]) % 256]; byte = S[(S[i] + S[j]) % 256];
//Collect Output blocks //Collect Output blocks
if (count >= drop) if (count >= drop)
output_blocks[x++] = byte; output_blocks[x++] = byte;
} }
} }

View File

@@ -186,48 +186,48 @@ void LFSR(dsd_state * state)
//Expand a 32-bit MI into a 64-bit IV for DES //Expand a 32-bit MI into a 64-bit IV for DES
void LFSR64(dsd_state * state) void LFSR64(dsd_state * state)
{ {
{ {
unsigned long long int lfsr = 0; unsigned long long int lfsr = 0;
if (state->currentslot == 0) if (state->currentslot == 0)
{ {
lfsr = (uint64_t) state->payload_mi; lfsr = (uint64_t) state->payload_mi;
} }
else lfsr = (uint64_t) state->payload_miR; else lfsr = (uint64_t) state->payload_miR;
uint8_t cnt = 0; uint8_t cnt = 0;
for(cnt=0;cnt<32;cnt++) for(cnt=0;cnt<32;cnt++)
{ {
unsigned long long int bit = ( (lfsr >> 31) ^ (lfsr >> 21) ^ (lfsr >> 1) ^ (lfsr >> 0) ) & 0x1; unsigned long long int bit = ( (lfsr >> 31) ^ (lfsr >> 21) ^ (lfsr >> 1) ^ (lfsr >> 0) ) & 0x1;
lfsr = (lfsr << 1) | bit; lfsr = (lfsr << 1) | bit;
} }
if (state->currentslot == 0) if (state->currentslot == 0)
{ {
fprintf (stderr, "%s", KYEL); fprintf (stderr, "%s", KYEL);
fprintf (stderr, " Slot 1"); fprintf (stderr, " Slot 1");
fprintf (stderr, " DMR PI C- ALG ID: 0x%02X KEY ID: 0x%02X", state->payload_algid, state->payload_keyid); fprintf (stderr, " DMR PI C- ALG ID: 0x%02X KEY ID: 0x%02X", state->payload_algid, state->payload_keyid);
fprintf (stderr, " MI(64): 0x%016llX", lfsr); fprintf (stderr, " MI(64): 0x%016llX", lfsr);
fprintf (stderr, "%s", KNRM); fprintf (stderr, "%s", KNRM);
state->payload_mi = lfsr & 0xFFFFFFFF; //truncate for next repitition and le verification state->payload_mi = lfsr & 0xFFFFFFFF; //truncate for next repitition and le verification
state->payload_miP = lfsr; state->payload_miP = lfsr;
state->DMRvcL = 0; state->DMRvcL = 0;
} }
if (state->currentslot == 1) if (state->currentslot == 1)
{ {
fprintf (stderr, "%s", KYEL); fprintf (stderr, "%s", KYEL);
fprintf (stderr, " Slot 2"); fprintf (stderr, " Slot 2");
fprintf (stderr, " DMR PI C- ALG ID: 0x%02X KEY ID: 0x%02X", state->payload_algidR, state->payload_keyidR); fprintf (stderr, " DMR PI C- ALG ID: 0x%02X KEY ID: 0x%02X", state->payload_algidR, state->payload_keyidR);
fprintf (stderr, " MI(64): 0x%016llX", lfsr); fprintf (stderr, " MI(64): 0x%016llX", lfsr);
fprintf (stderr, "%s", KNRM); fprintf (stderr, "%s", KNRM);
state->payload_miR = lfsr & 0xFFFFFFFF; //truncate for next repitition and le verification state->payload_miR = lfsr & 0xFFFFFFFF; //truncate for next repitition and le verification
state->payload_miN = lfsr; state->payload_miN = lfsr;
state->DMRvcR = 0; state->DMRvcR = 0;
} }
} }
} }

View File

@@ -516,12 +516,12 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a
{ {
//more debug //more debug
fprintf (stderr, " IV: %016llX; Key: %016llX", state->payload_miN, state->R); // fprintf (stderr, " IV: %016llX; Key: %016llX", state->payload_miN, state->R);
memset (state->ks_octetL, 0, sizeof(state->ks_octetL)); memset (state->ks_octetL, 0, sizeof(state->ks_octetL));
memset (state->ks_bitstreamL, 0, sizeof(state->ks_bitstreamL)); memset (state->ks_bitstreamL, 0, sizeof(state->ks_bitstreamL));
aes_ofb_keystream_output (state->aes_ivR, state->aes_key, state->ks_octetL, 2, 15); //14 + 1 discard round aes_ofb_keystream_output (state->aes_iv, state->aes_key, state->ks_octetL, 2, 15); //14 + 1 discard round
//reset bit_counter //reset bit_counter
state->bit_counterL = 0; state->bit_counterL = 0;

View File

@@ -1336,3 +1336,43 @@ uint8_t crc7_scch(uint8_t bits[], int len)
} }
return load_i(s, 7); return load_i(s, 7);
} }
void LFSR128n(dsd_state * state)
{
//generate a 128-bit IV from a 64-bit IV for AES blocks
unsigned long long int lfsr = state->payload_miN;
//start packing aes_iv
state->aes_iv[0] = (lfsr >> 56) & 0xFF;
state->aes_iv[1] = (lfsr >> 48) & 0xFF;
state->aes_iv[2] = (lfsr >> 40) & 0xFF;
state->aes_iv[3] = (lfsr >> 32) & 0xFF;
state->aes_iv[4] = (lfsr >> 24) & 0xFF;
state->aes_iv[5] = (lfsr >> 16) & 0xFF;
state->aes_iv[6] = (lfsr >> 8 ) & 0xFF;
state->aes_iv[7] = (lfsr >> 0 ) & 0xFF;
int cnt = 0; int x = 64;
unsigned long long int bit;
//polynomial P(x) = 1 + X15 + X27 + X38 + X46 + X62 + X64
for(cnt=0;cnt<64;cnt++)
{
//63,61,45,37,27,14
// Polynomial is C(x) = x^64 + x^62 + x^46 + x^38 + x^27 + x^15 + 1
bit = ((lfsr >> 63) ^ (lfsr >> 61) ^ (lfsr >> 45) ^ (lfsr >> 37) ^ (lfsr >> 26) ^ (lfsr >> 14)) & 0x1;
lfsr = (lfsr << 1) | bit;
//continue packing aes_iv
state->aes_iv[x/8] = (state->aes_iv[x/8] << 1) + bit;
x++;
}
fprintf (stderr, "%s", KYEL);
fprintf (stderr, "\n");
fprintf (stderr, " IV(128): 0x");
for (x = 0; x < 16; x++)
fprintf (stderr, "%02X", state->aes_iv[x]);
fprintf (stderr, "%s", KNRM);
}

View File

@@ -1420,14 +1420,9 @@ void NXDN_decode_VCALL_IV(dsd_opts * opts, dsd_state * state, uint8_t * Message)
} }
//use the P25 Slot 1 LFSR128 to generate a 128-bit IV (state->payload_miN is on slot1) //expand the IV into 128-bit
if (state->nxdn_cipher_type == 0x03) if (state->nxdn_cipher_type == 0x03)
{ LFSR128n(state);
state->currentslot = 1; //quickly change to slot 1
fprintf (stderr, "\n NXDN -- ");
LFSR128(state); //TODO: Make a different function so we don't see the LDU2/ESS-B info and null key values, or something
state->currentslot = 0; //change back to slot 0
}
if (state->nxdn_cipher_type == 0x02 && state->keyloader == 1) if (state->nxdn_cipher_type == 0x02 && state->keyloader == 1)
{ {