Subversion Repositories Kolibri OS

Rev

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.         counter rb 16
  20.         output  rb 16   ; 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, 16/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_128bit_hex, [_in]
  46.         DEBUGF  1,'\n'
  47.  
  48.         mov     esi, [_ctx]
  49.         lea     eax, [esi + aes256_ctr_context.key]
  50.         lea     ebx, [esi + aes256_ctr_context.counter]
  51.         lea     ecx, [esi + aes256_ctr_context.output]
  52.  
  53.         stdcall aes256_encrypt, eax, ebx, ecx   ; Key, in, out
  54.  
  55.         mov     ebx, [_ctx]
  56.         mov     esi, [_in]
  57.         mov     edi, [_out]
  58.  
  59.         mov     eax, [esi + 4*0]
  60.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*0]
  61.         mov     [edi + 4*0], eax
  62.  
  63.         mov     eax, [esi + 4*1]
  64.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*1]
  65.         mov     [edi + 4*1], eax
  66.  
  67.         mov     eax, [esi + 4*2]
  68.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*2]
  69.         mov     [edi + 4*2], eax
  70.  
  71.         mov     eax, [esi + 4*3]
  72.         xor     eax, dword[ebx + aes256_ctr_context.output + 4*3]
  73.         mov     [edi + 4*3], eax
  74.  
  75. ; Increment counter
  76.         mov     esi, [_ctx]
  77.  
  78.         mov     eax, dword[esi + aes256_ctr_context.counter + 4*0]
  79.         mov     ebx, dword[esi + aes256_ctr_context.counter + 4*1]
  80.         mov     ecx, dword[esi + aes256_ctr_context.counter + 4*2]
  81.         mov     edx, dword[esi + aes256_ctr_context.counter + 4*3]
  82.  
  83.         bswap   eax
  84.         bswap   ebx
  85.         bswap   ecx
  86.         bswap   edx
  87.  
  88.         inc     edx
  89.         adc     ecx, 0
  90.         adc     ebx, 0
  91.         adc     eax, 0
  92.  
  93.         bswap   eax
  94.         bswap   ebx
  95.         bswap   ecx
  96.         bswap   edx
  97.  
  98.         mov     dword[esi + aes256_ctr_context.counter + 4*0], eax
  99.         mov     dword[esi + aes256_ctr_context.counter + 4*1], ebx
  100.         mov     dword[esi + aes256_ctr_context.counter + 4*2], ecx
  101.         mov     dword[esi + aes256_ctr_context.counter + 4*3], edx
  102.  
  103.         DEBUGF  1,'cipher : '
  104.         stdcall dump_128bit_hex, [_out]
  105.         DEBUGF  1,'\n\n'
  106.  
  107.         pop     edi esi ebx
  108.         ret
  109. endp