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_ctr
  21.         aes ctx_aes
  22.         ctr ctx_ctr
  23. ends
  24.  
  25. assert sizeof.ctx_aes_ctr <= LIBCRASH_CTX_LEN
  26.  
  27. ; _crypt: 0/1 = encrypt/decrypt
  28. proc aes256ctr.init uses ebx, _ctx, _key, _iv, _flags
  29.         mov     ebx, [_ctx]
  30.         stdcall aes256.init, ebx, [_key], LIBCRASH_CIPHER_ENCRYPT
  31.         add     ebx, ctx_aes_ctr.ctr
  32.         stdcall ctr.init, [_iv]
  33.         ret
  34. endp
  35.  
  36. proc a22es_ctr._.block_init _ctx
  37.         mov     edi, [_ctx]
  38.         lea     esi, [edi+ctx_aes_ctr.ctr.block_counter]
  39.         mov     ecx, AES_BLOCK_SIZE/4
  40.         rep movsd
  41.         ret
  42. endp
  43.  
  44. proc aes256ctr.update uses ebx esi edi, _ctx, _in, _len, _out
  45.         mov     eax, [_len]
  46.         pushad
  47.         mov     ebx, [_ctx]
  48.         mov     edi, [_in]
  49.         mov     edx, [ebx+ctx_aes_ctr.ctr.partial_cnt]
  50. .next_chunk:
  51.         mov     ecx, [_len]
  52.         test    ecx, ecx
  53.         jz      .done
  54.         test    edx, edx
  55.         jnz     @f
  56.         pushad
  57.         lea     ecx, [ebx+ctx_aes_ctr.ctr.block_counter]
  58.         lea     edx, [ebx+ctx_aes_ctr.aes.state]
  59.         stdcall aes.encrypt, ebx, ecx, edx
  60.         popad
  61.         mov     edx, AES_BLOCK_SIZE
  62.  
  63.         pushad
  64.         mov     esi, ebx
  65.         mov     eax, dword[esi+ctx_aes_ctr.ctr.block_counter+4*0]
  66.         mov     ebx, dword[esi+ctx_aes_ctr.ctr.block_counter+4*1]
  67.         mov     ecx, dword[esi+ctx_aes_ctr.ctr.block_counter+4*2]
  68.         mov     edx, dword[esi+ctx_aes_ctr.ctr.block_counter+4*3]
  69.  
  70.         bswap   eax
  71.         bswap   ebx
  72.         bswap   ecx
  73.         bswap   edx
  74.  
  75.         add     edx, 1
  76.         adc     ecx, 0
  77.         adc     ebx, 0
  78.         adc     eax, 0
  79.  
  80.         bswap   eax
  81.         bswap   ebx
  82.         bswap   ecx
  83.         bswap   edx
  84.  
  85.         mov     dword[esi+ctx_aes_ctr.ctr.block_counter+4*0], eax
  86.         mov     dword[esi+ctx_aes_ctr.ctr.block_counter+4*1], ebx
  87.         mov     dword[esi+ctx_aes_ctr.ctr.block_counter+4*2], ecx
  88.         mov     dword[esi+ctx_aes_ctr.ctr.block_counter+4*3], edx
  89.         popad
  90.  
  91. @@:
  92.         cmp     ecx, edx
  93.         jbe     @f
  94.         mov     ecx, edx
  95. @@:
  96.         lea     esi, [ebx+ctx_aes_ctr.aes.state]
  97.         add     esi, AES_BLOCK_SIZE
  98.         sub     esi, edx
  99.         sub     [_len], ecx
  100.         sub     edx, ecx
  101.         push    ebx
  102.         mov     edi, [_out]
  103.         mov     ebx, [_in]
  104.         add     [_in], ecx
  105.         add     [_out], ecx
  106. @@:
  107.         lodsb
  108.         xor     al, [ebx]
  109.         inc     ebx
  110.         stosb
  111.         loop    @b
  112.         pop     ebx
  113.         jmp     .next_chunk
  114. .done:
  115.         mov     [ebx+ctx_aes_ctr.ctr.partial_cnt], edx
  116.         popad
  117.         ret
  118. endp
  119.  
  120. proc aes256ctr.finish _ctx, _out
  121.         xor     eax, eax
  122.         ret
  123. endp
  124.  
  125. proc aes256ctr.oneshot _ctx, _key, _iv, _flags, _in, _len, _out
  126. locals
  127.         .done dd ?
  128. endl
  129.         mov     [.done], 0
  130.         stdcall aes256ctr.init, [_ctx], [_key], [_iv], [_flags]
  131.         stdcall aes256ctr.update, [_ctx], [_in], [_len], [_out]
  132.         add     [_out], eax
  133.         add     [.done], eax
  134.         stdcall aes256ctr.finish, [_ctx], [_out]
  135.         add     eax, [.done]
  136.         ret
  137. endp
  138.