Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ; Password handling in 7-Zip: "7zAES" filter (SHA256 + AES256).
  2. ; Ported from C++ sources of 7-Zip (c) Igor Pavlov.
  3. aes7z_decoder:
  4. virtual at 0
  5. .outStream      rb      streamInfo.size
  6. .inStream       dd      ?
  7. .inLen          dd      ?
  8. .inPtr          dd      ?
  9. .bufRest        dd      ?
  10. ; key data
  11. .NumCyclesPower dd      ?
  12. .SaltSize       dd      ?
  13. .Salt           rb      16
  14. ; AES data
  15. .iv             rb      16
  16. .Key            rb      32
  17. .nr             dd      ?
  18. .KeyExpanded    rb      32*15
  19. .size = $
  20. end virtual
  21.  
  22. .fillBuf:
  23.         mov     esi, [eax+.inPtr]
  24.         mov     ebp, eax
  25.         add     edi, [eax+.bufRest]
  26.         sub     ecx, [eax+.bufRest]
  27.         js      .rest1
  28.         and     [eax+.bufRest], 0
  29. .mainloop:
  30.         test    ecx, ecx
  31.         jz      .done
  32.         sub     [ebp+.inLen], 16
  33.         js      .refill
  34. .refilled:
  35.         push    esi edi ecx
  36.         mov     ebx, edi
  37.         lea     edi, [ebp+.nr]
  38.         call    aes_decode
  39.         pop     ecx edi esi
  40.         mov     eax, dword [ebp+.iv]
  41.         xor     [edi], eax
  42.         lodsd
  43.         mov     dword [ebp+.iv], eax
  44.         mov     eax, dword [ebp+.iv+4]
  45.         xor     [edi+4], eax
  46.         lodsd
  47.         mov     dword [ebp+.iv+4], eax
  48.         mov     eax, dword [ebp+.iv+8]
  49.         xor     [edi+8], eax
  50.         lodsd
  51.         mov     dword [ebp+.iv+8], eax
  52.         mov     eax, dword [ebp+.iv+12]
  53.         xor     [edi+12], eax
  54.         lodsd
  55.         mov     dword [ebp+.iv+12], eax
  56.         add     edi, 16
  57.         sub     ecx, 16
  58.         jns     .mainloop
  59. .rest1:
  60.         neg     ecx
  61.         mov     [ebp+.bufRest], ecx
  62. .done:
  63.         mov     [ebp+.inPtr], esi
  64.         popad
  65.         ret
  66.  
  67. .refill:
  68.         mov     edx, [ebp+.inLen]
  69.         add     edx, 16
  70.         jnz     .rest
  71.         js      return.err
  72.         mov     eax, [ebp+.inStream]
  73.         call    fillBuf
  74.         mov     edx, [eax+streamInfo.bufDataLen]
  75.         test    edx, edx
  76.         jz      return.err
  77.         mov     esi, [eax+streamInfo.bufPtr]
  78.         mov     [ebp+.inLen], edx
  79.         sub     [ebp+.inLen], 16
  80.         jns     .refilled
  81. .rest:
  82. ; ASSERT([eax+streamInfo.fullSize] == 0);
  83.         sub     edx, ecx
  84.         jb      return.err
  85.         add     ecx, edx
  86.         rep     movsb
  87.         mov     [ebp+.bufRest], edx
  88.         jmp     .done
  89.  
  90. aes7z_get_buf_size:
  91.         mov     eax, aes7z_decoder.size
  92.         mov     edx, 0x4000
  93.         ret
  94.  
  95. aes7z_init_decoder:
  96. ; zero all
  97.         xor     eax, eax
  98.         mov     [ebp+aes7z_decoder.inLen], eax
  99.         mov     [ebp+aes7z_decoder.bufRest], eax
  100.         mov     [ebp+aes7z_decoder.NumCyclesPower], eax
  101.         mov     [ebp+aes7z_decoder.SaltSize], eax
  102.         lea     edi, [ebp+aes7z_decoder.Salt]
  103.         push    8
  104.         pop     ecx
  105.         rep     stosd   ; zero .Salt and .iv
  106.         mov     [ebp+streamInfo.fillBuf], aes7z_decoder.fillBuf
  107. ; parse parameters
  108.         cmp     dword [esi-4], eax
  109.         jz      .parok  ; no parameters - OK
  110.         lodsb
  111.         mov     cl, al
  112.         and     al, 0x3F
  113.         mov     byte [ebp+aes7z_decoder.NumCyclesPower], al
  114.         test    cl, 0xC0
  115.         jz      .parok
  116.         test    cl, 0x80
  117.         setnz   byte [ebp+aes7z_decoder.SaltSize]
  118.         shr     cl, 6
  119.         and     ecx, 1
  120.         cmp     dword [esi-1-4], 2
  121.         jb      return.err
  122.         lodsb
  123.         mov     dl, al
  124.         shr     al, 4
  125.         add     byte [ebp+aes7z_decoder.SaltSize], al
  126.         and     edx, 0xF
  127.         add     ecx, edx
  128.         lea     edx, [ecx+2]
  129.         push    ecx
  130.         mov     ecx, [ebp+aes7z_decoder.SaltSize]
  131.         add     edx, ecx
  132.         cmp     dword [esi-2-4], edx
  133.         jb      return.err
  134.         lea     edi, [ebp+aes7z_decoder.Salt]
  135.         rep     movsb
  136.         pop     ecx
  137.         lea     edi, [ebp+aes7z_decoder.iv]
  138.         rep     movsb
  139. .parok:
  140.         test    bl, bl
  141.         jnz     .ret    ; if reinitializing - all calculations have been already done
  142.         call    query_password
  143.         jz      return.clear
  144. ;.CalculateDigest:
  145.         mov     cl, byte [ebp+aes7z_decoder.NumCyclesPower]
  146.         cmp     cl, 0x3F
  147.         jnz     .sha
  148.         lea     edi, [ebp+aes7z_decoder.Key]
  149.         mov     ecx, [ebp+aes7z_decoder.SaltSize]
  150.         push    32
  151.         pop     edx
  152.         sub     edx, ecx
  153.         lea     esi, [ebp+aes7z_decoder.Salt]
  154.         rep     movsb
  155.         mov     ecx, [password_size]
  156.         add     ecx, ecx
  157.         cmp     ecx, edx
  158.         jbe     @f
  159.         mov     ecx, edx
  160. @@:
  161.         sub     edx, ecx
  162.         mov     esi, password_unicode
  163.         rep     movsb
  164.         mov     ecx, edx
  165.         xor     eax, eax
  166.         rep     stosb
  167.         jmp     .setkey
  168. .sha:
  169.         cmp     cl, 32
  170.         jb      .normal
  171.         push    1
  172.         shl     dword [esp], cl
  173.         push    0
  174.         jmp     @f
  175. .normal:
  176.         push    0
  177.         push    1
  178.         shl     dword [esp], cl
  179. @@:
  180.         push    0
  181.         push    0
  182.         call    sha256_init
  183. .loop:
  184.         lea     esi, [ebp+aes7z_decoder.Salt]
  185.         mov     edx, [ebp+aes7z_decoder.SaltSize]
  186.         call    sha256_update
  187.         mov     esi, password_unicode
  188.         mov     edx, [password_size]
  189.         add     edx, edx
  190.         call    sha256_update
  191.         mov     esi, esp
  192.         push    8
  193.         pop     edx
  194.         call    sha256_update
  195.         mov     esi, esp
  196.         dec     esi
  197. @@:
  198.         inc     esi
  199.         inc     byte [esi]
  200.         jz      @b
  201.         sub     dword [esp+8], 1
  202.         sbb     dword [esp+12], 0
  203.         mov     eax, [esp+8]
  204.         or      eax, [esp+12]
  205.         jnz     .loop
  206.         lea     edi, [ebp+aes7z_decoder.Key]
  207.         call    sha256_final
  208.         add     esp, 16
  209. .setkey:
  210.         lea     esi, [ebp+aes7z_decoder.Key]
  211.         push    8
  212.         pop     edx     ; 7z uses 256-bit keys
  213.         lea     edi, [ebp+aes7z_decoder.nr]
  214.         call    aes_setkey
  215. .ret:
  216.         ret
  217.