#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;
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);
}
}
#ifndef LIBAESDEC_H_
#define LIBAESDEC_H_
+#include <stdint.h>
#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