how-does-it-work.md 17 KB

How is "rarreg.key" generated?

中文版

WinRAR uses an ECC-based signature algorithm to generate rarreg.key. The algorithm it used is a varient of Chinese SM2 digital signature algorithm. Different to many standard ECDSAs, the curve that WinRAR selected is a curve over composite field GF2p15p17-inlined.

1. Composite field GF2p15p17-inlined

Elements in ground field GF2p15-inlined are represented with standard basis, i.e. polynomial basis. The irreducible polynomial is

where each coefficients is in GF2-inlined. If we use

as the standard basis of the ground field, an element A-inlined in GF2p15-inlined can be denoted as


The irreducible polynomial of composite field GF2p15p17-inlined is

where each coefficients is in GF2p15-inlined. If we use

as the standard basis of the composite field, an element B-inlined in GF2p15p17-inlined can be denoted as


For clarity, we use D-inlined, which is a 255-bits-long integer to denote an element B-inlined in GF2p15p17-inlined. The map between them is

2. Elliptic curve over GF2p15p17-inlined

The equation of the elliptic curve that WinRAR uses is

The base point G-inlined is

whose order n-inlined is

3. Message hash algorithm

We use

to denote a message whose length is l-inlined. So the SHA1 value of M-inlined should be

where are 5 state values when SHA1 outputs. Generally speaking, the final SHA1 value should be the join of these 5 state values while each of state values is serialized in big-endian.

However, WinRAR doesn't serialize the 5 state values. Instead, it use a big integer h-inlined as the hash of the input message.

4. ECC digital signature algorithm

We use k-inlined to denote private key, P-inlined to denote public key. So there must be

If we use h-inlined to denote the hash of input data, WinRAR use the following algorithm to perform signing:

  1. Generate a random big integer Rnd-inlined which satisfies .

  2. Calculate r-inlined

   <img src="http://latex.codecogs.com/svg.latex?r%3D%28%28Rnd%20%5Ccdot%20G%29_x&plus;h%29%5C%20%5C%20Mod%5C%20%5C%20n">

where means we take X coordinate of and convert it from GF2p15p17-inlined to a big integer.

If or , go back to step 1.

  1. Calculate s-inlined

   <img src="http://latex.codecogs.com/svg.latex?s%3D%28Rnd-kr%29%5C%20%5C%20Mod%5C%20%5C%20n">

If , go back to step 1.

  1. Output .

5. WinRAR private key generation algorithm

We use

to denote input data whose length is l-inlined. WinRAR use it to generate private key k-inlined.

  1. We use to denote 6 32-bits-long integer. So there is

   <img src="http://latex.codecogs.com/svg.latex?g_j%3D%5Csum_%7Bi%3D0%7D%5E%7B3%7Dg_%7Bj%2Ci%7D%20%5Ccdot%202%5E%7B8i%7D%20%5Cquad%20%5Cquad%20g_%7Bj%2Ci%7D%5Cin%5B0%2C256%29">

  1. Let .

  2. If , we calculate SHA1 value of T-inlined. Then assign SHA1 state value to :

   <img src="http://latex.codecogs.com/svg.latex?%5Cbegin%7Baligned%7D%20%5Ctextrm%7BSHA%7D_1%28T%29%26%3DS_0%7C%7CS_1%7C%7CS_2%7C%7CS_3%7C%7CS_4%20%5C%5C%20g_1%26%3DS_0%20%5C%5C%20g_2%26%3DS_1%20%5C%5C%20g_3%26%3DS_2%20%5C%5C%20g_4%26%3DS_3%20%5C%5C%20g_5%26%3DS_4%20%5C%5C%20%5Cend%7Baligned%7D">

Otherwise, when , we let

   <img src="http://latex.codecogs.com/svg.latex?%5Cbegin%7Baligned%7D%20g_1%26%3D%5Ctextrm%7B0xeb3eb781%7D%20%5C%5C%20g_2%26%3D%5Ctextrm%7B0x50265329%7D%20%5C%5C%20g_3%26%3D%5Ctextrm%7B0xdc5ef4a3%7D%20%5C%5C%20g_4%26%3D%5Ctextrm%7B0x6847b9d5%7D%20%5C%5C%20g_5%26%3D%5Ctextrm%7B0xcde43b4c%7D%20%5C%5C%20%5Cend%7Baligned%7D">

  1. Regard as counter, add itself by 1.

Calculate SHA1:

   <img src="http://latex.codecogs.com/svg.latex?%5Ctextrm%7BSHA%7D_1%28g_%7B0%2C0%7D%7C%7Cg_%7B0%2C1%7D%7C%7Cg_%7B0%2C2%7D%7C%7Cg_%7B0%2C3%7D%7C%7Cg_%7B1%2C0%7D%7C%7Cg_%7B1%2C1%7D%7C%7C%5Cldots%7C%7Cg_%7B5%2C0%7D%7C%7Cg_%7B5%2C1%7D%7C%7Cg_%7B5%2C2%7D%7C%7Cg_%7B5%2C3%7D%29%3DS_0%7C%7CS_1%7C%7CS_2%7C%7CS_3%7C%7CS_4">

We takes the lowest 16 bits of and donote it as .

  1. Repeat step 4 again with 14 times.

  2. After that, we will get . Then output private key

   <img src="http://latex.codecogs.com/svg.latex?k%3D%5Csum_%7Bi%3D1%7D%5E%7B15%7Dk_i%20%5Ccdot%202%5E%7B16i%7D">

6. The private key and public key of WinRAR

Private key k-inlined is

This private key is generated by the algorithm describled in section 5 where the length of data T-inlined is zero.

Public key P-inlined is

7. Generation of "rarreg.key"

The generation of license file rarreg.key requires 2 arguments:

  1. Username, an ANSI-encoded string, without null-terminator. Denoted as

   <img src="http://latex.codecogs.com/svg.latex?U%3Du_0u_1%20%5Cldots%20u_%7Bl-1%7D">

  1. License type, an ANSI-encoded string, without null-terminator. Denoted as

   <img src="http://latex.codecogs.com/svg.latex?L%3Dl_0l_1%20%5Cldots%20l_%7Bl-1%7D">

The following is the algorithm to generate rarreg.key.

  1. Use the algorithm describled in section 5, with argument UU-inlined, to generate private key and public key . Then output hexlified public key string with SM2 compressed public key format. The hexlified public key is denoted as Temp-inlined.

The length of Temp-inlined should be 64. If less, pad with '0' until the length is 64.

  1. Let Data3-inlined be

   <img src="http://latex.codecogs.com/svg.latex?Data%5E3%3D%5Ctexttt%7B%2260%22%7D%7C%7CTemp_0%7C%7CTemp_1%7C%7C%5Cldots%7C%7CTemp_%7B47%7D">

  1. Use the algorithm describled in section 5, with argument Data3-inlined, to generate private key and public key . Then output hexlified public key string with SM2 compressed public key format. The hexlified public key is denoted as Data0-inlined.

The length of Data0-inlined should be 64. If less, pad with '0' until the length is 64.

  1. Let UID-inlined be

   <img src="http://latex.codecogs.com/svg.latex?UID%3DTemp_%7B48%7D%7C%7CTemp_%7B49%7D%7C%7C%5Cldots%7C%7CTemp_%7B63%7D%7C%7CData%5E0_0%7C%7CData%5E0_1%7C%7CData%5E0_2%7C%7CData%5E0_3">

  1. Use the algorithm describled in section 4, with argument LL-inlined and private key k-inlined describled section 6, to get signature .

The bit length of and shall not be more than 240. Otherwise, repeat this step.

  1. Convert and to hex-integer string and , without "0x" prefix.

If the length of or is less than 60, pad character '0' until the length is 60.

  1. Let Data1-inlined be

   <img src="http://latex.codecogs.com/svg.latex?Data%5E1%3D%5Ctexttt%7B%2260%22%7D%7C%7CSZ%5E%7Bs_L%7D%7C%7CSZ%5E%7Br_L%7D">

  1. Let Temp-inlined be

   <img src="http://latex.codecogs.com/svg.latex?Temp%3DU%7C%7CData%5E0">

Use the algorithm describled in section 4, with argument Temp-inlined and private key k-inlined describled section 6, to get signature .

The bit length of and shall not be more than 240. Otherwise, repeat this step.

  1. Convert and to hex-integer string and , without "0x" prefix.

If the length of or is less than 60, pad character '0' until the length is 60.

  1. Let Data2-inlined be

  2. Calculate CRC32 value of

    The final checksum the complement of CRC32 value.

    Then convert the checksum to decimal string . If the length is less than 10, pad character '0' until the length is 10.

  3. Let Data-inlined be

  4. Output with format

    • A fixed header "RAR registration data", taking one line.

    • Username, taking one line.

    • License type, taking one line

    • UID, taking one line, with format:

    <img src="http://latex.codecogs.com/svg.latex?%5Ctexttt%7B%22UID%3D%22%7D%7C%7CUID">
    

    • Output Data-inlined, with 54 characters a line.