Subversion Repositories Kolibri OS

Rev

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

  1. ;    blowfish-cbc.inc - Blowfish Cipher Block Chaining
  2. ;
  3. ;    Copyright (C) 2018 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 blowfish_cbc_context blowfish_context
  19.         vector  rb BLOWFISH_BLOCKSIZE
  20. ends
  21.  
  22.  
  23. proc blowfish_cbc_init _vector
  24.         push    ebx esi edi
  25.  
  26.         mcall   68, 12, sizeof.blowfish_cbc_context
  27.         ; handle errors
  28.         mov     ecx, BLOWFISH_BLOCKSIZE/4
  29.         mov     esi, [_vector]
  30.         lea     edi, [eax + blowfish_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 blowfish_cbc_encrypt _ctx, _in, _out
  39.         push    ebx esi edi
  40.  
  41.         DEBUGF  1,'plain  : '
  42.         stdcall dump_hex, [_in], 4
  43.  
  44.         mov     edi, [_ctx]
  45.         lea     edi, [edi + blowfish_cbc_context.vector]
  46.         mov     esi, [_in]
  47. repeat blowfish_BLOCKSIZE/4
  48.         lodsd
  49.         xor     eax, [edi]
  50.         stosd
  51. end repeat
  52.  
  53.         mov     esi, [_ctx]
  54.         lea     eax, [esi + blowfish_cbc_context.pbox]
  55.         lea     ebx, [esi + blowfish_cbc_context.vector]
  56.         stdcall blowfish_encrypt, eax, ebx, [_out]   ; Key, in, out
  57.  
  58.         mov     esi, [_out]
  59.         mov     eax, [_ctx]
  60.         lea     edi, [eax + blowfish_cbc_context.vector]
  61. repeat blowfish_BLOCKSIZE/4
  62.         movsd
  63. end repeat
  64.  
  65.         DEBUGF  1,'cipher : '
  66.         stdcall dump_hex, [_out], 4
  67.  
  68.         pop     edi esi ebx
  69.         ret
  70. endp
  71.  
  72. proc blowfish_cbc_decrypt _ctx, _in, _out
  73.  
  74. locals
  75.         temp_iv rb BLOWFISH_BLOCKSIZE
  76. endl
  77.  
  78.         push    ebx esi edi
  79.  
  80.         DEBUGF  1,'cipher : '
  81.         stdcall dump_hex, [_in], 4
  82.  
  83.         mov     esi, [_in]
  84.         lea     edi, [temp_iv]
  85. repeat BLOWFISH_BLOCKSIZE/4
  86.         movsd
  87. end repeat
  88.  
  89.         mov     esi, [_ctx]
  90.         lea     eax, [esi + blowfish_cbc_context.pbox]
  91.         stdcall blowfish_decrypt, eax, [_in], [_out]   ; Key, in, out
  92.  
  93.         mov     esi, [_ctx]
  94.         lea     esi, [esi + blowfish_cbc_context.vector]
  95.         mov     edi, [_out]
  96. repeat BLOWFISH_BLOCKSIZE/4
  97.         lodsd
  98.         xor     eax, [edi]
  99.         stosd
  100. end repeat
  101.  
  102.         lea     esi, [temp_iv]
  103.         mov     edi, [_ctx]
  104.         lea     edi, [edi + blowfish_cbc_context.vector]
  105. repeat BLOWFISH_BLOCKSIZE/4
  106.         movsd
  107. end repeat
  108.  
  109.         DEBUGF  1,'plain  : '
  110.         stdcall dump_hex, [_out], 4
  111.  
  112.         pop     edi esi ebx
  113.         ret
  114. endp