Subversion Repositories Kolibri OS

Rev

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

  1. ;    libcrash -- cryptographic hash functions
  2. ;
  3. ;    Copyright (C) 2012-2013 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. format MS COFF
  19.  
  20. public @EXPORT as 'EXPORTS'
  21.  
  22. include '../../../../struct.inc'
  23. include '../../../../proc32.inc'
  24. include '../../../../macros.inc'
  25. include '../../../../config.inc'
  26. ;include '../../../../debug.inc'
  27.  
  28. purge section,mov,add,sub
  29. section '.flat' code readable align 16
  30.  
  31.  
  32. include 'libcrash.inc'
  33. include 'md4.asm'
  34. include 'md5.asm'
  35. include 'sha1.asm'
  36. include 'sha224_256.asm'
  37. include 'sha384_512.asm'
  38.  
  39.  
  40. proc lib_init
  41.         ret
  42. endp
  43.  
  44.  
  45. proc crash.hash  _hid, _hash, _data, _len, _callback, _msglen
  46. locals
  47.         hash_func       rd      1
  48.         final           rd      1
  49.         hi              rd      1
  50. endl
  51.         mov     eax, [_hid]
  52.         imul    eax, sizeof.crash_item
  53.         lea     eax, [crash._.table + eax]
  54.         mov     [hi], eax
  55.  
  56.         mov     eax, [hi]
  57.         mov     edx, [eax + crash_item.function]
  58.         mov     esi, [eax + crash_item.init_val]
  59.         mov     edi, [_hash]
  60.         mov     [hash_func], edx
  61.         mov     ecx, [hi]
  62.         mov     ecx, [ecx + crash_item.len_in]
  63.         rep     movsd
  64.  
  65.         mov     [final], 0
  66.   .first:
  67.         mov     eax, [_msglen]
  68.         mov     ecx, [_len]
  69.         add     [eax], ecx
  70.         mov     esi, [_data]
  71.         test    ecx, ecx
  72.         jz      .callback
  73.   .begin:
  74.         mov     eax, [hi]
  75.         mov     eax, [eax + crash_item.len_blk]
  76.         sub     [_len], eax
  77.         jnc     @f
  78.         add     [_len], eax
  79.         jmp     .endofblock
  80.     @@:
  81.         stdcall [hash_func], [_hash], [_data]
  82.         jmp     .begin
  83.   .endofblock:
  84.         cmp     [final], 1
  85.         je      .quit
  86.  
  87.   .callback:
  88.         call    [_callback]
  89.         test    eax, eax
  90.         jz      @f
  91.         mov     [_len], eax
  92.         jmp     .first
  93.     @@:
  94.  
  95.         mov     edi, [_data]
  96.         mov     ecx, [_len]
  97.         rep     movsb
  98.         mov     eax, [_msglen]
  99.         mov     eax, [eax]
  100.         mov     edx, [hi]
  101.         mov     edx, [edx + crash_item.len_blk]
  102.         sub     edx, 1
  103.         and     eax, edx
  104.         mov     edx, [hi]
  105.         mov     ecx, [edx + crash_item.len_blk]
  106.         sub     ecx, [edx + crash_item.len_size]
  107.         sub     ecx, eax
  108.         ja      @f
  109.         add     ecx, [edx + crash_item.len_blk]
  110.     @@:
  111.         add     [_len], ecx
  112.         mov     eax, [hi]
  113.         mov     byte[edi], 0x80
  114.         add     edi, 1
  115.         sub     ecx, 1
  116.         mov     al, 0
  117.         rep     stosb
  118.         push    ecx
  119.         xor     eax, eax
  120.         mov     ecx, [hi]
  121.         mov     ecx, [ecx + crash_item.len_size]
  122.         sub     ecx, 8                  ; FIXME for > 2^64 input length
  123.         shr     ecx, 2
  124.         rep     stosd
  125.         pop     ecx
  126.         mov     eax, [_msglen]
  127.         mov     eax, [eax]
  128.         mov     edx, 8
  129.         mul     edx
  130.         mov     ecx, [hi]
  131.         cmp     [ecx + crash_item.endianness], LIBCRASH_ENDIAN_BIG
  132.         jne     @f
  133.         bswap   eax
  134.         bswap   edx
  135.         xchg    eax, edx
  136.     @@:
  137.         mov     dword[edi], eax
  138.         mov     dword[edi + 4], edx
  139.         mov     ecx, [hi]
  140.         mov     eax, [ecx + crash_item.len_size]
  141.         add     [_len], eax
  142.         mov     [final], 1
  143.         jmp     .first
  144.   .quit:
  145.         mov     eax, [hi]
  146.         stdcall [eax + crash_item.postproc], [eax + crash_item.len_out], [_hash]
  147.         ret
  148. endp
  149.  
  150.  
  151. proc crash._.md4_md5_postprocess _len_out, _hash
  152.         ret
  153. endp
  154.  
  155.  
  156. proc crash._.sha1_224_256_postprocess _len_out, _hash
  157.         mov     ecx, [_len_out]
  158.         mov     esi, [_hash]
  159.         mov     edi, esi
  160.     @@:
  161.         lodsd
  162.         bswap   eax
  163.         stosd
  164.         dec     ecx
  165.         jnz     @b
  166.         ret
  167. endp
  168.  
  169.  
  170. proc crash._.sha384_512_postprocess _len_out, _hash
  171.         mov     ecx, [_len_out]
  172.         mov     esi, [_hash]
  173.         mov     edi, esi
  174.     @@:
  175.         lodsd
  176.         mov     ebx, eax
  177.         lodsd
  178.         bswap   eax
  179.         bswap   ebx
  180.         stosd
  181.         mov     eax, ebx
  182.         stosd
  183.         dec     ecx
  184.         jnz     @b
  185.         emms
  186.         ret
  187. endp
  188.  
  189.  
  190. proc crash.bin2hex _bin, _hex, _hid     ; FIXME _hid param?
  191.         mov     eax, [_hid]
  192.         imul    eax, sizeof.crash_item
  193.         mov     ecx, [crash._.table + eax + crash_item.len_out]
  194.         mov     ebx, crash._.bin2hex_table
  195.         mov     esi, [_bin]
  196.         mov     edi, [_hex]
  197.         shl     ecx, 2
  198.   .next_byte:
  199.         xor     eax, eax
  200.         lodsb
  201.         shl     eax, 4
  202.         shr     al, 4
  203.         xlatb
  204.         xchg    al, ah
  205.         xlatb
  206.         stosw
  207.         dec     ecx
  208.         jnz     .next_byte
  209.         xor     al, al
  210.         stosb
  211.         ret
  212. endp
  213.  
  214.  
  215. crash._.bin2hex_table   db      '0123456789abcdef'
  216.  
  217. ; see crash_item struct for details
  218. crash._.table           dd      crash.md4,      crash._.md4_init,    crash._.md4_md5_postprocess,       4,  4,  64,  8, 0
  219.                         dd      crash.md5,      crash._.md5_init,    crash._.md4_md5_postprocess,       4,  4,  64,  8, 0
  220.                         dd      crash.sha1,     crash._.sha1_init,   crash._.sha1_224_256_postprocess,  5,  5,  64,  8, 1
  221.                         dd      crash.sha256,   crash._.sha224_init, crash._.sha1_224_256_postprocess,  8,  7,  64,  8, 1
  222.                         dd      crash.sha256,   crash._.sha256_init, crash._.sha1_224_256_postprocess,  8,  8,  64,  8, 1
  223.                         dd      crash.sha512,   crash._.sha384_init, crash._.sha384_512_postprocess, 16, 12, 128, 16, 1
  224.                         dd      crash.sha512,   crash._.sha512_init, crash._.sha384_512_postprocess, 16, 16, 128, 16, 1
  225.  
  226. crash._.crc_init        dd      0xffffffff
  227.  
  228. crash._.md4_init:
  229. crash._.md5_init:
  230. crash._.sha1_init       dd      0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
  231.  
  232. crash._.sha224_init     dd      0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
  233.  
  234. crash._.sha256_init     dd      0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  235.  
  236. crash._.sha384_init     dq      0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,\
  237.                                 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4
  238.  
  239. crash._.sha512_init     dq      0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,\
  240.                                 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
  241.  
  242. crash._.sha256_table    dd      0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
  243.                                 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
  244.                                 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
  245.                                 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
  246.                                 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
  247.                                 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
  248.                                 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
  249.                                 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  250.  
  251. crash._.sha512_table    dq      0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,\
  252.                                 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,\
  253.                                 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,\
  254.                                 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,\
  255.                                 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,\
  256.                                 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,\
  257.                                 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,\
  258.                                 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,\
  259.                                 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,\
  260.                                 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,\
  261.                                 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,\
  262.                                 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,\
  263.                                 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,\
  264.                                 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,\
  265.                                 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,\
  266.                                 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,\
  267.                                 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\
  268.                                 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,\
  269.                                 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\
  270.                                 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
  271.  
  272.  
  273. align 4
  274. @EXPORT:
  275.  
  276. export                                   \
  277.     lib_init        , 'lib_init'       , \
  278.     crash.hash      , 'crash_hash'     , \
  279.     crash.bin2hex   , 'crash_bin2hex'
  280.  
  281. section '.data' data readable writable align 16
  282. crash._.crc32_table     rd      256
  283.