forked from nx111/oscam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-emulator-nagravision.c
294 lines (261 loc) · 5.45 KB
/
module-emulator-nagravision.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#define MODULE_LOG_PREFIX "emu"
#include "globals.h"
#ifdef WITH_EMU
#include "cscrypt/bn.h"
#include "cscrypt/des.h"
#include "cscrypt/idea.h"
#include "module-emulator-osemu.h"
static void ReverseMem(uint8_t *in, int32_t len)
{
uint8_t temp;
int32_t i;
for(i = 0; i < (len / 2); i++) {
temp = in[i];
in[i] = in[len - i - 1];
in[len - i - 1] = temp;
}
}
static void ReverseMemInOut(uint8_t *out, const uint8_t *in, int32_t n)
{
if(n>0) {
out+=n;
do {
*(--out)=*(in++);
}
while(--n);
}
}
static int8_t EmuRSAInput(BIGNUM *d, const uint8_t *in, int32_t n, int8_t le)
{
int8_t result = 0;
if(le) {
uint8_t *tmp = (uint8_t *)malloc(sizeof(uint8_t)*n);
if(tmp == NULL) {
return 0;
}
ReverseMemInOut(tmp,in,n);
result = BN_bin2bn(tmp,n,d)!=0;
free(tmp);
}
else {
result = BN_bin2bn(in,n,d)!=0;
}
return result;
}
static int32_t EmuRSAOutput(uint8_t *out, int32_t n, BIGNUM *r, int8_t le)
{
int32_t s = BN_num_bytes(r);
if(s>n) {
uint8_t *buff = (uint8_t *)malloc(sizeof(uint8_t)*s);
if(buff == NULL) {
return 0;
}
BN_bn2bin(r,buff);
memcpy(out,buff+s-n,n);
free(buff);
}
else if(s<n) {
int32_t l=n-s;
memset(out,0,l);
BN_bn2bin(r,out+l);
}
else {
BN_bn2bin(r,out);
}
if(le) {
ReverseMem(out,n);
}
return s;
}
static int32_t EmuRSA(uint8_t *out, const uint8_t *in, int32_t n, BIGNUM *exp, BIGNUM *mod, int8_t le)
{
BN_CTX *ctx;
BIGNUM *r, *d;
int32_t result = 0;
ctx = BN_CTX_new();
r = BN_new();
d = BN_new();
if(EmuRSAInput(d,in,n,le) && BN_mod_exp(r,d,exp,mod,ctx)) {
result = EmuRSAOutput(out,n,r,le);
}
BN_free(d);
BN_free(r);
BN_CTX_free(ctx);
return result;
}
// Nagra EMU
static int8_t GetNagraKey(uint8_t *buf, uint32_t ident, char keyName, uint32_t keyIndex, uint8_t isCriticalKey)
{
char keyStr[EMU_MAX_CHAR_KEYNAME];
snprintf(keyStr, EMU_MAX_CHAR_KEYNAME, "%c%X", keyName, keyIndex);
if(FindKey('N', ident, 0, keyStr, buf, keyName == 'M' ? 64 : 16, isCriticalKey, 0, 0, NULL)) {
return 1;
}
return 0;
}
static int8_t Nagra2Signature(const uint8_t *vkey, const uint8_t *sig, const uint8_t *msg, int32_t len)
{
uint8_t buff[16];
uint8_t iv[8];
int32_t i,j;
memcpy(buff,vkey,sizeof(buff));
for(i=0; i+7<len; i+=8) {
IDEA_KEY_SCHEDULE ek;
idea_set_encrypt_key(buff, &ek);
memcpy(buff,buff+8,8);
memset(iv,0,sizeof(iv));
idea_cbc_encrypt(msg+i,buff+8,8,&ek,iv,IDEA_ENCRYPT);
for(j=7; j>=0; j--) {
buff[j+8]^=msg[i+j];
}
}
buff[8]&=0x7F;
return (memcmp(sig, buff+8, 8) == 0);
}
static int8_t DecryptNagra2ECM(uint8_t *in, uint8_t *out, const uint8_t *key, int32_t len, const uint8_t *vkey, uint8_t *keyM)
{
BIGNUM *exp, *mod;
uint8_t iv[8];
int32_t i = 0, sign = in[0] & 0x80;
uint8_t binExp = 3;
int8_t result = 1;
exp = BN_new();
mod = BN_new();
BN_bin2bn(&binExp, 1, exp);
BN_bin2bn(keyM, 64, mod);
if(EmuRSA(out,in+1,64,exp,mod,1)<=0) {
BN_free(exp);
BN_free(mod);
return 0;
}
out[63]|=sign;
if(len>64) {
memcpy(out+64,in+65,len-64);
}
memset(iv,0,sizeof(iv));
if(in[0]&0x04) {
uint8_t key1[8], key2[8];
ReverseMemInOut(key1,&key[0],8);
ReverseMemInOut(key2,&key[8],8);
for(i=7; i>=0; i--) {
ReverseMem(out+8*i,8);
}
des_ede2_cbc_decrypt(out, iv, key1, key2, len);
for(i=7; i>=0; i--) {
ReverseMem(out+8*i,8);
}
}
else {
IDEA_KEY_SCHEDULE ek;
idea_set_encrypt_key(key, &ek);
idea_cbc_encrypt(out, out, len&~7, &ek, iv, IDEA_DECRYPT);
}
ReverseMem(out,64);
if(result && EmuRSA(out,out,64,exp,mod,0)<=0) {
result = 0;
}
if(result && vkey && !Nagra2Signature(vkey,out,out+8,len-8)) {
result = 0;
}
BN_free(exp);
BN_free(mod);
return result;
}
int8_t Nagra2ECM(uint8_t *ecm, uint8_t *dw)
{
uint32_t ident, identMask, tmp1, tmp2, tmp3;
uint8_t cmdLen, ideaKeyNr, *dec, ideaKey[16], vKey[16], m1Key[64], mecmAlgo = 0;
int8_t useVerifyKey = 0;
int32_t l=0, s;
uint16_t i = 0, ecmLen = GetEcmLen(ecm);
if(ecmLen < 8) {
return 1;
}
cmdLen = ecm[4] - 5;
ident = (ecm[5] << 8) + ecm[6];
ideaKeyNr = (ecm[7]&0x10)>>4;
if(ideaKeyNr) {
ideaKeyNr = 1;
}
if(ident == 1283 || ident == 1285 || ident == 1297) {
ident = 1281;
}
if(cmdLen <= 63 || ecmLen < cmdLen + 10) {
return 1;
}
if(!GetNagraKey(ideaKey, ident, '0', ideaKeyNr, 1)) {
return 2;
}
if(GetNagraKey(vKey, ident, 'V', 0, 0)) {
useVerifyKey = 1;
}
if(!GetNagraKey(m1Key, ident, 'M', 1, 1)) {
return 2;
}
ReverseMem(m1Key, 64);
dec = (uint8_t*)malloc(sizeof(uint8_t)*cmdLen);
if(dec == NULL) {
return 7;
}
if(!DecryptNagra2ECM(ecm+9, dec, ideaKey, cmdLen, useVerifyKey?vKey:0, m1Key)) {
free(dec);
return 1;
}
for(i=(dec[14]&0x10)?16:20; i<cmdLen && l!=3; ) {
switch(dec[i]) {
case 0x10:
case 0x11:
if(i+10 < cmdLen && dec[i+1] == 0x09) {
s = (~dec[i])&1;
mecmAlgo = dec[i+2]&0x60;
memcpy(dw+(s<<3), &dec[i+3], 8);
i+=11;
l|=(s+1);
}
else {
i++;
}
break;
case 0x00:
i+=2;
break;
case 0x30:
case 0x31:
case 0x32:
case 0x33:
case 0x34:
case 0x35:
case 0x36:
case 0xB0:
if(i+1 < cmdLen) {
i+=dec[i+1]+2;
}
else {
i++;
}
break;
default:
i++;
continue;
}
}
free(dec);
if(l!=3) {
return 1;
}
if(mecmAlgo>0) {
return 1;
}
identMask = ident & 0xFF00;
if (identMask == 0x1100 || identMask == 0x500 || identMask == 0x3100) {
memcpy(&tmp1, dw, 4);
memcpy(&tmp2, dw + 4, 4);
memcpy(&tmp3, dw + 12, 4);
memcpy(dw, dw + 8, 4);
memcpy(dw + 4, &tmp3, 4);
memcpy(dw + 8, &tmp1, 4);
memcpy(dw + 12, &tmp2, 4);
}
return 0;
}
#endif // WITH_EMU