Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial libaesdec commit
- Loading branch information
Showing
5 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |