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. MD5_BLOCK_SIZE = 64
  20. MD5_HASH_SIZE  = 16
  21. MD5_ALIGN      = 4
  22. MD5_ALIGN_MASK = MD5_ALIGN - 1
  23.  
  24. struct ctx_md5
  25.         hash            rb MD5_HASH_SIZE
  26.         block           rb MD5_BLOCK_SIZE
  27.         index           rd 1
  28.         msglen_0        rd 1
  29.         msglen_1        rd 1
  30. ends
  31.  
  32.  
  33. macro md5._.f b, c, d
  34. {
  35.         push    c
  36.         xor     c, d
  37.         and     b, c
  38.         xor     b, d
  39.         pop     c
  40. }
  41.  
  42. macro md5._.g b, c, d
  43. {
  44.         push    c  d
  45.         and     b, d
  46.         not     d
  47.         and     c, d
  48.         or      b, c
  49.         pop     d  c
  50. }
  51.  
  52. macro md5._.h b, c, d
  53. {
  54.         xor     b, c
  55.         xor     b, d
  56. }
  57.  
  58. macro md5._.i b, c, d
  59. {
  60.         push    d
  61.         not     d
  62.         or      b, d
  63.         xor     b, c
  64.         pop     d
  65. }
  66.  
  67. macro md5._.round func, a, b, c, d, index, shift, ac
  68. {
  69.         push    b
  70.         func    b, c, d
  71.         lea     a, [a + b + ac]
  72.         add     a, [esi + index*4]
  73.         rol     a, shift
  74.         pop     b
  75.         add     a, b
  76. }
  77.  
  78.  
  79. proc md5.init _ctx
  80.         mov     ebx, [_ctx]
  81.         lea     edi, [ebx + ctx_md5.hash]
  82.         mov     esi, md5._.hash_init
  83.         mov     ecx, MD5_HASH_SIZE/4
  84.         rep     movsd
  85.         xor     eax, eax
  86.         mov     [ebx + ctx_md5.index], eax
  87.         mov     [ebx + ctx_md5.msglen_0], eax
  88.         mov     [ebx + ctx_md5.msglen_1], eax
  89.         ret
  90. endp
  91.  
  92.  
  93. proc md5._.block _hash
  94.  
  95.         mov     edi, [_hash]
  96.         mov     eax, [edi + 0x0]
  97.         mov     ebx, [edi + 0x4]
  98.         mov     ecx, [edi + 0x8]
  99.         mov     edx, [edi + 0xc]
  100.  
  101.         md5._.round     md5._.f, eax, ebx, ecx, edx,  0,  7, 0xd76aa478
  102.         md5._.round     md5._.f, edx, eax, ebx, ecx,  1, 12, 0xe8c7b756
  103.         md5._.round     md5._.f, ecx, edx, eax, ebx,  2, 17, 0x242070db
  104.         md5._.round     md5._.f, ebx, ecx, edx, eax,  3, 22, 0xc1bdceee
  105.         md5._.round     md5._.f, eax, ebx, ecx, edx,  4,  7, 0xf57c0faf
  106.         md5._.round     md5._.f, edx, eax, ebx, ecx,  5, 12, 0x4787c62a
  107.         md5._.round     md5._.f, ecx, edx, eax, ebx,  6, 17, 0xa8304613
  108.         md5._.round     md5._.f, ebx, ecx, edx, eax,  7, 22, 0xfd469501
  109.         md5._.round     md5._.f, eax, ebx, ecx, edx,  8,  7, 0x698098d8
  110.         md5._.round     md5._.f, edx, eax, ebx, ecx,  9, 12, 0x8b44f7af
  111.         md5._.round     md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1
  112.         md5._.round     md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be
  113.         md5._.round     md5._.f, eax, ebx, ecx, edx, 12,  7, 0x6b901122
  114.         md5._.round     md5._.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193
  115.         md5._.round     md5._.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e
  116.         md5._.round     md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821
  117.  
  118.         md5._.round     md5._.g, eax, ebx, ecx, edx,  1,  5, 0xf61e2562
  119.         md5._.round     md5._.g, edx, eax, ebx, ecx,  6,  9, 0xc040b340
  120.         md5._.round     md5._.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
  121.         md5._.round     md5._.g, ebx, ecx, edx, eax,  0, 20, 0xe9b6c7aa
  122.         md5._.round     md5._.g, eax, ebx, ecx, edx,  5,  5, 0xd62f105d
  123.         md5._.round     md5._.g, edx, eax, ebx, ecx, 10,  9, 0x02441453
  124.         md5._.round     md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681
  125.         md5._.round     md5._.g, ebx, ecx, edx, eax,  4, 20, 0xe7d3fbc8
  126.         md5._.round     md5._.g, eax, ebx, ecx, edx,  9,  5, 0x21e1cde6
  127.         md5._.round     md5._.g, edx, eax, ebx, ecx, 14,  9, 0xc33707d6
  128.         md5._.round     md5._.g, ecx, edx, eax, ebx,  3, 14, 0xf4d50d87
  129.         md5._.round     md5._.g, ebx, ecx, edx, eax,  8, 20, 0x455a14ed
  130.         md5._.round     md5._.g, eax, ebx, ecx, edx, 13,  5, 0xa9e3e905
  131.         md5._.round     md5._.g, edx, eax, ebx, ecx,  2,  9, 0xfcefa3f8
  132.         md5._.round     md5._.g, ecx, edx, eax, ebx,  7, 14, 0x676f02d9
  133.         md5._.round     md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a
  134.  
  135.         md5._.round     md5._.h, eax, ebx, ecx, edx,  5,  4, 0xfffa3942
  136.         md5._.round     md5._.h, edx, eax, ebx, ecx,  8, 11, 0x8771f681
  137.         md5._.round     md5._.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
  138.         md5._.round     md5._.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c
  139.         md5._.round     md5._.h, eax, ebx, ecx, edx,  1,  4, 0xa4beea44
  140.         md5._.round     md5._.h, edx, eax, ebx, ecx,  4, 11, 0x4bdecfa9
  141.         md5._.round     md5._.h, ecx, edx, eax, ebx,  7, 16, 0xf6bb4b60
  142.         md5._.round     md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70
  143.         md5._.round     md5._.h, eax, ebx, ecx, edx, 13,  4, 0x289b7ec6
  144.         md5._.round     md5._.h, edx, eax, ebx, ecx,  0, 11, 0xeaa127fa
  145.         md5._.round     md5._.h, ecx, edx, eax, ebx,  3, 16, 0xd4ef3085
  146.         md5._.round     md5._.h, ebx, ecx, edx, eax,  6, 23, 0x04881d05
  147.         md5._.round     md5._.h, eax, ebx, ecx, edx,  9,  4, 0xd9d4d039
  148.         md5._.round     md5._.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5
  149.         md5._.round     md5._.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8
  150.         md5._.round     md5._.h, ebx, ecx, edx, eax,  2, 23, 0xc4ac5665
  151.  
  152.         md5._.round     md5._.i, eax, ebx, ecx, edx,  0,  6, 0xf4292244
  153.         md5._.round     md5._.i, edx, eax, ebx, ecx,  7, 10, 0x432aff97
  154.         md5._.round     md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
  155.         md5._.round     md5._.i, ebx, ecx, edx, eax,  5, 21, 0xfc93a039
  156.         md5._.round     md5._.i, eax, ebx, ecx, edx, 12,  6, 0x655b59c3
  157.         md5._.round     md5._.i, edx, eax, ebx, ecx,  3, 10, 0x8f0ccc92
  158.         md5._.round     md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d
  159.         md5._.round     md5._.i, ebx, ecx, edx, eax,  1, 21, 0x85845dd1
  160.         md5._.round     md5._.i, eax, ebx, ecx, edx,  8,  6, 0x6fa87e4f
  161.         md5._.round     md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0
  162.         md5._.round     md5._.i, ecx, edx, eax, ebx,  6, 15, 0xa3014314
  163.         md5._.round     md5._.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
  164.         md5._.round     md5._.i, eax, ebx, ecx, edx,  4,  6, 0xf7537e82
  165.         md5._.round     md5._.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235
  166.         md5._.round     md5._.i, ecx, edx, eax, ebx,  2, 15, 0x2ad7d2bb
  167.         md5._.round     md5._.i, ebx, ecx, edx, eax,  9, 21, 0xeb86d391
  168.  
  169.         mov     edi, [_hash]
  170.         add     [edi + 0x0], eax
  171.         add     [edi + 0x4], ebx
  172.         add     [edi + 0x8], ecx
  173.         add     [edi + 0xc], edx
  174.  
  175.         ret
  176. endp
  177.  
  178.  
  179. proc md5.update _ctx, _msg, _size
  180.         mov     ebx, [_ctx]
  181.         mov     ecx, [_size]
  182.         add     [ebx + ctx_md5.msglen_0], ecx
  183.         adc     [ebx + ctx_md5.msglen_1], 0
  184.  
  185.   .next_block:
  186.         mov     ebx, [_ctx]
  187.         mov     esi, [_msg]
  188.         mov     eax, [ebx + ctx_md5.index]
  189.         and     eax, MD5_BLOCK_SIZE-1
  190.         jnz     .copy_to_buf
  191.         test    esi, MD5_ALIGN_MASK
  192.         jnz     .copy_to_buf
  193.   .no_copy:
  194.         ; data is aligned, hash it in place without copying
  195.         mov     ebx, [_ctx]
  196.         cmp     [_size], MD5_BLOCK_SIZE
  197.         jb      .copy_quit
  198.         lea     eax, [ebx + ctx_md5.hash]
  199.         stdcall md5._.block, eax
  200.         sub     [_size], MD5_BLOCK_SIZE
  201.         add     esi, MD5_BLOCK_SIZE
  202.         jmp     .no_copy
  203.  
  204.   .copy_to_buf:
  205.         lea     edi, [ebx + ctx_md5.block]
  206.         add     edi, eax
  207.         mov     ecx, MD5_BLOCK_SIZE
  208.         sub     ecx, eax
  209.         cmp     [_size], ecx
  210.         jb      .copy_quit
  211.         sub     [_size], ecx
  212.         add     [_msg], ecx
  213.         add     [ebx + ctx_md5.index], ecx
  214.         rep     movsb
  215.         lea     eax, [ebx + ctx_md5.hash]
  216.         lea     esi, [ebx + ctx_md5.block]
  217.         stdcall md5._.block, eax
  218.         jmp     .next_block
  219.  
  220.   .copy_quit:
  221.         mov     ebx, [_ctx]
  222.         lea     edi, [ebx + ctx_md5.block]
  223.         mov     eax, [ebx + ctx_md5.index]
  224.         and     eax, MD5_BLOCK_SIZE-1
  225.         add     edi, eax
  226.         mov     ecx, [_size]
  227.         add     [ebx + ctx_md5.index], ecx
  228.         rep     movsb
  229.   .quit:
  230.  
  231.         ret
  232. endp
  233.  
  234.  
  235. proc md5.final _ctx
  236.         mov     ebx, [_ctx]
  237.         lea     edi, [ebx + ctx_md5.block]
  238.         mov     ecx, [ebx + ctx_md5.msglen_0]
  239.         and     ecx, MD5_BLOCK_SIZE-1
  240.         add     edi, ecx
  241.         mov     byte[edi], 0x80
  242.         inc     edi
  243.         neg     ecx
  244.         add     ecx, MD5_BLOCK_SIZE
  245.         cmp     ecx, 8
  246.         ja      .last
  247.  
  248.         dec     ecx
  249.         xor     eax, eax
  250.         rep     stosb
  251.         lea     esi, [ebx + ctx_md5.block]
  252.         lea     eax, [ebx + ctx_md5.hash]
  253.         stdcall md5._.block, eax
  254.         mov     ebx, [_ctx]
  255.         lea     edi, [ebx + ctx_md5.block]
  256.         mov     ecx, MD5_BLOCK_SIZE+1
  257.   .last:
  258.         dec     ecx
  259.         sub     ecx, 8
  260.         xor     eax, eax
  261.         rep     stosb
  262.         mov     eax, [ebx + ctx_md5.msglen_0]
  263.         mov     edx, [ebx + ctx_md5.msglen_1]
  264.         shld    edx, eax, 3
  265.         shl     eax, 3
  266.         mov     dword[edi], eax
  267.         mov     dword[edi+4], edx
  268.         lea     esi, [ebx + ctx_md5.block]
  269.         lea     eax, [ebx + ctx_md5.hash]
  270.         stdcall md5._.block, eax
  271.  
  272.         ret
  273. endp
  274.  
  275.  
  276. align MD5_ALIGN
  277.  
  278. md5._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
  279.  
  280.