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 #ifndef DVBCSA_PV_H_
25 # define DVBCSA_PV_H_
26
27 #include "config.h"
28
29 #if STDC_HEADERS
30 # include <stdlib.h>
31 # include <stddef.h>
32 #else
33 # if HAVE_STDLIB_H
34 # include <stdlib.h>
35 # endif
36 #endif
37 #if HAVE_STRING_H
38 # if !STDC_HEADERS && HAVE_MEMORY_H
39 # include <memory.h>
40 # endif
41 # include <string.h>
42 #endif
43 #if HAVE_STRINGS_H
44 # include <strings.h>
45 #endif
46 #if HAVE_INTTYPES_H
47 # include <inttypes.h>
48 #else
49 # if HAVE_STDINT_H
50 # include <stdint.h>
51 # endif
52 #endif
53
54 #if !defined(DVBCSA_DEBUG) && defined(__GNUC__)
55 #define DVBCSA_INLINE __attribute__ ((always_inline))
56 #else
57 #define DVBCSA_INLINE
58 #endif
59
60 void worddump (const char *str, const void *data, size_t len, size_t ws);
61
62 #define DVBCSA_DATA_SIZE 8
63 #define DVBCSA_KEYSBUFF_SIZE 56
64 #define DVBCSA_CWBITS_SIZE 64
65
66 typedef uint8_t dvbcsa_block_t[DVBCSA_DATA_SIZE];
67 typedef uint8_t dvbcsa_keys_t[DVBCSA_KEYSBUFF_SIZE];
68
69 struct dvbcsa_key_s
70 {
71 dvbcsa_cw_t cw;
72 dvbcsa_cw_t cws; /* nibble swapped CW */
73 dvbcsa_keys_t sch;
74 };
75
76 extern const uint8_t dvbcsa_block_sbox[256];
77
78 void dvbcsa_block_decrypt (const dvbcsa_keys_t key, const dvbcsa_block_t in, dvbcsa_block_t out);
79 void dvbcsa_block_encrypt (const dvbcsa_keys_t key, const dvbcsa_block_t in, dvbcsa_block_t out);
80
81 void dvbcsa_stream_xor (const dvbcsa_cw_t cw, const dvbcsa_block_t iv,
82 uint8_t *stream, unsigned int len);
83
84 void dvbcsa_key_schedule_block(const dvbcsa_cw_t cw, uint8_t * kk);
85
86 DVBCSA_INLINE static inline void
dvbcsa_xor_64(uint8_t * b,const uint8_t * a)87 dvbcsa_xor_64 (uint8_t *b, const uint8_t *a)
88 {
89 #if defined(__i386__) || defined(__x86_64__)
90 /* target support non aligned memory access */
91 *(uint64_t*)b ^= *(uint64_t*)a;
92 #else
93 unsigned int i;
94
95 for (i = 0; i < 8; i++)
96 b[i] ^= a[i];
97 #endif
98 }
99
100 DVBCSA_INLINE static inline uint32_t
dvbcsa_load_le32(const uint8_t * p)101 dvbcsa_load_le32(const uint8_t *p)
102 {
103 #if defined(__i386__) || defined(__x86_64__)
104 /* target support non aligned le memory access */
105 return *(uint32_t*)p;
106 #else
107 return ((uint32_t)p[3] << 24) |
108 ((uint32_t)p[2] << 16) |
109 ((uint32_t)p[1] << 8 ) |
110 (uint32_t)p[0];
111 #endif
112 }
113
114 DVBCSA_INLINE static inline uint64_t
dvbcsa_load_le64(const uint8_t * p)115 dvbcsa_load_le64(const uint8_t *p)
116 {
117 #if defined(__i386__) || defined(__x86_64__)
118 /* target support non aligned le memory access */
119 return *(uint64_t*)p;
120 #else
121 return (uint64_t)( ((uint64_t)p[7] << 56) |
122 ((uint64_t)p[6] << 48) |
123 ((uint64_t)p[5] << 40) |
124 ((uint64_t)p[4] << 32) |
125 ((uint64_t)p[3] << 24) |
126 ((uint64_t)p[2] << 16) |
127 ((uint64_t)p[1] << 8 ) |
128 (uint64_t)p[0]
129 );
130 #endif
131 }
132
133 DVBCSA_INLINE static inline void
dvbcsa_store_le32(uint8_t * p,const uint32_t w)134 dvbcsa_store_le32(uint8_t *p, const uint32_t w)
135 {
136 #if defined(__i386__) || defined(__x86_64__)
137 /* target support non aligned le memory access */
138 *(uint32_t*)p = w;
139 #else
140 p[3] = (w >> 24);
141 p[2] = (w >> 16);
142 p[1] = (w >> 8);
143 p[0] = (w);
144 #endif
145 }
146
147 DVBCSA_INLINE static inline void
dvbcsa_store_le64(uint8_t * p,const uint64_t w)148 dvbcsa_store_le64(uint8_t *p, const uint64_t w)
149 {
150 #if defined(__i386__) || defined(__x86_64__)
151 /* target support non aligned le memory access */
152 *(uint64_t*)p = w;
153 #else
154 p[7] = (w >> 56);
155 p[6] = (w >> 48);
156 p[5] = (w >> 40);
157 p[4] = (w >> 32);
158 p[3] = (w >> 24);
159 p[2] = (w >> 16);
160 p[1] = (w >> 8);
161 p[0] = (w);
162 #endif
163 }
164
165
166 #endif
167
168