Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial libaesdec commit
  • Loading branch information
spdfrk1 authored and perexg committed Aug 6, 2014
1 parent 37a1aab commit cb0c94d
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 8 deletions.
9 changes: 3 additions & 6 deletions Makefile
Expand Up @@ -292,12 +292,9 @@ FFDECSA-$(CONFIG_CWC) = yes
endif

ifeq ($(FFDECSA-yes),yes)
SRCS-yes += src/descrambler/ffdecsa/ffdecsa_interface.c \
src/descrambler/ffdecsa/ffdecsa_int.c
SRCS-${CONFIG_MMX} += src/descrambler/ffdecsa/ffdecsa_mmx.c
SRCS-${CONFIG_SSE2} += src/descrambler/ffdecsa/ffdecsa_sse2.c
${BUILDDIR}/src/descrambler/ffdecsa/ffdecsa_mmx.o : CFLAGS += -mmmx
${BUILDDIR}/src/descrambler/ffdecsa/ffdecsa_sse2.o : CFLAGS += -msse2
SRCS-yes += src/descrambler/libaesdec/libaesdec_interface.c \
src/descrambler/libaesdec/libaesdec.c
${BUILDDIR}/src/descrambler/libaesdec/libaesdec.o : CFLAGS += -msse2
endif

# File bundles
Expand Down
4 changes: 2 additions & 2 deletions src/descrambler/descrambler.c
Expand Up @@ -20,7 +20,7 @@
#include "descrambler.h"
#include "cwc.h"
#include "capmt.h"
#include "ffdecsa/FFdecsa.h"
#include "libaesdec/libaesdec.h"
#include "input.h"
#include "tvhcsa.h"

Expand Down Expand Up @@ -88,7 +88,7 @@ descrambler_init ( void )
capmt_init();
#endif
#if (ENABLE_CWC || ENABLE_CAPMT) && !ENABLE_DVBCSA
ffdecsa_init();
libaesdec_init();
#endif
}

Expand Down
100 changes: 100 additions & 0 deletions src/descrambler/libaesdec/libaesdec.c
@@ -0,0 +1,100 @@
/*
* libaesdec.c
*
* Created on: Jun 22, 2014
* Author: root
*/

#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "openssl/aes.h"

#include "libaesdec.h"

struct aes_keys_t {
AES_KEY even;
AES_KEY odd;
};

void aes_set_even_control_word(void *keys, const unsigned char *pk) {
//AES_set_decrypt_key(pk, 128, &((struct aes_keys_t *) keys)->even);
}

void aes_set_odd_control_word(void *keys, const unsigned char *pk) {
//AES_set_decrypt_key(pk, 128, &((struct aes_keys_t *) keys)->odd);
}

//-----set control words
void aes_set_control_words(void *keys, const unsigned char *ev,
const unsigned char *od) {
unsigned char key[16];
memcpy(key, ev, 8);
memcpy(key + 8, od, 8);
AES_set_decrypt_key(key, 128, &((struct aes_keys_t *) keys)->even);
}

//-----key structure

void *aes_get_key_struct(void) {
struct aes_keys_t *keys = (struct aes_keys_t *) malloc(
sizeof(struct aes_keys_t));
if (keys) {
static const unsigned char pk[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
aes_set_control_words(keys, pk, pk);
}
return keys;
}

void aes_free_key_struct(void *keys) {
return free(keys);
}

//----- decrypt

void aes_decrypt_packet(void *keys, unsigned char *packet) {
unsigned char *pkt;
int xc0, len, offset, n;

pkt = packet;
AES_KEY k;

// TODO check all flags
xc0 = pkt[3] & 0xc0;

if (xc0 == 0x00) {//skip clear pkt
return;
}
if (xc0 == 0x40) { //skip reserved pkt
return;
}

if (xc0 == 0x80 || xc0 == 0xc0) { // encrypted
pkt[3] &= 0x3f; // consider it decrypted now
if (pkt[3] & 0x20) { // incomplete packet
offset = 4 + pkt[4] + 1;
len = 188 - offset;
n = len >> 3;
//residue = len - (n << 3);
if (n == 0) { // decrypted==encrypted!
//DBG(fprintf(stderr,"DECRYPTED MINI!\n"));
return; // this doesn't need more processing
}
return; // TODO Handle incomplete packets?
}
}
else {
return;
}

k = ((struct aes_keys_t *) keys)->even;

// TODO room for improvement?
int i;
for (i = 4; i <= 164; i += 16) {
AES_ecb_encrypt(pkt + i, pkt + i, &k, AES_DECRYPT);
}
return;
}
40 changes: 40 additions & 0 deletions src/descrambler/libaesdec/libaesdec.h
@@ -0,0 +1,40 @@
/*
* libaesdec.h
*
* Created on: Jun 22, 2014
* Author: root
*/

#ifndef LIBAESDEC_H_
#define LIBAESDEC_H_

void libaesdec_init(void);
int get_internal_parallelism(void);
int get_suggested_cluster_size(void);
void * get_key_struct(void);
void free_key_struct(void *keys);
void set_even_control_word(void *keys, const unsigned char *even);
void set_odd_control_word(void *keys, const unsigned char *odd);
int decrypt_packets(void *keys, unsigned char **cluster);

// -- alloc & free the key structure
void *aes_get_key_struct(void);
void aes_free_key_struct(void *keys);

// -- set aes keys, 16 bytes each
void aes_set_control_words(void *keys, const unsigned char *even, const unsigned char *odd);

// -- set even aes key, 16 bytes
void aes_set_even_control_word(void *keys, const unsigned char *even);

// -- set odd aes key, 16 bytes
void aes_set_odd_control_word(void *keys, const unsigned char *odd);

// -- get aes keys, 16 bytes each
//void get_control_words(void *keys, unsigned char *even, unsigned char *odd);

// -- decrypt TS packet
void aes_decrypt_packet(void *keys, unsigned char *packet);


#endif /* LIBAESDEC_H_ */
51 changes: 51 additions & 0 deletions src/descrambler/libaesdec/libaesdec_interface.c
@@ -0,0 +1,51 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"
#include "tvheadend.h"
#include "libaesdec.h"


void libaesdec_init(void) {
tvhlog(LOG_INFO, "CSA", "Using AES descrambling");
}

int get_internal_parallelism(void) {
return 0;
}
int get_suggested_cluster_size(void) {
return 1;
}

void *
get_key_struct(void) {
return aes_get_key_struct();
}
void free_key_struct(void *keys) {
aes_free_key_struct(keys);
}

void set_even_control_word(void *keys, const unsigned char *even) {
aes_set_even_control_word(keys, even);
}

void set_odd_control_word(void *keys, const unsigned char *odd) {
aes_set_even_control_word(keys, odd);
}

int decrypt_packets(void *keys, unsigned char **cluster) {
aes_decrypt_packet(keys, cluster[0]);
return 1;
}

0 comments on commit cb0c94d

Please sign in to comment.