1 /*
2    - * Copyright (C) 2014-2020 Catalin Toda <catalinii@yahoo.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  */
20 
21 #include "adapter.h"
22 #include "dvb.h"
23 #include "minisatip.h"
24 #include "pmt.h"
25 #include "socketworks.h"
26 #include "tables.h"
27 #include "utils.h"
28 #include <arpa/inet.h>
29 #include <ctype.h>
30 #include <dvbcsa/dvbcsa.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <math.h>
34 #include <net/if.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <time.h>
46 #include <unistd.h>
47 
48 #define DEFAULT_LOG LOG_DVBCA
49 
dvbcsa_create_key(SCW * cw)50 void dvbcsa_create_key(SCW *cw) { cw->key = dvbcsa_bs_key_alloc(); }
51 
dvbcsa_delete_key(SCW * cw)52 void dvbcsa_delete_key(SCW *cw) { dvbcsa_key_free(cw->key); }
53 
dvbcsa_set_cw(SCW * cw,SPMT * pmt)54 void dvbcsa_set_cw(SCW *cw, SPMT *pmt) {
55     dvbcsa_bs_key_set((unsigned char *)cw->cw, cw->key);
56 }
57 
copy_batch(struct dvbcsa_bs_batch_s * d,SPMT_batch * s,int len)58 void copy_batch(struct dvbcsa_bs_batch_s *d, SPMT_batch *s, int len) {
59     int i = 0;
60     for (i = 0; i < len; i++) {
61         d[i].data = s[i].data;
62         d[i].len = s[i].len;
63     }
64     d[i].data = 0;
65     d[i].len = 0;
66 }
67 
dvbcsa_decrypt_stream(SCW * cw,SPMT_batch * batch,int batch_len)68 void dvbcsa_decrypt_stream(SCW *cw, SPMT_batch *batch, int batch_len) {
69     int batch_size = dvbcsa_bs_batch_size();
70     struct dvbcsa_bs_batch_s b[batch_size + 1];
71     int i;
72     for (i = 0; i < batch_len; i += batch_size) {
73         int len = batch_len - i;
74         if (len > batch_size)
75             len = batch_size;
76         copy_batch(b, batch + i, len);
77         dvbcsa_bs_decrypt((const struct dvbcsa_bs_key_s *)cw->key,
78                           (const struct dvbcsa_bs_batch_s *)b, 184);
79     }
80 }
81 
82 SCW_op csa_op = {.algo = CA_ALGO_DVBCSA,
83                  .create_cw = (Create_CW)dvbcsa_create_key,
84                  .delete_cw = (Delete_CW)dvbcsa_delete_key,
85                  .set_cw = (Set_CW)dvbcsa_set_cw,
86                  .stop_cw = NULL,
87                  .decrypt_stream = (Decrypt_Stream)dvbcsa_decrypt_stream};
88 
init_algo_csa()89 void init_algo_csa() { register_algo(&csa_op); }
90