-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmsg-cccam.c
265 lines (225 loc) · 7.09 KB
/
msg-cccam.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#if 0
#
# Copyright (c) 2014 - 2015 Javier Sayago <admin@lonasdigital.com>
# Contact: javilonas@esp-desarrolladores.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
#include "cscrypt/md5.h"
#include "cscrypt/sha1.h"
#include "common.h"
#include "debug.h"
#include "msg-cccam.h"
#include "sockets.h"
///////////////////////////////////////////////////////////////////////////////
void cc_crypt_swap(unsigned char *p1, unsigned char *p2)
{
unsigned char tmp=*p1; *p1=*p2; *p2=tmp;
}
///////////////////////////////////////////////////////////////////////////////
void cc_crypt_init( struct cc_crypt_block *block, uint8 *key, int32_t len)
{
int32_t i = 0 ;
uint8 j = 0;
for (i=0; i<256; i++) {
block->keytable[i] = i;
}
for (i=0; i<256; i++) {
j += key[i % len] + block->keytable[i];
cc_crypt_swap(&block->keytable[i], &block->keytable[j]);
}
block->state = *key;
block->counter=0;
block->sum=0;
}
///////////////////////////////////////////////////////////////////////////////
// XOR init bytes with 'CCcam'
void cc_crypt_xor(uint8 *buf)
{
const char cccam[] = "CCcam";
uint8 i;
for ( i = 0; i < 8; i++ ) {
buf[8 + i] = i * buf[i];
if ( i <= 5 ) {
buf[i] ^= cccam[i];
}
}
}
///////////////////////////////////////////////////////////////////////////////
void cc_decrypt(struct cc_crypt_block *block, uint8 *data, int32_t len)
{
int32_t i;
uint8 z;
for (i = 0; i < len; i++) {
block->counter++;
block->sum += block->keytable[block->counter];
cc_crypt_swap(&block->keytable[block->counter], &block->keytable[block->sum]);
z = data[i];
data[i] = z ^ block->keytable[(block->keytable[block->counter] + block->keytable[block->sum]) & 0xff] ^ block->state;
z = data[i];
block->state = block->state ^ z;
}
}
///////////////////////////////////////////////////////////////////////////////
void cc_encrypt(struct cc_crypt_block *block, uint8 *data, int32_t len)
{
int32_t i;
uint8 z;
// There is a side-effect in this function:
// If in & out pointer are the same, then state is xor'ed with modified input
// (because output(=in ptr) is written before state xor)
// This side-effect is used when initialising the encrypt state!
for (i = 0; i < len; i++) {
block->counter++;
block->sum += block->keytable[block->counter];
cc_crypt_swap(&block->keytable[block->counter], &block->keytable[block->sum]);
z = data[i];
data[i] = z ^ block->keytable[(block->keytable[block->counter] + block->keytable[block->sum]) & 0xff] ^ block->state;
block->state = block->state ^ z;
}
}
///////////////////////////////////////////////////////////////////////////////
// node_id : client nodeid, the sender of the ECM Request(big endian)
// card_id : local card_id for the server
void cc_crypt_cw(uint8 *nodeid/*client node id*/, uint32 card_id, uint8 *cws)
{
uint8 tmp;
int32_t i,n;
uint8 nod[8];
for(i=0; i<8; i++) nod[i] = nodeid[7-i];
for (i = 0; i < 16; i++) {
if (i&1)
if (i!=15) n = (nod[i>>1]>>4) | (nod[(i>>1)+1]<<4); else n = nod[i>>1]>>4;
else n = nod[i>>1];
n = n & 0xff;
tmp = cws[i] ^ n;
if (i & 1) tmp = ~tmp;
cws[i] = (card_id >> (2 * i)) ^ tmp;
//printf("(%d) n=%02x, tmp=%02x, cw=%02x\n",i,n,tmp,cws[i]);
}
}
///////////////////////////////////////////////////////////////////////////////
// Return:
// =0: Disconnected
// -1: Packet Error
// >0: Success
int32_t cc_msg_recv(int32_t handle,struct cc_crypt_block *recvblock, uint8 *buf, int32_t timeout)
{
int32_t len,msglen;
uint8 netbuf[CC_MAXMSGSIZE];
if (handle < 0) return -1;
len = recv_nonb(handle, netbuf, 4,timeout);
if (len<=0) {
debugf(" CCcam: recv error %d(%d)\n",len, errno);
return len;
}
if (len != 4) { // invalid header length read
debugf(" CCcam: invalid header length\n");
//debugdump(netbuf, len, "Header:");
return -1;
}
cc_decrypt(recvblock, netbuf, 4);
//debugdump(netbuf, 4, "CCcam: decrypted header:");
if (((netbuf[2] << 8) | netbuf[3]) != 0) { // check if any data is expected in msg
if (((netbuf[2] << 8) | netbuf[3]) > CC_MAXMSGSIZE - 2) {
debugf(" CCcam: message too big\n");
return -1;
}
len = recv_nonb(handle, netbuf+4, (netbuf[2] << 8) | netbuf[3],timeout);
if (len != ((netbuf[2] << 8) | netbuf[3])) {
debugf(" CCcam: invalid message length read %d(%d)\n",len,errno);
return -1;
}
cc_decrypt(recvblock, netbuf+4, len);
len += 4;
}
//debugdump(netbuf, len, "CCcam: Reveive Data");
if (flag_debugnet) {
debugf(" CCcam: receive data %d\n",len);
debughex(netbuf,len);
}
memcpy(buf, netbuf, len);
return len;
}
// -1: not yet
// 0: disconnect
// >0: ok
int32_t cc_msg_chkrecv(int32_t handle,struct cc_crypt_block *recvblock)
{
int32_t len;
uint8 netbuf[CC_MAXMSGSIZE];
struct cc_crypt_block block;
if (handle<=0) return -1;
//len = recv(handle, netbuf, 4, 0);
len = recv(handle, netbuf, 4, MSG_PEEK|MSG_NOSIGNAL|MSG_DONTWAIT);
if (len==0) return 0;
if (len!=4) return -1;
memcpy( &block, recvblock, sizeof(struct cc_crypt_block));
cc_decrypt(&block, netbuf, 4);
int32_t datasize = (netbuf[2] << 8) | netbuf[3];
if ( datasize!=0 ) { // check if any data is expected in msg
if ( datasize > CC_MAXMSGSIZE - 2) return 0; // Disconnect
len = recv(handle, netbuf, 4+datasize, MSG_PEEK|MSG_NOSIGNAL|MSG_DONTWAIT);
if (len==0) return 0;
if (len != 4+datasize) return -1;
cc_decrypt(&block, netbuf+4, len-4);
}
//debugf("CCcam: Check Reveive Data %d\n",datasize+4);
return len;
}
///////////////////////////////////////////////////////////////////////////////
// Return:
// =0: Disconnected
// -1: Packet Error
// >0: Success
int32_t cc_msg_recv_nohead(int32_t handle, struct cc_crypt_block *recvblock, uint8 *buf, int32_t len)
{
if (handle < 0) return -1;
len = recv_nonb(handle, buf, len, 2000); // read rest of msg
cc_decrypt(recvblock, buf, len);
return len;
}
///////////////////////////////////////////////////////////////////////////////
int32_t cc_msg_send(int32_t handle,struct cc_crypt_block *sendblock, cc_msg_cmd cmd, int32_t len, uint8_t *buf)
{
int32_t n;
uint8_t netbuf[CC_MAXMSGSIZE];
memset(netbuf, 0, len+4);
if (cmd == CC_MSG_NO_HEADER) memcpy(netbuf, buf, len);
else {
// build command message
netbuf[0] = 0; // flags??
netbuf[1] = cmd & 0xff;
netbuf[2] = len >> 8;
netbuf[3] = len & 0xff;
if (buf) memcpy(netbuf+4, buf, len);
len += 4;
}
//debugdump(netbuf, len, "CCcam: Send data");
if (flag_debugnet) {
debugf(" CCcam: send data %d\n",len);
debughex(netbuf,len);
}
cc_encrypt(sendblock, netbuf, len);
n = send_nonb(handle, netbuf, len, 100);
return n;
}