Subversion Repositories Kolibri OS

Rev

Rev 3431 | 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. MD4_BLOCK_SIZE = 64
  20. MD4_HASH_SIZE  = 16
  21. MD4_ALIGN      = 4
  22. MD4_ALIGN_MASK = MD4_ALIGN - 1
  23.  
  24. struct ctx_md4
  25.         hash            rb MD4_HASH_SIZE
  26.         block           rb MD4_BLOCK_SIZE
  27.         index           rd 1
  28.         msglen_0        rd 1
  29.         msglen_1        rd 1
  30. ends
  31.  
  32.  
  33. macro md4._.f b, c, d
  34. {
  35.         mov     eax, c
  36.         xor     eax, d
  37.         and     eax, b
  38.         xor     eax, d
  39. }
  40.  
  41. macro md4._.g b, c, d
  42. {
  43.         push    c d
  44.         mov     eax, b
  45.         and     eax, c
  46.         and     c, d
  47.         and     d, b
  48.         or      eax, c
  49.         or      eax, d
  50.         pop     d c
  51. }
  52.  
  53. macro md4._.h b, c, d
  54. {
  55.         mov     eax, b
  56.         xor     eax, c
  57.         xor     eax, d
  58. }
  59.  
  60. macro md4._.round func, a, b, c, d, index, shift, ac
  61. {
  62.         func    b, c, d
  63.         add     eax, [esi + index*4]
  64.         lea     a, [a + eax + ac]
  65.         rol     a, shift
  66. }
  67.  
  68.  
  69. proc md4.init _ctx
  70.         mov     ebx, [_ctx]
  71.         lea     edi, [ebx + ctx_md4.hash]
  72.         mov     esi, md4._.hash_init
  73.         mov     ecx, MD4_HASH_SIZE/4
  74.         rep     movsd
  75.         xor     eax, eax
  76.         mov     [ebx + ctx_md4.index], eax
  77.         mov     [ebx + ctx_md4.msglen_0], eax
  78.         mov     [ebx + ctx_md4.msglen_1], eax
  79.         ret
  80. endp
  81.  
  82.  
  83. proc md4._.block _hash
  84.  
  85.         mov     eax, [_hash]
  86.         mov     edi, [eax + 0x0]
  87.         mov     ebx, [eax + 0x4]
  88.         mov     ecx, [eax + 0x8]
  89.         mov     edx, [eax + 0xc]
  90.  
  91.         md4._.round     md4._.f, edi, ebx, ecx, edx,  0,  3, 0x00000000
  92.         md4._.round     md4._.f, edx, edi, ebx, ecx,  1,  7, 0x00000000
  93.         md4._.round     md4._.f, ecx, edx, edi, ebx,  2, 11, 0x00000000
  94.         md4._.round     md4._.f, ebx, ecx, edx, edi,  3, 19, 0x00000000
  95.         md4._.round     md4._.f, edi, ebx, ecx, edx,  4,  3, 0x00000000
  96.         md4._.round     md4._.f, edx, edi, ebx, ecx,  5,  7, 0x00000000
  97.         md4._.round     md4._.f, ecx, edx, edi, ebx,  6, 11, 0x00000000
  98.         md4._.round     md4._.f, ebx, ecx, edx, edi,  7, 19, 0x00000000
  99.         md4._.round     md4._.f, edi, ebx, ecx, edx,  8,  3, 0x00000000
  100.         md4._.round     md4._.f, edx, edi, ebx, ecx,  9,  7, 0x00000000
  101.         md4._.round     md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000
  102.         md4._.round     md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000
  103.         md4._.round     md4._.f, edi, ebx, ecx, edx, 12,  3, 0x00000000
  104.         md4._.round     md4._.f, edx, edi, ebx, ecx, 13,  7, 0x00000000
  105.         md4._.round     md4._.f, ecx, edx, edi, ebx, 14, 11, 0x00000000
  106.         md4._.round     md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000
  107.  
  108.         md4._.round     md4._.g, edi, ebx, ecx, edx,  0,  3, 0x5a827999
  109.         md4._.round     md4._.g, edx, edi, ebx, ecx,  4,  5, 0x5a827999
  110.         md4._.round     md4._.g, ecx, edx, edi, ebx,  8,  9, 0x5a827999
  111.         md4._.round     md4._.g, ebx, ecx, edx, edi, 12, 13, 0x5a827999
  112.         md4._.round     md4._.g, edi, ebx, ecx, edx,  1,  3, 0x5a827999
  113.         md4._.round     md4._.g, edx, edi, ebx, ecx,  5,  5, 0x5a827999
  114.         md4._.round     md4._.g, ecx, edx, edi, ebx,  9,  9, 0x5a827999
  115.         md4._.round     md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999
  116.         md4._.round     md4._.g, edi, ebx, ecx, edx,  2,  3, 0x5a827999
  117.         md4._.round     md4._.g, edx, edi, ebx, ecx,  6,  5, 0x5a827999
  118.         md4._.round     md4._.g, ecx, edx, edi, ebx, 10,  9, 0x5a827999
  119.         md4._.round     md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999
  120.         md4._.round     md4._.g, edi, ebx, ecx, edx,  3,  3, 0x5a827999
  121.         md4._.round     md4._.g, edx, edi, ebx, ecx,  7,  5, 0x5a827999
  122.         md4._.round     md4._.g, ecx, edx, edi, ebx, 11,  9, 0x5a827999
  123.         md4._.round     md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999
  124.  
  125.         md4._.round     md4._.h, edi, ebx, ecx, edx,  0,  3, 0x6ed9eba1
  126.         md4._.round     md4._.h, edx, edi, ebx, ecx,  8,  9, 0x6ed9eba1
  127.         md4._.round     md4._.h, ecx, edx, edi, ebx,  4, 11, 0x6ed9eba1
  128.         md4._.round     md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1
  129.         md4._.round     md4._.h, edi, ebx, ecx, edx,  2,  3, 0x6ed9eba1
  130.         md4._.round     md4._.h, edx, edi, ebx, ecx, 10,  9, 0x6ed9eba1
  131.         md4._.round     md4._.h, ecx, edx, edi, ebx,  6, 11, 0x6ed9eba1
  132.         md4._.round     md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1
  133.         md4._.round     md4._.h, edi, ebx, ecx, edx,  1,  3, 0x6ed9eba1
  134.         md4._.round     md4._.h, edx, edi, ebx, ecx,  9,  9, 0x6ed9eba1
  135.         md4._.round     md4._.h, ecx, edx, edi, ebx,  5, 11, 0x6ed9eba1
  136.         md4._.round     md4._.h, ebx, ecx, edx, edi, 13, 15, 0x6ed9eba1
  137.         md4._.round     md4._.h, edi, ebx, ecx, edx,  3,  3, 0x6ed9eba1
  138.         md4._.round     md4._.h, edx, edi, ebx, ecx, 11,  9, 0x6ed9eba1
  139.         md4._.round     md4._.h, ecx, edx, edi, ebx,  7, 11, 0x6ed9eba1
  140.         md4._.round     md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1
  141.  
  142.         mov     eax, [_hash]
  143.         add     [eax + 0x0], edi
  144.         add     [eax + 0x4], ebx
  145.         add     [eax + 0x8], ecx
  146.         add     [eax + 0xc], edx
  147.  
  148.         ret
  149. endp
  150.  
  151.  
  152. proc md4.update _ctx, _msg, _size
  153.         mov     ebx, [_ctx]
  154.         mov     ecx, [_size]
  155.         add     [ebx + ctx_md4.msglen_0], ecx
  156.         adc     [ebx + ctx_md4.msglen_1], 0
  157.  
  158.   .next_block:
  159.         mov     ebx, [_ctx]
  160.         mov     esi, [_msg]
  161.         mov     eax, [ebx + ctx_md4.index]
  162.         and     eax, MD4_BLOCK_SIZE-1
  163.         jnz     .copy_to_buf
  164.         test    esi, MD4_ALIGN_MASK
  165.         jnz     .copy_to_buf
  166.   .no_copy:
  167.         ; data is aligned, hash it in place without copying
  168.         mov     ebx, [_ctx]
  169.         cmp     [_size], MD4_BLOCK_SIZE
  170.         jb      .copy_quit
  171.         lea     eax, [ebx + ctx_md4.hash]
  172.         stdcall md4._.block, eax
  173.         sub     [_size], MD4_BLOCK_SIZE
  174.         add     esi, MD4_BLOCK_SIZE
  175.         jmp     .no_copy
  176.  
  177.   .copy_to_buf:
  178.         lea     edi, [ebx + ctx_md4.block]
  179.         add     edi, eax
  180.         mov     ecx, MD4_BLOCK_SIZE
  181.         sub     ecx, eax
  182.         cmp     [_size], ecx
  183.         jb      .copy_quit
  184.         sub     [_size], ecx
  185.         add     [_msg], ecx
  186.         add     [ebx + ctx_md4.index], ecx
  187.         rep     movsb
  188.         lea     eax, [ebx + ctx_md4.hash]
  189.         lea     esi, [ebx + ctx_md4.block]
  190.         stdcall md4._.block, eax
  191.         jmp     .next_block
  192.  
  193.   .copy_quit:
  194.         mov     ebx, [_ctx]
  195.         lea     edi, [ebx + ctx_md4.block]
  196.         mov     eax, [ebx + ctx_md4.index]
  197.         and     eax, MD4_BLOCK_SIZE-1
  198.         add     edi, eax
  199.         mov     ecx, [_size]
  200.         add     [ebx + ctx_md4.index], ecx
  201.         rep     movsb
  202.   .quit:
  203.         ret
  204. endp
  205.  
  206.  
  207. proc md4.final _ctx
  208.         mov     ebx, [_ctx]
  209.         lea     edi, [ebx + ctx_md4.block]
  210.         mov     ecx, [ebx + ctx_md4.msglen_0]
  211.         and     ecx, MD4_BLOCK_SIZE-1
  212.         add     edi, ecx
  213.         mov     byte[edi], 0x80
  214.         inc     edi
  215.         neg     ecx
  216.         add     ecx, MD4_BLOCK_SIZE
  217.         cmp     ecx, 8
  218.         ja      .last
  219.  
  220.         dec     ecx
  221.         xor     eax, eax
  222.         rep     stosb
  223.         lea     esi, [ebx + ctx_md4.block]
  224.         lea     eax, [ebx + ctx_md4.hash]
  225.         stdcall md4._.block, eax
  226.         mov     ebx, [_ctx]
  227.         lea     edi, [ebx + ctx_md4.block]
  228.         mov     ecx, MD4_BLOCK_SIZE+1
  229.   .last:
  230.         dec     ecx
  231.         sub     ecx, 8
  232.         xor     eax, eax
  233.         rep     stosb
  234.         mov     eax, [ebx + ctx_md4.msglen_0]
  235.         mov     edx, [ebx + ctx_md4.msglen_1]
  236.         shld    edx, eax, 3
  237.         shl     eax, 3
  238.         mov     dword[edi], eax
  239.         mov     dword[edi+4], edx
  240.         lea     esi, [ebx + ctx_md4.block]
  241.         lea     eax, [ebx + ctx_md4.hash]
  242.         stdcall md4._.block, eax
  243.  
  244.         ret
  245. endp
  246.  
  247.  
  248. align MD4_ALIGN
  249.  
  250. md4._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
  251.  
  252.