From dc4b76313e9166f502b9e471e5cb37c961b0545c Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Wed, 24 May 2017 14:45:50 +0200 Subject: [PATCH] libaesdec: code cleanups --- src/descrambler/algo/libaesdec.c | 79 ++++++++++++++++---------------- src/descrambler/algo/libaesdec.h | 17 +++---- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/descrambler/algo/libaesdec.c b/src/descrambler/algo/libaesdec.c index 1d4a5c1c8..3a41a827c 100644 --- a/src/descrambler/algo/libaesdec.c +++ b/src/descrambler/algo/libaesdec.c @@ -14,53 +14,57 @@ #include "libaesdec.h" -//-----key structure -struct aes_keys_t { - AES_KEY even; - AES_KEY odd; -}; - -//-----even cw represents one full 128-bit AES key -void aes_set_even_control_word(void *keys, const unsigned char *pk) { - AES_set_decrypt_key(pk, 128, &((struct aes_keys_t *) keys)->even); +/* key structure */ +typedef struct aes_priv { + AES_KEY keys[2]; /* 0 = even, 1 = odd */ +} aes_priv_t; + +/* even cw represents one full 128-bit AES key */ +void aes_set_even_control_word(void *keys, const uint8_t *pk) +{ + AES_set_decrypt_key(pk, 128, &((aes_priv_t *) keys)->keys[0]); } -//-----odd cw represents one full 128-bit AES key -void aes_set_odd_control_word(void *keys, const unsigned char *pk) { - AES_set_decrypt_key(pk, 128, &((struct aes_keys_t *) keys)->odd); +/* odd cw represents one full 128-bit AES key */ +void aes_set_odd_control_word(void *keys, const uint8_t *pk) +{ + AES_set_decrypt_key(pk, 128, &((aes_priv_t *) keys)->keys[1]); } -//-----set control words -void aes_set_control_words(void *keys, const unsigned char *ev, - const unsigned char *od) { - AES_set_decrypt_key(ev, 128, &((struct aes_keys_t *) keys)->even); - AES_set_decrypt_key(od, 128, &((struct aes_keys_t *) keys)->odd); +/* set control words */ +void aes_set_control_words(void *keys, + const uint8_t *ev, + const uint8_t *od) +{ + AES_set_decrypt_key(ev, 128, &((aes_priv_t *) keys)->keys[0]); + AES_set_decrypt_key(od, 128, &((aes_priv_t *) keys)->keys[1]); } -//-----allocate key structure -void * aes_get_key_struct(void) { - struct aes_keys_t *keys = (struct aes_keys_t *) malloc( - sizeof(struct aes_keys_t)); +/* allocate key structure */ +void * aes_get_key_struct(void) +{ + aes_priv_t *keys; + + keys = (aes_priv_t *) malloc(sizeof(aes_priv_t)); if (keys) { - static const unsigned char pk[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + static const uint8_t pk[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; aes_set_control_words(keys, pk, pk); } return keys; } -//-----free key structure -void aes_free_key_struct(void *keys) { - if (keys) - free(keys); +/* free key structure */ +void aes_free_key_struct(void *keys) +{ + free(keys); } -//----- decrypt -void aes_decrypt_packet(void *keys, unsigned char *packet) { - unsigned char *pkt; - unsigned char ev_od = 0; - int xc0, offset, n; - pkt = packet; - AES_KEY k; +/* decrypt */ +void aes_decrypt_packet(void *keys, uint8_t *pkt) +{ + uint8_t ev_od = 0; + uint_fast8_t xc0, offset, n; + AES_KEY *k; xc0 = pkt[3] & 0xc0; @@ -88,13 +92,8 @@ void aes_decrypt_packet(void *keys, unsigned char *packet) { return; } - if (ev_od == 0) { - k = ((struct aes_keys_t *) keys)->even; - } else { - k = ((struct aes_keys_t *) keys)->odd; - } - + k = &((aes_priv_t *) keys)->keys[ev_od]; for (; offset <= (188 - 16); offset += 16) { - AES_ecb_encrypt(pkt + offset, pkt + offset, &k, AES_DECRYPT); + AES_ecb_encrypt(pkt + offset, pkt + offset, k, AES_DECRYPT); } } diff --git a/src/descrambler/algo/libaesdec.h b/src/descrambler/algo/libaesdec.h index 912670f38..1577ed298 100644 --- a/src/descrambler/algo/libaesdec.h +++ b/src/descrambler/algo/libaesdec.h @@ -8,26 +8,27 @@ #ifndef LIBAESDEC_H_ #define LIBAESDEC_H_ +#include #include "build.h" #if ENABLE_SSL void *aes_get_key_struct(void); void aes_free_key_struct(void *keys); -void aes_set_control_words(void *keys, const unsigned char *even, const unsigned char *odd); -void aes_set_even_control_word(void *keys, const unsigned char *even); -void aes_set_odd_control_word(void *keys, const unsigned char *odd); -void aes_decrypt_packet(void *keys, unsigned char *packet); +void aes_set_control_words(void *keys, const uint8_t *even, const uint8_t *odd); +void aes_set_even_control_word(void *keys, const uint8_t *even); +void aes_set_odd_control_word(void *keys, const uint8_t *odd); +void aes_decrypt_packet(void *keys, uint8_t *pkt); #else // empty functions static inline void *aes_get_key_struct(void) { return 0; }; static inline void aes_free_key_struct(void *keys) { return; }; -static inline void aes_set_control_words(void *keys, const unsigned char *even, const unsigned char *odd) { return; }; -static inline void aes_set_even_control_word(void *keys, const unsigned char *even) { return; }; -static inline void aes_set_odd_control_word(void *keys, const unsigned char *odd) { return; }; -static inline void aes_decrypt_packet(void *keys, unsigned char *packet) { return; }; +static inline void aes_set_control_words(void *keys, const uint8_t *even, const uint8_t *odd) { return; }; +static inline void aes_set_even_control_word(void *keys, const uint8_t *even) { return; }; +static inline void aes_set_odd_control_word(void *keys, const uint8_t *odd) { return; }; +static inline void aes_decrypt_packet(void *keys, uint8_t *pkt) { return; }; #endif -- 2.47.3