]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
aesdec/decdec: a little optimizations
authorJaroslav Kysela <perex@perex.cz>
Sun, 28 May 2017 06:54:53 +0000 (08:54 +0200)
committerJaroslav Kysela <perex@perex.cz>
Sun, 28 May 2017 13:15:48 +0000 (15:15 +0200)
src/descrambler/algo/libaesdec.c
src/descrambler/algo/libdesdec.c

index cab1d5f51e4be8919a92e453685865df0c9a308a..4aac4bc78c53eb4beb294504b483c51937f0702a 100644 (file)
@@ -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];
index 641d96d386202fb90e8e661c21c47e48d3800dd4..74880d60752709a98cdffec475a727ab6957645f 100644 (file)
@@ -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 {