Subversion Repositories Kolibri OS

Rev

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

  1. ;    aes256-cbc.inc - AES256 Cipher Block Chaining
  2. ;
  3. ;    Copyright (C) 2016 Jeffrey Amelynck
  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_cbc_context aes256_context
  19.         vector  rb 16
  20. ends
  21.  
  22.  
  23. proc aes256_cbc_init _vector
  24.         push    ebx esi edi
  25.  
  26.         mcall   68, 12, sizeof.aes256_cbc_context
  27.         ; handle errors
  28.         mov     ecx, 16/4
  29.         mov     esi, [_vector]
  30.         lea     edi, [eax + aes256_cbc_context.vector]
  31.         rep     movsd
  32.         ; rep movsd is slow, but we don't care while init
  33.  
  34.         pop     edi esi ebx
  35.         ret
  36. endp
  37.  
  38. proc aes256_cbc_encrypt _ctx, _in, _out
  39.         push    ebx esi edi
  40.  
  41.         DEBUGF  1,'plain  : '
  42.         stdcall dump_128bit_hex, [_in]
  43.         DEBUGF  1,'\n'
  44.  
  45.         mov     edi, [_ctx]
  46.         lea     edi, [edi + aes256_cbc_context.vector]
  47.         mov     esi, [_in]
  48.         lodsd
  49.         xor     eax, [edi]
  50.         stosd
  51.         lodsd
  52.         xor     eax, [edi]
  53.         stosd
  54.         lodsd
  55.         xor     eax, [edi]
  56.         stosd
  57.         lodsd
  58.         xor     eax, [edi]
  59.         stosd
  60.  
  61.         mov     esi, [_ctx]
  62.         lea     eax, [esi + aes256_cbc_context.key]
  63.         lea     ebx, [esi + aes256_cbc_context.vector]
  64.         stdcall aes256_encrypt, eax, ebx, [_out]   ; Key, in, out
  65.  
  66.         mov     esi, [_out]
  67.         mov     eax, [_ctx]
  68.         lea     edi, [eax + aes256_cbc_context.vector]
  69.         movsd
  70.         movsd
  71.         movsd
  72.         movsd
  73.  
  74.         DEBUGF  1,'cipher : '
  75.         stdcall dump_128bit_hex, [_out]
  76.         DEBUGF  1,'\n\n'
  77.  
  78.         pop     edi esi ebx
  79.         ret
  80. endp
  81.  
  82. proc aes256_cbc_decrypt _ctx, _in, _out
  83.         push    ebx esi edi
  84.  
  85.         DEBUGF  1,'cipher : '
  86.         stdcall dump_128bit_hex, [_in]
  87.         DEBUGF  1,'\n'
  88.  
  89.         mov     esi, [_ctx]
  90.         lea     eax, [esi + aes256_cbc_context.key]
  91.         stdcall aes256_decrypt, eax, [_in], [_out]   ; Key, in, out
  92.  
  93.         mov     esi, [_ctx]
  94.         lea     esi, [esi + aes256_cbc_context.vector]
  95.         mov     edi, [_out]
  96.         lodsd
  97.         xor     eax, [edi]
  98.         stosd
  99.         lodsd
  100.         xor     eax, [edi]
  101.         stosd
  102.         lodsd
  103.         xor     eax, [edi]
  104.         stosd
  105.         lodsd
  106.         xor     eax, [edi]
  107.         stosd
  108.  
  109.         mov     esi, [_in]
  110.         mov     edi, [_ctx]
  111.         lea     edi, [edi + aes256_cbc_context.vector]
  112.         movsd
  113.         movsd
  114.         movsd
  115.         movsd
  116.  
  117.         DEBUGF  1,'plain  : '
  118.         stdcall dump_128bit_hex, [_out]
  119.         DEBUGF  1,'\n\n'
  120.  
  121.         pop     edi esi ebx
  122.         ret
  123. endp