From e36672f54cd998284cba873113c84600ca8e8f84 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Sat, 15 Mar 2025 20:26:25 -0400 Subject: [PATCH] Fix Some Tab Alignment Issues; NXDN: Make seperate AES IV expansion function; --- include/dsd.h | 3 ++- src/crypt-des.c | 62 +++++++++++++++++++++++----------------------- src/crypt-rc4.c | 58 +++++++++++++++++++++---------------------- src/dmr_pi.c | 30 +++++++++++----------- src/dsd_mbe.c | 4 +-- src/nxdn_deperm.c | 40 ++++++++++++++++++++++++++++++ src/nxdn_element.c | 9 ++----- 7 files changed, 121 insertions(+), 85 deletions(-) diff --git a/include/dsd.h b/include/dsd.h index 529c32f..9a75919 100644 --- a/include/dsd.h +++ b/include/dsd.h @@ -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_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 LFSR128n(dsd_state * state); void LFSR128d(dsd_state * state); diff --git a/src/crypt-des.c b/src/crypt-des.c index 44afae3..91d1559 100644 --- a/src/crypt-des.c +++ b/src/crypt-des.c @@ -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 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; - uint8_t iv[8]; memset (iv, 0, sizeof(iv)); - uint8_t key[8]; memset(key, 0, sizeof(key)); + int i = 0; + uint8_t iv[8]; memset (iv, 0, sizeof(iv)); + uint8_t key[8]; memset(key, 0, sizeof(key)); - //convert unsinged long long int values into array values - for (i = 7; i >= 0; i--) - iv[7-i] = (mi >> (8*i)) & 0xFF; + //convert unsinged long long int values into array values + for (i = 7; i >= 0; i--) + iv[7-i] = (mi >> (8*i)) & 0xFF; - for (i = 7; i >= 0; i--) - key[7-i] = (key_ulli >> (8*i)) & 0xFF; + for (i = 7; i >= 0; i--) + key[7-i] = (key_ulli >> (8*i)) & 0xFF; - //debug - // fprintf (stderr, "\n IV: "); + //debug + // fprintf (stderr, "\n IV: "); // for (i = 0; i < 8; i++) // fprintf (stderr, "%02X", iv[i]); - // fprintf (stderr, "\n Key: "); + // fprintf (stderr, "\n Key: "); // for (i = 0; i < 8; i++) // fprintf (stderr, "%02X", key[i]); - //types: 1 = DES56 OFB; 2 = DES56 CA (XL) - if (type == 2) - { - if (len == 0) - 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_ofb_keystream_output(key, iv, output, 1, len); + //types: 1 = DES56 OFB; 2 = DES56 CA (XL) + if (type == 2) + { + if (len == 0) + 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_ofb_keystream_output(key, iv, output, 1, len); } //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) { - int i = 0; - uint8_t iv[8]; memset (iv, 0, sizeof(iv)); + int i = 0; + uint8_t iv[8]; memset (iv, 0, sizeof(iv)); - //convert unsinged long long int values into array values - for (i = 7; i >= 0; i--) - iv[7-i] = (mi >> (8*i)) & 0xFF; + //convert unsinged long long int values into array values + for (i = 7; i >= 0; i--) + iv[7-i] = (mi >> (8*i)) & 0xFF; - //debug - // fprintf (stderr, "\n IV: "); + //debug + // fprintf (stderr, "\n IV: "); // for (i = 0; i < 8; i++) // fprintf (stderr, "%02X", iv[i]); - - //type 1 = TDEA TOFB //TODO: Add more types like 2DES? - if (type == 1) - 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); + + //type 1 = TDEA TOFB //TODO: Add more types like 2DES? + if (type == 1) + 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); } \ No newline at end of file diff --git a/src/crypt-rc4.c b/src/crypt-rc4.c index ca95944..6f482be 100644 --- a/src/crypt-rc4.c +++ b/src/crypt-rc4.c @@ -47,43 +47,43 @@ void rc4_voice_decrypt(int drop, uint8_t keylength, uint8_t messagelength, uint8 //this is for PDU usage void rc4_block_output (int drop, int keylen, int meslen, uint8_t * key, uint8_t * output_blocks) { - int i, j, x, count; - unsigned int keylength = (unsigned int)keylen; + int i, j, x, count; + unsigned int keylength = (unsigned int)keylen; unsigned int messagelength = (unsigned int)meslen; - unsigned int S[256]; + unsigned int S[256]; - for(i=0; i<256; i++) - S[i] = i; + for(i=0; i<256; i++) + S[i] = i; - j = 0; - for(i = 0; i<256; i++) - { - j = (j + S[i] + key[i % keylength]) % 256; - unsigned int temp = S[i]; - S[i] = S[j]; - S[j] = temp; - } + j = 0; + for(i = 0; i<256; i++) + { + j = (j + S[i] + key[i % keylength]) % 256; + unsigned int temp = S[i]; + S[i] = S[j]; + S[j] = temp; + } - //Generate Keystream - i = 0; - j = 0; + //Generate Keystream + i = 0; + j = 0; x = 0; - unsigned int byte; + unsigned int byte; - // fprintf (stderr, " Keystream Octets = "); - for(count = 0; count < (messagelength + drop); count++) - { - i = (i + 1) % 256; - j = (j + S[i]) % 256; - unsigned int temp = S[i]; - S[i] = S[j]; - S[j] = temp; - byte = S[(S[i] + S[j]) % 256]; + // fprintf (stderr, " Keystream Octets = "); + for(count = 0; count < (messagelength + drop); count++) + { + i = (i + 1) % 256; + j = (j + S[i]) % 256; + unsigned int temp = S[i]; + S[i] = S[j]; + S[j] = temp; + byte = S[(S[i] + S[j]) % 256]; - //Collect Output blocks - if (count >= drop) + //Collect Output blocks + if (count >= drop) output_blocks[x++] = byte; - } + } } \ No newline at end of file diff --git a/src/dmr_pi.c b/src/dmr_pi.c index 1238522..bf3dd08 100644 --- a/src/dmr_pi.c +++ b/src/dmr_pi.c @@ -186,48 +186,48 @@ void LFSR(dsd_state * state) //Expand a 32-bit MI into a 64-bit IV for DES void LFSR64(dsd_state * state) { - { + { unsigned long long int lfsr = 0; - if (state->currentslot == 0) - { - lfsr = (uint64_t) state->payload_mi; - } + if (state->currentslot == 0) + { + lfsr = (uint64_t) state->payload_mi; + } else lfsr = (uint64_t) state->payload_miR; uint8_t cnt = 0; 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; } - if (state->currentslot == 0) - { + if (state->currentslot == 0) + { fprintf (stderr, "%s", KYEL); 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, " MI(64): 0x%016llX", lfsr); 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->DMRvcL = 0; - } + } - if (state->currentslot == 1) - { + if (state->currentslot == 1) + { fprintf (stderr, "%s", KYEL); 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, " MI(64): 0x%016llX", lfsr); 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->DMRvcR = 0; - } + } - } + } } diff --git a/src/dsd_mbe.c b/src/dsd_mbe.c index 1edc5e9..0d8b8e3 100644 --- a/src/dsd_mbe.c +++ b/src/dsd_mbe.c @@ -516,12 +516,12 @@ processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char a { //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_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 state->bit_counterL = 0; diff --git a/src/nxdn_deperm.c b/src/nxdn_deperm.c index e3e6ff1..11d397a 100644 --- a/src/nxdn_deperm.c +++ b/src/nxdn_deperm.c @@ -1335,4 +1335,44 @@ uint8_t crc7_scch(uint8_t bits[], int len) s[6] = a; } 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); + } \ No newline at end of file diff --git a/src/nxdn_element.c b/src/nxdn_element.c index 4d8ed38..b77d10e 100644 --- a/src/nxdn_element.c +++ b/src/nxdn_element.c @@ -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) - { - 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 - } + LFSR128n(state); if (state->nxdn_cipher_type == 0x02 && state->keyloader == 1) {