Subversion Repositories Kolibri OS

Rev

Rev 7698 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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