Subversion Repositories Kolibri OS

Rev

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