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. macro chn x, y, z
  19. {
  20.         mov     eax, [y]
  21.         xor     eax, [z]
  22.         and     eax, [x]
  23.         xor     eax, [z]
  24. }
  25.  
  26. macro maj x, y, z
  27. {
  28.         mov     eax, [x]
  29.         xor     eax, [y]
  30.         and     eax, [z]
  31.         mov     ecx, [x]
  32.         and     ecx, [y]
  33.         xor     eax, ecx
  34. }
  35.  
  36. macro Sigma0 x
  37. {
  38.         mov     eax, x
  39.         mov     ecx, eax
  40.         ror     ecx, 2
  41.         ror     eax, 13
  42.         xor     eax, ecx
  43.         mov     ecx, x
  44.         ror     ecx, 22
  45.         xor     eax, ecx
  46. }
  47.  
  48. macro Sigma1 x
  49. {
  50.         mov     eax, x
  51.         mov     ecx, eax
  52.         ror     ecx, 6
  53.         ror     eax, 11
  54.         xor     eax, ecx
  55.         mov     ecx, x
  56.         ror     ecx, 25
  57.         xor     eax, ecx
  58. }
  59.  
  60. macro sigma0 x
  61. {
  62.         mov     eax, x
  63.         mov     ecx, eax
  64.         ror     ecx, 7
  65.         ror     eax, 18
  66.         xor     eax, ecx
  67.         mov     ecx, x
  68.         shr     ecx, 3
  69.         xor     eax, ecx
  70. }
  71.  
  72. macro sigma1 x
  73. {
  74.         mov     eax, x
  75.         mov     ecx, eax
  76.         ror     ecx, 17
  77.         ror     eax, 19
  78.         xor     eax, ecx
  79.         mov     ecx, x
  80.         shr     ecx, 10
  81.         xor     eax, ecx
  82. }
  83.  
  84. macro recalculate_w n
  85. {
  86.         mov     edx, [w + ((n-2) and 15)*4]
  87.         sigma1  edx
  88.         add     eax, [w + ((n-7) and 15)*4]
  89.         push    eax
  90.         mov     edx, [w + ((n-15) and 15)*4]
  91.         sigma0  edx
  92.         pop     ecx
  93.         add     eax, ecx
  94.         add     [w + (n)*4], eax
  95. }
  96.  
  97. macro crash.sha256.round a, b, c, d, e, f, g, h, k
  98. {
  99.         mov     ebx, [h]
  100.         mov     edx, [e]
  101.         Sigma1  edx
  102.  
  103.         add     ebx, eax
  104.         chn     e, f, g
  105.  
  106.         add     ebx, eax
  107.         add     ebx, [k]
  108.         add     ebx, edi
  109.  
  110.         add     [d], ebx
  111.  
  112.         mov     edx, [a]
  113.         Sigma0  edx
  114.         add     ebx, eax
  115.         maj     a, b, c
  116.         add     eax, ebx
  117.         mov     [h], eax
  118. }
  119.  
  120.  
  121. macro crash.sha256.round_1_16 a, b, c, d, e, f, g, h, n
  122. {
  123.  
  124.         mov     eax, [esi + (n)*4]
  125.         bswap   eax
  126.  
  127.         mov     dword[w + (n)*4], eax
  128.         mov     edi, eax
  129.         crash.sha256.round a, b, c, d, e, f, g, h, (crash._.sha256_table + (n)*4)
  130. }
  131.  
  132. macro crash.sha256.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
  133. {
  134.         recalculate_w n
  135.         mov     edi, [w + (n)*4]
  136.         crash.sha256.round a, b, c, d, e, f, g, h, (crash._.sha256_table + (n+16*rep_num)*4)
  137. }
  138.  
  139.  
  140. proc crash.sha256 _sha256, _data
  141. locals
  142.         w       rd 64
  143.         A       rd 1
  144.         B       rd 1
  145.         C       rd 1
  146.         D       rd 1
  147.         E       rd 1
  148.         F       rd 1
  149.         G       rd 1
  150.         H       rd 1
  151. endl
  152.         mov     edi, [_sha256]
  153.         mov     eax, [edi + 0x00]
  154.         mov     [A], eax
  155.         mov     eax, [edi + 0x04]
  156.         mov     [B], eax
  157.         mov     eax, [edi + 0x08]
  158.         mov     [C], eax
  159.         mov     eax, [edi + 0x0c]
  160.         mov     [D], eax
  161.         mov     eax, [edi + 0x10]
  162.         mov     [E], eax
  163.         mov     eax, [edi + 0x14]
  164.         mov     [F], eax
  165.         mov     eax, [edi + 0x18]
  166.         mov     [G], eax
  167.         mov     eax, [edi + 0x1c]
  168.         mov     [H], eax
  169.  
  170.         crash.sha256.round_1_16         A, B, C, D, E, F, G, H,  0
  171.         crash.sha256.round_1_16         H, A, B, C, D, E, F, G,  1
  172.         crash.sha256.round_1_16         G, H, A, B, C, D, E, F,  2
  173.         crash.sha256.round_1_16         F, G, H, A, B, C, D, E,  3
  174.         crash.sha256.round_1_16         E, F, G, H, A, B, C, D,  4
  175.         crash.sha256.round_1_16         D, E, F, G, H, A, B, C,  5
  176.         crash.sha256.round_1_16         C, D, E, F, G, H, A, B,  6
  177.         crash.sha256.round_1_16         B, C, D, E, F, G, H, A,  7
  178.         crash.sha256.round_1_16         A, B, C, D, E, F, G, H,  8
  179.         crash.sha256.round_1_16         H, A, B, C, D, E, F, G,  9
  180.         crash.sha256.round_1_16         G, H, A, B, C, D, E, F, 10
  181.         crash.sha256.round_1_16         F, G, H, A, B, C, D, E, 11
  182.         crash.sha256.round_1_16         E, F, G, H, A, B, C, D, 12
  183.         crash.sha256.round_1_16         D, E, F, G, H, A, B, C, 13
  184.         crash.sha256.round_1_16         C, D, E, F, G, H, A, B, 14
  185.         crash.sha256.round_1_16         B, C, D, E, F, G, H, A, 15
  186.  
  187. repeat 3
  188.         crash.sha256.round_17_64        A, B, C, D, E, F, G, H,  0, %
  189.         crash.sha256.round_17_64        H, A, B, C, D, E, F, G,  1, %
  190.         crash.sha256.round_17_64        G, H, A, B, C, D, E, F,  2, %
  191.         crash.sha256.round_17_64        F, G, H, A, B, C, D, E,  3, %
  192.         crash.sha256.round_17_64        E, F, G, H, A, B, C, D,  4, %
  193.         crash.sha256.round_17_64        D, E, F, G, H, A, B, C,  5, %
  194.         crash.sha256.round_17_64        C, D, E, F, G, H, A, B,  6, %
  195.         crash.sha256.round_17_64        B, C, D, E, F, G, H, A,  7, %
  196.         crash.sha256.round_17_64        A, B, C, D, E, F, G, H,  8, %
  197.         crash.sha256.round_17_64        H, A, B, C, D, E, F, G,  9, %
  198.         crash.sha256.round_17_64        G, H, A, B, C, D, E, F, 10, %
  199.         crash.sha256.round_17_64        F, G, H, A, B, C, D, E, 11, %
  200.         crash.sha256.round_17_64        E, F, G, H, A, B, C, D, 12, %
  201.         crash.sha256.round_17_64        D, E, F, G, H, A, B, C, 13, %
  202.         crash.sha256.round_17_64        C, D, E, F, G, H, A, B, 14, %
  203.         crash.sha256.round_17_64        B, C, D, E, F, G, H, A, 15, %
  204. end repeat
  205.  
  206.         mov     edi, [_sha256]
  207.         mov     eax, [A]
  208.         add     [edi + 0x00], eax
  209.         mov     eax, [B]
  210.         add     [edi + 0x04], eax
  211.         mov     eax, [C]
  212.         add     [edi + 0x08], eax
  213.         mov     eax, [D]
  214.         add     [edi + 0x0c], eax
  215.         mov     eax, [E]
  216.         add     [edi + 0x10], eax
  217.         mov     eax, [F]
  218.         add     [edi + 0x14], eax
  219.         mov     eax, [G]
  220.         add     [edi + 0x18], eax
  221.         mov     eax, [H]
  222.         add     [edi + 0x1c], eax
  223.         add     esi, 64
  224.  
  225.         ret
  226. endp
  227.  
  228.