Subversion Repositories Kolibri OS

Rev

Rev 3115 | 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.         movq    mm0, [y]
  21.         pxor    mm0, [z]
  22.         pand    mm0, [x]
  23.         pxor    mm0, [z]
  24. }
  25.  
  26. macro maj x, y, z
  27. {
  28.         movq    mm0, [x]
  29.         pxor    mm0, [y]
  30.         pand    mm0, [z]
  31.         movq    mm2, [x]
  32.         pand    mm2, [y]
  33.         pxor    mm0, mm2
  34. }
  35.  
  36. macro Sigma0 x
  37. {
  38.         movq    mm0, x
  39.         movq    mm2, mm0
  40.         movq    mm7, mm2
  41.         psrlq   mm2, 28
  42.         psllq   mm7, 36
  43.         por     mm2, mm7
  44.         movq    mm7, mm0
  45.         psrlq   mm0, 34
  46.         psllq   mm7, 30
  47.         por     mm0, mm7
  48.         pxor    mm0, mm2
  49.         movq    mm2, x
  50.         movq    mm7, mm2
  51.         psrlq   mm2, 39
  52.         psllq   mm7, 25
  53.         por     mm2, mm7
  54.         pxor    mm0, mm2
  55. }
  56.  
  57. macro Sigma1 x
  58. {
  59.         movq    mm0, x
  60.         movq    mm2, mm0
  61.         movq    mm7, mm2
  62.         psrlq   mm2, 14
  63.         psllq   mm7, 50
  64.         por     mm2, mm7
  65.         movq    mm7, mm0
  66.         psrlq   mm0, 18
  67.         psllq   mm7, 46
  68.         por     mm0, mm7
  69.         pxor    mm0, mm2
  70.         movq    mm2, x
  71.         movq    mm7, mm2
  72.         psrlq   mm2, 41
  73.         psllq   mm7, 23
  74.         por     mm2, mm7
  75.         pxor    mm0, mm2
  76. }
  77.  
  78. macro sigma0 x
  79. {
  80.         movq    mm0, x
  81.         movq    mm2, mm0
  82.         movq    mm7, mm2
  83.         psrlq   mm2, 1
  84.         psllq   mm7, 63
  85.         por     mm2, mm7
  86.         movq    mm7, mm0
  87.         psrlq   mm0, 8
  88.         psllq   mm7, 56
  89.         por     mm0, mm7
  90.         pxor    mm0, mm2
  91.         movq    mm2, x
  92.         psrlq   mm2, 7
  93.         pxor    mm0, mm2
  94. }
  95.  
  96. macro sigma1 x
  97. {
  98.         movq    mm0, x
  99.         movq    mm2, mm0
  100.         movq    mm7, mm2
  101.         psrlq   mm2, 19
  102.         psllq   mm7, 45
  103.         por     mm2, mm7
  104.         movq    mm7, mm0
  105.         psrlq   mm0, 61
  106.         psllq   mm7, 3
  107.         por     mm0, mm7
  108.         pxor    mm0, mm2
  109.         movq    mm2, x
  110.         psrlq   mm2, 6
  111.         pxor    mm0, mm2
  112. }
  113.  
  114. macro recalculate_w n
  115. {
  116.         movq    mm3, [w + ((n-2) and 15)*8]
  117.         sigma1  mm3
  118.         paddq   mm0, [w + ((n-7) and 15)*8]
  119.         movq    mm6, mm0
  120.         movq    mm3, [w + ((n-15) and 15)*8]
  121.         sigma0  mm3
  122.         movq    mm2, mm6
  123.         paddq   mm0, mm2
  124.         movq    mm7, [w + (n)*8]
  125.         paddq   mm7, mm0
  126.         movq    [w + (n)*8], mm7
  127. }
  128.  
  129. macro crash.sha512.round a, b, c, d, e, f, g, h, k
  130. {
  131.         movq    mm1, [h]
  132.         movq    mm3, [e]
  133.         Sigma1  mm3
  134.         paddq   mm1, mm0
  135.         chn     e, f, g
  136.         paddq   mm1, mm0
  137.         paddq   mm1, [k]
  138.         paddq   mm1, mm5
  139.         movq    mm7, [d]
  140.         paddq   mm7, mm1
  141.         movq    [d], mm7
  142.         movq    mm3, [a]
  143.         Sigma0  mm3
  144.         paddq   mm1, mm0
  145.         maj     a, b, c
  146.         paddq   mm0, mm1
  147.         movq    [h], mm0
  148. }
  149.  
  150.  
  151. macro crash.sha512.round_1_16 a, b, c, d, e, f, g, h, n
  152. {
  153.  
  154.         movq    mm0, [esi + (n)*8]
  155.         movq    [temp], mm0
  156.         mov     eax, dword[temp]
  157.         bswap   eax
  158.         push    eax
  159.         mov     eax, dword[temp + 4]
  160.         bswap   eax
  161.         mov     dword[temp], eax
  162.         pop     eax
  163.         mov     dword[temp + 4], eax
  164.         movq    mm0, [temp]
  165.         movq    [w + (n)*8], mm0
  166.         movq    mm5, mm0
  167.         crash.sha512.round a, b, c, d, e, f, g, h, (crash._.sha512_table + (n)*8)
  168. }
  169.  
  170. macro crash.sha512.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
  171. {
  172.         recalculate_w n
  173.         movq    mm5, [w + (n)*8]
  174.         crash.sha512.round a, b, c, d, e, f, g, h, (crash._.sha512_table + (n+16*rep_num)*8)
  175. }
  176.  
  177.  
  178. proc crash.sha512 _sha512, _data
  179. locals
  180.         w       rq 80
  181.         A       rq 1
  182.         B       rq 1
  183.         C       rq 1
  184.         D       rq 1
  185.         E       rq 1
  186.         F       rq 1
  187.         G       rq 1
  188.         H       rq 1
  189.         temp    rq 1
  190. endl
  191.         mov     edi, [_sha512]
  192.         movq    mm0, [edi + 0x00]
  193.         movq    [A], mm0
  194.         movq    mm0, [edi + 0x08]
  195.         movq    [B], mm0
  196.         movq    mm0, [edi + 0x10]
  197.         movq    [C], mm0
  198.         movq    mm0, [edi + 0x18]
  199.         movq    [D], mm0
  200.         movq    mm0, [edi + 0x20]
  201.         movq    [E], mm0
  202.         movq    mm0, [edi + 0x28]
  203.         movq    [F], mm0
  204.         movq    mm0, [edi + 0x30]
  205.         movq    [G], mm0
  206.         movq    mm0, [edi + 0x38]
  207.         movq    [H], mm0
  208.  
  209.  
  210.         crash.sha512.round_1_16         A, B, C, D, E, F, G, H,  0
  211.         crash.sha512.round_1_16         H, A, B, C, D, E, F, G,  1
  212.         crash.sha512.round_1_16         G, H, A, B, C, D, E, F,  2
  213.         crash.sha512.round_1_16         F, G, H, A, B, C, D, E,  3
  214.         crash.sha512.round_1_16         E, F, G, H, A, B, C, D,  4
  215.         crash.sha512.round_1_16         D, E, F, G, H, A, B, C,  5
  216.         crash.sha512.round_1_16         C, D, E, F, G, H, A, B,  6
  217.         crash.sha512.round_1_16         B, C, D, E, F, G, H, A,  7
  218.         crash.sha512.round_1_16         A, B, C, D, E, F, G, H,  8
  219.         crash.sha512.round_1_16         H, A, B, C, D, E, F, G,  9
  220.         crash.sha512.round_1_16         G, H, A, B, C, D, E, F, 10
  221.         crash.sha512.round_1_16         F, G, H, A, B, C, D, E, 11
  222.         crash.sha512.round_1_16         E, F, G, H, A, B, C, D, 12
  223.         crash.sha512.round_1_16         D, E, F, G, H, A, B, C, 13
  224.         crash.sha512.round_1_16         C, D, E, F, G, H, A, B, 14
  225.         crash.sha512.round_1_16         B, C, D, E, F, G, H, A, 15
  226.  
  227. repeat 4
  228.         crash.sha512.round_17_64        A, B, C, D, E, F, G, H,  0, %
  229.         crash.sha512.round_17_64        H, A, B, C, D, E, F, G,  1, %
  230.         crash.sha512.round_17_64        G, H, A, B, C, D, E, F,  2, %
  231.         crash.sha512.round_17_64        F, G, H, A, B, C, D, E,  3, %
  232.         crash.sha512.round_17_64        E, F, G, H, A, B, C, D,  4, %
  233.         crash.sha512.round_17_64        D, E, F, G, H, A, B, C,  5, %
  234.         crash.sha512.round_17_64        C, D, E, F, G, H, A, B,  6, %
  235.         crash.sha512.round_17_64        B, C, D, E, F, G, H, A,  7, %
  236.         crash.sha512.round_17_64        A, B, C, D, E, F, G, H,  8, %
  237.         crash.sha512.round_17_64        H, A, B, C, D, E, F, G,  9, %
  238.         crash.sha512.round_17_64        G, H, A, B, C, D, E, F, 10, %
  239.         crash.sha512.round_17_64        F, G, H, A, B, C, D, E, 11, %
  240.         crash.sha512.round_17_64        E, F, G, H, A, B, C, D, 12, %
  241.         crash.sha512.round_17_64        D, E, F, G, H, A, B, C, 13, %
  242.         crash.sha512.round_17_64        C, D, E, F, G, H, A, B, 14, %
  243.         crash.sha512.round_17_64        B, C, D, E, F, G, H, A, 15, %
  244. end repeat
  245.  
  246.  
  247.         mov     edi, [_sha512]
  248.         movq    mm0, [A]
  249.         paddq   mm0, [edi + 0x00]
  250.         movq    [edi + 0x00], mm0
  251.         movq    mm0, [B]
  252.         paddq   mm0, [edi + 0x08]
  253.         movq    [edi + 0x08], mm0
  254.         movq    mm0, [C]
  255.         paddq   mm0, [edi + 0x10]
  256.         movq    [edi + 0x10], mm0
  257.         movq    mm0, [D]
  258.         paddq   mm0, [edi + 0x18]
  259.         movq    [edi + 0x18], mm0
  260.         movq    mm0, [E]
  261.         paddq   mm0, [edi + 0x20]
  262.         movq    [edi + 0x20], mm0
  263.         movq    mm0, [F]
  264.         paddq   mm0, [edi + 0x28]
  265.         movq    [edi + 0x28], mm0
  266.         movq    mm0, [G]
  267.         paddq   mm0, [edi + 0x30]
  268.         movq    [edi + 0x30], mm0
  269.         movq    mm0, [H]
  270.         paddq   mm0, [edi + 0x38]
  271.         movq    [edi + 0x38], mm0
  272.         add     esi, 128
  273.  
  274.         ret
  275. endp
  276.  
  277.