From 0de60d592dfb51be8ef05aacc14b90fe3bc0e0f8 Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Sun, 28 May 2017 08:54:53 +0200 Subject: [PATCH] aesdec/decdec: a little optimizations --- src/descrambler/algo/libaesdec.c | 33 +++++++++------------------ src/descrambler/algo/libdesdec.c | 39 ++++++++++++-------------------- 2 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/descrambler/algo/libaesdec.c b/src/descrambler/algo/libaesdec.c index cab1d5f51..4aac4bc78 100644 --- a/src/descrambler/algo/libaesdec.c +++ b/src/descrambler/algo/libaesdec.c @@ -62,34 +62,23 @@ void aes_free_priv_struct(void *keys) /* decrypt */ void aes_decrypt_packet(void *keys, const uint8_t *pkt) { - uint8_t ev_od = 0; - uint_fast8_t xc0, offset, n; + uint_fast8_t ev_od = 0; + uint_fast8_t xc0, offset; AES_KEY *k; - xc0 = pkt[3] & 0xc0; - - //skip clear pkt - if (xc0 == 0x00) - return; - - //skip reserved pkt - if (xc0 == 0x40) + // skip reserved and not encrypted pkt + if (((xc0 = pkt[3]) & 0x80) == 0) return; - if (xc0 == 0x80 || xc0 == 0xc0) { // encrypted - ev_od = (xc0 & 0x40) >> 6; // 0 even, 1 odd - ((uint8_t *)pkt)[3] &= 0x3f; // consider it decrypted now - if (pkt[3] & 0x20) { // incomplete packet - offset = 4 + pkt[4] + 1; - n = (188 - offset) >> 4; - if (n == 0) { // decrypted==encrypted! - return; // this doesn't need more processing - } - } else { - offset = 4; + ev_od = (xc0 & 0x40) >> 6; // 0 even, 1 odd + ((uint8_t *)pkt)[3] = xc0 & 0x3f; // consider it decrypted now + if (xc0 & 0x20) { // incomplete packet + offset = 4 + pkt[4] + 1; + if (offset + 16 > 188) { // decrypted==encrypted! + return; // this doesn't need more processing } } else { - return; + offset = 4; } k = &((aes_priv_t *) keys)->keys[ev_od]; diff --git a/src/descrambler/algo/libdesdec.c b/src/descrambler/algo/libdesdec.c index 641d96d38..74880d607 100644 --- a/src/descrambler/algo/libdesdec.c +++ b/src/descrambler/algo/libdesdec.c @@ -59,44 +59,33 @@ void des_free_priv_struct(void *priv) /* decrypt */ void des_decrypt_packet(void *priv, const uint8_t *pkt) { - uint8_t ev_od = 0; - uint_fast8_t xc0, offset, offset2, n; + uint_fast8_t ev_od = 0; + uint_fast8_t xc0, offset, offset2, offset3; DES_key_schedule *sched; uint8_t buf[188]; - xc0 = pkt[3] & 0xc0; - - //skip clear pkt - if (xc0 == 0x00) - return; - - //skip reserved pkt - if (xc0 == 0x40) + // skip reserved and not encrypted pkt + if (((xc0 = pkt[3]) & 0x80) == 0) return; - if (xc0 == 0x80 || xc0 == 0xc0) { // encrypted - ev_od = (xc0 & 0x40) >> 6; // 0 even, 1 odd - ((uint8_t *)pkt)[3] &= 0x3f; // consider it decrypted now - if (pkt[3] & 0x20) { // incomplete packet - offset = 4 + pkt[4] + 1; - n = (188 - offset) >> 4; - if (n == 0) { // decrypted==encrypted! - return; // this doesn't need more processing - } - } else { - offset = 4; + ev_od = (xc0 & 0x40) >> 6; // 0 even, 1 odd + ((uint8_t *)pkt)[3] = xc0 & 0x3f; // consider it decrypted now + if (xc0 & 0x20) { // incomplete packet + offset = 4 + pkt[4] + 1; + if (offset + 8 > 188) { // decrypted==encrypted! + return; // this doesn't need more processing } } else { - return; + offset = 4; } sched = &((des_priv_t *)priv)->sched[ev_od]; if (offset & 3) { - /* data must be aligned for DES_encrypt2() */ + /* data must be aligned for DES_encrypt1() */ offset2 = (offset + 3) & ~3; memcpy(buf + offset2, pkt + offset, 188 - offset2); - for (; offset2 <= (188 - 8); offset2 += 8) { - DES_encrypt1((DES_LONG *)(buf + offset2), sched, 0); + for (offset3 = offset2; offset3 <= (188 - 8); offset3 += 8) { + DES_encrypt1((DES_LONG *)(buf + offset3), sched, 0); } memcpy((uint8_t *)(pkt + offset), buf + offset2, 188 - offset2); } else { -- 2.47.3