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. SHA224256_BLOCK_SIZE = 64
  20. SHA224256_INIT_SIZE  = 32
  21. SHA224_HASH_SIZE     = 28
  22. SHA256_HASH_SIZE     = 32
  23. SHA224256_ALIGN      = 4
  24. SHA224256_ALIGN_MASK = SHA224256_ALIGN - 1
  25.  
  26. struct ctx_sha224256
  27.         hash            rb SHA224256_INIT_SIZE
  28.         block           rb SHA224256_BLOCK_SIZE
  29.         index           rd 1
  30.         msglen_0        rd 1
  31.         msglen_1        rd 1
  32. ends
  33.  
  34.  
  35. macro sha224256._.chn x, y, z
  36. {
  37.         mov     eax, [y]
  38.         xor     eax, [z]
  39.         and     eax, [x]
  40.         xor     eax, [z]
  41. }
  42.  
  43. macro sha224256._.maj x, y, z
  44. {
  45.         mov     eax, [x]
  46.         xor     eax, [y]
  47.         and     eax, [z]
  48.         mov     ecx, [x]
  49.         and     ecx, [y]
  50.         xor     eax, ecx
  51. }
  52.  
  53. macro sha224256._.Sigma0 x
  54. {
  55.         mov     eax, x
  56.         mov     ecx, eax
  57.         ror     ecx, 2
  58.         ror     eax, 13
  59.         xor     eax, ecx
  60.         mov     ecx, x
  61.         ror     ecx, 22
  62.         xor     eax, ecx
  63. }
  64.  
  65. macro sha224256._.Sigma1 x
  66. {
  67.         mov     eax, x
  68.         mov     ecx, eax
  69.         ror     ecx, 6
  70.         ror     eax, 11
  71.         xor     eax, ecx
  72.         mov     ecx, x
  73.         ror     ecx, 25
  74.         xor     eax, ecx
  75. }
  76.  
  77. macro sha224256._.sigma0 x
  78. {
  79.         mov     eax, x
  80.         mov     ecx, eax
  81.         ror     ecx, 7
  82.         ror     eax, 18
  83.         xor     eax, ecx
  84.         mov     ecx, x
  85.         shr     ecx, 3
  86.         xor     eax, ecx
  87. }
  88.  
  89. macro sha224256._.sigma1 x
  90. {
  91.         mov     eax, x
  92.         mov     ecx, eax
  93.         ror     ecx, 17
  94.         ror     eax, 19
  95.         xor     eax, ecx
  96.         mov     ecx, x
  97.         shr     ecx, 10
  98.         xor     eax, ecx
  99. }
  100.  
  101. macro sha224256._.recalculate_w n
  102. {
  103.         mov     edx, [w + ((n-2) and 15)*4]
  104.         sha224256._.sigma1  edx
  105.         add     eax, [w + ((n-7) and 15)*4]
  106.         push    eax
  107.         mov     edx, [w + ((n-15) and 15)*4]
  108.         sha224256._.sigma0  edx
  109.         pop     ecx
  110.         add     eax, ecx
  111.         add     [w + (n)*4], eax
  112. }
  113.  
  114. macro sha224256._.round a, b, c, d, e, f, g, h, k
  115. {
  116.         mov     ebx, [h]
  117.         mov     edx, [e]
  118.         sha224256._.Sigma1  edx
  119.  
  120.         add     ebx, eax
  121.         sha224256._.chn     e, f, g
  122.  
  123.         add     ebx, eax
  124.         add     ebx, [k]
  125.         add     ebx, edi
  126.  
  127.         add     [d], ebx
  128.  
  129.         mov     edx, [a]
  130.         sha224256._.Sigma0  edx
  131.         add     ebx, eax
  132.         sha224256._.maj     a, b, c
  133.         add     eax, ebx
  134.         mov     [h], eax
  135. }
  136.  
  137.  
  138. macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n
  139. {
  140.  
  141.         mov     eax, [esi + (n)*4]
  142.         bswap   eax
  143.  
  144.         mov     dword[w + (n)*4], eax
  145.         mov     edi, eax
  146.         sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4)
  147. }
  148.  
  149. macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
  150. {
  151.         sha224256._.recalculate_w n
  152.         mov     edi, [w + (n)*4]
  153.         sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n+16*rep_num)*4)
  154. }
  155.  
  156.  
  157. proc sha224.init _ctx
  158.         mov     ebx, [_ctx]
  159.         lea     edi, [ebx + ctx_sha224256.hash]
  160.         mov     esi, sha224._.hash_init
  161.         mov     ecx, SHA224256_INIT_SIZE/4
  162.         rep     movsd
  163.         xor     eax, eax
  164.         mov     [ebx + ctx_sha224256.index], eax
  165.         mov     [ebx + ctx_sha224256.msglen_0], eax
  166.         mov     [ebx + ctx_sha224256.msglen_1], eax
  167.         ret
  168. endp
  169.  
  170.  
  171. proc sha256.init _ctx
  172.         mov     ebx, [_ctx]
  173.         lea     edi, [ebx + ctx_sha224256.hash]
  174.         mov     esi, sha256._.hash_init
  175.         mov     ecx, SHA224256_INIT_SIZE/4
  176.         rep     movsd
  177.         xor     eax, eax
  178.         mov     [ebx + ctx_sha224256.index], eax
  179.         mov     [ebx + ctx_sha224256.msglen_0], eax
  180.         mov     [ebx + ctx_sha224256.msglen_1], eax
  181.         ret
  182. endp
  183.  
  184.  
  185. proc sha224256._.block _hash
  186. locals
  187.         w       rd 64
  188.         A       rd 1
  189.         B       rd 1
  190.         C       rd 1
  191.         D       rd 1
  192.         E       rd 1
  193.         F       rd 1
  194.         G       rd 1
  195.         H       rd 1
  196. endl
  197.         mov     edi, [_hash]
  198.         mov     eax, [edi + 0x00]
  199.         mov     [A], eax
  200.         mov     eax, [edi + 0x04]
  201.         mov     [B], eax
  202.         mov     eax, [edi + 0x08]
  203.         mov     [C], eax
  204.         mov     eax, [edi + 0x0c]
  205.         mov     [D], eax
  206.         mov     eax, [edi + 0x10]
  207.         mov     [E], eax
  208.         mov     eax, [edi + 0x14]
  209.         mov     [F], eax
  210.         mov     eax, [edi + 0x18]
  211.         mov     [G], eax
  212.         mov     eax, [edi + 0x1c]
  213.         mov     [H], eax
  214.  
  215.         sha224256._.round_1_16  A, B, C, D, E, F, G, H,  0
  216.         sha224256._.round_1_16  H, A, B, C, D, E, F, G,  1
  217.         sha224256._.round_1_16  G, H, A, B, C, D, E, F,  2
  218.         sha224256._.round_1_16  F, G, H, A, B, C, D, E,  3
  219.         sha224256._.round_1_16  E, F, G, H, A, B, C, D,  4
  220.         sha224256._.round_1_16  D, E, F, G, H, A, B, C,  5
  221.         sha224256._.round_1_16  C, D, E, F, G, H, A, B,  6
  222.         sha224256._.round_1_16  B, C, D, E, F, G, H, A,  7
  223.         sha224256._.round_1_16  A, B, C, D, E, F, G, H,  8
  224.         sha224256._.round_1_16  H, A, B, C, D, E, F, G,  9
  225.         sha224256._.round_1_16  G, H, A, B, C, D, E, F, 10
  226.         sha224256._.round_1_16  F, G, H, A, B, C, D, E, 11
  227.         sha224256._.round_1_16  E, F, G, H, A, B, C, D, 12
  228.         sha224256._.round_1_16  D, E, F, G, H, A, B, C, 13
  229.         sha224256._.round_1_16  C, D, E, F, G, H, A, B, 14
  230.         sha224256._.round_1_16  B, C, D, E, F, G, H, A, 15
  231.  
  232. repeat 3
  233.         sha224256._.round_17_64 A, B, C, D, E, F, G, H,  0, %
  234.         sha224256._.round_17_64 H, A, B, C, D, E, F, G,  1, %
  235.         sha224256._.round_17_64 G, H, A, B, C, D, E, F,  2, %
  236.         sha224256._.round_17_64 F, G, H, A, B, C, D, E,  3, %
  237.         sha224256._.round_17_64 E, F, G, H, A, B, C, D,  4, %
  238.         sha224256._.round_17_64 D, E, F, G, H, A, B, C,  5, %
  239.         sha224256._.round_17_64 C, D, E, F, G, H, A, B,  6, %
  240.         sha224256._.round_17_64 B, C, D, E, F, G, H, A,  7, %
  241.         sha224256._.round_17_64 A, B, C, D, E, F, G, H,  8, %
  242.         sha224256._.round_17_64 H, A, B, C, D, E, F, G,  9, %
  243.         sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, %
  244.         sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, %
  245.         sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, %
  246.         sha224256._.round_17_64 D, E, F, G, H, A, B, C, 13, %
  247.         sha224256._.round_17_64 C, D, E, F, G, H, A, B, 14, %
  248.         sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, %
  249. end repeat
  250.  
  251.         mov     edi, [_hash]
  252.         mov     eax, [A]
  253.         add     [edi + 0x00], eax
  254.         mov     eax, [B]
  255.         add     [edi + 0x04], eax
  256.         mov     eax, [C]
  257.         add     [edi + 0x08], eax
  258.         mov     eax, [D]
  259.         add     [edi + 0x0c], eax
  260.         mov     eax, [E]
  261.         add     [edi + 0x10], eax
  262.         mov     eax, [F]
  263.         add     [edi + 0x14], eax
  264.         mov     eax, [G]
  265.         add     [edi + 0x18], eax
  266.         mov     eax, [H]
  267.         add     [edi + 0x1c], eax
  268.  
  269.         ret
  270. endp
  271.  
  272.  
  273. proc sha224256.update _ctx, _msg, _size
  274.         mov     ebx, [_ctx]
  275.         mov     ecx, [_size]
  276.         add     [ebx + ctx_sha224256.msglen_0], ecx
  277.         adc     [ebx + ctx_sha224256.msglen_1], 0
  278.  
  279.   .next_block:
  280.         mov     ebx, [_ctx]
  281.         mov     esi, [_msg]
  282.         mov     eax, [ebx + ctx_sha224256.index]
  283.         and     eax, SHA224256_BLOCK_SIZE-1
  284.         jnz     .copy_to_buf
  285.         test    esi, SHA224256_ALIGN_MASK
  286.         jnz     .copy_to_buf
  287.   .no_copy:
  288.         ; data is aligned, hash it in place without copying
  289.         mov     ebx, [_ctx]
  290.         cmp     [_size], SHA224256_BLOCK_SIZE
  291.         jb      .copy_quit
  292.         lea     eax, [ebx + ctx_sha224256.hash]
  293.         stdcall sha224256._.block, eax
  294.         sub     [_size], SHA224256_BLOCK_SIZE
  295.         add     esi, SHA224256_BLOCK_SIZE           ; FIXME
  296.         jmp     .no_copy
  297.  
  298.   .copy_to_buf:
  299.         lea     edi, [ebx + ctx_sha224256.block]
  300.         add     edi, eax
  301.         mov     ecx, SHA224256_BLOCK_SIZE
  302.         sub     ecx, eax
  303.         cmp     [_size], ecx
  304.         jb      .copy_quit
  305.         sub     [_size], ecx
  306.         add     [_msg], ecx
  307.         add     [ebx + ctx_sha224256.index], ecx
  308.         rep     movsb
  309.         lea     eax, [ebx + ctx_sha224256.hash]
  310.         lea     esi, [ebx + ctx_sha224256.block]
  311.         stdcall sha224256._.block, eax
  312.         jmp     .next_block
  313.  
  314.   .copy_quit:
  315.         mov     ebx, [_ctx]
  316.         lea     edi, [ebx + ctx_sha224256.block]
  317.         mov     eax, [ebx + ctx_sha224256.index]
  318.         and     eax, SHA224256_BLOCK_SIZE-1
  319.         add     edi, eax
  320.         mov     ecx, [_size]
  321.         add     [ebx + ctx_sha224256.index], ecx
  322.         rep     movsb
  323.   .quit:
  324.  
  325.         ret
  326. endp
  327.  
  328.  
  329. proc sha224256.final _ctx
  330.         mov     ebx, [_ctx]
  331.         lea     edi, [ebx + ctx_sha224256.block]
  332.         mov     ecx, [ebx + ctx_sha224256.msglen_0]
  333.         and     ecx, SHA224256_BLOCK_SIZE-1
  334.         add     edi, ecx
  335.         mov     byte[edi], 0x80
  336.         inc     edi
  337.         neg     ecx
  338.         add     ecx, SHA224256_BLOCK_SIZE
  339.         cmp     ecx, 8
  340.         ja      .last
  341.  
  342.         dec     ecx
  343.         xor     eax, eax
  344.         rep     stosb
  345.         lea     esi, [ebx + ctx_sha224256.block]
  346.         lea     eax, [ebx + ctx_sha224256.hash]
  347.         stdcall sha224256._.block, eax
  348.         mov     ebx, [_ctx]
  349.         lea     edi, [ebx + ctx_sha224256.block]
  350.         mov     ecx, SHA224256_BLOCK_SIZE+1
  351.   .last:
  352.         dec     ecx
  353.         sub     ecx, 8
  354.         xor     eax, eax
  355.         rep     stosb
  356.         mov     eax, [ebx + ctx_sha224256.msglen_0]
  357.         mov     edx, [ebx + ctx_sha224256.msglen_1]
  358.         shld    edx, eax, 3
  359.         shl     eax, 3
  360.         bswap   eax
  361.         bswap   edx
  362.         mov     dword[edi], edx
  363.         mov     dword[edi+4], eax
  364.         lea     esi, [ebx + ctx_sha224256.block]
  365.         lea     eax, [ebx + ctx_sha224256.hash]
  366.         stdcall sha224256._.block, eax
  367.  
  368.         mov     ebx, [_ctx]
  369.         lea     eax, [ebx + ctx_sha224256.hash]
  370.         stdcall sha224256._.postprocess, ebx, eax
  371.  
  372.         ret
  373. endp
  374.  
  375.  
  376. proc sha224256._.postprocess _ctx, _hash
  377.         mov     ecx, 8
  378.         mov     esi, [_hash]
  379.         mov     edi, esi
  380.     @@:
  381.         lodsd
  382.         bswap   eax
  383.         stosd
  384.         dec     ecx
  385.         jnz     @b
  386.         ret
  387. endp
  388.  
  389.  
  390. align SHA224256_ALIGN
  391.  
  392. sha224._.hash_init      dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\
  393.                            0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
  394.  
  395. sha256._.hash_init      dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\
  396.                            0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  397.  
  398. sha256_table            dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
  399.                            0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
  400.                            0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
  401.                            0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
  402.                            0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
  403.                            0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
  404.                            0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
  405.                            0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
  406.                            0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
  407.                            0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
  408.                            0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
  409.                            0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
  410.                            0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
  411.                            0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
  412.                            0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
  413.                            0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  414.  
  415.