Subversion Repositories Kolibri OS

Rev

Rev 6461 | 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. macro sha384512._.chn x, y, z
  20. {
  21.         movq    mm0, [y]
  22.         pxor    mm0, [z]
  23.         pand    mm0, [x]
  24.         pxor    mm0, [z]
  25. }
  26.  
  27. macro sha384512._.maj x, y, z
  28. {
  29.         movq    mm0, [x]
  30.         pxor    mm0, [y]
  31.         pand    mm0, [z]
  32.         movq    mm2, [x]
  33.         pand    mm2, [y]
  34.         pxor    mm0, mm2
  35. }
  36.  
  37. macro sha384512._.Sigma0 x
  38. {
  39.         movq    mm0, x
  40.         movq    mm2, mm0
  41.         movq    mm7, mm2
  42.         psrlq   mm2, 28
  43.         psllq   mm7, 36
  44.         por     mm2, mm7
  45.         movq    mm7, mm0
  46.         psrlq   mm0, 34
  47.         psllq   mm7, 30
  48.         por     mm0, mm7
  49.         pxor    mm0, mm2
  50.         movq    mm2, x
  51.         movq    mm7, mm2
  52.         psrlq   mm2, 39
  53.         psllq   mm7, 25
  54.         por     mm2, mm7
  55.         pxor    mm0, mm2
  56. }
  57.  
  58. macro sha384512._.Sigma1 x
  59. {
  60.         movq    mm0, x
  61.         movq    mm2, mm0
  62.         movq    mm7, mm2
  63.         psrlq   mm2, 14
  64.         psllq   mm7, 50
  65.         por     mm2, mm7
  66.         movq    mm7, mm0
  67.         psrlq   mm0, 18
  68.         psllq   mm7, 46
  69.         por     mm0, mm7
  70.         pxor    mm0, mm2
  71.         movq    mm2, x
  72.         movq    mm7, mm2
  73.         psrlq   mm2, 41
  74.         psllq   mm7, 23
  75.         por     mm2, mm7
  76.         pxor    mm0, mm2
  77. }
  78.  
  79. macro sha384512._.sigma0 x
  80. {
  81.         movq    mm0, x
  82.         movq    mm2, mm0
  83.         movq    mm7, mm2
  84.         psrlq   mm2, 1
  85.         psllq   mm7, 63
  86.         por     mm2, mm7
  87.         movq    mm7, mm0
  88.         psrlq   mm0, 8
  89.         psllq   mm7, 56
  90.         por     mm0, mm7
  91.         pxor    mm0, mm2
  92.         movq    mm2, x
  93.         psrlq   mm2, 7
  94.         pxor    mm0, mm2
  95. }
  96.  
  97. macro sha384512._.sigma1 x
  98. {
  99.         movq    mm0, x
  100.         movq    mm2, mm0
  101.         movq    mm7, mm2
  102.         psrlq   mm2, 19
  103.         psllq   mm7, 45
  104.         por     mm2, mm7
  105.         movq    mm7, mm0
  106.         psrlq   mm0, 61
  107.         psllq   mm7, 3
  108.         por     mm0, mm7
  109.         pxor    mm0, mm2
  110.         movq    mm2, x
  111.         psrlq   mm2, 6
  112.         pxor    mm0, mm2
  113. }
  114.  
  115. macro sha384512._.recalculate_w n
  116. {
  117.         movq    mm3, [w + ((n-2) and 15)*8]
  118.         sha384512._.sigma1  mm3
  119.         paddq   mm0, [w + ((n-7) and 15)*8]
  120.         movq    mm6, mm0
  121.         movq    mm3, [w + ((n-15) and 15)*8]
  122.         sha384512._.sigma0  mm3
  123.         movq    mm2, mm6
  124.         paddq   mm0, mm2
  125.         movq    mm7, [w + (n)*8]
  126.         paddq   mm7, mm0
  127.         movq    [w + (n)*8], mm7
  128. }
  129.  
  130. macro sha384512._.round a, b, c, d, e, f, g, h, k
  131. {
  132.         movq    mm1, [h]
  133.         movq    mm3, [e]
  134.         sha384512._.Sigma1  mm3
  135.         paddq   mm1, mm0
  136.         sha384512._.chn     e, f, g
  137.         paddq   mm1, mm0
  138.         paddq   mm1, [k]
  139.         paddq   mm1, mm5
  140.         movq    mm7, [d]
  141.         paddq   mm7, mm1
  142.         movq    [d], mm7
  143.         movq    mm3, [a]
  144.         sha384512._.Sigma0  mm3
  145.         paddq   mm1, mm0
  146.         sha384512._.maj     a, b, c
  147.         paddq   mm0, mm1
  148.         movq    [h], mm0
  149. }
  150.  
  151.  
  152. macro sha384512._.round_1_16 a, b, c, d, e, f, g, h, n
  153. {
  154.  
  155.         movq    mm0, [esi + (n)*8]
  156.         movq    [temp], mm0
  157.         mov     eax, dword[temp]
  158.         bswap   eax
  159.         push    eax
  160.         mov     eax, dword[temp + 4]
  161.         bswap   eax
  162.         mov     dword[temp], eax
  163.         pop     eax
  164.         mov     dword[temp + 4], eax
  165.         movq    mm0, [temp]
  166.         movq    [w + (n)*8], mm0
  167.         movq    mm5, mm0
  168.         sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n)*8)
  169. }
  170.  
  171. macro sha384512._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
  172. {
  173.         sha384512._.recalculate_w n
  174.         movq    mm5, [w + (n)*8]
  175.         sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n+16*rep_num)*8)
  176. }
  177.  
  178.  
  179. proc sha384.init _ctx
  180.         mov     ebx, [_ctx]
  181.         lea     edi, [ebx + ctx_sha384512.hash]
  182.         mov     esi, sha384._.hash_init
  183.         mov     ecx, SHA384512_INIT_SIZE/4
  184.         rep     movsd
  185.         xor     eax, eax
  186.         mov     [ebx + ctx_sha384512.index], eax
  187.         mov     [ebx + ctx_sha384512.msglen_0], eax
  188.         mov     [ebx + ctx_sha384512.msglen_1], eax
  189.         mov     [ebx + ctx_sha384512.msglen_2], eax
  190.         mov     [ebx + ctx_sha384512.msglen_3], eax
  191.         ret
  192. endp
  193.  
  194.  
  195. proc sha512.init _ctx
  196.         mov     ebx, [_ctx]
  197.         lea     edi, [ebx + ctx_sha384512.hash]
  198.         mov     esi, sha512._.hash_init
  199.         mov     ecx, SHA384512_INIT_SIZE/4
  200.         rep     movsd
  201.         xor     eax, eax
  202.         mov     [ebx + ctx_sha384512.index], eax
  203.         mov     [ebx + ctx_sha384512.msglen_0], eax
  204.         mov     [ebx + ctx_sha384512.msglen_1], eax
  205.         mov     [ebx + ctx_sha384512.msglen_2], eax
  206.         mov     [ebx + ctx_sha384512.msglen_3], eax
  207.         ret
  208. endp
  209.  
  210.  
  211. proc sha384512._.block _hash
  212. ;locals
  213. ;        w       rq 80
  214. ;        A       rq 1
  215. ;        B       rq 1
  216. ;        C       rq 1
  217. ;        D       rq 1
  218. ;        E       rq 1
  219. ;        F       rq 1
  220. ;        G       rq 1
  221. ;        H       rq 1
  222. ;        temp    rq 1
  223. ;endl
  224. w equ ebx + ctx_sha384512.w
  225. A equ ebx + ctx_sha384512.A
  226. B equ ebx + ctx_sha384512.B
  227. C equ ebx + ctx_sha384512.C
  228. D equ ebx + ctx_sha384512.D
  229. E equ ebx + ctx_sha384512.E
  230. F equ ebx + ctx_sha384512.F
  231. G equ ebx + ctx_sha384512.G
  232. H equ ebx + ctx_sha384512.H
  233. temp equ ebx + ctx_sha384512.temp
  234.  
  235.         mov     edi, [_hash]
  236.         movq    mm0, [edi + 0x00]
  237.         movq    [A], mm0
  238.         movq    mm0, [edi + 0x08]
  239.         movq    [B], mm0
  240.         movq    mm0, [edi + 0x10]
  241.         movq    [C], mm0
  242.         movq    mm0, [edi + 0x18]
  243.         movq    [D], mm0
  244.         movq    mm0, [edi + 0x20]
  245.         movq    [E], mm0
  246.         movq    mm0, [edi + 0x28]
  247.         movq    [F], mm0
  248.         movq    mm0, [edi + 0x30]
  249.         movq    [G], mm0
  250.         movq    mm0, [edi + 0x38]
  251.         movq    [H], mm0
  252.  
  253.  
  254.         sha384512._.round_1_16  A, B, C, D, E, F, G, H,  0
  255.         sha384512._.round_1_16  H, A, B, C, D, E, F, G,  1
  256.         sha384512._.round_1_16  G, H, A, B, C, D, E, F,  2
  257.         sha384512._.round_1_16  F, G, H, A, B, C, D, E,  3
  258.         sha384512._.round_1_16  E, F, G, H, A, B, C, D,  4
  259.         sha384512._.round_1_16  D, E, F, G, H, A, B, C,  5
  260.         sha384512._.round_1_16  C, D, E, F, G, H, A, B,  6
  261.         sha384512._.round_1_16  B, C, D, E, F, G, H, A,  7
  262.         sha384512._.round_1_16  A, B, C, D, E, F, G, H,  8
  263.         sha384512._.round_1_16  H, A, B, C, D, E, F, G,  9
  264.         sha384512._.round_1_16  G, H, A, B, C, D, E, F, 10
  265.         sha384512._.round_1_16  F, G, H, A, B, C, D, E, 11
  266.         sha384512._.round_1_16  E, F, G, H, A, B, C, D, 12
  267.         sha384512._.round_1_16  D, E, F, G, H, A, B, C, 13
  268.         sha384512._.round_1_16  C, D, E, F, G, H, A, B, 14
  269.         sha384512._.round_1_16  B, C, D, E, F, G, H, A, 15
  270.  
  271. repeat 4
  272.         sha384512._.round_17_64 A, B, C, D, E, F, G, H,  0, %
  273.         sha384512._.round_17_64 H, A, B, C, D, E, F, G,  1, %
  274.         sha384512._.round_17_64 G, H, A, B, C, D, E, F,  2, %
  275.         sha384512._.round_17_64 F, G, H, A, B, C, D, E,  3, %
  276.         sha384512._.round_17_64 E, F, G, H, A, B, C, D,  4, %
  277.         sha384512._.round_17_64 D, E, F, G, H, A, B, C,  5, %
  278.         sha384512._.round_17_64 C, D, E, F, G, H, A, B,  6, %
  279.         sha384512._.round_17_64 B, C, D, E, F, G, H, A,  7, %
  280.         sha384512._.round_17_64 A, B, C, D, E, F, G, H,  8, %
  281.         sha384512._.round_17_64 H, A, B, C, D, E, F, G,  9, %
  282.         sha384512._.round_17_64 G, H, A, B, C, D, E, F, 10, %
  283.         sha384512._.round_17_64 F, G, H, A, B, C, D, E, 11, %
  284.         sha384512._.round_17_64 E, F, G, H, A, B, C, D, 12, %
  285.         sha384512._.round_17_64 D, E, F, G, H, A, B, C, 13, %
  286.         sha384512._.round_17_64 C, D, E, F, G, H, A, B, 14, %
  287.         sha384512._.round_17_64 B, C, D, E, F, G, H, A, 15, %
  288. end repeat
  289.  
  290.  
  291.         mov     edi, [_hash]
  292.         movq    mm0, [A]
  293.         paddq   mm0, [edi + 0x00]
  294.         movq    [edi + 0x00], mm0
  295.         movq    mm0, [B]
  296.         paddq   mm0, [edi + 0x08]
  297.         movq    [edi + 0x08], mm0
  298.         movq    mm0, [C]
  299.         paddq   mm0, [edi + 0x10]
  300.         movq    [edi + 0x10], mm0
  301.         movq    mm0, [D]
  302.         paddq   mm0, [edi + 0x18]
  303.         movq    [edi + 0x18], mm0
  304.         movq    mm0, [E]
  305.         paddq   mm0, [edi + 0x20]
  306.         movq    [edi + 0x20], mm0
  307.         movq    mm0, [F]
  308.         paddq   mm0, [edi + 0x28]
  309.         movq    [edi + 0x28], mm0
  310.         movq    mm0, [G]
  311.         paddq   mm0, [edi + 0x30]
  312.         movq    [edi + 0x30], mm0
  313.         movq    mm0, [H]
  314.         paddq   mm0, [edi + 0x38]
  315.         movq    [edi + 0x38], mm0
  316.  
  317.         ret
  318. restore w,A,B,C,D,E,F,G,H,temp
  319. endp
  320.  
  321. sha512.update = sha384.update
  322. proc sha384.update _ctx, _msg, _size
  323.         mov     ebx, [_ctx]
  324.         mov     ecx, [_size]
  325.         add     [ebx + ctx_sha384512.msglen_0], ecx
  326.         adc     [ebx + ctx_sha384512.msglen_1], 0
  327.         adc     [ebx + ctx_sha384512.msglen_2], 0
  328.         adc     [ebx + ctx_sha384512.msglen_3], 0
  329.  
  330.   .next_block:
  331.         mov     ebx, [_ctx]
  332.         mov     esi, [_msg]
  333.         mov     eax, [ebx + ctx_sha384512.index]
  334.         and     eax, SHA384512_BLOCK_SIZE-1
  335.         jnz     .copy_to_buf
  336.         test    esi, SHA384512_ALIGN_MASK
  337.         jnz     .copy_to_buf
  338.   .no_copy:
  339.         ; data is aligned, hash it in place without copying
  340.         mov     ebx, [_ctx]
  341.         cmp     [_size], SHA384512_BLOCK_SIZE
  342.         jb      .copy_quit
  343.         lea     eax, [ebx + ctx_sha384512.hash]
  344.         stdcall sha384512._.block, eax
  345.         sub     [_size], SHA384512_BLOCK_SIZE
  346.         add     esi, SHA384512_BLOCK_SIZE           ; FIXME
  347.         jmp     .no_copy
  348.  
  349.   .copy_to_buf:
  350.         lea     edi, [ebx + ctx_sha384512.block]
  351.         add     edi, eax
  352.         mov     ecx, SHA384512_BLOCK_SIZE
  353.         sub     ecx, eax
  354.         cmp     [_size], ecx
  355.         jb      .copy_quit
  356.         sub     [_size], ecx
  357.         add     [_msg], ecx
  358.         add     [ebx + ctx_sha384512.index], ecx
  359.         rep     movsb
  360.         lea     eax, [ebx + ctx_sha384512.hash]
  361.         lea     esi, [ebx + ctx_sha384512.block]
  362.         stdcall sha384512._.block, eax
  363.         jmp     .next_block
  364.  
  365.   .copy_quit:
  366.         mov     ebx, [_ctx]
  367.         lea     edi, [ebx + ctx_sha384512.block]
  368.         mov     eax, [ebx + ctx_sha384512.index]
  369.         and     eax, SHA384512_BLOCK_SIZE-1
  370.         add     edi, eax
  371.         mov     ecx, [_size]
  372.         add     [ebx + ctx_sha384512.index], ecx
  373.         rep     movsb
  374.   .quit:
  375.  
  376.         ret
  377. endp
  378.  
  379.  
  380. sha512.final = sha384.final
  381. proc sha384.final _ctx
  382.         mov     ebx, [_ctx]
  383.         lea     edi, [ebx + ctx_sha384512.block]
  384.         mov     ecx, [ebx + ctx_sha384512.msglen_0]
  385.         and     ecx, SHA384512_BLOCK_SIZE-1
  386.         add     edi, ecx
  387.         mov     byte[edi], 0x80
  388.         inc     edi
  389.         neg     ecx
  390.         add     ecx, SHA384512_BLOCK_SIZE
  391.         cmp     ecx, 16
  392.         ja      .last
  393.  
  394.         dec     ecx
  395.         xor     eax, eax
  396.         rep     stosb
  397.         lea     esi, [ebx + ctx_sha384512.block]
  398.         lea     eax, [ebx + ctx_sha384512.hash]
  399.         stdcall sha384512._.block, eax
  400.         mov     ebx, [_ctx]
  401.         lea     edi, [ebx + ctx_sha384512.block]
  402.         mov     ecx, SHA384512_BLOCK_SIZE+1
  403.   .last:
  404.         dec     ecx
  405.         sub     ecx, 16
  406.         xor     eax, eax
  407.         rep     stosb
  408.         mov     eax, [ebx + ctx_sha384512.msglen_1]
  409.         shld    [ebx + ctx_sha384512.msglen_0], eax, 3
  410.         mov     eax, [ebx + ctx_sha384512.msglen_2]
  411.         shld    [ebx + ctx_sha384512.msglen_1], eax, 3
  412.         mov     eax, [ebx + ctx_sha384512.msglen_3]
  413.         shld    [ebx + ctx_sha384512.msglen_2], eax, 3
  414.         shl     eax, 3
  415.         bswap   eax
  416.         mov     dword[edi + 0], eax
  417.         mov     eax, [ebx + ctx_sha384512.msglen_2]
  418.         bswap   eax
  419.         mov     dword[edi + 4], eax
  420.         mov     eax, [ebx + ctx_sha384512.msglen_1]
  421.         bswap   eax
  422.         mov     dword[edi + 8], eax
  423.         mov     eax, [ebx + ctx_sha384512.msglen_0]
  424.         bswap   eax
  425.         mov     dword[edi + 12], eax
  426.         mov     ebx, [_ctx]
  427.         lea     esi, [ebx + ctx_sha384512.block]
  428.         lea     eax, [ebx + ctx_sha384512.hash]
  429.         stdcall sha384512._.block, eax
  430.  
  431.         mov     ebx, [_ctx]
  432.         lea     eax, [ebx + ctx_sha384512.hash]
  433.         stdcall sha384512._.postprocess, ebx, eax
  434.  
  435.         ret
  436. endp
  437.  
  438.  
  439. proc sha384512._.postprocess _ctx, _hash
  440.         mov     ecx, 8
  441.         mov     esi, [_hash]
  442.         mov     edi, esi
  443.     @@:
  444.         lodsd
  445.         mov     ebx, eax
  446.         lodsd
  447.         bswap   eax
  448.         bswap   ebx
  449.         stosd
  450.         mov     eax, ebx
  451.         stosd
  452.         dec     ecx     ; FIXME: what should I fix here?
  453.         jnz     @b
  454.         emms
  455.         ret
  456. endp
  457.  
  458.  
  459. align SHA384512_ALIGN
  460.  
  461. sha384._.hash_init      dq 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,\
  462.                            0x9159015a3070dd17, 0x152fecd8f70e5939,\
  463.                            0x67332667ffc00b31, 0x8eb44a8768581511,\
  464.                            0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4
  465.  
  466. sha512._.hash_init      dq 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,\
  467.                            0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,\
  468.                            0x510e527fade682d1, 0x9b05688c2b3e6c1f,\
  469.                            0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
  470.  
  471. sha384512._.table       dq 0x428a2f98d728ae22, 0x7137449123ef65cd,\
  472.                            0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,\
  473.                            0x3956c25bf348b538, 0x59f111f1b605d019,\
  474.                            0x923f82a4af194f9b, 0xab1c5ed5da6d8118,\
  475.                            0xd807aa98a3030242, 0x12835b0145706fbe,\
  476.                            0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,\
  477.                            0x72be5d74f27b896f, 0x80deb1fe3b1696b1,\
  478.                            0x9bdc06a725c71235, 0xc19bf174cf692694,\
  479.                            0xe49b69c19ef14ad2, 0xefbe4786384f25e3,\
  480.                            0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,\
  481.                            0x2de92c6f592b0275, 0x4a7484aa6ea6e483,\
  482.                            0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,\
  483.                            0x983e5152ee66dfab, 0xa831c66d2db43210,\
  484.                            0xb00327c898fb213f, 0xbf597fc7beef0ee4,\
  485.                            0xc6e00bf33da88fc2, 0xd5a79147930aa725,\
  486.                            0x06ca6351e003826f, 0x142929670a0e6e70,\
  487.                            0x27b70a8546d22ffc, 0x2e1b21385c26c926,\
  488.                            0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,\
  489.                            0x650a73548baf63de, 0x766a0abb3c77b2a8,\
  490.                            0x81c2c92e47edaee6, 0x92722c851482353b,\
  491.                            0xa2bfe8a14cf10364, 0xa81a664bbc423001,\
  492.                            0xc24b8b70d0f89791, 0xc76c51a30654be30,\
  493.                            0xd192e819d6ef5218, 0xd69906245565a910,\
  494.                            0xf40e35855771202a, 0x106aa07032bbd1b8,\
  495.                            0x19a4c116b8d2d0c8, 0x1e376c085141ab53,\
  496.                            0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,\
  497.                            0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,\
  498.                            0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,\
  499.                            0x748f82ee5defb2fc, 0x78a5636f43172f60,\
  500.                            0x84c87814a1f0ab72, 0x8cc702081a6439ec,\
  501.                            0x90befffa23631e28, 0xa4506cebde82bde9,\
  502.                            0xbef9a3f7b2c67915, 0xc67178f2e372532b,\
  503.                            0xca273eceea26619c, 0xd186b8c721c0c207,\
  504.                            0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\
  505.                            0x06f067aa72176fba, 0x0a637dc5a2c898a6,\
  506.                            0x113f9804bef90dae, 0x1b710b35131c471b,\
  507.                            0x28db77f523047d84, 0x32caab7b40c72493,\
  508.                            0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\
  509.                            0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,\
  510.                            0x5fcb6fab3ad6faec, 0x6c44198c4a475817
  511.  
  512.