oscam-emu / oscam-patched-old

Open Source Cam Emulator
GNU General Public License v3.0
96 stars 55 forks source link

about some calculation #19

Closed momi133 closed 5 years ago

momi133 commented 5 years ago

1st hash is calculated by the enigma2  (orbital position, frequency, polarization) and the service id of the channel.

  1. how does oscam calculate it? please explain in detail. please!
oscam-emu commented 5 years ago

See https://github.com/oscam-emu/oscam-patched/blob/master/module-emulator-biss.c#L393

and https://github.com/oscam-emu/oscam-patched/blob/master/module-dvbapi.c#L2216

momi133 commented 5 years ago

Hi dear I understand that you have not enough time. but I'm not expert in c language. 1.is it possible explain the calculation of namespace hash algorithmically. I want this code in python language. we assume that enigma2 namespace and sid in known. 2.what is ecm and ecmLen in writed code? most of variables like srvid,tsid and ... are function of these two variables.

  1. I find crc32 function in web but what are these functions? b2i() and i2b_buf() thanks for replying and responsibility
oscam-emu commented 5 years ago

I assume you know how enigma2 creates the namespace (what info is included and how it is formed).

There are 2 namespace hashes, because there are 2 namespaces in enigma. When the tsid and the onid are deemed valid by enigma, the frequency info is stripped off and the namespace contains only the orbital position. This is described here: https://github.com/oscam-emu/oscam-patched/wiki/BISS-config#note-6

So in case of "full" namespace (with frequency), we use only the namespace and the service id in the hash. We place them like this: [srvid][namespace], so we get a 2+4=6 byte long sequence. Then we create a crc32 hash of these (caid is 0x2600 for biss and used as initial value in our crc32).

In case of stripped namespace, we use the tsid and onid as well in our hash, so it is still unique. So we place then like this: [srvid][tsid][onid][namespace] and we get a 2+2+2+4=10 byte sequence, from which we calculate the crc32 hash again.

Another thing you should know is that our namespace is not exactly the same as enigma's. In our namespace, we clear the 4 most significant bits (in case of DVB-T or DVB-C they are 0xF or 0xE respectively - for DVB-S they are 0) and we replace them with 0xA. This is our flag, so the emu can tell that this is the namespace and not another pid like audio etc. This is done here: https://github.com/oscam-emu/oscam-patched/blob/master/module-dvbapi.c#L2216

So the ecm you asked is a byte sequence, part of which is the srvid, namespace, tsid, onid. The b2i and i2b functions mean buffer-to-integer and integer-to-buffer and are used to take some bytes from a buffer, e.g. take the namespace from the ecm, or vice versa, e.g. place the srvid to a specific position in the the ecm.

momi133 commented 5 years ago

crc32 function: is it right? image

please get a valid web address about crc32 algorithm and it's function

hash = crc32(caid, ecmCopy + ecmLen - 10, 10);

is crc32() is "C" function or you create this function?

our namespace is not exactly the same as enigma's. we clear the 4 most significant bits

Is your namespace, crc32 output or input? it seems to be crc32 output.

thanks for replying and responsibility

oscam-emu commented 5 years ago

Stripped namespace is 4 byte long, as full namespace. 00460000 is the correct. Also you forgot the flag we put in the beginning. After flagging full namespace is A0460ACB and stripped is A0460000. It's a simple "or", ens = ens | 0xA0000000

crc32 algorith in general, takes a byte string as input and calculates a 32 bit (4 byte) output string, our namespace hash.

momi133 commented 5 years ago

thank a lot! my problem is solved! I am ashamed because I waste your time! I admire and respect you because you are noble, unselfish and morally good. I have a Criticism about oscam-emu(how read biss keys from softcam.key) that I will propose you tomorrow! thanks for replying and responsibility

sentarokun commented 3 years ago

Hi @oscam-emu , would you help me on this? I would like to understand how the oscam namespace hash is calculated. Can you follow this example? data: E2 namespace: dec 4598562 (including freq) hex namespace: => 0x00462B22 modified namespace 0xA0462B22 (adding oscam identifier)

How to go further? How do you calc the CRC32 on the above result.

Thak you so much.

oscam-emu commented 3 years ago

The crc32 function is defined here: https://github.com/oscam-emu/oscam-patched/blob/master/oscam-string.c#L714

A python version that produces the same hash is:

table = array('L')

for byte in range(256):
    crc = 0
    for bit in range(8):
        if (byte ^ crc) & 1:
            crc = (crc >> 1) ^ 0xEDB88320
        else:
            crc >>= 1
        byte >>= 1
    table.append(crc)

def crc32(caid, string):

    value = caid ^ 0xffffffffL
    for char in string:
        value = table[(ord(char) ^ value) & 0xff] ^ (value >> 8)
    return value ^ 0xffffffffL
sentarokun commented 3 years ago

So I need to study some Python before attempting it. But please, in my example above, is it correct to set the string (in the crc32 func) to "0001A0462B22"? Is it the correct approach?

thx!!

oscam-emu commented 3 years ago

Yes, read the comments in the code:

https://github.com/oscam-emu/oscam-patched/blob/master/module-emulator-biss.c#L421 https://github.com/oscam-emu/oscam-patched/blob/master/module-emulator-biss.c#L426

sentarokun commented 3 years ago

Hi, thank you. I tried the whole process till Python, but the result differs from the suggested hash in the livelog. Could you please have a fast look at my steps? 1) Getting the E2 namespace from the DreamboxEDIT (freq 12550V, TSID 0001, ONID 0000, SID 0008 ==> E2 namespace 0x0046b106 ) or by the box itself 2) Changing the namespace by adding the 0xA identifier: ==> 0xA046B106 3) Adding the SRVID (SID 0008) ==> 0008A046B106 4) Running the python CRC32 function, by using:

The python code is attached.

Thanks!

hash.py.txt

oscam-emu commented 3 years ago

0x0008A046B106 is correct. You need to convert the hex data to acsii string first:

data = 0x0008A046B106 string = binascii.unhexlify(data) b = crc32(0x2600, string)

sentarokun commented 3 years ago

Great! Thank you so much for your support!