Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ; libcrash -- cryptographic hash (and other) functions
  2. ;
  3. ; Copyright (C) <2016> Jeffrey Amelynck
  4. ; Copyright (C) <2016,2021> Ivan Baravy
  5. ;
  6. ; SPDX-License-Identifier: GPL-2.0-or-later
  7. ;
  8. ; This program is free software: you can redistribute it and/or modify it under
  9. ; the terms of the GNU General Public License as published by the Free Software
  10. ; Foundation, either version 2 of the License, or (at your option) any later
  11. ; version.
  12. ;
  13. ; This program is distributed in the hope that it will be useful, but WITHOUT
  14. ; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. ; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. ;
  17. ; You should have received a copy of the GNU General Public License along with
  18. ; this program. If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. struct ctx_aes_cbc
  21.         aes ctx_aes
  22.         cbc ctx_cbc
  23.         crypt dd ?
  24.         finish dd ?
  25.         block rd CBC128_BLOCK_SIZE/4
  26.         index dd ?
  27.         padding dd ?
  28. ends
  29.  
  30. assert sizeof.ctx_aes_cbc <= LIBCRASH_CTX_LEN
  31.  
  32. ; _crypt: 0/1 = encrypt/decrypt
  33. proc aes256cbc.init uses ebx esi edi, _ctx, _key, _iv, _flags
  34.         mov     ebx, [_ctx]
  35.         stdcall aes256.init, ebx, [_key], [_flags]
  36.         mov     ecx, CBC128_BLOCK_SIZE/4
  37.         mov     esi, [_iv]
  38.         lea     edi, [ebx+ctx_aes_cbc.cbc.vector]
  39.         rep movsd
  40.         mov     [ebx+ctx_aes_cbc.cbc.has_data], 0
  41.         mov     [ebx+ctx_aes_cbc.index], 0
  42.         mov     [ebx+ctx_aes_cbc.crypt], aes256cbc._.encrypt_block
  43.         mov     [ebx+ctx_aes_cbc.finish], aes256cbc._.finish_encrypt
  44.         test    [_flags], LIBCRASH_CIPHER_DECRYPT
  45.         jz      @f
  46.         mov     [ebx+ctx_aes_cbc.crypt], aes256cbc._.decrypt_block
  47.         mov     [ebx+ctx_aes_cbc.finish], aes256cbc._.finish_decrypt
  48. @@:
  49.         xor     eax, eax
  50.         test    [_flags], LIBCRASH_CIPHER_PADDING
  51.         setnz   al
  52.         mov     [ebx+ctx_aes_cbc.padding], eax
  53.         ret
  54. endp
  55.  
  56. proc aes256cbc._.encrypt_block uses ebx esi edi, _ctx, _in, _out
  57.         mov     ebx, [_ctx]
  58.         mov     esi, [_in]
  59.         lea     edi, [ebx+ctx_aes_cbc.cbc.vector]
  60.         mov     ecx, CBC128_BLOCK_SIZE/4
  61. @@:
  62.         lodsd
  63.         xor     [edi], eax
  64.         add     edi, 4
  65.         dec     ecx
  66.         jnz     @b
  67.  
  68.         lea     ecx, [ebx+ctx_aes_cbc.cbc.vector]
  69.         lea     edx, [ebx+ctx_aes_cbc.aes.state]
  70.         stdcall aes.encrypt, ebx, ecx, edx
  71.         lea     esi, [ebx+ctx_aes_cbc.aes.state]
  72.         lea     edi, [ebx+ctx_aes_cbc.cbc.vector]
  73.         mov     ecx, CBC128_BLOCK_SIZE/4
  74.         rep movsd
  75.         lea     esi, [ebx+ctx_aes_cbc.aes.state]
  76.         mov     edi, [_out]
  77.         mov     ecx, CBC128_BLOCK_SIZE/4
  78.         rep movsd
  79.  
  80.         mov     eax, CBC128_BLOCK_SIZE
  81.         ret
  82. endp
  83.  
  84. proc aes256cbc._.decrypt_block uses ebx esi edi, _ctx, _in, _out
  85. locals
  86.         .done dd ?
  87. endl
  88.         mov     [.done], 0
  89.         mov     ebx, [_ctx]
  90.  
  91.         mov     ecx, [_in]
  92.         lea     edx, [ebx+ctx_aes_cbc.aes.state]
  93.         stdcall aes.decrypt, ebx, ecx, edx
  94.  
  95.         bts     [ebx+ctx_aes_cbc.cbc.has_data], 0
  96.         jnc     @f
  97.         lea     esi, [ebx+ctx_aes_cbc.cbc.block]
  98.         mov     edi, [_out]
  99.         mov     ecx, CBC128_BLOCK_SIZE/4
  100.         rep movsd
  101.         add     [.done], CBC128_BLOCK_SIZE
  102. @@:
  103.         lea     esi, [ebx+ctx_aes_cbc.aes.state]
  104.         lea     edx, [ebx+ctx_aes_cbc.cbc.vector]
  105.         lea     edi, [ebx+ctx_aes_cbc.cbc.block]
  106.         mov     ecx, CBC128_BLOCK_SIZE/4
  107. @@:
  108.         lodsd
  109.         xor     eax, [edx]
  110.         add     edx, 4
  111.         stosd
  112.         dec     ecx
  113.         jnz     @b
  114.  
  115.         mov     esi, [_in]
  116.         lea     edi, [ebx+ctx_aes_cbc.cbc.vector]
  117.         mov     ecx, CBC128_BLOCK_SIZE/4
  118.         rep movsd
  119.  
  120.         mov     eax, [.done]
  121.         ret
  122. endp
  123.  
  124. proc aes256cbc.update uses ebx esi edi, _ctx, _in, _len, _out
  125. locals
  126.         .done dd ?
  127. endl
  128.         mov     [.done], 0
  129. .next_block:
  130.         mov     ebx, [_ctx]
  131.         mov     eax, [ebx+ctx_aes_cbc.index]
  132.         test    eax, eax
  133.         jnz     .copy_to_buf
  134.         test    [_in], LIBCRASH_ALIGN-1
  135.         jnz     .copy_to_buf
  136. .no_copy:
  137.         ; data is aligned, process it in place without copying
  138.         mov     ebx, [_ctx]
  139.         cmp     [_len], CBC128_BLOCK_SIZE
  140.         jb      .copy_quit
  141.         stdcall [ebx+ctx_aes_cbc.crypt], [_ctx], [_in], [_out]
  142.         add     [_in], CBC128_BLOCK_SIZE
  143.         add     [_out], eax
  144.         add     [.done], eax
  145.         sub     [_len], CBC128_BLOCK_SIZE
  146.         jmp     .no_copy
  147.  
  148. .copy_to_buf:
  149.         lea     edi, [ebx+ctx_aes_cbc.block]
  150.         add     edi, [ebx+ctx_aes_cbc.index]
  151.         mov     ecx, CBC128_BLOCK_SIZE
  152.         sub     ecx, [ebx+ctx_aes_cbc.index]
  153.         cmp     [_len], ecx
  154.         jb      .copy_quit
  155.         mov     esi, [_in]
  156.         sub     [_len], ecx
  157.         add     [_in], ecx
  158.         rep movsb
  159.         mov     [ebx+ctx_aes_cbc.index], 0
  160.         lea     esi, [ebx+ctx_aes_cbc.block]
  161.         stdcall [ebx+ctx_aes_cbc.crypt], [_ctx], esi, [_out]
  162.         add     [.done], eax
  163.         add     [_out], eax
  164.         jmp     .next_block
  165.  
  166. .copy_quit:
  167.         mov     ebx, [_ctx]
  168.         mov     esi, [_in]
  169.         lea     edi, [ebx+ctx_aes_cbc.block]
  170.         add     edi, [ebx+ctx_aes_cbc.index]
  171.         mov     ecx, [_len]
  172.         add     [ebx+ctx_aes_cbc.index], ecx
  173.         rep movsb
  174. .quit:
  175.         mov     eax, [.done]
  176.         ret
  177. endp
  178.  
  179. proc aes256cbc.finish uses ebx esi edi, _ctx, _out
  180.         mov     ebx, [_ctx]
  181.         stdcall [ebx+ctx_aes_cbc.finish], ebx, [_out]
  182.         ret
  183. endp
  184.  
  185. proc aes256cbc._.finish_encrypt uses ebx esi edi, _ctx, _out
  186.         mov     ebx, [_ctx]
  187.         xor     eax, eax
  188.         cmp     [ebx+ctx_aes_cbc.padding], 0
  189.         jz      .no_padding
  190.         ; add padding
  191.         lea     edi, [ebx+ctx_aes_cbc.block]
  192.         add     edi, [ebx+ctx_aes_cbc.index]
  193.         mov     ecx, CBC128_BLOCK_SIZE
  194.         sub     ecx, [ebx+ctx_aes_cbc.index]
  195.         mov     eax, ecx
  196.         rep stosb
  197.  
  198.         lea     eax, [ebx+ctx_aes_cbc.block]
  199.         stdcall aes256cbc._.encrypt_block, [_ctx], eax, [_out]
  200.         mov     eax, CBC128_BLOCK_SIZE
  201. .no_padding:
  202.         ret
  203. endp
  204.  
  205. proc aes256cbc._.finish_decrypt uses ebx esi edi, _ctx, _out
  206.         mov     ebx, [_ctx]
  207.         xor     eax, eax
  208.         cmp     eax, [ebx+ctx_aes_cbc.cbc.has_data]
  209.         jz      .done
  210.         lea     esi, [ebx+ctx_aes_cbc.cbc.block]
  211.         mov     edi, [_out]
  212.         mov     ecx, CBC128_BLOCK_SIZE
  213.         cmp     [ebx+ctx_aes_cbc.padding], eax
  214.         jz      @f
  215.         sub     cl, [esi+CBC128_BLOCK_SIZE-1]
  216. @@:
  217.         mov     eax, ecx
  218.         rep movsb
  219. .done:
  220.         ret
  221. endp
  222.  
  223. proc aes256cbc.oneshot _ctx, _key, _iv, _flags, _in, _len, _out
  224. locals
  225.         .done dd ?
  226. endl
  227.         mov     [.done], 0
  228.         stdcall aes256cbc.init, [_ctx], [_key], [_iv], [_flags]
  229.         stdcall aes256cbc.update, [_ctx], [_in], [_len], [_out]
  230.         add     [_out], eax
  231.         add     [.done], eax
  232.         stdcall aes256cbc.finish, [_ctx], [_out]
  233.         add     eax, [.done]
  234.         ret
  235. endp
  236.