1 /*
2 
3     This file is part of libdvbcsa.
4 
5     libdvbcsa is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published
7     by the Free Software Foundation; either version 2 of the License,
8     or (at your option) any later version.
9 
10     libdvbcsa is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with libdvbcsa; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18     02111-1307 USA
19 
20     (c) 2006-2008 Alexandre Becoulet <alexandre.becoulet@free.fr>
21 
22 */
23 
24 #include "dvbcsa/dvbcsa.h"
25 #include "dvbcsa_pv.h"
26 
dvbcsa_decrypt(const struct dvbcsa_key_s * key,uint8_t * data,unsigned int len)27 void dvbcsa_decrypt (const struct dvbcsa_key_s *key, uint8_t *data, unsigned int len)
28 {
29   unsigned int	alen = len & (unsigned)~0x7;
30   int		i;
31 
32   if (len < 8)
33     return;
34 
35 #ifndef DVBCSA_DISABLE_STREAM
36   dvbcsa_stream_xor(key->cws, data, data + 8, len - 8);
37 #endif
38 
39 #ifndef DVBCSA_DISABLE_BLOCK
40   dvbcsa_block_decrypt(key->sch, data, data);
41 
42   for (i = 8; i < alen; i += 8)
43     {
44       dvbcsa_xor_64(data + i - 8, data + i);
45       dvbcsa_block_decrypt(key->sch, data + i, data + i);
46     }
47 #endif
48 }
49 
dvbcsa_encrypt(const struct dvbcsa_key_s * key,uint8_t * data,unsigned int len)50 void dvbcsa_encrypt (const struct dvbcsa_key_s *key, uint8_t *data, unsigned int len)
51 {
52   unsigned int	alen = len & (unsigned)~0x7;
53   int		i;
54 
55   if (len < 8)
56     return;
57 
58 #ifndef DVBCSA_DISABLE_BLOCK
59   dvbcsa_block_encrypt(key->sch, data + alen - 8, data + alen - 8);
60 
61   for (i = alen - 16; i >= 0; i -= 8)
62     {
63       dvbcsa_xor_64(data + i, data + i + 8);
64       dvbcsa_block_encrypt(key->sch, data + i, data + i);
65     }
66 #endif
67 
68 #ifndef DVBCSA_DISABLE_STREAM
69   dvbcsa_stream_xor(key->cws, data, data + 8, len - 8);
70 #endif
71 }
72 
dvbcsa_key_alloc()73 struct dvbcsa_key_s * dvbcsa_key_alloc()
74 {
75   return malloc(sizeof (struct dvbcsa_key_s));
76 }
77 
dvbcsa_key_free(struct dvbcsa_key_s * key)78 void dvbcsa_key_free(struct dvbcsa_key_s *key)
79 {
80   free(key);
81 }
82 
83