Subversion Repositories Kolibri OS

Rev

Rev 6461 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;    libcrash -- cryptographic hash functions
  2. ;
  3. ;    Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
  4. ;
  5. ;    This program is free software: you can redistribute it and/or modify
  6. ;    it under the terms of the GNU General Public License as published by
  7. ;    the Free Software Foundation, either version 3 of the License, or
  8. ;    (at your option) any later version.
  9. ;
  10. ;    This program is distributed in the hope that it will be useful,
  11. ;    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;    GNU General Public License for more details.
  14. ;
  15. ;    You should have received a copy of the GNU General Public License
  16. ;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18.  
  19. macro md4._.f b, c, d
  20. {
  21.         mov     eax, c
  22.         xor     eax, d
  23.         and     eax, b
  24.         xor     eax, d
  25. }
  26.  
  27. macro md4._.g b, c, d
  28. {
  29.         push    c d
  30.         mov     eax, b
  31.         and     eax, c
  32.         and     c, d
  33.         and     d, b
  34.         or      eax, c
  35.         or      eax, d
  36.         pop     d c
  37. }
  38.  
  39. macro md4._.h b, c, d
  40. {
  41.         mov     eax, b
  42.         xor     eax, c
  43.         xor     eax, d
  44. }
  45.  
  46. macro md4._.round func, a, b, c, d, index, shift, ac
  47. {
  48.         func    b, c, d
  49.         add     eax, [esi + index*4]
  50.         lea     a, [a + eax + ac]
  51.         rol     a, shift
  52. }
  53.  
  54.  
  55. proc md4.init _ctx
  56.         mov     ebx, [_ctx]
  57.         lea     edi, [ebx + ctx_md4.hash]
  58.         mov     esi, md4._.hash_init
  59.         mov     ecx, MD4_HASH_SIZE/4
  60.         rep     movsd
  61.         xor     eax, eax
  62.         mov     [ebx + ctx_md4.index], eax
  63.         mov     [ebx + ctx_md4.msglen_0], eax
  64.         mov     [ebx + ctx_md4.msglen_1], eax
  65.         ret
  66. endp
  67.  
  68.  
  69. proc md4._.block _hash
  70.  
  71.         mov     eax, [_hash]
  72.         mov     edi, [eax + 0x0]
  73.         mov     ebx, [eax + 0x4]
  74.         mov     ecx, [eax + 0x8]
  75.         mov     edx, [eax + 0xc]
  76.  
  77.         md4._.round     md4._.f, edi, ebx, ecx, edx,  0,  3, 0x00000000
  78.         md4._.round     md4._.f, edx, edi, ebx, ecx,  1,  7, 0x00000000
  79.         md4._.round     md4._.f, ecx, edx, edi, ebx,  2, 11, 0x00000000
  80.         md4._.round     md4._.f, ebx, ecx, edx, edi,  3, 19, 0x00000000
  81.         md4._.round     md4._.f, edi, ebx, ecx, edx,  4,  3, 0x00000000
  82.         md4._.round     md4._.f, edx, edi, ebx, ecx,  5,  7, 0x00000000
  83.         md4._.round     md4._.f, ecx, edx, edi, ebx,  6, 11, 0x00000000
  84.         md4._.round     md4._.f, ebx, ecx, edx, edi,  7, 19, 0x00000000
  85.         md4._.round     md4._.f, edi, ebx, ecx, edx,  8,  3, 0x00000000
  86.         md4._.round     md4._.f, edx, edi, ebx, ecx,  9,  7, 0x00000000
  87.         md4._.round     md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000
  88.         md4._.round     md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000
  89.         md4._.round     md4._.f, edi, ebx, ecx, edx, 12,  3, 0x00000000
  90.         md4._.round     md4._.f, edx, edi, ebx, ecx, 13,  7, 0x00000000
  91.         md4._.round     md4._.f, ecx, edx, edi, ebx, 14, 11, 0x00000000
  92.         md4._.round     md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000
  93.  
  94.         md4._.round     md4._.g, edi, ebx, ecx, edx,  0,  3, 0x5a827999
  95.         md4._.round     md4._.g, edx, edi, ebx, ecx,  4,  5, 0x5a827999
  96.         md4._.round     md4._.g, ecx, edx, edi, ebx,  8,  9, 0x5a827999
  97.         md4._.round     md4._.g, ebx, ecx, edx, edi, 12, 13, 0x5a827999
  98.         md4._.round     md4._.g, edi, ebx, ecx, edx,  1,  3, 0x5a827999
  99.         md4._.round     md4._.g, edx, edi, ebx, ecx,  5,  5, 0x5a827999
  100.         md4._.round     md4._.g, ecx, edx, edi, ebx,  9,  9, 0x5a827999
  101.         md4._.round     md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999
  102.         md4._.round     md4._.g, edi, ebx, ecx, edx,  2,  3, 0x5a827999
  103.         md4._.round     md4._.g, edx, edi, ebx, ecx,  6,  5, 0x5a827999
  104.         md4._.round     md4._.g, ecx, edx, edi, ebx, 10,  9, 0x5a827999
  105.         md4._.round     md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999
  106.         md4._.round     md4._.g, edi, ebx, ecx, edx,  3,  3, 0x5a827999
  107.         md4._.round     md4._.g, edx, edi, ebx, ecx,  7,  5, 0x5a827999
  108.         md4._.round     md4._.g, ecx, edx, edi, ebx, 11,  9, 0x5a827999
  109.         md4._.round     md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999
  110.  
  111.         md4._.round     md4._.h, edi, ebx, ecx, edx,  0,  3, 0x6ed9eba1
  112.         md4._.round     md4._.h, edx, edi, ebx, ecx,  8,  9, 0x6ed9eba1
  113.         md4._.round     md4._.h, ecx, edx, edi, ebx,  4, 11, 0x6ed9eba1
  114.         md4._.round     md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1
  115.         md4._.round     md4._.h, edi, ebx, ecx, edx,  2,  3, 0x6ed9eba1
  116.         md4._.round     md4._.h, edx, edi, ebx, ecx, 10,  9, 0x6ed9eba1
  117.         md4._.round     md4._.h, ecx, edx, edi, ebx,  6, 11, 0x6ed9eba1
  118.         md4._.round     md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1
  119.         md4._.round     md4._.h, edi, ebx, ecx, edx,  1,  3, 0x6ed9eba1
  120.         md4._.round     md4._.h, edx, edi, ebx, ecx,  9,  9, 0x6ed9eba1
  121.         md4._.round     md4._.h, ecx, edx, edi, ebx,  5, 11, 0x6ed9eba1
  122.         md4._.round     md4._.h, ebx, ecx, edx, edi, 13, 15, 0x6ed9eba1
  123.         md4._.round     md4._.h, edi, ebx, ecx, edx,  3,  3, 0x6ed9eba1
  124.         md4._.round     md4._.h, edx, edi, ebx, ecx, 11,  9, 0x6ed9eba1
  125.         md4._.round     md4._.h, ecx, edx, edi, ebx,  7, 11, 0x6ed9eba1
  126.         md4._.round     md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1
  127.  
  128.         mov     eax, [_hash]
  129.         add     [eax + 0x0], edi
  130.         add     [eax + 0x4], ebx
  131.         add     [eax + 0x8], ecx
  132.         add     [eax + 0xc], edx
  133.  
  134.         ret
  135. endp
  136.  
  137.  
  138. proc md4.update _ctx, _msg, _size
  139.         mov     ebx, [_ctx]
  140.         mov     ecx, [_size]
  141.         add     [ebx + ctx_md4.msglen_0], ecx
  142.         adc     [ebx + ctx_md4.msglen_1], 0
  143.  
  144.   .next_block:
  145.         mov     ebx, [_ctx]
  146.         mov     esi, [_msg]
  147.         mov     eax, [ebx + ctx_md4.index]
  148.         and     eax, MD4_BLOCK_SIZE-1
  149.         jnz     .copy_to_buf
  150.         test    esi, MD4_ALIGN_MASK
  151.         jnz     .copy_to_buf
  152.   .no_copy:
  153.         ; data is aligned, hash it in place without copying
  154.         mov     ebx, [_ctx]
  155.         cmp     [_size], MD4_BLOCK_SIZE
  156.         jb      .copy_quit
  157.         lea     eax, [ebx + ctx_md4.hash]
  158.         stdcall md4._.block, eax
  159.         sub     [_size], MD4_BLOCK_SIZE
  160.         add     esi, MD4_BLOCK_SIZE
  161.         jmp     .no_copy
  162.  
  163.   .copy_to_buf:
  164.         lea     edi, [ebx + ctx_md4.block]
  165.         add     edi, eax
  166.         mov     ecx, MD4_BLOCK_SIZE
  167.         sub     ecx, eax
  168.         cmp     [_size], ecx
  169.         jb      .copy_quit
  170.         sub     [_size], ecx
  171.         add     [_msg], ecx
  172.         add     [ebx + ctx_md4.index], ecx
  173.         rep     movsb
  174.         lea     eax, [ebx + ctx_md4.hash]
  175.         lea     esi, [ebx + ctx_md4.block]
  176.         stdcall md4._.block, eax
  177.         jmp     .next_block
  178.  
  179.   .copy_quit:
  180.         mov     ebx, [_ctx]
  181.         lea     edi, [ebx + ctx_md4.block]
  182.         mov     eax, [ebx + ctx_md4.index]
  183.         and     eax, MD4_BLOCK_SIZE-1
  184.         add     edi, eax
  185.         mov     ecx, [_size]
  186.         add     [ebx + ctx_md4.index], ecx
  187.         rep     movsb
  188.   .quit:
  189.         ret
  190. endp
  191.  
  192.  
  193. proc md4.final _ctx
  194.         mov     ebx, [_ctx]
  195.         lea     edi, [ebx + ctx_md4.block]
  196.         mov     ecx, [ebx + ctx_md4.msglen_0]
  197.         and     ecx, MD4_BLOCK_SIZE-1
  198.         add     edi, ecx
  199.         mov     byte[edi], 0x80
  200.         inc     edi
  201.         neg     ecx
  202.         add     ecx, MD4_BLOCK_SIZE
  203.         cmp     ecx, 8
  204.         ja      .last
  205.  
  206.         dec     ecx
  207.         xor     eax, eax
  208.         rep     stosb
  209.         lea     esi, [ebx + ctx_md4.block]
  210.         lea     eax, [ebx + ctx_md4.hash]
  211.         stdcall md4._.block, eax
  212.         mov     ebx, [_ctx]
  213.         lea     edi, [ebx + ctx_md4.block]
  214.         mov     ecx, MD4_BLOCK_SIZE+1
  215.   .last:
  216.         dec     ecx
  217.         sub     ecx, 8
  218.         xor     eax, eax
  219.         rep     stosb
  220.         mov     eax, [ebx + ctx_md4.msglen_0]
  221.         mov     edx, [ebx + ctx_md4.msglen_1]
  222.         shld    edx, eax, 3
  223.         shl     eax, 3
  224.         mov     dword[edi], eax
  225.         mov     dword[edi+4], edx
  226.         lea     esi, [ebx + ctx_md4.block]
  227.         lea     eax, [ebx + ctx_md4.hash]
  228.         stdcall md4._.block, eax
  229.  
  230.         ret
  231. endp
  232.  
  233.  
  234. align MD4_ALIGN
  235.  
  236. md4._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
  237.  
  238.