Subversion Repositories Kolibri OS

Rev

Rev 6469 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;    aes256-ctr.inc - AES256 Counter Mode
  2. ;
  3. ;    Copyright (C) 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. struct aes256_ctr_context aes256_context
  19.  
  20.         counter rb AES256_BLOCKSIZE
  21.         output  rb AES256_BLOCKSIZE     ; counter after aes_crypt
  22.  
  23. ends
  24.  
  25.  
  26. proc aes256_ctr_init _counter
  27.  
  28.         push    ebx esi edi
  29.  
  30.         mcall   68, 12, sizeof.aes256_ctr_context
  31.         ; handle errors
  32.         mov     ecx, AES256_BLOCKSIZE/4
  33.         mov     esi, [_counter]
  34.         lea     edi, [eax + aes256_ctr_context.counter]
  35.         rep movsd
  36.         ; rep movsd is slow, but we don't care while init
  37.  
  38.         pop     edi esi ebx
  39.         ret
  40.  
  41. endp
  42.  
  43.  
  44. proc aes256_ctr_crypt _ctx, _in, _out
  45.  
  46.         push    ebx esi edi
  47.  
  48.         DEBUGF  1,'plain  : '
  49.         stdcall dump_hex, [_in], 4
  50.  
  51.         mov     esi, [_ctx]
  52.         lea     eax, [esi + aes256_ctr_context.key]
  53.         lea     ebx, [esi + aes256_ctr_context.counter]
  54.         lea     ecx, [esi + aes256_ctr_context.output]
  55.  
  56.         stdcall aes256_encrypt, eax, ebx, ecx   ; Key, in, out
  57.  
  58.         mov     ebx, [_ctx]
  59.         mov     esi, [_in]
  60.         mov     edi, [_out]
  61.  
  62.         mov     eax, [esi + 4*0]
  63.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*0]
  64.         mov     [edi + 4*0], eax
  65.  
  66.         mov     eax, [esi + 4*1]
  67.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*1]
  68.         mov     [edi + 4*1], eax
  69.  
  70.         mov     eax, [esi + 4*2]
  71.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*2]
  72.         mov     [edi + 4*2], eax
  73.  
  74.         mov     eax, [esi + 4*3]
  75.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*3]
  76.         mov     [edi + 4*3], eax
  77.  
  78. ; Increment counter
  79.         mov     esi, [_ctx]
  80.  
  81.         mov     eax, dword[esi + aes256_ctr_context.counter + 4*0]
  82.         mov     ebx, dword[esi + aes256_ctr_context.counter + 4*1]
  83.         mov     ecx, dword[esi + aes256_ctr_context.counter + 4*2]
  84.         mov     edx, dword[esi + aes256_ctr_context.counter + 4*3]
  85.  
  86.         bswap   eax
  87.         bswap   ebx
  88.         bswap   ecx
  89.         bswap   edx
  90.  
  91.         adc     edx, 1
  92.         adc     ecx, 0
  93.         adc     ebx, 0
  94.         adc     eax, 0
  95.  
  96.         bswap   eax
  97.         bswap   ebx
  98.         bswap   ecx
  99.         bswap   edx
  100.  
  101.         mov     dword[esi + aes256_ctr_context.counter + 4*0], eax
  102.         mov     dword[esi + aes256_ctr_context.counter + 4*1], ebx
  103.         mov     dword[esi + aes256_ctr_context.counter + 4*2], ecx
  104.         mov     dword[esi + aes256_ctr_context.counter + 4*3], edx
  105.  
  106.         DEBUGF  1,'cipher : '
  107.         stdcall dump_hex, [_out], 4
  108.  
  109.         pop     edi esi ebx
  110.         ret
  111. endp